2 # Copyright (C) 2019 EfficiOS Inc.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; only version 2
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 from functools
import partial
, partialmethod
27 from utils
import get_default_trace_class
, create_const_field
30 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
33 # Create and return a stream with the field classes part of its stream packet
36 # The stream is part of a dummy trace created from trace class `tc`.
39 def _create_stream(tc
, ctx_field_classes
):
40 packet_context_fc
= tc
.create_structure_field_class()
41 for name
, fc
in ctx_field_classes
:
42 packet_context_fc
.append_member(name
, fc
)
45 stream_class
= tc
.create_stream_class(
46 packet_context_field_class
=packet_context_fc
, supports_packets
=True
49 stream
= trace
.create_stream(stream_class
)
53 # Create a field of the given field class.
55 # The field is part of a dummy stream, itself part of a dummy trace created
56 # from trace class `tc`.
59 def _create_field(tc
, field_class
):
61 stream
= _create_stream(tc
, [(field_name
, field_class
)])
62 packet
= stream
.create_packet()
63 return packet
.context_field
[field_name
]
66 # Create a field of type string.
68 # The field is part of a dummy stream, itself part of a dummy trace created
69 # from trace class `tc`. It is made out of a dummy string field class.
72 def _create_string_field(tc
):
73 field_name
= 'string_field'
74 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
75 packet
= stream
.create_packet()
76 return packet
.context_field
[field_name
]
79 # Create a field of type static array of ints.
81 # The field is part of a dummy stream, itself part of a dummy trace created
82 # from trace class `tc`. It is made out of a dummy static array field class,
83 # with a dummy integer field class as element class.
86 def _create_int_array_field(tc
, length
):
87 elem_fc
= tc
.create_signed_integer_field_class(32)
88 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
89 field_name
= 'int_array'
90 stream
= _create_stream(tc
, [(field_name
, fc
)])
91 packet
= stream
.create_packet()
92 return packet
.context_field
[field_name
]
95 # Create a field of type dynamic array of ints.
97 # The field is part of a dummy stream, itself part of a dummy trace created
98 # from trace class `tc`. It is made out of a dummy static array field class,
99 # with a dummy integer field class as element and length classes.
102 def _create_dynamic_array(tc
):
103 elem_fc
= tc
.create_signed_integer_field_class(32)
104 len_fc
= tc
.create_signed_integer_field_class(32)
105 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
106 field_name
= 'int_dyn_array'
107 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
108 packet
= stream
.create_packet()
109 packet
.context_field
[field_name
].length
= 3
110 return packet
.context_field
[field_name
]
113 # Create a field of type array of (empty) structures.
115 # The field is part of a dummy stream, itself part of a dummy trace created
116 # from trace class `tc`. It is made out of a dummy static array field class,
117 # with a dummy struct field class as element class.
120 def _create_struct_array_field(tc
, length
):
121 elem_fc
= tc
.create_structure_field_class()
122 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
123 field_name
= 'struct_array'
124 stream
= _create_stream(tc
, [(field_name
, fc
)])
125 packet
= stream
.create_packet()
126 return packet
.context_field
[field_name
]
129 class BitArrayFieldTestCase(unittest
.TestCase
):
130 def _create_field(self
):
131 return _create_field(self
._tc
, self
._tc
.create_bit_array_field_class(24))
134 self
._tc
= get_default_trace_class()
135 self
._def
_value
= 15497
136 self
._def
= self
._create
_field
()
137 self
._def
.value_as_integer
= self
._def
_value
138 self
._def
_new
_value
= 101542
140 def test_assign_invalid_type(self
):
141 with self
.assertRaises(TypeError):
142 self
._def
.value_as_integer
= 'onze'
144 def test_assign(self
):
145 self
._def
.value_as_integer
= 199
146 self
.assertEqual(self
._def
.value_as_integer
, 199)
148 def test_assign_masked(self
):
149 self
._def
.value_as_integer
= 0xE1549BB
150 self
.assertEqual(self
._def
.value_as_integer
, 0xE1549BB & ((1 << 24) - 1))
153 other
= self
._create
_field
()
154 other
.value_as_integer
= self
._def
_value
155 self
.assertEqual(self
._def
, other
)
157 def test_ne_same_type(self
):
158 other
= self
._create
_field
()
159 other
.value_as_integer
= self
._def
_value
- 1
160 self
.assertNotEqual(self
._def
, other
)
162 def test_ne_diff_type(self
):
163 self
.assertNotEqual(self
._def
, self
._def
_value
)
166 self
.assertEqual(len(self
._def
), 24)
169 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
172 self
.assertEqual(repr(self
._def
), repr(self
._def
_value
))
175 # Base class for numeric field test cases.
177 # To be compatible with this base class, a derived class must, in its
180 # * Set `self._def` to a field object with an arbitrary value.
181 # * Set `self._def_value` to the equivalent value of `self._def`.
182 class _TestNumericField
:
183 # Tries the binary operation `op`:
185 # 1. Between `self._def`, which is a field object, and `rhs`.
186 # 2. Between `self._def_value`, which is the raw value of
187 # `self._def`, and `rhs`.
189 # Returns the results of 1. and 2.
191 # If there's an exception while performing 1. or 2., asserts that
192 # both operations raised exceptions, that both exceptions have the
193 # same type, and returns `None` for both results.
194 def _binop(self
, op
, rhs
):
199 # try with field object
201 r
= op(self
._def
, rhs
)
202 except Exception as e
:
207 rv
= op(self
._def
_value
, comp_value
)
208 except Exception as e
:
211 if type_rexc
is not None or type_rvexc
is not None:
212 # at least one of the operations raised an exception: in
213 # this case both operations should have raised the same
214 # type of exception (division by zero, bit shift with a
215 # floating point number operand, etc.)
216 self
.assertIs(type_rexc
, type_rvexc
)
221 # Tries the unary operation `op`:
223 # 1. On `self._def`, which is a field object.
224 # 2. On `self._def_value`, which is the value of `self._def`.
226 # Returns the results of 1. and 2.
228 # If there's an exception while performing 1. or 2., asserts that
229 # both operations raised exceptions, that both exceptions have the
230 # same type, and returns `None` for both results.
231 def _unaryop(self
, op
):
235 # try with field object
238 except Exception as e
:
243 rv
= op(self
._def
_value
)
244 except Exception as e
:
247 if type_rexc
is not None or type_rvexc
is not None:
248 # at least one of the operations raised an exception: in
249 # this case both operations should have raised the same
250 # type of exception (division by zero, bit shift with a
251 # floating point number operand, etc.)
252 self
.assertIs(type_rexc
, type_rvexc
)
257 # Tests that the unary operation `op` gives results with the same
258 # type for both `self._def` and `self._def_value`.
259 def _test_unaryop_type(self
, op
):
260 r
, rv
= self
._unaryop
(op
)
265 self
.assertIsInstance(r
, type(rv
))
267 # Tests that the unary operation `op` gives results with the same
268 # value for both `self._def` and `self._def_value`. This uses the
269 # __eq__() operator of `self._def`.
270 def _test_unaryop_value(self
, op
):
271 r
, rv
= self
._unaryop
(op
)
276 self
.assertEqual(r
, rv
)
278 # Tests that the unary operation `op`, when applied to `self._def`,
279 # does not change its underlying BT object address.
280 def _test_unaryop_addr_same(self
, op
):
281 addr_before
= self
._def
.addr
283 self
.assertEqual(self
._def
.addr
, addr_before
)
285 # Tests that the unary operation `op`, when applied to `self._def`,
286 # does not change its value.
287 def _test_unaryop_value_same(self
, op
):
288 value_before
= copy
.copy(self
._def
_value
)
290 self
.assertEqual(self
._def
, value_before
)
292 # Tests that the binary operation `op` gives results with the same
293 # type for both `self._def` and `self._def_value`.
294 def _test_binop_type(self
, op
, rhs
):
295 r
, rv
= self
._binop
(op
, rhs
)
300 if op
in _COMP_BINOPS
:
301 # __eq__() and __ne__() always return a 'bool' object
302 self
.assertIsInstance(r
, bool)
304 self
.assertIsInstance(r
, type(rv
))
306 # Tests that the binary operation `op` gives results with the same
307 # value for both `self._def` and `self._def_value`. This uses the
308 # __eq__() operator of `self._def`.
309 def _test_binop_value(self
, op
, rhs
):
310 r
, rv
= self
._binop
(op
, rhs
)
315 self
.assertEqual(r
, rv
)
317 # Tests that the binary operation `op`, when applied to `self._def`,
318 # does not change its underlying BT object address.
319 def _test_binop_lhs_addr_same(self
, op
, rhs
):
320 addr_before
= self
._def
.addr
321 r
, rv
= self
._binop
(op
, rhs
)
322 self
.assertEqual(self
._def
.addr
, addr_before
)
324 # Tests that the binary operation `op`, when applied to `self._def`,
325 # does not change its value.
326 def _test_binop_lhs_value_same(self
, op
, rhs
):
327 value_before
= copy
.copy(self
._def
_value
)
328 r
, rv
= self
._binop
(op
, rhs
)
329 self
.assertEqual(self
._def
, value_before
)
331 # The methods below which take the `test_cb` and/or `op` parameters
332 # are meant to be used with one of the _test_binop_*() functions
333 # above as `test_cb` and a binary operator function as `op`.
337 # self._test_binop_rhs_pos_int(self._test_binop_value,
340 # This tests that a numeric field object added to a positive integer
341 # value gives a result with the expected value.
343 # `vint` and `vfloat` mean a signed integer value object and a real
346 def _test_binop_invalid_unknown(self
, op
):
347 if op
in _COMP_BINOPS
:
348 self
.skipTest('not testing')
353 with self
.assertRaises(TypeError):
356 def _test_binop_invalid_none(self
, op
):
357 if op
in _COMP_BINOPS
:
358 self
.skipTest('not testing')
360 with self
.assertRaises(TypeError):
363 def _test_binop_rhs_false(self
, test_cb
, op
):
366 def _test_binop_rhs_true(self
, test_cb
, op
):
369 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
372 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
375 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
378 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
379 test_cb(op
, bt2
.create_value(2))
381 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
382 test_cb(op
, bt2
.create_value(-23))
384 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
385 test_cb(op
, bt2
.create_value(0))
387 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
390 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
393 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
396 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
397 test_cb(op
, bt2
.create_value(2.2))
399 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
400 test_cb(op
, bt2
.create_value(-23.4))
402 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
403 test_cb(op
, bt2
.create_value(0.0))
405 def _test_binop_rhs_complex(self
, test_cb
, op
):
406 test_cb(op
, -23 + 19j
)
408 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
411 def _test_binop_type_false(self
, op
):
412 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
414 def _test_binop_type_true(self
, op
):
415 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
417 def _test_binop_type_pos_int(self
, op
):
418 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
420 def _test_binop_type_neg_int(self
, op
):
421 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
423 def _test_binop_type_zero_int(self
, op
):
424 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
426 def _test_binop_type_pos_vint(self
, op
):
427 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
429 def _test_binop_type_neg_vint(self
, op
):
430 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
432 def _test_binop_type_zero_vint(self
, op
):
433 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
435 def _test_binop_type_pos_float(self
, op
):
436 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
438 def _test_binop_type_neg_float(self
, op
):
439 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
441 def _test_binop_type_zero_float(self
, op
):
442 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
444 def _test_binop_type_pos_vfloat(self
, op
):
445 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
447 def _test_binop_type_neg_vfloat(self
, op
):
448 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
450 def _test_binop_type_zero_vfloat(self
, op
):
451 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
453 def _test_binop_type_complex(self
, op
):
454 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
456 def _test_binop_type_zero_complex(self
, op
):
457 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
459 def _test_binop_value_false(self
, op
):
460 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
462 def _test_binop_value_true(self
, op
):
463 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
465 def _test_binop_value_pos_int(self
, op
):
466 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
468 def _test_binop_value_neg_int(self
, op
):
469 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
471 def _test_binop_value_zero_int(self
, op
):
472 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
474 def _test_binop_value_pos_vint(self
, op
):
475 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
477 def _test_binop_value_neg_vint(self
, op
):
478 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
480 def _test_binop_value_zero_vint(self
, op
):
481 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
483 def _test_binop_value_pos_float(self
, op
):
484 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
486 def _test_binop_value_neg_float(self
, op
):
487 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
489 def _test_binop_value_zero_float(self
, op
):
490 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
492 def _test_binop_value_pos_vfloat(self
, op
):
493 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
495 def _test_binop_value_neg_vfloat(self
, op
):
496 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
498 def _test_binop_value_zero_vfloat(self
, op
):
499 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
501 def _test_binop_value_complex(self
, op
):
502 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
504 def _test_binop_value_zero_complex(self
, op
):
505 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
507 def _test_binop_lhs_addr_same_false(self
, op
):
508 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
510 def _test_binop_lhs_addr_same_true(self
, op
):
511 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
513 def _test_binop_lhs_addr_same_pos_int(self
, op
):
514 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
516 def _test_binop_lhs_addr_same_neg_int(self
, op
):
517 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
519 def _test_binop_lhs_addr_same_zero_int(self
, op
):
520 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
522 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
523 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
525 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
526 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
528 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
529 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
531 def _test_binop_lhs_addr_same_pos_float(self
, op
):
532 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
534 def _test_binop_lhs_addr_same_neg_float(self
, op
):
535 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
537 def _test_binop_lhs_addr_same_zero_float(self
, op
):
538 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
540 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
541 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
543 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
544 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
546 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
547 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
549 def _test_binop_lhs_addr_same_complex(self
, op
):
550 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
552 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
553 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
555 def _test_binop_lhs_value_same_false(self
, op
):
556 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
558 def _test_binop_lhs_value_same_true(self
, op
):
559 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
561 def _test_binop_lhs_value_same_pos_int(self
, op
):
562 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
564 def _test_binop_lhs_value_same_neg_int(self
, op
):
565 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
567 def _test_binop_lhs_value_same_zero_int(self
, op
):
568 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
570 def _test_binop_lhs_value_same_pos_vint(self
, op
):
571 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
573 def _test_binop_lhs_value_same_neg_vint(self
, op
):
574 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
576 def _test_binop_lhs_value_same_zero_vint(self
, op
):
577 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
579 def _test_binop_lhs_value_same_pos_float(self
, op
):
580 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
582 def _test_binop_lhs_value_same_neg_float(self
, op
):
583 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
585 def _test_binop_lhs_value_same_zero_float(self
, op
):
586 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
588 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
589 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
591 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
592 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
594 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
595 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
597 def _test_binop_lhs_value_same_complex(self
, op
):
598 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
600 def _test_binop_lhs_value_same_zero_complex(self
, op
):
601 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
603 def test_bool_op(self
):
604 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
606 def test_int_op(self
):
607 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
609 def test_float_op(self
):
610 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
612 def test_complex_op(self
):
613 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
615 def test_str_op(self
):
616 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
618 def test_hash_op(self
):
619 with self
.assertRaises(TypeError):
622 def test_const_hash_op(self
):
623 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
625 def test_const_hash_dict(self
):
627 my_dict
[self
._def
_const
] = 'my_value'
628 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
630 def test_eq_none(self
):
631 # Ignore this lint error:
632 # E711 comparison to None should be 'if cond is None:'
633 # since this is what we want to test (even though not good practice).
634 self
.assertFalse(self
._def
== None) # noqa: E711
636 def test_ne_none(self
):
637 # Ignore this lint error:
638 # E711 comparison to None should be 'if cond is not None:'
639 # since this is what we want to test (even though not good practice).
640 self
.assertTrue(self
._def
!= None) # noqa: E711
643 # This is a list of binary operators used for
644 # _inject_numeric_testing_methods().
646 # Each entry is a pair of binary operator name (used as part of the
647 # created testing method's name) and operator function.
655 ('add', operator
.add
),
656 ('radd', lambda a
, b
: operator
.add(b
, a
)),
657 ('and', operator
.and_
),
658 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
659 ('floordiv', operator
.floordiv
),
660 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
661 ('lshift', operator
.lshift
),
662 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
663 ('mod', operator
.mod
),
664 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
665 ('mul', operator
.mul
),
666 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
667 ('or', operator
.or_
),
668 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
669 ('pow', operator
.pow),
670 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
671 ('rshift', operator
.rshift
),
672 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
673 ('sub', operator
.sub
),
674 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
675 ('truediv', operator
.truediv
),
676 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
677 ('xor', operator
.xor
),
678 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
682 # This is a list of unary operators used for
683 # _inject_numeric_testing_methods().
685 # Each entry is a pair of unary operator name (used as part of the
686 # created testing method's name) and operator function.
688 ('neg', operator
.neg
),
689 ('pos', operator
.pos
),
690 ('abs', operator
.abs),
691 ('invert', operator
.invert
),
693 ('round_0', partial(round, ndigits
=0)),
694 ('round_1', partial(round, ndigits
=1)),
695 ('round_2', partial(round, ndigits
=2)),
696 ('round_3', partial(round, ndigits
=3)),
698 ('floor', math
.floor
),
699 ('trunc', math
.trunc
),
703 # This function injects a bunch of testing methods to a numeric
706 # It is meant to be used like this:
708 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
710 # This function injects:
712 # * One testing method for each _TestNumericField._test_binop_*()
713 # method, for each binary operator in the _BINOPS tuple.
715 # * One testing method for each _TestNumericField._test_unaryop*()
716 # method, for each unary operator in the _UNARYOPS tuple.
717 def _inject_numeric_testing_methods(cls
):
718 def test_binop_name(suffix
):
719 return 'test_binop_{}_{}'.format(name
, suffix
)
721 def test_unaryop_name(suffix
):
722 return 'test_unaryop_{}_{}'.format(name
, suffix
)
724 # inject testing methods for each binary operation
725 for name
, binop
in _BINOPS
:
728 test_binop_name('invalid_unknown'),
729 partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
),
733 test_binop_name('invalid_none'),
734 partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
),
738 test_binop_name('type_true'),
739 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
743 test_binop_name('type_pos_int'),
744 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
748 test_binop_name('type_pos_vint'),
749 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
753 test_binop_name('value_true'),
754 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
758 test_binop_name('value_pos_int'),
759 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
763 test_binop_name('value_pos_vint'),
764 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
768 test_binop_name('lhs_addr_same_true'),
769 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
773 test_binop_name('lhs_addr_same_pos_int'),
775 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
780 test_binop_name('lhs_addr_same_pos_vint'),
782 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
787 test_binop_name('lhs_value_same_true'),
788 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
792 test_binop_name('lhs_value_same_pos_int'),
794 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
799 test_binop_name('lhs_value_same_pos_vint'),
801 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
806 test_binop_name('type_neg_int'),
807 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
811 test_binop_name('type_neg_vint'),
812 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
816 test_binop_name('value_neg_int'),
817 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
821 test_binop_name('value_neg_vint'),
822 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
826 test_binop_name('lhs_addr_same_neg_int'),
828 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
833 test_binop_name('lhs_addr_same_neg_vint'),
835 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
840 test_binop_name('lhs_value_same_neg_int'),
842 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
847 test_binop_name('lhs_value_same_neg_vint'),
849 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
854 test_binop_name('type_false'),
855 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
859 test_binop_name('type_zero_int'),
860 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
864 test_binop_name('type_zero_vint'),
865 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
869 test_binop_name('value_false'),
870 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
874 test_binop_name('value_zero_int'),
875 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
879 test_binop_name('value_zero_vint'),
880 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
884 test_binop_name('lhs_addr_same_false'),
885 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
889 test_binop_name('lhs_addr_same_zero_int'),
891 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
896 test_binop_name('lhs_addr_same_zero_vint'),
898 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
903 test_binop_name('lhs_value_same_false'),
904 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
908 test_binop_name('lhs_value_same_zero_int'),
910 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
915 test_binop_name('lhs_value_same_zero_vint'),
917 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
922 test_binop_name('type_pos_float'),
923 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
927 test_binop_name('type_neg_float'),
928 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
932 test_binop_name('type_pos_vfloat'),
933 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
937 test_binop_name('type_neg_vfloat'),
938 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
942 test_binop_name('value_pos_float'),
943 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
947 test_binop_name('value_neg_float'),
948 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
952 test_binop_name('value_pos_vfloat'),
953 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
957 test_binop_name('value_neg_vfloat'),
958 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
962 test_binop_name('lhs_addr_same_pos_float'),
964 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
969 test_binop_name('lhs_addr_same_neg_float'),
971 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
976 test_binop_name('lhs_addr_same_pos_vfloat'),
978 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
983 test_binop_name('lhs_addr_same_neg_vfloat'),
985 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
990 test_binop_name('lhs_value_same_pos_float'),
992 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
997 test_binop_name('lhs_value_same_neg_float'),
999 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
1004 test_binop_name('lhs_value_same_pos_vfloat'),
1006 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
1011 test_binop_name('lhs_value_same_neg_vfloat'),
1013 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
1018 test_binop_name('type_zero_float'),
1019 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
1023 test_binop_name('type_zero_vfloat'),
1024 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
1028 test_binop_name('value_zero_float'),
1029 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
1033 test_binop_name('value_zero_vfloat'),
1034 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
1038 test_binop_name('lhs_addr_same_zero_float'),
1040 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
1045 test_binop_name('lhs_addr_same_zero_vfloat'),
1047 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
1052 test_binop_name('lhs_value_same_zero_float'),
1054 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1059 test_binop_name('lhs_value_same_zero_vfloat'),
1061 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1066 test_binop_name('type_complex'),
1067 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1071 test_binop_name('type_zero_complex'),
1072 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1076 test_binop_name('value_complex'),
1077 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1081 test_binop_name('value_zero_complex'),
1082 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1086 test_binop_name('lhs_addr_same_complex'),
1088 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1093 test_binop_name('lhs_addr_same_zero_complex'),
1095 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1100 test_binop_name('lhs_value_same_complex'),
1102 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1107 test_binop_name('lhs_value_same_zero_complex'),
1109 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1113 # inject testing methods for each unary operation
1114 for name
, unaryop
in _UNARYOPS
:
1117 test_unaryop_name('type'),
1118 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1122 test_unaryop_name('value'),
1123 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1127 test_unaryop_name('addr_same'),
1128 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1132 test_unaryop_name('value_same'),
1133 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1137 class BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1139 def _const_value_setter(field
):
1142 def _create_fc(self
, tc
):
1143 return tc
.create_bool_field_class()
1146 self
._tc
= get_default_trace_class()
1147 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1148 self
._def
.value
= True
1149 self
._def
_value
= True
1150 self
._def
_const
= create_const_field(
1151 self
._tc
, self
._tc
.create_bool_field_class(), self
._const
_value
_setter
1153 self
._def
_new
_value
= False
1155 def test_classes(self
):
1156 self
.assertIs(type(self
._def
), bt2
._BoolField
)
1157 self
.assertIs(type(self
._def
_const
), bt2
._BoolFieldConst
)
1159 def test_assign_true(self
):
1161 self
._def
.value
= raw
1162 self
.assertEqual(self
._def
, raw
)
1164 def test_assign_false(self
):
1166 self
._def
.value
= raw
1167 self
.assertEqual(self
._def
, raw
)
1169 def test_assign_field_true(self
):
1170 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1173 self
._def
.value
= field
1174 self
.assertEqual(self
._def
, raw
)
1176 def test_assign_field_false(self
):
1177 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1180 self
._def
.value
= field
1181 self
.assertEqual(self
._def
, raw
)
1183 def test_assign_invalid_type(self
):
1184 with self
.assertRaises(TypeError):
1185 self
._def
.value
= 17
1187 def test_str_op(self
):
1188 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1191 _inject_numeric_testing_methods(BoolFieldTestCase
)
1194 class _TestIntegerFieldCommon(_TestNumericField
):
1195 def test_assign_true(self
):
1197 self
._def
.value
= raw
1198 self
.assertEqual(self
._def
, raw
)
1200 def test_assign_false(self
):
1202 self
._def
.value
= raw
1203 self
.assertEqual(self
._def
, raw
)
1205 def test_assign_pos_int(self
):
1207 self
._def
.value
= raw
1208 self
.assertEqual(self
._def
, raw
)
1210 def test_assign_neg_int(self
):
1212 self
._def
.value
= raw
1213 self
.assertEqual(self
._def
, raw
)
1215 def test_assign_int_field(self
):
1217 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1219 self
._def
.value
= field
1220 self
.assertEqual(self
._def
, raw
)
1222 def test_assign_invalid_type(self
):
1223 with self
.assertRaises(TypeError):
1224 self
._def
.value
= 'yes'
1226 def test_assign_uint(self
):
1227 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1228 field
= _create_field(self
._tc
, uint_fc
)
1231 self
.assertEqual(field
, raw
)
1233 def test_assign_big_uint(self
):
1234 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1235 field
= _create_field(self
._tc
, uint_fc
)
1236 # Larger than the IEEE 754 double-precision exact representation of
1239 field
.value
= (2 ** 53) + 1
1240 self
.assertEqual(field
, raw
)
1242 def test_assign_uint_out_of_range(self
):
1243 uint_fc
= self
._tc
.create_unsigned_integer_field_class(8)
1244 field
= _create_field(self
._tc
, uint_fc
)
1246 with self
.assertRaises(ValueError) as ctx
:
1249 str(ctx
.exception
), 'Value 256 is outside valid range [0, 255]'
1252 with self
.assertRaises(ValueError) as ctx
:
1254 self
.assertEqual(str(ctx
.exception
), 'Value -1 is outside valid range [0, 255]')
1256 def test_assign_int_out_of_range(self
):
1257 int_fc
= self
._tc
.create_signed_integer_field_class(8)
1258 field
= _create_field(self
._tc
, int_fc
)
1260 with self
.assertRaises(ValueError) as ctx
:
1263 str(ctx
.exception
), 'Value 128 is outside valid range [-128, 127]'
1266 with self
.assertRaises(ValueError) as ctx
:
1269 str(ctx
.exception
), 'Value -129 is outside valid range [-128, 127]'
1272 def test_str_op(self
):
1273 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1276 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1279 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1281 def _const_value_setter(field
):
1284 def _create_fc(self
, tc
):
1285 return tc
.create_signed_integer_field_class(25)
1288 self
._tc
= get_default_trace_class()
1289 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1290 self
._field
.value
= 17
1291 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1292 self
._def
.value
= 17
1293 self
._def
_value
= 17
1294 self
._def
_const
= create_const_field(
1295 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1297 self
._def
_new
_value
= -101
1300 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1302 def _const_value_setter(field
):
1305 def _create_fc(self
, tc
):
1306 fc
= tc
.create_signed_enumeration_field_class(32)
1307 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1308 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1309 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1311 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1313 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1317 self
._tc
= get_default_trace_class()
1318 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1319 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1320 self
._def
.value
= 17
1321 self
._def
_value
= 17
1322 self
._def
_const
= create_const_field(
1323 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1325 self
._def
_new
_value
= -101
1327 def test_str_op(self
):
1328 expected_string_found
= False
1331 # Establish all permutations of the three expected matches since
1332 # the order in which mappings are enumerated is not explicitly part of
1334 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1335 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1337 expected_string_found
= True
1340 self
.assertTrue(expected_string_found
)
1342 def test_labels(self
):
1343 self
._field
.value
= 17
1344 labels
= sorted(self
._field
.labels
)
1345 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1348 class SingleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1350 def _const_value_setter(field
):
1353 def _create_fc(self
, tc
):
1354 return tc
.create_single_precision_real_field_class()
1357 self
._tc
= get_default_trace_class()
1358 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1359 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1360 self
._def
_const
= create_const_field(
1362 self
._tc
.create_single_precision_real_field_class(),
1363 self
._const
_value
_setter
,
1365 self
._def
.value
= 52.0
1366 self
._def
_value
= 52.0
1367 self
._def
_new
_value
= -17.0
1369 def _test_invalid_op(self
, cb
):
1370 with self
.assertRaises(TypeError):
1373 def test_assign_true(self
):
1374 self
._def
.value
= True
1375 self
.assertTrue(self
._def
)
1377 def test_assign_false(self
):
1378 self
._def
.value
= False
1379 self
.assertFalse(self
._def
)
1381 def test_assign_pos_int(self
):
1383 self
._def
.value
= raw
1384 self
.assertEqual(self
._def
, float(raw
))
1386 def test_assign_neg_int(self
):
1388 self
._def
.value
= raw
1389 self
.assertEqual(self
._def
, float(raw
))
1391 def test_assign_int_field(self
):
1392 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1393 int_field
= _create_field(self
._tc
, int_fc
)
1395 int_field
.value
= raw
1396 self
._def
.value
= int_field
1397 self
.assertEqual(self
._def
, float(raw
))
1399 def test_assign_float(self
):
1401 self
._def
.value
= raw
1402 # It's expected to have some lost of precision because of the field
1403 # that is in single precision.
1404 self
.assertEqual(round(self
._def
, 5), raw
)
1406 def test_assign_float_field(self
):
1407 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1410 self
._def
.value
= field
1411 # It's expected to have some lost of precision because of the field
1412 # that is in single precision.
1413 self
.assertEqual(round(self
._def
, 5), raw
)
1415 def test_assign_invalid_type(self
):
1416 with self
.assertRaises(TypeError):
1417 self
._def
.value
= 'yes'
1419 def test_invalid_lshift(self
):
1420 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1422 def test_invalid_rshift(self
):
1423 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1425 def test_invalid_and(self
):
1426 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1428 def test_invalid_or(self
):
1429 self
._test
_invalid
_op
(lambda: self
._def |
23)
1431 def test_invalid_xor(self
):
1432 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1434 def test_invalid_invert(self
):
1435 self
._test
_invalid
_op
(lambda: ~self
._def
)
1437 def test_str_op(self
):
1438 self
.assertEqual(str(round(self
._def
, 5)), str(self
._def
_value
))
1441 _inject_numeric_testing_methods(SingleRealFieldTestCase
)
1444 class DoubleRealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1446 def _const_value_setter(field
):
1449 def _create_fc(self
, tc
):
1450 return tc
.create_double_precision_real_field_class()
1453 self
._tc
= get_default_trace_class()
1454 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1455 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1456 self
._def
_const
= create_const_field(
1458 self
._tc
.create_double_precision_real_field_class(),
1459 self
._const
_value
_setter
,
1461 self
._def
.value
= 52.7
1462 self
._def
_value
= 52.7
1463 self
._def
_new
_value
= -17.164857
1465 def _test_invalid_op(self
, cb
):
1466 with self
.assertRaises(TypeError):
1469 def test_assign_true(self
):
1470 self
._def
.value
= True
1471 self
.assertTrue(self
._def
)
1473 def test_assign_false(self
):
1474 self
._def
.value
= False
1475 self
.assertFalse(self
._def
)
1477 def test_assign_pos_int(self
):
1479 self
._def
.value
= raw
1480 self
.assertEqual(self
._def
, float(raw
))
1482 def test_assign_neg_int(self
):
1484 self
._def
.value
= raw
1485 self
.assertEqual(self
._def
, float(raw
))
1487 def test_assign_int_field(self
):
1488 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1489 int_field
= _create_field(self
._tc
, int_fc
)
1491 int_field
.value
= raw
1492 self
._def
.value
= int_field
1493 self
.assertEqual(self
._def
, float(raw
))
1495 def test_assign_float(self
):
1497 self
._def
.value
= raw
1498 self
.assertEqual(self
._def
, raw
)
1500 def test_assign_float_field(self
):
1501 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1504 self
._def
.value
= field
1505 self
.assertEqual(self
._def
, raw
)
1507 def test_assign_invalid_type(self
):
1508 with self
.assertRaises(TypeError):
1509 self
._def
.value
= 'yes'
1511 def test_invalid_lshift(self
):
1512 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1514 def test_invalid_rshift(self
):
1515 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1517 def test_invalid_and(self
):
1518 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1520 def test_invalid_or(self
):
1521 self
._test
_invalid
_op
(lambda: self
._def |
23)
1523 def test_invalid_xor(self
):
1524 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1526 def test_invalid_invert(self
):
1527 self
._test
_invalid
_op
(lambda: ~self
._def
)
1529 def test_str_op(self
):
1530 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1533 _inject_numeric_testing_methods(DoubleRealFieldTestCase
)
1536 class StringFieldTestCase(unittest
.TestCase
):
1538 def _const_value_setter(field
):
1539 field
.value
= 'Hello, World!'
1542 self
._tc
= get_default_trace_class()
1543 self
._def
_value
= 'Hello, World!'
1544 self
._def
= _create_string_field(self
._tc
)
1545 self
._def
_const
= create_const_field(
1546 self
._tc
, self
._tc
.create_string_field_class(), self
._const
_value
_setter
1548 self
._def
.value
= self
._def
_value
1549 self
._def
_new
_value
= 'Yes!'
1551 def test_assign_int(self
):
1552 with self
.assertRaises(TypeError):
1553 self
._def
.value
= 283
1555 def test_assign_string_field(self
):
1556 field
= _create_string_field(self
._tc
)
1559 self
.assertEqual(field
, raw
)
1562 self
.assertEqual(self
._def
, self
._def
_value
)
1564 def test_const_eq(self
):
1565 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1567 def test_not_eq(self
):
1568 self
.assertNotEqual(self
._def
, 23)
1570 def test_lt_vstring(self
):
1571 s1
= _create_string_field(self
._tc
)
1573 s2
= _create_string_field(self
._tc
)
1575 self
.assertLess(s1
, s2
)
1577 def test_lt_string(self
):
1578 s1
= _create_string_field(self
._tc
)
1580 self
.assertLess(s1
, 'bateau')
1582 def test_le_vstring(self
):
1583 s1
= _create_string_field(self
._tc
)
1585 s2
= _create_string_field(self
._tc
)
1587 self
.assertLessEqual(s1
, s2
)
1589 def test_le_string(self
):
1590 s1
= _create_string_field(self
._tc
)
1592 self
.assertLessEqual(s1
, 'bateau')
1594 def test_gt_vstring(self
):
1595 s1
= _create_string_field(self
._tc
)
1597 s2
= _create_string_field(self
._tc
)
1599 self
.assertGreater(s2
, s1
)
1601 def test_gt_string(self
):
1602 s1
= _create_string_field(self
._tc
)
1604 self
.assertGreater('bateau', s1
)
1606 def test_ge_vstring(self
):
1607 s1
= _create_string_field(self
._tc
)
1609 s2
= _create_string_field(self
._tc
)
1611 self
.assertGreaterEqual(s2
, s1
)
1613 def test_ge_string(self
):
1614 s1
= _create_string_field(self
._tc
)
1616 self
.assertGreaterEqual('bateau', s1
)
1618 def test_bool_op(self
):
1619 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1621 def test_str_op(self
):
1622 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1625 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1627 def test_getitem(self
):
1628 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1630 def test_const_getitem(self
):
1631 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1633 def test_append_str(self
):
1634 to_append
= 'meow meow meow'
1635 self
._def
+= to_append
1636 self
._def
_value
+= to_append
1637 self
.assertEqual(self
._def
, self
._def
_value
)
1639 def test_const_append_str(self
):
1640 to_append
= 'meow meow meow'
1641 with self
.assertRaises(TypeError):
1642 self
._def
_const
+= to_append
1643 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1645 def test_append_string_field(self
):
1646 field
= _create_string_field(self
._tc
)
1647 to_append
= 'meow meow meow'
1648 field
.value
= to_append
1650 self
._def
_value
+= to_append
1651 self
.assertEqual(self
._def
, self
._def
_value
)
1653 def test_hash_op(self
):
1654 with self
.assertRaises(TypeError):
1657 def test_const_hash_op(self
):
1658 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
1660 def test_const_hash_dict(self
):
1662 my_dict
[self
._def
_const
] = 'my_value'
1663 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
1666 class _TestArrayFieldCommon
:
1667 def _modify_def(self
):
1670 def test_bool_op_true(self
):
1671 self
.assertTrue(self
._def
)
1674 self
.assertEqual(len(self
._def
), 3)
1676 def test_length(self
):
1677 self
.assertEqual(self
._def
.length
, 3)
1679 def test_getitem(self
):
1680 field
= self
._def
[1]
1681 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1682 self
.assertEqual(field
, 1847)
1684 def test_const_getitem(self
):
1685 field
= self
._def
_const
[1]
1686 self
.assertIs(type(field
), bt2
._SignedIntegerFieldConst
)
1687 self
.assertEqual(field
, 1847)
1690 field
= _create_int_array_field(self
._tc
, 3)
1694 self
.assertEqual(self
._def
, field
)
1696 def test_eq_invalid_type(self
):
1697 self
.assertNotEqual(self
._def
, 23)
1699 def test_eq_diff_len(self
):
1700 field
= _create_int_array_field(self
._tc
, 2)
1703 self
.assertNotEqual(self
._def
, field
)
1705 def test_eq_diff_content_same_len(self
):
1706 field
= _create_int_array_field(self
._tc
, 3)
1710 self
.assertNotEqual(self
._def
, field
)
1712 def test_eq_non_sequence_iterable(self
):
1713 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1714 field
= _create_int_array_field(self
._tc
, 3)
1718 self
.assertEqual(field
, list(dct
.keys()))
1719 self
.assertNotEqual(field
, dct
)
1721 def test_setitem(self
):
1723 self
.assertEqual(self
._def
[2], 24)
1725 def test_setitem_int_field(self
):
1726 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1727 int_field
= _create_field(self
._tc
, int_fc
)
1728 int_field
.value
= 19487
1729 self
._def
[1] = int_field
1730 self
.assertEqual(self
._def
[1], 19487)
1732 def test_setitem_non_basic_field(self
):
1733 array_field
= _create_struct_array_field(self
._tc
, 2)
1734 with self
.assertRaises(TypeError):
1737 def test_setitem_none(self
):
1738 with self
.assertRaises(TypeError):
1741 def test_setitem_index_wrong_type(self
):
1742 with self
.assertRaises(TypeError):
1743 self
._def
['yes'] = 23
1745 def test_setitem_index_neg(self
):
1746 with self
.assertRaises(IndexError):
1749 def test_setitem_index_out_of_range(self
):
1750 with self
.assertRaises(IndexError):
1751 self
._def
[len(self
._def
)] = 134679
1753 def test_const_setitem(self
):
1754 with self
.assertRaises(TypeError):
1755 self
._def
_const
[0] = 134679
1757 def test_iter(self
):
1758 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1759 self
.assertEqual(field
, value
)
1761 def test_const_iter(self
):
1762 for field
, value
in zip(self
._def
_const
, (45, 1847, 1948754)):
1763 self
.assertEqual(field
, value
)
1765 def test_value_int_field(self
):
1766 values
= [45646, 145, 12145]
1767 self
._def
.value
= values
1768 self
.assertEqual(values
, self
._def
)
1770 def test_value_check_sequence(self
):
1772 with self
.assertRaises(TypeError):
1773 self
._def
.value
= values
1775 def test_value_wrong_type_in_sequence(self
):
1776 values
= [32, 'hello', 11]
1777 with self
.assertRaises(TypeError):
1778 self
._def
.value
= values
1780 def test_value_complex_type(self
):
1781 struct_fc
= self
._tc
.create_structure_field_class()
1782 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1783 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1784 str_fc
= self
._tc
.create_string_field_class()
1785 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1786 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1787 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1788 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1789 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1791 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1792 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1793 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1796 array
= stream
.create_packet().context_field
['array_field']
1797 array
.value
= values
1798 self
.assertEqual(values
, array
)
1799 values
[0]['an_int'] = 'a string'
1800 with self
.assertRaises(TypeError):
1801 array
.value
= values
1803 def test_str_op(self
):
1805 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1806 self
.assertEqual(expected_string
, s
)
1809 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1811 def _const_value_setter(field
):
1812 field
.value
= [45, 1847, 1948754]
1815 self
._tc
= get_default_trace_class()
1816 self
._def
= _create_int_array_field(self
._tc
, 3)
1819 self
._def
[2] = 1948754
1820 self
._def
_value
= [45, 1847, 1948754]
1821 self
._def
_const
= create_const_field(
1823 self
._tc
.create_static_array_field_class(
1824 self
._tc
.create_signed_integer_field_class(32), 3
1826 self
._const
_value
_setter
,
1829 def test_value_wrong_len(self
):
1831 with self
.assertRaises(ValueError):
1832 self
._def
.value
= values
1835 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1837 def _const_value_setter(field
):
1838 field
.value
= [45, 1847, 1948754]
1841 self
._tc
= get_default_trace_class()
1842 self
._def
= _create_dynamic_array(self
._tc
)
1845 self
._def
[2] = 1948754
1846 self
._def
_value
= [45, 1847, 1948754]
1847 self
._def
_const
= create_const_field(
1849 self
._tc
.create_dynamic_array_field_class(
1850 self
._tc
.create_signed_integer_field_class(32)
1852 self
._const
_value
_setter
,
1855 def test_value_resize(self
):
1856 new_values
= [1, 2, 3, 4]
1857 self
._def
.value
= new_values
1858 self
.assertCountEqual(self
._def
, new_values
)
1860 def test_set_length(self
):
1861 self
._def
.length
= 4
1863 self
.assertEqual(len(self
._def
), 4)
1865 def test_const_set_length(self
):
1866 with self
.assertRaises(AttributeError):
1867 self
._def
_const
.length
= 4
1868 self
.assertEqual(len(self
._def
), 3)
1870 def test_set_invalid_length(self
):
1871 with self
.assertRaises(TypeError):
1872 self
._def
.length
= 'cheval'
1875 class StructureFieldTestCase(unittest
.TestCase
):
1877 def _const_value_setter(field
):
1887 def _create_fc(self
, tc
):
1888 fc
= tc
.create_structure_field_class()
1889 fc
.append_member('A', self
._fc
0_fn
())
1890 fc
.append_member('B', self
._fc
1_fn
())
1891 fc
.append_member('C', self
._fc
2_fn
())
1892 fc
.append_member('D', self
._fc
3_fn
())
1893 fc
.append_member('E', self
._fc
4_fn
())
1894 fc5
= self
._fc
5_fn
()
1895 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1896 fc
.append_member('F', fc5
)
1900 self
._tc
= get_default_trace_class()
1901 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1902 self
._fc
1_fn
= self
._tc
.create_string_field_class
1903 self
._fc
2_fn
= self
._tc
.create_double_precision_real_field_class
1904 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1905 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1906 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1907 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1909 self
._fc
= self
._create
_fc
(self
._tc
)
1910 self
._def
= _create_field(self
._tc
, self
._fc
)
1911 self
._def
['A'] = -1872
1912 self
._def
['B'] = 'salut'
1913 self
._def
['C'] = 17.5
1914 self
._def
['D'] = 16497
1916 self
._def
['F'] = {'F_1': 52}
1926 self
._def
_const
= create_const_field(
1927 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1930 def _modify_def(self
):
1931 self
._def
['B'] = 'hola'
1933 def test_bool_op_true(self
):
1934 self
.assertTrue(self
._def
)
1936 def test_bool_op_false(self
):
1937 field
= self
._def
['E']
1938 self
.assertFalse(field
)
1941 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1943 def test_getitem(self
):
1944 field1
= self
._def
['A']
1945 field2
= self
._def
['B']
1946 field3
= self
._def
['C']
1947 field4
= self
._def
['D']
1948 field5
= self
._def
['E']
1949 field6
= self
._def
['F']
1951 self
.assertIs(type(field1
), bt2
._SignedIntegerField
)
1952 self
.assertEqual(field1
, -1872)
1954 self
.assertIs(type(field2
), bt2
._StringField
)
1955 self
.assertEqual(field2
, 'salut')
1957 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealField
)
1958 self
.assertEqual(field3
, 17.5)
1960 self
.assertIs(type(field4
), bt2
._SignedIntegerField
)
1961 self
.assertEqual(field4
, 16497)
1963 self
.assertIs(type(field5
), bt2
._StructureField
)
1964 self
.assertEqual(field5
, {})
1966 self
.assertIs(type(field6
), bt2
._StructureField
)
1967 self
.assertEqual(field6
, {'F_1': 52})
1969 def test_const_getitem(self
):
1970 field1
= self
._def
_const
['A']
1971 field2
= self
._def
_const
['B']
1972 field3
= self
._def
_const
['C']
1973 field4
= self
._def
_const
['D']
1974 field5
= self
._def
_const
['E']
1975 field6
= self
._def
_const
['F']
1977 self
.assertIs(type(field1
), bt2
._SignedIntegerFieldConst
)
1978 self
.assertEqual(field1
, -1872)
1980 self
.assertIs(type(field2
), bt2
._StringFieldConst
)
1981 self
.assertEqual(field2
, 'salut')
1983 self
.assertIs(type(field3
), bt2
._DoublePrecisionRealFieldConst
)
1984 self
.assertEqual(field3
, 17.5)
1986 self
.assertIs(type(field4
), bt2
._SignedIntegerFieldConst
)
1987 self
.assertEqual(field4
, 16497)
1989 self
.assertIs(type(field5
), bt2
._StructureFieldConst
)
1990 self
.assertEqual(field5
, {})
1992 self
.assertIs(type(field6
), bt2
._StructureFieldConst
)
1993 self
.assertEqual(field6
, {'F_1': 52})
1995 def test_member_at_index_out_of_bounds_after(self
):
1996 with self
.assertRaises(IndexError):
1997 self
._def
.member_at_index(len(self
._def
_value
))
2000 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2002 field
['B'] = 'salut'
2006 field
['F'] = {'F_1': 52}
2007 self
.assertEqual(self
._def
, field
)
2009 def test_const_eq(self
):
2010 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2012 field
['B'] = 'salut'
2016 field
['F'] = {'F_1': 52}
2017 self
.assertEqual(self
._def
_const
, field
)
2019 def test_eq_invalid_type(self
):
2020 self
.assertNotEqual(self
._def
, 23)
2022 def test_eq_diff_len(self
):
2023 fc
= self
._tc
.create_structure_field_class()
2024 fc
.append_member('A', self
._fc
0_fn
())
2025 fc
.append_member('B', self
._fc
1_fn
())
2026 fc
.append_member('C', self
._fc
2_fn
())
2028 field
= _create_field(self
._tc
, fc
)
2030 field
['B'] = 'salut'
2032 self
.assertNotEqual(self
._def
, field
)
2034 def test_eq_diff_keys(self
):
2035 fc
= self
._tc
.create_structure_field_class()
2036 fc
.append_member('U', self
._fc
0_fn
())
2037 fc
.append_member('V', self
._fc
1_fn
())
2038 fc
.append_member('W', self
._fc
2_fn
())
2039 fc
.append_member('X', self
._fc
3_fn
())
2040 fc
.append_member('Y', self
._fc
4_fn
())
2041 fc
.append_member('Z', self
._fc
5_fn
())
2042 field
= _create_field(self
._tc
, fc
)
2044 field
['V'] = "gerry"
2049 self
.assertNotEqual(self
._def
, field
)
2051 def test_eq_diff_content_same_len(self
):
2052 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2054 field
['B'] = 'salut'
2058 field
['F'] = {'F_1': 0}
2059 self
.assertNotEqual(self
._def
, field
)
2061 def test_eq_same_content_diff_keys(self
):
2062 fc
= self
._tc
.create_structure_field_class()
2063 fc
.append_member('A', self
._fc
0_fn
())
2064 fc
.append_member('B', self
._fc
1_fn
())
2065 fc
.append_member('E', self
._fc
2_fn
())
2066 fc
.append_member('D', self
._fc
3_fn
())
2067 fc
.append_member('C', self
._fc
4_fn
())
2068 fc
.append_member('F', self
._fc
5_fn
())
2069 field
= _create_field(self
._tc
, fc
)
2071 field
['B'] = 'salut'
2076 self
.assertNotEqual(self
._def
, field
)
2078 def test_setitem(self
):
2079 self
._def
['C'] = -18.47
2080 self
.assertEqual(self
._def
['C'], -18.47)
2082 def test_const_setitem(self
):
2083 with self
.assertRaises(TypeError):
2084 self
._def
_const
['A'] = 134679
2086 def test_setitem_int_field(self
):
2087 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2088 int_field
= _create_field(self
._tc
, int_fc
)
2089 int_field
.value
= 19487
2090 self
._def
['D'] = int_field
2091 self
.assertEqual(self
._def
['D'], 19487)
2093 def test_setitem_non_basic_field(self
):
2094 elem_fc
= self
._tc
.create_structure_field_class()
2095 struct_fc
= self
._tc
.create_structure_field_class()
2096 struct_fc
.append_member('A', elem_fc
)
2097 struct_field
= _create_field(self
._tc
, struct_fc
)
2099 # Will fail on access to .items() of the value
2100 with self
.assertRaises(AttributeError):
2101 struct_field
['A'] = 23
2103 def test_setitem_none(self
):
2104 with self
.assertRaises(TypeError):
2105 self
._def
['C'] = None
2107 def test_setitem_key_wrong_type(self
):
2108 with self
.assertRaises(TypeError):
2111 def test_setitem_wrong_key(self
):
2112 with self
.assertRaises(KeyError):
2113 self
._def
['hi'] = 134679
2115 def test_member_at_index(self
):
2116 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
2118 def test_const_member_at_index(self
):
2119 self
.assertEqual(self
._def
_const
.member_at_index(1), 'salut')
2121 def test_iter(self
):
2131 for vkey
, vval
in self
._def
.items():
2132 val
= orig_values
[vkey
]
2133 self
.assertEqual(vval
, val
)
2135 def test_value(self
):
2144 self
.assertEqual(self
._def
, orig_values
)
2146 def test_set_value(self
):
2147 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2148 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
2149 str_fc
= self
._tc
.create_string_field_class()
2150 struct_fc
= self
._tc
.create_structure_field_class()
2151 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
2152 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
2153 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
2154 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2156 struct
= _create_field(self
._tc
, struct_fc
)
2157 struct
.value
= values
2158 self
.assertEqual(values
, struct
)
2160 bad_type_values
= copy
.deepcopy(values
)
2161 bad_type_values
['an_int'] = 'a string'
2162 with self
.assertRaises(TypeError):
2163 struct
.value
= bad_type_values
2165 unknown_key_values
= copy
.deepcopy(values
)
2166 unknown_key_values
['unknown_key'] = 16546
2167 with self
.assertRaises(KeyError):
2168 struct
.value
= unknown_key_values
2170 def test_str_op(self
):
2171 expected_string_found
= False
2173 # Establish all permutations of the three expected matches since
2174 # the order in which mappings are enumerated is not explicitly part of
2176 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
2177 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
2178 candidate
= '{{{}}}'.format(', '.join(items
))
2180 expected_string_found
= True
2183 self
.assertTrue(expected_string_found
)
2186 class OptionFieldTestCase(unittest
.TestCase
):
2188 def _const_value_setter(field
):
2189 field
.value
= {'opt_field': 'hiboux'}
2191 def _create_fc(self
, tc
):
2192 fc
= tc
.create_option_without_selector_field_class(
2193 tc
.create_string_field_class()
2195 top_fc
= tc
.create_structure_field_class()
2196 top_fc
.append_member('opt_field', fc
)
2200 self
._tc
= get_default_trace_class()
2201 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2202 self
._def
= fld
['opt_field']
2203 self
._def
_value
= 'hiboux'
2204 self
._def
_const
= create_const_field(
2205 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
2208 def test_value_prop(self
):
2209 self
._def
.value
= 'hiboux'
2210 self
.assertEqual(self
._def
.field
, 'hiboux')
2211 self
.assertIs(type(self
._def
), bt2
._OptionField
)
2212 self
.assertIs(type(self
._def
.field
), bt2
._StringField
)
2213 self
.assertTrue(self
._def
.has_field
)
2215 def test_const_value_prop(self
):
2216 self
.assertEqual(self
._def
_const
.field
, 'hiboux')
2217 self
.assertIs(type(self
._def
_const
), bt2
._OptionFieldConst
)
2218 self
.assertIs(type(self
._def
_const
.field
), bt2
._StringFieldConst
)
2219 self
.assertTrue(self
._def
_const
.has_field
)
2221 def test_has_field_prop_true(self
):
2222 self
._def
.has_field
= True
2223 self
.assertTrue(self
._def
.has_field
)
2225 def test_has_field_prop_false(self
):
2226 self
._def
.has_field
= False
2227 self
.assertFalse(self
._def
.has_field
)
2229 def test_bool_op_true(self
):
2230 self
._def
.value
= 'allo'
2231 self
.assertTrue(self
._def
)
2233 def test_bool_op_false(self
):
2234 self
._def
.has_field
= False
2235 self
.assertFalse(self
._def
)
2237 def test_field_prop_existing(self
):
2238 self
._def
.value
= 'meow'
2239 field
= self
._def
.field
2240 self
.assertEqual(field
, 'meow')
2242 def test_field_prop_none(self
):
2243 self
._def
.has_field
= False
2244 field
= self
._def
.field
2245 self
.assertIsNone(field
)
2247 def test_const_field_prop(self
):
2248 with self
.assertRaises(AttributeError):
2249 self
._def
_const
.has_field
= False
2251 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2252 self
.assertTrue(self
._def
_const
.has_field
)
2254 def test_field_prop_existing_then_none(self
):
2255 self
._def
.value
= 'meow'
2256 field
= self
._def
.field
2257 self
.assertEqual(field
, 'meow')
2258 self
._def
.has_field
= False
2259 field
= self
._def
.field
2260 self
.assertIsNone(field
)
2263 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2264 field
= field
['opt_field']
2265 field
.value
= 'walk'
2266 self
._def
.value
= 'walk'
2267 self
.assertEqual(self
._def
, field
)
2269 def test_const_eq(self
):
2270 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2271 field
= field
['opt_field']
2272 field
.value
= 'hiboux'
2273 self
.assertEqual(self
._def
_const
, field
)
2274 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2276 def test_eq_invalid_type(self
):
2277 self
._def
.value
= 'gerry'
2278 self
.assertNotEqual(self
._def
, 23)
2280 def test_str_op(self
):
2281 self
._def
.value
= 'marcel'
2282 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
2284 def test_repr_op(self
):
2285 self
._def
.value
= 'mireille'
2286 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
2289 class VariantFieldTestCase(unittest
.TestCase
):
2291 def _const_value_setter(field
):
2292 field
.selected_option_index
= 3
2295 def _create_fc(self
, tc
):
2296 ft0
= tc
.create_signed_integer_field_class(32)
2297 ft1
= tc
.create_string_field_class()
2298 ft2
= tc
.create_double_precision_real_field_class()
2299 ft3
= tc
.create_signed_integer_field_class(17)
2300 fc
= tc
.create_variant_field_class()
2301 fc
.append_option('corner', ft0
)
2302 fc
.append_option('zoom', ft1
)
2303 fc
.append_option('mellotron', ft2
)
2304 fc
.append_option('giorgio', ft3
)
2305 top_fc
= tc
.create_structure_field_class()
2306 top_fc
.append_member('variant_field', fc
)
2310 self
._tc
= get_default_trace_class()
2311 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2312 self
._def
= fld
['variant_field']
2314 self
._def
_value
= 1334
2315 self
._def
_selected
_index
= 3
2316 const_fc
= self
._create
_fc
(self
._tc
)['variant_field']
2318 fld_const
= create_const_field(
2319 self
._tc
, const_fc
.field_class
, self
._const
_value
_setter
2321 self
._def
_const
= fld_const
2323 def test_bool_op(self
):
2324 self
._def
.selected_option_index
= 2
2325 self
._def
.value
= -17.34
2326 with self
.assertRaises(NotImplementedError):
2329 def test_selected_option_index(self
):
2330 self
._def
.selected_option_index
= 2
2331 self
.assertEqual(self
._def
.selected_option_index
, 2)
2333 def test_selected_option_index_above_range(self
):
2334 with self
.assertRaises(IndexError):
2335 self
._def
.selected_option_index
= 4
2337 def test_selected_option_index_below_range(self
):
2338 with self
.assertRaises(IndexError):
2339 self
._def
.selected_option_index
= -1
2341 def test_const_selected_option_index(self
):
2342 with self
.assertRaises(AttributeError):
2343 self
._def
_const
.selected_option_index
= 2
2344 self
.assertEqual(self
._def
_const
.selected_option_index
, 3)
2346 def test_selected_option(self
):
2347 self
._def
.selected_option_index
= 2
2348 self
._def
.value
= -17.34
2349 self
.assertEqual(self
._def
.selected_option
, -17.34)
2350 self
.assertEqual(type(self
._def
.selected_option
), bt2
._DoublePrecisionRealField
)
2352 self
._def
.selected_option_index
= 3
2353 self
._def
.value
= 1921
2354 self
.assertEqual(self
._def
.selected_option
, 1921)
2355 self
.assertEqual(type(self
._def
.selected_option
), bt2
._SignedIntegerField
)
2357 def test_const_selected_option(self
):
2358 self
.assertEqual(self
._def
_const
.selected_option
, 1334)
2360 type(self
._def
_const
.selected_option
), bt2
._SignedIntegerFieldConst
2364 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2365 field
= field
['variant_field']
2366 field
.selected_option_index
= 0
2368 self
._def
.selected_option_index
= 0
2369 self
._def
.value
= 1774
2370 self
.assertEqual(self
._def
, field
)
2372 def test_const_eq(self
):
2373 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2374 field
= field
['variant_field']
2375 field
.selected_option_index
= 3
2377 self
.assertEqual(self
._def
_const
, field
)
2380 self
.assertEqual(len(self
._def
), 4)
2382 def test_eq_invalid_type(self
):
2383 self
._def
.selected_option_index
= 1
2384 self
._def
.value
= 'gerry'
2385 self
.assertNotEqual(self
._def
, 23)
2387 def test_str_op_int(self
):
2388 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2389 field
= field
['variant_field']
2390 field
.selected_option_index
= 0
2392 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2393 other_field
= other_field
['variant_field']
2394 other_field
.selected_option_index
= 0
2395 other_field
.value
= 1774
2396 self
.assertEqual(str(field
), str(other_field
))
2398 def test_str_op_str(self
):
2399 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2400 field
= field
['variant_field']
2401 field
.selected_option_index
= 1
2402 field
.value
= 'un beau grand bateau'
2403 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2404 other_field
= other_field
['variant_field']
2405 other_field
.selected_option_index
= 1
2406 other_field
.value
= 'un beau grand bateau'
2407 self
.assertEqual(str(field
), str(other_field
))
2409 def test_str_op_float(self
):
2410 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2411 field
= field
['variant_field']
2412 field
.selected_option_index
= 2
2413 field
.value
= 14.4245
2414 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2415 other_field
= other_field
['variant_field']
2416 other_field
.selected_option_index
= 2
2417 other_field
.value
= 14.4245
2418 self
.assertEqual(str(field
), str(other_field
))
2421 if __name__
== '__main__':