1 from functools
import partial
, partialmethod
10 class _TestCopySimple
:
12 cpy
= copy
.copy(self
._def
)
13 self
.assertIsNot(cpy
, self
._def
)
14 self
.assertNotEqual(cpy
.addr
, self
._def
.addr
)
15 self
.assertEqual(cpy
, self
._def
)
17 def test_deepcopy(self
):
18 cpy
= copy
.deepcopy(self
._def
)
19 self
.assertIsNot(cpy
, self
._def
)
20 self
.assertNotEqual(cpy
.addr
, self
._def
.addr
)
21 self
.assertEqual(cpy
, self
._def
)
30 class _TestNumericField(_TestCopySimple
):
31 def _binop(self
, op
, rhs
):
36 if isinstance(rhs
, (bt2
.fields
._IntegerField
, bt2
.fields
._FloatingPointNumberField
)):
37 comp_value
= copy
.copy(rhs
)
40 r
= op(self
._def
, rhs
)
41 except Exception as e
:
45 rv
= op(self
._def
_value
, comp_value
)
46 except Exception as e
:
49 if rexc
is not None or rvexc
is not None:
50 # at least one of the operations raised an exception: in
51 # this case both operations should have raised the same
52 # type of exception (division by zero, bit shift with a
53 # floating point number operand, etc.)
54 self
.assertIs(type(rexc
), type(rvexc
))
59 def _unaryop(self
, op
):
65 except Exception as e
:
69 rv
= op(self
._def
_value
)
70 except Exception as e
:
73 if rexc
is not None or rvexc
is not None:
74 # at least one of the operations raised an exception: in
75 # this case both operations should have raised the same
76 # type of exception (division by zero, bit shift with a
77 # floating point number operand, etc.)
78 self
.assertIs(type(rexc
), type(rvexc
))
83 def _test_unaryop_type(self
, op
):
84 r
, rv
= self
._unaryop
(op
)
89 self
.assertIsInstance(r
, type(rv
))
91 def _test_unaryop_value(self
, op
):
92 r
, rv
= self
._unaryop
(op
)
97 self
.assertEqual(r
, rv
)
99 def _test_unaryop_addr_same(self
, op
):
100 addr_before
= self
._def
.addr
102 self
.assertEqual(self
._def
.addr
, addr_before
)
104 def _test_unaryop_value_same(self
, op
):
105 value_before
= copy
.copy(self
._def
_value
)
107 self
.assertEqual(self
._def
, value_before
)
109 def _test_binop_type(self
, op
, rhs
):
110 r
, rv
= self
._binop
(op
, rhs
)
115 if op
in _COMP_BINOPS
:
116 # __eq__() and __ne__() always return a 'bool' object
117 self
.assertIsInstance(r
, bool)
119 self
.assertIsInstance(r
, type(rv
))
121 def _test_binop_value(self
, op
, rhs
):
122 r
, rv
= self
._binop
(op
, rhs
)
127 self
.assertEqual(r
, rv
)
129 def _test_binop_lhs_addr_same(self
, op
, rhs
):
130 addr_before
= self
._def
.addr
131 r
, rv
= self
._binop
(op
, rhs
)
132 self
.assertEqual(self
._def
.addr
, addr_before
)
134 def _test_binop_lhs_value_same(self
, op
, rhs
):
135 value_before
= copy
.copy(self
._def
)
136 r
, rv
= self
._binop
(op
, rhs
)
137 self
.assertEqual(self
._def
, value_before
)
139 def _test_binop_invalid_unknown(self
, op
):
140 if op
in _COMP_BINOPS
:
141 self
.skipTest('not testing')
146 with self
.assertRaises(TypeError):
149 def _test_binop_invalid_none(self
, op
):
150 if op
in _COMP_BINOPS
:
151 self
.skipTest('not testing')
153 with self
.assertRaises(TypeError):
156 def _test_ibinop_value(self
, op
, rhs
):
157 r
, rv
= self
._binop
(op
, rhs
)
162 # The inplace operators are special for field objects because
163 # they do not return a new, immutable object like it's the case
164 # for Python numbers. In Python, `a += 2`, where `a` is a number
165 # object, assigns a new number object reference to `a`, dropping
166 # the old reference. Since BT's field objects are mutable, we
167 # modify their internal value with the inplace operators. This
168 # means however that we can lose data in the process, for
171 # int_value_obj += 3.3
173 # Here, if `int_value_obj` is a Python `int` with the value 2,
174 # it would be a `float` object after this, holding the value
175 # 5.3. In our case, if `int_value_obj` is an integer field
176 # object, 3.3 is converted to an `int` object (3) and added to
177 # the current value of `int_value_obj`, so after this the value
178 # of the object is 5. This does not compare to 5.3, which is
179 # why we also use the `int()` type here.
180 if isinstance(self
._def
, bt2
.fields
._IntegerField
):
183 self
.assertEqual(r
, rv
)
185 def _test_ibinop_type(self
, op
, rhs
):
186 r
, rv
= self
._binop
(op
, rhs
)
191 self
.assertIs(r
, self
._def
)
193 def _test_ibinop_invalid_unknown(self
, op
):
197 with self
.assertRaises(TypeError):
200 def _test_ibinop_invalid_none(self
, op
):
201 with self
.assertRaises(TypeError):
204 def _test_binop_rhs_false(self
, test_cb
, op
):
207 def _test_binop_rhs_true(self
, test_cb
, op
):
210 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
213 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
216 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
219 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
220 test_cb(op
, bt2
.create_value(2))
222 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
223 test_cb(op
, bt2
.create_value(-23))
225 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
226 test_cb(op
, bt2
.create_value(0))
228 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
231 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
234 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
237 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
238 test_cb(op
, bt2
.create_value(2.2))
240 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
241 test_cb(op
, bt2
.create_value(-23.4))
243 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
244 test_cb(op
, bt2
.create_value(0.0))
246 def _test_binop_type_false(self
, op
):
247 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
249 def _test_binop_type_true(self
, op
):
250 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
252 def _test_binop_type_pos_int(self
, op
):
253 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
255 def _test_binop_type_neg_int(self
, op
):
256 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
258 def _test_binop_type_zero_int(self
, op
):
259 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
261 def _test_binop_type_pos_vint(self
, op
):
262 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
264 def _test_binop_type_neg_vint(self
, op
):
265 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
267 def _test_binop_type_zero_vint(self
, op
):
268 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
270 def _test_binop_type_pos_float(self
, op
):
271 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
273 def _test_binop_type_neg_float(self
, op
):
274 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
276 def _test_binop_type_zero_float(self
, op
):
277 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
279 def _test_binop_type_pos_vfloat(self
, op
):
280 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
282 def _test_binop_type_neg_vfloat(self
, op
):
283 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
285 def _test_binop_type_zero_vfloat(self
, op
):
286 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
288 def _test_binop_value_false(self
, op
):
289 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
291 def _test_binop_value_true(self
, op
):
292 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
294 def _test_binop_value_pos_int(self
, op
):
295 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
297 def _test_binop_value_neg_int(self
, op
):
298 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
300 def _test_binop_value_zero_int(self
, op
):
301 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
303 def _test_binop_value_pos_vint(self
, op
):
304 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
306 def _test_binop_value_neg_vint(self
, op
):
307 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
309 def _test_binop_value_zero_vint(self
, op
):
310 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
312 def _test_binop_value_pos_float(self
, op
):
313 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
315 def _test_binop_value_neg_float(self
, op
):
316 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
318 def _test_binop_value_zero_float(self
, op
):
319 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
321 def _test_binop_value_pos_vfloat(self
, op
):
322 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
324 def _test_binop_value_neg_vfloat(self
, op
):
325 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
327 def _test_binop_value_zero_vfloat(self
, op
):
328 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
330 def _test_binop_lhs_addr_same_false(self
, op
):
331 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
333 def _test_binop_lhs_addr_same_true(self
, op
):
334 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
336 def _test_binop_lhs_addr_same_pos_int(self
, op
):
337 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
339 def _test_binop_lhs_addr_same_neg_int(self
, op
):
340 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
342 def _test_binop_lhs_addr_same_zero_int(self
, op
):
343 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
345 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
346 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
348 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
349 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
351 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
352 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
354 def _test_binop_lhs_addr_same_pos_float(self
, op
):
355 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
357 def _test_binop_lhs_addr_same_neg_float(self
, op
):
358 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
360 def _test_binop_lhs_addr_same_zero_float(self
, op
):
361 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
363 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
364 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
366 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
367 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
369 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
370 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
372 def _test_binop_lhs_value_same_false(self
, op
):
373 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
375 def _test_binop_lhs_value_same_true(self
, op
):
376 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
378 def _test_binop_lhs_value_same_pos_int(self
, op
):
379 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
381 def _test_binop_lhs_value_same_neg_int(self
, op
):
382 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
384 def _test_binop_lhs_value_same_zero_int(self
, op
):
385 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
387 def _test_binop_lhs_value_same_pos_vint(self
, op
):
388 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
390 def _test_binop_lhs_value_same_neg_vint(self
, op
):
391 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
393 def _test_binop_lhs_value_same_zero_vint(self
, op
):
394 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
396 def _test_binop_lhs_value_same_pos_float(self
, op
):
397 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
399 def _test_binop_lhs_value_same_neg_float(self
, op
):
400 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
402 def _test_binop_lhs_value_same_zero_float(self
, op
):
403 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
405 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
406 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
408 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
409 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
411 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
412 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
414 def _test_ibinop_type_false(self
, op
):
415 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_type
, op
)
417 def _test_ibinop_type_true(self
, op
):
418 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_type
, op
)
420 def _test_ibinop_type_pos_int(self
, op
):
421 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_type
, op
)
423 def _test_ibinop_type_neg_int(self
, op
):
424 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_type
, op
)
426 def _test_ibinop_type_zero_int(self
, op
):
427 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_type
, op
)
429 def _test_ibinop_type_pos_vint(self
, op
):
430 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_type
, op
)
432 def _test_ibinop_type_neg_vint(self
, op
):
433 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_type
, op
)
435 def _test_ibinop_type_zero_vint(self
, op
):
436 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_type
, op
)
438 def _test_ibinop_type_pos_float(self
, op
):
439 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_type
, op
)
441 def _test_ibinop_type_neg_float(self
, op
):
442 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_type
, op
)
444 def _test_ibinop_type_zero_float(self
, op
):
445 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_type
, op
)
447 def _test_ibinop_type_pos_vfloat(self
, op
):
448 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_type
, op
)
450 def _test_ibinop_type_neg_vfloat(self
, op
):
451 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_type
, op
)
453 def _test_ibinop_type_zero_vfloat(self
, op
):
454 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_type
, op
)
456 def _test_ibinop_value_false(self
, op
):
457 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_value
, op
)
459 def _test_ibinop_value_true(self
, op
):
460 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_value
, op
)
462 def _test_ibinop_value_pos_int(self
, op
):
463 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_value
, op
)
465 def _test_ibinop_value_neg_int(self
, op
):
466 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_value
, op
)
468 def _test_ibinop_value_zero_int(self
, op
):
469 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_value
, op
)
471 def _test_ibinop_value_pos_vint(self
, op
):
472 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_value
, op
)
474 def _test_ibinop_value_neg_vint(self
, op
):
475 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_value
, op
)
477 def _test_ibinop_value_zero_vint(self
, op
):
478 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_value
, op
)
480 def _test_ibinop_value_pos_float(self
, op
):
481 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_value
, op
)
483 def _test_ibinop_value_neg_float(self
, op
):
484 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_value
, op
)
486 def _test_ibinop_value_zero_float(self
, op
):
487 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_value
, op
)
489 def _test_ibinop_value_pos_vfloat(self
, op
):
490 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_value
, op
)
492 def _test_ibinop_value_neg_vfloat(self
, op
):
493 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_value
, op
)
495 def _test_ibinop_value_zero_vfloat(self
, op
):
496 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_value
, op
)
498 def test_bool_op(self
):
499 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
501 def test_int_op(self
):
502 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
504 def test_float_op(self
):
505 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
507 def test_complex_op(self
):
508 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
510 def test_str_op(self
):
511 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
513 def test_eq_none(self
):
514 self
.assertFalse(self
._def
== None)
516 def test_ne_none(self
):
517 self
.assertTrue(self
._def
!= None)
519 def test_is_set(self
):
520 raw
= self
._def
_value
522 self
.assertFalse(field
.is_set
)
524 self
.assertTrue(field
.is_set
)
526 def test_reset(self
):
527 raw
= self
._def
_value
530 self
.assertTrue(field
.is_set
)
532 self
.assertFalse(field
.is_set
)
534 self
.assertEqual(other
, field
)
544 ('add', operator
.add
),
545 ('radd', lambda a
, b
: operator
.add(b
, a
)),
546 ('and', operator
.and_
),
547 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
548 ('floordiv', operator
.floordiv
),
549 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
550 ('lshift', operator
.lshift
),
551 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
552 ('mod', operator
.mod
),
553 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
554 ('mul', operator
.mul
),
555 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
556 ('or', operator
.or_
),
557 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
558 ('pow', operator
.pow),
559 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
560 ('rshift', operator
.rshift
),
561 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
562 ('sub', operator
.sub
),
563 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
564 ('truediv', operator
.truediv
),
565 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
566 ('xor', operator
.xor
),
567 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
572 ('iadd', operator
.iadd
),
573 ('iand', operator
.iand
),
574 ('ifloordiv', operator
.ifloordiv
),
575 ('ilshift', operator
.ilshift
),
576 ('imod', operator
.imod
),
577 ('imul', operator
.imul
),
578 ('ior', operator
.ior
),
579 ('ipow', operator
.ipow
),
580 ('irshift', operator
.irshift
),
581 ('isub', operator
.isub
),
582 ('itruediv', operator
.itruediv
),
583 ('ixor', operator
.ixor
),
588 ('neg', operator
.neg
),
589 ('pos', operator
.pos
),
590 ('abs', operator
.abs),
591 ('invert', operator
.invert
),
593 ('round_0', partial(round, ndigits
=0)),
594 ('round_1', partial(round, ndigits
=1)),
595 ('round_2', partial(round, ndigits
=2)),
596 ('round_3', partial(round, ndigits
=3)),
598 ('floor', math
.floor
),
599 ('trunc', math
.trunc
),
603 def _inject_numeric_testing_methods(cls
):
604 def test_binop_name(suffix
):
605 return 'test_binop_{}_{}'.format(name
, suffix
)
607 def test_ibinop_name(suffix
):
608 return 'test_ibinop_{}_{}'.format(name
, suffix
)
610 def test_unaryop_name(suffix
):
611 return 'test_unaryop_{}_{}'.format(name
, suffix
)
613 # inject testing methods for each binary operation
614 for name
, binop
in _BINOPS
:
615 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_binop
_invalid
_unknown
, op
=binop
))
616 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericField
._test
_binop
_invalid
_none
, op
=binop
))
617 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericField
._test
_binop
_type
_true
, op
=binop
))
618 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_int
, op
=binop
))
619 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vint
, op
=binop
))
620 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericField
._test
_binop
_value
_true
, op
=binop
))
621 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_int
, op
=binop
))
622 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vint
, op
=binop
))
623 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
624 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
625 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
626 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
627 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
628 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
629 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_int
, op
=binop
))
630 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vint
, op
=binop
))
631 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_int
, op
=binop
))
632 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vint
, op
=binop
))
633 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
634 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
635 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
636 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
637 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericField
._test
_binop
_type
_false
, op
=binop
))
638 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_int
, op
=binop
))
639 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vint
, op
=binop
))
640 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericField
._test
_binop
_value
_false
, op
=binop
))
641 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_int
, op
=binop
))
642 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vint
, op
=binop
))
643 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
644 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
645 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
646 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
647 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
648 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
649 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_float
, op
=binop
))
650 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_float
, op
=binop
))
651 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
652 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
653 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_float
, op
=binop
))
654 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_float
, op
=binop
))
655 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
656 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
657 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
658 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
659 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
660 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
661 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
662 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
663 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
664 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
665 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_float
, op
=binop
))
666 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
667 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_float
, op
=binop
))
668 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
669 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
670 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
671 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
672 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
674 # inject testing methods for each unary operation
675 for name
, unaryop
in _UNARYOPS
:
676 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericField
._test
_unaryop
_type
, op
=unaryop
))
677 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericField
._test
_unaryop
_value
, op
=unaryop
))
678 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericField
._test
_unaryop
_addr
_same
, op
=unaryop
))
679 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericField
._test
_unaryop
_value
_same
, op
=unaryop
))
681 # inject testing methods for each inplace binary operation
682 for name
, ibinop
in _IBINOPS
:
683 setattr(cls
, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericField
._test
_ibinop
_invalid
_unknown
, op
=ibinop
))
684 setattr(cls
, test_ibinop_name('invalid_none'), partialmethod(_TestNumericField
._test
_ibinop
_invalid
_none
, op
=ibinop
))
685 setattr(cls
, test_ibinop_name('type_true'), partialmethod(_TestNumericField
._test
_ibinop
_type
_true
, op
=ibinop
))
686 setattr(cls
, test_ibinop_name('value_true'), partialmethod(_TestNumericField
._test
_ibinop
_value
_true
, op
=ibinop
))
687 setattr(cls
, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_int
, op
=ibinop
))
688 setattr(cls
, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_vint
, op
=ibinop
))
689 setattr(cls
, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_int
, op
=ibinop
))
690 setattr(cls
, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_vint
, op
=ibinop
))
691 setattr(cls
, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_int
, op
=ibinop
))
692 setattr(cls
, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_vint
, op
=ibinop
))
693 setattr(cls
, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_int
, op
=ibinop
))
694 setattr(cls
, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_vint
, op
=ibinop
))
695 setattr(cls
, test_ibinop_name('type_false'), partialmethod(_TestNumericField
._test
_ibinop
_type
_false
, op
=ibinop
))
696 setattr(cls
, test_ibinop_name('value_false'), partialmethod(_TestNumericField
._test
_ibinop
_value
_false
, op
=ibinop
))
697 setattr(cls
, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_int
, op
=ibinop
))
698 setattr(cls
, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_vint
, op
=ibinop
))
699 setattr(cls
, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_int
, op
=ibinop
))
700 setattr(cls
, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_vint
, op
=ibinop
))
701 setattr(cls
, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_float
, op
=ibinop
))
702 setattr(cls
, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_float
, op
=ibinop
))
703 setattr(cls
, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_pos
_vfloat
, op
=ibinop
))
704 setattr(cls
, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_neg
_vfloat
, op
=ibinop
))
705 setattr(cls
, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_float
, op
=ibinop
))
706 setattr(cls
, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_float
, op
=ibinop
))
707 setattr(cls
, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_pos
_vfloat
, op
=ibinop
))
708 setattr(cls
, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_neg
_vfloat
, op
=ibinop
))
709 setattr(cls
, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_float
, op
=ibinop
))
710 setattr(cls
, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_type
_zero
_vfloat
, op
=ibinop
))
711 setattr(cls
, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_float
, op
=ibinop
))
712 setattr(cls
, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericField
._test
_ibinop
_value
_zero
_vfloat
, op
=ibinop
))
715 class _TestIntegerFieldCommon(_TestNumericField
):
716 def test_assign_true(self
):
718 self
._def
.value
= raw
719 self
.assertEqual(self
._def
, raw
)
721 def test_assign_false(self
):
723 self
._def
.value
= raw
724 self
.assertEqual(self
._def
, raw
)
726 def test_assign_pos_int(self
):
728 self
._def
.value
= raw
729 self
.assertEqual(self
._def
, raw
)
731 def test_assign_neg_int(self
):
733 self
._def
.value
= raw
734 self
.assertEqual(self
._def
, raw
)
736 def test_assign_int_field(self
):
740 self
._def
.value
= field
741 self
.assertEqual(self
._def
, raw
)
743 def test_assign_float(self
):
745 self
._def
.value
= raw
746 self
.assertEqual(self
._def
, int(raw
))
748 def test_assign_invalid_type(self
):
749 with self
.assertRaises(TypeError):
750 self
._def
.value
= 'yes'
752 def test_assign_uint(self
):
753 ft
= bt2
.IntegerFieldType(size
=32, is_signed
=False)
757 self
.assertEqual(field
, raw
)
759 def test_assign_uint_invalid_neg(self
):
760 ft
= bt2
.IntegerFieldType(size
=32, is_signed
=False)
763 with self
.assertRaises(ValueError):
767 _inject_numeric_testing_methods(_TestIntegerFieldCommon
)
770 class IntegerFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
772 self
._ft
= bt2
.IntegerFieldType(25, is_signed
=True)
773 self
._field
= self
._ft
()
774 self
._def
= self
._ft
()
777 self
._def
_new
_value
= -101
785 class EnumerationFieldTestCase(_TestIntegerFieldCommon
, unittest
.TestCase
):
787 self
._ft
= bt2
.EnumerationFieldType(size
=32, is_signed
=True)
788 self
._ft
.append_mapping('whole range', -(2 ** 31), (2 ** 31) - 1)
789 self
._ft
.append_mapping('something', 17)
790 self
._ft
.append_mapping('speaker', 12, 16)
791 self
._ft
.append_mapping('can', 18, 2540)
792 self
._ft
.append_mapping('zip', -45, 1001)
793 self
._def
= self
._ft
()
796 self
._def
_new
_value
= -101
802 def test_mappings(self
):
804 ('whole range', -(2 ** 31), (2 ** 31) - 1),
805 ('something', 17, 17),
812 for fm
in self
._def
.mappings
:
814 for index
, mapping
in enumerate(mappings
):
815 if fm
.name
== mapping
[0] and fm
.lower
== mapping
[1] and fm
.upper
== mapping
[2]:
818 self
.assertEqual(total
, 3)
819 self
.assertTrue(0 in index_set
and 1 in index_set
and 2 in index_set
)
822 class FloatingPointNumberFieldTestCase(_TestNumericField
, unittest
.TestCase
):
824 self
._ft
= bt2
.FloatingPointNumberFieldType()
825 self
._field
= self
._ft
()
826 self
._def
= self
._ft
()
827 self
._def
.value
= 52.7
828 self
._def
_value
= 52.7
829 self
._def
_new
_value
= -17.164857
836 def _test_invalid_op(self
, cb
):
837 with self
.assertRaises(TypeError):
840 def test_assign_true(self
):
841 self
._def
.value
= True
842 self
.assertTrue(self
._def
)
844 def test_assign_false(self
):
845 self
._def
.value
= False
846 self
.assertFalse(self
._def
)
848 def test_assign_pos_int(self
):
850 self
._def
.value
= raw
851 self
.assertEqual(self
._def
, float(raw
))
853 def test_assign_neg_int(self
):
855 self
._def
.value
= raw
856 self
.assertEqual(self
._def
, float(raw
))
858 def test_assign_int_field(self
):
859 ft
= bt2
.IntegerFieldType(32)
863 self
._def
.value
= field
864 self
.assertEqual(self
._def
, float(raw
))
866 def test_assign_float(self
):
868 self
._def
.value
= raw
869 self
.assertEqual(self
._def
, raw
)
871 def test_assign_float_field(self
):
872 ft
= bt2
.FloatingPointNumberFieldType(32)
876 self
._def
.value
= field
877 self
.assertEqual(self
._def
, raw
)
879 def test_assign_invalid_type(self
):
880 with self
.assertRaises(TypeError):
881 self
._def
.value
= 'yes'
883 def test_invalid_lshift(self
):
884 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
886 def test_invalid_rshift(self
):
887 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
889 def test_invalid_and(self
):
890 self
._test
_invalid
_op
(lambda: self
._def
& 23)
892 def test_invalid_or(self
):
893 self
._test
_invalid
_op
(lambda: self
._def |
23)
895 def test_invalid_xor(self
):
896 self
._test
_invalid
_op
(lambda: self
._def ^
23)
898 def test_invalid_invert(self
):
899 self
._test
_invalid
_op
(lambda: ~self
._def
)
902 _inject_numeric_testing_methods(FloatingPointNumberFieldTestCase
)
905 class StringFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
907 self
._ft
= bt2
.StringFieldType()
908 self
._def
_value
= 'Hello, World!'
909 self
._def
= self
._ft
()
910 self
._def
.value
= self
._def
_value
911 self
._def
_new
_value
= 'Yes!'
917 def test_assign_int(self
):
918 with self
.assertRaises(TypeError):
919 self
._def
.value
= 283
921 def test_assign_string_field(self
):
922 ft
= bt2
.StringFieldType()
926 self
.assertEqual(field
, raw
)
929 self
.assertEqual(self
._def
, self
._def
_value
)
932 self
.assertNotEqual(self
._def
, 23)
934 def test_lt_vstring(self
):
939 self
.assertLess(s1
, s2
)
941 def test_lt_string(self
):
944 self
.assertLess(s1
, 'bateau')
946 def test_le_vstring(self
):
951 self
.assertLessEqual(s1
, s2
)
953 def test_le_string(self
):
956 self
.assertLessEqual(s1
, 'bateau')
958 def test_gt_vstring(self
):
963 self
.assertGreater(s2
, s1
)
965 def test_gt_string(self
):
968 self
.assertGreater('bateau', s1
)
970 def test_ge_vstring(self
):
975 self
.assertGreaterEqual(s2
, s1
)
977 def test_ge_string(self
):
980 self
.assertGreaterEqual('bateau', s1
)
982 def test_bool_op(self
):
983 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
985 def test_str_op(self
):
986 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
989 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
991 def test_getitem(self
):
992 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
994 def test_append_str(self
):
995 to_append
= 'meow meow meow'
996 self
._def
+= to_append
997 self
._def
_value
+= to_append
998 self
.assertEqual(self
._def
, self
._def
_value
)
1000 def test_append_string_field(self
):
1001 ft
= bt2
.StringFieldType()
1003 to_append
= 'meow meow meow'
1004 field
.value
= to_append
1006 self
._def
_value
+= to_append
1007 self
.assertEqual(self
._def
, self
._def
_value
)
1009 def test_is_set(self
):
1010 raw
= self
._def
_value
1012 self
.assertFalse(field
.is_set
)
1014 self
.assertTrue(field
.is_set
)
1016 def test_reset(self
):
1017 raw
= self
._def
_value
1020 self
.assertTrue(field
.is_set
)
1022 self
.assertFalse(field
.is_set
)
1024 self
.assertEqual(other
, field
)
1027 class _TestArraySequenceFieldCommon(_TestCopySimple
):
1028 def _modify_def(self
):
1031 def test_bool_op_true(self
):
1032 self
.assertTrue(self
._def
)
1035 self
.assertEqual(len(self
._def
), 3)
1037 def test_getitem(self
):
1038 field
= self
._def
[1]
1039 self
.assertIs(type(field
), bt2
.fields
._IntegerField
)
1040 self
.assertEqual(field
, 1847)
1043 ft
= bt2
.ArrayFieldType(self
._elem
_ft
, 3)
1048 self
.assertEqual(self
._def
, field
)
1050 def test_eq_invalid_type(self
):
1051 self
.assertNotEqual(self
._def
, 23)
1053 def test_eq_diff_len(self
):
1054 ft
= bt2
.ArrayFieldType(self
._elem
_ft
, 2)
1058 self
.assertNotEqual(self
._def
, field
)
1060 def test_eq_diff_content_same_len(self
):
1061 ft
= bt2
.ArrayFieldType(self
._elem
_ft
, 3)
1066 self
.assertNotEqual(self
._def
, field
)
1068 def test_setitem(self
):
1070 self
.assertEqual(self
._def
[2], 24)
1072 def test_setitem_int_field(self
):
1073 int_field
= self
._elem
_ft
()
1074 int_field
.value
= 19487
1075 self
._def
[1] = int_field
1076 self
.assertEqual(self
._def
[1], 19487)
1078 def test_setitem_non_basic_field(self
):
1079 elem_ft
= bt2
.StructureFieldType()
1080 array_ft
= bt2
.ArrayFieldType(elem_ft
, 3)
1081 elem_field
= elem_ft()
1082 array_field
= array_ft()
1084 with self
.assertRaises(TypeError):
1087 def test_setitem_none(self
):
1088 with self
.assertRaises(TypeError):
1091 def test_setitem_index_wrong_type(self
):
1092 with self
.assertRaises(TypeError):
1093 self
._def
['yes'] = 23
1095 def test_setitem_index_neg(self
):
1096 with self
.assertRaises(IndexError):
1099 def test_setitem_index_out_of_range(self
):
1100 with self
.assertRaises(IndexError):
1101 self
._def
[len(self
._def
)] = 134679
1103 def test_iter(self
):
1104 for field
, value
in zip(self
._def
, (45, 1847, 1948754)):
1105 self
.assertEqual(field
, value
)
1107 def test_value_int_field(self
):
1108 values
= [45646, 145, 12145]
1109 self
._def
.value
= values
1110 self
.assertEqual(values
, self
._def
)
1112 def test_value_unset(self
):
1113 values
= [45646, None, 12145]
1114 self
._def
.value
= values
1115 self
.assertFalse(self
._def
[1].is_set
)
1117 def test_value_rollback(self
):
1118 values
= [45, 1847, 1948754]
1119 # value is out of range, should not affect those we set previously
1120 with self
.assertRaises(bt2
.Error
):
1121 self
._def
[2].value
= 2**60
1122 self
.assertEqual(values
, self
._def
)
1124 def test_value_check_sequence(self
):
1126 with self
.assertRaises(TypeError):
1127 self
._def
.value
= values
1129 def test_value_wrong_type_in_sequence(self
):
1130 values
= [32, 'hello', 11]
1131 with self
.assertRaises(TypeError):
1132 self
._def
.value
= values
1134 def test_value_complex_type(self
):
1135 struct_ft
= bt2
.StructureFieldType()
1136 int_ft
= bt2
.IntegerFieldType(32)
1137 str_ft
= bt2
.StringFieldType()
1138 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1139 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1140 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1141 array_ft
= bt2
.ArrayFieldType(struct_ft
, 3)
1145 'a_string': 'hello',
1150 'a_string': 'goodbye',
1155 'a_string': 'or not',
1161 array
.value
= values
1162 self
.assertEqual(values
, array
)
1163 values
[0]['an_int'] = 'a string'
1164 with self
.assertRaises(TypeError):
1165 array
.value
= values
1167 def test_is_set(self
):
1168 raw
= self
._def
_value
1170 self
.assertFalse(field
.is_set
)
1172 self
.assertTrue(field
.is_set
)
1174 def test_reset(self
):
1175 raw
= self
._def
_value
1178 self
.assertTrue(field
.is_set
)
1180 self
.assertFalse(field
.is_set
)
1182 self
.assertEqual(other
, field
)
1185 class ArrayFieldTestCase(_TestArraySequenceFieldCommon
, unittest
.TestCase
):
1187 self
._elem
_ft
= bt2
.IntegerFieldType(32)
1188 self
._ft
= bt2
.ArrayFieldType(self
._elem
_ft
, 3)
1189 self
._def
= self
._ft
()
1192 self
._def
[2] = 1948754
1193 self
._def
_value
= [45, 1847, 1948754]
1200 def test_value_wrong_len(self
):
1202 with self
.assertRaises(ValueError):
1203 self
._def
.value
= values
1206 class SequenceFieldTestCase(_TestArraySequenceFieldCommon
, unittest
.TestCase
):
1208 self
._elem
_ft
= bt2
.IntegerFieldType(32)
1209 self
._ft
= bt2
.SequenceFieldType(self
._elem
_ft
, 'the.length')
1210 self
._def
= self
._ft
()
1211 self
._length
_field
= self
._elem
_ft
(3)
1212 self
._def
.length_field
= self
._length
_field
1215 self
._def
[2] = 1948754
1216 self
._def
_value
= [45, 1847, 1948754]
1222 del self
._length
_field
1224 def test_value_resize(self
):
1225 new_values
= [1, 2, 3, 4]
1226 self
._def
.value
= new_values
1227 self
.assertCountEqual(self
._def
, new_values
)
1229 def test_value_resize_rollback(self
):
1230 with self
.assertRaises(TypeError):
1231 self
._def
.value
= [1, 2, 3, 'unexpected string']
1232 self
.assertEqual(self
._def
, self
._def
_value
)
1235 with self
.assertRaises(TypeError):
1236 self
._def
.value
= [1, 2, 3, 'unexpected string']
1237 self
.assertFalse(self
._def
.is_set
)
1240 class StructureFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
1242 self
._ft
0 = bt2
.IntegerFieldType(32, is_signed
=True)
1243 self
._ft
1 = bt2
.StringFieldType()
1244 self
._ft
2 = bt2
.FloatingPointNumberFieldType()
1245 self
._ft
3 = bt2
.IntegerFieldType(17)
1246 self
._ft
= bt2
.StructureFieldType()
1247 self
._ft
.append_field('A', self
._ft
0)
1248 self
._ft
.append_field('B', self
._ft
1)
1249 self
._ft
.append_field('C', self
._ft
2)
1250 self
._ft
.append_field('D', self
._ft
3)
1251 self
._def
= self
._ft
()
1252 self
._def
['A'] = -1872
1253 self
._def
['B'] = 'salut'
1254 self
._def
['C'] = 17.5
1255 self
._def
['D'] = 16497
1265 def _modify_def(self
):
1266 self
._def
['B'] = 'hola'
1268 def test_bool_op_true(self
):
1269 self
.assertTrue(self
._def
)
1271 def test_bool_op_false(self
):
1272 ft
= bt2
.StructureFieldType()
1274 self
.assertFalse(field
)
1277 self
.assertEqual(len(self
._def
), 4)
1279 def test_getitem(self
):
1280 field
= self
._def
['A']
1281 self
.assertIs(type(field
), bt2
.fields
._IntegerField
)
1282 self
.assertEqual(field
, -1872)
1284 def test_at_index_out_of_bounds_after(self
):
1285 with self
.assertRaises(IndexError):
1286 self
._def
.at_index(len(self
._ft
))
1289 ft
= bt2
.StructureFieldType()
1290 ft
.append_field('A', self
._ft
0)
1291 ft
.append_field('B', self
._ft
1)
1292 ft
.append_field('C', self
._ft
2)
1293 ft
.append_field('D', self
._ft
3)
1296 field
['B'] = 'salut'
1299 self
.assertEqual(self
._def
, field
)
1301 def test_eq_invalid_type(self
):
1302 self
.assertNotEqual(self
._def
, 23)
1304 def test_eq_diff_len(self
):
1305 ft
= bt2
.StructureFieldType()
1306 ft
.append_field('A', self
._ft
0)
1307 ft
.append_field('B', self
._ft
1)
1308 ft
.append_field('C', self
._ft
2)
1311 field
['B'] = 'salut'
1313 self
.assertNotEqual(self
._def
, field
)
1315 def test_eq_diff_content_same_len(self
):
1316 ft
= bt2
.StructureFieldType()
1317 ft
.append_field('A', self
._ft
0)
1318 ft
.append_field('B', self
._ft
1)
1319 ft
.append_field('C', self
._ft
2)
1320 ft
.append_field('D', self
._ft
3)
1323 field
['B'] = 'salut'
1326 self
.assertNotEqual(self
._def
, field
)
1328 def test_eq_same_content_diff_keys(self
):
1329 ft
= bt2
.StructureFieldType()
1330 ft
.append_field('A', self
._ft
0)
1331 ft
.append_field('B', self
._ft
1)
1332 ft
.append_field('E', self
._ft
2)
1333 ft
.append_field('D', self
._ft
3)
1336 field
['B'] = 'salut'
1339 self
.assertNotEqual(self
._def
, field
)
1341 def test_setitem(self
):
1342 self
._def
['C'] = -18.47
1343 self
.assertEqual(self
._def
['C'], -18.47)
1345 def test_setitem_int_field(self
):
1346 int_ft
= bt2
.IntegerFieldType(16)
1347 int_field
= int_ft()
1348 int_field
.value
= 19487
1349 self
._def
['D'] = int_field
1350 self
.assertEqual(self
._def
['D'], 19487)
1352 def test_setitem_non_basic_field(self
):
1353 elem_ft
= bt2
.StructureFieldType()
1354 elem_field
= elem_ft()
1355 struct_ft
= bt2
.StructureFieldType()
1356 struct_ft
.append_field('A', elem_ft
)
1357 struct_field
= struct_ft()
1359 # Will fail on access to .items() of the value
1360 with self
.assertRaises(AttributeError):
1361 struct_field
['A'] = 23
1363 def test_setitem_none(self
):
1364 with self
.assertRaises(TypeError):
1365 self
._def
['C'] = None
1367 def test_setitem_key_wrong_type(self
):
1368 with self
.assertRaises(TypeError):
1371 def test_setitem_wrong_key(self
):
1372 with self
.assertRaises(KeyError):
1373 self
._def
['hi'] = 134679
1375 def test_at_index(self
):
1376 self
.assertEqual(self
._def
.at_index(1), 'salut')
1378 def test_iter(self
):
1386 for vkey
, vval
in self
._def
.items():
1387 val
= orig_values
[vkey
]
1388 self
.assertEqual(vval
, val
)
1390 def test_value(self
):
1397 self
.assertEqual(self
._def
, orig_values
)
1399 def test_set_value(self
):
1400 int_ft
= bt2
.IntegerFieldType(32)
1401 str_ft
= bt2
.StringFieldType()
1402 struct_ft
= bt2
.StructureFieldType()
1403 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1404 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1405 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1408 'a_string': 'hello',
1412 struct
= struct_ft()
1413 struct
.value
= values
1414 self
.assertEqual(values
, struct
)
1416 bad_type_values
= copy
.deepcopy(values
)
1417 bad_type_values
['an_int'] = 'a string'
1418 with self
.assertRaises(TypeError):
1419 struct
.value
= bad_type_values
1421 unknown_key_values
= copy
.deepcopy(values
)
1422 unknown_key_values
['unknown_key'] = 16546
1423 with self
.assertRaises(KeyError):
1424 struct
.value
= unknown_key_values
1426 def test_value_rollback(self
):
1427 int_ft
= bt2
.IntegerFieldType(32)
1428 str_ft
= bt2
.StringFieldType()
1429 struct_ft
= bt2
.StructureFieldType()
1430 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1431 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1432 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1435 'a_string': 'hello',
1439 def test_is_set(self
):
1442 'a_string': 'hello',
1446 int_ft
= bt2
.IntegerFieldType(32)
1447 str_ft
= bt2
.StringFieldType()
1448 struct_ft
= bt2
.StructureFieldType()
1449 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1450 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1451 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1453 struct
= struct_ft()
1454 self
.assertFalse(struct
.is_set
)
1455 struct
.value
= values
1456 self
.assertTrue(struct
.is_set
)
1458 struct
= struct_ft()
1459 struct
['an_int'].value
= 42
1460 self
.assertFalse(struct
.is_set
)
1462 def test_reset(self
):
1465 'a_string': 'hello',
1469 int_ft
= bt2
.IntegerFieldType(32)
1470 str_ft
= bt2
.StringFieldType()
1471 struct_ft
= bt2
.StructureFieldType()
1472 struct_ft
.append_field(field_type
=int_ft
, name
='an_int')
1473 struct_ft
.append_field(field_type
=str_ft
, name
='a_string')
1474 struct_ft
.append_field(field_type
=int_ft
, name
='another_int')
1476 struct
= struct_ft()
1477 struct
.value
= values
1478 self
.assertTrue(struct
.is_set
)
1480 self
.assertEqual(struct_ft(), struct
)
1483 class VariantFieldTestCase(_TestCopySimple
, unittest
.TestCase
):
1485 self
._tag
_ft
= bt2
.EnumerationFieldType(size
=32)
1486 self
._tag
_ft
.append_mapping('corner', 23)
1487 self
._tag
_ft
.append_mapping('zoom', 17, 20)
1488 self
._tag
_ft
.append_mapping('mellotron', 1001)
1489 self
._tag
_ft
.append_mapping('giorgio', 2000, 3000)
1490 self
._ft
0 = bt2
.IntegerFieldType(32, is_signed
=True)
1491 self
._ft
1 = bt2
.StringFieldType()
1492 self
._ft
2 = bt2
.FloatingPointNumberFieldType()
1493 self
._ft
3 = bt2
.IntegerFieldType(17)
1494 self
._ft
= bt2
.VariantFieldType('salut', self
._tag
_ft
)
1495 self
._ft
.append_field('corner', self
._ft
0)
1496 self
._ft
.append_field('zoom', self
._ft
1)
1497 self
._ft
.append_field('mellotron', self
._ft
2)
1498 self
._ft
.append_field('giorgio', self
._ft
3)
1499 self
._def
= self
._ft
()
1510 def test_bool_op_true(self
):
1511 tag_field
= self
._tag
_ft
(1001)
1512 self
._def
.field(tag_field
).value
= -17.34
1513 self
.assertTrue(self
._def
)
1515 def test_bool_op_false(self
):
1516 self
.assertFalse(self
._def
)
1518 def test_tag_field_none(self
):
1519 self
.assertIsNone(self
._def
.tag_field
)
1521 def test_tag_field(self
):
1522 tag_field
= self
._tag
_ft
(2800)
1523 self
._def
.field(tag_field
).value
= 1847
1524 self
.assertEqual(self
._def
.tag_field
, tag_field
)
1525 self
.assertEqual(self
._def
.tag_field
.addr
, tag_field
.addr
)
1527 def test_selected_field_none(self
):
1528 self
.assertIsNone(self
._def
.selected_field
)
1530 def test_selected_field(self
):
1531 var_field1
= self
._ft
()
1532 tag_field1
= self
._tag
_ft
(1001)
1533 var_field1
.field(tag_field1
).value
= -17.34
1534 self
.assertEqual(var_field1
.field(), -17.34)
1535 self
.assertEqual(var_field1
.selected_field
, -17.34)
1536 var_field2
= self
._ft
()
1537 tag_field2
= self
._tag
_ft
(2500)
1538 var_field2
.field(tag_field2
).value
= 1921
1539 self
.assertEqual(var_field2
.field(), 1921)
1540 self
.assertEqual(var_field2
.selected_field
, 1921)
1543 tag_ft
= bt2
.EnumerationFieldType(size
=32)
1544 tag_ft
.append_mapping('corner', 23)
1545 tag_ft
.append_mapping('zoom', 17, 20)
1546 tag_ft
.append_mapping('mellotron', 1001)
1547 tag_ft
.append_mapping('giorgio', 2000, 3000)
1548 ft0
= bt2
.IntegerFieldType(32, is_signed
=True)
1549 ft1
= bt2
.StringFieldType()
1550 ft2
= bt2
.FloatingPointNumberFieldType()
1551 ft3
= bt2
.IntegerFieldType(17)
1552 ft
= bt2
.VariantFieldType('salut', tag_ft
)
1553 ft
.append_field('corner', ft0
)
1554 ft
.append_field('zoom', ft1
)
1555 ft
.append_field('mellotron', ft2
)
1556 ft
.append_field('giorgio', ft3
)
1558 field_tag
= tag_ft(23)
1559 def_tag
= self
._tag
_ft
(23)
1560 field
.field(field_tag
).value
= 1774
1561 self
._def
.field(def_tag
).value
= 1774
1562 self
.assertEqual(self
._def
, field
)
1564 def test_eq_invalid_type(self
):
1565 self
.assertNotEqual(self
._def
, 23)
1567 def test_is_set(self
):
1568 self
.assertFalse(self
._def
.is_set
)
1569 tag_field
= self
._tag
_ft
(2800)
1570 self
._def
.field(tag_field
).value
= 684
1571 self
.assertTrue(self
._def
.is_set
)
1573 def test_reset(self
):
1574 tag_field
= self
._tag
_ft
(2800)
1575 self
._def
.field(tag_field
).value
= 684
1577 self
.assertFalse(self
._def
.is_set
)
1578 self
.assertIsNone(self
._def
.selected_field
)
1579 self
.assertIsNone(self
._def
.tag_field
)