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_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_invalid_neg(self
):
1288 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1289 field
= _create_field(self
._tc
, uint_fc
)
1291 with self
.assertRaises(ValueError):
1294 def test_str_op(self
):
1295 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1298 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1301 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1303 def _const_value_setter(field
):
1306 def _create_fc(self
, tc
):
1307 return tc
.create_signed_integer_field_class(25)
1310 self
._tc
= get_default_trace_class()
1311 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1312 self
._field
.value
= 17
1313 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1314 self
._def
.value
= 17
1315 self
._def
_value
= 17
1316 self
._def
_const
= _create_const_field(
1317 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1319 self
._def
_new
_value
= -101
1322 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1324 def _const_value_setter(field
):
1327 def _create_fc(self
, tc
):
1328 fc
= tc
.create_signed_enumeration_field_class(32)
1329 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1330 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1331 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1333 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1335 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1339 self
._tc
= get_default_trace_class()
1340 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1341 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1342 self
._def
.value
= 17
1343 self
._def
_value
= 17
1344 self
._def
_const
= _create_const_field(
1345 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1347 self
._def
_new
_value
= -101
1349 def test_str_op(self
):
1350 expected_string_found
= False
1353 # Establish all permutations of the three expected matches since
1354 # the order in which mappings are enumerated is not explicitly part of
1356 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1357 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1359 expected_string_found
= True
1362 self
.assertTrue(expected_string_found
)
1364 def test_labels(self
):
1365 self
._field
.value
= 17
1366 labels
= sorted(self
._field
.labels
)
1367 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1370 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1372 def _const_value_setter(field
):
1375 def _create_fc(self
, tc
):
1376 return tc
.create_real_field_class()
1379 self
._tc
= get_default_trace_class()
1380 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1381 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1382 self
._def
_const
= _create_const_field(
1383 self
._tc
, self
._tc
.create_real_field_class(), self
._const
_value
_setter
1385 self
._def
.value
= 52.7
1386 self
._def
_value
= 52.7
1387 self
._def
_new
_value
= -17.164857
1389 def _test_invalid_op(self
, cb
):
1390 with self
.assertRaises(TypeError):
1393 def test_assign_true(self
):
1394 self
._def
.value
= True
1395 self
.assertTrue(self
._def
)
1397 def test_assign_false(self
):
1398 self
._def
.value
= False
1399 self
.assertFalse(self
._def
)
1401 def test_assign_pos_int(self
):
1403 self
._def
.value
= raw
1404 self
.assertEqual(self
._def
, float(raw
))
1406 def test_assign_neg_int(self
):
1408 self
._def
.value
= raw
1409 self
.assertEqual(self
._def
, float(raw
))
1411 def test_assign_int_field(self
):
1412 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1413 int_field
= _create_field(self
._tc
, int_fc
)
1415 int_field
.value
= raw
1416 self
._def
.value
= int_field
1417 self
.assertEqual(self
._def
, float(raw
))
1419 def test_assign_float(self
):
1421 self
._def
.value
= raw
1422 self
.assertEqual(self
._def
, raw
)
1424 def test_assign_float_field(self
):
1425 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1428 self
._def
.value
= field
1429 self
.assertEqual(self
._def
, raw
)
1431 def test_assign_invalid_type(self
):
1432 with self
.assertRaises(TypeError):
1433 self
._def
.value
= 'yes'
1435 def test_invalid_lshift(self
):
1436 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1438 def test_invalid_rshift(self
):
1439 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1441 def test_invalid_and(self
):
1442 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1444 def test_invalid_or(self
):
1445 self
._test
_invalid
_op
(lambda: self
._def |
23)
1447 def test_invalid_xor(self
):
1448 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1450 def test_invalid_invert(self
):
1451 self
._test
_invalid
_op
(lambda: ~self
._def
)
1453 def test_str_op(self
):
1454 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1457 _inject_numeric_testing_methods(RealFieldTestCase
)
1460 class StringFieldTestCase(unittest
.TestCase
):
1462 def _const_value_setter(field
):
1463 field
.value
= 'Hello, World!'
1466 self
._tc
= get_default_trace_class()
1467 self
._def
_value
= 'Hello, World!'
1468 self
._def
= _create_string_field(self
._tc
)
1469 self
._def
_const
= _create_const_field(
1470 self
._tc
, self
._tc
.create_string_field_class(), self
._const
_value
_setter
1472 self
._def
.value
= self
._def
_value
1473 self
._def
_new
_value
= 'Yes!'
1475 def test_assign_int(self
):
1476 with self
.assertRaises(TypeError):
1477 self
._def
.value
= 283
1479 def test_assign_string_field(self
):
1480 field
= _create_string_field(self
._tc
)
1483 self
.assertEqual(field
, raw
)
1486 self
.assertEqual(self
._def
, self
._def
_value
)
1488 def test_const_eq(self
):
1489 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1491 def test_not_eq(self
):
1492 self
.assertNotEqual(self
._def
, 23)
1494 def test_lt_vstring(self
):
1495 s1
= _create_string_field(self
._tc
)
1497 s2
= _create_string_field(self
._tc
)
1499 self
.assertLess(s1
, s2
)
1501 def test_lt_string(self
):
1502 s1
= _create_string_field(self
._tc
)
1504 self
.assertLess(s1
, 'bateau')
1506 def test_le_vstring(self
):
1507 s1
= _create_string_field(self
._tc
)
1509 s2
= _create_string_field(self
._tc
)
1511 self
.assertLessEqual(s1
, s2
)
1513 def test_le_string(self
):
1514 s1
= _create_string_field(self
._tc
)
1516 self
.assertLessEqual(s1
, 'bateau')
1518 def test_gt_vstring(self
):
1519 s1
= _create_string_field(self
._tc
)
1521 s2
= _create_string_field(self
._tc
)
1523 self
.assertGreater(s2
, s1
)
1525 def test_gt_string(self
):
1526 s1
= _create_string_field(self
._tc
)
1528 self
.assertGreater('bateau', s1
)
1530 def test_ge_vstring(self
):
1531 s1
= _create_string_field(self
._tc
)
1533 s2
= _create_string_field(self
._tc
)
1535 self
.assertGreaterEqual(s2
, s1
)
1537 def test_ge_string(self
):
1538 s1
= _create_string_field(self
._tc
)
1540 self
.assertGreaterEqual('bateau', s1
)
1542 def test_bool_op(self
):
1543 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1545 def test_str_op(self
):
1546 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1549 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1551 def test_getitem(self
):
1552 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1554 def test_const_getitem(self
):
1555 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1557 def test_append_str(self
):
1558 to_append
= 'meow meow meow'
1559 self
._def
+= to_append
1560 self
._def
_value
+= to_append
1561 self
.assertEqual(self
._def
, self
._def
_value
)
1563 def test_const_append_str(self
):
1564 to_append
= 'meow meow meow'
1565 with self
.assertRaises(TypeError):
1566 self
._def
_const
+= to_append
1567 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1569 def test_append_string_field(self
):
1570 field
= _create_string_field(self
._tc
)
1571 to_append
= 'meow meow meow'
1572 field
.value
= to_append
1574 self
._def
_value
+= to_append
1575 self
.assertEqual(self
._def
, self
._def
_value
)
1577 def test_hash_op(self
):
1578 with self
.assertRaises(TypeError):
1581 def test_const_hash_op(self
):
1582 self
.assertEqual(hash(self
._def
_const
), hash(self
._def
_value
))
1584 def test_const_hash_dict(self
):
1586 my_dict
[self
._def
_const
] = 'my_value'
1587 self
.assertEqual(my_dict
[self
._def
_value
], 'my_value')
1590 class _TestArrayFieldCommon
:
1591 def _modify_def(self
):
1594 def test_bool_op_true(self
):
1595 self
.assertTrue(self
._def
)
1598 self
.assertEqual(len(self
._def
), 3)
1600 def test_length(self
):
1601 self
.assertEqual(self
._def
.length
, 3)
1603 def test_getitem(self
):
1604 field
= self
._def
[1]
1605 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1606 self
.assertEqual(field
, 1847)
1608 def test_const_getitem(self
):
1609 field
= self
._def
_const
[1]
1610 self
.assertIs(type(field
), bt2
._SignedIntegerFieldConst
)
1611 self
.assertEqual(field
, 1847)
1614 field
= _create_int_array_field(self
._tc
, 3)
1618 self
.assertEqual(self
._def
, field
)
1620 def test_eq_invalid_type(self
):
1621 self
.assertNotEqual(self
._def
, 23)
1623 def test_eq_diff_len(self
):
1624 field
= _create_int_array_field(self
._tc
, 2)
1627 self
.assertNotEqual(self
._def
, field
)
1629 def test_eq_diff_content_same_len(self
):
1630 field
= _create_int_array_field(self
._tc
, 3)
1634 self
.assertNotEqual(self
._def
, field
)
1636 def test_eq_non_sequence_iterable(self
):
1637 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1638 field
= _create_int_array_field(self
._tc
, 3)
1642 self
.assertEqual(field
, list(dct
.keys()))
1643 self
.assertNotEqual(field
, dct
)
1645 def test_setitem(self
):
1647 self
.assertEqual(self
._def
[2], 24)
1649 def test_setitem_int_field(self
):
1650 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1651 int_field
= _create_field(self
._tc
, int_fc
)
1652 int_field
.value
= 19487
1653 self
._def
[1] = int_field
1654 self
.assertEqual(self
._def
[1], 19487)
1656 def test_setitem_non_basic_field(self
):
1657 array_field
= _create_struct_array_field(self
._tc
, 2)
1658 with self
.assertRaises(TypeError):
1661 def test_setitem_none(self
):
1662 with self
.assertRaises(TypeError):
1665 def test_setitem_index_wrong_type(self
):
1666 with self
.assertRaises(TypeError):
1667 self
._def
['yes'] = 23
1669 def test_setitem_index_neg(self
):
1670 with self
.assertRaises(IndexError):
1673 def test_setitem_index_out_of_range(self
):
1674 with self
.assertRaises(IndexError):
1675 self
._def
[len(self
._def
)] = 134679
1677 def test_const_setitem(self
):
1678 with self
.assertRaises(TypeError):
1679 self
._def
_const
[0] = 134679
1681 def test_iter(self
):
1682 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1683 self
.assertEqual(field
, value
)
1685 def test_const_iter(self
):
1686 for field
, value
in zip(self
._def
_const
, (45, 1847, 1948754)):
1687 self
.assertEqual(field
, value
)
1689 def test_value_int_field(self
):
1690 values
= [45646, 145, 12145]
1691 self
._def
.value
= values
1692 self
.assertEqual(values
, self
._def
)
1694 def test_value_check_sequence(self
):
1696 with self
.assertRaises(TypeError):
1697 self
._def
.value
= values
1699 def test_value_wrong_type_in_sequence(self
):
1700 values
= [32, 'hello', 11]
1701 with self
.assertRaises(TypeError):
1702 self
._def
.value
= values
1704 def test_value_complex_type(self
):
1705 struct_fc
= self
._tc
.create_structure_field_class()
1706 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1707 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1708 str_fc
= self
._tc
.create_string_field_class()
1709 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1710 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1711 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1712 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1713 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1715 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1716 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1717 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1720 array
= stream
.create_packet().context_field
['array_field']
1721 array
.value
= values
1722 self
.assertEqual(values
, array
)
1723 values
[0]['an_int'] = 'a string'
1724 with self
.assertRaises(TypeError):
1725 array
.value
= values
1727 def test_str_op(self
):
1729 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1730 self
.assertEqual(expected_string
, s
)
1733 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1735 def _const_value_setter(field
):
1736 field
.value
= [45, 1847, 1948754]
1739 self
._tc
= get_default_trace_class()
1740 self
._def
= _create_int_array_field(self
._tc
, 3)
1743 self
._def
[2] = 1948754
1744 self
._def
_value
= [45, 1847, 1948754]
1745 self
._def
_const
= _create_const_field(
1747 self
._tc
.create_static_array_field_class(
1748 self
._tc
.create_signed_integer_field_class(32), 3
1750 self
._const
_value
_setter
,
1753 def test_value_wrong_len(self
):
1755 with self
.assertRaises(ValueError):
1756 self
._def
.value
= values
1759 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1761 def _const_value_setter(field
):
1762 field
.value
= [45, 1847, 1948754]
1765 self
._tc
= get_default_trace_class()
1766 self
._def
= _create_dynamic_array(self
._tc
)
1769 self
._def
[2] = 1948754
1770 self
._def
_value
= [45, 1847, 1948754]
1771 self
._def
_const
= _create_const_field(
1773 self
._tc
.create_dynamic_array_field_class(
1774 self
._tc
.create_signed_integer_field_class(32)
1776 self
._const
_value
_setter
,
1779 def test_value_resize(self
):
1780 new_values
= [1, 2, 3, 4]
1781 self
._def
.value
= new_values
1782 self
.assertCountEqual(self
._def
, new_values
)
1784 def test_set_length(self
):
1785 self
._def
.length
= 4
1787 self
.assertEqual(len(self
._def
), 4)
1789 def test_const_set_length(self
):
1790 with self
.assertRaises(AttributeError):
1791 self
._def
_const
.length
= 4
1792 self
.assertEqual(len(self
._def
), 3)
1794 def test_set_invalid_length(self
):
1795 with self
.assertRaises(TypeError):
1796 self
._def
.length
= 'cheval'
1799 class StructureFieldTestCase(unittest
.TestCase
):
1801 def _const_value_setter(field
):
1811 def _create_fc(self
, tc
):
1812 fc
= tc
.create_structure_field_class()
1813 fc
.append_member('A', self
._fc
0_fn
())
1814 fc
.append_member('B', self
._fc
1_fn
())
1815 fc
.append_member('C', self
._fc
2_fn
())
1816 fc
.append_member('D', self
._fc
3_fn
())
1817 fc
.append_member('E', self
._fc
4_fn
())
1818 fc5
= self
._fc
5_fn
()
1819 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1820 fc
.append_member('F', fc5
)
1824 self
._tc
= get_default_trace_class()
1825 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1826 self
._fc
1_fn
= self
._tc
.create_string_field_class
1827 self
._fc
2_fn
= self
._tc
.create_real_field_class
1828 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1829 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1830 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1831 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1833 self
._fc
= self
._create
_fc
(self
._tc
)
1834 self
._def
= _create_field(self
._tc
, self
._fc
)
1835 self
._def
['A'] = -1872
1836 self
._def
['B'] = 'salut'
1837 self
._def
['C'] = 17.5
1838 self
._def
['D'] = 16497
1840 self
._def
['F'] = {'F_1': 52}
1850 self
._def
_const
= _create_const_field(
1851 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
1854 def _modify_def(self
):
1855 self
._def
['B'] = 'hola'
1857 def test_bool_op_true(self
):
1858 self
.assertTrue(self
._def
)
1860 def test_bool_op_false(self
):
1861 field
= self
._def
['E']
1862 self
.assertFalse(field
)
1865 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1867 def test_getitem(self
):
1868 field1
= self
._def
['A']
1869 field2
= self
._def
['B']
1870 field3
= self
._def
['C']
1871 field4
= self
._def
['D']
1872 field5
= self
._def
['E']
1873 field6
= self
._def
['F']
1875 self
.assertIs(type(field1
), bt2
._SignedIntegerField
)
1876 self
.assertEqual(field1
, -1872)
1878 self
.assertIs(type(field2
), bt2
._StringField
)
1879 self
.assertEqual(field2
, 'salut')
1881 self
.assertIs(type(field3
), bt2
._RealField
)
1882 self
.assertEqual(field3
, 17.5)
1884 self
.assertIs(type(field4
), bt2
._SignedIntegerField
)
1885 self
.assertEqual(field4
, 16497)
1887 self
.assertIs(type(field5
), bt2
._StructureField
)
1888 self
.assertEqual(field5
, {})
1890 self
.assertIs(type(field6
), bt2
._StructureField
)
1891 self
.assertEqual(field6
, {'F_1': 52})
1893 def test_const_getitem(self
):
1894 field1
= self
._def
_const
['A']
1895 field2
= self
._def
_const
['B']
1896 field3
= self
._def
_const
['C']
1897 field4
= self
._def
_const
['D']
1898 field5
= self
._def
_const
['E']
1899 field6
= self
._def
_const
['F']
1901 self
.assertIs(type(field1
), bt2
._SignedIntegerFieldConst
)
1902 self
.assertEqual(field1
, -1872)
1904 self
.assertIs(type(field2
), bt2
._StringFieldConst
)
1905 self
.assertEqual(field2
, 'salut')
1907 self
.assertIs(type(field3
), bt2
._RealFieldConst
)
1908 self
.assertEqual(field3
, 17.5)
1910 self
.assertIs(type(field4
), bt2
._SignedIntegerFieldConst
)
1911 self
.assertEqual(field4
, 16497)
1913 self
.assertIs(type(field5
), bt2
._StructureFieldConst
)
1914 self
.assertEqual(field5
, {})
1916 self
.assertIs(type(field6
), bt2
._StructureFieldConst
)
1917 self
.assertEqual(field6
, {'F_1': 52})
1919 def test_member_at_index_out_of_bounds_after(self
):
1920 with self
.assertRaises(IndexError):
1921 self
._def
.member_at_index(len(self
._def
_value
))
1924 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1926 field
['B'] = 'salut'
1930 field
['F'] = {'F_1': 52}
1931 self
.assertEqual(self
._def
, field
)
1933 def test_const_eq(self
):
1934 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1936 field
['B'] = 'salut'
1940 field
['F'] = {'F_1': 52}
1941 self
.assertEqual(self
._def
_const
, field
)
1943 def test_eq_invalid_type(self
):
1944 self
.assertNotEqual(self
._def
, 23)
1946 def test_eq_diff_len(self
):
1947 fc
= self
._tc
.create_structure_field_class()
1948 fc
.append_member('A', self
._fc
0_fn
())
1949 fc
.append_member('B', self
._fc
1_fn
())
1950 fc
.append_member('C', self
._fc
2_fn
())
1952 field
= _create_field(self
._tc
, fc
)
1954 field
['B'] = 'salut'
1956 self
.assertNotEqual(self
._def
, field
)
1958 def test_eq_diff_keys(self
):
1959 fc
= self
._tc
.create_structure_field_class()
1960 fc
.append_member('U', self
._fc
0_fn
())
1961 fc
.append_member('V', self
._fc
1_fn
())
1962 fc
.append_member('W', self
._fc
2_fn
())
1963 fc
.append_member('X', self
._fc
3_fn
())
1964 fc
.append_member('Y', self
._fc
4_fn
())
1965 fc
.append_member('Z', self
._fc
5_fn
())
1966 field
= _create_field(self
._tc
, fc
)
1968 field
['V'] = "gerry"
1973 self
.assertNotEqual(self
._def
, field
)
1975 def test_eq_diff_content_same_len(self
):
1976 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1978 field
['B'] = 'salut'
1982 field
['F'] = {'F_1': 0}
1983 self
.assertNotEqual(self
._def
, field
)
1985 def test_eq_same_content_diff_keys(self
):
1986 fc
= self
._tc
.create_structure_field_class()
1987 fc
.append_member('A', self
._fc
0_fn
())
1988 fc
.append_member('B', self
._fc
1_fn
())
1989 fc
.append_member('E', self
._fc
2_fn
())
1990 fc
.append_member('D', self
._fc
3_fn
())
1991 fc
.append_member('C', self
._fc
4_fn
())
1992 fc
.append_member('F', self
._fc
5_fn
())
1993 field
= _create_field(self
._tc
, fc
)
1995 field
['B'] = 'salut'
2000 self
.assertNotEqual(self
._def
, field
)
2002 def test_setitem(self
):
2003 self
._def
['C'] = -18.47
2004 self
.assertEqual(self
._def
['C'], -18.47)
2006 def test_const_setitem(self
):
2007 with self
.assertRaises(TypeError):
2008 self
._def
_const
['A'] = 134679
2010 def test_setitem_int_field(self
):
2011 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2012 int_field
= _create_field(self
._tc
, int_fc
)
2013 int_field
.value
= 19487
2014 self
._def
['D'] = int_field
2015 self
.assertEqual(self
._def
['D'], 19487)
2017 def test_setitem_non_basic_field(self
):
2018 elem_fc
= self
._tc
.create_structure_field_class()
2019 struct_fc
= self
._tc
.create_structure_field_class()
2020 struct_fc
.append_member('A', elem_fc
)
2021 struct_field
= _create_field(self
._tc
, struct_fc
)
2023 # Will fail on access to .items() of the value
2024 with self
.assertRaises(AttributeError):
2025 struct_field
['A'] = 23
2027 def test_setitem_none(self
):
2028 with self
.assertRaises(TypeError):
2029 self
._def
['C'] = None
2031 def test_setitem_key_wrong_type(self
):
2032 with self
.assertRaises(TypeError):
2035 def test_setitem_wrong_key(self
):
2036 with self
.assertRaises(KeyError):
2037 self
._def
['hi'] = 134679
2039 def test_member_at_index(self
):
2040 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
2042 def test_const_member_at_index(self
):
2043 self
.assertEqual(self
._def
_const
.member_at_index(1), 'salut')
2045 def test_iter(self
):
2055 for vkey
, vval
in self
._def
.items():
2056 val
= orig_values
[vkey
]
2057 self
.assertEqual(vval
, val
)
2059 def test_value(self
):
2068 self
.assertEqual(self
._def
, orig_values
)
2070 def test_set_value(self
):
2071 int_fc
= self
._tc
.create_signed_integer_field_class(32)
2072 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
2073 str_fc
= self
._tc
.create_string_field_class()
2074 struct_fc
= self
._tc
.create_structure_field_class()
2075 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
2076 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
2077 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
2078 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2080 struct
= _create_field(self
._tc
, struct_fc
)
2081 struct
.value
= values
2082 self
.assertEqual(values
, struct
)
2084 bad_type_values
= copy
.deepcopy(values
)
2085 bad_type_values
['an_int'] = 'a string'
2086 with self
.assertRaises(TypeError):
2087 struct
.value
= bad_type_values
2089 unknown_key_values
= copy
.deepcopy(values
)
2090 unknown_key_values
['unknown_key'] = 16546
2091 with self
.assertRaises(KeyError):
2092 struct
.value
= unknown_key_values
2094 def test_str_op(self
):
2095 expected_string_found
= False
2097 # Establish all permutations of the three expected matches since
2098 # the order in which mappings are enumerated is not explicitly part of
2100 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
2101 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
2102 candidate
= '{{{}}}'.format(', '.join(items
))
2104 expected_string_found
= True
2107 self
.assertTrue(expected_string_found
)
2110 class OptionFieldTestCase(unittest
.TestCase
):
2112 def _const_value_setter(field
):
2113 field
.value
= {'opt_field': 'hiboux'}
2115 def _create_fc(self
, tc
):
2116 fc
= tc
.create_option_field_class(tc
.create_string_field_class())
2117 top_fc
= tc
.create_structure_field_class()
2118 top_fc
.append_member('opt_field', fc
)
2122 self
._tc
= get_default_trace_class()
2123 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2124 self
._def
= fld
['opt_field']
2125 self
._def
_value
= 'hiboux'
2126 self
._def
_const
= _create_const_field(
2127 self
._tc
, self
._create
_fc
(self
._tc
), self
._const
_value
_setter
2130 def test_value_prop(self
):
2131 self
._def
.value
= 'hiboux'
2132 self
.assertEqual(self
._def
.field
, 'hiboux')
2133 self
.assertIs(type(self
._def
), bt2
._OptionField
)
2134 self
.assertIs(type(self
._def
.field
), bt2
._StringField
)
2135 self
.assertTrue(self
._def
.has_field
)
2137 def test_const_value_prop(self
):
2138 self
.assertEqual(self
._def
_const
.field
, 'hiboux')
2139 self
.assertIs(type(self
._def
_const
), bt2
._OptionFieldConst
)
2140 self
.assertIs(type(self
._def
_const
.field
), bt2
._StringFieldConst
)
2141 self
.assertTrue(self
._def
_const
.has_field
)
2143 def test_has_field_prop_true(self
):
2144 self
._def
.has_field
= True
2145 self
.assertTrue(self
._def
.has_field
)
2147 def test_has_field_prop_true(self
):
2148 self
._def
.has_field
= False
2149 self
.assertFalse(self
._def
.has_field
)
2151 def test_bool_op_true(self
):
2152 self
._def
.value
= 'allo'
2153 self
.assertTrue(self
._def
)
2155 def test_bool_op_true(self
):
2156 self
._def
.has_field
= False
2157 self
.assertFalse(self
._def
)
2159 def test_field_prop_existing(self
):
2160 self
._def
.value
= 'meow'
2161 field
= self
._def
.field
2162 self
.assertEqual(field
, 'meow')
2164 def test_field_prop_none(self
):
2165 self
._def
.has_field
= False
2166 field
= self
._def
.field
2167 self
.assertIsNone(field
)
2169 def test_const_field_prop(self
):
2170 with self
.assertRaises(AttributeError):
2171 self
._def
_const
.has_field
= False
2173 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2174 self
.assertTrue(self
._def
_const
.has_field
)
2176 def test_field_prop_existing_then_none(self
):
2177 self
._def
.value
= 'meow'
2178 field
= self
._def
.field
2179 self
.assertEqual(field
, 'meow')
2180 self
._def
.has_field
= False
2181 field
= self
._def
.field
2182 self
.assertIsNone(field
)
2185 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2186 field
= field
['opt_field']
2187 field
.value
= 'walk'
2188 self
._def
.value
= 'walk'
2189 self
.assertEqual(self
._def
, field
)
2191 def test_const_eq(self
):
2192 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2193 field
= field
['opt_field']
2194 field
.value
= 'hiboux'
2195 self
.assertEqual(self
._def
_const
, field
)
2196 self
.assertEqual(self
._def
_const
, self
._def
_value
)
2198 def test_eq_invalid_type(self
):
2199 self
._def
.value
= 'gerry'
2200 self
.assertNotEqual(self
._def
, 23)
2202 def test_str_op(self
):
2203 self
._def
.value
= 'marcel'
2204 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
2206 def test_repr_op(self
):
2207 self
._def
.value
= 'mireille'
2208 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
2211 class VariantFieldTestCase(unittest
.TestCase
):
2213 def _const_value_setter(field
):
2214 field
.selected_option_index
= 3
2217 def _create_fc(self
, tc
):
2218 ft0
= tc
.create_signed_integer_field_class(32)
2219 ft1
= tc
.create_string_field_class()
2220 ft2
= tc
.create_real_field_class()
2221 ft3
= tc
.create_signed_integer_field_class(17)
2222 fc
= tc
.create_variant_field_class()
2223 fc
.append_option('corner', ft0
)
2224 fc
.append_option('zoom', ft1
)
2225 fc
.append_option('mellotron', ft2
)
2226 fc
.append_option('giorgio', ft3
)
2227 top_fc
= tc
.create_structure_field_class()
2228 top_fc
.append_member('variant_field', fc
)
2232 self
._tc
= get_default_trace_class()
2233 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2234 self
._def
= fld
['variant_field']
2236 self
._def
_value
= 1334
2237 self
._def
_selected
_index
= 3
2238 const_fc
= self
._create
_fc
(self
._tc
)['variant_field']
2240 fld_const
= _create_const_field(
2241 self
._tc
, const_fc
.field_class
, self
._const
_value
_setter
2243 self
._def
_const
= fld_const
2245 def test_bool_op(self
):
2246 self
._def
.selected_option_index
= 2
2247 self
._def
.value
= -17.34
2248 with self
.assertRaises(NotImplementedError):
2251 def test_selected_option_index(self
):
2252 self
._def
.selected_option_index
= 2
2253 self
.assertEqual(self
._def
.selected_option_index
, 2)
2255 def test_selected_option_index_above_range(self
):
2256 with self
.assertRaises(IndexError):
2257 self
._def
.selected_option_index
= 4
2259 def test_selected_option_index_below_range(self
):
2260 with self
.assertRaises(IndexError):
2261 self
._def
.selected_option_index
= -1
2263 def test_const_selected_option_index(self
):
2264 with self
.assertRaises(AttributeError):
2265 self
._def
_const
.selected_option_index
= 2
2266 self
.assertEqual(self
._def
_const
.selected_option_index
, 3)
2268 def test_selected_option(self
):
2269 self
._def
.selected_option_index
= 2
2270 self
._def
.value
= -17.34
2271 self
.assertEqual(self
._def
.selected_option
, -17.34)
2272 self
.assertEqual(type(self
._def
.selected_option
), bt2
._RealField
)
2274 self
._def
.selected_option_index
= 3
2275 self
._def
.value
= 1921
2276 self
.assertEqual(self
._def
.selected_option
, 1921)
2277 self
.assertEqual(type(self
._def
.selected_option
), bt2
._SignedIntegerField
)
2279 def test_const_selected_option(self
):
2280 self
.assertEqual(self
._def
_const
.selected_option
, 1334)
2282 type(self
._def
_const
.selected_option
), bt2
._SignedIntegerFieldConst
2286 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2287 field
= field
['variant_field']
2288 field
.selected_option_index
= 0
2290 self
._def
.selected_option_index
= 0
2291 self
._def
.value
= 1774
2292 self
.assertEqual(self
._def
, field
)
2294 def test_const_eq(self
):
2295 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2296 field
= field
['variant_field']
2297 field
.selected_option_index
= 3
2299 self
.assertEqual(self
._def
_const
, field
)
2302 self
.assertEqual(len(self
._def
), 4)
2304 def test_eq_invalid_type(self
):
2305 self
._def
.selected_option_index
= 1
2306 self
._def
.value
= 'gerry'
2307 self
.assertNotEqual(self
._def
, 23)
2309 def test_str_op_int(self
):
2310 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2311 field
= field
['variant_field']
2312 field
.selected_option_index
= 0
2314 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2315 other_field
= other_field
['variant_field']
2316 other_field
.selected_option_index
= 0
2317 other_field
.value
= 1774
2318 self
.assertEqual(str(field
), str(other_field
))
2320 def test_str_op_str(self
):
2321 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2322 field
= field
['variant_field']
2323 field
.selected_option_index
= 1
2324 field
.value
= 'un beau grand bateau'
2325 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2326 other_field
= other_field
['variant_field']
2327 other_field
.selected_option_index
= 1
2328 other_field
.value
= 'un beau grand bateau'
2329 self
.assertEqual(str(field
), str(other_field
))
2331 def test_str_op_float(self
):
2332 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2333 field
= field
['variant_field']
2334 field
.selected_option_index
= 2
2335 field
.value
= 14.4245
2336 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
2337 other_field
= other_field
['variant_field']
2338 other_field
.selected_option_index
= 2
2339 other_field
.value
= 14.4245
2340 self
.assertEqual(str(field
), str(other_field
))
2343 if __name__
== '__main__':