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
30 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
33 # Create and return a stream with the field classes part of its stream packet
36 # The stream is part of a dummy trace created from trace class `tc`.
39 def _create_stream(tc
, ctx_field_classes
):
40 packet_context_fc
= tc
.create_structure_field_class()
41 for name
, fc
in ctx_field_classes
:
42 packet_context_fc
.append_member(name
, fc
)
45 stream_class
= tc
.create_stream_class(
46 packet_context_field_class
=packet_context_fc
, supports_packets
=True
49 stream
= trace
.create_stream(stream_class
)
53 # Create a field of the given field class.
55 # The field is part of a dummy stream, itself part of a dummy trace created
56 # from trace class `tc`.
59 def _create_field(tc
, field_class
):
61 stream
= _create_stream(tc
, [(field_name
, field_class
)])
62 packet
= stream
.create_packet()
63 return packet
.context_field
[field_name
]
66 # Create a field of type string.
68 # The field is part of a dummy stream, itself part of a dummy trace created
69 # from trace class `tc`. It is made out of a dummy string field class.
72 def _create_string_field(tc
):
73 field_name
= 'string_field'
74 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
75 packet
= stream
.create_packet()
76 return packet
.context_field
[field_name
]
79 # Create a field of type static array of ints.
81 # The field is part of a dummy stream, itself part of a dummy trace created
82 # from trace class `tc`. It is made out of a dummy static array field class,
83 # with a dummy integer field class as element class.
86 def _create_int_array_field(tc
, length
):
87 elem_fc
= tc
.create_signed_integer_field_class(32)
88 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
89 field_name
= 'int_array'
90 stream
= _create_stream(tc
, [(field_name
, fc
)])
91 packet
= stream
.create_packet()
92 return packet
.context_field
[field_name
]
95 # Create a field of type dynamic array of ints.
97 # The field is part of a dummy stream, itself part of a dummy trace created
98 # from trace class `tc`. It is made out of a dummy static array field class,
99 # with a dummy integer field class as element and length classes.
102 def _create_dynamic_array(tc
):
103 elem_fc
= tc
.create_signed_integer_field_class(32)
104 len_fc
= tc
.create_signed_integer_field_class(32)
105 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
106 field_name
= 'int_dyn_array'
107 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
108 packet
= stream
.create_packet()
109 packet
.context_field
[field_name
].length
= 3
110 return packet
.context_field
[field_name
]
113 # Create a field of type array of (empty) structures.
115 # The field is part of a dummy stream, itself part of a dummy trace created
116 # from trace class `tc`. It is made out of a dummy static array field class,
117 # with a dummy struct field class as element class.
120 def _create_struct_array_field(tc
, length
):
121 elem_fc
= tc
.create_structure_field_class()
122 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
123 field_name
= 'struct_array'
124 stream
= _create_stream(tc
, [(field_name
, fc
)])
125 packet
= stream
.create_packet()
126 return packet
.context_field
[field_name
]
129 # Base class for numeric field test cases.
131 # To be compatible with this base class, a derived class must, in its
134 # * Set `self._def` to a field object with an arbitrary value.
135 # * Set `self._def_value` to the equivalent value of `self._def`.
136 class _TestNumericField
:
137 # Tries the binary operation `op`:
139 # 1. Between `self._def`, which is a field object, and `rhs`.
140 # 2. Between `self._def_value`, which is the raw value of
141 # `self._def`, and `rhs`.
143 # Returns the results of 1. and 2.
145 # If there's an exception while performing 1. or 2., asserts that
146 # both operations raised exceptions, that both exceptions have the
147 # same type, and returns `None` for both results.
148 def _binop(self
, op
, rhs
):
153 # try with field object
155 r
= op(self
._def
, rhs
)
156 except Exception as e
:
161 rv
= op(self
._def
_value
, comp_value
)
162 except Exception as e
:
165 if type_rexc
is not None or type_rvexc
is not None:
166 # at least one of the operations raised an exception: in
167 # this case both operations should have raised the same
168 # type of exception (division by zero, bit shift with a
169 # floating point number operand, etc.)
170 self
.assertIs(type_rexc
, type_rvexc
)
175 # Tries the unary operation `op`:
177 # 1. On `self._def`, which is a field object.
178 # 2. On `self._def_value`, which is the value of `self._def`.
180 # Returns the results of 1. and 2.
182 # If there's an exception while performing 1. or 2., asserts that
183 # both operations raised exceptions, that both exceptions have the
184 # same type, and returns `None` for both results.
185 def _unaryop(self
, op
):
189 # try with field object
192 except Exception as e
:
197 rv
= op(self
._def
_value
)
198 except Exception as e
:
201 if type_rexc
is not None or type_rvexc
is not None:
202 # at least one of the operations raised an exception: in
203 # this case both operations should have raised the same
204 # type of exception (division by zero, bit shift with a
205 # floating point number operand, etc.)
206 self
.assertIs(type_rexc
, type_rvexc
)
211 # Tests that the unary operation `op` gives results with the same
212 # type for both `self._def` and `self._def_value`.
213 def _test_unaryop_type(self
, op
):
214 r
, rv
= self
._unaryop
(op
)
219 self
.assertIsInstance(r
, type(rv
))
221 # Tests that the unary operation `op` gives results with the same
222 # value for both `self._def` and `self._def_value`. This uses the
223 # __eq__() operator of `self._def`.
224 def _test_unaryop_value(self
, op
):
225 r
, rv
= self
._unaryop
(op
)
230 self
.assertEqual(r
, rv
)
232 # Tests that the unary operation `op`, when applied to `self._def`,
233 # does not change its underlying BT object address.
234 def _test_unaryop_addr_same(self
, op
):
235 addr_before
= self
._def
.addr
237 self
.assertEqual(self
._def
.addr
, addr_before
)
239 # Tests that the unary operation `op`, when applied to `self._def`,
240 # does not change its value.
241 def _test_unaryop_value_same(self
, op
):
242 value_before
= copy
.copy(self
._def
_value
)
244 self
.assertEqual(self
._def
, value_before
)
246 # Tests that the binary operation `op` gives results with the same
247 # type for both `self._def` and `self._def_value`.
248 def _test_binop_type(self
, op
, rhs
):
249 r
, rv
= self
._binop
(op
, rhs
)
254 if op
in _COMP_BINOPS
:
255 # __eq__() and __ne__() always return a 'bool' object
256 self
.assertIsInstance(r
, bool)
258 self
.assertIsInstance(r
, type(rv
))
260 # Tests that the binary operation `op` gives results with the same
261 # value for both `self._def` and `self._def_value`. This uses the
262 # __eq__() operator of `self._def`.
263 def _test_binop_value(self
, op
, rhs
):
264 r
, rv
= self
._binop
(op
, rhs
)
269 self
.assertEqual(r
, rv
)
271 # Tests that the binary operation `op`, when applied to `self._def`,
272 # does not change its underlying BT object address.
273 def _test_binop_lhs_addr_same(self
, op
, rhs
):
274 addr_before
= self
._def
.addr
275 r
, rv
= self
._binop
(op
, rhs
)
276 self
.assertEqual(self
._def
.addr
, addr_before
)
278 # Tests that the binary operation `op`, when applied to `self._def`,
279 # does not change its value.
280 @unittest.skip('copy is not implemented')
281 def _test_binop_lhs_value_same(self
, op
, rhs
):
282 value_before
= copy
.copy(self
._def
)
283 r
, rv
= self
._binop
(op
, rhs
)
284 self
.assertEqual(self
._def
, value_before
)
286 # The methods below which take the `test_cb` and/or `op` parameters
287 # are meant to be used with one of the _test_binop_*() functions
288 # above as `test_cb` and a binary operator function as `op`.
292 # self._test_binop_rhs_pos_int(self._test_binop_value,
295 # This tests that a numeric field object added to a positive integer
296 # value gives a result with the expected value.
298 # `vint` and `vfloat` mean a signed integer value object and a real
301 def _test_binop_invalid_unknown(self
, op
):
302 if op
in _COMP_BINOPS
:
303 self
.skipTest('not testing')
308 with self
.assertRaises(TypeError):
311 def _test_binop_invalid_none(self
, op
):
312 if op
in _COMP_BINOPS
:
313 self
.skipTest('not testing')
315 with self
.assertRaises(TypeError):
318 def _test_binop_rhs_false(self
, test_cb
, op
):
321 def _test_binop_rhs_true(self
, test_cb
, op
):
324 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
327 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
330 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
333 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
334 test_cb(op
, bt2
.create_value(2))
336 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
337 test_cb(op
, bt2
.create_value(-23))
339 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
340 test_cb(op
, bt2
.create_value(0))
342 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
345 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
348 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
351 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
352 test_cb(op
, bt2
.create_value(2.2))
354 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
355 test_cb(op
, bt2
.create_value(-23.4))
357 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
358 test_cb(op
, bt2
.create_value(0.0))
360 def _test_binop_rhs_complex(self
, test_cb
, op
):
361 test_cb(op
, -23 + 19j
)
363 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
366 def _test_binop_type_false(self
, op
):
367 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
369 def _test_binop_type_true(self
, op
):
370 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
372 def _test_binop_type_pos_int(self
, op
):
373 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
375 def _test_binop_type_neg_int(self
, op
):
376 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
378 def _test_binop_type_zero_int(self
, op
):
379 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
381 def _test_binop_type_pos_vint(self
, op
):
382 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
384 def _test_binop_type_neg_vint(self
, op
):
385 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
387 def _test_binop_type_zero_vint(self
, op
):
388 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
390 def _test_binop_type_pos_float(self
, op
):
391 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
393 def _test_binop_type_neg_float(self
, op
):
394 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
396 def _test_binop_type_zero_float(self
, op
):
397 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
399 def _test_binop_type_pos_vfloat(self
, op
):
400 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
402 def _test_binop_type_neg_vfloat(self
, op
):
403 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
405 def _test_binop_type_zero_vfloat(self
, op
):
406 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
408 def _test_binop_type_complex(self
, op
):
409 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
411 def _test_binop_type_zero_complex(self
, op
):
412 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
414 def _test_binop_value_false(self
, op
):
415 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
417 def _test_binop_value_true(self
, op
):
418 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
420 def _test_binop_value_pos_int(self
, op
):
421 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
423 def _test_binop_value_neg_int(self
, op
):
424 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
426 def _test_binop_value_zero_int(self
, op
):
427 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
429 def _test_binop_value_pos_vint(self
, op
):
430 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
432 def _test_binop_value_neg_vint(self
, op
):
433 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
435 def _test_binop_value_zero_vint(self
, op
):
436 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
438 def _test_binop_value_pos_float(self
, op
):
439 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
441 def _test_binop_value_neg_float(self
, op
):
442 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
444 def _test_binop_value_zero_float(self
, op
):
445 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
447 def _test_binop_value_pos_vfloat(self
, op
):
448 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
450 def _test_binop_value_neg_vfloat(self
, op
):
451 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
453 def _test_binop_value_zero_vfloat(self
, op
):
454 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
456 def _test_binop_value_complex(self
, op
):
457 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
459 def _test_binop_value_zero_complex(self
, op
):
460 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
462 def _test_binop_lhs_addr_same_false(self
, op
):
463 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
465 def _test_binop_lhs_addr_same_true(self
, op
):
466 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
468 def _test_binop_lhs_addr_same_pos_int(self
, op
):
469 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
471 def _test_binop_lhs_addr_same_neg_int(self
, op
):
472 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
474 def _test_binop_lhs_addr_same_zero_int(self
, op
):
475 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
477 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
478 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
480 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
481 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
483 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
484 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
486 def _test_binop_lhs_addr_same_pos_float(self
, op
):
487 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
489 def _test_binop_lhs_addr_same_neg_float(self
, op
):
490 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
492 def _test_binop_lhs_addr_same_zero_float(self
, op
):
493 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
495 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
496 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
498 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
499 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
501 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
502 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
504 def _test_binop_lhs_addr_same_complex(self
, op
):
505 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
507 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
508 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
510 def _test_binop_lhs_value_same_false(self
, op
):
511 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
513 def _test_binop_lhs_value_same_true(self
, op
):
514 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
516 def _test_binop_lhs_value_same_pos_int(self
, op
):
517 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
519 def _test_binop_lhs_value_same_neg_int(self
, op
):
520 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
522 def _test_binop_lhs_value_same_zero_int(self
, op
):
523 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
525 def _test_binop_lhs_value_same_pos_vint(self
, op
):
526 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
528 def _test_binop_lhs_value_same_neg_vint(self
, op
):
529 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
531 def _test_binop_lhs_value_same_zero_vint(self
, op
):
532 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
534 def _test_binop_lhs_value_same_pos_float(self
, op
):
535 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
537 def _test_binop_lhs_value_same_neg_float(self
, op
):
538 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
540 def _test_binop_lhs_value_same_zero_float(self
, op
):
541 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
543 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
544 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
546 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
547 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
549 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
550 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
552 def _test_binop_lhs_value_same_complex(self
, op
):
553 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
555 def _test_binop_lhs_value_same_zero_complex(self
, op
):
556 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
558 def test_bool_op(self
):
559 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
561 def test_int_op(self
):
562 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
564 def test_float_op(self
):
565 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
567 def test_complex_op(self
):
568 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
570 def test_str_op(self
):
571 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
573 def test_eq_none(self
):
574 # Ignore this lint error:
575 # E711 comparison to None should be 'if cond is None:'
576 # since this is what we want to test (even though not good practice).
577 self
.assertFalse(self
._def
== None) # noqa: E711
579 def test_ne_none(self
):
580 # Ignore this lint error:
581 # E711 comparison to None should be 'if cond is not None:'
582 # since this is what we want to test (even though not good practice).
583 self
.assertTrue(self
._def
!= None) # noqa: E711
586 # This is a list of binary operators used for
587 # _inject_numeric_testing_methods().
589 # Each entry is a pair of binary operator name (used as part of the
590 # created testing method's name) and operator function.
598 ('add', operator
.add
),
599 ('radd', lambda a
, b
: operator
.add(b
, a
)),
600 ('and', operator
.and_
),
601 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
602 ('floordiv', operator
.floordiv
),
603 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
604 ('lshift', operator
.lshift
),
605 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
606 ('mod', operator
.mod
),
607 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
608 ('mul', operator
.mul
),
609 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
610 ('or', operator
.or_
),
611 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
612 ('pow', operator
.pow),
613 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
614 ('rshift', operator
.rshift
),
615 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
616 ('sub', operator
.sub
),
617 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
618 ('truediv', operator
.truediv
),
619 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
620 ('xor', operator
.xor
),
621 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
625 # This is a list of unary operators used for
626 # _inject_numeric_testing_methods().
628 # Each entry is a pair of unary operator name (used as part of the
629 # created testing method's name) and operator function.
631 ('neg', operator
.neg
),
632 ('pos', operator
.pos
),
633 ('abs', operator
.abs),
634 ('invert', operator
.invert
),
636 ('round_0', partial(round, ndigits
=0)),
637 ('round_1', partial(round, ndigits
=1)),
638 ('round_2', partial(round, ndigits
=2)),
639 ('round_3', partial(round, ndigits
=3)),
641 ('floor', math
.floor
),
642 ('trunc', math
.trunc
),
646 # This function injects a bunch of testing methods to a numeric
649 # It is meant to be used like this:
651 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
653 # This function injects:
655 # * One testing method for each _TestNumericField._test_binop_*()
656 # method, for each binary operator in the _BINOPS tuple.
658 # * One testing method for each _TestNumericField._test_unaryop*()
659 # method, for each unary operator in the _UNARYOPS tuple.
660 def _inject_numeric_testing_methods(cls
):
661 def test_binop_name(suffix
):
662 return 'test_binop_{}_{}'.format(name
, suffix
)
664 def test_unaryop_name(suffix
):
665 return 'test_unaryop_{}_{}'.format(name
, suffix
)
667 # inject testing methods for each binary operation
668 for name
, binop
in _BINOPS
:
671 test_binop_name('invalid_unknown'),
672 partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
),
676 test_binop_name('invalid_none'),
677 partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
),
681 test_binop_name('type_true'),
682 partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
),
686 test_binop_name('type_pos_int'),
687 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
),
691 test_binop_name('type_pos_vint'),
692 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
),
696 test_binop_name('value_true'),
697 partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
),
701 test_binop_name('value_pos_int'),
702 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
),
706 test_binop_name('value_pos_vint'),
707 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
),
711 test_binop_name('lhs_addr_same_true'),
712 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
716 test_binop_name('lhs_addr_same_pos_int'),
718 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
723 test_binop_name('lhs_addr_same_pos_vint'),
725 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
730 test_binop_name('lhs_value_same_true'),
731 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
735 test_binop_name('lhs_value_same_pos_int'),
737 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
742 test_binop_name('lhs_value_same_pos_vint'),
744 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
749 test_binop_name('type_neg_int'),
750 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
),
754 test_binop_name('type_neg_vint'),
755 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
),
759 test_binop_name('value_neg_int'),
760 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
),
764 test_binop_name('value_neg_vint'),
765 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
),
769 test_binop_name('lhs_addr_same_neg_int'),
771 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
776 test_binop_name('lhs_addr_same_neg_vint'),
778 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
783 test_binop_name('lhs_value_same_neg_int'),
785 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
790 test_binop_name('lhs_value_same_neg_vint'),
792 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
797 test_binop_name('type_false'),
798 partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
),
802 test_binop_name('type_zero_int'),
803 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
),
807 test_binop_name('type_zero_vint'),
808 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
),
812 test_binop_name('value_false'),
813 partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
),
817 test_binop_name('value_zero_int'),
818 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
),
822 test_binop_name('value_zero_vint'),
823 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
),
827 test_binop_name('lhs_addr_same_false'),
828 partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
832 test_binop_name('lhs_addr_same_zero_int'),
834 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
839 test_binop_name('lhs_addr_same_zero_vint'),
841 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
846 test_binop_name('lhs_value_same_false'),
847 partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
851 test_binop_name('lhs_value_same_zero_int'),
853 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
858 test_binop_name('lhs_value_same_zero_vint'),
860 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
865 test_binop_name('type_pos_float'),
866 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
),
870 test_binop_name('type_neg_float'),
871 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
),
875 test_binop_name('type_pos_vfloat'),
876 partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
880 test_binop_name('type_neg_vfloat'),
881 partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
885 test_binop_name('value_pos_float'),
886 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
),
890 test_binop_name('value_neg_float'),
891 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
),
895 test_binop_name('value_pos_vfloat'),
896 partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
900 test_binop_name('value_neg_vfloat'),
901 partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
905 test_binop_name('lhs_addr_same_pos_float'),
907 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
912 test_binop_name('lhs_addr_same_neg_float'),
914 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
919 test_binop_name('lhs_addr_same_pos_vfloat'),
921 _TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
926 test_binop_name('lhs_addr_same_neg_vfloat'),
928 _TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
933 test_binop_name('lhs_value_same_pos_float'),
935 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
940 test_binop_name('lhs_value_same_neg_float'),
942 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
947 test_binop_name('lhs_value_same_pos_vfloat'),
949 _TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
954 test_binop_name('lhs_value_same_neg_vfloat'),
956 _TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
961 test_binop_name('type_zero_float'),
962 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
),
966 test_binop_name('type_zero_vfloat'),
967 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
971 test_binop_name('value_zero_float'),
972 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
),
976 test_binop_name('value_zero_vfloat'),
977 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
981 test_binop_name('lhs_addr_same_zero_float'),
983 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
988 test_binop_name('lhs_addr_same_zero_vfloat'),
990 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
995 test_binop_name('lhs_value_same_zero_float'),
997 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
1002 test_binop_name('lhs_value_same_zero_vfloat'),
1004 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
1009 test_binop_name('type_complex'),
1010 partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
),
1014 test_binop_name('type_zero_complex'),
1015 partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
),
1019 test_binop_name('value_complex'),
1020 partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
),
1024 test_binop_name('value_zero_complex'),
1025 partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
),
1029 test_binop_name('lhs_addr_same_complex'),
1031 _TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
1036 test_binop_name('lhs_addr_same_zero_complex'),
1038 _TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
1043 test_binop_name('lhs_value_same_complex'),
1045 _TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
1050 test_binop_name('lhs_value_same_zero_complex'),
1052 _TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
1056 # inject testing methods for each unary operation
1057 for name
, unaryop
in _UNARYOPS
:
1060 test_unaryop_name('type'),
1061 partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
),
1065 test_unaryop_name('value'),
1066 partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
),
1070 test_unaryop_name('addr_same'),
1071 partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
),
1075 test_unaryop_name('value_same'),
1076 partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
),
1080 class _TestIntegerFieldCommon(_TestNumericField
):
1081 def test_assign_true(self
):
1083 self
._def
.value
= raw
1084 self
.assertEqual(self
._def
, raw
)
1086 def test_assign_false(self
):
1088 self
._def
.value
= raw
1089 self
.assertEqual(self
._def
, raw
)
1091 def test_assign_pos_int(self
):
1093 self
._def
.value
= raw
1094 self
.assertEqual(self
._def
, raw
)
1096 def test_assign_neg_int(self
):
1098 self
._def
.value
= raw
1099 self
.assertEqual(self
._def
, raw
)
1101 def test_assign_int_field(self
):
1103 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1105 self
._def
.value
= field
1106 self
.assertEqual(self
._def
, raw
)
1108 def test_assign_invalid_type(self
):
1109 with self
.assertRaises(TypeError):
1110 self
._def
.value
= 'yes'
1112 def test_assign_uint(self
):
1113 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1114 field
= _create_field(self
._tc
, uint_fc
)
1117 self
.assertEqual(field
, raw
)
1119 def test_assign_big_uint(self
):
1120 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1121 field
= _create_field(self
._tc
, uint_fc
)
1122 # Larger than the IEEE 754 double-precision exact representation of
1125 field
.value
= (2 ** 53) + 1
1126 self
.assertEqual(field
, raw
)
1128 def test_assign_uint_invalid_neg(self
):
1129 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1130 field
= _create_field(self
._tc
, uint_fc
)
1132 with self
.assertRaises(ValueError):
1135 def test_str_op(self
):
1136 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1139 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1142 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1143 def _create_fc(self
, tc
):
1144 return tc
.create_signed_integer_field_class(25)
1147 self
._tc
= get_default_trace_class()
1148 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1149 self
._field
.value
= 17
1150 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1151 self
._def
.value
= 17
1152 self
._def
_value
= 17
1153 self
._def
_new
_value
= -101
1156 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1157 def _create_fc(self
, tc
):
1158 fc
= tc
.create_signed_enumeration_field_class(32)
1159 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1160 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1161 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1163 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1165 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1169 self
._tc
= get_default_trace_class()
1170 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1171 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1172 self
._def
.value
= 17
1173 self
._def
_value
= 17
1174 self
._def
_new
_value
= -101
1176 def test_str_op(self
):
1177 expected_string_found
= False
1180 # Establish all permutations of the three expected matches since
1181 # the order in which mappings are enumerated is not explicitly part of
1183 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1184 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1186 expected_string_found
= True
1189 self
.assertTrue(expected_string_found
)
1191 def test_labels(self
):
1192 self
._field
.value
= 17
1193 labels
= sorted(self
._field
.labels
)
1194 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1197 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1198 def _create_fc(self
, tc
):
1199 return tc
.create_real_field_class()
1202 self
._tc
= get_default_trace_class()
1203 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1204 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1205 self
._def
.value
= 52.7
1206 self
._def
_value
= 52.7
1207 self
._def
_new
_value
= -17.164857
1209 def _test_invalid_op(self
, cb
):
1210 with self
.assertRaises(TypeError):
1213 def test_assign_true(self
):
1214 self
._def
.value
= True
1215 self
.assertTrue(self
._def
)
1217 def test_assign_false(self
):
1218 self
._def
.value
= False
1219 self
.assertFalse(self
._def
)
1221 def test_assign_pos_int(self
):
1223 self
._def
.value
= raw
1224 self
.assertEqual(self
._def
, float(raw
))
1226 def test_assign_neg_int(self
):
1228 self
._def
.value
= raw
1229 self
.assertEqual(self
._def
, float(raw
))
1231 def test_assign_int_field(self
):
1232 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1233 int_field
= _create_field(self
._tc
, int_fc
)
1235 int_field
.value
= raw
1236 self
._def
.value
= int_field
1237 self
.assertEqual(self
._def
, float(raw
))
1239 def test_assign_float(self
):
1241 self
._def
.value
= raw
1242 self
.assertEqual(self
._def
, raw
)
1244 def test_assign_float_field(self
):
1245 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1248 self
._def
.value
= field
1249 self
.assertEqual(self
._def
, raw
)
1251 def test_assign_invalid_type(self
):
1252 with self
.assertRaises(TypeError):
1253 self
._def
.value
= 'yes'
1255 def test_invalid_lshift(self
):
1256 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1258 def test_invalid_rshift(self
):
1259 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1261 def test_invalid_and(self
):
1262 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1264 def test_invalid_or(self
):
1265 self
._test
_invalid
_op
(lambda: self
._def |
23)
1267 def test_invalid_xor(self
):
1268 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1270 def test_invalid_invert(self
):
1271 self
._test
_invalid
_op
(lambda: ~self
._def
)
1273 def test_str_op(self
):
1274 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1277 _inject_numeric_testing_methods(RealFieldTestCase
)
1280 class StringFieldTestCase(unittest
.TestCase
):
1282 self
._tc
= get_default_trace_class()
1283 self
._def
_value
= 'Hello, World!'
1284 self
._def
= _create_string_field(self
._tc
)
1285 self
._def
.value
= self
._def
_value
1286 self
._def
_new
_value
= 'Yes!'
1288 def test_assign_int(self
):
1289 with self
.assertRaises(TypeError):
1290 self
._def
.value
= 283
1292 def test_assign_string_field(self
):
1293 field
= _create_string_field(self
._tc
)
1296 self
.assertEqual(field
, raw
)
1299 self
.assertEqual(self
._def
, self
._def
_value
)
1301 def test_not_eq(self
):
1302 self
.assertNotEqual(self
._def
, 23)
1304 def test_lt_vstring(self
):
1305 s1
= _create_string_field(self
._tc
)
1307 s2
= _create_string_field(self
._tc
)
1309 self
.assertLess(s1
, s2
)
1311 def test_lt_string(self
):
1312 s1
= _create_string_field(self
._tc
)
1314 self
.assertLess(s1
, 'bateau')
1316 def test_le_vstring(self
):
1317 s1
= _create_string_field(self
._tc
)
1319 s2
= _create_string_field(self
._tc
)
1321 self
.assertLessEqual(s1
, s2
)
1323 def test_le_string(self
):
1324 s1
= _create_string_field(self
._tc
)
1326 self
.assertLessEqual(s1
, 'bateau')
1328 def test_gt_vstring(self
):
1329 s1
= _create_string_field(self
._tc
)
1331 s2
= _create_string_field(self
._tc
)
1333 self
.assertGreater(s2
, s1
)
1335 def test_gt_string(self
):
1336 s1
= _create_string_field(self
._tc
)
1338 self
.assertGreater('bateau', s1
)
1340 def test_ge_vstring(self
):
1341 s1
= _create_string_field(self
._tc
)
1343 s2
= _create_string_field(self
._tc
)
1345 self
.assertGreaterEqual(s2
, s1
)
1347 def test_ge_string(self
):
1348 s1
= _create_string_field(self
._tc
)
1350 self
.assertGreaterEqual('bateau', s1
)
1352 def test_bool_op(self
):
1353 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1355 def test_str_op(self
):
1356 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1359 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1361 def test_getitem(self
):
1362 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1364 def test_append_str(self
):
1365 to_append
= 'meow meow meow'
1366 self
._def
+= to_append
1367 self
._def
_value
+= to_append
1368 self
.assertEqual(self
._def
, self
._def
_value
)
1370 def test_append_string_field(self
):
1371 field
= _create_string_field(self
._tc
)
1372 to_append
= 'meow meow meow'
1373 field
.value
= to_append
1375 self
._def
_value
+= to_append
1376 self
.assertEqual(self
._def
, self
._def
_value
)
1379 class _TestArrayFieldCommon
:
1380 def _modify_def(self
):
1383 def test_bool_op_true(self
):
1384 self
.assertTrue(self
._def
)
1387 self
.assertEqual(len(self
._def
), 3)
1389 def test_length(self
):
1390 self
.assertEqual(self
._def
.length
, 3)
1392 def test_getitem(self
):
1393 field
= self
._def
[1]
1394 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1395 self
.assertEqual(field
, 1847)
1398 field
= _create_int_array_field(self
._tc
, 3)
1402 self
.assertEqual(self
._def
, field
)
1404 def test_eq_invalid_type(self
):
1405 self
.assertNotEqual(self
._def
, 23)
1407 def test_eq_diff_len(self
):
1408 field
= _create_int_array_field(self
._tc
, 2)
1411 self
.assertNotEqual(self
._def
, field
)
1413 def test_eq_diff_content_same_len(self
):
1414 field
= _create_int_array_field(self
._tc
, 3)
1418 self
.assertNotEqual(self
._def
, field
)
1420 def test_eq_non_sequence_iterable(self
):
1421 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1422 field
= _create_int_array_field(self
._tc
, 3)
1426 self
.assertEqual(field
, list(dct
.keys()))
1427 self
.assertNotEqual(field
, dct
)
1429 def test_setitem(self
):
1431 self
.assertEqual(self
._def
[2], 24)
1433 def test_setitem_int_field(self
):
1434 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1435 int_field
= _create_field(self
._tc
, int_fc
)
1436 int_field
.value
= 19487
1437 self
._def
[1] = int_field
1438 self
.assertEqual(self
._def
[1], 19487)
1440 def test_setitem_non_basic_field(self
):
1441 array_field
= _create_struct_array_field(self
._tc
, 2)
1442 with self
.assertRaises(TypeError):
1445 def test_setitem_none(self
):
1446 with self
.assertRaises(TypeError):
1449 def test_setitem_index_wrong_type(self
):
1450 with self
.assertRaises(TypeError):
1451 self
._def
['yes'] = 23
1453 def test_setitem_index_neg(self
):
1454 with self
.assertRaises(IndexError):
1457 def test_setitem_index_out_of_range(self
):
1458 with self
.assertRaises(IndexError):
1459 self
._def
[len(self
._def
)] = 134679
1461 def test_iter(self
):
1462 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1463 self
.assertEqual(field
, value
)
1465 def test_value_int_field(self
):
1466 values
= [45646, 145, 12145]
1467 self
._def
.value
= values
1468 self
.assertEqual(values
, self
._def
)
1470 def test_value_check_sequence(self
):
1472 with self
.assertRaises(TypeError):
1473 self
._def
.value
= values
1475 def test_value_wrong_type_in_sequence(self
):
1476 values
= [32, 'hello', 11]
1477 with self
.assertRaises(TypeError):
1478 self
._def
.value
= values
1480 def test_value_complex_type(self
):
1481 struct_fc
= self
._tc
.create_structure_field_class()
1482 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1483 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1484 str_fc
= self
._tc
.create_string_field_class()
1485 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1486 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1487 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1488 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1489 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1491 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1492 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1493 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1496 array
= stream
.create_packet().context_field
['array_field']
1497 array
.value
= values
1498 self
.assertEqual(values
, array
)
1499 values
[0]['an_int'] = 'a string'
1500 with self
.assertRaises(TypeError):
1501 array
.value
= values
1503 def test_str_op(self
):
1505 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1506 self
.assertEqual(expected_string
, s
)
1509 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1511 self
._tc
= get_default_trace_class()
1512 self
._def
= _create_int_array_field(self
._tc
, 3)
1515 self
._def
[2] = 1948754
1516 self
._def
_value
= [45, 1847, 1948754]
1518 def test_value_wrong_len(self
):
1520 with self
.assertRaises(ValueError):
1521 self
._def
.value
= values
1524 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1526 self
._tc
= get_default_trace_class()
1527 self
._def
= _create_dynamic_array(self
._tc
)
1530 self
._def
[2] = 1948754
1531 self
._def
_value
= [45, 1847, 1948754]
1533 def test_value_resize(self
):
1534 new_values
= [1, 2, 3, 4]
1535 self
._def
.value
= new_values
1536 self
.assertCountEqual(self
._def
, new_values
)
1538 def test_set_length(self
):
1539 self
._def
.length
= 4
1541 self
.assertEqual(len(self
._def
), 4)
1543 def test_set_invalid_length(self
):
1544 with self
.assertRaises(TypeError):
1545 self
._def
.length
= 'cheval'
1548 class StructureFieldTestCase(unittest
.TestCase
):
1549 def _create_fc(self
, tc
):
1550 fc
= tc
.create_structure_field_class()
1551 fc
.append_member('A', self
._fc
0_fn
())
1552 fc
.append_member('B', self
._fc
1_fn
())
1553 fc
.append_member('C', self
._fc
2_fn
())
1554 fc
.append_member('D', self
._fc
3_fn
())
1555 fc
.append_member('E', self
._fc
4_fn
())
1556 fc5
= self
._fc
5_fn
()
1557 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1558 fc
.append_member('F', fc5
)
1562 self
._tc
= get_default_trace_class()
1563 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1564 self
._fc
1_fn
= self
._tc
.create_string_field_class
1565 self
._fc
2_fn
= self
._tc
.create_real_field_class
1566 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1567 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1568 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1569 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1571 self
._fc
= self
._create
_fc
(self
._tc
)
1572 self
._def
= _create_field(self
._tc
, self
._fc
)
1573 self
._def
['A'] = -1872
1574 self
._def
['B'] = 'salut'
1575 self
._def
['C'] = 17.5
1576 self
._def
['D'] = 16497
1578 self
._def
['F'] = {'F_1': 52}
1588 def _modify_def(self
):
1589 self
._def
['B'] = 'hola'
1591 def test_bool_op_true(self
):
1592 self
.assertTrue(self
._def
)
1594 def test_bool_op_false(self
):
1595 field
= self
._def
['E']
1596 self
.assertFalse(field
)
1599 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1601 def test_getitem(self
):
1602 field
= self
._def
['A']
1603 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1604 self
.assertEqual(field
, -1872)
1606 def test_member_at_index_out_of_bounds_after(self
):
1607 with self
.assertRaises(IndexError):
1608 self
._def
.member_at_index(len(self
._def
_value
))
1611 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1613 field
['B'] = 'salut'
1617 field
['F'] = {'F_1': 52}
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 fc
= self
._tc
.create_structure_field_class()
1625 fc
.append_member('A', self
._fc
0_fn
())
1626 fc
.append_member('B', self
._fc
1_fn
())
1627 fc
.append_member('C', self
._fc
2_fn
())
1629 field
= _create_field(self
._tc
, fc
)
1631 field
['B'] = 'salut'
1633 self
.assertNotEqual(self
._def
, field
)
1635 def test_eq_diff_keys(self
):
1636 fc
= self
._tc
.create_structure_field_class()
1637 fc
.append_member('U', self
._fc
0_fn
())
1638 fc
.append_member('V', self
._fc
1_fn
())
1639 fc
.append_member('W', self
._fc
2_fn
())
1640 fc
.append_member('X', self
._fc
3_fn
())
1641 fc
.append_member('Y', self
._fc
4_fn
())
1642 fc
.append_member('Z', self
._fc
5_fn
())
1643 field
= _create_field(self
._tc
, fc
)
1645 field
['V'] = "gerry"
1650 self
.assertNotEqual(self
._def
, field
)
1652 def test_eq_diff_content_same_len(self
):
1653 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1655 field
['B'] = 'salut'
1659 field
['F'] = {'F_1': 0}
1660 self
.assertNotEqual(self
._def
, field
)
1662 def test_eq_same_content_diff_keys(self
):
1663 fc
= self
._tc
.create_structure_field_class()
1664 fc
.append_member('A', self
._fc
0_fn
())
1665 fc
.append_member('B', self
._fc
1_fn
())
1666 fc
.append_member('E', self
._fc
2_fn
())
1667 fc
.append_member('D', self
._fc
3_fn
())
1668 fc
.append_member('C', self
._fc
4_fn
())
1669 fc
.append_member('F', self
._fc
5_fn
())
1670 field
= _create_field(self
._tc
, fc
)
1672 field
['B'] = 'salut'
1677 self
.assertNotEqual(self
._def
, field
)
1679 def test_setitem(self
):
1680 self
._def
['C'] = -18.47
1681 self
.assertEqual(self
._def
['C'], -18.47)
1683 def test_setitem_int_field(self
):
1684 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1685 int_field
= _create_field(self
._tc
, int_fc
)
1686 int_field
.value
= 19487
1687 self
._def
['D'] = int_field
1688 self
.assertEqual(self
._def
['D'], 19487)
1690 def test_setitem_non_basic_field(self
):
1691 elem_fc
= self
._tc
.create_structure_field_class()
1692 struct_fc
= self
._tc
.create_structure_field_class()
1693 struct_fc
.append_member('A', elem_fc
)
1694 struct_field
= _create_field(self
._tc
, struct_fc
)
1696 # Will fail on access to .items() of the value
1697 with self
.assertRaises(AttributeError):
1698 struct_field
['A'] = 23
1700 def test_setitem_none(self
):
1701 with self
.assertRaises(TypeError):
1702 self
._def
['C'] = None
1704 def test_setitem_key_wrong_type(self
):
1705 with self
.assertRaises(TypeError):
1708 def test_setitem_wrong_key(self
):
1709 with self
.assertRaises(KeyError):
1710 self
._def
['hi'] = 134679
1712 def test_member_at_index(self
):
1713 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1715 def test_iter(self
):
1725 for vkey
, vval
in self
._def
.items():
1726 val
= orig_values
[vkey
]
1727 self
.assertEqual(vval
, val
)
1729 def test_value(self
):
1738 self
.assertEqual(self
._def
, orig_values
)
1740 def test_set_value(self
):
1741 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1742 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1743 str_fc
= self
._tc
.create_string_field_class()
1744 struct_fc
= self
._tc
.create_structure_field_class()
1745 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1746 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1747 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1748 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
1750 struct
= _create_field(self
._tc
, struct_fc
)
1751 struct
.value
= values
1752 self
.assertEqual(values
, struct
)
1754 bad_type_values
= copy
.deepcopy(values
)
1755 bad_type_values
['an_int'] = 'a string'
1756 with self
.assertRaises(TypeError):
1757 struct
.value
= bad_type_values
1759 unknown_key_values
= copy
.deepcopy(values
)
1760 unknown_key_values
['unknown_key'] = 16546
1761 with self
.assertRaises(KeyError):
1762 struct
.value
= unknown_key_values
1764 def test_str_op(self
):
1765 expected_string_found
= False
1767 # Establish all permutations of the three expected matches since
1768 # the order in which mappings are enumerated is not explicitly part of
1770 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1771 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1772 candidate
= '{{{}}}'.format(', '.join(items
))
1774 expected_string_found
= True
1777 self
.assertTrue(expected_string_found
)
1780 class VariantFieldTestCase(unittest
.TestCase
):
1781 def _create_fc(self
, tc
):
1782 ft0
= tc
.create_signed_integer_field_class(32)
1783 ft1
= tc
.create_string_field_class()
1784 ft2
= tc
.create_real_field_class()
1785 ft3
= tc
.create_signed_integer_field_class(17)
1786 fc
= tc
.create_variant_field_class()
1787 fc
.append_option('corner', ft0
)
1788 fc
.append_option('zoom', ft1
)
1789 fc
.append_option('mellotron', ft2
)
1790 fc
.append_option('giorgio', ft3
)
1791 top_fc
= tc
.create_structure_field_class()
1792 top_fc
.append_member('variant_field', fc
)
1796 self
._tc
= get_default_trace_class()
1797 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1798 self
._def
= fld
['variant_field']
1800 def test_bool_op(self
):
1801 self
._def
.selected_option_index
= 2
1802 self
._def
.value
= -17.34
1803 with self
.assertRaises(NotImplementedError):
1806 def test_selected_option_index(self
):
1807 self
._def
.selected_option_index
= 2
1808 self
.assertEqual(self
._def
.selected_option_index
, 2)
1810 def test_selected_option(self
):
1811 self
._def
.selected_option_index
= 2
1812 self
._def
.value
= -17.34
1813 self
.assertEqual(self
._def
.selected_option
, -17.34)
1815 self
._def
.selected_option_index
= 3
1816 self
._def
.value
= 1921
1817 self
.assertEqual(self
._def
.selected_option
, 1921)
1820 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1821 field
= field
['variant_field']
1822 field
.selected_option_index
= 0
1824 self
._def
.selected_option_index
= 0
1825 self
._def
.value
= 1774
1826 self
.assertEqual(self
._def
, field
)
1828 def test_eq_invalid_type(self
):
1829 self
._def
.selected_option_index
= 1
1830 self
._def
.value
= 'gerry'
1831 self
.assertNotEqual(self
._def
, 23)
1833 def test_str_op_int(self
):
1834 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1835 field
= field
['variant_field']
1836 field
.selected_option_index
= 0
1838 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1839 other_field
= other_field
['variant_field']
1840 other_field
.selected_option_index
= 0
1841 other_field
.value
= 1774
1842 self
.assertEqual(str(field
), str(other_field
))
1844 def test_str_op_str(self
):
1845 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1846 field
= field
['variant_field']
1847 field
.selected_option_index
= 1
1848 field
.value
= 'un beau grand bateau'
1849 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1850 other_field
= other_field
['variant_field']
1851 other_field
.selected_option_index
= 1
1852 other_field
.value
= 'un beau grand bateau'
1853 self
.assertEqual(str(field
), str(other_field
))
1855 def test_str_op_float(self
):
1856 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1857 field
= field
['variant_field']
1858 field
.selected_option_index
= 2
1859 field
.value
= 14.4245
1860 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1861 other_field
= other_field
['variant_field']
1862 other_field
.selected_option_index
= 2
1863 other_field
.value
= 14.4245
1864 self
.assertEqual(str(field
), str(other_field
))