1 from functools
import partial
, partialmethod
10 class _TestCopySimple
:
12 with self
.assertRaises(NotImplementedError):
15 def test_deepcopy(self
):
16 with self
.assertRaises(NotImplementedError):
17 copy
.deepcopy(self
._def
)
26 class _TestNumericValue(_TestCopySimple
):
27 def _binop(self
, op
, rhs
):
33 r
= op(self
._def
, rhs
)
34 except Exception as e
:
38 rv
= op(self
._def
_value
, comp_value
)
39 except Exception as e
:
42 if rexc
is not None or rvexc
is not None:
43 # at least one of the operations raised an exception: in
44 # this case both operations should have raised the same
45 # type of exception (division by zero, bit shift with a
46 # floating point number operand, etc.)
47 self
.assertIs(type(rexc
), type(rvexc
))
52 def _unaryop(self
, op
):
58 except Exception as e
:
62 rv
= op(self
._def
_value
)
63 except Exception as e
:
66 if rexc
is not None or rvexc
is not None:
67 # at least one of the operations raised an exception: in
68 # this case both operations should have raised the same
69 # type of exception (division by zero, bit shift with a
70 # floating point number operand, etc.)
71 self
.assertIs(type(rexc
), type(rvexc
))
76 def _test_unaryop_type(self
, op
):
77 r
, rv
= self
._unaryop
(op
)
82 self
.assertIsInstance(r
, type(rv
))
84 def _test_unaryop_value(self
, op
):
85 r
, rv
= self
._unaryop
(op
)
90 self
.assertEqual(r
, rv
)
92 def _test_unaryop_addr_same(self
, op
):
93 addr_before
= self
._def
.addr
95 self
.assertEqual(self
._def
.addr
, addr_before
)
97 def _test_unaryop_value_same(self
, op
):
98 value_before
= self
._def
.__class
__(self
._def
)
100 self
.assertEqual(self
._def
, value_before
)
102 def _test_binop_type(self
, op
, rhs
):
103 r
, rv
= self
._binop
(op
, rhs
)
108 if op
in _COMP_BINOPS
:
109 # __eq__() and __ne__() always return a 'bool' object
110 self
.assertIsInstance(r
, bool)
112 self
.assertIsInstance(r
, type(rv
))
114 def _test_binop_value(self
, op
, rhs
):
115 r
, rv
= self
._binop
(op
, rhs
)
120 self
.assertEqual(r
, rv
)
122 def _test_binop_lhs_addr_same(self
, op
, rhs
):
123 addr_before
= self
._def
.addr
124 r
, rv
= self
._binop
(op
, rhs
)
125 self
.assertEqual(self
._def
.addr
, addr_before
)
127 def _test_binop_lhs_value_same(self
, op
, rhs
):
128 value_before
= self
._def
.__class
__(self
._def
)
129 r
, rv
= self
._binop
(op
, rhs
)
130 self
.assertEqual(self
._def
, value_before
)
132 def _test_binop_invalid_unknown(self
, op
):
133 if op
in _COMP_BINOPS
:
134 self
.skipTest('not testing')
139 with self
.assertRaises(TypeError):
142 def _test_binop_invalid_none(self
, op
):
143 if op
in _COMP_BINOPS
:
144 self
.skipTest('not testing')
146 with self
.assertRaises(TypeError):
149 def _test_ibinop_value(self
, op
, rhs
):
150 r
, rv
= self
._binop
(op
, rhs
)
155 # The inplace operators are special for value objects because
156 # they do not return a new, immutable object like it's the case
157 # for Python numbers. In Python, `a += 2`, where `a` is a number
158 # object, assigns a new number object reference to `a`, dropping
159 # the old reference. Since BT's value objects are mutable, we
160 # modify their internal value with the inplace operators. This
161 # means however that we can lose data in the process, for
164 # int_value_obj += 3.3
166 # Here, if `int_value_obj` is a Python `int` with the value 2,
167 # it would be a `float` object after this, holding the value
168 # 5.3. In our case, if `int_value_obj` is an integer value
169 # object, 3.3 is converted to an `int` object (3) and added to
170 # the current value of `int_value_obj`, so after this the value
171 # of the object is 5. This does not compare to 5.3, which is
172 # why we also use the `int()` type here.
173 if isinstance(self
._def
, bt2
._IntegerValue
):
176 self
.assertEqual(r
, rv
)
178 def _test_ibinop_type(self
, op
, rhs
):
179 r
, rv
= self
._binop
(op
, rhs
)
184 self
.assertIs(r
, self
._def
)
186 def _test_ibinop_invalid_unknown(self
, op
):
190 with self
.assertRaises(TypeError):
193 def _test_ibinop_invalid_none(self
, op
):
194 with self
.assertRaises(TypeError):
197 def _test_binop_rhs_false(self
, test_cb
, op
):
200 def _test_binop_rhs_true(self
, test_cb
, op
):
203 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
206 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
209 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
212 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
213 test_cb(op
, bt2
.create_value(2))
215 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
216 test_cb(op
, bt2
.create_value(-23))
218 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
219 test_cb(op
, bt2
.create_value(0))
221 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
224 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
227 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
230 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
231 test_cb(op
, bt2
.create_value(2.2))
233 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
234 test_cb(op
, bt2
.create_value(-23.4))
236 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
237 test_cb(op
, bt2
.create_value(0.0))
239 def _test_binop_type_false(self
, op
):
240 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
242 def _test_binop_type_true(self
, op
):
243 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
245 def _test_binop_type_pos_int(self
, op
):
246 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
248 def _test_binop_type_neg_int(self
, op
):
249 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
251 def _test_binop_type_zero_int(self
, op
):
252 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
254 def _test_binop_type_pos_vint(self
, op
):
255 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
257 def _test_binop_type_neg_vint(self
, op
):
258 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
260 def _test_binop_type_zero_vint(self
, op
):
261 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
263 def _test_binop_type_pos_float(self
, op
):
264 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
266 def _test_binop_type_neg_float(self
, op
):
267 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
269 def _test_binop_type_zero_float(self
, op
):
270 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
272 def _test_binop_type_pos_vfloat(self
, op
):
273 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
275 def _test_binop_type_neg_vfloat(self
, op
):
276 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
278 def _test_binop_type_zero_vfloat(self
, op
):
279 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
281 def _test_binop_value_false(self
, op
):
282 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
284 def _test_binop_value_true(self
, op
):
285 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
287 def _test_binop_value_pos_int(self
, op
):
288 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
290 def _test_binop_value_neg_int(self
, op
):
291 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
293 def _test_binop_value_zero_int(self
, op
):
294 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
296 def _test_binop_value_pos_vint(self
, op
):
297 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
299 def _test_binop_value_neg_vint(self
, op
):
300 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
302 def _test_binop_value_zero_vint(self
, op
):
303 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
305 def _test_binop_value_pos_float(self
, op
):
306 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
308 def _test_binop_value_neg_float(self
, op
):
309 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
311 def _test_binop_value_zero_float(self
, op
):
312 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
314 def _test_binop_value_pos_vfloat(self
, op
):
315 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
317 def _test_binop_value_neg_vfloat(self
, op
):
318 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
320 def _test_binop_value_zero_vfloat(self
, op
):
321 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
323 def _test_binop_lhs_addr_same_false(self
, op
):
324 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
326 def _test_binop_lhs_addr_same_true(self
, op
):
327 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
329 def _test_binop_lhs_addr_same_pos_int(self
, op
):
330 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
332 def _test_binop_lhs_addr_same_neg_int(self
, op
):
333 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
335 def _test_binop_lhs_addr_same_zero_int(self
, op
):
336 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
338 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
339 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
341 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
342 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
344 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
345 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
347 def _test_binop_lhs_addr_same_pos_float(self
, op
):
348 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
350 def _test_binop_lhs_addr_same_neg_float(self
, op
):
351 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
353 def _test_binop_lhs_addr_same_zero_float(self
, op
):
354 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
356 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
357 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
359 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
360 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
362 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
363 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
365 def _test_binop_lhs_value_same_false(self
, op
):
366 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
368 def _test_binop_lhs_value_same_true(self
, op
):
369 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
371 def _test_binop_lhs_value_same_pos_int(self
, op
):
372 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
374 def _test_binop_lhs_value_same_neg_int(self
, op
):
375 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
377 def _test_binop_lhs_value_same_zero_int(self
, op
):
378 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
380 def _test_binop_lhs_value_same_pos_vint(self
, op
):
381 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
383 def _test_binop_lhs_value_same_neg_vint(self
, op
):
384 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
386 def _test_binop_lhs_value_same_zero_vint(self
, op
):
387 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
389 def _test_binop_lhs_value_same_pos_float(self
, op
):
390 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
392 def _test_binop_lhs_value_same_neg_float(self
, op
):
393 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
395 def _test_binop_lhs_value_same_zero_float(self
, op
):
396 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
398 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
399 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
401 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
402 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
404 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
405 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
407 def _test_ibinop_type_false(self
, op
):
408 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_type
, op
)
410 def _test_ibinop_type_true(self
, op
):
411 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_type
, op
)
413 def _test_ibinop_type_pos_int(self
, op
):
414 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_type
, op
)
416 def _test_ibinop_type_neg_int(self
, op
):
417 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_type
, op
)
419 def _test_ibinop_type_zero_int(self
, op
):
420 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_type
, op
)
422 def _test_ibinop_type_pos_vint(self
, op
):
423 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_type
, op
)
425 def _test_ibinop_type_neg_vint(self
, op
):
426 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_type
, op
)
428 def _test_ibinop_type_zero_vint(self
, op
):
429 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_type
, op
)
431 def _test_ibinop_type_pos_float(self
, op
):
432 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_type
, op
)
434 def _test_ibinop_type_neg_float(self
, op
):
435 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_type
, op
)
437 def _test_ibinop_type_zero_float(self
, op
):
438 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_type
, op
)
440 def _test_ibinop_type_pos_vfloat(self
, op
):
441 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_type
, op
)
443 def _test_ibinop_type_neg_vfloat(self
, op
):
444 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_type
, op
)
446 def _test_ibinop_type_zero_vfloat(self
, op
):
447 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_type
, op
)
449 def _test_ibinop_value_false(self
, op
):
450 self
._test
_binop
_rhs
_false
(self
._test
_ibinop
_value
, op
)
452 def _test_ibinop_value_true(self
, op
):
453 self
._test
_binop
_rhs
_true
(self
._test
_ibinop
_value
, op
)
455 def _test_ibinop_value_pos_int(self
, op
):
456 self
._test
_binop
_rhs
_pos
_int
(self
._test
_ibinop
_value
, op
)
458 def _test_ibinop_value_neg_int(self
, op
):
459 self
._test
_binop
_rhs
_neg
_int
(self
._test
_ibinop
_value
, op
)
461 def _test_ibinop_value_zero_int(self
, op
):
462 self
._test
_binop
_rhs
_zero
_int
(self
._test
_ibinop
_value
, op
)
464 def _test_ibinop_value_pos_vint(self
, op
):
465 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_ibinop
_value
, op
)
467 def _test_ibinop_value_neg_vint(self
, op
):
468 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_ibinop
_value
, op
)
470 def _test_ibinop_value_zero_vint(self
, op
):
471 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_ibinop
_value
, op
)
473 def _test_ibinop_value_pos_float(self
, op
):
474 self
._test
_binop
_rhs
_pos
_float
(self
._test
_ibinop
_value
, op
)
476 def _test_ibinop_value_neg_float(self
, op
):
477 self
._test
_binop
_rhs
_neg
_float
(self
._test
_ibinop
_value
, op
)
479 def _test_ibinop_value_zero_float(self
, op
):
480 self
._test
_binop
_rhs
_zero
_float
(self
._test
_ibinop
_value
, op
)
482 def _test_ibinop_value_pos_vfloat(self
, op
):
483 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_ibinop
_value
, op
)
485 def _test_ibinop_value_neg_vfloat(self
, op
):
486 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_ibinop
_value
, op
)
488 def _test_ibinop_value_zero_vfloat(self
, op
):
489 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_ibinop
_value
, op
)
491 def test_bool_op(self
):
492 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
494 def test_int_op(self
):
495 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
497 def test_float_op(self
):
498 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
500 def test_complex_op(self
):
501 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
503 def test_str_op(self
):
504 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
506 def test_eq_none(self
):
507 self
.assertFalse(self
._def
== None)
509 def test_ne_none(self
):
510 self
.assertTrue(self
._def
!= None)
520 ('add', operator
.add
),
521 ('radd', lambda a
, b
: operator
.add(b
, a
)),
522 ('and', operator
.and_
),
523 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
524 ('floordiv', operator
.floordiv
),
525 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
526 ('lshift', operator
.lshift
),
527 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
528 ('mod', operator
.mod
),
529 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
530 ('mul', operator
.mul
),
531 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
532 ('or', operator
.or_
),
533 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
534 ('pow', operator
.pow),
535 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
536 ('rshift', operator
.rshift
),
537 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
538 ('sub', operator
.sub
),
539 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
540 ('truediv', operator
.truediv
),
541 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
542 ('xor', operator
.xor
),
543 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
548 ('iadd', operator
.iadd
),
549 ('iand', operator
.iand
),
550 ('ifloordiv', operator
.ifloordiv
),
551 ('ilshift', operator
.ilshift
),
552 ('imod', operator
.imod
),
553 ('imul', operator
.imul
),
554 ('ior', operator
.ior
),
555 ('ipow', operator
.ipow
),
556 ('irshift', operator
.irshift
),
557 ('isub', operator
.isub
),
558 ('itruediv', operator
.itruediv
),
559 ('ixor', operator
.ixor
),
564 ('neg', operator
.neg
),
565 ('pos', operator
.pos
),
566 ('abs', operator
.abs),
567 ('invert', operator
.invert
),
569 ('round_0', partial(round, ndigits
=0)),
570 ('round_1', partial(round, ndigits
=1)),
571 ('round_2', partial(round, ndigits
=2)),
572 ('round_3', partial(round, ndigits
=3)),
574 ('floor', math
.floor
),
575 ('trunc', math
.trunc
),
579 def _inject_numeric_testing_methods(cls
, has_neg
=True):
580 def test_binop_name(suffix
):
581 return 'test_binop_{}_{}'.format(name
, suffix
)
583 def test_ibinop_name(suffix
):
584 return 'test_ibinop_{}_{}'.format(name
, suffix
)
586 def test_unaryop_name(suffix
):
587 return 'test_unaryop_{}_{}'.format(name
, suffix
)
589 # inject testing methods for each binary operation
590 for name
, binop
in _BINOPS
:
591 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
))
592 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
))
593 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
))
594 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
))
595 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
))
596 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
))
597 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
))
598 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
))
599 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
600 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
601 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
602 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
603 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
604 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
607 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
))
608 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
))
609 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
))
610 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
))
611 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
612 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
613 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
614 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
616 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
))
617 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
))
618 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
))
619 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
))
620 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
))
621 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
))
622 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
623 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
624 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
625 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
626 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
627 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
630 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
))
631 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
632 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
))
633 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
634 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
635 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
636 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
637 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
639 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
))
640 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
641 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
))
642 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
643 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
644 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
645 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
646 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
647 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
))
648 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
649 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
))
650 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
651 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
652 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
653 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
654 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
656 # inject testing methods for each unary operation
657 for name
, unaryop
in _UNARYOPS
:
658 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
))
659 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
))
660 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
))
661 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
))
663 # inject testing methods for each inplace binary operation
664 for name
, ibinop
in _IBINOPS
:
665 setattr(cls
, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericValue
._test
_ibinop
_invalid
_unknown
, op
=ibinop
))
666 setattr(cls
, test_ibinop_name('invalid_none'), partialmethod(_TestNumericValue
._test
_ibinop
_invalid
_none
, op
=ibinop
))
667 setattr(cls
, test_ibinop_name('type_true'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_true
, op
=ibinop
))
668 setattr(cls
, test_ibinop_name('value_true'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_true
, op
=ibinop
))
669 setattr(cls
, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_pos
_int
, op
=ibinop
))
670 setattr(cls
, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_pos
_vint
, op
=ibinop
))
671 setattr(cls
, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_pos
_int
, op
=ibinop
))
672 setattr(cls
, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_pos
_vint
, op
=ibinop
))
675 setattr(cls
, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_neg
_int
, op
=ibinop
))
676 setattr(cls
, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_neg
_vint
, op
=ibinop
))
677 setattr(cls
, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_neg
_int
, op
=ibinop
))
678 setattr(cls
, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_neg
_vint
, op
=ibinop
))
680 setattr(cls
, test_ibinop_name('type_false'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_false
, op
=ibinop
))
681 setattr(cls
, test_ibinop_name('value_false'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_false
, op
=ibinop
))
682 setattr(cls
, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_zero
_int
, op
=ibinop
))
683 setattr(cls
, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_zero
_vint
, op
=ibinop
))
684 setattr(cls
, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_zero
_int
, op
=ibinop
))
685 setattr(cls
, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_zero
_vint
, op
=ibinop
))
686 setattr(cls
, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_pos
_float
, op
=ibinop
))
689 setattr(cls
, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_neg
_float
, op
=ibinop
))
690 setattr(cls
, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_neg
_vfloat
, op
=ibinop
))
691 setattr(cls
, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_neg
_float
, op
=ibinop
))
692 setattr(cls
, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_neg
_vfloat
, op
=ibinop
))
694 setattr(cls
, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_pos
_vfloat
, op
=ibinop
))
695 setattr(cls
, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_pos
_float
, op
=ibinop
))
696 setattr(cls
, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_pos
_vfloat
, op
=ibinop
))
697 setattr(cls
, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_zero
_float
, op
=ibinop
))
698 setattr(cls
, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_type
_zero
_vfloat
, op
=ibinop
))
699 setattr(cls
, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_zero
_float
, op
=ibinop
))
700 setattr(cls
, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericValue
._test
_ibinop
_value
_zero
_vfloat
, op
=ibinop
))
703 class CreateValueFuncTestCase(unittest
.TestCase
):
704 def test_create_none(self
):
705 v
= bt2
.create_value(None)
708 def test_create_bool_false(self
):
709 v
= bt2
.create_value(False)
710 self
.assertIsInstance(v
, bt2
.BoolValue
)
713 def test_create_bool_true(self
):
714 v
= bt2
.create_value(True)
715 self
.assertIsInstance(v
, bt2
.BoolValue
)
718 def test_create_int_pos(self
):
720 v
= bt2
.create_value(raw
)
721 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
722 self
.assertEqual(v
, raw
)
724 def test_create_int_neg(self
):
726 v
= bt2
.create_value(raw
)
727 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
728 self
.assertEqual(v
, raw
)
730 def test_create_float_pos(self
):
732 v
= bt2
.create_value(raw
)
733 self
.assertIsInstance(v
, bt2
.RealValue
)
734 self
.assertEqual(v
, raw
)
736 def test_create_float_neg(self
):
738 v
= bt2
.create_value(raw
)
739 self
.assertIsInstance(v
, bt2
.RealValue
)
740 self
.assertEqual(v
, raw
)
742 def test_create_string(self
):
744 v
= bt2
.create_value(raw
)
745 self
.assertIsInstance(v
, bt2
.StringValue
)
746 self
.assertEqual(v
, raw
)
748 def test_create_string_empty(self
):
750 v
= bt2
.create_value(raw
)
751 self
.assertIsInstance(v
, bt2
.StringValue
)
752 self
.assertEqual(v
, raw
)
754 def test_create_array_from_list(self
):
756 v
= bt2
.create_value(raw
)
757 self
.assertIsInstance(v
, bt2
.ArrayValue
)
758 self
.assertEqual(v
, raw
)
760 def test_create_array_from_tuple(self
):
762 v
= bt2
.create_value(raw
)
763 self
.assertIsInstance(v
, bt2
.ArrayValue
)
764 self
.assertEqual(v
, raw
)
766 def test_create_array_from_empty_list(self
):
768 v
= bt2
.create_value(raw
)
769 self
.assertIsInstance(v
, bt2
.ArrayValue
)
770 self
.assertEqual(v
, raw
)
772 def test_create_array_from_empty_tuple(self
):
774 v
= bt2
.create_value(raw
)
775 self
.assertIsInstance(v
, bt2
.ArrayValue
)
776 self
.assertEqual(v
, raw
)
778 def test_create_map(self
):
780 v
= bt2
.create_value(raw
)
781 self
.assertIsInstance(v
, bt2
.MapValue
)
782 self
.assertEqual(v
, raw
)
784 def test_create_map_empty(self
):
786 v
= bt2
.create_value(raw
)
787 self
.assertIsInstance(v
, bt2
.MapValue
)
788 self
.assertEqual(v
, raw
)
790 def test_create_vfalse(self
):
791 v
= bt2
.create_value(bt2
.create_value(False))
792 self
.assertIsInstance(v
, bt2
.BoolValue
)
795 def test_create_invalid(self
):
801 with self
.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm
:
802 v
= bt2
.create_value(a
)
805 class BoolValueTestCase(_TestCopySimple
, unittest
.TestCase
):
807 self
._f
= bt2
.BoolValue(False)
808 self
._t
= bt2
.BoolValue(True)
810 self
._def
_value
= False
811 self
._def
_new
_value
= True
818 def _assert_expecting_bool(self
):
819 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
821 def test_create_default(self
):
825 def test_create_false(self
):
826 self
.assertFalse(self
._f
)
828 def test_create_true(self
):
829 self
.assertTrue(self
._t
)
831 def test_create_from_vfalse(self
):
832 b
= bt2
.BoolValue(self
._f
)
835 def test_create_from_vtrue(self
):
836 b
= bt2
.BoolValue(self
._t
)
839 def test_create_from_int_non_zero(self
):
840 with self
.assertRaises(TypeError):
841 b
= bt2
.BoolValue(23)
843 def test_create_from_int_zero(self
):
844 with self
.assertRaises(TypeError):
847 def test_assign_true(self
):
852 def test_assign_false(self
):
857 def test_assign_vtrue(self
):
862 def test_assign_vfalse(self
):
867 def test_assign_int(self
):
868 with self
.assertRaises(TypeError):
872 def test_bool_op(self
):
873 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
875 def test_str_op(self
):
876 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
878 def test_eq_none(self
):
879 self
.assertFalse(self
._def
== None)
881 def test_ne_none(self
):
882 self
.assertTrue(self
._def
!= None)
884 def test_vfalse_eq_false(self
):
885 self
.assertEqual(self
._f
, False)
887 def test_vfalse_ne_true(self
):
888 self
.assertNotEqual(self
._f
, True)
890 def test_vtrue_eq_true(self
):
891 self
.assertEqual(self
._t
, True)
893 def test_vtrue_ne_false(self
):
894 self
.assertNotEqual(self
._t
, False)
897 class _TestIntegerValue(_TestNumericValue
):
900 self
._ip
= self
._CLS
(self
._pv
)
902 self
._def
_value
= self
._pv
903 self
._def
_new
_value
= 101
910 def _assert_expecting_int(self
):
911 return self
.assertRaisesRegex(TypeError, r
'expecting a number object')
913 def _assert_expecting_int64(self
):
914 return self
.assertRaisesRegex(ValueError, r
"expecting a signed 64-bit integral value")
916 def _assert_expecting_uint64(self
):
917 return self
.assertRaisesRegex(ValueError, r
"expecting an unsigned 64-bit integral value")
919 def test_create_default(self
):
921 self
.assertEqual(i
, 0)
923 def test_create_pos(self
):
924 self
.assertEqual(self
._ip
, self
._pv
)
926 def test_create_neg(self
):
927 self
.assertEqual(self
._in
, self
._nv
)
929 def test_create_from_vint(self
):
930 i
= self
._CLS
(self
._ip
)
931 self
.assertEqual(i
, self
._pv
)
933 def test_create_from_false(self
):
937 def test_create_from_true(self
):
941 def test_create_from_float(self
):
943 self
.assertEqual(i
, 99)
945 def test_create_from_vfloat(self
):
946 f
= bt2
.create_value(17.5)
948 self
.assertEqual(i
, 17)
950 def test_create_from_unknown(self
):
954 with self
._assert
_expecting
_int
():
957 def test_create_from_varray(self
):
958 with self
._assert
_expecting
_int
():
959 i
= self
._CLS
(bt2
.ArrayValue())
961 def test_assign_true(self
):
963 self
._def
.value
= raw
964 self
.assertEqual(self
._def
, raw
)
966 def test_assign_false(self
):
968 self
._def
.value
= raw
969 self
.assertEqual(self
._def
, raw
)
971 def test_assign_pos_int(self
):
973 self
._def
.value
= raw
974 self
.assertEqual(self
._def
, raw
)
976 def test_assign_vint(self
):
978 self
._def
.value
= bt2
.create_value(raw
)
979 self
.assertEqual(self
._def
, raw
)
981 def test_assign_vfloat(self
):
983 self
._def
.value
= bt2
.create_value(raw
)
984 self
.assertEqual(self
._def
, int(raw
))
987 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
988 _CLS
= bt2
.SignedIntegerValue
993 self
._in
= self
._CLS
(self
._nv
)
994 self
._def
_new
_value
= -101
1000 def test_create_neg(self
):
1001 self
.assertEqual(self
._in
, self
._nv
)
1003 def test_create_pos_too_big(self
):
1004 with self
._assert
_expecting
_int
64():
1005 i
= self
._CLS
(2 ** 63)
1007 def test_create_neg_too_big(self
):
1008 with self
._assert
_expecting
_int
64():
1009 i
= self
._CLS
(-(2 ** 63) - 1)
1011 def test_assign_neg_int(self
):
1013 self
._def
.value
= raw
1014 self
.assertEqual(self
._def
, raw
)
1017 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
1020 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1021 _CLS
= bt2
.UnsignedIntegerValue
1023 def test_create_pos_too_big(self
):
1024 with self
._assert
_expecting
_uint
64():
1025 i
= self
._CLS
(2 ** 64)
1027 def test_create_neg(self
):
1028 with self
._assert
_expecting
_uint
64():
1032 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
, False)
1035 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1039 self
._fp
= bt2
.RealValue(self
._pv
)
1040 self
._fn
= bt2
.RealValue(self
._nv
)
1041 self
._def
= self
._fp
1042 self
._def
_value
= self
._pv
1043 self
._def
_new
_value
= -101.88
1051 def _assert_expecting_float(self
):
1052 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
1054 def _test_invalid_op(self
, cb
):
1055 with self
.assertRaises(TypeError):
1058 def test_create_default(self
):
1060 self
.assertEqual(f
, 0.0)
1062 def test_create_pos(self
):
1063 self
.assertEqual(self
._fp
, self
._pv
)
1065 def test_create_neg(self
):
1066 self
.assertEqual(self
._fn
, self
._nv
)
1068 def test_create_from_vint(self
):
1069 f
= bt2
.RealValue(self
._fp
)
1070 self
.assertEqual(f
, self
._pv
)
1072 def test_create_from_false(self
):
1073 f
= bt2
.RealValue(False)
1076 def test_create_from_true(self
):
1077 f
= bt2
.RealValue(True)
1080 def test_create_from_int(self
):
1082 f
= bt2
.RealValue(raw
)
1083 self
.assertEqual(f
, float(raw
))
1085 def test_create_from_vint(self
):
1087 f
= bt2
.RealValue(bt2
.create_value(raw
))
1088 self
.assertEqual(f
, float(raw
))
1090 def test_create_from_vfloat(self
):
1092 f
= bt2
.RealValue(bt2
.create_value(raw
))
1093 self
.assertEqual(f
, raw
)
1095 def test_create_from_unknown(self
):
1099 with self
._assert
_expecting
_float
():
1100 f
= bt2
.RealValue(A())
1102 def test_create_from_varray(self
):
1103 with self
._assert
_expecting
_float
():
1104 f
= bt2
.RealValue(bt2
.ArrayValue())
1106 def test_assign_true(self
):
1107 self
._def
.value
= True
1108 self
.assertTrue(self
._def
)
1110 def test_assign_false(self
):
1111 self
._def
.value
= False
1112 self
.assertFalse(self
._def
)
1114 def test_assign_pos_int(self
):
1116 self
._def
.value
= raw
1117 self
.assertEqual(self
._def
, float(raw
))
1119 def test_assign_neg_int(self
):
1121 self
._def
.value
= raw
1122 self
.assertEqual(self
._def
, float(raw
))
1124 def test_assign_vint(self
):
1126 self
._def
.value
= bt2
.create_value(raw
)
1127 self
.assertEqual(self
._def
, float(raw
))
1129 def test_assign_float(self
):
1131 self
._def
.value
= raw
1132 self
.assertEqual(self
._def
, raw
)
1134 def test_assign_vfloat(self
):
1136 self
._def
.value
= bt2
.create_value(raw
)
1137 self
.assertEqual(self
._def
, raw
)
1139 def test_invalid_lshift(self
):
1140 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1142 def test_invalid_rshift(self
):
1143 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1145 def test_invalid_and(self
):
1146 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1148 def test_invalid_or(self
):
1149 self
._test
_invalid
_op
(lambda: self
._def |
23)
1151 def test_invalid_xor(self
):
1152 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1154 def test_invalid_invert(self
):
1155 self
._test
_invalid
_op
(lambda: ~self
._def
)
1158 _inject_numeric_testing_methods(RealValueTestCase
)
1161 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1163 self
._def
_value
= 'Hello, World!'
1164 self
._def
= bt2
.StringValue(self
._def
_value
)
1165 self
._def
_new
_value
= 'Yes!'
1170 def _assert_expecting_str(self
):
1171 return self
.assertRaises(TypeError)
1173 def test_create_default(self
):
1174 s
= bt2
.StringValue()
1175 self
.assertEqual(s
, '')
1177 def test_create_from_str(self
):
1179 s
= bt2
.StringValue(raw
)
1180 self
.assertEqual(s
, raw
)
1182 def test_create_from_vstr(self
):
1184 s
= bt2
.StringValue(bt2
.create_value(raw
))
1185 self
.assertEqual(s
, raw
)
1187 def test_create_from_unknown(self
):
1191 with self
._assert
_expecting
_str
():
1192 i
= bt2
.StringValue(A())
1194 def test_create_from_varray(self
):
1195 with self
._assert
_expecting
_str
():
1196 i
= bt2
.StringValue(bt2
.ArrayValue())
1198 def test_assign_int(self
):
1199 with self
._assert
_expecting
_str
():
1200 self
._def
.value
= 283
1202 def test_assign_str(self
):
1205 self
.assertEqual(self
._def
, raw
)
1207 def test_assign_vstr(self
):
1209 self
._def
= bt2
.create_value(raw
)
1210 self
.assertEqual(self
._def
, raw
)
1213 self
.assertEqual(self
._def
, self
._def
_value
)
1216 self
.assertNotEqual(self
._def
, 23)
1218 def test_lt_vstring(self
):
1219 s1
= bt2
.StringValue('allo')
1220 s2
= bt2
.StringValue('bateau')
1221 self
.assertLess(s1
, s2
)
1223 def test_lt_string(self
):
1224 s1
= bt2
.StringValue('allo')
1225 self
.assertLess(s1
, 'bateau')
1227 def test_le_vstring(self
):
1228 s1
= bt2
.StringValue('allo')
1229 s2
= bt2
.StringValue('bateau')
1230 self
.assertLessEqual(s1
, s2
)
1232 def test_le_string(self
):
1233 s1
= bt2
.StringValue('allo')
1234 self
.assertLessEqual(s1
, 'bateau')
1236 def test_gt_vstring(self
):
1237 s1
= bt2
.StringValue('allo')
1238 s2
= bt2
.StringValue('bateau')
1239 self
.assertGreater(s2
, s1
)
1241 def test_gt_string(self
):
1242 s1
= bt2
.StringValue('allo')
1243 self
.assertGreater('bateau', s1
)
1245 def test_ge_vstring(self
):
1246 s1
= bt2
.StringValue('allo')
1247 s2
= bt2
.StringValue('bateau')
1248 self
.assertGreaterEqual(s2
, s1
)
1250 def test_ge_string(self
):
1251 s1
= bt2
.StringValue('allo')
1252 self
.assertGreaterEqual('bateau', s1
)
1254 def test_bool_op(self
):
1255 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1257 def test_str_op(self
):
1258 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1261 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1263 def test_getitem(self
):
1264 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1266 def test_append_str(self
):
1267 to_append
= 'meow meow meow'
1268 self
._def
+= to_append
1269 self
._def
_value
+= to_append
1270 self
.assertEqual(self
._def
, self
._def
_value
)
1272 def test_append_vstr(self
):
1273 to_append
= 'meow meow meow'
1274 self
._def
+= bt2
.create_value(to_append
)
1275 self
._def
_value
+= to_append
1276 self
.assertEqual(self
._def
, self
._def
_value
)
1279 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1281 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1282 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1287 def _modify_def(self
):
1288 self
._def
[2] = 'xyz'
1290 def _assert_type_error(self
):
1291 return self
.assertRaises(TypeError)
1293 def test_create_default(self
):
1294 a
= bt2
.ArrayValue()
1295 self
.assertEqual(len(a
), 0)
1297 def test_create_from_array(self
):
1298 self
.assertEqual(self
._def
, self
._def
_value
)
1300 def test_create_from_tuple(self
):
1301 t
= 1, 2, False, None
1302 a
= bt2
.ArrayValue(t
)
1303 self
.assertEqual(a
, t
)
1305 def test_create_from_varray(self
):
1306 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1307 a
= bt2
.ArrayValue(va
)
1308 self
.assertEqual(va
, a
)
1310 def test_create_from_unknown(self
):
1314 with self
._assert
_type
_error
():
1315 a
= bt2
.ArrayValue(A())
1317 def test_bool_op_true(self
):
1318 self
.assertTrue(bool(self
._def
))
1320 def test_bool_op_false(self
):
1321 self
.assertFalse(bool(bt2
.ArrayValue()))
1324 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1326 def test_eq_int(self
):
1327 self
.assertNotEqual(self
._def
, 23)
1329 def test_eq_diff_len(self
):
1330 a1
= bt2
.create_value([1, 2, 3])
1331 a2
= bt2
.create_value([1, 2])
1332 self
.assertNotEqual(a1
, a2
)
1334 def test_eq_diff_content_same_len(self
):
1335 a1
= bt2
.create_value([1, 2, 3])
1336 a2
= bt2
.create_value([4, 5, 6])
1337 self
.assertNotEqual(a1
, a2
)
1339 def test_eq_same_content_same_len(self
):
1340 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1341 a1
= bt2
.ArrayValue(raw
)
1342 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1343 self
.assertEqual(a1
, a2
)
1345 def test_setitem_int(self
):
1348 self
.assertEqual(self
._def
[2], raw
)
1350 def test_setitem_vint(self
):
1352 self
._def
[2] = bt2
.create_value(raw
)
1353 self
.assertEqual(self
._def
[2], raw
)
1355 def test_setitem_none(self
):
1357 self
.assertIsNone(self
._def
[2])
1359 def test_setitem_index_wrong_type(self
):
1360 with self
._assert
_type
_error
():
1361 self
._def
['yes'] = 23
1363 def test_setitem_index_neg(self
):
1364 with self
.assertRaises(IndexError):
1367 def test_setitem_index_out_of_range(self
):
1368 with self
.assertRaises(IndexError):
1369 self
._def
[len(self
._def
)] = 23
1371 def test_append_none(self
):
1372 self
._def
.append(None)
1373 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1375 def test_append_int(self
):
1377 self
._def
.append(raw
)
1378 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1380 def test_append_vint(self
):
1382 self
._def
.append(bt2
.create_value(raw
))
1383 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1385 def test_append_unknown(self
):
1389 with self
._assert
_type
_error
():
1390 self
._def
.append(A())
1392 def test_iadd(self
):
1395 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1396 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1397 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1399 def test_iadd_unknown(self
):
1403 with self
._assert
_type
_error
():
1406 def test_iadd_list_unknown(self
):
1410 with self
._assert
_type
_error
():
1413 def test_iter(self
):
1414 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1415 self
.assertEqual(velem
, elem
)
1418 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1431 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1436 def _modify_def(self
):
1437 self
._def
['zero'] = 1
1439 def test_create_default(self
):
1441 self
.assertEqual(len(m
), 0)
1443 def test_create_from_dict(self
):
1444 self
.assertEqual(self
._def
, self
._def
_value
)
1446 def test_create_from_vmap(self
):
1447 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1448 m
= bt2
.MapValue(vm
)
1449 self
.assertEqual(vm
, m
)
1451 def test_create_from_unknown(self
):
1455 with self
.assertRaises(AttributeError):
1456 m
= bt2
.MapValue(A())
1458 def test_bool_op_true(self
):
1459 self
.assertTrue(bool(self
._def
))
1461 def test_bool_op_false(self
):
1462 self
.assertFalse(bool(bt2
.MapValue()))
1465 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1467 def test_eq_int(self
):
1468 self
.assertNotEqual(self
._def
, 23)
1470 def test_eq_diff_len(self
):
1471 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1472 a2
= bt2
.create_value({'a': 1, 'b': 2})
1473 self
.assertNotEqual(a1
, a2
)
1475 def test_eq_diff_content_same_len(self
):
1476 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1477 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1478 self
.assertNotEqual(a1
, a2
)
1480 def test_eq_same_content_diff_keys(self
):
1481 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1482 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1483 self
.assertNotEqual(a1
, a2
)
1485 def test_eq_same_content_same_len(self
):
1489 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]
1491 a1
= bt2
.MapValue(raw
)
1492 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1493 self
.assertEqual(a1
, a2
)
1494 self
.assertEqual(a1
, raw
)
1496 def test_setitem_int(self
):
1498 self
._def
['pos-int'] = raw
1499 self
.assertEqual(self
._def
['pos-int'], raw
)
1501 def test_setitem_vint(self
):
1503 self
._def
['pos-int'] = bt2
.create_value(raw
)
1504 self
.assertEqual(self
._def
['pos-int'], raw
)
1506 def test_setitem_none(self
):
1507 self
._def
['none'] = None
1508 self
.assertIsNone(self
._def
['none'])
1510 def test_setitem_new_int(self
):
1511 old_len
= len(self
._def
)
1512 self
._def
['new-int'] = 23
1513 self
.assertEqual(self
._def
['new-int'], 23)
1514 self
.assertEqual(len(self
._def
), old_len
+ 1)
1516 def test_setitem_index_wrong_type(self
):
1517 with self
.assertRaises(TypeError):
1520 def test_iter(self
):
1521 for vkey
, vval
in self
._def
.items():
1522 val
= self
._def
_value
[vkey
]
1523 self
.assertEqual(vval
, val
)
1525 def test_getitem_wrong_key(self
):
1526 with self
.assertRaises(KeyError):
1527 self
._def
['kilojoule']