2 # Copyright (C) 2019 EfficiOS Inc.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; only version 2
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 from functools
import partial
, partialmethod
27 from utils
import get_default_trace_class
30 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
33 # Create and return a stream with the field classes part of its stream packet
36 # The stream is part of a dummy trace created from trace class `tc`.
39 def _create_stream(tc
, ctx_field_classes
):
40 packet_context_fc
= tc
.create_structure_field_class()
41 for name
, fc
in ctx_field_classes
:
42 packet_context_fc
.append_member(name
, fc
)
45 stream_class
= tc
.create_stream_class(
46 packet_context_field_class
=packet_context_fc
, supports_packets
=True
49 stream
= trace
.create_stream(stream_class
)
53 # Create a field of the given field class.
55 # The field is part of a dummy stream, itself part of a dummy trace created
56 # from trace class `tc`.
59 def _create_field(tc
, field_class
):
61 stream
= _create_stream(tc
, [(field_name
, field_class
)])
62 packet
= stream
.create_packet()
63 return packet
.context_field
[field_name
]
66 # Create a field of type string.
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 string field class.
72 def _create_string_field(tc
):
73 field_name
= 'string_field'
74 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
75 packet
= stream
.create_packet()
76 return packet
.context_field
[field_name
]
79 # Create a field of type static array of ints.
81 # The field is part of a dummy stream, itself part of a dummy trace created
82 # from trace class `tc`. It is made out of a dummy static array field class,
83 # with a dummy integer field class as element class.
86 def _create_int_array_field(tc
, length
):
87 elem_fc
= tc
.create_signed_integer_field_class(32)
88 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
89 field_name
= 'int_array'
90 stream
= _create_stream(tc
, [(field_name
, fc
)])
91 packet
= stream
.create_packet()
92 return packet
.context_field
[field_name
]
95 # Create a field of type dynamic array of ints.
97 # The field is part of a dummy stream, itself part of a dummy trace created
98 # from trace class `tc`. It is made out of a dummy static array field class,
99 # with a dummy integer field class as element and length classes.
102 def _create_dynamic_array(tc
):
103 elem_fc
= tc
.create_signed_integer_field_class(32)
104 len_fc
= tc
.create_signed_integer_field_class(32)
105 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
106 field_name
= 'int_dyn_array'
107 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
108 packet
= stream
.create_packet()
109 packet
.context_field
[field_name
].length
= 3
110 return packet
.context_field
[field_name
]
113 # Create a field of type array of (empty) structures.
115 # The field is part of a dummy stream, itself part of a dummy trace created
116 # from trace class `tc`. It is made out of a dummy static array field class,
117 # with a dummy struct field class as element class.
120 def _create_struct_array_field(tc
, length
):
121 elem_fc
= tc
.create_structure_field_class()
122 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
123 field_name
= 'struct_array'
124 stream
= _create_stream(tc
, [(field_name
, fc
)])
125 packet
= stream
.create_packet()
126 return packet
.context_field
[field_name
]
129 class BitArrayFieldTestCase(unittest
.TestCase
):
130 def _create_field(self
):
131 return _create_field(self
._tc
, self
._tc
.create_bit_array_field_class(24))
134 self
._tc
= get_default_trace_class()
135 self
._def
_value
= 15497
136 self
._def
= self
._create
_field
()
137 self
._def
.value_as_integer
= self
._def
_value
138 self
._def
_new
_value
= 101542
140 def test_assign_invalid_type(self
):
141 with self
.assertRaises(TypeError):
142 self
._def
.value_as_integer
= 'onze'
144 def test_assign(self
):
145 self
._def
.value_as_integer
= 199
146 self
.assertEqual(self
._def
.value_as_integer
, 199)
148 def test_assign_masked(self
):
149 self
._def
.value_as_integer
= 0xE1549BB
150 self
.assertEqual(self
._def
.value_as_integer
, 0xE1549BB & ((1 << 24) - 1))
153 other
= self
._create
_field
()
154 other
.value_as_integer
= self
._def
_value
155 self
.assertEqual(self
._def
, other
)
157 def test_ne_same_type(self
):
158 other
= self
._create
_field
()
159 other
.value_as_integer
= self
._def
_value
- 1
160 self
.assertNotEqual(self
._def
, other
)
162 def test_ne_diff_type(self
):
163 self
.assertNotEqual(self
._def
, self
._def
_value
)
166 self
.assertEqual(len(self
._def
), 24)
169 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
172 self
.assertEqual(repr(self
._def
), repr(self
._def
_value
))
175 # Base class for numeric field test cases.
177 # To be compatible with this base class, a derived class must, in its
180 # * Set `self._def` to a field object with an arbitrary value.
181 # * Set `self._def_value` to the equivalent value of `self._def`.
182 class _TestNumericField
:
183 # Tries the binary operation `op`:
185 # 1. Between `self._def`, which is a field object, and `rhs`.
186 # 2. Between `self._def_value`, which is the raw value of
187 # `self._def`, and `rhs`.
189 # Returns the results of 1. and 2.
191 # If there's an exception while performing 1. or 2., asserts that
192 # both operations raised exceptions, that both exceptions have the
193 # same type, and returns `None` for both results.
194 def _binop(self
, op
, rhs
):
199 # try with field object
201 r
= op(self
._def
, rhs
)
202 except Exception as e
:
207 rv
= op(self
._def
_value
, comp_value
)
208 except Exception as e
:
211 if type_rexc
is not None or type_rvexc
is not None:
212 # at least one of the operations raised an exception: in
213 # this case both operations should have raised the same
214 # type of exception (division by zero, bit shift with a
215 # floating point number operand, etc.)
216 self
.assertIs(type_rexc
, type_rvexc
)
221 # Tries the unary operation `op`:
223 # 1. On `self._def`, which is a field object.
224 # 2. On `self._def_value`, which is the value of `self._def`.
226 # Returns the results of 1. and 2.
228 # If there's an exception while performing 1. or 2., asserts that
229 # both operations raised exceptions, that both exceptions have the
230 # same type, and returns `None` for both results.
231 def _unaryop(self
, op
):
235 # try with field object
238 except Exception as e
:
243 rv
= op(self
._def
_value
)
244 except Exception as e
:
247 if type_rexc
is not None or type_rvexc
is not None:
248 # at least one of the operations raised an exception: in
249 # this case both operations should have raised the same
250 # type of exception (division by zero, bit shift with a
251 # floating point number operand, etc.)
252 self
.assertIs(type_rexc
, type_rvexc
)
257 # Tests that the unary operation `op` gives results with the same
258 # type for both `self._def` and `self._def_value`.
259 def _test_unaryop_type(self
, op
):
260 r
, rv
= self
._unaryop
(op
)
265 self
.assertIsInstance(r
, type(rv
))
267 # Tests that the unary operation `op` gives results with the same
268 # value for both `self._def` and `self._def_value`. This uses the
269 # __eq__() operator of `self._def`.
270 def _test_unaryop_value(self
, op
):
271 r
, rv
= self
._unaryop
(op
)
276 self
.assertEqual(r
, rv
)
278 # Tests that the unary operation `op`, when applied to `self._def`,
279 # does not change its underlying BT object address.
280 def _test_unaryop_addr_same(self
, op
):
281 addr_before
= self
._def
.addr
283 self
.assertEqual(self
._def
.addr
, addr_before
)
285 # Tests that the unary operation `op`, when applied to `self._def`,
286 # does not change its value.
287 def _test_unaryop_value_same(self
, op
):
288 value_before
= copy
.copy(self
._def
_value
)
290 self
.assertEqual(self
._def
, value_before
)
292 # Tests that the binary operation `op` gives results with the same
293 # type for both `self._def` and `self._def_value`.
294 def _test_binop_type(self
, op
, rhs
):
295 r
, rv
= self
._binop
(op
, rhs
)
300 if op
in _COMP_BINOPS
:
301 # __eq__() and __ne__() always return a 'bool' object
302 self
.assertIsInstance(r
, bool)
304 self
.assertIsInstance(r
, type(rv
))
306 # Tests that the binary operation `op` gives results with the same
307 # value for both `self._def` and `self._def_value`. This uses the
308 # __eq__() operator of `self._def`.
309 def _test_binop_value(self
, op
, rhs
):
310 r
, rv
= self
._binop
(op
, rhs
)
315 self
.assertEqual(r
, rv
)
317 # Tests that the binary operation `op`, when applied to `self._def`,
318 # does not change its underlying BT object address.
319 def _test_binop_lhs_addr_same(self
, op
, rhs
):
320 addr_before
= self
._def
.addr
321 r
, rv
= self
._binop
(op
, rhs
)
322 self
.assertEqual(self
._def
.addr
, addr_before
)
324 # Tests that the binary operation `op`, when applied to `self._def`,
325 # does not change its value.
326 @unittest.skip('copy is not implemented')
327 def _test_binop_lhs_value_same(self
, op
, rhs
):
328 value_before
= copy
.copy(self
._def
)
329 r
, rv
= self
._binop
(op
, rhs
)
330 self
.assertEqual(self
._def
, value_before
)
332 # The methods below which take the `test_cb` and/or `op` parameters
333 # are meant to be used with one of the _test_binop_*() functions
334 # above as `test_cb` and a binary operator function as `op`.
338 # self._test_binop_rhs_pos_int(self._test_binop_value,
341 # This tests that a numeric field object added to a positive integer
342 # value gives a result with the expected value.
344 # `vint` and `vfloat` mean a signed integer value object and a real
347 def _test_binop_invalid_unknown(self
, op
):
348 if op
in _COMP_BINOPS
:
349 self
.skipTest('not testing')
354 with self
.assertRaises(TypeError):
357 def _test_binop_invalid_none(self
, op
):
358 if op
in _COMP_BINOPS
:
359 self
.skipTest('not testing')
361 with self
.assertRaises(TypeError):
364 def _test_binop_rhs_false(self
, test_cb
, op
):
367 def _test_binop_rhs_true(self
, test_cb
, op
):
370 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
373 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
376 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
379 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
380 test_cb(op
, bt2
.create_value(2))
382 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
383 test_cb(op
, bt2
.create_value(-23))
385 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
386 test_cb(op
, bt2
.create_value(0))
388 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
391 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
394 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
397 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
398 test_cb(op
, bt2
.create_value(2.2))
400 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
401 test_cb(op
, bt2
.create_value(-23.4))
403 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
404 test_cb(op
, bt2
.create_value(0.0))
406 def _test_binop_rhs_complex(self
, test_cb
, op
):
407 test_cb(op
, -23 + 19j
)
409 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
412 def _test_binop_type_false(self
, op
):
413 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
415 def _test_binop_type_true(self
, op
):
416 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
418 def _test_binop_type_pos_int(self
, op
):
419 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
421 def _test_binop_type_neg_int(self
, op
):
422 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
424 def _test_binop_type_zero_int(self
, op
):
425 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
427 def _test_binop_type_pos_vint(self
, op
):
428 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
430 def _test_binop_type_neg_vint(self
, op
):
431 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
433 def _test_binop_type_zero_vint(self
, op
):
434 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
436 def _test_binop_type_pos_float(self
, op
):
437 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
439 def _test_binop_type_neg_float(self
, op
):
440 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
442 def _test_binop_type_zero_float(self
, op
):
443 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
445 def _test_binop_type_pos_vfloat(self
, op
):
446 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
448 def _test_binop_type_neg_vfloat(self
, op
):
449 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
451 def _test_binop_type_zero_vfloat(self
, op
):
452 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
454 def _test_binop_type_complex(self
, op
):
455 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
457 def _test_binop_type_zero_complex(self
, op
):
458 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
460 def _test_binop_value_false(self
, op
):
461 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
463 def _test_binop_value_true(self
, op
):
464 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
466 def _test_binop_value_pos_int(self
, op
):
467 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
469 def _test_binop_value_neg_int(self
, op
):
470 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
472 def _test_binop_value_zero_int(self
, op
):
473 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
475 def _test_binop_value_pos_vint(self
, op
):
476 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
478 def _test_binop_value_neg_vint(self
, op
):
479 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
481 def _test_binop_value_zero_vint(self
, op
):
482 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
484 def _test_binop_value_pos_float(self
, op
):
485 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
487 def _test_binop_value_neg_float(self
, op
):
488 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
490 def _test_binop_value_zero_float(self
, op
):
491 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
493 def _test_binop_value_pos_vfloat(self
, op
):
494 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
496 def _test_binop_value_neg_vfloat(self
, op
):
497 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
499 def _test_binop_value_zero_vfloat(self
, op
):
500 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
502 def _test_binop_value_complex(self
, op
):
503 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
505 def _test_binop_value_zero_complex(self
, op
):
506 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
508 def _test_binop_lhs_addr_same_false(self
, op
):
509 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
511 def _test_binop_lhs_addr_same_true(self
, op
):
512 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
514 def _test_binop_lhs_addr_same_pos_int(self
, op
):
515 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
517 def _test_binop_lhs_addr_same_neg_int(self
, op
):
518 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
520 def _test_binop_lhs_addr_same_zero_int(self
, op
):
521 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
523 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
524 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
526 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
527 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
529 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
530 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
532 def _test_binop_lhs_addr_same_pos_float(self
, op
):
533 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
535 def _test_binop_lhs_addr_same_neg_float(self
, op
):
536 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
538 def _test_binop_lhs_addr_same_zero_float(self
, op
):
539 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
541 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
542 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
544 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
545 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
547 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
548 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
550 def _test_binop_lhs_addr_same_complex(self
, op
):
551 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
553 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
554 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
556 def _test_binop_lhs_value_same_false(self
, op
):
557 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
559 def _test_binop_lhs_value_same_true(self
, op
):
560 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
562 def _test_binop_lhs_value_same_pos_int(self
, op
):
563 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
565 def _test_binop_lhs_value_same_neg_int(self
, op
):
566 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
568 def _test_binop_lhs_value_same_zero_int(self
, op
):
569 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
571 def _test_binop_lhs_value_same_pos_vint(self
, op
):
572 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
574 def _test_binop_lhs_value_same_neg_vint(self
, op
):
575 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
577 def _test_binop_lhs_value_same_zero_vint(self
, op
):
578 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
580 def _test_binop_lhs_value_same_pos_float(self
, op
):
581 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
583 def _test_binop_lhs_value_same_neg_float(self
, op
):
584 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
586 def _test_binop_lhs_value_same_zero_float(self
, op
):
587 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
589 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
590 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
592 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
593 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
595 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
596 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
598 def _test_binop_lhs_value_same_complex(self
, op
):
599 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
601 def _test_binop_lhs_value_same_zero_complex(self
, op
):
602 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
604 def test_bool_op(self
):
605 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
607 def test_int_op(self
):
608 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
610 def test_float_op(self
):
611 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
613 def test_complex_op(self
):
614 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
616 def test_str_op(self
):
617 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
619 def test_eq_none(self
):
620 # Ignore this lint error:
621 # E711 comparison to None should be 'if cond is None:'
622 # since this is what we want to test (even though not good practice).
623 self
.assertFalse(self
._def
== None) # noqa: E711
625 def test_ne_none(self
):
626 # Ignore this lint error:
627 # E711 comparison to None should be 'if cond is not None:'
628 # since this is what we want to test (even though not good practice).
629 self
.assertTrue(self
._def
!= None) # noqa: E711
632 # This is a list of binary operators used for
633 # _inject_numeric_testing_methods().
635 # Each entry is a pair of binary operator name (used as part of the
636 # created testing method's name) and operator function.
644 ('add', operator
.add
),
645 ('radd', lambda a
, b
: operator
.add(b
, a
)),
646 ('and', operator
.and_
),
647 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
648 ('floordiv', operator
.floordiv
),
649 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
650 ('lshift', operator
.lshift
),
651 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
652 ('mod', operator
.mod
),
653 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
654 ('mul', operator
.mul
),
655 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
656 ('or', operator
.or_
),
657 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
658 ('pow', operator
.pow),
659 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
660 ('rshift', operator
.rshift
),
661 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
662 ('sub', operator
.sub
),
663 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
664 ('truediv', operator
.truediv
),
665 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
666 ('xor', operator
.xor
),
667 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
671 # This is a list of unary operators used for
672 # _inject_numeric_testing_methods().
674 # Each entry is a pair of unary operator name (used as part of the
675 # created testing method's name) and operator function.
677 ('neg', operator
.neg
),
678 ('pos', operator
.pos
),
679 ('abs', operator
.abs),
680 ('invert', operator
.invert
),
682 ('round_0', partial(round, ndigits
=0)),
683 ('round_1', partial(round, ndigits
=1)),
684 ('round_2', partial(round, ndigits
=2)),
685 ('round_3', partial(round, ndigits
=3)),
687 ('floor', math
.floor
),
688 ('trunc', math
.trunc
),
692 # This function injects a bunch of testing methods to a numeric
695 # It is meant to be used like this:
697 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
699 # This function injects:
701 # * One testing method for each _TestNumericField._test_binop_*()
702 # method, for each binary operator in the _BINOPS tuple.
704 # * One testing method for each _TestNumericField._test_unaryop*()
705 # method, for each unary operator in the _UNARYOPS tuple.
706 def _inject_numeric_testing_methods(cls
):
707 def test_binop_name(suffix
):
708 return 'test_binop_{}_{}'.format(name
, suffix
)
710 def test_unaryop_name(suffix
):
711 return 'test_unaryop_{}_{}'.format(name
, suffix
)
713 # inject testing methods for each binary operation
714 for name
, binop
in _BINOPS
:
717 test_binop_name('invalid_unknown'),
718 partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
),
722 test_binop_name('invalid_none'),
723 partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
),
727 test_binop_name('type_true'),
728 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
732 test_binop_name('type_pos_int'),
733 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
737 test_binop_name('type_pos_vint'),
738 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
742 test_binop_name('value_true'),
743 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
747 test_binop_name('value_pos_int'),
748 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
752 test_binop_name('value_pos_vint'),
753 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
757 test_binop_name('lhs_addr_same_true'),
758 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
762 test_binop_name('lhs_addr_same_pos_int'),
764 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
769 test_binop_name('lhs_addr_same_pos_vint'),
771 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
776 test_binop_name('lhs_value_same_true'),
777 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
781 test_binop_name('lhs_value_same_pos_int'),
783 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
788 test_binop_name('lhs_value_same_pos_vint'),
790 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
795 test_binop_name('type_neg_int'),
796 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
800 test_binop_name('type_neg_vint'),
801 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
805 test_binop_name('value_neg_int'),
806 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
810 test_binop_name('value_neg_vint'),
811 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
815 test_binop_name('lhs_addr_same_neg_int'),
817 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
822 test_binop_name('lhs_addr_same_neg_vint'),
824 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
829 test_binop_name('lhs_value_same_neg_int'),
831 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
836 test_binop_name('lhs_value_same_neg_vint'),
838 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
843 test_binop_name('type_false'),
844 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
848 test_binop_name('type_zero_int'),
849 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
853 test_binop_name('type_zero_vint'),
854 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
858 test_binop_name('value_false'),
859 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
863 test_binop_name('value_zero_int'),
864 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
868 test_binop_name('value_zero_vint'),
869 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
873 test_binop_name('lhs_addr_same_false'),
874 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
878 test_binop_name('lhs_addr_same_zero_int'),
880 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
885 test_binop_name('lhs_addr_same_zero_vint'),
887 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
892 test_binop_name('lhs_value_same_false'),
893 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
897 test_binop_name('lhs_value_same_zero_int'),
899 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
904 test_binop_name('lhs_value_same_zero_vint'),
906 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
911 test_binop_name('type_pos_float'),
912 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
916 test_binop_name('type_neg_float'),
917 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
921 test_binop_name('type_pos_vfloat'),
922 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
926 test_binop_name('type_neg_vfloat'),
927 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
931 test_binop_name('value_pos_float'),
932 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
936 test_binop_name('value_neg_float'),
937 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
941 test_binop_name('value_pos_vfloat'),
942 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
946 test_binop_name('value_neg_vfloat'),
947 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
951 test_binop_name('lhs_addr_same_pos_float'),
953 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
958 test_binop_name('lhs_addr_same_neg_float'),
960 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
965 test_binop_name('lhs_addr_same_pos_vfloat'),
967 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
972 test_binop_name('lhs_addr_same_neg_vfloat'),
974 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
979 test_binop_name('lhs_value_same_pos_float'),
981 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
986 test_binop_name('lhs_value_same_neg_float'),
988 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
993 test_binop_name('lhs_value_same_pos_vfloat'),
995 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
1000 test_binop_name('lhs_value_same_neg_vfloat'),
1002 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
1007 test_binop_name('type_zero_float'),
1008 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
1012 test_binop_name('type_zero_vfloat'),
1013 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
1017 test_binop_name('value_zero_float'),
1018 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
1022 test_binop_name('value_zero_vfloat'),
1023 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
1027 test_binop_name('lhs_addr_same_zero_float'),
1029 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
1034 test_binop_name('lhs_addr_same_zero_vfloat'),
1036 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
1041 test_binop_name('lhs_value_same_zero_float'),
1043 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1048 test_binop_name('lhs_value_same_zero_vfloat'),
1050 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1055 test_binop_name('type_complex'),
1056 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1060 test_binop_name('type_zero_complex'),
1061 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1065 test_binop_name('value_complex'),
1066 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1070 test_binop_name('value_zero_complex'),
1071 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1075 test_binop_name('lhs_addr_same_complex'),
1077 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1082 test_binop_name('lhs_addr_same_zero_complex'),
1084 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1089 test_binop_name('lhs_value_same_complex'),
1091 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1096 test_binop_name('lhs_value_same_zero_complex'),
1098 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1102 # inject testing methods for each unary operation
1103 for name
, unaryop
in _UNARYOPS
:
1106 test_unaryop_name('type'),
1107 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1111 test_unaryop_name('value'),
1112 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1116 test_unaryop_name('addr_same'),
1117 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1121 test_unaryop_name('value_same'),
1122 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1126 class BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1127 def _create_fc(self
, tc
):
1128 return tc
.create_bool_field_class()
1131 self
._tc
= get_default_trace_class()
1132 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1133 self
._def
.value
= True
1134 self
._def
_value
= True
1135 self
._def
_new
_value
= False
1137 def test_assign_true(self
):
1139 self
._def
.value
= raw
1140 self
.assertEqual(self
._def
, raw
)
1142 def test_assign_false(self
):
1144 self
._def
.value
= raw
1145 self
.assertEqual(self
._def
, raw
)
1147 def test_assign_field_true(self
):
1148 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1151 self
._def
.value
= field
1152 self
.assertEqual(self
._def
, raw
)
1154 def test_assign_field_false(self
):
1155 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1158 self
._def
.value
= field
1159 self
.assertEqual(self
._def
, raw
)
1161 def test_assign_invalid_type(self
):
1162 with self
.assertRaises(TypeError):
1163 self
._def
.value
= 17
1165 def test_str_op(self
):
1166 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1169 _inject_numeric_testing_methods(BoolFieldTestCase
)
1172 class _TestIntegerFieldCommon(_TestNumericField
):
1173 def test_assign_true(self
):
1175 self
._def
.value
= raw
1176 self
.assertEqual(self
._def
, raw
)
1178 def test_assign_false(self
):
1180 self
._def
.value
= raw
1181 self
.assertEqual(self
._def
, raw
)
1183 def test_assign_pos_int(self
):
1185 self
._def
.value
= raw
1186 self
.assertEqual(self
._def
, raw
)
1188 def test_assign_neg_int(self
):
1190 self
._def
.value
= raw
1191 self
.assertEqual(self
._def
, raw
)
1193 def test_assign_int_field(self
):
1195 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1197 self
._def
.value
= field
1198 self
.assertEqual(self
._def
, raw
)
1200 def test_assign_invalid_type(self
):
1201 with self
.assertRaises(TypeError):
1202 self
._def
.value
= 'yes'
1204 def test_assign_uint(self
):
1205 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1206 field
= _create_field(self
._tc
, uint_fc
)
1209 self
.assertEqual(field
, raw
)
1211 def test_assign_big_uint(self
):
1212 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1213 field
= _create_field(self
._tc
, uint_fc
)
1214 # Larger than the IEEE 754 double-precision exact representation of
1217 field
.value
= (2 ** 53) + 1
1218 self
.assertEqual(field
, raw
)
1220 def test_assign_uint_invalid_neg(self
):
1221 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1222 field
= _create_field(self
._tc
, uint_fc
)
1224 with self
.assertRaises(ValueError):
1227 def test_str_op(self
):
1228 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1231 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1234 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1235 def _create_fc(self
, tc
):
1236 return tc
.create_signed_integer_field_class(25)
1239 self
._tc
= get_default_trace_class()
1240 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1241 self
._field
.value
= 17
1242 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1243 self
._def
.value
= 17
1244 self
._def
_value
= 17
1245 self
._def
_new
_value
= -101
1248 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1249 def _create_fc(self
, tc
):
1250 fc
= tc
.create_signed_enumeration_field_class(32)
1251 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1252 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1253 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1255 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1257 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1261 self
._tc
= get_default_trace_class()
1262 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1263 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1264 self
._def
.value
= 17
1265 self
._def
_value
= 17
1266 self
._def
_new
_value
= -101
1268 def test_str_op(self
):
1269 expected_string_found
= False
1272 # Establish all permutations of the three expected matches since
1273 # the order in which mappings are enumerated is not explicitly part of
1275 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1276 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1278 expected_string_found
= True
1281 self
.assertTrue(expected_string_found
)
1283 def test_labels(self
):
1284 self
._field
.value
= 17
1285 labels
= sorted(self
._field
.labels
)
1286 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1289 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1290 def _create_fc(self
, tc
):
1291 return tc
.create_real_field_class()
1294 self
._tc
= get_default_trace_class()
1295 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1296 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1297 self
._def
.value
= 52.7
1298 self
._def
_value
= 52.7
1299 self
._def
_new
_value
= -17.164857
1301 def _test_invalid_op(self
, cb
):
1302 with self
.assertRaises(TypeError):
1305 def test_assign_true(self
):
1306 self
._def
.value
= True
1307 self
.assertTrue(self
._def
)
1309 def test_assign_false(self
):
1310 self
._def
.value
= False
1311 self
.assertFalse(self
._def
)
1313 def test_assign_pos_int(self
):
1315 self
._def
.value
= raw
1316 self
.assertEqual(self
._def
, float(raw
))
1318 def test_assign_neg_int(self
):
1320 self
._def
.value
= raw
1321 self
.assertEqual(self
._def
, float(raw
))
1323 def test_assign_int_field(self
):
1324 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1325 int_field
= _create_field(self
._tc
, int_fc
)
1327 int_field
.value
= raw
1328 self
._def
.value
= int_field
1329 self
.assertEqual(self
._def
, float(raw
))
1331 def test_assign_float(self
):
1333 self
._def
.value
= raw
1334 self
.assertEqual(self
._def
, raw
)
1336 def test_assign_float_field(self
):
1337 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1340 self
._def
.value
= field
1341 self
.assertEqual(self
._def
, raw
)
1343 def test_assign_invalid_type(self
):
1344 with self
.assertRaises(TypeError):
1345 self
._def
.value
= 'yes'
1347 def test_invalid_lshift(self
):
1348 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1350 def test_invalid_rshift(self
):
1351 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1353 def test_invalid_and(self
):
1354 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1356 def test_invalid_or(self
):
1357 self
._test
_invalid
_op
(lambda: self
._def |
23)
1359 def test_invalid_xor(self
):
1360 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1362 def test_invalid_invert(self
):
1363 self
._test
_invalid
_op
(lambda: ~self
._def
)
1365 def test_str_op(self
):
1366 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1369 _inject_numeric_testing_methods(RealFieldTestCase
)
1372 class StringFieldTestCase(unittest
.TestCase
):
1374 self
._tc
= get_default_trace_class()
1375 self
._def
_value
= 'Hello, World!'
1376 self
._def
= _create_string_field(self
._tc
)
1377 self
._def
.value
= self
._def
_value
1378 self
._def
_new
_value
= 'Yes!'
1380 def test_assign_int(self
):
1381 with self
.assertRaises(TypeError):
1382 self
._def
.value
= 283
1384 def test_assign_string_field(self
):
1385 field
= _create_string_field(self
._tc
)
1388 self
.assertEqual(field
, raw
)
1391 self
.assertEqual(self
._def
, self
._def
_value
)
1393 def test_not_eq(self
):
1394 self
.assertNotEqual(self
._def
, 23)
1396 def test_lt_vstring(self
):
1397 s1
= _create_string_field(self
._tc
)
1399 s2
= _create_string_field(self
._tc
)
1401 self
.assertLess(s1
, s2
)
1403 def test_lt_string(self
):
1404 s1
= _create_string_field(self
._tc
)
1406 self
.assertLess(s1
, 'bateau')
1408 def test_le_vstring(self
):
1409 s1
= _create_string_field(self
._tc
)
1411 s2
= _create_string_field(self
._tc
)
1413 self
.assertLessEqual(s1
, s2
)
1415 def test_le_string(self
):
1416 s1
= _create_string_field(self
._tc
)
1418 self
.assertLessEqual(s1
, 'bateau')
1420 def test_gt_vstring(self
):
1421 s1
= _create_string_field(self
._tc
)
1423 s2
= _create_string_field(self
._tc
)
1425 self
.assertGreater(s2
, s1
)
1427 def test_gt_string(self
):
1428 s1
= _create_string_field(self
._tc
)
1430 self
.assertGreater('bateau', s1
)
1432 def test_ge_vstring(self
):
1433 s1
= _create_string_field(self
._tc
)
1435 s2
= _create_string_field(self
._tc
)
1437 self
.assertGreaterEqual(s2
, s1
)
1439 def test_ge_string(self
):
1440 s1
= _create_string_field(self
._tc
)
1442 self
.assertGreaterEqual('bateau', s1
)
1444 def test_bool_op(self
):
1445 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1447 def test_str_op(self
):
1448 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1451 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1453 def test_getitem(self
):
1454 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1456 def test_append_str(self
):
1457 to_append
= 'meow meow meow'
1458 self
._def
+= to_append
1459 self
._def
_value
+= to_append
1460 self
.assertEqual(self
._def
, self
._def
_value
)
1462 def test_append_string_field(self
):
1463 field
= _create_string_field(self
._tc
)
1464 to_append
= 'meow meow meow'
1465 field
.value
= to_append
1467 self
._def
_value
+= to_append
1468 self
.assertEqual(self
._def
, self
._def
_value
)
1471 class _TestArrayFieldCommon
:
1472 def _modify_def(self
):
1475 def test_bool_op_true(self
):
1476 self
.assertTrue(self
._def
)
1479 self
.assertEqual(len(self
._def
), 3)
1481 def test_length(self
):
1482 self
.assertEqual(self
._def
.length
, 3)
1484 def test_getitem(self
):
1485 field
= self
._def
[1]
1486 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1487 self
.assertEqual(field
, 1847)
1490 field
= _create_int_array_field(self
._tc
, 3)
1494 self
.assertEqual(self
._def
, field
)
1496 def test_eq_invalid_type(self
):
1497 self
.assertNotEqual(self
._def
, 23)
1499 def test_eq_diff_len(self
):
1500 field
= _create_int_array_field(self
._tc
, 2)
1503 self
.assertNotEqual(self
._def
, field
)
1505 def test_eq_diff_content_same_len(self
):
1506 field
= _create_int_array_field(self
._tc
, 3)
1510 self
.assertNotEqual(self
._def
, field
)
1512 def test_eq_non_sequence_iterable(self
):
1513 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1514 field
= _create_int_array_field(self
._tc
, 3)
1518 self
.assertEqual(field
, list(dct
.keys()))
1519 self
.assertNotEqual(field
, dct
)
1521 def test_setitem(self
):
1523 self
.assertEqual(self
._def
[2], 24)
1525 def test_setitem_int_field(self
):
1526 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1527 int_field
= _create_field(self
._tc
, int_fc
)
1528 int_field
.value
= 19487
1529 self
._def
[1] = int_field
1530 self
.assertEqual(self
._def
[1], 19487)
1532 def test_setitem_non_basic_field(self
):
1533 array_field
= _create_struct_array_field(self
._tc
, 2)
1534 with self
.assertRaises(TypeError):
1537 def test_setitem_none(self
):
1538 with self
.assertRaises(TypeError):
1541 def test_setitem_index_wrong_type(self
):
1542 with self
.assertRaises(TypeError):
1543 self
._def
['yes'] = 23
1545 def test_setitem_index_neg(self
):
1546 with self
.assertRaises(IndexError):
1549 def test_setitem_index_out_of_range(self
):
1550 with self
.assertRaises(IndexError):
1551 self
._def
[len(self
._def
)] = 134679
1553 def test_iter(self
):
1554 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1555 self
.assertEqual(field
, value
)
1557 def test_value_int_field(self
):
1558 values
= [45646, 145, 12145]
1559 self
._def
.value
= values
1560 self
.assertEqual(values
, self
._def
)
1562 def test_value_check_sequence(self
):
1564 with self
.assertRaises(TypeError):
1565 self
._def
.value
= values
1567 def test_value_wrong_type_in_sequence(self
):
1568 values
= [32, 'hello', 11]
1569 with self
.assertRaises(TypeError):
1570 self
._def
.value
= values
1572 def test_value_complex_type(self
):
1573 struct_fc
= self
._tc
.create_structure_field_class()
1574 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1575 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1576 str_fc
= self
._tc
.create_string_field_class()
1577 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1578 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1579 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1580 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1581 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1583 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1584 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1585 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1588 array
= stream
.create_packet().context_field
['array_field']
1589 array
.value
= values
1590 self
.assertEqual(values
, array
)
1591 values
[0]['an_int'] = 'a string'
1592 with self
.assertRaises(TypeError):
1593 array
.value
= values
1595 def test_str_op(self
):
1597 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1598 self
.assertEqual(expected_string
, s
)
1601 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1603 self
._tc
= get_default_trace_class()
1604 self
._def
= _create_int_array_field(self
._tc
, 3)
1607 self
._def
[2] = 1948754
1608 self
._def
_value
= [45, 1847, 1948754]
1610 def test_value_wrong_len(self
):
1612 with self
.assertRaises(ValueError):
1613 self
._def
.value
= values
1616 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1618 self
._tc
= get_default_trace_class()
1619 self
._def
= _create_dynamic_array(self
._tc
)
1622 self
._def
[2] = 1948754
1623 self
._def
_value
= [45, 1847, 1948754]
1625 def test_value_resize(self
):
1626 new_values
= [1, 2, 3, 4]
1627 self
._def
.value
= new_values
1628 self
.assertCountEqual(self
._def
, new_values
)
1630 def test_set_length(self
):
1631 self
._def
.length
= 4
1633 self
.assertEqual(len(self
._def
), 4)
1635 def test_set_invalid_length(self
):
1636 with self
.assertRaises(TypeError):
1637 self
._def
.length
= 'cheval'
1640 class StructureFieldTestCase(unittest
.TestCase
):
1641 def _create_fc(self
, tc
):
1642 fc
= tc
.create_structure_field_class()
1643 fc
.append_member('A', self
._fc
0_fn
())
1644 fc
.append_member('B', self
._fc
1_fn
())
1645 fc
.append_member('C', self
._fc
2_fn
())
1646 fc
.append_member('D', self
._fc
3_fn
())
1647 fc
.append_member('E', self
._fc
4_fn
())
1648 fc5
= self
._fc
5_fn
()
1649 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1650 fc
.append_member('F', fc5
)
1654 self
._tc
= get_default_trace_class()
1655 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1656 self
._fc
1_fn
= self
._tc
.create_string_field_class
1657 self
._fc
2_fn
= self
._tc
.create_real_field_class
1658 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1659 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1660 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1661 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1663 self
._fc
= self
._create
_fc
(self
._tc
)
1664 self
._def
= _create_field(self
._tc
, self
._fc
)
1665 self
._def
['A'] = -1872
1666 self
._def
['B'] = 'salut'
1667 self
._def
['C'] = 17.5
1668 self
._def
['D'] = 16497
1670 self
._def
['F'] = {'F_1': 52}
1680 def _modify_def(self
):
1681 self
._def
['B'] = 'hola'
1683 def test_bool_op_true(self
):
1684 self
.assertTrue(self
._def
)
1686 def test_bool_op_false(self
):
1687 field
= self
._def
['E']
1688 self
.assertFalse(field
)
1691 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1693 def test_getitem(self
):
1694 field
= self
._def
['A']
1695 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1696 self
.assertEqual(field
, -1872)
1698 def test_member_at_index_out_of_bounds_after(self
):
1699 with self
.assertRaises(IndexError):
1700 self
._def
.member_at_index(len(self
._def
_value
))
1703 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1705 field
['B'] = 'salut'
1709 field
['F'] = {'F_1': 52}
1710 self
.assertEqual(self
._def
, field
)
1712 def test_eq_invalid_type(self
):
1713 self
.assertNotEqual(self
._def
, 23)
1715 def test_eq_diff_len(self
):
1716 fc
= self
._tc
.create_structure_field_class()
1717 fc
.append_member('A', self
._fc
0_fn
())
1718 fc
.append_member('B', self
._fc
1_fn
())
1719 fc
.append_member('C', self
._fc
2_fn
())
1721 field
= _create_field(self
._tc
, fc
)
1723 field
['B'] = 'salut'
1725 self
.assertNotEqual(self
._def
, field
)
1727 def test_eq_diff_keys(self
):
1728 fc
= self
._tc
.create_structure_field_class()
1729 fc
.append_member('U', self
._fc
0_fn
())
1730 fc
.append_member('V', self
._fc
1_fn
())
1731 fc
.append_member('W', self
._fc
2_fn
())
1732 fc
.append_member('X', self
._fc
3_fn
())
1733 fc
.append_member('Y', self
._fc
4_fn
())
1734 fc
.append_member('Z', self
._fc
5_fn
())
1735 field
= _create_field(self
._tc
, fc
)
1737 field
['V'] = "gerry"
1742 self
.assertNotEqual(self
._def
, field
)
1744 def test_eq_diff_content_same_len(self
):
1745 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1747 field
['B'] = 'salut'
1751 field
['F'] = {'F_1': 0}
1752 self
.assertNotEqual(self
._def
, field
)
1754 def test_eq_same_content_diff_keys(self
):
1755 fc
= self
._tc
.create_structure_field_class()
1756 fc
.append_member('A', self
._fc
0_fn
())
1757 fc
.append_member('B', self
._fc
1_fn
())
1758 fc
.append_member('E', self
._fc
2_fn
())
1759 fc
.append_member('D', self
._fc
3_fn
())
1760 fc
.append_member('C', self
._fc
4_fn
())
1761 fc
.append_member('F', self
._fc
5_fn
())
1762 field
= _create_field(self
._tc
, fc
)
1764 field
['B'] = 'salut'
1769 self
.assertNotEqual(self
._def
, field
)
1771 def test_setitem(self
):
1772 self
._def
['C'] = -18.47
1773 self
.assertEqual(self
._def
['C'], -18.47)
1775 def test_setitem_int_field(self
):
1776 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1777 int_field
= _create_field(self
._tc
, int_fc
)
1778 int_field
.value
= 19487
1779 self
._def
['D'] = int_field
1780 self
.assertEqual(self
._def
['D'], 19487)
1782 def test_setitem_non_basic_field(self
):
1783 elem_fc
= self
._tc
.create_structure_field_class()
1784 struct_fc
= self
._tc
.create_structure_field_class()
1785 struct_fc
.append_member('A', elem_fc
)
1786 struct_field
= _create_field(self
._tc
, struct_fc
)
1788 # Will fail on access to .items() of the value
1789 with self
.assertRaises(AttributeError):
1790 struct_field
['A'] = 23
1792 def test_setitem_none(self
):
1793 with self
.assertRaises(TypeError):
1794 self
._def
['C'] = None
1796 def test_setitem_key_wrong_type(self
):
1797 with self
.assertRaises(TypeError):
1800 def test_setitem_wrong_key(self
):
1801 with self
.assertRaises(KeyError):
1802 self
._def
['hi'] = 134679
1804 def test_member_at_index(self
):
1805 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1807 def test_iter(self
):
1817 for vkey
, vval
in self
._def
.items():
1818 val
= orig_values
[vkey
]
1819 self
.assertEqual(vval
, val
)
1821 def test_value(self
):
1830 self
.assertEqual(self
._def
, orig_values
)
1832 def test_set_value(self
):
1833 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1834 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1835 str_fc
= self
._tc
.create_string_field_class()
1836 struct_fc
= self
._tc
.create_structure_field_class()
1837 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1838 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1839 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1840 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
1842 struct
= _create_field(self
._tc
, struct_fc
)
1843 struct
.value
= values
1844 self
.assertEqual(values
, struct
)
1846 bad_type_values
= copy
.deepcopy(values
)
1847 bad_type_values
['an_int'] = 'a string'
1848 with self
.assertRaises(TypeError):
1849 struct
.value
= bad_type_values
1851 unknown_key_values
= copy
.deepcopy(values
)
1852 unknown_key_values
['unknown_key'] = 16546
1853 with self
.assertRaises(KeyError):
1854 struct
.value
= unknown_key_values
1856 def test_str_op(self
):
1857 expected_string_found
= False
1859 # Establish all permutations of the three expected matches since
1860 # the order in which mappings are enumerated is not explicitly part of
1862 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1863 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1864 candidate
= '{{{}}}'.format(', '.join(items
))
1866 expected_string_found
= True
1869 self
.assertTrue(expected_string_found
)
1872 class OptionFieldTestCase(unittest
.TestCase
):
1873 def _create_fc(self
, tc
):
1874 fc
= tc
.create_option_field_class(tc
.create_string_field_class())
1875 top_fc
= tc
.create_structure_field_class()
1876 top_fc
.append_member('opt_field', fc
)
1880 self
._tc
= get_default_trace_class()
1881 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1882 self
._def
= fld
['opt_field']
1884 def test_value_prop(self
):
1885 self
._def
.value
= 'hiboux'
1886 self
.assertEqual(self
._def
.field
, 'hiboux')
1887 self
.assertTrue(self
._def
.has_field
)
1889 def test_has_field_prop_true(self
):
1890 self
._def
.has_field
= True
1891 self
.assertTrue(self
._def
.has_field
)
1893 def test_has_field_prop_true(self
):
1894 self
._def
.has_field
= False
1895 self
.assertFalse(self
._def
.has_field
)
1897 def test_bool_op_true(self
):
1898 self
._def
.value
= 'allo'
1899 self
.assertTrue(self
._def
)
1901 def test_bool_op_true(self
):
1902 self
._def
.has_field
= False
1903 self
.assertFalse(self
._def
)
1905 def test_field_prop_existing(self
):
1906 self
._def
.value
= 'meow'
1907 field
= self
._def
.field
1908 self
.assertEqual(field
, 'meow')
1910 def test_field_prop_none(self
):
1911 self
._def
.has_field
= False
1912 field
= self
._def
.field
1913 self
.assertIsNone(field
)
1915 def test_field_prop_existing_then_none(self
):
1916 self
._def
.value
= 'meow'
1917 field
= self
._def
.field
1918 self
.assertEqual(field
, 'meow')
1919 self
._def
.has_field
= False
1920 field
= self
._def
.field
1921 self
.assertIsNone(field
)
1924 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1925 field
= field
['opt_field']
1926 field
.value
= 'walk'
1927 self
._def
.value
= 'walk'
1928 self
.assertEqual(self
._def
, field
)
1930 def test_eq_invalid_type(self
):
1931 self
._def
.value
= 'gerry'
1932 self
.assertNotEqual(self
._def
, 23)
1934 def test_str_op(self
):
1935 self
._def
.value
= 'marcel'
1936 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
1938 def test_repr_op(self
):
1939 self
._def
.value
= 'mireille'
1940 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
1943 class VariantFieldTestCase(unittest
.TestCase
):
1944 def _create_fc(self
, tc
):
1945 ft0
= tc
.create_signed_integer_field_class(32)
1946 ft1
= tc
.create_string_field_class()
1947 ft2
= tc
.create_real_field_class()
1948 ft3
= tc
.create_signed_integer_field_class(17)
1949 fc
= tc
.create_variant_field_class()
1950 fc
.append_option('corner', ft0
)
1951 fc
.append_option('zoom', ft1
)
1952 fc
.append_option('mellotron', ft2
)
1953 fc
.append_option('giorgio', ft3
)
1954 top_fc
= tc
.create_structure_field_class()
1955 top_fc
.append_member('variant_field', fc
)
1959 self
._tc
= get_default_trace_class()
1960 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1961 self
._def
= fld
['variant_field']
1963 def test_bool_op(self
):
1964 self
._def
.selected_option_index
= 2
1965 self
._def
.value
= -17.34
1966 with self
.assertRaises(NotImplementedError):
1969 def test_selected_option_index(self
):
1970 self
._def
.selected_option_index
= 2
1971 self
.assertEqual(self
._def
.selected_option_index
, 2)
1973 def test_selected_option(self
):
1974 self
._def
.selected_option_index
= 2
1975 self
._def
.value
= -17.34
1976 self
.assertEqual(self
._def
.selected_option
, -17.34)
1978 self
._def
.selected_option_index
= 3
1979 self
._def
.value
= 1921
1980 self
.assertEqual(self
._def
.selected_option
, 1921)
1983 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1984 field
= field
['variant_field']
1985 field
.selected_option_index
= 0
1987 self
._def
.selected_option_index
= 0
1988 self
._def
.value
= 1774
1989 self
.assertEqual(self
._def
, field
)
1992 self
.assertEqual(len(self
._def
), 4)
1994 def test_eq_invalid_type(self
):
1995 self
._def
.selected_option_index
= 1
1996 self
._def
.value
= 'gerry'
1997 self
.assertNotEqual(self
._def
, 23)
1999 def test_str_op_int(self
):
2000 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2001 field
= field
['variant_field']
2002 field
.selected_option_index
= 0
2004 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2005 other_field
= other_field
['variant_field']
2006 other_field
.selected_option_index
= 0
2007 other_field
.value
= 1774
2008 self
.assertEqual(str(field
), str(other_field
))
2010 def test_str_op_str(self
):
2011 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2012 field
= field
['variant_field']
2013 field
.selected_option_index
= 1
2014 field
.value
= 'un beau grand bateau'
2015 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2016 other_field
= other_field
['variant_field']
2017 other_field
.selected_option_index
= 1
2018 other_field
.value
= 'un beau grand bateau'
2019 self
.assertEqual(str(field
), str(other_field
))
2021 def test_str_op_float(self
):
2022 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2023 field
= field
['variant_field']
2024 field
.selected_option_index
= 2
2025 field
.value
= 14.4245
2026 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2027 other_field
= other_field
['variant_field']
2028 other_field
.selected_option_index
= 2
2029 other_field
.value
= 14.4245
2030 self
.assertEqual(str(field
), str(other_field
))