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
36 # Create and return a stream with the field classes part of its stream packet
39 # The stream is part of a dummy trace created from trace class `tc`.
41 def _create_stream(tc
, ctx_field_classes
):
42 packet_context_fc
= tc
.create_structure_field_class()
43 for name
, fc
in ctx_field_classes
:
44 packet_context_fc
.append_member(name
, fc
)
47 stream_class
= tc
.create_stream_class(packet_context_field_class
=packet_context_fc
,
48 supports_packets
=True)
50 stream
= trace
.create_stream(stream_class
)
54 # Create a field of the given field class.
56 # The field is part of a dummy stream, itself part of a dummy trace created
57 # from trace class `tc`.
59 def _create_field(tc
, field_class
):
61 stream
= _create_stream(tc
, [(field_name
, field_class
)])
62 packet
= stream
.create_packet()
63 return packet
.context_field
[field_name
]
66 # Create a field of type string.
68 # The field is part of a dummy stream, itself part of a dummy trace created
69 # from trace class `tc`. It is made out of a dummy string field class.
71 def _create_string_field(tc
):
72 field_name
= 'string_field'
73 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
74 packet
= stream
.create_packet()
75 return packet
.context_field
[field_name
]
78 # Create a field of type static array of ints.
80 # The field is part of a dummy stream, itself part of a dummy trace created
81 # from trace class `tc`. It is made out of a dummy static array field class,
82 # with a dummy integer field class as element class.
84 def _create_int_array_field(tc
, length
):
85 elem_fc
= tc
.create_signed_integer_field_class(32)
86 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
87 field_name
= 'int_array'
88 stream
= _create_stream(tc
, [(field_name
, fc
)])
89 packet
= stream
.create_packet()
90 return packet
.context_field
[field_name
]
93 # Create a field of type dynamic array of ints.
95 # The field is part of a dummy stream, itself part of a dummy trace created
96 # from trace class `tc`. It is made out of a dummy static array field class,
97 # with a dummy integer field class as element and length classes.
99 def _create_dynamic_array(tc
):
100 elem_fc
= tc
.create_signed_integer_field_class(32)
101 len_fc
= tc
.create_signed_integer_field_class(32)
102 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
103 field_name
= 'int_dyn_array'
104 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
105 packet
= stream
.create_packet()
106 packet
.context_field
[field_name
].length
= 3
107 return packet
.context_field
[field_name
]
110 # Create a field of type array of (empty) structures.
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 static array field class,
114 # with a dummy struct field class as element class.
116 def _create_struct_array_field(tc
, length
):
117 elem_fc
= tc
.create_structure_field_class()
118 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
119 field_name
= 'struct_array'
120 stream
= _create_stream(tc
, [(field_name
, fc
)])
121 packet
= stream
.create_packet()
122 return packet
.context_field
[field_name
]
125 # Base class for numeric field test cases.
127 # To be compatible with this base class, a derived class must, in its
130 # * Set `self._def` to a field object with an arbitrary value.
131 # * Set `self._def_value` to the equivalent value of `self._def`.
132 class _TestNumericField
:
133 # Tries the binary operation `op`:
135 # 1. Between `self._def`, which is a field object, and `rhs`.
136 # 2. Between `self._def_value`, which is the raw value of
137 # `self._def`, and `rhs`.
139 # Returns the results of 1. and 2.
141 # If there's an exception while performing 1. or 2., asserts that
142 # both operations raised exceptions, that both exceptions have the
143 # same type, and returns `None` for both results.
144 def _binop(self
, op
, rhs
):
149 # try with field object
151 r
= op(self
._def
, rhs
)
152 except Exception as e
:
157 rv
= op(self
._def
_value
, comp_value
)
158 except Exception as e
:
161 if type_rexc
is not None or type_rvexc
is not None:
162 # at least one of the operations raised an exception: in
163 # this case both operations should have raised the same
164 # type of exception (division by zero, bit shift with a
165 # floating point number operand, etc.)
166 self
.assertIs(type_rexc
, type_rvexc
)
171 # Tries the unary operation `op`:
173 # 1. On `self._def`, which is a field object.
174 # 2. On `self._def_value`, which is the value of `self._def`.
176 # Returns the results of 1. and 2.
178 # If there's an exception while performing 1. or 2., asserts that
179 # both operations raised exceptions, that both exceptions have the
180 # same type, and returns `None` for both results.
181 def _unaryop(self
, op
):
185 # try with field object
188 except Exception as e
:
193 rv
= op(self
._def
_value
)
194 except Exception as e
:
197 if type_rexc
is not None or type_rvexc
is not None:
198 # at least one of the operations raised an exception: in
199 # this case both operations should have raised the same
200 # type of exception (division by zero, bit shift with a
201 # floating point number operand, etc.)
202 self
.assertIs(type_rexc
, type_rvexc
)
207 # Tests that the unary operation `op` gives results with the same
208 # type for both `self._def` and `self._def_value`.
209 def _test_unaryop_type(self
, op
):
210 r
, rv
= self
._unaryop
(op
)
215 self
.assertIsInstance(r
, type(rv
))
217 # Tests that the unary operation `op` gives results with the same
218 # value for both `self._def` and `self._def_value`. This uses the
219 # __eq__() operator of `self._def`.
220 def _test_unaryop_value(self
, op
):
221 r
, rv
= self
._unaryop
(op
)
226 self
.assertEqual(r
, rv
)
228 # Tests that the unary operation `op`, when applied to `self._def`,
229 # does not change its underlying BT object address.
230 def _test_unaryop_addr_same(self
, op
):
231 addr_before
= self
._def
.addr
233 self
.assertEqual(self
._def
.addr
, addr_before
)
235 # Tests that the unary operation `op`, when applied to `self._def`,
236 # does not change its value.
237 def _test_unaryop_value_same(self
, op
):
238 value_before
= copy
.copy(self
._def
_value
)
240 self
.assertEqual(self
._def
, value_before
)
242 # Tests that the binary operation `op` gives results with the same
243 # type for both `self._def` and `self._def_value`.
244 def _test_binop_type(self
, op
, rhs
):
245 r
, rv
= self
._binop
(op
, rhs
)
250 if op
in _COMP_BINOPS
:
251 # __eq__() and __ne__() always return a 'bool' object
252 self
.assertIsInstance(r
, bool)
254 self
.assertIsInstance(r
, type(rv
))
256 # Tests that the binary operation `op` gives results with the same
257 # value for both `self._def` and `self._def_value`. This uses the
258 # __eq__() operator of `self._def`.
259 def _test_binop_value(self
, op
, rhs
):
260 r
, rv
= self
._binop
(op
, rhs
)
265 self
.assertEqual(r
, rv
)
267 # Tests that the binary operation `op`, when applied to `self._def`,
268 # does not change its underlying BT object address.
269 def _test_binop_lhs_addr_same(self
, op
, rhs
):
270 addr_before
= self
._def
.addr
271 r
, rv
= self
._binop
(op
, rhs
)
272 self
.assertEqual(self
._def
.addr
, addr_before
)
274 # Tests that the binary operation `op`, when applied to `self._def`,
275 # does not change its value.
276 @unittest.skip('copy is not implemented')
277 def _test_binop_lhs_value_same(self
, op
, rhs
):
278 value_before
= copy
.copy(self
._def
)
279 r
, rv
= self
._binop
(op
, rhs
)
280 self
.assertEqual(self
._def
, value_before
)
282 # The methods below which take the `test_cb` and/or `op` parameters
283 # are meant to be used with one of the _test_binop_*() functions
284 # above as `test_cb` and a binary operator function as `op`.
288 # self._test_binop_rhs_pos_int(self._test_binop_value,
291 # This tests that a numeric field object added to a positive integer
292 # value gives a result with the expected value.
294 # `vint` and `vfloat` mean a signed integer value object and a real
297 def _test_binop_invalid_unknown(self
, op
):
298 if op
in _COMP_BINOPS
:
299 self
.skipTest('not testing')
304 with self
.assertRaises(TypeError):
307 def _test_binop_invalid_none(self
, op
):
308 if op
in _COMP_BINOPS
:
309 self
.skipTest('not testing')
311 with self
.assertRaises(TypeError):
314 def _test_binop_rhs_false(self
, test_cb
, op
):
317 def _test_binop_rhs_true(self
, test_cb
, op
):
320 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
323 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
326 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
329 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
330 test_cb(op
, bt2
.create_value(2))
332 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
333 test_cb(op
, bt2
.create_value(-23))
335 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
336 test_cb(op
, bt2
.create_value(0))
338 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
341 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
344 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
347 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
348 test_cb(op
, bt2
.create_value(2.2))
350 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
351 test_cb(op
, bt2
.create_value(-23.4))
353 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
354 test_cb(op
, bt2
.create_value(0.0))
356 def _test_binop_rhs_complex(self
, test_cb
, op
):
359 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
362 def _test_binop_type_false(self
, op
):
363 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
365 def _test_binop_type_true(self
, op
):
366 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
368 def _test_binop_type_pos_int(self
, op
):
369 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
371 def _test_binop_type_neg_int(self
, op
):
372 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
374 def _test_binop_type_zero_int(self
, op
):
375 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
377 def _test_binop_type_pos_vint(self
, op
):
378 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
380 def _test_binop_type_neg_vint(self
, op
):
381 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
383 def _test_binop_type_zero_vint(self
, op
):
384 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
386 def _test_binop_type_pos_float(self
, op
):
387 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
389 def _test_binop_type_neg_float(self
, op
):
390 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
392 def _test_binop_type_zero_float(self
, op
):
393 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
395 def _test_binop_type_pos_vfloat(self
, op
):
396 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
398 def _test_binop_type_neg_vfloat(self
, op
):
399 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
401 def _test_binop_type_zero_vfloat(self
, op
):
402 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
404 def _test_binop_type_complex(self
, op
):
405 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
407 def _test_binop_type_zero_complex(self
, op
):
408 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
410 def _test_binop_value_false(self
, op
):
411 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
413 def _test_binop_value_true(self
, op
):
414 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
416 def _test_binop_value_pos_int(self
, op
):
417 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
419 def _test_binop_value_neg_int(self
, op
):
420 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
422 def _test_binop_value_zero_int(self
, op
):
423 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
425 def _test_binop_value_pos_vint(self
, op
):
426 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
428 def _test_binop_value_neg_vint(self
, op
):
429 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
431 def _test_binop_value_zero_vint(self
, op
):
432 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
434 def _test_binop_value_pos_float(self
, op
):
435 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
437 def _test_binop_value_neg_float(self
, op
):
438 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
440 def _test_binop_value_zero_float(self
, op
):
441 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
443 def _test_binop_value_pos_vfloat(self
, op
):
444 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
446 def _test_binop_value_neg_vfloat(self
, op
):
447 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
449 def _test_binop_value_zero_vfloat(self
, op
):
450 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
452 def _test_binop_value_complex(self
, op
):
453 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
455 def _test_binop_value_zero_complex(self
, op
):
456 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
458 def _test_binop_lhs_addr_same_false(self
, op
):
459 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
461 def _test_binop_lhs_addr_same_true(self
, op
):
462 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
464 def _test_binop_lhs_addr_same_pos_int(self
, op
):
465 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
467 def _test_binop_lhs_addr_same_neg_int(self
, op
):
468 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
470 def _test_binop_lhs_addr_same_zero_int(self
, op
):
471 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
473 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
474 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
476 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
477 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
479 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
480 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
482 def _test_binop_lhs_addr_same_pos_float(self
, op
):
483 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
485 def _test_binop_lhs_addr_same_neg_float(self
, op
):
486 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
488 def _test_binop_lhs_addr_same_zero_float(self
, op
):
489 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
491 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
492 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
494 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
495 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
497 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
498 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
500 def _test_binop_lhs_addr_same_complex(self
, op
):
501 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
503 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
504 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
506 def _test_binop_lhs_value_same_false(self
, op
):
507 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
509 def _test_binop_lhs_value_same_true(self
, op
):
510 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
512 def _test_binop_lhs_value_same_pos_int(self
, op
):
513 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
515 def _test_binop_lhs_value_same_neg_int(self
, op
):
516 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
518 def _test_binop_lhs_value_same_zero_int(self
, op
):
519 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
521 def _test_binop_lhs_value_same_pos_vint(self
, op
):
522 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
524 def _test_binop_lhs_value_same_neg_vint(self
, op
):
525 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
527 def _test_binop_lhs_value_same_zero_vint(self
, op
):
528 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
530 def _test_binop_lhs_value_same_pos_float(self
, op
):
531 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
533 def _test_binop_lhs_value_same_neg_float(self
, op
):
534 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
536 def _test_binop_lhs_value_same_zero_float(self
, op
):
537 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
539 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
540 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
542 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
543 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
545 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
546 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
548 def _test_binop_lhs_value_same_complex(self
, op
):
549 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
551 def _test_binop_lhs_value_same_zero_complex(self
, op
):
552 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
554 def test_bool_op(self
):
555 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
557 def test_int_op(self
):
558 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
560 def test_float_op(self
):
561 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
563 def test_complex_op(self
):
564 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
566 def test_str_op(self
):
567 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
569 def test_eq_none(self
):
570 # Ignore this lint error:
571 # E711 comparison to None should be 'if cond is None:'
572 # since this is what we want to test (even though not good practice).
573 self
.assertFalse(self
._def
== None) # noqa: E711
575 def test_ne_none(self
):
576 # Ignore this lint error:
577 # E711 comparison to None should be 'if cond is not None:'
578 # since this is what we want to test (even though not good practice).
579 self
.assertTrue(self
._def
!= None) # noqa: E711
582 # This is a list of binary operators used for
583 # _inject_numeric_testing_methods().
585 # Each entry is a pair of binary operator name (used as part of the
586 # created testing method's name) and operator function.
594 ('add', operator
.add
),
595 ('radd', lambda a
, b
: operator
.add(b
, a
)),
596 ('and', operator
.and_
),
597 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
598 ('floordiv', operator
.floordiv
),
599 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
600 ('lshift', operator
.lshift
),
601 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
602 ('mod', operator
.mod
),
603 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
604 ('mul', operator
.mul
),
605 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
606 ('or', operator
.or_
),
607 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
608 ('pow', operator
.pow),
609 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
610 ('rshift', operator
.rshift
),
611 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
612 ('sub', operator
.sub
),
613 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
614 ('truediv', operator
.truediv
),
615 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
616 ('xor', operator
.xor
),
617 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
621 # This is a list of unary operators used for
622 # _inject_numeric_testing_methods().
624 # Each entry is a pair of unary operator name (used as part of the
625 # created testing method's name) and operator function.
627 ('neg', operator
.neg
),
628 ('pos', operator
.pos
),
629 ('abs', operator
.abs),
630 ('invert', operator
.invert
),
632 ('round_0', partial(round, ndigits
=0)),
633 ('round_1', partial(round, ndigits
=1)),
634 ('round_2', partial(round, ndigits
=2)),
635 ('round_3', partial(round, ndigits
=3)),
637 ('floor', math
.floor
),
638 ('trunc', math
.trunc
),
642 # This function injects a bunch of testing methods to a numeric
645 # It is meant to be used like this:
647 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
649 # This function injects:
651 # * One testing method for each _TestNumericField._test_binop_*()
652 # method, for each binary operator in the _BINOPS tuple.
654 # * One testing method for each _TestNumericField._test_unaryop*()
655 # method, for each unary operator in the _UNARYOPS tuple.
656 def _inject_numeric_testing_methods(cls
):
657 def test_binop_name(suffix
):
658 return 'test_binop_{}_{}'.format(name
, suffix
)
660 def test_unaryop_name(suffix
):
661 return 'test_unaryop_{}_{}'.format(name
, suffix
)
663 # inject testing methods for each binary operation
664 for name
, binop
in _BINOPS
:
665 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
))
666 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
))
667 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
))
668 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
))
669 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
))
670 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
))
671 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
))
672 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
))
673 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
674 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
675 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
676 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
677 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
678 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
679 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
))
680 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
))
681 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
))
682 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
))
683 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
684 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
685 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
686 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
687 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
))
688 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
))
689 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
))
690 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
))
691 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
))
692 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
))
693 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
694 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
695 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
696 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
697 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
698 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
699 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
))
700 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
))
701 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
702 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
703 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
))
704 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
))
705 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
706 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
707 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
708 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
709 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
710 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
711 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
712 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
713 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
714 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
715 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
))
716 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
717 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
))
718 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
719 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
720 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
721 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
722 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
723 setattr(cls
, test_binop_name('type_complex'), partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
))
724 setattr(cls
, test_binop_name('type_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
))
725 setattr(cls
, test_binop_name('value_complex'), partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
))
726 setattr(cls
, test_binop_name('value_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
))
727 setattr(cls
, test_binop_name('lhs_addr_same_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
))
728 setattr(cls
, test_binop_name('lhs_addr_same_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
))
729 setattr(cls
, test_binop_name('lhs_value_same_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
))
730 setattr(cls
, test_binop_name('lhs_value_same_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
))
732 # inject testing methods for each unary operation
733 for name
, unaryop
in _UNARYOPS
:
734 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
))
735 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
))
736 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
))
737 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
))
740 class _TestIntegerFieldCommon(_TestNumericField
):
741 def test_assign_true(self
):
743 self
._def
.value
= raw
744 self
.assertEqual(self
._def
, raw
)
746 def test_assign_false(self
):
748 self
._def
.value
= raw
749 self
.assertEqual(self
._def
, raw
)
751 def test_assign_pos_int(self
):
753 self
._def
.value
= raw
754 self
.assertEqual(self
._def
, raw
)
756 def test_assign_neg_int(self
):
758 self
._def
.value
= raw
759 self
.assertEqual(self
._def
, raw
)
761 def test_assign_int_field(self
):
763 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
765 self
._def
.value
= field
766 self
.assertEqual(self
._def
, raw
)
768 def test_assign_invalid_type(self
):
769 with self
.assertRaises(TypeError):
770 self
._def
.value
= 'yes'
772 def test_assign_uint(self
):
773 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
774 field
= _create_field(self
._tc
, uint_fc
)
777 self
.assertEqual(field
, raw
)
779 def test_assign_big_uint(self
):
780 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
781 field
= _create_field(self
._tc
, uint_fc
)
782 # Larger than the IEEE 754 double-precision exact representation of
785 field
.value
= (2**53) + 1
786 self
.assertEqual(field
, raw
)
788 def test_assign_uint_invalid_neg(self
):
789 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
790 field
= _create_field(self
._tc
, uint_fc
)
792 with self
.assertRaises(ValueError):
795 def test_str_op(self
):
796 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
799 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
802 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
803 def _create_fc(self
, tc
):
804 return tc
.create_signed_integer_field_class(25)
807 self
._tc
= get_default_trace_class()
808 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
809 self
._field
.value
= 17
810 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
813 self
._def
_new
_value
= -101
816 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
817 def _create_fc(self
, tc
):
818 fc
= tc
.create_signed_enumeration_field_class(32)
819 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
820 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
821 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
822 fc
.add_mapping('whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)]))
823 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
827 self
._tc
= get_default_trace_class()
828 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
829 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
832 self
._def
_new
_value
= -101
834 def test_str_op(self
):
835 expected_string_found
= False
838 # Establish all permutations of the three expected matches since
839 # the order in which mappings are enumerated is not explicitly part of
841 for p
in itertools
.permutations(['whole range', 'something',
843 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
845 expected_string_found
= True
848 self
.assertTrue(expected_string_found
)
850 def test_labels(self
):
851 self
._field
.value
= 17
852 labels
= sorted(self
._field
.labels
)
853 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
856 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
857 def _create_fc(self
, tc
):
858 return tc
.create_real_field_class()
861 self
._tc
= get_default_trace_class()
862 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
863 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
864 self
._def
.value
= 52.7
865 self
._def
_value
= 52.7
866 self
._def
_new
_value
= -17.164857
868 def _test_invalid_op(self
, cb
):
869 with self
.assertRaises(TypeError):
872 def test_assign_true(self
):
873 self
._def
.value
= True
874 self
.assertTrue(self
._def
)
876 def test_assign_false(self
):
877 self
._def
.value
= False
878 self
.assertFalse(self
._def
)
880 def test_assign_pos_int(self
):
882 self
._def
.value
= raw
883 self
.assertEqual(self
._def
, float(raw
))
885 def test_assign_neg_int(self
):
887 self
._def
.value
= raw
888 self
.assertEqual(self
._def
, float(raw
))
890 def test_assign_int_field(self
):
891 int_fc
= self
._tc
.create_signed_integer_field_class(32)
892 int_field
= _create_field(self
._tc
, int_fc
)
894 int_field
.value
= raw
895 self
._def
.value
= int_field
896 self
.assertEqual(self
._def
, float(raw
))
898 def test_assign_float(self
):
900 self
._def
.value
= raw
901 self
.assertEqual(self
._def
, raw
)
903 def test_assign_float_field(self
):
904 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
907 self
._def
.value
= field
908 self
.assertEqual(self
._def
, raw
)
910 def test_assign_invalid_type(self
):
911 with self
.assertRaises(TypeError):
912 self
._def
.value
= 'yes'
914 def test_invalid_lshift(self
):
915 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
917 def test_invalid_rshift(self
):
918 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
920 def test_invalid_and(self
):
921 self
._test
_invalid
_op
(lambda: self
._def
& 23)
923 def test_invalid_or(self
):
924 self
._test
_invalid
_op
(lambda: self
._def |
23)
926 def test_invalid_xor(self
):
927 self
._test
_invalid
_op
(lambda: self
._def ^
23)
929 def test_invalid_invert(self
):
930 self
._test
_invalid
_op
(lambda: ~self
._def
)
932 def test_str_op(self
):
933 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
936 _inject_numeric_testing_methods(RealFieldTestCase
)
939 class StringFieldTestCase(unittest
.TestCase
):
941 self
._tc
= get_default_trace_class()
942 self
._def
_value
= 'Hello, World!'
943 self
._def
= _create_string_field(self
._tc
)
944 self
._def
.value
= self
._def
_value
945 self
._def
_new
_value
= 'Yes!'
947 def test_assign_int(self
):
948 with self
.assertRaises(TypeError):
949 self
._def
.value
= 283
951 def test_assign_string_field(self
):
952 field
= _create_string_field(self
._tc
)
955 self
.assertEqual(field
, raw
)
958 self
.assertEqual(self
._def
, self
._def
_value
)
960 def test_not_eq(self
):
961 self
.assertNotEqual(self
._def
, 23)
963 def test_lt_vstring(self
):
964 s1
= _create_string_field(self
._tc
)
966 s2
= _create_string_field(self
._tc
)
968 self
.assertLess(s1
, s2
)
970 def test_lt_string(self
):
971 s1
= _create_string_field(self
._tc
)
973 self
.assertLess(s1
, 'bateau')
975 def test_le_vstring(self
):
976 s1
= _create_string_field(self
._tc
)
978 s2
= _create_string_field(self
._tc
)
980 self
.assertLessEqual(s1
, s2
)
982 def test_le_string(self
):
983 s1
= _create_string_field(self
._tc
)
985 self
.assertLessEqual(s1
, 'bateau')
987 def test_gt_vstring(self
):
988 s1
= _create_string_field(self
._tc
)
990 s2
= _create_string_field(self
._tc
)
992 self
.assertGreater(s2
, s1
)
994 def test_gt_string(self
):
995 s1
= _create_string_field(self
._tc
)
997 self
.assertGreater('bateau', s1
)
999 def test_ge_vstring(self
):
1000 s1
= _create_string_field(self
._tc
)
1002 s2
= _create_string_field(self
._tc
)
1004 self
.assertGreaterEqual(s2
, s1
)
1006 def test_ge_string(self
):
1007 s1
= _create_string_field(self
._tc
)
1009 self
.assertGreaterEqual('bateau', s1
)
1011 def test_bool_op(self
):
1012 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1014 def test_str_op(self
):
1015 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1018 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1020 def test_getitem(self
):
1021 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1023 def test_append_str(self
):
1024 to_append
= 'meow meow meow'
1025 self
._def
+= to_append
1026 self
._def
_value
+= to_append
1027 self
.assertEqual(self
._def
, self
._def
_value
)
1029 def test_append_string_field(self
):
1030 field
= _create_string_field(self
._tc
)
1031 to_append
= 'meow meow meow'
1032 field
.value
= to_append
1034 self
._def
_value
+= to_append
1035 self
.assertEqual(self
._def
, self
._def
_value
)
1038 class _TestArrayFieldCommon
:
1039 def _modify_def(self
):
1042 def test_bool_op_true(self
):
1043 self
.assertTrue(self
._def
)
1046 self
.assertEqual(len(self
._def
), 3)
1048 def test_length(self
):
1049 self
.assertEqual(self
._def
.length
, 3)
1051 def test_getitem(self
):
1052 field
= self
._def
[1]
1053 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1054 self
.assertEqual(field
, 1847)
1057 field
= _create_int_array_field(self
._tc
, 3)
1061 self
.assertEqual(self
._def
, field
)
1063 def test_eq_invalid_type(self
):
1064 self
.assertNotEqual(self
._def
, 23)
1066 def test_eq_diff_len(self
):
1067 field
= _create_int_array_field(self
._tc
, 2)
1070 self
.assertNotEqual(self
._def
, field
)
1072 def test_eq_diff_content_same_len(self
):
1073 field
= _create_int_array_field(self
._tc
, 3)
1077 self
.assertNotEqual(self
._def
, field
)
1079 def test_eq_non_sequence_iterable(self
):
1080 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1081 field
= _create_int_array_field(self
._tc
, 3)
1085 self
.assertEqual(field
, list(dct
.keys()))
1086 self
.assertNotEqual(field
, dct
)
1088 def test_setitem(self
):
1090 self
.assertEqual(self
._def
[2], 24)
1092 def test_setitem_int_field(self
):
1093 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1094 int_field
= _create_field(self
._tc
, int_fc
)
1095 int_field
.value
= 19487
1096 self
._def
[1] = int_field
1097 self
.assertEqual(self
._def
[1], 19487)
1099 def test_setitem_non_basic_field(self
):
1100 array_field
= _create_struct_array_field(self
._tc
, 2)
1101 with self
.assertRaises(TypeError):
1104 def test_setitem_none(self
):
1105 with self
.assertRaises(TypeError):
1108 def test_setitem_index_wrong_type(self
):
1109 with self
.assertRaises(TypeError):
1110 self
._def
['yes'] = 23
1112 def test_setitem_index_neg(self
):
1113 with self
.assertRaises(IndexError):
1116 def test_setitem_index_out_of_range(self
):
1117 with self
.assertRaises(IndexError):
1118 self
._def
[len(self
._def
)] = 134679
1120 def test_iter(self
):
1121 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1122 self
.assertEqual(field
, value
)
1124 def test_value_int_field(self
):
1125 values
= [45646, 145, 12145]
1126 self
._def
.value
= values
1127 self
.assertEqual(values
, self
._def
)
1129 def test_value_check_sequence(self
):
1131 with self
.assertRaises(TypeError):
1132 self
._def
.value
= values
1134 def test_value_wrong_type_in_sequence(self
):
1135 values
= [32, 'hello', 11]
1136 with self
.assertRaises(TypeError):
1137 self
._def
.value
= values
1139 def test_value_complex_type(self
):
1140 struct_fc
= self
._tc
.create_structure_field_class()
1141 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1142 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1143 str_fc
= self
._tc
.create_string_field_class()
1144 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1145 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1146 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1147 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1148 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1152 'a_string': 'hello',
1157 'a_string': 'goodbye',
1162 'a_string': 'or not',
1167 array
= stream
.create_packet().context_field
['array_field']
1168 array
.value
= values
1169 self
.assertEqual(values
, array
)
1170 values
[0]['an_int'] = 'a string'
1171 with self
.assertRaises(TypeError):
1172 array
.value
= values
1174 def test_str_op(self
):
1176 expected_string
= '[{}]'.format(', '.join(
1177 [repr(v
) for v
in self
._def
_value
]))
1178 self
.assertEqual(expected_string
, s
)
1181 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1183 self
._tc
= get_default_trace_class()
1184 self
._def
= _create_int_array_field(self
._tc
, 3)
1187 self
._def
[2] = 1948754
1188 self
._def
_value
= [45, 1847, 1948754]
1190 def test_value_wrong_len(self
):
1192 with self
.assertRaises(ValueError):
1193 self
._def
.value
= values
1196 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1198 self
._tc
= get_default_trace_class()
1199 self
._def
= _create_dynamic_array(self
._tc
)
1202 self
._def
[2] = 1948754
1203 self
._def
_value
= [45, 1847, 1948754]
1205 def test_value_resize(self
):
1206 new_values
= [1, 2, 3, 4]
1207 self
._def
.value
= new_values
1208 self
.assertCountEqual(self
._def
, new_values
)
1210 def test_set_length(self
):
1211 self
._def
.length
= 4
1213 self
.assertEqual(len(self
._def
), 4)
1215 def test_set_invalid_length(self
):
1216 with self
.assertRaises(TypeError):
1217 self
._def
.length
= 'cheval'
1220 class StructureFieldTestCase(unittest
.TestCase
):
1221 def _create_fc(self
, tc
):
1222 fc
= tc
.create_structure_field_class()
1223 fc
.append_member('A', self
._fc
0_fn
())
1224 fc
.append_member('B', self
._fc
1_fn
())
1225 fc
.append_member('C', self
._fc
2_fn
())
1226 fc
.append_member('D', self
._fc
3_fn
())
1227 fc
.append_member('E', self
._fc
4_fn
())
1228 fc5
= self
._fc
5_fn
()
1229 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1230 fc
.append_member('F', fc5
)
1234 self
._tc
= get_default_trace_class()
1235 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1236 self
._fc
1_fn
= self
._tc
.create_string_field_class
1237 self
._fc
2_fn
= self
._tc
.create_real_field_class
1238 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1239 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1240 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1241 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1243 self
._fc
= self
._create
_fc
(self
._tc
)
1244 self
._def
= _create_field(self
._tc
, self
._fc
)
1245 self
._def
['A'] = -1872
1246 self
._def
['B'] = 'salut'
1247 self
._def
['C'] = 17.5
1248 self
._def
['D'] = 16497
1250 self
._def
['F'] = {'F_1': 52}
1260 def _modify_def(self
):
1261 self
._def
['B'] = 'hola'
1263 def test_bool_op_true(self
):
1264 self
.assertTrue(self
._def
)
1266 def test_bool_op_false(self
):
1267 field
= self
._def
['E']
1268 self
.assertFalse(field
)
1271 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1273 def test_getitem(self
):
1274 field
= self
._def
['A']
1275 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1276 self
.assertEqual(field
, -1872)
1278 def test_member_at_index_out_of_bounds_after(self
):
1279 with self
.assertRaises(IndexError):
1280 self
._def
.member_at_index(len(self
._def
_value
))
1283 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
, ))
1285 field
['B'] = 'salut'
1289 field
['F'] = {'F_1': 52}
1290 self
.assertEqual(self
._def
, field
)
1292 def test_eq_invalid_type(self
):
1293 self
.assertNotEqual(self
._def
, 23)
1295 def test_eq_diff_len(self
):
1296 fc
= self
._tc
.create_structure_field_class()
1297 fc
.append_member('A', self
._fc
0_fn
())
1298 fc
.append_member('B', self
._fc
1_fn
())
1299 fc
.append_member('C', self
._fc
2_fn
())
1301 field
= _create_field(self
._tc
, fc
)
1303 field
['B'] = 'salut'
1305 self
.assertNotEqual(self
._def
, field
)
1307 def test_eq_diff_keys(self
):
1308 fc
= self
._tc
.create_structure_field_class()
1309 fc
.append_member('U', self
._fc
0_fn
())
1310 fc
.append_member('V', self
._fc
1_fn
())
1311 fc
.append_member('W', self
._fc
2_fn
())
1312 fc
.append_member('X', self
._fc
3_fn
())
1313 fc
.append_member('Y', self
._fc
4_fn
())
1314 fc
.append_member('Z', self
._fc
5_fn
())
1315 field
= _create_field(self
._tc
, fc
)
1317 field
['V'] = "gerry"
1322 self
.assertNotEqual(self
._def
, field
)
1324 def test_eq_diff_content_same_len(self
):
1325 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1327 field
['B'] = 'salut'
1331 field
['F'] = {'F_1': 0}
1332 self
.assertNotEqual(self
._def
, field
)
1334 def test_eq_same_content_diff_keys(self
):
1335 fc
= self
._tc
.create_structure_field_class()
1336 fc
.append_member('A', self
._fc
0_fn
())
1337 fc
.append_member('B', self
._fc
1_fn
())
1338 fc
.append_member('E', self
._fc
2_fn
())
1339 fc
.append_member('D', self
._fc
3_fn
())
1340 fc
.append_member('C', self
._fc
4_fn
())
1341 fc
.append_member('F', self
._fc
5_fn
())
1342 field
= _create_field(self
._tc
, fc
)
1344 field
['B'] = 'salut'
1349 self
.assertNotEqual(self
._def
, field
)
1351 def test_setitem(self
):
1352 self
._def
['C'] = -18.47
1353 self
.assertEqual(self
._def
['C'], -18.47)
1355 def test_setitem_int_field(self
):
1356 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1357 int_field
= _create_field(self
._tc
, int_fc
)
1358 int_field
.value
= 19487
1359 self
._def
['D'] = int_field
1360 self
.assertEqual(self
._def
['D'], 19487)
1362 def test_setitem_non_basic_field(self
):
1363 elem_fc
= self
._tc
.create_structure_field_class()
1364 struct_fc
= self
._tc
.create_structure_field_class()
1365 struct_fc
.append_member('A', elem_fc
)
1366 struct_field
= _create_field(self
._tc
, struct_fc
)
1368 # Will fail on access to .items() of the value
1369 with self
.assertRaises(AttributeError):
1370 struct_field
['A'] = 23
1372 def test_setitem_none(self
):
1373 with self
.assertRaises(TypeError):
1374 self
._def
['C'] = None
1376 def test_setitem_key_wrong_type(self
):
1377 with self
.assertRaises(TypeError):
1380 def test_setitem_wrong_key(self
):
1381 with self
.assertRaises(KeyError):
1382 self
._def
['hi'] = 134679
1384 def test_member_at_index(self
):
1385 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1387 def test_iter(self
):
1397 for vkey
, vval
in self
._def
.items():
1398 val
= orig_values
[vkey
]
1399 self
.assertEqual(vval
, val
)
1401 def test_value(self
):
1410 self
.assertEqual(self
._def
, orig_values
)
1412 def test_set_value(self
):
1413 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1414 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1415 str_fc
= self
._tc
.create_string_field_class()
1416 struct_fc
= self
._tc
.create_structure_field_class()
1417 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1418 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1419 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1422 'a_string': 'hello',
1426 struct
= _create_field(self
._tc
, struct_fc
)
1427 struct
.value
= values
1428 self
.assertEqual(values
, struct
)
1430 bad_type_values
= copy
.deepcopy(values
)
1431 bad_type_values
['an_int'] = 'a string'
1432 with self
.assertRaises(TypeError):
1433 struct
.value
= bad_type_values
1435 unknown_key_values
= copy
.deepcopy(values
)
1436 unknown_key_values
['unknown_key'] = 16546
1437 with self
.assertRaises(KeyError):
1438 struct
.value
= unknown_key_values
1440 def test_str_op(self
):
1441 expected_string_found
= False
1443 # Establish all permutations of the three expected matches since
1444 # the order in which mappings are enumerated is not explicitly part of
1446 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1447 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1448 candidate
= '{{{}}}'.format(', '.join(items
))
1450 expected_string_found
= True
1453 self
.assertTrue(expected_string_found
)
1456 class VariantFieldTestCase(unittest
.TestCase
):
1457 def _create_fc(self
, tc
):
1458 ft0
= tc
.create_signed_integer_field_class(32)
1459 ft1
= tc
.create_string_field_class()
1460 ft2
= tc
.create_real_field_class()
1461 ft3
= tc
.create_signed_integer_field_class(17)
1462 fc
= tc
.create_variant_field_class()
1463 fc
.append_option('corner', ft0
)
1464 fc
.append_option('zoom', ft1
)
1465 fc
.append_option('mellotron', ft2
)
1466 fc
.append_option('giorgio', ft3
)
1467 top_fc
= tc
.create_structure_field_class()
1468 top_fc
.append_member('variant_field', fc
)
1472 self
._tc
= get_default_trace_class()
1473 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1474 self
._def
= fld
['variant_field']
1476 def test_bool_op(self
):
1477 self
._def
.selected_option_index
= 2
1478 self
._def
.value
= -17.34
1479 with self
.assertRaises(NotImplementedError):
1482 def test_selected_option_index(self
):
1483 self
._def
.selected_option_index
= 2
1484 self
.assertEqual(self
._def
.selected_option_index
, 2)
1486 def test_selected_option(self
):
1487 self
._def
.selected_option_index
= 2
1488 self
._def
.value
= -17.34
1489 self
.assertEqual(self
._def
.selected_option
, -17.34)
1491 self
._def
.selected_option_index
= 3
1492 self
._def
.value
= 1921
1493 self
.assertEqual(self
._def
.selected_option
, 1921)
1496 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1497 field
= field
['variant_field']
1498 field
.selected_option_index
= 0
1500 self
._def
.selected_option_index
= 0
1501 self
._def
.value
= 1774
1502 self
.assertEqual(self
._def
, field
)
1504 def test_eq_invalid_type(self
):
1505 self
._def
.selected_option_index
= 1
1506 self
._def
.value
= 'gerry'
1507 self
.assertNotEqual(self
._def
, 23)
1509 def test_str_op_int(self
):
1510 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1511 field
= field
['variant_field']
1512 field
.selected_option_index
= 0
1514 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1515 other_field
= other_field
['variant_field']
1516 other_field
.selected_option_index
= 0
1517 other_field
.value
= 1774
1518 self
.assertEqual(str(field
), str(other_field
))
1520 def test_str_op_str(self
):
1521 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1522 field
= field
['variant_field']
1523 field
.selected_option_index
= 1
1524 field
.value
= 'un beau grand bateau'
1525 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1526 other_field
= other_field
['variant_field']
1527 other_field
.selected_option_index
= 1
1528 other_field
.value
= 'un beau grand bateau'
1529 self
.assertEqual(str(field
), str(other_field
))
1531 def test_str_op_float(self
):
1532 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1533 field
= field
['variant_field']
1534 field
.selected_option_index
= 2
1535 field
.value
= 14.4245
1536 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1537 other_field
= other_field
['variant_field']
1538 other_field
.selected_option_index
= 2
1539 other_field
.value
= 14.4245
1540 self
.assertEqual(str(field
), str(other_field
))