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 @unittest.skip('copy is not implemented')
327 def _test_binop_lhs_value_same(self
, op
, rhs
):
328 value_before
= copy
.copy(self
._def
)
329 r
, rv
= self
._binop
(op
, rhs
)
330 self
.assertEqual(self
._def
, value_before
)
332 # The methods below which take the `test_cb` and/or `op` parameters
333 # are meant to be used with one of the _test_binop_*() functions
334 # above as `test_cb` and a binary operator function as `op`.
338 # self._test_binop_rhs_pos_int(self._test_binop_value,
341 # This tests that a numeric field object added to a positive integer
342 # value gives a result with the expected value.
344 # `vint` and `vfloat` mean a signed integer value object and a real
347 def _test_binop_invalid_unknown(self
, op
):
348 if op
in _COMP_BINOPS
:
349 self
.skipTest('not testing')
354 with self
.assertRaises(TypeError):
357 def _test_binop_invalid_none(self
, op
):
358 if op
in _COMP_BINOPS
:
359 self
.skipTest('not testing')
361 with self
.assertRaises(TypeError):
364 def _test_binop_rhs_false(self
, test_cb
, op
):
367 def _test_binop_rhs_true(self
, test_cb
, op
):
370 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
373 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
376 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
379 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
380 test_cb(op
, bt2
.create_value(2))
382 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
383 test_cb(op
, bt2
.create_value(-23))
385 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
386 test_cb(op
, bt2
.create_value(0))
388 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
391 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
394 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
397 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
398 test_cb(op
, bt2
.create_value(2.2))
400 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
401 test_cb(op
, bt2
.create_value(-23.4))
403 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
404 test_cb(op
, bt2
.create_value(0.0))
406 def _test_binop_rhs_complex(self
, test_cb
, op
):
407 test_cb(op
, -23 + 19j
)
409 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
412 def _test_binop_type_false(self
, op
):
413 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
415 def _test_binop_type_true(self
, op
):
416 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
418 def _test_binop_type_pos_int(self
, op
):
419 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
421 def _test_binop_type_neg_int(self
, op
):
422 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
424 def _test_binop_type_zero_int(self
, op
):
425 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
427 def _test_binop_type_pos_vint(self
, op
):
428 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
430 def _test_binop_type_neg_vint(self
, op
):
431 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
433 def _test_binop_type_zero_vint(self
, op
):
434 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
436 def _test_binop_type_pos_float(self
, op
):
437 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
439 def _test_binop_type_neg_float(self
, op
):
440 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
442 def _test_binop_type_zero_float(self
, op
):
443 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
445 def _test_binop_type_pos_vfloat(self
, op
):
446 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
448 def _test_binop_type_neg_vfloat(self
, op
):
449 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
451 def _test_binop_type_zero_vfloat(self
, op
):
452 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
454 def _test_binop_type_complex(self
, op
):
455 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
457 def _test_binop_type_zero_complex(self
, op
):
458 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
460 def _test_binop_value_false(self
, op
):
461 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
463 def _test_binop_value_true(self
, op
):
464 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
466 def _test_binop_value_pos_int(self
, op
):
467 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
469 def _test_binop_value_neg_int(self
, op
):
470 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
472 def _test_binop_value_zero_int(self
, op
):
473 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
475 def _test_binop_value_pos_vint(self
, op
):
476 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
478 def _test_binop_value_neg_vint(self
, op
):
479 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
481 def _test_binop_value_zero_vint(self
, op
):
482 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
484 def _test_binop_value_pos_float(self
, op
):
485 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
487 def _test_binop_value_neg_float(self
, op
):
488 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
490 def _test_binop_value_zero_float(self
, op
):
491 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
493 def _test_binop_value_pos_vfloat(self
, op
):
494 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
496 def _test_binop_value_neg_vfloat(self
, op
):
497 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
499 def _test_binop_value_zero_vfloat(self
, op
):
500 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
502 def _test_binop_value_complex(self
, op
):
503 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
505 def _test_binop_value_zero_complex(self
, op
):
506 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
508 def _test_binop_lhs_addr_same_false(self
, op
):
509 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
511 def _test_binop_lhs_addr_same_true(self
, op
):
512 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
514 def _test_binop_lhs_addr_same_pos_int(self
, op
):
515 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
517 def _test_binop_lhs_addr_same_neg_int(self
, op
):
518 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
520 def _test_binop_lhs_addr_same_zero_int(self
, op
):
521 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
523 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
524 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
526 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
527 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
529 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
530 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
532 def _test_binop_lhs_addr_same_pos_float(self
, op
):
533 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
535 def _test_binop_lhs_addr_same_neg_float(self
, op
):
536 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
538 def _test_binop_lhs_addr_same_zero_float(self
, op
):
539 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
541 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
542 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
544 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
545 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
547 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
548 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
550 def _test_binop_lhs_addr_same_complex(self
, op
):
551 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
553 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
554 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
556 def _test_binop_lhs_value_same_false(self
, op
):
557 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
559 def _test_binop_lhs_value_same_true(self
, op
):
560 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
562 def _test_binop_lhs_value_same_pos_int(self
, op
):
563 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
565 def _test_binop_lhs_value_same_neg_int(self
, op
):
566 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
568 def _test_binop_lhs_value_same_zero_int(self
, op
):
569 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
571 def _test_binop_lhs_value_same_pos_vint(self
, op
):
572 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
574 def _test_binop_lhs_value_same_neg_vint(self
, op
):
575 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
577 def _test_binop_lhs_value_same_zero_vint(self
, op
):
578 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
580 def _test_binop_lhs_value_same_pos_float(self
, op
):
581 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
583 def _test_binop_lhs_value_same_neg_float(self
, op
):
584 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
586 def _test_binop_lhs_value_same_zero_float(self
, op
):
587 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
589 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
590 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
592 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
593 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
595 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
596 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
598 def _test_binop_lhs_value_same_complex(self
, op
):
599 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
601 def _test_binop_lhs_value_same_zero_complex(self
, op
):
602 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
604 def test_bool_op(self
):
605 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
607 def test_int_op(self
):
608 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
610 def test_float_op(self
):
611 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
613 def test_complex_op(self
):
614 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
616 def test_str_op(self
):
617 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
619 def test_hash_op(self
):
620 with self
.assertRaises(TypeError):
623 def test_const_hash_op(self
):
624 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
626 def test_const_hash_dict(self
):
628 my_dict
[self
._def
_const
] = 'my_value'
629 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
631 def test_eq_none(self
):
632 # Ignore this lint error:
633 # E711 comparison to None should be 'if cond is None:'
634 # since this is what we want to test (even though not good practice).
635 self
.assertFalse(self
._def
== None) # noqa: E711
637 def test_ne_none(self
):
638 # Ignore this lint error:
639 # E711 comparison to None should be 'if cond is not None:'
640 # since this is what we want to test (even though not good practice).
641 self
.assertTrue(self
._def
!= None) # noqa: E711
644 # This is a list of binary operators used for
645 # _inject_numeric_testing_methods().
647 # Each entry is a pair of binary operator name (used as part of the
648 # created testing method's name) and operator function.
656 ('add', operator
.add
),
657 ('radd', lambda a
, b
: operator
.add(b
, a
)),
658 ('and', operator
.and_
),
659 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
660 ('floordiv', operator
.floordiv
),
661 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
662 ('lshift', operator
.lshift
),
663 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
664 ('mod', operator
.mod
),
665 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
666 ('mul', operator
.mul
),
667 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
668 ('or', operator
.or_
),
669 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
670 ('pow', operator
.pow),
671 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
672 ('rshift', operator
.rshift
),
673 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
674 ('sub', operator
.sub
),
675 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
676 ('truediv', operator
.truediv
),
677 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
678 ('xor', operator
.xor
),
679 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
683 # This is a list of unary operators used for
684 # _inject_numeric_testing_methods().
686 # Each entry is a pair of unary operator name (used as part of the
687 # created testing method's name) and operator function.
689 ('neg', operator
.neg
),
690 ('pos', operator
.pos
),
691 ('abs', operator
.abs),
692 ('invert', operator
.invert
),
694 ('round_0', partial(round, ndigits
=0)),
695 ('round_1', partial(round, ndigits
=1)),
696 ('round_2', partial(round, ndigits
=2)),
697 ('round_3', partial(round, ndigits
=3)),
699 ('floor', math
.floor
),
700 ('trunc', math
.trunc
),
704 # This function injects a bunch of testing methods to a numeric
707 # It is meant to be used like this:
709 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
711 # This function injects:
713 # * One testing method for each _TestNumericField._test_binop_*()
714 # method, for each binary operator in the _BINOPS tuple.
716 # * One testing method for each _TestNumericField._test_unaryop*()
717 # method, for each unary operator in the _UNARYOPS tuple.
718 def _inject_numeric_testing_methods(cls
):
719 def test_binop_name(suffix
):
720 return 'test_binop_{}_{}'.format(name
, suffix
)
722 def test_unaryop_name(suffix
):
723 return 'test_unaryop_{}_{}'.format(name
, suffix
)
725 # inject testing methods for each binary operation
726 for name
, binop
in _BINOPS
:
729 test_binop_name('invalid_unknown'),
730 partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
),
734 test_binop_name('invalid_none'),
735 partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
),
739 test_binop_name('type_true'),
740 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
744 test_binop_name('type_pos_int'),
745 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
749 test_binop_name('type_pos_vint'),
750 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
754 test_binop_name('value_true'),
755 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
759 test_binop_name('value_pos_int'),
760 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
764 test_binop_name('value_pos_vint'),
765 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
769 test_binop_name('lhs_addr_same_true'),
770 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
774 test_binop_name('lhs_addr_same_pos_int'),
776 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
781 test_binop_name('lhs_addr_same_pos_vint'),
783 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
788 test_binop_name('lhs_value_same_true'),
789 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
793 test_binop_name('lhs_value_same_pos_int'),
795 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
800 test_binop_name('lhs_value_same_pos_vint'),
802 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
807 test_binop_name('type_neg_int'),
808 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
812 test_binop_name('type_neg_vint'),
813 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
817 test_binop_name('value_neg_int'),
818 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
822 test_binop_name('value_neg_vint'),
823 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
827 test_binop_name('lhs_addr_same_neg_int'),
829 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
834 test_binop_name('lhs_addr_same_neg_vint'),
836 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
841 test_binop_name('lhs_value_same_neg_int'),
843 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
848 test_binop_name('lhs_value_same_neg_vint'),
850 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
855 test_binop_name('type_false'),
856 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
860 test_binop_name('type_zero_int'),
861 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
865 test_binop_name('type_zero_vint'),
866 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
870 test_binop_name('value_false'),
871 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
875 test_binop_name('value_zero_int'),
876 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
880 test_binop_name('value_zero_vint'),
881 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
885 test_binop_name('lhs_addr_same_false'),
886 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
890 test_binop_name('lhs_addr_same_zero_int'),
892 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
897 test_binop_name('lhs_addr_same_zero_vint'),
899 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
904 test_binop_name('lhs_value_same_false'),
905 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
909 test_binop_name('lhs_value_same_zero_int'),
911 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
916 test_binop_name('lhs_value_same_zero_vint'),
918 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
923 test_binop_name('type_pos_float'),
924 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
928 test_binop_name('type_neg_float'),
929 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
933 test_binop_name('type_pos_vfloat'),
934 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
938 test_binop_name('type_neg_vfloat'),
939 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
943 test_binop_name('value_pos_float'),
944 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
948 test_binop_name('value_neg_float'),
949 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
953 test_binop_name('value_pos_vfloat'),
954 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
958 test_binop_name('value_neg_vfloat'),
959 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
963 test_binop_name('lhs_addr_same_pos_float'),
965 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
970 test_binop_name('lhs_addr_same_neg_float'),
972 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
977 test_binop_name('lhs_addr_same_pos_vfloat'),
979 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
984 test_binop_name('lhs_addr_same_neg_vfloat'),
986 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
991 test_binop_name('lhs_value_same_pos_float'),
993 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
998 test_binop_name('lhs_value_same_neg_float'),
1000 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
1005 test_binop_name('lhs_value_same_pos_vfloat'),
1007 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
1012 test_binop_name('lhs_value_same_neg_vfloat'),
1014 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
1019 test_binop_name('type_zero_float'),
1020 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
1024 test_binop_name('type_zero_vfloat'),
1025 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
1029 test_binop_name('value_zero_float'),
1030 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
1034 test_binop_name('value_zero_vfloat'),
1035 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
1039 test_binop_name('lhs_addr_same_zero_float'),
1041 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
1046 test_binop_name('lhs_addr_same_zero_vfloat'),
1048 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
1053 test_binop_name('lhs_value_same_zero_float'),
1055 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1060 test_binop_name('lhs_value_same_zero_vfloat'),
1062 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1067 test_binop_name('type_complex'),
1068 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1072 test_binop_name('type_zero_complex'),
1073 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1077 test_binop_name('value_complex'),
1078 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1082 test_binop_name('value_zero_complex'),
1083 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1087 test_binop_name('lhs_addr_same_complex'),
1089 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1094 test_binop_name('lhs_addr_same_zero_complex'),
1096 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1101 test_binop_name('lhs_value_same_complex'),
1103 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1108 test_binop_name('lhs_value_same_zero_complex'),
1110 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1114 # inject testing methods for each unary operation
1115 for name
, unaryop
in _UNARYOPS
:
1118 test_unaryop_name('type'),
1119 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1123 test_unaryop_name('value'),
1124 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1128 test_unaryop_name('addr_same'),
1129 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1133 test_unaryop_name('value_same'),
1134 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1138 class BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1140 def _const_value_setter(field
):
1143 def _create_fc(self
, tc
):
1144 return tc
.create_bool_field_class()
1147 self
._tc
= get_default_trace_class()
1148 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1149 self
._def
.value
= True
1150 self
._def
_value
= True
1151 self
._def
_const
= create_const_field(
1152 self
._tc
, self
._tc
.create_bool_field_class(), self
._const
_value
_setter
1154 self
._def
_new
_value
= False
1156 def test_classes(self
):
1157 self
.assertIs(type(self
._def
), bt2
._BoolField
)
1158 self
.assertIs(type(self
._def
_const
), bt2
._BoolFieldConst
)
1160 def test_assign_true(self
):
1162 self
._def
.value
= raw
1163 self
.assertEqual(self
._def
, raw
)
1165 def test_assign_false(self
):
1167 self
._def
.value
= raw
1168 self
.assertEqual(self
._def
, raw
)
1170 def test_assign_field_true(self
):
1171 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1174 self
._def
.value
= field
1175 self
.assertEqual(self
._def
, raw
)
1177 def test_assign_field_false(self
):
1178 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1181 self
._def
.value
= field
1182 self
.assertEqual(self
._def
, raw
)
1184 def test_assign_invalid_type(self
):
1185 with self
.assertRaises(TypeError):
1186 self
._def
.value
= 17
1188 def test_str_op(self
):
1189 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1192 _inject_numeric_testing_methods(BoolFieldTestCase
)
1195 class _TestIntegerFieldCommon(_TestNumericField
):
1196 def test_assign_true(self
):
1198 self
._def
.value
= raw
1199 self
.assertEqual(self
._def
, raw
)
1201 def test_assign_false(self
):
1203 self
._def
.value
= raw
1204 self
.assertEqual(self
._def
, raw
)
1206 def test_assign_pos_int(self
):
1208 self
._def
.value
= raw
1209 self
.assertEqual(self
._def
, raw
)
1211 def test_assign_neg_int(self
):
1213 self
._def
.value
= raw
1214 self
.assertEqual(self
._def
, raw
)
1216 def test_assign_int_field(self
):
1218 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1220 self
._def
.value
= field
1221 self
.assertEqual(self
._def
, raw
)
1223 def test_assign_invalid_type(self
):
1224 with self
.assertRaises(TypeError):
1225 self
._def
.value
= 'yes'
1227 def test_assign_uint(self
):
1228 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1229 field
= _create_field(self
._tc
, uint_fc
)
1232 self
.assertEqual(field
, raw
)
1234 def test_assign_big_uint(self
):
1235 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1236 field
= _create_field(self
._tc
, uint_fc
)
1237 # Larger than the IEEE 754 double-precision exact representation of
1240 field
.value
= (2 ** 53) + 1
1241 self
.assertEqual(field
, raw
)
1243 def test_assign_uint_out_of_range(self
):
1244 uint_fc
= self
._tc
.create_unsigned_integer_field_class(8)
1245 field
= _create_field(self
._tc
, uint_fc
)
1247 with self
.assertRaises(ValueError) as ctx
:
1250 str(ctx
.exception
), 'Value 256 is outside valid range [0, 255]'
1253 with self
.assertRaises(ValueError) as ctx
:
1255 self
.assertEqual(str(ctx
.exception
), 'Value -1 is outside valid range [0, 255]')
1257 def test_assign_int_out_of_range(self
):
1258 int_fc
= self
._tc
.create_signed_integer_field_class(8)
1259 field
= _create_field(self
._tc
, int_fc
)
1261 with self
.assertRaises(ValueError) as ctx
:
1264 str(ctx
.exception
), 'Value 128 is outside valid range [-128, 127]'
1267 with self
.assertRaises(ValueError) as ctx
:
1270 str(ctx
.exception
), 'Value -129 is outside valid range [-128, 127]'
1273 def test_str_op(self
):
1274 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1277 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1280 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1282 def _const_value_setter(field
):
1285 def _create_fc(self
, tc
):
1286 return tc
.create_signed_integer_field_class(25)
1289 self
._tc
= get_default_trace_class()
1290 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1291 self
._field
.value
= 17
1292 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1293 self
._def
.value
= 17
1294 self
._def
_value
= 17
1295 self
._def
_const
= create_const_field(
1296 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1298 self
._def
_new
_value
= -101
1301 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1303 def _const_value_setter(field
):
1306 def _create_fc(self
, tc
):
1307 fc
= tc
.create_signed_enumeration_field_class(32)
1308 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1309 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1310 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1312 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1314 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1318 self
._tc
= get_default_trace_class()
1319 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1320 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1321 self
._def
.value
= 17
1322 self
._def
_value
= 17
1323 self
._def
_const
= create_const_field(
1324 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1326 self
._def
_new
_value
= -101
1328 def test_str_op(self
):
1329 expected_string_found
= False
1332 # Establish all permutations of the three expected matches since
1333 # the order in which mappings are enumerated is not explicitly part of
1335 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1336 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1338 expected_string_found
= True
1341 self
.assertTrue(expected_string_found
)
1343 def test_labels(self
):
1344 self
._field
.value
= 17
1345 labels
= sorted(self
._field
.labels
)
1346 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1349 class SingleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1351 def _const_value_setter(field
):
1354 def _create_fc(self
, tc
):
1355 return tc
.create_single_precision_real_field_class()
1358 self
._tc
= get_default_trace_class()
1359 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1360 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1361 self
._def
_const
= create_const_field(
1363 self
._tc
.create_single_precision_real_field_class(),
1364 self
._const
_value
_setter
,
1366 self
._def
.value
= 52.0
1367 self
._def
_value
= 52.0
1368 self
._def
_new
_value
= -17.0
1370 def _test_invalid_op(self
, cb
):
1371 with self
.assertRaises(TypeError):
1374 def test_assign_true(self
):
1375 self
._def
.value
= True
1376 self
.assertTrue(self
._def
)
1378 def test_assign_false(self
):
1379 self
._def
.value
= False
1380 self
.assertFalse(self
._def
)
1382 def test_assign_pos_int(self
):
1384 self
._def
.value
= raw
1385 self
.assertEqual(self
._def
, float(raw
))
1387 def test_assign_neg_int(self
):
1389 self
._def
.value
= raw
1390 self
.assertEqual(self
._def
, float(raw
))
1392 def test_assign_int_field(self
):
1393 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1394 int_field
= _create_field(self
._tc
, int_fc
)
1396 int_field
.value
= raw
1397 self
._def
.value
= int_field
1398 self
.assertEqual(self
._def
, float(raw
))
1400 def test_assign_float(self
):
1402 self
._def
.value
= raw
1403 # It's expected to have some lost of precision because of the field
1404 # that is in single precision.
1405 self
.assertEqual(round(self
._def
, 5), raw
)
1407 def test_assign_float_field(self
):
1408 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1411 self
._def
.value
= field
1412 # It's expected to have some lost of precision because of the field
1413 # that is in single precision.
1414 self
.assertEqual(round(self
._def
, 5), raw
)
1416 def test_assign_invalid_type(self
):
1417 with self
.assertRaises(TypeError):
1418 self
._def
.value
= 'yes'
1420 def test_invalid_lshift(self
):
1421 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1423 def test_invalid_rshift(self
):
1424 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1426 def test_invalid_and(self
):
1427 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1429 def test_invalid_or(self
):
1430 self
._test
_invalid
_op
(lambda: self
._def |
23)
1432 def test_invalid_xor(self
):
1433 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1435 def test_invalid_invert(self
):
1436 self
._test
_invalid
_op
(lambda: ~self
._def
)
1438 def test_str_op(self
):
1439 self
.assertEqual(str(round(self
._def
, 5)), str(self
._def
_value
))
1442 _inject_numeric_testing_methods(SingleRealFieldTestCase
)
1445 class DoubleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1447 def _const_value_setter(field
):
1450 def _create_fc(self
, tc
):
1451 return tc
.create_double_precision_real_field_class()
1454 self
._tc
= get_default_trace_class()
1455 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1456 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1457 self
._def
_const
= create_const_field(
1459 self
._tc
.create_double_precision_real_field_class(),
1460 self
._const
_value
_setter
,
1462 self
._def
.value
= 52.7
1463 self
._def
_value
= 52.7
1464 self
._def
_new
_value
= -17.164857
1466 def _test_invalid_op(self
, cb
):
1467 with self
.assertRaises(TypeError):
1470 def test_assign_true(self
):
1471 self
._def
.value
= True
1472 self
.assertTrue(self
._def
)
1474 def test_assign_false(self
):
1475 self
._def
.value
= False
1476 self
.assertFalse(self
._def
)
1478 def test_assign_pos_int(self
):
1480 self
._def
.value
= raw
1481 self
.assertEqual(self
._def
, float(raw
))
1483 def test_assign_neg_int(self
):
1485 self
._def
.value
= raw
1486 self
.assertEqual(self
._def
, float(raw
))
1488 def test_assign_int_field(self
):
1489 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1490 int_field
= _create_field(self
._tc
, int_fc
)
1492 int_field
.value
= raw
1493 self
._def
.value
= int_field
1494 self
.assertEqual(self
._def
, float(raw
))
1496 def test_assign_float(self
):
1498 self
._def
.value
= raw
1499 self
.assertEqual(self
._def
, raw
)
1501 def test_assign_float_field(self
):
1502 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1505 self
._def
.value
= field
1506 self
.assertEqual(self
._def
, raw
)
1508 def test_assign_invalid_type(self
):
1509 with self
.assertRaises(TypeError):
1510 self
._def
.value
= 'yes'
1512 def test_invalid_lshift(self
):
1513 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1515 def test_invalid_rshift(self
):
1516 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1518 def test_invalid_and(self
):
1519 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1521 def test_invalid_or(self
):
1522 self
._test
_invalid
_op
(lambda: self
._def |
23)
1524 def test_invalid_xor(self
):
1525 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1527 def test_invalid_invert(self
):
1528 self
._test
_invalid
_op
(lambda: ~self
._def
)
1530 def test_str_op(self
):
1531 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1534 _inject_numeric_testing_methods(DoubleRealFieldTestCase
)
1537 class StringFieldTestCase(unittest
.TestCase
):
1539 def _const_value_setter(field
):
1540 field
.value
= 'Hello, World!'
1543 self
._tc
= get_default_trace_class()
1544 self
._def
_value
= 'Hello, World!'
1545 self
._def
= _create_string_field(self
._tc
)
1546 self
._def
_const
= create_const_field(
1547 self
._tc
, self
._tc
.create_string_field_class(), self
._const
_value
_setter
1549 self
._def
.value
= self
._def
_value
1550 self
._def
_new
_value
= 'Yes!'
1552 def test_assign_int(self
):
1553 with self
.assertRaises(TypeError):
1554 self
._def
.value
= 283
1556 def test_assign_string_field(self
):
1557 field
= _create_string_field(self
._tc
)
1560 self
.assertEqual(field
, raw
)
1563 self
.assertEqual(self
._def
, self
._def
_value
)
1565 def test_const_eq(self
):
1566 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1568 def test_not_eq(self
):
1569 self
.assertNotEqual(self
._def
, 23)
1571 def test_lt_vstring(self
):
1572 s1
= _create_string_field(self
._tc
)
1574 s2
= _create_string_field(self
._tc
)
1576 self
.assertLess(s1
, s2
)
1578 def test_lt_string(self
):
1579 s1
= _create_string_field(self
._tc
)
1581 self
.assertLess(s1
, 'bateau')
1583 def test_le_vstring(self
):
1584 s1
= _create_string_field(self
._tc
)
1586 s2
= _create_string_field(self
._tc
)
1588 self
.assertLessEqual(s1
, s2
)
1590 def test_le_string(self
):
1591 s1
= _create_string_field(self
._tc
)
1593 self
.assertLessEqual(s1
, 'bateau')
1595 def test_gt_vstring(self
):
1596 s1
= _create_string_field(self
._tc
)
1598 s2
= _create_string_field(self
._tc
)
1600 self
.assertGreater(s2
, s1
)
1602 def test_gt_string(self
):
1603 s1
= _create_string_field(self
._tc
)
1605 self
.assertGreater('bateau', s1
)
1607 def test_ge_vstring(self
):
1608 s1
= _create_string_field(self
._tc
)
1610 s2
= _create_string_field(self
._tc
)
1612 self
.assertGreaterEqual(s2
, s1
)
1614 def test_ge_string(self
):
1615 s1
= _create_string_field(self
._tc
)
1617 self
.assertGreaterEqual('bateau', s1
)
1619 def test_bool_op(self
):
1620 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1622 def test_str_op(self
):
1623 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1626 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1628 def test_getitem(self
):
1629 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1631 def test_const_getitem(self
):
1632 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1634 def test_append_str(self
):
1635 to_append
= 'meow meow meow'
1636 self
._def
+= to_append
1637 self
._def
_value
+= to_append
1638 self
.assertEqual(self
._def
, self
._def
_value
)
1640 def test_const_append_str(self
):
1641 to_append
= 'meow meow meow'
1642 with self
.assertRaises(TypeError):
1643 self
._def
_const
+= to_append
1644 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1646 def test_append_string_field(self
):
1647 field
= _create_string_field(self
._tc
)
1648 to_append
= 'meow meow meow'
1649 field
.value
= to_append
1651 self
._def
_value
+= to_append
1652 self
.assertEqual(self
._def
, self
._def
_value
)
1654 def test_hash_op(self
):
1655 with self
.assertRaises(TypeError):
1658 def test_const_hash_op(self
):
1659 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
1661 def test_const_hash_dict(self
):
1663 my_dict
[self
._def
_const
] = 'my_value'
1664 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
1667 class _TestArrayFieldCommon
:
1668 def _modify_def(self
):
1671 def test_bool_op_true(self
):
1672 self
.assertTrue(self
._def
)
1675 self
.assertEqual(len(self
._def
), 3)
1677 def test_length(self
):
1678 self
.assertEqual(self
._def
.length
, 3)
1680 def test_getitem(self
):
1681 field
= self
._def
[1]
1682 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1683 self
.assertEqual(field
, 1847)
1685 def test_const_getitem(self
):
1686 field
= self
._def
_const
[1]
1687 self
.assertIs(type(field
), bt2
._SignedIntegerFieldConst
)
1688 self
.assertEqual(field
, 1847)
1691 field
= _create_int_array_field(self
._tc
, 3)
1695 self
.assertEqual(self
._def
, field
)
1697 def test_eq_invalid_type(self
):
1698 self
.assertNotEqual(self
._def
, 23)
1700 def test_eq_diff_len(self
):
1701 field
= _create_int_array_field(self
._tc
, 2)
1704 self
.assertNotEqual(self
._def
, field
)
1706 def test_eq_diff_content_same_len(self
):
1707 field
= _create_int_array_field(self
._tc
, 3)
1711 self
.assertNotEqual(self
._def
, field
)
1713 def test_eq_non_sequence_iterable(self
):
1714 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1715 field
= _create_int_array_field(self
._tc
, 3)
1719 self
.assertEqual(field
, list(dct
.keys()))
1720 self
.assertNotEqual(field
, dct
)
1722 def test_setitem(self
):
1724 self
.assertEqual(self
._def
[2], 24)
1726 def test_setitem_int_field(self
):
1727 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1728 int_field
= _create_field(self
._tc
, int_fc
)
1729 int_field
.value
= 19487
1730 self
._def
[1] = int_field
1731 self
.assertEqual(self
._def
[1], 19487)
1733 def test_setitem_non_basic_field(self
):
1734 array_field
= _create_struct_array_field(self
._tc
, 2)
1735 with self
.assertRaises(TypeError):
1738 def test_setitem_none(self
):
1739 with self
.assertRaises(TypeError):
1742 def test_setitem_index_wrong_type(self
):
1743 with self
.assertRaises(TypeError):
1744 self
._def
['yes'] = 23
1746 def test_setitem_index_neg(self
):
1747 with self
.assertRaises(IndexError):
1750 def test_setitem_index_out_of_range(self
):
1751 with self
.assertRaises(IndexError):
1752 self
._def
[len(self
._def
)] = 134679
1754 def test_const_setitem(self
):
1755 with self
.assertRaises(TypeError):
1756 self
._def
_const
[0] = 134679
1758 def test_iter(self
):
1759 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1760 self
.assertEqual(field
, value
)
1762 def test_const_iter(self
):
1763 for field
, value
in zip(self
._def
_const
, (45, 1847, 1948754)):
1764 self
.assertEqual(field
, value
)
1766 def test_value_int_field(self
):
1767 values
= [45646, 145, 12145]
1768 self
._def
.value
= values
1769 self
.assertEqual(values
, self
._def
)
1771 def test_value_check_sequence(self
):
1773 with self
.assertRaises(TypeError):
1774 self
._def
.value
= values
1776 def test_value_wrong_type_in_sequence(self
):
1777 values
= [32, 'hello', 11]
1778 with self
.assertRaises(TypeError):
1779 self
._def
.value
= values
1781 def test_value_complex_type(self
):
1782 struct_fc
= self
._tc
.create_structure_field_class()
1783 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1784 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1785 str_fc
= self
._tc
.create_string_field_class()
1786 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1787 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1788 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1789 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1790 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1792 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1793 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1794 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1797 array
= stream
.create_packet().context_field
['array_field']
1798 array
.value
= values
1799 self
.assertEqual(values
, array
)
1800 values
[0]['an_int'] = 'a string'
1801 with self
.assertRaises(TypeError):
1802 array
.value
= values
1804 def test_str_op(self
):
1806 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1807 self
.assertEqual(expected_string
, s
)
1810 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1812 def _const_value_setter(field
):
1813 field
.value
= [45, 1847, 1948754]
1816 self
._tc
= get_default_trace_class()
1817 self
._def
= _create_int_array_field(self
._tc
, 3)
1820 self
._def
[2] = 1948754
1821 self
._def
_value
= [45, 1847, 1948754]
1822 self
._def
_const
= create_const_field(
1824 self
._tc
.create_static_array_field_class(
1825 self
._tc
.create_signed_integer_field_class(32), 3
1827 self
._const
_value
_setter
,
1830 def test_value_wrong_len(self
):
1832 with self
.assertRaises(ValueError):
1833 self
._def
.value
= values
1836 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1838 def _const_value_setter(field
):
1839 field
.value
= [45, 1847, 1948754]
1842 self
._tc
= get_default_trace_class()
1843 self
._def
= _create_dynamic_array(self
._tc
)
1846 self
._def
[2] = 1948754
1847 self
._def
_value
= [45, 1847, 1948754]
1848 self
._def
_const
= create_const_field(
1850 self
._tc
.create_dynamic_array_field_class(
1851 self
._tc
.create_signed_integer_field_class(32)
1853 self
._const
_value
_setter
,
1856 def test_value_resize(self
):
1857 new_values
= [1, 2, 3, 4]
1858 self
._def
.value
= new_values
1859 self
.assertCountEqual(self
._def
, new_values
)
1861 def test_set_length(self
):
1862 self
._def
.length
= 4
1864 self
.assertEqual(len(self
._def
), 4)
1866 def test_const_set_length(self
):
1867 with self
.assertRaises(AttributeError):
1868 self
._def
_const
.length
= 4
1869 self
.assertEqual(len(self
._def
), 3)
1871 def test_set_invalid_length(self
):
1872 with self
.assertRaises(TypeError):
1873 self
._def
.length
= 'cheval'
1876 class StructureFieldTestCase(unittest
.TestCase
):
1878 def _const_value_setter(field
):
1888 def _create_fc(self
, tc
):
1889 fc
= tc
.create_structure_field_class()
1890 fc
.append_member('A', self
._fc
0_fn
())
1891 fc
.append_member('B', self
._fc
1_fn
())
1892 fc
.append_member('C', self
._fc
2_fn
())
1893 fc
.append_member('D', self
._fc
3_fn
())
1894 fc
.append_member('E', self
._fc
4_fn
())
1895 fc5
= self
._fc
5_fn
()
1896 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1897 fc
.append_member('F', fc5
)
1901 self
._tc
= get_default_trace_class()
1902 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1903 self
._fc
1_fn
= self
._tc
.create_string_field_class
1904 self
._fc
2_fn
= self
._tc
.create_double_precision_real_field_class
1905 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1906 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1907 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1908 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1910 self
._fc
= self
._create
_fc
(self
._tc
)
1911 self
._def
= _create_field(self
._tc
, self
._fc
)
1912 self
._def
['A'] = -1872
1913 self
._def
['B'] = 'salut'
1914 self
._def
['C'] = 17.5
1915 self
._def
['D'] = 16497
1917 self
._def
['F'] = {'F_1': 52}
1927 self
._def
_const
= create_const_field(
1928 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1931 def _modify_def(self
):
1932 self
._def
['B'] = 'hola'
1934 def test_bool_op_true(self
):
1935 self
.assertTrue(self
._def
)
1937 def test_bool_op_false(self
):
1938 field
= self
._def
['E']
1939 self
.assertFalse(field
)
1942 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1944 def test_getitem(self
):
1945 field1
= self
._def
['A']
1946 field2
= self
._def
['B']
1947 field3
= self
._def
['C']
1948 field4
= self
._def
['D']
1949 field5
= self
._def
['E']
1950 field6
= self
._def
['F']
1952 self
.assertIs(type(field1
), bt2
._SignedIntegerField
)
1953 self
.assertEqual(field1
, -1872)
1955 self
.assertIs(type(field2
), bt2
._StringField
)
1956 self
.assertEqual(field2
, 'salut')
1958 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealField
)
1959 self
.assertEqual(field3
, 17.5)
1961 self
.assertIs(type(field4
), bt2
._SignedIntegerField
)
1962 self
.assertEqual(field4
, 16497)
1964 self
.assertIs(type(field5
), bt2
._StructureField
)
1965 self
.assertEqual(field5
, {})
1967 self
.assertIs(type(field6
), bt2
._StructureField
)
1968 self
.assertEqual(field6
, {'F_1': 52})
1970 def test_const_getitem(self
):
1971 field1
= self
._def
_const
['A']
1972 field2
= self
._def
_const
['B']
1973 field3
= self
._def
_const
['C']
1974 field4
= self
._def
_const
['D']
1975 field5
= self
._def
_const
['E']
1976 field6
= self
._def
_const
['F']
1978 self
.assertIs(type(field1
), bt2
._SignedIntegerFieldConst
)
1979 self
.assertEqual(field1
, -1872)
1981 self
.assertIs(type(field2
), bt2
._StringFieldConst
)
1982 self
.assertEqual(field2
, 'salut')
1984 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealFieldConst
)
1985 self
.assertEqual(field3
, 17.5)
1987 self
.assertIs(type(field4
), bt2
._SignedIntegerFieldConst
)
1988 self
.assertEqual(field4
, 16497)
1990 self
.assertIs(type(field5
), bt2
._StructureFieldConst
)
1991 self
.assertEqual(field5
, {})
1993 self
.assertIs(type(field6
), bt2
._StructureFieldConst
)
1994 self
.assertEqual(field6
, {'F_1': 52})
1996 def test_member_at_index_out_of_bounds_after(self
):
1997 with self
.assertRaises(IndexError):
1998 self
._def
.member_at_index(len(self
._def
_value
))
2001 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2003 field
['B'] = 'salut'
2007 field
['F'] = {'F_1': 52}
2008 self
.assertEqual(self
._def
, field
)
2010 def test_const_eq(self
):
2011 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2013 field
['B'] = 'salut'
2017 field
['F'] = {'F_1': 52}
2018 self
.assertEqual(self
._def
_const
, field
)
2020 def test_eq_invalid_type(self
):
2021 self
.assertNotEqual(self
._def
, 23)
2023 def test_eq_diff_len(self
):
2024 fc
= self
._tc
.create_structure_field_class()
2025 fc
.append_member('A', self
._fc
0_fn
())
2026 fc
.append_member('B', self
._fc
1_fn
())
2027 fc
.append_member('C', self
._fc
2_fn
())
2029 field
= _create_field(self
._tc
, fc
)
2031 field
['B'] = 'salut'
2033 self
.assertNotEqual(self
._def
, field
)
2035 def test_eq_diff_keys(self
):
2036 fc
= self
._tc
.create_structure_field_class()
2037 fc
.append_member('U', self
._fc
0_fn
())
2038 fc
.append_member('V', self
._fc
1_fn
())
2039 fc
.append_member('W', self
._fc
2_fn
())
2040 fc
.append_member('X', self
._fc
3_fn
())
2041 fc
.append_member('Y', self
._fc
4_fn
())
2042 fc
.append_member('Z', self
._fc
5_fn
())
2043 field
= _create_field(self
._tc
, fc
)
2045 field
['V'] = "gerry"
2050 self
.assertNotEqual(self
._def
, field
)
2052 def test_eq_diff_content_same_len(self
):
2053 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2055 field
['B'] = 'salut'
2059 field
['F'] = {'F_1': 0}
2060 self
.assertNotEqual(self
._def
, field
)
2062 def test_eq_same_content_diff_keys(self
):
2063 fc
= self
._tc
.create_structure_field_class()
2064 fc
.append_member('A', self
._fc
0_fn
())
2065 fc
.append_member('B', self
._fc
1_fn
())
2066 fc
.append_member('E', self
._fc
2_fn
())
2067 fc
.append_member('D', self
._fc
3_fn
())
2068 fc
.append_member('C', self
._fc
4_fn
())
2069 fc
.append_member('F', self
._fc
5_fn
())
2070 field
= _create_field(self
._tc
, fc
)
2072 field
['B'] = 'salut'
2077 self
.assertNotEqual(self
._def
, field
)
2079 def test_setitem(self
):
2080 self
._def
['C'] = -18.47
2081 self
.assertEqual(self
._def
['C'], -18.47)
2083 def test_const_setitem(self
):
2084 with self
.assertRaises(TypeError):
2085 self
._def
_const
['A'] = 134679
2087 def test_setitem_int_field(self
):
2088 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2089 int_field
= _create_field(self
._tc
, int_fc
)
2090 int_field
.value
= 19487
2091 self
._def
['D'] = int_field
2092 self
.assertEqual(self
._def
['D'], 19487)
2094 def test_setitem_non_basic_field(self
):
2095 elem_fc
= self
._tc
.create_structure_field_class()
2096 struct_fc
= self
._tc
.create_structure_field_class()
2097 struct_fc
.append_member('A', elem_fc
)
2098 struct_field
= _create_field(self
._tc
, struct_fc
)
2100 # Will fail on access to .items() of the value
2101 with self
.assertRaises(AttributeError):
2102 struct_field
['A'] = 23
2104 def test_setitem_none(self
):
2105 with self
.assertRaises(TypeError):
2106 self
._def
['C'] = None
2108 def test_setitem_key_wrong_type(self
):
2109 with self
.assertRaises(TypeError):
2112 def test_setitem_wrong_key(self
):
2113 with self
.assertRaises(KeyError):
2114 self
._def
['hi'] = 134679
2116 def test_member_at_index(self
):
2117 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
2119 def test_const_member_at_index(self
):
2120 self
.assertEqual(self
._def
_const
.member_at_index(1), 'salut')
2122 def test_iter(self
):
2132 for vkey
, vval
in self
._def
.items():
2133 val
= orig_values
[vkey
]
2134 self
.assertEqual(vval
, val
)
2136 def test_value(self
):
2145 self
.assertEqual(self
._def
, orig_values
)
2147 def test_set_value(self
):
2148 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2149 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
2150 str_fc
= self
._tc
.create_string_field_class()
2151 struct_fc
= self
._tc
.create_structure_field_class()
2152 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
2153 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
2154 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
2155 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2157 struct
= _create_field(self
._tc
, struct_fc
)
2158 struct
.value
= values
2159 self
.assertEqual(values
, struct
)
2161 bad_type_values
= copy
.deepcopy(values
)
2162 bad_type_values
['an_int'] = 'a string'
2163 with self
.assertRaises(TypeError):
2164 struct
.value
= bad_type_values
2166 unknown_key_values
= copy
.deepcopy(values
)
2167 unknown_key_values
['unknown_key'] = 16546
2168 with self
.assertRaises(KeyError):
2169 struct
.value
= unknown_key_values
2171 def test_str_op(self
):
2172 expected_string_found
= False
2174 # Establish all permutations of the three expected matches since
2175 # the order in which mappings are enumerated is not explicitly part of
2177 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
2178 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
2179 candidate
= '{{{}}}'.format(', '.join(items
))
2181 expected_string_found
= True
2184 self
.assertTrue(expected_string_found
)
2187 class OptionFieldTestCase(unittest
.TestCase
):
2189 def _const_value_setter(field
):
2190 field
.value
= {'opt_field': 'hiboux'}
2192 def _create_fc(self
, tc
):
2193 fc
= tc
.create_option_without_selector_field_class(
2194 tc
.create_string_field_class()
2196 top_fc
= tc
.create_structure_field_class()
2197 top_fc
.append_member('opt_field', fc
)
2201 self
._tc
= get_default_trace_class()
2202 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2203 self
._def
= fld
['opt_field']
2204 self
._def
_value
= 'hiboux'
2205 self
._def
_const
= create_const_field(
2206 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
2209 def test_value_prop(self
):
2210 self
._def
.value
= 'hiboux'
2211 self
.assertEqual(self
._def
.field
, 'hiboux')
2212 self
.assertIs(type(self
._def
), bt2
._OptionField
)
2213 self
.assertIs(type(self
._def
.field
), bt2
._StringField
)
2214 self
.assertTrue(self
._def
.has_field
)
2216 def test_const_value_prop(self
):
2217 self
.assertEqual(self
._def
_const
.field
, 'hiboux')
2218 self
.assertIs(type(self
._def
_const
), bt2
._OptionFieldConst
)
2219 self
.assertIs(type(self
._def
_const
.field
), bt2
._StringFieldConst
)
2220 self
.assertTrue(self
._def
_const
.has_field
)
2222 def test_has_field_prop_true(self
):
2223 self
._def
.has_field
= True
2224 self
.assertTrue(self
._def
.has_field
)
2226 def test_has_field_prop_false(self
):
2227 self
._def
.has_field
= False
2228 self
.assertFalse(self
._def
.has_field
)
2230 def test_bool_op_true(self
):
2231 self
._def
.value
= 'allo'
2232 self
.assertTrue(self
._def
)
2234 def test_bool_op_false(self
):
2235 self
._def
.has_field
= False
2236 self
.assertFalse(self
._def
)
2238 def test_field_prop_existing(self
):
2239 self
._def
.value
= 'meow'
2240 field
= self
._def
.field
2241 self
.assertEqual(field
, 'meow')
2243 def test_field_prop_none(self
):
2244 self
._def
.has_field
= False
2245 field
= self
._def
.field
2246 self
.assertIsNone(field
)
2248 def test_const_field_prop(self
):
2249 with self
.assertRaises(AttributeError):
2250 self
._def
_const
.has_field
= False
2252 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2253 self
.assertTrue(self
._def
_const
.has_field
)
2255 def test_field_prop_existing_then_none(self
):
2256 self
._def
.value
= 'meow'
2257 field
= self
._def
.field
2258 self
.assertEqual(field
, 'meow')
2259 self
._def
.has_field
= False
2260 field
= self
._def
.field
2261 self
.assertIsNone(field
)
2264 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2265 field
= field
['opt_field']
2266 field
.value
= 'walk'
2267 self
._def
.value
= 'walk'
2268 self
.assertEqual(self
._def
, field
)
2270 def test_const_eq(self
):
2271 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2272 field
= field
['opt_field']
2273 field
.value
= 'hiboux'
2274 self
.assertEqual(self
._def
_const
, field
)
2275 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2277 def test_eq_invalid_type(self
):
2278 self
._def
.value
= 'gerry'
2279 self
.assertNotEqual(self
._def
, 23)
2281 def test_str_op(self
):
2282 self
._def
.value
= 'marcel'
2283 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
2285 def test_repr_op(self
):
2286 self
._def
.value
= 'mireille'
2287 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
2290 class VariantFieldTestCase(unittest
.TestCase
):
2292 def _const_value_setter(field
):
2293 field
.selected_option_index
= 3
2296 def _create_fc(self
, tc
):
2297 ft0
= tc
.create_signed_integer_field_class(32)
2298 ft1
= tc
.create_string_field_class()
2299 ft2
= tc
.create_double_precision_real_field_class()
2300 ft3
= tc
.create_signed_integer_field_class(17)
2301 fc
= tc
.create_variant_field_class()
2302 fc
.append_option('corner', ft0
)
2303 fc
.append_option('zoom', ft1
)
2304 fc
.append_option('mellotron', ft2
)
2305 fc
.append_option('giorgio', ft3
)
2306 top_fc
= tc
.create_structure_field_class()
2307 top_fc
.append_member('variant_field', fc
)
2311 self
._tc
= get_default_trace_class()
2312 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2313 self
._def
= fld
['variant_field']
2315 self
._def
_value
= 1334
2316 self
._def
_selected
_index
= 3
2317 const_fc
= self
._create
_fc
(self
._tc
)['variant_field']
2319 fld_const
= create_const_field(
2320 self
._tc
, const_fc
.field_class
, self
._const
_value
_setter
2322 self
._def
_const
= fld_const
2324 def test_bool_op(self
):
2325 self
._def
.selected_option_index
= 2
2326 self
._def
.value
= -17.34
2327 with self
.assertRaises(NotImplementedError):
2330 def test_selected_option_index(self
):
2331 self
._def
.selected_option_index
= 2
2332 self
.assertEqual(self
._def
.selected_option_index
, 2)
2334 def test_selected_option_index_above_range(self
):
2335 with self
.assertRaises(IndexError):
2336 self
._def
.selected_option_index
= 4
2338 def test_selected_option_index_below_range(self
):
2339 with self
.assertRaises(IndexError):
2340 self
._def
.selected_option_index
= -1
2342 def test_const_selected_option_index(self
):
2343 with self
.assertRaises(AttributeError):
2344 self
._def
_const
.selected_option_index
= 2
2345 self
.assertEqual(self
._def
_const
.selected_option_index
, 3)
2347 def test_selected_option(self
):
2348 self
._def
.selected_option_index
= 2
2349 self
._def
.value
= -17.34
2350 self
.assertEqual(self
._def
.selected_option
, -17.34)
2351 self
.assertEqual(type(self
._def
.selected_option
), bt2
._DoublePrecisionRealField
)
2353 self
._def
.selected_option_index
= 3
2354 self
._def
.value
= 1921
2355 self
.assertEqual(self
._def
.selected_option
, 1921)
2356 self
.assertEqual(type(self
._def
.selected_option
), bt2
._SignedIntegerField
)
2358 def test_const_selected_option(self
):
2359 self
.assertEqual(self
._def
_const
.selected_option
, 1334)
2361 type(self
._def
_const
.selected_option
), bt2
._SignedIntegerFieldConst
2365 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2366 field
= field
['variant_field']
2367 field
.selected_option_index
= 0
2369 self
._def
.selected_option_index
= 0
2370 self
._def
.value
= 1774
2371 self
.assertEqual(self
._def
, field
)
2373 def test_const_eq(self
):
2374 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2375 field
= field
['variant_field']
2376 field
.selected_option_index
= 3
2378 self
.assertEqual(self
._def
_const
, field
)
2381 self
.assertEqual(len(self
._def
), 4)
2383 def test_eq_invalid_type(self
):
2384 self
._def
.selected_option_index
= 1
2385 self
._def
.value
= 'gerry'
2386 self
.assertNotEqual(self
._def
, 23)
2388 def test_str_op_int(self
):
2389 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2390 field
= field
['variant_field']
2391 field
.selected_option_index
= 0
2393 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2394 other_field
= other_field
['variant_field']
2395 other_field
.selected_option_index
= 0
2396 other_field
.value
= 1774
2397 self
.assertEqual(str(field
), str(other_field
))
2399 def test_str_op_str(self
):
2400 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2401 field
= field
['variant_field']
2402 field
.selected_option_index
= 1
2403 field
.value
= 'un beau grand bateau'
2404 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2405 other_field
= other_field
['variant_field']
2406 other_field
.selected_option_index
= 1
2407 other_field
.value
= 'un beau grand bateau'
2408 self
.assertEqual(str(field
), str(other_field
))
2410 def test_str_op_float(self
):
2411 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2412 field
= field
['variant_field']
2413 field
.selected_option_index
= 2
2414 field
.value
= 14.4245
2415 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2416 other_field
= other_field
['variant_field']
2417 other_field
.selected_option_index
= 2
2418 other_field
.value
= 14.4245
2419 self
.assertEqual(str(field
), str(other_field
))
2422 if __name__
== '__main__':