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_uint_invalid_neg(self
):
840 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
841 field
= _create_field(self
._tc
, uint_fc
)
843 with self
.assertRaises(ValueError):
846 def test_str_op(self
):
847 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
850 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
853 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
854 def _create_fc(self
, tc
):
855 return tc
.create_signed_integer_field_class(25)
858 self
._tc
= get_default_trace_class()
859 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
860 self
._field
.value
= 17
861 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
864 self
._def
_new
_value
= -101
867 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
868 def _create_fc(self
, tc
):
869 fc
= tc
.create_signed_enumeration_field_class(32)
870 fc
.map_range('something', 17)
871 fc
.map_range('speaker', 12, 16)
872 fc
.map_range('can', 18, 2540)
873 fc
.map_range('whole range', -(2 ** 31), (2 ** 31) - 1)
874 fc
.map_range('zip', -45, 1001)
878 self
._tc
= get_default_trace_class()
879 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
880 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
883 self
._def
_new
_value
= -101
885 def test_str_op(self
):
886 expected_string_found
= False
889 # Establish all permutations of the three expected matches since
890 # the order in which mappings are enumerated is not explicitly part of
892 for p
in itertools
.permutations(['whole range', 'something',
894 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
896 expected_string_found
= True
899 self
.assertTrue(expected_string_found
)
901 def test_labels(self
):
902 self
._field
.value
= 17
903 labels
= sorted(self
._field
.labels
)
904 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
907 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
908 def _create_fc(self
, tc
):
909 return tc
.create_real_field_class()
912 self
._tc
= get_default_trace_class()
913 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
914 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
915 self
._def
.value
= 52.7
916 self
._def
_value
= 52.7
917 self
._def
_new
_value
= -17.164857
919 def _test_invalid_op(self
, cb
):
920 with self
.assertRaises(TypeError):
923 def test_assign_true(self
):
924 self
._def
.value
= True
925 self
.assertTrue(self
._def
)
927 def test_assign_false(self
):
928 self
._def
.value
= False
929 self
.assertFalse(self
._def
)
931 def test_assign_pos_int(self
):
933 self
._def
.value
= raw
934 self
.assertEqual(self
._def
, float(raw
))
936 def test_assign_neg_int(self
):
938 self
._def
.value
= raw
939 self
.assertEqual(self
._def
, float(raw
))
941 def test_assign_int_field(self
):
942 int_fc
= self
._tc
.create_signed_integer_field_class(32)
943 int_field
= _create_field(self
._tc
, int_fc
)
945 int_field
.value
= raw
946 self
._def
.value
= int_field
947 self
.assertEqual(self
._def
, float(raw
))
949 def test_assign_float(self
):
951 self
._def
.value
= raw
952 self
.assertEqual(self
._def
, raw
)
954 def test_assign_float_field(self
):
955 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
958 self
._def
.value
= field
959 self
.assertEqual(self
._def
, raw
)
961 def test_assign_invalid_type(self
):
962 with self
.assertRaises(TypeError):
963 self
._def
.value
= 'yes'
965 def test_invalid_lshift(self
):
966 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
968 def test_invalid_rshift(self
):
969 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
971 def test_invalid_and(self
):
972 self
._test
_invalid
_op
(lambda: self
._def
& 23)
974 def test_invalid_or(self
):
975 self
._test
_invalid
_op
(lambda: self
._def |
23)
977 def test_invalid_xor(self
):
978 self
._test
_invalid
_op
(lambda: self
._def ^
23)
980 def test_invalid_invert(self
):
981 self
._test
_invalid
_op
(lambda: ~self
._def
)
983 def test_str_op(self
):
984 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
987 _inject_numeric_testing_methods(RealFieldTestCase
)
990 class StringFieldTestCase(unittest
.TestCase
):
992 self
._tc
= get_default_trace_class()
993 self
._def
_value
= 'Hello, World!'
994 self
._def
= _create_string_field(self
._tc
)
995 self
._def
.value
= self
._def
_value
996 self
._def
_new
_value
= 'Yes!'
998 def test_assign_int(self
):
999 with self
.assertRaises(TypeError):
1000 self
._def
.value
= 283
1002 def test_assign_string_field(self
):
1003 field
= _create_string_field(self
._tc
)
1006 self
.assertEqual(field
, raw
)
1009 self
.assertEqual(self
._def
, self
._def
_value
)
1011 def test_not_eq(self
):
1012 self
.assertNotEqual(self
._def
, 23)
1014 def test_lt_vstring(self
):
1015 s1
= _create_string_field(self
._tc
)
1017 s2
= _create_string_field(self
._tc
)
1019 self
.assertLess(s1
, s2
)
1021 def test_lt_string(self
):
1022 s1
= _create_string_field(self
._tc
)
1024 self
.assertLess(s1
, 'bateau')
1026 def test_le_vstring(self
):
1027 s1
= _create_string_field(self
._tc
)
1029 s2
= _create_string_field(self
._tc
)
1031 self
.assertLessEqual(s1
, s2
)
1033 def test_le_string(self
):
1034 s1
= _create_string_field(self
._tc
)
1036 self
.assertLessEqual(s1
, 'bateau')
1038 def test_gt_vstring(self
):
1039 s1
= _create_string_field(self
._tc
)
1041 s2
= _create_string_field(self
._tc
)
1043 self
.assertGreater(s2
, s1
)
1045 def test_gt_string(self
):
1046 s1
= _create_string_field(self
._tc
)
1048 self
.assertGreater('bateau', s1
)
1050 def test_ge_vstring(self
):
1051 s1
= _create_string_field(self
._tc
)
1053 s2
= _create_string_field(self
._tc
)
1055 self
.assertGreaterEqual(s2
, s1
)
1057 def test_ge_string(self
):
1058 s1
= _create_string_field(self
._tc
)
1060 self
.assertGreaterEqual('bateau', s1
)
1062 def test_bool_op(self
):
1063 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1065 def test_str_op(self
):
1066 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1069 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1071 def test_getitem(self
):
1072 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1074 def test_append_str(self
):
1075 to_append
= 'meow meow meow'
1076 self
._def
+= to_append
1077 self
._def
_value
+= to_append
1078 self
.assertEqual(self
._def
, self
._def
_value
)
1080 def test_append_string_field(self
):
1081 field
= _create_string_field(self
._tc
)
1082 to_append
= 'meow meow meow'
1083 field
.value
= to_append
1085 self
._def
_value
+= to_append
1086 self
.assertEqual(self
._def
, self
._def
_value
)
1089 class _TestArrayFieldCommon
:
1090 def _modify_def(self
):
1093 def test_bool_op_true(self
):
1094 self
.assertTrue(self
._def
)
1097 self
.assertEqual(len(self
._def
), 3)
1099 def test_length(self
):
1100 self
.assertEqual(self
._def
.length
, 3)
1102 def test_getitem(self
):
1103 field
= self
._def
[1]
1104 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1105 self
.assertEqual(field
, 1847)
1108 field
= _create_int_array_field(self
._tc
, 3)
1112 self
.assertEqual(self
._def
, field
)
1114 def test_eq_invalid_type(self
):
1115 self
.assertNotEqual(self
._def
, 23)
1117 def test_eq_diff_len(self
):
1118 field
= _create_int_array_field(self
._tc
, 2)
1121 self
.assertNotEqual(self
._def
, field
)
1123 def test_eq_diff_content_same_len(self
):
1124 field
= _create_int_array_field(self
._tc
, 3)
1128 self
.assertNotEqual(self
._def
, field
)
1130 def test_setitem(self
):
1132 self
.assertEqual(self
._def
[2], 24)
1134 def test_setitem_int_field(self
):
1135 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1136 int_field
= _create_field(self
._tc
, int_fc
)
1137 int_field
.value
= 19487
1138 self
._def
[1] = int_field
1139 self
.assertEqual(self
._def
[1], 19487)
1141 def test_setitem_non_basic_field(self
):
1142 array_field
= _create_struct_array_field(self
._tc
, 2)
1143 with self
.assertRaises(TypeError):
1146 def test_setitem_none(self
):
1147 with self
.assertRaises(TypeError):
1150 def test_setitem_index_wrong_type(self
):
1151 with self
.assertRaises(TypeError):
1152 self
._def
['yes'] = 23
1154 def test_setitem_index_neg(self
):
1155 with self
.assertRaises(IndexError):
1158 def test_setitem_index_out_of_range(self
):
1159 with self
.assertRaises(IndexError):
1160 self
._def
[len(self
._def
)] = 134679
1162 def test_iter(self
):
1163 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1164 self
.assertEqual(field
, value
)
1166 def test_value_int_field(self
):
1167 values
= [45646, 145, 12145]
1168 self
._def
.value
= values
1169 self
.assertEqual(values
, self
._def
)
1171 def test_value_check_sequence(self
):
1173 with self
.assertRaises(TypeError):
1174 self
._def
.value
= values
1176 def test_value_wrong_type_in_sequence(self
):
1177 values
= [32, 'hello', 11]
1178 with self
.assertRaises(TypeError):
1179 self
._def
.value
= values
1181 def test_value_complex_type(self
):
1182 struct_fc
= self
._tc
.create_structure_field_class()
1183 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1184 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1185 str_fc
= self
._tc
.create_string_field_class()
1186 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1187 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1188 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1189 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1190 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1194 'a_string': 'hello',
1199 'a_string': 'goodbye',
1204 'a_string': 'or not',
1209 array
= stream
.create_packet().context_field
['array_field']
1210 array
.value
= values
1211 self
.assertEqual(values
, array
)
1212 values
[0]['an_int'] = 'a string'
1213 with self
.assertRaises(TypeError):
1214 array
.value
= values
1216 def test_str_op(self
):
1218 expected_string
= '[{}]'.format(', '.join(
1219 [repr(v
) for v
in self
._def
_value
]))
1220 self
.assertEqual(expected_string
, s
)
1223 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1225 self
._tc
= get_default_trace_class()
1226 self
._def
= _create_int_array_field(self
._tc
, 3)
1229 self
._def
[2] = 1948754
1230 self
._def
_value
= [45, 1847, 1948754]
1232 def test_value_wrong_len(self
):
1234 with self
.assertRaises(ValueError):
1235 self
._def
.value
= values
1238 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1240 self
._tc
= get_default_trace_class()
1241 self
._def
= _create_dynamic_array(self
._tc
)
1244 self
._def
[2] = 1948754
1245 self
._def
_value
= [45, 1847, 1948754]
1247 def test_value_resize(self
):
1248 new_values
= [1, 2, 3, 4]
1249 self
._def
.value
= new_values
1250 self
.assertCountEqual(self
._def
, new_values
)
1252 def test_set_length(self
):
1253 self
._def
.length
= 4
1255 self
.assertEqual(len(self
._def
), 4)
1257 def test_set_invalid_length(self
):
1258 with self
.assertRaises(TypeError):
1259 self
._def
.length
= 'cheval'
1262 class StructureFieldTestCase(unittest
.TestCase
):
1263 def _create_fc(self
, tc
):
1264 fc
= tc
.create_structure_field_class()
1265 fc
.append_member('A', self
._fc
0_fn
())
1266 fc
.append_member('B', self
._fc
1_fn
())
1267 fc
.append_member('C', self
._fc
2_fn
())
1268 fc
.append_member('D', self
._fc
3_fn
())
1269 fc
.append_member('E', self
._fc
4_fn
())
1270 fc5
= self
._fc
5_fn
()
1271 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1272 fc
.append_member('F', fc5
)
1276 self
._tc
= get_default_trace_class()
1277 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1278 self
._fc
1_fn
= self
._tc
.create_string_field_class
1279 self
._fc
2_fn
= self
._tc
.create_real_field_class
1280 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1281 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1282 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1283 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1285 self
._fc
= self
._create
_fc
(self
._tc
)
1286 self
._def
= _create_field(self
._tc
, self
._fc
)
1287 self
._def
['A'] = -1872
1288 self
._def
['B'] = 'salut'
1289 self
._def
['C'] = 17.5
1290 self
._def
['D'] = 16497
1292 self
._def
['F'] = {'F_1': 52}
1302 def _modify_def(self
):
1303 self
._def
['B'] = 'hola'
1305 def test_bool_op_true(self
):
1306 self
.assertTrue(self
._def
)
1308 def test_bool_op_false(self
):
1309 field
= self
._def
['E']
1310 self
.assertFalse(field
)
1313 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1315 def test_getitem(self
):
1316 field
= self
._def
['A']
1317 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1318 self
.assertEqual(field
, -1872)
1320 def test_member_at_index_out_of_bounds_after(self
):
1321 with self
.assertRaises(IndexError):
1322 self
._def
.member_at_index(len(self
._def
_value
))
1325 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
, ))
1327 field
['B'] = 'salut'
1331 field
['F'] = {'F_1': 52}
1332 self
.assertEqual(self
._def
, field
)
1334 def test_eq_invalid_type(self
):
1335 self
.assertNotEqual(self
._def
, 23)
1337 def test_eq_diff_len(self
):
1338 fc
= self
._tc
.create_structure_field_class()
1339 fc
.append_member('A', self
._fc
0_fn
())
1340 fc
.append_member('B', self
._fc
1_fn
())
1341 fc
.append_member('C', self
._fc
2_fn
())
1343 field
= _create_field(self
._tc
, fc
)
1345 field
['B'] = 'salut'
1347 self
.assertNotEqual(self
._def
, field
)
1349 def test_eq_diff_keys(self
):
1350 fc
= self
._tc
.create_structure_field_class()
1351 fc
.append_member('U', self
._fc
0_fn
())
1352 fc
.append_member('V', self
._fc
1_fn
())
1353 fc
.append_member('W', self
._fc
2_fn
())
1354 fc
.append_member('X', self
._fc
3_fn
())
1355 fc
.append_member('Y', self
._fc
4_fn
())
1356 fc
.append_member('Z', self
._fc
5_fn
())
1357 field
= _create_field(self
._tc
, fc
)
1359 field
['V'] = "gerry"
1364 self
.assertNotEqual(self
._def
, field
)
1366 def test_eq_diff_content_same_len(self
):
1367 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1369 field
['B'] = 'salut'
1373 field
['F'] = {'F_1': 0}
1374 self
.assertNotEqual(self
._def
, field
)
1376 def test_eq_same_content_diff_keys(self
):
1377 fc
= self
._tc
.create_structure_field_class()
1378 fc
.append_member('A', self
._fc
0_fn
())
1379 fc
.append_member('B', self
._fc
1_fn
())
1380 fc
.append_member('E', self
._fc
2_fn
())
1381 fc
.append_member('D', self
._fc
3_fn
())
1382 fc
.append_member('C', self
._fc
4_fn
())
1383 fc
.append_member('F', self
._fc
5_fn
())
1384 field
= _create_field(self
._tc
, fc
)
1386 field
['B'] = 'salut'
1391 self
.assertNotEqual(self
._def
, field
)
1393 def test_setitem(self
):
1394 self
._def
['C'] = -18.47
1395 self
.assertEqual(self
._def
['C'], -18.47)
1397 def test_setitem_int_field(self
):
1398 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1399 int_field
= _create_field(self
._tc
, int_fc
)
1400 int_field
.value
= 19487
1401 self
._def
['D'] = int_field
1402 self
.assertEqual(self
._def
['D'], 19487)
1404 def test_setitem_non_basic_field(self
):
1405 elem_fc
= self
._tc
.create_structure_field_class()
1406 struct_fc
= self
._tc
.create_structure_field_class()
1407 struct_fc
.append_member('A', elem_fc
)
1408 struct_field
= _create_field(self
._tc
, struct_fc
)
1410 # Will fail on access to .items() of the value
1411 with self
.assertRaises(AttributeError):
1412 struct_field
['A'] = 23
1414 def test_setitem_none(self
):
1415 with self
.assertRaises(TypeError):
1416 self
._def
['C'] = None
1418 def test_setitem_key_wrong_type(self
):
1419 with self
.assertRaises(TypeError):
1422 def test_setitem_wrong_key(self
):
1423 with self
.assertRaises(KeyError):
1424 self
._def
['hi'] = 134679
1426 def test_member_at_index(self
):
1427 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1429 def test_iter(self
):
1439 for vkey
, vval
in self
._def
.items():
1440 val
= orig_values
[vkey
]
1441 self
.assertEqual(vval
, val
)
1443 def test_value(self
):
1452 self
.assertEqual(self
._def
, orig_values
)
1454 def test_set_value(self
):
1455 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1456 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1457 str_fc
= self
._tc
.create_string_field_class()
1458 struct_fc
= self
._tc
.create_structure_field_class()
1459 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1460 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1461 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1464 'a_string': 'hello',
1468 struct
= _create_field(self
._tc
, struct_fc
)
1469 struct
.value
= values
1470 self
.assertEqual(values
, struct
)
1472 bad_type_values
= copy
.deepcopy(values
)
1473 bad_type_values
['an_int'] = 'a string'
1474 with self
.assertRaises(TypeError):
1475 struct
.value
= bad_type_values
1477 unknown_key_values
= copy
.deepcopy(values
)
1478 unknown_key_values
['unknown_key'] = 16546
1479 with self
.assertRaises(KeyError):
1480 struct
.value
= unknown_key_values
1482 def test_str_op(self
):
1483 expected_string_found
= False
1485 # Establish all permutations of the three expected matches since
1486 # the order in which mappings are enumerated is not explicitly part of
1488 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1489 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1490 candidate
= '{{{}}}'.format(', '.join(items
))
1492 expected_string_found
= True
1495 self
.assertTrue(expected_string_found
)
1498 class VariantFieldTestCase(unittest
.TestCase
):
1499 def _create_fc(self
, tc
):
1500 selector_fc
= tc
.create_signed_enumeration_field_class(field_value_range
=32)
1501 selector_fc
.map_range('corner', 23)
1502 selector_fc
.map_range('zoom', 17, 20)
1503 selector_fc
.map_range('mellotron', 1001)
1504 selector_fc
.map_range('giorgio', 2000, 3000)
1506 ft0
= tc
.create_signed_integer_field_class(32)
1507 ft1
= tc
.create_string_field_class()
1508 ft2
= tc
.create_real_field_class()
1509 ft3
= tc
.create_signed_integer_field_class(17)
1511 fc
= tc
.create_variant_field_class()
1512 fc
.append_option('corner', ft0
)
1513 fc
.append_option('zoom', ft1
)
1514 fc
.append_option('mellotron', ft2
)
1515 fc
.append_option('giorgio', ft3
)
1516 fc
.selector_field_class
= selector_fc
1518 top_fc
= tc
.create_structure_field_class()
1519 top_fc
.append_member('selector_field', selector_fc
)
1520 top_fc
.append_member('variant_field', fc
)
1524 self
._tc
= get_default_trace_class()
1525 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1526 self
._def
= fld
['variant_field']
1528 def test_bool_op(self
):
1529 self
._def
.selected_option_index
= 2
1530 self
._def
.value
= -17.34
1531 with self
.assertRaises(NotImplementedError):
1534 def test_selected_option_index(self
):
1535 self
._def
.selected_option_index
= 2
1536 self
.assertEqual(self
._def
.selected_option_index
, 2)
1538 def test_selected_option(self
):
1539 self
._def
.selected_option_index
= 2
1540 self
._def
.value
= -17.34
1541 self
.assertEqual(self
._def
.selected_option
, -17.34)
1543 self
._def
.selected_option_index
= 3
1544 self
._def
.value
= 1921
1545 self
.assertEqual(self
._def
.selected_option
, 1921)
1548 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1549 field
= field
['variant_field']
1550 field
.selected_option_index
= 0
1552 self
._def
.selected_option_index
= 0
1553 self
._def
.value
= 1774
1554 self
.assertEqual(self
._def
, field
)
1556 def test_eq_invalid_type(self
):
1557 self
._def
.selected_option_index
= 1
1558 self
._def
.value
= 'gerry'
1559 self
.assertNotEqual(self
._def
, 23)
1561 def test_str_op_int(self
):
1562 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1563 field
= field
['variant_field']
1564 field
.selected_option_index
= 0
1566 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1567 other_field
= other_field
['variant_field']
1568 other_field
.selected_option_index
= 0
1569 other_field
.value
= 1774
1570 self
.assertEqual(str(field
), str(other_field
))
1572 def test_str_op_str(self
):
1573 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1574 field
= field
['variant_field']
1575 field
.selected_option_index
= 1
1576 field
.value
= 'un beau grand bateau'
1577 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1578 other_field
= other_field
['variant_field']
1579 other_field
.selected_option_index
= 1
1580 other_field
.value
= 'un beau grand bateau'
1581 self
.assertEqual(str(field
), str(other_field
))
1583 def test_str_op_float(self
):
1584 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1585 field
= field
['variant_field']
1586 field
.selected_option_index
= 2
1587 field
.value
= 14.4245
1588 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1589 other_field
= other_field
['variant_field']
1590 other_field
.selected_option_index
= 2
1591 other_field
.value
= 14.4245
1592 self
.assertEqual(str(field
), str(other_field
))