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
, 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_eq_none(self
):
664 # Ignore this lint error:
665 # E711 comparison to None should be 'if cond is None:'
666 # since this is what we want to test (even though not good practice).
667 self
.assertFalse(self
._def
== None) # noqa: E711
669 def test_ne_none(self
):
670 # Ignore this lint error:
671 # E711 comparison to None should be 'if cond is not None:'
672 # since this is what we want to test (even though not good practice).
673 self
.assertTrue(self
._def
!= None) # noqa: E711
676 # This is a list of binary operators used for
677 # _inject_numeric_testing_methods().
679 # Each entry is a pair of binary operator name (used as part of the
680 # created testing method's name) and operator function.
688 ('add', operator
.add
),
689 ('radd', lambda a
, b
: operator
.add(b
, a
)),
690 ('and', operator
.and_
),
691 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
692 ('floordiv', operator
.floordiv
),
693 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
694 ('lshift', operator
.lshift
),
695 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
696 ('mod', operator
.mod
),
697 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
698 ('mul', operator
.mul
),
699 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
700 ('or', operator
.or_
),
701 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
702 ('pow', operator
.pow),
703 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
704 ('rshift', operator
.rshift
),
705 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
706 ('sub', operator
.sub
),
707 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
708 ('truediv', operator
.truediv
),
709 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
710 ('xor', operator
.xor
),
711 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
715 # This is a list of unary operators used for
716 # _inject_numeric_testing_methods().
718 # Each entry is a pair of unary operator name (used as part of the
719 # created testing method's name) and operator function.
721 ('neg', operator
.neg
),
722 ('pos', operator
.pos
),
723 ('abs', operator
.abs),
724 ('invert', operator
.invert
),
726 ('round_0', partial(round, ndigits
=0)),
727 ('round_1', partial(round, ndigits
=1)),
728 ('round_2', partial(round, ndigits
=2)),
729 ('round_3', partial(round, ndigits
=3)),
731 ('floor', math
.floor
),
732 ('trunc', math
.trunc
),
736 # This function injects a bunch of testing methods to a numeric
739 # It is meant to be used like this:
741 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
743 # This function injects:
745 # * One testing method for each _TestNumericField._test_binop_*()
746 # method, for each binary operator in the _BINOPS tuple.
748 # * One testing method for each _TestNumericField._test_unaryop*()
749 # method, for each unary operator in the _UNARYOPS tuple.
750 def _inject_numeric_testing_methods(cls
):
751 def test_binop_name(suffix
):
752 return 'test_binop_{}_{}'.format(name
, suffix
)
754 def test_unaryop_name(suffix
):
755 return 'test_unaryop_{}_{}'.format(name
, suffix
)
757 # inject testing methods for each binary operation
758 for name
, binop
in _BINOPS
:
761 test_binop_name('invalid_unknown'),
762 partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
),
766 test_binop_name('invalid_none'),
767 partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
),
771 test_binop_name('type_true'),
772 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
776 test_binop_name('type_pos_int'),
777 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
781 test_binop_name('type_pos_vint'),
782 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
786 test_binop_name('value_true'),
787 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
791 test_binop_name('value_pos_int'),
792 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
796 test_binop_name('value_pos_vint'),
797 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
801 test_binop_name('lhs_addr_same_true'),
802 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
806 test_binop_name('lhs_addr_same_pos_int'),
808 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
813 test_binop_name('lhs_addr_same_pos_vint'),
815 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
820 test_binop_name('lhs_value_same_true'),
821 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
825 test_binop_name('lhs_value_same_pos_int'),
827 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
832 test_binop_name('lhs_value_same_pos_vint'),
834 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
839 test_binop_name('type_neg_int'),
840 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
844 test_binop_name('type_neg_vint'),
845 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
849 test_binop_name('value_neg_int'),
850 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
854 test_binop_name('value_neg_vint'),
855 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
859 test_binop_name('lhs_addr_same_neg_int'),
861 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
866 test_binop_name('lhs_addr_same_neg_vint'),
868 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
873 test_binop_name('lhs_value_same_neg_int'),
875 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
880 test_binop_name('lhs_value_same_neg_vint'),
882 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
887 test_binop_name('type_false'),
888 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
892 test_binop_name('type_zero_int'),
893 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
897 test_binop_name('type_zero_vint'),
898 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
902 test_binop_name('value_false'),
903 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
907 test_binop_name('value_zero_int'),
908 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
912 test_binop_name('value_zero_vint'),
913 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
917 test_binop_name('lhs_addr_same_false'),
918 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
922 test_binop_name('lhs_addr_same_zero_int'),
924 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
929 test_binop_name('lhs_addr_same_zero_vint'),
931 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
936 test_binop_name('lhs_value_same_false'),
937 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
941 test_binop_name('lhs_value_same_zero_int'),
943 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
948 test_binop_name('lhs_value_same_zero_vint'),
950 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
955 test_binop_name('type_pos_float'),
956 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
960 test_binop_name('type_neg_float'),
961 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
965 test_binop_name('type_pos_vfloat'),
966 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
970 test_binop_name('type_neg_vfloat'),
971 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
975 test_binop_name('value_pos_float'),
976 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
980 test_binop_name('value_neg_float'),
981 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
985 test_binop_name('value_pos_vfloat'),
986 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
990 test_binop_name('value_neg_vfloat'),
991 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
995 test_binop_name('lhs_addr_same_pos_float'),
997 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
1002 test_binop_name('lhs_addr_same_neg_float'),
1004 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
1009 test_binop_name('lhs_addr_same_pos_vfloat'),
1011 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
1016 test_binop_name('lhs_addr_same_neg_vfloat'),
1018 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
1023 test_binop_name('lhs_value_same_pos_float'),
1025 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
1030 test_binop_name('lhs_value_same_neg_float'),
1032 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
1037 test_binop_name('lhs_value_same_pos_vfloat'),
1039 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
1044 test_binop_name('lhs_value_same_neg_vfloat'),
1046 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
1051 test_binop_name('type_zero_float'),
1052 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
1056 test_binop_name('type_zero_vfloat'),
1057 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
1061 test_binop_name('value_zero_float'),
1062 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
1066 test_binop_name('value_zero_vfloat'),
1067 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
1071 test_binop_name('lhs_addr_same_zero_float'),
1073 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
1078 test_binop_name('lhs_addr_same_zero_vfloat'),
1080 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
1085 test_binop_name('lhs_value_same_zero_float'),
1087 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1092 test_binop_name('lhs_value_same_zero_vfloat'),
1094 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1099 test_binop_name('type_complex'),
1100 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1104 test_binop_name('type_zero_complex'),
1105 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1109 test_binop_name('value_complex'),
1110 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1114 test_binop_name('value_zero_complex'),
1115 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1119 test_binop_name('lhs_addr_same_complex'),
1121 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1126 test_binop_name('lhs_addr_same_zero_complex'),
1128 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1133 test_binop_name('lhs_value_same_complex'),
1135 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1140 test_binop_name('lhs_value_same_zero_complex'),
1142 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1146 # inject testing methods for each unary operation
1147 for name
, unaryop
in _UNARYOPS
:
1150 test_unaryop_name('type'),
1151 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1155 test_unaryop_name('value'),
1156 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1160 test_unaryop_name('addr_same'),
1161 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1165 test_unaryop_name('value_same'),
1166 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1170 class BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1172 def _const_value_setter(field
):
1175 def _create_fc(self
, tc
):
1176 return tc
.create_bool_field_class()
1179 self
._tc
= get_default_trace_class()
1180 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1181 self
._def
.value
= True
1182 self
._def
_value
= True
1183 self
._def
_const
= _create_const_field(
1184 self
._tc
, self
._tc
.create_bool_field_class(), self
._const
_value
_setter
1186 self
._def
_new
_value
= False
1188 def test_classes(self
):
1189 self
.assertIs(type(self
._def
), bt2
._BoolField
)
1190 self
.assertIs(type(self
._def
_const
), bt2
._BoolFieldConst
)
1192 def test_assign_true(self
):
1194 self
._def
.value
= raw
1195 self
.assertEqual(self
._def
, raw
)
1197 def test_assign_false(self
):
1199 self
._def
.value
= raw
1200 self
.assertEqual(self
._def
, raw
)
1202 def test_assign_field_true(self
):
1203 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1206 self
._def
.value
= field
1207 self
.assertEqual(self
._def
, raw
)
1209 def test_assign_field_false(self
):
1210 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1213 self
._def
.value
= field
1214 self
.assertEqual(self
._def
, raw
)
1216 def test_assign_invalid_type(self
):
1217 with self
.assertRaises(TypeError):
1218 self
._def
.value
= 17
1220 def test_str_op(self
):
1221 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1224 _inject_numeric_testing_methods(BoolFieldTestCase
)
1227 class _TestIntegerFieldCommon(_TestNumericField
):
1228 def test_assign_true(self
):
1230 self
._def
.value
= raw
1231 self
.assertEqual(self
._def
, raw
)
1233 def test_assign_false(self
):
1235 self
._def
.value
= raw
1236 self
.assertEqual(self
._def
, raw
)
1238 def test_assign_pos_int(self
):
1240 self
._def
.value
= raw
1241 self
.assertEqual(self
._def
, raw
)
1243 def test_assign_neg_int(self
):
1245 self
._def
.value
= raw
1246 self
.assertEqual(self
._def
, raw
)
1248 def test_assign_int_field(self
):
1250 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1252 self
._def
.value
= field
1253 self
.assertEqual(self
._def
, raw
)
1255 def test_assign_invalid_type(self
):
1256 with self
.assertRaises(TypeError):
1257 self
._def
.value
= 'yes'
1259 def test_assign_uint(self
):
1260 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1261 field
= _create_field(self
._tc
, uint_fc
)
1264 self
.assertEqual(field
, raw
)
1266 def test_assign_big_uint(self
):
1267 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1268 field
= _create_field(self
._tc
, uint_fc
)
1269 # Larger than the IEEE 754 double-precision exact representation of
1272 field
.value
= (2 ** 53) + 1
1273 self
.assertEqual(field
, raw
)
1275 def test_assign_uint_invalid_neg(self
):
1276 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1277 field
= _create_field(self
._tc
, uint_fc
)
1279 with self
.assertRaises(ValueError):
1282 def test_str_op(self
):
1283 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1286 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1289 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1290 def _create_fc(self
, tc
):
1291 return tc
.create_signed_integer_field_class(25)
1294 self
._tc
= get_default_trace_class()
1295 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1296 self
._field
.value
= 17
1297 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1298 self
._def
.value
= 17
1299 self
._def
_value
= 17
1300 self
._def
_new
_value
= -101
1303 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1304 def _create_fc(self
, tc
):
1305 fc
= tc
.create_signed_enumeration_field_class(32)
1306 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1307 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1308 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1310 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1312 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1316 self
._tc
= get_default_trace_class()
1317 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1318 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1319 self
._def
.value
= 17
1320 self
._def
_value
= 17
1321 self
._def
_new
_value
= -101
1323 def test_str_op(self
):
1324 expected_string_found
= False
1327 # Establish all permutations of the three expected matches since
1328 # the order in which mappings are enumerated is not explicitly part of
1330 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1331 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1333 expected_string_found
= True
1336 self
.assertTrue(expected_string_found
)
1338 def test_labels(self
):
1339 self
._field
.value
= 17
1340 labels
= sorted(self
._field
.labels
)
1341 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1344 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1345 def _create_fc(self
, tc
):
1346 return tc
.create_real_field_class()
1349 self
._tc
= get_default_trace_class()
1350 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1351 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1352 self
._def
.value
= 52.7
1353 self
._def
_value
= 52.7
1354 self
._def
_new
_value
= -17.164857
1356 def _test_invalid_op(self
, cb
):
1357 with self
.assertRaises(TypeError):
1360 def test_assign_true(self
):
1361 self
._def
.value
= True
1362 self
.assertTrue(self
._def
)
1364 def test_assign_false(self
):
1365 self
._def
.value
= False
1366 self
.assertFalse(self
._def
)
1368 def test_assign_pos_int(self
):
1370 self
._def
.value
= raw
1371 self
.assertEqual(self
._def
, float(raw
))
1373 def test_assign_neg_int(self
):
1375 self
._def
.value
= raw
1376 self
.assertEqual(self
._def
, float(raw
))
1378 def test_assign_int_field(self
):
1379 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1380 int_field
= _create_field(self
._tc
, int_fc
)
1382 int_field
.value
= raw
1383 self
._def
.value
= int_field
1384 self
.assertEqual(self
._def
, float(raw
))
1386 def test_assign_float(self
):
1388 self
._def
.value
= raw
1389 self
.assertEqual(self
._def
, raw
)
1391 def test_assign_float_field(self
):
1392 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1395 self
._def
.value
= field
1396 self
.assertEqual(self
._def
, raw
)
1398 def test_assign_invalid_type(self
):
1399 with self
.assertRaises(TypeError):
1400 self
._def
.value
= 'yes'
1402 def test_invalid_lshift(self
):
1403 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1405 def test_invalid_rshift(self
):
1406 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1408 def test_invalid_and(self
):
1409 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1411 def test_invalid_or(self
):
1412 self
._test
_invalid
_op
(lambda: self
._def |
23)
1414 def test_invalid_xor(self
):
1415 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1417 def test_invalid_invert(self
):
1418 self
._test
_invalid
_op
(lambda: ~self
._def
)
1420 def test_str_op(self
):
1421 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1424 _inject_numeric_testing_methods(RealFieldTestCase
)
1427 class StringFieldTestCase(unittest
.TestCase
):
1429 def _const_value_setter(field
):
1430 field
.value
= 'Hello, World!'
1433 self
._tc
= get_default_trace_class()
1434 self
._def
_value
= 'Hello, World!'
1435 self
._def
= _create_string_field(self
._tc
)
1436 self
._def
_const
= _create_const_field(
1437 self
._tc
, self
._tc
.create_string_field_class(), self
._const
_value
_setter
1439 self
._def
.value
= self
._def
_value
1440 self
._def
_new
_value
= 'Yes!'
1442 def test_assign_int(self
):
1443 with self
.assertRaises(TypeError):
1444 self
._def
.value
= 283
1446 def test_assign_string_field(self
):
1447 field
= _create_string_field(self
._tc
)
1450 self
.assertEqual(field
, raw
)
1453 self
.assertEqual(self
._def
, self
._def
_value
)
1455 def test_const_eq(self
):
1456 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1458 def test_not_eq(self
):
1459 self
.assertNotEqual(self
._def
, 23)
1461 def test_lt_vstring(self
):
1462 s1
= _create_string_field(self
._tc
)
1464 s2
= _create_string_field(self
._tc
)
1466 self
.assertLess(s1
, s2
)
1468 def test_lt_string(self
):
1469 s1
= _create_string_field(self
._tc
)
1471 self
.assertLess(s1
, 'bateau')
1473 def test_le_vstring(self
):
1474 s1
= _create_string_field(self
._tc
)
1476 s2
= _create_string_field(self
._tc
)
1478 self
.assertLessEqual(s1
, s2
)
1480 def test_le_string(self
):
1481 s1
= _create_string_field(self
._tc
)
1483 self
.assertLessEqual(s1
, 'bateau')
1485 def test_gt_vstring(self
):
1486 s1
= _create_string_field(self
._tc
)
1488 s2
= _create_string_field(self
._tc
)
1490 self
.assertGreater(s2
, s1
)
1492 def test_gt_string(self
):
1493 s1
= _create_string_field(self
._tc
)
1495 self
.assertGreater('bateau', s1
)
1497 def test_ge_vstring(self
):
1498 s1
= _create_string_field(self
._tc
)
1500 s2
= _create_string_field(self
._tc
)
1502 self
.assertGreaterEqual(s2
, s1
)
1504 def test_ge_string(self
):
1505 s1
= _create_string_field(self
._tc
)
1507 self
.assertGreaterEqual('bateau', s1
)
1509 def test_bool_op(self
):
1510 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1512 def test_str_op(self
):
1513 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1516 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1518 def test_getitem(self
):
1519 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1521 def test_const_getitem(self
):
1522 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1524 def test_append_str(self
):
1525 to_append
= 'meow meow meow'
1526 self
._def
+= to_append
1527 self
._def
_value
+= to_append
1528 self
.assertEqual(self
._def
, self
._def
_value
)
1530 def test_const_append_str(self
):
1531 to_append
= 'meow meow meow'
1532 with self
.assertRaises(TypeError):
1533 self
._def
_const
+= to_append
1534 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1536 def test_append_string_field(self
):
1537 field
= _create_string_field(self
._tc
)
1538 to_append
= 'meow meow meow'
1539 field
.value
= to_append
1541 self
._def
_value
+= to_append
1542 self
.assertEqual(self
._def
, self
._def
_value
)
1545 class _TestArrayFieldCommon
:
1546 def _modify_def(self
):
1549 def test_bool_op_true(self
):
1550 self
.assertTrue(self
._def
)
1553 self
.assertEqual(len(self
._def
), 3)
1555 def test_length(self
):
1556 self
.assertEqual(self
._def
.length
, 3)
1558 def test_getitem(self
):
1559 field
= self
._def
[1]
1560 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1561 self
.assertEqual(field
, 1847)
1563 def test_const_getitem(self
):
1564 field
= self
._def
_const
[1]
1565 self
.assertIs(type(field
), bt2
._SignedIntegerFieldConst
)
1566 self
.assertEqual(field
, 1847)
1569 field
= _create_int_array_field(self
._tc
, 3)
1573 self
.assertEqual(self
._def
, field
)
1575 def test_eq_invalid_type(self
):
1576 self
.assertNotEqual(self
._def
, 23)
1578 def test_eq_diff_len(self
):
1579 field
= _create_int_array_field(self
._tc
, 2)
1582 self
.assertNotEqual(self
._def
, field
)
1584 def test_eq_diff_content_same_len(self
):
1585 field
= _create_int_array_field(self
._tc
, 3)
1589 self
.assertNotEqual(self
._def
, field
)
1591 def test_eq_non_sequence_iterable(self
):
1592 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1593 field
= _create_int_array_field(self
._tc
, 3)
1597 self
.assertEqual(field
, list(dct
.keys()))
1598 self
.assertNotEqual(field
, dct
)
1600 def test_setitem(self
):
1602 self
.assertEqual(self
._def
[2], 24)
1604 def test_setitem_int_field(self
):
1605 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1606 int_field
= _create_field(self
._tc
, int_fc
)
1607 int_field
.value
= 19487
1608 self
._def
[1] = int_field
1609 self
.assertEqual(self
._def
[1], 19487)
1611 def test_setitem_non_basic_field(self
):
1612 array_field
= _create_struct_array_field(self
._tc
, 2)
1613 with self
.assertRaises(TypeError):
1616 def test_setitem_none(self
):
1617 with self
.assertRaises(TypeError):
1620 def test_setitem_index_wrong_type(self
):
1621 with self
.assertRaises(TypeError):
1622 self
._def
['yes'] = 23
1624 def test_setitem_index_neg(self
):
1625 with self
.assertRaises(IndexError):
1628 def test_setitem_index_out_of_range(self
):
1629 with self
.assertRaises(IndexError):
1630 self
._def
[len(self
._def
)] = 134679
1632 def test_const_setitem(self
):
1633 with self
.assertRaises(TypeError):
1634 self
._def
_const
[0] = 134679
1636 def test_iter(self
):
1637 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1638 self
.assertEqual(field
, value
)
1640 def test_const_iter(self
):
1641 for field
, value
in zip(self
._def
_const
, (45, 1847, 1948754)):
1642 self
.assertEqual(field
, value
)
1644 def test_value_int_field(self
):
1645 values
= [45646, 145, 12145]
1646 self
._def
.value
= values
1647 self
.assertEqual(values
, self
._def
)
1649 def test_value_check_sequence(self
):
1651 with self
.assertRaises(TypeError):
1652 self
._def
.value
= values
1654 def test_value_wrong_type_in_sequence(self
):
1655 values
= [32, 'hello', 11]
1656 with self
.assertRaises(TypeError):
1657 self
._def
.value
= values
1659 def test_value_complex_type(self
):
1660 struct_fc
= self
._tc
.create_structure_field_class()
1661 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1662 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1663 str_fc
= self
._tc
.create_string_field_class()
1664 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1665 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1666 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1667 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1668 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1670 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1671 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1672 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1675 array
= stream
.create_packet().context_field
['array_field']
1676 array
.value
= values
1677 self
.assertEqual(values
, array
)
1678 values
[0]['an_int'] = 'a string'
1679 with self
.assertRaises(TypeError):
1680 array
.value
= values
1682 def test_str_op(self
):
1684 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1685 self
.assertEqual(expected_string
, s
)
1688 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1690 def _const_value_setter(field
):
1691 field
.value
= [45, 1847, 1948754]
1694 self
._tc
= get_default_trace_class()
1695 self
._def
= _create_int_array_field(self
._tc
, 3)
1698 self
._def
[2] = 1948754
1699 self
._def
_value
= [45, 1847, 1948754]
1700 self
._def
_const
= _create_const_field(
1702 self
._tc
.create_static_array_field_class(
1703 self
._tc
.create_signed_integer_field_class(32), 3
1705 self
._const
_value
_setter
,
1708 def test_value_wrong_len(self
):
1710 with self
.assertRaises(ValueError):
1711 self
._def
.value
= values
1714 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1716 def _const_value_setter(field
):
1717 field
.value
= [45, 1847, 1948754]
1720 self
._tc
= get_default_trace_class()
1721 self
._def
= _create_dynamic_array(self
._tc
)
1724 self
._def
[2] = 1948754
1725 self
._def
_value
= [45, 1847, 1948754]
1726 self
._def
_const
= _create_const_field(
1728 self
._tc
.create_dynamic_array_field_class(
1729 self
._tc
.create_signed_integer_field_class(32)
1731 self
._const
_value
_setter
,
1734 def test_value_resize(self
):
1735 new_values
= [1, 2, 3, 4]
1736 self
._def
.value
= new_values
1737 self
.assertCountEqual(self
._def
, new_values
)
1739 def test_set_length(self
):
1740 self
._def
.length
= 4
1742 self
.assertEqual(len(self
._def
), 4)
1744 def test_const_set_length(self
):
1745 with self
.assertRaises(AttributeError):
1746 self
._def
_const
.length
= 4
1747 self
.assertEqual(len(self
._def
), 3)
1749 def test_set_invalid_length(self
):
1750 with self
.assertRaises(TypeError):
1751 self
._def
.length
= 'cheval'
1754 class StructureFieldTestCase(unittest
.TestCase
):
1756 def _const_value_setter(field
):
1766 def _create_fc(self
, tc
):
1767 fc
= tc
.create_structure_field_class()
1768 fc
.append_member('A', self
._fc
0_fn
())
1769 fc
.append_member('B', self
._fc
1_fn
())
1770 fc
.append_member('C', self
._fc
2_fn
())
1771 fc
.append_member('D', self
._fc
3_fn
())
1772 fc
.append_member('E', self
._fc
4_fn
())
1773 fc5
= self
._fc
5_fn
()
1774 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1775 fc
.append_member('F', fc5
)
1779 self
._tc
= get_default_trace_class()
1780 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1781 self
._fc
1_fn
= self
._tc
.create_string_field_class
1782 self
._fc
2_fn
= self
._tc
.create_real_field_class
1783 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1784 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1785 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1786 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1788 self
._fc
= self
._create
_fc
(self
._tc
)
1789 self
._def
= _create_field(self
._tc
, self
._fc
)
1790 self
._def
['A'] = -1872
1791 self
._def
['B'] = 'salut'
1792 self
._def
['C'] = 17.5
1793 self
._def
['D'] = 16497
1795 self
._def
['F'] = {'F_1': 52}
1805 self
._def
_const
= _create_const_field(
1806 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1809 def _modify_def(self
):
1810 self
._def
['B'] = 'hola'
1812 def test_bool_op_true(self
):
1813 self
.assertTrue(self
._def
)
1815 def test_bool_op_false(self
):
1816 field
= self
._def
['E']
1817 self
.assertFalse(field
)
1820 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1822 def test_getitem(self
):
1823 field1
= self
._def
['A']
1824 field2
= self
._def
['B']
1825 field3
= self
._def
['C']
1826 field4
= self
._def
['D']
1827 field5
= self
._def
['E']
1828 field6
= self
._def
['F']
1830 self
.assertIs(type(field1
), bt2
._SignedIntegerField
)
1831 self
.assertEqual(field1
, -1872)
1833 self
.assertIs(type(field2
), bt2
._StringField
)
1834 self
.assertEqual(field2
, 'salut')
1836 self
.assertIs(type(field3
), bt2
._RealField
)
1837 self
.assertEqual(field3
, 17.5)
1839 self
.assertIs(type(field4
), bt2
._SignedIntegerField
)
1840 self
.assertEqual(field4
, 16497)
1842 self
.assertIs(type(field5
), bt2
._StructureField
)
1843 self
.assertEqual(field5
, {})
1845 self
.assertIs(type(field6
), bt2
._StructureField
)
1846 self
.assertEqual(field6
, {'F_1': 52})
1848 def test_const_getitem(self
):
1849 field1
= self
._def
_const
['A']
1850 field2
= self
._def
_const
['B']
1851 field3
= self
._def
_const
['C']
1852 field4
= self
._def
_const
['D']
1853 field5
= self
._def
_const
['E']
1854 field6
= self
._def
_const
['F']
1856 self
.assertIs(type(field1
), bt2
._SignedIntegerFieldConst
)
1857 self
.assertEqual(field1
, -1872)
1859 self
.assertIs(type(field2
), bt2
._StringFieldConst
)
1860 self
.assertEqual(field2
, 'salut')
1862 self
.assertIs(type(field3
), bt2
._RealFieldConst
)
1863 self
.assertEqual(field3
, 17.5)
1865 self
.assertIs(type(field4
), bt2
._SignedIntegerFieldConst
)
1866 self
.assertEqual(field4
, 16497)
1868 self
.assertIs(type(field5
), bt2
._StructureFieldConst
)
1869 self
.assertEqual(field5
, {})
1871 self
.assertIs(type(field6
), bt2
._StructureFieldConst
)
1872 self
.assertEqual(field6
, {'F_1': 52})
1874 def test_member_at_index_out_of_bounds_after(self
):
1875 with self
.assertRaises(IndexError):
1876 self
._def
.member_at_index(len(self
._def
_value
))
1879 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1881 field
['B'] = 'salut'
1885 field
['F'] = {'F_1': 52}
1886 self
.assertEqual(self
._def
, field
)
1888 def test_const_eq(self
):
1889 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1891 field
['B'] = 'salut'
1895 field
['F'] = {'F_1': 52}
1896 self
.assertEqual(self
._def
_const
, field
)
1898 def test_eq_invalid_type(self
):
1899 self
.assertNotEqual(self
._def
, 23)
1901 def test_eq_diff_len(self
):
1902 fc
= self
._tc
.create_structure_field_class()
1903 fc
.append_member('A', self
._fc
0_fn
())
1904 fc
.append_member('B', self
._fc
1_fn
())
1905 fc
.append_member('C', self
._fc
2_fn
())
1907 field
= _create_field(self
._tc
, fc
)
1909 field
['B'] = 'salut'
1911 self
.assertNotEqual(self
._def
, field
)
1913 def test_eq_diff_keys(self
):
1914 fc
= self
._tc
.create_structure_field_class()
1915 fc
.append_member('U', self
._fc
0_fn
())
1916 fc
.append_member('V', self
._fc
1_fn
())
1917 fc
.append_member('W', self
._fc
2_fn
())
1918 fc
.append_member('X', self
._fc
3_fn
())
1919 fc
.append_member('Y', self
._fc
4_fn
())
1920 fc
.append_member('Z', self
._fc
5_fn
())
1921 field
= _create_field(self
._tc
, fc
)
1923 field
['V'] = "gerry"
1928 self
.assertNotEqual(self
._def
, field
)
1930 def test_eq_diff_content_same_len(self
):
1931 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1933 field
['B'] = 'salut'
1937 field
['F'] = {'F_1': 0}
1938 self
.assertNotEqual(self
._def
, field
)
1940 def test_eq_same_content_diff_keys(self
):
1941 fc
= self
._tc
.create_structure_field_class()
1942 fc
.append_member('A', self
._fc
0_fn
())
1943 fc
.append_member('B', self
._fc
1_fn
())
1944 fc
.append_member('E', self
._fc
2_fn
())
1945 fc
.append_member('D', self
._fc
3_fn
())
1946 fc
.append_member('C', self
._fc
4_fn
())
1947 fc
.append_member('F', self
._fc
5_fn
())
1948 field
= _create_field(self
._tc
, fc
)
1950 field
['B'] = 'salut'
1955 self
.assertNotEqual(self
._def
, field
)
1957 def test_setitem(self
):
1958 self
._def
['C'] = -18.47
1959 self
.assertEqual(self
._def
['C'], -18.47)
1961 def test_const_setitem(self
):
1962 with self
.assertRaises(TypeError):
1963 self
._def
_const
['A'] = 134679
1965 def test_setitem_int_field(self
):
1966 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1967 int_field
= _create_field(self
._tc
, int_fc
)
1968 int_field
.value
= 19487
1969 self
._def
['D'] = int_field
1970 self
.assertEqual(self
._def
['D'], 19487)
1972 def test_setitem_non_basic_field(self
):
1973 elem_fc
= self
._tc
.create_structure_field_class()
1974 struct_fc
= self
._tc
.create_structure_field_class()
1975 struct_fc
.append_member('A', elem_fc
)
1976 struct_field
= _create_field(self
._tc
, struct_fc
)
1978 # Will fail on access to .items() of the value
1979 with self
.assertRaises(AttributeError):
1980 struct_field
['A'] = 23
1982 def test_setitem_none(self
):
1983 with self
.assertRaises(TypeError):
1984 self
._def
['C'] = None
1986 def test_setitem_key_wrong_type(self
):
1987 with self
.assertRaises(TypeError):
1990 def test_setitem_wrong_key(self
):
1991 with self
.assertRaises(KeyError):
1992 self
._def
['hi'] = 134679
1994 def test_member_at_index(self
):
1995 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1997 def test_const_member_at_index(self
):
1998 self
.assertEqual(self
._def
_const
.member_at_index(1), 'salut')
2000 def test_iter(self
):
2010 for vkey
, vval
in self
._def
.items():
2011 val
= orig_values
[vkey
]
2012 self
.assertEqual(vval
, val
)
2014 def test_value(self
):
2023 self
.assertEqual(self
._def
, orig_values
)
2025 def test_set_value(self
):
2026 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2027 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
2028 str_fc
= self
._tc
.create_string_field_class()
2029 struct_fc
= self
._tc
.create_structure_field_class()
2030 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
2031 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
2032 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
2033 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2035 struct
= _create_field(self
._tc
, struct_fc
)
2036 struct
.value
= values
2037 self
.assertEqual(values
, struct
)
2039 bad_type_values
= copy
.deepcopy(values
)
2040 bad_type_values
['an_int'] = 'a string'
2041 with self
.assertRaises(TypeError):
2042 struct
.value
= bad_type_values
2044 unknown_key_values
= copy
.deepcopy(values
)
2045 unknown_key_values
['unknown_key'] = 16546
2046 with self
.assertRaises(KeyError):
2047 struct
.value
= unknown_key_values
2049 def test_str_op(self
):
2050 expected_string_found
= False
2052 # Establish all permutations of the three expected matches since
2053 # the order in which mappings are enumerated is not explicitly part of
2055 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
2056 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
2057 candidate
= '{{{}}}'.format(', '.join(items
))
2059 expected_string_found
= True
2062 self
.assertTrue(expected_string_found
)
2065 class OptionFieldTestCase(unittest
.TestCase
):
2067 def _const_value_setter(field
):
2068 field
.value
= {'opt_field': 'hiboux'}
2070 def _create_fc(self
, tc
):
2071 fc
= tc
.create_option_field_class(tc
.create_string_field_class())
2072 top_fc
= tc
.create_structure_field_class()
2073 top_fc
.append_member('opt_field', fc
)
2077 self
._tc
= get_default_trace_class()
2078 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2079 self
._def
= fld
['opt_field']
2080 self
._def
_value
= 'hiboux'
2081 self
._def
_const
= _create_const_field(
2082 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
2085 def test_value_prop(self
):
2086 self
._def
.value
= 'hiboux'
2087 self
.assertEqual(self
._def
.field
, 'hiboux')
2088 self
.assertIs(type(self
._def
), bt2
._OptionField
)
2089 self
.assertIs(type(self
._def
.field
), bt2
._StringField
)
2090 self
.assertTrue(self
._def
.has_field
)
2092 def test_const_value_prop(self
):
2093 self
.assertEqual(self
._def
_const
.field
, 'hiboux')
2094 self
.assertIs(type(self
._def
_const
), bt2
._OptionFieldConst
)
2095 self
.assertIs(type(self
._def
_const
.field
), bt2
._StringFieldConst
)
2096 self
.assertTrue(self
._def
_const
.has_field
)
2098 def test_has_field_prop_true(self
):
2099 self
._def
.has_field
= True
2100 self
.assertTrue(self
._def
.has_field
)
2102 def test_has_field_prop_true(self
):
2103 self
._def
.has_field
= False
2104 self
.assertFalse(self
._def
.has_field
)
2106 def test_bool_op_true(self
):
2107 self
._def
.value
= 'allo'
2108 self
.assertTrue(self
._def
)
2110 def test_bool_op_true(self
):
2111 self
._def
.has_field
= False
2112 self
.assertFalse(self
._def
)
2114 def test_field_prop_existing(self
):
2115 self
._def
.value
= 'meow'
2116 field
= self
._def
.field
2117 self
.assertEqual(field
, 'meow')
2119 def test_field_prop_none(self
):
2120 self
._def
.has_field
= False
2121 field
= self
._def
.field
2122 self
.assertIsNone(field
)
2124 def test_const_field_prop(self
):
2125 with self
.assertRaises(AttributeError):
2126 self
._def
_const
.has_field
= False
2128 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2129 self
.assertTrue(self
._def
_const
.has_field
)
2131 def test_field_prop_existing_then_none(self
):
2132 self
._def
.value
= 'meow'
2133 field
= self
._def
.field
2134 self
.assertEqual(field
, 'meow')
2135 self
._def
.has_field
= False
2136 field
= self
._def
.field
2137 self
.assertIsNone(field
)
2140 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2141 field
= field
['opt_field']
2142 field
.value
= 'walk'
2143 self
._def
.value
= 'walk'
2144 self
.assertEqual(self
._def
, field
)
2146 def test_const_eq(self
):
2147 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2148 field
= field
['opt_field']
2149 field
.value
= 'hiboux'
2150 self
.assertEqual(self
._def
_const
, field
)
2151 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2153 def test_eq_invalid_type(self
):
2154 self
._def
.value
= 'gerry'
2155 self
.assertNotEqual(self
._def
, 23)
2157 def test_str_op(self
):
2158 self
._def
.value
= 'marcel'
2159 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
2161 def test_repr_op(self
):
2162 self
._def
.value
= 'mireille'
2163 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
2166 class VariantFieldTestCase(unittest
.TestCase
):
2168 def _const_value_setter(field
):
2169 field
.selected_option_index
= 3
2172 def _create_fc(self
, tc
):
2173 ft0
= tc
.create_signed_integer_field_class(32)
2174 ft1
= tc
.create_string_field_class()
2175 ft2
= tc
.create_real_field_class()
2176 ft3
= tc
.create_signed_integer_field_class(17)
2177 fc
= tc
.create_variant_field_class()
2178 fc
.append_option('corner', ft0
)
2179 fc
.append_option('zoom', ft1
)
2180 fc
.append_option('mellotron', ft2
)
2181 fc
.append_option('giorgio', ft3
)
2182 top_fc
= tc
.create_structure_field_class()
2183 top_fc
.append_member('variant_field', fc
)
2187 self
._tc
= get_default_trace_class()
2188 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2189 self
._def
= fld
['variant_field']
2191 self
._def
_value
= 1334
2192 self
._def
_selected
_index
= 3
2193 const_fc
= self
._create
_fc
(self
._tc
)['variant_field']
2195 fld_const
= _create_const_field(
2196 self
._tc
, const_fc
.field_class
, self
._const
_value
_setter
2198 self
._def
_const
= fld_const
2200 def test_bool_op(self
):
2201 self
._def
.selected_option_index
= 2
2202 self
._def
.value
= -17.34
2203 with self
.assertRaises(NotImplementedError):
2206 def test_selected_option_index(self
):
2207 self
._def
.selected_option_index
= 2
2208 self
.assertEqual(self
._def
.selected_option_index
, 2)
2210 def test_selected_option_index_above_range(self
):
2211 with self
.assertRaises(IndexError):
2212 self
._def
.selected_option_index
= 4
2214 def test_selected_option_index_below_range(self
):
2215 with self
.assertRaises(IndexError):
2216 self
._def
.selected_option_index
= -1
2218 def test_const_selected_option_index(self
):
2219 with self
.assertRaises(AttributeError):
2220 self
._def
_const
.selected_option_index
= 2
2221 self
.assertEqual(self
._def
_const
.selected_option_index
, 3)
2223 def test_selected_option(self
):
2224 self
._def
.selected_option_index
= 2
2225 self
._def
.value
= -17.34
2226 self
.assertEqual(self
._def
.selected_option
, -17.34)
2227 self
.assertEqual(type(self
._def
.selected_option
), bt2
._RealField
)
2229 self
._def
.selected_option_index
= 3
2230 self
._def
.value
= 1921
2231 self
.assertEqual(self
._def
.selected_option
, 1921)
2232 self
.assertEqual(type(self
._def
.selected_option
), bt2
._SignedIntegerField
)
2234 def test_const_selected_option(self
):
2235 self
.assertEqual(self
._def
_const
.selected_option
, 1334)
2237 type(self
._def
_const
.selected_option
), bt2
._SignedIntegerFieldConst
2241 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2242 field
= field
['variant_field']
2243 field
.selected_option_index
= 0
2245 self
._def
.selected_option_index
= 0
2246 self
._def
.value
= 1774
2247 self
.assertEqual(self
._def
, field
)
2249 def test_const_eq(self
):
2250 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2251 field
= field
['variant_field']
2252 field
.selected_option_index
= 3
2254 self
.assertEqual(self
._def
_const
, field
)
2257 self
.assertEqual(len(self
._def
), 4)
2259 def test_eq_invalid_type(self
):
2260 self
._def
.selected_option_index
= 1
2261 self
._def
.value
= 'gerry'
2262 self
.assertNotEqual(self
._def
, 23)
2264 def test_str_op_int(self
):
2265 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2266 field
= field
['variant_field']
2267 field
.selected_option_index
= 0
2269 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2270 other_field
= other_field
['variant_field']
2271 other_field
.selected_option_index
= 0
2272 other_field
.value
= 1774
2273 self
.assertEqual(str(field
), str(other_field
))
2275 def test_str_op_str(self
):
2276 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2277 field
= field
['variant_field']
2278 field
.selected_option_index
= 1
2279 field
.value
= 'un beau grand bateau'
2280 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2281 other_field
= other_field
['variant_field']
2282 other_field
.selected_option_index
= 1
2283 other_field
.value
= 'un beau grand bateau'
2284 self
.assertEqual(str(field
), str(other_field
))
2286 def test_str_op_float(self
):
2287 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2288 field
= field
['variant_field']
2289 field
.selected_option_index
= 2
2290 field
.value
= 14.4245
2291 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2292 other_field
= other_field
['variant_field']
2293 other_field
.selected_option_index
= 2
2294 other_field
.value
= 14.4245
2295 self
.assertEqual(str(field
), str(other_field
))