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
27 from utils
import get_default_trace_class
36 # Create and return a stream with the field classes part of its stream packet
39 # The stream is part of a dummy trace created from trace class `tc`.
41 def _create_stream(tc
, ctx_field_classes
):
42 packet_context_fc
= tc
.create_structure_field_class()
43 for name
, fc
in ctx_field_classes
:
44 packet_context_fc
.append_member(name
, fc
)
47 stream_class
= tc
.create_stream_class(packet_context_field_class
=packet_context_fc
)
49 stream
= trace
.create_stream(stream_class
)
53 # Create a field of the given field class.
55 # The field is part of a dummy stream, itself part of a dummy trace created
56 # from trace class `tc`.
58 def _create_field(tc
, field_class
):
60 stream
= _create_stream(tc
, [(field_name
, field_class
)])
61 packet
= stream
.create_packet()
62 return packet
.context_field
[field_name
]
65 # Create a field of type string.
67 # The field is part of a dummy stream, itself part of a dummy trace created
68 # from trace class `tc`. It is made out of a dummy string field class.
70 def _create_string_field(tc
):
71 field_name
= 'string_field'
72 stream
= _create_stream(tc
, [(field_name
, tc
.create_string_field_class())])
73 packet
= stream
.create_packet()
74 return packet
.context_field
[field_name
]
77 # Create a field of type static array of ints.
79 # The field is part of a dummy stream, itself part of a dummy trace created
80 # from trace class `tc`. It is made out of a dummy static array field class,
81 # with a dummy integer field class as element class.
83 def _create_int_array_field(tc
, length
):
84 elem_fc
= tc
.create_signed_integer_field_class(32)
85 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
86 field_name
= 'int_array'
87 stream
= _create_stream(tc
, [(field_name
, fc
)])
88 packet
= stream
.create_packet()
89 return packet
.context_field
[field_name
]
92 # Create a field of type dynamic array of ints.
94 # The field is part of a dummy stream, itself part of a dummy trace created
95 # from trace class `tc`. It is made out of a dummy static array field class,
96 # with a dummy integer field class as element and length classes.
98 def _create_dynamic_array(tc
):
99 elem_fc
= tc
.create_signed_integer_field_class(32)
100 len_fc
= tc
.create_signed_integer_field_class(32)
101 fc
= tc
.create_dynamic_array_field_class(elem_fc
)
102 field_name
= 'int_dyn_array'
103 stream
= _create_stream(tc
, [('thelength', len_fc
), (field_name
, fc
)])
104 packet
= stream
.create_packet()
105 packet
.context_field
[field_name
].length
= 3
106 return packet
.context_field
[field_name
]
109 # Create a field of type array of (empty) structures.
111 # The field is part of a dummy stream, itself part of a dummy trace created
112 # from trace class `tc`. It is made out of a dummy static array field class,
113 # with a dummy struct field class as element class.
115 def _create_struct_array_field(tc
, length
):
116 elem_fc
= tc
.create_structure_field_class()
117 fc
= tc
.create_static_array_field_class(elem_fc
, length
)
118 field_name
= 'struct_array'
119 stream
= _create_stream(tc
, [(field_name
, fc
)])
120 packet
= stream
.create_packet()
121 return packet
.context_field
[field_name
]
124 # Base class for numeric field test cases.
126 # To be compatible with this base class, a derived class must, in its
129 # * Set `self._def` to a field object with an arbitrary value.
130 # * Set `self._def_value` to the equivalent value of `self._def`.
131 class _TestNumericField
:
132 # Tries the binary operation `op`:
134 # 1. Between `self._def`, which is a field object, and `rhs`.
135 # 2. Between `self._def_value`, which is the raw value of
136 # `self._def`, and `rhs`.
138 # Returns the results of 1. and 2.
140 # If there's an exception while performing 1. or 2., asserts that
141 # both operations raised exceptions, that both exceptions have the
142 # same type, and returns `None` for both results.
143 def _binop(self
, op
, rhs
):
148 # try with field object
150 r
= op(self
._def
, rhs
)
151 except Exception as e
:
156 rv
= op(self
._def
_value
, comp_value
)
157 except Exception as e
:
160 if type_rexc
is not None or type_rvexc
is not None:
161 # at least one of the operations raised an exception: in
162 # this case both operations should have raised the same
163 # type of exception (division by zero, bit shift with a
164 # floating point number operand, etc.)
165 self
.assertIs(type_rexc
, type_rvexc
)
170 # Tries the unary operation `op`:
172 # 1. On `self._def`, which is a field object.
173 # 2. On `self._def_value`, which is the value of `self._def`.
175 # Returns the results of 1. and 2.
177 # If there's an exception while performing 1. or 2., asserts that
178 # both operations raised exceptions, that both exceptions have the
179 # same type, and returns `None` for both results.
180 def _unaryop(self
, op
):
184 # try with field object
187 except Exception as e
:
192 rv
= op(self
._def
_value
)
193 except Exception as e
:
196 if type_rexc
is not None or type_rvexc
is not None:
197 # at least one of the operations raised an exception: in
198 # this case both operations should have raised the same
199 # type of exception (division by zero, bit shift with a
200 # floating point number operand, etc.)
201 self
.assertIs(type_rexc
, type_rvexc
)
206 # Tests that the unary operation `op` gives results with the same
207 # type for both `self._def` and `self._def_value`.
208 def _test_unaryop_type(self
, op
):
209 r
, rv
= self
._unaryop
(op
)
214 self
.assertIsInstance(r
, type(rv
))
216 # Tests that the unary operation `op` gives results with the same
217 # value for both `self._def` and `self._def_value`. This uses the
218 # __eq__() operator of `self._def`.
219 def _test_unaryop_value(self
, op
):
220 r
, rv
= self
._unaryop
(op
)
225 self
.assertEqual(r
, rv
)
227 # Tests that the unary operation `op`, when applied to `self._def`,
228 # does not change its underlying BT object address.
229 def _test_unaryop_addr_same(self
, op
):
230 addr_before
= self
._def
.addr
232 self
.assertEqual(self
._def
.addr
, addr_before
)
234 # Tests that the unary operation `op`, when applied to `self._def`,
235 # does not change its value.
236 def _test_unaryop_value_same(self
, op
):
237 value_before
= copy
.copy(self
._def
_value
)
239 self
.assertEqual(self
._def
, value_before
)
241 # Tests that the binary operation `op` gives results with the same
242 # type for both `self._def` and `self._def_value`.
243 def _test_binop_type(self
, op
, rhs
):
244 r
, rv
= self
._binop
(op
, rhs
)
249 if op
in _COMP_BINOPS
:
250 # __eq__() and __ne__() always return a 'bool' object
251 self
.assertIsInstance(r
, bool)
253 self
.assertIsInstance(r
, type(rv
))
255 # Tests that the binary operation `op` gives results with the same
256 # value for both `self._def` and `self._def_value`. This uses the
257 # __eq__() operator of `self._def`.
258 def _test_binop_value(self
, op
, rhs
):
259 r
, rv
= self
._binop
(op
, rhs
)
264 self
.assertEqual(r
, rv
)
266 # Tests that the binary operation `op`, when applied to `self._def`,
267 # does not change its underlying BT object address.
268 def _test_binop_lhs_addr_same(self
, op
, rhs
):
269 addr_before
= self
._def
.addr
270 r
, rv
= self
._binop
(op
, rhs
)
271 self
.assertEqual(self
._def
.addr
, addr_before
)
273 # Tests that the binary operation `op`, when applied to `self._def`,
274 # does not change its value.
275 @unittest.skip('copy is not implemented')
276 def _test_binop_lhs_value_same(self
, op
, rhs
):
277 value_before
= copy
.copy(self
._def
)
278 r
, rv
= self
._binop
(op
, rhs
)
279 self
.assertEqual(self
._def
, value_before
)
281 # The methods below which take the `test_cb` and/or `op` parameters
282 # are meant to be used with one of the _test_binop_*() functions
283 # above as `test_cb` and a binary operator function as `op`.
287 # self._test_binop_rhs_pos_int(self._test_binop_value,
290 # This tests that a numeric field object added to a positive integer
291 # value gives a result with the expected value.
293 # `vint` and `vfloat` mean a signed integer value object and a real
296 def _test_binop_invalid_unknown(self
, op
):
297 if op
in _COMP_BINOPS
:
298 self
.skipTest('not testing')
303 with self
.assertRaises(TypeError):
306 def _test_binop_invalid_none(self
, op
):
307 if op
in _COMP_BINOPS
:
308 self
.skipTest('not testing')
310 with self
.assertRaises(TypeError):
313 def _test_binop_rhs_false(self
, test_cb
, op
):
316 def _test_binop_rhs_true(self
, test_cb
, op
):
319 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
322 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
325 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
328 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
329 test_cb(op
, bt2
.create_value(2))
331 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
332 test_cb(op
, bt2
.create_value(-23))
334 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
335 test_cb(op
, bt2
.create_value(0))
337 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
340 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
343 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
346 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
347 test_cb(op
, bt2
.create_value(2.2))
349 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
350 test_cb(op
, bt2
.create_value(-23.4))
352 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
353 test_cb(op
, bt2
.create_value(0.0))
355 def _test_binop_rhs_complex(self
, test_cb
, op
):
358 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
361 def _test_binop_type_false(self
, op
):
362 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
364 def _test_binop_type_true(self
, op
):
365 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
367 def _test_binop_type_pos_int(self
, op
):
368 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
370 def _test_binop_type_neg_int(self
, op
):
371 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
373 def _test_binop_type_zero_int(self
, op
):
374 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
376 def _test_binop_type_pos_vint(self
, op
):
377 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
379 def _test_binop_type_neg_vint(self
, op
):
380 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
382 def _test_binop_type_zero_vint(self
, op
):
383 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
385 def _test_binop_type_pos_float(self
, op
):
386 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
388 def _test_binop_type_neg_float(self
, op
):
389 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
391 def _test_binop_type_zero_float(self
, op
):
392 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
394 def _test_binop_type_pos_vfloat(self
, op
):
395 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
397 def _test_binop_type_neg_vfloat(self
, op
):
398 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
400 def _test_binop_type_zero_vfloat(self
, op
):
401 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
403 def _test_binop_type_complex(self
, op
):
404 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
406 def _test_binop_type_zero_complex(self
, op
):
407 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
409 def _test_binop_value_false(self
, op
):
410 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
412 def _test_binop_value_true(self
, op
):
413 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
415 def _test_binop_value_pos_int(self
, op
):
416 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
418 def _test_binop_value_neg_int(self
, op
):
419 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
421 def _test_binop_value_zero_int(self
, op
):
422 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
424 def _test_binop_value_pos_vint(self
, op
):
425 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
427 def _test_binop_value_neg_vint(self
, op
):
428 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
430 def _test_binop_value_zero_vint(self
, op
):
431 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
433 def _test_binop_value_pos_float(self
, op
):
434 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
436 def _test_binop_value_neg_float(self
, op
):
437 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
439 def _test_binop_value_zero_float(self
, op
):
440 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
442 def _test_binop_value_pos_vfloat(self
, op
):
443 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
445 def _test_binop_value_neg_vfloat(self
, op
):
446 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
448 def _test_binop_value_zero_vfloat(self
, op
):
449 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
451 def _test_binop_value_complex(self
, op
):
452 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
454 def _test_binop_value_zero_complex(self
, op
):
455 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
457 def _test_binop_lhs_addr_same_false(self
, op
):
458 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
460 def _test_binop_lhs_addr_same_true(self
, op
):
461 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
463 def _test_binop_lhs_addr_same_pos_int(self
, op
):
464 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
466 def _test_binop_lhs_addr_same_neg_int(self
, op
):
467 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
469 def _test_binop_lhs_addr_same_zero_int(self
, op
):
470 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
472 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
473 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
475 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
476 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
478 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
479 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
481 def _test_binop_lhs_addr_same_pos_float(self
, op
):
482 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
484 def _test_binop_lhs_addr_same_neg_float(self
, op
):
485 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
487 def _test_binop_lhs_addr_same_zero_float(self
, op
):
488 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
490 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
491 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
493 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
494 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
496 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
497 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
499 def _test_binop_lhs_addr_same_complex(self
, op
):
500 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
502 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
503 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
505 def _test_binop_lhs_value_same_false(self
, op
):
506 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
508 def _test_binop_lhs_value_same_true(self
, op
):
509 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
511 def _test_binop_lhs_value_same_pos_int(self
, op
):
512 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
514 def _test_binop_lhs_value_same_neg_int(self
, op
):
515 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
517 def _test_binop_lhs_value_same_zero_int(self
, op
):
518 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
520 def _test_binop_lhs_value_same_pos_vint(self
, op
):
521 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
523 def _test_binop_lhs_value_same_neg_vint(self
, op
):
524 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
526 def _test_binop_lhs_value_same_zero_vint(self
, op
):
527 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
529 def _test_binop_lhs_value_same_pos_float(self
, op
):
530 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
532 def _test_binop_lhs_value_same_neg_float(self
, op
):
533 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
535 def _test_binop_lhs_value_same_zero_float(self
, op
):
536 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
538 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
539 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
541 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
542 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
544 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
545 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
547 def _test_binop_lhs_value_same_complex(self
, op
):
548 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
550 def _test_binop_lhs_value_same_zero_complex(self
, op
):
551 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
553 def test_bool_op(self
):
554 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
556 def test_int_op(self
):
557 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
559 def test_float_op(self
):
560 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
562 def test_complex_op(self
):
563 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
565 def test_str_op(self
):
566 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
568 def test_eq_none(self
):
569 # Ignore this lint error:
570 # E711 comparison to None should be 'if cond is None:'
571 # since this is what we want to test (even though not good practice).
572 self
.assertFalse(self
._def
== None) # noqa: E711
574 def test_ne_none(self
):
575 # Ignore this lint error:
576 # E711 comparison to None should be 'if cond is not None:'
577 # since this is what we want to test (even though not good practice).
578 self
.assertTrue(self
._def
!= None) # noqa: E711
581 # This is a list of binary operators used for
582 # _inject_numeric_testing_methods().
584 # Each entry is a pair of binary operator name (used as part of the
585 # created testing method's name) and operator function.
593 ('add', operator
.add
),
594 ('radd', lambda a
, b
: operator
.add(b
, a
)),
595 ('and', operator
.and_
),
596 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
597 ('floordiv', operator
.floordiv
),
598 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
599 ('lshift', operator
.lshift
),
600 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
601 ('mod', operator
.mod
),
602 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
603 ('mul', operator
.mul
),
604 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
605 ('or', operator
.or_
),
606 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
607 ('pow', operator
.pow),
608 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
609 ('rshift', operator
.rshift
),
610 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
611 ('sub', operator
.sub
),
612 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
613 ('truediv', operator
.truediv
),
614 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
615 ('xor', operator
.xor
),
616 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
620 # This is a list of unary operators used for
621 # _inject_numeric_testing_methods().
623 # Each entry is a pair of unary operator name (used as part of the
624 # created testing method's name) and operator function.
626 ('neg', operator
.neg
),
627 ('pos', operator
.pos
),
628 ('abs', operator
.abs),
629 ('invert', operator
.invert
),
631 ('round_0', partial(round, ndigits
=0)),
632 ('round_1', partial(round, ndigits
=1)),
633 ('round_2', partial(round, ndigits
=2)),
634 ('round_3', partial(round, ndigits
=3)),
636 ('floor', math
.floor
),
637 ('trunc', math
.trunc
),
641 # This function injects a bunch of testing methods to a numeric
644 # It is meant to be used like this:
646 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
648 # This function injects:
650 # * One testing method for each _TestNumericField._test_binop_*()
651 # method, for each binary operator in the _BINOPS tuple.
653 # * One testing method for each _TestNumericField._test_unaryop*()
654 # method, for each unary operator in the _UNARYOPS tuple.
655 def _inject_numeric_testing_methods(cls
):
656 def test_binop_name(suffix
):
657 return 'test_binop_{}_{}'.format(name
, suffix
)
659 def test_unaryop_name(suffix
):
660 return 'test_unaryop_{}_{}'.format(name
, suffix
)
662 # inject testing methods for each binary operation
663 for name
, binop
in _BINOPS
:
664 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
))
665 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
))
666 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
))
667 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
))
668 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
))
669 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
))
670 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
))
671 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
))
672 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
673 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
674 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
675 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
676 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
677 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
678 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
))
679 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
))
680 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
))
681 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
))
682 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
683 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
684 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
685 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
686 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
))
687 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
))
688 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
))
689 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
))
690 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
))
691 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
))
692 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
693 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
694 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
695 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
696 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
697 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
698 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
))
699 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
))
700 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
701 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
702 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
))
703 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
))
704 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
705 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
706 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
707 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
708 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
709 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
710 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
711 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
712 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
713 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
714 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
))
715 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
716 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
))
717 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
718 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
719 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
720 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
721 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
722 setattr(cls
, test_binop_name('type_complex'), partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
))
723 setattr(cls
, test_binop_name('type_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
))
724 setattr(cls
, test_binop_name('value_complex'), partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
))
725 setattr(cls
, test_binop_name('value_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
))
726 setattr(cls
, test_binop_name('lhs_addr_same_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
))
727 setattr(cls
, test_binop_name('lhs_addr_same_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
))
728 setattr(cls
, test_binop_name('lhs_value_same_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
))
729 setattr(cls
, test_binop_name('lhs_value_same_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
))
731 # inject testing methods for each unary operation
732 for name
, unaryop
in _UNARYOPS
:
733 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
))
734 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
))
735 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
))
736 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
))
739 class _TestIntegerFieldCommon(_TestNumericField
):
740 def test_assign_true(self
):
742 self
._def
.value
= raw
743 self
.assertEqual(self
._def
, raw
)
745 def test_assign_false(self
):
747 self
._def
.value
= raw
748 self
.assertEqual(self
._def
, raw
)
750 def test_assign_pos_int(self
):
752 self
._def
.value
= raw
753 self
.assertEqual(self
._def
, raw
)
755 def test_assign_neg_int(self
):
757 self
._def
.value
= raw
758 self
.assertEqual(self
._def
, raw
)
760 def test_assign_int_field(self
):
762 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
764 self
._def
.value
= field
765 self
.assertEqual(self
._def
, raw
)
767 def test_assign_invalid_type(self
):
768 with self
.assertRaises(TypeError):
769 self
._def
.value
= 'yes'
771 def test_assign_uint(self
):
772 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
773 field
= _create_field(self
._tc
, uint_fc
)
776 self
.assertEqual(field
, raw
)
778 def test_assign_big_uint(self
):
779 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
780 field
= _create_field(self
._tc
, uint_fc
)
781 # Larger than the IEEE 754 double-precision exact representation of
784 field
.value
= (2**53) + 1
785 self
.assertEqual(field
, raw
)
787 def test_assign_uint_invalid_neg(self
):
788 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
789 field
= _create_field(self
._tc
, uint_fc
)
791 with self
.assertRaises(ValueError):
794 def test_str_op(self
):
795 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
798 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
801 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
802 def _create_fc(self
, tc
):
803 return tc
.create_signed_integer_field_class(25)
806 self
._tc
= get_default_trace_class()
807 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
808 self
._field
.value
= 17
809 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
812 self
._def
_new
_value
= -101
815 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
816 def _create_fc(self
, tc
):
817 fc
= tc
.create_signed_enumeration_field_class(32)
818 fc
.map_range('something', 17)
819 fc
.map_range('speaker', 12, 16)
820 fc
.map_range('can', 18, 2540)
821 fc
.map_range('whole range', -(2 ** 31), (2 ** 31) - 1)
822 fc
.map_range('zip', -45, 1001)
826 self
._tc
= get_default_trace_class()
827 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
828 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
831 self
._def
_new
_value
= -101
833 def test_str_op(self
):
834 expected_string_found
= False
837 # Establish all permutations of the three expected matches since
838 # the order in which mappings are enumerated is not explicitly part of
840 for p
in itertools
.permutations(['whole range', 'something',
842 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
844 expected_string_found
= True
847 self
.assertTrue(expected_string_found
)
849 def test_labels(self
):
850 self
._field
.value
= 17
851 labels
= sorted(self
._field
.labels
)
852 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
855 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
856 def _create_fc(self
, tc
):
857 return tc
.create_real_field_class()
860 self
._tc
= get_default_trace_class()
861 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
862 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
863 self
._def
.value
= 52.7
864 self
._def
_value
= 52.7
865 self
._def
_new
_value
= -17.164857
867 def _test_invalid_op(self
, cb
):
868 with self
.assertRaises(TypeError):
871 def test_assign_true(self
):
872 self
._def
.value
= True
873 self
.assertTrue(self
._def
)
875 def test_assign_false(self
):
876 self
._def
.value
= False
877 self
.assertFalse(self
._def
)
879 def test_assign_pos_int(self
):
881 self
._def
.value
= raw
882 self
.assertEqual(self
._def
, float(raw
))
884 def test_assign_neg_int(self
):
886 self
._def
.value
= raw
887 self
.assertEqual(self
._def
, float(raw
))
889 def test_assign_int_field(self
):
890 int_fc
= self
._tc
.create_signed_integer_field_class(32)
891 int_field
= _create_field(self
._tc
, int_fc
)
893 int_field
.value
= raw
894 self
._def
.value
= int_field
895 self
.assertEqual(self
._def
, float(raw
))
897 def test_assign_float(self
):
899 self
._def
.value
= raw
900 self
.assertEqual(self
._def
, raw
)
902 def test_assign_float_field(self
):
903 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
906 self
._def
.value
= field
907 self
.assertEqual(self
._def
, raw
)
909 def test_assign_invalid_type(self
):
910 with self
.assertRaises(TypeError):
911 self
._def
.value
= 'yes'
913 def test_invalid_lshift(self
):
914 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
916 def test_invalid_rshift(self
):
917 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
919 def test_invalid_and(self
):
920 self
._test
_invalid
_op
(lambda: self
._def
& 23)
922 def test_invalid_or(self
):
923 self
._test
_invalid
_op
(lambda: self
._def |
23)
925 def test_invalid_xor(self
):
926 self
._test
_invalid
_op
(lambda: self
._def ^
23)
928 def test_invalid_invert(self
):
929 self
._test
_invalid
_op
(lambda: ~self
._def
)
931 def test_str_op(self
):
932 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
935 _inject_numeric_testing_methods(RealFieldTestCase
)
938 class StringFieldTestCase(unittest
.TestCase
):
940 self
._tc
= get_default_trace_class()
941 self
._def
_value
= 'Hello, World!'
942 self
._def
= _create_string_field(self
._tc
)
943 self
._def
.value
= self
._def
_value
944 self
._def
_new
_value
= 'Yes!'
946 def test_assign_int(self
):
947 with self
.assertRaises(TypeError):
948 self
._def
.value
= 283
950 def test_assign_string_field(self
):
951 field
= _create_string_field(self
._tc
)
954 self
.assertEqual(field
, raw
)
957 self
.assertEqual(self
._def
, self
._def
_value
)
959 def test_not_eq(self
):
960 self
.assertNotEqual(self
._def
, 23)
962 def test_lt_vstring(self
):
963 s1
= _create_string_field(self
._tc
)
965 s2
= _create_string_field(self
._tc
)
967 self
.assertLess(s1
, s2
)
969 def test_lt_string(self
):
970 s1
= _create_string_field(self
._tc
)
972 self
.assertLess(s1
, 'bateau')
974 def test_le_vstring(self
):
975 s1
= _create_string_field(self
._tc
)
977 s2
= _create_string_field(self
._tc
)
979 self
.assertLessEqual(s1
, s2
)
981 def test_le_string(self
):
982 s1
= _create_string_field(self
._tc
)
984 self
.assertLessEqual(s1
, 'bateau')
986 def test_gt_vstring(self
):
987 s1
= _create_string_field(self
._tc
)
989 s2
= _create_string_field(self
._tc
)
991 self
.assertGreater(s2
, s1
)
993 def test_gt_string(self
):
994 s1
= _create_string_field(self
._tc
)
996 self
.assertGreater('bateau', s1
)
998 def test_ge_vstring(self
):
999 s1
= _create_string_field(self
._tc
)
1001 s2
= _create_string_field(self
._tc
)
1003 self
.assertGreaterEqual(s2
, s1
)
1005 def test_ge_string(self
):
1006 s1
= _create_string_field(self
._tc
)
1008 self
.assertGreaterEqual('bateau', s1
)
1010 def test_bool_op(self
):
1011 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1013 def test_str_op(self
):
1014 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1017 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1019 def test_getitem(self
):
1020 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1022 def test_append_str(self
):
1023 to_append
= 'meow meow meow'
1024 self
._def
+= to_append
1025 self
._def
_value
+= to_append
1026 self
.assertEqual(self
._def
, self
._def
_value
)
1028 def test_append_string_field(self
):
1029 field
= _create_string_field(self
._tc
)
1030 to_append
= 'meow meow meow'
1031 field
.value
= to_append
1033 self
._def
_value
+= to_append
1034 self
.assertEqual(self
._def
, self
._def
_value
)
1037 class _TestArrayFieldCommon
:
1038 def _modify_def(self
):
1041 def test_bool_op_true(self
):
1042 self
.assertTrue(self
._def
)
1045 self
.assertEqual(len(self
._def
), 3)
1047 def test_length(self
):
1048 self
.assertEqual(self
._def
.length
, 3)
1050 def test_getitem(self
):
1051 field
= self
._def
[1]
1052 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1053 self
.assertEqual(field
, 1847)
1056 field
= _create_int_array_field(self
._tc
, 3)
1060 self
.assertEqual(self
._def
, field
)
1062 def test_eq_invalid_type(self
):
1063 self
.assertNotEqual(self
._def
, 23)
1065 def test_eq_diff_len(self
):
1066 field
= _create_int_array_field(self
._tc
, 2)
1069 self
.assertNotEqual(self
._def
, field
)
1071 def test_eq_diff_content_same_len(self
):
1072 field
= _create_int_array_field(self
._tc
, 3)
1076 self
.assertNotEqual(self
._def
, field
)
1078 def test_eq_non_sequence_iterable(self
):
1079 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1080 field
= _create_int_array_field(self
._tc
, 3)
1084 self
.assertEqual(field
, list(dct
.keys()))
1085 self
.assertNotEqual(field
, dct
)
1087 def test_setitem(self
):
1089 self
.assertEqual(self
._def
[2], 24)
1091 def test_setitem_int_field(self
):
1092 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1093 int_field
= _create_field(self
._tc
, int_fc
)
1094 int_field
.value
= 19487
1095 self
._def
[1] = int_field
1096 self
.assertEqual(self
._def
[1], 19487)
1098 def test_setitem_non_basic_field(self
):
1099 array_field
= _create_struct_array_field(self
._tc
, 2)
1100 with self
.assertRaises(TypeError):
1103 def test_setitem_none(self
):
1104 with self
.assertRaises(TypeError):
1107 def test_setitem_index_wrong_type(self
):
1108 with self
.assertRaises(TypeError):
1109 self
._def
['yes'] = 23
1111 def test_setitem_index_neg(self
):
1112 with self
.assertRaises(IndexError):
1115 def test_setitem_index_out_of_range(self
):
1116 with self
.assertRaises(IndexError):
1117 self
._def
[len(self
._def
)] = 134679
1119 def test_iter(self
):
1120 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1121 self
.assertEqual(field
, value
)
1123 def test_value_int_field(self
):
1124 values
= [45646, 145, 12145]
1125 self
._def
.value
= values
1126 self
.assertEqual(values
, self
._def
)
1128 def test_value_check_sequence(self
):
1130 with self
.assertRaises(TypeError):
1131 self
._def
.value
= values
1133 def test_value_wrong_type_in_sequence(self
):
1134 values
= [32, 'hello', 11]
1135 with self
.assertRaises(TypeError):
1136 self
._def
.value
= values
1138 def test_value_complex_type(self
):
1139 struct_fc
= self
._tc
.create_structure_field_class()
1140 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1141 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1142 str_fc
= self
._tc
.create_string_field_class()
1143 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1144 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1145 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1146 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1147 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1151 'a_string': 'hello',
1156 'a_string': 'goodbye',
1161 'a_string': 'or not',
1166 array
= stream
.create_packet().context_field
['array_field']
1167 array
.value
= values
1168 self
.assertEqual(values
, array
)
1169 values
[0]['an_int'] = 'a string'
1170 with self
.assertRaises(TypeError):
1171 array
.value
= values
1173 def test_str_op(self
):
1175 expected_string
= '[{}]'.format(', '.join(
1176 [repr(v
) for v
in self
._def
_value
]))
1177 self
.assertEqual(expected_string
, s
)
1180 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1182 self
._tc
= get_default_trace_class()
1183 self
._def
= _create_int_array_field(self
._tc
, 3)
1186 self
._def
[2] = 1948754
1187 self
._def
_value
= [45, 1847, 1948754]
1189 def test_value_wrong_len(self
):
1191 with self
.assertRaises(ValueError):
1192 self
._def
.value
= values
1195 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1197 self
._tc
= get_default_trace_class()
1198 self
._def
= _create_dynamic_array(self
._tc
)
1201 self
._def
[2] = 1948754
1202 self
._def
_value
= [45, 1847, 1948754]
1204 def test_value_resize(self
):
1205 new_values
= [1, 2, 3, 4]
1206 self
._def
.value
= new_values
1207 self
.assertCountEqual(self
._def
, new_values
)
1209 def test_set_length(self
):
1210 self
._def
.length
= 4
1212 self
.assertEqual(len(self
._def
), 4)
1214 def test_set_invalid_length(self
):
1215 with self
.assertRaises(TypeError):
1216 self
._def
.length
= 'cheval'
1219 class StructureFieldTestCase(unittest
.TestCase
):
1220 def _create_fc(self
, tc
):
1221 fc
= tc
.create_structure_field_class()
1222 fc
.append_member('A', self
._fc
0_fn
())
1223 fc
.append_member('B', self
._fc
1_fn
())
1224 fc
.append_member('C', self
._fc
2_fn
())
1225 fc
.append_member('D', self
._fc
3_fn
())
1226 fc
.append_member('E', self
._fc
4_fn
())
1227 fc5
= self
._fc
5_fn
()
1228 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1229 fc
.append_member('F', fc5
)
1233 self
._tc
= get_default_trace_class()
1234 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1235 self
._fc
1_fn
= self
._tc
.create_string_field_class
1236 self
._fc
2_fn
= self
._tc
.create_real_field_class
1237 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1238 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1239 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1240 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1242 self
._fc
= self
._create
_fc
(self
._tc
)
1243 self
._def
= _create_field(self
._tc
, self
._fc
)
1244 self
._def
['A'] = -1872
1245 self
._def
['B'] = 'salut'
1246 self
._def
['C'] = 17.5
1247 self
._def
['D'] = 16497
1249 self
._def
['F'] = {'F_1': 52}
1259 def _modify_def(self
):
1260 self
._def
['B'] = 'hola'
1262 def test_bool_op_true(self
):
1263 self
.assertTrue(self
._def
)
1265 def test_bool_op_false(self
):
1266 field
= self
._def
['E']
1267 self
.assertFalse(field
)
1270 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1272 def test_getitem(self
):
1273 field
= self
._def
['A']
1274 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1275 self
.assertEqual(field
, -1872)
1277 def test_member_at_index_out_of_bounds_after(self
):
1278 with self
.assertRaises(IndexError):
1279 self
._def
.member_at_index(len(self
._def
_value
))
1282 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
, ))
1284 field
['B'] = 'salut'
1288 field
['F'] = {'F_1': 52}
1289 self
.assertEqual(self
._def
, field
)
1291 def test_eq_invalid_type(self
):
1292 self
.assertNotEqual(self
._def
, 23)
1294 def test_eq_diff_len(self
):
1295 fc
= self
._tc
.create_structure_field_class()
1296 fc
.append_member('A', self
._fc
0_fn
())
1297 fc
.append_member('B', self
._fc
1_fn
())
1298 fc
.append_member('C', self
._fc
2_fn
())
1300 field
= _create_field(self
._tc
, fc
)
1302 field
['B'] = 'salut'
1304 self
.assertNotEqual(self
._def
, field
)
1306 def test_eq_diff_keys(self
):
1307 fc
= self
._tc
.create_structure_field_class()
1308 fc
.append_member('U', self
._fc
0_fn
())
1309 fc
.append_member('V', self
._fc
1_fn
())
1310 fc
.append_member('W', self
._fc
2_fn
())
1311 fc
.append_member('X', self
._fc
3_fn
())
1312 fc
.append_member('Y', self
._fc
4_fn
())
1313 fc
.append_member('Z', self
._fc
5_fn
())
1314 field
= _create_field(self
._tc
, fc
)
1316 field
['V'] = "gerry"
1321 self
.assertNotEqual(self
._def
, field
)
1323 def test_eq_diff_content_same_len(self
):
1324 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1326 field
['B'] = 'salut'
1330 field
['F'] = {'F_1': 0}
1331 self
.assertNotEqual(self
._def
, field
)
1333 def test_eq_same_content_diff_keys(self
):
1334 fc
= self
._tc
.create_structure_field_class()
1335 fc
.append_member('A', self
._fc
0_fn
())
1336 fc
.append_member('B', self
._fc
1_fn
())
1337 fc
.append_member('E', self
._fc
2_fn
())
1338 fc
.append_member('D', self
._fc
3_fn
())
1339 fc
.append_member('C', self
._fc
4_fn
())
1340 fc
.append_member('F', self
._fc
5_fn
())
1341 field
= _create_field(self
._tc
, fc
)
1343 field
['B'] = 'salut'
1348 self
.assertNotEqual(self
._def
, field
)
1350 def test_setitem(self
):
1351 self
._def
['C'] = -18.47
1352 self
.assertEqual(self
._def
['C'], -18.47)
1354 def test_setitem_int_field(self
):
1355 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1356 int_field
= _create_field(self
._tc
, int_fc
)
1357 int_field
.value
= 19487
1358 self
._def
['D'] = int_field
1359 self
.assertEqual(self
._def
['D'], 19487)
1361 def test_setitem_non_basic_field(self
):
1362 elem_fc
= self
._tc
.create_structure_field_class()
1363 struct_fc
= self
._tc
.create_structure_field_class()
1364 struct_fc
.append_member('A', elem_fc
)
1365 struct_field
= _create_field(self
._tc
, struct_fc
)
1367 # Will fail on access to .items() of the value
1368 with self
.assertRaises(AttributeError):
1369 struct_field
['A'] = 23
1371 def test_setitem_none(self
):
1372 with self
.assertRaises(TypeError):
1373 self
._def
['C'] = None
1375 def test_setitem_key_wrong_type(self
):
1376 with self
.assertRaises(TypeError):
1379 def test_setitem_wrong_key(self
):
1380 with self
.assertRaises(KeyError):
1381 self
._def
['hi'] = 134679
1383 def test_member_at_index(self
):
1384 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1386 def test_iter(self
):
1396 for vkey
, vval
in self
._def
.items():
1397 val
= orig_values
[vkey
]
1398 self
.assertEqual(vval
, val
)
1400 def test_value(self
):
1409 self
.assertEqual(self
._def
, orig_values
)
1411 def test_set_value(self
):
1412 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1413 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1414 str_fc
= self
._tc
.create_string_field_class()
1415 struct_fc
= self
._tc
.create_structure_field_class()
1416 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1417 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1418 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1421 'a_string': 'hello',
1425 struct
= _create_field(self
._tc
, struct_fc
)
1426 struct
.value
= values
1427 self
.assertEqual(values
, struct
)
1429 bad_type_values
= copy
.deepcopy(values
)
1430 bad_type_values
['an_int'] = 'a string'
1431 with self
.assertRaises(TypeError):
1432 struct
.value
= bad_type_values
1434 unknown_key_values
= copy
.deepcopy(values
)
1435 unknown_key_values
['unknown_key'] = 16546
1436 with self
.assertRaises(KeyError):
1437 struct
.value
= unknown_key_values
1439 def test_str_op(self
):
1440 expected_string_found
= False
1442 # Establish all permutations of the three expected matches since
1443 # the order in which mappings are enumerated is not explicitly part of
1445 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1446 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1447 candidate
= '{{{}}}'.format(', '.join(items
))
1449 expected_string_found
= True
1452 self
.assertTrue(expected_string_found
)
1455 class VariantFieldTestCase(unittest
.TestCase
):
1456 def _create_fc(self
, tc
):
1457 selector_fc
= tc
.create_signed_enumeration_field_class(field_value_range
=32)
1458 selector_fc
.map_range('corner', 23)
1459 selector_fc
.map_range('zoom', 17, 20)
1460 selector_fc
.map_range('mellotron', 1001)
1461 selector_fc
.map_range('giorgio', 2000, 3000)
1463 ft0
= tc
.create_signed_integer_field_class(32)
1464 ft1
= tc
.create_string_field_class()
1465 ft2
= tc
.create_real_field_class()
1466 ft3
= tc
.create_signed_integer_field_class(17)
1468 fc
= tc
.create_variant_field_class()
1469 fc
.append_option('corner', ft0
)
1470 fc
.append_option('zoom', ft1
)
1471 fc
.append_option('mellotron', ft2
)
1472 fc
.append_option('giorgio', ft3
)
1473 fc
.selector_field_class
= selector_fc
1475 top_fc
= tc
.create_structure_field_class()
1476 top_fc
.append_member('selector_field', selector_fc
)
1477 top_fc
.append_member('variant_field', fc
)
1481 self
._tc
= get_default_trace_class()
1482 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1483 self
._def
= fld
['variant_field']
1485 def test_bool_op(self
):
1486 self
._def
.selected_option_index
= 2
1487 self
._def
.value
= -17.34
1488 with self
.assertRaises(NotImplementedError):
1491 def test_selected_option_index(self
):
1492 self
._def
.selected_option_index
= 2
1493 self
.assertEqual(self
._def
.selected_option_index
, 2)
1495 def test_selected_option(self
):
1496 self
._def
.selected_option_index
= 2
1497 self
._def
.value
= -17.34
1498 self
.assertEqual(self
._def
.selected_option
, -17.34)
1500 self
._def
.selected_option_index
= 3
1501 self
._def
.value
= 1921
1502 self
.assertEqual(self
._def
.selected_option
, 1921)
1505 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1506 field
= field
['variant_field']
1507 field
.selected_option_index
= 0
1509 self
._def
.selected_option_index
= 0
1510 self
._def
.value
= 1774
1511 self
.assertEqual(self
._def
, field
)
1513 def test_eq_invalid_type(self
):
1514 self
._def
.selected_option_index
= 1
1515 self
._def
.value
= 'gerry'
1516 self
.assertNotEqual(self
._def
, 23)
1518 def test_str_op_int(self
):
1519 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1520 field
= field
['variant_field']
1521 field
.selected_option_index
= 0
1523 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1524 other_field
= other_field
['variant_field']
1525 other_field
.selected_option_index
= 0
1526 other_field
.value
= 1774
1527 self
.assertEqual(str(field
), str(other_field
))
1529 def test_str_op_str(self
):
1530 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1531 field
= field
['variant_field']
1532 field
.selected_option_index
= 1
1533 field
.value
= 'un beau grand bateau'
1534 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1535 other_field
= other_field
['variant_field']
1536 other_field
.selected_option_index
= 1
1537 other_field
.value
= 'un beau grand bateau'
1538 self
.assertEqual(str(field
), str(other_field
))
1540 def test_str_op_float(self
):
1541 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1542 field
= field
['variant_field']
1543 field
.selected_option_index
= 2
1544 field
.value
= 14.4245
1545 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1546 other_field
= other_field
['variant_field']
1547 other_field
.selected_option_index
= 2
1548 other_field
.value
= 14.4245
1549 self
.assertEqual(str(field
), str(other_field
))