1 # SPDX-License-Identifier: GPL-2.0-only
3 # Copyright (C) 2019 EfficiOS Inc.
6 from functools
import partial
, partialmethod
15 # The value object classes explicitly do not implement the copy methods,
16 # raising `NotImplementedError`, just in case we decide to implement
18 class _TestCopySimple
:
20 with self
.assertRaises(NotImplementedError):
23 def test_deepcopy(self
):
24 with self
.assertRaises(NotImplementedError):
25 copy
.deepcopy(self
._def
)
28 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
31 # Base class for numeric value test cases.
33 # To be compatible with this base class, a derived class must, in its
36 # * Set `self._def` to a value object with an arbitrary raw value.
37 # * Set `self._def_value` to the equivalent raw value of `self._def`.
38 class _TestNumericValue(_TestCopySimple
):
39 # Tries the binary operation `op`:
41 # 1. Between `self._def`, which is a value object, and `rhs`.
42 # 2. Between `self._def_value`, which is the raw value of
43 # `self._def`, and `rhs`.
45 # Returns the results of 1. and 2.
47 # If there's an exception while performing 1. or 2., asserts that
48 # both operations raised exceptions, that both exceptions have the
49 # same type, and returns `None` for both results.
50 def _binop(self
, op
, rhs
):
55 # try with value object
57 r
= op(self
._def
, rhs
)
58 except Exception as e
:
63 rv
= op(self
._def
_value
, comp_value
)
64 except Exception as e
:
67 if type_rexc
is not None or type_rvexc
is not None:
68 # at least one of the operations raised an exception: in
69 # this case both operations should have raised the same
70 # type of exception (division by zero, bit shift with a
71 # floating point number operand, etc.)
72 self
.assertIs(type_rexc
, type_rvexc
)
77 # Tries the unary operation `op`:
79 # 1. On `self._def`, which is a value object.
80 # 2. On `self._def_value`, which is the raw value of `self._def`.
82 # Returns the results of 1. and 2.
84 # If there's an exception while performing 1. or 2., asserts that
85 # both operations raised exceptions, that both exceptions have the
86 # same type, and returns `None` for both results.
87 def _unaryop(self
, op
):
91 # try with value object
94 except Exception as e
:
99 rv
= op(self
._def
_value
)
100 except Exception as e
:
103 if type_rexc
is not None or type_rvexc
is not None:
104 # at least one of the operations raised an exception: in
105 # this case both operations should have raised the same
106 # type of exception (division by zero, bit shift with a
107 # floating point number operand, etc.)
108 self
.assertIs(type_rexc
, type_rvexc
)
113 # Tests that the unary operation `op` gives results with the same
114 # type for both `self._def` and `self._def_value`.
115 def _test_unaryop_type(self
, op
):
116 r
, rv
= self
._unaryop
(op
)
121 self
.assertIsInstance(r
, type(rv
))
123 # Tests that the unary operation `op` gives results with the same
124 # value for both `self._def` and `self._def_value`. This uses the
125 # __eq__() operator of `self._def`.
126 def _test_unaryop_value(self
, op
):
127 r
, rv
= self
._unaryop
(op
)
132 self
.assertEqual(r
, rv
)
134 # Tests that the unary operation `op`, when applied to `self._def`,
135 # does not change its underlying BT object address.
136 def _test_unaryop_addr_same(self
, op
):
137 addr_before
= self
._def
.addr
139 self
.assertEqual(self
._def
.addr
, addr_before
)
141 # Tests that the unary operation `op`, when applied to `self._def`,
142 # does not change its value.
143 def _test_unaryop_value_same(self
, op
):
144 value_before
= self
._def
.__class
__(self
._def
)
146 self
.assertEqual(self
._def
, value_before
)
148 # Tests that the binary operation `op` gives results with the same
149 # type for both `self._def` and `self._def_value`.
150 def _test_binop_type(self
, op
, rhs
):
151 r
, rv
= self
._binop
(op
, rhs
)
156 if op
in _COMP_BINOPS
:
157 # __eq__() and __ne__() always return a 'bool' object
158 self
.assertIsInstance(r
, bool)
160 self
.assertIsInstance(r
, type(rv
))
162 # Tests that the binary operation `op` gives results with the same
163 # value for both `self._def` and `self._def_value`. This uses the
164 # __eq__() operator of `self._def`.
165 def _test_binop_value(self
, op
, rhs
):
166 r
, rv
= self
._binop
(op
, rhs
)
171 self
.assertEqual(r
, rv
)
173 # Tests that the binary operation `op`, when applied to `self._def`,
174 # does not change its underlying BT object address.
175 def _test_binop_lhs_addr_same(self
, op
, rhs
):
176 addr_before
= self
._def
.addr
177 r
, rv
= self
._binop
(op
, rhs
)
178 self
.assertEqual(self
._def
.addr
, addr_before
)
180 # Tests that the binary operation `op`, when applied to `self._def`,
181 # does not change its value.
182 def _test_binop_lhs_value_same(self
, op
, rhs
):
183 value_before
= self
._def
.__class
__(self
._def
)
184 r
, rv
= self
._binop
(op
, rhs
)
185 self
.assertEqual(self
._def
, value_before
)
187 # The methods below which take the `test_cb` and `op` parameters
188 # are meant to be used with one of the _test_binop_*() functions
189 # above as `test_cb` and a binary operator function as `op`.
193 # self._test_binop_rhs_pos_int(self._test_binop_value,
196 # This tests that a numeric value object added to a positive integer
197 # raw value gives a result with the expected value.
199 # `vint` and `vfloat` mean a signed integer value object and a real
202 def _test_binop_unknown(self
, op
):
203 if op
is operator
.eq
:
204 self
.assertIs(op(self
._def
, object()), False)
205 elif op
is operator
.ne
:
206 self
.assertIs(op(self
._def
, object()), True)
208 with self
.assertRaises(TypeError):
209 op(self
._def
, object())
211 def _test_binop_none(self
, op
):
212 if op
is operator
.eq
:
213 self
.assertIs(op(self
._def
, None), False)
214 elif op
is operator
.ne
:
215 self
.assertIs(op(self
._def
, None), True)
217 with self
.assertRaises(TypeError):
220 def _test_binop_rhs_false(self
, test_cb
, op
):
223 def _test_binop_rhs_true(self
, test_cb
, op
):
226 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
229 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
232 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
235 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
236 test_cb(op
, bt2
.create_value(2))
238 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
239 test_cb(op
, bt2
.create_value(-23))
241 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
242 test_cb(op
, bt2
.create_value(0))
244 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
247 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
250 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
253 def _test_binop_rhs_complex(self
, test_cb
, op
):
254 test_cb(op
, -23 + 19j
)
256 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
259 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
260 test_cb(op
, bt2
.create_value(2.2))
262 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
263 test_cb(op
, bt2
.create_value(-23.4))
265 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
266 test_cb(op
, bt2
.create_value(0.0))
268 def _test_binop_type_false(self
, op
):
269 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
271 def _test_binop_type_true(self
, op
):
272 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
274 def _test_binop_type_pos_int(self
, op
):
275 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
277 def _test_binop_type_neg_int(self
, op
):
278 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
280 def _test_binop_type_zero_int(self
, op
):
281 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
283 def _test_binop_type_pos_vint(self
, op
):
284 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
286 def _test_binop_type_neg_vint(self
, op
):
287 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
289 def _test_binop_type_zero_vint(self
, op
):
290 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
292 def _test_binop_type_pos_float(self
, op
):
293 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
295 def _test_binop_type_neg_float(self
, op
):
296 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
298 def _test_binop_type_zero_float(self
, op
):
299 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
301 def _test_binop_type_pos_vfloat(self
, op
):
302 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
304 def _test_binop_type_neg_vfloat(self
, op
):
305 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
307 def _test_binop_type_zero_vfloat(self
, op
):
308 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
310 def _test_binop_type_complex(self
, op
):
311 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
313 def _test_binop_type_zero_complex(self
, op
):
314 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
316 def _test_binop_value_false(self
, op
):
317 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
319 def _test_binop_value_true(self
, op
):
320 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
322 def _test_binop_value_pos_int(self
, op
):
323 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
325 def _test_binop_value_neg_int(self
, op
):
326 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
328 def _test_binop_value_zero_int(self
, op
):
329 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
331 def _test_binop_value_pos_vint(self
, op
):
332 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
334 def _test_binop_value_neg_vint(self
, op
):
335 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
337 def _test_binop_value_zero_vint(self
, op
):
338 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
340 def _test_binop_value_pos_float(self
, op
):
341 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
343 def _test_binop_value_neg_float(self
, op
):
344 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
346 def _test_binop_value_zero_float(self
, op
):
347 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
349 def _test_binop_value_pos_vfloat(self
, op
):
350 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
352 def _test_binop_value_neg_vfloat(self
, op
):
353 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
355 def _test_binop_value_zero_vfloat(self
, op
):
356 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
358 def _test_binop_value_complex(self
, op
):
359 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
361 def _test_binop_value_zero_complex(self
, op
):
362 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
364 def _test_binop_lhs_addr_same_false(self
, op
):
365 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
367 def _test_binop_lhs_addr_same_true(self
, op
):
368 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
370 def _test_binop_lhs_addr_same_pos_int(self
, op
):
371 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
373 def _test_binop_lhs_addr_same_neg_int(self
, op
):
374 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
376 def _test_binop_lhs_addr_same_zero_int(self
, op
):
377 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
379 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
380 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
382 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
383 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
385 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
386 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
388 def _test_binop_lhs_addr_same_pos_float(self
, op
):
389 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
391 def _test_binop_lhs_addr_same_neg_float(self
, op
):
392 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
394 def _test_binop_lhs_addr_same_zero_float(self
, op
):
395 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
397 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
398 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
400 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
401 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
403 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
404 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
406 def _test_binop_lhs_addr_same_complex(self
, op
):
407 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
409 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
410 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
412 def _test_binop_lhs_value_same_false(self
, op
):
413 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
415 def _test_binop_lhs_value_same_true(self
, op
):
416 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
418 def _test_binop_lhs_value_same_pos_int(self
, op
):
419 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
421 def _test_binop_lhs_value_same_neg_int(self
, op
):
422 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
424 def _test_binop_lhs_value_same_zero_int(self
, op
):
425 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
427 def _test_binop_lhs_value_same_pos_vint(self
, op
):
428 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
430 def _test_binop_lhs_value_same_neg_vint(self
, op
):
431 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
433 def _test_binop_lhs_value_same_zero_vint(self
, op
):
434 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
436 def _test_binop_lhs_value_same_pos_float(self
, op
):
437 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
439 def _test_binop_lhs_value_same_neg_float(self
, op
):
440 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
442 def _test_binop_lhs_value_same_zero_float(self
, op
):
443 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
445 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
446 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
448 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
449 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
451 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
452 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
454 def _test_binop_lhs_value_same_complex(self
, op
):
455 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
457 def _test_binop_lhs_value_same_zero_complex(self
, op
):
458 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
460 def test_bool_op(self
):
461 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
463 def test_int_op(self
):
464 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
466 def test_float_op(self
):
467 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
469 def test_complex_op(self
):
470 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
472 def test_str_op(self
):
473 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
475 def test_eq_none(self
):
476 # Disable the "comparison to None" warning, as this is precisely what
477 # we want to test here.
478 self
.assertFalse(self
._def
== None) # noqa: E711
480 def test_ne_none(self
):
481 # Disable the "comparison to None" warning, as this is precisely what
482 # we want to test here.
483 self
.assertTrue(self
._def
!= None) # noqa: E711
486 # This is a list of binary operators used for
487 # _inject_numeric_testing_methods().
489 # Each entry is a pair of binary operator name (used as part of the
490 # created testing method's name) and operator function.
498 ('add', operator
.add
),
499 ('radd', lambda a
, b
: operator
.add(b
, a
)),
500 ('and', operator
.and_
),
501 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
502 ('floordiv', operator
.floordiv
),
503 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
504 ('lshift', operator
.lshift
),
505 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
506 ('mod', operator
.mod
),
507 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
508 ('mul', operator
.mul
),
509 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
510 ('or', operator
.or_
),
511 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
512 ('pow', operator
.pow),
513 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
514 ('rshift', operator
.rshift
),
515 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
516 ('sub', operator
.sub
),
517 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
518 ('truediv', operator
.truediv
),
519 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
520 ('xor', operator
.xor
),
521 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
525 # This is a list of unary operators used for
526 # _inject_numeric_testing_methods().
528 # Each entry is a pair of unary operator name (used as part of the
529 # created testing method's name) and operator function.
531 ('neg', operator
.neg
),
532 ('pos', operator
.pos
),
533 ('abs', operator
.abs),
534 ('invert', operator
.invert
),
536 ('round_0', partial(round, ndigits
=0)),
537 ('round_1', partial(round, ndigits
=1)),
538 ('round_2', partial(round, ndigits
=2)),
539 ('round_3', partial(round, ndigits
=3)),
541 ('floor', math
.floor
),
542 ('trunc', math
.trunc
),
546 # This function injects a bunch of testing methods to a numeric
549 # It is meant to be used like this:
551 # _inject_numeric_testing_methods(MyNumericValueTestCase)
553 # This function injects:
555 # * One testing method for each _TestNumericValue._test_binop_*()
556 # method, for each binary operator in the _BINOPS tuple.
558 # * One testing method for each _TestNumericValue._test_unaryop*()
559 # method, for each unary operator in the _UNARYOPS tuple.
560 def _inject_numeric_testing_methods(cls
):
561 def test_binop_name(suffix
):
562 return 'test_binop_{}_{}'.format(name
, suffix
)
564 def test_unaryop_name(suffix
):
565 return 'test_unaryop_{}_{}'.format(name
, suffix
)
567 # inject testing methods for each binary operation
568 for name
, binop
in _BINOPS
:
571 test_binop_name('unknown'),
572 partialmethod(_TestNumericValue
._test
_binop
_unknown
, op
=binop
),
576 test_binop_name('none'),
577 partialmethod(_TestNumericValue
._test
_binop
_none
, op
=binop
),
581 test_binop_name('type_true'),
582 partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
),
586 test_binop_name('type_pos_int'),
587 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
),
591 test_binop_name('type_pos_vint'),
592 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
),
596 test_binop_name('value_true'),
597 partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
),
601 test_binop_name('value_pos_int'),
602 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
),
606 test_binop_name('value_pos_vint'),
607 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
),
611 test_binop_name('lhs_addr_same_true'),
612 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
616 test_binop_name('lhs_addr_same_pos_int'),
618 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
623 test_binop_name('lhs_addr_same_pos_vint'),
625 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
630 test_binop_name('lhs_value_same_true'),
631 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
635 test_binop_name('lhs_value_same_pos_int'),
637 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
642 test_binop_name('lhs_value_same_pos_vint'),
644 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
649 test_binop_name('type_neg_int'),
650 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
),
654 test_binop_name('type_neg_vint'),
655 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
),
659 test_binop_name('value_neg_int'),
660 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
),
664 test_binop_name('value_neg_vint'),
665 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
),
669 test_binop_name('lhs_addr_same_neg_int'),
671 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
676 test_binop_name('lhs_addr_same_neg_vint'),
678 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
683 test_binop_name('lhs_value_same_neg_int'),
685 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
690 test_binop_name('lhs_value_same_neg_vint'),
692 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
697 test_binop_name('type_false'),
698 partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
),
702 test_binop_name('type_zero_int'),
703 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
),
707 test_binop_name('type_zero_vint'),
708 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
),
712 test_binop_name('value_false'),
713 partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
),
717 test_binop_name('value_zero_int'),
718 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
),
722 test_binop_name('value_zero_vint'),
723 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
),
727 test_binop_name('lhs_addr_same_false'),
728 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
732 test_binop_name('lhs_addr_same_zero_int'),
734 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
739 test_binop_name('lhs_addr_same_zero_vint'),
741 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
746 test_binop_name('lhs_value_same_false'),
747 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
751 test_binop_name('lhs_value_same_zero_int'),
753 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
758 test_binop_name('lhs_value_same_zero_vint'),
760 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
765 test_binop_name('type_neg_float'),
766 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
),
770 test_binop_name('type_neg_vfloat'),
771 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
775 test_binop_name('value_neg_float'),
776 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
),
780 test_binop_name('value_neg_vfloat'),
781 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
785 test_binop_name('lhs_addr_same_neg_float'),
787 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
792 test_binop_name('lhs_addr_same_neg_vfloat'),
794 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
799 test_binop_name('lhs_value_same_neg_float'),
801 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
806 test_binop_name('lhs_value_same_neg_vfloat'),
808 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
813 test_binop_name('type_pos_float'),
814 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
),
818 test_binop_name('type_pos_vfloat'),
819 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
823 test_binop_name('value_pos_float'),
824 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
),
828 test_binop_name('value_pos_vfloat'),
829 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
833 test_binop_name('lhs_addr_same_pos_float'),
835 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
840 test_binop_name('lhs_addr_same_pos_vfloat'),
842 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
847 test_binop_name('lhs_value_same_pos_float'),
849 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
854 test_binop_name('lhs_value_same_pos_vfloat'),
856 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
861 test_binop_name('type_zero_float'),
862 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
),
866 test_binop_name('type_zero_vfloat'),
867 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
871 test_binop_name('value_zero_float'),
872 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
),
876 test_binop_name('value_zero_vfloat'),
877 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
881 test_binop_name('lhs_addr_same_zero_float'),
883 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
888 test_binop_name('lhs_addr_same_zero_vfloat'),
890 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
895 test_binop_name('lhs_value_same_zero_float'),
897 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
902 test_binop_name('lhs_value_same_zero_vfloat'),
904 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
909 test_binop_name('type_complex'),
910 partialmethod(_TestNumericValue
._test
_binop
_type
_complex
, op
=binop
),
914 test_binop_name('type_zero_complex'),
915 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_complex
, op
=binop
),
919 test_binop_name('value_complex'),
920 partialmethod(_TestNumericValue
._test
_binop
_value
_complex
, op
=binop
),
924 test_binop_name('value_zero_complex'),
925 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_complex
, op
=binop
),
929 test_binop_name('lhs_addr_same_complex'),
931 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
936 test_binop_name('lhs_addr_same_zero_complex'),
938 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
943 test_binop_name('lhs_value_same_complex'),
945 _TestNumericValue
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
950 test_binop_name('lhs_value_same_zero_complex'),
952 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
956 # inject testing methods for each unary operation
957 for name
, unaryop
in _UNARYOPS
:
960 test_unaryop_name('type'),
961 partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
),
965 test_unaryop_name('value'),
966 partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
),
970 test_unaryop_name('addr_same'),
971 partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
),
975 test_unaryop_name('value_same'),
976 partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
),
980 class CreateValueFuncTestCase(unittest
.TestCase
):
981 def test_create_none(self
):
982 v
= bt2
.create_value(None)
985 def test_create_bool_false(self
):
986 v
= bt2
.create_value(False)
987 self
.assertIsInstance(v
, bt2
.BoolValue
)
990 def test_create_bool_true(self
):
991 v
= bt2
.create_value(True)
992 self
.assertIsInstance(v
, bt2
.BoolValue
)
995 def test_create_int_pos(self
):
997 v
= bt2
.create_value(raw
)
998 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
999 self
.assertEqual(v
, raw
)
1001 def test_create_int_neg(self
):
1003 v
= bt2
.create_value(raw
)
1004 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1005 self
.assertEqual(v
, raw
)
1007 def test_create_float_pos(self
):
1009 v
= bt2
.create_value(raw
)
1010 self
.assertIsInstance(v
, bt2
.RealValue
)
1011 self
.assertEqual(v
, raw
)
1013 def test_create_float_neg(self
):
1015 v
= bt2
.create_value(raw
)
1016 self
.assertIsInstance(v
, bt2
.RealValue
)
1017 self
.assertEqual(v
, raw
)
1019 def test_create_string(self
):
1021 v
= bt2
.create_value(raw
)
1022 self
.assertIsInstance(v
, bt2
.StringValue
)
1023 self
.assertEqual(v
, raw
)
1025 def test_create_string_empty(self
):
1027 v
= bt2
.create_value(raw
)
1028 self
.assertIsInstance(v
, bt2
.StringValue
)
1029 self
.assertEqual(v
, raw
)
1031 def test_create_array_from_list(self
):
1033 v
= bt2
.create_value(raw
)
1034 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1035 self
.assertEqual(v
, raw
)
1037 def test_create_array_from_tuple(self
):
1039 v
= bt2
.create_value(raw
)
1040 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1041 self
.assertEqual(v
, raw
)
1043 def test_create_array_from_empty_list(self
):
1045 v
= bt2
.create_value(raw
)
1046 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1047 self
.assertEqual(v
, raw
)
1049 def test_create_array_from_empty_tuple(self
):
1051 v
= bt2
.create_value(raw
)
1052 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1053 self
.assertEqual(v
, raw
)
1055 def test_create_map(self
):
1057 v
= bt2
.create_value(raw
)
1058 self
.assertIsInstance(v
, bt2
.MapValue
)
1059 self
.assertEqual(v
, raw
)
1061 def test_create_map_empty(self
):
1063 v
= bt2
.create_value(raw
)
1064 self
.assertIsInstance(v
, bt2
.MapValue
)
1065 self
.assertEqual(v
, raw
)
1067 def test_create_vfalse(self
):
1068 v
= bt2
.create_value(bt2
.create_value(False))
1069 self
.assertIsInstance(v
, bt2
.BoolValue
)
1072 def test_create_invalid(self
):
1078 with self
.assertRaisesRegex(
1079 TypeError, "cannot create value object from 'A' object"
1084 def _create_const_value(value
):
1085 class MySink(bt2
._UserSinkComponent
):
1086 def _user_consume(self
):
1090 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
1092 return {'my_value': value
}
1094 res
= bt2
.QueryExecutor(MySink
, 'obj', None).query()
1095 return res
['my_value']
1098 class BoolValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1100 self
._f
= bt2
.BoolValue(False)
1101 self
._t
= bt2
.BoolValue(True)
1103 self
._def
_value
= False
1104 self
._def
_new
_value
= True
1111 def _assert_expecting_bool(self
):
1112 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
1114 def test_create_default(self
):
1118 def test_create_false(self
):
1119 self
.assertFalse(self
._f
)
1121 def test_create_true(self
):
1122 self
.assertTrue(self
._t
)
1124 def test_create_from_vfalse(self
):
1125 b
= bt2
.BoolValue(self
._f
)
1128 def test_create_from_vtrue(self
):
1129 b
= bt2
.BoolValue(self
._t
)
1132 def test_create_from_int_non_zero(self
):
1133 with self
.assertRaises(TypeError):
1136 def test_create_from_int_zero(self
):
1137 with self
.assertRaises(TypeError):
1140 def test_assign_true(self
):
1145 def test_assign_false(self
):
1150 def test_assign_vtrue(self
):
1155 def test_assign_vfalse(self
):
1160 def test_assign_int(self
):
1161 with self
.assertRaises(TypeError):
1165 def test_bool_op(self
):
1166 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1168 def test_str_op(self
):
1169 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1171 def test_eq_none(self
):
1172 # Disable the "comparison to None" warning, as this is precisely what
1173 # we want to test here.
1174 self
.assertFalse(self
._def
== None) # noqa: E711
1176 def test_ne_none(self
):
1177 # Disable the "comparison to None" warning, as this is precisely what
1178 # we want to test here.
1179 self
.assertTrue(self
._def
!= None) # noqa: E711
1181 def test_vfalse_eq_false(self
):
1182 self
.assertEqual(self
._f
, False)
1184 def test_vfalse_ne_true(self
):
1185 self
.assertNotEqual(self
._f
, True)
1187 def test_vtrue_eq_true(self
):
1188 self
.assertEqual(self
._t
, True)
1190 def test_vtrue_ne_false(self
):
1191 self
.assertNotEqual(self
._t
, False)
1194 _inject_numeric_testing_methods(BoolValueTestCase
)
1197 class _TestIntegerValue(_TestNumericValue
):
1200 self
._ip
= self
._CLS
(self
._pv
)
1201 self
._def
= self
._ip
1202 self
._def
_value
= self
._pv
1203 self
._def
_new
_value
= 101
1210 def _assert_expecting_int(self
):
1211 return self
.assertRaisesRegex(TypeError, r
'expecting an integral number object')
1213 def _assert_expecting_int64(self
):
1214 return self
.assertRaisesRegex(
1215 ValueError, r
"expecting a signed 64-bit integral value"
1218 def _assert_expecting_uint64(self
):
1219 return self
.assertRaisesRegex(
1220 ValueError, r
"expecting an unsigned 64-bit integral value"
1223 def test_create_default(self
):
1225 self
.assertEqual(i
, 0)
1227 def test_create_pos(self
):
1228 self
.assertEqual(self
._ip
, self
._pv
)
1230 def test_create_neg(self
):
1231 self
.assertEqual(self
._in
, self
._nv
)
1233 def test_create_from_vint(self
):
1234 i
= self
._CLS
(self
._ip
)
1235 self
.assertEqual(i
, self
._pv
)
1237 def test_create_from_false(self
):
1238 i
= self
._CLS
(False)
1241 def test_create_from_true(self
):
1245 def test_create_from_unknown(self
):
1249 with self
._assert
_expecting
_int
():
1252 def test_create_from_varray(self
):
1253 with self
._assert
_expecting
_int
():
1254 self
._CLS
(bt2
.ArrayValue())
1256 def test_assign_true(self
):
1258 self
._def
.value
= raw
1259 self
.assertEqual(self
._def
, raw
)
1261 def test_assign_false(self
):
1263 self
._def
.value
= raw
1264 self
.assertEqual(self
._def
, raw
)
1266 def test_assign_pos_int(self
):
1268 self
._def
.value
= raw
1269 self
.assertEqual(self
._def
, raw
)
1271 def test_assign_vint(self
):
1273 self
._def
.value
= bt2
.create_value(raw
)
1274 self
.assertEqual(self
._def
, raw
)
1277 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1278 _CLS
= bt2
.SignedIntegerValue
1283 self
._in
= self
._CLS
(self
._nv
)
1284 self
._def
_new
_value
= -101
1290 def test_create_neg(self
):
1291 self
.assertEqual(self
._in
, self
._nv
)
1293 def test_create_pos_too_big(self
):
1294 with self
._assert
_expecting
_int
64():
1297 def test_create_neg_too_big(self
):
1298 with self
._assert
_expecting
_int
64():
1299 self
._CLS
(-(2 ** 63) - 1)
1301 def test_assign_neg_int(self
):
1303 self
._def
.value
= raw
1304 self
.assertEqual(self
._def
, raw
)
1306 def test_compare_big_int(self
):
1307 # Larger than the IEEE 754 double-precision exact representation of
1310 v
= bt2
.create_value(raw
)
1311 self
.assertEqual(v
, raw
)
1314 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
1317 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1318 _CLS
= bt2
.UnsignedIntegerValue
1320 def test_create_pos_too_big(self
):
1321 with self
._assert
_expecting
_uint
64():
1324 def test_create_neg(self
):
1325 with self
._assert
_expecting
_uint
64():
1329 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
)
1332 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1336 self
._fp
= bt2
.RealValue(self
._pv
)
1337 self
._fn
= bt2
.RealValue(self
._nv
)
1338 self
._def
= self
._fp
1339 self
._def
_value
= self
._pv
1340 self
._def
_new
_value
= -101.88
1348 def _assert_expecting_float(self
):
1349 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
1351 def _test_invalid_op(self
, cb
):
1352 with self
.assertRaises(TypeError):
1355 def test_create_default(self
):
1357 self
.assertEqual(f
, 0.0)
1359 def test_create_pos(self
):
1360 self
.assertEqual(self
._fp
, self
._pv
)
1362 def test_create_neg(self
):
1363 self
.assertEqual(self
._fn
, self
._nv
)
1365 def test_create_from_false(self
):
1366 f
= bt2
.RealValue(False)
1369 def test_create_from_true(self
):
1370 f
= bt2
.RealValue(True)
1373 def test_create_from_int(self
):
1375 f
= bt2
.RealValue(raw
)
1376 self
.assertEqual(f
, float(raw
))
1378 def test_create_from_vint(self
):
1380 f
= bt2
.RealValue(bt2
.create_value(raw
))
1381 self
.assertEqual(f
, float(raw
))
1383 def test_create_from_vfloat(self
):
1385 f
= bt2
.RealValue(bt2
.create_value(raw
))
1386 self
.assertEqual(f
, raw
)
1388 def test_create_from_unknown(self
):
1392 with self
._assert
_expecting
_float
():
1395 def test_create_from_varray(self
):
1396 with self
._assert
_expecting
_float
():
1397 bt2
.RealValue(bt2
.ArrayValue())
1399 def test_assign_true(self
):
1400 self
._def
.value
= True
1401 self
.assertTrue(self
._def
)
1403 def test_assign_false(self
):
1404 self
._def
.value
= False
1405 self
.assertFalse(self
._def
)
1407 def test_assign_pos_int(self
):
1409 self
._def
.value
= raw
1410 self
.assertEqual(self
._def
, float(raw
))
1412 def test_assign_neg_int(self
):
1414 self
._def
.value
= raw
1415 self
.assertEqual(self
._def
, float(raw
))
1417 def test_assign_vint(self
):
1419 self
._def
.value
= bt2
.create_value(raw
)
1420 self
.assertEqual(self
._def
, float(raw
))
1422 def test_assign_float(self
):
1424 self
._def
.value
= raw
1425 self
.assertEqual(self
._def
, raw
)
1427 def test_assign_vfloat(self
):
1429 self
._def
.value
= bt2
.create_value(raw
)
1430 self
.assertEqual(self
._def
, raw
)
1432 def test_invalid_lshift(self
):
1433 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1435 def test_invalid_rshift(self
):
1436 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1438 def test_invalid_and(self
):
1439 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1441 def test_invalid_or(self
):
1442 self
._test
_invalid
_op
(lambda: self
._def |
23)
1444 def test_invalid_xor(self
):
1445 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1447 def test_invalid_invert(self
):
1448 self
._test
_invalid
_op
(lambda: ~self
._def
)
1451 _inject_numeric_testing_methods(RealValueTestCase
)
1454 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1456 self
._def
_value
= 'Hello, World!'
1457 self
._def
= bt2
.StringValue(self
._def
_value
)
1458 self
._def
_const
= _create_const_value(self
._def
_value
)
1459 self
._def
_new
_value
= 'Yes!'
1464 def _assert_expecting_str(self
):
1465 return self
.assertRaises(TypeError)
1467 def test_create_default(self
):
1468 s
= bt2
.StringValue()
1469 self
.assertEqual(s
, '')
1471 def test_create_from_str(self
):
1473 s
= bt2
.StringValue(raw
)
1474 self
.assertEqual(s
, raw
)
1476 def test_create_from_vstr(self
):
1478 s
= bt2
.StringValue(bt2
.create_value(raw
))
1479 self
.assertEqual(s
, raw
)
1481 def test_create_from_unknown(self
):
1485 with self
._assert
_expecting
_str
():
1486 bt2
.StringValue(A())
1488 def test_create_from_varray(self
):
1489 with self
._assert
_expecting
_str
():
1490 bt2
.StringValue(bt2
.ArrayValue())
1492 def test_assign_int(self
):
1493 with self
._assert
_expecting
_str
():
1494 self
._def
.value
= 283
1496 def test_assign_str(self
):
1499 self
.assertEqual(self
._def
, raw
)
1501 def test_assign_vstr(self
):
1503 self
._def
= bt2
.create_value(raw
)
1504 self
.assertEqual(self
._def
, raw
)
1507 self
.assertEqual(self
._def
, self
._def
_value
)
1509 def test_const_eq(self
):
1510 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1512 def test_eq_raw(self
):
1513 self
.assertNotEqual(self
._def
, 23)
1515 def test_lt_vstring(self
):
1516 s1
= bt2
.StringValue('allo')
1517 s2
= bt2
.StringValue('bateau')
1518 self
.assertLess(s1
, s2
)
1520 def test_lt_string(self
):
1521 s1
= bt2
.StringValue('allo')
1522 self
.assertLess(s1
, 'bateau')
1524 def test_le_vstring(self
):
1525 s1
= bt2
.StringValue('allo')
1526 s2
= bt2
.StringValue('bateau')
1527 self
.assertLessEqual(s1
, s2
)
1529 def test_le_string(self
):
1530 s1
= bt2
.StringValue('allo')
1531 self
.assertLessEqual(s1
, 'bateau')
1533 def test_gt_vstring(self
):
1534 s1
= bt2
.StringValue('allo')
1535 s2
= bt2
.StringValue('bateau')
1536 self
.assertGreater(s2
, s1
)
1538 def test_gt_string(self
):
1539 s1
= bt2
.StringValue('allo')
1540 self
.assertGreater('bateau', s1
)
1542 def test_ge_vstring(self
):
1543 s1
= bt2
.StringValue('allo')
1544 s2
= bt2
.StringValue('bateau')
1545 self
.assertGreaterEqual(s2
, s1
)
1547 def test_ge_string(self
):
1548 s1
= bt2
.StringValue('allo')
1549 self
.assertGreaterEqual('bateau', s1
)
1551 def test_in_string(self
):
1552 s1
= bt2
.StringValue('beau grand bateau')
1553 self
.assertIn('bateau', s1
)
1555 def test_in_vstring(self
):
1556 s1
= bt2
.StringValue('beau grand bateau')
1557 s2
= bt2
.StringValue('bateau')
1558 self
.assertIn(s2
, s1
)
1560 def test_bool_op(self
):
1561 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1563 def test_str_op(self
):
1564 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1567 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1569 def test_getitem(self
):
1570 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1572 def test_const_getitem(self
):
1573 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1575 def test_iadd_str(self
):
1576 to_append
= 'meow meow meow'
1577 self
._def
+= to_append
1578 self
._def
_value
+= to_append
1579 self
.assertEqual(self
._def
, self
._def
_value
)
1581 def test_const_iadd_str(self
):
1582 to_append
= 'meow meow meow'
1583 with self
.assertRaises(TypeError):
1584 self
._def
_const
+= to_append
1586 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1588 def test_append_vstr(self
):
1589 to_append
= 'meow meow meow'
1590 self
._def
+= bt2
.create_value(to_append
)
1591 self
._def
_value
+= to_append
1592 self
.assertEqual(self
._def
, self
._def
_value
)
1595 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1597 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1598 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1599 self
._def
_const
= _create_const_value(copy
.deepcopy(self
._def
_value
))
1604 def _modify_def(self
):
1605 self
._def
[2] = 'xyz'
1607 def _assert_type_error(self
):
1608 return self
.assertRaises(TypeError)
1610 def test_create_default(self
):
1611 a
= bt2
.ArrayValue()
1612 self
.assertEqual(len(a
), 0)
1614 def test_create_from_array(self
):
1615 self
.assertEqual(self
._def
, self
._def
_value
)
1617 def test_create_from_tuple(self
):
1618 t
= 1, 2, False, None
1619 a
= bt2
.ArrayValue(t
)
1620 self
.assertEqual(a
, t
)
1622 def test_create_from_varray(self
):
1623 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1624 a
= bt2
.ArrayValue(va
)
1625 self
.assertEqual(va
, a
)
1627 def test_create_from_unknown(self
):
1631 with self
._assert
_type
_error
():
1634 def test_bool_op_true(self
):
1635 self
.assertTrue(bool(self
._def
))
1637 def test_bool_op_false(self
):
1638 self
.assertFalse(bool(bt2
.ArrayValue()))
1641 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1643 def test_eq_int(self
):
1644 self
.assertNotEqual(self
._def
, 23)
1646 def test_const_eq(self
):
1647 a1
= _create_const_value([1, 2, 3])
1649 self
.assertEqual(a1
, a2
)
1651 def test_eq_diff_len(self
):
1652 a1
= bt2
.create_value([1, 2, 3])
1653 a2
= bt2
.create_value([1, 2])
1654 self
.assertIs(type(a1
), bt2
.ArrayValue
)
1655 self
.assertIs(type(a2
), bt2
.ArrayValue
)
1656 self
.assertNotEqual(a1
, a2
)
1658 def test_eq_diff_content_same_len(self
):
1659 a1
= bt2
.create_value([1, 2, 3])
1660 a2
= bt2
.create_value([4, 5, 6])
1661 self
.assertNotEqual(a1
, a2
)
1663 def test_eq_same_content_same_len(self
):
1664 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1665 a1
= bt2
.ArrayValue(raw
)
1666 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1667 self
.assertEqual(a1
, a2
)
1669 def test_eq_non_sequence_iterable(self
):
1670 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1671 a
= bt2
.ArrayValue((1, 3, 5))
1672 self
.assertEqual(a
, list(dct
.keys()))
1673 self
.assertNotEqual(a
, dct
)
1675 def test_setitem_int(self
):
1678 self
.assertEqual(self
._def
[2], raw
)
1680 def test_setitem_vint(self
):
1682 self
._def
[2] = bt2
.create_value(raw
)
1683 self
.assertEqual(self
._def
[2], raw
)
1685 def test_setitem_none(self
):
1687 self
.assertIsNone(self
._def
[2])
1689 def test_setitem_index_wrong_type(self
):
1690 with self
._assert
_type
_error
():
1691 self
._def
['yes'] = 23
1693 def test_setitem_index_neg(self
):
1694 with self
.assertRaises(IndexError):
1697 def test_setitem_index_out_of_range(self
):
1698 with self
.assertRaises(IndexError):
1699 self
._def
[len(self
._def
)] = 23
1701 def test_const_setitem(self
):
1702 with self
.assertRaises(TypeError):
1703 self
._def
_const
[2] = 19
1705 def test_append_none(self
):
1706 self
._def
.append(None)
1707 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1709 def test_append_int(self
):
1711 self
._def
.append(raw
)
1712 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1714 def test_const_append(self
):
1715 with self
.assertRaises(AttributeError):
1716 self
._def
_const
.append(12194)
1718 def test_append_vint(self
):
1720 self
._def
.append(bt2
.create_value(raw
))
1721 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1723 def test_append_unknown(self
):
1727 with self
._assert
_type
_error
():
1728 self
._def
.append(A())
1730 def test_iadd(self
):
1733 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1734 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1735 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1737 def test_const_iadd(self
):
1738 with self
.assertRaises(TypeError):
1739 self
._def
_const
+= 12194
1741 def test_iadd_unknown(self
):
1745 with self
._assert
_type
_error
():
1748 def test_iadd_list_unknown(self
):
1752 with self
._assert
_type
_error
():
1755 def test_iter(self
):
1756 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1757 self
.assertEqual(velem
, elem
)
1759 def test_const_iter(self
):
1760 for velem
, elem
in zip(self
._def
_const
, self
._def
_value
):
1761 self
.assertEqual(velem
, elem
)
1763 def test_const_get_item(self
):
1764 item1
= self
._def
_const
[0]
1765 item2
= self
._def
_const
[2]
1766 item3
= self
._def
_const
[5]
1767 item4
= self
._def
_const
[7]
1768 item5
= self
._def
_const
[8]
1770 self
.assertEqual(item1
, None)
1772 self
.assertIs(type(item2
), bt2
._BoolValueConst
)
1773 self
.assertEqual(item2
, True)
1775 self
.assertIs(type(item3
), bt2
._SignedIntegerValueConst
)
1776 self
.assertEqual(item3
, 42)
1778 self
.assertIs(type(item4
), bt2
._RealValueConst
)
1779 self
.assertEqual(item4
, 23.17)
1781 self
.assertIs(type(item5
), bt2
._StringValueConst
)
1782 self
.assertEqual(item5
, 'yes')
1785 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1798 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1799 self
._def
_const
= _create_const_value(self
._def
_value
)
1804 def _modify_def(self
):
1805 self
._def
['zero'] = 1
1807 def test_create_default(self
):
1809 self
.assertEqual(len(m
), 0)
1811 def test_create_from_dict(self
):
1812 self
.assertEqual(self
._def
, self
._def
_value
)
1814 def test_create_from_vmap(self
):
1815 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1816 m
= bt2
.MapValue(vm
)
1817 self
.assertEqual(vm
, m
)
1819 def test_create_from_unknown(self
):
1823 with self
.assertRaises(AttributeError):
1826 def test_bool_op_true(self
):
1827 self
.assertTrue(bool(self
._def
))
1829 def test_bool_op_false(self
):
1830 self
.assertFalse(bool(bt2
.MapValue()))
1833 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1835 def test_const_eq(self
):
1836 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1837 a2
= {'a': 1, 'b': 2, 'c': 3}
1838 self
.assertEqual(a1
, a2
)
1840 def test_eq_int(self
):
1841 self
.assertNotEqual(self
._def
, 23)
1843 def test_eq_diff_len(self
):
1844 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1845 a2
= bt2
.create_value({'a': 1, 'b': 2})
1846 self
.assertNotEqual(a1
, a2
)
1848 def test_const_eq_diff_len(self
):
1849 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1850 a2
= _create_const_value({'a': 1, 'b': 2})
1851 self
.assertNotEqual(a1
, a2
)
1853 def test_eq_diff_content_same_len(self
):
1854 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1855 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1856 self
.assertNotEqual(a1
, a2
)
1858 def test_const_eq_diff_content_same_len(self
):
1859 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1860 a2
= _create_const_value({'a': 4, 'b': 2, 'c': 3})
1861 self
.assertNotEqual(a1
, a2
)
1863 def test_eq_same_content_diff_keys(self
):
1864 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1865 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1866 self
.assertNotEqual(a1
, a2
)
1868 def test_const_eq_same_content_diff_keys(self
):
1869 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1870 a2
= _create_const_value({'a': 1, 'k': 2, 'c': 3})
1871 self
.assertNotEqual(a1
, a2
)
1873 def test_eq_same_content_same_len(self
):
1874 raw
= {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1875 a1
= bt2
.MapValue(raw
)
1876 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1877 self
.assertEqual(a1
, a2
)
1878 self
.assertEqual(a1
, raw
)
1880 def test_const_eq_same_content_same_len(self
):
1881 raw
= {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1882 a1
= _create_const_value(raw
)
1883 a2
= _create_const_value(copy
.deepcopy(raw
))
1884 self
.assertEqual(a1
, a2
)
1885 self
.assertEqual(a1
, raw
)
1887 def test_setitem_int(self
):
1889 self
._def
['pos-int'] = raw
1890 self
.assertEqual(self
._def
['pos-int'], raw
)
1892 def test_const_setitem_int(self
):
1893 with self
.assertRaises(TypeError):
1894 self
._def
_const
['pos-int'] = 19
1896 def test_setitem_vint(self
):
1898 self
._def
['pos-int'] = bt2
.create_value(raw
)
1899 self
.assertEqual(self
._def
['pos-int'], raw
)
1901 def test_setitem_none(self
):
1902 self
._def
['none'] = None
1903 self
.assertIsNone(self
._def
['none'])
1905 def test_setitem_new_int(self
):
1906 old_len
= len(self
._def
)
1907 self
._def
['new-int'] = 23
1908 self
.assertEqual(self
._def
['new-int'], 23)
1909 self
.assertEqual(len(self
._def
), old_len
+ 1)
1911 def test_setitem_index_wrong_type(self
):
1912 with self
.assertRaises(TypeError):
1915 def test_iter(self
):
1916 for vkey
, vval
in self
._def
.items():
1917 val
= self
._def
_value
[vkey
]
1918 self
.assertEqual(vval
, val
)
1920 def test_const_iter(self
):
1921 for vkey
, vval
in self
._def
_const
.items():
1922 val
= self
._def
_value
[vkey
]
1923 self
.assertEqual(vval
, val
)
1925 def test_get_item(self
):
1926 i
= self
._def
['pos-float']
1927 self
.assertIs(type(i
), bt2
.RealValue
)
1928 self
.assertEqual(i
, 23.17)
1930 def test_const_get_item(self
):
1931 item1
= self
._def
_const
['none']
1932 item2
= self
._def
_const
['true']
1933 item3
= self
._def
_const
['pos-int']
1934 item4
= self
._def
_const
['pos-float']
1935 item5
= self
._def
_const
['str']
1937 self
.assertEqual(item1
, None)
1939 self
.assertIs(type(item2
), bt2
._BoolValueConst
)
1940 self
.assertEqual(item2
, True)
1942 self
.assertIs(type(item3
), bt2
._SignedIntegerValueConst
)
1943 self
.assertEqual(item3
, 42)
1945 self
.assertIs(type(item4
), bt2
._RealValueConst
)
1946 self
.assertEqual(item4
, 23.17)
1948 self
.assertIs(type(item5
), bt2
._StringValueConst
)
1949 self
.assertEqual(item5
, 'yes')
1951 def test_getitem_wrong_key(self
):
1952 with self
.assertRaises(KeyError):
1953 self
._def
['kilojoule']
1956 if __name__
== '__main__':