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
, TestOutputPortMessageIterator
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 const field of the given field class.
68 # The field is part of a dummy stream, itself part of a dummy trace created
69 # from trace class `tc`.
72 def _create_const_field(tc
, field_class
, field_value_setter_fn
):
73 field_name
= 'const field'
75 class MyIter(bt2
._UserMessageIterator
):
76 def __init__(self
, self_port_output
):
78 nonlocal field_value_setter_fn
79 stream
= _create_stream(tc
, [(field_name
, field_class
)])
80 packet
= stream
.create_packet()
82 field_value_setter_fn(packet
.context_field
[field_name
])
85 self
._create
_stream
_beginning
_message
(stream
),
86 self
._create
_packet
_beginning
_message
(packet
),
90 if len(self
._msgs
) == 0:
93 return self
._msgs
.pop(0)
95 class MySrc(bt2
._UserSourceComponent
, message_iterator_class
=MyIter
):
96 def __init__(self
, config
, params
, obj
):
97 self
._add
_output
_port
('out', params
)
100 src_comp
= graph
.add_component(MySrc
, 'my_source', None)
101 msg_iter
= TestOutputPortMessageIterator(graph
, src_comp
.output_ports
['out'])
103 # Ignore first message, stream beginning
105 packet_beg_msg
= next(msg_iter
)
107 return packet_beg_msg
.packet
.context_field
[field_name
]
110 # Create a field of type string.
112 # The field is part of a dummy stream, itself part of a dummy trace created
113 # from trace class `tc`. It is made out of a dummy string field class.
116 def _create_string_field(tc
):
117 field_name
= 'string_field'
118 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
119 packet
= stream
.create_packet()
120 return packet
.context_field
[field_name
]
123 # Create a field of type static array of ints.
125 # The field is part of a dummy stream, itself part of a dummy trace created
126 # from trace class `tc`. It is made out of a dummy static array field class,
127 # with a dummy integer field class as element class.
130 def _create_int_array_field(tc
, length
):
131 elem_fc
= tc
.create_signed_integer_field_class(32)
132 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
133 field_name
= 'int_array'
134 stream
= _create_stream(tc
, [(field_name
, fc
)])
135 packet
= stream
.create_packet()
136 return packet
.context_field
[field_name
]
139 # Create a field of type dynamic array of ints.
141 # The field is part of a dummy stream, itself part of a dummy trace created
142 # from trace class `tc`. It is made out of a dummy static array field class,
143 # with a dummy integer field class as element and length classes.
146 def _create_dynamic_array(tc
):
147 elem_fc
= tc
.create_signed_integer_field_class(32)
148 len_fc
= tc
.create_signed_integer_field_class(32)
149 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
150 field_name
= 'int_dyn_array'
151 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
152 packet
= stream
.create_packet()
153 packet
.context_field
[field_name
].length
= 3
154 return packet
.context_field
[field_name
]
157 # Create a field of type array of (empty) structures.
159 # The field is part of a dummy stream, itself part of a dummy trace created
160 # from trace class `tc`. It is made out of a dummy static array field class,
161 # with a dummy struct field class as element class.
164 def _create_struct_array_field(tc
, length
):
165 elem_fc
= tc
.create_structure_field_class()
166 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
167 field_name
= 'struct_array'
168 stream
= _create_stream(tc
, [(field_name
, fc
)])
169 packet
= stream
.create_packet()
170 return packet
.context_field
[field_name
]
173 class BitArrayFieldTestCase(unittest
.TestCase
):
174 def _create_field(self
):
175 return _create_field(self
._tc
, self
._tc
.create_bit_array_field_class(24))
178 self
._tc
= get_default_trace_class()
179 self
._def
_value
= 15497
180 self
._def
= self
._create
_field
()
181 self
._def
.value_as_integer
= self
._def
_value
182 self
._def
_new
_value
= 101542
184 def test_assign_invalid_type(self
):
185 with self
.assertRaises(TypeError):
186 self
._def
.value_as_integer
= 'onze'
188 def test_assign(self
):
189 self
._def
.value_as_integer
= 199
190 self
.assertEqual(self
._def
.value_as_integer
, 199)
192 def test_assign_masked(self
):
193 self
._def
.value_as_integer
= 0xE1549BB
194 self
.assertEqual(self
._def
.value_as_integer
, 0xE1549BB & ((1 << 24) - 1))
197 other
= self
._create
_field
()
198 other
.value_as_integer
= self
._def
_value
199 self
.assertEqual(self
._def
, other
)
201 def test_ne_same_type(self
):
202 other
= self
._create
_field
()
203 other
.value_as_integer
= self
._def
_value
- 1
204 self
.assertNotEqual(self
._def
, other
)
206 def test_ne_diff_type(self
):
207 self
.assertNotEqual(self
._def
, self
._def
_value
)
210 self
.assertEqual(len(self
._def
), 24)
213 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
216 self
.assertEqual(repr(self
._def
), repr(self
._def
_value
))
219 # Base class for numeric field test cases.
221 # To be compatible with this base class, a derived class must, in its
224 # * Set `self._def` to a field object with an arbitrary value.
225 # * Set `self._def_value` to the equivalent value of `self._def`.
226 class _TestNumericField
:
227 # Tries the binary operation `op`:
229 # 1. Between `self._def`, which is a field object, and `rhs`.
230 # 2. Between `self._def_value`, which is the raw value of
231 # `self._def`, and `rhs`.
233 # Returns the results of 1. and 2.
235 # If there's an exception while performing 1. or 2., asserts that
236 # both operations raised exceptions, that both exceptions have the
237 # same type, and returns `None` for both results.
238 def _binop(self
, op
, rhs
):
243 # try with field object
245 r
= op(self
._def
, rhs
)
246 except Exception as e
:
251 rv
= op(self
._def
_value
, comp_value
)
252 except Exception as e
:
255 if type_rexc
is not None or type_rvexc
is not None:
256 # at least one of the operations raised an exception: in
257 # this case both operations should have raised the same
258 # type of exception (division by zero, bit shift with a
259 # floating point number operand, etc.)
260 self
.assertIs(type_rexc
, type_rvexc
)
265 # Tries the unary operation `op`:
267 # 1. On `self._def`, which is a field object.
268 # 2. On `self._def_value`, which is the value of `self._def`.
270 # Returns the results of 1. and 2.
272 # If there's an exception while performing 1. or 2., asserts that
273 # both operations raised exceptions, that both exceptions have the
274 # same type, and returns `None` for both results.
275 def _unaryop(self
, op
):
279 # try with field object
282 except Exception as e
:
287 rv
= op(self
._def
_value
)
288 except Exception as e
:
291 if type_rexc
is not None or type_rvexc
is not None:
292 # at least one of the operations raised an exception: in
293 # this case both operations should have raised the same
294 # type of exception (division by zero, bit shift with a
295 # floating point number operand, etc.)
296 self
.assertIs(type_rexc
, type_rvexc
)
301 # Tests that the unary operation `op` gives results with the same
302 # type for both `self._def` and `self._def_value`.
303 def _test_unaryop_type(self
, op
):
304 r
, rv
= self
._unaryop
(op
)
309 self
.assertIsInstance(r
, type(rv
))
311 # Tests that the unary operation `op` gives results with the same
312 # value for both `self._def` and `self._def_value`. This uses the
313 # __eq__() operator of `self._def`.
314 def _test_unaryop_value(self
, op
):
315 r
, rv
= self
._unaryop
(op
)
320 self
.assertEqual(r
, rv
)
322 # Tests that the unary operation `op`, when applied to `self._def`,
323 # does not change its underlying BT object address.
324 def _test_unaryop_addr_same(self
, op
):
325 addr_before
= self
._def
.addr
327 self
.assertEqual(self
._def
.addr
, addr_before
)
329 # Tests that the unary operation `op`, when applied to `self._def`,
330 # does not change its value.
331 def _test_unaryop_value_same(self
, op
):
332 value_before
= copy
.copy(self
._def
_value
)
334 self
.assertEqual(self
._def
, value_before
)
336 # Tests that the binary operation `op` gives results with the same
337 # type for both `self._def` and `self._def_value`.
338 def _test_binop_type(self
, op
, rhs
):
339 r
, rv
= self
._binop
(op
, rhs
)
344 if op
in _COMP_BINOPS
:
345 # __eq__() and __ne__() always return a 'bool' object
346 self
.assertIsInstance(r
, bool)
348 self
.assertIsInstance(r
, type(rv
))
350 # Tests that the binary operation `op` gives results with the same
351 # value for both `self._def` and `self._def_value`. This uses the
352 # __eq__() operator of `self._def`.
353 def _test_binop_value(self
, op
, rhs
):
354 r
, rv
= self
._binop
(op
, rhs
)
359 self
.assertEqual(r
, rv
)
361 # Tests that the binary operation `op`, when applied to `self._def`,
362 # does not change its underlying BT object address.
363 def _test_binop_lhs_addr_same(self
, op
, rhs
):
364 addr_before
= self
._def
.addr
365 r
, rv
= self
._binop
(op
, rhs
)
366 self
.assertEqual(self
._def
.addr
, addr_before
)
368 # Tests that the binary operation `op`, when applied to `self._def`,
369 # does not change its value.
370 @unittest.skip('copy is not implemented')
371 def _test_binop_lhs_value_same(self
, op
, rhs
):
372 value_before
= copy
.copy(self
._def
)
373 r
, rv
= self
._binop
(op
, rhs
)
374 self
.assertEqual(self
._def
, value_before
)
376 # The methods below which take the `test_cb` and/or `op` parameters
377 # are meant to be used with one of the _test_binop_*() functions
378 # above as `test_cb` and a binary operator function as `op`.
382 # self._test_binop_rhs_pos_int(self._test_binop_value,
385 # This tests that a numeric field object added to a positive integer
386 # value gives a result with the expected value.
388 # `vint` and `vfloat` mean a signed integer value object and a real
391 def _test_binop_invalid_unknown(self
, op
):
392 if op
in _COMP_BINOPS
:
393 self
.skipTest('not testing')
398 with self
.assertRaises(TypeError):
401 def _test_binop_invalid_none(self
, op
):
402 if op
in _COMP_BINOPS
:
403 self
.skipTest('not testing')
405 with self
.assertRaises(TypeError):
408 def _test_binop_rhs_false(self
, test_cb
, op
):
411 def _test_binop_rhs_true(self
, test_cb
, op
):
414 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
417 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
420 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
423 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
424 test_cb(op
, bt2
.create_value(2))
426 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
427 test_cb(op
, bt2
.create_value(-23))
429 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
430 test_cb(op
, bt2
.create_value(0))
432 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
435 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
438 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
441 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
442 test_cb(op
, bt2
.create_value(2.2))
444 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
445 test_cb(op
, bt2
.create_value(-23.4))
447 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
448 test_cb(op
, bt2
.create_value(0.0))
450 def _test_binop_rhs_complex(self
, test_cb
, op
):
451 test_cb(op
, -23 + 19j
)
453 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
456 def _test_binop_type_false(self
, op
):
457 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
459 def _test_binop_type_true(self
, op
):
460 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
462 def _test_binop_type_pos_int(self
, op
):
463 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
465 def _test_binop_type_neg_int(self
, op
):
466 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
468 def _test_binop_type_zero_int(self
, op
):
469 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
471 def _test_binop_type_pos_vint(self
, op
):
472 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
474 def _test_binop_type_neg_vint(self
, op
):
475 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
477 def _test_binop_type_zero_vint(self
, op
):
478 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
480 def _test_binop_type_pos_float(self
, op
):
481 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
483 def _test_binop_type_neg_float(self
, op
):
484 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
486 def _test_binop_type_zero_float(self
, op
):
487 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
489 def _test_binop_type_pos_vfloat(self
, op
):
490 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
492 def _test_binop_type_neg_vfloat(self
, op
):
493 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
495 def _test_binop_type_zero_vfloat(self
, op
):
496 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
498 def _test_binop_type_complex(self
, op
):
499 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
501 def _test_binop_type_zero_complex(self
, op
):
502 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
504 def _test_binop_value_false(self
, op
):
505 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
507 def _test_binop_value_true(self
, op
):
508 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
510 def _test_binop_value_pos_int(self
, op
):
511 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
513 def _test_binop_value_neg_int(self
, op
):
514 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
516 def _test_binop_value_zero_int(self
, op
):
517 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
519 def _test_binop_value_pos_vint(self
, op
):
520 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
522 def _test_binop_value_neg_vint(self
, op
):
523 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
525 def _test_binop_value_zero_vint(self
, op
):
526 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
528 def _test_binop_value_pos_float(self
, op
):
529 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
531 def _test_binop_value_neg_float(self
, op
):
532 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
534 def _test_binop_value_zero_float(self
, op
):
535 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
537 def _test_binop_value_pos_vfloat(self
, op
):
538 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
540 def _test_binop_value_neg_vfloat(self
, op
):
541 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
543 def _test_binop_value_zero_vfloat(self
, op
):
544 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
546 def _test_binop_value_complex(self
, op
):
547 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
549 def _test_binop_value_zero_complex(self
, op
):
550 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
552 def _test_binop_lhs_addr_same_false(self
, op
):
553 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
555 def _test_binop_lhs_addr_same_true(self
, op
):
556 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
558 def _test_binop_lhs_addr_same_pos_int(self
, op
):
559 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
561 def _test_binop_lhs_addr_same_neg_int(self
, op
):
562 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
564 def _test_binop_lhs_addr_same_zero_int(self
, op
):
565 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
567 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
568 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
570 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
571 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
573 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
574 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
576 def _test_binop_lhs_addr_same_pos_float(self
, op
):
577 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
579 def _test_binop_lhs_addr_same_neg_float(self
, op
):
580 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
582 def _test_binop_lhs_addr_same_zero_float(self
, op
):
583 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
585 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
586 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
588 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
589 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
591 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
592 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
594 def _test_binop_lhs_addr_same_complex(self
, op
):
595 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
597 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
598 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
600 def _test_binop_lhs_value_same_false(self
, op
):
601 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
603 def _test_binop_lhs_value_same_true(self
, op
):
604 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
606 def _test_binop_lhs_value_same_pos_int(self
, op
):
607 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
609 def _test_binop_lhs_value_same_neg_int(self
, op
):
610 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
612 def _test_binop_lhs_value_same_zero_int(self
, op
):
613 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
615 def _test_binop_lhs_value_same_pos_vint(self
, op
):
616 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
618 def _test_binop_lhs_value_same_neg_vint(self
, op
):
619 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
621 def _test_binop_lhs_value_same_zero_vint(self
, op
):
622 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
624 def _test_binop_lhs_value_same_pos_float(self
, op
):
625 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
627 def _test_binop_lhs_value_same_neg_float(self
, op
):
628 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
630 def _test_binop_lhs_value_same_zero_float(self
, op
):
631 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
633 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
634 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
636 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
637 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
639 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
640 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
642 def _test_binop_lhs_value_same_complex(self
, op
):
643 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
645 def _test_binop_lhs_value_same_zero_complex(self
, op
):
646 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
648 def test_bool_op(self
):
649 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
651 def test_int_op(self
):
652 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
654 def test_float_op(self
):
655 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
657 def test_complex_op(self
):
658 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
660 def test_str_op(self
):
661 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
663 def test_hash_op(self
):
664 with self
.assertRaises(TypeError):
667 def test_const_hash_op(self
):
668 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
670 def test_const_hash_dict(self
):
672 my_dict
[self
._def
_const
] = 'my_value'
673 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
675 def test_eq_none(self
):
676 # Ignore this lint error:
677 # E711 comparison to None should be 'if cond is None:'
678 # since this is what we want to test (even though not good practice).
679 self
.assertFalse(self
._def
== None) # noqa: E711
681 def test_ne_none(self
):
682 # Ignore this lint error:
683 # E711 comparison to None should be 'if cond is not None:'
684 # since this is what we want to test (even though not good practice).
685 self
.assertTrue(self
._def
!= None) # noqa: E711
688 # This is a list of binary operators used for
689 # _inject_numeric_testing_methods().
691 # Each entry is a pair of binary operator name (used as part of the
692 # created testing method's name) and operator function.
700 ('add', operator
.add
),
701 ('radd', lambda a
, b
: operator
.add(b
, a
)),
702 ('and', operator
.and_
),
703 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
704 ('floordiv', operator
.floordiv
),
705 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
706 ('lshift', operator
.lshift
),
707 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
708 ('mod', operator
.mod
),
709 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
710 ('mul', operator
.mul
),
711 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
712 ('or', operator
.or_
),
713 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
714 ('pow', operator
.pow),
715 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
716 ('rshift', operator
.rshift
),
717 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
718 ('sub', operator
.sub
),
719 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
720 ('truediv', operator
.truediv
),
721 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
722 ('xor', operator
.xor
),
723 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
727 # This is a list of unary operators used for
728 # _inject_numeric_testing_methods().
730 # Each entry is a pair of unary operator name (used as part of the
731 # created testing method's name) and operator function.
733 ('neg', operator
.neg
),
734 ('pos', operator
.pos
),
735 ('abs', operator
.abs),
736 ('invert', operator
.invert
),
738 ('round_0', partial(round, ndigits
=0)),
739 ('round_1', partial(round, ndigits
=1)),
740 ('round_2', partial(round, ndigits
=2)),
741 ('round_3', partial(round, ndigits
=3)),
743 ('floor', math
.floor
),
744 ('trunc', math
.trunc
),
748 # This function injects a bunch of testing methods to a numeric
751 # It is meant to be used like this:
753 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
755 # This function injects:
757 # * One testing method for each _TestNumericField._test_binop_*()
758 # method, for each binary operator in the _BINOPS tuple.
760 # * One testing method for each _TestNumericField._test_unaryop*()
761 # method, for each unary operator in the _UNARYOPS tuple.
762 def _inject_numeric_testing_methods(cls
):
763 def test_binop_name(suffix
):
764 return 'test_binop_{}_{}'.format(name
, suffix
)
766 def test_unaryop_name(suffix
):
767 return 'test_unaryop_{}_{}'.format(name
, suffix
)
769 # inject testing methods for each binary operation
770 for name
, binop
in _BINOPS
:
773 test_binop_name('invalid_unknown'),
774 partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
),
778 test_binop_name('invalid_none'),
779 partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
),
783 test_binop_name('type_true'),
784 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
788 test_binop_name('type_pos_int'),
789 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
793 test_binop_name('type_pos_vint'),
794 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
798 test_binop_name('value_true'),
799 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
803 test_binop_name('value_pos_int'),
804 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
808 test_binop_name('value_pos_vint'),
809 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
813 test_binop_name('lhs_addr_same_true'),
814 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
818 test_binop_name('lhs_addr_same_pos_int'),
820 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
825 test_binop_name('lhs_addr_same_pos_vint'),
827 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
832 test_binop_name('lhs_value_same_true'),
833 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
837 test_binop_name('lhs_value_same_pos_int'),
839 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
844 test_binop_name('lhs_value_same_pos_vint'),
846 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
851 test_binop_name('type_neg_int'),
852 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
856 test_binop_name('type_neg_vint'),
857 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
861 test_binop_name('value_neg_int'),
862 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
866 test_binop_name('value_neg_vint'),
867 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
871 test_binop_name('lhs_addr_same_neg_int'),
873 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
878 test_binop_name('lhs_addr_same_neg_vint'),
880 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
885 test_binop_name('lhs_value_same_neg_int'),
887 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
892 test_binop_name('lhs_value_same_neg_vint'),
894 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
899 test_binop_name('type_false'),
900 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
904 test_binop_name('type_zero_int'),
905 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
909 test_binop_name('type_zero_vint'),
910 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
914 test_binop_name('value_false'),
915 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
919 test_binop_name('value_zero_int'),
920 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
924 test_binop_name('value_zero_vint'),
925 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
929 test_binop_name('lhs_addr_same_false'),
930 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
934 test_binop_name('lhs_addr_same_zero_int'),
936 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
941 test_binop_name('lhs_addr_same_zero_vint'),
943 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
948 test_binop_name('lhs_value_same_false'),
949 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
953 test_binop_name('lhs_value_same_zero_int'),
955 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
960 test_binop_name('lhs_value_same_zero_vint'),
962 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
967 test_binop_name('type_pos_float'),
968 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
972 test_binop_name('type_neg_float'),
973 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
977 test_binop_name('type_pos_vfloat'),
978 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
982 test_binop_name('type_neg_vfloat'),
983 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
987 test_binop_name('value_pos_float'),
988 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
992 test_binop_name('value_neg_float'),
993 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
997 test_binop_name('value_pos_vfloat'),
998 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
1002 test_binop_name('value_neg_vfloat'),
1003 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
1007 test_binop_name('lhs_addr_same_pos_float'),
1009 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
1014 test_binop_name('lhs_addr_same_neg_float'),
1016 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
1021 test_binop_name('lhs_addr_same_pos_vfloat'),
1023 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
1028 test_binop_name('lhs_addr_same_neg_vfloat'),
1030 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
1035 test_binop_name('lhs_value_same_pos_float'),
1037 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
1042 test_binop_name('lhs_value_same_neg_float'),
1044 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
1049 test_binop_name('lhs_value_same_pos_vfloat'),
1051 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
1056 test_binop_name('lhs_value_same_neg_vfloat'),
1058 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
1063 test_binop_name('type_zero_float'),
1064 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
1068 test_binop_name('type_zero_vfloat'),
1069 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
1073 test_binop_name('value_zero_float'),
1074 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
1078 test_binop_name('value_zero_vfloat'),
1079 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
1083 test_binop_name('lhs_addr_same_zero_float'),
1085 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
1090 test_binop_name('lhs_addr_same_zero_vfloat'),
1092 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
1097 test_binop_name('lhs_value_same_zero_float'),
1099 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1104 test_binop_name('lhs_value_same_zero_vfloat'),
1106 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1111 test_binop_name('type_complex'),
1112 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1116 test_binop_name('type_zero_complex'),
1117 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1121 test_binop_name('value_complex'),
1122 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1126 test_binop_name('value_zero_complex'),
1127 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1131 test_binop_name('lhs_addr_same_complex'),
1133 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1138 test_binop_name('lhs_addr_same_zero_complex'),
1140 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1145 test_binop_name('lhs_value_same_complex'),
1147 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1152 test_binop_name('lhs_value_same_zero_complex'),
1154 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1158 # inject testing methods for each unary operation
1159 for name
, unaryop
in _UNARYOPS
:
1162 test_unaryop_name('type'),
1163 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1167 test_unaryop_name('value'),
1168 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1172 test_unaryop_name('addr_same'),
1173 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1177 test_unaryop_name('value_same'),
1178 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1182 class BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1184 def _const_value_setter(field
):
1187 def _create_fc(self
, tc
):
1188 return tc
.create_bool_field_class()
1191 self
._tc
= get_default_trace_class()
1192 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1193 self
._def
.value
= True
1194 self
._def
_value
= True
1195 self
._def
_const
= _create_const_field(
1196 self
._tc
, self
._tc
.create_bool_field_class(), self
._const
_value
_setter
1198 self
._def
_new
_value
= False
1200 def test_classes(self
):
1201 self
.assertIs(type(self
._def
), bt2
._BoolField
)
1202 self
.assertIs(type(self
._def
_const
), bt2
._BoolFieldConst
)
1204 def test_assign_true(self
):
1206 self
._def
.value
= raw
1207 self
.assertEqual(self
._def
, raw
)
1209 def test_assign_false(self
):
1211 self
._def
.value
= raw
1212 self
.assertEqual(self
._def
, raw
)
1214 def test_assign_field_true(self
):
1215 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1218 self
._def
.value
= field
1219 self
.assertEqual(self
._def
, raw
)
1221 def test_assign_field_false(self
):
1222 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1225 self
._def
.value
= field
1226 self
.assertEqual(self
._def
, raw
)
1228 def test_assign_invalid_type(self
):
1229 with self
.assertRaises(TypeError):
1230 self
._def
.value
= 17
1232 def test_str_op(self
):
1233 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1236 _inject_numeric_testing_methods(BoolFieldTestCase
)
1239 class _TestIntegerFieldCommon(_TestNumericField
):
1240 def test_assign_true(self
):
1242 self
._def
.value
= raw
1243 self
.assertEqual(self
._def
, raw
)
1245 def test_assign_false(self
):
1247 self
._def
.value
= raw
1248 self
.assertEqual(self
._def
, raw
)
1250 def test_assign_pos_int(self
):
1252 self
._def
.value
= raw
1253 self
.assertEqual(self
._def
, raw
)
1255 def test_assign_neg_int(self
):
1257 self
._def
.value
= raw
1258 self
.assertEqual(self
._def
, raw
)
1260 def test_assign_int_field(self
):
1262 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1264 self
._def
.value
= field
1265 self
.assertEqual(self
._def
, raw
)
1267 def test_assign_invalid_type(self
):
1268 with self
.assertRaises(TypeError):
1269 self
._def
.value
= 'yes'
1271 def test_assign_uint(self
):
1272 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1273 field
= _create_field(self
._tc
, uint_fc
)
1276 self
.assertEqual(field
, raw
)
1278 def test_assign_big_uint(self
):
1279 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1280 field
= _create_field(self
._tc
, uint_fc
)
1281 # Larger than the IEEE 754 double-precision exact representation of
1284 field
.value
= (2 ** 53) + 1
1285 self
.assertEqual(field
, raw
)
1287 def test_assign_uint_out_of_range(self
):
1288 uint_fc
= self
._tc
.create_unsigned_integer_field_class(8)
1289 field
= _create_field(self
._tc
, uint_fc
)
1291 with self
.assertRaises(ValueError) as ctx
:
1294 str(ctx
.exception
), 'Value 256 is outside valid range [0, 255]'
1297 with self
.assertRaises(ValueError) as ctx
:
1299 self
.assertEqual(str(ctx
.exception
), 'Value -1 is outside valid range [0, 255]')
1301 def test_assign_int_out_of_range(self
):
1302 int_fc
= self
._tc
.create_signed_integer_field_class(8)
1303 field
= _create_field(self
._tc
, int_fc
)
1305 with self
.assertRaises(ValueError) as ctx
:
1308 str(ctx
.exception
), 'Value 128 is outside valid range [-128, 127]'
1311 with self
.assertRaises(ValueError) as ctx
:
1314 str(ctx
.exception
), 'Value -129 is outside valid range [-128, 127]'
1317 def test_str_op(self
):
1318 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1321 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1324 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1326 def _const_value_setter(field
):
1329 def _create_fc(self
, tc
):
1330 return tc
.create_signed_integer_field_class(25)
1333 self
._tc
= get_default_trace_class()
1334 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1335 self
._field
.value
= 17
1336 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1337 self
._def
.value
= 17
1338 self
._def
_value
= 17
1339 self
._def
_const
= _create_const_field(
1340 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1342 self
._def
_new
_value
= -101
1345 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1347 def _const_value_setter(field
):
1350 def _create_fc(self
, tc
):
1351 fc
= tc
.create_signed_enumeration_field_class(32)
1352 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1353 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1354 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1356 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1358 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1362 self
._tc
= get_default_trace_class()
1363 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1364 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1365 self
._def
.value
= 17
1366 self
._def
_value
= 17
1367 self
._def
_const
= _create_const_field(
1368 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1370 self
._def
_new
_value
= -101
1372 def test_str_op(self
):
1373 expected_string_found
= False
1376 # Establish all permutations of the three expected matches since
1377 # the order in which mappings are enumerated is not explicitly part of
1379 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1380 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1382 expected_string_found
= True
1385 self
.assertTrue(expected_string_found
)
1387 def test_labels(self
):
1388 self
._field
.value
= 17
1389 labels
= sorted(self
._field
.labels
)
1390 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1393 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1395 def _const_value_setter(field
):
1398 def _create_fc(self
, tc
):
1399 return tc
.create_real_field_class()
1402 self
._tc
= get_default_trace_class()
1403 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1404 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1405 self
._def
_const
= _create_const_field(
1406 self
._tc
, self
._tc
.create_real_field_class(), self
._const
_value
_setter
1408 self
._def
.value
= 52.7
1409 self
._def
_value
= 52.7
1410 self
._def
_new
_value
= -17.164857
1412 def _test_invalid_op(self
, cb
):
1413 with self
.assertRaises(TypeError):
1416 def test_assign_true(self
):
1417 self
._def
.value
= True
1418 self
.assertTrue(self
._def
)
1420 def test_assign_false(self
):
1421 self
._def
.value
= False
1422 self
.assertFalse(self
._def
)
1424 def test_assign_pos_int(self
):
1426 self
._def
.value
= raw
1427 self
.assertEqual(self
._def
, float(raw
))
1429 def test_assign_neg_int(self
):
1431 self
._def
.value
= raw
1432 self
.assertEqual(self
._def
, float(raw
))
1434 def test_assign_int_field(self
):
1435 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1436 int_field
= _create_field(self
._tc
, int_fc
)
1438 int_field
.value
= raw
1439 self
._def
.value
= int_field
1440 self
.assertEqual(self
._def
, float(raw
))
1442 def test_assign_float(self
):
1444 self
._def
.value
= raw
1445 self
.assertEqual(self
._def
, raw
)
1447 def test_assign_float_field(self
):
1448 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1451 self
._def
.value
= field
1452 self
.assertEqual(self
._def
, raw
)
1454 def test_assign_invalid_type(self
):
1455 with self
.assertRaises(TypeError):
1456 self
._def
.value
= 'yes'
1458 def test_invalid_lshift(self
):
1459 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1461 def test_invalid_rshift(self
):
1462 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1464 def test_invalid_and(self
):
1465 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1467 def test_invalid_or(self
):
1468 self
._test
_invalid
_op
(lambda: self
._def |
23)
1470 def test_invalid_xor(self
):
1471 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1473 def test_invalid_invert(self
):
1474 self
._test
_invalid
_op
(lambda: ~self
._def
)
1476 def test_str_op(self
):
1477 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1480 _inject_numeric_testing_methods(RealFieldTestCase
)
1483 class StringFieldTestCase(unittest
.TestCase
):
1485 def _const_value_setter(field
):
1486 field
.value
= 'Hello, World!'
1489 self
._tc
= get_default_trace_class()
1490 self
._def
_value
= 'Hello, World!'
1491 self
._def
= _create_string_field(self
._tc
)
1492 self
._def
_const
= _create_const_field(
1493 self
._tc
, self
._tc
.create_string_field_class(), self
._const
_value
_setter
1495 self
._def
.value
= self
._def
_value
1496 self
._def
_new
_value
= 'Yes!'
1498 def test_assign_int(self
):
1499 with self
.assertRaises(TypeError):
1500 self
._def
.value
= 283
1502 def test_assign_string_field(self
):
1503 field
= _create_string_field(self
._tc
)
1506 self
.assertEqual(field
, raw
)
1509 self
.assertEqual(self
._def
, self
._def
_value
)
1511 def test_const_eq(self
):
1512 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1514 def test_not_eq(self
):
1515 self
.assertNotEqual(self
._def
, 23)
1517 def test_lt_vstring(self
):
1518 s1
= _create_string_field(self
._tc
)
1520 s2
= _create_string_field(self
._tc
)
1522 self
.assertLess(s1
, s2
)
1524 def test_lt_string(self
):
1525 s1
= _create_string_field(self
._tc
)
1527 self
.assertLess(s1
, 'bateau')
1529 def test_le_vstring(self
):
1530 s1
= _create_string_field(self
._tc
)
1532 s2
= _create_string_field(self
._tc
)
1534 self
.assertLessEqual(s1
, s2
)
1536 def test_le_string(self
):
1537 s1
= _create_string_field(self
._tc
)
1539 self
.assertLessEqual(s1
, 'bateau')
1541 def test_gt_vstring(self
):
1542 s1
= _create_string_field(self
._tc
)
1544 s2
= _create_string_field(self
._tc
)
1546 self
.assertGreater(s2
, s1
)
1548 def test_gt_string(self
):
1549 s1
= _create_string_field(self
._tc
)
1551 self
.assertGreater('bateau', s1
)
1553 def test_ge_vstring(self
):
1554 s1
= _create_string_field(self
._tc
)
1556 s2
= _create_string_field(self
._tc
)
1558 self
.assertGreaterEqual(s2
, s1
)
1560 def test_ge_string(self
):
1561 s1
= _create_string_field(self
._tc
)
1563 self
.assertGreaterEqual('bateau', s1
)
1565 def test_bool_op(self
):
1566 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1568 def test_str_op(self
):
1569 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1572 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1574 def test_getitem(self
):
1575 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1577 def test_const_getitem(self
):
1578 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1580 def test_append_str(self
):
1581 to_append
= 'meow meow meow'
1582 self
._def
+= to_append
1583 self
._def
_value
+= to_append
1584 self
.assertEqual(self
._def
, self
._def
_value
)
1586 def test_const_append_str(self
):
1587 to_append
= 'meow meow meow'
1588 with self
.assertRaises(TypeError):
1589 self
._def
_const
+= to_append
1590 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1592 def test_append_string_field(self
):
1593 field
= _create_string_field(self
._tc
)
1594 to_append
= 'meow meow meow'
1595 field
.value
= to_append
1597 self
._def
_value
+= to_append
1598 self
.assertEqual(self
._def
, self
._def
_value
)
1600 def test_hash_op(self
):
1601 with self
.assertRaises(TypeError):
1604 def test_const_hash_op(self
):
1605 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
1607 def test_const_hash_dict(self
):
1609 my_dict
[self
._def
_const
] = 'my_value'
1610 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
1613 class _TestArrayFieldCommon
:
1614 def _modify_def(self
):
1617 def test_bool_op_true(self
):
1618 self
.assertTrue(self
._def
)
1621 self
.assertEqual(len(self
._def
), 3)
1623 def test_length(self
):
1624 self
.assertEqual(self
._def
.length
, 3)
1626 def test_getitem(self
):
1627 field
= self
._def
[1]
1628 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1629 self
.assertEqual(field
, 1847)
1631 def test_const_getitem(self
):
1632 field
= self
._def
_const
[1]
1633 self
.assertIs(type(field
), bt2
._SignedIntegerFieldConst
)
1634 self
.assertEqual(field
, 1847)
1637 field
= _create_int_array_field(self
._tc
, 3)
1641 self
.assertEqual(self
._def
, field
)
1643 def test_eq_invalid_type(self
):
1644 self
.assertNotEqual(self
._def
, 23)
1646 def test_eq_diff_len(self
):
1647 field
= _create_int_array_field(self
._tc
, 2)
1650 self
.assertNotEqual(self
._def
, field
)
1652 def test_eq_diff_content_same_len(self
):
1653 field
= _create_int_array_field(self
._tc
, 3)
1657 self
.assertNotEqual(self
._def
, field
)
1659 def test_eq_non_sequence_iterable(self
):
1660 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1661 field
= _create_int_array_field(self
._tc
, 3)
1665 self
.assertEqual(field
, list(dct
.keys()))
1666 self
.assertNotEqual(field
, dct
)
1668 def test_setitem(self
):
1670 self
.assertEqual(self
._def
[2], 24)
1672 def test_setitem_int_field(self
):
1673 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1674 int_field
= _create_field(self
._tc
, int_fc
)
1675 int_field
.value
= 19487
1676 self
._def
[1] = int_field
1677 self
.assertEqual(self
._def
[1], 19487)
1679 def test_setitem_non_basic_field(self
):
1680 array_field
= _create_struct_array_field(self
._tc
, 2)
1681 with self
.assertRaises(TypeError):
1684 def test_setitem_none(self
):
1685 with self
.assertRaises(TypeError):
1688 def test_setitem_index_wrong_type(self
):
1689 with self
.assertRaises(TypeError):
1690 self
._def
['yes'] = 23
1692 def test_setitem_index_neg(self
):
1693 with self
.assertRaises(IndexError):
1696 def test_setitem_index_out_of_range(self
):
1697 with self
.assertRaises(IndexError):
1698 self
._def
[len(self
._def
)] = 134679
1700 def test_const_setitem(self
):
1701 with self
.assertRaises(TypeError):
1702 self
._def
_const
[0] = 134679
1704 def test_iter(self
):
1705 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1706 self
.assertEqual(field
, value
)
1708 def test_const_iter(self
):
1709 for field
, value
in zip(self
._def
_const
, (45, 1847, 1948754)):
1710 self
.assertEqual(field
, value
)
1712 def test_value_int_field(self
):
1713 values
= [45646, 145, 12145]
1714 self
._def
.value
= values
1715 self
.assertEqual(values
, self
._def
)
1717 def test_value_check_sequence(self
):
1719 with self
.assertRaises(TypeError):
1720 self
._def
.value
= values
1722 def test_value_wrong_type_in_sequence(self
):
1723 values
= [32, 'hello', 11]
1724 with self
.assertRaises(TypeError):
1725 self
._def
.value
= values
1727 def test_value_complex_type(self
):
1728 struct_fc
= self
._tc
.create_structure_field_class()
1729 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1730 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1731 str_fc
= self
._tc
.create_string_field_class()
1732 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1733 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1734 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1735 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1736 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1738 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1739 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1740 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1743 array
= stream
.create_packet().context_field
['array_field']
1744 array
.value
= values
1745 self
.assertEqual(values
, array
)
1746 values
[0]['an_int'] = 'a string'
1747 with self
.assertRaises(TypeError):
1748 array
.value
= values
1750 def test_str_op(self
):
1752 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1753 self
.assertEqual(expected_string
, s
)
1756 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1758 def _const_value_setter(field
):
1759 field
.value
= [45, 1847, 1948754]
1762 self
._tc
= get_default_trace_class()
1763 self
._def
= _create_int_array_field(self
._tc
, 3)
1766 self
._def
[2] = 1948754
1767 self
._def
_value
= [45, 1847, 1948754]
1768 self
._def
_const
= _create_const_field(
1770 self
._tc
.create_static_array_field_class(
1771 self
._tc
.create_signed_integer_field_class(32), 3
1773 self
._const
_value
_setter
,
1776 def test_value_wrong_len(self
):
1778 with self
.assertRaises(ValueError):
1779 self
._def
.value
= values
1782 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1784 def _const_value_setter(field
):
1785 field
.value
= [45, 1847, 1948754]
1788 self
._tc
= get_default_trace_class()
1789 self
._def
= _create_dynamic_array(self
._tc
)
1792 self
._def
[2] = 1948754
1793 self
._def
_value
= [45, 1847, 1948754]
1794 self
._def
_const
= _create_const_field(
1796 self
._tc
.create_dynamic_array_field_class(
1797 self
._tc
.create_signed_integer_field_class(32)
1799 self
._const
_value
_setter
,
1802 def test_value_resize(self
):
1803 new_values
= [1, 2, 3, 4]
1804 self
._def
.value
= new_values
1805 self
.assertCountEqual(self
._def
, new_values
)
1807 def test_set_length(self
):
1808 self
._def
.length
= 4
1810 self
.assertEqual(len(self
._def
), 4)
1812 def test_const_set_length(self
):
1813 with self
.assertRaises(AttributeError):
1814 self
._def
_const
.length
= 4
1815 self
.assertEqual(len(self
._def
), 3)
1817 def test_set_invalid_length(self
):
1818 with self
.assertRaises(TypeError):
1819 self
._def
.length
= 'cheval'
1822 class StructureFieldTestCase(unittest
.TestCase
):
1824 def _const_value_setter(field
):
1834 def _create_fc(self
, tc
):
1835 fc
= tc
.create_structure_field_class()
1836 fc
.append_member('A', self
._fc
0_fn
())
1837 fc
.append_member('B', self
._fc
1_fn
())
1838 fc
.append_member('C', self
._fc
2_fn
())
1839 fc
.append_member('D', self
._fc
3_fn
())
1840 fc
.append_member('E', self
._fc
4_fn
())
1841 fc5
= self
._fc
5_fn
()
1842 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1843 fc
.append_member('F', fc5
)
1847 self
._tc
= get_default_trace_class()
1848 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1849 self
._fc
1_fn
= self
._tc
.create_string_field_class
1850 self
._fc
2_fn
= self
._tc
.create_real_field_class
1851 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1852 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1853 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1854 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1856 self
._fc
= self
._create
_fc
(self
._tc
)
1857 self
._def
= _create_field(self
._tc
, self
._fc
)
1858 self
._def
['A'] = -1872
1859 self
._def
['B'] = 'salut'
1860 self
._def
['C'] = 17.5
1861 self
._def
['D'] = 16497
1863 self
._def
['F'] = {'F_1': 52}
1873 self
._def
_const
= _create_const_field(
1874 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1877 def _modify_def(self
):
1878 self
._def
['B'] = 'hola'
1880 def test_bool_op_true(self
):
1881 self
.assertTrue(self
._def
)
1883 def test_bool_op_false(self
):
1884 field
= self
._def
['E']
1885 self
.assertFalse(field
)
1888 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1890 def test_getitem(self
):
1891 field1
= self
._def
['A']
1892 field2
= self
._def
['B']
1893 field3
= self
._def
['C']
1894 field4
= self
._def
['D']
1895 field5
= self
._def
['E']
1896 field6
= self
._def
['F']
1898 self
.assertIs(type(field1
), bt2
._SignedIntegerField
)
1899 self
.assertEqual(field1
, -1872)
1901 self
.assertIs(type(field2
), bt2
._StringField
)
1902 self
.assertEqual(field2
, 'salut')
1904 self
.assertIs(type(field3
), bt2
._RealField
)
1905 self
.assertEqual(field3
, 17.5)
1907 self
.assertIs(type(field4
), bt2
._SignedIntegerField
)
1908 self
.assertEqual(field4
, 16497)
1910 self
.assertIs(type(field5
), bt2
._StructureField
)
1911 self
.assertEqual(field5
, {})
1913 self
.assertIs(type(field6
), bt2
._StructureField
)
1914 self
.assertEqual(field6
, {'F_1': 52})
1916 def test_const_getitem(self
):
1917 field1
= self
._def
_const
['A']
1918 field2
= self
._def
_const
['B']
1919 field3
= self
._def
_const
['C']
1920 field4
= self
._def
_const
['D']
1921 field5
= self
._def
_const
['E']
1922 field6
= self
._def
_const
['F']
1924 self
.assertIs(type(field1
), bt2
._SignedIntegerFieldConst
)
1925 self
.assertEqual(field1
, -1872)
1927 self
.assertIs(type(field2
), bt2
._StringFieldConst
)
1928 self
.assertEqual(field2
, 'salut')
1930 self
.assertIs(type(field3
), bt2
._RealFieldConst
)
1931 self
.assertEqual(field3
, 17.5)
1933 self
.assertIs(type(field4
), bt2
._SignedIntegerFieldConst
)
1934 self
.assertEqual(field4
, 16497)
1936 self
.assertIs(type(field5
), bt2
._StructureFieldConst
)
1937 self
.assertEqual(field5
, {})
1939 self
.assertIs(type(field6
), bt2
._StructureFieldConst
)
1940 self
.assertEqual(field6
, {'F_1': 52})
1942 def test_member_at_index_out_of_bounds_after(self
):
1943 with self
.assertRaises(IndexError):
1944 self
._def
.member_at_index(len(self
._def
_value
))
1947 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1949 field
['B'] = 'salut'
1953 field
['F'] = {'F_1': 52}
1954 self
.assertEqual(self
._def
, field
)
1956 def test_const_eq(self
):
1957 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1959 field
['B'] = 'salut'
1963 field
['F'] = {'F_1': 52}
1964 self
.assertEqual(self
._def
_const
, field
)
1966 def test_eq_invalid_type(self
):
1967 self
.assertNotEqual(self
._def
, 23)
1969 def test_eq_diff_len(self
):
1970 fc
= self
._tc
.create_structure_field_class()
1971 fc
.append_member('A', self
._fc
0_fn
())
1972 fc
.append_member('B', self
._fc
1_fn
())
1973 fc
.append_member('C', self
._fc
2_fn
())
1975 field
= _create_field(self
._tc
, fc
)
1977 field
['B'] = 'salut'
1979 self
.assertNotEqual(self
._def
, field
)
1981 def test_eq_diff_keys(self
):
1982 fc
= self
._tc
.create_structure_field_class()
1983 fc
.append_member('U', self
._fc
0_fn
())
1984 fc
.append_member('V', self
._fc
1_fn
())
1985 fc
.append_member('W', self
._fc
2_fn
())
1986 fc
.append_member('X', self
._fc
3_fn
())
1987 fc
.append_member('Y', self
._fc
4_fn
())
1988 fc
.append_member('Z', self
._fc
5_fn
())
1989 field
= _create_field(self
._tc
, fc
)
1991 field
['V'] = "gerry"
1996 self
.assertNotEqual(self
._def
, field
)
1998 def test_eq_diff_content_same_len(self
):
1999 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2001 field
['B'] = 'salut'
2005 field
['F'] = {'F_1': 0}
2006 self
.assertNotEqual(self
._def
, field
)
2008 def test_eq_same_content_diff_keys(self
):
2009 fc
= self
._tc
.create_structure_field_class()
2010 fc
.append_member('A', self
._fc
0_fn
())
2011 fc
.append_member('B', self
._fc
1_fn
())
2012 fc
.append_member('E', self
._fc
2_fn
())
2013 fc
.append_member('D', self
._fc
3_fn
())
2014 fc
.append_member('C', self
._fc
4_fn
())
2015 fc
.append_member('F', self
._fc
5_fn
())
2016 field
= _create_field(self
._tc
, fc
)
2018 field
['B'] = 'salut'
2023 self
.assertNotEqual(self
._def
, field
)
2025 def test_setitem(self
):
2026 self
._def
['C'] = -18.47
2027 self
.assertEqual(self
._def
['C'], -18.47)
2029 def test_const_setitem(self
):
2030 with self
.assertRaises(TypeError):
2031 self
._def
_const
['A'] = 134679
2033 def test_setitem_int_field(self
):
2034 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2035 int_field
= _create_field(self
._tc
, int_fc
)
2036 int_field
.value
= 19487
2037 self
._def
['D'] = int_field
2038 self
.assertEqual(self
._def
['D'], 19487)
2040 def test_setitem_non_basic_field(self
):
2041 elem_fc
= self
._tc
.create_structure_field_class()
2042 struct_fc
= self
._tc
.create_structure_field_class()
2043 struct_fc
.append_member('A', elem_fc
)
2044 struct_field
= _create_field(self
._tc
, struct_fc
)
2046 # Will fail on access to .items() of the value
2047 with self
.assertRaises(AttributeError):
2048 struct_field
['A'] = 23
2050 def test_setitem_none(self
):
2051 with self
.assertRaises(TypeError):
2052 self
._def
['C'] = None
2054 def test_setitem_key_wrong_type(self
):
2055 with self
.assertRaises(TypeError):
2058 def test_setitem_wrong_key(self
):
2059 with self
.assertRaises(KeyError):
2060 self
._def
['hi'] = 134679
2062 def test_member_at_index(self
):
2063 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
2065 def test_const_member_at_index(self
):
2066 self
.assertEqual(self
._def
_const
.member_at_index(1), 'salut')
2068 def test_iter(self
):
2078 for vkey
, vval
in self
._def
.items():
2079 val
= orig_values
[vkey
]
2080 self
.assertEqual(vval
, val
)
2082 def test_value(self
):
2091 self
.assertEqual(self
._def
, orig_values
)
2093 def test_set_value(self
):
2094 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2095 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
2096 str_fc
= self
._tc
.create_string_field_class()
2097 struct_fc
= self
._tc
.create_structure_field_class()
2098 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
2099 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
2100 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
2101 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2103 struct
= _create_field(self
._tc
, struct_fc
)
2104 struct
.value
= values
2105 self
.assertEqual(values
, struct
)
2107 bad_type_values
= copy
.deepcopy(values
)
2108 bad_type_values
['an_int'] = 'a string'
2109 with self
.assertRaises(TypeError):
2110 struct
.value
= bad_type_values
2112 unknown_key_values
= copy
.deepcopy(values
)
2113 unknown_key_values
['unknown_key'] = 16546
2114 with self
.assertRaises(KeyError):
2115 struct
.value
= unknown_key_values
2117 def test_str_op(self
):
2118 expected_string_found
= False
2120 # Establish all permutations of the three expected matches since
2121 # the order in which mappings are enumerated is not explicitly part of
2123 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
2124 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
2125 candidate
= '{{{}}}'.format(', '.join(items
))
2127 expected_string_found
= True
2130 self
.assertTrue(expected_string_found
)
2133 class OptionFieldTestCase(unittest
.TestCase
):
2135 def _const_value_setter(field
):
2136 field
.value
= {'opt_field': 'hiboux'}
2138 def _create_fc(self
, tc
):
2139 fc
= tc
.create_option_field_class(tc
.create_string_field_class())
2140 top_fc
= tc
.create_structure_field_class()
2141 top_fc
.append_member('opt_field', fc
)
2145 self
._tc
= get_default_trace_class()
2146 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2147 self
._def
= fld
['opt_field']
2148 self
._def
_value
= 'hiboux'
2149 self
._def
_const
= _create_const_field(
2150 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
2153 def test_value_prop(self
):
2154 self
._def
.value
= 'hiboux'
2155 self
.assertEqual(self
._def
.field
, 'hiboux')
2156 self
.assertIs(type(self
._def
), bt2
._OptionField
)
2157 self
.assertIs(type(self
._def
.field
), bt2
._StringField
)
2158 self
.assertTrue(self
._def
.has_field
)
2160 def test_const_value_prop(self
):
2161 self
.assertEqual(self
._def
_const
.field
, 'hiboux')
2162 self
.assertIs(type(self
._def
_const
), bt2
._OptionFieldConst
)
2163 self
.assertIs(type(self
._def
_const
.field
), bt2
._StringFieldConst
)
2164 self
.assertTrue(self
._def
_const
.has_field
)
2166 def test_has_field_prop_true(self
):
2167 self
._def
.has_field
= True
2168 self
.assertTrue(self
._def
.has_field
)
2170 def test_has_field_prop_true(self
):
2171 self
._def
.has_field
= False
2172 self
.assertFalse(self
._def
.has_field
)
2174 def test_bool_op_true(self
):
2175 self
._def
.value
= 'allo'
2176 self
.assertTrue(self
._def
)
2178 def test_bool_op_true(self
):
2179 self
._def
.has_field
= False
2180 self
.assertFalse(self
._def
)
2182 def test_field_prop_existing(self
):
2183 self
._def
.value
= 'meow'
2184 field
= self
._def
.field
2185 self
.assertEqual(field
, 'meow')
2187 def test_field_prop_none(self
):
2188 self
._def
.has_field
= False
2189 field
= self
._def
.field
2190 self
.assertIsNone(field
)
2192 def test_const_field_prop(self
):
2193 with self
.assertRaises(AttributeError):
2194 self
._def
_const
.has_field
= False
2196 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2197 self
.assertTrue(self
._def
_const
.has_field
)
2199 def test_field_prop_existing_then_none(self
):
2200 self
._def
.value
= 'meow'
2201 field
= self
._def
.field
2202 self
.assertEqual(field
, 'meow')
2203 self
._def
.has_field
= False
2204 field
= self
._def
.field
2205 self
.assertIsNone(field
)
2208 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2209 field
= field
['opt_field']
2210 field
.value
= 'walk'
2211 self
._def
.value
= 'walk'
2212 self
.assertEqual(self
._def
, field
)
2214 def test_const_eq(self
):
2215 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2216 field
= field
['opt_field']
2217 field
.value
= 'hiboux'
2218 self
.assertEqual(self
._def
_const
, field
)
2219 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2221 def test_eq_invalid_type(self
):
2222 self
._def
.value
= 'gerry'
2223 self
.assertNotEqual(self
._def
, 23)
2225 def test_str_op(self
):
2226 self
._def
.value
= 'marcel'
2227 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
2229 def test_repr_op(self
):
2230 self
._def
.value
= 'mireille'
2231 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
2234 class VariantFieldTestCase(unittest
.TestCase
):
2236 def _const_value_setter(field
):
2237 field
.selected_option_index
= 3
2240 def _create_fc(self
, tc
):
2241 ft0
= tc
.create_signed_integer_field_class(32)
2242 ft1
= tc
.create_string_field_class()
2243 ft2
= tc
.create_real_field_class()
2244 ft3
= tc
.create_signed_integer_field_class(17)
2245 fc
= tc
.create_variant_field_class()
2246 fc
.append_option('corner', ft0
)
2247 fc
.append_option('zoom', ft1
)
2248 fc
.append_option('mellotron', ft2
)
2249 fc
.append_option('giorgio', ft3
)
2250 top_fc
= tc
.create_structure_field_class()
2251 top_fc
.append_member('variant_field', fc
)
2255 self
._tc
= get_default_trace_class()
2256 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2257 self
._def
= fld
['variant_field']
2259 self
._def
_value
= 1334
2260 self
._def
_selected
_index
= 3
2261 const_fc
= self
._create
_fc
(self
._tc
)['variant_field']
2263 fld_const
= _create_const_field(
2264 self
._tc
, const_fc
.field_class
, self
._const
_value
_setter
2266 self
._def
_const
= fld_const
2268 def test_bool_op(self
):
2269 self
._def
.selected_option_index
= 2
2270 self
._def
.value
= -17.34
2271 with self
.assertRaises(NotImplementedError):
2274 def test_selected_option_index(self
):
2275 self
._def
.selected_option_index
= 2
2276 self
.assertEqual(self
._def
.selected_option_index
, 2)
2278 def test_selected_option_index_above_range(self
):
2279 with self
.assertRaises(IndexError):
2280 self
._def
.selected_option_index
= 4
2282 def test_selected_option_index_below_range(self
):
2283 with self
.assertRaises(IndexError):
2284 self
._def
.selected_option_index
= -1
2286 def test_const_selected_option_index(self
):
2287 with self
.assertRaises(AttributeError):
2288 self
._def
_const
.selected_option_index
= 2
2289 self
.assertEqual(self
._def
_const
.selected_option_index
, 3)
2291 def test_selected_option(self
):
2292 self
._def
.selected_option_index
= 2
2293 self
._def
.value
= -17.34
2294 self
.assertEqual(self
._def
.selected_option
, -17.34)
2295 self
.assertEqual(type(self
._def
.selected_option
), bt2
._RealField
)
2297 self
._def
.selected_option_index
= 3
2298 self
._def
.value
= 1921
2299 self
.assertEqual(self
._def
.selected_option
, 1921)
2300 self
.assertEqual(type(self
._def
.selected_option
), bt2
._SignedIntegerField
)
2302 def test_const_selected_option(self
):
2303 self
.assertEqual(self
._def
_const
.selected_option
, 1334)
2305 type(self
._def
_const
.selected_option
), bt2
._SignedIntegerFieldConst
2309 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2310 field
= field
['variant_field']
2311 field
.selected_option_index
= 0
2313 self
._def
.selected_option_index
= 0
2314 self
._def
.value
= 1774
2315 self
.assertEqual(self
._def
, field
)
2317 def test_const_eq(self
):
2318 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2319 field
= field
['variant_field']
2320 field
.selected_option_index
= 3
2322 self
.assertEqual(self
._def
_const
, field
)
2325 self
.assertEqual(len(self
._def
), 4)
2327 def test_eq_invalid_type(self
):
2328 self
._def
.selected_option_index
= 1
2329 self
._def
.value
= 'gerry'
2330 self
.assertNotEqual(self
._def
, 23)
2332 def test_str_op_int(self
):
2333 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2334 field
= field
['variant_field']
2335 field
.selected_option_index
= 0
2337 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2338 other_field
= other_field
['variant_field']
2339 other_field
.selected_option_index
= 0
2340 other_field
.value
= 1774
2341 self
.assertEqual(str(field
), str(other_field
))
2343 def test_str_op_str(self
):
2344 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2345 field
= field
['variant_field']
2346 field
.selected_option_index
= 1
2347 field
.value
= 'un beau grand bateau'
2348 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2349 other_field
= other_field
['variant_field']
2350 other_field
.selected_option_index
= 1
2351 other_field
.value
= 'un beau grand bateau'
2352 self
.assertEqual(str(field
), str(other_field
))
2354 def test_str_op_float(self
):
2355 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2356 field
= field
['variant_field']
2357 field
.selected_option_index
= 2
2358 field
.value
= 14.4245
2359 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2360 other_field
= other_field
['variant_field']
2361 other_field
.selected_option_index
= 2
2362 other_field
.value
= 14.4245
2363 self
.assertEqual(str(field
), str(other_field
))
2366 if __name__
== '__main__':