54
54
# See the License for the specific language governing permissions and
55
55
# limitations under the License.
56
56
57
- import itertools
58
57
import sys
59
- import typing
60
58
61
59
from abc import ABCMeta , abstractmethod
62
60
from typing import Iterator , List , Tuple , TypeVar
68
66
T = TypeVar ('T' ) #pylint: disable=invalid-name
69
67
70
68
def hex_to_int (val : str ) -> int :
71
- return int (val , 16 ) if val else 0
69
+ """Implement the syntax accepted by mbedtls_test_read_mpi().
70
+
71
+ This is a superset of what is accepted by mbedtls_test_read_mpi_core().
72
+ """
73
+ if val in ['' , '-' ]:
74
+ return 0
75
+ return int (val , 16 )
72
76
73
77
def quote_str (val ) -> str :
74
78
return "\" {}\" " .format (val )
75
79
76
80
def combination_pairs (values : List [T ]) -> List [Tuple [T , T ]]:
77
81
"""Return all pair combinations from input values."""
78
- # The return value is cast, as older versions of mypy are unable to derive
79
- # the specific type returned by itertools.combinations_with_replacement.
80
- return typing .cast (
81
- List [Tuple [T , T ]],
82
- list (itertools .combinations_with_replacement (values , 2 ))
83
- )
84
-
82
+ return [(x , y ) for x in values for y in values ]
85
83
86
84
class BignumTarget (test_data_generation .BaseTarget , metaclass = ABCMeta ):
87
85
#pylint: disable=abstract-method
@@ -105,7 +103,8 @@ class BignumOperation(BignumTarget, metaclass=ABCMeta):
105
103
"""
106
104
symbol = ""
107
105
input_values = [
108
- "" , "0" , "7b" , "-7b" ,
106
+ "" , "0" , "-" , "-0" ,
107
+ "7b" , "-7b" ,
109
108
"0000000000000000123" , "-0000000000000000123" ,
110
109
"1230000000000000000" , "-1230000000000000000"
111
110
] # type: List[str]
@@ -120,6 +119,11 @@ def __init__(self, val_a: str, val_b: str) -> None:
120
119
def arguments (self ) -> List [str ]:
121
120
return [quote_str (self .arg_a ), quote_str (self .arg_b ), self .result ()]
122
121
122
+ def description_suffix (self ) -> str :
123
+ #pylint: disable=no-self-use # derived classes need self
124
+ """Text to add at the end of the test case description."""
125
+ return ""
126
+
123
127
def description (self ) -> str :
124
128
"""Generate a description for the test case.
125
129
@@ -133,6 +137,9 @@ def description(self) -> str:
133
137
self .symbol ,
134
138
self .value_description (self .arg_b )
135
139
)
140
+ description_suffix = self .description_suffix ()
141
+ if description_suffix :
142
+ self .case_description += " " + description_suffix
136
143
return super ().description ()
137
144
138
145
@abstractmethod
@@ -153,6 +160,8 @@ def value_description(val) -> str:
153
160
"""
154
161
if val == "" :
155
162
return "0 (null)"
163
+ if val == "-" :
164
+ return "negative 0 (null)"
156
165
if val == "0" :
157
166
return "0 (1 limb)"
158
167
@@ -228,8 +237,21 @@ class BignumAdd(BignumOperation):
228
237
]
229
238
)
230
239
240
+ def __init__ (self , val_a : str , val_b : str ) -> None :
241
+ super ().__init__ (val_a , val_b )
242
+ self ._result = self .int_a + self .int_b
243
+
244
+ def description_suffix (self ) -> str :
245
+ if (self .int_a >= 0 and self .int_b >= 0 ):
246
+ return "" # obviously positive result or 0
247
+ if (self .int_a <= 0 and self .int_b <= 0 ):
248
+ return "" # obviously negative result or 0
249
+ # The sign of the result is not obvious, so indicate it
250
+ return ", result{}0" .format ('>' if self ._result > 0 else
251
+ '<' if self ._result < 0 else '=' )
252
+
231
253
def result (self ) -> str :
232
- return quote_str ("{:x}" .format (self .int_a + self . int_b ))
254
+ return quote_str ("{:x}" .format (self ._result ))
233
255
234
256
if __name__ == '__main__' :
235
257
# Use the section of the docstring relevant to the CLI as description
0 commit comments