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_binop_rhs_false(self
, test_cb
, op
):
250 def _test_binop_rhs_true(self
, test_cb
, op
):
253 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
256 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
259 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
262 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
263 test_cb(op
, bt2
.create_value(2))
265 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
266 test_cb(op
, bt2
.create_value(-23))
268 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
269 test_cb(op
, bt2
.create_value(0))
271 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
274 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
277 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
280 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
281 test_cb(op
, bt2
.create_value(2.2))
283 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
284 test_cb(op
, bt2
.create_value(-23.4))
286 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
287 test_cb(op
, bt2
.create_value(0.0))
289 def _test_binop_type_false(self
, op
):
290 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
292 def _test_binop_type_true(self
, op
):
293 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
295 def _test_binop_type_pos_int(self
, op
):
296 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
298 def _test_binop_type_neg_int(self
, op
):
299 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
301 def _test_binop_type_zero_int(self
, op
):
302 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
304 def _test_binop_type_pos_vint(self
, op
):
305 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
307 def _test_binop_type_neg_vint(self
, op
):
308 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
310 def _test_binop_type_zero_vint(self
, op
):
311 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
313 def _test_binop_type_pos_float(self
, op
):
314 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
316 def _test_binop_type_neg_float(self
, op
):
317 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
319 def _test_binop_type_zero_float(self
, op
):
320 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
322 def _test_binop_type_pos_vfloat(self
, op
):
323 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
325 def _test_binop_type_neg_vfloat(self
, op
):
326 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
328 def _test_binop_type_zero_vfloat(self
, op
):
329 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
331 def _test_binop_value_false(self
, op
):
332 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
334 def _test_binop_value_true(self
, op
):
335 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
337 def _test_binop_value_pos_int(self
, op
):
338 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
340 def _test_binop_value_neg_int(self
, op
):
341 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
343 def _test_binop_value_zero_int(self
, op
):
344 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
346 def _test_binop_value_pos_vint(self
, op
):
347 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
349 def _test_binop_value_neg_vint(self
, op
):
350 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
352 def _test_binop_value_zero_vint(self
, op
):
353 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
355 def _test_binop_value_pos_float(self
, op
):
356 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
358 def _test_binop_value_neg_float(self
, op
):
359 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
361 def _test_binop_value_zero_float(self
, op
):
362 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
364 def _test_binop_value_pos_vfloat(self
, op
):
365 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
367 def _test_binop_value_neg_vfloat(self
, op
):
368 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
370 def _test_binop_value_zero_vfloat(self
, op
):
371 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
373 def _test_binop_lhs_addr_same_false(self
, op
):
374 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
376 def _test_binop_lhs_addr_same_true(self
, op
):
377 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
379 def _test_binop_lhs_addr_same_pos_int(self
, op
):
380 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
382 def _test_binop_lhs_addr_same_neg_int(self
, op
):
383 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
385 def _test_binop_lhs_addr_same_zero_int(self
, op
):
386 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
388 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
389 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
391 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
392 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
394 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
395 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
397 def _test_binop_lhs_addr_same_pos_float(self
, op
):
398 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
400 def _test_binop_lhs_addr_same_neg_float(self
, op
):
401 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
403 def _test_binop_lhs_addr_same_zero_float(self
, op
):
404 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
406 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
407 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
409 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
410 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
412 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
413 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
415 def _test_binop_lhs_value_same_false(self
, op
):
416 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
418 def _test_binop_lhs_value_same_true(self
, op
):
419 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
421 def _test_binop_lhs_value_same_pos_int(self
, op
):
422 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
424 def _test_binop_lhs_value_same_neg_int(self
, op
):
425 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
427 def _test_binop_lhs_value_same_zero_int(self
, op
):
428 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
430 def _test_binop_lhs_value_same_pos_vint(self
, op
):
431 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
433 def _test_binop_lhs_value_same_neg_vint(self
, op
):
434 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
436 def _test_binop_lhs_value_same_zero_vint(self
, op
):
437 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
439 def _test_binop_lhs_value_same_pos_float(self
, op
):
440 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
442 def _test_binop_lhs_value_same_neg_float(self
, op
):
443 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
445 def _test_binop_lhs_value_same_zero_float(self
, op
):
446 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
448 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
449 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
451 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
452 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
454 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
455 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
457 def test_bool_op(self
):
458 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
460 def test_int_op(self
):
461 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
463 def test_float_op(self
):
464 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
466 def test_complex_op(self
):
467 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
469 def test_str_op(self
):
470 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
472 def test_eq_none(self
):
473 # Ignore this lint error:
474 # E711 comparison to None should be 'if cond is None:'
475 # since this is what we want to test (even though not good practice).
476 self
.assertFalse(self
._def
== None) # noqa: E711
478 def test_ne_none(self
):
479 # Ignore this lint error:
480 # E711 comparison to None should be 'if cond is not None:'
481 # since this is what we want to test (even though not good practice).
482 self
.assertTrue(self
._def
!= None) # noqa: E711
492 ('add', operator
.add
),
493 ('radd', lambda a
, b
: operator
.add(b
, a
)),
494 ('and', operator
.and_
),
495 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
496 ('floordiv', operator
.floordiv
),
497 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
498 ('lshift', operator
.lshift
),
499 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
500 ('mod', operator
.mod
),
501 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
502 ('mul', operator
.mul
),
503 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
504 ('or', operator
.or_
),
505 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
506 ('pow', operator
.pow),
507 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
508 ('rshift', operator
.rshift
),
509 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
510 ('sub', operator
.sub
),
511 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
512 ('truediv', operator
.truediv
),
513 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
514 ('xor', operator
.xor
),
515 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
520 ('neg', operator
.neg
),
521 ('pos', operator
.pos
),
522 ('abs', operator
.abs),
523 ('invert', operator
.invert
),
525 ('round_0', partial(round, ndigits
=0)),
526 ('round_1', partial(round, ndigits
=1)),
527 ('round_2', partial(round, ndigits
=2)),
528 ('round_3', partial(round, ndigits
=3)),
530 ('floor', math
.floor
),
531 ('trunc', math
.trunc
),
535 def _inject_numeric_testing_methods(cls
):
536 def test_binop_name(suffix
):
537 return 'test_binop_{}_{}'.format(name
, suffix
)
539 def test_unaryop_name(suffix
):
540 return 'test_unaryop_{}_{}'.format(name
, suffix
)
542 # inject testing methods for each binary operation
543 for name
, binop
in _BINOPS
:
544 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
))
545 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
))
546 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
))
547 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
))
548 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
))
549 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
))
550 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
))
551 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
))
552 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
553 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
554 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
555 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
556 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
557 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
558 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
))
559 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
))
560 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
))
561 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
))
562 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
563 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
564 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
565 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
566 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
))
567 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
))
568 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
))
569 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
))
570 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
))
571 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
))
572 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
573 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
574 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
575 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
576 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
577 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
578 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
))
579 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
))
580 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
581 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
582 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
))
583 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
))
584 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
585 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
586 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
587 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
588 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
589 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
590 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
591 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
592 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
593 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
594 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
))
595 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
596 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
))
597 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
598 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
599 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
600 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
601 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
603 # inject testing methods for each unary operation
604 for name
, unaryop
in _UNARYOPS
:
605 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
))
606 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
))
607 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
))
608 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
))
611 class _TestIntegerFieldCommon(_TestNumericField
):
612 def test_assign_true(self
):
614 self
._def
.value
= raw
615 self
.assertEqual(self
._def
, raw
)
617 def test_assign_false(self
):
619 self
._def
.value
= raw
620 self
.assertEqual(self
._def
, raw
)
622 def test_assign_pos_int(self
):
624 self
._def
.value
= raw
625 self
.assertEqual(self
._def
, raw
)
627 def test_assign_neg_int(self
):
629 self
._def
.value
= raw
630 self
.assertEqual(self
._def
, raw
)
632 def test_assign_int_field(self
):
634 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
636 self
._def
.value
= field
637 self
.assertEqual(self
._def
, raw
)
639 def test_assign_float(self
):
641 self
._def
.value
= raw
642 self
.assertEqual(self
._def
, int(raw
))
644 def test_assign_invalid_type(self
):
645 with self
.assertRaises(TypeError):
646 self
._def
.value
= 'yes'
648 def test_assign_uint(self
):
649 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
650 field
= _create_field(self
._tc
, uint_fc
)
653 self
.assertEqual(field
, raw
)
655 def test_assign_big_uint(self
):
656 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
657 field
= _create_field(self
._tc
, uint_fc
)
658 # Larger than the IEEE 754 double-precision exact representation of
661 field
.value
= (2**53) + 1
662 self
.assertEqual(field
, raw
)
664 def test_assign_uint_invalid_neg(self
):
665 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
666 field
= _create_field(self
._tc
, uint_fc
)
668 with self
.assertRaises(ValueError):
671 def test_str_op(self
):
672 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
675 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
678 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
679 def _create_fc(self
, tc
):
680 return tc
.create_signed_integer_field_class(25)
683 self
._tc
= get_default_trace_class()
684 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
685 self
._field
.value
= 17
686 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
689 self
._def
_new
_value
= -101
692 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
693 def _create_fc(self
, tc
):
694 fc
= tc
.create_signed_enumeration_field_class(32)
695 fc
.map_range('something', 17)
696 fc
.map_range('speaker', 12, 16)
697 fc
.map_range('can', 18, 2540)
698 fc
.map_range('whole range', -(2 ** 31), (2 ** 31) - 1)
699 fc
.map_range('zip', -45, 1001)
703 self
._tc
= get_default_trace_class()
704 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
705 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
708 self
._def
_new
_value
= -101
710 def test_str_op(self
):
711 expected_string_found
= False
714 # Establish all permutations of the three expected matches since
715 # the order in which mappings are enumerated is not explicitly part of
717 for p
in itertools
.permutations(['whole range', 'something',
719 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
721 expected_string_found
= True
724 self
.assertTrue(expected_string_found
)
726 def test_labels(self
):
727 self
._field
.value
= 17
728 labels
= sorted(self
._field
.labels
)
729 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
732 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
733 def _create_fc(self
, tc
):
734 return tc
.create_real_field_class()
737 self
._tc
= get_default_trace_class()
738 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
739 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
740 self
._def
.value
= 52.7
741 self
._def
_value
= 52.7
742 self
._def
_new
_value
= -17.164857
744 def _test_invalid_op(self
, cb
):
745 with self
.assertRaises(TypeError):
748 def test_assign_true(self
):
749 self
._def
.value
= True
750 self
.assertTrue(self
._def
)
752 def test_assign_false(self
):
753 self
._def
.value
= False
754 self
.assertFalse(self
._def
)
756 def test_assign_pos_int(self
):
758 self
._def
.value
= raw
759 self
.assertEqual(self
._def
, float(raw
))
761 def test_assign_neg_int(self
):
763 self
._def
.value
= raw
764 self
.assertEqual(self
._def
, float(raw
))
766 def test_assign_int_field(self
):
767 int_fc
= self
._tc
.create_signed_integer_field_class(32)
768 int_field
= _create_field(self
._tc
, int_fc
)
770 int_field
.value
= raw
771 self
._def
.value
= int_field
772 self
.assertEqual(self
._def
, float(raw
))
774 def test_assign_float(self
):
776 self
._def
.value
= raw
777 self
.assertEqual(self
._def
, raw
)
779 def test_assign_float_field(self
):
780 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
783 self
._def
.value
= field
784 self
.assertEqual(self
._def
, raw
)
786 def test_assign_invalid_type(self
):
787 with self
.assertRaises(TypeError):
788 self
._def
.value
= 'yes'
790 def test_invalid_lshift(self
):
791 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
793 def test_invalid_rshift(self
):
794 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
796 def test_invalid_and(self
):
797 self
._test
_invalid
_op
(lambda: self
._def
& 23)
799 def test_invalid_or(self
):
800 self
._test
_invalid
_op
(lambda: self
._def |
23)
802 def test_invalid_xor(self
):
803 self
._test
_invalid
_op
(lambda: self
._def ^
23)
805 def test_invalid_invert(self
):
806 self
._test
_invalid
_op
(lambda: ~self
._def
)
808 def test_str_op(self
):
809 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
812 _inject_numeric_testing_methods(RealFieldTestCase
)
815 class StringFieldTestCase(unittest
.TestCase
):
817 self
._tc
= get_default_trace_class()
818 self
._def
_value
= 'Hello, World!'
819 self
._def
= _create_string_field(self
._tc
)
820 self
._def
.value
= self
._def
_value
821 self
._def
_new
_value
= 'Yes!'
823 def test_assign_int(self
):
824 with self
.assertRaises(TypeError):
825 self
._def
.value
= 283
827 def test_assign_string_field(self
):
828 field
= _create_string_field(self
._tc
)
831 self
.assertEqual(field
, raw
)
834 self
.assertEqual(self
._def
, self
._def
_value
)
836 def test_not_eq(self
):
837 self
.assertNotEqual(self
._def
, 23)
839 def test_lt_vstring(self
):
840 s1
= _create_string_field(self
._tc
)
842 s2
= _create_string_field(self
._tc
)
844 self
.assertLess(s1
, s2
)
846 def test_lt_string(self
):
847 s1
= _create_string_field(self
._tc
)
849 self
.assertLess(s1
, 'bateau')
851 def test_le_vstring(self
):
852 s1
= _create_string_field(self
._tc
)
854 s2
= _create_string_field(self
._tc
)
856 self
.assertLessEqual(s1
, s2
)
858 def test_le_string(self
):
859 s1
= _create_string_field(self
._tc
)
861 self
.assertLessEqual(s1
, 'bateau')
863 def test_gt_vstring(self
):
864 s1
= _create_string_field(self
._tc
)
866 s2
= _create_string_field(self
._tc
)
868 self
.assertGreater(s2
, s1
)
870 def test_gt_string(self
):
871 s1
= _create_string_field(self
._tc
)
873 self
.assertGreater('bateau', s1
)
875 def test_ge_vstring(self
):
876 s1
= _create_string_field(self
._tc
)
878 s2
= _create_string_field(self
._tc
)
880 self
.assertGreaterEqual(s2
, s1
)
882 def test_ge_string(self
):
883 s1
= _create_string_field(self
._tc
)
885 self
.assertGreaterEqual('bateau', s1
)
887 def test_bool_op(self
):
888 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
890 def test_str_op(self
):
891 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
894 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
896 def test_getitem(self
):
897 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
899 def test_append_str(self
):
900 to_append
= 'meow meow meow'
901 self
._def
+= to_append
902 self
._def
_value
+= to_append
903 self
.assertEqual(self
._def
, self
._def
_value
)
905 def test_append_string_field(self
):
906 field
= _create_string_field(self
._tc
)
907 to_append
= 'meow meow meow'
908 field
.value
= to_append
910 self
._def
_value
+= to_append
911 self
.assertEqual(self
._def
, self
._def
_value
)
914 class _TestArrayFieldCommon
:
915 def _modify_def(self
):
918 def test_bool_op_true(self
):
919 self
.assertTrue(self
._def
)
922 self
.assertEqual(len(self
._def
), 3)
924 def test_length(self
):
925 self
.assertEqual(self
._def
.length
, 3)
927 def test_getitem(self
):
929 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
930 self
.assertEqual(field
, 1847)
933 field
= _create_int_array_field(self
._tc
, 3)
937 self
.assertEqual(self
._def
, field
)
939 def test_eq_invalid_type(self
):
940 self
.assertNotEqual(self
._def
, 23)
942 def test_eq_diff_len(self
):
943 field
= _create_int_array_field(self
._tc
, 2)
946 self
.assertNotEqual(self
._def
, field
)
948 def test_eq_diff_content_same_len(self
):
949 field
= _create_int_array_field(self
._tc
, 3)
953 self
.assertNotEqual(self
._def
, field
)
955 def test_setitem(self
):
957 self
.assertEqual(self
._def
[2], 24)
959 def test_setitem_int_field(self
):
960 int_fc
= self
._tc
.create_signed_integer_field_class(32)
961 int_field
= _create_field(self
._tc
, int_fc
)
962 int_field
.value
= 19487
963 self
._def
[1] = int_field
964 self
.assertEqual(self
._def
[1], 19487)
966 def test_setitem_non_basic_field(self
):
967 array_field
= _create_struct_array_field(self
._tc
, 2)
968 with self
.assertRaises(TypeError):
971 def test_setitem_none(self
):
972 with self
.assertRaises(TypeError):
975 def test_setitem_index_wrong_type(self
):
976 with self
.assertRaises(TypeError):
977 self
._def
['yes'] = 23
979 def test_setitem_index_neg(self
):
980 with self
.assertRaises(IndexError):
983 def test_setitem_index_out_of_range(self
):
984 with self
.assertRaises(IndexError):
985 self
._def
[len(self
._def
)] = 134679
988 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
989 self
.assertEqual(field
, value
)
991 def test_value_int_field(self
):
992 values
= [45646, 145, 12145]
993 self
._def
.value
= values
994 self
.assertEqual(values
, self
._def
)
996 def test_value_check_sequence(self
):
998 with self
.assertRaises(TypeError):
999 self
._def
.value
= values
1001 def test_value_wrong_type_in_sequence(self
):
1002 values
= [32, 'hello', 11]
1003 with self
.assertRaises(TypeError):
1004 self
._def
.value
= values
1006 def test_value_complex_type(self
):
1007 struct_fc
= self
._tc
.create_structure_field_class()
1008 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1009 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1010 str_fc
= self
._tc
.create_string_field_class()
1011 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1012 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1013 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1014 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1015 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1019 'a_string': 'hello',
1024 'a_string': 'goodbye',
1029 'a_string': 'or not',
1034 array
= stream
.create_packet().context_field
['array_field']
1035 array
.value
= values
1036 self
.assertEqual(values
, array
)
1037 values
[0]['an_int'] = 'a string'
1038 with self
.assertRaises(TypeError):
1039 array
.value
= values
1041 def test_str_op(self
):
1043 expected_string
= '[{}]'.format(', '.join(
1044 [repr(v
) for v
in self
._def
_value
]))
1045 self
.assertEqual(expected_string
, s
)
1048 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1050 self
._tc
= get_default_trace_class()
1051 self
._def
= _create_int_array_field(self
._tc
, 3)
1054 self
._def
[2] = 1948754
1055 self
._def
_value
= [45, 1847, 1948754]
1057 def test_value_wrong_len(self
):
1059 with self
.assertRaises(ValueError):
1060 self
._def
.value
= values
1063 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1065 self
._tc
= get_default_trace_class()
1066 self
._def
= _create_dynamic_array(self
._tc
)
1069 self
._def
[2] = 1948754
1070 self
._def
_value
= [45, 1847, 1948754]
1072 def test_value_resize(self
):
1073 new_values
= [1, 2, 3, 4]
1074 self
._def
.value
= new_values
1075 self
.assertCountEqual(self
._def
, new_values
)
1077 def test_set_length(self
):
1078 self
._def
.length
= 4
1080 self
.assertEqual(len(self
._def
), 4)
1082 def test_set_invalid_length(self
):
1083 with self
.assertRaises(TypeError):
1084 self
._def
.length
= 'cheval'
1087 class StructureFieldTestCase(unittest
.TestCase
):
1088 def _create_fc(self
, tc
):
1089 fc
= tc
.create_structure_field_class()
1090 fc
.append_member('A', self
._fc
0_fn
())
1091 fc
.append_member('B', self
._fc
1_fn
())
1092 fc
.append_member('C', self
._fc
2_fn
())
1093 fc
.append_member('D', self
._fc
3_fn
())
1094 fc
.append_member('E', self
._fc
4_fn
())
1095 fc5
= self
._fc
5_fn
()
1096 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1097 fc
.append_member('F', fc5
)
1101 self
._tc
= get_default_trace_class()
1102 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1103 self
._fc
1_fn
= self
._tc
.create_string_field_class
1104 self
._fc
2_fn
= self
._tc
.create_real_field_class
1105 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1106 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1107 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1108 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1110 self
._fc
= self
._create
_fc
(self
._tc
)
1111 self
._def
= _create_field(self
._tc
, self
._fc
)
1112 self
._def
['A'] = -1872
1113 self
._def
['B'] = 'salut'
1114 self
._def
['C'] = 17.5
1115 self
._def
['D'] = 16497
1117 self
._def
['F'] = {'F_1': 52}
1127 def _modify_def(self
):
1128 self
._def
['B'] = 'hola'
1130 def test_bool_op_true(self
):
1131 self
.assertTrue(self
._def
)
1133 def test_bool_op_false(self
):
1134 field
= self
._def
['E']
1135 self
.assertFalse(field
)
1138 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1140 def test_getitem(self
):
1141 field
= self
._def
['A']
1142 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1143 self
.assertEqual(field
, -1872)
1145 def test_member_at_index_out_of_bounds_after(self
):
1146 with self
.assertRaises(IndexError):
1147 self
._def
.member_at_index(len(self
._def
_value
))
1150 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
, ))
1152 field
['B'] = 'salut'
1156 field
['F'] = {'F_1': 52}
1157 self
.assertEqual(self
._def
, field
)
1159 def test_eq_invalid_type(self
):
1160 self
.assertNotEqual(self
._def
, 23)
1162 def test_eq_diff_len(self
):
1163 fc
= self
._tc
.create_structure_field_class()
1164 fc
.append_member('A', self
._fc
0_fn
())
1165 fc
.append_member('B', self
._fc
1_fn
())
1166 fc
.append_member('C', self
._fc
2_fn
())
1168 field
= _create_field(self
._tc
, fc
)
1170 field
['B'] = 'salut'
1172 self
.assertNotEqual(self
._def
, field
)
1174 def test_eq_diff_keys(self
):
1175 fc
= self
._tc
.create_structure_field_class()
1176 fc
.append_member('U', self
._fc
0_fn
())
1177 fc
.append_member('V', self
._fc
1_fn
())
1178 fc
.append_member('W', self
._fc
2_fn
())
1179 fc
.append_member('X', self
._fc
3_fn
())
1180 fc
.append_member('Y', self
._fc
4_fn
())
1181 fc
.append_member('Z', self
._fc
5_fn
())
1182 field
= _create_field(self
._tc
, fc
)
1184 field
['V'] = "gerry"
1189 self
.assertNotEqual(self
._def
, field
)
1191 def test_eq_diff_content_same_len(self
):
1192 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1194 field
['B'] = 'salut'
1198 field
['F'] = {'F_1': 0}
1199 self
.assertNotEqual(self
._def
, field
)
1201 def test_eq_same_content_diff_keys(self
):
1202 fc
= self
._tc
.create_structure_field_class()
1203 fc
.append_member('A', self
._fc
0_fn
())
1204 fc
.append_member('B', self
._fc
1_fn
())
1205 fc
.append_member('E', self
._fc
2_fn
())
1206 fc
.append_member('D', self
._fc
3_fn
())
1207 fc
.append_member('C', self
._fc
4_fn
())
1208 fc
.append_member('F', self
._fc
5_fn
())
1209 field
= _create_field(self
._tc
, fc
)
1211 field
['B'] = 'salut'
1216 self
.assertNotEqual(self
._def
, field
)
1218 def test_setitem(self
):
1219 self
._def
['C'] = -18.47
1220 self
.assertEqual(self
._def
['C'], -18.47)
1222 def test_setitem_int_field(self
):
1223 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1224 int_field
= _create_field(self
._tc
, int_fc
)
1225 int_field
.value
= 19487
1226 self
._def
['D'] = int_field
1227 self
.assertEqual(self
._def
['D'], 19487)
1229 def test_setitem_non_basic_field(self
):
1230 elem_fc
= self
._tc
.create_structure_field_class()
1231 struct_fc
= self
._tc
.create_structure_field_class()
1232 struct_fc
.append_member('A', elem_fc
)
1233 struct_field
= _create_field(self
._tc
, struct_fc
)
1235 # Will fail on access to .items() of the value
1236 with self
.assertRaises(AttributeError):
1237 struct_field
['A'] = 23
1239 def test_setitem_none(self
):
1240 with self
.assertRaises(TypeError):
1241 self
._def
['C'] = None
1243 def test_setitem_key_wrong_type(self
):
1244 with self
.assertRaises(TypeError):
1247 def test_setitem_wrong_key(self
):
1248 with self
.assertRaises(KeyError):
1249 self
._def
['hi'] = 134679
1251 def test_member_at_index(self
):
1252 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1254 def test_iter(self
):
1264 for vkey
, vval
in self
._def
.items():
1265 val
= orig_values
[vkey
]
1266 self
.assertEqual(vval
, val
)
1268 def test_value(self
):
1277 self
.assertEqual(self
._def
, orig_values
)
1279 def test_set_value(self
):
1280 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1281 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1282 str_fc
= self
._tc
.create_string_field_class()
1283 struct_fc
= self
._tc
.create_structure_field_class()
1284 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1285 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1286 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1289 'a_string': 'hello',
1293 struct
= _create_field(self
._tc
, struct_fc
)
1294 struct
.value
= values
1295 self
.assertEqual(values
, struct
)
1297 bad_type_values
= copy
.deepcopy(values
)
1298 bad_type_values
['an_int'] = 'a string'
1299 with self
.assertRaises(TypeError):
1300 struct
.value
= bad_type_values
1302 unknown_key_values
= copy
.deepcopy(values
)
1303 unknown_key_values
['unknown_key'] = 16546
1304 with self
.assertRaises(KeyError):
1305 struct
.value
= unknown_key_values
1307 def test_str_op(self
):
1308 expected_string_found
= False
1310 # Establish all permutations of the three expected matches since
1311 # the order in which mappings are enumerated is not explicitly part of
1313 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1314 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1315 candidate
= '{{{}}}'.format(', '.join(items
))
1317 expected_string_found
= True
1320 self
.assertTrue(expected_string_found
)
1323 class VariantFieldTestCase(unittest
.TestCase
):
1324 def _create_fc(self
, tc
):
1325 selector_fc
= tc
.create_signed_enumeration_field_class(field_value_range
=32)
1326 selector_fc
.map_range('corner', 23)
1327 selector_fc
.map_range('zoom', 17, 20)
1328 selector_fc
.map_range('mellotron', 1001)
1329 selector_fc
.map_range('giorgio', 2000, 3000)
1331 ft0
= tc
.create_signed_integer_field_class(32)
1332 ft1
= tc
.create_string_field_class()
1333 ft2
= tc
.create_real_field_class()
1334 ft3
= tc
.create_signed_integer_field_class(17)
1336 fc
= tc
.create_variant_field_class()
1337 fc
.append_option('corner', ft0
)
1338 fc
.append_option('zoom', ft1
)
1339 fc
.append_option('mellotron', ft2
)
1340 fc
.append_option('giorgio', ft3
)
1341 fc
.selector_field_class
= selector_fc
1343 top_fc
= tc
.create_structure_field_class()
1344 top_fc
.append_member('selector_field', selector_fc
)
1345 top_fc
.append_member('variant_field', fc
)
1349 self
._tc
= get_default_trace_class()
1350 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1351 self
._def
= fld
['variant_field']
1353 def test_bool_op(self
):
1354 self
._def
.selected_option_index
= 2
1355 self
._def
.value
= -17.34
1356 with self
.assertRaises(NotImplementedError):
1359 def test_selected_option_index(self
):
1360 self
._def
.selected_option_index
= 2
1361 self
.assertEqual(self
._def
.selected_option_index
, 2)
1363 def test_selected_option(self
):
1364 self
._def
.selected_option_index
= 2
1365 self
._def
.value
= -17.34
1366 self
.assertEqual(self
._def
.selected_option
, -17.34)
1368 self
._def
.selected_option_index
= 3
1369 self
._def
.value
= 1921
1370 self
.assertEqual(self
._def
.selected_option
, 1921)
1373 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1374 field
= field
['variant_field']
1375 field
.selected_option_index
= 0
1377 self
._def
.selected_option_index
= 0
1378 self
._def
.value
= 1774
1379 self
.assertEqual(self
._def
, field
)
1381 def test_eq_invalid_type(self
):
1382 self
._def
.selected_option_index
= 1
1383 self
._def
.value
= 'gerry'
1384 self
.assertNotEqual(self
._def
, 23)
1386 def test_str_op_int(self
):
1387 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1388 field
= field
['variant_field']
1389 field
.selected_option_index
= 0
1391 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1392 other_field
= other_field
['variant_field']
1393 other_field
.selected_option_index
= 0
1394 other_field
.value
= 1774
1395 self
.assertEqual(str(field
), str(other_field
))
1397 def test_str_op_str(self
):
1398 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1399 field
= field
['variant_field']
1400 field
.selected_option_index
= 1
1401 field
.value
= 'un beau grand bateau'
1402 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1403 other_field
= other_field
['variant_field']
1404 other_field
.selected_option_index
= 1
1405 other_field
.value
= 'un beau grand bateau'
1406 self
.assertEqual(str(field
), str(other_field
))
1408 def test_str_op_float(self
):
1409 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1410 field
= field
['variant_field']
1411 field
.selected_option_index
= 2
1412 field
.value
= 14.4245
1413 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1414 other_field
= other_field
['variant_field']
1415 other_field
.selected_option_index
= 2
1416 other_field
.value
= 14.4245
1417 self
.assertEqual(str(field
), str(other_field
))