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
.fields
._IntegerField
, bt2
.fields
._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
.fields
._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 ft
= bt2
.IntegerFieldType(size
=32, is_signed
=False)
758 self
.assertEqual(field
, raw
)
760 def test_assign_uint_invalid_neg(self
):
761 ft
= bt2
.IntegerFieldType(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
._ft
()), 'Unset')
774 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
777 class IntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
779 self
._ft
= bt2
.IntegerFieldType(25, is_signed
=True)
780 self
._field
= self
._ft
()
781 self
._def
= self
._ft
()
784 self
._def
_new
_value
= -101
792 class EnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
794 self
._ft
= bt2
.EnumerationFieldType(size
=32, is_signed
=True)
795 self
._ft
.append_mapping('whole range', -(2 ** 31), (2 ** 31) - 1)
796 self
._ft
.append_mapping('something', 17)
797 self
._ft
.append_mapping('speaker', 12, 16)
798 self
._ft
.append_mapping('can', 18, 2540)
799 self
._ft
.append_mapping('zip', -45, 1001)
800 self
._def
= self
._ft
()
803 self
._def
_new
_value
= -101
809 def test_mappings(self
):
811 ('whole range', -(2 ** 31), (2 ** 31) - 1),
812 ('something', 17, 17),
819 for fm
in self
._def
.mappings
:
821 for index
, mapping
in enumerate(mappings
):
822 if fm
.name
== mapping
[0] and fm
.lower
== mapping
[1] and fm
.upper
== mapping
[2]:
825 self
.assertEqual(total
, 3)
826 self
.assertTrue(0 in index_set
and 1 in index_set
and 2 in index_set
)
828 def test_str_op(self
):
829 expected_string_found
= False
832 # Establish all permutations of the three expected matches since
833 # the order in which mappings are enumerated is not explicitly part of
835 for p
in itertools
.permutations(["'whole range'", "'something'",
837 candidate
= '{} ({})'.format(self
._def
_value
, ', '.join(p
))
839 expected_string_found
= True
842 self
.assertTrue(expected_string_found
)
844 def test_str_op_unset(self
):
845 self
.assertEqual(str(self
._ft
()), 'Unset')
848 class FloatingPointNumberFieldTestCase(_TestNumericField
, unittest
.TestCase
):
850 self
._ft
= bt2
.FloatingPointNumberFieldType()
851 self
._field
= self
._ft
()
852 self
._def
= self
._ft
()
853 self
._def
.value
= 52.7
854 self
._def
_value
= 52.7
855 self
._def
_new
_value
= -17.164857
862 def _test_invalid_op(self
, cb
):
863 with self
.assertRaises(TypeError):
866 def test_assign_true(self
):
867 self
._def
.value
= True
868 self
.assertTrue(self
._def
)
870 def test_assign_false(self
):
871 self
._def
.value
= False
872 self
.assertFalse(self
._def
)
874 def test_assign_pos_int(self
):
876 self
._def
.value
= raw
877 self
.assertEqual(self
._def
, float(raw
))
879 def test_assign_neg_int(self
):
881 self
._def
.value
= raw
882 self
.assertEqual(self
._def
, float(raw
))
884 def test_assign_int_field(self
):
885 ft
= bt2
.IntegerFieldType(32)
889 self
._def
.value
= field
890 self
.assertEqual(self
._def
, float(raw
))
892 def test_assign_float(self
):
894 self
._def
.value
= raw
895 self
.assertEqual(self
._def
, raw
)
897 def test_assign_float_field(self
):
898 ft
= bt2
.FloatingPointNumberFieldType(32)
902 self
._def
.value
= field
903 self
.assertEqual(self
._def
, raw
)
905 def test_assign_invalid_type(self
):
906 with self
.assertRaises(TypeError):
907 self
._def
.value
= 'yes'
909 def test_invalid_lshift(self
):
910 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
912 def test_invalid_rshift(self
):
913 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
915 def test_invalid_and(self
):
916 self
._test
_invalid
_op
(lambda: self
._def
& 23)
918 def test_invalid_or(self
):
919 self
._test
_invalid
_op
(lambda: self
._def |
23)
921 def test_invalid_xor(self
):
922 self
._test
_invalid
_op
(lambda: self
._def ^
23)
924 def test_invalid_invert(self
):
925 self
._test
_invalid
_op
(lambda: ~self
._def
)
927 def test_str_op(self
):
928 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
930 def test_str_op_unset(self
):
931 self
.assertEqual(str(self
._ft
()), 'Unset')
933 _inject_numeric_testing_methods(FloatingPointNumberFieldTestCase
)
936 class StringFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
938 self
._ft
= bt2
.StringFieldType()
939 self
._def
_value
= 'Hello, World!'
940 self
._def
= self
._ft
()
941 self
._def
.value
= self
._def
_value
942 self
._def
_new
_value
= 'Yes!'
948 def test_assign_int(self
):
949 with self
.assertRaises(TypeError):
950 self
._def
.value
= 283
952 def test_assign_string_field(self
):
953 ft
= bt2
.StringFieldType()
957 self
.assertEqual(field
, raw
)
960 self
.assertEqual(self
._def
, self
._def
_value
)
963 self
.assertNotEqual(self
._def
, 23)
965 def test_lt_vstring(self
):
970 self
.assertLess(s1
, s2
)
972 def test_lt_string(self
):
975 self
.assertLess(s1
, 'bateau')
977 def test_le_vstring(self
):
982 self
.assertLessEqual(s1
, s2
)
984 def test_le_string(self
):
987 self
.assertLessEqual(s1
, 'bateau')
989 def test_gt_vstring(self
):
994 self
.assertGreater(s2
, s1
)
996 def test_gt_string(self
):
999 self
.assertGreater('bateau', s1
)
1001 def test_ge_vstring(self
):
1006 self
.assertGreaterEqual(s2
, s1
)
1008 def test_ge_string(self
):
1011 self
.assertGreaterEqual('bateau', s1
)
1013 def test_bool_op(self
):
1014 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1016 def test_str_op(self
):
1017 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1019 def test_str_op_unset(self
):
1020 self
.assertEqual(str(self
._ft
()), 'Unset')
1023 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1025 def test_getitem(self
):
1026 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1028 def test_append_str(self
):
1029 to_append
= 'meow meow meow'
1030 self
._def
+= to_append
1031 self
._def
_value
+= to_append
1032 self
.assertEqual(self
._def
, self
._def
_value
)
1034 def test_append_string_field(self
):
1035 ft
= bt2
.StringFieldType()
1037 to_append
= 'meow meow meow'
1038 field
.value
= to_append
1040 self
._def
_value
+= to_append
1041 self
.assertEqual(self
._def
, self
._def
_value
)
1043 def test_is_set(self
):
1044 raw
= self
._def
_value
1046 self
.assertFalse(field
.is_set
)
1048 self
.assertTrue(field
.is_set
)
1050 def test_reset(self
):
1051 raw
= self
._def
_value
1054 self
.assertTrue(field
.is_set
)
1056 self
.assertFalse(field
.is_set
)
1058 self
.assertEqual(other
, field
)
1061 class _TestArraySequenceFieldCommon(_TestCopySimple
):
1062 def _modify_def(self
):
1065 def test_bool_op_true(self
):
1066 self
.assertTrue(self
._def
)
1069 self
.assertEqual(len(self
._def
), 3)
1071 def test_getitem(self
):
1072 field
= self
._def
[1]
1073 self
.assertIs(type(field
), bt2
.fields
._IntegerField
)
1074 self
.assertEqual(field
, 1847)
1077 ft
= bt2
.ArrayFieldType(self
._elem
_ft
, 3)
1082 self
.assertEqual(self
._def
, field
)
1084 def test_eq_invalid_type(self
):
1085 self
.assertNotEqual(self
._def
, 23)
1087 def test_eq_diff_len(self
):
1088 ft
= bt2
.ArrayFieldType(self
._elem
_ft
, 2)
1092 self
.assertNotEqual(self
._def
, field
)
1094 def test_eq_diff_content_same_len(self
):
1095 ft
= bt2
.ArrayFieldType(self
._elem
_ft
, 3)
1100 self
.assertNotEqual(self
._def
, field
)
1102 def test_setitem(self
):
1104 self
.assertEqual(self
._def
[2], 24)
1106 def test_setitem_int_field(self
):
1107 int_field
= self
._elem
_ft
()
1108 int_field
.value
= 19487
1109 self
._def
[1] = int_field
1110 self
.assertEqual(self
._def
[1], 19487)
1112 def test_setitem_non_basic_field(self
):
1113 elem_ft
= bt2
.StructureFieldType()
1114 array_ft
= bt2
.ArrayFieldType(elem_ft
, 3)
1115 elem_field
= elem_ft()
1116 array_field
= array_ft()
1118 with self
.assertRaises(TypeError):
1121 def test_setitem_none(self
):
1122 with self
.assertRaises(TypeError):
1125 def test_setitem_index_wrong_type(self
):
1126 with self
.assertRaises(TypeError):
1127 self
._def
['yes'] = 23
1129 def test_setitem_index_neg(self
):
1130 with self
.assertRaises(IndexError):
1133 def test_setitem_index_out_of_range(self
):
1134 with self
.assertRaises(IndexError):
1135 self
._def
[len(self
._def
)] = 134679
1137 def test_iter(self
):
1138 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1139 self
.assertEqual(field
, value
)
1141 def test_value_int_field(self
):
1142 values
= [45646, 145, 12145]
1143 self
._def
.value
= values
1144 self
.assertEqual(values
, self
._def
)
1146 def test_value_unset(self
):
1147 values
= [45646, None, 12145]
1148 self
._def
.value
= values
1149 self
.assertFalse(self
._def
[1].is_set
)
1151 def test_value_rollback(self
):
1152 values
= [45, 1847, 1948754]
1153 # value is out of range, should not affect those we set previously
1154 with self
.assertRaises(bt2
.Error
):
1155 self
._def
[2].value
= 2**60
1156 self
.assertEqual(values
, self
._def
)
1158 def test_value_check_sequence(self
):
1160 with self
.assertRaises(TypeError):
1161 self
._def
.value
= values
1163 def test_value_wrong_type_in_sequence(self
):
1164 values
= [32, 'hello', 11]
1165 with self
.assertRaises(TypeError):
1166 self
._def
.value
= values
1168 def test_value_complex_type(self
):
1169 struct_ft
= bt2
.StructureFieldType()
1170 int_ft
= bt2
.IntegerFieldType(32)
1171 str_ft
= bt2
.StringFieldType()
1172 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1173 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1174 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1175 array_ft
= bt2
.ArrayFieldType(struct_ft
, 3)
1179 'a_string': 'hello',
1184 'a_string': 'goodbye',
1189 'a_string': 'or not',
1195 array
.value
= values
1196 self
.assertEqual(values
, array
)
1197 values
[0]['an_int'] = 'a string'
1198 with self
.assertRaises(TypeError):
1199 array
.value
= values
1201 def test_is_set(self
):
1202 raw
= self
._def
_value
1204 self
.assertFalse(field
.is_set
)
1206 self
.assertTrue(field
.is_set
)
1208 def test_reset(self
):
1209 raw
= self
._def
_value
1212 self
.assertTrue(field
.is_set
)
1214 self
.assertFalse(field
.is_set
)
1216 self
.assertEqual(other
, field
)
1218 def test_str_op(self
):
1220 expected_string
= '[{}]'.format(', '.join(
1221 [repr(v
) for v
in self
._def
_value
]))
1222 self
.assertEqual(expected_string
, s
)
1224 def test_str_op_unset(self
):
1225 self
.assertEqual(str(self
._ft
()), 'Unset')
1228 class ArrayFieldTestCase(_TestArraySequenceFieldCommon
, unittest
.TestCase
):
1230 self
._elem
_ft
= bt2
.IntegerFieldType(32)
1231 self
._ft
= bt2
.ArrayFieldType(self
._elem
_ft
, 3)
1232 self
._def
= self
._ft
()
1235 self
._def
[2] = 1948754
1236 self
._def
_value
= [45, 1847, 1948754]
1243 def test_value_wrong_len(self
):
1245 with self
.assertRaises(ValueError):
1246 self
._def
.value
= values
1249 class SequenceFieldTestCase(_TestArraySequenceFieldCommon
, unittest
.TestCase
):
1251 self
._elem
_ft
= bt2
.IntegerFieldType(32)
1252 self
._ft
= bt2
.SequenceFieldType(self
._elem
_ft
, 'the.length')
1253 self
._def
= self
._ft
()
1254 self
._length
_field
= self
._elem
_ft
(3)
1255 self
._def
.length_field
= self
._length
_field
1258 self
._def
[2] = 1948754
1259 self
._def
_value
= [45, 1847, 1948754]
1265 del self
._length
_field
1267 def test_value_resize(self
):
1268 new_values
= [1, 2, 3, 4]
1269 self
._def
.value
= new_values
1270 self
.assertCountEqual(self
._def
, new_values
)
1272 def test_value_resize_rollback(self
):
1273 with self
.assertRaises(TypeError):
1274 self
._def
.value
= [1, 2, 3, 'unexpected string']
1275 self
.assertEqual(self
._def
, self
._def
_value
)
1278 with self
.assertRaises(TypeError):
1279 self
._def
.value
= [1, 2, 3, 'unexpected string']
1280 self
.assertFalse(self
._def
.is_set
)
1283 class StructureFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
1285 self
._ft
0 = bt2
.IntegerFieldType(32, is_signed
=True)
1286 self
._ft
1 = bt2
.StringFieldType()
1287 self
._ft
2 = bt2
.FloatingPointNumberFieldType()
1288 self
._ft
3 = bt2
.IntegerFieldType(17)
1289 self
._ft
= bt2
.StructureFieldType()
1290 self
._ft
.append_field('A', self
._ft
0)
1291 self
._ft
.append_field('B', self
._ft
1)
1292 self
._ft
.append_field('C', self
._ft
2)
1293 self
._ft
.append_field('D', self
._ft
3)
1294 self
._def
= self
._ft
()
1295 self
._def
['A'] = -1872
1296 self
._def
['B'] = 'salut'
1297 self
._def
['C'] = 17.5
1298 self
._def
['D'] = 16497
1314 def _modify_def(self
):
1315 self
._def
['B'] = 'hola'
1317 def test_bool_op_true(self
):
1318 self
.assertTrue(self
._def
)
1320 def test_bool_op_false(self
):
1321 ft
= bt2
.StructureFieldType()
1323 self
.assertFalse(field
)
1326 self
.assertEqual(len(self
._def
), 4)
1328 def test_getitem(self
):
1329 field
= self
._def
['A']
1330 self
.assertIs(type(field
), bt2
.fields
._IntegerField
)
1331 self
.assertEqual(field
, -1872)
1333 def test_at_index_out_of_bounds_after(self
):
1334 with self
.assertRaises(IndexError):
1335 self
._def
.at_index(len(self
._ft
))
1338 ft
= bt2
.StructureFieldType()
1339 ft
.append_field('A', self
._ft
0)
1340 ft
.append_field('B', self
._ft
1)
1341 ft
.append_field('C', self
._ft
2)
1342 ft
.append_field('D', self
._ft
3)
1345 field
['B'] = 'salut'
1348 self
.assertEqual(self
._def
, field
)
1350 def test_eq_invalid_type(self
):
1351 self
.assertNotEqual(self
._def
, 23)
1353 def test_eq_diff_len(self
):
1354 ft
= bt2
.StructureFieldType()
1355 ft
.append_field('A', self
._ft
0)
1356 ft
.append_field('B', self
._ft
1)
1357 ft
.append_field('C', self
._ft
2)
1360 field
['B'] = 'salut'
1362 self
.assertNotEqual(self
._def
, field
)
1364 def test_eq_diff_content_same_len(self
):
1365 ft
= bt2
.StructureFieldType()
1366 ft
.append_field('A', self
._ft
0)
1367 ft
.append_field('B', self
._ft
1)
1368 ft
.append_field('C', self
._ft
2)
1369 ft
.append_field('D', self
._ft
3)
1372 field
['B'] = 'salut'
1375 self
.assertNotEqual(self
._def
, field
)
1377 def test_eq_same_content_diff_keys(self
):
1378 ft
= bt2
.StructureFieldType()
1379 ft
.append_field('A', self
._ft
0)
1380 ft
.append_field('B', self
._ft
1)
1381 ft
.append_field('E', self
._ft
2)
1382 ft
.append_field('D', self
._ft
3)
1385 field
['B'] = 'salut'
1388 self
.assertNotEqual(self
._def
, field
)
1390 def test_setitem(self
):
1391 self
._def
['C'] = -18.47
1392 self
.assertEqual(self
._def
['C'], -18.47)
1394 def test_setitem_int_field(self
):
1395 int_ft
= bt2
.IntegerFieldType(16)
1396 int_field
= int_ft()
1397 int_field
.value
= 19487
1398 self
._def
['D'] = int_field
1399 self
.assertEqual(self
._def
['D'], 19487)
1401 def test_setitem_non_basic_field(self
):
1402 elem_ft
= bt2
.StructureFieldType()
1403 elem_field
= elem_ft()
1404 struct_ft
= bt2
.StructureFieldType()
1405 struct_ft
.append_field('A', elem_ft
)
1406 struct_field
= struct_ft()
1408 # Will fail on access to .items() of the value
1409 with self
.assertRaises(AttributeError):
1410 struct_field
['A'] = 23
1412 def test_setitem_none(self
):
1413 with self
.assertRaises(TypeError):
1414 self
._def
['C'] = None
1416 def test_setitem_key_wrong_type(self
):
1417 with self
.assertRaises(TypeError):
1420 def test_setitem_wrong_key(self
):
1421 with self
.assertRaises(KeyError):
1422 self
._def
['hi'] = 134679
1424 def test_at_index(self
):
1425 self
.assertEqual(self
._def
.at_index(1), 'salut')
1427 def test_iter(self
):
1435 for vkey
, vval
in self
._def
.items():
1436 val
= orig_values
[vkey
]
1437 self
.assertEqual(vval
, val
)
1439 def test_value(self
):
1446 self
.assertEqual(self
._def
, orig_values
)
1448 def test_set_value(self
):
1449 int_ft
= bt2
.IntegerFieldType(32)
1450 str_ft
= bt2
.StringFieldType()
1451 struct_ft
= bt2
.StructureFieldType()
1452 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1453 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1454 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1457 'a_string': 'hello',
1461 struct
= struct_ft()
1462 struct
.value
= values
1463 self
.assertEqual(values
, struct
)
1465 bad_type_values
= copy
.deepcopy(values
)
1466 bad_type_values
['an_int'] = 'a string'
1467 with self
.assertRaises(TypeError):
1468 struct
.value
= bad_type_values
1470 unknown_key_values
= copy
.deepcopy(values
)
1471 unknown_key_values
['unknown_key'] = 16546
1472 with self
.assertRaises(KeyError):
1473 struct
.value
= unknown_key_values
1475 def test_value_rollback(self
):
1476 int_ft
= bt2
.IntegerFieldType(32)
1477 str_ft
= bt2
.StringFieldType()
1478 struct_ft
= bt2
.StructureFieldType()
1479 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1480 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1481 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1484 'a_string': 'hello',
1488 def test_is_set(self
):
1491 'a_string': 'hello',
1495 int_ft
= bt2
.IntegerFieldType(32)
1496 str_ft
= bt2
.StringFieldType()
1497 struct_ft
= bt2
.StructureFieldType()
1498 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1499 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1500 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1502 struct
= struct_ft()
1503 self
.assertFalse(struct
.is_set
)
1504 struct
.value
= values
1505 self
.assertTrue(struct
.is_set
)
1507 struct
= struct_ft()
1508 struct
['an_int'].value
= 42
1509 self
.assertFalse(struct
.is_set
)
1511 def test_reset(self
):
1514 'a_string': 'hello',
1518 int_ft
= bt2
.IntegerFieldType(32)
1519 str_ft
= bt2
.StringFieldType()
1520 struct_ft
= bt2
.StructureFieldType()
1521 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1522 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1523 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1525 struct
= struct_ft()
1526 struct
.value
= values
1527 self
.assertTrue(struct
.is_set
)
1529 self
.assertEqual(struct_ft(), struct
)
1531 def test_str_op(self
):
1532 expected_string_found
= False
1534 # Establish all permutations of the three expected matches since
1535 # the order in which mappings are enumerated is not explicitly part of
1537 for p
in itertools
.permutations([(k
, v
) for k
, v
in self
._def
.items()]):
1538 items
= ['{}: {}'.format(repr(k
), repr(v
)) for k
, v
in p
]
1539 candidate
= '{{{}}}'.format(', '.join(items
))
1541 expected_string_found
= True
1544 self
.assertTrue(expected_string_found
)
1546 def test_str_op_unset(self
):
1547 self
.assertEqual(str(self
._ft
()), 'Unset')
1550 class VariantFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
1552 self
._tag
_ft
= bt2
.EnumerationFieldType(size
=32)
1553 self
._tag
_ft
.append_mapping('corner', 23)
1554 self
._tag
_ft
.append_mapping('zoom', 17, 20)
1555 self
._tag
_ft
.append_mapping('mellotron', 1001)
1556 self
._tag
_ft
.append_mapping('giorgio', 2000, 3000)
1557 self
._ft
0 = bt2
.IntegerFieldType(32, is_signed
=True)
1558 self
._ft
1 = bt2
.StringFieldType()
1559 self
._ft
2 = bt2
.FloatingPointNumberFieldType()
1560 self
._ft
3 = bt2
.IntegerFieldType(17)
1561 self
._ft
= bt2
.VariantFieldType('salut', self
._tag
_ft
)
1562 self
._ft
.append_field('corner', self
._ft
0)
1563 self
._ft
.append_field('zoom', self
._ft
1)
1564 self
._ft
.append_field('mellotron', self
._ft
2)
1565 self
._ft
.append_field('giorgio', self
._ft
3)
1566 self
._def
= self
._ft
()
1577 def test_bool_op_true(self
):
1578 tag_field
= self
._tag
_ft
(1001)
1579 self
._def
.field(tag_field
).value
= -17.34
1580 self
.assertTrue(self
._def
)
1582 def test_bool_op_false(self
):
1583 self
.assertFalse(self
._def
)
1585 def test_tag_field_none(self
):
1586 self
.assertIsNone(self
._def
.tag_field
)
1588 def test_tag_field(self
):
1589 tag_field
= self
._tag
_ft
(2800)
1590 self
._def
.field(tag_field
).value
= 1847
1591 self
.assertEqual(self
._def
.tag_field
, tag_field
)
1592 self
.assertEqual(self
._def
.tag_field
.addr
, tag_field
.addr
)
1594 def test_selected_field_none(self
):
1595 self
.assertIsNone(self
._def
.selected_field
)
1597 def test_selected_field(self
):
1598 var_field1
= self
._ft
()
1599 tag_field1
= self
._tag
_ft
(1001)
1600 var_field1
.field(tag_field1
).value
= -17.34
1601 self
.assertEqual(var_field1
.field(), -17.34)
1602 self
.assertEqual(var_field1
.selected_field
, -17.34)
1603 var_field2
= self
._ft
()
1604 tag_field2
= self
._tag
_ft
(2500)
1605 var_field2
.field(tag_field2
).value
= 1921
1606 self
.assertEqual(var_field2
.field(), 1921)
1607 self
.assertEqual(var_field2
.selected_field
, 1921)
1610 tag_ft
= bt2
.EnumerationFieldType(size
=32)
1611 tag_ft
.append_mapping('corner', 23)
1612 tag_ft
.append_mapping('zoom', 17, 20)
1613 tag_ft
.append_mapping('mellotron', 1001)
1614 tag_ft
.append_mapping('giorgio', 2000, 3000)
1615 ft0
= bt2
.IntegerFieldType(32, is_signed
=True)
1616 ft1
= bt2
.StringFieldType()
1617 ft2
= bt2
.FloatingPointNumberFieldType()
1618 ft3
= bt2
.IntegerFieldType(17)
1619 ft
= bt2
.VariantFieldType('salut', tag_ft
)
1620 ft
.append_field('corner', ft0
)
1621 ft
.append_field('zoom', ft1
)
1622 ft
.append_field('mellotron', ft2
)
1623 ft
.append_field('giorgio', ft3
)
1625 field_tag
= tag_ft(23)
1626 def_tag
= self
._tag
_ft
(23)
1627 field
.field(field_tag
).value
= 1774
1628 self
._def
.field(def_tag
).value
= 1774
1629 self
.assertEqual(self
._def
, field
)
1631 def test_eq_invalid_type(self
):
1632 self
.assertNotEqual(self
._def
, 23)
1634 def test_is_set(self
):
1635 self
.assertFalse(self
._def
.is_set
)
1636 tag_field
= self
._tag
_ft
(2800)
1637 self
._def
.field(tag_field
).value
= 684
1638 self
.assertTrue(self
._def
.is_set
)
1640 def test_reset(self
):
1641 tag_field
= self
._tag
_ft
(2800)
1642 self
._def
.field(tag_field
).value
= 684
1644 self
.assertFalse(self
._def
.is_set
)
1645 self
.assertIsNone(self
._def
.selected_field
)
1646 self
.assertIsNone(self
._def
.tag_field
)
1648 def test_str_op_int(self
):
1650 v
.field(self
._tag
_ft
(23)).value
= 42
1652 self
.assertEqual(str(f
), str(v
))
1654 def test_str_op_str(self
):
1656 v
.field(self
._tag
_ft
(18)).value
= 'some test string'
1657 f
= self
._ft
1('some test string')
1658 self
.assertEqual(str(f
), str(v
))
1660 def test_str_op_flt(self
):
1662 v
.field(self
._tag
_ft
(1001)).value
= 14.4245
1663 f
= self
._ft
2(14.4245)
1664 self
.assertEqual(str(f
), str(v
))
1666 def test_str_op_unset(self
):
1667 self
.assertEqual(str(self
._ft
()), 'Unset')