Remove abbreviations and clarify attributes

Signed-off-by: Werner Lewis <werner.lewis@arm.com>
This commit is contained in:
Werner Lewis 2022-08-23 14:21:53 +01:00
parent fbb75e3fc5
commit 55e638ca57
2 changed files with 44 additions and 49 deletions

View File

@ -35,37 +35,37 @@ class BaseTarget:
"""Base target for test case generation. """Base target for test case generation.
Attributes: Attributes:
count: Counter for test class. count: Counter for test cases from this class.
desc: Short description of test case. case_description: Short description of the test case. This may be
func: Function which the class generates tests for. automatically generated using the class, or manually set.
gen_file: File to write generated tests to. target_basename: Basename of file to write generated tests to. This
title: Description of the test function/purpose. should be specified in a child class of BaseTarget.
test_function: Test function which the class generates cases for.
test_name: A common name or description of the test function. This can
be the function's name, or a short summary of its purpose.
""" """
count = 0 count = 0
desc = "" case_description = ""
func = "" target_basename = ""
gen_file = "" test_function = ""
title = "" test_name = ""
def __init__(self) -> None: def __init__(self) -> None:
type(self).count += 1 type(self).count += 1
@property def arguments(self) -> List[str]:
def args(self) -> List[str]:
"""Create list of arguments for test case."""
return [] return []
@property
def description(self) -> str: def description(self) -> str:
"""Create a numbered test description.""" """Create a numbered test description."""
return "{} #{} {}".format(self.title, self.count, self.desc) return "{} #{} {}".format(self.test_name, self.count, self.case_description)
def create_test_case(self) -> test_case.TestCase: def create_test_case(self) -> test_case.TestCase:
"""Generate test case from the current object.""" """Generate test case from the current object."""
tc = test_case.TestCase() tc = test_case.TestCase()
tc.set_description(self.description) tc.set_description(self.description())
tc.set_function(self.func) tc.set_function(self.test_function)
tc.set_arguments(self.args) tc.set_arguments(self.arguments())
return tc return tc

View File

@ -39,20 +39,20 @@ def quote_str(val):
class BignumTarget(test_generation.BaseTarget): class BignumTarget(test_generation.BaseTarget):
"""Target for bignum (mpi) test case generation.""" """Target for bignum (mpi) test case generation."""
gen_file = 'test_suite_mpi.generated' target_basename = 'test_suite_mpi.generated'
class BignumOperation(BignumTarget): class BignumOperation(BignumTarget):
"""Common features for test cases covering bignum operations. """Common features for test cases covering bignum operations.
Attributes: Attributes:
symb: Symbol used for operation in description. symbol: Symbol used for operation in description.
input_vals: List of values used to generate test case args. input_values: List of values to use as test case inputs.
input_cases: List of tuples containing test case inputs. This input_cases: List of tuples containing pairs of test case inputs. This
can be used to implement specific pairs of inputs. can be used to implement specific pairs of inputs.
""" """
symb = "" symbol = ""
input_vals = [ input_values = [
"", "0", "7b", "-7b", "", "0", "7b", "-7b",
"0000000000000000123", "-0000000000000000123", "0000000000000000123", "-0000000000000000123",
"1230000000000000000", "-1230000000000000000" "1230000000000000000", "-1230000000000000000"
@ -67,26 +67,23 @@ class BignumOperation(BignumTarget):
self.int_l = hex_to_int(val_l) self.int_l = hex_to_int(val_l)
self.int_r = hex_to_int(val_r) self.int_r = hex_to_int(val_r)
@property def arguments(self):
def args(self): return [quote_str(self.arg_l), quote_str(self.arg_r), self.result()]
return [quote_str(self.arg_l), quote_str(self.arg_r), self.result]
@property
def description(self): def description(self):
desc = self.desc if self.desc else "{} {} {}".format( if not self.case_description:
self.val_desc(self.arg_l), self.case_description = "{} {} {}".format(
self.symb, self.value_description(self.arg_l),
self.val_desc(self.arg_r) self.symbol,
) self.value_description(self.arg_r)
return "{} #{} {}".format(self.title, self.count, desc) )
return super().description()
@property
def result(self) -> Optional[str]: def result(self) -> Optional[str]:
return None return None
@staticmethod @staticmethod
def val_desc(val) -> str: def value_description(val) -> str:
"""Generate description of the argument val."""
if val == "": if val == "":
return "0 (null)" return "0 (null)"
if val == "0": if val == "0":
@ -107,13 +104,13 @@ class BignumOperation(BignumTarget):
def get_value_pairs(cls) -> Iterator[Tuple[str, ...]]: def get_value_pairs(cls) -> Iterator[Tuple[str, ...]]:
"""Generate value pairs.""" """Generate value pairs."""
for pair in list( for pair in list(
itertools.combinations(cls.input_vals, 2) itertools.combinations(cls.input_values, 2)
) + cls.input_cases: ) + cls.input_cases:
yield pair yield pair
@classmethod @classmethod
def generate_tests(cls) -> Iterator[test_case.TestCase]: def generate_tests(cls) -> Iterator[test_case.TestCase]:
if cls.func: if cls.test_function:
# Generate tests for the current class # Generate tests for the current class
for l_value, r_value in cls.get_value_pairs(): for l_value, r_value in cls.get_value_pairs():
cur_op = cls(l_value, r_value) cur_op = cls(l_value, r_value)
@ -125,8 +122,8 @@ class BignumOperation(BignumTarget):
class BignumCmp(BignumOperation): class BignumCmp(BignumOperation):
"""Target for bignum comparison test cases.""" """Target for bignum comparison test cases."""
count = 0 count = 0
func = "mbedtls_mpi_cmp_mpi" test_function = "mbedtls_mpi_cmp_mpi"
title = "MPI compare" test_name = "MPI compare"
input_cases = [ input_cases = [
("-2", "-3"), ("-2", "-3"),
("-2", "-2"), ("-2", "-2"),
@ -137,9 +134,8 @@ class BignumCmp(BignumOperation):
def __init__(self, val_l, val_r): def __init__(self, val_l, val_r):
super().__init__(val_l, val_r) super().__init__(val_l, val_r)
self._result = (self.int_l > self.int_r) - (self.int_l < self.int_r) self._result = (self.int_l > self.int_r) - (self.int_l < self.int_r)
self.symb = ["<", "==", ">"][self._result + 1] self.symbol = ["<", "==", ">"][self._result + 1]
@property
def result(self): def result(self):
return str(self._result) return str(self._result)
@ -147,8 +143,8 @@ class BignumCmp(BignumOperation):
class BignumCmpAbs(BignumCmp): class BignumCmpAbs(BignumCmp):
"""Target for abs comparison variant.""" """Target for abs comparison variant."""
count = 0 count = 0
func = "mbedtls_mpi_cmp_abs" test_function = "mbedtls_mpi_cmp_abs"
title = "MPI compare (abs)" test_name = "MPI compare (abs)"
def __init__(self, val_l, val_r): def __init__(self, val_l, val_r):
super().__init__(val_l.strip("-"), val_r.strip("-")) super().__init__(val_l.strip("-"), val_r.strip("-"))
@ -157,8 +153,8 @@ class BignumCmpAbs(BignumCmp):
class BignumAdd(BignumOperation): class BignumAdd(BignumOperation):
"""Target for bignum addition test cases.""" """Target for bignum addition test cases."""
count = 0 count = 0
func = "mbedtls_mpi_add_mpi" test_function = "mbedtls_mpi_add_mpi"
title = "MPI add" test_name = "MPI add"
input_cases = list(itertools.combinations( input_cases = list(itertools.combinations(
[ [
"1c67967269c6", "9cde3", "1c67967269c6", "9cde3",
@ -168,9 +164,8 @@ class BignumAdd(BignumOperation):
def __init__(self, val_l, val_r): def __init__(self, val_l, val_r):
super().__init__(val_l, val_r) super().__init__(val_l, val_r)
self.symb = "+" self.symbol = "+"
@property
def result(self): def result(self):
return quote_str(hex(self.int_l + self.int_r).replace("0x", "", 1)) return quote_str(hex(self.int_l + self.int_r).replace("0x", "", 1))
@ -178,7 +173,7 @@ class BignumAdd(BignumOperation):
class BignumTestGenerator(test_generation.TestGenerator): class BignumTestGenerator(test_generation.TestGenerator):
"""Test generator subclass including bignum targets.""" """Test generator subclass including bignum targets."""
TARGETS = { TARGETS = {
subclass.gen_file: subclass.generate_tests for subclass in subclass.target_basename: subclass.generate_tests for subclass in
test_generation.BaseTarget.__subclasses__() test_generation.BaseTarget.__subclasses__()
} # type: Dict[str, Callable[[], test_case.TestCase]] } # type: Dict[str, Callable[[], test_case.TestCase]]