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_invalid_type(self
):
640 with self
.assertRaises(TypeError):
641 self
._def
.value
= 'yes'
643 def test_assign_uint(self
):
644 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
645 field
= _create_field(self
._tc
, uint_fc
)
648 self
.assertEqual(field
, raw
)
650 def test_assign_big_uint(self
):
651 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
652 field
= _create_field(self
._tc
, uint_fc
)
653 # Larger than the IEEE 754 double-precision exact representation of
656 field
.value
= (2**53) + 1
657 self
.assertEqual(field
, raw
)
659 def test_assign_uint_invalid_neg(self
):
660 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
661 field
= _create_field(self
._tc
, uint_fc
)
663 with self
.assertRaises(ValueError):
666 def test_str_op(self
):
667 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
670 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
673 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
674 def _create_fc(self
, tc
):
675 return tc
.create_signed_integer_field_class(25)
678 self
._tc
= get_default_trace_class()
679 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
680 self
._field
.value
= 17
681 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
684 self
._def
_new
_value
= -101
687 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
688 def _create_fc(self
, tc
):
689 fc
= tc
.create_signed_enumeration_field_class(32)
690 fc
.map_range('something', 17)
691 fc
.map_range('speaker', 12, 16)
692 fc
.map_range('can', 18, 2540)
693 fc
.map_range('whole range', -(2 ** 31), (2 ** 31) - 1)
694 fc
.map_range('zip', -45, 1001)
698 self
._tc
= get_default_trace_class()
699 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
700 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
703 self
._def
_new
_value
= -101
705 def test_str_op(self
):
706 expected_string_found
= False
709 # Establish all permutations of the three expected matches since
710 # the order in which mappings are enumerated is not explicitly part of
712 for p
in itertools
.permutations(['whole range', 'something',
714 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
716 expected_string_found
= True
719 self
.assertTrue(expected_string_found
)
721 def test_labels(self
):
722 self
._field
.value
= 17
723 labels
= sorted(self
._field
.labels
)
724 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
727 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
728 def _create_fc(self
, tc
):
729 return tc
.create_real_field_class()
732 self
._tc
= get_default_trace_class()
733 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
734 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
735 self
._def
.value
= 52.7
736 self
._def
_value
= 52.7
737 self
._def
_new
_value
= -17.164857
739 def _test_invalid_op(self
, cb
):
740 with self
.assertRaises(TypeError):
743 def test_assign_true(self
):
744 self
._def
.value
= True
745 self
.assertTrue(self
._def
)
747 def test_assign_false(self
):
748 self
._def
.value
= False
749 self
.assertFalse(self
._def
)
751 def test_assign_pos_int(self
):
753 self
._def
.value
= raw
754 self
.assertEqual(self
._def
, float(raw
))
756 def test_assign_neg_int(self
):
758 self
._def
.value
= raw
759 self
.assertEqual(self
._def
, float(raw
))
761 def test_assign_int_field(self
):
762 int_fc
= self
._tc
.create_signed_integer_field_class(32)
763 int_field
= _create_field(self
._tc
, int_fc
)
765 int_field
.value
= raw
766 self
._def
.value
= int_field
767 self
.assertEqual(self
._def
, float(raw
))
769 def test_assign_float(self
):
771 self
._def
.value
= raw
772 self
.assertEqual(self
._def
, raw
)
774 def test_assign_float_field(self
):
775 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
778 self
._def
.value
= field
779 self
.assertEqual(self
._def
, raw
)
781 def test_assign_invalid_type(self
):
782 with self
.assertRaises(TypeError):
783 self
._def
.value
= 'yes'
785 def test_invalid_lshift(self
):
786 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
788 def test_invalid_rshift(self
):
789 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
791 def test_invalid_and(self
):
792 self
._test
_invalid
_op
(lambda: self
._def
& 23)
794 def test_invalid_or(self
):
795 self
._test
_invalid
_op
(lambda: self
._def |
23)
797 def test_invalid_xor(self
):
798 self
._test
_invalid
_op
(lambda: self
._def ^
23)
800 def test_invalid_invert(self
):
801 self
._test
_invalid
_op
(lambda: ~self
._def
)
803 def test_str_op(self
):
804 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
807 _inject_numeric_testing_methods(RealFieldTestCase
)
810 class StringFieldTestCase(unittest
.TestCase
):
812 self
._tc
= get_default_trace_class()
813 self
._def
_value
= 'Hello, World!'
814 self
._def
= _create_string_field(self
._tc
)
815 self
._def
.value
= self
._def
_value
816 self
._def
_new
_value
= 'Yes!'
818 def test_assign_int(self
):
819 with self
.assertRaises(TypeError):
820 self
._def
.value
= 283
822 def test_assign_string_field(self
):
823 field
= _create_string_field(self
._tc
)
826 self
.assertEqual(field
, raw
)
829 self
.assertEqual(self
._def
, self
._def
_value
)
831 def test_not_eq(self
):
832 self
.assertNotEqual(self
._def
, 23)
834 def test_lt_vstring(self
):
835 s1
= _create_string_field(self
._tc
)
837 s2
= _create_string_field(self
._tc
)
839 self
.assertLess(s1
, s2
)
841 def test_lt_string(self
):
842 s1
= _create_string_field(self
._tc
)
844 self
.assertLess(s1
, 'bateau')
846 def test_le_vstring(self
):
847 s1
= _create_string_field(self
._tc
)
849 s2
= _create_string_field(self
._tc
)
851 self
.assertLessEqual(s1
, s2
)
853 def test_le_string(self
):
854 s1
= _create_string_field(self
._tc
)
856 self
.assertLessEqual(s1
, 'bateau')
858 def test_gt_vstring(self
):
859 s1
= _create_string_field(self
._tc
)
861 s2
= _create_string_field(self
._tc
)
863 self
.assertGreater(s2
, s1
)
865 def test_gt_string(self
):
866 s1
= _create_string_field(self
._tc
)
868 self
.assertGreater('bateau', s1
)
870 def test_ge_vstring(self
):
871 s1
= _create_string_field(self
._tc
)
873 s2
= _create_string_field(self
._tc
)
875 self
.assertGreaterEqual(s2
, s1
)
877 def test_ge_string(self
):
878 s1
= _create_string_field(self
._tc
)
880 self
.assertGreaterEqual('bateau', s1
)
882 def test_bool_op(self
):
883 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
885 def test_str_op(self
):
886 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
889 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
891 def test_getitem(self
):
892 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
894 def test_append_str(self
):
895 to_append
= 'meow meow meow'
896 self
._def
+= to_append
897 self
._def
_value
+= to_append
898 self
.assertEqual(self
._def
, self
._def
_value
)
900 def test_append_string_field(self
):
901 field
= _create_string_field(self
._tc
)
902 to_append
= 'meow meow meow'
903 field
.value
= to_append
905 self
._def
_value
+= to_append
906 self
.assertEqual(self
._def
, self
._def
_value
)
909 class _TestArrayFieldCommon
:
910 def _modify_def(self
):
913 def test_bool_op_true(self
):
914 self
.assertTrue(self
._def
)
917 self
.assertEqual(len(self
._def
), 3)
919 def test_length(self
):
920 self
.assertEqual(self
._def
.length
, 3)
922 def test_getitem(self
):
924 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
925 self
.assertEqual(field
, 1847)
928 field
= _create_int_array_field(self
._tc
, 3)
932 self
.assertEqual(self
._def
, field
)
934 def test_eq_invalid_type(self
):
935 self
.assertNotEqual(self
._def
, 23)
937 def test_eq_diff_len(self
):
938 field
= _create_int_array_field(self
._tc
, 2)
941 self
.assertNotEqual(self
._def
, field
)
943 def test_eq_diff_content_same_len(self
):
944 field
= _create_int_array_field(self
._tc
, 3)
948 self
.assertNotEqual(self
._def
, field
)
950 def test_setitem(self
):
952 self
.assertEqual(self
._def
[2], 24)
954 def test_setitem_int_field(self
):
955 int_fc
= self
._tc
.create_signed_integer_field_class(32)
956 int_field
= _create_field(self
._tc
, int_fc
)
957 int_field
.value
= 19487
958 self
._def
[1] = int_field
959 self
.assertEqual(self
._def
[1], 19487)
961 def test_setitem_non_basic_field(self
):
962 array_field
= _create_struct_array_field(self
._tc
, 2)
963 with self
.assertRaises(TypeError):
966 def test_setitem_none(self
):
967 with self
.assertRaises(TypeError):
970 def test_setitem_index_wrong_type(self
):
971 with self
.assertRaises(TypeError):
972 self
._def
['yes'] = 23
974 def test_setitem_index_neg(self
):
975 with self
.assertRaises(IndexError):
978 def test_setitem_index_out_of_range(self
):
979 with self
.assertRaises(IndexError):
980 self
._def
[len(self
._def
)] = 134679
983 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
984 self
.assertEqual(field
, value
)
986 def test_value_int_field(self
):
987 values
= [45646, 145, 12145]
988 self
._def
.value
= values
989 self
.assertEqual(values
, self
._def
)
991 def test_value_check_sequence(self
):
993 with self
.assertRaises(TypeError):
994 self
._def
.value
= values
996 def test_value_wrong_type_in_sequence(self
):
997 values
= [32, 'hello', 11]
998 with self
.assertRaises(TypeError):
999 self
._def
.value
= values
1001 def test_value_complex_type(self
):
1002 struct_fc
= self
._tc
.create_structure_field_class()
1003 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1004 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1005 str_fc
= self
._tc
.create_string_field_class()
1006 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1007 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1008 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1009 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1010 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1014 'a_string': 'hello',
1019 'a_string': 'goodbye',
1024 'a_string': 'or not',
1029 array
= stream
.create_packet().context_field
['array_field']
1030 array
.value
= values
1031 self
.assertEqual(values
, array
)
1032 values
[0]['an_int'] = 'a string'
1033 with self
.assertRaises(TypeError):
1034 array
.value
= values
1036 def test_str_op(self
):
1038 expected_string
= '[{}]'.format(', '.join(
1039 [repr(v
) for v
in self
._def
_value
]))
1040 self
.assertEqual(expected_string
, s
)
1043 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1045 self
._tc
= get_default_trace_class()
1046 self
._def
= _create_int_array_field(self
._tc
, 3)
1049 self
._def
[2] = 1948754
1050 self
._def
_value
= [45, 1847, 1948754]
1052 def test_value_wrong_len(self
):
1054 with self
.assertRaises(ValueError):
1055 self
._def
.value
= values
1058 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1060 self
._tc
= get_default_trace_class()
1061 self
._def
= _create_dynamic_array(self
._tc
)
1064 self
._def
[2] = 1948754
1065 self
._def
_value
= [45, 1847, 1948754]
1067 def test_value_resize(self
):
1068 new_values
= [1, 2, 3, 4]
1069 self
._def
.value
= new_values
1070 self
.assertCountEqual(self
._def
, new_values
)
1072 def test_set_length(self
):
1073 self
._def
.length
= 4
1075 self
.assertEqual(len(self
._def
), 4)
1077 def test_set_invalid_length(self
):
1078 with self
.assertRaises(TypeError):
1079 self
._def
.length
= 'cheval'
1082 class StructureFieldTestCase(unittest
.TestCase
):
1083 def _create_fc(self
, tc
):
1084 fc
= tc
.create_structure_field_class()
1085 fc
.append_member('A', self
._fc
0_fn
())
1086 fc
.append_member('B', self
._fc
1_fn
())
1087 fc
.append_member('C', self
._fc
2_fn
())
1088 fc
.append_member('D', self
._fc
3_fn
())
1089 fc
.append_member('E', self
._fc
4_fn
())
1090 fc5
= self
._fc
5_fn
()
1091 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1092 fc
.append_member('F', fc5
)
1096 self
._tc
= get_default_trace_class()
1097 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1098 self
._fc
1_fn
= self
._tc
.create_string_field_class
1099 self
._fc
2_fn
= self
._tc
.create_real_field_class
1100 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1101 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1102 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1103 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1105 self
._fc
= self
._create
_fc
(self
._tc
)
1106 self
._def
= _create_field(self
._tc
, self
._fc
)
1107 self
._def
['A'] = -1872
1108 self
._def
['B'] = 'salut'
1109 self
._def
['C'] = 17.5
1110 self
._def
['D'] = 16497
1112 self
._def
['F'] = {'F_1': 52}
1122 def _modify_def(self
):
1123 self
._def
['B'] = 'hola'
1125 def test_bool_op_true(self
):
1126 self
.assertTrue(self
._def
)
1128 def test_bool_op_false(self
):
1129 field
= self
._def
['E']
1130 self
.assertFalse(field
)
1133 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1135 def test_getitem(self
):
1136 field
= self
._def
['A']
1137 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1138 self
.assertEqual(field
, -1872)
1140 def test_member_at_index_out_of_bounds_after(self
):
1141 with self
.assertRaises(IndexError):
1142 self
._def
.member_at_index(len(self
._def
_value
))
1145 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
, ))
1147 field
['B'] = 'salut'
1151 field
['F'] = {'F_1': 52}
1152 self
.assertEqual(self
._def
, field
)
1154 def test_eq_invalid_type(self
):
1155 self
.assertNotEqual(self
._def
, 23)
1157 def test_eq_diff_len(self
):
1158 fc
= self
._tc
.create_structure_field_class()
1159 fc
.append_member('A', self
._fc
0_fn
())
1160 fc
.append_member('B', self
._fc
1_fn
())
1161 fc
.append_member('C', self
._fc
2_fn
())
1163 field
= _create_field(self
._tc
, fc
)
1165 field
['B'] = 'salut'
1167 self
.assertNotEqual(self
._def
, field
)
1169 def test_eq_diff_keys(self
):
1170 fc
= self
._tc
.create_structure_field_class()
1171 fc
.append_member('U', self
._fc
0_fn
())
1172 fc
.append_member('V', self
._fc
1_fn
())
1173 fc
.append_member('W', self
._fc
2_fn
())
1174 fc
.append_member('X', self
._fc
3_fn
())
1175 fc
.append_member('Y', self
._fc
4_fn
())
1176 fc
.append_member('Z', self
._fc
5_fn
())
1177 field
= _create_field(self
._tc
, fc
)
1179 field
['V'] = "gerry"
1184 self
.assertNotEqual(self
._def
, field
)
1186 def test_eq_diff_content_same_len(self
):
1187 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1189 field
['B'] = 'salut'
1193 field
['F'] = {'F_1': 0}
1194 self
.assertNotEqual(self
._def
, field
)
1196 def test_eq_same_content_diff_keys(self
):
1197 fc
= self
._tc
.create_structure_field_class()
1198 fc
.append_member('A', self
._fc
0_fn
())
1199 fc
.append_member('B', self
._fc
1_fn
())
1200 fc
.append_member('E', self
._fc
2_fn
())
1201 fc
.append_member('D', self
._fc
3_fn
())
1202 fc
.append_member('C', self
._fc
4_fn
())
1203 fc
.append_member('F', self
._fc
5_fn
())
1204 field
= _create_field(self
._tc
, fc
)
1206 field
['B'] = 'salut'
1211 self
.assertNotEqual(self
._def
, field
)
1213 def test_setitem(self
):
1214 self
._def
['C'] = -18.47
1215 self
.assertEqual(self
._def
['C'], -18.47)
1217 def test_setitem_int_field(self
):
1218 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1219 int_field
= _create_field(self
._tc
, int_fc
)
1220 int_field
.value
= 19487
1221 self
._def
['D'] = int_field
1222 self
.assertEqual(self
._def
['D'], 19487)
1224 def test_setitem_non_basic_field(self
):
1225 elem_fc
= self
._tc
.create_structure_field_class()
1226 struct_fc
= self
._tc
.create_structure_field_class()
1227 struct_fc
.append_member('A', elem_fc
)
1228 struct_field
= _create_field(self
._tc
, struct_fc
)
1230 # Will fail on access to .items() of the value
1231 with self
.assertRaises(AttributeError):
1232 struct_field
['A'] = 23
1234 def test_setitem_none(self
):
1235 with self
.assertRaises(TypeError):
1236 self
._def
['C'] = None
1238 def test_setitem_key_wrong_type(self
):
1239 with self
.assertRaises(TypeError):
1242 def test_setitem_wrong_key(self
):
1243 with self
.assertRaises(KeyError):
1244 self
._def
['hi'] = 134679
1246 def test_member_at_index(self
):
1247 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1249 def test_iter(self
):
1259 for vkey
, vval
in self
._def
.items():
1260 val
= orig_values
[vkey
]
1261 self
.assertEqual(vval
, val
)
1263 def test_value(self
):
1272 self
.assertEqual(self
._def
, orig_values
)
1274 def test_set_value(self
):
1275 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1276 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1277 str_fc
= self
._tc
.create_string_field_class()
1278 struct_fc
= self
._tc
.create_structure_field_class()
1279 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1280 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1281 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1284 'a_string': 'hello',
1288 struct
= _create_field(self
._tc
, struct_fc
)
1289 struct
.value
= values
1290 self
.assertEqual(values
, struct
)
1292 bad_type_values
= copy
.deepcopy(values
)
1293 bad_type_values
['an_int'] = 'a string'
1294 with self
.assertRaises(TypeError):
1295 struct
.value
= bad_type_values
1297 unknown_key_values
= copy
.deepcopy(values
)
1298 unknown_key_values
['unknown_key'] = 16546
1299 with self
.assertRaises(KeyError):
1300 struct
.value
= unknown_key_values
1302 def test_str_op(self
):
1303 expected_string_found
= False
1305 # Establish all permutations of the three expected matches since
1306 # the order in which mappings are enumerated is not explicitly part of
1308 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1309 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1310 candidate
= '{{{}}}'.format(', '.join(items
))
1312 expected_string_found
= True
1315 self
.assertTrue(expected_string_found
)
1318 class VariantFieldTestCase(unittest
.TestCase
):
1319 def _create_fc(self
, tc
):
1320 selector_fc
= tc
.create_signed_enumeration_field_class(field_value_range
=32)
1321 selector_fc
.map_range('corner', 23)
1322 selector_fc
.map_range('zoom', 17, 20)
1323 selector_fc
.map_range('mellotron', 1001)
1324 selector_fc
.map_range('giorgio', 2000, 3000)
1326 ft0
= tc
.create_signed_integer_field_class(32)
1327 ft1
= tc
.create_string_field_class()
1328 ft2
= tc
.create_real_field_class()
1329 ft3
= tc
.create_signed_integer_field_class(17)
1331 fc
= tc
.create_variant_field_class()
1332 fc
.append_option('corner', ft0
)
1333 fc
.append_option('zoom', ft1
)
1334 fc
.append_option('mellotron', ft2
)
1335 fc
.append_option('giorgio', ft3
)
1336 fc
.selector_field_class
= selector_fc
1338 top_fc
= tc
.create_structure_field_class()
1339 top_fc
.append_member('selector_field', selector_fc
)
1340 top_fc
.append_member('variant_field', fc
)
1344 self
._tc
= get_default_trace_class()
1345 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1346 self
._def
= fld
['variant_field']
1348 def test_bool_op(self
):
1349 self
._def
.selected_option_index
= 2
1350 self
._def
.value
= -17.34
1351 with self
.assertRaises(NotImplementedError):
1354 def test_selected_option_index(self
):
1355 self
._def
.selected_option_index
= 2
1356 self
.assertEqual(self
._def
.selected_option_index
, 2)
1358 def test_selected_option(self
):
1359 self
._def
.selected_option_index
= 2
1360 self
._def
.value
= -17.34
1361 self
.assertEqual(self
._def
.selected_option
, -17.34)
1363 self
._def
.selected_option_index
= 3
1364 self
._def
.value
= 1921
1365 self
.assertEqual(self
._def
.selected_option
, 1921)
1368 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1369 field
= field
['variant_field']
1370 field
.selected_option_index
= 0
1372 self
._def
.selected_option_index
= 0
1373 self
._def
.value
= 1774
1374 self
.assertEqual(self
._def
, field
)
1376 def test_eq_invalid_type(self
):
1377 self
._def
.selected_option_index
= 1
1378 self
._def
.value
= 'gerry'
1379 self
.assertNotEqual(self
._def
, 23)
1381 def test_str_op_int(self
):
1382 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1383 field
= field
['variant_field']
1384 field
.selected_option_index
= 0
1386 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1387 other_field
= other_field
['variant_field']
1388 other_field
.selected_option_index
= 0
1389 other_field
.value
= 1774
1390 self
.assertEqual(str(field
), str(other_field
))
1392 def test_str_op_str(self
):
1393 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1394 field
= field
['variant_field']
1395 field
.selected_option_index
= 1
1396 field
.value
= 'un beau grand bateau'
1397 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1398 other_field
= other_field
['variant_field']
1399 other_field
.selected_option_index
= 1
1400 other_field
.value
= 'un beau grand bateau'
1401 self
.assertEqual(str(field
), str(other_field
))
1403 def test_str_op_float(self
):
1404 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1405 field
= field
['variant_field']
1406 field
.selected_option_index
= 2
1407 field
.value
= 14.4245
1408 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1409 other_field
= other_field
['variant_field']
1410 other_field
.selected_option_index
= 2
1411 other_field
.value
= 14.4245
1412 self
.assertEqual(str(field
), str(other_field
))