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
, create_const_field
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 def _test_binop_lhs_value_same(self
, op
, rhs
):
327 value_before
= copy
.copy(self
._def
_value
)
328 r
, rv
= self
._binop
(op
, rhs
)
329 self
.assertEqual(self
._def
, value_before
)
331 # The methods below which take the `test_cb` and/or `op` parameters
332 # are meant to be used with one of the _test_binop_*() functions
333 # above as `test_cb` and a binary operator function as `op`.
337 # self._test_binop_rhs_pos_int(self._test_binop_value,
340 # This tests that a numeric field object added to a positive integer
341 # value gives a result with the expected value.
343 # `vint` and `vfloat` mean a signed integer value object and a real
346 def _test_binop_unknown(self
, op
):
350 # Operators == and != are defined when comparing the field to an
352 if op
is operator
.eq
:
353 self
.assertIs(op(self
._def
, A()), False)
354 elif op
is operator
.ne
:
355 self
.assertIs(op(self
._def
, A()), True)
357 # But not other operators.
358 with self
.assertRaises(TypeError):
361 def _test_binop_none(self
, op
):
362 # Operators == and != are defined when comparing the field to None.
363 if op
is operator
.eq
:
364 self
.assertIs(op(self
._def
, None), False)
365 elif op
is operator
.ne
:
366 self
.assertIs(op(self
._def
, None), True)
368 # But not other operators.
369 with self
.assertRaises(TypeError):
372 def _test_binop_rhs_false(self
, test_cb
, op
):
375 def _test_binop_rhs_true(self
, test_cb
, op
):
378 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
381 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
384 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
387 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
388 test_cb(op
, bt2
.create_value(2))
390 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
391 test_cb(op
, bt2
.create_value(-23))
393 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
394 test_cb(op
, bt2
.create_value(0))
396 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
399 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
402 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
405 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
406 test_cb(op
, bt2
.create_value(2.2))
408 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
409 test_cb(op
, bt2
.create_value(-23.4))
411 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
412 test_cb(op
, bt2
.create_value(0.0))
414 def _test_binop_rhs_complex(self
, test_cb
, op
):
415 test_cb(op
, -23 + 19j
)
417 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
420 def _test_binop_type_false(self
, op
):
421 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
423 def _test_binop_type_true(self
, op
):
424 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
426 def _test_binop_type_pos_int(self
, op
):
427 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
429 def _test_binop_type_neg_int(self
, op
):
430 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
432 def _test_binop_type_zero_int(self
, op
):
433 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
435 def _test_binop_type_pos_vint(self
, op
):
436 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
438 def _test_binop_type_neg_vint(self
, op
):
439 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
441 def _test_binop_type_zero_vint(self
, op
):
442 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
444 def _test_binop_type_pos_float(self
, op
):
445 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
447 def _test_binop_type_neg_float(self
, op
):
448 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
450 def _test_binop_type_zero_float(self
, op
):
451 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
453 def _test_binop_type_pos_vfloat(self
, op
):
454 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
456 def _test_binop_type_neg_vfloat(self
, op
):
457 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
459 def _test_binop_type_zero_vfloat(self
, op
):
460 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
462 def _test_binop_type_complex(self
, op
):
463 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
465 def _test_binop_type_zero_complex(self
, op
):
466 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
468 def _test_binop_value_false(self
, op
):
469 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
471 def _test_binop_value_true(self
, op
):
472 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
474 def _test_binop_value_pos_int(self
, op
):
475 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
477 def _test_binop_value_neg_int(self
, op
):
478 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
480 def _test_binop_value_zero_int(self
, op
):
481 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
483 def _test_binop_value_pos_vint(self
, op
):
484 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
486 def _test_binop_value_neg_vint(self
, op
):
487 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
489 def _test_binop_value_zero_vint(self
, op
):
490 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
492 def _test_binop_value_pos_float(self
, op
):
493 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
495 def _test_binop_value_neg_float(self
, op
):
496 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
498 def _test_binop_value_zero_float(self
, op
):
499 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
501 def _test_binop_value_pos_vfloat(self
, op
):
502 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
504 def _test_binop_value_neg_vfloat(self
, op
):
505 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
507 def _test_binop_value_zero_vfloat(self
, op
):
508 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
510 def _test_binop_value_complex(self
, op
):
511 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
513 def _test_binop_value_zero_complex(self
, op
):
514 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
516 def _test_binop_lhs_addr_same_false(self
, op
):
517 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
519 def _test_binop_lhs_addr_same_true(self
, op
):
520 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
522 def _test_binop_lhs_addr_same_pos_int(self
, op
):
523 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
525 def _test_binop_lhs_addr_same_neg_int(self
, op
):
526 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
528 def _test_binop_lhs_addr_same_zero_int(self
, op
):
529 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
531 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
532 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
534 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
535 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
537 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
538 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
540 def _test_binop_lhs_addr_same_pos_float(self
, op
):
541 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
543 def _test_binop_lhs_addr_same_neg_float(self
, op
):
544 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
546 def _test_binop_lhs_addr_same_zero_float(self
, op
):
547 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
549 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
550 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
552 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
553 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
555 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
556 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
558 def _test_binop_lhs_addr_same_complex(self
, op
):
559 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
561 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
562 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
564 def _test_binop_lhs_value_same_false(self
, op
):
565 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
567 def _test_binop_lhs_value_same_true(self
, op
):
568 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
570 def _test_binop_lhs_value_same_pos_int(self
, op
):
571 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
573 def _test_binop_lhs_value_same_neg_int(self
, op
):
574 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
576 def _test_binop_lhs_value_same_zero_int(self
, op
):
577 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
579 def _test_binop_lhs_value_same_pos_vint(self
, op
):
580 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
582 def _test_binop_lhs_value_same_neg_vint(self
, op
):
583 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
585 def _test_binop_lhs_value_same_zero_vint(self
, op
):
586 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
588 def _test_binop_lhs_value_same_pos_float(self
, op
):
589 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
591 def _test_binop_lhs_value_same_neg_float(self
, op
):
592 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
594 def _test_binop_lhs_value_same_zero_float(self
, op
):
595 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
597 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
598 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
600 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
601 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
603 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
604 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
606 def _test_binop_lhs_value_same_complex(self
, op
):
607 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
609 def _test_binop_lhs_value_same_zero_complex(self
, op
):
610 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
612 def test_bool_op(self
):
613 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
615 def test_int_op(self
):
616 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
618 def test_float_op(self
):
619 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
621 def test_complex_op(self
):
622 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
624 def test_str_op(self
):
625 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
627 def test_hash_op(self
):
628 with self
.assertRaises(TypeError):
631 def test_const_hash_op(self
):
632 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
634 def test_const_hash_dict(self
):
636 my_dict
[self
._def
_const
] = 'my_value'
637 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
639 def test_eq_none(self
):
640 # Ignore this lint error:
641 # E711 comparison to None should be 'if cond is None:'
642 # since this is what we want to test (even though not good practice).
643 self
.assertFalse(self
._def
== None) # noqa: E711
645 def test_ne_none(self
):
646 # Ignore this lint error:
647 # E711 comparison to None should be 'if cond is not None:'
648 # since this is what we want to test (even though not good practice).
649 self
.assertTrue(self
._def
!= None) # noqa: E711
652 # This is a list of binary operators used for
653 # _inject_numeric_testing_methods().
655 # Each entry is a pair of binary operator name (used as part of the
656 # created testing method's name) and operator function.
664 ('add', operator
.add
),
665 ('radd', lambda a
, b
: operator
.add(b
, a
)),
666 ('and', operator
.and_
),
667 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
668 ('floordiv', operator
.floordiv
),
669 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
670 ('lshift', operator
.lshift
),
671 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
672 ('mod', operator
.mod
),
673 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
674 ('mul', operator
.mul
),
675 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
676 ('or', operator
.or_
),
677 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
678 ('pow', operator
.pow),
679 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
680 ('rshift', operator
.rshift
),
681 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
682 ('sub', operator
.sub
),
683 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
684 ('truediv', operator
.truediv
),
685 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
686 ('xor', operator
.xor
),
687 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
691 # This is a list of unary operators used for
692 # _inject_numeric_testing_methods().
694 # Each entry is a pair of unary operator name (used as part of the
695 # created testing method's name) and operator function.
697 ('neg', operator
.neg
),
698 ('pos', operator
.pos
),
699 ('abs', operator
.abs),
700 ('invert', operator
.invert
),
702 ('round_0', partial(round, ndigits
=0)),
703 ('round_1', partial(round, ndigits
=1)),
704 ('round_2', partial(round, ndigits
=2)),
705 ('round_3', partial(round, ndigits
=3)),
707 ('floor', math
.floor
),
708 ('trunc', math
.trunc
),
712 # This function injects a bunch of testing methods to a numeric
715 # It is meant to be used like this:
717 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
719 # This function injects:
721 # * One testing method for each _TestNumericField._test_binop_*()
722 # method, for each binary operator in the _BINOPS tuple.
724 # * One testing method for each _TestNumericField._test_unaryop*()
725 # method, for each unary operator in the _UNARYOPS tuple.
726 def _inject_numeric_testing_methods(cls
):
727 def test_binop_name(suffix
):
728 return 'test_binop_{}_{}'.format(name
, suffix
)
730 def test_unaryop_name(suffix
):
731 return 'test_unaryop_{}_{}'.format(name
, suffix
)
733 # inject testing methods for each binary operation
734 for name
, binop
in _BINOPS
:
737 test_binop_name('unknown'),
738 partialmethod(_TestNumericField
._test
_binop
_unknown
, op
=binop
),
742 test_binop_name('none'),
743 partialmethod(_TestNumericField
._test
_binop
_none
, op
=binop
),
747 test_binop_name('type_true'),
748 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
752 test_binop_name('type_pos_int'),
753 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
757 test_binop_name('type_pos_vint'),
758 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
762 test_binop_name('value_true'),
763 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
767 test_binop_name('value_pos_int'),
768 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
772 test_binop_name('value_pos_vint'),
773 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
777 test_binop_name('lhs_addr_same_true'),
778 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
782 test_binop_name('lhs_addr_same_pos_int'),
784 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
789 test_binop_name('lhs_addr_same_pos_vint'),
791 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
796 test_binop_name('lhs_value_same_true'),
797 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
801 test_binop_name('lhs_value_same_pos_int'),
803 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
808 test_binop_name('lhs_value_same_pos_vint'),
810 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
815 test_binop_name('type_neg_int'),
816 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
820 test_binop_name('type_neg_vint'),
821 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
825 test_binop_name('value_neg_int'),
826 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
830 test_binop_name('value_neg_vint'),
831 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
835 test_binop_name('lhs_addr_same_neg_int'),
837 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
842 test_binop_name('lhs_addr_same_neg_vint'),
844 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
849 test_binop_name('lhs_value_same_neg_int'),
851 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
856 test_binop_name('lhs_value_same_neg_vint'),
858 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
863 test_binop_name('type_false'),
864 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
868 test_binop_name('type_zero_int'),
869 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
873 test_binop_name('type_zero_vint'),
874 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
878 test_binop_name('value_false'),
879 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
883 test_binop_name('value_zero_int'),
884 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
888 test_binop_name('value_zero_vint'),
889 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
893 test_binop_name('lhs_addr_same_false'),
894 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
898 test_binop_name('lhs_addr_same_zero_int'),
900 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
905 test_binop_name('lhs_addr_same_zero_vint'),
907 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
912 test_binop_name('lhs_value_same_false'),
913 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
917 test_binop_name('lhs_value_same_zero_int'),
919 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
924 test_binop_name('lhs_value_same_zero_vint'),
926 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
931 test_binop_name('type_pos_float'),
932 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
936 test_binop_name('type_neg_float'),
937 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
941 test_binop_name('type_pos_vfloat'),
942 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
946 test_binop_name('type_neg_vfloat'),
947 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
951 test_binop_name('value_pos_float'),
952 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
956 test_binop_name('value_neg_float'),
957 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
961 test_binop_name('value_pos_vfloat'),
962 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
966 test_binop_name('value_neg_vfloat'),
967 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
971 test_binop_name('lhs_addr_same_pos_float'),
973 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
978 test_binop_name('lhs_addr_same_neg_float'),
980 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
985 test_binop_name('lhs_addr_same_pos_vfloat'),
987 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
992 test_binop_name('lhs_addr_same_neg_vfloat'),
994 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
999 test_binop_name('lhs_value_same_pos_float'),
1001 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
1006 test_binop_name('lhs_value_same_neg_float'),
1008 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
1013 test_binop_name('lhs_value_same_pos_vfloat'),
1015 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
1020 test_binop_name('lhs_value_same_neg_vfloat'),
1022 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
1027 test_binop_name('type_zero_float'),
1028 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
1032 test_binop_name('type_zero_vfloat'),
1033 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
1037 test_binop_name('value_zero_float'),
1038 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
1042 test_binop_name('value_zero_vfloat'),
1043 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
1047 test_binop_name('lhs_addr_same_zero_float'),
1049 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
1054 test_binop_name('lhs_addr_same_zero_vfloat'),
1056 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
1061 test_binop_name('lhs_value_same_zero_float'),
1063 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1068 test_binop_name('lhs_value_same_zero_vfloat'),
1070 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1075 test_binop_name('type_complex'),
1076 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1080 test_binop_name('type_zero_complex'),
1081 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1085 test_binop_name('value_complex'),
1086 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1090 test_binop_name('value_zero_complex'),
1091 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1095 test_binop_name('lhs_addr_same_complex'),
1097 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1102 test_binop_name('lhs_addr_same_zero_complex'),
1104 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1109 test_binop_name('lhs_value_same_complex'),
1111 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1116 test_binop_name('lhs_value_same_zero_complex'),
1118 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1122 # inject testing methods for each unary operation
1123 for name
, unaryop
in _UNARYOPS
:
1126 test_unaryop_name('type'),
1127 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1131 test_unaryop_name('value'),
1132 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1136 test_unaryop_name('addr_same'),
1137 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1141 test_unaryop_name('value_same'),
1142 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1146 class BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1148 def _const_value_setter(field
):
1151 def _create_fc(self
, tc
):
1152 return tc
.create_bool_field_class()
1155 self
._tc
= get_default_trace_class()
1156 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1157 self
._def
.value
= True
1158 self
._def
_value
= True
1159 self
._def
_const
= create_const_field(
1160 self
._tc
, self
._tc
.create_bool_field_class(), self
._const
_value
_setter
1162 self
._def
_new
_value
= False
1164 def test_classes(self
):
1165 self
.assertIs(type(self
._def
), bt2
._BoolField
)
1166 self
.assertIs(type(self
._def
_const
), bt2
._BoolFieldConst
)
1168 def test_assign_true(self
):
1170 self
._def
.value
= raw
1171 self
.assertEqual(self
._def
, raw
)
1173 def test_assign_false(self
):
1175 self
._def
.value
= raw
1176 self
.assertEqual(self
._def
, raw
)
1178 def test_assign_field_true(self
):
1179 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1182 self
._def
.value
= field
1183 self
.assertEqual(self
._def
, raw
)
1185 def test_assign_field_false(self
):
1186 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1189 self
._def
.value
= field
1190 self
.assertEqual(self
._def
, raw
)
1192 def test_assign_invalid_type(self
):
1193 with self
.assertRaises(TypeError):
1194 self
._def
.value
= 17
1196 def test_str_op(self
):
1197 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1200 _inject_numeric_testing_methods(BoolFieldTestCase
)
1203 class _TestIntegerFieldCommon(_TestNumericField
):
1204 def test_assign_true(self
):
1206 self
._def
.value
= raw
1207 self
.assertEqual(self
._def
, raw
)
1209 def test_assign_false(self
):
1211 self
._def
.value
= raw
1212 self
.assertEqual(self
._def
, raw
)
1214 def test_assign_pos_int(self
):
1216 self
._def
.value
= raw
1217 self
.assertEqual(self
._def
, raw
)
1219 def test_assign_neg_int(self
):
1221 self
._def
.value
= raw
1222 self
.assertEqual(self
._def
, raw
)
1224 def test_assign_int_field(self
):
1226 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1228 self
._def
.value
= field
1229 self
.assertEqual(self
._def
, raw
)
1231 def test_assign_invalid_type(self
):
1232 with self
.assertRaises(TypeError):
1233 self
._def
.value
= 'yes'
1235 def test_assign_uint(self
):
1236 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1237 field
= _create_field(self
._tc
, uint_fc
)
1240 self
.assertEqual(field
, raw
)
1242 def test_assign_big_uint(self
):
1243 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1244 field
= _create_field(self
._tc
, uint_fc
)
1245 # Larger than the IEEE 754 double-precision exact representation of
1248 field
.value
= (2 ** 53) + 1
1249 self
.assertEqual(field
, raw
)
1251 def test_assign_uint_out_of_range(self
):
1252 uint_fc
= self
._tc
.create_unsigned_integer_field_class(8)
1253 field
= _create_field(self
._tc
, uint_fc
)
1255 with self
.assertRaises(ValueError) as ctx
:
1258 str(ctx
.exception
), 'Value 256 is outside valid range [0, 255]'
1261 with self
.assertRaises(ValueError) as ctx
:
1263 self
.assertEqual(str(ctx
.exception
), 'Value -1 is outside valid range [0, 255]')
1265 def test_assign_int_out_of_range(self
):
1266 int_fc
= self
._tc
.create_signed_integer_field_class(8)
1267 field
= _create_field(self
._tc
, int_fc
)
1269 with self
.assertRaises(ValueError) as ctx
:
1272 str(ctx
.exception
), 'Value 128 is outside valid range [-128, 127]'
1275 with self
.assertRaises(ValueError) as ctx
:
1278 str(ctx
.exception
), 'Value -129 is outside valid range [-128, 127]'
1281 def test_str_op(self
):
1282 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1285 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1288 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1290 def _const_value_setter(field
):
1293 def _create_fc(self
, tc
):
1294 return tc
.create_signed_integer_field_class(25)
1297 self
._tc
= get_default_trace_class()
1298 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1299 self
._field
.value
= 17
1300 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1301 self
._def
.value
= 17
1302 self
._def
_value
= 17
1303 self
._def
_const
= create_const_field(
1304 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1306 self
._def
_new
_value
= -101
1309 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1311 def _const_value_setter(field
):
1314 def _create_fc(self
, tc
):
1315 fc
= tc
.create_signed_enumeration_field_class(32)
1316 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1317 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1318 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1320 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1322 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1326 self
._tc
= get_default_trace_class()
1327 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1328 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1329 self
._def
.value
= 17
1330 self
._def
_value
= 17
1331 self
._def
_const
= create_const_field(
1332 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1334 self
._def
_new
_value
= -101
1336 def test_str_op(self
):
1337 expected_string_found
= False
1340 # Establish all permutations of the three expected matches since
1341 # the order in which mappings are enumerated is not explicitly part of
1343 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1344 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1346 expected_string_found
= True
1349 self
.assertTrue(expected_string_found
)
1351 def test_labels(self
):
1352 self
._field
.value
= 17
1353 labels
= sorted(self
._field
.labels
)
1354 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1357 class SingleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1359 def _const_value_setter(field
):
1362 def _create_fc(self
, tc
):
1363 return tc
.create_single_precision_real_field_class()
1366 self
._tc
= get_default_trace_class()
1367 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1368 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1369 self
._def
_const
= create_const_field(
1371 self
._tc
.create_single_precision_real_field_class(),
1372 self
._const
_value
_setter
,
1374 self
._def
.value
= 52.0
1375 self
._def
_value
= 52.0
1376 self
._def
_new
_value
= -17.0
1378 def _test_invalid_op(self
, cb
):
1379 with self
.assertRaises(TypeError):
1382 def test_assign_true(self
):
1383 self
._def
.value
= True
1384 self
.assertTrue(self
._def
)
1386 def test_assign_false(self
):
1387 self
._def
.value
= False
1388 self
.assertFalse(self
._def
)
1390 def test_assign_pos_int(self
):
1392 self
._def
.value
= raw
1393 self
.assertEqual(self
._def
, float(raw
))
1395 def test_assign_neg_int(self
):
1397 self
._def
.value
= raw
1398 self
.assertEqual(self
._def
, float(raw
))
1400 def test_assign_int_field(self
):
1401 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1402 int_field
= _create_field(self
._tc
, int_fc
)
1404 int_field
.value
= raw
1405 self
._def
.value
= int_field
1406 self
.assertEqual(self
._def
, float(raw
))
1408 def test_assign_float(self
):
1410 self
._def
.value
= raw
1411 # It's expected to have some lost of precision because of the field
1412 # that is in single precision.
1413 self
.assertEqual(round(self
._def
, 5), raw
)
1415 def test_assign_float_field(self
):
1416 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1419 self
._def
.value
= field
1420 # It's expected to have some lost of precision because of the field
1421 # that is in single precision.
1422 self
.assertEqual(round(self
._def
, 5), raw
)
1424 def test_assign_invalid_type(self
):
1425 with self
.assertRaises(TypeError):
1426 self
._def
.value
= 'yes'
1428 def test_invalid_lshift(self
):
1429 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1431 def test_invalid_rshift(self
):
1432 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1434 def test_invalid_and(self
):
1435 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1437 def test_invalid_or(self
):
1438 self
._test
_invalid
_op
(lambda: self
._def |
23)
1440 def test_invalid_xor(self
):
1441 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1443 def test_invalid_invert(self
):
1444 self
._test
_invalid
_op
(lambda: ~self
._def
)
1446 def test_str_op(self
):
1447 self
.assertEqual(str(round(self
._def
, 5)), str(self
._def
_value
))
1450 _inject_numeric_testing_methods(SingleRealFieldTestCase
)
1453 class DoubleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1455 def _const_value_setter(field
):
1458 def _create_fc(self
, tc
):
1459 return tc
.create_double_precision_real_field_class()
1462 self
._tc
= get_default_trace_class()
1463 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1464 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1465 self
._def
_const
= create_const_field(
1467 self
._tc
.create_double_precision_real_field_class(),
1468 self
._const
_value
_setter
,
1470 self
._def
.value
= 52.7
1471 self
._def
_value
= 52.7
1472 self
._def
_new
_value
= -17.164857
1474 def _test_invalid_op(self
, cb
):
1475 with self
.assertRaises(TypeError):
1478 def test_assign_true(self
):
1479 self
._def
.value
= True
1480 self
.assertTrue(self
._def
)
1482 def test_assign_false(self
):
1483 self
._def
.value
= False
1484 self
.assertFalse(self
._def
)
1486 def test_assign_pos_int(self
):
1488 self
._def
.value
= raw
1489 self
.assertEqual(self
._def
, float(raw
))
1491 def test_assign_neg_int(self
):
1493 self
._def
.value
= raw
1494 self
.assertEqual(self
._def
, float(raw
))
1496 def test_assign_int_field(self
):
1497 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1498 int_field
= _create_field(self
._tc
, int_fc
)
1500 int_field
.value
= raw
1501 self
._def
.value
= int_field
1502 self
.assertEqual(self
._def
, float(raw
))
1504 def test_assign_float(self
):
1506 self
._def
.value
= raw
1507 self
.assertEqual(self
._def
, raw
)
1509 def test_assign_float_field(self
):
1510 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1513 self
._def
.value
= field
1514 self
.assertEqual(self
._def
, raw
)
1516 def test_assign_invalid_type(self
):
1517 with self
.assertRaises(TypeError):
1518 self
._def
.value
= 'yes'
1520 def test_invalid_lshift(self
):
1521 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1523 def test_invalid_rshift(self
):
1524 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1526 def test_invalid_and(self
):
1527 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1529 def test_invalid_or(self
):
1530 self
._test
_invalid
_op
(lambda: self
._def |
23)
1532 def test_invalid_xor(self
):
1533 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1535 def test_invalid_invert(self
):
1536 self
._test
_invalid
_op
(lambda: ~self
._def
)
1538 def test_str_op(self
):
1539 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1542 _inject_numeric_testing_methods(DoubleRealFieldTestCase
)
1545 class StringFieldTestCase(unittest
.TestCase
):
1547 def _const_value_setter(field
):
1548 field
.value
= 'Hello, World!'
1551 self
._tc
= get_default_trace_class()
1552 self
._def
_value
= 'Hello, World!'
1553 self
._def
= _create_string_field(self
._tc
)
1554 self
._def
_const
= create_const_field(
1555 self
._tc
, self
._tc
.create_string_field_class(), self
._const
_value
_setter
1557 self
._def
.value
= self
._def
_value
1558 self
._def
_new
_value
= 'Yes!'
1560 def test_assign_int(self
):
1561 with self
.assertRaises(TypeError):
1562 self
._def
.value
= 283
1564 def test_assign_string_field(self
):
1565 field
= _create_string_field(self
._tc
)
1568 self
.assertEqual(field
, raw
)
1571 self
.assertEqual(self
._def
, self
._def
_value
)
1573 def test_const_eq(self
):
1574 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1576 def test_not_eq(self
):
1577 self
.assertNotEqual(self
._def
, 23)
1579 def test_lt_vstring(self
):
1580 s1
= _create_string_field(self
._tc
)
1582 s2
= _create_string_field(self
._tc
)
1584 self
.assertLess(s1
, s2
)
1586 def test_lt_string(self
):
1587 s1
= _create_string_field(self
._tc
)
1589 self
.assertLess(s1
, 'bateau')
1591 def test_le_vstring(self
):
1592 s1
= _create_string_field(self
._tc
)
1594 s2
= _create_string_field(self
._tc
)
1596 self
.assertLessEqual(s1
, s2
)
1598 def test_le_string(self
):
1599 s1
= _create_string_field(self
._tc
)
1601 self
.assertLessEqual(s1
, 'bateau')
1603 def test_gt_vstring(self
):
1604 s1
= _create_string_field(self
._tc
)
1606 s2
= _create_string_field(self
._tc
)
1608 self
.assertGreater(s2
, s1
)
1610 def test_gt_string(self
):
1611 s1
= _create_string_field(self
._tc
)
1613 self
.assertGreater('bateau', s1
)
1615 def test_ge_vstring(self
):
1616 s1
= _create_string_field(self
._tc
)
1618 s2
= _create_string_field(self
._tc
)
1620 self
.assertGreaterEqual(s2
, s1
)
1622 def test_ge_string(self
):
1623 s1
= _create_string_field(self
._tc
)
1625 self
.assertGreaterEqual('bateau', s1
)
1627 def test_bool_op(self
):
1628 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1630 def test_str_op(self
):
1631 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1634 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1636 def test_getitem(self
):
1637 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1639 def test_const_getitem(self
):
1640 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1642 def test_append_str(self
):
1643 to_append
= 'meow meow meow'
1644 self
._def
+= to_append
1645 self
._def
_value
+= to_append
1646 self
.assertEqual(self
._def
, self
._def
_value
)
1648 def test_const_append_str(self
):
1649 to_append
= 'meow meow meow'
1650 with self
.assertRaises(TypeError):
1651 self
._def
_const
+= to_append
1652 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1654 def test_append_string_field(self
):
1655 field
= _create_string_field(self
._tc
)
1656 to_append
= 'meow meow meow'
1657 field
.value
= to_append
1659 self
._def
_value
+= to_append
1660 self
.assertEqual(self
._def
, self
._def
_value
)
1662 def test_hash_op(self
):
1663 with self
.assertRaises(TypeError):
1666 def test_const_hash_op(self
):
1667 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
1669 def test_const_hash_dict(self
):
1671 my_dict
[self
._def
_const
] = 'my_value'
1672 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
1675 class _TestArrayFieldCommon
:
1676 def _modify_def(self
):
1679 def test_bool_op_true(self
):
1680 self
.assertTrue(self
._def
)
1683 self
.assertEqual(len(self
._def
), 3)
1685 def test_length(self
):
1686 self
.assertEqual(self
._def
.length
, 3)
1688 def test_getitem(self
):
1689 field
= self
._def
[1]
1690 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1691 self
.assertEqual(field
, 1847)
1693 def test_const_getitem(self
):
1694 field
= self
._def
_const
[1]
1695 self
.assertIs(type(field
), bt2
._SignedIntegerFieldConst
)
1696 self
.assertEqual(field
, 1847)
1699 field
= _create_int_array_field(self
._tc
, 3)
1703 self
.assertEqual(self
._def
, field
)
1705 def test_eq_invalid_type(self
):
1706 self
.assertNotEqual(self
._def
, 23)
1708 def test_eq_diff_len(self
):
1709 field
= _create_int_array_field(self
._tc
, 2)
1712 self
.assertNotEqual(self
._def
, field
)
1714 def test_eq_diff_content_same_len(self
):
1715 field
= _create_int_array_field(self
._tc
, 3)
1719 self
.assertNotEqual(self
._def
, field
)
1721 def test_eq_non_sequence_iterable(self
):
1722 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1723 field
= _create_int_array_field(self
._tc
, 3)
1727 self
.assertEqual(field
, list(dct
.keys()))
1728 self
.assertNotEqual(field
, dct
)
1730 def test_setitem(self
):
1732 self
.assertEqual(self
._def
[2], 24)
1734 def test_setitem_int_field(self
):
1735 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1736 int_field
= _create_field(self
._tc
, int_fc
)
1737 int_field
.value
= 19487
1738 self
._def
[1] = int_field
1739 self
.assertEqual(self
._def
[1], 19487)
1741 def test_setitem_non_basic_field(self
):
1742 array_field
= _create_struct_array_field(self
._tc
, 2)
1743 with self
.assertRaises(TypeError):
1746 def test_setitem_none(self
):
1747 with self
.assertRaises(TypeError):
1750 def test_setitem_index_wrong_type(self
):
1751 with self
.assertRaises(TypeError):
1752 self
._def
['yes'] = 23
1754 def test_setitem_index_neg(self
):
1755 with self
.assertRaises(IndexError):
1758 def test_setitem_index_out_of_range(self
):
1759 with self
.assertRaises(IndexError):
1760 self
._def
[len(self
._def
)] = 134679
1762 def test_const_setitem(self
):
1763 with self
.assertRaises(TypeError):
1764 self
._def
_const
[0] = 134679
1766 def test_iter(self
):
1767 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1768 self
.assertEqual(field
, value
)
1770 def test_const_iter(self
):
1771 for field
, value
in zip(self
._def
_const
, (45, 1847, 1948754)):
1772 self
.assertEqual(field
, value
)
1774 def test_value_int_field(self
):
1775 values
= [45646, 145, 12145]
1776 self
._def
.value
= values
1777 self
.assertEqual(values
, self
._def
)
1779 def test_value_check_sequence(self
):
1781 with self
.assertRaises(TypeError):
1782 self
._def
.value
= values
1784 def test_value_wrong_type_in_sequence(self
):
1785 values
= [32, 'hello', 11]
1786 with self
.assertRaises(TypeError):
1787 self
._def
.value
= values
1789 def test_value_complex_type(self
):
1790 struct_fc
= self
._tc
.create_structure_field_class()
1791 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1792 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1793 str_fc
= self
._tc
.create_string_field_class()
1794 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1795 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1796 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1797 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1798 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1800 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1801 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1802 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1805 array
= stream
.create_packet().context_field
['array_field']
1806 array
.value
= values
1807 self
.assertEqual(values
, array
)
1808 values
[0]['an_int'] = 'a string'
1809 with self
.assertRaises(TypeError):
1810 array
.value
= values
1812 def test_str_op(self
):
1814 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1815 self
.assertEqual(expected_string
, s
)
1818 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1820 def _const_value_setter(field
):
1821 field
.value
= [45, 1847, 1948754]
1824 self
._tc
= get_default_trace_class()
1825 self
._def
= _create_int_array_field(self
._tc
, 3)
1828 self
._def
[2] = 1948754
1829 self
._def
_value
= [45, 1847, 1948754]
1830 self
._def
_const
= create_const_field(
1832 self
._tc
.create_static_array_field_class(
1833 self
._tc
.create_signed_integer_field_class(32), 3
1835 self
._const
_value
_setter
,
1838 def test_value_wrong_len(self
):
1840 with self
.assertRaises(ValueError):
1841 self
._def
.value
= values
1844 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1846 def _const_value_setter(field
):
1847 field
.value
= [45, 1847, 1948754]
1850 self
._tc
= get_default_trace_class()
1851 self
._def
= _create_dynamic_array(self
._tc
)
1854 self
._def
[2] = 1948754
1855 self
._def
_value
= [45, 1847, 1948754]
1856 self
._def
_const
= create_const_field(
1858 self
._tc
.create_dynamic_array_field_class(
1859 self
._tc
.create_signed_integer_field_class(32)
1861 self
._const
_value
_setter
,
1864 def test_value_resize(self
):
1865 new_values
= [1, 2, 3, 4]
1866 self
._def
.value
= new_values
1867 self
.assertCountEqual(self
._def
, new_values
)
1869 def test_set_length(self
):
1870 self
._def
.length
= 4
1872 self
.assertEqual(len(self
._def
), 4)
1874 def test_const_set_length(self
):
1875 with self
.assertRaises(AttributeError):
1876 self
._def
_const
.length
= 4
1877 self
.assertEqual(len(self
._def
), 3)
1879 def test_set_invalid_length(self
):
1880 with self
.assertRaises(TypeError):
1881 self
._def
.length
= 'cheval'
1884 class StructureFieldTestCase(unittest
.TestCase
):
1886 def _const_value_setter(field
):
1896 def _create_fc(self
, tc
):
1897 fc
= tc
.create_structure_field_class()
1898 fc
.append_member('A', self
._fc
0_fn
())
1899 fc
.append_member('B', self
._fc
1_fn
())
1900 fc
.append_member('C', self
._fc
2_fn
())
1901 fc
.append_member('D', self
._fc
3_fn
())
1902 fc
.append_member('E', self
._fc
4_fn
())
1903 fc5
= self
._fc
5_fn
()
1904 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1905 fc
.append_member('F', fc5
)
1909 self
._tc
= get_default_trace_class()
1910 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1911 self
._fc
1_fn
= self
._tc
.create_string_field_class
1912 self
._fc
2_fn
= self
._tc
.create_double_precision_real_field_class
1913 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1914 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1915 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1916 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1918 self
._fc
= self
._create
_fc
(self
._tc
)
1919 self
._def
= _create_field(self
._tc
, self
._fc
)
1920 self
._def
['A'] = -1872
1921 self
._def
['B'] = 'salut'
1922 self
._def
['C'] = 17.5
1923 self
._def
['D'] = 16497
1925 self
._def
['F'] = {'F_1': 52}
1935 self
._def
_const
= create_const_field(
1936 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1939 def _modify_def(self
):
1940 self
._def
['B'] = 'hola'
1942 def test_bool_op_true(self
):
1943 self
.assertTrue(self
._def
)
1945 def test_bool_op_false(self
):
1946 field
= self
._def
['E']
1947 self
.assertFalse(field
)
1950 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1952 def test_getitem(self
):
1953 field1
= self
._def
['A']
1954 field2
= self
._def
['B']
1955 field3
= self
._def
['C']
1956 field4
= self
._def
['D']
1957 field5
= self
._def
['E']
1958 field6
= self
._def
['F']
1960 self
.assertIs(type(field1
), bt2
._SignedIntegerField
)
1961 self
.assertEqual(field1
, -1872)
1963 self
.assertIs(type(field2
), bt2
._StringField
)
1964 self
.assertEqual(field2
, 'salut')
1966 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealField
)
1967 self
.assertEqual(field3
, 17.5)
1969 self
.assertIs(type(field4
), bt2
._SignedIntegerField
)
1970 self
.assertEqual(field4
, 16497)
1972 self
.assertIs(type(field5
), bt2
._StructureField
)
1973 self
.assertEqual(field5
, {})
1975 self
.assertIs(type(field6
), bt2
._StructureField
)
1976 self
.assertEqual(field6
, {'F_1': 52})
1978 def test_const_getitem(self
):
1979 field1
= self
._def
_const
['A']
1980 field2
= self
._def
_const
['B']
1981 field3
= self
._def
_const
['C']
1982 field4
= self
._def
_const
['D']
1983 field5
= self
._def
_const
['E']
1984 field6
= self
._def
_const
['F']
1986 self
.assertIs(type(field1
), bt2
._SignedIntegerFieldConst
)
1987 self
.assertEqual(field1
, -1872)
1989 self
.assertIs(type(field2
), bt2
._StringFieldConst
)
1990 self
.assertEqual(field2
, 'salut')
1992 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealFieldConst
)
1993 self
.assertEqual(field3
, 17.5)
1995 self
.assertIs(type(field4
), bt2
._SignedIntegerFieldConst
)
1996 self
.assertEqual(field4
, 16497)
1998 self
.assertIs(type(field5
), bt2
._StructureFieldConst
)
1999 self
.assertEqual(field5
, {})
2001 self
.assertIs(type(field6
), bt2
._StructureFieldConst
)
2002 self
.assertEqual(field6
, {'F_1': 52})
2004 def test_member_at_index_out_of_bounds_after(self
):
2005 with self
.assertRaises(IndexError):
2006 self
._def
.member_at_index(len(self
._def
_value
))
2009 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2011 field
['B'] = 'salut'
2015 field
['F'] = {'F_1': 52}
2016 self
.assertEqual(self
._def
, field
)
2018 def test_const_eq(self
):
2019 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2021 field
['B'] = 'salut'
2025 field
['F'] = {'F_1': 52}
2026 self
.assertEqual(self
._def
_const
, field
)
2028 def test_eq_invalid_type(self
):
2029 self
.assertNotEqual(self
._def
, 23)
2031 def test_eq_diff_len(self
):
2032 fc
= self
._tc
.create_structure_field_class()
2033 fc
.append_member('A', self
._fc
0_fn
())
2034 fc
.append_member('B', self
._fc
1_fn
())
2035 fc
.append_member('C', self
._fc
2_fn
())
2037 field
= _create_field(self
._tc
, fc
)
2039 field
['B'] = 'salut'
2041 self
.assertNotEqual(self
._def
, field
)
2043 def test_eq_diff_keys(self
):
2044 fc
= self
._tc
.create_structure_field_class()
2045 fc
.append_member('U', self
._fc
0_fn
())
2046 fc
.append_member('V', self
._fc
1_fn
())
2047 fc
.append_member('W', self
._fc
2_fn
())
2048 fc
.append_member('X', self
._fc
3_fn
())
2049 fc
.append_member('Y', self
._fc
4_fn
())
2050 fc
.append_member('Z', self
._fc
5_fn
())
2051 field
= _create_field(self
._tc
, fc
)
2053 field
['V'] = "gerry"
2058 self
.assertNotEqual(self
._def
, field
)
2060 def test_eq_diff_content_same_len(self
):
2061 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2063 field
['B'] = 'salut'
2067 field
['F'] = {'F_1': 0}
2068 self
.assertNotEqual(self
._def
, field
)
2070 def test_eq_same_content_diff_keys(self
):
2071 fc
= self
._tc
.create_structure_field_class()
2072 fc
.append_member('A', self
._fc
0_fn
())
2073 fc
.append_member('B', self
._fc
1_fn
())
2074 fc
.append_member('E', self
._fc
2_fn
())
2075 fc
.append_member('D', self
._fc
3_fn
())
2076 fc
.append_member('C', self
._fc
4_fn
())
2077 fc
.append_member('F', self
._fc
5_fn
())
2078 field
= _create_field(self
._tc
, fc
)
2080 field
['B'] = 'salut'
2085 self
.assertNotEqual(self
._def
, field
)
2087 def test_setitem(self
):
2088 self
._def
['C'] = -18.47
2089 self
.assertEqual(self
._def
['C'], -18.47)
2091 def test_const_setitem(self
):
2092 with self
.assertRaises(TypeError):
2093 self
._def
_const
['A'] = 134679
2095 def test_setitem_int_field(self
):
2096 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2097 int_field
= _create_field(self
._tc
, int_fc
)
2098 int_field
.value
= 19487
2099 self
._def
['D'] = int_field
2100 self
.assertEqual(self
._def
['D'], 19487)
2102 def test_setitem_non_basic_field(self
):
2103 elem_fc
= self
._tc
.create_structure_field_class()
2104 struct_fc
= self
._tc
.create_structure_field_class()
2105 struct_fc
.append_member('A', elem_fc
)
2106 struct_field
= _create_field(self
._tc
, struct_fc
)
2108 # Will fail on access to .items() of the value
2109 with self
.assertRaises(AttributeError):
2110 struct_field
['A'] = 23
2112 def test_setitem_none(self
):
2113 with self
.assertRaises(TypeError):
2114 self
._def
['C'] = None
2116 def test_setitem_key_wrong_type(self
):
2117 with self
.assertRaises(TypeError):
2120 def test_setitem_wrong_key(self
):
2121 with self
.assertRaises(KeyError):
2122 self
._def
['hi'] = 134679
2124 def test_member_at_index(self
):
2125 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
2127 def test_const_member_at_index(self
):
2128 self
.assertEqual(self
._def
_const
.member_at_index(1), 'salut')
2130 def test_iter(self
):
2140 for vkey
, vval
in self
._def
.items():
2141 val
= orig_values
[vkey
]
2142 self
.assertEqual(vval
, val
)
2144 def test_value(self
):
2153 self
.assertEqual(self
._def
, orig_values
)
2155 def test_set_value(self
):
2156 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2157 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
2158 str_fc
= self
._tc
.create_string_field_class()
2159 struct_fc
= self
._tc
.create_structure_field_class()
2160 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
2161 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
2162 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
2163 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2165 struct
= _create_field(self
._tc
, struct_fc
)
2166 struct
.value
= values
2167 self
.assertEqual(values
, struct
)
2169 bad_type_values
= copy
.deepcopy(values
)
2170 bad_type_values
['an_int'] = 'a string'
2171 with self
.assertRaises(TypeError):
2172 struct
.value
= bad_type_values
2174 unknown_key_values
= copy
.deepcopy(values
)
2175 unknown_key_values
['unknown_key'] = 16546
2176 with self
.assertRaises(KeyError):
2177 struct
.value
= unknown_key_values
2179 def test_str_op(self
):
2180 expected_string_found
= False
2182 # Establish all permutations of the three expected matches since
2183 # the order in which mappings are enumerated is not explicitly part of
2185 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
2186 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
2187 candidate
= '{{{}}}'.format(', '.join(items
))
2189 expected_string_found
= True
2192 self
.assertTrue(expected_string_found
)
2195 class OptionFieldTestCase(unittest
.TestCase
):
2197 def _const_value_setter(field
):
2198 field
.value
= {'opt_field': 'hiboux'}
2200 def _create_fc(self
, tc
):
2201 fc
= tc
.create_option_without_selector_field_class(
2202 tc
.create_string_field_class()
2204 top_fc
= tc
.create_structure_field_class()
2205 top_fc
.append_member('opt_field', fc
)
2209 self
._tc
= get_default_trace_class()
2210 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2211 self
._def
= fld
['opt_field']
2212 self
._def
_value
= 'hiboux'
2213 self
._def
_const
= create_const_field(
2214 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
2217 def test_value_prop(self
):
2218 self
._def
.value
= 'hiboux'
2219 self
.assertEqual(self
._def
.field
, 'hiboux')
2220 self
.assertIs(type(self
._def
), bt2
._OptionField
)
2221 self
.assertIs(type(self
._def
.field
), bt2
._StringField
)
2222 self
.assertTrue(self
._def
.has_field
)
2224 def test_const_value_prop(self
):
2225 self
.assertEqual(self
._def
_const
.field
, 'hiboux')
2226 self
.assertIs(type(self
._def
_const
), bt2
._OptionFieldConst
)
2227 self
.assertIs(type(self
._def
_const
.field
), bt2
._StringFieldConst
)
2228 self
.assertTrue(self
._def
_const
.has_field
)
2230 def test_has_field_prop_true(self
):
2231 self
._def
.has_field
= True
2232 self
.assertTrue(self
._def
.has_field
)
2234 def test_has_field_prop_false(self
):
2235 self
._def
.has_field
= False
2236 self
.assertFalse(self
._def
.has_field
)
2238 def test_bool_op_true(self
):
2239 self
._def
.value
= 'allo'
2240 self
.assertTrue(self
._def
)
2242 def test_bool_op_false(self
):
2243 self
._def
.has_field
= False
2244 self
.assertFalse(self
._def
)
2246 def test_field_prop_existing(self
):
2247 self
._def
.value
= 'meow'
2248 field
= self
._def
.field
2249 self
.assertEqual(field
, 'meow')
2251 def test_field_prop_none(self
):
2252 self
._def
.has_field
= False
2253 field
= self
._def
.field
2254 self
.assertIsNone(field
)
2256 def test_const_field_prop(self
):
2257 with self
.assertRaises(AttributeError):
2258 self
._def
_const
.has_field
= False
2260 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2261 self
.assertTrue(self
._def
_const
.has_field
)
2263 def test_field_prop_existing_then_none(self
):
2264 self
._def
.value
= 'meow'
2265 field
= self
._def
.field
2266 self
.assertEqual(field
, 'meow')
2267 self
._def
.has_field
= False
2268 field
= self
._def
.field
2269 self
.assertIsNone(field
)
2272 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2273 field
= field
['opt_field']
2274 field
.value
= 'walk'
2275 self
._def
.value
= 'walk'
2276 self
.assertEqual(self
._def
, field
)
2278 def test_const_eq(self
):
2279 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2280 field
= field
['opt_field']
2281 field
.value
= 'hiboux'
2282 self
.assertEqual(self
._def
_const
, field
)
2283 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2285 def test_eq_invalid_type(self
):
2286 self
._def
.value
= 'gerry'
2287 self
.assertNotEqual(self
._def
, 23)
2289 def test_str_op(self
):
2290 self
._def
.value
= 'marcel'
2291 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
2293 def test_repr_op(self
):
2294 self
._def
.value
= 'mireille'
2295 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
2298 class VariantFieldTestCase(unittest
.TestCase
):
2300 def _const_value_setter(field
):
2301 field
.selected_option_index
= 3
2304 def _create_fc(self
, tc
):
2305 ft0
= tc
.create_signed_integer_field_class(32)
2306 ft1
= tc
.create_string_field_class()
2307 ft2
= tc
.create_double_precision_real_field_class()
2308 ft3
= tc
.create_signed_integer_field_class(17)
2309 fc
= tc
.create_variant_field_class()
2310 fc
.append_option('corner', ft0
)
2311 fc
.append_option('zoom', ft1
)
2312 fc
.append_option('mellotron', ft2
)
2313 fc
.append_option('giorgio', ft3
)
2314 top_fc
= tc
.create_structure_field_class()
2315 top_fc
.append_member('variant_field', fc
)
2319 self
._tc
= get_default_trace_class()
2320 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2321 self
._def
= fld
['variant_field']
2323 self
._def
_value
= 1334
2324 self
._def
_selected
_index
= 3
2325 const_fc
= self
._create
_fc
(self
._tc
)['variant_field']
2327 fld_const
= create_const_field(
2328 self
._tc
, const_fc
.field_class
, self
._const
_value
_setter
2330 self
._def
_const
= fld_const
2332 def test_bool_op(self
):
2333 self
._def
.selected_option_index
= 2
2334 self
._def
.value
= -17.34
2335 with self
.assertRaises(NotImplementedError):
2338 def test_selected_option_index(self
):
2339 self
._def
.selected_option_index
= 2
2340 self
.assertEqual(self
._def
.selected_option_index
, 2)
2342 def test_selected_option_index_above_range(self
):
2343 with self
.assertRaises(IndexError):
2344 self
._def
.selected_option_index
= 4
2346 def test_selected_option_index_below_range(self
):
2347 with self
.assertRaises(IndexError):
2348 self
._def
.selected_option_index
= -1
2350 def test_const_selected_option_index(self
):
2351 with self
.assertRaises(AttributeError):
2352 self
._def
_const
.selected_option_index
= 2
2353 self
.assertEqual(self
._def
_const
.selected_option_index
, 3)
2355 def test_selected_option(self
):
2356 self
._def
.selected_option_index
= 2
2357 self
._def
.value
= -17.34
2358 self
.assertEqual(self
._def
.selected_option
, -17.34)
2359 self
.assertEqual(type(self
._def
.selected_option
), bt2
._DoublePrecisionRealField
)
2361 self
._def
.selected_option_index
= 3
2362 self
._def
.value
= 1921
2363 self
.assertEqual(self
._def
.selected_option
, 1921)
2364 self
.assertEqual(type(self
._def
.selected_option
), bt2
._SignedIntegerField
)
2366 def test_const_selected_option(self
):
2367 self
.assertEqual(self
._def
_const
.selected_option
, 1334)
2369 type(self
._def
_const
.selected_option
), bt2
._SignedIntegerFieldConst
2373 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2374 field
= field
['variant_field']
2375 field
.selected_option_index
= 0
2377 self
._def
.selected_option_index
= 0
2378 self
._def
.value
= 1774
2379 self
.assertEqual(self
._def
, field
)
2381 def test_const_eq(self
):
2382 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2383 field
= field
['variant_field']
2384 field
.selected_option_index
= 3
2386 self
.assertEqual(self
._def
_const
, field
)
2389 self
.assertEqual(len(self
._def
), 4)
2391 def test_eq_invalid_type(self
):
2392 self
._def
.selected_option_index
= 1
2393 self
._def
.value
= 'gerry'
2394 self
.assertNotEqual(self
._def
, 23)
2396 def test_str_op_int(self
):
2397 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2398 field
= field
['variant_field']
2399 field
.selected_option_index
= 0
2401 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2402 other_field
= other_field
['variant_field']
2403 other_field
.selected_option_index
= 0
2404 other_field
.value
= 1774
2405 self
.assertEqual(str(field
), str(other_field
))
2407 def test_str_op_str(self
):
2408 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2409 field
= field
['variant_field']
2410 field
.selected_option_index
= 1
2411 field
.value
= 'un beau grand bateau'
2412 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2413 other_field
= other_field
['variant_field']
2414 other_field
.selected_option_index
= 1
2415 other_field
.value
= 'un beau grand bateau'
2416 self
.assertEqual(str(field
), str(other_field
))
2418 def test_str_op_float(self
):
2419 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2420 field
= field
['variant_field']
2421 field
.selected_option_index
= 2
2422 field
.value
= 14.4245
2423 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2424 other_field
= other_field
['variant_field']
2425 other_field
.selected_option_index
= 2
2426 other_field
.value
= 14.4245
2427 self
.assertEqual(str(field
), str(other_field
))
2430 if __name__
== '__main__':