1 from functools
import partial
, partialmethod
11 def test_is_frozen(self
):
13 self
.assertTrue(self
._def
.is_frozen
)
15 def test_frozen(self
):
17 self
.assertTrue(self
._def
.frozen
)
19 def test_frozen_exc(self
):
22 with self
.assertRaisesRegex(bt2
.Frozen
, r
'.* value object is frozen$') as cm
:
25 self
.assertEqual(self
._def
, self
._def
_value
)
27 def test_get_value_when_frozen(self
):
29 self
.assertEqual(self
._def
, self
._def
_value
)
32 class _TestFrozenSimple(_TestFrozen
):
33 def _modify_def(self
):
34 self
._def
.value
= self
._def
_new
_value
37 class _TestCopySimple
:
39 cpy
= copy
.copy(self
._def
)
40 self
.assertIsNot(cpy
, self
._def
)
41 self
.assertNotEqual(cpy
.addr
, self
._def
.addr
)
42 self
.assertEqual(cpy
, self
._def
)
44 def test_deepcopy(self
):
45 cpy
= copy
.deepcopy(self
._def
)
46 self
.assertIsNot(cpy
, self
._def
)
47 self
.assertNotEqual(cpy
.addr
, self
._def
.addr
)
48 self
.assertEqual(cpy
, self
._def
)
57 class _TestNumericValue(_TestFrozenSimple
, _TestCopySimple
):
58 def _binop(self
, op
, rhs
):
64 r
= op(self
._def
, rhs
)
65 except Exception as e
:
69 rv
= op(self
._def
_value
, comp_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 _unaryop(self
, op
):
89 except Exception as e
:
93 rv
= op(self
._def
_value
)
94 except Exception as e
:
97 if rexc
is not None or rvexc
is not None:
98 # at least one of the operations raised an exception: in
99 # this case both operations should have raised the same
100 # type of exception (division by zero, bit shift with a
101 # floating point number operand, etc.)
102 self
.assertIs(type(rexc
), type(rvexc
))
107 def _test_unaryop_type(self
, op
):
108 r
, rv
= self
._unaryop
(op
)
113 self
.assertIsInstance(r
, type(rv
))
115 def _test_unaryop_value(self
, op
):
116 r
, rv
= self
._unaryop
(op
)
121 self
.assertEqual(r
, rv
)
123 def _test_unaryop_addr_same(self
, op
):
124 addr_before
= self
._def
.addr
126 self
.assertEqual(self
._def
.addr
, addr_before
)
128 def _test_unaryop_value_same(self
, op
):
129 value_before
= copy
.copy(self
._def
)
131 self
.assertEqual(self
._def
, value_before
)
133 def _test_binop_type(self
, op
, rhs
):
134 r
, rv
= self
._binop
(op
, rhs
)
139 if op
in _COMP_BINOPS
:
140 # __eq__() and __ne__() always return a 'bool' object
141 self
.assertIsInstance(r
, bool)
143 self
.assertIsInstance(r
, type(rv
))
145 def _test_binop_value(self
, op
, rhs
):
146 r
, rv
= self
._binop
(op
, rhs
)
151 self
.assertEqual(r
, rv
)
153 def _test_binop_lhs_addr_same(self
, op
, rhs
):
154 addr_before
= self
._def
.addr
155 r
, rv
= self
._binop
(op
, rhs
)
156 self
.assertEqual(self
._def
.addr
, addr_before
)
158 def _test_binop_lhs_value_same(self
, op
, rhs
):
159 value_before
= copy
.copy(self
._def
)
160 r
, rv
= self
._binop
(op
, rhs
)
161 self
.assertEqual(self
._def
, value_before
)
163 def _test_binop_invalid_unknown(self
, op
):
164 if op
in _COMP_BINOPS
:
165 self
.skipTest('not testing')
170 with self
.assertRaises(TypeError):
173 def _test_binop_invalid_none(self
, op
):
174 if op
in _COMP_BINOPS
:
175 self
.skipTest('not testing')
177 with self
.assertRaises(TypeError):
180 def _test_ibinop_value(self
, op
, rhs
):
181 r
, rv
= self
._binop
(op
, rhs
)
186 # The inplace operators are special for value objects because
187 # they do not return a new, immutable object like it's the case
188 # for Python numbers. In Python, `a += 2`, where `a` is a number
189 # object, assigns a new number object reference to `a`, dropping
190 # the old reference. Since BT's value objects are mutable, we
191 # modify their internal value with the inplace operators. This
192 # means however that we can lose data in the process, for
195 # int_value_obj += 3.3
197 # Here, if `int_value_obj` is a Python `int` with the value 2,
198 # it would be a `float` object after this, holding the value
199 # 5.3. In our case, if `int_value_obj` is an integer value
200 # object, 3.3 is converted to an `int` object (3) and added to
201 # the current value of `int_value_obj`, so after this the value
202 # of the object is 5. This does not compare to 5.3, which is
203 # why we also use the `int()` type here.
204 if type(self
._def
) is bt2
.IntegerValue
:
207 self
.assertEqual(r
, rv
)
209 def _test_ibinop_type(self
, op
, rhs
):
210 r
, rv
= self
._binop
(op
, rhs
)
215 self
.assertIs(r
, self
._def
)
217 def _test_ibinop_invalid_unknown(self
, op
):
221 with self
.assertRaises(TypeError):
224 def _test_ibinop_invalid_none(self
, op
):
225 with self
.assertRaises(TypeError):
228 def _test_binop_rhs_false(self
, test_cb
, op
):
231 def _test_binop_rhs_true(self
, test_cb
, op
):
234 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
237 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
240 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
243 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
244 test_cb(op
, bt2
.create_value(2))
246 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
247 test_cb(op
, bt2
.create_value(-23))
249 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
250 test_cb(op
, bt2
.create_value(0))
252 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
255 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
258 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
261 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
262 test_cb(op
, bt2
.create_value(2.2))
264 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
265 test_cb(op
, bt2
.create_value(-23.4))
267 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
268 test_cb(op
, bt2
.create_value(0.0))
270 def _test_binop_type_false(self
, op
):
271 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
273 def _test_binop_type_true(self
, op
):
274 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
276 def _test_binop_type_pos_int(self
, op
):
277 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
279 def _test_binop_type_neg_int(self
, op
):
280 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
282 def _test_binop_type_zero_int(self
, op
):
283 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
285 def _test_binop_type_pos_vint(self
, op
):
286 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
288 def _test_binop_type_neg_vint(self
, op
):
289 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
291 def _test_binop_type_zero_vint(self
, op
):
292 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
294 def _test_binop_type_pos_float(self
, op
):
295 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
297 def _test_binop_type_neg_float(self
, op
):
298 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
300 def _test_binop_type_zero_float(self
, op
):
301 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
303 def _test_binop_type_pos_vfloat(self
, op
):
304 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
306 def _test_binop_type_neg_vfloat(self
, op
):
307 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
309 def _test_binop_type_zero_vfloat(self
, op
):
310 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
312 def _test_binop_value_false(self
, op
):
313 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
315 def _test_binop_value_true(self
, op
):
316 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
318 def _test_binop_value_pos_int(self
, op
):
319 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
321 def _test_binop_value_neg_int(self
, op
):
322 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
324 def _test_binop_value_zero_int(self
, op
):
325 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
327 def _test_binop_value_pos_vint(self
, op
):
328 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
330 def _test_binop_value_neg_vint(self
, op
):
331 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
333 def _test_binop_value_zero_vint(self
, op
):
334 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
336 def _test_binop_value_pos_float(self
, op
):
337 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
339 def _test_binop_value_neg_float(self
, op
):
340 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
342 def _test_binop_value_zero_float(self
, op
):
343 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
345 def _test_binop_value_pos_vfloat(self
, op
):
346 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
348 def _test_binop_value_neg_vfloat(self
, op
):
349 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
351 def _test_binop_value_zero_vfloat(self
, op
):
352 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
354 def _test_binop_lhs_addr_same_false(self
, op
):
355 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
357 def _test_binop_lhs_addr_same_true(self
, op
):
358 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
360 def _test_binop_lhs_addr_same_pos_int(self
, op
):
361 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
363 def _test_binop_lhs_addr_same_neg_int(self
, op
):
364 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
366 def _test_binop_lhs_addr_same_zero_int(self
, op
):
367 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
369 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
370 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
372 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
373 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
375 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
376 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
378 def _test_binop_lhs_addr_same_pos_float(self
, op
):
379 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
381 def _test_binop_lhs_addr_same_neg_float(self
, op
):
382 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
384 def _test_binop_lhs_addr_same_zero_float(self
, op
):
385 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
387 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
388 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
390 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
391 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
393 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
394 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
396 def _test_binop_lhs_value_same_false(self
, op
):
397 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
399 def _test_binop_lhs_value_same_true(self
, op
):
400 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
402 def _test_binop_lhs_value_same_pos_int(self
, op
):
403 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
405 def _test_binop_lhs_value_same_neg_int(self
, op
):
406 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
408 def _test_binop_lhs_value_same_zero_int(self
, op
):
409 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
411 def _test_binop_lhs_value_same_pos_vint(self
, op
):
412 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
414 def _test_binop_lhs_value_same_neg_vint(self
, op
):
415 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
417 def _test_binop_lhs_value_same_zero_vint(self
, op
):
418 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
420 def _test_binop_lhs_value_same_pos_float(self
, op
):
421 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
423 def _test_binop_lhs_value_same_neg_float(self
, op
):
424 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
426 def _test_binop_lhs_value_same_zero_float(self
, op
):
427 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
429 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
430 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
432 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
433 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
435 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
436 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
438 def _test_ibinop_type_false(self
, op
):
439 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_type
, op
)
441 def _test_ibinop_type_true(self
, op
):
442 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_type
, op
)
444 def _test_ibinop_type_pos_int(self
, op
):
445 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_type
, op
)
447 def _test_ibinop_type_neg_int(self
, op
):
448 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_type
, op
)
450 def _test_ibinop_type_zero_int(self
, op
):
451 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_type
, op
)
453 def _test_ibinop_type_pos_vint(self
, op
):
454 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_type
, op
)
456 def _test_ibinop_type_neg_vint(self
, op
):
457 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_type
, op
)
459 def _test_ibinop_type_zero_vint(self
, op
):
460 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_type
, op
)
462 def _test_ibinop_type_pos_float(self
, op
):
463 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_type
, op
)
465 def _test_ibinop_type_neg_float(self
, op
):
466 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_type
, op
)
468 def _test_ibinop_type_zero_float(self
, op
):
469 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_type
, op
)
471 def _test_ibinop_type_pos_vfloat(self
, op
):
472 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_type
, op
)
474 def _test_ibinop_type_neg_vfloat(self
, op
):
475 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_type
, op
)
477 def _test_ibinop_type_zero_vfloat(self
, op
):
478 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_type
, op
)
480 def _test_ibinop_value_false(self
, op
):
481 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_value
, op
)
483 def _test_ibinop_value_true(self
, op
):
484 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_value
, op
)
486 def _test_ibinop_value_pos_int(self
, op
):
487 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_value
, op
)
489 def _test_ibinop_value_neg_int(self
, op
):
490 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_value
, op
)
492 def _test_ibinop_value_zero_int(self
, op
):
493 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_value
, op
)
495 def _test_ibinop_value_pos_vint(self
, op
):
496 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_value
, op
)
498 def _test_ibinop_value_neg_vint(self
, op
):
499 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_value
, op
)
501 def _test_ibinop_value_zero_vint(self
, op
):
502 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_value
, op
)
504 def _test_ibinop_value_pos_float(self
, op
):
505 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_value
, op
)
507 def _test_ibinop_value_neg_float(self
, op
):
508 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_value
, op
)
510 def _test_ibinop_value_zero_float(self
, op
):
511 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_value
, op
)
513 def _test_ibinop_value_pos_vfloat(self
, op
):
514 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_value
, op
)
516 def _test_ibinop_value_neg_vfloat(self
, op
):
517 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_value
, op
)
519 def _test_ibinop_value_zero_vfloat(self
, op
):
520 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_value
, op
)
522 def test_bool_op(self
):
523 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
525 def test_int_op(self
):
526 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
528 def test_float_op(self
):
529 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
531 def test_complex_op(self
):
532 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
534 def test_str_op(self
):
535 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
537 def test_eq_none(self
):
538 self
.assertFalse(self
._def
== None)
540 def test_ne_none(self
):
541 self
.assertTrue(self
._def
!= None)
551 ('add', operator
.add
),
552 ('radd', lambda a
, b
: operator
.add(b
, a
)),
553 ('and', operator
.and_
),
554 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
555 ('floordiv', operator
.floordiv
),
556 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
557 ('lshift', operator
.lshift
),
558 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
559 ('mod', operator
.mod
),
560 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
561 ('mul', operator
.mul
),
562 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
563 ('or', operator
.or_
),
564 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
565 ('pow', operator
.pow),
566 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
567 ('rshift', operator
.rshift
),
568 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
569 ('sub', operator
.sub
),
570 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
571 ('truediv', operator
.truediv
),
572 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
573 ('xor', operator
.xor
),
574 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
579 ('iadd', operator
.iadd
),
580 ('iand', operator
.iand
),
581 ('ifloordiv', operator
.ifloordiv
),
582 ('ilshift', operator
.ilshift
),
583 ('imod', operator
.imod
),
584 ('imul', operator
.imul
),
585 ('ior', operator
.ior
),
586 ('ipow', operator
.ipow
),
587 ('irshift', operator
.irshift
),
588 ('isub', operator
.isub
),
589 ('itruediv', operator
.itruediv
),
590 ('ixor', operator
.ixor
),
595 ('neg', operator
.neg
),
596 ('pos', operator
.pos
),
597 ('abs', operator
.abs),
598 ('invert', operator
.invert
),
600 ('round_0', partial(round, ndigits
=0)),
601 ('round_1', partial(round, ndigits
=1)),
602 ('round_2', partial(round, ndigits
=2)),
603 ('round_3', partial(round, ndigits
=3)),
605 ('floor', math
.floor
),
606 ('trunc', math
.trunc
),
610 def _inject_numeric_testing_methods(cls
):
611 def test_binop_name(suffix
):
612 return 'test_binop_{}_{}'.format(name
, suffix
)
614 def test_ibinop_name(suffix
):
615 return 'test_ibinop_{}_{}'.format(name
, suffix
)
617 def test_unaryop_name(suffix
):
618 return 'test_unaryop_{}_{}'.format(name
, suffix
)
620 # inject testing methods for each binary operation
621 for name
, binop
in _BINOPS
:
623 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
))
624 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
))
625 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
))
626 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
))
627 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
))
628 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
))
629 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
))
630 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
))
631 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
632 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
633 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
634 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
635 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
636 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
637 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
))
638 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
))
639 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
))
640 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
))
641 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
642 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
643 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
644 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
645 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
))
646 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
))
647 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
))
648 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
))
649 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
))
650 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
))
651 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
652 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
653 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
654 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
655 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
656 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
657 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
))
658 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
))
659 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
660 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
661 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
))
662 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
))
663 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
664 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
665 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
666 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
667 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
668 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
669 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
670 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
671 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
672 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
673 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
))
674 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
675 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
))
676 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
677 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
678 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
679 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
680 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
682 # inject testing methods for each unary operation
683 for name
, unaryop
in _UNARYOPS
:
684 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
))
685 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
))
686 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
))
687 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
))
689 # inject testing methods for each inplace binary operation
690 for name
, ibinop
in _IBINOPS
:
691 setattr(cls
, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericValue
._test
_ibinop
_invalid
_unknown
, op
=ibinop
))
692 setattr(cls
, test_ibinop_name('invalid_none'), partialmethod(_TestNumericValue
._test
_ibinop
_invalid
_none
, op
=ibinop
))
693 setattr(cls
, test_ibinop_name('type_true'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_true
, op
=ibinop
))
694 setattr(cls
, test_ibinop_name('value_true'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_true
, op
=ibinop
))
695 setattr(cls
, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_pos
_int
, op
=ibinop
))
696 setattr(cls
, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_pos
_vint
, op
=ibinop
))
697 setattr(cls
, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_pos
_int
, op
=ibinop
))
698 setattr(cls
, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_pos
_vint
, op
=ibinop
))
699 setattr(cls
, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_neg
_int
, op
=ibinop
))
700 setattr(cls
, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_neg
_vint
, op
=ibinop
))
701 setattr(cls
, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_neg
_int
, op
=ibinop
))
702 setattr(cls
, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_neg
_vint
, op
=ibinop
))
703 setattr(cls
, test_ibinop_name('type_false'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_false
, op
=ibinop
))
704 setattr(cls
, test_ibinop_name('value_false'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_false
, op
=ibinop
))
705 setattr(cls
, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_zero
_int
, op
=ibinop
))
706 setattr(cls
, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_zero
_vint
, op
=ibinop
))
707 setattr(cls
, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_zero
_int
, op
=ibinop
))
708 setattr(cls
, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_zero
_vint
, op
=ibinop
))
709 setattr(cls
, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_pos
_float
, op
=ibinop
))
710 setattr(cls
, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_neg
_float
, op
=ibinop
))
711 setattr(cls
, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_pos
_vfloat
, op
=ibinop
))
712 setattr(cls
, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_neg
_vfloat
, op
=ibinop
))
713 setattr(cls
, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_pos
_float
, op
=ibinop
))
714 setattr(cls
, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_neg
_float
, op
=ibinop
))
715 setattr(cls
, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_pos
_vfloat
, op
=ibinop
))
716 setattr(cls
, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_neg
_vfloat
, op
=ibinop
))
717 setattr(cls
, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_zero
_float
, op
=ibinop
))
718 setattr(cls
, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_zero
_vfloat
, op
=ibinop
))
719 setattr(cls
, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_zero
_float
, op
=ibinop
))
720 setattr(cls
, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_zero
_vfloat
, op
=ibinop
))
723 class CreateValueFuncTestCase(unittest
.TestCase
):
724 def test_create_none(self
):
725 v
= bt2
.create_value(None)
728 def test_create_bool_false(self
):
729 v
= bt2
.create_value(False)
730 self
.assertIsInstance(v
, bt2
.BoolValue
)
733 def test_create_bool_true(self
):
734 v
= bt2
.create_value(True)
735 self
.assertIsInstance(v
, bt2
.BoolValue
)
738 def test_create_int_pos(self
):
740 v
= bt2
.create_value(raw
)
741 self
.assertIsInstance(v
, bt2
.IntegerValue
)
742 self
.assertEqual(v
, raw
)
744 def test_create_int_neg(self
):
746 v
= bt2
.create_value(raw
)
747 self
.assertIsInstance(v
, bt2
.IntegerValue
)
748 self
.assertEqual(v
, raw
)
750 def test_create_float_pos(self
):
752 v
= bt2
.create_value(raw
)
753 self
.assertIsInstance(v
, bt2
.FloatValue
)
754 self
.assertEqual(v
, raw
)
756 def test_create_float_neg(self
):
758 v
= bt2
.create_value(raw
)
759 self
.assertIsInstance(v
, bt2
.FloatValue
)
760 self
.assertEqual(v
, raw
)
762 def test_create_string(self
):
764 v
= bt2
.create_value(raw
)
765 self
.assertIsInstance(v
, bt2
.StringValue
)
766 self
.assertEqual(v
, raw
)
768 def test_create_string_empty(self
):
770 v
= bt2
.create_value(raw
)
771 self
.assertIsInstance(v
, bt2
.StringValue
)
772 self
.assertEqual(v
, raw
)
774 def test_create_array_from_list(self
):
776 v
= bt2
.create_value(raw
)
777 self
.assertIsInstance(v
, bt2
.ArrayValue
)
778 self
.assertEqual(v
, raw
)
780 def test_create_array_from_tuple(self
):
782 v
= bt2
.create_value(raw
)
783 self
.assertIsInstance(v
, bt2
.ArrayValue
)
784 self
.assertEqual(v
, raw
)
786 def test_create_array_from_empty_list(self
):
788 v
= bt2
.create_value(raw
)
789 self
.assertIsInstance(v
, bt2
.ArrayValue
)
790 self
.assertEqual(v
, raw
)
792 def test_create_array_from_empty_tuple(self
):
794 v
= bt2
.create_value(raw
)
795 self
.assertIsInstance(v
, bt2
.ArrayValue
)
796 self
.assertEqual(v
, raw
)
798 def test_create_map(self
):
800 v
= bt2
.create_value(raw
)
801 self
.assertIsInstance(v
, bt2
.MapValue
)
802 self
.assertEqual(v
, raw
)
804 def test_create_map_empty(self
):
806 v
= bt2
.create_value(raw
)
807 self
.assertIsInstance(v
, bt2
.MapValue
)
808 self
.assertEqual(v
, raw
)
810 def test_create_vfalse(self
):
811 v
= bt2
.create_value(bt2
.create_value(False))
812 self
.assertIsInstance(v
, bt2
.BoolValue
)
815 def test_create_invalid(self
):
821 with self
.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm
:
822 v
= bt2
.create_value(a
)
825 class BoolValueTestCase(_TestFrozenSimple
, _TestCopySimple
, unittest
.TestCase
):
827 self
._f
= bt2
.BoolValue(False)
828 self
._t
= bt2
.BoolValue(True)
830 self
._def
_value
= False
831 self
._def
_new
_value
= True
838 def _assert_expecting_bool(self
):
839 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
841 def test_create_default(self
):
845 def test_create_false(self
):
846 self
.assertFalse(self
._f
)
848 def test_create_true(self
):
849 self
.assertTrue(self
._t
)
851 def test_create_from_vfalse(self
):
852 b
= bt2
.BoolValue(self
._f
)
855 def test_create_from_vtrue(self
):
856 b
= bt2
.BoolValue(self
._t
)
859 def test_create_from_int_non_zero(self
):
860 with self
.assertRaises(TypeError):
861 b
= bt2
.BoolValue(23)
863 def test_create_from_int_zero(self
):
864 with self
.assertRaises(TypeError):
867 def test_assign_true(self
):
872 def test_assign_false(self
):
877 def test_assign_vtrue(self
):
882 def test_assign_vfalse(self
):
887 def test_assign_int(self
):
888 with self
.assertRaises(TypeError):
892 def test_bool_op(self
):
893 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
895 def test_str_op(self
):
896 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
898 def test_eq_none(self
):
899 self
.assertFalse(self
._def
== None)
901 def test_ne_none(self
):
902 self
.assertTrue(self
._def
!= None)
904 def test_vfalse_eq_false(self
):
905 self
.assertEqual(self
._f
, False)
907 def test_vfalse_ne_true(self
):
908 self
.assertNotEqual(self
._f
, True)
910 def test_vtrue_eq_true(self
):
911 self
.assertEqual(self
._t
, True)
913 def test_vtrue_ne_false(self
):
914 self
.assertNotEqual(self
._t
, False)
917 class IntegerValueTestCase(_TestNumericValue
, unittest
.TestCase
):
921 self
._ip
= bt2
.IntegerValue(self
._pv
)
922 self
._in
= bt2
.IntegerValue(self
._nv
)
924 self
._def
_value
= self
._pv
925 self
._def
_new
_value
= -101
933 def _assert_expecting_int(self
):
934 return self
.assertRaisesRegex(TypeError, r
'expecting a number object')
936 def _assert_expecting_int64(self
):
937 return self
.assertRaisesRegex(ValueError, r
"expecting a signed 64-bit integral value")
939 def _assert_expecting_uint64(self
):
940 return self
.assertRaisesRegex(ValueError, r
"expecting an unsigned 64-bit integral value")
942 def test_create_default(self
):
943 i
= bt2
.IntegerValue()
944 self
.assertEqual(i
, 0)
946 def test_create_pos(self
):
947 self
.assertEqual(self
._ip
, self
._pv
)
949 def test_create_neg(self
):
950 self
.assertEqual(self
._in
, self
._nv
)
952 def test_create_pos_too_big(self
):
953 with self
._assert
_expecting
_int
64():
954 i
= bt2
.IntegerValue(2 ** 63)
956 def test_create_neg_too_big(self
):
957 with self
._assert
_expecting
_int
64():
958 i
= bt2
.IntegerValue(-(2 ** 63) - 1)
960 def test_create_from_vint(self
):
961 i
= bt2
.IntegerValue(self
._ip
)
962 self
.assertEqual(i
, self
._pv
)
964 def test_create_from_false(self
):
965 i
= bt2
.IntegerValue(False)
968 def test_create_from_true(self
):
969 i
= bt2
.IntegerValue(True)
972 def test_create_from_float(self
):
973 i
= bt2
.IntegerValue(99.6)
974 self
.assertEqual(i
, 99)
976 def test_create_from_vfloat(self
):
977 f
= bt2
.create_value(17.5)
978 i
= bt2
.IntegerValue(f
)
979 self
.assertEqual(i
, 17)
981 def test_create_from_unknown(self
):
985 with self
._assert
_expecting
_int
():
986 i
= bt2
.IntegerValue(A())
988 def test_create_from_varray(self
):
989 with self
._assert
_expecting
_int
():
990 i
= bt2
.IntegerValue(bt2
.ArrayValue())
992 def test_assign_true(self
):
994 self
._def
.value
= raw
995 self
.assertEqual(self
._def
, raw
)
997 def test_assign_false(self
):
999 self
._def
.value
= raw
1000 self
.assertEqual(self
._def
, raw
)
1002 def test_assign_pos_int(self
):
1004 self
._def
.value
= raw
1005 self
.assertEqual(self
._def
, raw
)
1007 def test_assign_neg_int(self
):
1009 self
._def
.value
= raw
1010 self
.assertEqual(self
._def
, raw
)
1012 def test_assign_vint(self
):
1014 self
._def
.value
= bt2
.create_value(raw
)
1015 self
.assertEqual(self
._def
, raw
)
1017 def test_assign_vfloat(self
):
1019 self
._def
.value
= bt2
.create_value(raw
)
1020 self
.assertEqual(self
._def
, int(raw
))
1023 _inject_numeric_testing_methods(IntegerValueTestCase
)
1026 class FloatValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1030 self
._fp
= bt2
.FloatValue(self
._pv
)
1031 self
._fn
= bt2
.FloatValue(self
._nv
)
1032 self
._def
= self
._fp
1033 self
._def
_value
= self
._pv
1034 self
._def
_new
_value
= -101.88
1042 def _assert_expecting_float(self
):
1043 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
1045 def _test_invalid_op(self
, cb
):
1046 with self
.assertRaises(TypeError):
1049 def test_create_default(self
):
1050 f
= bt2
.FloatValue()
1051 self
.assertEqual(f
, 0.0)
1053 def test_create_pos(self
):
1054 self
.assertEqual(self
._fp
, self
._pv
)
1056 def test_create_neg(self
):
1057 self
.assertEqual(self
._fn
, self
._nv
)
1059 def test_create_from_vint(self
):
1060 f
= bt2
.FloatValue(self
._fp
)
1061 self
.assertEqual(f
, self
._pv
)
1063 def test_create_from_false(self
):
1064 f
= bt2
.FloatValue(False)
1067 def test_create_from_true(self
):
1068 f
= bt2
.FloatValue(True)
1071 def test_create_from_int(self
):
1073 f
= bt2
.FloatValue(raw
)
1074 self
.assertEqual(f
, float(raw
))
1076 def test_create_from_vint(self
):
1078 f
= bt2
.FloatValue(bt2
.create_value(raw
))
1079 self
.assertEqual(f
, float(raw
))
1081 def test_create_from_vfloat(self
):
1083 f
= bt2
.FloatValue(bt2
.create_value(raw
))
1084 self
.assertEqual(f
, raw
)
1086 def test_create_from_unknown(self
):
1090 with self
._assert
_expecting
_float
():
1091 f
= bt2
.FloatValue(A())
1093 def test_create_from_varray(self
):
1094 with self
._assert
_expecting
_float
():
1095 f
= bt2
.FloatValue(bt2
.ArrayValue())
1097 def test_assign_true(self
):
1098 self
._def
.value
= True
1099 self
.assertTrue(self
._def
)
1101 def test_assign_false(self
):
1102 self
._def
.value
= False
1103 self
.assertFalse(self
._def
)
1105 def test_assign_pos_int(self
):
1107 self
._def
.value
= raw
1108 self
.assertEqual(self
._def
, float(raw
))
1110 def test_assign_neg_int(self
):
1112 self
._def
.value
= raw
1113 self
.assertEqual(self
._def
, float(raw
))
1115 def test_assign_vint(self
):
1117 self
._def
.value
= bt2
.create_value(raw
)
1118 self
.assertEqual(self
._def
, float(raw
))
1120 def test_assign_float(self
):
1122 self
._def
.value
= raw
1123 self
.assertEqual(self
._def
, raw
)
1125 def test_assign_vfloat(self
):
1127 self
._def
.value
= bt2
.create_value(raw
)
1128 self
.assertEqual(self
._def
, raw
)
1130 def test_invalid_lshift(self
):
1131 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1133 def test_invalid_rshift(self
):
1134 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1136 def test_invalid_and(self
):
1137 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1139 def test_invalid_or(self
):
1140 self
._test
_invalid
_op
(lambda: self
._def |
23)
1142 def test_invalid_xor(self
):
1143 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1145 def test_invalid_invert(self
):
1146 self
._test
_invalid
_op
(lambda: ~self
._def
)
1149 _inject_numeric_testing_methods(FloatValueTestCase
)
1152 class StringValueTestCase(_TestCopySimple
, _TestFrozenSimple
, unittest
.TestCase
):
1154 self
._def
_value
= 'Hello, World!'
1155 self
._def
= bt2
.StringValue(self
._def
_value
)
1156 self
._def
_new
_value
= 'Yes!'
1161 def _assert_expecting_str(self
):
1162 return self
.assertRaises(TypeError)
1164 def test_create_default(self
):
1165 s
= bt2
.StringValue()
1166 self
.assertEqual(s
, '')
1168 def test_create_from_str(self
):
1170 s
= bt2
.StringValue(raw
)
1171 self
.assertEqual(s
, raw
)
1173 def test_create_from_vstr(self
):
1175 s
= bt2
.StringValue(bt2
.create_value(raw
))
1176 self
.assertEqual(s
, raw
)
1178 def test_create_from_unknown(self
):
1182 with self
._assert
_expecting
_str
():
1183 i
= bt2
.StringValue(A())
1185 def test_create_from_varray(self
):
1186 with self
._assert
_expecting
_str
():
1187 i
= bt2
.StringValue(bt2
.ArrayValue())
1189 def test_assign_int(self
):
1190 with self
._assert
_expecting
_str
():
1191 self
._def
.value
= 283
1193 def test_assign_str(self
):
1196 self
.assertEqual(self
._def
, raw
)
1198 def test_assign_vstr(self
):
1200 self
._def
= bt2
.create_value(raw
)
1201 self
.assertEqual(self
._def
, raw
)
1204 self
.assertEqual(self
._def
, self
._def
_value
)
1207 self
.assertNotEqual(self
._def
, 23)
1209 def test_lt_vstring(self
):
1210 s1
= bt2
.StringValue('allo')
1211 s2
= bt2
.StringValue('bateau')
1212 self
.assertLess(s1
, s2
)
1214 def test_lt_string(self
):
1215 s1
= bt2
.StringValue('allo')
1216 self
.assertLess(s1
, 'bateau')
1218 def test_le_vstring(self
):
1219 s1
= bt2
.StringValue('allo')
1220 s2
= bt2
.StringValue('bateau')
1221 self
.assertLessEqual(s1
, s2
)
1223 def test_le_string(self
):
1224 s1
= bt2
.StringValue('allo')
1225 self
.assertLessEqual(s1
, 'bateau')
1227 def test_gt_vstring(self
):
1228 s1
= bt2
.StringValue('allo')
1229 s2
= bt2
.StringValue('bateau')
1230 self
.assertGreater(s2
, s1
)
1232 def test_gt_string(self
):
1233 s1
= bt2
.StringValue('allo')
1234 self
.assertGreater('bateau', s1
)
1236 def test_ge_vstring(self
):
1237 s1
= bt2
.StringValue('allo')
1238 s2
= bt2
.StringValue('bateau')
1239 self
.assertGreaterEqual(s2
, s1
)
1241 def test_ge_string(self
):
1242 s1
= bt2
.StringValue('allo')
1243 self
.assertGreaterEqual('bateau', s1
)
1245 def test_bool_op(self
):
1246 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1248 def test_str_op(self
):
1249 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1252 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1254 def test_getitem(self
):
1255 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1257 def test_append_str(self
):
1258 to_append
= 'meow meow meow'
1259 self
._def
+= to_append
1260 self
._def
_value
+= to_append
1261 self
.assertEqual(self
._def
, self
._def
_value
)
1263 def test_append_vstr(self
):
1264 to_append
= 'meow meow meow'
1265 self
._def
+= bt2
.create_value(to_append
)
1266 self
._def
_value
+= to_append
1267 self
.assertEqual(self
._def
, self
._def
_value
)
1270 class ArrayValueTestCase(_TestFrozen
, unittest
.TestCase
):
1272 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1273 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1278 def _modify_def(self
):
1279 self
._def
[2] = 'xyz'
1281 def _assert_type_error(self
):
1282 return self
.assertRaises(TypeError)
1284 def test_create_default(self
):
1285 a
= bt2
.ArrayValue()
1286 self
.assertEqual(len(a
), 0)
1288 def test_create_from_array(self
):
1289 self
.assertEqual(self
._def
, self
._def
_value
)
1291 def test_create_from_tuple(self
):
1292 t
= 1, 2, False, None
1293 a
= bt2
.ArrayValue(t
)
1294 self
.assertEqual(a
, t
)
1296 def test_create_from_varray(self
):
1297 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1298 a
= bt2
.ArrayValue(va
)
1299 self
.assertEqual(va
, a
)
1301 def test_create_from_unknown(self
):
1305 with self
._assert
_type
_error
():
1306 a
= bt2
.ArrayValue(A())
1308 def test_bool_op_true(self
):
1309 self
.assertTrue(bool(self
._def
))
1311 def test_bool_op_false(self
):
1312 self
.assertFalse(bool(bt2
.ArrayValue()))
1315 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1317 def test_copy(self
):
1318 to_copy
= (1, 2, 'hello', (4, 5.2))
1319 a
= bt2
.ArrayValue(to_copy
)
1321 self
.assertEqual(a
, cpy
)
1322 self
.assertNotEqual(a
.addr
, cpy
.addr
)
1323 self
.assertEqual(a
[3].addr
, cpy
[3].addr
)
1325 def test_deepcopy(self
):
1326 to_copy
= (1, 2, 'hello', (4, 5.2))
1327 a
= bt2
.ArrayValue(to_copy
)
1328 cpy
= copy
.deepcopy(a
)
1329 self
.assertEqual(a
, cpy
)
1330 self
.assertNotEqual(a
.addr
, cpy
.addr
)
1331 self
.assertNotEqual(a
[3].addr
, cpy
[3].addr
)
1333 def test_eq_int(self
):
1334 self
.assertNotEqual(self
._def
, 23)
1336 def test_eq_diff_len(self
):
1337 a1
= bt2
.create_value([1, 2, 3])
1338 a2
= bt2
.create_value([1, 2])
1339 self
.assertNotEqual(a1
, a2
)
1341 def test_eq_diff_content_same_len(self
):
1342 a1
= bt2
.create_value([1, 2, 3])
1343 a2
= bt2
.create_value([4, 5, 6])
1344 self
.assertNotEqual(a1
, a2
)
1346 def test_eq_same_content_same_len(self
):
1347 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1348 a1
= bt2
.ArrayValue(raw
)
1349 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1350 self
.assertEqual(a1
, a2
)
1352 def test_setitem_int(self
):
1355 self
.assertEqual(self
._def
[2], raw
)
1357 def test_setitem_vint(self
):
1359 self
._def
[2] = bt2
.create_value(raw
)
1360 self
.assertEqual(self
._def
[2], raw
)
1362 def test_setitem_none(self
):
1364 self
.assertIsNone(self
._def
[2])
1366 def test_setitem_index_wrong_type(self
):
1367 with self
._assert
_type
_error
():
1368 self
._def
['yes'] = 23
1370 def test_setitem_index_neg(self
):
1371 with self
.assertRaises(IndexError):
1374 def test_setitem_index_out_of_range(self
):
1375 with self
.assertRaises(IndexError):
1376 self
._def
[len(self
._def
)] = 23
1378 def test_append_none(self
):
1379 self
._def
.append(None)
1380 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1382 def test_append_int(self
):
1384 self
._def
.append(raw
)
1385 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1387 def test_append_vint(self
):
1389 self
._def
.append(bt2
.create_value(raw
))
1390 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1392 def test_append_unknown(self
):
1396 with self
._assert
_type
_error
():
1397 self
._def
.append(A())
1399 def test_iadd(self
):
1402 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1403 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1404 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1406 def test_iadd_unknown(self
):
1410 with self
._assert
_type
_error
():
1413 def test_iadd_list_unknown(self
):
1417 with self
._assert
_type
_error
():
1420 def test_iter(self
):
1421 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1422 self
.assertEqual(velem
, elem
)
1425 class MapValueTestCase(_TestFrozen
, unittest
.TestCase
):
1438 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1443 def _modify_def(self
):
1444 self
._def
['zero'] = 1
1446 def test_create_default(self
):
1448 self
.assertEqual(len(m
), 0)
1450 def test_create_from_dict(self
):
1451 self
.assertEqual(self
._def
, self
._def
_value
)
1453 def test_create_from_vmap(self
):
1454 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1455 m
= bt2
.MapValue(vm
)
1456 self
.assertEqual(vm
, m
)
1458 def test_create_from_unknown(self
):
1462 with self
.assertRaises(AttributeError):
1463 m
= bt2
.MapValue(A())
1465 def test_bool_op_true(self
):
1466 self
.assertTrue(bool(self
._def
))
1468 def test_bool_op_false(self
):
1469 self
.assertFalse(bool(bt2
.MapValue()))
1472 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1474 def test_copy(self
):
1481 m
= bt2
.MapValue(to_copy
)
1483 self
.assertEqual(m
, cpy
)
1484 self
.assertNotEqual(m
.addr
, cpy
.addr
)
1485 self
.assertEqual(m
['inner'].addr
, cpy
['inner'].addr
)
1487 def test_deepcopy(self
):
1494 m
= bt2
.MapValue(to_copy
)
1495 cpy
= copy
.deepcopy(m
)
1496 self
.assertEqual(m
, cpy
)
1497 self
.assertNotEqual(m
.addr
, cpy
.addr
)
1498 self
.assertNotEqual(m
['inner'].addr
, cpy
['inner'].addr
)
1500 def test_eq_int(self
):
1501 self
.assertNotEqual(self
._def
, 23)
1503 def test_eq_diff_len(self
):
1504 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1505 a2
= bt2
.create_value({'a': 1, 'b': 2})
1506 self
.assertNotEqual(a1
, a2
)
1508 def test_eq_diff_content_same_len(self
):
1509 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1510 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1511 self
.assertNotEqual(a1
, a2
)
1513 def test_eq_same_content_diff_keys(self
):
1514 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1515 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1516 self
.assertNotEqual(a1
, a2
)
1518 def test_eq_same_content_same_len(self
):
1522 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]
1524 a1
= bt2
.MapValue(raw
)
1525 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1526 self
.assertEqual(a1
, a2
)
1527 self
.assertEqual(a1
, raw
)
1529 def test_setitem_int(self
):
1531 self
._def
['pos-int'] = raw
1532 self
.assertEqual(self
._def
['pos-int'], raw
)
1534 def test_setitem_vint(self
):
1536 self
._def
['pos-int'] = bt2
.create_value(raw
)
1537 self
.assertEqual(self
._def
['pos-int'], raw
)
1539 def test_setitem_none(self
):
1540 self
._def
['none'] = None
1541 self
.assertIsNone(self
._def
['none'])
1543 def test_setitem_new_int(self
):
1544 old_len
= len(self
._def
)
1545 self
._def
['new-int'] = 23
1546 self
.assertEqual(self
._def
['new-int'], 23)
1547 self
.assertEqual(len(self
._def
), old_len
+ 1)
1549 def test_setitem_index_wrong_type(self
):
1550 with self
.assertRaises(TypeError):
1553 def test_iter(self
):
1554 for vkey
, vval
in self
._def
.items():
1555 val
= self
._def
_value
[vkey
]
1556 self
.assertEqual(vval
, val
)
1558 def test_getitem_wrong_key(self
):
1559 with self
.assertRaises(KeyError):
1560 self
._def
['kilojoule']
This page took 0.078557 seconds and 5 git commands to generate.