1 from functools
import partial
, partialmethod
8 from utils
import get_default_trace_class
17 # Create and return a stream with the field classes part of its stream packet
20 # The stream is part of a dummy trace created from trace class `tc`.
22 def _create_stream(tc
, ctx_field_classes
):
23 packet_context_fc
= tc
.create_structure_field_class()
24 for name
, fc
in ctx_field_classes
:
25 packet_context_fc
.append_member(name
, fc
)
28 stream_class
= tc
.create_stream_class(packet_context_field_class
=packet_context_fc
)
30 stream
= trace
.create_stream(stream_class
)
34 # Create a field of the given field class.
36 # The field is part of a dummy stream, itself part of a dummy trace created
37 # from trace class `tc`.
39 def _create_field(tc
, field_class
):
41 stream
= _create_stream(tc
, [(field_name
, field_class
)])
42 packet
= stream
.create_packet()
43 return packet
.context_field
[field_name
]
46 # Create a field of type string.
48 # The field is part of a dummy stream, itself part of a dummy trace created
49 # from trace class `tc`. It is made out of a dummy string field class.
51 def _create_string_field(tc
):
52 field_name
= 'string_field'
53 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
54 packet
= stream
.create_packet()
55 return packet
.context_field
[field_name
]
58 # Create a field of type static array of ints.
60 # The field is part of a dummy stream, itself part of a dummy trace created
61 # from trace class `tc`. It is made out of a dummy static array field class,
62 # with a dummy integer field class as element class.
64 def _create_int_array_field(tc
, length
):
65 elem_fc
= tc
.create_signed_integer_field_class(32)
66 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
67 field_name
= 'int_array'
68 stream
= _create_stream(tc
, [(field_name
, fc
)])
69 packet
= stream
.create_packet()
70 return packet
.context_field
[field_name
]
73 # Create a field of type dynamic array of ints.
75 # The field is part of a dummy stream, itself part of a dummy trace created
76 # from trace class `tc`. It is made out of a dummy static array field class,
77 # with a dummy integer field class as element and length classes.
79 def _create_dynamic_array(tc
):
80 elem_fc
= tc
.create_signed_integer_field_class(32)
81 len_fc
= tc
.create_signed_integer_field_class(32)
82 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
83 field_name
= 'int_dyn_array'
84 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
85 packet
= stream
.create_packet()
86 packet
.context_field
[field_name
].length
= 3
87 return packet
.context_field
[field_name
]
90 # Create a field of type array of (empty) structures.
92 # The field is part of a dummy stream, itself part of a dummy trace created
93 # from trace class `tc`. It is made out of a dummy static array field class,
94 # with a dummy struct field class as element class.
96 def _create_struct_array_field(tc
, length
):
97 elem_fc
= tc
.create_structure_field_class()
98 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
99 field_name
= 'struct_array'
100 stream
= _create_stream(tc
, [(field_name
, fc
)])
101 packet
= stream
.create_packet()
102 return packet
.context_field
[field_name
]
105 class _TestNumericField
:
106 def _binop(self
, op
, rhs
):
112 r
= op(self
._def
, rhs
)
113 except Exception as e
:
117 rv
= op(self
._def
_value
, comp_value
)
118 except Exception as e
:
121 if rexc
is not None or rvexc
is not None:
122 # at least one of the operations raised an exception: in
123 # this case both operations should have raised the same
124 # type of exception (division by zero, bit shift with a
125 # floating point number operand, etc.)
126 self
.assertIs(type(rexc
), type(rvexc
))
131 def _unaryop(self
, op
):
137 except Exception as e
:
141 rv
= op(self
._def
_value
)
142 except Exception as e
:
145 if rexc
is not None or rvexc
is not None:
146 # at least one of the operations raised an exception: in
147 # this case both operations should have raised the same
148 # type of exception (division by zero, bit shift with a
149 # floating point number operand, etc.)
150 self
.assertIs(type(rexc
), type(rvexc
))
155 def _test_unaryop_type(self
, op
):
156 r
, rv
= self
._unaryop
(op
)
161 self
.assertIsInstance(r
, type(rv
))
163 def _test_unaryop_value(self
, op
):
164 r
, rv
= self
._unaryop
(op
)
169 self
.assertEqual(r
, rv
)
171 def _test_unaryop_addr_same(self
, op
):
172 addr_before
= self
._def
.addr
174 self
.assertEqual(self
._def
.addr
, addr_before
)
176 def _test_unaryop_value_same(self
, op
):
177 value_before
= copy
.copy(self
._def
_value
)
179 self
.assertEqual(self
._def
, value_before
)
181 def _test_binop_type(self
, op
, rhs
):
182 r
, rv
= self
._binop
(op
, rhs
)
187 if op
in _COMP_BINOPS
:
188 # __eq__() and __ne__() always return a 'bool' object
189 self
.assertIsInstance(r
, bool)
191 self
.assertIsInstance(r
, type(rv
))
193 def _test_binop_value(self
, op
, rhs
):
194 r
, rv
= self
._binop
(op
, rhs
)
199 self
.assertEqual(r
, rv
)
201 def _test_binop_lhs_addr_same(self
, op
, rhs
):
202 addr_before
= self
._def
.addr
203 r
, rv
= self
._binop
(op
, rhs
)
204 self
.assertEqual(self
._def
.addr
, addr_before
)
206 @unittest.skip('copy is not implemented')
207 def _test_binop_lhs_value_same(self
, op
, rhs
):
208 value_before
= copy
.copy(self
._def
)
209 r
, rv
= self
._binop
(op
, rhs
)
210 self
.assertEqual(self
._def
, value_before
)
212 def _test_binop_invalid_unknown(self
, op
):
213 if op
in _COMP_BINOPS
:
214 self
.skipTest('not testing')
219 with self
.assertRaises(TypeError):
222 def _test_binop_invalid_none(self
, op
):
223 if op
in _COMP_BINOPS
:
224 self
.skipTest('not testing')
226 with self
.assertRaises(TypeError):
229 def _test_ibinop_value(self
, op
, rhs
):
230 r
, rv
= self
._binop
(op
, rhs
)
235 # The inplace operators are special for field objects because
236 # they do not return a new, immutable object like it's the case
237 # for Python numbers. In Python, `a += 2`, where `a` is a number
238 # object, assigns a new number object reference to `a`, dropping
239 # the old reference. Since BT's field objects are mutable, we
240 # modify their internal value with the inplace operators. This
241 # means however that we can lose data in the process, for
244 # int_value_obj += 3.3
246 # Here, if `int_value_obj` is a Python `int` with the value 2,
247 # it would be a `float` object after this, holding the value
248 # 5.3. In our case, if `int_value_obj` is an integer field
249 # object, 3.3 is converted to an `int` object (3) and added to
250 # the current value of `int_value_obj`, so after this the value
251 # of the object is 5. This does not compare to 5.3, which is
252 # why we also use the `int()` type here.
253 if isinstance(self
._def
, bt2
.field
._IntegerField
):
256 self
.assertEqual(r
, rv
)
258 def _test_ibinop_type(self
, op
, rhs
):
259 r
, rv
= self
._binop
(op
, rhs
)
264 self
.assertIs(r
, self
._def
)
266 def _test_ibinop_invalid_unknown(self
, op
):
270 with self
.assertRaises(TypeError):
273 def _test_ibinop_invalid_none(self
, op
):
274 with self
.assertRaises(TypeError):
277 def _test_binop_rhs_false(self
, test_cb
, op
):
280 def _test_binop_rhs_true(self
, test_cb
, op
):
283 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
286 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
289 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
292 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
293 test_cb(op
, bt2
.create_value(2))
295 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
296 test_cb(op
, bt2
.create_value(-23))
298 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
299 test_cb(op
, bt2
.create_value(0))
301 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
304 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
307 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
310 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
311 test_cb(op
, bt2
.create_value(2.2))
313 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
314 test_cb(op
, bt2
.create_value(-23.4))
316 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
317 test_cb(op
, bt2
.create_value(0.0))
319 def _test_binop_type_false(self
, op
):
320 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
322 def _test_binop_type_true(self
, op
):
323 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
325 def _test_binop_type_pos_int(self
, op
):
326 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
328 def _test_binop_type_neg_int(self
, op
):
329 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
331 def _test_binop_type_zero_int(self
, op
):
332 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
334 def _test_binop_type_pos_vint(self
, op
):
335 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
337 def _test_binop_type_neg_vint(self
, op
):
338 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
340 def _test_binop_type_zero_vint(self
, op
):
341 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
343 def _test_binop_type_pos_float(self
, op
):
344 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
346 def _test_binop_type_neg_float(self
, op
):
347 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
349 def _test_binop_type_zero_float(self
, op
):
350 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
352 def _test_binop_type_pos_vfloat(self
, op
):
353 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
355 def _test_binop_type_neg_vfloat(self
, op
):
356 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
358 def _test_binop_type_zero_vfloat(self
, op
):
359 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
361 def _test_binop_value_false(self
, op
):
362 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
364 def _test_binop_value_true(self
, op
):
365 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
367 def _test_binop_value_pos_int(self
, op
):
368 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
370 def _test_binop_value_neg_int(self
, op
):
371 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
373 def _test_binop_value_zero_int(self
, op
):
374 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
376 def _test_binop_value_pos_vint(self
, op
):
377 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
379 def _test_binop_value_neg_vint(self
, op
):
380 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
382 def _test_binop_value_zero_vint(self
, op
):
383 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
385 def _test_binop_value_pos_float(self
, op
):
386 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
388 def _test_binop_value_neg_float(self
, op
):
389 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
391 def _test_binop_value_zero_float(self
, op
):
392 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
394 def _test_binop_value_pos_vfloat(self
, op
):
395 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
397 def _test_binop_value_neg_vfloat(self
, op
):
398 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
400 def _test_binop_value_zero_vfloat(self
, op
):
401 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
403 def _test_binop_lhs_addr_same_false(self
, op
):
404 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
406 def _test_binop_lhs_addr_same_true(self
, op
):
407 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
409 def _test_binop_lhs_addr_same_pos_int(self
, op
):
410 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
412 def _test_binop_lhs_addr_same_neg_int(self
, op
):
413 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
415 def _test_binop_lhs_addr_same_zero_int(self
, op
):
416 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
418 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
419 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
421 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
422 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
424 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
425 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
427 def _test_binop_lhs_addr_same_pos_float(self
, op
):
428 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
430 def _test_binop_lhs_addr_same_neg_float(self
, op
):
431 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
433 def _test_binop_lhs_addr_same_zero_float(self
, op
):
434 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
436 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
437 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
439 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
440 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
442 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
443 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
445 def _test_binop_lhs_value_same_false(self
, op
):
446 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
448 def _test_binop_lhs_value_same_true(self
, op
):
449 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
451 def _test_binop_lhs_value_same_pos_int(self
, op
):
452 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
454 def _test_binop_lhs_value_same_neg_int(self
, op
):
455 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
457 def _test_binop_lhs_value_same_zero_int(self
, op
):
458 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
460 def _test_binop_lhs_value_same_pos_vint(self
, op
):
461 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
463 def _test_binop_lhs_value_same_neg_vint(self
, op
):
464 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
466 def _test_binop_lhs_value_same_zero_vint(self
, op
):
467 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
469 def _test_binop_lhs_value_same_pos_float(self
, op
):
470 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
472 def _test_binop_lhs_value_same_neg_float(self
, op
):
473 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
475 def _test_binop_lhs_value_same_zero_float(self
, op
):
476 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
478 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
479 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
481 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
482 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
484 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
485 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
487 def _test_ibinop_type_false(self
, op
):
488 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_type
, op
)
490 def _test_ibinop_type_true(self
, op
):
491 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_type
, op
)
493 def _test_ibinop_type_pos_int(self
, op
):
494 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_type
, op
)
496 def _test_ibinop_type_neg_int(self
, op
):
497 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_type
, op
)
499 def _test_ibinop_type_zero_int(self
, op
):
500 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_type
, op
)
502 def _test_ibinop_type_pos_vint(self
, op
):
503 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_type
, op
)
505 def _test_ibinop_type_neg_vint(self
, op
):
506 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_type
, op
)
508 def _test_ibinop_type_zero_vint(self
, op
):
509 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_type
, op
)
511 def _test_ibinop_type_pos_float(self
, op
):
512 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_type
, op
)
514 def _test_ibinop_type_neg_float(self
, op
):
515 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_type
, op
)
517 def _test_ibinop_type_zero_float(self
, op
):
518 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_type
, op
)
520 def _test_ibinop_type_pos_vfloat(self
, op
):
521 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_type
, op
)
523 def _test_ibinop_type_neg_vfloat(self
, op
):
524 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_type
, op
)
526 def _test_ibinop_type_zero_vfloat(self
, op
):
527 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_type
, op
)
529 def _test_ibinop_value_false(self
, op
):
530 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_value
, op
)
532 def _test_ibinop_value_true(self
, op
):
533 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_value
, op
)
535 def _test_ibinop_value_pos_int(self
, op
):
536 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_value
, op
)
538 def _test_ibinop_value_neg_int(self
, op
):
539 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_value
, op
)
541 def _test_ibinop_value_zero_int(self
, op
):
542 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_value
, op
)
544 def _test_ibinop_value_pos_vint(self
, op
):
545 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_value
, op
)
547 def _test_ibinop_value_neg_vint(self
, op
):
548 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_value
, op
)
550 def _test_ibinop_value_zero_vint(self
, op
):
551 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_value
, op
)
553 def _test_ibinop_value_pos_float(self
, op
):
554 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_value
, op
)
556 def _test_ibinop_value_neg_float(self
, op
):
557 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_value
, op
)
559 def _test_ibinop_value_zero_float(self
, op
):
560 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_value
, op
)
562 def _test_ibinop_value_pos_vfloat(self
, op
):
563 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_value
, op
)
565 def _test_ibinop_value_neg_vfloat(self
, op
):
566 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_value
, op
)
568 def _test_ibinop_value_zero_vfloat(self
, op
):
569 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_value
, op
)
571 def test_bool_op(self
):
572 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
574 def test_int_op(self
):
575 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
577 def test_float_op(self
):
578 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
580 def test_complex_op(self
):
581 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
583 def test_str_op(self
):
584 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
586 def test_eq_none(self
):
587 # Ignore this lint error:
588 # E711 comparison to None should be 'if cond is None:'
589 # since this is what we want to test (even though not good practice).
590 self
.assertFalse(self
._def
== None) # noqa: E711
592 def test_ne_none(self
):
593 # Ignore this lint error:
594 # E711 comparison to None should be 'if cond is not None:'
595 # since this is what we want to test (even though not good practice).
596 self
.assertTrue(self
._def
!= None) # noqa: E711
606 ('add', operator
.add
),
607 ('radd', lambda a
, b
: operator
.add(b
, a
)),
608 ('and', operator
.and_
),
609 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
610 ('floordiv', operator
.floordiv
),
611 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
612 ('lshift', operator
.lshift
),
613 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
614 ('mod', operator
.mod
),
615 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
616 ('mul', operator
.mul
),
617 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
618 ('or', operator
.or_
),
619 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
620 ('pow', operator
.pow),
621 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
622 ('rshift', operator
.rshift
),
623 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
624 ('sub', operator
.sub
),
625 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
626 ('truediv', operator
.truediv
),
627 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
628 ('xor', operator
.xor
),
629 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
634 ('iadd', operator
.iadd
),
635 ('iand', operator
.iand
),
636 ('ifloordiv', operator
.ifloordiv
),
637 ('ilshift', operator
.ilshift
),
638 ('imod', operator
.imod
),
639 ('imul', operator
.imul
),
640 ('ior', operator
.ior
),
641 ('ipow', operator
.ipow
),
642 ('irshift', operator
.irshift
),
643 ('isub', operator
.isub
),
644 ('itruediv', operator
.itruediv
),
645 ('ixor', operator
.ixor
),
650 ('neg', operator
.neg
),
651 ('pos', operator
.pos
),
652 ('abs', operator
.abs),
653 ('invert', operator
.invert
),
655 ('round_0', partial(round, ndigits
=0)),
656 ('round_1', partial(round, ndigits
=1)),
657 ('round_2', partial(round, ndigits
=2)),
658 ('round_3', partial(round, ndigits
=3)),
660 ('floor', math
.floor
),
661 ('trunc', math
.trunc
),
665 def _inject_numeric_testing_methods(cls
):
666 def test_binop_name(suffix
):
667 return 'test_binop_{}_{}'.format(name
, suffix
)
669 def test_ibinop_name(suffix
):
670 return 'test_ibinop_{}_{}'.format(name
, suffix
)
672 def test_unaryop_name(suffix
):
673 return 'test_unaryop_{}_{}'.format(name
, suffix
)
675 # inject testing methods for each binary operation
676 for name
, binop
in _BINOPS
:
677 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
))
678 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
))
679 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
))
680 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
))
681 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
))
682 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
))
683 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
))
684 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
))
685 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
686 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
687 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
688 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
689 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
690 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
691 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
))
692 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
))
693 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
))
694 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
))
695 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
696 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
697 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
698 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
699 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
))
700 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
))
701 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
))
702 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
))
703 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
))
704 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
))
705 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
706 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
707 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
708 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
709 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
710 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
711 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
))
712 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
))
713 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
714 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
715 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
))
716 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
))
717 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
718 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
719 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
720 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
721 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
722 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
723 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
724 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
725 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
726 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
727 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
))
728 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
729 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
))
730 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
731 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
732 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
733 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
734 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
736 # inject testing methods for each unary operation
737 for name
, unaryop
in _UNARYOPS
:
738 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
))
739 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
))
740 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
))
741 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
))
743 # inject testing methods for each inplace binary operation
744 for name
, ibinop
in _IBINOPS
:
745 setattr(cls
, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_ibinop
_invalid
_unknown
, op
=ibinop
))
746 setattr(cls
, test_ibinop_name('invalid_none'), partialmethod(_TestNumericField
._test
_ibinop
_invalid
_none
, op
=ibinop
))
747 setattr(cls
, test_ibinop_name('type_true'), partialmethod(_TestNumericField
._test
_ibinop
_type
_true
, op
=ibinop
))
748 setattr(cls
, test_ibinop_name('value_true'), partialmethod(_TestNumericField
._test
_ibinop
_value
_true
, op
=ibinop
))
749 setattr(cls
, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_int
, op
=ibinop
))
750 setattr(cls
, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_vint
, op
=ibinop
))
751 setattr(cls
, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_int
, op
=ibinop
))
752 setattr(cls
, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_vint
, op
=ibinop
))
753 setattr(cls
, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_int
, op
=ibinop
))
754 setattr(cls
, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_vint
, op
=ibinop
))
755 setattr(cls
, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_int
, op
=ibinop
))
756 setattr(cls
, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_vint
, op
=ibinop
))
757 setattr(cls
, test_ibinop_name('type_false'), partialmethod(_TestNumericField
._test
_ibinop
_type
_false
, op
=ibinop
))
758 setattr(cls
, test_ibinop_name('value_false'), partialmethod(_TestNumericField
._test
_ibinop
_value
_false
, op
=ibinop
))
759 setattr(cls
, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_int
, op
=ibinop
))
760 setattr(cls
, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_vint
, op
=ibinop
))
761 setattr(cls
, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_int
, op
=ibinop
))
762 setattr(cls
, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_vint
, op
=ibinop
))
763 setattr(cls
, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_float
, op
=ibinop
))
764 setattr(cls
, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_float
, op
=ibinop
))
765 setattr(cls
, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_vfloat
, op
=ibinop
))
766 setattr(cls
, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_vfloat
, op
=ibinop
))
767 setattr(cls
, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_float
, op
=ibinop
))
768 setattr(cls
, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_float
, op
=ibinop
))
769 setattr(cls
, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_vfloat
, op
=ibinop
))
770 setattr(cls
, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_vfloat
, op
=ibinop
))
771 setattr(cls
, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_float
, op
=ibinop
))
772 setattr(cls
, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_vfloat
, op
=ibinop
))
773 setattr(cls
, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_float
, op
=ibinop
))
774 setattr(cls
, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_vfloat
, op
=ibinop
))
777 class _TestIntegerFieldCommon(_TestNumericField
):
778 def test_assign_true(self
):
780 self
._def
.value
= raw
781 self
.assertEqual(self
._def
, raw
)
783 def test_assign_false(self
):
785 self
._def
.value
= raw
786 self
.assertEqual(self
._def
, raw
)
788 def test_assign_pos_int(self
):
790 self
._def
.value
= raw
791 self
.assertEqual(self
._def
, raw
)
793 def test_assign_neg_int(self
):
795 self
._def
.value
= raw
796 self
.assertEqual(self
._def
, raw
)
798 def test_assign_int_field(self
):
800 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
802 self
._def
.value
= field
803 self
.assertEqual(self
._def
, raw
)
805 def test_assign_float(self
):
807 self
._def
.value
= raw
808 self
.assertEqual(self
._def
, int(raw
))
810 def test_assign_invalid_type(self
):
811 with self
.assertRaises(TypeError):
812 self
._def
.value
= 'yes'
814 def test_assign_uint(self
):
815 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
816 field
= _create_field(self
._tc
, uint_fc
)
819 self
.assertEqual(field
, raw
)
821 def test_assign_uint_invalid_neg(self
):
822 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
823 field
= _create_field(self
._tc
, uint_fc
)
825 with self
.assertRaises(ValueError):
828 def test_str_op(self
):
829 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
832 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
835 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
836 def _create_fc(self
, tc
):
837 return tc
.create_signed_integer_field_class(25)
840 self
._tc
= get_default_trace_class()
841 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
842 self
._field
.value
= 17
843 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
846 self
._def
_new
_value
= -101
849 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
850 def _create_fc(self
, tc
):
851 fc
= tc
.create_signed_enumeration_field_class(32)
852 fc
.map_range('something', 17)
853 fc
.map_range('speaker', 12, 16)
854 fc
.map_range('can', 18, 2540)
855 fc
.map_range('whole range', -(2 ** 31), (2 ** 31) - 1)
856 fc
.map_range('zip', -45, 1001)
860 self
._tc
= get_default_trace_class()
861 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
862 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
865 self
._def
_new
_value
= -101
867 def test_str_op(self
):
868 expected_string_found
= False
871 # Establish all permutations of the three expected matches since
872 # the order in which mappings are enumerated is not explicitly part of
874 for p
in itertools
.permutations(['whole range', 'something',
876 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
878 expected_string_found
= True
881 self
.assertTrue(expected_string_found
)
883 def test_labels(self
):
884 self
._field
.value
= 17
885 labels
= sorted(self
._field
.labels
)
886 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
889 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
890 def _create_fc(self
, tc
):
891 return tc
.create_real_field_class()
894 self
._tc
= get_default_trace_class()
895 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
896 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
897 self
._def
.value
= 52.7
898 self
._def
_value
= 52.7
899 self
._def
_new
_value
= -17.164857
901 def _test_invalid_op(self
, cb
):
902 with self
.assertRaises(TypeError):
905 def test_assign_true(self
):
906 self
._def
.value
= True
907 self
.assertTrue(self
._def
)
909 def test_assign_false(self
):
910 self
._def
.value
= False
911 self
.assertFalse(self
._def
)
913 def test_assign_pos_int(self
):
915 self
._def
.value
= raw
916 self
.assertEqual(self
._def
, float(raw
))
918 def test_assign_neg_int(self
):
920 self
._def
.value
= raw
921 self
.assertEqual(self
._def
, float(raw
))
923 def test_assign_int_field(self
):
924 int_fc
= self
._tc
.create_signed_integer_field_class(32)
925 int_field
= _create_field(self
._tc
, int_fc
)
927 int_field
.value
= raw
928 self
._def
.value
= int_field
929 self
.assertEqual(self
._def
, float(raw
))
931 def test_assign_float(self
):
933 self
._def
.value
= raw
934 self
.assertEqual(self
._def
, raw
)
936 def test_assign_float_field(self
):
937 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
940 self
._def
.value
= field
941 self
.assertEqual(self
._def
, raw
)
943 def test_assign_invalid_type(self
):
944 with self
.assertRaises(TypeError):
945 self
._def
.value
= 'yes'
947 def test_invalid_lshift(self
):
948 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
950 def test_invalid_rshift(self
):
951 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
953 def test_invalid_and(self
):
954 self
._test
_invalid
_op
(lambda: self
._def
& 23)
956 def test_invalid_or(self
):
957 self
._test
_invalid
_op
(lambda: self
._def |
23)
959 def test_invalid_xor(self
):
960 self
._test
_invalid
_op
(lambda: self
._def ^
23)
962 def test_invalid_invert(self
):
963 self
._test
_invalid
_op
(lambda: ~self
._def
)
965 def test_str_op(self
):
966 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
969 _inject_numeric_testing_methods(RealFieldTestCase
)
972 class StringFieldTestCase(unittest
.TestCase
):
974 self
._tc
= get_default_trace_class()
975 self
._def
_value
= 'Hello, World!'
976 self
._def
= _create_string_field(self
._tc
)
977 self
._def
.value
= self
._def
_value
978 self
._def
_new
_value
= 'Yes!'
980 def test_assign_int(self
):
981 with self
.assertRaises(TypeError):
982 self
._def
.value
= 283
984 def test_assign_string_field(self
):
985 field
= _create_string_field(self
._tc
)
988 self
.assertEqual(field
, raw
)
991 self
.assertEqual(self
._def
, self
._def
_value
)
993 def test_not_eq(self
):
994 self
.assertNotEqual(self
._def
, 23)
996 def test_lt_vstring(self
):
997 s1
= _create_string_field(self
._tc
)
999 s2
= _create_string_field(self
._tc
)
1001 self
.assertLess(s1
, s2
)
1003 def test_lt_string(self
):
1004 s1
= _create_string_field(self
._tc
)
1006 self
.assertLess(s1
, 'bateau')
1008 def test_le_vstring(self
):
1009 s1
= _create_string_field(self
._tc
)
1011 s2
= _create_string_field(self
._tc
)
1013 self
.assertLessEqual(s1
, s2
)
1015 def test_le_string(self
):
1016 s1
= _create_string_field(self
._tc
)
1018 self
.assertLessEqual(s1
, 'bateau')
1020 def test_gt_vstring(self
):
1021 s1
= _create_string_field(self
._tc
)
1023 s2
= _create_string_field(self
._tc
)
1025 self
.assertGreater(s2
, s1
)
1027 def test_gt_string(self
):
1028 s1
= _create_string_field(self
._tc
)
1030 self
.assertGreater('bateau', s1
)
1032 def test_ge_vstring(self
):
1033 s1
= _create_string_field(self
._tc
)
1035 s2
= _create_string_field(self
._tc
)
1037 self
.assertGreaterEqual(s2
, s1
)
1039 def test_ge_string(self
):
1040 s1
= _create_string_field(self
._tc
)
1042 self
.assertGreaterEqual('bateau', s1
)
1044 def test_bool_op(self
):
1045 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1047 def test_str_op(self
):
1048 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1051 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1053 def test_getitem(self
):
1054 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1056 def test_append_str(self
):
1057 to_append
= 'meow meow meow'
1058 self
._def
+= to_append
1059 self
._def
_value
+= to_append
1060 self
.assertEqual(self
._def
, self
._def
_value
)
1062 def test_append_string_field(self
):
1063 field
= _create_string_field(self
._tc
)
1064 to_append
= 'meow meow meow'
1065 field
.value
= to_append
1067 self
._def
_value
+= to_append
1068 self
.assertEqual(self
._def
, self
._def
_value
)
1071 class _TestArrayFieldCommon
:
1072 def _modify_def(self
):
1075 def test_bool_op_true(self
):
1076 self
.assertTrue(self
._def
)
1079 self
.assertEqual(len(self
._def
), 3)
1081 def test_length(self
):
1082 self
.assertEqual(self
._def
.length
, 3)
1084 def test_getitem(self
):
1085 field
= self
._def
[1]
1086 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1087 self
.assertEqual(field
, 1847)
1090 field
= _create_int_array_field(self
._tc
, 3)
1094 self
.assertEqual(self
._def
, field
)
1096 def test_eq_invalid_type(self
):
1097 self
.assertNotEqual(self
._def
, 23)
1099 def test_eq_diff_len(self
):
1100 field
= _create_int_array_field(self
._tc
, 2)
1103 self
.assertNotEqual(self
._def
, field
)
1105 def test_eq_diff_content_same_len(self
):
1106 field
= _create_int_array_field(self
._tc
, 3)
1110 self
.assertNotEqual(self
._def
, field
)
1112 def test_setitem(self
):
1114 self
.assertEqual(self
._def
[2], 24)
1116 def test_setitem_int_field(self
):
1117 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1118 int_field
= _create_field(self
._tc
, int_fc
)
1119 int_field
.value
= 19487
1120 self
._def
[1] = int_field
1121 self
.assertEqual(self
._def
[1], 19487)
1123 def test_setitem_non_basic_field(self
):
1124 array_field
= _create_struct_array_field(self
._tc
, 2)
1125 with self
.assertRaises(TypeError):
1128 def test_setitem_none(self
):
1129 with self
.assertRaises(TypeError):
1132 def test_setitem_index_wrong_type(self
):
1133 with self
.assertRaises(TypeError):
1134 self
._def
['yes'] = 23
1136 def test_setitem_index_neg(self
):
1137 with self
.assertRaises(IndexError):
1140 def test_setitem_index_out_of_range(self
):
1141 with self
.assertRaises(IndexError):
1142 self
._def
[len(self
._def
)] = 134679
1144 def test_iter(self
):
1145 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1146 self
.assertEqual(field
, value
)
1148 def test_value_int_field(self
):
1149 values
= [45646, 145, 12145]
1150 self
._def
.value
= values
1151 self
.assertEqual(values
, self
._def
)
1153 def test_value_check_sequence(self
):
1155 with self
.assertRaises(TypeError):
1156 self
._def
.value
= values
1158 def test_value_wrong_type_in_sequence(self
):
1159 values
= [32, 'hello', 11]
1160 with self
.assertRaises(TypeError):
1161 self
._def
.value
= values
1163 def test_value_complex_type(self
):
1164 struct_fc
= self
._tc
.create_structure_field_class()
1165 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1166 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1167 str_fc
= self
._tc
.create_string_field_class()
1168 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1169 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1170 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1171 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1172 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1176 'a_string': 'hello',
1181 'a_string': 'goodbye',
1186 'a_string': 'or not',
1191 array
= stream
.create_packet().context_field
['array_field']
1192 array
.value
= values
1193 self
.assertEqual(values
, array
)
1194 values
[0]['an_int'] = 'a string'
1195 with self
.assertRaises(TypeError):
1196 array
.value
= values
1198 def test_str_op(self
):
1200 expected_string
= '[{}]'.format(', '.join(
1201 [repr(v
) for v
in self
._def
_value
]))
1202 self
.assertEqual(expected_string
, s
)
1205 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1207 self
._tc
= get_default_trace_class()
1208 self
._def
= _create_int_array_field(self
._tc
, 3)
1211 self
._def
[2] = 1948754
1212 self
._def
_value
= [45, 1847, 1948754]
1214 def test_value_wrong_len(self
):
1216 with self
.assertRaises(ValueError):
1217 self
._def
.value
= values
1220 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1222 self
._tc
= get_default_trace_class()
1223 self
._def
= _create_dynamic_array(self
._tc
)
1226 self
._def
[2] = 1948754
1227 self
._def
_value
= [45, 1847, 1948754]
1229 def test_value_resize(self
):
1230 new_values
= [1, 2, 3, 4]
1231 self
._def
.value
= new_values
1232 self
.assertCountEqual(self
._def
, new_values
)
1234 def test_set_length(self
):
1235 self
._def
.length
= 4
1237 self
.assertEqual(len(self
._def
), 4)
1239 def test_set_invalid_length(self
):
1240 with self
.assertRaises(TypeError):
1241 self
._def
.length
= 'cheval'
1244 class StructureFieldTestCase(unittest
.TestCase
):
1245 def _create_fc(self
, tc
):
1246 fc
= tc
.create_structure_field_class()
1247 fc
.append_member('A', self
._fc
0_fn
())
1248 fc
.append_member('B', self
._fc
1_fn
())
1249 fc
.append_member('C', self
._fc
2_fn
())
1250 fc
.append_member('D', self
._fc
3_fn
())
1251 fc
.append_member('E', self
._fc
4_fn
())
1252 fc5
= self
._fc
5_fn
()
1253 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1254 fc
.append_member('F', fc5
)
1258 self
._tc
= get_default_trace_class()
1259 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1260 self
._fc
1_fn
= self
._tc
.create_string_field_class
1261 self
._fc
2_fn
= self
._tc
.create_real_field_class
1262 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1263 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1264 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1265 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1267 self
._fc
= self
._create
_fc
(self
._tc
)
1268 self
._def
= _create_field(self
._tc
, self
._fc
)
1269 self
._def
['A'] = -1872
1270 self
._def
['B'] = 'salut'
1271 self
._def
['C'] = 17.5
1272 self
._def
['D'] = 16497
1274 self
._def
['F'] = {'F_1': 52}
1284 def _modify_def(self
):
1285 self
._def
['B'] = 'hola'
1287 def test_bool_op_true(self
):
1288 self
.assertTrue(self
._def
)
1290 def test_bool_op_false(self
):
1291 field
= self
._def
['E']
1292 self
.assertFalse(field
)
1295 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1297 def test_getitem(self
):
1298 field
= self
._def
['A']
1299 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1300 self
.assertEqual(field
, -1872)
1302 def test_member_at_index_out_of_bounds_after(self
):
1303 with self
.assertRaises(IndexError):
1304 self
._def
.member_at_index(len(self
._def
_value
))
1307 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
, ))
1309 field
['B'] = 'salut'
1313 field
['F'] = {'F_1': 52}
1314 self
.assertEqual(self
._def
, field
)
1316 def test_eq_invalid_type(self
):
1317 self
.assertNotEqual(self
._def
, 23)
1319 def test_eq_diff_len(self
):
1320 fc
= self
._tc
.create_structure_field_class()
1321 fc
.append_member('A', self
._fc
0_fn
())
1322 fc
.append_member('B', self
._fc
1_fn
())
1323 fc
.append_member('C', self
._fc
2_fn
())
1325 field
= _create_field(self
._tc
, fc
)
1327 field
['B'] = 'salut'
1329 self
.assertNotEqual(self
._def
, field
)
1331 def test_eq_diff_keys(self
):
1332 fc
= self
._tc
.create_structure_field_class()
1333 fc
.append_member('U', self
._fc
0_fn
())
1334 fc
.append_member('V', self
._fc
1_fn
())
1335 fc
.append_member('W', self
._fc
2_fn
())
1336 fc
.append_member('X', self
._fc
3_fn
())
1337 fc
.append_member('Y', self
._fc
4_fn
())
1338 fc
.append_member('Z', self
._fc
5_fn
())
1339 field
= _create_field(self
._tc
, fc
)
1341 field
['V'] = "gerry"
1346 self
.assertNotEqual(self
._def
, field
)
1348 def test_eq_diff_content_same_len(self
):
1349 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1351 field
['B'] = 'salut'
1355 field
['F'] = {'F_1': 0}
1356 self
.assertNotEqual(self
._def
, field
)
1358 def test_eq_same_content_diff_keys(self
):
1359 fc
= self
._tc
.create_structure_field_class()
1360 fc
.append_member('A', self
._fc
0_fn
())
1361 fc
.append_member('B', self
._fc
1_fn
())
1362 fc
.append_member('E', self
._fc
2_fn
())
1363 fc
.append_member('D', self
._fc
3_fn
())
1364 fc
.append_member('C', self
._fc
4_fn
())
1365 fc
.append_member('F', self
._fc
5_fn
())
1366 field
= _create_field(self
._tc
, fc
)
1368 field
['B'] = 'salut'
1373 self
.assertNotEqual(self
._def
, field
)
1375 def test_setitem(self
):
1376 self
._def
['C'] = -18.47
1377 self
.assertEqual(self
._def
['C'], -18.47)
1379 def test_setitem_int_field(self
):
1380 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1381 int_field
= _create_field(self
._tc
, int_fc
)
1382 int_field
.value
= 19487
1383 self
._def
['D'] = int_field
1384 self
.assertEqual(self
._def
['D'], 19487)
1386 def test_setitem_non_basic_field(self
):
1387 elem_fc
= self
._tc
.create_structure_field_class()
1388 struct_fc
= self
._tc
.create_structure_field_class()
1389 struct_fc
.append_member('A', elem_fc
)
1390 struct_field
= _create_field(self
._tc
, struct_fc
)
1392 # Will fail on access to .items() of the value
1393 with self
.assertRaises(AttributeError):
1394 struct_field
['A'] = 23
1396 def test_setitem_none(self
):
1397 with self
.assertRaises(TypeError):
1398 self
._def
['C'] = None
1400 def test_setitem_key_wrong_type(self
):
1401 with self
.assertRaises(TypeError):
1404 def test_setitem_wrong_key(self
):
1405 with self
.assertRaises(KeyError):
1406 self
._def
['hi'] = 134679
1408 def test_member_at_index(self
):
1409 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1411 def test_iter(self
):
1421 for vkey
, vval
in self
._def
.items():
1422 val
= orig_values
[vkey
]
1423 self
.assertEqual(vval
, val
)
1425 def test_value(self
):
1434 self
.assertEqual(self
._def
, orig_values
)
1436 def test_set_value(self
):
1437 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1438 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1439 str_fc
= self
._tc
.create_string_field_class()
1440 struct_fc
= self
._tc
.create_structure_field_class()
1441 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1442 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1443 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1446 'a_string': 'hello',
1450 struct
= _create_field(self
._tc
, struct_fc
)
1451 struct
.value
= values
1452 self
.assertEqual(values
, struct
)
1454 bad_type_values
= copy
.deepcopy(values
)
1455 bad_type_values
['an_int'] = 'a string'
1456 with self
.assertRaises(TypeError):
1457 struct
.value
= bad_type_values
1459 unknown_key_values
= copy
.deepcopy(values
)
1460 unknown_key_values
['unknown_key'] = 16546
1461 with self
.assertRaises(KeyError):
1462 struct
.value
= unknown_key_values
1464 def test_str_op(self
):
1465 expected_string_found
= False
1467 # Establish all permutations of the three expected matches since
1468 # the order in which mappings are enumerated is not explicitly part of
1470 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1471 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1472 candidate
= '{{{}}}'.format(', '.join(items
))
1474 expected_string_found
= True
1477 self
.assertTrue(expected_string_found
)
1480 class VariantFieldTestCase(unittest
.TestCase
):
1481 def _create_fc(self
, tc
):
1482 selector_fc
= tc
.create_signed_enumeration_field_class(field_value_range
=32)
1483 selector_fc
.map_range('corner', 23)
1484 selector_fc
.map_range('zoom', 17, 20)
1485 selector_fc
.map_range('mellotron', 1001)
1486 selector_fc
.map_range('giorgio', 2000, 3000)
1488 ft0
= tc
.create_signed_integer_field_class(32)
1489 ft1
= tc
.create_string_field_class()
1490 ft2
= tc
.create_real_field_class()
1491 ft3
= tc
.create_signed_integer_field_class(17)
1493 fc
= tc
.create_variant_field_class()
1494 fc
.append_option('corner', ft0
)
1495 fc
.append_option('zoom', ft1
)
1496 fc
.append_option('mellotron', ft2
)
1497 fc
.append_option('giorgio', ft3
)
1498 fc
.selector_field_class
= selector_fc
1500 top_fc
= tc
.create_structure_field_class()
1501 top_fc
.append_member('selector_field', selector_fc
)
1502 top_fc
.append_member('variant_field', fc
)
1506 self
._tc
= get_default_trace_class()
1507 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1508 self
._def
= fld
['variant_field']
1510 def test_bool_op(self
):
1511 self
._def
.selected_option_index
= 2
1512 self
._def
.value
= -17.34
1513 with self
.assertRaises(NotImplementedError):
1516 def test_selected_option_index(self
):
1517 self
._def
.selected_option_index
= 2
1518 self
.assertEqual(self
._def
.selected_option_index
, 2)
1520 def test_selected_option(self
):
1521 self
._def
.selected_option_index
= 2
1522 self
._def
.value
= -17.34
1523 self
.assertEqual(self
._def
.selected_option
, -17.34)
1525 self
._def
.selected_option_index
= 3
1526 self
._def
.value
= 1921
1527 self
.assertEqual(self
._def
.selected_option
, 1921)
1530 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1531 field
= field
['variant_field']
1532 field
.selected_option_index
= 0
1534 self
._def
.selected_option_index
= 0
1535 self
._def
.value
= 1774
1536 self
.assertEqual(self
._def
, field
)
1538 def test_eq_invalid_type(self
):
1539 self
._def
.selected_option_index
= 1
1540 self
._def
.value
= 'gerry'
1541 self
.assertNotEqual(self
._def
, 23)
1543 def test_str_op_int(self
):
1544 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1545 field
= field
['variant_field']
1546 field
.selected_option_index
= 0
1548 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1549 other_field
= other_field
['variant_field']
1550 other_field
.selected_option_index
= 0
1551 other_field
.value
= 1774
1552 self
.assertEqual(str(field
), str(other_field
))
1554 def test_str_op_str(self
):
1555 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1556 field
= field
['variant_field']
1557 field
.selected_option_index
= 1
1558 field
.value
= 'un beau grand bateau'
1559 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1560 other_field
= other_field
['variant_field']
1561 other_field
.selected_option_index
= 1
1562 other_field
.value
= 'un beau grand bateau'
1563 self
.assertEqual(str(field
), str(other_field
))
1565 def test_str_op_float(self
):
1566 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1567 field
= field
['variant_field']
1568 field
.selected_option_index
= 2
1569 field
.value
= 14.4245
1570 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1571 other_field
= other_field
['variant_field']
1572 other_field
.selected_option_index
= 2
1573 other_field
.value
= 14.4245
1574 self
.assertEqual(str(field
), str(other_field
))