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
28 get_default_trace_class
,
29 TestOutputPortMessageIterator
,
34 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
37 # Create and return a stream with the field classes part of its stream packet
40 # The stream is part of a dummy trace created from trace class `tc`.
43 def _create_stream(tc
, ctx_field_classes
):
44 packet_context_fc
= tc
.create_structure_field_class()
45 for name
, fc
in ctx_field_classes
:
46 packet_context_fc
.append_member(name
, fc
)
49 stream_class
= tc
.create_stream_class(
50 packet_context_field_class
=packet_context_fc
, supports_packets
=True
53 stream
= trace
.create_stream(stream_class
)
57 # Create a field of the given field class.
59 # The field is part of a dummy stream, itself part of a dummy trace created
60 # from trace class `tc`.
63 def _create_field(tc
, field_class
):
65 stream
= _create_stream(tc
, [(field_name
, field_class
)])
66 packet
= stream
.create_packet()
67 return packet
.context_field
[field_name
]
70 # Create a field of type string.
72 # The field is part of a dummy stream, itself part of a dummy trace created
73 # from trace class `tc`. It is made out of a dummy string field class.
76 def _create_string_field(tc
):
77 field_name
= 'string_field'
78 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
79 packet
= stream
.create_packet()
80 return packet
.context_field
[field_name
]
83 # Create a field of type static array of ints.
85 # The field is part of a dummy stream, itself part of a dummy trace created
86 # from trace class `tc`. It is made out of a dummy static array field class,
87 # with a dummy integer field class as element class.
90 def _create_int_array_field(tc
, length
):
91 elem_fc
= tc
.create_signed_integer_field_class(32)
92 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
93 field_name
= 'int_array'
94 stream
= _create_stream(tc
, [(field_name
, fc
)])
95 packet
= stream
.create_packet()
96 return packet
.context_field
[field_name
]
99 # Create a field of type dynamic array of ints.
101 # The field is part of a dummy stream, itself part of a dummy trace created
102 # from trace class `tc`. It is made out of a dummy static array field class,
103 # with a dummy integer field class as element and length classes.
106 def _create_dynamic_array(tc
):
107 elem_fc
= tc
.create_signed_integer_field_class(32)
108 len_fc
= tc
.create_signed_integer_field_class(32)
109 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
110 field_name
= 'int_dyn_array'
111 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
112 packet
= stream
.create_packet()
113 packet
.context_field
[field_name
].length
= 3
114 return packet
.context_field
[field_name
]
117 # Create a field of type array of (empty) structures.
119 # The field is part of a dummy stream, itself part of a dummy trace created
120 # from trace class `tc`. It is made out of a dummy static array field class,
121 # with a dummy struct field class as element class.
124 def _create_struct_array_field(tc
, length
):
125 elem_fc
= tc
.create_structure_field_class()
126 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
127 field_name
= 'struct_array'
128 stream
= _create_stream(tc
, [(field_name
, fc
)])
129 packet
= stream
.create_packet()
130 return packet
.context_field
[field_name
]
133 class BitArrayFieldTestCase(unittest
.TestCase
):
134 def _create_field(self
):
135 return _create_field(self
._tc
, self
._tc
.create_bit_array_field_class(24))
138 self
._tc
= get_default_trace_class()
139 self
._def
_value
= 15497
140 self
._def
= self
._create
_field
()
141 self
._def
.value_as_integer
= self
._def
_value
142 self
._def
_new
_value
= 101542
144 def test_assign_invalid_type(self
):
145 with self
.assertRaises(TypeError):
146 self
._def
.value_as_integer
= 'onze'
148 def test_assign(self
):
149 self
._def
.value_as_integer
= 199
150 self
.assertEqual(self
._def
.value_as_integer
, 199)
152 def test_assign_masked(self
):
153 self
._def
.value_as_integer
= 0xE1549BB
154 self
.assertEqual(self
._def
.value_as_integer
, 0xE1549BB & ((1 << 24) - 1))
157 other
= self
._create
_field
()
158 other
.value_as_integer
= self
._def
_value
159 self
.assertEqual(self
._def
, other
)
161 def test_ne_same_type(self
):
162 other
= self
._create
_field
()
163 other
.value_as_integer
= self
._def
_value
- 1
164 self
.assertNotEqual(self
._def
, other
)
166 def test_ne_diff_type(self
):
167 self
.assertNotEqual(self
._def
, self
._def
_value
)
170 self
.assertEqual(len(self
._def
), 24)
173 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
176 self
.assertEqual(repr(self
._def
), repr(self
._def
_value
))
179 # Base class for numeric field test cases.
181 # To be compatible with this base class, a derived class must, in its
184 # * Set `self._def` to a field object with an arbitrary value.
185 # * Set `self._def_value` to the equivalent value of `self._def`.
186 class _TestNumericField
:
187 # Tries the binary operation `op`:
189 # 1. Between `self._def`, which is a field object, and `rhs`.
190 # 2. Between `self._def_value`, which is the raw value of
191 # `self._def`, and `rhs`.
193 # Returns the results of 1. and 2.
195 # If there's an exception while performing 1. or 2., asserts that
196 # both operations raised exceptions, that both exceptions have the
197 # same type, and returns `None` for both results.
198 def _binop(self
, op
, rhs
):
203 # try with field object
205 r
= op(self
._def
, rhs
)
206 except Exception as e
:
211 rv
= op(self
._def
_value
, comp_value
)
212 except Exception as e
:
215 if type_rexc
is not None or type_rvexc
is not None:
216 # at least one of the operations raised an exception: in
217 # this case both operations should have raised the same
218 # type of exception (division by zero, bit shift with a
219 # floating point number operand, etc.)
220 self
.assertIs(type_rexc
, type_rvexc
)
225 # Tries the unary operation `op`:
227 # 1. On `self._def`, which is a field object.
228 # 2. On `self._def_value`, which is the value of `self._def`.
230 # Returns the results of 1. and 2.
232 # If there's an exception while performing 1. or 2., asserts that
233 # both operations raised exceptions, that both exceptions have the
234 # same type, and returns `None` for both results.
235 def _unaryop(self
, op
):
239 # try with field object
242 except Exception as e
:
247 rv
= op(self
._def
_value
)
248 except Exception as e
:
251 if type_rexc
is not None or type_rvexc
is not None:
252 # at least one of the operations raised an exception: in
253 # this case both operations should have raised the same
254 # type of exception (division by zero, bit shift with a
255 # floating point number operand, etc.)
256 self
.assertIs(type_rexc
, type_rvexc
)
261 # Tests that the unary operation `op` gives results with the same
262 # type for both `self._def` and `self._def_value`.
263 def _test_unaryop_type(self
, op
):
264 r
, rv
= self
._unaryop
(op
)
269 self
.assertIsInstance(r
, type(rv
))
271 # Tests that the unary operation `op` gives results with the same
272 # value for both `self._def` and `self._def_value`. This uses the
273 # __eq__() operator of `self._def`.
274 def _test_unaryop_value(self
, op
):
275 r
, rv
= self
._unaryop
(op
)
280 self
.assertEqual(r
, rv
)
282 # Tests that the unary operation `op`, when applied to `self._def`,
283 # does not change its underlying BT object address.
284 def _test_unaryop_addr_same(self
, op
):
285 addr_before
= self
._def
.addr
287 self
.assertEqual(self
._def
.addr
, addr_before
)
289 # Tests that the unary operation `op`, when applied to `self._def`,
290 # does not change its value.
291 def _test_unaryop_value_same(self
, op
):
292 value_before
= copy
.copy(self
._def
_value
)
294 self
.assertEqual(self
._def
, value_before
)
296 # Tests that the binary operation `op` gives results with the same
297 # type for both `self._def` and `self._def_value`.
298 def _test_binop_type(self
, op
, rhs
):
299 r
, rv
= self
._binop
(op
, rhs
)
304 if op
in _COMP_BINOPS
:
305 # __eq__() and __ne__() always return a 'bool' object
306 self
.assertIsInstance(r
, bool)
308 self
.assertIsInstance(r
, type(rv
))
310 # Tests that the binary operation `op` gives results with the same
311 # value for both `self._def` and `self._def_value`. This uses the
312 # __eq__() operator of `self._def`.
313 def _test_binop_value(self
, op
, rhs
):
314 r
, rv
= self
._binop
(op
, rhs
)
319 self
.assertEqual(r
, rv
)
321 # Tests that the binary operation `op`, when applied to `self._def`,
322 # does not change its underlying BT object address.
323 def _test_binop_lhs_addr_same(self
, op
, rhs
):
324 addr_before
= self
._def
.addr
325 r
, rv
= self
._binop
(op
, rhs
)
326 self
.assertEqual(self
._def
.addr
, addr_before
)
328 # Tests that the binary operation `op`, when applied to `self._def`,
329 # does not change its value.
330 @unittest.skip('copy is not implemented')
331 def _test_binop_lhs_value_same(self
, op
, rhs
):
332 value_before
= copy
.copy(self
._def
)
333 r
, rv
= self
._binop
(op
, rhs
)
334 self
.assertEqual(self
._def
, value_before
)
336 # The methods below which take the `test_cb` and/or `op` parameters
337 # are meant to be used with one of the _test_binop_*() functions
338 # above as `test_cb` and a binary operator function as `op`.
342 # self._test_binop_rhs_pos_int(self._test_binop_value,
345 # This tests that a numeric field object added to a positive integer
346 # value gives a result with the expected value.
348 # `vint` and `vfloat` mean a signed integer value object and a real
351 def _test_binop_invalid_unknown(self
, op
):
352 if op
in _COMP_BINOPS
:
353 self
.skipTest('not testing')
358 with self
.assertRaises(TypeError):
361 def _test_binop_invalid_none(self
, op
):
362 if op
in _COMP_BINOPS
:
363 self
.skipTest('not testing')
365 with self
.assertRaises(TypeError):
368 def _test_binop_rhs_false(self
, test_cb
, op
):
371 def _test_binop_rhs_true(self
, test_cb
, op
):
374 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
377 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
380 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
383 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
384 test_cb(op
, bt2
.create_value(2))
386 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
387 test_cb(op
, bt2
.create_value(-23))
389 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
390 test_cb(op
, bt2
.create_value(0))
392 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
395 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
398 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
401 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
402 test_cb(op
, bt2
.create_value(2.2))
404 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
405 test_cb(op
, bt2
.create_value(-23.4))
407 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
408 test_cb(op
, bt2
.create_value(0.0))
410 def _test_binop_rhs_complex(self
, test_cb
, op
):
411 test_cb(op
, -23 + 19j
)
413 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
416 def _test_binop_type_false(self
, op
):
417 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
419 def _test_binop_type_true(self
, op
):
420 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
422 def _test_binop_type_pos_int(self
, op
):
423 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
425 def _test_binop_type_neg_int(self
, op
):
426 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
428 def _test_binop_type_zero_int(self
, op
):
429 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
431 def _test_binop_type_pos_vint(self
, op
):
432 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
434 def _test_binop_type_neg_vint(self
, op
):
435 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
437 def _test_binop_type_zero_vint(self
, op
):
438 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
440 def _test_binop_type_pos_float(self
, op
):
441 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
443 def _test_binop_type_neg_float(self
, op
):
444 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
446 def _test_binop_type_zero_float(self
, op
):
447 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
449 def _test_binop_type_pos_vfloat(self
, op
):
450 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
452 def _test_binop_type_neg_vfloat(self
, op
):
453 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
455 def _test_binop_type_zero_vfloat(self
, op
):
456 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
458 def _test_binop_type_complex(self
, op
):
459 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
461 def _test_binop_type_zero_complex(self
, op
):
462 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
464 def _test_binop_value_false(self
, op
):
465 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
467 def _test_binop_value_true(self
, op
):
468 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
470 def _test_binop_value_pos_int(self
, op
):
471 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
473 def _test_binop_value_neg_int(self
, op
):
474 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
476 def _test_binop_value_zero_int(self
, op
):
477 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
479 def _test_binop_value_pos_vint(self
, op
):
480 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
482 def _test_binop_value_neg_vint(self
, op
):
483 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
485 def _test_binop_value_zero_vint(self
, op
):
486 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
488 def _test_binop_value_pos_float(self
, op
):
489 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
491 def _test_binop_value_neg_float(self
, op
):
492 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
494 def _test_binop_value_zero_float(self
, op
):
495 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
497 def _test_binop_value_pos_vfloat(self
, op
):
498 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
500 def _test_binop_value_neg_vfloat(self
, op
):
501 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
503 def _test_binop_value_zero_vfloat(self
, op
):
504 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
506 def _test_binop_value_complex(self
, op
):
507 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
509 def _test_binop_value_zero_complex(self
, op
):
510 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
512 def _test_binop_lhs_addr_same_false(self
, op
):
513 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
515 def _test_binop_lhs_addr_same_true(self
, op
):
516 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
518 def _test_binop_lhs_addr_same_pos_int(self
, op
):
519 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
521 def _test_binop_lhs_addr_same_neg_int(self
, op
):
522 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
524 def _test_binop_lhs_addr_same_zero_int(self
, op
):
525 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
527 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
528 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
530 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
531 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
533 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
534 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
536 def _test_binop_lhs_addr_same_pos_float(self
, op
):
537 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
539 def _test_binop_lhs_addr_same_neg_float(self
, op
):
540 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
542 def _test_binop_lhs_addr_same_zero_float(self
, op
):
543 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
545 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
546 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
548 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
549 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
551 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
552 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
554 def _test_binop_lhs_addr_same_complex(self
, op
):
555 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
557 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
558 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
560 def _test_binop_lhs_value_same_false(self
, op
):
561 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
563 def _test_binop_lhs_value_same_true(self
, op
):
564 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
566 def _test_binop_lhs_value_same_pos_int(self
, op
):
567 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
569 def _test_binop_lhs_value_same_neg_int(self
, op
):
570 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
572 def _test_binop_lhs_value_same_zero_int(self
, op
):
573 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
575 def _test_binop_lhs_value_same_pos_vint(self
, op
):
576 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
578 def _test_binop_lhs_value_same_neg_vint(self
, op
):
579 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
581 def _test_binop_lhs_value_same_zero_vint(self
, op
):
582 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
584 def _test_binop_lhs_value_same_pos_float(self
, op
):
585 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
587 def _test_binop_lhs_value_same_neg_float(self
, op
):
588 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
590 def _test_binop_lhs_value_same_zero_float(self
, op
):
591 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
593 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
594 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
596 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
597 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
599 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
600 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
602 def _test_binop_lhs_value_same_complex(self
, op
):
603 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
605 def _test_binop_lhs_value_same_zero_complex(self
, op
):
606 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
608 def test_bool_op(self
):
609 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
611 def test_int_op(self
):
612 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
614 def test_float_op(self
):
615 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
617 def test_complex_op(self
):
618 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
620 def test_str_op(self
):
621 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
623 def test_hash_op(self
):
624 with self
.assertRaises(TypeError):
627 def test_const_hash_op(self
):
628 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
630 def test_const_hash_dict(self
):
632 my_dict
[self
._def
_const
] = 'my_value'
633 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
635 def test_eq_none(self
):
636 # Ignore this lint error:
637 # E711 comparison to None should be 'if cond is None:'
638 # since this is what we want to test (even though not good practice).
639 self
.assertFalse(self
._def
== None) # noqa: E711
641 def test_ne_none(self
):
642 # Ignore this lint error:
643 # E711 comparison to None should be 'if cond is not None:'
644 # since this is what we want to test (even though not good practice).
645 self
.assertTrue(self
._def
!= None) # noqa: E711
648 # This is a list of binary operators used for
649 # _inject_numeric_testing_methods().
651 # Each entry is a pair of binary operator name (used as part of the
652 # created testing method's name) and operator function.
660 ('add', operator
.add
),
661 ('radd', lambda a
, b
: operator
.add(b
, a
)),
662 ('and', operator
.and_
),
663 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
664 ('floordiv', operator
.floordiv
),
665 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
666 ('lshift', operator
.lshift
),
667 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
668 ('mod', operator
.mod
),
669 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
670 ('mul', operator
.mul
),
671 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
672 ('or', operator
.or_
),
673 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
674 ('pow', operator
.pow),
675 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
676 ('rshift', operator
.rshift
),
677 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
678 ('sub', operator
.sub
),
679 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
680 ('truediv', operator
.truediv
),
681 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
682 ('xor', operator
.xor
),
683 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
687 # This is a list of unary operators used for
688 # _inject_numeric_testing_methods().
690 # Each entry is a pair of unary operator name (used as part of the
691 # created testing method's name) and operator function.
693 ('neg', operator
.neg
),
694 ('pos', operator
.pos
),
695 ('abs', operator
.abs),
696 ('invert', operator
.invert
),
698 ('round_0', partial(round, ndigits
=0)),
699 ('round_1', partial(round, ndigits
=1)),
700 ('round_2', partial(round, ndigits
=2)),
701 ('round_3', partial(round, ndigits
=3)),
703 ('floor', math
.floor
),
704 ('trunc', math
.trunc
),
708 # This function injects a bunch of testing methods to a numeric
711 # It is meant to be used like this:
713 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
715 # This function injects:
717 # * One testing method for each _TestNumericField._test_binop_*()
718 # method, for each binary operator in the _BINOPS tuple.
720 # * One testing method for each _TestNumericField._test_unaryop*()
721 # method, for each unary operator in the _UNARYOPS tuple.
722 def _inject_numeric_testing_methods(cls
):
723 def test_binop_name(suffix
):
724 return 'test_binop_{}_{}'.format(name
, suffix
)
726 def test_unaryop_name(suffix
):
727 return 'test_unaryop_{}_{}'.format(name
, suffix
)
729 # inject testing methods for each binary operation
730 for name
, binop
in _BINOPS
:
733 test_binop_name('invalid_unknown'),
734 partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
),
738 test_binop_name('invalid_none'),
739 partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
),
743 test_binop_name('type_true'),
744 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
748 test_binop_name('type_pos_int'),
749 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
753 test_binop_name('type_pos_vint'),
754 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
758 test_binop_name('value_true'),
759 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
763 test_binop_name('value_pos_int'),
764 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
768 test_binop_name('value_pos_vint'),
769 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
773 test_binop_name('lhs_addr_same_true'),
774 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
778 test_binop_name('lhs_addr_same_pos_int'),
780 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
785 test_binop_name('lhs_addr_same_pos_vint'),
787 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
792 test_binop_name('lhs_value_same_true'),
793 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
797 test_binop_name('lhs_value_same_pos_int'),
799 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
804 test_binop_name('lhs_value_same_pos_vint'),
806 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
811 test_binop_name('type_neg_int'),
812 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
816 test_binop_name('type_neg_vint'),
817 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
821 test_binop_name('value_neg_int'),
822 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
826 test_binop_name('value_neg_vint'),
827 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
831 test_binop_name('lhs_addr_same_neg_int'),
833 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
838 test_binop_name('lhs_addr_same_neg_vint'),
840 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
845 test_binop_name('lhs_value_same_neg_int'),
847 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
852 test_binop_name('lhs_value_same_neg_vint'),
854 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
859 test_binop_name('type_false'),
860 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
864 test_binop_name('type_zero_int'),
865 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
869 test_binop_name('type_zero_vint'),
870 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
874 test_binop_name('value_false'),
875 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
879 test_binop_name('value_zero_int'),
880 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
884 test_binop_name('value_zero_vint'),
885 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
889 test_binop_name('lhs_addr_same_false'),
890 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
894 test_binop_name('lhs_addr_same_zero_int'),
896 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
901 test_binop_name('lhs_addr_same_zero_vint'),
903 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
908 test_binop_name('lhs_value_same_false'),
909 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
913 test_binop_name('lhs_value_same_zero_int'),
915 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
920 test_binop_name('lhs_value_same_zero_vint'),
922 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
927 test_binop_name('type_pos_float'),
928 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
932 test_binop_name('type_neg_float'),
933 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
937 test_binop_name('type_pos_vfloat'),
938 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
942 test_binop_name('type_neg_vfloat'),
943 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
947 test_binop_name('value_pos_float'),
948 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
952 test_binop_name('value_neg_float'),
953 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
957 test_binop_name('value_pos_vfloat'),
958 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
962 test_binop_name('value_neg_vfloat'),
963 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
967 test_binop_name('lhs_addr_same_pos_float'),
969 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
974 test_binop_name('lhs_addr_same_neg_float'),
976 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
981 test_binop_name('lhs_addr_same_pos_vfloat'),
983 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
988 test_binop_name('lhs_addr_same_neg_vfloat'),
990 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
995 test_binop_name('lhs_value_same_pos_float'),
997 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
1002 test_binop_name('lhs_value_same_neg_float'),
1004 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
1009 test_binop_name('lhs_value_same_pos_vfloat'),
1011 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
1016 test_binop_name('lhs_value_same_neg_vfloat'),
1018 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
1023 test_binop_name('type_zero_float'),
1024 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
1028 test_binop_name('type_zero_vfloat'),
1029 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
1033 test_binop_name('value_zero_float'),
1034 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
1038 test_binop_name('value_zero_vfloat'),
1039 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
1043 test_binop_name('lhs_addr_same_zero_float'),
1045 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
1050 test_binop_name('lhs_addr_same_zero_vfloat'),
1052 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
1057 test_binop_name('lhs_value_same_zero_float'),
1059 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1064 test_binop_name('lhs_value_same_zero_vfloat'),
1066 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1071 test_binop_name('type_complex'),
1072 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1076 test_binop_name('type_zero_complex'),
1077 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1081 test_binop_name('value_complex'),
1082 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1086 test_binop_name('value_zero_complex'),
1087 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1091 test_binop_name('lhs_addr_same_complex'),
1093 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1098 test_binop_name('lhs_addr_same_zero_complex'),
1100 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1105 test_binop_name('lhs_value_same_complex'),
1107 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1112 test_binop_name('lhs_value_same_zero_complex'),
1114 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1118 # inject testing methods for each unary operation
1119 for name
, unaryop
in _UNARYOPS
:
1122 test_unaryop_name('type'),
1123 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1127 test_unaryop_name('value'),
1128 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1132 test_unaryop_name('addr_same'),
1133 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1137 test_unaryop_name('value_same'),
1138 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1142 class BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1144 def _const_value_setter(field
):
1147 def _create_fc(self
, tc
):
1148 return tc
.create_bool_field_class()
1151 self
._tc
= get_default_trace_class()
1152 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1153 self
._def
.value
= True
1154 self
._def
_value
= True
1155 self
._def
_const
= create_const_field(
1156 self
._tc
, self
._tc
.create_bool_field_class(), self
._const
_value
_setter
1158 self
._def
_new
_value
= False
1160 def test_classes(self
):
1161 self
.assertIs(type(self
._def
), bt2
._BoolField
)
1162 self
.assertIs(type(self
._def
_const
), bt2
._BoolFieldConst
)
1164 def test_assign_true(self
):
1166 self
._def
.value
= raw
1167 self
.assertEqual(self
._def
, raw
)
1169 def test_assign_false(self
):
1171 self
._def
.value
= raw
1172 self
.assertEqual(self
._def
, raw
)
1174 def test_assign_field_true(self
):
1175 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1178 self
._def
.value
= field
1179 self
.assertEqual(self
._def
, raw
)
1181 def test_assign_field_false(self
):
1182 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1185 self
._def
.value
= field
1186 self
.assertEqual(self
._def
, raw
)
1188 def test_assign_invalid_type(self
):
1189 with self
.assertRaises(TypeError):
1190 self
._def
.value
= 17
1192 def test_str_op(self
):
1193 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1196 _inject_numeric_testing_methods(BoolFieldTestCase
)
1199 class _TestIntegerFieldCommon(_TestNumericField
):
1200 def test_assign_true(self
):
1202 self
._def
.value
= raw
1203 self
.assertEqual(self
._def
, raw
)
1205 def test_assign_false(self
):
1207 self
._def
.value
= raw
1208 self
.assertEqual(self
._def
, raw
)
1210 def test_assign_pos_int(self
):
1212 self
._def
.value
= raw
1213 self
.assertEqual(self
._def
, raw
)
1215 def test_assign_neg_int(self
):
1217 self
._def
.value
= raw
1218 self
.assertEqual(self
._def
, raw
)
1220 def test_assign_int_field(self
):
1222 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1224 self
._def
.value
= field
1225 self
.assertEqual(self
._def
, raw
)
1227 def test_assign_invalid_type(self
):
1228 with self
.assertRaises(TypeError):
1229 self
._def
.value
= 'yes'
1231 def test_assign_uint(self
):
1232 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1233 field
= _create_field(self
._tc
, uint_fc
)
1236 self
.assertEqual(field
, raw
)
1238 def test_assign_big_uint(self
):
1239 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1240 field
= _create_field(self
._tc
, uint_fc
)
1241 # Larger than the IEEE 754 double-precision exact representation of
1244 field
.value
= (2 ** 53) + 1
1245 self
.assertEqual(field
, raw
)
1247 def test_assign_uint_out_of_range(self
):
1248 uint_fc
= self
._tc
.create_unsigned_integer_field_class(8)
1249 field
= _create_field(self
._tc
, uint_fc
)
1251 with self
.assertRaises(ValueError) as ctx
:
1254 str(ctx
.exception
), 'Value 256 is outside valid range [0, 255]'
1257 with self
.assertRaises(ValueError) as ctx
:
1259 self
.assertEqual(str(ctx
.exception
), 'Value -1 is outside valid range [0, 255]')
1261 def test_assign_int_out_of_range(self
):
1262 int_fc
= self
._tc
.create_signed_integer_field_class(8)
1263 field
= _create_field(self
._tc
, int_fc
)
1265 with self
.assertRaises(ValueError) as ctx
:
1268 str(ctx
.exception
), 'Value 128 is outside valid range [-128, 127]'
1271 with self
.assertRaises(ValueError) as ctx
:
1274 str(ctx
.exception
), 'Value -129 is outside valid range [-128, 127]'
1277 def test_str_op(self
):
1278 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1281 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1284 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1286 def _const_value_setter(field
):
1289 def _create_fc(self
, tc
):
1290 return tc
.create_signed_integer_field_class(25)
1293 self
._tc
= get_default_trace_class()
1294 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1295 self
._field
.value
= 17
1296 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1297 self
._def
.value
= 17
1298 self
._def
_value
= 17
1299 self
._def
_const
= create_const_field(
1300 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1302 self
._def
_new
_value
= -101
1305 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1307 def _const_value_setter(field
):
1310 def _create_fc(self
, tc
):
1311 fc
= tc
.create_signed_enumeration_field_class(32)
1312 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1313 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1314 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1316 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1318 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1322 self
._tc
= get_default_trace_class()
1323 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1324 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1325 self
._def
.value
= 17
1326 self
._def
_value
= 17
1327 self
._def
_const
= create_const_field(
1328 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1330 self
._def
_new
_value
= -101
1332 def test_str_op(self
):
1333 expected_string_found
= False
1336 # Establish all permutations of the three expected matches since
1337 # the order in which mappings are enumerated is not explicitly part of
1339 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1340 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1342 expected_string_found
= True
1345 self
.assertTrue(expected_string_found
)
1347 def test_labels(self
):
1348 self
._field
.value
= 17
1349 labels
= sorted(self
._field
.labels
)
1350 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1353 class SingleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1355 def _const_value_setter(field
):
1358 def _create_fc(self
, tc
):
1359 return tc
.create_single_precision_real_field_class()
1362 self
._tc
= get_default_trace_class()
1363 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1364 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1365 self
._def
_const
= create_const_field(
1367 self
._tc
.create_single_precision_real_field_class(),
1368 self
._const
_value
_setter
,
1370 self
._def
.value
= 52.0
1371 self
._def
_value
= 52.0
1372 self
._def
_new
_value
= -17.0
1374 def _test_invalid_op(self
, cb
):
1375 with self
.assertRaises(TypeError):
1378 def test_assign_true(self
):
1379 self
._def
.value
= True
1380 self
.assertTrue(self
._def
)
1382 def test_assign_false(self
):
1383 self
._def
.value
= False
1384 self
.assertFalse(self
._def
)
1386 def test_assign_pos_int(self
):
1388 self
._def
.value
= raw
1389 self
.assertEqual(self
._def
, float(raw
))
1391 def test_assign_neg_int(self
):
1393 self
._def
.value
= raw
1394 self
.assertEqual(self
._def
, float(raw
))
1396 def test_assign_int_field(self
):
1397 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1398 int_field
= _create_field(self
._tc
, int_fc
)
1400 int_field
.value
= raw
1401 self
._def
.value
= int_field
1402 self
.assertEqual(self
._def
, float(raw
))
1404 def test_assign_float(self
):
1406 self
._def
.value
= raw
1407 # It's expected to have some lost of precision because of the field
1408 # that is in single precision.
1409 self
.assertEqual(round(self
._def
, 5), raw
)
1411 def test_assign_float_field(self
):
1412 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1415 self
._def
.value
= field
1416 # It's expected to have some lost of precision because of the field
1417 # that is in single precision.
1418 self
.assertEqual(round(self
._def
, 5), raw
)
1420 def test_assign_invalid_type(self
):
1421 with self
.assertRaises(TypeError):
1422 self
._def
.value
= 'yes'
1424 def test_invalid_lshift(self
):
1425 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1427 def test_invalid_rshift(self
):
1428 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1430 def test_invalid_and(self
):
1431 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1433 def test_invalid_or(self
):
1434 self
._test
_invalid
_op
(lambda: self
._def |
23)
1436 def test_invalid_xor(self
):
1437 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1439 def test_invalid_invert(self
):
1440 self
._test
_invalid
_op
(lambda: ~self
._def
)
1442 def test_str_op(self
):
1443 self
.assertEqual(str(round(self
._def
, 5)), str(self
._def
_value
))
1446 _inject_numeric_testing_methods(SingleRealFieldTestCase
)
1449 class DoubleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1451 def _const_value_setter(field
):
1454 def _create_fc(self
, tc
):
1455 return tc
.create_double_precision_real_field_class()
1458 self
._tc
= get_default_trace_class()
1459 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1460 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1461 self
._def
_const
= create_const_field(
1463 self
._tc
.create_double_precision_real_field_class(),
1464 self
._const
_value
_setter
,
1466 self
._def
.value
= 52.7
1467 self
._def
_value
= 52.7
1468 self
._def
_new
_value
= -17.164857
1470 def _test_invalid_op(self
, cb
):
1471 with self
.assertRaises(TypeError):
1474 def test_assign_true(self
):
1475 self
._def
.value
= True
1476 self
.assertTrue(self
._def
)
1478 def test_assign_false(self
):
1479 self
._def
.value
= False
1480 self
.assertFalse(self
._def
)
1482 def test_assign_pos_int(self
):
1484 self
._def
.value
= raw
1485 self
.assertEqual(self
._def
, float(raw
))
1487 def test_assign_neg_int(self
):
1489 self
._def
.value
= raw
1490 self
.assertEqual(self
._def
, float(raw
))
1492 def test_assign_int_field(self
):
1493 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1494 int_field
= _create_field(self
._tc
, int_fc
)
1496 int_field
.value
= raw
1497 self
._def
.value
= int_field
1498 self
.assertEqual(self
._def
, float(raw
))
1500 def test_assign_float(self
):
1502 self
._def
.value
= raw
1503 self
.assertEqual(self
._def
, raw
)
1505 def test_assign_float_field(self
):
1506 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1509 self
._def
.value
= field
1510 self
.assertEqual(self
._def
, raw
)
1512 def test_assign_invalid_type(self
):
1513 with self
.assertRaises(TypeError):
1514 self
._def
.value
= 'yes'
1516 def test_invalid_lshift(self
):
1517 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1519 def test_invalid_rshift(self
):
1520 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1522 def test_invalid_and(self
):
1523 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1525 def test_invalid_or(self
):
1526 self
._test
_invalid
_op
(lambda: self
._def |
23)
1528 def test_invalid_xor(self
):
1529 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1531 def test_invalid_invert(self
):
1532 self
._test
_invalid
_op
(lambda: ~self
._def
)
1534 def test_str_op(self
):
1535 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1538 _inject_numeric_testing_methods(DoubleRealFieldTestCase
)
1541 class StringFieldTestCase(unittest
.TestCase
):
1543 def _const_value_setter(field
):
1544 field
.value
= 'Hello, World!'
1547 self
._tc
= get_default_trace_class()
1548 self
._def
_value
= 'Hello, World!'
1549 self
._def
= _create_string_field(self
._tc
)
1550 self
._def
_const
= create_const_field(
1551 self
._tc
, self
._tc
.create_string_field_class(), self
._const
_value
_setter
1553 self
._def
.value
= self
._def
_value
1554 self
._def
_new
_value
= 'Yes!'
1556 def test_assign_int(self
):
1557 with self
.assertRaises(TypeError):
1558 self
._def
.value
= 283
1560 def test_assign_string_field(self
):
1561 field
= _create_string_field(self
._tc
)
1564 self
.assertEqual(field
, raw
)
1567 self
.assertEqual(self
._def
, self
._def
_value
)
1569 def test_const_eq(self
):
1570 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1572 def test_not_eq(self
):
1573 self
.assertNotEqual(self
._def
, 23)
1575 def test_lt_vstring(self
):
1576 s1
= _create_string_field(self
._tc
)
1578 s2
= _create_string_field(self
._tc
)
1580 self
.assertLess(s1
, s2
)
1582 def test_lt_string(self
):
1583 s1
= _create_string_field(self
._tc
)
1585 self
.assertLess(s1
, 'bateau')
1587 def test_le_vstring(self
):
1588 s1
= _create_string_field(self
._tc
)
1590 s2
= _create_string_field(self
._tc
)
1592 self
.assertLessEqual(s1
, s2
)
1594 def test_le_string(self
):
1595 s1
= _create_string_field(self
._tc
)
1597 self
.assertLessEqual(s1
, 'bateau')
1599 def test_gt_vstring(self
):
1600 s1
= _create_string_field(self
._tc
)
1602 s2
= _create_string_field(self
._tc
)
1604 self
.assertGreater(s2
, s1
)
1606 def test_gt_string(self
):
1607 s1
= _create_string_field(self
._tc
)
1609 self
.assertGreater('bateau', s1
)
1611 def test_ge_vstring(self
):
1612 s1
= _create_string_field(self
._tc
)
1614 s2
= _create_string_field(self
._tc
)
1616 self
.assertGreaterEqual(s2
, s1
)
1618 def test_ge_string(self
):
1619 s1
= _create_string_field(self
._tc
)
1621 self
.assertGreaterEqual('bateau', s1
)
1623 def test_bool_op(self
):
1624 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1626 def test_str_op(self
):
1627 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1630 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1632 def test_getitem(self
):
1633 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1635 def test_const_getitem(self
):
1636 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1638 def test_append_str(self
):
1639 to_append
= 'meow meow meow'
1640 self
._def
+= to_append
1641 self
._def
_value
+= to_append
1642 self
.assertEqual(self
._def
, self
._def
_value
)
1644 def test_const_append_str(self
):
1645 to_append
= 'meow meow meow'
1646 with self
.assertRaises(TypeError):
1647 self
._def
_const
+= to_append
1648 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1650 def test_append_string_field(self
):
1651 field
= _create_string_field(self
._tc
)
1652 to_append
= 'meow meow meow'
1653 field
.value
= to_append
1655 self
._def
_value
+= to_append
1656 self
.assertEqual(self
._def
, self
._def
_value
)
1658 def test_hash_op(self
):
1659 with self
.assertRaises(TypeError):
1662 def test_const_hash_op(self
):
1663 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
1665 def test_const_hash_dict(self
):
1667 my_dict
[self
._def
_const
] = 'my_value'
1668 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
1671 class _TestArrayFieldCommon
:
1672 def _modify_def(self
):
1675 def test_bool_op_true(self
):
1676 self
.assertTrue(self
._def
)
1679 self
.assertEqual(len(self
._def
), 3)
1681 def test_length(self
):
1682 self
.assertEqual(self
._def
.length
, 3)
1684 def test_getitem(self
):
1685 field
= self
._def
[1]
1686 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1687 self
.assertEqual(field
, 1847)
1689 def test_const_getitem(self
):
1690 field
= self
._def
_const
[1]
1691 self
.assertIs(type(field
), bt2
._SignedIntegerFieldConst
)
1692 self
.assertEqual(field
, 1847)
1695 field
= _create_int_array_field(self
._tc
, 3)
1699 self
.assertEqual(self
._def
, field
)
1701 def test_eq_invalid_type(self
):
1702 self
.assertNotEqual(self
._def
, 23)
1704 def test_eq_diff_len(self
):
1705 field
= _create_int_array_field(self
._tc
, 2)
1708 self
.assertNotEqual(self
._def
, field
)
1710 def test_eq_diff_content_same_len(self
):
1711 field
= _create_int_array_field(self
._tc
, 3)
1715 self
.assertNotEqual(self
._def
, field
)
1717 def test_eq_non_sequence_iterable(self
):
1718 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1719 field
= _create_int_array_field(self
._tc
, 3)
1723 self
.assertEqual(field
, list(dct
.keys()))
1724 self
.assertNotEqual(field
, dct
)
1726 def test_setitem(self
):
1728 self
.assertEqual(self
._def
[2], 24)
1730 def test_setitem_int_field(self
):
1731 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1732 int_field
= _create_field(self
._tc
, int_fc
)
1733 int_field
.value
= 19487
1734 self
._def
[1] = int_field
1735 self
.assertEqual(self
._def
[1], 19487)
1737 def test_setitem_non_basic_field(self
):
1738 array_field
= _create_struct_array_field(self
._tc
, 2)
1739 with self
.assertRaises(TypeError):
1742 def test_setitem_none(self
):
1743 with self
.assertRaises(TypeError):
1746 def test_setitem_index_wrong_type(self
):
1747 with self
.assertRaises(TypeError):
1748 self
._def
['yes'] = 23
1750 def test_setitem_index_neg(self
):
1751 with self
.assertRaises(IndexError):
1754 def test_setitem_index_out_of_range(self
):
1755 with self
.assertRaises(IndexError):
1756 self
._def
[len(self
._def
)] = 134679
1758 def test_const_setitem(self
):
1759 with self
.assertRaises(TypeError):
1760 self
._def
_const
[0] = 134679
1762 def test_iter(self
):
1763 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1764 self
.assertEqual(field
, value
)
1766 def test_const_iter(self
):
1767 for field
, value
in zip(self
._def
_const
, (45, 1847, 1948754)):
1768 self
.assertEqual(field
, value
)
1770 def test_value_int_field(self
):
1771 values
= [45646, 145, 12145]
1772 self
._def
.value
= values
1773 self
.assertEqual(values
, self
._def
)
1775 def test_value_check_sequence(self
):
1777 with self
.assertRaises(TypeError):
1778 self
._def
.value
= values
1780 def test_value_wrong_type_in_sequence(self
):
1781 values
= [32, 'hello', 11]
1782 with self
.assertRaises(TypeError):
1783 self
._def
.value
= values
1785 def test_value_complex_type(self
):
1786 struct_fc
= self
._tc
.create_structure_field_class()
1787 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1788 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1789 str_fc
= self
._tc
.create_string_field_class()
1790 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1791 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1792 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1793 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1794 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1796 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1797 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1798 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1801 array
= stream
.create_packet().context_field
['array_field']
1802 array
.value
= values
1803 self
.assertEqual(values
, array
)
1804 values
[0]['an_int'] = 'a string'
1805 with self
.assertRaises(TypeError):
1806 array
.value
= values
1808 def test_str_op(self
):
1810 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1811 self
.assertEqual(expected_string
, s
)
1814 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1816 def _const_value_setter(field
):
1817 field
.value
= [45, 1847, 1948754]
1820 self
._tc
= get_default_trace_class()
1821 self
._def
= _create_int_array_field(self
._tc
, 3)
1824 self
._def
[2] = 1948754
1825 self
._def
_value
= [45, 1847, 1948754]
1826 self
._def
_const
= create_const_field(
1828 self
._tc
.create_static_array_field_class(
1829 self
._tc
.create_signed_integer_field_class(32), 3
1831 self
._const
_value
_setter
,
1834 def test_value_wrong_len(self
):
1836 with self
.assertRaises(ValueError):
1837 self
._def
.value
= values
1840 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1842 def _const_value_setter(field
):
1843 field
.value
= [45, 1847, 1948754]
1846 self
._tc
= get_default_trace_class()
1847 self
._def
= _create_dynamic_array(self
._tc
)
1850 self
._def
[2] = 1948754
1851 self
._def
_value
= [45, 1847, 1948754]
1852 self
._def
_const
= create_const_field(
1854 self
._tc
.create_dynamic_array_field_class(
1855 self
._tc
.create_signed_integer_field_class(32)
1857 self
._const
_value
_setter
,
1860 def test_value_resize(self
):
1861 new_values
= [1, 2, 3, 4]
1862 self
._def
.value
= new_values
1863 self
.assertCountEqual(self
._def
, new_values
)
1865 def test_set_length(self
):
1866 self
._def
.length
= 4
1868 self
.assertEqual(len(self
._def
), 4)
1870 def test_const_set_length(self
):
1871 with self
.assertRaises(AttributeError):
1872 self
._def
_const
.length
= 4
1873 self
.assertEqual(len(self
._def
), 3)
1875 def test_set_invalid_length(self
):
1876 with self
.assertRaises(TypeError):
1877 self
._def
.length
= 'cheval'
1880 class StructureFieldTestCase(unittest
.TestCase
):
1882 def _const_value_setter(field
):
1892 def _create_fc(self
, tc
):
1893 fc
= tc
.create_structure_field_class()
1894 fc
.append_member('A', self
._fc
0_fn
())
1895 fc
.append_member('B', self
._fc
1_fn
())
1896 fc
.append_member('C', self
._fc
2_fn
())
1897 fc
.append_member('D', self
._fc
3_fn
())
1898 fc
.append_member('E', self
._fc
4_fn
())
1899 fc5
= self
._fc
5_fn
()
1900 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1901 fc
.append_member('F', fc5
)
1905 self
._tc
= get_default_trace_class()
1906 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1907 self
._fc
1_fn
= self
._tc
.create_string_field_class
1908 self
._fc
2_fn
= self
._tc
.create_double_precision_real_field_class
1909 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1910 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1911 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1912 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1914 self
._fc
= self
._create
_fc
(self
._tc
)
1915 self
._def
= _create_field(self
._tc
, self
._fc
)
1916 self
._def
['A'] = -1872
1917 self
._def
['B'] = 'salut'
1918 self
._def
['C'] = 17.5
1919 self
._def
['D'] = 16497
1921 self
._def
['F'] = {'F_1': 52}
1931 self
._def
_const
= create_const_field(
1932 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1935 def _modify_def(self
):
1936 self
._def
['B'] = 'hola'
1938 def test_bool_op_true(self
):
1939 self
.assertTrue(self
._def
)
1941 def test_bool_op_false(self
):
1942 field
= self
._def
['E']
1943 self
.assertFalse(field
)
1946 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1948 def test_getitem(self
):
1949 field1
= self
._def
['A']
1950 field2
= self
._def
['B']
1951 field3
= self
._def
['C']
1952 field4
= self
._def
['D']
1953 field5
= self
._def
['E']
1954 field6
= self
._def
['F']
1956 self
.assertIs(type(field1
), bt2
._SignedIntegerField
)
1957 self
.assertEqual(field1
, -1872)
1959 self
.assertIs(type(field2
), bt2
._StringField
)
1960 self
.assertEqual(field2
, 'salut')
1962 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealField
)
1963 self
.assertEqual(field3
, 17.5)
1965 self
.assertIs(type(field4
), bt2
._SignedIntegerField
)
1966 self
.assertEqual(field4
, 16497)
1968 self
.assertIs(type(field5
), bt2
._StructureField
)
1969 self
.assertEqual(field5
, {})
1971 self
.assertIs(type(field6
), bt2
._StructureField
)
1972 self
.assertEqual(field6
, {'F_1': 52})
1974 def test_const_getitem(self
):
1975 field1
= self
._def
_const
['A']
1976 field2
= self
._def
_const
['B']
1977 field3
= self
._def
_const
['C']
1978 field4
= self
._def
_const
['D']
1979 field5
= self
._def
_const
['E']
1980 field6
= self
._def
_const
['F']
1982 self
.assertIs(type(field1
), bt2
._SignedIntegerFieldConst
)
1983 self
.assertEqual(field1
, -1872)
1985 self
.assertIs(type(field2
), bt2
._StringFieldConst
)
1986 self
.assertEqual(field2
, 'salut')
1988 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealFieldConst
)
1989 self
.assertEqual(field3
, 17.5)
1991 self
.assertIs(type(field4
), bt2
._SignedIntegerFieldConst
)
1992 self
.assertEqual(field4
, 16497)
1994 self
.assertIs(type(field5
), bt2
._StructureFieldConst
)
1995 self
.assertEqual(field5
, {})
1997 self
.assertIs(type(field6
), bt2
._StructureFieldConst
)
1998 self
.assertEqual(field6
, {'F_1': 52})
2000 def test_member_at_index_out_of_bounds_after(self
):
2001 with self
.assertRaises(IndexError):
2002 self
._def
.member_at_index(len(self
._def
_value
))
2005 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2007 field
['B'] = 'salut'
2011 field
['F'] = {'F_1': 52}
2012 self
.assertEqual(self
._def
, field
)
2014 def test_const_eq(self
):
2015 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2017 field
['B'] = 'salut'
2021 field
['F'] = {'F_1': 52}
2022 self
.assertEqual(self
._def
_const
, field
)
2024 def test_eq_invalid_type(self
):
2025 self
.assertNotEqual(self
._def
, 23)
2027 def test_eq_diff_len(self
):
2028 fc
= self
._tc
.create_structure_field_class()
2029 fc
.append_member('A', self
._fc
0_fn
())
2030 fc
.append_member('B', self
._fc
1_fn
())
2031 fc
.append_member('C', self
._fc
2_fn
())
2033 field
= _create_field(self
._tc
, fc
)
2035 field
['B'] = 'salut'
2037 self
.assertNotEqual(self
._def
, field
)
2039 def test_eq_diff_keys(self
):
2040 fc
= self
._tc
.create_structure_field_class()
2041 fc
.append_member('U', self
._fc
0_fn
())
2042 fc
.append_member('V', self
._fc
1_fn
())
2043 fc
.append_member('W', self
._fc
2_fn
())
2044 fc
.append_member('X', self
._fc
3_fn
())
2045 fc
.append_member('Y', self
._fc
4_fn
())
2046 fc
.append_member('Z', self
._fc
5_fn
())
2047 field
= _create_field(self
._tc
, fc
)
2049 field
['V'] = "gerry"
2054 self
.assertNotEqual(self
._def
, field
)
2056 def test_eq_diff_content_same_len(self
):
2057 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2059 field
['B'] = 'salut'
2063 field
['F'] = {'F_1': 0}
2064 self
.assertNotEqual(self
._def
, field
)
2066 def test_eq_same_content_diff_keys(self
):
2067 fc
= self
._tc
.create_structure_field_class()
2068 fc
.append_member('A', self
._fc
0_fn
())
2069 fc
.append_member('B', self
._fc
1_fn
())
2070 fc
.append_member('E', self
._fc
2_fn
())
2071 fc
.append_member('D', self
._fc
3_fn
())
2072 fc
.append_member('C', self
._fc
4_fn
())
2073 fc
.append_member('F', self
._fc
5_fn
())
2074 field
= _create_field(self
._tc
, fc
)
2076 field
['B'] = 'salut'
2081 self
.assertNotEqual(self
._def
, field
)
2083 def test_setitem(self
):
2084 self
._def
['C'] = -18.47
2085 self
.assertEqual(self
._def
['C'], -18.47)
2087 def test_const_setitem(self
):
2088 with self
.assertRaises(TypeError):
2089 self
._def
_const
['A'] = 134679
2091 def test_setitem_int_field(self
):
2092 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2093 int_field
= _create_field(self
._tc
, int_fc
)
2094 int_field
.value
= 19487
2095 self
._def
['D'] = int_field
2096 self
.assertEqual(self
._def
['D'], 19487)
2098 def test_setitem_non_basic_field(self
):
2099 elem_fc
= self
._tc
.create_structure_field_class()
2100 struct_fc
= self
._tc
.create_structure_field_class()
2101 struct_fc
.append_member('A', elem_fc
)
2102 struct_field
= _create_field(self
._tc
, struct_fc
)
2104 # Will fail on access to .items() of the value
2105 with self
.assertRaises(AttributeError):
2106 struct_field
['A'] = 23
2108 def test_setitem_none(self
):
2109 with self
.assertRaises(TypeError):
2110 self
._def
['C'] = None
2112 def test_setitem_key_wrong_type(self
):
2113 with self
.assertRaises(TypeError):
2116 def test_setitem_wrong_key(self
):
2117 with self
.assertRaises(KeyError):
2118 self
._def
['hi'] = 134679
2120 def test_member_at_index(self
):
2121 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
2123 def test_const_member_at_index(self
):
2124 self
.assertEqual(self
._def
_const
.member_at_index(1), 'salut')
2126 def test_iter(self
):
2136 for vkey
, vval
in self
._def
.items():
2137 val
= orig_values
[vkey
]
2138 self
.assertEqual(vval
, val
)
2140 def test_value(self
):
2149 self
.assertEqual(self
._def
, orig_values
)
2151 def test_set_value(self
):
2152 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2153 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
2154 str_fc
= self
._tc
.create_string_field_class()
2155 struct_fc
= self
._tc
.create_structure_field_class()
2156 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
2157 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
2158 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
2159 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2161 struct
= _create_field(self
._tc
, struct_fc
)
2162 struct
.value
= values
2163 self
.assertEqual(values
, struct
)
2165 bad_type_values
= copy
.deepcopy(values
)
2166 bad_type_values
['an_int'] = 'a string'
2167 with self
.assertRaises(TypeError):
2168 struct
.value
= bad_type_values
2170 unknown_key_values
= copy
.deepcopy(values
)
2171 unknown_key_values
['unknown_key'] = 16546
2172 with self
.assertRaises(KeyError):
2173 struct
.value
= unknown_key_values
2175 def test_str_op(self
):
2176 expected_string_found
= False
2178 # Establish all permutations of the three expected matches since
2179 # the order in which mappings are enumerated is not explicitly part of
2181 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
2182 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
2183 candidate
= '{{{}}}'.format(', '.join(items
))
2185 expected_string_found
= True
2188 self
.assertTrue(expected_string_found
)
2191 class OptionFieldTestCase(unittest
.TestCase
):
2193 def _const_value_setter(field
):
2194 field
.value
= {'opt_field': 'hiboux'}
2196 def _create_fc(self
, tc
):
2197 fc
= tc
.create_option_without_selector_field_class(
2198 tc
.create_string_field_class()
2200 top_fc
= tc
.create_structure_field_class()
2201 top_fc
.append_member('opt_field', fc
)
2205 self
._tc
= get_default_trace_class()
2206 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2207 self
._def
= fld
['opt_field']
2208 self
._def
_value
= 'hiboux'
2209 self
._def
_const
= create_const_field(
2210 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
2213 def test_value_prop(self
):
2214 self
._def
.value
= 'hiboux'
2215 self
.assertEqual(self
._def
.field
, 'hiboux')
2216 self
.assertIs(type(self
._def
), bt2
._OptionField
)
2217 self
.assertIs(type(self
._def
.field
), bt2
._StringField
)
2218 self
.assertTrue(self
._def
.has_field
)
2220 def test_const_value_prop(self
):
2221 self
.assertEqual(self
._def
_const
.field
, 'hiboux')
2222 self
.assertIs(type(self
._def
_const
), bt2
._OptionFieldConst
)
2223 self
.assertIs(type(self
._def
_const
.field
), bt2
._StringFieldConst
)
2224 self
.assertTrue(self
._def
_const
.has_field
)
2226 def test_has_field_prop_true(self
):
2227 self
._def
.has_field
= True
2228 self
.assertTrue(self
._def
.has_field
)
2230 def test_has_field_prop_false(self
):
2231 self
._def
.has_field
= False
2232 self
.assertFalse(self
._def
.has_field
)
2234 def test_bool_op_true(self
):
2235 self
._def
.value
= 'allo'
2236 self
.assertTrue(self
._def
)
2238 def test_bool_op_false(self
):
2239 self
._def
.has_field
= False
2240 self
.assertFalse(self
._def
)
2242 def test_field_prop_existing(self
):
2243 self
._def
.value
= 'meow'
2244 field
= self
._def
.field
2245 self
.assertEqual(field
, 'meow')
2247 def test_field_prop_none(self
):
2248 self
._def
.has_field
= False
2249 field
= self
._def
.field
2250 self
.assertIsNone(field
)
2252 def test_const_field_prop(self
):
2253 with self
.assertRaises(AttributeError):
2254 self
._def
_const
.has_field
= False
2256 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2257 self
.assertTrue(self
._def
_const
.has_field
)
2259 def test_field_prop_existing_then_none(self
):
2260 self
._def
.value
= 'meow'
2261 field
= self
._def
.field
2262 self
.assertEqual(field
, 'meow')
2263 self
._def
.has_field
= False
2264 field
= self
._def
.field
2265 self
.assertIsNone(field
)
2268 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2269 field
= field
['opt_field']
2270 field
.value
= 'walk'
2271 self
._def
.value
= 'walk'
2272 self
.assertEqual(self
._def
, field
)
2274 def test_const_eq(self
):
2275 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2276 field
= field
['opt_field']
2277 field
.value
= 'hiboux'
2278 self
.assertEqual(self
._def
_const
, field
)
2279 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2281 def test_eq_invalid_type(self
):
2282 self
._def
.value
= 'gerry'
2283 self
.assertNotEqual(self
._def
, 23)
2285 def test_str_op(self
):
2286 self
._def
.value
= 'marcel'
2287 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
2289 def test_repr_op(self
):
2290 self
._def
.value
= 'mireille'
2291 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
2294 class VariantFieldTestCase(unittest
.TestCase
):
2296 def _const_value_setter(field
):
2297 field
.selected_option_index
= 3
2300 def _create_fc(self
, tc
):
2301 ft0
= tc
.create_signed_integer_field_class(32)
2302 ft1
= tc
.create_string_field_class()
2303 ft2
= tc
.create_double_precision_real_field_class()
2304 ft3
= tc
.create_signed_integer_field_class(17)
2305 fc
= tc
.create_variant_field_class()
2306 fc
.append_option('corner', ft0
)
2307 fc
.append_option('zoom', ft1
)
2308 fc
.append_option('mellotron', ft2
)
2309 fc
.append_option('giorgio', ft3
)
2310 top_fc
= tc
.create_structure_field_class()
2311 top_fc
.append_member('variant_field', fc
)
2315 self
._tc
= get_default_trace_class()
2316 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2317 self
._def
= fld
['variant_field']
2319 self
._def
_value
= 1334
2320 self
._def
_selected
_index
= 3
2321 const_fc
= self
._create
_fc
(self
._tc
)['variant_field']
2323 fld_const
= create_const_field(
2324 self
._tc
, const_fc
.field_class
, self
._const
_value
_setter
2326 self
._def
_const
= fld_const
2328 def test_bool_op(self
):
2329 self
._def
.selected_option_index
= 2
2330 self
._def
.value
= -17.34
2331 with self
.assertRaises(NotImplementedError):
2334 def test_selected_option_index(self
):
2335 self
._def
.selected_option_index
= 2
2336 self
.assertEqual(self
._def
.selected_option_index
, 2)
2338 def test_selected_option_index_above_range(self
):
2339 with self
.assertRaises(IndexError):
2340 self
._def
.selected_option_index
= 4
2342 def test_selected_option_index_below_range(self
):
2343 with self
.assertRaises(IndexError):
2344 self
._def
.selected_option_index
= -1
2346 def test_const_selected_option_index(self
):
2347 with self
.assertRaises(AttributeError):
2348 self
._def
_const
.selected_option_index
= 2
2349 self
.assertEqual(self
._def
_const
.selected_option_index
, 3)
2351 def test_selected_option(self
):
2352 self
._def
.selected_option_index
= 2
2353 self
._def
.value
= -17.34
2354 self
.assertEqual(self
._def
.selected_option
, -17.34)
2355 self
.assertEqual(type(self
._def
.selected_option
), bt2
._DoublePrecisionRealField
)
2357 self
._def
.selected_option_index
= 3
2358 self
._def
.value
= 1921
2359 self
.assertEqual(self
._def
.selected_option
, 1921)
2360 self
.assertEqual(type(self
._def
.selected_option
), bt2
._SignedIntegerField
)
2362 def test_const_selected_option(self
):
2363 self
.assertEqual(self
._def
_const
.selected_option
, 1334)
2365 type(self
._def
_const
.selected_option
), bt2
._SignedIntegerFieldConst
2369 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2370 field
= field
['variant_field']
2371 field
.selected_option_index
= 0
2373 self
._def
.selected_option_index
= 0
2374 self
._def
.value
= 1774
2375 self
.assertEqual(self
._def
, field
)
2377 def test_const_eq(self
):
2378 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2379 field
= field
['variant_field']
2380 field
.selected_option_index
= 3
2382 self
.assertEqual(self
._def
_const
, field
)
2385 self
.assertEqual(len(self
._def
), 4)
2387 def test_eq_invalid_type(self
):
2388 self
._def
.selected_option_index
= 1
2389 self
._def
.value
= 'gerry'
2390 self
.assertNotEqual(self
._def
, 23)
2392 def test_str_op_int(self
):
2393 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2394 field
= field
['variant_field']
2395 field
.selected_option_index
= 0
2397 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2398 other_field
= other_field
['variant_field']
2399 other_field
.selected_option_index
= 0
2400 other_field
.value
= 1774
2401 self
.assertEqual(str(field
), str(other_field
))
2403 def test_str_op_str(self
):
2404 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2405 field
= field
['variant_field']
2406 field
.selected_option_index
= 1
2407 field
.value
= 'un beau grand bateau'
2408 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2409 other_field
= other_field
['variant_field']
2410 other_field
.selected_option_index
= 1
2411 other_field
.value
= 'un beau grand bateau'
2412 self
.assertEqual(str(field
), str(other_field
))
2414 def test_str_op_float(self
):
2415 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2416 field
= field
['variant_field']
2417 field
.selected_option_index
= 2
2418 field
.value
= 14.4245
2419 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2420 other_field
= other_field
['variant_field']
2421 other_field
.selected_option_index
= 2
2422 other_field
.value
= 14.4245
2423 self
.assertEqual(str(field
), str(other_field
))
2426 if __name__
== '__main__':