2 # Copyright (C) 2019 EfficiOS Inc.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; only version 2
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 from functools
import partial
, partialmethod
28 # The value object classes explicitly do not implement the copy methods,
29 # raising `NotImplementedError`, just in case we decide to implement
31 class _TestCopySimple
:
33 with self
.assertRaises(NotImplementedError):
36 def test_deepcopy(self
):
37 with self
.assertRaises(NotImplementedError):
38 copy
.deepcopy(self
._def
)
41 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
44 # Base class for numeric value test cases.
46 # To be compatible with this base class, a derived class must, in its
49 # * Set `self._def` to a value object with an arbitrary raw value.
50 # * Set `self._def_value` to the equivalent raw value of `self._def`.
51 class _TestNumericValue(_TestCopySimple
):
52 # Tries the binary operation `op`:
54 # 1. Between `self._def`, which is a value object, and `rhs`.
55 # 2. Between `self._def_value`, which is the raw value of
56 # `self._def`, and `rhs`.
58 # Returns the results of 1. and 2.
60 # If there's an exception while performing 1. or 2., asserts that
61 # both operations raised exceptions, that both exceptions have the
62 # same type, and returns `None` for both results.
63 def _binop(self
, op
, rhs
):
68 # try with value object
70 r
= op(self
._def
, rhs
)
71 except Exception as e
:
76 rv
= op(self
._def
_value
, comp_value
)
77 except Exception as e
:
80 if type_rexc
is not None or type_rvexc
is not None:
81 # at least one of the operations raised an exception: in
82 # this case both operations should have raised the same
83 # type of exception (division by zero, bit shift with a
84 # floating point number operand, etc.)
85 self
.assertIs(type_rexc
, type_rvexc
)
90 # Tries the unary operation `op`:
92 # 1. On `self._def`, which is a value object.
93 # 2. On `self._def_value`, which is the raw value of `self._def`.
95 # Returns the results of 1. and 2.
97 # If there's an exception while performing 1. or 2., asserts that
98 # both operations raised exceptions, that both exceptions have the
99 # same type, and returns `None` for both results.
100 def _unaryop(self
, op
):
104 # try with value object
107 except Exception as e
:
112 rv
= op(self
._def
_value
)
113 except Exception as e
:
116 if type_rexc
is not None or type_rvexc
is not None:
117 # at least one of the operations raised an exception: in
118 # this case both operations should have raised the same
119 # type of exception (division by zero, bit shift with a
120 # floating point number operand, etc.)
121 self
.assertIs(type_rexc
, type_rvexc
)
126 # Tests that the unary operation `op` gives results with the same
127 # type for both `self._def` and `self._def_value`.
128 def _test_unaryop_type(self
, op
):
129 r
, rv
= self
._unaryop
(op
)
134 self
.assertIsInstance(r
, type(rv
))
136 # Tests that the unary operation `op` gives results with the same
137 # value for both `self._def` and `self._def_value`. This uses the
138 # __eq__() operator of `self._def`.
139 def _test_unaryop_value(self
, op
):
140 r
, rv
= self
._unaryop
(op
)
145 self
.assertEqual(r
, rv
)
147 # Tests that the unary operation `op`, when applied to `self._def`,
148 # does not change its underlying BT object address.
149 def _test_unaryop_addr_same(self
, op
):
150 addr_before
= self
._def
.addr
152 self
.assertEqual(self
._def
.addr
, addr_before
)
154 # Tests that the unary operation `op`, when applied to `self._def`,
155 # does not change its value.
156 def _test_unaryop_value_same(self
, op
):
157 value_before
= self
._def
.__class
__(self
._def
)
159 self
.assertEqual(self
._def
, value_before
)
161 # Tests that the binary operation `op` gives results with the same
162 # type for both `self._def` and `self._def_value`.
163 def _test_binop_type(self
, op
, rhs
):
164 r
, rv
= self
._binop
(op
, rhs
)
169 if op
in _COMP_BINOPS
:
170 # __eq__() and __ne__() always return a 'bool' object
171 self
.assertIsInstance(r
, bool)
173 self
.assertIsInstance(r
, type(rv
))
175 # Tests that the binary operation `op` gives results with the same
176 # value for both `self._def` and `self._def_value`. This uses the
177 # __eq__() operator of `self._def`.
178 def _test_binop_value(self
, op
, rhs
):
179 r
, rv
= self
._binop
(op
, rhs
)
184 self
.assertEqual(r
, rv
)
186 # Tests that the binary operation `op`, when applied to `self._def`,
187 # does not change its underlying BT object address.
188 def _test_binop_lhs_addr_same(self
, op
, rhs
):
189 addr_before
= self
._def
.addr
190 r
, rv
= self
._binop
(op
, rhs
)
191 self
.assertEqual(self
._def
.addr
, addr_before
)
193 # Tests that the binary operation `op`, when applied to `self._def`,
194 # does not change its value.
195 def _test_binop_lhs_value_same(self
, op
, rhs
):
196 value_before
= self
._def
.__class
__(self
._def
)
197 r
, rv
= self
._binop
(op
, rhs
)
198 self
.assertEqual(self
._def
, value_before
)
200 # The methods below which take the `test_cb` and `op` parameters
201 # are meant to be used with one of the _test_binop_*() functions
202 # above as `test_cb` and a binary operator function as `op`.
206 # self._test_binop_rhs_pos_int(self._test_binop_value,
209 # This tests that a numeric value object added to a positive integer
210 # raw value gives a result with the expected value.
212 # `vint` and `vfloat` mean a signed integer value object and a real
215 def _test_binop_invalid_unknown(self
, op
):
216 if op
in _COMP_BINOPS
:
217 self
.skipTest('not testing')
219 with self
.assertRaises(TypeError):
220 op(self
._def
, object())
222 def _test_binop_invalid_none(self
, op
):
223 if op
in _COMP_BINOPS
:
224 self
.skipTest('not testing')
226 with self
.assertRaises(TypeError):
229 def _test_binop_rhs_false(self
, test_cb
, op
):
232 def _test_binop_rhs_true(self
, test_cb
, op
):
235 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
238 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
241 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
244 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
245 test_cb(op
, bt2
.create_value(2))
247 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
248 test_cb(op
, bt2
.create_value(-23))
250 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
251 test_cb(op
, bt2
.create_value(0))
253 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
256 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
259 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
262 def _test_binop_rhs_complex(self
, test_cb
, op
):
263 test_cb(op
, -23 + 19j
)
265 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
268 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
269 test_cb(op
, bt2
.create_value(2.2))
271 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
272 test_cb(op
, bt2
.create_value(-23.4))
274 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
275 test_cb(op
, bt2
.create_value(0.0))
277 def _test_binop_type_false(self
, op
):
278 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
280 def _test_binop_type_true(self
, op
):
281 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
283 def _test_binop_type_pos_int(self
, op
):
284 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
286 def _test_binop_type_neg_int(self
, op
):
287 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
289 def _test_binop_type_zero_int(self
, op
):
290 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
292 def _test_binop_type_pos_vint(self
, op
):
293 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
295 def _test_binop_type_neg_vint(self
, op
):
296 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
298 def _test_binop_type_zero_vint(self
, op
):
299 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
301 def _test_binop_type_pos_float(self
, op
):
302 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
304 def _test_binop_type_neg_float(self
, op
):
305 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
307 def _test_binop_type_zero_float(self
, op
):
308 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
310 def _test_binop_type_pos_vfloat(self
, op
):
311 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
313 def _test_binop_type_neg_vfloat(self
, op
):
314 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
316 def _test_binop_type_zero_vfloat(self
, op
):
317 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
319 def _test_binop_type_complex(self
, op
):
320 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
322 def _test_binop_type_zero_complex(self
, op
):
323 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
325 def _test_binop_value_false(self
, op
):
326 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
328 def _test_binop_value_true(self
, op
):
329 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
331 def _test_binop_value_pos_int(self
, op
):
332 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
334 def _test_binop_value_neg_int(self
, op
):
335 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
337 def _test_binop_value_zero_int(self
, op
):
338 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
340 def _test_binop_value_pos_vint(self
, op
):
341 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
343 def _test_binop_value_neg_vint(self
, op
):
344 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
346 def _test_binop_value_zero_vint(self
, op
):
347 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
349 def _test_binop_value_pos_float(self
, op
):
350 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
352 def _test_binop_value_neg_float(self
, op
):
353 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
355 def _test_binop_value_zero_float(self
, op
):
356 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
358 def _test_binop_value_pos_vfloat(self
, op
):
359 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
361 def _test_binop_value_neg_vfloat(self
, op
):
362 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
364 def _test_binop_value_zero_vfloat(self
, op
):
365 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
367 def _test_binop_value_complex(self
, op
):
368 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
370 def _test_binop_value_zero_complex(self
, op
):
371 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
373 def _test_binop_lhs_addr_same_false(self
, op
):
374 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
376 def _test_binop_lhs_addr_same_true(self
, op
):
377 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
379 def _test_binop_lhs_addr_same_pos_int(self
, op
):
380 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
382 def _test_binop_lhs_addr_same_neg_int(self
, op
):
383 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
385 def _test_binop_lhs_addr_same_zero_int(self
, op
):
386 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
388 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
389 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
391 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
392 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
394 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
395 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
397 def _test_binop_lhs_addr_same_pos_float(self
, op
):
398 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
400 def _test_binop_lhs_addr_same_neg_float(self
, op
):
401 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
403 def _test_binop_lhs_addr_same_zero_float(self
, op
):
404 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
406 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
407 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
409 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
410 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
412 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
413 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
415 def _test_binop_lhs_addr_same_complex(self
, op
):
416 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
418 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
419 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
421 def _test_binop_lhs_value_same_false(self
, op
):
422 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
424 def _test_binop_lhs_value_same_true(self
, op
):
425 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
427 def _test_binop_lhs_value_same_pos_int(self
, op
):
428 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
430 def _test_binop_lhs_value_same_neg_int(self
, op
):
431 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
433 def _test_binop_lhs_value_same_zero_int(self
, op
):
434 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
436 def _test_binop_lhs_value_same_pos_vint(self
, op
):
437 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
439 def _test_binop_lhs_value_same_neg_vint(self
, op
):
440 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
442 def _test_binop_lhs_value_same_zero_vint(self
, op
):
443 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
445 def _test_binop_lhs_value_same_pos_float(self
, op
):
446 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
448 def _test_binop_lhs_value_same_neg_float(self
, op
):
449 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
451 def _test_binop_lhs_value_same_zero_float(self
, op
):
452 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
454 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
455 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
457 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
458 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
460 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
461 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
463 def _test_binop_lhs_value_same_complex(self
, op
):
464 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
466 def _test_binop_lhs_value_same_zero_complex(self
, op
):
467 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
469 def test_bool_op(self
):
470 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
472 def test_int_op(self
):
473 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
475 def test_float_op(self
):
476 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
478 def test_complex_op(self
):
479 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
481 def test_str_op(self
):
482 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
484 def test_eq_none(self
):
485 # Disable the "comparison to None" warning, as this is precisely what
486 # we want to test here.
487 self
.assertFalse(self
._def
== None) # noqa: E711
489 def test_ne_none(self
):
490 # Disable the "comparison to None" warning, as this is precisely what
491 # we want to test here.
492 self
.assertTrue(self
._def
!= None) # noqa: E711
495 # This is a list of binary operators used for
496 # _inject_numeric_testing_methods().
498 # Each entry is a pair of binary operator name (used as part of the
499 # created testing method's name) and operator function.
507 ('add', operator
.add
),
508 ('radd', lambda a
, b
: operator
.add(b
, a
)),
509 ('and', operator
.and_
),
510 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
511 ('floordiv', operator
.floordiv
),
512 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
513 ('lshift', operator
.lshift
),
514 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
515 ('mod', operator
.mod
),
516 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
517 ('mul', operator
.mul
),
518 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
519 ('or', operator
.or_
),
520 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
521 ('pow', operator
.pow),
522 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
523 ('rshift', operator
.rshift
),
524 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
525 ('sub', operator
.sub
),
526 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
527 ('truediv', operator
.truediv
),
528 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
529 ('xor', operator
.xor
),
530 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
534 # This is a list of unary operators used for
535 # _inject_numeric_testing_methods().
537 # Each entry is a pair of unary operator name (used as part of the
538 # created testing method's name) and operator function.
540 ('neg', operator
.neg
),
541 ('pos', operator
.pos
),
542 ('abs', operator
.abs),
543 ('invert', operator
.invert
),
545 ('round_0', partial(round, ndigits
=0)),
546 ('round_1', partial(round, ndigits
=1)),
547 ('round_2', partial(round, ndigits
=2)),
548 ('round_3', partial(round, ndigits
=3)),
550 ('floor', math
.floor
),
551 ('trunc', math
.trunc
),
555 # This function injects a bunch of testing methods to a numeric
558 # It is meant to be used like this:
560 # _inject_numeric_testing_methods(MyNumericValueTestCase)
562 # This function injects:
564 # * One testing method for each _TestNumericValue._test_binop_*()
565 # method, for each binary operator in the _BINOPS tuple.
567 # * One testing method for each _TestNumericValue._test_unaryop*()
568 # method, for each unary operator in the _UNARYOPS tuple.
569 def _inject_numeric_testing_methods(cls
):
570 def test_binop_name(suffix
):
571 return 'test_binop_{}_{}'.format(name
, suffix
)
573 def test_unaryop_name(suffix
):
574 return 'test_unaryop_{}_{}'.format(name
, suffix
)
576 # inject testing methods for each binary operation
577 for name
, binop
in _BINOPS
:
580 test_binop_name('invalid_unknown'),
581 partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
),
585 test_binop_name('invalid_none'),
586 partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
),
590 test_binop_name('type_true'),
591 partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
),
595 test_binop_name('type_pos_int'),
596 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
),
600 test_binop_name('type_pos_vint'),
601 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
),
605 test_binop_name('value_true'),
606 partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
),
610 test_binop_name('value_pos_int'),
611 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
),
615 test_binop_name('value_pos_vint'),
616 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
),
620 test_binop_name('lhs_addr_same_true'),
621 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
625 test_binop_name('lhs_addr_same_pos_int'),
627 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
632 test_binop_name('lhs_addr_same_pos_vint'),
634 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
639 test_binop_name('lhs_value_same_true'),
640 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
644 test_binop_name('lhs_value_same_pos_int'),
646 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
651 test_binop_name('lhs_value_same_pos_vint'),
653 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
658 test_binop_name('type_neg_int'),
659 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
),
663 test_binop_name('type_neg_vint'),
664 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
),
668 test_binop_name('value_neg_int'),
669 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
),
673 test_binop_name('value_neg_vint'),
674 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
),
678 test_binop_name('lhs_addr_same_neg_int'),
680 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
685 test_binop_name('lhs_addr_same_neg_vint'),
687 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
692 test_binop_name('lhs_value_same_neg_int'),
694 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
699 test_binop_name('lhs_value_same_neg_vint'),
701 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
706 test_binop_name('type_false'),
707 partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
),
711 test_binop_name('type_zero_int'),
712 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
),
716 test_binop_name('type_zero_vint'),
717 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
),
721 test_binop_name('value_false'),
722 partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
),
726 test_binop_name('value_zero_int'),
727 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
),
731 test_binop_name('value_zero_vint'),
732 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
),
736 test_binop_name('lhs_addr_same_false'),
737 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
741 test_binop_name('lhs_addr_same_zero_int'),
743 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
748 test_binop_name('lhs_addr_same_zero_vint'),
750 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
755 test_binop_name('lhs_value_same_false'),
756 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
760 test_binop_name('lhs_value_same_zero_int'),
762 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
767 test_binop_name('lhs_value_same_zero_vint'),
769 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
774 test_binop_name('type_neg_float'),
775 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
),
779 test_binop_name('type_neg_vfloat'),
780 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
784 test_binop_name('value_neg_float'),
785 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
),
789 test_binop_name('value_neg_vfloat'),
790 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
794 test_binop_name('lhs_addr_same_neg_float'),
796 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
801 test_binop_name('lhs_addr_same_neg_vfloat'),
803 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
808 test_binop_name('lhs_value_same_neg_float'),
810 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
815 test_binop_name('lhs_value_same_neg_vfloat'),
817 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
822 test_binop_name('type_pos_float'),
823 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
),
827 test_binop_name('type_pos_vfloat'),
828 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
832 test_binop_name('value_pos_float'),
833 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
),
837 test_binop_name('value_pos_vfloat'),
838 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
842 test_binop_name('lhs_addr_same_pos_float'),
844 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
849 test_binop_name('lhs_addr_same_pos_vfloat'),
851 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
856 test_binop_name('lhs_value_same_pos_float'),
858 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
863 test_binop_name('lhs_value_same_pos_vfloat'),
865 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
870 test_binop_name('type_zero_float'),
871 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
),
875 test_binop_name('type_zero_vfloat'),
876 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
880 test_binop_name('value_zero_float'),
881 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
),
885 test_binop_name('value_zero_vfloat'),
886 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
890 test_binop_name('lhs_addr_same_zero_float'),
892 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
897 test_binop_name('lhs_addr_same_zero_vfloat'),
899 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
904 test_binop_name('lhs_value_same_zero_float'),
906 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
911 test_binop_name('lhs_value_same_zero_vfloat'),
913 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
918 test_binop_name('type_complex'),
919 partialmethod(_TestNumericValue
._test
_binop
_type
_complex
, op
=binop
),
923 test_binop_name('type_zero_complex'),
924 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_complex
, op
=binop
),
928 test_binop_name('value_complex'),
929 partialmethod(_TestNumericValue
._test
_binop
_value
_complex
, op
=binop
),
933 test_binop_name('value_zero_complex'),
934 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_complex
, op
=binop
),
938 test_binop_name('lhs_addr_same_complex'),
940 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
945 test_binop_name('lhs_addr_same_zero_complex'),
947 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
952 test_binop_name('lhs_value_same_complex'),
954 _TestNumericValue
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
959 test_binop_name('lhs_value_same_zero_complex'),
961 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
965 # inject testing methods for each unary operation
966 for name
, unaryop
in _UNARYOPS
:
969 test_unaryop_name('type'),
970 partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
),
974 test_unaryop_name('value'),
975 partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
),
979 test_unaryop_name('addr_same'),
980 partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
),
984 test_unaryop_name('value_same'),
985 partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
),
989 class CreateValueFuncTestCase(unittest
.TestCase
):
990 def test_create_none(self
):
991 v
= bt2
.create_value(None)
994 def test_create_bool_false(self
):
995 v
= bt2
.create_value(False)
996 self
.assertIsInstance(v
, bt2
.BoolValue
)
999 def test_create_bool_true(self
):
1000 v
= bt2
.create_value(True)
1001 self
.assertIsInstance(v
, bt2
.BoolValue
)
1004 def test_create_int_pos(self
):
1006 v
= bt2
.create_value(raw
)
1007 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1008 self
.assertEqual(v
, raw
)
1010 def test_create_int_neg(self
):
1012 v
= bt2
.create_value(raw
)
1013 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1014 self
.assertEqual(v
, raw
)
1016 def test_create_float_pos(self
):
1018 v
= bt2
.create_value(raw
)
1019 self
.assertIsInstance(v
, bt2
.RealValue
)
1020 self
.assertEqual(v
, raw
)
1022 def test_create_float_neg(self
):
1024 v
= bt2
.create_value(raw
)
1025 self
.assertIsInstance(v
, bt2
.RealValue
)
1026 self
.assertEqual(v
, raw
)
1028 def test_create_string(self
):
1030 v
= bt2
.create_value(raw
)
1031 self
.assertIsInstance(v
, bt2
.StringValue
)
1032 self
.assertEqual(v
, raw
)
1034 def test_create_string_empty(self
):
1036 v
= bt2
.create_value(raw
)
1037 self
.assertIsInstance(v
, bt2
.StringValue
)
1038 self
.assertEqual(v
, raw
)
1040 def test_create_array_from_list(self
):
1042 v
= bt2
.create_value(raw
)
1043 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1044 self
.assertEqual(v
, raw
)
1046 def test_create_array_from_tuple(self
):
1048 v
= bt2
.create_value(raw
)
1049 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1050 self
.assertEqual(v
, raw
)
1052 def test_create_array_from_empty_list(self
):
1054 v
= bt2
.create_value(raw
)
1055 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1056 self
.assertEqual(v
, raw
)
1058 def test_create_array_from_empty_tuple(self
):
1060 v
= bt2
.create_value(raw
)
1061 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1062 self
.assertEqual(v
, raw
)
1064 def test_create_map(self
):
1066 v
= bt2
.create_value(raw
)
1067 self
.assertIsInstance(v
, bt2
.MapValue
)
1068 self
.assertEqual(v
, raw
)
1070 def test_create_map_empty(self
):
1072 v
= bt2
.create_value(raw
)
1073 self
.assertIsInstance(v
, bt2
.MapValue
)
1074 self
.assertEqual(v
, raw
)
1076 def test_create_vfalse(self
):
1077 v
= bt2
.create_value(bt2
.create_value(False))
1078 self
.assertIsInstance(v
, bt2
.BoolValue
)
1081 def test_create_invalid(self
):
1087 with self
.assertRaisesRegex(
1088 TypeError, "cannot create value object from 'A' object"
1093 def _create_const_value(value
):
1094 class MySink(bt2
._UserSinkComponent
):
1095 def _user_consume(self
):
1099 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
1101 return {'my_value': value
}
1103 res
= bt2
.QueryExecutor(MySink
, 'obj', None).query()
1104 return res
['my_value']
1107 class BoolValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1109 self
._f
= bt2
.BoolValue(False)
1110 self
._t
= bt2
.BoolValue(True)
1112 self
._def
_value
= False
1113 self
._def
_new
_value
= True
1120 def _assert_expecting_bool(self
):
1121 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
1123 def test_create_default(self
):
1127 def test_create_false(self
):
1128 self
.assertFalse(self
._f
)
1130 def test_create_true(self
):
1131 self
.assertTrue(self
._t
)
1133 def test_create_from_vfalse(self
):
1134 b
= bt2
.BoolValue(self
._f
)
1137 def test_create_from_vtrue(self
):
1138 b
= bt2
.BoolValue(self
._t
)
1141 def test_create_from_int_non_zero(self
):
1142 with self
.assertRaises(TypeError):
1145 def test_create_from_int_zero(self
):
1146 with self
.assertRaises(TypeError):
1149 def test_assign_true(self
):
1154 def test_assign_false(self
):
1159 def test_assign_vtrue(self
):
1164 def test_assign_vfalse(self
):
1169 def test_assign_int(self
):
1170 with self
.assertRaises(TypeError):
1174 def test_bool_op(self
):
1175 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1177 def test_str_op(self
):
1178 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1180 def test_eq_none(self
):
1181 # Disable the "comparison to None" warning, as this is precisely what
1182 # we want to test here.
1183 self
.assertFalse(self
._def
== None) # noqa: E711
1185 def test_ne_none(self
):
1186 # Disable the "comparison to None" warning, as this is precisely what
1187 # we want to test here.
1188 self
.assertTrue(self
._def
!= None) # noqa: E711
1190 def test_vfalse_eq_false(self
):
1191 self
.assertEqual(self
._f
, False)
1193 def test_vfalse_ne_true(self
):
1194 self
.assertNotEqual(self
._f
, True)
1196 def test_vtrue_eq_true(self
):
1197 self
.assertEqual(self
._t
, True)
1199 def test_vtrue_ne_false(self
):
1200 self
.assertNotEqual(self
._t
, False)
1203 _inject_numeric_testing_methods(BoolValueTestCase
)
1206 class _TestIntegerValue(_TestNumericValue
):
1209 self
._ip
= self
._CLS
(self
._pv
)
1210 self
._def
= self
._ip
1211 self
._def
_value
= self
._pv
1212 self
._def
_new
_value
= 101
1219 def _assert_expecting_int(self
):
1220 return self
.assertRaisesRegex(TypeError, r
'expecting an integral number object')
1222 def _assert_expecting_int64(self
):
1223 return self
.assertRaisesRegex(
1224 ValueError, r
"expecting a signed 64-bit integral value"
1227 def _assert_expecting_uint64(self
):
1228 return self
.assertRaisesRegex(
1229 ValueError, r
"expecting an unsigned 64-bit integral value"
1232 def test_create_default(self
):
1234 self
.assertEqual(i
, 0)
1236 def test_create_pos(self
):
1237 self
.assertEqual(self
._ip
, self
._pv
)
1239 def test_create_neg(self
):
1240 self
.assertEqual(self
._in
, self
._nv
)
1242 def test_create_from_vint(self
):
1243 i
= self
._CLS
(self
._ip
)
1244 self
.assertEqual(i
, self
._pv
)
1246 def test_create_from_false(self
):
1247 i
= self
._CLS
(False)
1250 def test_create_from_true(self
):
1254 def test_create_from_unknown(self
):
1258 with self
._assert
_expecting
_int
():
1261 def test_create_from_varray(self
):
1262 with self
._assert
_expecting
_int
():
1263 self
._CLS
(bt2
.ArrayValue())
1265 def test_assign_true(self
):
1267 self
._def
.value
= raw
1268 self
.assertEqual(self
._def
, raw
)
1270 def test_assign_false(self
):
1272 self
._def
.value
= raw
1273 self
.assertEqual(self
._def
, raw
)
1275 def test_assign_pos_int(self
):
1277 self
._def
.value
= raw
1278 self
.assertEqual(self
._def
, raw
)
1280 def test_assign_vint(self
):
1282 self
._def
.value
= bt2
.create_value(raw
)
1283 self
.assertEqual(self
._def
, raw
)
1286 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1287 _CLS
= bt2
.SignedIntegerValue
1292 self
._in
= self
._CLS
(self
._nv
)
1293 self
._def
_new
_value
= -101
1299 def test_create_neg(self
):
1300 self
.assertEqual(self
._in
, self
._nv
)
1302 def test_create_pos_too_big(self
):
1303 with self
._assert
_expecting
_int
64():
1306 def test_create_neg_too_big(self
):
1307 with self
._assert
_expecting
_int
64():
1308 self
._CLS
(-(2 ** 63) - 1)
1310 def test_assign_neg_int(self
):
1312 self
._def
.value
= raw
1313 self
.assertEqual(self
._def
, raw
)
1315 def test_compare_big_int(self
):
1316 # Larger than the IEEE 754 double-precision exact representation of
1319 v
= bt2
.create_value(raw
)
1320 self
.assertEqual(v
, raw
)
1323 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
1326 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1327 _CLS
= bt2
.UnsignedIntegerValue
1329 def test_create_pos_too_big(self
):
1330 with self
._assert
_expecting
_uint
64():
1333 def test_create_neg(self
):
1334 with self
._assert
_expecting
_uint
64():
1338 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
)
1341 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1345 self
._fp
= bt2
.RealValue(self
._pv
)
1346 self
._fn
= bt2
.RealValue(self
._nv
)
1347 self
._def
= self
._fp
1348 self
._def
_value
= self
._pv
1349 self
._def
_new
_value
= -101.88
1357 def _assert_expecting_float(self
):
1358 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
1360 def _test_invalid_op(self
, cb
):
1361 with self
.assertRaises(TypeError):
1364 def test_create_default(self
):
1366 self
.assertEqual(f
, 0.0)
1368 def test_create_pos(self
):
1369 self
.assertEqual(self
._fp
, self
._pv
)
1371 def test_create_neg(self
):
1372 self
.assertEqual(self
._fn
, self
._nv
)
1374 def test_create_from_false(self
):
1375 f
= bt2
.RealValue(False)
1378 def test_create_from_true(self
):
1379 f
= bt2
.RealValue(True)
1382 def test_create_from_int(self
):
1384 f
= bt2
.RealValue(raw
)
1385 self
.assertEqual(f
, float(raw
))
1387 def test_create_from_vint(self
):
1389 f
= bt2
.RealValue(bt2
.create_value(raw
))
1390 self
.assertEqual(f
, float(raw
))
1392 def test_create_from_vfloat(self
):
1394 f
= bt2
.RealValue(bt2
.create_value(raw
))
1395 self
.assertEqual(f
, raw
)
1397 def test_create_from_unknown(self
):
1401 with self
._assert
_expecting
_float
():
1404 def test_create_from_varray(self
):
1405 with self
._assert
_expecting
_float
():
1406 bt2
.RealValue(bt2
.ArrayValue())
1408 def test_assign_true(self
):
1409 self
._def
.value
= True
1410 self
.assertTrue(self
._def
)
1412 def test_assign_false(self
):
1413 self
._def
.value
= False
1414 self
.assertFalse(self
._def
)
1416 def test_assign_pos_int(self
):
1418 self
._def
.value
= raw
1419 self
.assertEqual(self
._def
, float(raw
))
1421 def test_assign_neg_int(self
):
1423 self
._def
.value
= raw
1424 self
.assertEqual(self
._def
, float(raw
))
1426 def test_assign_vint(self
):
1428 self
._def
.value
= bt2
.create_value(raw
)
1429 self
.assertEqual(self
._def
, float(raw
))
1431 def test_assign_float(self
):
1433 self
._def
.value
= raw
1434 self
.assertEqual(self
._def
, raw
)
1436 def test_assign_vfloat(self
):
1438 self
._def
.value
= bt2
.create_value(raw
)
1439 self
.assertEqual(self
._def
, raw
)
1441 def test_invalid_lshift(self
):
1442 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1444 def test_invalid_rshift(self
):
1445 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1447 def test_invalid_and(self
):
1448 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1450 def test_invalid_or(self
):
1451 self
._test
_invalid
_op
(lambda: self
._def |
23)
1453 def test_invalid_xor(self
):
1454 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1456 def test_invalid_invert(self
):
1457 self
._test
_invalid
_op
(lambda: ~self
._def
)
1460 _inject_numeric_testing_methods(RealValueTestCase
)
1463 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1465 self
._def
_value
= 'Hello, World!'
1466 self
._def
= bt2
.StringValue(self
._def
_value
)
1467 self
._def
_const
= _create_const_value(self
._def
_value
)
1468 self
._def
_new
_value
= 'Yes!'
1473 def _assert_expecting_str(self
):
1474 return self
.assertRaises(TypeError)
1476 def test_create_default(self
):
1477 s
= bt2
.StringValue()
1478 self
.assertEqual(s
, '')
1480 def test_create_from_str(self
):
1482 s
= bt2
.StringValue(raw
)
1483 self
.assertEqual(s
, raw
)
1485 def test_create_from_vstr(self
):
1487 s
= bt2
.StringValue(bt2
.create_value(raw
))
1488 self
.assertEqual(s
, raw
)
1490 def test_create_from_unknown(self
):
1494 with self
._assert
_expecting
_str
():
1495 bt2
.StringValue(A())
1497 def test_create_from_varray(self
):
1498 with self
._assert
_expecting
_str
():
1499 bt2
.StringValue(bt2
.ArrayValue())
1501 def test_assign_int(self
):
1502 with self
._assert
_expecting
_str
():
1503 self
._def
.value
= 283
1505 def test_assign_str(self
):
1508 self
.assertEqual(self
._def
, raw
)
1510 def test_assign_vstr(self
):
1512 self
._def
= bt2
.create_value(raw
)
1513 self
.assertEqual(self
._def
, raw
)
1516 self
.assertEqual(self
._def
, self
._def
_value
)
1518 def test_const_eq(self
):
1519 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1521 def test_eq_raw(self
):
1522 self
.assertNotEqual(self
._def
, 23)
1524 def test_lt_vstring(self
):
1525 s1
= bt2
.StringValue('allo')
1526 s2
= bt2
.StringValue('bateau')
1527 self
.assertLess(s1
, s2
)
1529 def test_lt_string(self
):
1530 s1
= bt2
.StringValue('allo')
1531 self
.assertLess(s1
, 'bateau')
1533 def test_le_vstring(self
):
1534 s1
= bt2
.StringValue('allo')
1535 s2
= bt2
.StringValue('bateau')
1536 self
.assertLessEqual(s1
, s2
)
1538 def test_le_string(self
):
1539 s1
= bt2
.StringValue('allo')
1540 self
.assertLessEqual(s1
, 'bateau')
1542 def test_gt_vstring(self
):
1543 s1
= bt2
.StringValue('allo')
1544 s2
= bt2
.StringValue('bateau')
1545 self
.assertGreater(s2
, s1
)
1547 def test_gt_string(self
):
1548 s1
= bt2
.StringValue('allo')
1549 self
.assertGreater('bateau', s1
)
1551 def test_ge_vstring(self
):
1552 s1
= bt2
.StringValue('allo')
1553 s2
= bt2
.StringValue('bateau')
1554 self
.assertGreaterEqual(s2
, s1
)
1556 def test_ge_string(self
):
1557 s1
= bt2
.StringValue('allo')
1558 self
.assertGreaterEqual('bateau', s1
)
1560 def test_in_string(self
):
1561 s1
= bt2
.StringValue('beau grand bateau')
1562 self
.assertIn('bateau', s1
)
1564 def test_in_vstring(self
):
1565 s1
= bt2
.StringValue('beau grand bateau')
1566 s2
= bt2
.StringValue('bateau')
1567 self
.assertIn(s2
, s1
)
1569 def test_bool_op(self
):
1570 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1572 def test_str_op(self
):
1573 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1576 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1578 def test_getitem(self
):
1579 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1581 def test_const_getitem(self
):
1582 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1584 def test_iadd_str(self
):
1585 to_append
= 'meow meow meow'
1586 self
._def
+= to_append
1587 self
._def
_value
+= to_append
1588 self
.assertEqual(self
._def
, self
._def
_value
)
1590 def test_const_iadd_str(self
):
1591 to_append
= 'meow meow meow'
1592 with self
.assertRaises(TypeError):
1593 self
._def
_const
+= to_append
1595 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1597 def test_append_vstr(self
):
1598 to_append
= 'meow meow meow'
1599 self
._def
+= bt2
.create_value(to_append
)
1600 self
._def
_value
+= to_append
1601 self
.assertEqual(self
._def
, self
._def
_value
)
1604 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1606 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1607 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1608 self
._def
_const
= _create_const_value(copy
.deepcopy(self
._def
_value
))
1613 def _modify_def(self
):
1614 self
._def
[2] = 'xyz'
1616 def _assert_type_error(self
):
1617 return self
.assertRaises(TypeError)
1619 def test_create_default(self
):
1620 a
= bt2
.ArrayValue()
1621 self
.assertEqual(len(a
), 0)
1623 def test_create_from_array(self
):
1624 self
.assertEqual(self
._def
, self
._def
_value
)
1626 def test_create_from_tuple(self
):
1627 t
= 1, 2, False, None
1628 a
= bt2
.ArrayValue(t
)
1629 self
.assertEqual(a
, t
)
1631 def test_create_from_varray(self
):
1632 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1633 a
= bt2
.ArrayValue(va
)
1634 self
.assertEqual(va
, a
)
1636 def test_create_from_unknown(self
):
1640 with self
._assert
_type
_error
():
1643 def test_bool_op_true(self
):
1644 self
.assertTrue(bool(self
._def
))
1646 def test_bool_op_false(self
):
1647 self
.assertFalse(bool(bt2
.ArrayValue()))
1650 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1652 def test_eq_int(self
):
1653 self
.assertNotEqual(self
._def
, 23)
1655 def test_const_eq(self
):
1656 a1
= _create_const_value([1, 2, 3])
1658 self
.assertEqual(a1
, a2
)
1660 def test_eq_diff_len(self
):
1661 a1
= bt2
.create_value([1, 2, 3])
1662 a2
= bt2
.create_value([1, 2])
1663 self
.assertIs(type(a1
), bt2
.ArrayValue
)
1664 self
.assertIs(type(a2
), bt2
.ArrayValue
)
1665 self
.assertNotEqual(a1
, a2
)
1667 def test_eq_diff_content_same_len(self
):
1668 a1
= bt2
.create_value([1, 2, 3])
1669 a2
= bt2
.create_value([4, 5, 6])
1670 self
.assertNotEqual(a1
, a2
)
1672 def test_eq_same_content_same_len(self
):
1673 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1674 a1
= bt2
.ArrayValue(raw
)
1675 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1676 self
.assertEqual(a1
, a2
)
1678 def test_eq_non_sequence_iterable(self
):
1679 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1680 a
= bt2
.ArrayValue((1, 3, 5))
1681 self
.assertEqual(a
, list(dct
.keys()))
1682 self
.assertNotEqual(a
, dct
)
1684 def test_setitem_int(self
):
1687 self
.assertEqual(self
._def
[2], raw
)
1689 def test_setitem_vint(self
):
1691 self
._def
[2] = bt2
.create_value(raw
)
1692 self
.assertEqual(self
._def
[2], raw
)
1694 def test_setitem_none(self
):
1696 self
.assertIsNone(self
._def
[2])
1698 def test_setitem_index_wrong_type(self
):
1699 with self
._assert
_type
_error
():
1700 self
._def
['yes'] = 23
1702 def test_setitem_index_neg(self
):
1703 with self
.assertRaises(IndexError):
1706 def test_setitem_index_out_of_range(self
):
1707 with self
.assertRaises(IndexError):
1708 self
._def
[len(self
._def
)] = 23
1710 def test_const_setitem(self
):
1711 with self
.assertRaises(TypeError):
1712 self
._def
_const
[2] = 19
1714 def test_append_none(self
):
1715 self
._def
.append(None)
1716 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1718 def test_append_int(self
):
1720 self
._def
.append(raw
)
1721 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1723 def test_const_append(self
):
1724 with self
.assertRaises(AttributeError):
1725 self
._def
_const
.append(12194)
1727 def test_append_vint(self
):
1729 self
._def
.append(bt2
.create_value(raw
))
1730 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1732 def test_append_unknown(self
):
1736 with self
._assert
_type
_error
():
1737 self
._def
.append(A())
1739 def test_iadd(self
):
1742 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1743 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1744 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1746 def test_const_iadd(self
):
1747 with self
.assertRaises(TypeError):
1748 self
._def
_const
+= 12194
1750 def test_iadd_unknown(self
):
1754 with self
._assert
_type
_error
():
1757 def test_iadd_list_unknown(self
):
1761 with self
._assert
_type
_error
():
1764 def test_iter(self
):
1765 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1766 self
.assertEqual(velem
, elem
)
1768 def test_const_iter(self
):
1769 for velem
, elem
in zip(self
._def
_const
, self
._def
_value
):
1770 self
.assertEqual(velem
, elem
)
1772 def test_const_get_item(self
):
1773 item1
= self
._def
_const
[0]
1774 item2
= self
._def
_const
[2]
1775 item3
= self
._def
_const
[5]
1776 item4
= self
._def
_const
[7]
1777 item5
= self
._def
_const
[8]
1779 self
.assertEqual(item1
, None)
1781 self
.assertIs(type(item2
), bt2
._BoolValueConst
)
1782 self
.assertEqual(item2
, True)
1784 self
.assertIs(type(item3
), bt2
._SignedIntegerValueConst
)
1785 self
.assertEqual(item3
, 42)
1787 self
.assertIs(type(item4
), bt2
._RealValueConst
)
1788 self
.assertEqual(item4
, 23.17)
1790 self
.assertIs(type(item5
), bt2
._StringValueConst
)
1791 self
.assertEqual(item5
, 'yes')
1794 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1807 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1808 self
._def
_const
= _create_const_value(self
._def
_value
)
1813 def _modify_def(self
):
1814 self
._def
['zero'] = 1
1816 def test_create_default(self
):
1818 self
.assertEqual(len(m
), 0)
1820 def test_create_from_dict(self
):
1821 self
.assertEqual(self
._def
, self
._def
_value
)
1823 def test_create_from_vmap(self
):
1824 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1825 m
= bt2
.MapValue(vm
)
1826 self
.assertEqual(vm
, m
)
1828 def test_create_from_unknown(self
):
1832 with self
.assertRaises(AttributeError):
1835 def test_bool_op_true(self
):
1836 self
.assertTrue(bool(self
._def
))
1838 def test_bool_op_false(self
):
1839 self
.assertFalse(bool(bt2
.MapValue()))
1842 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1844 def test_const_eq(self
):
1845 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1846 a2
= {'a': 1, 'b': 2, 'c': 3}
1847 self
.assertEqual(a1
, a2
)
1849 def test_eq_int(self
):
1850 self
.assertNotEqual(self
._def
, 23)
1852 def test_eq_diff_len(self
):
1853 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1854 a2
= bt2
.create_value({'a': 1, 'b': 2})
1855 self
.assertNotEqual(a1
, a2
)
1857 def test_const_eq_diff_len(self
):
1858 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1859 a2
= _create_const_value({'a': 1, 'b': 2})
1860 self
.assertNotEqual(a1
, a2
)
1862 def test_eq_diff_content_same_len(self
):
1863 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1864 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1865 self
.assertNotEqual(a1
, a2
)
1867 def test_const_eq_diff_content_same_len(self
):
1868 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1869 a2
= _create_const_value({'a': 4, 'b': 2, 'c': 3})
1870 self
.assertNotEqual(a1
, a2
)
1872 def test_eq_same_content_diff_keys(self
):
1873 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1874 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1875 self
.assertNotEqual(a1
, a2
)
1877 def test_const_eq_same_content_diff_keys(self
):
1878 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1879 a2
= _create_const_value({'a': 1, 'k': 2, 'c': 3})
1880 self
.assertNotEqual(a1
, a2
)
1882 def test_eq_same_content_same_len(self
):
1883 raw
= {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1884 a1
= bt2
.MapValue(raw
)
1885 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1886 self
.assertEqual(a1
, a2
)
1887 self
.assertEqual(a1
, raw
)
1889 def test_const_eq_same_content_same_len(self
):
1890 raw
= {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1891 a1
= _create_const_value(raw
)
1892 a2
= _create_const_value(copy
.deepcopy(raw
))
1893 self
.assertEqual(a1
, a2
)
1894 self
.assertEqual(a1
, raw
)
1896 def test_setitem_int(self
):
1898 self
._def
['pos-int'] = raw
1899 self
.assertEqual(self
._def
['pos-int'], raw
)
1901 def test_const_setitem_int(self
):
1902 with self
.assertRaises(TypeError):
1903 self
._def
_const
['pos-int'] = 19
1905 def test_setitem_vint(self
):
1907 self
._def
['pos-int'] = bt2
.create_value(raw
)
1908 self
.assertEqual(self
._def
['pos-int'], raw
)
1910 def test_setitem_none(self
):
1911 self
._def
['none'] = None
1912 self
.assertIsNone(self
._def
['none'])
1914 def test_setitem_new_int(self
):
1915 old_len
= len(self
._def
)
1916 self
._def
['new-int'] = 23
1917 self
.assertEqual(self
._def
['new-int'], 23)
1918 self
.assertEqual(len(self
._def
), old_len
+ 1)
1920 def test_setitem_index_wrong_type(self
):
1921 with self
.assertRaises(TypeError):
1924 def test_iter(self
):
1925 for vkey
, vval
in self
._def
.items():
1926 val
= self
._def
_value
[vkey
]
1927 self
.assertEqual(vval
, val
)
1929 def test_const_iter(self
):
1930 for vkey
, vval
in self
._def
_const
.items():
1931 val
= self
._def
_value
[vkey
]
1932 self
.assertEqual(vval
, val
)
1934 def test_get_item(self
):
1935 i
= self
._def
['pos-float']
1936 self
.assertIs(type(i
), bt2
.RealValue
)
1937 self
.assertEqual(i
, 23.17)
1939 def test_const_get_item(self
):
1940 item1
= self
._def
_const
['none']
1941 item2
= self
._def
_const
['true']
1942 item3
= self
._def
_const
['pos-int']
1943 item4
= self
._def
_const
['pos-float']
1944 item5
= self
._def
_const
['str']
1946 self
.assertEqual(item1
, None)
1948 self
.assertIs(type(item2
), bt2
._BoolValueConst
)
1949 self
.assertEqual(item2
, True)
1951 self
.assertIs(type(item3
), bt2
._SignedIntegerValueConst
)
1952 self
.assertEqual(item3
, 42)
1954 self
.assertIs(type(item4
), bt2
._RealValueConst
)
1955 self
.assertEqual(item4
, 23.17)
1957 self
.assertIs(type(item5
), bt2
._StringValueConst
)
1958 self
.assertEqual(item5
, 'yes')
1960 def test_getitem_wrong_key(self
):
1961 with self
.assertRaises(KeyError):
1962 self
._def
['kilojoule']
1965 if __name__
== '__main__':