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
26 from utils
import get_default_trace_class
35 # Create and return a stream with the field classes part of its stream packet
38 # The stream is part of a dummy trace created from trace class `tc`.
40 def _create_stream(tc
, ctx_field_classes
):
41 packet_context_fc
= tc
.create_structure_field_class()
42 for name
, fc
in ctx_field_classes
:
43 packet_context_fc
.append_member(name
, fc
)
46 stream_class
= tc
.create_stream_class(packet_context_field_class
=packet_context_fc
)
48 stream
= trace
.create_stream(stream_class
)
52 # Create a field of the given field class.
54 # The field is part of a dummy stream, itself part of a dummy trace created
55 # from trace class `tc`.
57 def _create_field(tc
, field_class
):
59 stream
= _create_stream(tc
, [(field_name
, field_class
)])
60 packet
= stream
.create_packet()
61 return packet
.context_field
[field_name
]
64 # Create a field of type string.
66 # The field is part of a dummy stream, itself part of a dummy trace created
67 # from trace class `tc`. It is made out of a dummy string field class.
69 def _create_string_field(tc
):
70 field_name
= 'string_field'
71 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
72 packet
= stream
.create_packet()
73 return packet
.context_field
[field_name
]
76 # Create a field of type static array of ints.
78 # The field is part of a dummy stream, itself part of a dummy trace created
79 # from trace class `tc`. It is made out of a dummy static array field class,
80 # with a dummy integer field class as element class.
82 def _create_int_array_field(tc
, length
):
83 elem_fc
= tc
.create_signed_integer_field_class(32)
84 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
85 field_name
= 'int_array'
86 stream
= _create_stream(tc
, [(field_name
, fc
)])
87 packet
= stream
.create_packet()
88 return packet
.context_field
[field_name
]
91 # Create a field of type dynamic array of ints.
93 # The field is part of a dummy stream, itself part of a dummy trace created
94 # from trace class `tc`. It is made out of a dummy static array field class,
95 # with a dummy integer field class as element and length classes.
97 def _create_dynamic_array(tc
):
98 elem_fc
= tc
.create_signed_integer_field_class(32)
99 len_fc
= tc
.create_signed_integer_field_class(32)
100 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
101 field_name
= 'int_dyn_array'
102 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
103 packet
= stream
.create_packet()
104 packet
.context_field
[field_name
].length
= 3
105 return packet
.context_field
[field_name
]
108 # Create a field of type array of (empty) structures.
110 # The field is part of a dummy stream, itself part of a dummy trace created
111 # from trace class `tc`. It is made out of a dummy static array field class,
112 # with a dummy struct field class as element class.
114 def _create_struct_array_field(tc
, length
):
115 elem_fc
= tc
.create_structure_field_class()
116 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
117 field_name
= 'struct_array'
118 stream
= _create_stream(tc
, [(field_name
, fc
)])
119 packet
= stream
.create_packet()
120 return packet
.context_field
[field_name
]
123 class _TestNumericField
:
124 def _binop(self
, op
, rhs
):
130 r
= op(self
._def
, rhs
)
131 except Exception as e
:
135 rv
= op(self
._def
_value
, comp_value
)
136 except Exception as e
:
139 if rexc
is not None or rvexc
is not None:
140 # at least one of the operations raised an exception: in
141 # this case both operations should have raised the same
142 # type of exception (division by zero, bit shift with a
143 # floating point number operand, etc.)
144 self
.assertIs(type(rexc
), type(rvexc
))
149 def _unaryop(self
, op
):
155 except Exception as e
:
159 rv
= op(self
._def
_value
)
160 except Exception as e
:
163 if rexc
is not None or rvexc
is not None:
164 # at least one of the operations raised an exception: in
165 # this case both operations should have raised the same
166 # type of exception (division by zero, bit shift with a
167 # floating point number operand, etc.)
168 self
.assertIs(type(rexc
), type(rvexc
))
173 def _test_unaryop_type(self
, op
):
174 r
, rv
= self
._unaryop
(op
)
179 self
.assertIsInstance(r
, type(rv
))
181 def _test_unaryop_value(self
, op
):
182 r
, rv
= self
._unaryop
(op
)
187 self
.assertEqual(r
, rv
)
189 def _test_unaryop_addr_same(self
, op
):
190 addr_before
= self
._def
.addr
192 self
.assertEqual(self
._def
.addr
, addr_before
)
194 def _test_unaryop_value_same(self
, op
):
195 value_before
= copy
.copy(self
._def
_value
)
197 self
.assertEqual(self
._def
, value_before
)
199 def _test_binop_type(self
, op
, rhs
):
200 r
, rv
= self
._binop
(op
, rhs
)
205 if op
in _COMP_BINOPS
:
206 # __eq__() and __ne__() always return a 'bool' object
207 self
.assertIsInstance(r
, bool)
209 self
.assertIsInstance(r
, type(rv
))
211 def _test_binop_value(self
, op
, rhs
):
212 r
, rv
= self
._binop
(op
, rhs
)
217 self
.assertEqual(r
, rv
)
219 def _test_binop_lhs_addr_same(self
, op
, rhs
):
220 addr_before
= self
._def
.addr
221 r
, rv
= self
._binop
(op
, rhs
)
222 self
.assertEqual(self
._def
.addr
, addr_before
)
224 @unittest.skip('copy is not implemented')
225 def _test_binop_lhs_value_same(self
, op
, rhs
):
226 value_before
= copy
.copy(self
._def
)
227 r
, rv
= self
._binop
(op
, rhs
)
228 self
.assertEqual(self
._def
, value_before
)
230 def _test_binop_invalid_unknown(self
, op
):
231 if op
in _COMP_BINOPS
:
232 self
.skipTest('not testing')
237 with self
.assertRaises(TypeError):
240 def _test_binop_invalid_none(self
, op
):
241 if op
in _COMP_BINOPS
:
242 self
.skipTest('not testing')
244 with self
.assertRaises(TypeError):
247 def _test_ibinop_value(self
, op
, rhs
):
248 r
, rv
= self
._binop
(op
, rhs
)
253 # The inplace operators are special for field objects because
254 # they do not return a new, immutable object like it's the case
255 # for Python numbers. In Python, `a += 2`, where `a` is a number
256 # object, assigns a new number object reference to `a`, dropping
257 # the old reference. Since BT's field objects are mutable, we
258 # modify their internal value with the inplace operators. This
259 # means however that we can lose data in the process, for
262 # int_value_obj += 3.3
264 # Here, if `int_value_obj` is a Python `int` with the value 2,
265 # it would be a `float` object after this, holding the value
266 # 5.3. In our case, if `int_value_obj` is an integer field
267 # object, 3.3 is converted to an `int` object (3) and added to
268 # the current value of `int_value_obj`, so after this the value
269 # of the object is 5. This does not compare to 5.3, which is
270 # why we also use the `int()` type here.
271 if isinstance(self
._def
, bt2
.field
._IntegerField
):
274 self
.assertEqual(r
, rv
)
276 def _test_ibinop_type(self
, op
, rhs
):
277 r
, rv
= self
._binop
(op
, rhs
)
282 self
.assertIs(r
, self
._def
)
284 def _test_ibinop_invalid_unknown(self
, op
):
288 with self
.assertRaises(TypeError):
291 def _test_ibinop_invalid_none(self
, op
):
292 with self
.assertRaises(TypeError):
295 def _test_binop_rhs_false(self
, test_cb
, op
):
298 def _test_binop_rhs_true(self
, test_cb
, op
):
301 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
304 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
307 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
310 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
311 test_cb(op
, bt2
.create_value(2))
313 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
314 test_cb(op
, bt2
.create_value(-23))
316 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
317 test_cb(op
, bt2
.create_value(0))
319 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
322 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
325 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
328 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
329 test_cb(op
, bt2
.create_value(2.2))
331 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
332 test_cb(op
, bt2
.create_value(-23.4))
334 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
335 test_cb(op
, bt2
.create_value(0.0))
337 def _test_binop_type_false(self
, op
):
338 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
340 def _test_binop_type_true(self
, op
):
341 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
343 def _test_binop_type_pos_int(self
, op
):
344 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
346 def _test_binop_type_neg_int(self
, op
):
347 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
349 def _test_binop_type_zero_int(self
, op
):
350 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
352 def _test_binop_type_pos_vint(self
, op
):
353 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
355 def _test_binop_type_neg_vint(self
, op
):
356 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
358 def _test_binop_type_zero_vint(self
, op
):
359 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
361 def _test_binop_type_pos_float(self
, op
):
362 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
364 def _test_binop_type_neg_float(self
, op
):
365 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
367 def _test_binop_type_zero_float(self
, op
):
368 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
370 def _test_binop_type_pos_vfloat(self
, op
):
371 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
373 def _test_binop_type_neg_vfloat(self
, op
):
374 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
376 def _test_binop_type_zero_vfloat(self
, op
):
377 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
379 def _test_binop_value_false(self
, op
):
380 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
382 def _test_binop_value_true(self
, op
):
383 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
385 def _test_binop_value_pos_int(self
, op
):
386 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
388 def _test_binop_value_neg_int(self
, op
):
389 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
391 def _test_binop_value_zero_int(self
, op
):
392 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
394 def _test_binop_value_pos_vint(self
, op
):
395 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
397 def _test_binop_value_neg_vint(self
, op
):
398 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
400 def _test_binop_value_zero_vint(self
, op
):
401 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
403 def _test_binop_value_pos_float(self
, op
):
404 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
406 def _test_binop_value_neg_float(self
, op
):
407 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
409 def _test_binop_value_zero_float(self
, op
):
410 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
412 def _test_binop_value_pos_vfloat(self
, op
):
413 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
415 def _test_binop_value_neg_vfloat(self
, op
):
416 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
418 def _test_binop_value_zero_vfloat(self
, op
):
419 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
421 def _test_binop_lhs_addr_same_false(self
, op
):
422 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
424 def _test_binop_lhs_addr_same_true(self
, op
):
425 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
427 def _test_binop_lhs_addr_same_pos_int(self
, op
):
428 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
430 def _test_binop_lhs_addr_same_neg_int(self
, op
):
431 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
433 def _test_binop_lhs_addr_same_zero_int(self
, op
):
434 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
436 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
437 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
439 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
440 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
442 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
443 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
445 def _test_binop_lhs_addr_same_pos_float(self
, op
):
446 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
448 def _test_binop_lhs_addr_same_neg_float(self
, op
):
449 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
451 def _test_binop_lhs_addr_same_zero_float(self
, op
):
452 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
454 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
455 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
457 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
458 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
460 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
461 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
463 def _test_binop_lhs_value_same_false(self
, op
):
464 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
466 def _test_binop_lhs_value_same_true(self
, op
):
467 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
469 def _test_binop_lhs_value_same_pos_int(self
, op
):
470 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
472 def _test_binop_lhs_value_same_neg_int(self
, op
):
473 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
475 def _test_binop_lhs_value_same_zero_int(self
, op
):
476 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
478 def _test_binop_lhs_value_same_pos_vint(self
, op
):
479 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
481 def _test_binop_lhs_value_same_neg_vint(self
, op
):
482 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
484 def _test_binop_lhs_value_same_zero_vint(self
, op
):
485 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
487 def _test_binop_lhs_value_same_pos_float(self
, op
):
488 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
490 def _test_binop_lhs_value_same_neg_float(self
, op
):
491 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
493 def _test_binop_lhs_value_same_zero_float(self
, op
):
494 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
496 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
497 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
499 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
500 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
502 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
503 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
505 def _test_ibinop_type_false(self
, op
):
506 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_type
, op
)
508 def _test_ibinop_type_true(self
, op
):
509 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_type
, op
)
511 def _test_ibinop_type_pos_int(self
, op
):
512 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_type
, op
)
514 def _test_ibinop_type_neg_int(self
, op
):
515 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_type
, op
)
517 def _test_ibinop_type_zero_int(self
, op
):
518 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_type
, op
)
520 def _test_ibinop_type_pos_vint(self
, op
):
521 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_type
, op
)
523 def _test_ibinop_type_neg_vint(self
, op
):
524 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_type
, op
)
526 def _test_ibinop_type_zero_vint(self
, op
):
527 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_type
, op
)
529 def _test_ibinop_type_pos_float(self
, op
):
530 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_type
, op
)
532 def _test_ibinop_type_neg_float(self
, op
):
533 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_type
, op
)
535 def _test_ibinop_type_zero_float(self
, op
):
536 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_type
, op
)
538 def _test_ibinop_type_pos_vfloat(self
, op
):
539 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_type
, op
)
541 def _test_ibinop_type_neg_vfloat(self
, op
):
542 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_type
, op
)
544 def _test_ibinop_type_zero_vfloat(self
, op
):
545 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_type
, op
)
547 def _test_ibinop_value_false(self
, op
):
548 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_value
, op
)
550 def _test_ibinop_value_true(self
, op
):
551 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_value
, op
)
553 def _test_ibinop_value_pos_int(self
, op
):
554 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_value
, op
)
556 def _test_ibinop_value_neg_int(self
, op
):
557 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_value
, op
)
559 def _test_ibinop_value_zero_int(self
, op
):
560 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_value
, op
)
562 def _test_ibinop_value_pos_vint(self
, op
):
563 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_value
, op
)
565 def _test_ibinop_value_neg_vint(self
, op
):
566 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_value
, op
)
568 def _test_ibinop_value_zero_vint(self
, op
):
569 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_value
, op
)
571 def _test_ibinop_value_pos_float(self
, op
):
572 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_value
, op
)
574 def _test_ibinop_value_neg_float(self
, op
):
575 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_value
, op
)
577 def _test_ibinop_value_zero_float(self
, op
):
578 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_value
, op
)
580 def _test_ibinop_value_pos_vfloat(self
, op
):
581 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_value
, op
)
583 def _test_ibinop_value_neg_vfloat(self
, op
):
584 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_value
, op
)
586 def _test_ibinop_value_zero_vfloat(self
, op
):
587 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_value
, op
)
589 def test_bool_op(self
):
590 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
592 def test_int_op(self
):
593 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
595 def test_float_op(self
):
596 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
598 def test_complex_op(self
):
599 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
601 def test_str_op(self
):
602 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
604 def test_eq_none(self
):
605 # Ignore this lint error:
606 # E711 comparison to None should be 'if cond is None:'
607 # since this is what we want to test (even though not good practice).
608 self
.assertFalse(self
._def
== None) # noqa: E711
610 def test_ne_none(self
):
611 # Ignore this lint error:
612 # E711 comparison to None should be 'if cond is not None:'
613 # since this is what we want to test (even though not good practice).
614 self
.assertTrue(self
._def
!= None) # noqa: E711
624 ('add', operator
.add
),
625 ('radd', lambda a
, b
: operator
.add(b
, a
)),
626 ('and', operator
.and_
),
627 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
628 ('floordiv', operator
.floordiv
),
629 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
630 ('lshift', operator
.lshift
),
631 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
632 ('mod', operator
.mod
),
633 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
634 ('mul', operator
.mul
),
635 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
636 ('or', operator
.or_
),
637 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
638 ('pow', operator
.pow),
639 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
640 ('rshift', operator
.rshift
),
641 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
642 ('sub', operator
.sub
),
643 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
644 ('truediv', operator
.truediv
),
645 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
646 ('xor', operator
.xor
),
647 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
652 ('iadd', operator
.iadd
),
653 ('iand', operator
.iand
),
654 ('ifloordiv', operator
.ifloordiv
),
655 ('ilshift', operator
.ilshift
),
656 ('imod', operator
.imod
),
657 ('imul', operator
.imul
),
658 ('ior', operator
.ior
),
659 ('ipow', operator
.ipow
),
660 ('irshift', operator
.irshift
),
661 ('isub', operator
.isub
),
662 ('itruediv', operator
.itruediv
),
663 ('ixor', operator
.ixor
),
668 ('neg', operator
.neg
),
669 ('pos', operator
.pos
),
670 ('abs', operator
.abs),
671 ('invert', operator
.invert
),
673 ('round_0', partial(round, ndigits
=0)),
674 ('round_1', partial(round, ndigits
=1)),
675 ('round_2', partial(round, ndigits
=2)),
676 ('round_3', partial(round, ndigits
=3)),
678 ('floor', math
.floor
),
679 ('trunc', math
.trunc
),
683 def _inject_numeric_testing_methods(cls
):
684 def test_binop_name(suffix
):
685 return 'test_binop_{}_{}'.format(name
, suffix
)
687 def test_ibinop_name(suffix
):
688 return 'test_ibinop_{}_{}'.format(name
, suffix
)
690 def test_unaryop_name(suffix
):
691 return 'test_unaryop_{}_{}'.format(name
, suffix
)
693 # inject testing methods for each binary operation
694 for name
, binop
in _BINOPS
:
695 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
))
696 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
))
697 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
))
698 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
))
699 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
))
700 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
))
701 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
))
702 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
))
703 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
704 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
705 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
706 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
707 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
708 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
709 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
))
710 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
))
711 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
))
712 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
))
713 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
714 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
715 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
716 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
717 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
))
718 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
))
719 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
))
720 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
))
721 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
))
722 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
))
723 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
724 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
725 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
726 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
727 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
728 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
729 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
))
730 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
))
731 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
732 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
733 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
))
734 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
))
735 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
736 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
737 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
738 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
739 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
740 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
741 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
742 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
743 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
744 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
745 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
))
746 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
747 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
))
748 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
749 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
750 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
751 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
752 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
754 # inject testing methods for each unary operation
755 for name
, unaryop
in _UNARYOPS
:
756 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
))
757 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
))
758 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
))
759 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
))
761 # inject testing methods for each inplace binary operation
762 for name
, ibinop
in _IBINOPS
:
763 setattr(cls
, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_ibinop
_invalid
_unknown
, op
=ibinop
))
764 setattr(cls
, test_ibinop_name('invalid_none'), partialmethod(_TestNumericField
._test
_ibinop
_invalid
_none
, op
=ibinop
))
765 setattr(cls
, test_ibinop_name('type_true'), partialmethod(_TestNumericField
._test
_ibinop
_type
_true
, op
=ibinop
))
766 setattr(cls
, test_ibinop_name('value_true'), partialmethod(_TestNumericField
._test
_ibinop
_value
_true
, op
=ibinop
))
767 setattr(cls
, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_int
, op
=ibinop
))
768 setattr(cls
, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_vint
, op
=ibinop
))
769 setattr(cls
, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_int
, op
=ibinop
))
770 setattr(cls
, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_vint
, op
=ibinop
))
771 setattr(cls
, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_int
, op
=ibinop
))
772 setattr(cls
, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_vint
, op
=ibinop
))
773 setattr(cls
, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_int
, op
=ibinop
))
774 setattr(cls
, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_vint
, op
=ibinop
))
775 setattr(cls
, test_ibinop_name('type_false'), partialmethod(_TestNumericField
._test
_ibinop
_type
_false
, op
=ibinop
))
776 setattr(cls
, test_ibinop_name('value_false'), partialmethod(_TestNumericField
._test
_ibinop
_value
_false
, op
=ibinop
))
777 setattr(cls
, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_int
, op
=ibinop
))
778 setattr(cls
, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_vint
, op
=ibinop
))
779 setattr(cls
, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_int
, op
=ibinop
))
780 setattr(cls
, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_vint
, op
=ibinop
))
781 setattr(cls
, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_float
, op
=ibinop
))
782 setattr(cls
, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_float
, op
=ibinop
))
783 setattr(cls
, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_vfloat
, op
=ibinop
))
784 setattr(cls
, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_vfloat
, op
=ibinop
))
785 setattr(cls
, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_float
, op
=ibinop
))
786 setattr(cls
, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_float
, op
=ibinop
))
787 setattr(cls
, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_vfloat
, op
=ibinop
))
788 setattr(cls
, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_vfloat
, op
=ibinop
))
789 setattr(cls
, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_float
, op
=ibinop
))
790 setattr(cls
, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_vfloat
, op
=ibinop
))
791 setattr(cls
, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_float
, op
=ibinop
))
792 setattr(cls
, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_vfloat
, op
=ibinop
))
795 class _TestIntegerFieldCommon(_TestNumericField
):
796 def test_assign_true(self
):
798 self
._def
.value
= raw
799 self
.assertEqual(self
._def
, raw
)
801 def test_assign_false(self
):
803 self
._def
.value
= raw
804 self
.assertEqual(self
._def
, raw
)
806 def test_assign_pos_int(self
):
808 self
._def
.value
= raw
809 self
.assertEqual(self
._def
, raw
)
811 def test_assign_neg_int(self
):
813 self
._def
.value
= raw
814 self
.assertEqual(self
._def
, raw
)
816 def test_assign_int_field(self
):
818 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
820 self
._def
.value
= field
821 self
.assertEqual(self
._def
, raw
)
823 def test_assign_float(self
):
825 self
._def
.value
= raw
826 self
.assertEqual(self
._def
, int(raw
))
828 def test_assign_invalid_type(self
):
829 with self
.assertRaises(TypeError):
830 self
._def
.value
= 'yes'
832 def test_assign_uint(self
):
833 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
834 field
= _create_field(self
._tc
, uint_fc
)
837 self
.assertEqual(field
, raw
)
839 def test_assign_big_uint(self
):
840 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
841 field
= _create_field(self
._tc
, uint_fc
)
842 # Larger than the IEEE 754 double-precision exact representation of
845 field
.value
= (2**53) + 1
846 self
.assertEqual(field
, raw
)
848 def test_assign_uint_invalid_neg(self
):
849 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
850 field
= _create_field(self
._tc
, uint_fc
)
852 with self
.assertRaises(ValueError):
855 def test_str_op(self
):
856 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
859 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
862 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
863 def _create_fc(self
, tc
):
864 return tc
.create_signed_integer_field_class(25)
867 self
._tc
= get_default_trace_class()
868 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
869 self
._field
.value
= 17
870 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
873 self
._def
_new
_value
= -101
876 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
877 def _create_fc(self
, tc
):
878 fc
= tc
.create_signed_enumeration_field_class(32)
879 fc
.map_range('something', 17)
880 fc
.map_range('speaker', 12, 16)
881 fc
.map_range('can', 18, 2540)
882 fc
.map_range('whole range', -(2 ** 31), (2 ** 31) - 1)
883 fc
.map_range('zip', -45, 1001)
887 self
._tc
= get_default_trace_class()
888 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
889 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
892 self
._def
_new
_value
= -101
894 def test_str_op(self
):
895 expected_string_found
= False
898 # Establish all permutations of the three expected matches since
899 # the order in which mappings are enumerated is not explicitly part of
901 for p
in itertools
.permutations(['whole range', 'something',
903 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
905 expected_string_found
= True
908 self
.assertTrue(expected_string_found
)
910 def test_labels(self
):
911 self
._field
.value
= 17
912 labels
= sorted(self
._field
.labels
)
913 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
916 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
917 def _create_fc(self
, tc
):
918 return tc
.create_real_field_class()
921 self
._tc
= get_default_trace_class()
922 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
923 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
924 self
._def
.value
= 52.7
925 self
._def
_value
= 52.7
926 self
._def
_new
_value
= -17.164857
928 def _test_invalid_op(self
, cb
):
929 with self
.assertRaises(TypeError):
932 def test_assign_true(self
):
933 self
._def
.value
= True
934 self
.assertTrue(self
._def
)
936 def test_assign_false(self
):
937 self
._def
.value
= False
938 self
.assertFalse(self
._def
)
940 def test_assign_pos_int(self
):
942 self
._def
.value
= raw
943 self
.assertEqual(self
._def
, float(raw
))
945 def test_assign_neg_int(self
):
947 self
._def
.value
= raw
948 self
.assertEqual(self
._def
, float(raw
))
950 def test_assign_int_field(self
):
951 int_fc
= self
._tc
.create_signed_integer_field_class(32)
952 int_field
= _create_field(self
._tc
, int_fc
)
954 int_field
.value
= raw
955 self
._def
.value
= int_field
956 self
.assertEqual(self
._def
, float(raw
))
958 def test_assign_float(self
):
960 self
._def
.value
= raw
961 self
.assertEqual(self
._def
, raw
)
963 def test_assign_float_field(self
):
964 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
967 self
._def
.value
= field
968 self
.assertEqual(self
._def
, raw
)
970 def test_assign_invalid_type(self
):
971 with self
.assertRaises(TypeError):
972 self
._def
.value
= 'yes'
974 def test_invalid_lshift(self
):
975 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
977 def test_invalid_rshift(self
):
978 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
980 def test_invalid_and(self
):
981 self
._test
_invalid
_op
(lambda: self
._def
& 23)
983 def test_invalid_or(self
):
984 self
._test
_invalid
_op
(lambda: self
._def |
23)
986 def test_invalid_xor(self
):
987 self
._test
_invalid
_op
(lambda: self
._def ^
23)
989 def test_invalid_invert(self
):
990 self
._test
_invalid
_op
(lambda: ~self
._def
)
992 def test_str_op(self
):
993 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
996 _inject_numeric_testing_methods(RealFieldTestCase
)
999 class StringFieldTestCase(unittest
.TestCase
):
1001 self
._tc
= get_default_trace_class()
1002 self
._def
_value
= 'Hello, World!'
1003 self
._def
= _create_string_field(self
._tc
)
1004 self
._def
.value
= self
._def
_value
1005 self
._def
_new
_value
= 'Yes!'
1007 def test_assign_int(self
):
1008 with self
.assertRaises(TypeError):
1009 self
._def
.value
= 283
1011 def test_assign_string_field(self
):
1012 field
= _create_string_field(self
._tc
)
1015 self
.assertEqual(field
, raw
)
1018 self
.assertEqual(self
._def
, self
._def
_value
)
1020 def test_not_eq(self
):
1021 self
.assertNotEqual(self
._def
, 23)
1023 def test_lt_vstring(self
):
1024 s1
= _create_string_field(self
._tc
)
1026 s2
= _create_string_field(self
._tc
)
1028 self
.assertLess(s1
, s2
)
1030 def test_lt_string(self
):
1031 s1
= _create_string_field(self
._tc
)
1033 self
.assertLess(s1
, 'bateau')
1035 def test_le_vstring(self
):
1036 s1
= _create_string_field(self
._tc
)
1038 s2
= _create_string_field(self
._tc
)
1040 self
.assertLessEqual(s1
, s2
)
1042 def test_le_string(self
):
1043 s1
= _create_string_field(self
._tc
)
1045 self
.assertLessEqual(s1
, 'bateau')
1047 def test_gt_vstring(self
):
1048 s1
= _create_string_field(self
._tc
)
1050 s2
= _create_string_field(self
._tc
)
1052 self
.assertGreater(s2
, s1
)
1054 def test_gt_string(self
):
1055 s1
= _create_string_field(self
._tc
)
1057 self
.assertGreater('bateau', s1
)
1059 def test_ge_vstring(self
):
1060 s1
= _create_string_field(self
._tc
)
1062 s2
= _create_string_field(self
._tc
)
1064 self
.assertGreaterEqual(s2
, s1
)
1066 def test_ge_string(self
):
1067 s1
= _create_string_field(self
._tc
)
1069 self
.assertGreaterEqual('bateau', s1
)
1071 def test_bool_op(self
):
1072 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1074 def test_str_op(self
):
1075 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1078 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1080 def test_getitem(self
):
1081 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1083 def test_append_str(self
):
1084 to_append
= 'meow meow meow'
1085 self
._def
+= to_append
1086 self
._def
_value
+= to_append
1087 self
.assertEqual(self
._def
, self
._def
_value
)
1089 def test_append_string_field(self
):
1090 field
= _create_string_field(self
._tc
)
1091 to_append
= 'meow meow meow'
1092 field
.value
= to_append
1094 self
._def
_value
+= to_append
1095 self
.assertEqual(self
._def
, self
._def
_value
)
1098 class _TestArrayFieldCommon
:
1099 def _modify_def(self
):
1102 def test_bool_op_true(self
):
1103 self
.assertTrue(self
._def
)
1106 self
.assertEqual(len(self
._def
), 3)
1108 def test_length(self
):
1109 self
.assertEqual(self
._def
.length
, 3)
1111 def test_getitem(self
):
1112 field
= self
._def
[1]
1113 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1114 self
.assertEqual(field
, 1847)
1117 field
= _create_int_array_field(self
._tc
, 3)
1121 self
.assertEqual(self
._def
, field
)
1123 def test_eq_invalid_type(self
):
1124 self
.assertNotEqual(self
._def
, 23)
1126 def test_eq_diff_len(self
):
1127 field
= _create_int_array_field(self
._tc
, 2)
1130 self
.assertNotEqual(self
._def
, field
)
1132 def test_eq_diff_content_same_len(self
):
1133 field
= _create_int_array_field(self
._tc
, 3)
1137 self
.assertNotEqual(self
._def
, field
)
1139 def test_setitem(self
):
1141 self
.assertEqual(self
._def
[2], 24)
1143 def test_setitem_int_field(self
):
1144 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1145 int_field
= _create_field(self
._tc
, int_fc
)
1146 int_field
.value
= 19487
1147 self
._def
[1] = int_field
1148 self
.assertEqual(self
._def
[1], 19487)
1150 def test_setitem_non_basic_field(self
):
1151 array_field
= _create_struct_array_field(self
._tc
, 2)
1152 with self
.assertRaises(TypeError):
1155 def test_setitem_none(self
):
1156 with self
.assertRaises(TypeError):
1159 def test_setitem_index_wrong_type(self
):
1160 with self
.assertRaises(TypeError):
1161 self
._def
['yes'] = 23
1163 def test_setitem_index_neg(self
):
1164 with self
.assertRaises(IndexError):
1167 def test_setitem_index_out_of_range(self
):
1168 with self
.assertRaises(IndexError):
1169 self
._def
[len(self
._def
)] = 134679
1171 def test_iter(self
):
1172 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1173 self
.assertEqual(field
, value
)
1175 def test_value_int_field(self
):
1176 values
= [45646, 145, 12145]
1177 self
._def
.value
= values
1178 self
.assertEqual(values
, self
._def
)
1180 def test_value_check_sequence(self
):
1182 with self
.assertRaises(TypeError):
1183 self
._def
.value
= values
1185 def test_value_wrong_type_in_sequence(self
):
1186 values
= [32, 'hello', 11]
1187 with self
.assertRaises(TypeError):
1188 self
._def
.value
= values
1190 def test_value_complex_type(self
):
1191 struct_fc
= self
._tc
.create_structure_field_class()
1192 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1193 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1194 str_fc
= self
._tc
.create_string_field_class()
1195 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1196 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1197 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1198 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1199 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1203 'a_string': 'hello',
1208 'a_string': 'goodbye',
1213 'a_string': 'or not',
1218 array
= stream
.create_packet().context_field
['array_field']
1219 array
.value
= values
1220 self
.assertEqual(values
, array
)
1221 values
[0]['an_int'] = 'a string'
1222 with self
.assertRaises(TypeError):
1223 array
.value
= values
1225 def test_str_op(self
):
1227 expected_string
= '[{}]'.format(', '.join(
1228 [repr(v
) for v
in self
._def
_value
]))
1229 self
.assertEqual(expected_string
, s
)
1232 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1234 self
._tc
= get_default_trace_class()
1235 self
._def
= _create_int_array_field(self
._tc
, 3)
1238 self
._def
[2] = 1948754
1239 self
._def
_value
= [45, 1847, 1948754]
1241 def test_value_wrong_len(self
):
1243 with self
.assertRaises(ValueError):
1244 self
._def
.value
= values
1247 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1249 self
._tc
= get_default_trace_class()
1250 self
._def
= _create_dynamic_array(self
._tc
)
1253 self
._def
[2] = 1948754
1254 self
._def
_value
= [45, 1847, 1948754]
1256 def test_value_resize(self
):
1257 new_values
= [1, 2, 3, 4]
1258 self
._def
.value
= new_values
1259 self
.assertCountEqual(self
._def
, new_values
)
1261 def test_set_length(self
):
1262 self
._def
.length
= 4
1264 self
.assertEqual(len(self
._def
), 4)
1266 def test_set_invalid_length(self
):
1267 with self
.assertRaises(TypeError):
1268 self
._def
.length
= 'cheval'
1271 class StructureFieldTestCase(unittest
.TestCase
):
1272 def _create_fc(self
, tc
):
1273 fc
= tc
.create_structure_field_class()
1274 fc
.append_member('A', self
._fc
0_fn
())
1275 fc
.append_member('B', self
._fc
1_fn
())
1276 fc
.append_member('C', self
._fc
2_fn
())
1277 fc
.append_member('D', self
._fc
3_fn
())
1278 fc
.append_member('E', self
._fc
4_fn
())
1279 fc5
= self
._fc
5_fn
()
1280 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1281 fc
.append_member('F', fc5
)
1285 self
._tc
= get_default_trace_class()
1286 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1287 self
._fc
1_fn
= self
._tc
.create_string_field_class
1288 self
._fc
2_fn
= self
._tc
.create_real_field_class
1289 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1290 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1291 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1292 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1294 self
._fc
= self
._create
_fc
(self
._tc
)
1295 self
._def
= _create_field(self
._tc
, self
._fc
)
1296 self
._def
['A'] = -1872
1297 self
._def
['B'] = 'salut'
1298 self
._def
['C'] = 17.5
1299 self
._def
['D'] = 16497
1301 self
._def
['F'] = {'F_1': 52}
1311 def _modify_def(self
):
1312 self
._def
['B'] = 'hola'
1314 def test_bool_op_true(self
):
1315 self
.assertTrue(self
._def
)
1317 def test_bool_op_false(self
):
1318 field
= self
._def
['E']
1319 self
.assertFalse(field
)
1322 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1324 def test_getitem(self
):
1325 field
= self
._def
['A']
1326 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1327 self
.assertEqual(field
, -1872)
1329 def test_member_at_index_out_of_bounds_after(self
):
1330 with self
.assertRaises(IndexError):
1331 self
._def
.member_at_index(len(self
._def
_value
))
1334 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
, ))
1336 field
['B'] = 'salut'
1340 field
['F'] = {'F_1': 52}
1341 self
.assertEqual(self
._def
, field
)
1343 def test_eq_invalid_type(self
):
1344 self
.assertNotEqual(self
._def
, 23)
1346 def test_eq_diff_len(self
):
1347 fc
= self
._tc
.create_structure_field_class()
1348 fc
.append_member('A', self
._fc
0_fn
())
1349 fc
.append_member('B', self
._fc
1_fn
())
1350 fc
.append_member('C', self
._fc
2_fn
())
1352 field
= _create_field(self
._tc
, fc
)
1354 field
['B'] = 'salut'
1356 self
.assertNotEqual(self
._def
, field
)
1358 def test_eq_diff_keys(self
):
1359 fc
= self
._tc
.create_structure_field_class()
1360 fc
.append_member('U', self
._fc
0_fn
())
1361 fc
.append_member('V', self
._fc
1_fn
())
1362 fc
.append_member('W', self
._fc
2_fn
())
1363 fc
.append_member('X', self
._fc
3_fn
())
1364 fc
.append_member('Y', self
._fc
4_fn
())
1365 fc
.append_member('Z', self
._fc
5_fn
())
1366 field
= _create_field(self
._tc
, fc
)
1368 field
['V'] = "gerry"
1373 self
.assertNotEqual(self
._def
, field
)
1375 def test_eq_diff_content_same_len(self
):
1376 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1378 field
['B'] = 'salut'
1382 field
['F'] = {'F_1': 0}
1383 self
.assertNotEqual(self
._def
, field
)
1385 def test_eq_same_content_diff_keys(self
):
1386 fc
= self
._tc
.create_structure_field_class()
1387 fc
.append_member('A', self
._fc
0_fn
())
1388 fc
.append_member('B', self
._fc
1_fn
())
1389 fc
.append_member('E', self
._fc
2_fn
())
1390 fc
.append_member('D', self
._fc
3_fn
())
1391 fc
.append_member('C', self
._fc
4_fn
())
1392 fc
.append_member('F', self
._fc
5_fn
())
1393 field
= _create_field(self
._tc
, fc
)
1395 field
['B'] = 'salut'
1400 self
.assertNotEqual(self
._def
, field
)
1402 def test_setitem(self
):
1403 self
._def
['C'] = -18.47
1404 self
.assertEqual(self
._def
['C'], -18.47)
1406 def test_setitem_int_field(self
):
1407 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1408 int_field
= _create_field(self
._tc
, int_fc
)
1409 int_field
.value
= 19487
1410 self
._def
['D'] = int_field
1411 self
.assertEqual(self
._def
['D'], 19487)
1413 def test_setitem_non_basic_field(self
):
1414 elem_fc
= self
._tc
.create_structure_field_class()
1415 struct_fc
= self
._tc
.create_structure_field_class()
1416 struct_fc
.append_member('A', elem_fc
)
1417 struct_field
= _create_field(self
._tc
, struct_fc
)
1419 # Will fail on access to .items() of the value
1420 with self
.assertRaises(AttributeError):
1421 struct_field
['A'] = 23
1423 def test_setitem_none(self
):
1424 with self
.assertRaises(TypeError):
1425 self
._def
['C'] = None
1427 def test_setitem_key_wrong_type(self
):
1428 with self
.assertRaises(TypeError):
1431 def test_setitem_wrong_key(self
):
1432 with self
.assertRaises(KeyError):
1433 self
._def
['hi'] = 134679
1435 def test_member_at_index(self
):
1436 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1438 def test_iter(self
):
1448 for vkey
, vval
in self
._def
.items():
1449 val
= orig_values
[vkey
]
1450 self
.assertEqual(vval
, val
)
1452 def test_value(self
):
1461 self
.assertEqual(self
._def
, orig_values
)
1463 def test_set_value(self
):
1464 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1465 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1466 str_fc
= self
._tc
.create_string_field_class()
1467 struct_fc
= self
._tc
.create_structure_field_class()
1468 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1469 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1470 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1473 'a_string': 'hello',
1477 struct
= _create_field(self
._tc
, struct_fc
)
1478 struct
.value
= values
1479 self
.assertEqual(values
, struct
)
1481 bad_type_values
= copy
.deepcopy(values
)
1482 bad_type_values
['an_int'] = 'a string'
1483 with self
.assertRaises(TypeError):
1484 struct
.value
= bad_type_values
1486 unknown_key_values
= copy
.deepcopy(values
)
1487 unknown_key_values
['unknown_key'] = 16546
1488 with self
.assertRaises(KeyError):
1489 struct
.value
= unknown_key_values
1491 def test_str_op(self
):
1492 expected_string_found
= False
1494 # Establish all permutations of the three expected matches since
1495 # the order in which mappings are enumerated is not explicitly part of
1497 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1498 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1499 candidate
= '{{{}}}'.format(', '.join(items
))
1501 expected_string_found
= True
1504 self
.assertTrue(expected_string_found
)
1507 class VariantFieldTestCase(unittest
.TestCase
):
1508 def _create_fc(self
, tc
):
1509 selector_fc
= tc
.create_signed_enumeration_field_class(field_value_range
=32)
1510 selector_fc
.map_range('corner', 23)
1511 selector_fc
.map_range('zoom', 17, 20)
1512 selector_fc
.map_range('mellotron', 1001)
1513 selector_fc
.map_range('giorgio', 2000, 3000)
1515 ft0
= tc
.create_signed_integer_field_class(32)
1516 ft1
= tc
.create_string_field_class()
1517 ft2
= tc
.create_real_field_class()
1518 ft3
= tc
.create_signed_integer_field_class(17)
1520 fc
= tc
.create_variant_field_class()
1521 fc
.append_option('corner', ft0
)
1522 fc
.append_option('zoom', ft1
)
1523 fc
.append_option('mellotron', ft2
)
1524 fc
.append_option('giorgio', ft3
)
1525 fc
.selector_field_class
= selector_fc
1527 top_fc
= tc
.create_structure_field_class()
1528 top_fc
.append_member('selector_field', selector_fc
)
1529 top_fc
.append_member('variant_field', fc
)
1533 self
._tc
= get_default_trace_class()
1534 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1535 self
._def
= fld
['variant_field']
1537 def test_bool_op(self
):
1538 self
._def
.selected_option_index
= 2
1539 self
._def
.value
= -17.34
1540 with self
.assertRaises(NotImplementedError):
1543 def test_selected_option_index(self
):
1544 self
._def
.selected_option_index
= 2
1545 self
.assertEqual(self
._def
.selected_option_index
, 2)
1547 def test_selected_option(self
):
1548 self
._def
.selected_option_index
= 2
1549 self
._def
.value
= -17.34
1550 self
.assertEqual(self
._def
.selected_option
, -17.34)
1552 self
._def
.selected_option_index
= 3
1553 self
._def
.value
= 1921
1554 self
.assertEqual(self
._def
.selected_option
, 1921)
1557 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1558 field
= field
['variant_field']
1559 field
.selected_option_index
= 0
1561 self
._def
.selected_option_index
= 0
1562 self
._def
.value
= 1774
1563 self
.assertEqual(self
._def
, field
)
1565 def test_eq_invalid_type(self
):
1566 self
._def
.selected_option_index
= 1
1567 self
._def
.value
= 'gerry'
1568 self
.assertNotEqual(self
._def
, 23)
1570 def test_str_op_int(self
):
1571 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1572 field
= field
['variant_field']
1573 field
.selected_option_index
= 0
1575 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1576 other_field
= other_field
['variant_field']
1577 other_field
.selected_option_index
= 0
1578 other_field
.value
= 1774
1579 self
.assertEqual(str(field
), str(other_field
))
1581 def test_str_op_str(self
):
1582 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1583 field
= field
['variant_field']
1584 field
.selected_option_index
= 1
1585 field
.value
= 'un beau grand bateau'
1586 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1587 other_field
= other_field
['variant_field']
1588 other_field
.selected_option_index
= 1
1589 other_field
.value
= 'un beau grand bateau'
1590 self
.assertEqual(str(field
), str(other_field
))
1592 def test_str_op_float(self
):
1593 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1594 field
= field
['variant_field']
1595 field
.selected_option_index
= 2
1596 field
.value
= 14.4245
1597 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1598 other_field
= other_field
['variant_field']
1599 other_field
.selected_option_index
= 2
1600 other_field
.value
= 14.4245
1601 self
.assertEqual(str(field
), str(other_field
))