1 # SPDX-License-Identifier: GPL-2.0-only
3 # Copyright (C) 2019 EfficiOS Inc.
11 from functools
import partial
, partialmethod
16 # The value object classes explicitly do not implement the copy methods,
17 # raising `NotImplementedError`, just in case we decide to implement
19 class _TestCopySimple
:
21 with self
.assertRaises(NotImplementedError):
24 def test_deepcopy(self
):
25 with self
.assertRaises(NotImplementedError):
26 copy
.deepcopy(self
._def
)
29 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
32 # Base class for numeric value test cases.
34 # To be compatible with this base class, a derived class must, in its
37 # * Set `self._def` to a value object with an arbitrary raw value.
38 # * Set `self._def_value` to the equivalent raw value of `self._def`.
39 class _TestNumericValue(_TestCopySimple
):
40 # Tries the binary operation `op`:
42 # 1. Between `self._def`, which is a value object, and `rhs`.
43 # 2. Between `self._def_value`, which is the raw value of
44 # `self._def`, and `rhs`.
46 # Returns the results of 1. and 2.
48 # If there's an exception while performing 1. or 2., asserts that
49 # both operations raised exceptions, that both exceptions have the
50 # same type, and returns `None` for both results.
51 def _binop(self
, op
, rhs
):
56 # try with value object
58 r
= op(self
._def
, rhs
)
59 except Exception as e
:
64 rv
= op(self
._def
_value
, comp_value
)
65 except Exception as e
:
68 if type_rexc
is not None or type_rvexc
is not None:
69 # at least one of the operations raised an exception: in
70 # this case both operations should have raised the same
71 # type of exception (division by zero, bit shift with a
72 # floating point number operand, etc.)
73 self
.assertIs(type_rexc
, type_rvexc
)
78 # Tries the unary operation `op`:
80 # 1. On `self._def`, which is a value object.
81 # 2. On `self._def_value`, which is the raw value of `self._def`.
83 # Returns the results of 1. and 2.
85 # If there's an exception while performing 1. or 2., asserts that
86 # both operations raised exceptions, that both exceptions have the
87 # same type, and returns `None` for both results.
88 def _unaryop(self
, op
):
92 # try with value object
95 except Exception as e
:
100 rv
= op(self
._def
_value
)
101 except Exception as e
:
104 if type_rexc
is not None or type_rvexc
is not None:
105 # at least one of the operations raised an exception: in
106 # this case both operations should have raised the same
107 # type of exception (division by zero, bit shift with a
108 # floating point number operand, etc.)
109 self
.assertIs(type_rexc
, type_rvexc
)
114 # Tests that the unary operation `op` gives results with the same
115 # type for both `self._def` and `self._def_value`.
116 def _test_unaryop_type(self
, op
):
117 r
, rv
= self
._unaryop
(op
)
122 self
.assertIsInstance(r
, type(rv
))
124 # Tests that the unary operation `op` gives results with the same
125 # value for both `self._def` and `self._def_value`. This uses the
126 # __eq__() operator of `self._def`.
127 def _test_unaryop_value(self
, op
):
128 r
, rv
= self
._unaryop
(op
)
133 self
.assertEqual(r
, rv
)
135 # Tests that the unary operation `op`, when applied to `self._def`,
136 # does not change its underlying BT object address.
137 def _test_unaryop_addr_same(self
, op
):
138 addr_before
= self
._def
.addr
140 self
.assertEqual(self
._def
.addr
, addr_before
)
142 # Tests that the unary operation `op`, when applied to `self._def`,
143 # does not change its value.
144 def _test_unaryop_value_same(self
, op
):
145 value_before
= self
._def
.__class
__(self
._def
)
147 self
.assertEqual(self
._def
, value_before
)
149 # Tests that the binary operation `op` gives results with the same
150 # type for both `self._def` and `self._def_value`.
151 def _test_binop_type(self
, op
, rhs
):
152 r
, rv
= self
._binop
(op
, rhs
)
157 if op
in _COMP_BINOPS
:
158 # __eq__() and __ne__() always return a 'bool' object
159 self
.assertIsInstance(r
, bool)
161 self
.assertIsInstance(r
, type(rv
))
163 # Tests that the binary operation `op` gives results with the same
164 # value for both `self._def` and `self._def_value`. This uses the
165 # __eq__() operator of `self._def`.
166 def _test_binop_value(self
, op
, rhs
):
167 r
, rv
= self
._binop
(op
, rhs
)
172 self
.assertEqual(r
, rv
)
174 # Tests that the binary operation `op`, when applied to `self._def`,
175 # does not change its underlying BT object address.
176 def _test_binop_lhs_addr_same(self
, op
, rhs
):
177 addr_before
= self
._def
.addr
178 r
, rv
= self
._binop
(op
, rhs
)
179 self
.assertEqual(self
._def
.addr
, addr_before
)
181 # Tests that the binary operation `op`, when applied to `self._def`,
182 # does not change its value.
183 def _test_binop_lhs_value_same(self
, op
, rhs
):
184 value_before
= self
._def
.__class
__(self
._def
)
185 r
, rv
= self
._binop
(op
, rhs
)
186 self
.assertEqual(self
._def
, value_before
)
188 # The methods below which take the `test_cb` and `op` parameters
189 # are meant to be used with one of the _test_binop_*() functions
190 # above as `test_cb` and a binary operator function as `op`.
194 # self._test_binop_rhs_pos_int(self._test_binop_value,
197 # This tests that a numeric value object added to a positive integer
198 # raw value gives a result with the expected value.
200 # `vint` and `vfloat` mean a signed integer value object and a real
203 def _test_binop_unknown(self
, op
):
204 if op
is operator
.eq
:
205 self
.assertIs(op(self
._def
, object()), False)
206 elif op
is operator
.ne
:
207 self
.assertIs(op(self
._def
, object()), True)
209 with self
.assertRaises(TypeError):
210 op(self
._def
, object())
212 def _test_binop_none(self
, op
):
213 if op
is operator
.eq
:
214 self
.assertIs(op(self
._def
, None), False)
215 elif op
is operator
.ne
:
216 self
.assertIs(op(self
._def
, None), True)
218 with self
.assertRaises(TypeError):
221 def _test_binop_rhs_false(self
, test_cb
, op
):
224 def _test_binop_rhs_true(self
, test_cb
, op
):
227 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
230 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
233 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
236 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
237 test_cb(op
, bt2
.create_value(2))
239 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
240 test_cb(op
, bt2
.create_value(-23))
242 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
243 test_cb(op
, bt2
.create_value(0))
245 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
248 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
251 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
254 def _test_binop_rhs_complex(self
, test_cb
, op
):
255 test_cb(op
, -23 + 19j
)
257 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
260 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
261 test_cb(op
, bt2
.create_value(2.2))
263 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
264 test_cb(op
, bt2
.create_value(-23.4))
266 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
267 test_cb(op
, bt2
.create_value(0.0))
269 def _test_binop_type_false(self
, op
):
270 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
272 def _test_binop_type_true(self
, op
):
273 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
275 def _test_binop_type_pos_int(self
, op
):
276 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
278 def _test_binop_type_neg_int(self
, op
):
279 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
281 def _test_binop_type_zero_int(self
, op
):
282 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
284 def _test_binop_type_pos_vint(self
, op
):
285 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
287 def _test_binop_type_neg_vint(self
, op
):
288 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
290 def _test_binop_type_zero_vint(self
, op
):
291 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
293 def _test_binop_type_pos_float(self
, op
):
294 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
296 def _test_binop_type_neg_float(self
, op
):
297 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
299 def _test_binop_type_zero_float(self
, op
):
300 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
302 def _test_binop_type_pos_vfloat(self
, op
):
303 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
305 def _test_binop_type_neg_vfloat(self
, op
):
306 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
308 def _test_binop_type_zero_vfloat(self
, op
):
309 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
311 def _test_binop_type_complex(self
, op
):
312 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
314 def _test_binop_type_zero_complex(self
, op
):
315 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
317 def _test_binop_value_false(self
, op
):
318 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
320 def _test_binop_value_true(self
, op
):
321 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
323 def _test_binop_value_pos_int(self
, op
):
324 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
326 def _test_binop_value_neg_int(self
, op
):
327 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
329 def _test_binop_value_zero_int(self
, op
):
330 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
332 def _test_binop_value_pos_vint(self
, op
):
333 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
335 def _test_binop_value_neg_vint(self
, op
):
336 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
338 def _test_binop_value_zero_vint(self
, op
):
339 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
341 def _test_binop_value_pos_float(self
, op
):
342 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
344 def _test_binop_value_neg_float(self
, op
):
345 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
347 def _test_binop_value_zero_float(self
, op
):
348 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
350 def _test_binop_value_pos_vfloat(self
, op
):
351 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
353 def _test_binop_value_neg_vfloat(self
, op
):
354 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
356 def _test_binop_value_zero_vfloat(self
, op
):
357 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
359 def _test_binop_value_complex(self
, op
):
360 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
362 def _test_binop_value_zero_complex(self
, op
):
363 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
365 def _test_binop_lhs_addr_same_false(self
, op
):
366 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
368 def _test_binop_lhs_addr_same_true(self
, op
):
369 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
371 def _test_binop_lhs_addr_same_pos_int(self
, op
):
372 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
374 def _test_binop_lhs_addr_same_neg_int(self
, op
):
375 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
377 def _test_binop_lhs_addr_same_zero_int(self
, op
):
378 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
380 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
381 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
383 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
384 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
386 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
387 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
389 def _test_binop_lhs_addr_same_pos_float(self
, op
):
390 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
392 def _test_binop_lhs_addr_same_neg_float(self
, op
):
393 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
395 def _test_binop_lhs_addr_same_zero_float(self
, op
):
396 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
398 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
399 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
401 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
402 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
404 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
405 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
407 def _test_binop_lhs_addr_same_complex(self
, op
):
408 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
410 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
411 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
413 def _test_binop_lhs_value_same_false(self
, op
):
414 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
416 def _test_binop_lhs_value_same_true(self
, op
):
417 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
419 def _test_binop_lhs_value_same_pos_int(self
, op
):
420 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
422 def _test_binop_lhs_value_same_neg_int(self
, op
):
423 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
425 def _test_binop_lhs_value_same_zero_int(self
, op
):
426 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
428 def _test_binop_lhs_value_same_pos_vint(self
, op
):
429 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
431 def _test_binop_lhs_value_same_neg_vint(self
, op
):
432 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
434 def _test_binop_lhs_value_same_zero_vint(self
, op
):
435 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
437 def _test_binop_lhs_value_same_pos_float(self
, op
):
438 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
440 def _test_binop_lhs_value_same_neg_float(self
, op
):
441 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
443 def _test_binop_lhs_value_same_zero_float(self
, op
):
444 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
446 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
447 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
449 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
450 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
452 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
453 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
455 def _test_binop_lhs_value_same_complex(self
, op
):
456 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
458 def _test_binop_lhs_value_same_zero_complex(self
, op
):
459 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
461 def test_bool_op(self
):
462 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
464 def test_int_op(self
):
465 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
467 def test_float_op(self
):
468 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
470 def test_complex_op(self
):
471 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
473 def test_str_op(self
):
474 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
476 def test_eq_none(self
):
477 # Disable the "comparison to None" warning, as this is precisely what
478 # we want to test here.
479 self
.assertFalse(self
._def
== None) # noqa: E711
481 def test_ne_none(self
):
482 # Disable the "comparison to None" warning, as this is precisely what
483 # we want to test here.
484 self
.assertTrue(self
._def
!= None) # noqa: E711
487 # This is a list of binary operators used for
488 # _inject_numeric_testing_methods().
490 # Each entry is a pair of binary operator name (used as part of the
491 # created testing method's name) and operator function.
499 ("add", operator
.add
),
500 ("radd", lambda a
, b
: operator
.add(b
, a
)),
501 ("and", operator
.and_
),
502 ("rand", lambda a
, b
: operator
.and_(b
, a
)),
503 ("floordiv", operator
.floordiv
),
504 ("rfloordiv", lambda a
, b
: operator
.floordiv(b
, a
)),
505 ("lshift", operator
.lshift
),
506 ("rlshift", lambda a
, b
: operator
.lshift(b
, a
)),
507 ("mod", operator
.mod
),
508 ("rmod", lambda a
, b
: operator
.mod(b
, a
)),
509 ("mul", operator
.mul
),
510 ("rmul", lambda a
, b
: operator
.mul(b
, a
)),
511 ("or", operator
.or_
),
512 ("ror", lambda a
, b
: operator
.or_(b
, a
)),
513 ("pow", operator
.pow),
514 ("rpow", lambda a
, b
: operator
.pow(b
, a
)),
515 ("rshift", operator
.rshift
),
516 ("rrshift", lambda a
, b
: operator
.rshift(b
, a
)),
517 ("sub", operator
.sub
),
518 ("rsub", lambda a
, b
: operator
.sub(b
, a
)),
519 ("truediv", operator
.truediv
),
520 ("rtruediv", lambda a
, b
: operator
.truediv(b
, a
)),
521 ("xor", operator
.xor
),
522 ("rxor", lambda a
, b
: operator
.xor(b
, a
)),
526 # This is a list of unary operators used for
527 # _inject_numeric_testing_methods().
529 # Each entry is a pair of unary operator name (used as part of the
530 # created testing method's name) and operator function.
532 ("neg", operator
.neg
),
533 ("pos", operator
.pos
),
534 ("abs", operator
.abs),
535 ("invert", operator
.invert
),
537 ("round_0", partial(round, ndigits
=0)),
538 ("round_1", partial(round, ndigits
=1)),
539 ("round_2", partial(round, ndigits
=2)),
540 ("round_3", partial(round, ndigits
=3)),
542 ("floor", math
.floor
),
543 ("trunc", math
.trunc
),
547 # This function injects a bunch of testing methods to a numeric
550 # It is meant to be used like this:
552 # _inject_numeric_testing_methods(MyNumericValueTestCase)
554 # This function injects:
556 # * One testing method for each _TestNumericValue._test_binop_*()
557 # method, for each binary operator in the _BINOPS tuple.
559 # * One testing method for each _TestNumericValue._test_unaryop*()
560 # method, for each unary operator in the _UNARYOPS tuple.
561 def _inject_numeric_testing_methods(cls
):
562 def test_binop_name(suffix
):
563 return "test_binop_{}_{}".format(name
, suffix
)
565 def test_unaryop_name(suffix
):
566 return "test_unaryop_{}_{}".format(name
, suffix
)
568 # inject testing methods for each binary operation
569 for name
, binop
in _BINOPS
:
572 test_binop_name("unknown"),
573 partialmethod(_TestNumericValue
._test
_binop
_unknown
, op
=binop
),
577 test_binop_name("none"),
578 partialmethod(_TestNumericValue
._test
_binop
_none
, op
=binop
),
582 test_binop_name("type_true"),
583 partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
),
587 test_binop_name("type_pos_int"),
588 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
),
592 test_binop_name("type_pos_vint"),
593 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
),
597 test_binop_name("value_true"),
598 partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
),
602 test_binop_name("value_pos_int"),
603 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
),
607 test_binop_name("value_pos_vint"),
608 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
),
612 test_binop_name("lhs_addr_same_true"),
613 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
617 test_binop_name("lhs_addr_same_pos_int"),
619 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
624 test_binop_name("lhs_addr_same_pos_vint"),
626 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
631 test_binop_name("lhs_value_same_true"),
632 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
636 test_binop_name("lhs_value_same_pos_int"),
638 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
643 test_binop_name("lhs_value_same_pos_vint"),
645 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
650 test_binop_name("type_neg_int"),
651 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
),
655 test_binop_name("type_neg_vint"),
656 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
),
660 test_binop_name("value_neg_int"),
661 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
),
665 test_binop_name("value_neg_vint"),
666 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
),
670 test_binop_name("lhs_addr_same_neg_int"),
672 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
677 test_binop_name("lhs_addr_same_neg_vint"),
679 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
684 test_binop_name("lhs_value_same_neg_int"),
686 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
691 test_binop_name("lhs_value_same_neg_vint"),
693 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
698 test_binop_name("type_false"),
699 partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
),
703 test_binop_name("type_zero_int"),
704 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
),
708 test_binop_name("type_zero_vint"),
709 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
),
713 test_binop_name("value_false"),
714 partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
),
718 test_binop_name("value_zero_int"),
719 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
),
723 test_binop_name("value_zero_vint"),
724 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
),
728 test_binop_name("lhs_addr_same_false"),
729 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
733 test_binop_name("lhs_addr_same_zero_int"),
735 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
740 test_binop_name("lhs_addr_same_zero_vint"),
742 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
747 test_binop_name("lhs_value_same_false"),
748 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
752 test_binop_name("lhs_value_same_zero_int"),
754 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
759 test_binop_name("lhs_value_same_zero_vint"),
761 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
766 test_binop_name("type_neg_float"),
767 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
),
771 test_binop_name("type_neg_vfloat"),
772 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
776 test_binop_name("value_neg_float"),
777 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
),
781 test_binop_name("value_neg_vfloat"),
782 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
786 test_binop_name("lhs_addr_same_neg_float"),
788 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
793 test_binop_name("lhs_addr_same_neg_vfloat"),
795 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
800 test_binop_name("lhs_value_same_neg_float"),
802 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
807 test_binop_name("lhs_value_same_neg_vfloat"),
809 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
814 test_binop_name("type_pos_float"),
815 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
),
819 test_binop_name("type_pos_vfloat"),
820 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
824 test_binop_name("value_pos_float"),
825 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
),
829 test_binop_name("value_pos_vfloat"),
830 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
834 test_binop_name("lhs_addr_same_pos_float"),
836 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
841 test_binop_name("lhs_addr_same_pos_vfloat"),
843 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
848 test_binop_name("lhs_value_same_pos_float"),
850 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
855 test_binop_name("lhs_value_same_pos_vfloat"),
857 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
862 test_binop_name("type_zero_float"),
863 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
),
867 test_binop_name("type_zero_vfloat"),
868 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
872 test_binop_name("value_zero_float"),
873 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
),
877 test_binop_name("value_zero_vfloat"),
878 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
882 test_binop_name("lhs_addr_same_zero_float"),
884 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
889 test_binop_name("lhs_addr_same_zero_vfloat"),
891 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
896 test_binop_name("lhs_value_same_zero_float"),
898 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
903 test_binop_name("lhs_value_same_zero_vfloat"),
905 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
910 test_binop_name("type_complex"),
911 partialmethod(_TestNumericValue
._test
_binop
_type
_complex
, op
=binop
),
915 test_binop_name("type_zero_complex"),
916 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_complex
, op
=binop
),
920 test_binop_name("value_complex"),
921 partialmethod(_TestNumericValue
._test
_binop
_value
_complex
, op
=binop
),
925 test_binop_name("value_zero_complex"),
926 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_complex
, op
=binop
),
930 test_binop_name("lhs_addr_same_complex"),
932 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
937 test_binop_name("lhs_addr_same_zero_complex"),
939 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
944 test_binop_name("lhs_value_same_complex"),
946 _TestNumericValue
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
951 test_binop_name("lhs_value_same_zero_complex"),
953 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
957 # inject testing methods for each unary operation
958 for name
, unaryop
in _UNARYOPS
:
961 test_unaryop_name("type"),
962 partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
),
966 test_unaryop_name("value"),
967 partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
),
971 test_unaryop_name("addr_same"),
972 partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
),
976 test_unaryop_name("value_same"),
977 partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
),
981 class CreateValueFuncTestCase(unittest
.TestCase
):
982 def test_create_none(self
):
983 v
= bt2
.create_value(None)
986 def test_create_bool_false(self
):
987 v
= bt2
.create_value(False)
988 self
.assertIsInstance(v
, bt2
.BoolValue
)
991 def test_create_bool_true(self
):
992 v
= bt2
.create_value(True)
993 self
.assertIsInstance(v
, bt2
.BoolValue
)
996 def test_create_int_pos(self
):
998 v
= bt2
.create_value(raw
)
999 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1000 self
.assertEqual(v
, raw
)
1002 def test_create_int_neg(self
):
1004 v
= bt2
.create_value(raw
)
1005 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1006 self
.assertEqual(v
, raw
)
1008 def test_create_float_pos(self
):
1010 v
= bt2
.create_value(raw
)
1011 self
.assertIsInstance(v
, bt2
.RealValue
)
1012 self
.assertEqual(v
, raw
)
1014 def test_create_float_neg(self
):
1016 v
= bt2
.create_value(raw
)
1017 self
.assertIsInstance(v
, bt2
.RealValue
)
1018 self
.assertEqual(v
, raw
)
1020 def test_create_string(self
):
1022 v
= bt2
.create_value(raw
)
1023 self
.assertIsInstance(v
, bt2
.StringValue
)
1024 self
.assertEqual(v
, raw
)
1026 def test_create_string_empty(self
):
1028 v
= bt2
.create_value(raw
)
1029 self
.assertIsInstance(v
, bt2
.StringValue
)
1030 self
.assertEqual(v
, raw
)
1032 def test_create_array_from_list(self
):
1034 v
= bt2
.create_value(raw
)
1035 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1036 self
.assertEqual(v
, raw
)
1038 def test_create_array_from_tuple(self
):
1040 v
= bt2
.create_value(raw
)
1041 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1042 self
.assertEqual(v
, raw
)
1044 def test_create_array_from_empty_list(self
):
1046 v
= bt2
.create_value(raw
)
1047 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1048 self
.assertEqual(v
, raw
)
1050 def test_create_array_from_empty_tuple(self
):
1052 v
= bt2
.create_value(raw
)
1053 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1054 self
.assertEqual(v
, raw
)
1056 def test_create_map(self
):
1058 v
= bt2
.create_value(raw
)
1059 self
.assertIsInstance(v
, bt2
.MapValue
)
1060 self
.assertEqual(v
, raw
)
1062 def test_create_map_empty(self
):
1064 v
= bt2
.create_value(raw
)
1065 self
.assertIsInstance(v
, bt2
.MapValue
)
1066 self
.assertEqual(v
, raw
)
1068 def test_create_vfalse(self
):
1069 v
= bt2
.create_value(bt2
.create_value(False))
1070 self
.assertIsInstance(v
, bt2
.BoolValue
)
1073 def test_create_invalid(self
):
1079 with self
.assertRaisesRegex(
1080 TypeError, "cannot create value object from 'A' object"
1085 def _create_const_value(value
):
1086 class MySink(bt2
._UserSinkComponent
):
1087 def _user_consume(self
):
1091 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
1093 return {"my_value": value
}
1095 res
= bt2
.QueryExecutor(MySink
, "obj", None).query()
1096 return res
["my_value"]
1099 class BoolValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1101 self
._f
= bt2
.BoolValue(False)
1102 self
._t
= bt2
.BoolValue(True)
1104 self
._def
_value
= False
1105 self
._def
_new
_value
= True
1112 def _assert_expecting_bool(self
):
1113 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
1115 def test_create_default(self
):
1119 def test_create_false(self
):
1120 self
.assertFalse(self
._f
)
1122 def test_create_true(self
):
1123 self
.assertTrue(self
._t
)
1125 def test_create_from_vfalse(self
):
1126 b
= bt2
.BoolValue(self
._f
)
1129 def test_create_from_vtrue(self
):
1130 b
= bt2
.BoolValue(self
._t
)
1133 def test_create_from_int_non_zero(self
):
1134 with self
.assertRaises(TypeError):
1137 def test_create_from_int_zero(self
):
1138 with self
.assertRaises(TypeError):
1141 def test_assign_true(self
):
1146 def test_assign_false(self
):
1151 def test_assign_vtrue(self
):
1156 def test_assign_vfalse(self
):
1161 def test_assign_int(self
):
1162 with self
.assertRaises(TypeError):
1166 def test_bool_op(self
):
1167 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1169 def test_str_op(self
):
1170 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1172 def test_eq_none(self
):
1173 # Disable the "comparison to None" warning, as this is precisely what
1174 # we want to test here.
1175 self
.assertFalse(self
._def
== None) # noqa: E711
1177 def test_ne_none(self
):
1178 # Disable the "comparison to None" warning, as this is precisely what
1179 # we want to test here.
1180 self
.assertTrue(self
._def
!= None) # noqa: E711
1182 def test_vfalse_eq_false(self
):
1183 self
.assertEqual(self
._f
, False)
1185 def test_vfalse_ne_true(self
):
1186 self
.assertNotEqual(self
._f
, True)
1188 def test_vtrue_eq_true(self
):
1189 self
.assertEqual(self
._t
, True)
1191 def test_vtrue_ne_false(self
):
1192 self
.assertNotEqual(self
._t
, False)
1195 _inject_numeric_testing_methods(BoolValueTestCase
)
1198 class _TestIntegerValue(_TestNumericValue
):
1201 self
._ip
= self
._CLS
(self
._pv
)
1202 self
._def
= self
._ip
1203 self
._def
_value
= self
._pv
1204 self
._def
_new
_value
= 101
1211 def _assert_expecting_int(self
):
1212 return self
.assertRaisesRegex(TypeError, r
"expecting an integral number object")
1214 def _assert_expecting_int64(self
):
1215 return self
.assertRaisesRegex(
1216 ValueError, r
"expecting a signed 64-bit integral value"
1219 def _assert_expecting_uint64(self
):
1220 return self
.assertRaisesRegex(
1221 ValueError, r
"expecting an unsigned 64-bit integral value"
1224 def test_create_default(self
):
1226 self
.assertEqual(i
, 0)
1228 def test_create_pos(self
):
1229 self
.assertEqual(self
._ip
, self
._pv
)
1231 def test_create_from_vint(self
):
1232 i
= self
._CLS
(self
._ip
)
1233 self
.assertEqual(i
, self
._pv
)
1235 def test_create_from_false(self
):
1236 i
= self
._CLS
(False)
1239 def test_create_from_true(self
):
1243 def test_create_from_unknown(self
):
1247 with self
._assert
_expecting
_int
():
1250 def test_create_from_varray(self
):
1251 with self
._assert
_expecting
_int
():
1252 self
._CLS
(bt2
.ArrayValue())
1254 def test_assign_true(self
):
1256 self
._def
.value
= raw
1257 self
.assertEqual(self
._def
, raw
)
1259 def test_assign_false(self
):
1261 self
._def
.value
= raw
1262 self
.assertEqual(self
._def
, raw
)
1264 def test_assign_pos_int(self
):
1266 self
._def
.value
= raw
1267 self
.assertEqual(self
._def
, raw
)
1269 def test_assign_vint(self
):
1271 self
._def
.value
= bt2
.create_value(raw
)
1272 self
.assertEqual(self
._def
, raw
)
1275 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1276 _CLS
= bt2
.SignedIntegerValue
1281 self
._in
= self
._CLS
(self
._nv
)
1282 self
._def
_new
_value
= -101
1288 def test_create_neg(self
):
1289 self
.assertEqual(self
._in
, self
._nv
)
1291 def test_create_pos_too_big(self
):
1292 with self
._assert
_expecting
_int
64():
1295 def test_create_neg_too_big(self
):
1296 with self
._assert
_expecting
_int
64():
1297 self
._CLS
(-(2**63) - 1)
1299 def test_assign_neg_int(self
):
1301 self
._def
.value
= raw
1302 self
.assertEqual(self
._def
, raw
)
1304 def test_compare_big_int(self
):
1305 # Larger than the IEEE 754 double-precision exact representation of
1308 v
= bt2
.create_value(raw
)
1309 self
.assertEqual(v
, raw
)
1312 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
1315 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1316 _CLS
= bt2
.UnsignedIntegerValue
1318 def test_create_pos_too_big(self
):
1319 with self
._assert
_expecting
_uint
64():
1322 def test_create_neg(self
):
1323 with self
._assert
_expecting
_uint
64():
1327 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
)
1330 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1334 self
._fp
= bt2
.RealValue(self
._pv
)
1335 self
._fn
= bt2
.RealValue(self
._nv
)
1336 self
._def
= self
._fp
1337 self
._def
_value
= self
._pv
1338 self
._def
_new
_value
= -101.88
1346 def _assert_expecting_float(self
):
1347 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
1349 def _test_invalid_op(self
, cb
):
1350 with self
.assertRaises(TypeError):
1353 def test_create_default(self
):
1355 self
.assertEqual(f
, 0.0)
1357 def test_create_pos(self
):
1358 self
.assertEqual(self
._fp
, self
._pv
)
1360 def test_create_neg(self
):
1361 self
.assertEqual(self
._fn
, self
._nv
)
1363 def test_create_from_false(self
):
1364 f
= bt2
.RealValue(False)
1367 def test_create_from_true(self
):
1368 f
= bt2
.RealValue(True)
1371 def test_create_from_int(self
):
1373 f
= bt2
.RealValue(raw
)
1374 self
.assertEqual(f
, float(raw
))
1376 def test_create_from_vint(self
):
1378 f
= bt2
.RealValue(bt2
.create_value(raw
))
1379 self
.assertEqual(f
, float(raw
))
1381 def test_create_from_vfloat(self
):
1383 f
= bt2
.RealValue(bt2
.create_value(raw
))
1384 self
.assertEqual(f
, raw
)
1386 def test_create_from_unknown(self
):
1390 with self
._assert
_expecting
_float
():
1393 def test_create_from_varray(self
):
1394 with self
._assert
_expecting
_float
():
1395 bt2
.RealValue(bt2
.ArrayValue())
1397 def test_assign_true(self
):
1398 self
._def
.value
= True
1399 self
.assertTrue(self
._def
)
1401 def test_assign_false(self
):
1402 self
._def
.value
= False
1403 self
.assertFalse(self
._def
)
1405 def test_assign_pos_int(self
):
1407 self
._def
.value
= raw
1408 self
.assertEqual(self
._def
, float(raw
))
1410 def test_assign_neg_int(self
):
1412 self
._def
.value
= raw
1413 self
.assertEqual(self
._def
, float(raw
))
1415 def test_assign_vint(self
):
1417 self
._def
.value
= bt2
.create_value(raw
)
1418 self
.assertEqual(self
._def
, float(raw
))
1420 def test_assign_float(self
):
1422 self
._def
.value
= raw
1423 self
.assertEqual(self
._def
, raw
)
1425 def test_assign_vfloat(self
):
1427 self
._def
.value
= bt2
.create_value(raw
)
1428 self
.assertEqual(self
._def
, raw
)
1430 def test_invalid_lshift(self
):
1431 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1433 def test_invalid_rshift(self
):
1434 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1436 def test_invalid_and(self
):
1437 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1439 def test_invalid_or(self
):
1440 self
._test
_invalid
_op
(lambda: self
._def |
23)
1442 def test_invalid_xor(self
):
1443 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1445 def test_invalid_invert(self
):
1446 self
._test
_invalid
_op
(lambda: ~self
._def
)
1449 _inject_numeric_testing_methods(RealValueTestCase
)
1452 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1454 self
._def
_value
= "Hello, World!"
1455 self
._def
= bt2
.StringValue(self
._def
_value
)
1456 self
._def
_const
= _create_const_value(self
._def
_value
)
1457 self
._def
_new
_value
= "Yes!"
1462 def _assert_expecting_str(self
):
1463 return self
.assertRaises(TypeError)
1465 def test_create_default(self
):
1466 s
= bt2
.StringValue()
1467 self
.assertEqual(s
, "")
1469 def test_create_from_str(self
):
1471 s
= bt2
.StringValue(raw
)
1472 self
.assertEqual(s
, raw
)
1474 def test_create_from_vstr(self
):
1476 s
= bt2
.StringValue(bt2
.create_value(raw
))
1477 self
.assertEqual(s
, raw
)
1479 def test_create_from_unknown(self
):
1483 with self
._assert
_expecting
_str
():
1484 bt2
.StringValue(A())
1486 def test_create_from_varray(self
):
1487 with self
._assert
_expecting
_str
():
1488 bt2
.StringValue(bt2
.ArrayValue())
1490 def test_assign_int(self
):
1491 with self
._assert
_expecting
_str
():
1492 self
._def
.value
= 283
1494 def test_assign_str(self
):
1497 self
.assertEqual(self
._def
, raw
)
1499 def test_assign_vstr(self
):
1501 self
._def
= bt2
.create_value(raw
)
1502 self
.assertEqual(self
._def
, raw
)
1505 self
.assertEqual(self
._def
, self
._def
_value
)
1507 def test_const_eq(self
):
1508 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1510 def test_eq_raw(self
):
1511 self
.assertNotEqual(self
._def
, 23)
1513 def test_lt_vstring(self
):
1514 s1
= bt2
.StringValue("allo")
1515 s2
= bt2
.StringValue("bateau")
1516 self
.assertLess(s1
, s2
)
1518 def test_lt_string(self
):
1519 s1
= bt2
.StringValue("allo")
1520 self
.assertLess(s1
, "bateau")
1522 def test_le_vstring(self
):
1523 s1
= bt2
.StringValue("allo")
1524 s2
= bt2
.StringValue("bateau")
1525 self
.assertLessEqual(s1
, s2
)
1527 def test_le_string(self
):
1528 s1
= bt2
.StringValue("allo")
1529 self
.assertLessEqual(s1
, "bateau")
1531 def test_gt_vstring(self
):
1532 s1
= bt2
.StringValue("allo")
1533 s2
= bt2
.StringValue("bateau")
1534 self
.assertGreater(s2
, s1
)
1536 def test_gt_string(self
):
1537 s1
= bt2
.StringValue("allo")
1538 self
.assertGreater("bateau", s1
)
1540 def test_ge_vstring(self
):
1541 s1
= bt2
.StringValue("allo")
1542 s2
= bt2
.StringValue("bateau")
1543 self
.assertGreaterEqual(s2
, s1
)
1545 def test_ge_string(self
):
1546 s1
= bt2
.StringValue("allo")
1547 self
.assertGreaterEqual("bateau", s1
)
1549 def test_in_string(self
):
1550 s1
= bt2
.StringValue("beau grand bateau")
1551 self
.assertIn("bateau", s1
)
1553 def test_in_vstring(self
):
1554 s1
= bt2
.StringValue("beau grand bateau")
1555 s2
= bt2
.StringValue("bateau")
1556 self
.assertIn(s2
, s1
)
1558 def test_bool_op(self
):
1559 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1561 def test_str_op(self
):
1562 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1565 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1567 def test_getitem(self
):
1568 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1570 def test_const_getitem(self
):
1571 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1573 def test_iadd_str(self
):
1574 to_append
= "meow meow meow"
1575 self
._def
+= to_append
1576 self
._def
_value
+= to_append
1577 self
.assertEqual(self
._def
, self
._def
_value
)
1579 def test_const_iadd_str(self
):
1580 to_append
= "meow meow meow"
1581 with self
.assertRaises(TypeError):
1582 self
._def
_const
+= to_append
1584 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1586 def test_append_vstr(self
):
1587 to_append
= "meow meow meow"
1588 self
._def
+= bt2
.create_value(to_append
)
1589 self
._def
_value
+= to_append
1590 self
.assertEqual(self
._def
, self
._def
_value
)
1593 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1595 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, "yes"]
1596 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1597 self
._def
_const
= _create_const_value(copy
.deepcopy(self
._def
_value
))
1602 def _modify_def(self
):
1603 self
._def
[2] = "xyz"
1605 def _assert_type_error(self
):
1606 return self
.assertRaises(TypeError)
1608 def test_create_default(self
):
1609 a
= bt2
.ArrayValue()
1610 self
.assertEqual(len(a
), 0)
1612 def test_create_from_array(self
):
1613 self
.assertEqual(self
._def
, self
._def
_value
)
1615 def test_create_from_tuple(self
):
1616 t
= 1, 2, False, None
1617 a
= bt2
.ArrayValue(t
)
1618 self
.assertEqual(a
, t
)
1620 def test_create_from_varray(self
):
1621 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1622 a
= bt2
.ArrayValue(va
)
1623 self
.assertEqual(va
, a
)
1625 def test_create_from_unknown(self
):
1629 with self
._assert
_type
_error
():
1632 def test_bool_op_true(self
):
1633 self
.assertTrue(bool(self
._def
))
1635 def test_bool_op_false(self
):
1636 self
.assertFalse(bool(bt2
.ArrayValue()))
1639 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1641 def test_eq_int(self
):
1642 self
.assertNotEqual(self
._def
, 23)
1644 def test_const_eq(self
):
1645 a1
= _create_const_value([1, 2, 3])
1647 self
.assertEqual(a1
, a2
)
1649 def test_eq_diff_len(self
):
1650 a1
= bt2
.create_value([1, 2, 3])
1651 a2
= bt2
.create_value([1, 2])
1652 self
.assertIs(type(a1
), bt2
.ArrayValue
)
1653 self
.assertIs(type(a2
), bt2
.ArrayValue
)
1654 self
.assertNotEqual(a1
, a2
)
1656 def test_eq_diff_content_same_len(self
):
1657 a1
= bt2
.create_value([1, 2, 3])
1658 a2
= bt2
.create_value([4, 5, 6])
1659 self
.assertNotEqual(a1
, a2
)
1661 def test_eq_same_content_same_len(self
):
1662 raw
= (3, True, [1, 2.5, None, {"a": 17.6, "b": None}])
1663 a1
= bt2
.ArrayValue(raw
)
1664 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1665 self
.assertEqual(a1
, a2
)
1667 def test_eq_non_sequence_iterable(self
):
1668 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1669 a
= bt2
.ArrayValue((1, 3, 5))
1670 self
.assertEqual(a
, list(dct
.keys()))
1671 self
.assertNotEqual(a
, dct
)
1673 def test_setitem_int(self
):
1676 self
.assertEqual(self
._def
[2], raw
)
1678 def test_setitem_vint(self
):
1680 self
._def
[2] = bt2
.create_value(raw
)
1681 self
.assertEqual(self
._def
[2], raw
)
1683 def test_setitem_none(self
):
1685 self
.assertIsNone(self
._def
[2])
1687 def test_setitem_index_wrong_type(self
):
1688 with self
._assert
_type
_error
():
1689 self
._def
["yes"] = 23
1691 def test_setitem_index_neg(self
):
1692 with self
.assertRaises(IndexError):
1695 def test_setitem_index_out_of_range(self
):
1696 with self
.assertRaises(IndexError):
1697 self
._def
[len(self
._def
)] = 23
1699 def test_const_setitem(self
):
1700 with self
.assertRaises(TypeError):
1701 self
._def
_const
[2] = 19
1703 def test_append_none(self
):
1704 self
._def
.append(None)
1705 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1707 def test_append_int(self
):
1709 self
._def
.append(raw
)
1710 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1712 def test_const_append(self
):
1713 with self
.assertRaises(AttributeError):
1714 self
._def
_const
.append(12194)
1716 def test_append_vint(self
):
1718 self
._def
.append(bt2
.create_value(raw
))
1719 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1721 def test_append_unknown(self
):
1725 with self
._assert
_type
_error
():
1726 self
._def
.append(A())
1728 def test_iadd(self
):
1731 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1732 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1733 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1735 def test_const_iadd(self
):
1736 with self
.assertRaises(TypeError):
1737 self
._def
_const
+= 12194
1739 def test_iadd_unknown(self
):
1743 with self
._assert
_type
_error
():
1746 def test_iadd_list_unknown(self
):
1750 with self
._assert
_type
_error
():
1753 def test_iter(self
):
1754 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1755 self
.assertEqual(velem
, elem
)
1757 def test_const_iter(self
):
1758 for velem
, elem
in zip(self
._def
_const
, self
._def
_value
):
1759 self
.assertEqual(velem
, elem
)
1761 def test_const_get_item(self
):
1762 item1
= self
._def
_const
[0]
1763 item2
= self
._def
_const
[2]
1764 item3
= self
._def
_const
[5]
1765 item4
= self
._def
_const
[7]
1766 item5
= self
._def
_const
[8]
1768 self
.assertEqual(item1
, None)
1770 self
.assertIs(type(item2
), bt2
._BoolValueConst
)
1771 self
.assertEqual(item2
, True)
1773 self
.assertIs(type(item3
), bt2
._SignedIntegerValueConst
)
1774 self
.assertEqual(item3
, 42)
1776 self
.assertIs(type(item4
), bt2
._RealValueConst
)
1777 self
.assertEqual(item4
, 23.17)
1779 self
.assertIs(type(item5
), bt2
._StringValueConst
)
1780 self
.assertEqual(item5
, "yes")
1783 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1796 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1797 self
._def
_const
= _create_const_value(self
._def
_value
)
1802 def _modify_def(self
):
1803 self
._def
["zero"] = 1
1805 def test_create_default(self
):
1807 self
.assertEqual(len(m
), 0)
1809 def test_create_from_dict(self
):
1810 self
.assertEqual(self
._def
, self
._def
_value
)
1812 def test_create_from_vmap(self
):
1813 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1814 m
= bt2
.MapValue(vm
)
1815 self
.assertEqual(vm
, m
)
1817 def test_create_from_unknown(self
):
1821 with self
.assertRaises(AttributeError):
1824 def test_bool_op_true(self
):
1825 self
.assertTrue(bool(self
._def
))
1827 def test_bool_op_false(self
):
1828 self
.assertFalse(bool(bt2
.MapValue()))
1831 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1833 def test_const_eq(self
):
1834 a1
= _create_const_value({"a": 1, "b": 2, "c": 3})
1835 a2
= {"a": 1, "b": 2, "c": 3}
1836 self
.assertEqual(a1
, a2
)
1838 def test_eq_int(self
):
1839 self
.assertNotEqual(self
._def
, 23)
1841 def test_eq_diff_len(self
):
1842 a1
= bt2
.create_value({"a": 1, "b": 2, "c": 3})
1843 a2
= bt2
.create_value({"a": 1, "b": 2})
1844 self
.assertNotEqual(a1
, a2
)
1846 def test_const_eq_diff_len(self
):
1847 a1
= _create_const_value({"a": 1, "b": 2, "c": 3})
1848 a2
= _create_const_value({"a": 1, "b": 2})
1849 self
.assertNotEqual(a1
, a2
)
1851 def test_eq_diff_content_same_len(self
):
1852 a1
= bt2
.create_value({"a": 1, "b": 2, "c": 3})
1853 a2
= bt2
.create_value({"a": 4, "b": 2, "c": 3})
1854 self
.assertNotEqual(a1
, a2
)
1856 def test_const_eq_diff_content_same_len(self
):
1857 a1
= _create_const_value({"a": 1, "b": 2, "c": 3})
1858 a2
= _create_const_value({"a": 4, "b": 2, "c": 3})
1859 self
.assertNotEqual(a1
, a2
)
1861 def test_eq_same_content_diff_keys(self
):
1862 a1
= bt2
.create_value({"a": 1, "b": 2, "c": 3})
1863 a2
= bt2
.create_value({"a": 1, "k": 2, "c": 3})
1864 self
.assertNotEqual(a1
, a2
)
1866 def test_const_eq_same_content_diff_keys(self
):
1867 a1
= _create_const_value({"a": 1, "b": 2, "c": 3})
1868 a2
= _create_const_value({"a": 1, "k": 2, "c": 3})
1869 self
.assertNotEqual(a1
, a2
)
1871 def test_eq_same_content_same_len(self
):
1872 raw
= {"3": 3, "True": True, "array": [1, 2.5, None, {"a": 17.6, "b": None}]}
1873 a1
= bt2
.MapValue(raw
)
1874 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1875 self
.assertEqual(a1
, a2
)
1876 self
.assertEqual(a1
, raw
)
1878 def test_const_eq_same_content_same_len(self
):
1879 raw
= {"3": 3, "True": True, "array": [1, 2.5, None, {"a": 17.6, "b": None}]}
1880 a1
= _create_const_value(raw
)
1881 a2
= _create_const_value(copy
.deepcopy(raw
))
1882 self
.assertEqual(a1
, a2
)
1883 self
.assertEqual(a1
, raw
)
1885 def test_setitem_int(self
):
1887 self
._def
["pos-int"] = raw
1888 self
.assertEqual(self
._def
["pos-int"], raw
)
1890 def test_const_setitem_int(self
):
1891 with self
.assertRaises(TypeError):
1892 self
._def
_const
["pos-int"] = 19
1894 def test_setitem_vint(self
):
1896 self
._def
["pos-int"] = bt2
.create_value(raw
)
1897 self
.assertEqual(self
._def
["pos-int"], raw
)
1899 def test_setitem_none(self
):
1900 self
._def
["none"] = None
1901 self
.assertIsNone(self
._def
["none"])
1903 def test_setitem_new_int(self
):
1904 old_len
= len(self
._def
)
1905 self
._def
["new-int"] = 23
1906 self
.assertEqual(self
._def
["new-int"], 23)
1907 self
.assertEqual(len(self
._def
), old_len
+ 1)
1909 def test_setitem_index_wrong_type(self
):
1910 with self
.assertRaises(TypeError):
1913 def test_iter(self
):
1914 for vkey
, vval
in self
._def
.items():
1915 val
= self
._def
_value
[vkey
]
1916 self
.assertEqual(vval
, val
)
1918 def test_const_iter(self
):
1919 for vkey
, vval
in self
._def
_const
.items():
1920 val
= self
._def
_value
[vkey
]
1921 self
.assertEqual(vval
, val
)
1923 def test_get_item(self
):
1924 i
= self
._def
["pos-float"]
1925 self
.assertIs(type(i
), bt2
.RealValue
)
1926 self
.assertEqual(i
, 23.17)
1928 def test_const_get_item(self
):
1929 item1
= self
._def
_const
["none"]
1930 item2
= self
._def
_const
["true"]
1931 item3
= self
._def
_const
["pos-int"]
1932 item4
= self
._def
_const
["pos-float"]
1933 item5
= self
._def
_const
["str"]
1935 self
.assertEqual(item1
, None)
1937 self
.assertIs(type(item2
), bt2
._BoolValueConst
)
1938 self
.assertEqual(item2
, True)
1940 self
.assertIs(type(item3
), bt2
._SignedIntegerValueConst
)
1941 self
.assertEqual(item3
, 42)
1943 self
.assertIs(type(item4
), bt2
._RealValueConst
)
1944 self
.assertEqual(item4
, 23.17)
1946 self
.assertIs(type(item5
), bt2
._StringValueConst
)
1947 self
.assertEqual(item5
, "yes")
1949 def test_getitem_wrong_key(self
):
1950 with self
.assertRaises(KeyError):
1951 self
._def
["kilojoule"]
1954 if __name__
== "__main__":