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 BoolFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1081 def _create_fc(self
, tc
):
1082 return tc
.create_bool_field_class()
1085 self
._tc
= get_default_trace_class()
1086 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1087 self
._def
.value
= True
1088 self
._def
_value
= True
1089 self
._def
_new
_value
= False
1091 def test_assign_true(self
):
1093 self
._def
.value
= raw
1094 self
.assertEqual(self
._def
, raw
)
1096 def test_assign_false(self
):
1098 self
._def
.value
= raw
1099 self
.assertEqual(self
._def
, raw
)
1101 def test_assign_field_true(self
):
1102 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_field_false(self
):
1109 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1112 self
._def
.value
= field
1113 self
.assertEqual(self
._def
, raw
)
1115 def test_assign_invalid_type(self
):
1116 with self
.assertRaises(TypeError):
1117 self
._def
.value
= 17
1119 def test_str_op(self
):
1120 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1123 _inject_numeric_testing_methods(BoolFieldTestCase
)
1126 class _TestIntegerFieldCommon(_TestNumericField
):
1127 def test_assign_true(self
):
1129 self
._def
.value
= raw
1130 self
.assertEqual(self
._def
, raw
)
1132 def test_assign_false(self
):
1134 self
._def
.value
= raw
1135 self
.assertEqual(self
._def
, raw
)
1137 def test_assign_pos_int(self
):
1139 self
._def
.value
= raw
1140 self
.assertEqual(self
._def
, raw
)
1142 def test_assign_neg_int(self
):
1144 self
._def
.value
= raw
1145 self
.assertEqual(self
._def
, raw
)
1147 def test_assign_int_field(self
):
1149 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1151 self
._def
.value
= field
1152 self
.assertEqual(self
._def
, raw
)
1154 def test_assign_invalid_type(self
):
1155 with self
.assertRaises(TypeError):
1156 self
._def
.value
= 'yes'
1158 def test_assign_uint(self
):
1159 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1160 field
= _create_field(self
._tc
, uint_fc
)
1163 self
.assertEqual(field
, raw
)
1165 def test_assign_big_uint(self
):
1166 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
1167 field
= _create_field(self
._tc
, uint_fc
)
1168 # Larger than the IEEE 754 double-precision exact representation of
1171 field
.value
= (2 ** 53) + 1
1172 self
.assertEqual(field
, raw
)
1174 def test_assign_uint_invalid_neg(self
):
1175 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
1176 field
= _create_field(self
._tc
, uint_fc
)
1178 with self
.assertRaises(ValueError):
1181 def test_str_op(self
):
1182 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1185 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
1188 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1189 def _create_fc(self
, tc
):
1190 return tc
.create_signed_integer_field_class(25)
1193 self
._tc
= get_default_trace_class()
1194 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1195 self
._field
.value
= 17
1196 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1197 self
._def
.value
= 17
1198 self
._def
_value
= 17
1199 self
._def
_new
_value
= -101
1202 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
1203 def _create_fc(self
, tc
):
1204 fc
= tc
.create_signed_enumeration_field_class(32)
1205 fc
.add_mapping('something', bt2
.SignedIntegerRangeSet([(17, 17)]))
1206 fc
.add_mapping('speaker', bt2
.SignedIntegerRangeSet([(12, 16)]))
1207 fc
.add_mapping('can', bt2
.SignedIntegerRangeSet([(18, 2540)]))
1209 'whole range', bt2
.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1211 fc
.add_mapping('zip', bt2
.SignedIntegerRangeSet([(-45, 1001)]))
1215 self
._tc
= get_default_trace_class()
1216 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1217 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1218 self
._def
.value
= 17
1219 self
._def
_value
= 17
1220 self
._def
_new
_value
= -101
1222 def test_str_op(self
):
1223 expected_string_found
= False
1226 # Establish all permutations of the three expected matches since
1227 # the order in which mappings are enumerated is not explicitly part of
1229 for p
in itertools
.permutations(['whole range', 'something', 'zip']):
1230 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
1232 expected_string_found
= True
1235 self
.assertTrue(expected_string_found
)
1237 def test_labels(self
):
1238 self
._field
.value
= 17
1239 labels
= sorted(self
._field
.labels
)
1240 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
1243 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
1244 def _create_fc(self
, tc
):
1245 return tc
.create_real_field_class()
1248 self
._tc
= get_default_trace_class()
1249 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1250 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1251 self
._def
.value
= 52.7
1252 self
._def
_value
= 52.7
1253 self
._def
_new
_value
= -17.164857
1255 def _test_invalid_op(self
, cb
):
1256 with self
.assertRaises(TypeError):
1259 def test_assign_true(self
):
1260 self
._def
.value
= True
1261 self
.assertTrue(self
._def
)
1263 def test_assign_false(self
):
1264 self
._def
.value
= False
1265 self
.assertFalse(self
._def
)
1267 def test_assign_pos_int(self
):
1269 self
._def
.value
= raw
1270 self
.assertEqual(self
._def
, float(raw
))
1272 def test_assign_neg_int(self
):
1274 self
._def
.value
= raw
1275 self
.assertEqual(self
._def
, float(raw
))
1277 def test_assign_int_field(self
):
1278 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1279 int_field
= _create_field(self
._tc
, int_fc
)
1281 int_field
.value
= raw
1282 self
._def
.value
= int_field
1283 self
.assertEqual(self
._def
, float(raw
))
1285 def test_assign_float(self
):
1287 self
._def
.value
= raw
1288 self
.assertEqual(self
._def
, raw
)
1290 def test_assign_float_field(self
):
1291 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1294 self
._def
.value
= field
1295 self
.assertEqual(self
._def
, raw
)
1297 def test_assign_invalid_type(self
):
1298 with self
.assertRaises(TypeError):
1299 self
._def
.value
= 'yes'
1301 def test_invalid_lshift(self
):
1302 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1304 def test_invalid_rshift(self
):
1305 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1307 def test_invalid_and(self
):
1308 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1310 def test_invalid_or(self
):
1311 self
._test
_invalid
_op
(lambda: self
._def |
23)
1313 def test_invalid_xor(self
):
1314 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1316 def test_invalid_invert(self
):
1317 self
._test
_invalid
_op
(lambda: ~self
._def
)
1319 def test_str_op(self
):
1320 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1323 _inject_numeric_testing_methods(RealFieldTestCase
)
1326 class StringFieldTestCase(unittest
.TestCase
):
1328 self
._tc
= get_default_trace_class()
1329 self
._def
_value
= 'Hello, World!'
1330 self
._def
= _create_string_field(self
._tc
)
1331 self
._def
.value
= self
._def
_value
1332 self
._def
_new
_value
= 'Yes!'
1334 def test_assign_int(self
):
1335 with self
.assertRaises(TypeError):
1336 self
._def
.value
= 283
1338 def test_assign_string_field(self
):
1339 field
= _create_string_field(self
._tc
)
1342 self
.assertEqual(field
, raw
)
1345 self
.assertEqual(self
._def
, self
._def
_value
)
1347 def test_not_eq(self
):
1348 self
.assertNotEqual(self
._def
, 23)
1350 def test_lt_vstring(self
):
1351 s1
= _create_string_field(self
._tc
)
1353 s2
= _create_string_field(self
._tc
)
1355 self
.assertLess(s1
, s2
)
1357 def test_lt_string(self
):
1358 s1
= _create_string_field(self
._tc
)
1360 self
.assertLess(s1
, 'bateau')
1362 def test_le_vstring(self
):
1363 s1
= _create_string_field(self
._tc
)
1365 s2
= _create_string_field(self
._tc
)
1367 self
.assertLessEqual(s1
, s2
)
1369 def test_le_string(self
):
1370 s1
= _create_string_field(self
._tc
)
1372 self
.assertLessEqual(s1
, 'bateau')
1374 def test_gt_vstring(self
):
1375 s1
= _create_string_field(self
._tc
)
1377 s2
= _create_string_field(self
._tc
)
1379 self
.assertGreater(s2
, s1
)
1381 def test_gt_string(self
):
1382 s1
= _create_string_field(self
._tc
)
1384 self
.assertGreater('bateau', s1
)
1386 def test_ge_vstring(self
):
1387 s1
= _create_string_field(self
._tc
)
1389 s2
= _create_string_field(self
._tc
)
1391 self
.assertGreaterEqual(s2
, s1
)
1393 def test_ge_string(self
):
1394 s1
= _create_string_field(self
._tc
)
1396 self
.assertGreaterEqual('bateau', s1
)
1398 def test_bool_op(self
):
1399 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1401 def test_str_op(self
):
1402 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1405 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1407 def test_getitem(self
):
1408 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1410 def test_append_str(self
):
1411 to_append
= 'meow meow meow'
1412 self
._def
+= to_append
1413 self
._def
_value
+= to_append
1414 self
.assertEqual(self
._def
, self
._def
_value
)
1416 def test_append_string_field(self
):
1417 field
= _create_string_field(self
._tc
)
1418 to_append
= 'meow meow meow'
1419 field
.value
= to_append
1421 self
._def
_value
+= to_append
1422 self
.assertEqual(self
._def
, self
._def
_value
)
1425 class _TestArrayFieldCommon
:
1426 def _modify_def(self
):
1429 def test_bool_op_true(self
):
1430 self
.assertTrue(self
._def
)
1433 self
.assertEqual(len(self
._def
), 3)
1435 def test_length(self
):
1436 self
.assertEqual(self
._def
.length
, 3)
1438 def test_getitem(self
):
1439 field
= self
._def
[1]
1440 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1441 self
.assertEqual(field
, 1847)
1444 field
= _create_int_array_field(self
._tc
, 3)
1448 self
.assertEqual(self
._def
, field
)
1450 def test_eq_invalid_type(self
):
1451 self
.assertNotEqual(self
._def
, 23)
1453 def test_eq_diff_len(self
):
1454 field
= _create_int_array_field(self
._tc
, 2)
1457 self
.assertNotEqual(self
._def
, field
)
1459 def test_eq_diff_content_same_len(self
):
1460 field
= _create_int_array_field(self
._tc
, 3)
1464 self
.assertNotEqual(self
._def
, field
)
1466 def test_eq_non_sequence_iterable(self
):
1467 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1468 field
= _create_int_array_field(self
._tc
, 3)
1472 self
.assertEqual(field
, list(dct
.keys()))
1473 self
.assertNotEqual(field
, dct
)
1475 def test_setitem(self
):
1477 self
.assertEqual(self
._def
[2], 24)
1479 def test_setitem_int_field(self
):
1480 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1481 int_field
= _create_field(self
._tc
, int_fc
)
1482 int_field
.value
= 19487
1483 self
._def
[1] = int_field
1484 self
.assertEqual(self
._def
[1], 19487)
1486 def test_setitem_non_basic_field(self
):
1487 array_field
= _create_struct_array_field(self
._tc
, 2)
1488 with self
.assertRaises(TypeError):
1491 def test_setitem_none(self
):
1492 with self
.assertRaises(TypeError):
1495 def test_setitem_index_wrong_type(self
):
1496 with self
.assertRaises(TypeError):
1497 self
._def
['yes'] = 23
1499 def test_setitem_index_neg(self
):
1500 with self
.assertRaises(IndexError):
1503 def test_setitem_index_out_of_range(self
):
1504 with self
.assertRaises(IndexError):
1505 self
._def
[len(self
._def
)] = 134679
1507 def test_iter(self
):
1508 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1509 self
.assertEqual(field
, value
)
1511 def test_value_int_field(self
):
1512 values
= [45646, 145, 12145]
1513 self
._def
.value
= values
1514 self
.assertEqual(values
, self
._def
)
1516 def test_value_check_sequence(self
):
1518 with self
.assertRaises(TypeError):
1519 self
._def
.value
= values
1521 def test_value_wrong_type_in_sequence(self
):
1522 values
= [32, 'hello', 11]
1523 with self
.assertRaises(TypeError):
1524 self
._def
.value
= values
1526 def test_value_complex_type(self
):
1527 struct_fc
= self
._tc
.create_structure_field_class()
1528 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1529 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1530 str_fc
= self
._tc
.create_string_field_class()
1531 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1532 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1533 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1534 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1535 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1537 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1538 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1539 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1542 array
= stream
.create_packet().context_field
['array_field']
1543 array
.value
= values
1544 self
.assertEqual(values
, array
)
1545 values
[0]['an_int'] = 'a string'
1546 with self
.assertRaises(TypeError):
1547 array
.value
= values
1549 def test_str_op(self
):
1551 expected_string
= '[{}]'.format(', '.join([repr(v
) for v
in self
._def
_value
]))
1552 self
.assertEqual(expected_string
, s
)
1555 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1557 self
._tc
= get_default_trace_class()
1558 self
._def
= _create_int_array_field(self
._tc
, 3)
1561 self
._def
[2] = 1948754
1562 self
._def
_value
= [45, 1847, 1948754]
1564 def test_value_wrong_len(self
):
1566 with self
.assertRaises(ValueError):
1567 self
._def
.value
= values
1570 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1572 self
._tc
= get_default_trace_class()
1573 self
._def
= _create_dynamic_array(self
._tc
)
1576 self
._def
[2] = 1948754
1577 self
._def
_value
= [45, 1847, 1948754]
1579 def test_value_resize(self
):
1580 new_values
= [1, 2, 3, 4]
1581 self
._def
.value
= new_values
1582 self
.assertCountEqual(self
._def
, new_values
)
1584 def test_set_length(self
):
1585 self
._def
.length
= 4
1587 self
.assertEqual(len(self
._def
), 4)
1589 def test_set_invalid_length(self
):
1590 with self
.assertRaises(TypeError):
1591 self
._def
.length
= 'cheval'
1594 class StructureFieldTestCase(unittest
.TestCase
):
1595 def _create_fc(self
, tc
):
1596 fc
= tc
.create_structure_field_class()
1597 fc
.append_member('A', self
._fc
0_fn
())
1598 fc
.append_member('B', self
._fc
1_fn
())
1599 fc
.append_member('C', self
._fc
2_fn
())
1600 fc
.append_member('D', self
._fc
3_fn
())
1601 fc
.append_member('E', self
._fc
4_fn
())
1602 fc5
= self
._fc
5_fn
()
1603 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1604 fc
.append_member('F', fc5
)
1608 self
._tc
= get_default_trace_class()
1609 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1610 self
._fc
1_fn
= self
._tc
.create_string_field_class
1611 self
._fc
2_fn
= self
._tc
.create_real_field_class
1612 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1613 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1614 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1615 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1617 self
._fc
= self
._create
_fc
(self
._tc
)
1618 self
._def
= _create_field(self
._tc
, self
._fc
)
1619 self
._def
['A'] = -1872
1620 self
._def
['B'] = 'salut'
1621 self
._def
['C'] = 17.5
1622 self
._def
['D'] = 16497
1624 self
._def
['F'] = {'F_1': 52}
1634 def _modify_def(self
):
1635 self
._def
['B'] = 'hola'
1637 def test_bool_op_true(self
):
1638 self
.assertTrue(self
._def
)
1640 def test_bool_op_false(self
):
1641 field
= self
._def
['E']
1642 self
.assertFalse(field
)
1645 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1647 def test_getitem(self
):
1648 field
= self
._def
['A']
1649 self
.assertIs(type(field
), bt2
._SignedIntegerField
)
1650 self
.assertEqual(field
, -1872)
1652 def test_member_at_index_out_of_bounds_after(self
):
1653 with self
.assertRaises(IndexError):
1654 self
._def
.member_at_index(len(self
._def
_value
))
1657 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1659 field
['B'] = 'salut'
1663 field
['F'] = {'F_1': 52}
1664 self
.assertEqual(self
._def
, field
)
1666 def test_eq_invalid_type(self
):
1667 self
.assertNotEqual(self
._def
, 23)
1669 def test_eq_diff_len(self
):
1670 fc
= self
._tc
.create_structure_field_class()
1671 fc
.append_member('A', self
._fc
0_fn
())
1672 fc
.append_member('B', self
._fc
1_fn
())
1673 fc
.append_member('C', self
._fc
2_fn
())
1675 field
= _create_field(self
._tc
, fc
)
1677 field
['B'] = 'salut'
1679 self
.assertNotEqual(self
._def
, field
)
1681 def test_eq_diff_keys(self
):
1682 fc
= self
._tc
.create_structure_field_class()
1683 fc
.append_member('U', self
._fc
0_fn
())
1684 fc
.append_member('V', self
._fc
1_fn
())
1685 fc
.append_member('W', self
._fc
2_fn
())
1686 fc
.append_member('X', self
._fc
3_fn
())
1687 fc
.append_member('Y', self
._fc
4_fn
())
1688 fc
.append_member('Z', self
._fc
5_fn
())
1689 field
= _create_field(self
._tc
, fc
)
1691 field
['V'] = "gerry"
1696 self
.assertNotEqual(self
._def
, field
)
1698 def test_eq_diff_content_same_len(self
):
1699 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1701 field
['B'] = 'salut'
1705 field
['F'] = {'F_1': 0}
1706 self
.assertNotEqual(self
._def
, field
)
1708 def test_eq_same_content_diff_keys(self
):
1709 fc
= self
._tc
.create_structure_field_class()
1710 fc
.append_member('A', self
._fc
0_fn
())
1711 fc
.append_member('B', self
._fc
1_fn
())
1712 fc
.append_member('E', self
._fc
2_fn
())
1713 fc
.append_member('D', self
._fc
3_fn
())
1714 fc
.append_member('C', self
._fc
4_fn
())
1715 fc
.append_member('F', self
._fc
5_fn
())
1716 field
= _create_field(self
._tc
, fc
)
1718 field
['B'] = 'salut'
1723 self
.assertNotEqual(self
._def
, field
)
1725 def test_setitem(self
):
1726 self
._def
['C'] = -18.47
1727 self
.assertEqual(self
._def
['C'], -18.47)
1729 def test_setitem_int_field(self
):
1730 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1731 int_field
= _create_field(self
._tc
, int_fc
)
1732 int_field
.value
= 19487
1733 self
._def
['D'] = int_field
1734 self
.assertEqual(self
._def
['D'], 19487)
1736 def test_setitem_non_basic_field(self
):
1737 elem_fc
= self
._tc
.create_structure_field_class()
1738 struct_fc
= self
._tc
.create_structure_field_class()
1739 struct_fc
.append_member('A', elem_fc
)
1740 struct_field
= _create_field(self
._tc
, struct_fc
)
1742 # Will fail on access to .items() of the value
1743 with self
.assertRaises(AttributeError):
1744 struct_field
['A'] = 23
1746 def test_setitem_none(self
):
1747 with self
.assertRaises(TypeError):
1748 self
._def
['C'] = None
1750 def test_setitem_key_wrong_type(self
):
1751 with self
.assertRaises(TypeError):
1754 def test_setitem_wrong_key(self
):
1755 with self
.assertRaises(KeyError):
1756 self
._def
['hi'] = 134679
1758 def test_member_at_index(self
):
1759 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1761 def test_iter(self
):
1771 for vkey
, vval
in self
._def
.items():
1772 val
= orig_values
[vkey
]
1773 self
.assertEqual(vval
, val
)
1775 def test_value(self
):
1784 self
.assertEqual(self
._def
, orig_values
)
1786 def test_set_value(self
):
1787 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1788 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1789 str_fc
= self
._tc
.create_string_field_class()
1790 struct_fc
= self
._tc
.create_structure_field_class()
1791 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1792 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1793 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1794 values
= {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
1796 struct
= _create_field(self
._tc
, struct_fc
)
1797 struct
.value
= values
1798 self
.assertEqual(values
, struct
)
1800 bad_type_values
= copy
.deepcopy(values
)
1801 bad_type_values
['an_int'] = 'a string'
1802 with self
.assertRaises(TypeError):
1803 struct
.value
= bad_type_values
1805 unknown_key_values
= copy
.deepcopy(values
)
1806 unknown_key_values
['unknown_key'] = 16546
1807 with self
.assertRaises(KeyError):
1808 struct
.value
= unknown_key_values
1810 def test_str_op(self
):
1811 expected_string_found
= False
1813 # Establish all permutations of the three expected matches since
1814 # the order in which mappings are enumerated is not explicitly part of
1816 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1817 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1818 candidate
= '{{{}}}'.format(', '.join(items
))
1820 expected_string_found
= True
1823 self
.assertTrue(expected_string_found
)
1826 class OptionFieldTestCase(unittest
.TestCase
):
1827 def _create_fc(self
, tc
):
1828 fc
= tc
.create_option_field_class(tc
.create_string_field_class())
1829 top_fc
= tc
.create_structure_field_class()
1830 top_fc
.append_member('opt_field', fc
)
1834 self
._tc
= get_default_trace_class()
1835 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1836 self
._def
= fld
['opt_field']
1838 def test_value_prop(self
):
1839 self
._def
.value
= 'hiboux'
1840 self
.assertEqual(self
._def
.field
, 'hiboux')
1841 self
.assertTrue(self
._def
.has_field
)
1843 def test_has_field_prop_true(self
):
1844 self
._def
.has_field
= True
1845 self
.assertTrue(self
._def
.has_field
)
1847 def test_has_field_prop_true(self
):
1848 self
._def
.has_field
= False
1849 self
.assertFalse(self
._def
.has_field
)
1851 def test_bool_op_true(self
):
1852 self
._def
.value
= 'allo'
1853 self
.assertTrue(self
._def
)
1855 def test_bool_op_true(self
):
1856 self
._def
.has_field
= False
1857 self
.assertFalse(self
._def
)
1859 def test_field_prop_existing(self
):
1860 self
._def
.value
= 'meow'
1861 field
= self
._def
.field
1862 self
.assertEqual(field
, 'meow')
1864 def test_field_prop_none(self
):
1865 self
._def
.has_field
= False
1866 field
= self
._def
.field
1867 self
.assertIsNone(field
)
1869 def test_field_prop_existing_then_none(self
):
1870 self
._def
.value
= 'meow'
1871 field
= self
._def
.field
1872 self
.assertEqual(field
, 'meow')
1873 self
._def
.has_field
= False
1874 field
= self
._def
.field
1875 self
.assertIsNone(field
)
1878 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1879 field
= field
['opt_field']
1880 field
.value
= 'walk'
1881 self
._def
.value
= 'walk'
1882 self
.assertEqual(self
._def
, field
)
1884 def test_eq_invalid_type(self
):
1885 self
._def
.value
= 'gerry'
1886 self
.assertNotEqual(self
._def
, 23)
1888 def test_str_op(self
):
1889 self
._def
.value
= 'marcel'
1890 self
.assertEqual(str(self
._def
), str(self
._def
.field
))
1892 def test_repr_op(self
):
1893 self
._def
.value
= 'mireille'
1894 self
.assertEqual(repr(self
._def
), repr(self
._def
.field
))
1897 class VariantFieldTestCase(unittest
.TestCase
):
1898 def _create_fc(self
, tc
):
1899 ft0
= tc
.create_signed_integer_field_class(32)
1900 ft1
= tc
.create_string_field_class()
1901 ft2
= tc
.create_real_field_class()
1902 ft3
= tc
.create_signed_integer_field_class(17)
1903 fc
= tc
.create_variant_field_class()
1904 fc
.append_option('corner', ft0
)
1905 fc
.append_option('zoom', ft1
)
1906 fc
.append_option('mellotron', ft2
)
1907 fc
.append_option('giorgio', ft3
)
1908 top_fc
= tc
.create_structure_field_class()
1909 top_fc
.append_member('variant_field', fc
)
1913 self
._tc
= get_default_trace_class()
1914 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1915 self
._def
= fld
['variant_field']
1917 def test_bool_op(self
):
1918 self
._def
.selected_option_index
= 2
1919 self
._def
.value
= -17.34
1920 with self
.assertRaises(NotImplementedError):
1923 def test_selected_option_index(self
):
1924 self
._def
.selected_option_index
= 2
1925 self
.assertEqual(self
._def
.selected_option_index
, 2)
1927 def test_selected_option(self
):
1928 self
._def
.selected_option_index
= 2
1929 self
._def
.value
= -17.34
1930 self
.assertEqual(self
._def
.selected_option
, -17.34)
1932 self
._def
.selected_option_index
= 3
1933 self
._def
.value
= 1921
1934 self
.assertEqual(self
._def
.selected_option
, 1921)
1937 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1938 field
= field
['variant_field']
1939 field
.selected_option_index
= 0
1941 self
._def
.selected_option_index
= 0
1942 self
._def
.value
= 1774
1943 self
.assertEqual(self
._def
, field
)
1945 def test_eq_invalid_type(self
):
1946 self
._def
.selected_option_index
= 1
1947 self
._def
.value
= 'gerry'
1948 self
.assertNotEqual(self
._def
, 23)
1950 def test_str_op_int(self
):
1951 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1952 field
= field
['variant_field']
1953 field
.selected_option_index
= 0
1955 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1956 other_field
= other_field
['variant_field']
1957 other_field
.selected_option_index
= 0
1958 other_field
.value
= 1774
1959 self
.assertEqual(str(field
), str(other_field
))
1961 def test_str_op_str(self
):
1962 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1963 field
= field
['variant_field']
1964 field
.selected_option_index
= 1
1965 field
.value
= 'un beau grand bateau'
1966 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1967 other_field
= other_field
['variant_field']
1968 other_field
.selected_option_index
= 1
1969 other_field
.value
= 'un beau grand bateau'
1970 self
.assertEqual(str(field
), str(other_field
))
1972 def test_str_op_float(self
):
1973 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1974 field
= field
['variant_field']
1975 field
.selected_option_index
= 2
1976 field
.value
= 14.4245
1977 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1978 other_field
= other_field
['variant_field']
1979 other_field
.selected_option_index
= 2
1980 other_field
.value
= 14.4245
1981 self
.assertEqual(str(field
), str(other_field
))