1 from functools
import partial
, partialmethod
11 class _TestCopySimple
:
13 cpy
= copy
.copy(self
._def
)
14 self
.assertIsNot(cpy
, self
._def
)
15 self
.assertNotEqual(cpy
.addr
, self
._def
.addr
)
16 self
.assertEqual(cpy
, self
._def
)
18 def test_deepcopy(self
):
19 cpy
= copy
.deepcopy(self
._def
)
20 self
.assertIsNot(cpy
, self
._def
)
21 self
.assertNotEqual(cpy
.addr
, self
._def
.addr
)
22 self
.assertEqual(cpy
, self
._def
)
31 class _TestNumericField(_TestCopySimple
):
32 def _binop(self
, op
, rhs
):
37 if isinstance(rhs
, (bt2
.field
._IntegerField
, bt2
.field
._FloatingPointNumberField
)):
38 comp_value
= copy
.copy(rhs
)
41 r
= op(self
._def
, rhs
)
42 except Exception as e
:
46 rv
= op(self
._def
_value
, comp_value
)
47 except Exception as e
:
50 if rexc
is not None or rvexc
is not None:
51 # at least one of the operations raised an exception: in
52 # this case both operations should have raised the same
53 # type of exception (division by zero, bit shift with a
54 # floating point number operand, etc.)
55 self
.assertIs(type(rexc
), type(rvexc
))
60 def _unaryop(self
, op
):
66 except Exception as e
:
70 rv
= op(self
._def
_value
)
71 except Exception as e
:
74 if rexc
is not None or rvexc
is not None:
75 # at least one of the operations raised an exception: in
76 # this case both operations should have raised the same
77 # type of exception (division by zero, bit shift with a
78 # floating point number operand, etc.)
79 self
.assertIs(type(rexc
), type(rvexc
))
84 def _test_unaryop_type(self
, op
):
85 r
, rv
= self
._unaryop
(op
)
90 self
.assertIsInstance(r
, type(rv
))
92 def _test_unaryop_value(self
, op
):
93 r
, rv
= self
._unaryop
(op
)
98 self
.assertEqual(r
, rv
)
100 def _test_unaryop_addr_same(self
, op
):
101 addr_before
= self
._def
.addr
103 self
.assertEqual(self
._def
.addr
, addr_before
)
105 def _test_unaryop_value_same(self
, op
):
106 value_before
= copy
.copy(self
._def
_value
)
108 self
.assertEqual(self
._def
, value_before
)
110 def _test_binop_type(self
, op
, rhs
):
111 r
, rv
= self
._binop
(op
, rhs
)
116 if op
in _COMP_BINOPS
:
117 # __eq__() and __ne__() always return a 'bool' object
118 self
.assertIsInstance(r
, bool)
120 self
.assertIsInstance(r
, type(rv
))
122 def _test_binop_value(self
, op
, rhs
):
123 r
, rv
= self
._binop
(op
, rhs
)
128 self
.assertEqual(r
, rv
)
130 def _test_binop_lhs_addr_same(self
, op
, rhs
):
131 addr_before
= self
._def
.addr
132 r
, rv
= self
._binop
(op
, rhs
)
133 self
.assertEqual(self
._def
.addr
, addr_before
)
135 def _test_binop_lhs_value_same(self
, op
, rhs
):
136 value_before
= copy
.copy(self
._def
)
137 r
, rv
= self
._binop
(op
, rhs
)
138 self
.assertEqual(self
._def
, value_before
)
140 def _test_binop_invalid_unknown(self
, op
):
141 if op
in _COMP_BINOPS
:
142 self
.skipTest('not testing')
147 with self
.assertRaises(TypeError):
150 def _test_binop_invalid_none(self
, op
):
151 if op
in _COMP_BINOPS
:
152 self
.skipTest('not testing')
154 with self
.assertRaises(TypeError):
157 def _test_ibinop_value(self
, op
, rhs
):
158 r
, rv
= self
._binop
(op
, rhs
)
163 # The inplace operators are special for field objects because
164 # they do not return a new, immutable object like it's the case
165 # for Python numbers. In Python, `a += 2`, where `a` is a number
166 # object, assigns a new number object reference to `a`, dropping
167 # the old reference. Since BT's field objects are mutable, we
168 # modify their internal value with the inplace operators. This
169 # means however that we can lose data in the process, for
172 # int_value_obj += 3.3
174 # Here, if `int_value_obj` is a Python `int` with the value 2,
175 # it would be a `float` object after this, holding the value
176 # 5.3. In our case, if `int_value_obj` is an integer field
177 # object, 3.3 is converted to an `int` object (3) and added to
178 # the current value of `int_value_obj`, so after this the value
179 # of the object is 5. This does not compare to 5.3, which is
180 # why we also use the `int()` type here.
181 if isinstance(self
._def
, bt2
.field
._IntegerField
):
184 self
.assertEqual(r
, rv
)
186 def _test_ibinop_type(self
, op
, rhs
):
187 r
, rv
= self
._binop
(op
, rhs
)
192 self
.assertIs(r
, self
._def
)
194 def _test_ibinop_invalid_unknown(self
, op
):
198 with self
.assertRaises(TypeError):
201 def _test_ibinop_invalid_none(self
, op
):
202 with self
.assertRaises(TypeError):
205 def _test_binop_rhs_false(self
, test_cb
, op
):
208 def _test_binop_rhs_true(self
, test_cb
, op
):
211 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
214 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
217 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
220 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
221 test_cb(op
, bt2
.create_value(2))
223 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
224 test_cb(op
, bt2
.create_value(-23))
226 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
227 test_cb(op
, bt2
.create_value(0))
229 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
232 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
235 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
238 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
239 test_cb(op
, bt2
.create_value(2.2))
241 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
242 test_cb(op
, bt2
.create_value(-23.4))
244 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
245 test_cb(op
, bt2
.create_value(0.0))
247 def _test_binop_type_false(self
, op
):
248 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
250 def _test_binop_type_true(self
, op
):
251 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
253 def _test_binop_type_pos_int(self
, op
):
254 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
256 def _test_binop_type_neg_int(self
, op
):
257 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
259 def _test_binop_type_zero_int(self
, op
):
260 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
262 def _test_binop_type_pos_vint(self
, op
):
263 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
265 def _test_binop_type_neg_vint(self
, op
):
266 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
268 def _test_binop_type_zero_vint(self
, op
):
269 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
271 def _test_binop_type_pos_float(self
, op
):
272 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
274 def _test_binop_type_neg_float(self
, op
):
275 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
277 def _test_binop_type_zero_float(self
, op
):
278 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
280 def _test_binop_type_pos_vfloat(self
, op
):
281 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
283 def _test_binop_type_neg_vfloat(self
, op
):
284 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
286 def _test_binop_type_zero_vfloat(self
, op
):
287 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
289 def _test_binop_value_false(self
, op
):
290 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
292 def _test_binop_value_true(self
, op
):
293 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
295 def _test_binop_value_pos_int(self
, op
):
296 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
298 def _test_binop_value_neg_int(self
, op
):
299 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
301 def _test_binop_value_zero_int(self
, op
):
302 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
304 def _test_binop_value_pos_vint(self
, op
):
305 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
307 def _test_binop_value_neg_vint(self
, op
):
308 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
310 def _test_binop_value_zero_vint(self
, op
):
311 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
313 def _test_binop_value_pos_float(self
, op
):
314 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
316 def _test_binop_value_neg_float(self
, op
):
317 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
319 def _test_binop_value_zero_float(self
, op
):
320 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
322 def _test_binop_value_pos_vfloat(self
, op
):
323 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
325 def _test_binop_value_neg_vfloat(self
, op
):
326 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
328 def _test_binop_value_zero_vfloat(self
, op
):
329 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
331 def _test_binop_lhs_addr_same_false(self
, op
):
332 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
334 def _test_binop_lhs_addr_same_true(self
, op
):
335 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
337 def _test_binop_lhs_addr_same_pos_int(self
, op
):
338 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
340 def _test_binop_lhs_addr_same_neg_int(self
, op
):
341 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
343 def _test_binop_lhs_addr_same_zero_int(self
, op
):
344 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
346 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
347 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
349 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
350 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
352 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
353 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
355 def _test_binop_lhs_addr_same_pos_float(self
, op
):
356 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
358 def _test_binop_lhs_addr_same_neg_float(self
, op
):
359 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
361 def _test_binop_lhs_addr_same_zero_float(self
, op
):
362 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
364 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
365 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
367 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
368 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
370 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
371 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
373 def _test_binop_lhs_value_same_false(self
, op
):
374 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
376 def _test_binop_lhs_value_same_true(self
, op
):
377 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
379 def _test_binop_lhs_value_same_pos_int(self
, op
):
380 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
382 def _test_binop_lhs_value_same_neg_int(self
, op
):
383 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
385 def _test_binop_lhs_value_same_zero_int(self
, op
):
386 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
388 def _test_binop_lhs_value_same_pos_vint(self
, op
):
389 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
391 def _test_binop_lhs_value_same_neg_vint(self
, op
):
392 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
394 def _test_binop_lhs_value_same_zero_vint(self
, op
):
395 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
397 def _test_binop_lhs_value_same_pos_float(self
, op
):
398 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
400 def _test_binop_lhs_value_same_neg_float(self
, op
):
401 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
403 def _test_binop_lhs_value_same_zero_float(self
, op
):
404 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
406 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
407 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
409 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
410 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
412 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
413 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
415 def _test_ibinop_type_false(self
, op
):
416 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_type
, op
)
418 def _test_ibinop_type_true(self
, op
):
419 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_type
, op
)
421 def _test_ibinop_type_pos_int(self
, op
):
422 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_type
, op
)
424 def _test_ibinop_type_neg_int(self
, op
):
425 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_type
, op
)
427 def _test_ibinop_type_zero_int(self
, op
):
428 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_type
, op
)
430 def _test_ibinop_type_pos_vint(self
, op
):
431 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_type
, op
)
433 def _test_ibinop_type_neg_vint(self
, op
):
434 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_type
, op
)
436 def _test_ibinop_type_zero_vint(self
, op
):
437 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_type
, op
)
439 def _test_ibinop_type_pos_float(self
, op
):
440 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_type
, op
)
442 def _test_ibinop_type_neg_float(self
, op
):
443 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_type
, op
)
445 def _test_ibinop_type_zero_float(self
, op
):
446 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_type
, op
)
448 def _test_ibinop_type_pos_vfloat(self
, op
):
449 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_type
, op
)
451 def _test_ibinop_type_neg_vfloat(self
, op
):
452 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_type
, op
)
454 def _test_ibinop_type_zero_vfloat(self
, op
):
455 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_type
, op
)
457 def _test_ibinop_value_false(self
, op
):
458 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_value
, op
)
460 def _test_ibinop_value_true(self
, op
):
461 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_value
, op
)
463 def _test_ibinop_value_pos_int(self
, op
):
464 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_value
, op
)
466 def _test_ibinop_value_neg_int(self
, op
):
467 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_value
, op
)
469 def _test_ibinop_value_zero_int(self
, op
):
470 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_value
, op
)
472 def _test_ibinop_value_pos_vint(self
, op
):
473 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_value
, op
)
475 def _test_ibinop_value_neg_vint(self
, op
):
476 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_value
, op
)
478 def _test_ibinop_value_zero_vint(self
, op
):
479 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_value
, op
)
481 def _test_ibinop_value_pos_float(self
, op
):
482 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_value
, op
)
484 def _test_ibinop_value_neg_float(self
, op
):
485 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_value
, op
)
487 def _test_ibinop_value_zero_float(self
, op
):
488 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_value
, op
)
490 def _test_ibinop_value_pos_vfloat(self
, op
):
491 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_value
, op
)
493 def _test_ibinop_value_neg_vfloat(self
, op
):
494 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_value
, op
)
496 def _test_ibinop_value_zero_vfloat(self
, op
):
497 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_value
, op
)
499 def test_bool_op(self
):
500 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
502 def test_int_op(self
):
503 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
505 def test_float_op(self
):
506 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
508 def test_complex_op(self
):
509 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
511 def test_str_op(self
):
512 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
514 def test_eq_none(self
):
515 self
.assertFalse(self
._def
== None)
517 def test_ne_none(self
):
518 self
.assertTrue(self
._def
!= None)
520 def test_is_set(self
):
521 raw
= self
._def
_value
523 self
.assertFalse(field
.is_set
)
525 self
.assertTrue(field
.is_set
)
527 def test_reset(self
):
528 raw
= self
._def
_value
531 self
.assertTrue(field
.is_set
)
533 self
.assertFalse(field
.is_set
)
535 self
.assertEqual(other
, field
)
545 ('add', operator
.add
),
546 ('radd', lambda a
, b
: operator
.add(b
, a
)),
547 ('and', operator
.and_
),
548 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
549 ('floordiv', operator
.floordiv
),
550 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
551 ('lshift', operator
.lshift
),
552 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
553 ('mod', operator
.mod
),
554 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
555 ('mul', operator
.mul
),
556 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
557 ('or', operator
.or_
),
558 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
559 ('pow', operator
.pow),
560 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
561 ('rshift', operator
.rshift
),
562 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
563 ('sub', operator
.sub
),
564 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
565 ('truediv', operator
.truediv
),
566 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
567 ('xor', operator
.xor
),
568 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
573 ('iadd', operator
.iadd
),
574 ('iand', operator
.iand
),
575 ('ifloordiv', operator
.ifloordiv
),
576 ('ilshift', operator
.ilshift
),
577 ('imod', operator
.imod
),
578 ('imul', operator
.imul
),
579 ('ior', operator
.ior
),
580 ('ipow', operator
.ipow
),
581 ('irshift', operator
.irshift
),
582 ('isub', operator
.isub
),
583 ('itruediv', operator
.itruediv
),
584 ('ixor', operator
.ixor
),
589 ('neg', operator
.neg
),
590 ('pos', operator
.pos
),
591 ('abs', operator
.abs),
592 ('invert', operator
.invert
),
594 ('round_0', partial(round, ndigits
=0)),
595 ('round_1', partial(round, ndigits
=1)),
596 ('round_2', partial(round, ndigits
=2)),
597 ('round_3', partial(round, ndigits
=3)),
599 ('floor', math
.floor
),
600 ('trunc', math
.trunc
),
604 def _inject_numeric_testing_methods(cls
):
605 def test_binop_name(suffix
):
606 return 'test_binop_{}_{}'.format(name
, suffix
)
608 def test_ibinop_name(suffix
):
609 return 'test_ibinop_{}_{}'.format(name
, suffix
)
611 def test_unaryop_name(suffix
):
612 return 'test_unaryop_{}_{}'.format(name
, suffix
)
614 # inject testing methods for each binary operation
615 for name
, binop
in _BINOPS
:
616 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
))
617 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
))
618 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
))
619 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
))
620 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
))
621 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
))
622 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
))
623 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
))
624 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
625 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
626 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
627 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
628 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
629 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
630 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
))
631 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
))
632 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
))
633 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
))
634 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
635 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
636 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
637 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
638 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
))
639 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
))
640 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
))
641 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
))
642 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
))
643 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
))
644 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
645 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
646 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
647 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
648 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
649 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
650 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
))
651 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
))
652 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
653 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
654 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
))
655 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
))
656 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
657 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
658 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
659 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
660 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
661 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
662 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
663 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
664 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
665 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
666 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
))
667 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
668 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
))
669 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
670 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
671 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
672 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
673 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
675 # inject testing methods for each unary operation
676 for name
, unaryop
in _UNARYOPS
:
677 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
))
678 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
))
679 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
))
680 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
))
682 # inject testing methods for each inplace binary operation
683 for name
, ibinop
in _IBINOPS
:
684 setattr(cls
, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_ibinop
_invalid
_unknown
, op
=ibinop
))
685 setattr(cls
, test_ibinop_name('invalid_none'), partialmethod(_TestNumericField
._test
_ibinop
_invalid
_none
, op
=ibinop
))
686 setattr(cls
, test_ibinop_name('type_true'), partialmethod(_TestNumericField
._test
_ibinop
_type
_true
, op
=ibinop
))
687 setattr(cls
, test_ibinop_name('value_true'), partialmethod(_TestNumericField
._test
_ibinop
_value
_true
, op
=ibinop
))
688 setattr(cls
, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_int
, op
=ibinop
))
689 setattr(cls
, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_vint
, op
=ibinop
))
690 setattr(cls
, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_int
, op
=ibinop
))
691 setattr(cls
, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_vint
, op
=ibinop
))
692 setattr(cls
, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_int
, op
=ibinop
))
693 setattr(cls
, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_vint
, op
=ibinop
))
694 setattr(cls
, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_int
, op
=ibinop
))
695 setattr(cls
, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_vint
, op
=ibinop
))
696 setattr(cls
, test_ibinop_name('type_false'), partialmethod(_TestNumericField
._test
_ibinop
_type
_false
, op
=ibinop
))
697 setattr(cls
, test_ibinop_name('value_false'), partialmethod(_TestNumericField
._test
_ibinop
_value
_false
, op
=ibinop
))
698 setattr(cls
, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_int
, op
=ibinop
))
699 setattr(cls
, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_vint
, op
=ibinop
))
700 setattr(cls
, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_int
, op
=ibinop
))
701 setattr(cls
, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_vint
, op
=ibinop
))
702 setattr(cls
, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_float
, op
=ibinop
))
703 setattr(cls
, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_float
, op
=ibinop
))
704 setattr(cls
, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_vfloat
, op
=ibinop
))
705 setattr(cls
, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_vfloat
, op
=ibinop
))
706 setattr(cls
, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_float
, op
=ibinop
))
707 setattr(cls
, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_float
, op
=ibinop
))
708 setattr(cls
, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_vfloat
, op
=ibinop
))
709 setattr(cls
, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_vfloat
, op
=ibinop
))
710 setattr(cls
, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_float
, op
=ibinop
))
711 setattr(cls
, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_vfloat
, op
=ibinop
))
712 setattr(cls
, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_float
, op
=ibinop
))
713 setattr(cls
, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_vfloat
, op
=ibinop
))
716 class _TestIntegerFieldCommon(_TestNumericField
):
717 def test_assign_true(self
):
719 self
._def
.value
= raw
720 self
.assertEqual(self
._def
, raw
)
722 def test_assign_false(self
):
724 self
._def
.value
= raw
725 self
.assertEqual(self
._def
, raw
)
727 def test_assign_pos_int(self
):
729 self
._def
.value
= raw
730 self
.assertEqual(self
._def
, raw
)
732 def test_assign_neg_int(self
):
734 self
._def
.value
= raw
735 self
.assertEqual(self
._def
, raw
)
737 def test_assign_int_field(self
):
741 self
._def
.value
= field
742 self
.assertEqual(self
._def
, raw
)
744 def test_assign_float(self
):
746 self
._def
.value
= raw
747 self
.assertEqual(self
._def
, int(raw
))
749 def test_assign_invalid_type(self
):
750 with self
.assertRaises(TypeError):
751 self
._def
.value
= 'yes'
753 def test_assign_uint(self
):
754 fc
= bt2
.IntegerFieldClass(size
=32, is_signed
=False)
758 self
.assertEqual(field
, raw
)
760 def test_assign_uint_invalid_neg(self
):
761 fc
= bt2
.IntegerFieldClass(size
=32, is_signed
=False)
764 with self
.assertRaises(ValueError):
767 def test_str_op(self
):
768 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
770 def test_str_op_unset(self
):
771 self
.assertEqual(str(self
._fc
()), 'Unset')
774 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
777 @unittest.skip("this is broken")
778 class IntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
780 self
._fc
= bt2
.IntegerFieldClass(25, is_signed
=True)
781 self
._field
= self
._fc
()
782 self
._def
= self
._fc
()
785 self
._def
_new
_value
= -101
793 @unittest.skip("this is broken")
794 class EnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
796 self
._fc
= bt2
.EnumerationFieldClass(size
=32, is_signed
=True)
797 self
._fc
.add_mapping('whole range', -(2 ** 31), (2 ** 31) - 1)
798 self
._fc
.add_mapping('something', 17)
799 self
._fc
.add_mapping('speaker', 12, 16)
800 self
._fc
.add_mapping('can', 18, 2540)
801 self
._fc
.add_mapping('zip', -45, 1001)
802 self
._def
= self
._fc
()
805 self
._def
_new
_value
= -101
811 def test_mappings(self
):
813 ('whole range', -(2 ** 31), (2 ** 31) - 1),
814 ('something', 17, 17),
821 for fm
in self
._def
.mappings
:
823 for index
, mapping
in enumerate(mappings
):
824 if fm
.name
== mapping
[0] and fm
.lower
== mapping
[1] and fm
.upper
== mapping
[2]:
827 self
.assertEqual(total
, 3)
828 self
.assertTrue(0 in index_set
and 1 in index_set
and 2 in index_set
)
830 def test_str_op(self
):
831 expected_string_found
= False
834 # Establish all permutations of the three expected matches since
835 # the order in which mappings are enumerated is not explicitly part of
837 for p
in itertools
.permutations(["'whole range'", "'something'",
839 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
841 expected_string_found
= True
844 self
.assertTrue(expected_string_found
)
846 def test_str_op_unset(self
):
847 self
.assertEqual(str(self
._fc
()), 'Unset')
850 @unittest.skip("this is broken")
851 class FloatingPointNumberFieldTestCase(_TestNumericField
, unittest
.TestCase
):
853 self
._fc
= bt2
.FloatingPointNumberFieldClass()
854 self
._field
= self
._fc
()
855 self
._def
= self
._fc
()
856 self
._def
.value
= 52.7
857 self
._def
_value
= 52.7
858 self
._def
_new
_value
= -17.164857
865 def _test_invalid_op(self
, cb
):
866 with self
.assertRaises(TypeError):
869 def test_assign_true(self
):
870 self
._def
.value
= True
871 self
.assertTrue(self
._def
)
873 def test_assign_false(self
):
874 self
._def
.value
= False
875 self
.assertFalse(self
._def
)
877 def test_assign_pos_int(self
):
879 self
._def
.value
= raw
880 self
.assertEqual(self
._def
, float(raw
))
882 def test_assign_neg_int(self
):
884 self
._def
.value
= raw
885 self
.assertEqual(self
._def
, float(raw
))
887 def test_assign_int_field(self
):
888 fc
= bt2
.IntegerFieldClass(32)
892 self
._def
.value
= field
893 self
.assertEqual(self
._def
, float(raw
))
895 def test_assign_float(self
):
897 self
._def
.value
= raw
898 self
.assertEqual(self
._def
, raw
)
900 def test_assign_float_field(self
):
901 fc
= bt2
.FloatingPointNumberFieldClass(32)
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
))
933 def test_str_op_unset(self
):
934 self
.assertEqual(str(self
._fc
()), 'Unset')
936 _inject_numeric_testing_methods(FloatingPointNumberFieldTestCase
)
939 @unittest.skip("this is broken")
940 class StringFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
942 self
._fc
= bt2
.StringFieldClass()
943 self
._def
_value
= 'Hello, World!'
944 self
._def
= self
._fc
()
945 self
._def
.value
= self
._def
_value
946 self
._def
_new
_value
= 'Yes!'
952 def test_assign_int(self
):
953 with self
.assertRaises(TypeError):
954 self
._def
.value
= 283
956 def test_assign_string_field(self
):
957 fc
= bt2
.StringFieldClass()
961 self
.assertEqual(field
, raw
)
964 self
.assertEqual(self
._def
, self
._def
_value
)
967 self
.assertNotEqual(self
._def
, 23)
969 def test_lt_vstring(self
):
974 self
.assertLess(s1
, s2
)
976 def test_lt_string(self
):
979 self
.assertLess(s1
, 'bateau')
981 def test_le_vstring(self
):
986 self
.assertLessEqual(s1
, s2
)
988 def test_le_string(self
):
991 self
.assertLessEqual(s1
, 'bateau')
993 def test_gt_vstring(self
):
998 self
.assertGreater(s2
, s1
)
1000 def test_gt_string(self
):
1003 self
.assertGreater('bateau', s1
)
1005 def test_ge_vstring(self
):
1010 self
.assertGreaterEqual(s2
, s1
)
1012 def test_ge_string(self
):
1015 self
.assertGreaterEqual('bateau', s1
)
1017 def test_bool_op(self
):
1018 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1020 def test_str_op(self
):
1021 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1023 def test_str_op_unset(self
):
1024 self
.assertEqual(str(self
._fc
()), 'Unset')
1027 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1029 def test_getitem(self
):
1030 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1032 def test_append_str(self
):
1033 to_append
= 'meow meow meow'
1034 self
._def
+= to_append
1035 self
._def
_value
+= to_append
1036 self
.assertEqual(self
._def
, self
._def
_value
)
1038 def test_append_string_field(self
):
1039 fc
= bt2
.StringFieldClass()
1041 to_append
= 'meow meow meow'
1042 field
.value
= to_append
1044 self
._def
_value
+= to_append
1045 self
.assertEqual(self
._def
, self
._def
_value
)
1047 def test_is_set(self
):
1048 raw
= self
._def
_value
1050 self
.assertFalse(field
.is_set
)
1052 self
.assertTrue(field
.is_set
)
1054 def test_reset(self
):
1055 raw
= self
._def
_value
1058 self
.assertTrue(field
.is_set
)
1060 self
.assertFalse(field
.is_set
)
1062 self
.assertEqual(other
, field
)
1065 class _TestArraySequenceFieldCommon(_TestCopySimple
):
1066 def _modify_def(self
):
1069 def test_bool_op_true(self
):
1070 self
.assertTrue(self
._def
)
1073 self
.assertEqual(len(self
._def
), 3)
1075 def test_getitem(self
):
1076 field
= self
._def
[1]
1077 self
.assertIs(type(field
), bt2
.field
._IntegerField
)
1078 self
.assertEqual(field
, 1847)
1081 fc
= bt2
.ArrayFieldClass(self
._elem
_fc
, 3)
1086 self
.assertEqual(self
._def
, field
)
1088 def test_eq_invalid_type(self
):
1089 self
.assertNotEqual(self
._def
, 23)
1091 def test_eq_diff_len(self
):
1092 fc
= bt2
.ArrayFieldClass(self
._elem
_fc
, 2)
1096 self
.assertNotEqual(self
._def
, field
)
1098 def test_eq_diff_content_same_len(self
):
1099 fc
= bt2
.ArrayFieldClass(self
._elem
_fc
, 3)
1104 self
.assertNotEqual(self
._def
, field
)
1106 def test_setitem(self
):
1108 self
.assertEqual(self
._def
[2], 24)
1110 def test_setitem_int_field(self
):
1111 int_field
= self
._elem
_fc
()
1112 int_field
.value
= 19487
1113 self
._def
[1] = int_field
1114 self
.assertEqual(self
._def
[1], 19487)
1116 def test_setitem_non_basic_field(self
):
1117 elem_fc
= bt2
.StructureFieldClass()
1118 array_fc
= bt2
.ArrayFieldClass(elem_fc
, 3)
1119 elem_field
= elem_fc()
1120 array_field
= array_fc()
1122 with self
.assertRaises(TypeError):
1125 def test_setitem_none(self
):
1126 with self
.assertRaises(TypeError):
1129 def test_setitem_index_wrong_type(self
):
1130 with self
.assertRaises(TypeError):
1131 self
._def
['yes'] = 23
1133 def test_setitem_index_neg(self
):
1134 with self
.assertRaises(IndexError):
1137 def test_setitem_index_out_of_range(self
):
1138 with self
.assertRaises(IndexError):
1139 self
._def
[len(self
._def
)] = 134679
1141 def test_iter(self
):
1142 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1143 self
.assertEqual(field
, value
)
1145 def test_value_int_field(self
):
1146 values
= [45646, 145, 12145]
1147 self
._def
.value
= values
1148 self
.assertEqual(values
, self
._def
)
1150 def test_value_unset(self
):
1151 values
= [45646, None, 12145]
1152 self
._def
.value
= values
1153 self
.assertFalse(self
._def
[1].is_set
)
1155 def test_value_rollback(self
):
1156 values
= [45, 1847, 1948754]
1157 # value is out of range, should not affect those we set previously
1158 with self
.assertRaises(bt2
.Error
):
1159 self
._def
[2].value
= 2**60
1160 self
.assertEqual(values
, self
._def
)
1162 def test_value_check_sequence(self
):
1164 with self
.assertRaises(TypeError):
1165 self
._def
.value
= values
1167 def test_value_wrong_type_in_sequence(self
):
1168 values
= [32, 'hello', 11]
1169 with self
.assertRaises(TypeError):
1170 self
._def
.value
= values
1172 def test_value_complex_type(self
):
1173 struct_fc
= bt2
.StructureFieldClass()
1174 int_fc
= bt2
.IntegerFieldClass(32)
1175 str_fc
= bt2
.StringFieldClass()
1176 struct_fc
.append_field(field_class
=int_fc
, name
='an_int')
1177 struct_fc
.append_field(field_class
=str_fc
, name
='a_string')
1178 struct_fc
.append_field(field_class
=int_fc
, name
='another_int')
1179 array_fc
= bt2
.ArrayFieldClass(struct_fc
, 3)
1183 'a_string': 'hello',
1188 'a_string': 'goodbye',
1193 'a_string': 'or not',
1199 array
.value
= values
1200 self
.assertEqual(values
, array
)
1201 values
[0]['an_int'] = 'a string'
1202 with self
.assertRaises(TypeError):
1203 array
.value
= values
1205 def test_is_set(self
):
1206 raw
= self
._def
_value
1208 self
.assertFalse(field
.is_set
)
1210 self
.assertTrue(field
.is_set
)
1212 def test_reset(self
):
1213 raw
= self
._def
_value
1216 self
.assertTrue(field
.is_set
)
1218 self
.assertFalse(field
.is_set
)
1220 self
.assertEqual(other
, field
)
1222 def test_str_op(self
):
1224 expected_string
= '[{}]'.format(', '.join(
1225 [repr(v
) for v
in self
._def
_value
]))
1226 self
.assertEqual(expected_string
, s
)
1228 def test_str_op_unset(self
):
1229 self
.assertEqual(str(self
._fc
()), 'Unset')
1232 @unittest.skip("this is broken")
1233 class ArrayFieldTestCase(_TestArraySequenceFieldCommon
, unittest
.TestCase
):
1235 self
._elem
_fc
= bt2
.IntegerFieldClass(32)
1236 self
._fc
= bt2
.ArrayFieldClass(self
._elem
_fc
, 3)
1237 self
._def
= self
._fc
()
1240 self
._def
[2] = 1948754
1241 self
._def
_value
= [45, 1847, 1948754]
1248 def test_value_wrong_len(self
):
1250 with self
.assertRaises(ValueError):
1251 self
._def
.value
= values
1254 @unittest.skip("this is broken")
1255 class SequenceFieldTestCase(_TestArraySequenceFieldCommon
, unittest
.TestCase
):
1257 self
._elem
_fc
= bt2
.IntegerFieldClass(32)
1258 self
._fc
= bt2
.SequenceFieldClass(self
._elem
_fc
, 'the.length')
1259 self
._def
= self
._fc
()
1260 self
._length
_field
= self
._elem
_fc
(3)
1261 self
._def
.length_field
= self
._length
_field
1264 self
._def
[2] = 1948754
1265 self
._def
_value
= [45, 1847, 1948754]
1271 del self
._length
_field
1273 def test_value_resize(self
):
1274 new_values
= [1, 2, 3, 4]
1275 self
._def
.value
= new_values
1276 self
.assertCountEqual(self
._def
, new_values
)
1278 def test_value_resize_rollback(self
):
1279 with self
.assertRaises(TypeError):
1280 self
._def
.value
= [1, 2, 3, 'unexpected string']
1281 self
.assertEqual(self
._def
, self
._def
_value
)
1284 with self
.assertRaises(TypeError):
1285 self
._def
.value
= [1, 2, 3, 'unexpected string']
1286 self
.assertFalse(self
._def
.is_set
)
1289 @unittest.skip("this is broken")
1290 class StructureFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
1292 self
._fc
0 = bt2
.IntegerFieldClass(32, is_signed
=True)
1293 self
._fc
1 = bt2
.StringFieldClass()
1294 self
._fc
2 = bt2
.FloatingPointNumberFieldClass()
1295 self
._fc
3 = bt2
.IntegerFieldClass(17)
1296 self
._fc
= bt2
.StructureFieldClass()
1297 self
._fc
.append_field('A', self
._fc
0)
1298 self
._fc
.append_field('B', self
._fc
1)
1299 self
._fc
.append_field('C', self
._fc
2)
1300 self
._fc
.append_field('D', self
._fc
3)
1301 self
._def
= self
._fc
()
1302 self
._def
['A'] = -1872
1303 self
._def
['B'] = 'salut'
1304 self
._def
['C'] = 17.5
1305 self
._def
['D'] = 16497
1321 def _modify_def(self
):
1322 self
._def
['B'] = 'hola'
1324 def test_bool_op_true(self
):
1325 self
.assertTrue(self
._def
)
1327 def test_bool_op_false(self
):
1328 fc
= bt2
.StructureFieldClass()
1330 self
.assertFalse(field
)
1333 self
.assertEqual(len(self
._def
), 4)
1335 def test_getitem(self
):
1336 field
= self
._def
['A']
1337 self
.assertIs(type(field
), bt2
.field
._IntegerField
)
1338 self
.assertEqual(field
, -1872)
1340 def test_at_index_out_of_bounds_after(self
):
1341 with self
.assertRaises(IndexError):
1342 self
._def
.at_index(len(self
._fc
))
1345 fc
= bt2
.StructureFieldClass()
1346 fc
.append_field('A', self
._fc
0)
1347 fc
.append_field('B', self
._fc
1)
1348 fc
.append_field('C', self
._fc
2)
1349 fc
.append_field('D', self
._fc
3)
1352 field
['B'] = 'salut'
1355 self
.assertEqual(self
._def
, field
)
1357 def test_eq_invalid_type(self
):
1358 self
.assertNotEqual(self
._def
, 23)
1360 def test_eq_diff_len(self
):
1361 fc
= bt2
.StructureFieldClass()
1362 fc
.append_field('A', self
._fc
0)
1363 fc
.append_field('B', self
._fc
1)
1364 fc
.append_field('C', self
._fc
2)
1367 field
['B'] = 'salut'
1369 self
.assertNotEqual(self
._def
, field
)
1371 def test_eq_diff_content_same_len(self
):
1372 fc
= bt2
.StructureFieldClass()
1373 fc
.append_field('A', self
._fc
0)
1374 fc
.append_field('B', self
._fc
1)
1375 fc
.append_field('C', self
._fc
2)
1376 fc
.append_field('D', self
._fc
3)
1379 field
['B'] = 'salut'
1382 self
.assertNotEqual(self
._def
, field
)
1384 def test_eq_same_content_diff_keys(self
):
1385 fc
= bt2
.StructureFieldClass()
1386 fc
.append_field('A', self
._fc
0)
1387 fc
.append_field('B', self
._fc
1)
1388 fc
.append_field('E', self
._fc
2)
1389 fc
.append_field('D', self
._fc
3)
1392 field
['B'] = 'salut'
1395 self
.assertNotEqual(self
._def
, field
)
1397 def test_setitem(self
):
1398 self
._def
['C'] = -18.47
1399 self
.assertEqual(self
._def
['C'], -18.47)
1401 def test_setitem_int_field(self
):
1402 int_fc
= bt2
.IntegerFieldClass(16)
1403 int_field
= int_fc()
1404 int_field
.value
= 19487
1405 self
._def
['D'] = int_field
1406 self
.assertEqual(self
._def
['D'], 19487)
1408 def test_setitem_non_basic_field(self
):
1409 elem_fc
= bt2
.StructureFieldClass()
1410 elem_field
= elem_fc()
1411 struct_fc
= bt2
.StructureFieldClass()
1412 struct_fc
.append_field('A', elem_fc
)
1413 struct_field
= struct_fc()
1415 # Will fail on access to .items() of the value
1416 with self
.assertRaises(AttributeError):
1417 struct_field
['A'] = 23
1419 def test_setitem_none(self
):
1420 with self
.assertRaises(TypeError):
1421 self
._def
['C'] = None
1423 def test_setitem_key_wrong_type(self
):
1424 with self
.assertRaises(TypeError):
1427 def test_setitem_wrong_key(self
):
1428 with self
.assertRaises(KeyError):
1429 self
._def
['hi'] = 134679
1431 def test_at_index(self
):
1432 self
.assertEqual(self
._def
.at_index(1), 'salut')
1434 def test_iter(self
):
1442 for vkey
, vval
in self
._def
.items():
1443 val
= orig_values
[vkey
]
1444 self
.assertEqual(vval
, val
)
1446 def test_value(self
):
1453 self
.assertEqual(self
._def
, orig_values
)
1455 def test_set_value(self
):
1456 int_fc
= bt2
.IntegerFieldClass(32)
1457 str_fc
= bt2
.StringFieldClass()
1458 struct_fc
= bt2
.StructureFieldClass()
1459 struct_fc
.append_field(field_class
=int_fc
, name
='an_int')
1460 struct_fc
.append_field(field_class
=str_fc
, name
='a_string')
1461 struct_fc
.append_field(field_class
=int_fc
, name
='another_int')
1464 'a_string': 'hello',
1468 struct
= struct_fc()
1469 struct
.value
= values
1470 self
.assertEqual(values
, struct
)
1472 bad_type_values
= copy
.deepcopy(values
)
1473 bad_type_values
['an_int'] = 'a string'
1474 with self
.assertRaises(TypeError):
1475 struct
.value
= bad_type_values
1477 unknown_key_values
= copy
.deepcopy(values
)
1478 unknown_key_values
['unknown_key'] = 16546
1479 with self
.assertRaises(KeyError):
1480 struct
.value
= unknown_key_values
1482 def test_value_rollback(self
):
1483 int_fc
= bt2
.IntegerFieldClass(32)
1484 str_fc
= bt2
.StringFieldClass()
1485 struct_fc
= bt2
.StructureFieldClass()
1486 struct_fc
.append_field(field_class
=int_fc
, name
='an_int')
1487 struct_fc
.append_field(field_class
=str_fc
, name
='a_string')
1488 struct_fc
.append_field(field_class
=int_fc
, name
='another_int')
1491 'a_string': 'hello',
1495 def test_is_set(self
):
1498 'a_string': 'hello',
1502 int_fc
= bt2
.IntegerFieldClass(32)
1503 str_fc
= bt2
.StringFieldClass()
1504 struct_fc
= bt2
.StructureFieldClass()
1505 struct_fc
.append_field(field_class
=int_fc
, name
='an_int')
1506 struct_fc
.append_field(field_class
=str_fc
, name
='a_string')
1507 struct_fc
.append_field(field_class
=int_fc
, name
='another_int')
1509 struct
= struct_fc()
1510 self
.assertFalse(struct
.is_set
)
1511 struct
.value
= values
1512 self
.assertTrue(struct
.is_set
)
1514 struct
= struct_fc()
1515 struct
['an_int'].value
= 42
1516 self
.assertFalse(struct
.is_set
)
1518 def test_reset(self
):
1521 'a_string': 'hello',
1525 int_fc
= bt2
.IntegerFieldClass(32)
1526 str_fc
= bt2
.StringFieldClass()
1527 struct_fc
= bt2
.StructureFieldClass()
1528 struct_fc
.append_field(field_class
=int_fc
, name
='an_int')
1529 struct_fc
.append_field(field_class
=str_fc
, name
='a_string')
1530 struct_fc
.append_field(field_class
=int_fc
, name
='another_int')
1532 struct
= struct_fc()
1533 struct
.value
= values
1534 self
.assertTrue(struct
.is_set
)
1536 self
.assertEqual(struct_fc(), struct
)
1538 def test_str_op(self
):
1539 expected_string_found
= False
1541 # Establish all permutations of the three expected matches since
1542 # the order in which mappings are enumerated is not explicitly part of
1544 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1545 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1546 candidate
= '{{{}}}'.format(', '.join(items
))
1548 expected_string_found
= True
1551 self
.assertTrue(expected_string_found
)
1553 def test_str_op_unset(self
):
1554 self
.assertEqual(str(self
._fc
()), 'Unset')
1557 @unittest.skip("this is broken")
1558 class VariantFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
1560 self
._tag
_fc
= bt2
.EnumerationFieldClass(size
=32)
1561 self
._tag
_fc
.add_mapping('corner', 23)
1562 self
._tag
_fc
.add_mapping('zoom', 17, 20)
1563 self
._tag
_fc
.add_mapping('mellotron', 1001)
1564 self
._tag
_fc
.add_mapping('giorgio', 2000, 3000)
1565 self
._fc
0 = bt2
.IntegerFieldClass(32, is_signed
=True)
1566 self
._fc
1 = bt2
.StringFieldClass()
1567 self
._fc
2 = bt2
.FloatingPointNumberFieldClass()
1568 self
._fc
3 = bt2
.IntegerFieldClass(17)
1569 self
._fc
= bt2
.VariantFieldClass('salut', self
._tag
_fc
)
1570 self
._fc
.append_field('corner', self
._fc
0)
1571 self
._fc
.append_field('zoom', self
._fc
1)
1572 self
._fc
.append_field('mellotron', self
._fc
2)
1573 self
._fc
.append_field('giorgio', self
._fc
3)
1574 self
._def
= self
._fc
()
1585 def test_bool_op_true(self
):
1586 tag_field
= self
._tag
_fc
(1001)
1587 self
._def
.field(tag_field
).value
= -17.34
1588 self
.assertTrue(self
._def
)
1590 def test_bool_op_false(self
):
1591 self
.assertFalse(self
._def
)
1593 def test_tag_field_none(self
):
1594 self
.assertIsNone(self
._def
.tag_field
)
1596 def test_tag_field(self
):
1597 tag_field
= self
._tag
_fc
(2800)
1598 self
._def
.field(tag_field
).value
= 1847
1599 self
.assertEqual(self
._def
.tag_field
, tag_field
)
1600 self
.assertEqual(self
._def
.tag_field
.addr
, tag_field
.addr
)
1602 def test_selected_field_none(self
):
1603 self
.assertIsNone(self
._def
.selected_field
)
1605 def test_selected_field(self
):
1606 var_field1
= self
._fc
()
1607 tag_field1
= self
._tag
_fc
(1001)
1608 var_field1
.field(tag_field1
).value
= -17.34
1609 self
.assertEqual(var_field1
.field(), -17.34)
1610 self
.assertEqual(var_field1
.selected_field
, -17.34)
1611 var_field2
= self
._fc
()
1612 tag_field2
= self
._tag
_fc
(2500)
1613 var_field2
.field(tag_field2
).value
= 1921
1614 self
.assertEqual(var_field2
.field(), 1921)
1615 self
.assertEqual(var_field2
.selected_field
, 1921)
1618 tag_fc
= bt2
.EnumerationFieldClass(size
=32)
1619 tag_fc
.add_mapping('corner', 23)
1620 tag_fc
.add_mapping('zoom', 17, 20)
1621 tag_fc
.add_mapping('mellotron', 1001)
1622 tag_fc
.add_mapping('giorgio', 2000, 3000)
1623 fc0
= bt2
.IntegerFieldClass(32, is_signed
=True)
1624 fc1
= bt2
.StringFieldClass()
1625 fc2
= bt2
.FloatingPointNumberFieldClass()
1626 fc3
= bt2
.IntegerFieldClass(17)
1627 fc
= bt2
.VariantFieldClass('salut', tag_fc
)
1628 fc
.append_field('corner', fc0
)
1629 fc
.append_field('zoom', fc1
)
1630 fc
.append_field('mellotron', fc2
)
1631 fc
.append_field('giorgio', fc3
)
1633 field_tag
= tag_fc(23)
1634 def_tag
= self
._tag
_fc
(23)
1635 field
.field(field_tag
).value
= 1774
1636 self
._def
.field(def_tag
).value
= 1774
1637 self
.assertEqual(self
._def
, field
)
1639 def test_eq_invalid_type(self
):
1640 self
.assertNotEqual(self
._def
, 23)
1642 def test_is_set(self
):
1643 self
.assertFalse(self
._def
.is_set
)
1644 tag_field
= self
._tag
_fc
(2800)
1645 self
._def
.field(tag_field
).value
= 684
1646 self
.assertTrue(self
._def
.is_set
)
1648 def test_reset(self
):
1649 tag_field
= self
._tag
_fc
(2800)
1650 self
._def
.field(tag_field
).value
= 684
1652 self
.assertFalse(self
._def
.is_set
)
1653 self
.assertIsNone(self
._def
.selected_field
)
1654 self
.assertIsNone(self
._def
.tag_field
)
1656 def test_str_op_int(self
):
1658 v
.field(self
._tag
_fc
(23)).value
= 42
1660 self
.assertEqual(str(f
), str(v
))
1662 def test_str_op_str(self
):
1664 v
.field(self
._tag
_fc
(18)).value
= 'some test string'
1665 f
= self
._fc
1('some test string')
1666 self
.assertEqual(str(f
), str(v
))
1668 def test_str_op_flt(self
):
1670 v
.field(self
._tag
_fc
(1001)).value
= 14.4245
1671 f
= self
._fc
2(14.4245)
1672 self
.assertEqual(str(f
), str(v
))
1674 def test_str_op_unset(self
):
1675 self
.assertEqual(str(self
._fc
()), 'Unset')