1 # SPDX-License-Identifier: GPL-2.0-only
3 # Copyright (C) 2019 EfficiOS Inc.
6 from functools
import partial
, partialmethod
14 from utils
import get_default_trace_class
, create_const_field
17 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
20 # Create and return a stream with the field classes part of its stream packet
23 # The stream is part of a dummy trace created from trace class `tc`.
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
)
32 stream_class
= tc
.create_stream_class(
33 packet_context_field_class
=packet_context_fc
, supports_packets
=True
36 stream
= trace
.create_stream(stream_class
)
40 # Create a field of the given field class.
42 # The field is part of a dummy stream, itself part of a dummy trace created
43 # from trace class `tc`.
46 def _create_field(tc
, field_class
):
48 stream
= _create_stream(tc
, [(field_name
, field_class
)])
49 packet
= stream
.create_packet()
50 return packet
.context_field
[field_name
]
53 # Create a field of type string.
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.
59 def _create_string_field(tc
):
60 field_name
= 'string_field'
61 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
62 packet
= stream
.create_packet()
63 return packet
.context_field
[field_name
]
66 # Create a field of type static array of ints.
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.
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
)
76 field_name
= 'int_array'
77 stream
= _create_stream(tc
, [(field_name
, fc
)])
78 packet
= stream
.create_packet()
79 return packet
.context_field
[field_name
]
82 # Create a field of type dynamic array of ints.
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.
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
)
93 field_name
= 'int_dyn_array'
94 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
95 packet
= stream
.create_packet()
96 packet
.context_field
[field_name
].length
= 3
97 return packet
.context_field
[field_name
]
100 # Create a field of type array of (empty) structures.
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.
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
)
110 field_name
= 'struct_array'
111 stream
= _create_stream(tc
, [(field_name
, fc
)])
112 packet
= stream
.create_packet()
113 return packet
.context_field
[field_name
]
116 class BitArrayFieldTestCase(unittest
.TestCase
):
117 def _create_field(self
):
118 return _create_field(self
._tc
, self
._tc
.create_bit_array_field_class(24))
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
127 def test_assign_invalid_type(self
):
128 with self
.assertRaises(TypeError):
129 self
._def
.value_as_integer
= 'onze'
131 def test_assign(self
):
132 self
._def
.value_as_integer
= 199
133 self
.assertEqual(self
._def
.value_as_integer
, 199)
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))
140 other
= self
._create
_field
()
141 other
.value_as_integer
= self
._def
_value
142 self
.assertEqual(self
._def
, other
)
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
)
149 def test_ne_diff_type(self
):
150 self
.assertNotEqual(self
._def
, self
._def
_value
)
153 self
.assertEqual(len(self
._def
), 24)
156 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
159 self
.assertEqual(repr(self
._def
), repr(self
._def
_value
))
162 # Base class for numeric field test cases.
164 # To be compatible with this base class, a derived class must, in its
167 # * Set `self._def` to a field object with an arbitrary value.
168 # * Set `self._def_value` to the equivalent value of `self._def`.
169 class _TestNumericField
:
170 # Tries the binary operation `op`:
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`.
176 # Returns the results of 1. and 2.
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.
181 def _binop(self
, op
, rhs
):
186 # try with field object
188 r
= op(self
._def
, rhs
)
189 except Exception as e
:
194 rv
= op(self
._def
_value
, comp_value
)
195 except Exception as e
:
198 if type_rexc
is not None or type_rvexc
is not None:
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.)
203 self
.assertIs(type_rexc
, type_rvexc
)
208 # Tries the unary operation `op`:
210 # 1. On `self._def`, which is a field object.
211 # 2. On `self._def_value`, which is the value of `self._def`.
213 # Returns the results of 1. and 2.
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.
218 def _unaryop(self
, op
):
222 # try with field object
225 except Exception as e
:
230 rv
= op(self
._def
_value
)
231 except Exception as e
:
234 if type_rexc
is not None or type_rvexc
is not None:
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.)
239 self
.assertIs(type_rexc
, type_rvexc
)
244 # Tests that the unary operation `op` gives results with the same
245 # type for both `self._def` and `self._def_value`.
246 def _test_unaryop_type(self
, op
):
247 r
, rv
= self
._unaryop
(op
)
252 self
.assertIsInstance(r
, type(rv
))
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`.
257 def _test_unaryop_value(self
, op
):
258 r
, rv
= self
._unaryop
(op
)
263 self
.assertEqual(r
, rv
)
265 # Tests that the unary operation `op`, when applied to `self._def`,
266 # does not change its underlying BT object address.
267 def _test_unaryop_addr_same(self
, op
):
268 addr_before
= self
._def
.addr
270 self
.assertEqual(self
._def
.addr
, addr_before
)
272 # Tests that the unary operation `op`, when applied to `self._def`,
273 # does not change its value.
274 def _test_unaryop_value_same(self
, op
):
275 value_before
= copy
.copy(self
._def
_value
)
277 self
.assertEqual(self
._def
, value_before
)
279 # Tests that the binary operation `op` gives results with the same
280 # type for both `self._def` and `self._def_value`.
281 def _test_binop_type(self
, op
, rhs
):
282 r
, rv
= self
._binop
(op
, rhs
)
287 if op
in _COMP_BINOPS
:
288 # __eq__() and __ne__() always return a 'bool' object
289 self
.assertIsInstance(r
, bool)
291 self
.assertIsInstance(r
, type(rv
))
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`.
296 def _test_binop_value(self
, op
, rhs
):
297 r
, rv
= self
._binop
(op
, rhs
)
302 self
.assertEqual(r
, rv
)
304 # Tests that the binary operation `op`, when applied to `self._def`,
305 # does not change its underlying BT object address.
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
)
311 # Tests that the binary operation `op`, when applied to `self._def`,
312 # does not change its value.
313 def _test_binop_lhs_value_same(self
, op
, rhs
):
314 value_before
= copy
.copy(self
._def
_value
)
315 r
, rv
= self
._binop
(op
, rhs
)
316 self
.assertEqual(self
._def
, value_before
)
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`.
324 # self._test_binop_rhs_pos_int(self._test_binop_value,
327 # This tests that a numeric field object added to a positive integer
328 # value gives a result with the expected value.
330 # `vint` and `vfloat` mean a signed integer value object and a real
333 def _test_binop_unknown(self
, op
):
337 # Operators == and != are defined when comparing the field to an
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)
344 # But not other operators.
345 with self
.assertRaises(TypeError):
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)
355 # But not other operators.
356 with self
.assertRaises(TypeError):
359 def _test_binop_rhs_false(self
, test_cb
, op
):
362 def _test_binop_rhs_true(self
, test_cb
, op
):
365 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
368 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
371 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
374 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
375 test_cb(op
, bt2
.create_value(2))
377 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
378 test_cb(op
, bt2
.create_value(-23))
380 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
381 test_cb(op
, bt2
.create_value(0))
383 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
386 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
389 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
392 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
393 test_cb(op
, bt2
.create_value(2.2))
395 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
396 test_cb(op
, bt2
.create_value(-23.4))
398 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
399 test_cb(op
, bt2
.create_value(0.0))
401 def _test_binop_rhs_complex(self
, test_cb
, op
):
402 test_cb(op
, -23 + 19j
)
404 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
407 def _test_binop_type_false(self
, op
):
408 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
410 def _test_binop_type_true(self
, op
):
411 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
413 def _test_binop_type_pos_int(self
, op
):
414 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
416 def _test_binop_type_neg_int(self
, op
):
417 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
419 def _test_binop_type_zero_int(self
, op
):
420 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
422 def _test_binop_type_pos_vint(self
, op
):
423 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
425 def _test_binop_type_neg_vint(self
, op
):
426 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
428 def _test_binop_type_zero_vint(self
, op
):
429 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
431 def _test_binop_type_pos_float(self
, op
):
432 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
434 def _test_binop_type_neg_float(self
, op
):
435 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
437 def _test_binop_type_zero_float(self
, op
):
438 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
440 def _test_binop_type_pos_vfloat(self
, op
):
441 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
443 def _test_binop_type_neg_vfloat(self
, op
):
444 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
446 def _test_binop_type_zero_vfloat(self
, op
):
447 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
449 def _test_binop_type_complex(self
, op
):
450 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
452 def _test_binop_type_zero_complex(self
, op
):
453 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
455 def _test_binop_value_false(self
, op
):
456 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
458 def _test_binop_value_true(self
, op
):
459 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
461 def _test_binop_value_pos_int(self
, op
):
462 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
464 def _test_binop_value_neg_int(self
, op
):
465 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
467 def _test_binop_value_zero_int(self
, op
):
468 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
470 def _test_binop_value_pos_vint(self
, op
):
471 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
473 def _test_binop_value_neg_vint(self
, op
):
474 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
476 def _test_binop_value_zero_vint(self
, op
):
477 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
479 def _test_binop_value_pos_float(self
, op
):
480 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
482 def _test_binop_value_neg_float(self
, op
):
483 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
485 def _test_binop_value_zero_float(self
, op
):
486 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
488 def _test_binop_value_pos_vfloat(self
, op
):
489 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
491 def _test_binop_value_neg_vfloat(self
, op
):
492 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
494 def _test_binop_value_zero_vfloat(self
, op
):
495 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
497 def _test_binop_value_complex(self
, op
):
498 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
500 def _test_binop_value_zero_complex(self
, op
):
501 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
503 def _test_binop_lhs_addr_same_false(self
, op
):
504 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
506 def _test_binop_lhs_addr_same_true(self
, op
):
507 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
545 def _test_binop_lhs_addr_same_complex(self
, op
):
546 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
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
)
551 def _test_binop_lhs_value_same_false(self
, op
):
552 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
554 def _test_binop_lhs_value_same_true(self
, op
):
555 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
593 def _test_binop_lhs_value_same_complex(self
, op
):
594 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
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
)
599 def test_bool_op(self
):
600 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
602 def test_int_op(self
):
603 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
605 def test_float_op(self
):
606 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
608 def test_complex_op(self
):
609 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
611 def test_str_op(self
):
612 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
614 def test_hash_op(self
):
615 with self
.assertRaises(TypeError):
618 def test_const_hash_op(self
):
619 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
621 def test_const_hash_dict(self
):
623 my_dict
[self
._def
_const
] = 'my_value'
624 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
626 def test_eq_none(self
):
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).
630 self
.assertFalse(self
._def
== None) # noqa: E711
632 def test_ne_none(self
):
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).
636 self
.assertTrue(self
._def
!= None) # noqa: E711
639 # This is a list of binary operators used for
640 # _inject_numeric_testing_methods().
642 # Each entry is a pair of binary operator name (used as part of the
643 # created testing method's name) and operator function.
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
)),
678 # This is a list of unary operators used for
679 # _inject_numeric_testing_methods().
681 # Each entry is a pair of unary operator name (used as part of the
682 # created testing method's name) and operator function.
684 ('neg', operator
.neg
),
685 ('pos', operator
.pos
),
686 ('abs', operator
.abs),
687 ('invert', operator
.invert
),
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)),
694 ('floor', math
.floor
),
695 ('trunc', math
.trunc
),
699 # This function injects a bunch of testing methods to a numeric
702 # It is meant to be used like this:
704 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
706 # This function injects:
708 # * One testing method for each _TestNumericField._test_binop_*()
709 # method, for each binary operator in the _BINOPS tuple.
711 # * One testing method for each _TestNumericField._test_unaryop*()
712 # method, for each unary operator in the _UNARYOPS tuple.
713 def _inject_numeric_testing_methods(cls
):
714 def test_binop_name(suffix
):
715 return 'test_binop_{}_{}'.format(name
, suffix
)
717 def test_unaryop_name(suffix
):
718 return 'test_unaryop_{}_{}'.format(name
, suffix
)
720 # inject testing methods for each binary operation
721 for name
, binop
in _BINOPS
:
724 test_binop_name('unknown'),
725 partialmethod(_TestNumericField
._test
_binop
_unknown
, op
=binop
),
729 test_binop_name('none'),
730 partialmethod(_TestNumericField
._test
_binop
_none
, op
=binop
),
734 test_binop_name('type_true'),
735 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
739 test_binop_name('type_pos_int'),
740 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
744 test_binop_name('type_pos_vint'),
745 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
749 test_binop_name('value_true'),
750 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
754 test_binop_name('value_pos_int'),
755 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
759 test_binop_name('value_pos_vint'),
760 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
764 test_binop_name('lhs_addr_same_true'),
765 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
769 test_binop_name('lhs_addr_same_pos_int'),
771 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
776 test_binop_name('lhs_addr_same_pos_vint'),
778 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
783 test_binop_name('lhs_value_same_true'),
784 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
788 test_binop_name('lhs_value_same_pos_int'),
790 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
795 test_binop_name('lhs_value_same_pos_vint'),
797 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
802 test_binop_name('type_neg_int'),
803 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
807 test_binop_name('type_neg_vint'),
808 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
812 test_binop_name('value_neg_int'),
813 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
817 test_binop_name('value_neg_vint'),
818 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
822 test_binop_name('lhs_addr_same_neg_int'),
824 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
829 test_binop_name('lhs_addr_same_neg_vint'),
831 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
836 test_binop_name('lhs_value_same_neg_int'),
838 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
843 test_binop_name('lhs_value_same_neg_vint'),
845 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
850 test_binop_name('type_false'),
851 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
855 test_binop_name('type_zero_int'),
856 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
860 test_binop_name('type_zero_vint'),
861 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
865 test_binop_name('value_false'),
866 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
870 test_binop_name('value_zero_int'),
871 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
875 test_binop_name('value_zero_vint'),
876 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
880 test_binop_name('lhs_addr_same_false'),
881 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
885 test_binop_name('lhs_addr_same_zero_int'),
887 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
892 test_binop_name('lhs_addr_same_zero_vint'),
894 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
899 test_binop_name('lhs_value_same_false'),
900 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
904 test_binop_name('lhs_value_same_zero_int'),
906 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
911 test_binop_name('lhs_value_same_zero_vint'),
913 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
918 test_binop_name('type_pos_float'),
919 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
923 test_binop_name('type_neg_float'),
924 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
928 test_binop_name('type_pos_vfloat'),
929 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
933 test_binop_name('type_neg_vfloat'),
934 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
938 test_binop_name('value_pos_float'),
939 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
943 test_binop_name('value_neg_float'),
944 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
948 test_binop_name('value_pos_vfloat'),
949 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
953 test_binop_name('value_neg_vfloat'),
954 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
958 test_binop_name('lhs_addr_same_pos_float'),
960 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
965 test_binop_name('lhs_addr_same_neg_float'),
967 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
972 test_binop_name('lhs_addr_same_pos_vfloat'),
974 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
979 test_binop_name('lhs_addr_same_neg_vfloat'),
981 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
986 test_binop_name('lhs_value_same_pos_float'),
988 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
993 test_binop_name('lhs_value_same_neg_float'),
995 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
1000 test_binop_name('lhs_value_same_pos_vfloat'),
1002 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
1007 test_binop_name('lhs_value_same_neg_vfloat'),
1009 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
1014 test_binop_name('type_zero_float'),
1015 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
1019 test_binop_name('type_zero_vfloat'),
1020 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
1024 test_binop_name('value_zero_float'),
1025 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
1029 test_binop_name('value_zero_vfloat'),
1030 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
1034 test_binop_name('lhs_addr_same_zero_float'),
1036 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
1041 test_binop_name('lhs_addr_same_zero_vfloat'),
1043 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
1048 test_binop_name('lhs_value_same_zero_float'),
1050 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1055 test_binop_name('lhs_value_same_zero_vfloat'),
1057 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1062 test_binop_name('type_complex'),
1063 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1067 test_binop_name('type_zero_complex'),
1068 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1072 test_binop_name('value_complex'),
1073 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1077 test_binop_name('value_zero_complex'),
1078 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1082 test_binop_name('lhs_addr_same_complex'),
1084 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1089 test_binop_name('lhs_addr_same_zero_complex'),
1091 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1096 test_binop_name('lhs_value_same_complex'),
1098 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1103 test_binop_name('lhs_value_same_zero_complex'),
1105 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1109 # inject testing methods for each unary operation
1110 for name
, unaryop
in _UNARYOPS
:
1113 test_unaryop_name('type'),
1114 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1118 test_unaryop_name('value'),
1119 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1123 test_unaryop_name('addr_same'),
1124 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1128 test_unaryop_name('value_same'),
1129 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1133 class BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1135 def _const_value_setter(field
):
1138 def _create_fc(self
, tc
):
1139 return tc
.create_bool_field_class()
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
1146 self
._def
_const
= create_const_field(
1147 self
._tc
, self
._tc
.create_bool_field_class(), self
._const
_value
_setter
1149 self
._def
_new
_value
= False
1151 def test_classes(self
):
1152 self
.assertIs(type(self
._def
), bt2
._BoolField
)
1153 self
.assertIs(type(self
._def
_const
), bt2
._BoolFieldConst
)
1155 def test_assign_true(self
):
1157 self
._def
.value
= raw
1158 self
.assertEqual(self
._def
, raw
)
1160 def test_assign_false(self
):
1162 self
._def
.value
= raw
1163 self
.assertEqual(self
._def
, raw
)
1165 def test_assign_field_true(self
):
1166 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1169 self
._def
.value
= field
1170 self
.assertEqual(self
._def
, raw
)
1172 def test_assign_field_false(self
):
1173 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1176 self
._def
.value
= field
1177 self
.assertEqual(self
._def
, raw
)
1179 def test_assign_invalid_type(self
):
1180 with self
.assertRaises(TypeError):
1181 self
._def
.value
= 17
1183 def test_str_op(self
):
1184 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1187 _inject_numeric_testing_methods(BoolFieldTestCase
)
1190 class _TestIntegerFieldCommon(_TestNumericField
):
1191 def test_assign_true(self
):
1193 self
._def
.value
= raw
1194 self
.assertEqual(self
._def
, raw
)
1196 def test_assign_false(self
):
1198 self
._def
.value
= raw
1199 self
.assertEqual(self
._def
, raw
)
1201 def test_assign_pos_int(self
):
1203 self
._def
.value
= raw
1204 self
.assertEqual(self
._def
, raw
)
1206 def test_assign_neg_int(self
):
1208 self
._def
.value
= raw
1209 self
.assertEqual(self
._def
, raw
)
1211 def test_assign_int_field(self
):
1213 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1215 self
._def
.value
= field
1216 self
.assertEqual(self
._def
, raw
)
1218 def test_assign_invalid_type(self
):
1219 with self
.assertRaises(TypeError):
1220 self
._def
.value
= 'yes'
1222 def test_assign_uint(self
):
1223 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1224 field
= _create_field(self
._tc
, uint_fc
)
1227 self
.assertEqual(field
, raw
)
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
1235 field
.value
= (2**53) + 1
1236 self
.assertEqual(field
, raw
)
1238 def test_assign_uint_out_of_range(self
):
1239 uint_fc
= self
._tc
.create_unsigned_integer_field_class(8)
1240 field
= _create_field(self
._tc
, uint_fc
)
1242 with self
.assertRaises(ValueError) as ctx
:
1245 str(ctx
.exception
), 'Value 256 is outside valid range [0, 255]'
1248 with self
.assertRaises(ValueError) as ctx
:
1250 self
.assertEqual(str(ctx
.exception
), 'Value -1 is outside valid range [0, 255]')
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
)
1256 with self
.assertRaises(ValueError) as ctx
:
1259 str(ctx
.exception
), 'Value 128 is outside valid range [-128, 127]'
1262 with self
.assertRaises(ValueError) as ctx
:
1265 str(ctx
.exception
), 'Value -129 is outside valid range [-128, 127]'
1268 def test_str_op(self
):
1269 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1272 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1275 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1277 def _const_value_setter(field
):
1280 def _create_fc(self
, tc
):
1281 return tc
.create_signed_integer_field_class(25)
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
))
1288 self
._def
.value
= 17
1289 self
._def
_value
= 17
1290 self
._def
_const
= create_const_field(
1291 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1293 self
._def
_new
_value
= -101
1296 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1298 def _const_value_setter(field
):
1301 def _create_fc(self
, tc
):
1302 fc
= tc
.create_signed_enumeration_field_class(32)
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)]))
1307 'whole range', bt2
.SignedIntegerRangeSet([(-(2**31), (2**31) - 1)])
1309 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
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
))
1316 self
._def
.value
= 17
1317 self
._def
_value
= 17
1318 self
._def
_const
= create_const_field(
1319 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1321 self
._def
_new
_value
= -101
1323 def test_str_op(self
):
1324 expected_string_found
= False
1327 # Establish all permutations of the three expected matches since
1328 # the order in which mappings are enumerated is not explicitly part of
1330 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1331 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1333 expected_string_found
= True
1336 self
.assertTrue(expected_string_found
)
1338 def test_labels(self
):
1339 self
._field
.value
= 17
1340 labels
= sorted(self
._field
.labels
)
1341 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1344 class SingleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1346 def _const_value_setter(field
):
1349 def _create_fc(self
, tc
):
1350 return tc
.create_single_precision_real_field_class()
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
))
1356 self
._def
_const
= create_const_field(
1358 self
._tc
.create_single_precision_real_field_class(),
1359 self
._const
_value
_setter
,
1361 self
._def
.value
= 52.0
1362 self
._def
_value
= 52.0
1363 self
._def
_new
_value
= -17.0
1365 def _test_invalid_op(self
, cb
):
1366 with self
.assertRaises(TypeError):
1369 def test_assign_true(self
):
1370 self
._def
.value
= True
1371 self
.assertTrue(self
._def
)
1373 def test_assign_false(self
):
1374 self
._def
.value
= False
1375 self
.assertFalse(self
._def
)
1377 def test_assign_pos_int(self
):
1379 self
._def
.value
= raw
1380 self
.assertEqual(self
._def
, float(raw
))
1382 def test_assign_neg_int(self
):
1384 self
._def
.value
= raw
1385 self
.assertEqual(self
._def
, float(raw
))
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
)
1391 int_field
.value
= raw
1392 self
._def
.value
= int_field
1393 self
.assertEqual(self
._def
, float(raw
))
1395 def test_assign_float(self
):
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
)
1402 def test_assign_float_field(self
):
1403 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
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
)
1411 def test_assign_invalid_type(self
):
1412 with self
.assertRaises(TypeError):
1413 self
._def
.value
= 'yes'
1415 def test_invalid_lshift(self
):
1416 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1418 def test_invalid_rshift(self
):
1419 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1421 def test_invalid_and(self
):
1422 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1424 def test_invalid_or(self
):
1425 self
._test
_invalid
_op
(lambda: self
._def |
23)
1427 def test_invalid_xor(self
):
1428 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1430 def test_invalid_invert(self
):
1431 self
._test
_invalid
_op
(lambda: ~self
._def
)
1433 def test_str_op(self
):
1434 self
.assertEqual(str(round(self
._def
, 5)), str(self
._def
_value
))
1437 _inject_numeric_testing_methods(SingleRealFieldTestCase
)
1440 class DoubleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1442 def _const_value_setter(field
):
1445 def _create_fc(self
, tc
):
1446 return tc
.create_double_precision_real_field_class()
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
))
1452 self
._def
_const
= create_const_field(
1454 self
._tc
.create_double_precision_real_field_class(),
1455 self
._const
_value
_setter
,
1457 self
._def
.value
= 52.7
1458 self
._def
_value
= 52.7
1459 self
._def
_new
_value
= -17.164857
1461 def _test_invalid_op(self
, cb
):
1462 with self
.assertRaises(TypeError):
1465 def test_assign_true(self
):
1466 self
._def
.value
= True
1467 self
.assertTrue(self
._def
)
1469 def test_assign_false(self
):
1470 self
._def
.value
= False
1471 self
.assertFalse(self
._def
)
1473 def test_assign_pos_int(self
):
1475 self
._def
.value
= raw
1476 self
.assertEqual(self
._def
, float(raw
))
1478 def test_assign_neg_int(self
):
1480 self
._def
.value
= raw
1481 self
.assertEqual(self
._def
, float(raw
))
1483 def test_assign_int_field(self
):
1484 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1485 int_field
= _create_field(self
._tc
, int_fc
)
1487 int_field
.value
= raw
1488 self
._def
.value
= int_field
1489 self
.assertEqual(self
._def
, float(raw
))
1491 def test_assign_float(self
):
1493 self
._def
.value
= raw
1494 self
.assertEqual(self
._def
, raw
)
1496 def test_assign_float_field(self
):
1497 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1500 self
._def
.value
= field
1501 self
.assertEqual(self
._def
, raw
)
1503 def test_assign_invalid_type(self
):
1504 with self
.assertRaises(TypeError):
1505 self
._def
.value
= 'yes'
1507 def test_invalid_lshift(self
):
1508 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1510 def test_invalid_rshift(self
):
1511 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1513 def test_invalid_and(self
):
1514 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1516 def test_invalid_or(self
):
1517 self
._test
_invalid
_op
(lambda: self
._def |
23)
1519 def test_invalid_xor(self
):
1520 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1522 def test_invalid_invert(self
):
1523 self
._test
_invalid
_op
(lambda: ~self
._def
)
1525 def test_str_op(self
):
1526 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1529 _inject_numeric_testing_methods(DoubleRealFieldTestCase
)
1532 class StringFieldTestCase(unittest
.TestCase
):
1534 def _const_value_setter(field
):
1535 field
.value
= 'Hello, World!'
1538 self
._tc
= get_default_trace_class()
1539 self
._def
_value
= 'Hello, World!'
1540 self
._def
= _create_string_field(self
._tc
)
1541 self
._def
_const
= create_const_field(
1542 self
._tc
, self
._tc
.create_string_field_class(), self
._const
_value
_setter
1544 self
._def
.value
= self
._def
_value
1545 self
._def
_new
_value
= 'Yes!'
1547 def test_assign_int(self
):
1548 with self
.assertRaises(TypeError):
1549 self
._def
.value
= 283
1551 def test_assign_string_field(self
):
1552 field
= _create_string_field(self
._tc
)
1555 self
.assertEqual(field
, raw
)
1558 self
.assertEqual(self
._def
, self
._def
_value
)
1560 def test_const_eq(self
):
1561 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1563 def test_not_eq(self
):
1564 self
.assertNotEqual(self
._def
, 23)
1566 def test_lt_vstring(self
):
1567 s1
= _create_string_field(self
._tc
)
1569 s2
= _create_string_field(self
._tc
)
1571 self
.assertLess(s1
, s2
)
1573 def test_lt_string(self
):
1574 s1
= _create_string_field(self
._tc
)
1576 self
.assertLess(s1
, 'bateau')
1578 def test_le_vstring(self
):
1579 s1
= _create_string_field(self
._tc
)
1581 s2
= _create_string_field(self
._tc
)
1583 self
.assertLessEqual(s1
, s2
)
1585 def test_le_string(self
):
1586 s1
= _create_string_field(self
._tc
)
1588 self
.assertLessEqual(s1
, 'bateau')
1590 def test_gt_vstring(self
):
1591 s1
= _create_string_field(self
._tc
)
1593 s2
= _create_string_field(self
._tc
)
1595 self
.assertGreater(s2
, s1
)
1597 def test_gt_string(self
):
1598 s1
= _create_string_field(self
._tc
)
1600 self
.assertGreater('bateau', s1
)
1602 def test_ge_vstring(self
):
1603 s1
= _create_string_field(self
._tc
)
1605 s2
= _create_string_field(self
._tc
)
1607 self
.assertGreaterEqual(s2
, s1
)
1609 def test_ge_string(self
):
1610 s1
= _create_string_field(self
._tc
)
1612 self
.assertGreaterEqual('bateau', s1
)
1614 def test_bool_op(self
):
1615 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1617 def test_str_op(self
):
1618 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1621 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1623 def test_getitem(self
):
1624 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1626 def test_const_getitem(self
):
1627 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1629 def test_append_str(self
):
1630 to_append
= 'meow meow meow'
1631 self
._def
+= to_append
1632 self
._def
_value
+= to_append
1633 self
.assertEqual(self
._def
, self
._def
_value
)
1635 def test_const_append_str(self
):
1636 to_append
= 'meow meow meow'
1637 with self
.assertRaises(TypeError):
1638 self
._def
_const
+= to_append
1639 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1641 def test_append_string_field(self
):
1642 field
= _create_string_field(self
._tc
)
1643 to_append
= 'meow meow meow'
1644 field
.value
= to_append
1646 self
._def
_value
+= to_append
1647 self
.assertEqual(self
._def
, self
._def
_value
)
1649 def test_hash_op(self
):
1650 with self
.assertRaises(TypeError):
1653 def test_const_hash_op(self
):
1654 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
1656 def test_const_hash_dict(self
):
1658 my_dict
[self
._def
_const
] = 'my_value'
1659 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
1662 class _TestArrayFieldCommon
:
1663 def _modify_def(self
):
1666 def test_bool_op_true(self
):
1667 self
.assertTrue(self
._def
)
1670 self
.assertEqual(len(self
._def
), 3)
1672 def test_length(self
):
1673 self
.assertEqual(self
._def
.length
, 3)
1675 def test_getitem(self
):
1676 field
= self
._def
[1]
1677 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1678 self
.assertEqual(field
, 1847)
1680 def test_const_getitem(self
):
1681 field
= self
._def
_const
[1]
1682 self
.assertIs(type(field
), bt2
._SignedIntegerFieldConst
)
1683 self
.assertEqual(field
, 1847)
1686 field
= _create_int_array_field(self
._tc
, 3)
1690 self
.assertEqual(self
._def
, field
)
1692 def test_eq_invalid_type(self
):
1693 self
.assertNotEqual(self
._def
, 23)
1695 def test_eq_diff_len(self
):
1696 field
= _create_int_array_field(self
._tc
, 2)
1699 self
.assertNotEqual(self
._def
, field
)
1701 def test_eq_diff_content_same_len(self
):
1702 field
= _create_int_array_field(self
._tc
, 3)
1706 self
.assertNotEqual(self
._def
, field
)
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)
1714 self
.assertEqual(field
, list(dct
.keys()))
1715 self
.assertNotEqual(field
, dct
)
1717 def test_setitem(self
):
1719 self
.assertEqual(self
._def
[2], 24)
1721 def test_setitem_int_field(self
):
1722 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1723 int_field
= _create_field(self
._tc
, int_fc
)
1724 int_field
.value
= 19487
1725 self
._def
[1] = int_field
1726 self
.assertEqual(self
._def
[1], 19487)
1728 def test_setitem_non_basic_field(self
):
1729 array_field
= _create_struct_array_field(self
._tc
, 2)
1730 with self
.assertRaises(TypeError):
1733 def test_setitem_none(self
):
1734 with self
.assertRaises(TypeError):
1737 def test_setitem_index_wrong_type(self
):
1738 with self
.assertRaises(TypeError):
1739 self
._def
['yes'] = 23
1741 def test_setitem_index_neg(self
):
1742 with self
.assertRaises(IndexError):
1745 def test_setitem_index_out_of_range(self
):
1746 with self
.assertRaises(IndexError):
1747 self
._def
[len(self
._def
)] = 134679
1749 def test_const_setitem(self
):
1750 with self
.assertRaises(TypeError):
1751 self
._def
_const
[0] = 134679
1753 def test_iter(self
):
1754 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1755 self
.assertEqual(field
, value
)
1757 def test_const_iter(self
):
1758 for field
, value
in zip(self
._def
_const
, (45, 1847, 1948754)):
1759 self
.assertEqual(field
, value
)
1761 def test_value_int_field(self
):
1762 values
= [45646, 145, 12145]
1763 self
._def
.value
= values
1764 self
.assertEqual(values
, self
._def
)
1766 def test_value_check_sequence(self
):
1768 with self
.assertRaises(TypeError):
1769 self
._def
.value
= values
1771 def test_value_wrong_type_in_sequence(self
):
1772 values
= [32, 'hello', 11]
1773 with self
.assertRaises(TypeError):
1774 self
._def
.value
= values
1776 def test_value_complex_type(self
):
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()
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')
1784 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1785 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
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},
1792 array
= stream
.create_packet().context_field
['array_field']
1793 array
.value
= values
1794 self
.assertEqual(values
, array
)
1795 values
[0]['an_int'] = 'a string'
1796 with self
.assertRaises(TypeError):
1797 array
.value
= values
1799 def test_str_op(self
):
1801 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1802 self
.assertEqual(expected_string
, s
)
1805 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1807 def _const_value_setter(field
):
1808 field
.value
= [45, 1847, 1948754]
1811 self
._tc
= get_default_trace_class()
1812 self
._def
= _create_int_array_field(self
._tc
, 3)
1815 self
._def
[2] = 1948754
1816 self
._def
_value
= [45, 1847, 1948754]
1817 self
._def
_const
= create_const_field(
1819 self
._tc
.create_static_array_field_class(
1820 self
._tc
.create_signed_integer_field_class(32), 3
1822 self
._const
_value
_setter
,
1825 def test_value_wrong_len(self
):
1827 with self
.assertRaises(ValueError):
1828 self
._def
.value
= values
1831 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1833 def _const_value_setter(field
):
1834 field
.value
= [45, 1847, 1948754]
1837 self
._tc
= get_default_trace_class()
1838 self
._def
= _create_dynamic_array(self
._tc
)
1841 self
._def
[2] = 1948754
1842 self
._def
_value
= [45, 1847, 1948754]
1843 self
._def
_const
= create_const_field(
1845 self
._tc
.create_dynamic_array_field_class(
1846 self
._tc
.create_signed_integer_field_class(32)
1848 self
._const
_value
_setter
,
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
)
1856 def test_set_length(self
):
1857 self
._def
.length
= 4
1859 self
.assertEqual(len(self
._def
), 4)
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)
1866 def test_set_invalid_length(self
):
1867 with self
.assertRaises(TypeError):
1868 self
._def
.length
= 'cheval'
1871 class StructureFieldTestCase(unittest
.TestCase
):
1873 def _const_value_setter(field
):
1883 def _create_fc(self
, tc
):
1884 fc
= tc
.create_structure_field_class()
1885 fc
.append_member('A', self
._fc
0_fn
())
1886 fc
.append_member('B', self
._fc
1_fn
())
1887 fc
.append_member('C', self
._fc
2_fn
())
1888 fc
.append_member('D', self
._fc
3_fn
())
1889 fc
.append_member('E', self
._fc
4_fn
())
1890 fc5
= self
._fc
5_fn
()
1891 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1892 fc
.append_member('F', fc5
)
1896 self
._tc
= get_default_trace_class()
1897 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1898 self
._fc
1_fn
= self
._tc
.create_string_field_class
1899 self
._fc
2_fn
= self
._tc
.create_double_precision_real_field_class
1900 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1901 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1902 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1903 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1905 self
._fc
= self
._create
_fc
(self
._tc
)
1906 self
._def
= _create_field(self
._tc
, self
._fc
)
1907 self
._def
['A'] = -1872
1908 self
._def
['B'] = 'salut'
1909 self
._def
['C'] = 17.5
1910 self
._def
['D'] = 16497
1912 self
._def
['F'] = {'F_1': 52}
1922 self
._def
_const
= create_const_field(
1923 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1926 def _modify_def(self
):
1927 self
._def
['B'] = 'hola'
1929 def test_bool_op_true(self
):
1930 self
.assertTrue(self
._def
)
1932 def test_bool_op_false(self
):
1933 field
= self
._def
['E']
1934 self
.assertFalse(field
)
1937 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1939 def test_getitem(self
):
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']
1947 self
.assertIs(type(field1
), bt2
._SignedIntegerField
)
1948 self
.assertEqual(field1
, -1872)
1950 self
.assertIs(type(field2
), bt2
._StringField
)
1951 self
.assertEqual(field2
, 'salut')
1953 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealField
)
1954 self
.assertEqual(field3
, 17.5)
1956 self
.assertIs(type(field4
), bt2
._SignedIntegerField
)
1957 self
.assertEqual(field4
, 16497)
1959 self
.assertIs(type(field5
), bt2
._StructureField
)
1960 self
.assertEqual(field5
, {})
1962 self
.assertIs(type(field6
), bt2
._StructureField
)
1963 self
.assertEqual(field6
, {'F_1': 52})
1965 def test_const_getitem(self
):
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']
1973 self
.assertIs(type(field1
), bt2
._SignedIntegerFieldConst
)
1974 self
.assertEqual(field1
, -1872)
1976 self
.assertIs(type(field2
), bt2
._StringFieldConst
)
1977 self
.assertEqual(field2
, 'salut')
1979 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealFieldConst
)
1980 self
.assertEqual(field3
, 17.5)
1982 self
.assertIs(type(field4
), bt2
._SignedIntegerFieldConst
)
1983 self
.assertEqual(field4
, 16497)
1985 self
.assertIs(type(field5
), bt2
._StructureFieldConst
)
1986 self
.assertEqual(field5
, {})
1988 self
.assertIs(type(field6
), bt2
._StructureFieldConst
)
1989 self
.assertEqual(field6
, {'F_1': 52})
1991 def test_member_at_index_out_of_bounds_after(self
):
1992 with self
.assertRaises(IndexError):
1993 self
._def
.member_at_index(len(self
._def
_value
))
1996 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1998 field
['B'] = 'salut'
2002 field
['F'] = {'F_1': 52}
2003 self
.assertEqual(self
._def
, field
)
2005 def test_const_eq(self
):
2006 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2008 field
['B'] = 'salut'
2012 field
['F'] = {'F_1': 52}
2013 self
.assertEqual(self
._def
_const
, field
)
2015 def test_eq_invalid_type(self
):
2016 self
.assertNotEqual(self
._def
, 23)
2018 def test_eq_diff_len(self
):
2019 fc
= self
._tc
.create_structure_field_class()
2020 fc
.append_member('A', self
._fc
0_fn
())
2021 fc
.append_member('B', self
._fc
1_fn
())
2022 fc
.append_member('C', self
._fc
2_fn
())
2024 field
= _create_field(self
._tc
, fc
)
2026 field
['B'] = 'salut'
2028 self
.assertNotEqual(self
._def
, field
)
2030 def test_eq_diff_keys(self
):
2031 fc
= self
._tc
.create_structure_field_class()
2032 fc
.append_member('U', self
._fc
0_fn
())
2033 fc
.append_member('V', self
._fc
1_fn
())
2034 fc
.append_member('W', self
._fc
2_fn
())
2035 fc
.append_member('X', self
._fc
3_fn
())
2036 fc
.append_member('Y', self
._fc
4_fn
())
2037 fc
.append_member('Z', self
._fc
5_fn
())
2038 field
= _create_field(self
._tc
, fc
)
2040 field
['V'] = "gerry"
2045 self
.assertNotEqual(self
._def
, field
)
2047 def test_eq_diff_content_same_len(self
):
2048 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2050 field
['B'] = 'salut'
2054 field
['F'] = {'F_1': 0}
2055 self
.assertNotEqual(self
._def
, field
)
2057 def test_eq_same_content_diff_keys(self
):
2058 fc
= self
._tc
.create_structure_field_class()
2059 fc
.append_member('A', self
._fc
0_fn
())
2060 fc
.append_member('B', self
._fc
1_fn
())
2061 fc
.append_member('E', self
._fc
2_fn
())
2062 fc
.append_member('D', self
._fc
3_fn
())
2063 fc
.append_member('C', self
._fc
4_fn
())
2064 fc
.append_member('F', self
._fc
5_fn
())
2065 field
= _create_field(self
._tc
, fc
)
2067 field
['B'] = 'salut'
2072 self
.assertNotEqual(self
._def
, field
)
2074 def test_setitem(self
):
2075 self
._def
['C'] = -18.47
2076 self
.assertEqual(self
._def
['C'], -18.47)
2078 def test_const_setitem(self
):
2079 with self
.assertRaises(TypeError):
2080 self
._def
_const
['A'] = 134679
2082 def test_setitem_int_field(self
):
2083 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2084 int_field
= _create_field(self
._tc
, int_fc
)
2085 int_field
.value
= 19487
2086 self
._def
['D'] = int_field
2087 self
.assertEqual(self
._def
['D'], 19487)
2089 def test_setitem_non_basic_field(self
):
2090 elem_fc
= self
._tc
.create_structure_field_class()
2091 struct_fc
= self
._tc
.create_structure_field_class()
2092 struct_fc
.append_member('A', elem_fc
)
2093 struct_field
= _create_field(self
._tc
, struct_fc
)
2095 # Will fail on access to .items() of the value
2096 with self
.assertRaises(AttributeError):
2097 struct_field
['A'] = 23
2099 def test_setitem_none(self
):
2100 with self
.assertRaises(TypeError):
2101 self
._def
['C'] = None
2103 def test_setitem_key_wrong_type(self
):
2104 with self
.assertRaises(TypeError):
2107 def test_setitem_wrong_key(self
):
2108 with self
.assertRaises(KeyError):
2109 self
._def
['hi'] = 134679
2111 def test_member_at_index(self
):
2112 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
2114 def test_const_member_at_index(self
):
2115 self
.assertEqual(self
._def
_const
.member_at_index(1), 'salut')
2117 def test_iter(self
):
2127 for vkey
, vval
in self
._def
.items():
2128 val
= orig_values
[vkey
]
2129 self
.assertEqual(vval
, val
)
2131 def test_value(self
):
2140 self
.assertEqual(self
._def
, orig_values
)
2142 def test_set_value(self
):
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()
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}
2152 struct
= _create_field(self
._tc
, struct_fc
)
2153 struct
.value
= values
2154 self
.assertEqual(values
, struct
)
2156 bad_type_values
= copy
.deepcopy(values
)
2157 bad_type_values
['an_int'] = 'a string'
2158 with self
.assertRaises(TypeError):
2159 struct
.value
= bad_type_values
2161 unknown_key_values
= copy
.deepcopy(values
)
2162 unknown_key_values
['unknown_key'] = 16546
2163 with self
.assertRaises(KeyError):
2164 struct
.value
= unknown_key_values
2166 def test_str_op(self
):
2167 expected_string_found
= False
2169 # Establish all permutations of the three expected matches since
2170 # the order in which mappings are enumerated is not explicitly part of
2172 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
2173 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
2174 candidate
= '{{{}}}'.format(', '.join(items
))
2176 expected_string_found
= True
2179 self
.assertTrue(expected_string_found
)
2182 class OptionFieldTestCase(unittest
.TestCase
):
2184 def _const_value_setter(field
):
2185 field
.value
= {'opt_field': 'hiboux'}
2187 def _create_fc(self
, tc
):
2188 fc
= tc
.create_option_without_selector_field_class(
2189 tc
.create_string_field_class()
2191 top_fc
= tc
.create_structure_field_class()
2192 top_fc
.append_member('opt_field', fc
)
2196 self
._tc
= get_default_trace_class()
2197 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2198 self
._def
= fld
['opt_field']
2199 self
._def
_value
= 'hiboux'
2200 self
._def
_const
= create_const_field(
2201 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
2204 def test_value_prop(self
):
2205 self
._def
.value
= 'hiboux'
2206 self
.assertEqual(self
._def
.field
, 'hiboux')
2207 self
.assertIs(type(self
._def
), bt2
._OptionField
)
2208 self
.assertIs(type(self
._def
.field
), bt2
._StringField
)
2209 self
.assertTrue(self
._def
.has_field
)
2211 def test_const_value_prop(self
):
2212 self
.assertEqual(self
._def
_const
.field
, 'hiboux')
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
)
2217 def test_has_field_prop_true(self
):
2218 self
._def
.has_field
= True
2219 self
.assertTrue(self
._def
.has_field
)
2221 def test_has_field_prop_false(self
):
2222 self
._def
.has_field
= False
2223 self
.assertFalse(self
._def
.has_field
)
2225 def test_bool_op_true(self
):
2226 self
._def
.value
= 'allo'
2227 self
.assertTrue(self
._def
)
2229 def test_bool_op_false(self
):
2230 self
._def
.has_field
= False
2231 self
.assertFalse(self
._def
)
2233 def test_field_prop_existing(self
):
2234 self
._def
.value
= 'meow'
2235 field
= self
._def
.field
2236 self
.assertEqual(field
, 'meow')
2238 def test_field_prop_none(self
):
2239 self
._def
.has_field
= False
2240 field
= self
._def
.field
2241 self
.assertIsNone(field
)
2243 def test_const_field_prop(self
):
2244 with self
.assertRaises(AttributeError):
2245 self
._def
_const
.has_field
= False
2247 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2248 self
.assertTrue(self
._def
_const
.has_field
)
2250 def test_field_prop_existing_then_none(self
):
2251 self
._def
.value
= 'meow'
2252 field
= self
._def
.field
2253 self
.assertEqual(field
, 'meow')
2254 self
._def
.has_field
= False
2255 field
= self
._def
.field
2256 self
.assertIsNone(field
)
2259 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2260 field
= field
['opt_field']
2261 field
.value
= 'walk'
2262 self
._def
.value
= 'walk'
2263 self
.assertEqual(self
._def
, field
)
2265 def test_const_eq(self
):
2266 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2267 field
= field
['opt_field']
2268 field
.value
= 'hiboux'
2269 self
.assertEqual(self
._def
_const
, field
)
2270 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2272 def test_eq_invalid_type(self
):
2273 self
._def
.value
= 'gerry'
2274 self
.assertNotEqual(self
._def
, 23)
2276 def test_str_op(self
):
2277 self
._def
.value
= 'marcel'
2278 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
2280 def test_repr_op(self
):
2281 self
._def
.value
= 'mireille'
2282 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
2285 class VariantFieldTestCase(unittest
.TestCase
):
2287 def _const_value_setter(field
):
2288 field
.selected_option_index
= 3
2291 def _create_fc(self
, tc
):
2292 ft0
= tc
.create_signed_integer_field_class(32)
2293 ft1
= tc
.create_string_field_class()
2294 ft2
= tc
.create_double_precision_real_field_class()
2295 ft3
= tc
.create_signed_integer_field_class(17)
2296 fc
= tc
.create_variant_field_class()
2297 fc
.append_option('corner', ft0
)
2298 fc
.append_option('zoom', ft1
)
2299 fc
.append_option('mellotron', ft2
)
2300 fc
.append_option('giorgio', ft3
)
2301 top_fc
= tc
.create_structure_field_class()
2302 top_fc
.append_member('variant_field', fc
)
2306 self
._tc
= get_default_trace_class()
2307 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2308 self
._def
= fld
['variant_field']
2310 self
._def
_value
= 1334
2311 self
._def
_selected
_index
= 3
2312 const_fc
= self
._create
_fc
(self
._tc
)['variant_field']
2314 fld_const
= create_const_field(
2315 self
._tc
, const_fc
.field_class
, self
._const
_value
_setter
2317 self
._def
_const
= fld_const
2319 def test_bool_op(self
):
2320 self
._def
.selected_option_index
= 2
2321 self
._def
.value
= -17.34
2322 with self
.assertRaises(NotImplementedError):
2325 def test_selected_option_index(self
):
2326 self
._def
.selected_option_index
= 2
2327 self
.assertEqual(self
._def
.selected_option_index
, 2)
2329 def test_selected_option_index_above_range(self
):
2330 with self
.assertRaises(IndexError):
2331 self
._def
.selected_option_index
= 4
2333 def test_selected_option_index_below_range(self
):
2334 with self
.assertRaises(IndexError):
2335 self
._def
.selected_option_index
= -1
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)
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)
2346 self
.assertEqual(type(self
._def
.selected_option
), bt2
._DoublePrecisionRealField
)
2348 self
._def
.selected_option_index
= 3
2349 self
._def
.value
= 1921
2350 self
.assertEqual(self
._def
.selected_option
, 1921)
2351 self
.assertEqual(type(self
._def
.selected_option
), bt2
._SignedIntegerField
)
2353 def test_const_selected_option(self
):
2354 self
.assertEqual(self
._def
_const
.selected_option
, 1334)
2356 type(self
._def
_const
.selected_option
), bt2
._SignedIntegerFieldConst
2360 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2361 field
= field
['variant_field']
2362 field
.selected_option_index
= 0
2364 self
._def
.selected_option_index
= 0
2365 self
._def
.value
= 1774
2366 self
.assertEqual(self
._def
, field
)
2368 def test_const_eq(self
):
2369 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2370 field
= field
['variant_field']
2371 field
.selected_option_index
= 3
2373 self
.assertEqual(self
._def
_const
, field
)
2376 self
.assertEqual(len(self
._def
), 4)
2378 def test_eq_invalid_type(self
):
2379 self
._def
.selected_option_index
= 1
2380 self
._def
.value
= 'gerry'
2381 self
.assertNotEqual(self
._def
, 23)
2383 def test_str_op_int(self
):
2384 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2385 field
= field
['variant_field']
2386 field
.selected_option_index
= 0
2388 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2389 other_field
= other_field
['variant_field']
2390 other_field
.selected_option_index
= 0
2391 other_field
.value
= 1774
2392 self
.assertEqual(str(field
), str(other_field
))
2394 def test_str_op_str(self
):
2395 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2396 field
= field
['variant_field']
2397 field
.selected_option_index
= 1
2398 field
.value
= 'un beau grand bateau'
2399 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2400 other_field
= other_field
['variant_field']
2401 other_field
.selected_option_index
= 1
2402 other_field
.value
= 'un beau grand bateau'
2403 self
.assertEqual(str(field
), str(other_field
))
2405 def test_str_op_float(self
):
2406 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2407 field
= field
['variant_field']
2408 field
.selected_option_index
= 2
2409 field
.value
= 14.4245
2410 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2411 other_field
= other_field
['variant_field']
2412 other_field
.selected_option_index
= 2
2413 other_field
.value
= 14.4245
2414 self
.assertEqual(str(field
), str(other_field
))
2417 if __name__
== '__main__':