Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: GPL-2.0-only |
d2d857a8 MJ |
2 | # |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
d2d857a8 | 5 | |
5995b304 SM |
6 | import copy |
7 | import math | |
9cf643d1 PP |
8 | import operator |
9 | import unittest | |
b0bdda42 | 10 | import itertools |
73051d46 | 11 | import collections |
5995b304 | 12 | from functools import partial, partialmethod |
9cf643d1 | 13 | |
5995b304 SM |
14 | import bt2 |
15 | from utils import create_const_field, get_default_trace_class | |
9cf643d1 | 16 | |
cfbd7cf3 | 17 | _COMP_BINOPS = (operator.eq, operator.ne) |
9cf643d1 PP |
18 | |
19 | ||
1eccc498 SM |
20 | # Create and return a stream with the field classes part of its stream packet |
21 | # context. | |
22 | # | |
23 | # The stream is part of a dummy trace created from trace class `tc`. | |
24 | ||
cfbd7cf3 | 25 | |
1eccc498 SM |
26 | def _create_stream(tc, ctx_field_classes): |
27 | packet_context_fc = tc.create_structure_field_class() | |
28 | for name, fc in ctx_field_classes: | |
29 | packet_context_fc.append_member(name, fc) | |
30 | ||
31 | trace = tc() | |
cfbd7cf3 FD |
32 | stream_class = tc.create_stream_class( |
33 | packet_context_field_class=packet_context_fc, supports_packets=True | |
34 | ) | |
1eccc498 SM |
35 | |
36 | stream = trace.create_stream(stream_class) | |
37 | return stream | |
38 | ||
39 | ||
40 | # Create a field of the given field class. | |
41 | # | |
42 | # The field is part of a dummy stream, itself part of a dummy trace created | |
43 | # from trace class `tc`. | |
44 | ||
cfbd7cf3 | 45 | |
1eccc498 | 46 | def _create_field(tc, field_class): |
f5567ea8 | 47 | field_name = "field" |
1eccc498 SM |
48 | stream = _create_stream(tc, [(field_name, field_class)]) |
49 | packet = stream.create_packet() | |
50 | return packet.context_field[field_name] | |
51 | ||
52 | ||
53 | # Create a field of type string. | |
54 | # | |
55 | # The field is part of a dummy stream, itself part of a dummy trace created | |
56 | # from trace class `tc`. It is made out of a dummy string field class. | |
57 | ||
cfbd7cf3 | 58 | |
1eccc498 | 59 | def _create_string_field(tc): |
f5567ea8 | 60 | field_name = "string_field" |
1eccc498 SM |
61 | stream = _create_stream(tc, [(field_name, tc.create_string_field_class())]) |
62 | packet = stream.create_packet() | |
63 | return packet.context_field[field_name] | |
64 | ||
65 | ||
66 | # Create a field of type static array of ints. | |
67 | # | |
68 | # The field is part of a dummy stream, itself part of a dummy trace created | |
69 | # from trace class `tc`. It is made out of a dummy static array field class, | |
70 | # with a dummy integer field class as element class. | |
71 | ||
cfbd7cf3 | 72 | |
1eccc498 SM |
73 | def _create_int_array_field(tc, length): |
74 | elem_fc = tc.create_signed_integer_field_class(32) | |
75 | fc = tc.create_static_array_field_class(elem_fc, length) | |
f5567ea8 | 76 | field_name = "int_array" |
1eccc498 SM |
77 | stream = _create_stream(tc, [(field_name, fc)]) |
78 | packet = stream.create_packet() | |
79 | return packet.context_field[field_name] | |
80 | ||
81 | ||
82 | # Create a field of type dynamic array of ints. | |
83 | # | |
84 | # The field is part of a dummy stream, itself part of a dummy trace created | |
85 | # from trace class `tc`. It is made out of a dummy static array field class, | |
86 | # with a dummy integer field class as element and length classes. | |
87 | ||
cfbd7cf3 | 88 | |
1eccc498 SM |
89 | def _create_dynamic_array(tc): |
90 | elem_fc = tc.create_signed_integer_field_class(32) | |
91 | len_fc = tc.create_signed_integer_field_class(32) | |
92 | fc = tc.create_dynamic_array_field_class(elem_fc) | |
f5567ea8 FD |
93 | field_name = "int_dyn_array" |
94 | stream = _create_stream(tc, [("thelength", len_fc), (field_name, fc)]) | |
1eccc498 SM |
95 | packet = stream.create_packet() |
96 | packet.context_field[field_name].length = 3 | |
97 | return packet.context_field[field_name] | |
98 | ||
99 | ||
100 | # Create a field of type array of (empty) structures. | |
101 | # | |
102 | # The field is part of a dummy stream, itself part of a dummy trace created | |
103 | # from trace class `tc`. It is made out of a dummy static array field class, | |
104 | # with a dummy struct field class as element class. | |
105 | ||
cfbd7cf3 | 106 | |
1eccc498 SM |
107 | def _create_struct_array_field(tc, length): |
108 | elem_fc = tc.create_structure_field_class() | |
109 | fc = tc.create_static_array_field_class(elem_fc, length) | |
f5567ea8 | 110 | field_name = "struct_array" |
1eccc498 SM |
111 | stream = _create_stream(tc, [(field_name, fc)]) |
112 | packet = stream.create_packet() | |
113 | return packet.context_field[field_name] | |
114 | ||
115 | ||
ead8c3d4 PP |
116 | class BitArrayFieldTestCase(unittest.TestCase): |
117 | def _create_field(self): | |
118 | return _create_field(self._tc, self._tc.create_bit_array_field_class(24)) | |
119 | ||
120 | def setUp(self): | |
121 | self._tc = get_default_trace_class() | |
122 | self._def_value = 15497 | |
123 | self._def = self._create_field() | |
124 | self._def.value_as_integer = self._def_value | |
125 | self._def_new_value = 101542 | |
126 | ||
127 | def test_assign_invalid_type(self): | |
128 | with self.assertRaises(TypeError): | |
f5567ea8 | 129 | self._def.value_as_integer = "onze" |
ead8c3d4 PP |
130 | |
131 | def test_assign(self): | |
132 | self._def.value_as_integer = 199 | |
133 | self.assertEqual(self._def.value_as_integer, 199) | |
134 | ||
135 | def test_assign_masked(self): | |
136 | self._def.value_as_integer = 0xE1549BB | |
137 | self.assertEqual(self._def.value_as_integer, 0xE1549BB & ((1 << 24) - 1)) | |
138 | ||
139 | def test_eq(self): | |
140 | other = self._create_field() | |
141 | other.value_as_integer = self._def_value | |
142 | self.assertEqual(self._def, other) | |
143 | ||
144 | def test_ne_same_type(self): | |
145 | other = self._create_field() | |
146 | other.value_as_integer = self._def_value - 1 | |
147 | self.assertNotEqual(self._def, other) | |
148 | ||
149 | def test_ne_diff_type(self): | |
150 | self.assertNotEqual(self._def, self._def_value) | |
151 | ||
152 | def test_len(self): | |
153 | self.assertEqual(len(self._def), 24) | |
154 | ||
155 | def test_str(self): | |
156 | self.assertEqual(str(self._def), str(self._def_value)) | |
157 | ||
158 | def test_repr(self): | |
159 | self.assertEqual(repr(self._def), repr(self._def_value)) | |
160 | ||
161 | ||
21368027 PP |
162 | # Base class for numeric field test cases. |
163 | # | |
164 | # To be compatible with this base class, a derived class must, in its | |
165 | # setUp() method: | |
166 | # | |
167 | # * Set `self._def` to a field object with an arbitrary value. | |
168 | # * Set `self._def_value` to the equivalent value of `self._def`. | |
1eccc498 | 169 | class _TestNumericField: |
21368027 PP |
170 | # Tries the binary operation `op`: |
171 | # | |
172 | # 1. Between `self._def`, which is a field object, and `rhs`. | |
173 | # 2. Between `self._def_value`, which is the raw value of | |
174 | # `self._def`, and `rhs`. | |
175 | # | |
176 | # Returns the results of 1. and 2. | |
177 | # | |
178 | # If there's an exception while performing 1. or 2., asserts that | |
179 | # both operations raised exceptions, that both exceptions have the | |
180 | # same type, and returns `None` for both results. | |
9cf643d1 | 181 | def _binop(self, op, rhs): |
21368027 PP |
182 | type_rexc = None |
183 | type_rvexc = None | |
9cf643d1 PP |
184 | comp_value = rhs |
185 | ||
21368027 | 186 | # try with field object |
9cf643d1 PP |
187 | try: |
188 | r = op(self._def, rhs) | |
189 | except Exception as e: | |
21368027 | 190 | type_rexc = type(e) |
9cf643d1 | 191 | |
21368027 | 192 | # try with value |
9cf643d1 PP |
193 | try: |
194 | rv = op(self._def_value, comp_value) | |
195 | except Exception as e: | |
21368027 | 196 | type_rvexc = type(e) |
9cf643d1 | 197 | |
21368027 | 198 | if type_rexc is not None or type_rvexc is not None: |
9cf643d1 PP |
199 | # at least one of the operations raised an exception: in |
200 | # this case both operations should have raised the same | |
201 | # type of exception (division by zero, bit shift with a | |
202 | # floating point number operand, etc.) | |
21368027 | 203 | self.assertIs(type_rexc, type_rvexc) |
9cf643d1 PP |
204 | return None, None |
205 | ||
206 | return r, rv | |
207 | ||
21368027 PP |
208 | # Tries the unary operation `op`: |
209 | # | |
210 | # 1. On `self._def`, which is a field object. | |
211 | # 2. On `self._def_value`, which is the value of `self._def`. | |
212 | # | |
213 | # Returns the results of 1. and 2. | |
214 | # | |
215 | # If there's an exception while performing 1. or 2., asserts that | |
216 | # both operations raised exceptions, that both exceptions have the | |
217 | # same type, and returns `None` for both results. | |
9cf643d1 | 218 | def _unaryop(self, op): |
21368027 PP |
219 | type_rexc = None |
220 | type_rvexc = None | |
9cf643d1 | 221 | |
21368027 | 222 | # try with field object |
9cf643d1 PP |
223 | try: |
224 | r = op(self._def) | |
225 | except Exception as e: | |
21368027 | 226 | type_rexc = type(e) |
9cf643d1 | 227 | |
21368027 | 228 | # try with value |
9cf643d1 PP |
229 | try: |
230 | rv = op(self._def_value) | |
231 | except Exception as e: | |
21368027 | 232 | type_rvexc = type(e) |
9cf643d1 | 233 | |
21368027 | 234 | if type_rexc is not None or type_rvexc is not None: |
9cf643d1 PP |
235 | # at least one of the operations raised an exception: in |
236 | # this case both operations should have raised the same | |
237 | # type of exception (division by zero, bit shift with a | |
238 | # floating point number operand, etc.) | |
21368027 | 239 | self.assertIs(type_rexc, type_rvexc) |
9cf643d1 PP |
240 | return None, None |
241 | ||
242 | return r, rv | |
243 | ||
21368027 PP |
244 | # Tests that the unary operation `op` gives results with the same |
245 | # type for both `self._def` and `self._def_value`. | |
9cf643d1 PP |
246 | def _test_unaryop_type(self, op): |
247 | r, rv = self._unaryop(op) | |
248 | ||
249 | if r is None: | |
250 | return | |
251 | ||
252 | self.assertIsInstance(r, type(rv)) | |
253 | ||
21368027 PP |
254 | # Tests that the unary operation `op` gives results with the same |
255 | # value for both `self._def` and `self._def_value`. This uses the | |
256 | # __eq__() operator of `self._def`. | |
9cf643d1 PP |
257 | def _test_unaryop_value(self, op): |
258 | r, rv = self._unaryop(op) | |
259 | ||
260 | if r is None: | |
261 | return | |
262 | ||
263 | self.assertEqual(r, rv) | |
264 | ||
21368027 PP |
265 | # Tests that the unary operation `op`, when applied to `self._def`, |
266 | # does not change its underlying BT object address. | |
9cf643d1 PP |
267 | def _test_unaryop_addr_same(self, op): |
268 | addr_before = self._def.addr | |
269 | self._unaryop(op) | |
270 | self.assertEqual(self._def.addr, addr_before) | |
271 | ||
21368027 PP |
272 | # Tests that the unary operation `op`, when applied to `self._def`, |
273 | # does not change its value. | |
9cf643d1 | 274 | def _test_unaryop_value_same(self, op): |
e1c6bebd | 275 | value_before = copy.copy(self._def_value) |
9cf643d1 | 276 | self._unaryop(op) |
e1c6bebd | 277 | self.assertEqual(self._def, value_before) |
9cf643d1 | 278 | |
21368027 PP |
279 | # Tests that the binary operation `op` gives results with the same |
280 | # type for both `self._def` and `self._def_value`. | |
9cf643d1 PP |
281 | def _test_binop_type(self, op, rhs): |
282 | r, rv = self._binop(op, rhs) | |
283 | ||
284 | if r is None: | |
285 | return | |
286 | ||
287 | if op in _COMP_BINOPS: | |
288 | # __eq__() and __ne__() always return a 'bool' object | |
289 | self.assertIsInstance(r, bool) | |
290 | else: | |
291 | self.assertIsInstance(r, type(rv)) | |
292 | ||
21368027 PP |
293 | # Tests that the binary operation `op` gives results with the same |
294 | # value for both `self._def` and `self._def_value`. This uses the | |
295 | # __eq__() operator of `self._def`. | |
9cf643d1 PP |
296 | def _test_binop_value(self, op, rhs): |
297 | r, rv = self._binop(op, rhs) | |
298 | ||
299 | if r is None: | |
300 | return | |
301 | ||
302 | self.assertEqual(r, rv) | |
303 | ||
21368027 PP |
304 | # Tests that the binary operation `op`, when applied to `self._def`, |
305 | # does not change its underlying BT object address. | |
9cf643d1 PP |
306 | def _test_binop_lhs_addr_same(self, op, rhs): |
307 | addr_before = self._def.addr | |
308 | r, rv = self._binop(op, rhs) | |
309 | self.assertEqual(self._def.addr, addr_before) | |
310 | ||
21368027 PP |
311 | # Tests that the binary operation `op`, when applied to `self._def`, |
312 | # does not change its value. | |
9cf643d1 | 313 | def _test_binop_lhs_value_same(self, op, rhs): |
fd00253e | 314 | value_before = copy.copy(self._def_value) |
9cf643d1 | 315 | r, rv = self._binop(op, rhs) |
e1c6bebd | 316 | self.assertEqual(self._def, value_before) |
9cf643d1 | 317 | |
21368027 PP |
318 | # The methods below which take the `test_cb` and/or `op` parameters |
319 | # are meant to be used with one of the _test_binop_*() functions | |
320 | # above as `test_cb` and a binary operator function as `op`. | |
321 | # | |
322 | # For example: | |
323 | # | |
324 | # self._test_binop_rhs_pos_int(self._test_binop_value, | |
325 | # operator.add) | |
326 | # | |
327 | # This tests that a numeric field object added to a positive integer | |
328 | # value gives a result with the expected value. | |
329 | # | |
330 | # `vint` and `vfloat` mean a signed integer value object and a real | |
331 | # value object. | |
332 | ||
7e481c9e | 333 | def _test_binop_unknown(self, op): |
9cf643d1 PP |
334 | class A: |
335 | pass | |
336 | ||
7e481c9e SM |
337 | # Operators == and != are defined when comparing the field to an |
338 | # arbitrary object. | |
339 | if op is operator.eq: | |
340 | self.assertIs(op(self._def, A()), False) | |
341 | elif op is operator.ne: | |
342 | self.assertIs(op(self._def, A()), True) | |
343 | else: | |
344 | # But not other operators. | |
345 | with self.assertRaises(TypeError): | |
346 | op(self._def, A()) | |
347 | ||
348 | def _test_binop_none(self, op): | |
349 | # Operators == and != are defined when comparing the field to None. | |
350 | if op is operator.eq: | |
351 | self.assertIs(op(self._def, None), False) | |
352 | elif op is operator.ne: | |
353 | self.assertIs(op(self._def, None), True) | |
354 | else: | |
355 | # But not other operators. | |
356 | with self.assertRaises(TypeError): | |
357 | op(self._def, None) | |
9cf643d1 | 358 | |
9cf643d1 PP |
359 | def _test_binop_rhs_false(self, test_cb, op): |
360 | test_cb(op, False) | |
361 | ||
362 | def _test_binop_rhs_true(self, test_cb, op): | |
363 | test_cb(op, True) | |
364 | ||
365 | def _test_binop_rhs_pos_int(self, test_cb, op): | |
366 | test_cb(op, 2) | |
367 | ||
368 | def _test_binop_rhs_neg_int(self, test_cb, op): | |
369 | test_cb(op, -23) | |
370 | ||
371 | def _test_binop_rhs_zero_int(self, test_cb, op): | |
372 | test_cb(op, 0) | |
373 | ||
374 | def _test_binop_rhs_pos_vint(self, test_cb, op): | |
375 | test_cb(op, bt2.create_value(2)) | |
376 | ||
377 | def _test_binop_rhs_neg_vint(self, test_cb, op): | |
378 | test_cb(op, bt2.create_value(-23)) | |
379 | ||
380 | def _test_binop_rhs_zero_vint(self, test_cb, op): | |
381 | test_cb(op, bt2.create_value(0)) | |
382 | ||
383 | def _test_binop_rhs_pos_float(self, test_cb, op): | |
384 | test_cb(op, 2.2) | |
385 | ||
386 | def _test_binop_rhs_neg_float(self, test_cb, op): | |
387 | test_cb(op, -23.4) | |
388 | ||
389 | def _test_binop_rhs_zero_float(self, test_cb, op): | |
390 | test_cb(op, 0.0) | |
391 | ||
392 | def _test_binop_rhs_pos_vfloat(self, test_cb, op): | |
393 | test_cb(op, bt2.create_value(2.2)) | |
394 | ||
395 | def _test_binop_rhs_neg_vfloat(self, test_cb, op): | |
396 | test_cb(op, bt2.create_value(-23.4)) | |
397 | ||
398 | def _test_binop_rhs_zero_vfloat(self, test_cb, op): | |
399 | test_cb(op, bt2.create_value(0.0)) | |
400 | ||
00512c97 | 401 | def _test_binop_rhs_complex(self, test_cb, op): |
cfbd7cf3 | 402 | test_cb(op, -23 + 19j) |
00512c97 PP |
403 | |
404 | def _test_binop_rhs_zero_complex(self, test_cb, op): | |
405 | test_cb(op, 0j) | |
406 | ||
9cf643d1 PP |
407 | def _test_binop_type_false(self, op): |
408 | self._test_binop_rhs_false(self._test_binop_type, op) | |
409 | ||
410 | def _test_binop_type_true(self, op): | |
411 | self._test_binop_rhs_true(self._test_binop_type, op) | |
412 | ||
413 | def _test_binop_type_pos_int(self, op): | |
414 | self._test_binop_rhs_pos_int(self._test_binop_type, op) | |
415 | ||
416 | def _test_binop_type_neg_int(self, op): | |
417 | self._test_binop_rhs_neg_int(self._test_binop_type, op) | |
418 | ||
419 | def _test_binop_type_zero_int(self, op): | |
420 | self._test_binop_rhs_zero_int(self._test_binop_type, op) | |
421 | ||
422 | def _test_binop_type_pos_vint(self, op): | |
423 | self._test_binop_rhs_pos_vint(self._test_binop_type, op) | |
424 | ||
425 | def _test_binop_type_neg_vint(self, op): | |
426 | self._test_binop_rhs_neg_vint(self._test_binop_type, op) | |
427 | ||
428 | def _test_binop_type_zero_vint(self, op): | |
429 | self._test_binop_rhs_zero_vint(self._test_binop_type, op) | |
430 | ||
431 | def _test_binop_type_pos_float(self, op): | |
432 | self._test_binop_rhs_pos_float(self._test_binop_type, op) | |
433 | ||
434 | def _test_binop_type_neg_float(self, op): | |
435 | self._test_binop_rhs_neg_float(self._test_binop_type, op) | |
436 | ||
437 | def _test_binop_type_zero_float(self, op): | |
438 | self._test_binop_rhs_zero_float(self._test_binop_type, op) | |
439 | ||
440 | def _test_binop_type_pos_vfloat(self, op): | |
441 | self._test_binop_rhs_pos_vfloat(self._test_binop_type, op) | |
442 | ||
443 | def _test_binop_type_neg_vfloat(self, op): | |
444 | self._test_binop_rhs_neg_vfloat(self._test_binop_type, op) | |
445 | ||
446 | def _test_binop_type_zero_vfloat(self, op): | |
447 | self._test_binop_rhs_zero_vfloat(self._test_binop_type, op) | |
448 | ||
00512c97 PP |
449 | def _test_binop_type_complex(self, op): |
450 | self._test_binop_rhs_complex(self._test_binop_type, op) | |
451 | ||
452 | def _test_binop_type_zero_complex(self, op): | |
453 | self._test_binop_rhs_zero_complex(self._test_binop_type, op) | |
454 | ||
9cf643d1 PP |
455 | def _test_binop_value_false(self, op): |
456 | self._test_binop_rhs_false(self._test_binop_value, op) | |
457 | ||
458 | def _test_binop_value_true(self, op): | |
459 | self._test_binop_rhs_true(self._test_binop_value, op) | |
460 | ||
461 | def _test_binop_value_pos_int(self, op): | |
462 | self._test_binop_rhs_pos_int(self._test_binop_value, op) | |
463 | ||
464 | def _test_binop_value_neg_int(self, op): | |
465 | self._test_binop_rhs_neg_int(self._test_binop_value, op) | |
466 | ||
467 | def _test_binop_value_zero_int(self, op): | |
468 | self._test_binop_rhs_zero_int(self._test_binop_value, op) | |
469 | ||
470 | def _test_binop_value_pos_vint(self, op): | |
471 | self._test_binop_rhs_pos_vint(self._test_binop_value, op) | |
472 | ||
473 | def _test_binop_value_neg_vint(self, op): | |
474 | self._test_binop_rhs_neg_vint(self._test_binop_value, op) | |
475 | ||
476 | def _test_binop_value_zero_vint(self, op): | |
477 | self._test_binop_rhs_zero_vint(self._test_binop_value, op) | |
478 | ||
479 | def _test_binop_value_pos_float(self, op): | |
480 | self._test_binop_rhs_pos_float(self._test_binop_value, op) | |
481 | ||
482 | def _test_binop_value_neg_float(self, op): | |
483 | self._test_binop_rhs_neg_float(self._test_binop_value, op) | |
484 | ||
485 | def _test_binop_value_zero_float(self, op): | |
486 | self._test_binop_rhs_zero_float(self._test_binop_value, op) | |
487 | ||
488 | def _test_binop_value_pos_vfloat(self, op): | |
489 | self._test_binop_rhs_pos_vfloat(self._test_binop_value, op) | |
490 | ||
491 | def _test_binop_value_neg_vfloat(self, op): | |
492 | self._test_binop_rhs_neg_vfloat(self._test_binop_value, op) | |
493 | ||
494 | def _test_binop_value_zero_vfloat(self, op): | |
495 | self._test_binop_rhs_zero_vfloat(self._test_binop_value, op) | |
496 | ||
00512c97 PP |
497 | def _test_binop_value_complex(self, op): |
498 | self._test_binop_rhs_complex(self._test_binop_value, op) | |
499 | ||
500 | def _test_binop_value_zero_complex(self, op): | |
501 | self._test_binop_rhs_zero_complex(self._test_binop_value, op) | |
502 | ||
9cf643d1 PP |
503 | def _test_binop_lhs_addr_same_false(self, op): |
504 | self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op) | |
505 | ||
506 | def _test_binop_lhs_addr_same_true(self, op): | |
507 | self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op) | |
508 | ||
509 | def _test_binop_lhs_addr_same_pos_int(self, op): | |
510 | self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op) | |
511 | ||
512 | def _test_binop_lhs_addr_same_neg_int(self, op): | |
513 | self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op) | |
514 | ||
515 | def _test_binop_lhs_addr_same_zero_int(self, op): | |
516 | self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op) | |
517 | ||
518 | def _test_binop_lhs_addr_same_pos_vint(self, op): | |
519 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op) | |
520 | ||
521 | def _test_binop_lhs_addr_same_neg_vint(self, op): | |
522 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op) | |
523 | ||
524 | def _test_binop_lhs_addr_same_zero_vint(self, op): | |
525 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op) | |
526 | ||
527 | def _test_binop_lhs_addr_same_pos_float(self, op): | |
528 | self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op) | |
529 | ||
530 | def _test_binop_lhs_addr_same_neg_float(self, op): | |
531 | self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op) | |
532 | ||
533 | def _test_binop_lhs_addr_same_zero_float(self, op): | |
534 | self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op) | |
535 | ||
536 | def _test_binop_lhs_addr_same_pos_vfloat(self, op): | |
537 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op) | |
538 | ||
539 | def _test_binop_lhs_addr_same_neg_vfloat(self, op): | |
540 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op) | |
541 | ||
542 | def _test_binop_lhs_addr_same_zero_vfloat(self, op): | |
543 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op) | |
544 | ||
00512c97 PP |
545 | def _test_binop_lhs_addr_same_complex(self, op): |
546 | self._test_binop_rhs_complex(self._test_binop_lhs_addr_same, op) | |
547 | ||
548 | def _test_binop_lhs_addr_same_zero_complex(self, op): | |
549 | self._test_binop_rhs_zero_complex(self._test_binop_lhs_addr_same, op) | |
550 | ||
9cf643d1 PP |
551 | def _test_binop_lhs_value_same_false(self, op): |
552 | self._test_binop_rhs_false(self._test_binop_lhs_value_same, op) | |
553 | ||
554 | def _test_binop_lhs_value_same_true(self, op): | |
555 | self._test_binop_rhs_true(self._test_binop_lhs_value_same, op) | |
556 | ||
557 | def _test_binop_lhs_value_same_pos_int(self, op): | |
558 | self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op) | |
559 | ||
560 | def _test_binop_lhs_value_same_neg_int(self, op): | |
561 | self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op) | |
562 | ||
563 | def _test_binop_lhs_value_same_zero_int(self, op): | |
564 | self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op) | |
565 | ||
566 | def _test_binop_lhs_value_same_pos_vint(self, op): | |
567 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op) | |
568 | ||
569 | def _test_binop_lhs_value_same_neg_vint(self, op): | |
570 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op) | |
571 | ||
572 | def _test_binop_lhs_value_same_zero_vint(self, op): | |
573 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op) | |
574 | ||
575 | def _test_binop_lhs_value_same_pos_float(self, op): | |
576 | self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op) | |
577 | ||
578 | def _test_binop_lhs_value_same_neg_float(self, op): | |
579 | self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op) | |
580 | ||
581 | def _test_binop_lhs_value_same_zero_float(self, op): | |
582 | self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op) | |
583 | ||
584 | def _test_binop_lhs_value_same_pos_vfloat(self, op): | |
585 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op) | |
586 | ||
587 | def _test_binop_lhs_value_same_neg_vfloat(self, op): | |
588 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op) | |
589 | ||
590 | def _test_binop_lhs_value_same_zero_vfloat(self, op): | |
591 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op) | |
592 | ||
00512c97 PP |
593 | def _test_binop_lhs_value_same_complex(self, op): |
594 | self._test_binop_rhs_complex(self._test_binop_lhs_value_same, op) | |
595 | ||
596 | def _test_binop_lhs_value_same_zero_complex(self, op): | |
597 | self._test_binop_rhs_zero_complex(self._test_binop_lhs_value_same, op) | |
598 | ||
9cf643d1 PP |
599 | def test_bool_op(self): |
600 | self.assertEqual(bool(self._def), bool(self._def_value)) | |
601 | ||
602 | def test_int_op(self): | |
603 | self.assertEqual(int(self._def), int(self._def_value)) | |
604 | ||
605 | def test_float_op(self): | |
606 | self.assertEqual(float(self._def), float(self._def_value)) | |
607 | ||
608 | def test_complex_op(self): | |
609 | self.assertEqual(complex(self._def), complex(self._def_value)) | |
610 | ||
611 | def test_str_op(self): | |
612 | self.assertEqual(str(self._def), str(self._def_value)) | |
613 | ||
dda659b3 FD |
614 | def test_hash_op(self): |
615 | with self.assertRaises(TypeError): | |
616 | hash(self._def) | |
617 | ||
618 | def test_const_hash_op(self): | |
619 | self.assertEqual(hash(self._def_const), hash(self._def_value)) | |
620 | ||
621 | def test_const_hash_dict(self): | |
622 | my_dict = {} | |
f5567ea8 FD |
623 | my_dict[self._def_const] = "my_value" |
624 | self.assertEqual(my_dict[self._def_value], "my_value") | |
dda659b3 | 625 | |
9cf643d1 | 626 | def test_eq_none(self): |
1eccc498 SM |
627 | # Ignore this lint error: |
628 | # E711 comparison to None should be 'if cond is None:' | |
629 | # since this is what we want to test (even though not good practice). | |
cfbd7cf3 | 630 | self.assertFalse(self._def == None) # noqa: E711 |
9cf643d1 PP |
631 | |
632 | def test_ne_none(self): | |
1eccc498 SM |
633 | # Ignore this lint error: |
634 | # E711 comparison to None should be 'if cond is not None:' | |
635 | # since this is what we want to test (even though not good practice). | |
cfbd7cf3 | 636 | self.assertTrue(self._def != None) # noqa: E711 |
742e4747 | 637 | |
9cf643d1 | 638 | |
21368027 PP |
639 | # This is a list of binary operators used for |
640 | # _inject_numeric_testing_methods(). | |
641 | # | |
642 | # Each entry is a pair of binary operator name (used as part of the | |
643 | # created testing method's name) and operator function. | |
9cf643d1 | 644 | _BINOPS = ( |
f5567ea8 FD |
645 | ("lt", operator.lt), |
646 | ("le", operator.le), | |
647 | ("eq", operator.eq), | |
648 | ("ne", operator.ne), | |
649 | ("ge", operator.ge), | |
650 | ("gt", operator.gt), | |
651 | ("add", operator.add), | |
652 | ("radd", lambda a, b: operator.add(b, a)), | |
653 | ("and", operator.and_), | |
654 | ("rand", lambda a, b: operator.and_(b, a)), | |
655 | ("floordiv", operator.floordiv), | |
656 | ("rfloordiv", lambda a, b: operator.floordiv(b, a)), | |
657 | ("lshift", operator.lshift), | |
658 | ("rlshift", lambda a, b: operator.lshift(b, a)), | |
659 | ("mod", operator.mod), | |
660 | ("rmod", lambda a, b: operator.mod(b, a)), | |
661 | ("mul", operator.mul), | |
662 | ("rmul", lambda a, b: operator.mul(b, a)), | |
663 | ("or", operator.or_), | |
664 | ("ror", lambda a, b: operator.or_(b, a)), | |
665 | ("pow", operator.pow), | |
666 | ("rpow", lambda a, b: operator.pow(b, a)), | |
667 | ("rshift", operator.rshift), | |
668 | ("rrshift", lambda a, b: operator.rshift(b, a)), | |
669 | ("sub", operator.sub), | |
670 | ("rsub", lambda a, b: operator.sub(b, a)), | |
671 | ("truediv", operator.truediv), | |
672 | ("rtruediv", lambda a, b: operator.truediv(b, a)), | |
673 | ("xor", operator.xor), | |
674 | ("rxor", lambda a, b: operator.xor(b, a)), | |
9cf643d1 PP |
675 | ) |
676 | ||
677 | ||
21368027 PP |
678 | # This is a list of unary operators used for |
679 | # _inject_numeric_testing_methods(). | |
680 | # | |
681 | # Each entry is a pair of unary operator name (used as part of the | |
682 | # created testing method's name) and operator function. | |
9cf643d1 | 683 | _UNARYOPS = ( |
f5567ea8 FD |
684 | ("neg", operator.neg), |
685 | ("pos", operator.pos), | |
686 | ("abs", operator.abs), | |
687 | ("invert", operator.invert), | |
688 | ("round", round), | |
689 | ("round_0", partial(round, ndigits=0)), | |
690 | ("round_1", partial(round, ndigits=1)), | |
691 | ("round_2", partial(round, ndigits=2)), | |
692 | ("round_3", partial(round, ndigits=3)), | |
693 | ("ceil", math.ceil), | |
694 | ("floor", math.floor), | |
695 | ("trunc", math.trunc), | |
9cf643d1 PP |
696 | ) |
697 | ||
698 | ||
21368027 PP |
699 | # This function injects a bunch of testing methods to a numeric |
700 | # field test case. | |
701 | # | |
702 | # It is meant to be used like this: | |
703 | # | |
704 | # _inject_numeric_testing_methods(MyNumericFieldTestCase) | |
705 | # | |
706 | # This function injects: | |
707 | # | |
708 | # * One testing method for each _TestNumericField._test_binop_*() | |
709 | # method, for each binary operator in the _BINOPS tuple. | |
710 | # | |
711 | # * One testing method for each _TestNumericField._test_unaryop*() | |
712 | # method, for each unary operator in the _UNARYOPS tuple. | |
9cf643d1 PP |
713 | def _inject_numeric_testing_methods(cls): |
714 | def test_binop_name(suffix): | |
f5567ea8 | 715 | return "test_binop_{}_{}".format(name, suffix) |
9cf643d1 | 716 | |
9cf643d1 | 717 | def test_unaryop_name(suffix): |
f5567ea8 | 718 | return "test_unaryop_{}_{}".format(name, suffix) |
9cf643d1 PP |
719 | |
720 | # inject testing methods for each binary operation | |
721 | for name, binop in _BINOPS: | |
cfbd7cf3 FD |
722 | setattr( |
723 | cls, | |
f5567ea8 | 724 | test_binop_name("unknown"), |
7e481c9e | 725 | partialmethod(_TestNumericField._test_binop_unknown, op=binop), |
cfbd7cf3 FD |
726 | ) |
727 | setattr( | |
728 | cls, | |
f5567ea8 | 729 | test_binop_name("none"), |
7e481c9e | 730 | partialmethod(_TestNumericField._test_binop_none, op=binop), |
cfbd7cf3 FD |
731 | ) |
732 | setattr( | |
733 | cls, | |
f5567ea8 | 734 | test_binop_name("type_true"), |
cfbd7cf3 FD |
735 | partialmethod(_TestNumericField._test_binop_type_true, op=binop), |
736 | ) | |
737 | setattr( | |
738 | cls, | |
f5567ea8 | 739 | test_binop_name("type_pos_int"), |
cfbd7cf3 FD |
740 | partialmethod(_TestNumericField._test_binop_type_pos_int, op=binop), |
741 | ) | |
742 | setattr( | |
743 | cls, | |
f5567ea8 | 744 | test_binop_name("type_pos_vint"), |
cfbd7cf3 FD |
745 | partialmethod(_TestNumericField._test_binop_type_pos_vint, op=binop), |
746 | ) | |
747 | setattr( | |
748 | cls, | |
f5567ea8 | 749 | test_binop_name("value_true"), |
cfbd7cf3 FD |
750 | partialmethod(_TestNumericField._test_binop_value_true, op=binop), |
751 | ) | |
752 | setattr( | |
753 | cls, | |
f5567ea8 | 754 | test_binop_name("value_pos_int"), |
cfbd7cf3 FD |
755 | partialmethod(_TestNumericField._test_binop_value_pos_int, op=binop), |
756 | ) | |
757 | setattr( | |
758 | cls, | |
f5567ea8 | 759 | test_binop_name("value_pos_vint"), |
cfbd7cf3 FD |
760 | partialmethod(_TestNumericField._test_binop_value_pos_vint, op=binop), |
761 | ) | |
762 | setattr( | |
763 | cls, | |
f5567ea8 | 764 | test_binop_name("lhs_addr_same_true"), |
cfbd7cf3 FD |
765 | partialmethod(_TestNumericField._test_binop_lhs_addr_same_true, op=binop), |
766 | ) | |
767 | setattr( | |
768 | cls, | |
f5567ea8 | 769 | test_binop_name("lhs_addr_same_pos_int"), |
cfbd7cf3 FD |
770 | partialmethod( |
771 | _TestNumericField._test_binop_lhs_addr_same_pos_int, op=binop | |
772 | ), | |
773 | ) | |
774 | setattr( | |
775 | cls, | |
f5567ea8 | 776 | test_binop_name("lhs_addr_same_pos_vint"), |
cfbd7cf3 FD |
777 | partialmethod( |
778 | _TestNumericField._test_binop_lhs_addr_same_pos_vint, op=binop | |
779 | ), | |
780 | ) | |
781 | setattr( | |
782 | cls, | |
f5567ea8 | 783 | test_binop_name("lhs_value_same_true"), |
cfbd7cf3 FD |
784 | partialmethod(_TestNumericField._test_binop_lhs_value_same_true, op=binop), |
785 | ) | |
786 | setattr( | |
787 | cls, | |
f5567ea8 | 788 | test_binop_name("lhs_value_same_pos_int"), |
cfbd7cf3 FD |
789 | partialmethod( |
790 | _TestNumericField._test_binop_lhs_value_same_pos_int, op=binop | |
791 | ), | |
792 | ) | |
793 | setattr( | |
794 | cls, | |
f5567ea8 | 795 | test_binop_name("lhs_value_same_pos_vint"), |
cfbd7cf3 FD |
796 | partialmethod( |
797 | _TestNumericField._test_binop_lhs_value_same_pos_vint, op=binop | |
798 | ), | |
799 | ) | |
800 | setattr( | |
801 | cls, | |
f5567ea8 | 802 | test_binop_name("type_neg_int"), |
cfbd7cf3 FD |
803 | partialmethod(_TestNumericField._test_binop_type_neg_int, op=binop), |
804 | ) | |
805 | setattr( | |
806 | cls, | |
f5567ea8 | 807 | test_binop_name("type_neg_vint"), |
cfbd7cf3 FD |
808 | partialmethod(_TestNumericField._test_binop_type_neg_vint, op=binop), |
809 | ) | |
810 | setattr( | |
811 | cls, | |
f5567ea8 | 812 | test_binop_name("value_neg_int"), |
cfbd7cf3 FD |
813 | partialmethod(_TestNumericField._test_binop_value_neg_int, op=binop), |
814 | ) | |
815 | setattr( | |
816 | cls, | |
f5567ea8 | 817 | test_binop_name("value_neg_vint"), |
cfbd7cf3 FD |
818 | partialmethod(_TestNumericField._test_binop_value_neg_vint, op=binop), |
819 | ) | |
820 | setattr( | |
821 | cls, | |
f5567ea8 | 822 | test_binop_name("lhs_addr_same_neg_int"), |
cfbd7cf3 FD |
823 | partialmethod( |
824 | _TestNumericField._test_binop_lhs_addr_same_neg_int, op=binop | |
825 | ), | |
826 | ) | |
827 | setattr( | |
828 | cls, | |
f5567ea8 | 829 | test_binop_name("lhs_addr_same_neg_vint"), |
cfbd7cf3 FD |
830 | partialmethod( |
831 | _TestNumericField._test_binop_lhs_addr_same_neg_vint, op=binop | |
832 | ), | |
833 | ) | |
834 | setattr( | |
835 | cls, | |
f5567ea8 | 836 | test_binop_name("lhs_value_same_neg_int"), |
cfbd7cf3 FD |
837 | partialmethod( |
838 | _TestNumericField._test_binop_lhs_value_same_neg_int, op=binop | |
839 | ), | |
840 | ) | |
841 | setattr( | |
842 | cls, | |
f5567ea8 | 843 | test_binop_name("lhs_value_same_neg_vint"), |
cfbd7cf3 FD |
844 | partialmethod( |
845 | _TestNumericField._test_binop_lhs_value_same_neg_vint, op=binop | |
846 | ), | |
847 | ) | |
848 | setattr( | |
849 | cls, | |
f5567ea8 | 850 | test_binop_name("type_false"), |
cfbd7cf3 FD |
851 | partialmethod(_TestNumericField._test_binop_type_false, op=binop), |
852 | ) | |
853 | setattr( | |
854 | cls, | |
f5567ea8 | 855 | test_binop_name("type_zero_int"), |
cfbd7cf3 FD |
856 | partialmethod(_TestNumericField._test_binop_type_zero_int, op=binop), |
857 | ) | |
858 | setattr( | |
859 | cls, | |
f5567ea8 | 860 | test_binop_name("type_zero_vint"), |
cfbd7cf3 FD |
861 | partialmethod(_TestNumericField._test_binop_type_zero_vint, op=binop), |
862 | ) | |
863 | setattr( | |
864 | cls, | |
f5567ea8 | 865 | test_binop_name("value_false"), |
cfbd7cf3 FD |
866 | partialmethod(_TestNumericField._test_binop_value_false, op=binop), |
867 | ) | |
868 | setattr( | |
869 | cls, | |
f5567ea8 | 870 | test_binop_name("value_zero_int"), |
cfbd7cf3 FD |
871 | partialmethod(_TestNumericField._test_binop_value_zero_int, op=binop), |
872 | ) | |
873 | setattr( | |
874 | cls, | |
f5567ea8 | 875 | test_binop_name("value_zero_vint"), |
cfbd7cf3 FD |
876 | partialmethod(_TestNumericField._test_binop_value_zero_vint, op=binop), |
877 | ) | |
878 | setattr( | |
879 | cls, | |
f5567ea8 | 880 | test_binop_name("lhs_addr_same_false"), |
cfbd7cf3 FD |
881 | partialmethod(_TestNumericField._test_binop_lhs_addr_same_false, op=binop), |
882 | ) | |
883 | setattr( | |
884 | cls, | |
f5567ea8 | 885 | test_binop_name("lhs_addr_same_zero_int"), |
cfbd7cf3 FD |
886 | partialmethod( |
887 | _TestNumericField._test_binop_lhs_addr_same_zero_int, op=binop | |
888 | ), | |
889 | ) | |
890 | setattr( | |
891 | cls, | |
f5567ea8 | 892 | test_binop_name("lhs_addr_same_zero_vint"), |
cfbd7cf3 FD |
893 | partialmethod( |
894 | _TestNumericField._test_binop_lhs_addr_same_zero_vint, op=binop | |
895 | ), | |
896 | ) | |
897 | setattr( | |
898 | cls, | |
f5567ea8 | 899 | test_binop_name("lhs_value_same_false"), |
cfbd7cf3 FD |
900 | partialmethod(_TestNumericField._test_binop_lhs_value_same_false, op=binop), |
901 | ) | |
902 | setattr( | |
903 | cls, | |
f5567ea8 | 904 | test_binop_name("lhs_value_same_zero_int"), |
cfbd7cf3 FD |
905 | partialmethod( |
906 | _TestNumericField._test_binop_lhs_value_same_zero_int, op=binop | |
907 | ), | |
908 | ) | |
909 | setattr( | |
910 | cls, | |
f5567ea8 | 911 | test_binop_name("lhs_value_same_zero_vint"), |
cfbd7cf3 FD |
912 | partialmethod( |
913 | _TestNumericField._test_binop_lhs_value_same_zero_vint, op=binop | |
914 | ), | |
915 | ) | |
916 | setattr( | |
917 | cls, | |
f5567ea8 | 918 | test_binop_name("type_pos_float"), |
cfbd7cf3 FD |
919 | partialmethod(_TestNumericField._test_binop_type_pos_float, op=binop), |
920 | ) | |
921 | setattr( | |
922 | cls, | |
f5567ea8 | 923 | test_binop_name("type_neg_float"), |
cfbd7cf3 FD |
924 | partialmethod(_TestNumericField._test_binop_type_neg_float, op=binop), |
925 | ) | |
926 | setattr( | |
927 | cls, | |
f5567ea8 | 928 | test_binop_name("type_pos_vfloat"), |
cfbd7cf3 FD |
929 | partialmethod(_TestNumericField._test_binop_type_pos_vfloat, op=binop), |
930 | ) | |
931 | setattr( | |
932 | cls, | |
f5567ea8 | 933 | test_binop_name("type_neg_vfloat"), |
cfbd7cf3 FD |
934 | partialmethod(_TestNumericField._test_binop_type_neg_vfloat, op=binop), |
935 | ) | |
936 | setattr( | |
937 | cls, | |
f5567ea8 | 938 | test_binop_name("value_pos_float"), |
cfbd7cf3 FD |
939 | partialmethod(_TestNumericField._test_binop_value_pos_float, op=binop), |
940 | ) | |
941 | setattr( | |
942 | cls, | |
f5567ea8 | 943 | test_binop_name("value_neg_float"), |
cfbd7cf3 FD |
944 | partialmethod(_TestNumericField._test_binop_value_neg_float, op=binop), |
945 | ) | |
946 | setattr( | |
947 | cls, | |
f5567ea8 | 948 | test_binop_name("value_pos_vfloat"), |
cfbd7cf3 FD |
949 | partialmethod(_TestNumericField._test_binop_value_pos_vfloat, op=binop), |
950 | ) | |
951 | setattr( | |
952 | cls, | |
f5567ea8 | 953 | test_binop_name("value_neg_vfloat"), |
cfbd7cf3 FD |
954 | partialmethod(_TestNumericField._test_binop_value_neg_vfloat, op=binop), |
955 | ) | |
956 | setattr( | |
957 | cls, | |
f5567ea8 | 958 | test_binop_name("lhs_addr_same_pos_float"), |
cfbd7cf3 FD |
959 | partialmethod( |
960 | _TestNumericField._test_binop_lhs_addr_same_pos_float, op=binop | |
961 | ), | |
962 | ) | |
963 | setattr( | |
964 | cls, | |
f5567ea8 | 965 | test_binop_name("lhs_addr_same_neg_float"), |
cfbd7cf3 FD |
966 | partialmethod( |
967 | _TestNumericField._test_binop_lhs_addr_same_neg_float, op=binop | |
968 | ), | |
969 | ) | |
970 | setattr( | |
971 | cls, | |
f5567ea8 | 972 | test_binop_name("lhs_addr_same_pos_vfloat"), |
cfbd7cf3 FD |
973 | partialmethod( |
974 | _TestNumericField._test_binop_lhs_addr_same_pos_vfloat, op=binop | |
975 | ), | |
976 | ) | |
977 | setattr( | |
978 | cls, | |
f5567ea8 | 979 | test_binop_name("lhs_addr_same_neg_vfloat"), |
cfbd7cf3 FD |
980 | partialmethod( |
981 | _TestNumericField._test_binop_lhs_addr_same_neg_vfloat, op=binop | |
982 | ), | |
983 | ) | |
984 | setattr( | |
985 | cls, | |
f5567ea8 | 986 | test_binop_name("lhs_value_same_pos_float"), |
cfbd7cf3 FD |
987 | partialmethod( |
988 | _TestNumericField._test_binop_lhs_value_same_pos_float, op=binop | |
989 | ), | |
990 | ) | |
991 | setattr( | |
992 | cls, | |
f5567ea8 | 993 | test_binop_name("lhs_value_same_neg_float"), |
cfbd7cf3 FD |
994 | partialmethod( |
995 | _TestNumericField._test_binop_lhs_value_same_neg_float, op=binop | |
996 | ), | |
997 | ) | |
998 | setattr( | |
999 | cls, | |
f5567ea8 | 1000 | test_binop_name("lhs_value_same_pos_vfloat"), |
cfbd7cf3 FD |
1001 | partialmethod( |
1002 | _TestNumericField._test_binop_lhs_value_same_pos_vfloat, op=binop | |
1003 | ), | |
1004 | ) | |
1005 | setattr( | |
1006 | cls, | |
f5567ea8 | 1007 | test_binop_name("lhs_value_same_neg_vfloat"), |
cfbd7cf3 FD |
1008 | partialmethod( |
1009 | _TestNumericField._test_binop_lhs_value_same_neg_vfloat, op=binop | |
1010 | ), | |
1011 | ) | |
1012 | setattr( | |
1013 | cls, | |
f5567ea8 | 1014 | test_binop_name("type_zero_float"), |
cfbd7cf3 FD |
1015 | partialmethod(_TestNumericField._test_binop_type_zero_float, op=binop), |
1016 | ) | |
1017 | setattr( | |
1018 | cls, | |
f5567ea8 | 1019 | test_binop_name("type_zero_vfloat"), |
cfbd7cf3 FD |
1020 | partialmethod(_TestNumericField._test_binop_type_zero_vfloat, op=binop), |
1021 | ) | |
1022 | setattr( | |
1023 | cls, | |
f5567ea8 | 1024 | test_binop_name("value_zero_float"), |
cfbd7cf3 FD |
1025 | partialmethod(_TestNumericField._test_binop_value_zero_float, op=binop), |
1026 | ) | |
1027 | setattr( | |
1028 | cls, | |
f5567ea8 | 1029 | test_binop_name("value_zero_vfloat"), |
cfbd7cf3 FD |
1030 | partialmethod(_TestNumericField._test_binop_value_zero_vfloat, op=binop), |
1031 | ) | |
1032 | setattr( | |
1033 | cls, | |
f5567ea8 | 1034 | test_binop_name("lhs_addr_same_zero_float"), |
cfbd7cf3 FD |
1035 | partialmethod( |
1036 | _TestNumericField._test_binop_lhs_addr_same_zero_float, op=binop | |
1037 | ), | |
1038 | ) | |
1039 | setattr( | |
1040 | cls, | |
f5567ea8 | 1041 | test_binop_name("lhs_addr_same_zero_vfloat"), |
cfbd7cf3 FD |
1042 | partialmethod( |
1043 | _TestNumericField._test_binop_lhs_addr_same_zero_vfloat, op=binop | |
1044 | ), | |
1045 | ) | |
1046 | setattr( | |
1047 | cls, | |
f5567ea8 | 1048 | test_binop_name("lhs_value_same_zero_float"), |
cfbd7cf3 FD |
1049 | partialmethod( |
1050 | _TestNumericField._test_binop_lhs_value_same_zero_float, op=binop | |
1051 | ), | |
1052 | ) | |
1053 | setattr( | |
1054 | cls, | |
f5567ea8 | 1055 | test_binop_name("lhs_value_same_zero_vfloat"), |
cfbd7cf3 FD |
1056 | partialmethod( |
1057 | _TestNumericField._test_binop_lhs_value_same_zero_vfloat, op=binop | |
1058 | ), | |
1059 | ) | |
1060 | setattr( | |
1061 | cls, | |
f5567ea8 | 1062 | test_binop_name("type_complex"), |
cfbd7cf3 FD |
1063 | partialmethod(_TestNumericField._test_binop_type_complex, op=binop), |
1064 | ) | |
1065 | setattr( | |
1066 | cls, | |
f5567ea8 | 1067 | test_binop_name("type_zero_complex"), |
cfbd7cf3 FD |
1068 | partialmethod(_TestNumericField._test_binop_type_zero_complex, op=binop), |
1069 | ) | |
1070 | setattr( | |
1071 | cls, | |
f5567ea8 | 1072 | test_binop_name("value_complex"), |
cfbd7cf3 FD |
1073 | partialmethod(_TestNumericField._test_binop_value_complex, op=binop), |
1074 | ) | |
1075 | setattr( | |
1076 | cls, | |
f5567ea8 | 1077 | test_binop_name("value_zero_complex"), |
cfbd7cf3 FD |
1078 | partialmethod(_TestNumericField._test_binop_value_zero_complex, op=binop), |
1079 | ) | |
1080 | setattr( | |
1081 | cls, | |
f5567ea8 | 1082 | test_binop_name("lhs_addr_same_complex"), |
cfbd7cf3 FD |
1083 | partialmethod( |
1084 | _TestNumericField._test_binop_lhs_addr_same_complex, op=binop | |
1085 | ), | |
1086 | ) | |
1087 | setattr( | |
1088 | cls, | |
f5567ea8 | 1089 | test_binop_name("lhs_addr_same_zero_complex"), |
cfbd7cf3 FD |
1090 | partialmethod( |
1091 | _TestNumericField._test_binop_lhs_addr_same_zero_complex, op=binop | |
1092 | ), | |
1093 | ) | |
1094 | setattr( | |
1095 | cls, | |
f5567ea8 | 1096 | test_binop_name("lhs_value_same_complex"), |
cfbd7cf3 FD |
1097 | partialmethod( |
1098 | _TestNumericField._test_binop_lhs_value_same_complex, op=binop | |
1099 | ), | |
1100 | ) | |
1101 | setattr( | |
1102 | cls, | |
f5567ea8 | 1103 | test_binop_name("lhs_value_same_zero_complex"), |
cfbd7cf3 FD |
1104 | partialmethod( |
1105 | _TestNumericField._test_binop_lhs_value_same_zero_complex, op=binop | |
1106 | ), | |
1107 | ) | |
9cf643d1 PP |
1108 | |
1109 | # inject testing methods for each unary operation | |
1110 | for name, unaryop in _UNARYOPS: | |
cfbd7cf3 FD |
1111 | setattr( |
1112 | cls, | |
f5567ea8 | 1113 | test_unaryop_name("type"), |
cfbd7cf3 FD |
1114 | partialmethod(_TestNumericField._test_unaryop_type, op=unaryop), |
1115 | ) | |
1116 | setattr( | |
1117 | cls, | |
f5567ea8 | 1118 | test_unaryop_name("value"), |
cfbd7cf3 FD |
1119 | partialmethod(_TestNumericField._test_unaryop_value, op=unaryop), |
1120 | ) | |
1121 | setattr( | |
1122 | cls, | |
f5567ea8 | 1123 | test_unaryop_name("addr_same"), |
cfbd7cf3 FD |
1124 | partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop), |
1125 | ) | |
1126 | setattr( | |
1127 | cls, | |
f5567ea8 | 1128 | test_unaryop_name("value_same"), |
cfbd7cf3 FD |
1129 | partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop), |
1130 | ) | |
9cf643d1 | 1131 | |
9cf643d1 | 1132 | |
aae30e61 | 1133 | class BoolFieldTestCase(_TestNumericField, unittest.TestCase): |
f0a42b33 FD |
1134 | @staticmethod |
1135 | def _const_value_setter(field): | |
1136 | field.value = True | |
1137 | ||
aae30e61 PP |
1138 | def _create_fc(self, tc): |
1139 | return tc.create_bool_field_class() | |
1140 | ||
1141 | def setUp(self): | |
1142 | self._tc = get_default_trace_class() | |
1143 | self._def = _create_field(self._tc, self._create_fc(self._tc)) | |
1144 | self._def.value = True | |
1145 | self._def_value = True | |
6d9a6c9e | 1146 | self._def_const = create_const_field( |
f0a42b33 FD |
1147 | self._tc, self._tc.create_bool_field_class(), self._const_value_setter |
1148 | ) | |
aae30e61 PP |
1149 | self._def_new_value = False |
1150 | ||
f0a42b33 FD |
1151 | def test_classes(self): |
1152 | self.assertIs(type(self._def), bt2._BoolField) | |
1153 | self.assertIs(type(self._def_const), bt2._BoolFieldConst) | |
1154 | ||
aae30e61 PP |
1155 | def test_assign_true(self): |
1156 | raw = True | |
1157 | self._def.value = raw | |
1158 | self.assertEqual(self._def, raw) | |
1159 | ||
1160 | def test_assign_false(self): | |
1161 | raw = False | |
1162 | self._def.value = raw | |
1163 | self.assertEqual(self._def, raw) | |
1164 | ||
1165 | def test_assign_field_true(self): | |
1166 | field = _create_field(self._tc, self._create_fc(self._tc)) | |
1167 | raw = True | |
1168 | field.value = raw | |
1169 | self._def.value = field | |
1170 | self.assertEqual(self._def, raw) | |
1171 | ||
1172 | def test_assign_field_false(self): | |
1173 | field = _create_field(self._tc, self._create_fc(self._tc)) | |
1174 | raw = False | |
1175 | field.value = raw | |
1176 | self._def.value = field | |
1177 | self.assertEqual(self._def, raw) | |
1178 | ||
1179 | def test_assign_invalid_type(self): | |
1180 | with self.assertRaises(TypeError): | |
1181 | self._def.value = 17 | |
1182 | ||
1183 | def test_str_op(self): | |
1184 | self.assertEqual(str(self._def), str(self._def_value)) | |
1185 | ||
1186 | ||
1187 | _inject_numeric_testing_methods(BoolFieldTestCase) | |
1188 | ||
1189 | ||
9cf643d1 PP |
1190 | class _TestIntegerFieldCommon(_TestNumericField): |
1191 | def test_assign_true(self): | |
1192 | raw = True | |
1193 | self._def.value = raw | |
1194 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
1195 | |
1196 | def test_assign_false(self): | |
1197 | raw = False | |
1198 | self._def.value = raw | |
1199 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
1200 | |
1201 | def test_assign_pos_int(self): | |
1202 | raw = 477 | |
1203 | self._def.value = raw | |
1204 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
1205 | |
1206 | def test_assign_neg_int(self): | |
1207 | raw = -13 | |
1208 | self._def.value = raw | |
1209 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
1210 | |
1211 | def test_assign_int_field(self): | |
1212 | raw = 999 | |
1eccc498 | 1213 | field = _create_field(self._tc, self._create_fc(self._tc)) |
9cf643d1 PP |
1214 | field.value = raw |
1215 | self._def.value = field | |
1216 | self.assertEqual(self._def, raw) | |
9cf643d1 | 1217 | |
9cf643d1 PP |
1218 | def test_assign_invalid_type(self): |
1219 | with self.assertRaises(TypeError): | |
f5567ea8 | 1220 | self._def.value = "yes" |
9cf643d1 PP |
1221 | |
1222 | def test_assign_uint(self): | |
1eccc498 SM |
1223 | uint_fc = self._tc.create_unsigned_integer_field_class(32) |
1224 | field = _create_field(self._tc, uint_fc) | |
9cf643d1 PP |
1225 | raw = 1777 |
1226 | field.value = 1777 | |
1227 | self.assertEqual(field, raw) | |
9cf643d1 | 1228 | |
7bb4180f FD |
1229 | def test_assign_big_uint(self): |
1230 | uint_fc = self._tc.create_unsigned_integer_field_class(64) | |
1231 | field = _create_field(self._tc, uint_fc) | |
1232 | # Larger than the IEEE 754 double-precision exact representation of | |
1233 | # integers. | |
768f9bcb MJ |
1234 | raw = (2**53) + 1 |
1235 | field.value = (2**53) + 1 | |
7bb4180f FD |
1236 | self.assertEqual(field, raw) |
1237 | ||
1198c635 FD |
1238 | def test_assign_uint_out_of_range(self): |
1239 | uint_fc = self._tc.create_unsigned_integer_field_class(8) | |
1eccc498 | 1240 | field = _create_field(self._tc, uint_fc) |
9cf643d1 | 1241 | |
1198c635 FD |
1242 | with self.assertRaises(ValueError) as ctx: |
1243 | field.value = 256 | |
1244 | self.assertEqual( | |
f5567ea8 | 1245 | str(ctx.exception), "Value 256 is outside valid range [0, 255]" |
1198c635 FD |
1246 | ) |
1247 | ||
1248 | with self.assertRaises(ValueError) as ctx: | |
1249 | field.value = -1 | |
f5567ea8 | 1250 | self.assertEqual(str(ctx.exception), "Value -1 is outside valid range [0, 255]") |
1198c635 FD |
1251 | |
1252 | def test_assign_int_out_of_range(self): | |
1253 | int_fc = self._tc.create_signed_integer_field_class(8) | |
1254 | field = _create_field(self._tc, int_fc) | |
1255 | ||
1256 | with self.assertRaises(ValueError) as ctx: | |
1257 | field.value = 128 | |
1258 | self.assertEqual( | |
f5567ea8 | 1259 | str(ctx.exception), "Value 128 is outside valid range [-128, 127]" |
1198c635 FD |
1260 | ) |
1261 | ||
1262 | with self.assertRaises(ValueError) as ctx: | |
1263 | field.value = -129 | |
1264 | self.assertEqual( | |
f5567ea8 | 1265 | str(ctx.exception), "Value -129 is outside valid range [-128, 127]" |
1198c635 | 1266 | ) |
9cf643d1 | 1267 | |
b0bdda42 JG |
1268 | def test_str_op(self): |
1269 | self.assertEqual(str(self._def), str(self._def_value)) | |
1270 | ||
9cf643d1 PP |
1271 | |
1272 | _inject_numeric_testing_methods(_TestIntegerFieldCommon) | |
1273 | ||
1274 | ||
1eccc498 | 1275 | class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): |
dda659b3 FD |
1276 | @staticmethod |
1277 | def _const_value_setter(field): | |
1278 | field.value = 17 | |
1279 | ||
1eccc498 SM |
1280 | def _create_fc(self, tc): |
1281 | return tc.create_signed_integer_field_class(25) | |
1282 | ||
9cf643d1 | 1283 | def setUp(self): |
1eccc498 SM |
1284 | self._tc = get_default_trace_class() |
1285 | self._field = _create_field(self._tc, self._create_fc(self._tc)) | |
1286 | self._field.value = 17 | |
1287 | self._def = _create_field(self._tc, self._create_fc(self._tc)) | |
9cf643d1 PP |
1288 | self._def.value = 17 |
1289 | self._def_value = 17 | |
6d9a6c9e | 1290 | self._def_const = create_const_field( |
dda659b3 FD |
1291 | self._tc, self._create_fc(self._tc), self._const_value_setter |
1292 | ) | |
9cf643d1 PP |
1293 | self._def_new_value = -101 |
1294 | ||
811644b8 | 1295 | |
1eccc498 | 1296 | class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): |
dda659b3 FD |
1297 | @staticmethod |
1298 | def _const_value_setter(field): | |
1299 | field.value = 17 | |
1300 | ||
1eccc498 SM |
1301 | def _create_fc(self, tc): |
1302 | fc = tc.create_signed_enumeration_field_class(32) | |
f5567ea8 FD |
1303 | fc.add_mapping("something", bt2.SignedIntegerRangeSet([(17, 17)])) |
1304 | fc.add_mapping("speaker", bt2.SignedIntegerRangeSet([(12, 16)])) | |
1305 | fc.add_mapping("can", bt2.SignedIntegerRangeSet([(18, 2540)])) | |
cfbd7cf3 | 1306 | fc.add_mapping( |
f5567ea8 | 1307 | "whole range", bt2.SignedIntegerRangeSet([(-(2**31), (2**31) - 1)]) |
cfbd7cf3 | 1308 | ) |
f5567ea8 | 1309 | fc.add_mapping("zip", bt2.SignedIntegerRangeSet([(-45, 1001)])) |
1eccc498 | 1310 | return fc |
9cf643d1 | 1311 | |
9cf643d1 | 1312 | def setUp(self): |
1eccc498 SM |
1313 | self._tc = get_default_trace_class() |
1314 | self._field = _create_field(self._tc, self._create_fc(self._tc)) | |
1315 | self._def = _create_field(self._tc, self._create_fc(self._tc)) | |
9cf643d1 PP |
1316 | self._def.value = 17 |
1317 | self._def_value = 17 | |
6d9a6c9e | 1318 | self._def_const = create_const_field( |
dda659b3 FD |
1319 | self._tc, self._create_fc(self._tc), self._const_value_setter |
1320 | ) | |
9cf643d1 PP |
1321 | self._def_new_value = -101 |
1322 | ||
b0bdda42 JG |
1323 | def test_str_op(self): |
1324 | expected_string_found = False | |
1325 | s = str(self._def) | |
1326 | ||
1327 | # Establish all permutations of the three expected matches since | |
1328 | # the order in which mappings are enumerated is not explicitly part of | |
1329 | # the API. | |
f5567ea8 FD |
1330 | for p in itertools.permutations(["whole range", "something", "zip"]): |
1331 | candidate = "{} ({})".format(self._def_value, ", ".join(p)) | |
b0bdda42 JG |
1332 | if candidate == s: |
1333 | expected_string_found = True | |
1334 | break | |
1335 | ||
1336 | self.assertTrue(expected_string_found) | |
1337 | ||
1eccc498 SM |
1338 | def test_labels(self): |
1339 | self._field.value = 17 | |
1340 | labels = sorted(self._field.labels) | |
f5567ea8 | 1341 | self.assertEqual(labels, ["something", "whole range", "zip"]) |
1eccc498 | 1342 | |
b0bdda42 | 1343 | |
fe4df857 FD |
1344 | class SingleRealFieldTestCase(_TestNumericField, unittest.TestCase): |
1345 | @staticmethod | |
1346 | def _const_value_setter(field): | |
1347 | field.value = 52.0 | |
1348 | ||
1349 | def _create_fc(self, tc): | |
1350 | return tc.create_single_precision_real_field_class() | |
1351 | ||
1352 | def setUp(self): | |
1353 | self._tc = get_default_trace_class() | |
1354 | self._field = _create_field(self._tc, self._create_fc(self._tc)) | |
1355 | self._def = _create_field(self._tc, self._create_fc(self._tc)) | |
6d9a6c9e | 1356 | self._def_const = create_const_field( |
fe4df857 FD |
1357 | self._tc, |
1358 | self._tc.create_single_precision_real_field_class(), | |
1359 | self._const_value_setter, | |
1360 | ) | |
1361 | self._def.value = 52.0 | |
1362 | self._def_value = 52.0 | |
1363 | self._def_new_value = -17.0 | |
1364 | ||
1365 | def _test_invalid_op(self, cb): | |
1366 | with self.assertRaises(TypeError): | |
1367 | cb() | |
1368 | ||
1369 | def test_assign_true(self): | |
1370 | self._def.value = True | |
1371 | self.assertTrue(self._def) | |
1372 | ||
1373 | def test_assign_false(self): | |
1374 | self._def.value = False | |
1375 | self.assertFalse(self._def) | |
1376 | ||
1377 | def test_assign_pos_int(self): | |
1378 | raw = 477 | |
1379 | self._def.value = raw | |
1380 | self.assertEqual(self._def, float(raw)) | |
1381 | ||
1382 | def test_assign_neg_int(self): | |
1383 | raw = -13 | |
1384 | self._def.value = raw | |
1385 | self.assertEqual(self._def, float(raw)) | |
1386 | ||
1387 | def test_assign_int_field(self): | |
1388 | int_fc = self._tc.create_signed_integer_field_class(32) | |
1389 | int_field = _create_field(self._tc, int_fc) | |
1390 | raw = 999 | |
1391 | int_field.value = raw | |
1392 | self._def.value = int_field | |
1393 | self.assertEqual(self._def, float(raw)) | |
1394 | ||
1395 | def test_assign_float(self): | |
1396 | raw = -19.23 | |
1397 | self._def.value = raw | |
1398 | # It's expected to have some lost of precision because of the field | |
1399 | # that is in single precision. | |
1400 | self.assertEqual(round(self._def, 5), raw) | |
1401 | ||
1402 | def test_assign_float_field(self): | |
1403 | field = _create_field(self._tc, self._create_fc(self._tc)) | |
1404 | raw = 101.32 | |
1405 | field.value = raw | |
1406 | self._def.value = field | |
1407 | # It's expected to have some lost of precision because of the field | |
1408 | # that is in single precision. | |
1409 | self.assertEqual(round(self._def, 5), raw) | |
1410 | ||
1411 | def test_assign_invalid_type(self): | |
1412 | with self.assertRaises(TypeError): | |
f5567ea8 | 1413 | self._def.value = "yes" |
fe4df857 FD |
1414 | |
1415 | def test_invalid_lshift(self): | |
1416 | self._test_invalid_op(lambda: self._def << 23) | |
1417 | ||
1418 | def test_invalid_rshift(self): | |
1419 | self._test_invalid_op(lambda: self._def >> 23) | |
1420 | ||
1421 | def test_invalid_and(self): | |
1422 | self._test_invalid_op(lambda: self._def & 23) | |
1423 | ||
1424 | def test_invalid_or(self): | |
1425 | self._test_invalid_op(lambda: self._def | 23) | |
1426 | ||
1427 | def test_invalid_xor(self): | |
1428 | self._test_invalid_op(lambda: self._def ^ 23) | |
1429 | ||
1430 | def test_invalid_invert(self): | |
1431 | self._test_invalid_op(lambda: ~self._def) | |
1432 | ||
1433 | def test_str_op(self): | |
1434 | self.assertEqual(str(round(self._def, 5)), str(self._def_value)) | |
1435 | ||
1436 | ||
1437 | _inject_numeric_testing_methods(SingleRealFieldTestCase) | |
1438 | ||
1439 | ||
1440 | class DoubleRealFieldTestCase(_TestNumericField, unittest.TestCase): | |
dda659b3 FD |
1441 | @staticmethod |
1442 | def _const_value_setter(field): | |
1443 | field.value = 52.7 | |
1444 | ||
1eccc498 | 1445 | def _create_fc(self, tc): |
fe4df857 | 1446 | return tc.create_double_precision_real_field_class() |
9cf643d1 | 1447 | |
9cf643d1 | 1448 | def setUp(self): |
1eccc498 SM |
1449 | self._tc = get_default_trace_class() |
1450 | self._field = _create_field(self._tc, self._create_fc(self._tc)) | |
1451 | self._def = _create_field(self._tc, self._create_fc(self._tc)) | |
6d9a6c9e | 1452 | self._def_const = create_const_field( |
fe4df857 FD |
1453 | self._tc, |
1454 | self._tc.create_double_precision_real_field_class(), | |
1455 | self._const_value_setter, | |
dda659b3 | 1456 | ) |
9cf643d1 PP |
1457 | self._def.value = 52.7 |
1458 | self._def_value = 52.7 | |
1459 | self._def_new_value = -17.164857 | |
1460 | ||
1461 | def _test_invalid_op(self, cb): | |
1462 | with self.assertRaises(TypeError): | |
1463 | cb() | |
1464 | ||
1465 | def test_assign_true(self): | |
1466 | self._def.value = True | |
1467 | self.assertTrue(self._def) | |
9cf643d1 PP |
1468 | |
1469 | def test_assign_false(self): | |
1470 | self._def.value = False | |
1471 | self.assertFalse(self._def) | |
9cf643d1 PP |
1472 | |
1473 | def test_assign_pos_int(self): | |
1474 | raw = 477 | |
1475 | self._def.value = raw | |
1476 | self.assertEqual(self._def, float(raw)) | |
9cf643d1 PP |
1477 | |
1478 | def test_assign_neg_int(self): | |
1479 | raw = -13 | |
1480 | self._def.value = raw | |
1481 | self.assertEqual(self._def, float(raw)) | |
9cf643d1 PP |
1482 | |
1483 | def test_assign_int_field(self): | |
1eccc498 SM |
1484 | int_fc = self._tc.create_signed_integer_field_class(32) |
1485 | int_field = _create_field(self._tc, int_fc) | |
9cf643d1 | 1486 | raw = 999 |
1eccc498 SM |
1487 | int_field.value = raw |
1488 | self._def.value = int_field | |
9cf643d1 | 1489 | self.assertEqual(self._def, float(raw)) |
9cf643d1 PP |
1490 | |
1491 | def test_assign_float(self): | |
1492 | raw = -19.23 | |
1493 | self._def.value = raw | |
1494 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
1495 | |
1496 | def test_assign_float_field(self): | |
1eccc498 | 1497 | field = _create_field(self._tc, self._create_fc(self._tc)) |
9cf643d1 PP |
1498 | raw = 101.32 |
1499 | field.value = raw | |
1500 | self._def.value = field | |
1501 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
1502 | |
1503 | def test_assign_invalid_type(self): | |
1504 | with self.assertRaises(TypeError): | |
f5567ea8 | 1505 | self._def.value = "yes" |
9cf643d1 PP |
1506 | |
1507 | def test_invalid_lshift(self): | |
1508 | self._test_invalid_op(lambda: self._def << 23) | |
1509 | ||
1510 | def test_invalid_rshift(self): | |
1511 | self._test_invalid_op(lambda: self._def >> 23) | |
1512 | ||
1513 | def test_invalid_and(self): | |
1514 | self._test_invalid_op(lambda: self._def & 23) | |
1515 | ||
1516 | def test_invalid_or(self): | |
1517 | self._test_invalid_op(lambda: self._def | 23) | |
1518 | ||
1519 | def test_invalid_xor(self): | |
1520 | self._test_invalid_op(lambda: self._def ^ 23) | |
1521 | ||
1522 | def test_invalid_invert(self): | |
1523 | self._test_invalid_op(lambda: ~self._def) | |
1524 | ||
b0bdda42 JG |
1525 | def test_str_op(self): |
1526 | self.assertEqual(str(self._def), str(self._def_value)) | |
1527 | ||
9cf643d1 | 1528 | |
fe4df857 | 1529 | _inject_numeric_testing_methods(DoubleRealFieldTestCase) |
9cf643d1 PP |
1530 | |
1531 | ||
1eccc498 | 1532 | class StringFieldTestCase(unittest.TestCase): |
f0a42b33 FD |
1533 | @staticmethod |
1534 | def _const_value_setter(field): | |
f5567ea8 | 1535 | field.value = "Hello, World!" |
f0a42b33 | 1536 | |
9cf643d1 | 1537 | def setUp(self): |
1eccc498 | 1538 | self._tc = get_default_trace_class() |
f5567ea8 | 1539 | self._def_value = "Hello, World!" |
1eccc498 | 1540 | self._def = _create_string_field(self._tc) |
6d9a6c9e | 1541 | self._def_const = create_const_field( |
f0a42b33 FD |
1542 | self._tc, self._tc.create_string_field_class(), self._const_value_setter |
1543 | ) | |
9cf643d1 | 1544 | self._def.value = self._def_value |
f5567ea8 | 1545 | self._def_new_value = "Yes!" |
9cf643d1 PP |
1546 | |
1547 | def test_assign_int(self): | |
1548 | with self.assertRaises(TypeError): | |
1549 | self._def.value = 283 | |
1550 | ||
9cf643d1 | 1551 | def test_assign_string_field(self): |
1eccc498 | 1552 | field = _create_string_field(self._tc) |
f5567ea8 | 1553 | raw = "zorg" |
9cf643d1 PP |
1554 | field.value = raw |
1555 | self.assertEqual(field, raw) | |
1556 | ||
1557 | def test_eq(self): | |
1558 | self.assertEqual(self._def, self._def_value) | |
1559 | ||
f0a42b33 FD |
1560 | def test_const_eq(self): |
1561 | self.assertEqual(self._def_const, self._def_value) | |
1562 | ||
1eccc498 | 1563 | def test_not_eq(self): |
9cf643d1 PP |
1564 | self.assertNotEqual(self._def, 23) |
1565 | ||
1566 | def test_lt_vstring(self): | |
1eccc498 | 1567 | s1 = _create_string_field(self._tc) |
f5567ea8 | 1568 | s1.value = "allo" |
1eccc498 | 1569 | s2 = _create_string_field(self._tc) |
f5567ea8 | 1570 | s2.value = "bateau" |
9cf643d1 PP |
1571 | self.assertLess(s1, s2) |
1572 | ||
1573 | def test_lt_string(self): | |
1eccc498 | 1574 | s1 = _create_string_field(self._tc) |
f5567ea8 FD |
1575 | s1.value = "allo" |
1576 | self.assertLess(s1, "bateau") | |
9cf643d1 PP |
1577 | |
1578 | def test_le_vstring(self): | |
1eccc498 | 1579 | s1 = _create_string_field(self._tc) |
f5567ea8 | 1580 | s1.value = "allo" |
1eccc498 | 1581 | s2 = _create_string_field(self._tc) |
f5567ea8 | 1582 | s2.value = "bateau" |
9cf643d1 PP |
1583 | self.assertLessEqual(s1, s2) |
1584 | ||
1585 | def test_le_string(self): | |
1eccc498 | 1586 | s1 = _create_string_field(self._tc) |
f5567ea8 FD |
1587 | s1.value = "allo" |
1588 | self.assertLessEqual(s1, "bateau") | |
9cf643d1 PP |
1589 | |
1590 | def test_gt_vstring(self): | |
1eccc498 | 1591 | s1 = _create_string_field(self._tc) |
f5567ea8 | 1592 | s1.value = "allo" |
1eccc498 | 1593 | s2 = _create_string_field(self._tc) |
f5567ea8 | 1594 | s2.value = "bateau" |
9cf643d1 PP |
1595 | self.assertGreater(s2, s1) |
1596 | ||
1597 | def test_gt_string(self): | |
1eccc498 | 1598 | s1 = _create_string_field(self._tc) |
f5567ea8 FD |
1599 | s1.value = "allo" |
1600 | self.assertGreater("bateau", s1) | |
9cf643d1 PP |
1601 | |
1602 | def test_ge_vstring(self): | |
1eccc498 | 1603 | s1 = _create_string_field(self._tc) |
f5567ea8 | 1604 | s1.value = "allo" |
1eccc498 | 1605 | s2 = _create_string_field(self._tc) |
f5567ea8 | 1606 | s2.value = "bateau" |
9cf643d1 PP |
1607 | self.assertGreaterEqual(s2, s1) |
1608 | ||
1609 | def test_ge_string(self): | |
1eccc498 | 1610 | s1 = _create_string_field(self._tc) |
f5567ea8 FD |
1611 | s1.value = "allo" |
1612 | self.assertGreaterEqual("bateau", s1) | |
9cf643d1 PP |
1613 | |
1614 | def test_bool_op(self): | |
1615 | self.assertEqual(bool(self._def), bool(self._def_value)) | |
1616 | ||
1617 | def test_str_op(self): | |
1618 | self.assertEqual(str(self._def), str(self._def_value)) | |
1619 | ||
1620 | def test_len(self): | |
1621 | self.assertEqual(len(self._def), len(self._def_value)) | |
1622 | ||
1623 | def test_getitem(self): | |
1624 | self.assertEqual(self._def[5], self._def_value[5]) | |
1625 | ||
f0a42b33 FD |
1626 | def test_const_getitem(self): |
1627 | self.assertEqual(self._def_const[5], self._def_value[5]) | |
1628 | ||
9cf643d1 | 1629 | def test_append_str(self): |
f5567ea8 | 1630 | to_append = "meow meow meow" |
9cf643d1 PP |
1631 | self._def += to_append |
1632 | self._def_value += to_append | |
1633 | self.assertEqual(self._def, self._def_value) | |
1634 | ||
f0a42b33 | 1635 | def test_const_append_str(self): |
f5567ea8 | 1636 | to_append = "meow meow meow" |
f0a42b33 FD |
1637 | with self.assertRaises(TypeError): |
1638 | self._def_const += to_append | |
1639 | self.assertEqual(self._def_const, self._def_value) | |
1640 | ||
9cf643d1 | 1641 | def test_append_string_field(self): |
1eccc498 | 1642 | field = _create_string_field(self._tc) |
f5567ea8 | 1643 | to_append = "meow meow meow" |
9cf643d1 PP |
1644 | field.value = to_append |
1645 | self._def += field | |
1646 | self._def_value += to_append | |
1647 | self.assertEqual(self._def, self._def_value) | |
1648 | ||
dda659b3 FD |
1649 | def test_hash_op(self): |
1650 | with self.assertRaises(TypeError): | |
1651 | hash(self._def) | |
1652 | ||
1653 | def test_const_hash_op(self): | |
1654 | self.assertEqual(hash(self._def_const), hash(self._def_value)) | |
1655 | ||
1656 | def test_const_hash_dict(self): | |
1657 | my_dict = {} | |
f5567ea8 FD |
1658 | my_dict[self._def_const] = "my_value" |
1659 | self.assertEqual(my_dict[self._def_value], "my_value") | |
dda659b3 | 1660 | |
742e4747 | 1661 | |
1eccc498 | 1662 | class _TestArrayFieldCommon: |
9cf643d1 PP |
1663 | def _modify_def(self): |
1664 | self._def[2] = 23 | |
1665 | ||
1666 | def test_bool_op_true(self): | |
1667 | self.assertTrue(self._def) | |
1668 | ||
1669 | def test_len(self): | |
1670 | self.assertEqual(len(self._def), 3) | |
1671 | ||
1eccc498 SM |
1672 | def test_length(self): |
1673 | self.assertEqual(self._def.length, 3) | |
1674 | ||
9cf643d1 PP |
1675 | def test_getitem(self): |
1676 | field = self._def[1] | |
3fb99a22 | 1677 | self.assertIs(type(field), bt2._SignedIntegerField) |
9cf643d1 PP |
1678 | self.assertEqual(field, 1847) |
1679 | ||
f0a42b33 FD |
1680 | def test_const_getitem(self): |
1681 | field = self._def_const[1] | |
1682 | self.assertIs(type(field), bt2._SignedIntegerFieldConst) | |
1683 | self.assertEqual(field, 1847) | |
1684 | ||
9cf643d1 | 1685 | def test_eq(self): |
1eccc498 | 1686 | field = _create_int_array_field(self._tc, 3) |
9cf643d1 PP |
1687 | field[0] = 45 |
1688 | field[1] = 1847 | |
1689 | field[2] = 1948754 | |
1690 | self.assertEqual(self._def, field) | |
1691 | ||
1692 | def test_eq_invalid_type(self): | |
1693 | self.assertNotEqual(self._def, 23) | |
1694 | ||
1695 | def test_eq_diff_len(self): | |
1eccc498 | 1696 | field = _create_int_array_field(self._tc, 2) |
9cf643d1 PP |
1697 | field[0] = 45 |
1698 | field[1] = 1847 | |
1699 | self.assertNotEqual(self._def, field) | |
1700 | ||
1701 | def test_eq_diff_content_same_len(self): | |
1eccc498 | 1702 | field = _create_int_array_field(self._tc, 3) |
9cf643d1 PP |
1703 | field[0] = 45 |
1704 | field[1] = 1846 | |
1705 | field[2] = 1948754 | |
1706 | self.assertNotEqual(self._def, field) | |
1707 | ||
73051d46 PP |
1708 | def test_eq_non_sequence_iterable(self): |
1709 | dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)]) | |
1710 | field = _create_int_array_field(self._tc, 3) | |
1711 | field[0] = 1 | |
1712 | field[1] = 3 | |
1713 | field[2] = 5 | |
1714 | self.assertEqual(field, list(dct.keys())) | |
1715 | self.assertNotEqual(field, dct) | |
1716 | ||
9cf643d1 PP |
1717 | def test_setitem(self): |
1718 | self._def[2] = 24 | |
1719 | self.assertEqual(self._def[2], 24) | |
1720 | ||
1721 | def test_setitem_int_field(self): | |
1eccc498 SM |
1722 | int_fc = self._tc.create_signed_integer_field_class(32) |
1723 | int_field = _create_field(self._tc, int_fc) | |
9cf643d1 PP |
1724 | int_field.value = 19487 |
1725 | self._def[1] = int_field | |
1726 | self.assertEqual(self._def[1], 19487) | |
1727 | ||
1728 | def test_setitem_non_basic_field(self): | |
1eccc498 | 1729 | array_field = _create_struct_array_field(self._tc, 2) |
9cf643d1 PP |
1730 | with self.assertRaises(TypeError): |
1731 | array_field[1] = 23 | |
1732 | ||
1733 | def test_setitem_none(self): | |
1734 | with self.assertRaises(TypeError): | |
1735 | self._def[1] = None | |
1736 | ||
1737 | def test_setitem_index_wrong_type(self): | |
1738 | with self.assertRaises(TypeError): | |
f5567ea8 | 1739 | self._def["yes"] = 23 |
9cf643d1 PP |
1740 | |
1741 | def test_setitem_index_neg(self): | |
1742 | with self.assertRaises(IndexError): | |
1743 | self._def[-2] = 23 | |
1744 | ||
1745 | def test_setitem_index_out_of_range(self): | |
1746 | with self.assertRaises(IndexError): | |
1747 | self._def[len(self._def)] = 134679 | |
1748 | ||
f0a42b33 FD |
1749 | def test_const_setitem(self): |
1750 | with self.assertRaises(TypeError): | |
1751 | self._def_const[0] = 134679 | |
1752 | ||
9cf643d1 PP |
1753 | def test_iter(self): |
1754 | for field, value in zip(self._def, (45, 1847, 1948754)): | |
1755 | self.assertEqual(field, value) | |
1756 | ||
f0a42b33 FD |
1757 | def test_const_iter(self): |
1758 | for field, value in zip(self._def_const, (45, 1847, 1948754)): | |
1759 | self.assertEqual(field, value) | |
1760 | ||
7c54e2e7 JG |
1761 | def test_value_int_field(self): |
1762 | values = [45646, 145, 12145] | |
1763 | self._def.value = values | |
1764 | self.assertEqual(values, self._def) | |
1765 | ||
7c54e2e7 JG |
1766 | def test_value_check_sequence(self): |
1767 | values = 42 | |
1768 | with self.assertRaises(TypeError): | |
1769 | self._def.value = values | |
1770 | ||
1771 | def test_value_wrong_type_in_sequence(self): | |
f5567ea8 | 1772 | values = [32, "hello", 11] |
7c54e2e7 JG |
1773 | with self.assertRaises(TypeError): |
1774 | self._def.value = values | |
1775 | ||
1776 | def test_value_complex_type(self): | |
1eccc498 SM |
1777 | struct_fc = self._tc.create_structure_field_class() |
1778 | int_fc = self._tc.create_signed_integer_field_class(32) | |
1779 | another_int_fc = self._tc.create_signed_integer_field_class(32) | |
1780 | str_fc = self._tc.create_string_field_class() | |
f5567ea8 FD |
1781 | struct_fc.append_member(field_class=int_fc, name="an_int") |
1782 | struct_fc.append_member(field_class=str_fc, name="a_string") | |
1783 | struct_fc.append_member(field_class=another_int_fc, name="another_int") | |
1eccc498 | 1784 | array_fc = self._tc.create_static_array_field_class(struct_fc, 3) |
f5567ea8 | 1785 | stream = _create_stream(self._tc, [("array_field", array_fc)]) |
7c54e2e7 | 1786 | values = [ |
f5567ea8 FD |
1787 | {"an_int": 42, "a_string": "hello", "another_int": 66}, |
1788 | {"an_int": 1, "a_string": "goodbye", "another_int": 488}, | |
1789 | {"an_int": 156, "a_string": "or not", "another_int": 4648}, | |
7c54e2e7 JG |
1790 | ] |
1791 | ||
f5567ea8 | 1792 | array = stream.create_packet().context_field["array_field"] |
7c54e2e7 JG |
1793 | array.value = values |
1794 | self.assertEqual(values, array) | |
f5567ea8 | 1795 | values[0]["an_int"] = "a string" |
7c54e2e7 JG |
1796 | with self.assertRaises(TypeError): |
1797 | array.value = values | |
9cf643d1 | 1798 | |
b0bdda42 JG |
1799 | def test_str_op(self): |
1800 | s = str(self._def) | |
f5567ea8 | 1801 | expected_string = "[{}]".format(", ".join([repr(v) for v in self._def_value])) |
b0bdda42 JG |
1802 | self.assertEqual(expected_string, s) |
1803 | ||
742e4747 | 1804 | |
1eccc498 | 1805 | class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): |
f0a42b33 FD |
1806 | @staticmethod |
1807 | def _const_value_setter(field): | |
1808 | field.value = [45, 1847, 1948754] | |
1809 | ||
9cf643d1 | 1810 | def setUp(self): |
1eccc498 SM |
1811 | self._tc = get_default_trace_class() |
1812 | self._def = _create_int_array_field(self._tc, 3) | |
9cf643d1 PP |
1813 | self._def[0] = 45 |
1814 | self._def[1] = 1847 | |
1815 | self._def[2] = 1948754 | |
7c54e2e7 | 1816 | self._def_value = [45, 1847, 1948754] |
6d9a6c9e | 1817 | self._def_const = create_const_field( |
f0a42b33 FD |
1818 | self._tc, |
1819 | self._tc.create_static_array_field_class( | |
1820 | self._tc.create_signed_integer_field_class(32), 3 | |
1821 | ), | |
1822 | self._const_value_setter, | |
1823 | ) | |
9cf643d1 | 1824 | |
7c54e2e7 JG |
1825 | def test_value_wrong_len(self): |
1826 | values = [45, 1847] | |
1827 | with self.assertRaises(ValueError): | |
1828 | self._def.value = values | |
1829 | ||
9cf643d1 | 1830 | |
1eccc498 | 1831 | class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): |
f0a42b33 FD |
1832 | @staticmethod |
1833 | def _const_value_setter(field): | |
1834 | field.value = [45, 1847, 1948754] | |
1835 | ||
9cf643d1 | 1836 | def setUp(self): |
1eccc498 SM |
1837 | self._tc = get_default_trace_class() |
1838 | self._def = _create_dynamic_array(self._tc) | |
9cf643d1 PP |
1839 | self._def[0] = 45 |
1840 | self._def[1] = 1847 | |
1841 | self._def[2] = 1948754 | |
7c54e2e7 | 1842 | self._def_value = [45, 1847, 1948754] |
6d9a6c9e | 1843 | self._def_const = create_const_field( |
f0a42b33 FD |
1844 | self._tc, |
1845 | self._tc.create_dynamic_array_field_class( | |
1846 | self._tc.create_signed_integer_field_class(32) | |
1847 | ), | |
1848 | self._const_value_setter, | |
1849 | ) | |
9cf643d1 | 1850 | |
7c54e2e7 JG |
1851 | def test_value_resize(self): |
1852 | new_values = [1, 2, 3, 4] | |
1853 | self._def.value = new_values | |
1854 | self.assertCountEqual(self._def, new_values) | |
1855 | ||
1eccc498 SM |
1856 | def test_set_length(self): |
1857 | self._def.length = 4 | |
1858 | self._def[3] = 0 | |
1859 | self.assertEqual(len(self._def), 4) | |
7c54e2e7 | 1860 | |
f0a42b33 FD |
1861 | def test_const_set_length(self): |
1862 | with self.assertRaises(AttributeError): | |
1863 | self._def_const.length = 4 | |
1864 | self.assertEqual(len(self._def), 3) | |
1865 | ||
1eccc498 | 1866 | def test_set_invalid_length(self): |
7c54e2e7 | 1867 | with self.assertRaises(TypeError): |
f5567ea8 | 1868 | self._def.length = "cheval" |
1eccc498 SM |
1869 | |
1870 | ||
1871 | class StructureFieldTestCase(unittest.TestCase): | |
f0a42b33 FD |
1872 | @staticmethod |
1873 | def _const_value_setter(field): | |
1874 | field.value = { | |
f5567ea8 FD |
1875 | "A": -1872, |
1876 | "B": "salut", | |
1877 | "C": 17.5, | |
1878 | "D": 16497, | |
1879 | "E": {}, | |
1880 | "F": {"F_1": 52}, | |
f0a42b33 FD |
1881 | } |
1882 | ||
1eccc498 SM |
1883 | def _create_fc(self, tc): |
1884 | fc = tc.create_structure_field_class() | |
f5567ea8 FD |
1885 | fc.append_member("A", self._fc0_fn()) |
1886 | fc.append_member("B", self._fc1_fn()) | |
1887 | fc.append_member("C", self._fc2_fn()) | |
1888 | fc.append_member("D", self._fc3_fn()) | |
1889 | fc.append_member("E", self._fc4_fn()) | |
1eccc498 | 1890 | fc5 = self._fc5_fn() |
f5567ea8 FD |
1891 | fc5.append_member("F_1", self._fc5_inner_fn()) |
1892 | fc.append_member("F", fc5) | |
1eccc498 | 1893 | return fc |
7c54e2e7 | 1894 | |
9cf643d1 | 1895 | def setUp(self): |
1eccc498 SM |
1896 | self._tc = get_default_trace_class() |
1897 | self._fc0_fn = self._tc.create_signed_integer_field_class | |
1898 | self._fc1_fn = self._tc.create_string_field_class | |
fe4df857 | 1899 | self._fc2_fn = self._tc.create_double_precision_real_field_class |
1eccc498 SM |
1900 | self._fc3_fn = self._tc.create_signed_integer_field_class |
1901 | self._fc4_fn = self._tc.create_structure_field_class | |
1902 | self._fc5_fn = self._tc.create_structure_field_class | |
1903 | self._fc5_inner_fn = self._tc.create_signed_integer_field_class | |
1904 | ||
1905 | self._fc = self._create_fc(self._tc) | |
1906 | self._def = _create_field(self._tc, self._fc) | |
f5567ea8 FD |
1907 | self._def["A"] = -1872 |
1908 | self._def["B"] = "salut" | |
1909 | self._def["C"] = 17.5 | |
1910 | self._def["D"] = 16497 | |
1911 | self._def["E"] = {} | |
1912 | self._def["F"] = {"F_1": 52} | |
b0bdda42 | 1913 | self._def_value = { |
f5567ea8 FD |
1914 | "A": -1872, |
1915 | "B": "salut", | |
1916 | "C": 17.5, | |
1917 | "D": 16497, | |
1918 | "E": {}, | |
1919 | "F": {"F_1": 52}, | |
b0bdda42 | 1920 | } |
9cf643d1 | 1921 | |
6d9a6c9e | 1922 | self._def_const = create_const_field( |
f0a42b33 FD |
1923 | self._tc, self._create_fc(self._tc), self._const_value_setter |
1924 | ) | |
1925 | ||
9cf643d1 | 1926 | def _modify_def(self): |
f5567ea8 | 1927 | self._def["B"] = "hola" |
9cf643d1 PP |
1928 | |
1929 | def test_bool_op_true(self): | |
1930 | self.assertTrue(self._def) | |
1931 | ||
1932 | def test_bool_op_false(self): | |
f5567ea8 | 1933 | field = self._def["E"] |
9cf643d1 PP |
1934 | self.assertFalse(field) |
1935 | ||
1936 | def test_len(self): | |
1eccc498 | 1937 | self.assertEqual(len(self._def), len(self._def_value)) |
9cf643d1 PP |
1938 | |
1939 | def test_getitem(self): | |
f5567ea8 FD |
1940 | field1 = self._def["A"] |
1941 | field2 = self._def["B"] | |
1942 | field3 = self._def["C"] | |
1943 | field4 = self._def["D"] | |
1944 | field5 = self._def["E"] | |
1945 | field6 = self._def["F"] | |
f0a42b33 FD |
1946 | |
1947 | self.assertIs(type(field1), bt2._SignedIntegerField) | |
1948 | self.assertEqual(field1, -1872) | |
1949 | ||
1950 | self.assertIs(type(field2), bt2._StringField) | |
f5567ea8 | 1951 | self.assertEqual(field2, "salut") |
f0a42b33 | 1952 | |
fe4df857 | 1953 | self.assertIs(type(field3), bt2._DoublePrecisionRealField) |
f0a42b33 FD |
1954 | self.assertEqual(field3, 17.5) |
1955 | ||
1956 | self.assertIs(type(field4), bt2._SignedIntegerField) | |
1957 | self.assertEqual(field4, 16497) | |
1958 | ||
1959 | self.assertIs(type(field5), bt2._StructureField) | |
1960 | self.assertEqual(field5, {}) | |
1961 | ||
1962 | self.assertIs(type(field6), bt2._StructureField) | |
f5567ea8 | 1963 | self.assertEqual(field6, {"F_1": 52}) |
f0a42b33 FD |
1964 | |
1965 | def test_const_getitem(self): | |
f5567ea8 FD |
1966 | field1 = self._def_const["A"] |
1967 | field2 = self._def_const["B"] | |
1968 | field3 = self._def_const["C"] | |
1969 | field4 = self._def_const["D"] | |
1970 | field5 = self._def_const["E"] | |
1971 | field6 = self._def_const["F"] | |
f0a42b33 FD |
1972 | |
1973 | self.assertIs(type(field1), bt2._SignedIntegerFieldConst) | |
1974 | self.assertEqual(field1, -1872) | |
1975 | ||
1976 | self.assertIs(type(field2), bt2._StringFieldConst) | |
f5567ea8 | 1977 | self.assertEqual(field2, "salut") |
f0a42b33 | 1978 | |
fe4df857 | 1979 | self.assertIs(type(field3), bt2._DoublePrecisionRealFieldConst) |
f0a42b33 FD |
1980 | self.assertEqual(field3, 17.5) |
1981 | ||
1982 | self.assertIs(type(field4), bt2._SignedIntegerFieldConst) | |
1983 | self.assertEqual(field4, 16497) | |
1984 | ||
1985 | self.assertIs(type(field5), bt2._StructureFieldConst) | |
1986 | self.assertEqual(field5, {}) | |
1987 | ||
1988 | self.assertIs(type(field6), bt2._StructureFieldConst) | |
f5567ea8 | 1989 | self.assertEqual(field6, {"F_1": 52}) |
9cf643d1 | 1990 | |
1eccc498 | 1991 | def test_member_at_index_out_of_bounds_after(self): |
811644b8 | 1992 | with self.assertRaises(IndexError): |
1eccc498 | 1993 | self._def.member_at_index(len(self._def_value)) |
811644b8 | 1994 | |
9cf643d1 | 1995 | def test_eq(self): |
cfbd7cf3 | 1996 | field = _create_field(self._tc, self._create_fc(self._tc)) |
f5567ea8 FD |
1997 | field["A"] = -1872 |
1998 | field["B"] = "salut" | |
1999 | field["C"] = 17.5 | |
2000 | field["D"] = 16497 | |
2001 | field["E"] = {} | |
2002 | field["F"] = {"F_1": 52} | |
9cf643d1 PP |
2003 | self.assertEqual(self._def, field) |
2004 | ||
f0a42b33 FD |
2005 | def test_const_eq(self): |
2006 | field = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 FD |
2007 | field["A"] = -1872 |
2008 | field["B"] = "salut" | |
2009 | field["C"] = 17.5 | |
2010 | field["D"] = 16497 | |
2011 | field["E"] = {} | |
2012 | field["F"] = {"F_1": 52} | |
f0a42b33 FD |
2013 | self.assertEqual(self._def_const, field) |
2014 | ||
9cf643d1 PP |
2015 | def test_eq_invalid_type(self): |
2016 | self.assertNotEqual(self._def, 23) | |
2017 | ||
2018 | def test_eq_diff_len(self): | |
1eccc498 | 2019 | fc = self._tc.create_structure_field_class() |
f5567ea8 FD |
2020 | fc.append_member("A", self._fc0_fn()) |
2021 | fc.append_member("B", self._fc1_fn()) | |
2022 | fc.append_member("C", self._fc2_fn()) | |
1eccc498 SM |
2023 | |
2024 | field = _create_field(self._tc, fc) | |
f5567ea8 FD |
2025 | field["A"] = -1872 |
2026 | field["B"] = "salut" | |
2027 | field["C"] = 17.5 | |
9cf643d1 PP |
2028 | self.assertNotEqual(self._def, field) |
2029 | ||
1eccc498 SM |
2030 | def test_eq_diff_keys(self): |
2031 | fc = self._tc.create_structure_field_class() | |
f5567ea8 FD |
2032 | fc.append_member("U", self._fc0_fn()) |
2033 | fc.append_member("V", self._fc1_fn()) | |
2034 | fc.append_member("W", self._fc2_fn()) | |
2035 | fc.append_member("X", self._fc3_fn()) | |
2036 | fc.append_member("Y", self._fc4_fn()) | |
2037 | fc.append_member("Z", self._fc5_fn()) | |
1eccc498 | 2038 | field = _create_field(self._tc, fc) |
f5567ea8 FD |
2039 | field["U"] = -1871 |
2040 | field["V"] = "gerry" | |
2041 | field["W"] = 18.19 | |
2042 | field["X"] = 16497 | |
2043 | field["Y"] = {} | |
2044 | field["Z"] = {} | |
1eccc498 SM |
2045 | self.assertNotEqual(self._def, field) |
2046 | ||
9cf643d1 | 2047 | def test_eq_diff_content_same_len(self): |
1eccc498 | 2048 | field = _create_field(self._tc, self._create_fc(self._tc)) |
f5567ea8 FD |
2049 | field["A"] = -1872 |
2050 | field["B"] = "salut" | |
2051 | field["C"] = 17.4 | |
2052 | field["D"] = 16497 | |
2053 | field["E"] = {} | |
2054 | field["F"] = {"F_1": 0} | |
9cf643d1 PP |
2055 | self.assertNotEqual(self._def, field) |
2056 | ||
2057 | def test_eq_same_content_diff_keys(self): | |
1eccc498 | 2058 | fc = self._tc.create_structure_field_class() |
f5567ea8 FD |
2059 | fc.append_member("A", self._fc0_fn()) |
2060 | fc.append_member("B", self._fc1_fn()) | |
2061 | fc.append_member("E", self._fc2_fn()) | |
2062 | fc.append_member("D", self._fc3_fn()) | |
2063 | fc.append_member("C", self._fc4_fn()) | |
2064 | fc.append_member("F", self._fc5_fn()) | |
1eccc498 | 2065 | field = _create_field(self._tc, fc) |
f5567ea8 FD |
2066 | field["A"] = -1872 |
2067 | field["B"] = "salut" | |
2068 | field["E"] = 17.5 | |
2069 | field["D"] = 16497 | |
2070 | field["C"] = {} | |
2071 | field["F"] = {} | |
9cf643d1 PP |
2072 | self.assertNotEqual(self._def, field) |
2073 | ||
2074 | def test_setitem(self): | |
f5567ea8 FD |
2075 | self._def["C"] = -18.47 |
2076 | self.assertEqual(self._def["C"], -18.47) | |
9cf643d1 | 2077 | |
f0a42b33 FD |
2078 | def test_const_setitem(self): |
2079 | with self.assertRaises(TypeError): | |
f5567ea8 | 2080 | self._def_const["A"] = 134679 |
f0a42b33 | 2081 | |
9cf643d1 | 2082 | def test_setitem_int_field(self): |
1eccc498 SM |
2083 | int_fc = self._tc.create_signed_integer_field_class(32) |
2084 | int_field = _create_field(self._tc, int_fc) | |
9cf643d1 | 2085 | int_field.value = 19487 |
f5567ea8 FD |
2086 | self._def["D"] = int_field |
2087 | self.assertEqual(self._def["D"], 19487) | |
9cf643d1 PP |
2088 | |
2089 | def test_setitem_non_basic_field(self): | |
1eccc498 SM |
2090 | elem_fc = self._tc.create_structure_field_class() |
2091 | struct_fc = self._tc.create_structure_field_class() | |
f5567ea8 | 2092 | struct_fc.append_member("A", elem_fc) |
1eccc498 | 2093 | struct_field = _create_field(self._tc, struct_fc) |
9cf643d1 | 2094 | |
236355c2 JG |
2095 | # Will fail on access to .items() of the value |
2096 | with self.assertRaises(AttributeError): | |
f5567ea8 | 2097 | struct_field["A"] = 23 |
9cf643d1 PP |
2098 | |
2099 | def test_setitem_none(self): | |
2100 | with self.assertRaises(TypeError): | |
f5567ea8 | 2101 | self._def["C"] = None |
9cf643d1 PP |
2102 | |
2103 | def test_setitem_key_wrong_type(self): | |
2104 | with self.assertRaises(TypeError): | |
2105 | self._def[3] = 23 | |
2106 | ||
2107 | def test_setitem_wrong_key(self): | |
2108 | with self.assertRaises(KeyError): | |
f5567ea8 | 2109 | self._def["hi"] = 134679 |
9cf643d1 | 2110 | |
1eccc498 | 2111 | def test_member_at_index(self): |
f5567ea8 | 2112 | self.assertEqual(self._def.member_at_index(1), "salut") |
9cf643d1 | 2113 | |
f0a42b33 | 2114 | def test_const_member_at_index(self): |
f5567ea8 | 2115 | self.assertEqual(self._def_const.member_at_index(1), "salut") |
f0a42b33 | 2116 | |
9cf643d1 PP |
2117 | def test_iter(self): |
2118 | orig_values = { | |
f5567ea8 FD |
2119 | "A": -1872, |
2120 | "B": "salut", | |
2121 | "C": 17.5, | |
2122 | "D": 16497, | |
2123 | "E": {}, | |
2124 | "F": {"F_1": 52}, | |
9cf643d1 PP |
2125 | } |
2126 | ||
2127 | for vkey, vval in self._def.items(): | |
2128 | val = orig_values[vkey] | |
2129 | self.assertEqual(vval, val) | |
811644b8 | 2130 | |
7c54e2e7 JG |
2131 | def test_value(self): |
2132 | orig_values = { | |
f5567ea8 FD |
2133 | "A": -1872, |
2134 | "B": "salut", | |
2135 | "C": 17.5, | |
2136 | "D": 16497, | |
2137 | "E": {}, | |
2138 | "F": {"F_1": 52}, | |
7c54e2e7 JG |
2139 | } |
2140 | self.assertEqual(self._def, orig_values) | |
2141 | ||
2142 | def test_set_value(self): | |
1eccc498 SM |
2143 | int_fc = self._tc.create_signed_integer_field_class(32) |
2144 | another_int_fc = self._tc.create_signed_integer_field_class(32) | |
2145 | str_fc = self._tc.create_string_field_class() | |
2146 | struct_fc = self._tc.create_structure_field_class() | |
f5567ea8 FD |
2147 | struct_fc.append_member(field_class=int_fc, name="an_int") |
2148 | struct_fc.append_member(field_class=str_fc, name="a_string") | |
2149 | struct_fc.append_member(field_class=another_int_fc, name="another_int") | |
2150 | values = {"an_int": 42, "a_string": "hello", "another_int": 66} | |
7c54e2e7 | 2151 | |
1eccc498 | 2152 | struct = _create_field(self._tc, struct_fc) |
7c54e2e7 JG |
2153 | struct.value = values |
2154 | self.assertEqual(values, struct) | |
2155 | ||
2156 | bad_type_values = copy.deepcopy(values) | |
f5567ea8 | 2157 | bad_type_values["an_int"] = "a string" |
7c54e2e7 JG |
2158 | with self.assertRaises(TypeError): |
2159 | struct.value = bad_type_values | |
2160 | ||
2161 | unknown_key_values = copy.deepcopy(values) | |
f5567ea8 | 2162 | unknown_key_values["unknown_key"] = 16546 |
7c54e2e7 JG |
2163 | with self.assertRaises(KeyError): |
2164 | struct.value = unknown_key_values | |
2165 | ||
b0bdda42 JG |
2166 | def test_str_op(self): |
2167 | expected_string_found = False | |
2168 | s = str(self._def) | |
2169 | # Establish all permutations of the three expected matches since | |
2170 | # the order in which mappings are enumerated is not explicitly part of | |
2171 | # the API. | |
2172 | for p in itertools.permutations([(k, v) for k, v in self._def.items()]): | |
f5567ea8 FD |
2173 | items = ["{}: {}".format(repr(k), repr(v)) for k, v in p] |
2174 | candidate = "{{{}}}".format(", ".join(items)) | |
b0bdda42 JG |
2175 | if candidate == s: |
2176 | expected_string_found = True | |
2177 | break | |
2178 | ||
2179 | self.assertTrue(expected_string_found) | |
2180 | ||
b0bdda42 | 2181 | |
cec0261d | 2182 | class OptionFieldTestCase(unittest.TestCase): |
f0a42b33 FD |
2183 | @staticmethod |
2184 | def _const_value_setter(field): | |
f5567ea8 | 2185 | field.value = {"opt_field": "hiboux"} |
f0a42b33 | 2186 | |
cec0261d | 2187 | def _create_fc(self, tc): |
0aa006b7 PP |
2188 | fc = tc.create_option_without_selector_field_class( |
2189 | tc.create_string_field_class() | |
2190 | ) | |
cec0261d | 2191 | top_fc = tc.create_structure_field_class() |
f5567ea8 | 2192 | top_fc.append_member("opt_field", fc) |
cec0261d PP |
2193 | return top_fc |
2194 | ||
2195 | def setUp(self): | |
2196 | self._tc = get_default_trace_class() | |
2197 | fld = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 FD |
2198 | self._def = fld["opt_field"] |
2199 | self._def_value = "hiboux" | |
6d9a6c9e | 2200 | self._def_const = create_const_field( |
f0a42b33 | 2201 | self._tc, self._create_fc(self._tc), self._const_value_setter |
f5567ea8 | 2202 | )["opt_field"] |
cec0261d PP |
2203 | |
2204 | def test_value_prop(self): | |
f5567ea8 FD |
2205 | self._def.value = "hiboux" |
2206 | self.assertEqual(self._def.field, "hiboux") | |
f0a42b33 FD |
2207 | self.assertIs(type(self._def), bt2._OptionField) |
2208 | self.assertIs(type(self._def.field), bt2._StringField) | |
cec0261d PP |
2209 | self.assertTrue(self._def.has_field) |
2210 | ||
f0a42b33 | 2211 | def test_const_value_prop(self): |
f5567ea8 | 2212 | self.assertEqual(self._def_const.field, "hiboux") |
f0a42b33 FD |
2213 | self.assertIs(type(self._def_const), bt2._OptionFieldConst) |
2214 | self.assertIs(type(self._def_const.field), bt2._StringFieldConst) | |
2215 | self.assertTrue(self._def_const.has_field) | |
2216 | ||
cec0261d PP |
2217 | def test_has_field_prop_true(self): |
2218 | self._def.has_field = True | |
2219 | self.assertTrue(self._def.has_field) | |
2220 | ||
d4db9beb | 2221 | def test_has_field_prop_false(self): |
cec0261d PP |
2222 | self._def.has_field = False |
2223 | self.assertFalse(self._def.has_field) | |
2224 | ||
2225 | def test_bool_op_true(self): | |
f5567ea8 | 2226 | self._def.value = "allo" |
cec0261d PP |
2227 | self.assertTrue(self._def) |
2228 | ||
d4db9beb | 2229 | def test_bool_op_false(self): |
cec0261d PP |
2230 | self._def.has_field = False |
2231 | self.assertFalse(self._def) | |
2232 | ||
2233 | def test_field_prop_existing(self): | |
f5567ea8 | 2234 | self._def.value = "meow" |
cec0261d | 2235 | field = self._def.field |
f5567ea8 | 2236 | self.assertEqual(field, "meow") |
cec0261d PP |
2237 | |
2238 | def test_field_prop_none(self): | |
2239 | self._def.has_field = False | |
2240 | field = self._def.field | |
2241 | self.assertIsNone(field) | |
2242 | ||
f0a42b33 FD |
2243 | def test_const_field_prop(self): |
2244 | with self.assertRaises(AttributeError): | |
2245 | self._def_const.has_field = False | |
2246 | ||
2247 | self.assertEqual(self._def_const, self._def_value) | |
2248 | self.assertTrue(self._def_const.has_field) | |
2249 | ||
cec0261d | 2250 | def test_field_prop_existing_then_none(self): |
f5567ea8 | 2251 | self._def.value = "meow" |
cec0261d | 2252 | field = self._def.field |
f5567ea8 | 2253 | self.assertEqual(field, "meow") |
cec0261d PP |
2254 | self._def.has_field = False |
2255 | field = self._def.field | |
2256 | self.assertIsNone(field) | |
2257 | ||
2258 | def test_eq(self): | |
2259 | field = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 FD |
2260 | field = field["opt_field"] |
2261 | field.value = "walk" | |
2262 | self._def.value = "walk" | |
cec0261d PP |
2263 | self.assertEqual(self._def, field) |
2264 | ||
f0a42b33 FD |
2265 | def test_const_eq(self): |
2266 | field = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 FD |
2267 | field = field["opt_field"] |
2268 | field.value = "hiboux" | |
f0a42b33 FD |
2269 | self.assertEqual(self._def_const, field) |
2270 | self.assertEqual(self._def_const, self._def_value) | |
2271 | ||
cec0261d | 2272 | def test_eq_invalid_type(self): |
f5567ea8 | 2273 | self._def.value = "gerry" |
cec0261d PP |
2274 | self.assertNotEqual(self._def, 23) |
2275 | ||
2276 | def test_str_op(self): | |
f5567ea8 | 2277 | self._def.value = "marcel" |
cec0261d PP |
2278 | self.assertEqual(str(self._def), str(self._def.field)) |
2279 | ||
2280 | def test_repr_op(self): | |
f5567ea8 | 2281 | self._def.value = "mireille" |
cec0261d PP |
2282 | self.assertEqual(repr(self._def), repr(self._def.field)) |
2283 | ||
2284 | ||
1eccc498 | 2285 | class VariantFieldTestCase(unittest.TestCase): |
f0a42b33 FD |
2286 | @staticmethod |
2287 | def _const_value_setter(field): | |
2288 | field.selected_option_index = 3 | |
2289 | field.value = 1334 | |
2290 | ||
1eccc498 | 2291 | def _create_fc(self, tc): |
1eccc498 SM |
2292 | ft0 = tc.create_signed_integer_field_class(32) |
2293 | ft1 = tc.create_string_field_class() | |
fe4df857 | 2294 | ft2 = tc.create_double_precision_real_field_class() |
1eccc498 | 2295 | ft3 = tc.create_signed_integer_field_class(17) |
1eccc498 | 2296 | fc = tc.create_variant_field_class() |
f5567ea8 FD |
2297 | fc.append_option("corner", ft0) |
2298 | fc.append_option("zoom", ft1) | |
2299 | fc.append_option("mellotron", ft2) | |
2300 | fc.append_option("giorgio", ft3) | |
1eccc498 | 2301 | top_fc = tc.create_structure_field_class() |
f5567ea8 | 2302 | top_fc.append_member("variant_field", fc) |
1eccc498 | 2303 | return top_fc |
811644b8 | 2304 | |
811644b8 | 2305 | def setUp(self): |
1eccc498 SM |
2306 | self._tc = get_default_trace_class() |
2307 | fld = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 | 2308 | self._def = fld["variant_field"] |
811644b8 | 2309 | |
f0a42b33 FD |
2310 | self._def_value = 1334 |
2311 | self._def_selected_index = 3 | |
f5567ea8 | 2312 | const_fc = self._create_fc(self._tc)["variant_field"] |
f0a42b33 | 2313 | |
6d9a6c9e | 2314 | fld_const = create_const_field( |
f0a42b33 FD |
2315 | self._tc, const_fc.field_class, self._const_value_setter |
2316 | ) | |
2317 | self._def_const = fld_const | |
2318 | ||
1eccc498 SM |
2319 | def test_bool_op(self): |
2320 | self._def.selected_option_index = 2 | |
2321 | self._def.value = -17.34 | |
2322 | with self.assertRaises(NotImplementedError): | |
2323 | bool(self._def) | |
811644b8 | 2324 | |
1eccc498 SM |
2325 | def test_selected_option_index(self): |
2326 | self._def.selected_option_index = 2 | |
2327 | self.assertEqual(self._def.selected_option_index, 2) | |
811644b8 | 2328 | |
d431b4de FD |
2329 | def test_selected_option_index_above_range(self): |
2330 | with self.assertRaises(IndexError): | |
2331 | self._def.selected_option_index = 4 | |
2332 | ||
2333 | def test_selected_option_index_below_range(self): | |
2334 | with self.assertRaises(IndexError): | |
2335 | self._def.selected_option_index = -1 | |
2336 | ||
f0a42b33 FD |
2337 | def test_const_selected_option_index(self): |
2338 | with self.assertRaises(AttributeError): | |
2339 | self._def_const.selected_option_index = 2 | |
2340 | self.assertEqual(self._def_const.selected_option_index, 3) | |
2341 | ||
1eccc498 SM |
2342 | def test_selected_option(self): |
2343 | self._def.selected_option_index = 2 | |
2344 | self._def.value = -17.34 | |
2345 | self.assertEqual(self._def.selected_option, -17.34) | |
fe4df857 | 2346 | self.assertEqual(type(self._def.selected_option), bt2._DoublePrecisionRealField) |
1eccc498 SM |
2347 | |
2348 | self._def.selected_option_index = 3 | |
2349 | self._def.value = 1921 | |
2350 | self.assertEqual(self._def.selected_option, 1921) | |
f0a42b33 FD |
2351 | self.assertEqual(type(self._def.selected_option), bt2._SignedIntegerField) |
2352 | ||
2353 | def test_const_selected_option(self): | |
2354 | self.assertEqual(self._def_const.selected_option, 1334) | |
2355 | self.assertEqual( | |
2356 | type(self._def_const.selected_option), bt2._SignedIntegerFieldConst | |
2357 | ) | |
811644b8 PP |
2358 | |
2359 | def test_eq(self): | |
1eccc498 | 2360 | field = _create_field(self._tc, self._create_fc(self._tc)) |
f5567ea8 | 2361 | field = field["variant_field"] |
1eccc498 SM |
2362 | field.selected_option_index = 0 |
2363 | field.value = 1774 | |
2364 | self._def.selected_option_index = 0 | |
2365 | self._def.value = 1774 | |
811644b8 PP |
2366 | self.assertEqual(self._def, field) |
2367 | ||
f0a42b33 FD |
2368 | def test_const_eq(self): |
2369 | field = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 | 2370 | field = field["variant_field"] |
f0a42b33 FD |
2371 | field.selected_option_index = 3 |
2372 | field.value = 1334 | |
2373 | self.assertEqual(self._def_const, field) | |
2374 | ||
2b9aa00b FD |
2375 | def test_len(self): |
2376 | self.assertEqual(len(self._def), 4) | |
2377 | ||
811644b8 | 2378 | def test_eq_invalid_type(self): |
1eccc498 | 2379 | self._def.selected_option_index = 1 |
f5567ea8 | 2380 | self._def.value = "gerry" |
811644b8 | 2381 | self.assertNotEqual(self._def, 23) |
742e4747 | 2382 | |
b0bdda42 | 2383 | def test_str_op_int(self): |
1eccc498 | 2384 | field = _create_field(self._tc, self._create_fc(self._tc)) |
f5567ea8 | 2385 | field = field["variant_field"] |
1eccc498 SM |
2386 | field.selected_option_index = 0 |
2387 | field.value = 1774 | |
2388 | other_field = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 | 2389 | other_field = other_field["variant_field"] |
1eccc498 SM |
2390 | other_field.selected_option_index = 0 |
2391 | other_field.value = 1774 | |
2392 | self.assertEqual(str(field), str(other_field)) | |
b0bdda42 JG |
2393 | |
2394 | def test_str_op_str(self): | |
1eccc498 | 2395 | field = _create_field(self._tc, self._create_fc(self._tc)) |
f5567ea8 | 2396 | field = field["variant_field"] |
1eccc498 | 2397 | field.selected_option_index = 1 |
f5567ea8 | 2398 | field.value = "un beau grand bateau" |
1eccc498 | 2399 | other_field = _create_field(self._tc, self._create_fc(self._tc)) |
f5567ea8 | 2400 | other_field = other_field["variant_field"] |
1eccc498 | 2401 | other_field.selected_option_index = 1 |
f5567ea8 | 2402 | other_field.value = "un beau grand bateau" |
1eccc498 SM |
2403 | self.assertEqual(str(field), str(other_field)) |
2404 | ||
2405 | def test_str_op_float(self): | |
2406 | field = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 | 2407 | field = field["variant_field"] |
1eccc498 SM |
2408 | field.selected_option_index = 2 |
2409 | field.value = 14.4245 | |
2410 | other_field = _create_field(self._tc, self._create_fc(self._tc)) | |
f5567ea8 | 2411 | other_field = other_field["variant_field"] |
1eccc498 SM |
2412 | other_field.selected_option_index = 2 |
2413 | other_field.value = 14.4245 | |
2414 | self.assertEqual(str(field), str(other_field)) | |
d14ddbba SM |
2415 | |
2416 | ||
f5567ea8 | 2417 | if __name__ == "__main__": |
d14ddbba | 2418 | unittest.main() |