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 # Base class for numeric field test cases.
125 # To be compatible with this base class, a derived class must, in its
128 # * Set `self._def` to a field object with an arbitrary value.
129 # * Set `self._def_value` to the equivalent value of `self._def`.
130 class _TestNumericField
:
131 # Tries the binary operation `op`:
133 # 1. Between `self._def`, which is a field object, and `rhs`.
134 # 2. Between `self._def_value`, which is the raw value of
135 # `self._def`, and `rhs`.
137 # Returns the results of 1. and 2.
139 # If there's an exception while performing 1. or 2., asserts that
140 # both operations raised exceptions, that both exceptions have the
141 # same type, and returns `None` for both results.
142 def _binop(self
, op
, rhs
):
147 # try with field object
149 r
= op(self
._def
, rhs
)
150 except Exception as e
:
155 rv
= op(self
._def
_value
, comp_value
)
156 except Exception as e
:
159 if type_rexc
is not None or type_rvexc
is not None:
160 # at least one of the operations raised an exception: in
161 # this case both operations should have raised the same
162 # type of exception (division by zero, bit shift with a
163 # floating point number operand, etc.)
164 self
.assertIs(type_rexc
, type_rvexc
)
169 # Tries the unary operation `op`:
171 # 1. On `self._def`, which is a field object.
172 # 2. On `self._def_value`, which is the value of `self._def`.
174 # Returns the results of 1. and 2.
176 # If there's an exception while performing 1. or 2., asserts that
177 # both operations raised exceptions, that both exceptions have the
178 # same type, and returns `None` for both results.
179 def _unaryop(self
, op
):
183 # try with field object
186 except Exception as e
:
191 rv
= op(self
._def
_value
)
192 except Exception as e
:
195 if type_rexc
is not None or type_rvexc
is not None:
196 # at least one of the operations raised an exception: in
197 # this case both operations should have raised the same
198 # type of exception (division by zero, bit shift with a
199 # floating point number operand, etc.)
200 self
.assertIs(type_rexc
, type_rvexc
)
205 # Tests that the unary operation `op` gives results with the same
206 # type for both `self._def` and `self._def_value`.
207 def _test_unaryop_type(self
, op
):
208 r
, rv
= self
._unaryop
(op
)
213 self
.assertIsInstance(r
, type(rv
))
215 # Tests that the unary operation `op` gives results with the same
216 # value for both `self._def` and `self._def_value`. This uses the
217 # __eq__() operator of `self._def`.
218 def _test_unaryop_value(self
, op
):
219 r
, rv
= self
._unaryop
(op
)
224 self
.assertEqual(r
, rv
)
226 # Tests that the unary operation `op`, when applied to `self._def`,
227 # does not change its underlying BT object address.
228 def _test_unaryop_addr_same(self
, op
):
229 addr_before
= self
._def
.addr
231 self
.assertEqual(self
._def
.addr
, addr_before
)
233 # Tests that the unary operation `op`, when applied to `self._def`,
234 # does not change its value.
235 def _test_unaryop_value_same(self
, op
):
236 value_before
= copy
.copy(self
._def
_value
)
238 self
.assertEqual(self
._def
, value_before
)
240 # Tests that the binary operation `op` gives results with the same
241 # type for both `self._def` and `self._def_value`.
242 def _test_binop_type(self
, op
, rhs
):
243 r
, rv
= self
._binop
(op
, rhs
)
248 if op
in _COMP_BINOPS
:
249 # __eq__() and __ne__() always return a 'bool' object
250 self
.assertIsInstance(r
, bool)
252 self
.assertIsInstance(r
, type(rv
))
254 # Tests that the binary operation `op` gives results with the same
255 # value for both `self._def` and `self._def_value`. This uses the
256 # __eq__() operator of `self._def`.
257 def _test_binop_value(self
, op
, rhs
):
258 r
, rv
= self
._binop
(op
, rhs
)
263 self
.assertEqual(r
, rv
)
265 # Tests that the binary operation `op`, when applied to `self._def`,
266 # does not change its underlying BT object address.
267 def _test_binop_lhs_addr_same(self
, op
, rhs
):
268 addr_before
= self
._def
.addr
269 r
, rv
= self
._binop
(op
, rhs
)
270 self
.assertEqual(self
._def
.addr
, addr_before
)
272 # Tests that the binary operation `op`, when applied to `self._def`,
273 # does not change its value.
274 @unittest.skip('copy is not implemented')
275 def _test_binop_lhs_value_same(self
, op
, rhs
):
276 value_before
= copy
.copy(self
._def
)
277 r
, rv
= self
._binop
(op
, rhs
)
278 self
.assertEqual(self
._def
, value_before
)
280 # The methods below which take the `test_cb` and/or `op` parameters
281 # are meant to be used with one of the _test_binop_*() functions
282 # above as `test_cb` and a binary operator function as `op`.
286 # self._test_binop_rhs_pos_int(self._test_binop_value,
289 # This tests that a numeric field object added to a positive integer
290 # value gives a result with the expected value.
292 # `vint` and `vfloat` mean a signed integer value object and a real
295 def _test_binop_invalid_unknown(self
, op
):
296 if op
in _COMP_BINOPS
:
297 self
.skipTest('not testing')
302 with self
.assertRaises(TypeError):
305 def _test_binop_invalid_none(self
, op
):
306 if op
in _COMP_BINOPS
:
307 self
.skipTest('not testing')
309 with self
.assertRaises(TypeError):
312 def _test_binop_rhs_false(self
, test_cb
, op
):
315 def _test_binop_rhs_true(self
, test_cb
, op
):
318 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
321 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
324 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
327 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
328 test_cb(op
, bt2
.create_value(2))
330 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
331 test_cb(op
, bt2
.create_value(-23))
333 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
334 test_cb(op
, bt2
.create_value(0))
336 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
339 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
342 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
345 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
346 test_cb(op
, bt2
.create_value(2.2))
348 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
349 test_cb(op
, bt2
.create_value(-23.4))
351 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
352 test_cb(op
, bt2
.create_value(0.0))
354 def _test_binop_rhs_complex(self
, test_cb
, op
):
357 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
360 def _test_binop_type_false(self
, op
):
361 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
363 def _test_binop_type_true(self
, op
):
364 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
366 def _test_binop_type_pos_int(self
, op
):
367 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
369 def _test_binop_type_neg_int(self
, op
):
370 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
372 def _test_binop_type_zero_int(self
, op
):
373 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
375 def _test_binop_type_pos_vint(self
, op
):
376 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
378 def _test_binop_type_neg_vint(self
, op
):
379 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
381 def _test_binop_type_zero_vint(self
, op
):
382 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
384 def _test_binop_type_pos_float(self
, op
):
385 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
387 def _test_binop_type_neg_float(self
, op
):
388 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
390 def _test_binop_type_zero_float(self
, op
):
391 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
393 def _test_binop_type_pos_vfloat(self
, op
):
394 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
396 def _test_binop_type_neg_vfloat(self
, op
):
397 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
399 def _test_binop_type_zero_vfloat(self
, op
):
400 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
402 def _test_binop_type_complex(self
, op
):
403 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
405 def _test_binop_type_zero_complex(self
, op
):
406 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
408 def _test_binop_value_false(self
, op
):
409 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
411 def _test_binop_value_true(self
, op
):
412 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
414 def _test_binop_value_pos_int(self
, op
):
415 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
417 def _test_binop_value_neg_int(self
, op
):
418 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
420 def _test_binop_value_zero_int(self
, op
):
421 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
423 def _test_binop_value_pos_vint(self
, op
):
424 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
426 def _test_binop_value_neg_vint(self
, op
):
427 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
429 def _test_binop_value_zero_vint(self
, op
):
430 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
432 def _test_binop_value_pos_float(self
, op
):
433 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
435 def _test_binop_value_neg_float(self
, op
):
436 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
438 def _test_binop_value_zero_float(self
, op
):
439 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
441 def _test_binop_value_pos_vfloat(self
, op
):
442 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
444 def _test_binop_value_neg_vfloat(self
, op
):
445 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
447 def _test_binop_value_zero_vfloat(self
, op
):
448 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
450 def _test_binop_value_complex(self
, op
):
451 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
453 def _test_binop_value_zero_complex(self
, op
):
454 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
456 def _test_binop_lhs_addr_same_false(self
, op
):
457 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
459 def _test_binop_lhs_addr_same_true(self
, op
):
460 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
462 def _test_binop_lhs_addr_same_pos_int(self
, op
):
463 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
465 def _test_binop_lhs_addr_same_neg_int(self
, op
):
466 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
468 def _test_binop_lhs_addr_same_zero_int(self
, op
):
469 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
471 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
472 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
474 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
475 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
477 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
478 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
480 def _test_binop_lhs_addr_same_pos_float(self
, op
):
481 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
483 def _test_binop_lhs_addr_same_neg_float(self
, op
):
484 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
486 def _test_binop_lhs_addr_same_zero_float(self
, op
):
487 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
489 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
490 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
492 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
493 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
495 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
496 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
498 def _test_binop_lhs_addr_same_complex(self
, op
):
499 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
501 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
502 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
504 def _test_binop_lhs_value_same_false(self
, op
):
505 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
507 def _test_binop_lhs_value_same_true(self
, op
):
508 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
510 def _test_binop_lhs_value_same_pos_int(self
, op
):
511 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
513 def _test_binop_lhs_value_same_neg_int(self
, op
):
514 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
516 def _test_binop_lhs_value_same_zero_int(self
, op
):
517 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
519 def _test_binop_lhs_value_same_pos_vint(self
, op
):
520 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
522 def _test_binop_lhs_value_same_neg_vint(self
, op
):
523 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
525 def _test_binop_lhs_value_same_zero_vint(self
, op
):
526 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
528 def _test_binop_lhs_value_same_pos_float(self
, op
):
529 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
531 def _test_binop_lhs_value_same_neg_float(self
, op
):
532 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
534 def _test_binop_lhs_value_same_zero_float(self
, op
):
535 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
537 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
538 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
540 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
541 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
543 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
544 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
546 def _test_binop_lhs_value_same_complex(self
, op
):
547 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
549 def _test_binop_lhs_value_same_zero_complex(self
, op
):
550 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
552 def test_bool_op(self
):
553 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
555 def test_int_op(self
):
556 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
558 def test_float_op(self
):
559 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
561 def test_complex_op(self
):
562 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
564 def test_str_op(self
):
565 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
567 def test_eq_none(self
):
568 # Ignore this lint error:
569 # E711 comparison to None should be 'if cond is None:'
570 # since this is what we want to test (even though not good practice).
571 self
.assertFalse(self
._def
== None) # noqa: E711
573 def test_ne_none(self
):
574 # Ignore this lint error:
575 # E711 comparison to None should be 'if cond is not None:'
576 # since this is what we want to test (even though not good practice).
577 self
.assertTrue(self
._def
!= None) # noqa: E711
580 # This is a list of binary operators used for
581 # _inject_numeric_testing_methods().
583 # Each entry is a pair of binary operator name (used as part of the
584 # created testing method's name) and operator function.
592 ('add', operator
.add
),
593 ('radd', lambda a
, b
: operator
.add(b
, a
)),
594 ('and', operator
.and_
),
595 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
596 ('floordiv', operator
.floordiv
),
597 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
598 ('lshift', operator
.lshift
),
599 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
600 ('mod', operator
.mod
),
601 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
602 ('mul', operator
.mul
),
603 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
604 ('or', operator
.or_
),
605 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
606 ('pow', operator
.pow),
607 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
608 ('rshift', operator
.rshift
),
609 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
610 ('sub', operator
.sub
),
611 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
612 ('truediv', operator
.truediv
),
613 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
614 ('xor', operator
.xor
),
615 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
619 # This is a list of unary operators used for
620 # _inject_numeric_testing_methods().
622 # Each entry is a pair of unary operator name (used as part of the
623 # created testing method's name) and operator function.
625 ('neg', operator
.neg
),
626 ('pos', operator
.pos
),
627 ('abs', operator
.abs),
628 ('invert', operator
.invert
),
630 ('round_0', partial(round, ndigits
=0)),
631 ('round_1', partial(round, ndigits
=1)),
632 ('round_2', partial(round, ndigits
=2)),
633 ('round_3', partial(round, ndigits
=3)),
635 ('floor', math
.floor
),
636 ('trunc', math
.trunc
),
640 # This function injects a bunch of testing methods to a numeric
643 # It is meant to be used like this:
645 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
647 # This function injects:
649 # * One testing method for each _TestNumericField._test_binop_*()
650 # method, for each binary operator in the _BINOPS tuple.
652 # * One testing method for each _TestNumericField._test_unaryop*()
653 # method, for each unary operator in the _UNARYOPS tuple.
654 def _inject_numeric_testing_methods(cls
):
655 def test_binop_name(suffix
):
656 return 'test_binop_{}_{}'.format(name
, suffix
)
658 def test_unaryop_name(suffix
):
659 return 'test_unaryop_{}_{}'.format(name
, suffix
)
661 # inject testing methods for each binary operation
662 for name
, binop
in _BINOPS
:
663 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
))
664 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
))
665 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
))
666 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
))
667 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
))
668 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
))
669 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
))
670 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
))
671 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
672 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
673 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
674 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
675 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
676 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
677 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
))
678 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
))
679 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
))
680 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
))
681 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
682 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
683 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
684 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
685 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
))
686 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
))
687 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
))
688 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
))
689 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
))
690 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
))
691 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
692 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
693 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
694 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
695 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
696 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
697 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
))
698 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
))
699 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
700 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
701 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
))
702 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
))
703 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
704 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
705 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
706 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
707 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
708 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
709 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
710 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
711 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
712 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
713 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
))
714 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
715 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
))
716 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
717 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
718 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
719 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
720 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
721 setattr(cls
, test_binop_name('type_complex'), partialmethod(_TestNumericField
._test
_binop
_type
_complex
, op
=binop
))
722 setattr(cls
, test_binop_name('type_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_complex
, op
=binop
))
723 setattr(cls
, test_binop_name('value_complex'), partialmethod(_TestNumericField
._test
_binop
_value
_complex
, op
=binop
))
724 setattr(cls
, test_binop_name('value_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_complex
, op
=binop
))
725 setattr(cls
, test_binop_name('lhs_addr_same_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
))
726 setattr(cls
, test_binop_name('lhs_addr_same_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
))
727 setattr(cls
, test_binop_name('lhs_value_same_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
))
728 setattr(cls
, test_binop_name('lhs_value_same_zero_complex'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
))
730 # inject testing methods for each unary operation
731 for name
, unaryop
in _UNARYOPS
:
732 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
))
733 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
))
734 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
))
735 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
))
738 class _TestIntegerFieldCommon(_TestNumericField
):
739 def test_assign_true(self
):
741 self
._def
.value
= raw
742 self
.assertEqual(self
._def
, raw
)
744 def test_assign_false(self
):
746 self
._def
.value
= raw
747 self
.assertEqual(self
._def
, raw
)
749 def test_assign_pos_int(self
):
751 self
._def
.value
= raw
752 self
.assertEqual(self
._def
, raw
)
754 def test_assign_neg_int(self
):
756 self
._def
.value
= raw
757 self
.assertEqual(self
._def
, raw
)
759 def test_assign_int_field(self
):
761 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
763 self
._def
.value
= field
764 self
.assertEqual(self
._def
, raw
)
766 def test_assign_invalid_type(self
):
767 with self
.assertRaises(TypeError):
768 self
._def
.value
= 'yes'
770 def test_assign_uint(self
):
771 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
772 field
= _create_field(self
._tc
, uint_fc
)
775 self
.assertEqual(field
, raw
)
777 def test_assign_big_uint(self
):
778 uint_fc
= self
._tc
.create_unsigned_integer_field_class(64)
779 field
= _create_field(self
._tc
, uint_fc
)
780 # Larger than the IEEE 754 double-precision exact representation of
783 field
.value
= (2**53) + 1
784 self
.assertEqual(field
, raw
)
786 def test_assign_uint_invalid_neg(self
):
787 uint_fc
= self
._tc
.create_unsigned_integer_field_class(32)
788 field
= _create_field(self
._tc
, uint_fc
)
790 with self
.assertRaises(ValueError):
793 def test_str_op(self
):
794 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
797 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
800 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
801 def _create_fc(self
, tc
):
802 return tc
.create_signed_integer_field_class(25)
805 self
._tc
= get_default_trace_class()
806 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
807 self
._field
.value
= 17
808 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
811 self
._def
_new
_value
= -101
814 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
815 def _create_fc(self
, tc
):
816 fc
= tc
.create_signed_enumeration_field_class(32)
817 fc
.map_range('something', 17)
818 fc
.map_range('speaker', 12, 16)
819 fc
.map_range('can', 18, 2540)
820 fc
.map_range('whole range', -(2 ** 31), (2 ** 31) - 1)
821 fc
.map_range('zip', -45, 1001)
825 self
._tc
= get_default_trace_class()
826 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
827 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
830 self
._def
_new
_value
= -101
832 def test_str_op(self
):
833 expected_string_found
= False
836 # Establish all permutations of the three expected matches since
837 # the order in which mappings are enumerated is not explicitly part of
839 for p
in itertools
.permutations(['whole range', 'something',
841 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
843 expected_string_found
= True
846 self
.assertTrue(expected_string_found
)
848 def test_labels(self
):
849 self
._field
.value
= 17
850 labels
= sorted(self
._field
.labels
)
851 self
.assertEqual(labels
, ['something', 'whole range', 'zip'])
854 class RealFieldTestCase(_TestNumericField
, unittest
.TestCase
):
855 def _create_fc(self
, tc
):
856 return tc
.create_real_field_class()
859 self
._tc
= get_default_trace_class()
860 self
._field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
861 self
._def
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
862 self
._def
.value
= 52.7
863 self
._def
_value
= 52.7
864 self
._def
_new
_value
= -17.164857
866 def _test_invalid_op(self
, cb
):
867 with self
.assertRaises(TypeError):
870 def test_assign_true(self
):
871 self
._def
.value
= True
872 self
.assertTrue(self
._def
)
874 def test_assign_false(self
):
875 self
._def
.value
= False
876 self
.assertFalse(self
._def
)
878 def test_assign_pos_int(self
):
880 self
._def
.value
= raw
881 self
.assertEqual(self
._def
, float(raw
))
883 def test_assign_neg_int(self
):
885 self
._def
.value
= raw
886 self
.assertEqual(self
._def
, float(raw
))
888 def test_assign_int_field(self
):
889 int_fc
= self
._tc
.create_signed_integer_field_class(32)
890 int_field
= _create_field(self
._tc
, int_fc
)
892 int_field
.value
= raw
893 self
._def
.value
= int_field
894 self
.assertEqual(self
._def
, float(raw
))
896 def test_assign_float(self
):
898 self
._def
.value
= raw
899 self
.assertEqual(self
._def
, raw
)
901 def test_assign_float_field(self
):
902 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
905 self
._def
.value
= field
906 self
.assertEqual(self
._def
, raw
)
908 def test_assign_invalid_type(self
):
909 with self
.assertRaises(TypeError):
910 self
._def
.value
= 'yes'
912 def test_invalid_lshift(self
):
913 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
915 def test_invalid_rshift(self
):
916 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
918 def test_invalid_and(self
):
919 self
._test
_invalid
_op
(lambda: self
._def
& 23)
921 def test_invalid_or(self
):
922 self
._test
_invalid
_op
(lambda: self
._def |
23)
924 def test_invalid_xor(self
):
925 self
._test
_invalid
_op
(lambda: self
._def ^
23)
927 def test_invalid_invert(self
):
928 self
._test
_invalid
_op
(lambda: ~self
._def
)
930 def test_str_op(self
):
931 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
934 _inject_numeric_testing_methods(RealFieldTestCase
)
937 class StringFieldTestCase(unittest
.TestCase
):
939 self
._tc
= get_default_trace_class()
940 self
._def
_value
= 'Hello, World!'
941 self
._def
= _create_string_field(self
._tc
)
942 self
._def
.value
= self
._def
_value
943 self
._def
_new
_value
= 'Yes!'
945 def test_assign_int(self
):
946 with self
.assertRaises(TypeError):
947 self
._def
.value
= 283
949 def test_assign_string_field(self
):
950 field
= _create_string_field(self
._tc
)
953 self
.assertEqual(field
, raw
)
956 self
.assertEqual(self
._def
, self
._def
_value
)
958 def test_not_eq(self
):
959 self
.assertNotEqual(self
._def
, 23)
961 def test_lt_vstring(self
):
962 s1
= _create_string_field(self
._tc
)
964 s2
= _create_string_field(self
._tc
)
966 self
.assertLess(s1
, s2
)
968 def test_lt_string(self
):
969 s1
= _create_string_field(self
._tc
)
971 self
.assertLess(s1
, 'bateau')
973 def test_le_vstring(self
):
974 s1
= _create_string_field(self
._tc
)
976 s2
= _create_string_field(self
._tc
)
978 self
.assertLessEqual(s1
, s2
)
980 def test_le_string(self
):
981 s1
= _create_string_field(self
._tc
)
983 self
.assertLessEqual(s1
, 'bateau')
985 def test_gt_vstring(self
):
986 s1
= _create_string_field(self
._tc
)
988 s2
= _create_string_field(self
._tc
)
990 self
.assertGreater(s2
, s1
)
992 def test_gt_string(self
):
993 s1
= _create_string_field(self
._tc
)
995 self
.assertGreater('bateau', s1
)
997 def test_ge_vstring(self
):
998 s1
= _create_string_field(self
._tc
)
1000 s2
= _create_string_field(self
._tc
)
1002 self
.assertGreaterEqual(s2
, s1
)
1004 def test_ge_string(self
):
1005 s1
= _create_string_field(self
._tc
)
1007 self
.assertGreaterEqual('bateau', s1
)
1009 def test_bool_op(self
):
1010 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1012 def test_str_op(self
):
1013 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1016 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1018 def test_getitem(self
):
1019 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1021 def test_append_str(self
):
1022 to_append
= 'meow meow meow'
1023 self
._def
+= to_append
1024 self
._def
_value
+= to_append
1025 self
.assertEqual(self
._def
, self
._def
_value
)
1027 def test_append_string_field(self
):
1028 field
= _create_string_field(self
._tc
)
1029 to_append
= 'meow meow meow'
1030 field
.value
= to_append
1032 self
._def
_value
+= to_append
1033 self
.assertEqual(self
._def
, self
._def
_value
)
1036 class _TestArrayFieldCommon
:
1037 def _modify_def(self
):
1040 def test_bool_op_true(self
):
1041 self
.assertTrue(self
._def
)
1044 self
.assertEqual(len(self
._def
), 3)
1046 def test_length(self
):
1047 self
.assertEqual(self
._def
.length
, 3)
1049 def test_getitem(self
):
1050 field
= self
._def
[1]
1051 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1052 self
.assertEqual(field
, 1847)
1055 field
= _create_int_array_field(self
._tc
, 3)
1059 self
.assertEqual(self
._def
, field
)
1061 def test_eq_invalid_type(self
):
1062 self
.assertNotEqual(self
._def
, 23)
1064 def test_eq_diff_len(self
):
1065 field
= _create_int_array_field(self
._tc
, 2)
1068 self
.assertNotEqual(self
._def
, field
)
1070 def test_eq_diff_content_same_len(self
):
1071 field
= _create_int_array_field(self
._tc
, 3)
1075 self
.assertNotEqual(self
._def
, field
)
1077 def test_setitem(self
):
1079 self
.assertEqual(self
._def
[2], 24)
1081 def test_setitem_int_field(self
):
1082 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1083 int_field
= _create_field(self
._tc
, int_fc
)
1084 int_field
.value
= 19487
1085 self
._def
[1] = int_field
1086 self
.assertEqual(self
._def
[1], 19487)
1088 def test_setitem_non_basic_field(self
):
1089 array_field
= _create_struct_array_field(self
._tc
, 2)
1090 with self
.assertRaises(TypeError):
1093 def test_setitem_none(self
):
1094 with self
.assertRaises(TypeError):
1097 def test_setitem_index_wrong_type(self
):
1098 with self
.assertRaises(TypeError):
1099 self
._def
['yes'] = 23
1101 def test_setitem_index_neg(self
):
1102 with self
.assertRaises(IndexError):
1105 def test_setitem_index_out_of_range(self
):
1106 with self
.assertRaises(IndexError):
1107 self
._def
[len(self
._def
)] = 134679
1109 def test_iter(self
):
1110 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1111 self
.assertEqual(field
, value
)
1113 def test_value_int_field(self
):
1114 values
= [45646, 145, 12145]
1115 self
._def
.value
= values
1116 self
.assertEqual(values
, self
._def
)
1118 def test_value_check_sequence(self
):
1120 with self
.assertRaises(TypeError):
1121 self
._def
.value
= values
1123 def test_value_wrong_type_in_sequence(self
):
1124 values
= [32, 'hello', 11]
1125 with self
.assertRaises(TypeError):
1126 self
._def
.value
= values
1128 def test_value_complex_type(self
):
1129 struct_fc
= self
._tc
.create_structure_field_class()
1130 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1131 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1132 str_fc
= self
._tc
.create_string_field_class()
1133 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1134 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1135 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1136 array_fc
= self
._tc
.create_static_array_field_class(struct_fc
, 3)
1137 stream
= _create_stream(self
._tc
, [('array_field', array_fc
)])
1141 'a_string': 'hello',
1146 'a_string': 'goodbye',
1151 'a_string': 'or not',
1156 array
= stream
.create_packet().context_field
['array_field']
1157 array
.value
= values
1158 self
.assertEqual(values
, array
)
1159 values
[0]['an_int'] = 'a string'
1160 with self
.assertRaises(TypeError):
1161 array
.value
= values
1163 def test_str_op(self
):
1165 expected_string
= '[{}]'.format(', '.join(
1166 [repr(v
) for v
in self
._def
_value
]))
1167 self
.assertEqual(expected_string
, s
)
1170 class StaticArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1172 self
._tc
= get_default_trace_class()
1173 self
._def
= _create_int_array_field(self
._tc
, 3)
1176 self
._def
[2] = 1948754
1177 self
._def
_value
= [45, 1847, 1948754]
1179 def test_value_wrong_len(self
):
1181 with self
.assertRaises(ValueError):
1182 self
._def
.value
= values
1185 class DynamicArrayFieldTestCase(_TestArrayFieldCommon
, unittest
.TestCase
):
1187 self
._tc
= get_default_trace_class()
1188 self
._def
= _create_dynamic_array(self
._tc
)
1191 self
._def
[2] = 1948754
1192 self
._def
_value
= [45, 1847, 1948754]
1194 def test_value_resize(self
):
1195 new_values
= [1, 2, 3, 4]
1196 self
._def
.value
= new_values
1197 self
.assertCountEqual(self
._def
, new_values
)
1199 def test_set_length(self
):
1200 self
._def
.length
= 4
1202 self
.assertEqual(len(self
._def
), 4)
1204 def test_set_invalid_length(self
):
1205 with self
.assertRaises(TypeError):
1206 self
._def
.length
= 'cheval'
1209 class StructureFieldTestCase(unittest
.TestCase
):
1210 def _create_fc(self
, tc
):
1211 fc
= tc
.create_structure_field_class()
1212 fc
.append_member('A', self
._fc
0_fn
())
1213 fc
.append_member('B', self
._fc
1_fn
())
1214 fc
.append_member('C', self
._fc
2_fn
())
1215 fc
.append_member('D', self
._fc
3_fn
())
1216 fc
.append_member('E', self
._fc
4_fn
())
1217 fc5
= self
._fc
5_fn
()
1218 fc5
.append_member('F_1', self
._fc
5_inner
_fn
())
1219 fc
.append_member('F', fc5
)
1223 self
._tc
= get_default_trace_class()
1224 self
._fc
0_fn
= self
._tc
.create_signed_integer_field_class
1225 self
._fc
1_fn
= self
._tc
.create_string_field_class
1226 self
._fc
2_fn
= self
._tc
.create_real_field_class
1227 self
._fc
3_fn
= self
._tc
.create_signed_integer_field_class
1228 self
._fc
4_fn
= self
._tc
.create_structure_field_class
1229 self
._fc
5_fn
= self
._tc
.create_structure_field_class
1230 self
._fc
5_inner
_fn
= self
._tc
.create_signed_integer_field_class
1232 self
._fc
= self
._create
_fc
(self
._tc
)
1233 self
._def
= _create_field(self
._tc
, self
._fc
)
1234 self
._def
['A'] = -1872
1235 self
._def
['B'] = 'salut'
1236 self
._def
['C'] = 17.5
1237 self
._def
['D'] = 16497
1239 self
._def
['F'] = {'F_1': 52}
1249 def _modify_def(self
):
1250 self
._def
['B'] = 'hola'
1252 def test_bool_op_true(self
):
1253 self
.assertTrue(self
._def
)
1255 def test_bool_op_false(self
):
1256 field
= self
._def
['E']
1257 self
.assertFalse(field
)
1260 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1262 def test_getitem(self
):
1263 field
= self
._def
['A']
1264 self
.assertIs(type(field
), bt2
.field
._SignedIntegerField
)
1265 self
.assertEqual(field
, -1872)
1267 def test_member_at_index_out_of_bounds_after(self
):
1268 with self
.assertRaises(IndexError):
1269 self
._def
.member_at_index(len(self
._def
_value
))
1272 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
, ))
1274 field
['B'] = 'salut'
1278 field
['F'] = {'F_1': 52}
1279 self
.assertEqual(self
._def
, field
)
1281 def test_eq_invalid_type(self
):
1282 self
.assertNotEqual(self
._def
, 23)
1284 def test_eq_diff_len(self
):
1285 fc
= self
._tc
.create_structure_field_class()
1286 fc
.append_member('A', self
._fc
0_fn
())
1287 fc
.append_member('B', self
._fc
1_fn
())
1288 fc
.append_member('C', self
._fc
2_fn
())
1290 field
= _create_field(self
._tc
, fc
)
1292 field
['B'] = 'salut'
1294 self
.assertNotEqual(self
._def
, field
)
1296 def test_eq_diff_keys(self
):
1297 fc
= self
._tc
.create_structure_field_class()
1298 fc
.append_member('U', self
._fc
0_fn
())
1299 fc
.append_member('V', self
._fc
1_fn
())
1300 fc
.append_member('W', self
._fc
2_fn
())
1301 fc
.append_member('X', self
._fc
3_fn
())
1302 fc
.append_member('Y', self
._fc
4_fn
())
1303 fc
.append_member('Z', self
._fc
5_fn
())
1304 field
= _create_field(self
._tc
, fc
)
1306 field
['V'] = "gerry"
1311 self
.assertNotEqual(self
._def
, field
)
1313 def test_eq_diff_content_same_len(self
):
1314 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1316 field
['B'] = 'salut'
1320 field
['F'] = {'F_1': 0}
1321 self
.assertNotEqual(self
._def
, field
)
1323 def test_eq_same_content_diff_keys(self
):
1324 fc
= self
._tc
.create_structure_field_class()
1325 fc
.append_member('A', self
._fc
0_fn
())
1326 fc
.append_member('B', self
._fc
1_fn
())
1327 fc
.append_member('E', self
._fc
2_fn
())
1328 fc
.append_member('D', self
._fc
3_fn
())
1329 fc
.append_member('C', self
._fc
4_fn
())
1330 fc
.append_member('F', self
._fc
5_fn
())
1331 field
= _create_field(self
._tc
, fc
)
1333 field
['B'] = 'salut'
1338 self
.assertNotEqual(self
._def
, field
)
1340 def test_setitem(self
):
1341 self
._def
['C'] = -18.47
1342 self
.assertEqual(self
._def
['C'], -18.47)
1344 def test_setitem_int_field(self
):
1345 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1346 int_field
= _create_field(self
._tc
, int_fc
)
1347 int_field
.value
= 19487
1348 self
._def
['D'] = int_field
1349 self
.assertEqual(self
._def
['D'], 19487)
1351 def test_setitem_non_basic_field(self
):
1352 elem_fc
= self
._tc
.create_structure_field_class()
1353 struct_fc
= self
._tc
.create_structure_field_class()
1354 struct_fc
.append_member('A', elem_fc
)
1355 struct_field
= _create_field(self
._tc
, struct_fc
)
1357 # Will fail on access to .items() of the value
1358 with self
.assertRaises(AttributeError):
1359 struct_field
['A'] = 23
1361 def test_setitem_none(self
):
1362 with self
.assertRaises(TypeError):
1363 self
._def
['C'] = None
1365 def test_setitem_key_wrong_type(self
):
1366 with self
.assertRaises(TypeError):
1369 def test_setitem_wrong_key(self
):
1370 with self
.assertRaises(KeyError):
1371 self
._def
['hi'] = 134679
1373 def test_member_at_index(self
):
1374 self
.assertEqual(self
._def
.member_at_index(1), 'salut')
1376 def test_iter(self
):
1386 for vkey
, vval
in self
._def
.items():
1387 val
= orig_values
[vkey
]
1388 self
.assertEqual(vval
, val
)
1390 def test_value(self
):
1399 self
.assertEqual(self
._def
, orig_values
)
1401 def test_set_value(self
):
1402 int_fc
= self
._tc
.create_signed_integer_field_class(32)
1403 another_int_fc
= self
._tc
.create_signed_integer_field_class(32)
1404 str_fc
= self
._tc
.create_string_field_class()
1405 struct_fc
= self
._tc
.create_structure_field_class()
1406 struct_fc
.append_member(field_class
=int_fc
, name
='an_int')
1407 struct_fc
.append_member(field_class
=str_fc
, name
='a_string')
1408 struct_fc
.append_member(field_class
=another_int_fc
, name
='another_int')
1411 'a_string': 'hello',
1415 struct
= _create_field(self
._tc
, struct_fc
)
1416 struct
.value
= values
1417 self
.assertEqual(values
, struct
)
1419 bad_type_values
= copy
.deepcopy(values
)
1420 bad_type_values
['an_int'] = 'a string'
1421 with self
.assertRaises(TypeError):
1422 struct
.value
= bad_type_values
1424 unknown_key_values
= copy
.deepcopy(values
)
1425 unknown_key_values
['unknown_key'] = 16546
1426 with self
.assertRaises(KeyError):
1427 struct
.value
= unknown_key_values
1429 def test_str_op(self
):
1430 expected_string_found
= False
1432 # Establish all permutations of the three expected matches since
1433 # the order in which mappings are enumerated is not explicitly part of
1435 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1436 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1437 candidate
= '{{{}}}'.format(', '.join(items
))
1439 expected_string_found
= True
1442 self
.assertTrue(expected_string_found
)
1445 class VariantFieldTestCase(unittest
.TestCase
):
1446 def _create_fc(self
, tc
):
1447 selector_fc
= tc
.create_signed_enumeration_field_class(field_value_range
=32)
1448 selector_fc
.map_range('corner', 23)
1449 selector_fc
.map_range('zoom', 17, 20)
1450 selector_fc
.map_range('mellotron', 1001)
1451 selector_fc
.map_range('giorgio', 2000, 3000)
1453 ft0
= tc
.create_signed_integer_field_class(32)
1454 ft1
= tc
.create_string_field_class()
1455 ft2
= tc
.create_real_field_class()
1456 ft3
= tc
.create_signed_integer_field_class(17)
1458 fc
= tc
.create_variant_field_class()
1459 fc
.append_option('corner', ft0
)
1460 fc
.append_option('zoom', ft1
)
1461 fc
.append_option('mellotron', ft2
)
1462 fc
.append_option('giorgio', ft3
)
1463 fc
.selector_field_class
= selector_fc
1465 top_fc
= tc
.create_structure_field_class()
1466 top_fc
.append_member('selector_field', selector_fc
)
1467 top_fc
.append_member('variant_field', fc
)
1471 self
._tc
= get_default_trace_class()
1472 fld
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1473 self
._def
= fld
['variant_field']
1475 def test_bool_op(self
):
1476 self
._def
.selected_option_index
= 2
1477 self
._def
.value
= -17.34
1478 with self
.assertRaises(NotImplementedError):
1481 def test_selected_option_index(self
):
1482 self
._def
.selected_option_index
= 2
1483 self
.assertEqual(self
._def
.selected_option_index
, 2)
1485 def test_selected_option(self
):
1486 self
._def
.selected_option_index
= 2
1487 self
._def
.value
= -17.34
1488 self
.assertEqual(self
._def
.selected_option
, -17.34)
1490 self
._def
.selected_option_index
= 3
1491 self
._def
.value
= 1921
1492 self
.assertEqual(self
._def
.selected_option
, 1921)
1495 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1496 field
= field
['variant_field']
1497 field
.selected_option_index
= 0
1499 self
._def
.selected_option_index
= 0
1500 self
._def
.value
= 1774
1501 self
.assertEqual(self
._def
, field
)
1503 def test_eq_invalid_type(self
):
1504 self
._def
.selected_option_index
= 1
1505 self
._def
.value
= 'gerry'
1506 self
.assertNotEqual(self
._def
, 23)
1508 def test_str_op_int(self
):
1509 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1510 field
= field
['variant_field']
1511 field
.selected_option_index
= 0
1513 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1514 other_field
= other_field
['variant_field']
1515 other_field
.selected_option_index
= 0
1516 other_field
.value
= 1774
1517 self
.assertEqual(str(field
), str(other_field
))
1519 def test_str_op_str(self
):
1520 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1521 field
= field
['variant_field']
1522 field
.selected_option_index
= 1
1523 field
.value
= 'un beau grand bateau'
1524 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1525 other_field
= other_field
['variant_field']
1526 other_field
.selected_option_index
= 1
1527 other_field
.value
= 'un beau grand bateau'
1528 self
.assertEqual(str(field
), str(other_field
))
1530 def test_str_op_float(self
):
1531 field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1532 field
= field
['variant_field']
1533 field
.selected_option_index
= 2
1534 field
.value
= 14.4245
1535 other_field
= _create_field(self
._tc
, self
._create
_fc
(self
._tc
))
1536 other_field
= other_field
['variant_field']
1537 other_field
.selected_option_index
= 2
1538 other_field
.value
= 14.4245
1539 self
.assertEqual(str(field
), str(other_field
))