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 self
.assertFalse(self
._def
== None)
487 def test_ne_none(self
):
488 self
.assertTrue(self
._def
!= None)
491 # This is a list of binary operators used for
492 # _inject_numeric_testing_methods().
494 # Each entry is a pair of binary operator name (used as part of the
495 # created testing method's name) and operator function.
503 ('add', operator
.add
),
504 ('radd', lambda a
, b
: operator
.add(b
, a
)),
505 ('and', operator
.and_
),
506 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
507 ('floordiv', operator
.floordiv
),
508 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
509 ('lshift', operator
.lshift
),
510 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
511 ('mod', operator
.mod
),
512 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
513 ('mul', operator
.mul
),
514 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
515 ('or', operator
.or_
),
516 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
517 ('pow', operator
.pow),
518 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
519 ('rshift', operator
.rshift
),
520 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
521 ('sub', operator
.sub
),
522 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
523 ('truediv', operator
.truediv
),
524 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
525 ('xor', operator
.xor
),
526 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
530 # This is a list of unary operators used for
531 # _inject_numeric_testing_methods().
533 # Each entry is a pair of unary operator name (used as part of the
534 # created testing method's name) and operator function.
536 ('neg', operator
.neg
),
537 ('pos', operator
.pos
),
538 ('abs', operator
.abs),
539 ('invert', operator
.invert
),
541 ('round_0', partial(round, ndigits
=0)),
542 ('round_1', partial(round, ndigits
=1)),
543 ('round_2', partial(round, ndigits
=2)),
544 ('round_3', partial(round, ndigits
=3)),
546 ('floor', math
.floor
),
547 ('trunc', math
.trunc
),
551 # This function injects a bunch of testing methods to a numeric
554 # It is meant to be used like this:
556 # _inject_numeric_testing_methods(MyNumericValueTestCase)
558 # This function injects:
560 # * One testing method for each _TestNumericValue._test_binop_*()
561 # method, for each binary operator in the _BINOPS tuple.
563 # * One testing method for each _TestNumericValue._test_unaryop*()
564 # method, for each unary operator in the _UNARYOPS tuple.
565 def _inject_numeric_testing_methods(cls
):
566 def test_binop_name(suffix
):
567 return 'test_binop_{}_{}'.format(name
, suffix
)
569 def test_unaryop_name(suffix
):
570 return 'test_unaryop_{}_{}'.format(name
, suffix
)
572 # inject testing methods for each binary operation
573 for name
, binop
in _BINOPS
:
576 test_binop_name('invalid_unknown'),
577 partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
),
581 test_binop_name('invalid_none'),
582 partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
),
586 test_binop_name('type_true'),
587 partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
),
591 test_binop_name('type_pos_int'),
592 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
),
596 test_binop_name('type_pos_vint'),
597 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
),
601 test_binop_name('value_true'),
602 partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
),
606 test_binop_name('value_pos_int'),
607 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
),
611 test_binop_name('value_pos_vint'),
612 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
),
616 test_binop_name('lhs_addr_same_true'),
617 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
621 test_binop_name('lhs_addr_same_pos_int'),
623 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
628 test_binop_name('lhs_addr_same_pos_vint'),
630 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
635 test_binop_name('lhs_value_same_true'),
636 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
640 test_binop_name('lhs_value_same_pos_int'),
642 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
647 test_binop_name('lhs_value_same_pos_vint'),
649 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
654 test_binop_name('type_neg_int'),
655 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
),
659 test_binop_name('type_neg_vint'),
660 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
),
664 test_binop_name('value_neg_int'),
665 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
),
669 test_binop_name('value_neg_vint'),
670 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
),
674 test_binop_name('lhs_addr_same_neg_int'),
676 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
681 test_binop_name('lhs_addr_same_neg_vint'),
683 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
688 test_binop_name('lhs_value_same_neg_int'),
690 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
695 test_binop_name('lhs_value_same_neg_vint'),
697 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
702 test_binop_name('type_false'),
703 partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
),
707 test_binop_name('type_zero_int'),
708 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
),
712 test_binop_name('type_zero_vint'),
713 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
),
717 test_binop_name('value_false'),
718 partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
),
722 test_binop_name('value_zero_int'),
723 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
),
727 test_binop_name('value_zero_vint'),
728 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
),
732 test_binop_name('lhs_addr_same_false'),
733 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
737 test_binop_name('lhs_addr_same_zero_int'),
739 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
744 test_binop_name('lhs_addr_same_zero_vint'),
746 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
751 test_binop_name('lhs_value_same_false'),
752 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
756 test_binop_name('lhs_value_same_zero_int'),
758 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
763 test_binop_name('lhs_value_same_zero_vint'),
765 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
770 test_binop_name('type_neg_float'),
771 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
),
775 test_binop_name('type_neg_vfloat'),
776 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
780 test_binop_name('value_neg_float'),
781 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
),
785 test_binop_name('value_neg_vfloat'),
786 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
790 test_binop_name('lhs_addr_same_neg_float'),
792 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
797 test_binop_name('lhs_addr_same_neg_vfloat'),
799 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
804 test_binop_name('lhs_value_same_neg_float'),
806 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
811 test_binop_name('lhs_value_same_neg_vfloat'),
813 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
818 test_binop_name('type_pos_float'),
819 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
),
823 test_binop_name('type_pos_vfloat'),
824 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
828 test_binop_name('value_pos_float'),
829 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
),
833 test_binop_name('value_pos_vfloat'),
834 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
838 test_binop_name('lhs_addr_same_pos_float'),
840 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
845 test_binop_name('lhs_addr_same_pos_vfloat'),
847 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
852 test_binop_name('lhs_value_same_pos_float'),
854 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
859 test_binop_name('lhs_value_same_pos_vfloat'),
861 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
866 test_binop_name('type_zero_float'),
867 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
),
871 test_binop_name('type_zero_vfloat'),
872 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
876 test_binop_name('value_zero_float'),
877 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
),
881 test_binop_name('value_zero_vfloat'),
882 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
886 test_binop_name('lhs_addr_same_zero_float'),
888 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
893 test_binop_name('lhs_addr_same_zero_vfloat'),
895 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
900 test_binop_name('lhs_value_same_zero_float'),
902 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
907 test_binop_name('lhs_value_same_zero_vfloat'),
909 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
914 test_binop_name('type_complex'),
915 partialmethod(_TestNumericValue
._test
_binop
_type
_complex
, op
=binop
),
919 test_binop_name('type_zero_complex'),
920 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_complex
, op
=binop
),
924 test_binop_name('value_complex'),
925 partialmethod(_TestNumericValue
._test
_binop
_value
_complex
, op
=binop
),
929 test_binop_name('value_zero_complex'),
930 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_complex
, op
=binop
),
934 test_binop_name('lhs_addr_same_complex'),
936 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
941 test_binop_name('lhs_addr_same_zero_complex'),
943 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
948 test_binop_name('lhs_value_same_complex'),
950 _TestNumericValue
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
955 test_binop_name('lhs_value_same_zero_complex'),
957 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
961 # inject testing methods for each unary operation
962 for name
, unaryop
in _UNARYOPS
:
965 test_unaryop_name('type'),
966 partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
),
970 test_unaryop_name('value'),
971 partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
),
975 test_unaryop_name('addr_same'),
976 partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
),
980 test_unaryop_name('value_same'),
981 partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
),
985 class CreateValueFuncTestCase(unittest
.TestCase
):
986 def test_create_none(self
):
987 v
= bt2
.create_value(None)
990 def test_create_bool_false(self
):
991 v
= bt2
.create_value(False)
992 self
.assertIsInstance(v
, bt2
.BoolValue
)
995 def test_create_bool_true(self
):
996 v
= bt2
.create_value(True)
997 self
.assertIsInstance(v
, bt2
.BoolValue
)
1000 def test_create_int_pos(self
):
1002 v
= bt2
.create_value(raw
)
1003 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1004 self
.assertEqual(v
, raw
)
1006 def test_create_int_neg(self
):
1008 v
= bt2
.create_value(raw
)
1009 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1010 self
.assertEqual(v
, raw
)
1012 def test_create_float_pos(self
):
1014 v
= bt2
.create_value(raw
)
1015 self
.assertIsInstance(v
, bt2
.RealValue
)
1016 self
.assertEqual(v
, raw
)
1018 def test_create_float_neg(self
):
1020 v
= bt2
.create_value(raw
)
1021 self
.assertIsInstance(v
, bt2
.RealValue
)
1022 self
.assertEqual(v
, raw
)
1024 def test_create_string(self
):
1026 v
= bt2
.create_value(raw
)
1027 self
.assertIsInstance(v
, bt2
.StringValue
)
1028 self
.assertEqual(v
, raw
)
1030 def test_create_string_empty(self
):
1032 v
= bt2
.create_value(raw
)
1033 self
.assertIsInstance(v
, bt2
.StringValue
)
1034 self
.assertEqual(v
, raw
)
1036 def test_create_array_from_list(self
):
1038 v
= bt2
.create_value(raw
)
1039 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1040 self
.assertEqual(v
, raw
)
1042 def test_create_array_from_tuple(self
):
1044 v
= bt2
.create_value(raw
)
1045 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1046 self
.assertEqual(v
, raw
)
1048 def test_create_array_from_empty_list(self
):
1050 v
= bt2
.create_value(raw
)
1051 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1052 self
.assertEqual(v
, raw
)
1054 def test_create_array_from_empty_tuple(self
):
1056 v
= bt2
.create_value(raw
)
1057 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1058 self
.assertEqual(v
, raw
)
1060 def test_create_map(self
):
1062 v
= bt2
.create_value(raw
)
1063 self
.assertIsInstance(v
, bt2
.MapValue
)
1064 self
.assertEqual(v
, raw
)
1066 def test_create_map_empty(self
):
1068 v
= bt2
.create_value(raw
)
1069 self
.assertIsInstance(v
, bt2
.MapValue
)
1070 self
.assertEqual(v
, raw
)
1072 def test_create_vfalse(self
):
1073 v
= bt2
.create_value(bt2
.create_value(False))
1074 self
.assertIsInstance(v
, bt2
.BoolValue
)
1077 def test_create_invalid(self
):
1083 with self
.assertRaisesRegex(
1084 TypeError, "cannot create value object from 'A' object"
1089 def _create_const_value(value
):
1090 class MySink(bt2
._UserSinkComponent
):
1091 def _user_consume(self
):
1095 def _user_query(cls
, priv_query_exec
, obj
, params
, method_obj
):
1097 return {'my_value': value
}
1099 res
= bt2
.QueryExecutor(MySink
, 'obj', None).query()
1100 return res
['my_value']
1103 class BoolValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1105 self
._f
= bt2
.BoolValue(False)
1106 self
._t
= bt2
.BoolValue(True)
1108 self
._def
_value
= False
1109 self
._def
_new
_value
= True
1116 def _assert_expecting_bool(self
):
1117 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
1119 def test_create_default(self
):
1123 def test_create_false(self
):
1124 self
.assertFalse(self
._f
)
1126 def test_create_true(self
):
1127 self
.assertTrue(self
._t
)
1129 def test_create_from_vfalse(self
):
1130 b
= bt2
.BoolValue(self
._f
)
1133 def test_create_from_vtrue(self
):
1134 b
= bt2
.BoolValue(self
._t
)
1137 def test_create_from_int_non_zero(self
):
1138 with self
.assertRaises(TypeError):
1141 def test_create_from_int_zero(self
):
1142 with self
.assertRaises(TypeError):
1145 def test_assign_true(self
):
1150 def test_assign_false(self
):
1155 def test_assign_vtrue(self
):
1160 def test_assign_vfalse(self
):
1165 def test_assign_int(self
):
1166 with self
.assertRaises(TypeError):
1170 def test_bool_op(self
):
1171 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1173 def test_str_op(self
):
1174 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1176 def test_eq_none(self
):
1177 self
.assertFalse(self
._def
== None)
1179 def test_ne_none(self
):
1180 self
.assertTrue(self
._def
!= None)
1182 def test_vfalse_eq_false(self
):
1183 self
.assertEqual(self
._f
, False)
1185 def test_vfalse_ne_true(self
):
1186 self
.assertNotEqual(self
._f
, True)
1188 def test_vtrue_eq_true(self
):
1189 self
.assertEqual(self
._t
, True)
1191 def test_vtrue_ne_false(self
):
1192 self
.assertNotEqual(self
._t
, False)
1195 _inject_numeric_testing_methods(BoolValueTestCase
)
1198 class _TestIntegerValue(_TestNumericValue
):
1201 self
._ip
= self
._CLS
(self
._pv
)
1202 self
._def
= self
._ip
1203 self
._def
_value
= self
._pv
1204 self
._def
_new
_value
= 101
1211 def _assert_expecting_int(self
):
1212 return self
.assertRaisesRegex(TypeError, r
'expecting an integral number object')
1214 def _assert_expecting_int64(self
):
1215 return self
.assertRaisesRegex(
1216 ValueError, r
"expecting a signed 64-bit integral value"
1219 def _assert_expecting_uint64(self
):
1220 return self
.assertRaisesRegex(
1221 ValueError, r
"expecting an unsigned 64-bit integral value"
1224 def test_create_default(self
):
1226 self
.assertEqual(i
, 0)
1228 def test_create_pos(self
):
1229 self
.assertEqual(self
._ip
, self
._pv
)
1231 def test_create_neg(self
):
1232 self
.assertEqual(self
._in
, self
._nv
)
1234 def test_create_from_vint(self
):
1235 i
= self
._CLS
(self
._ip
)
1236 self
.assertEqual(i
, self
._pv
)
1238 def test_create_from_false(self
):
1239 i
= self
._CLS
(False)
1242 def test_create_from_true(self
):
1246 def test_create_from_unknown(self
):
1250 with self
._assert
_expecting
_int
():
1253 def test_create_from_varray(self
):
1254 with self
._assert
_expecting
_int
():
1255 self
._CLS
(bt2
.ArrayValue())
1257 def test_assign_true(self
):
1259 self
._def
.value
= raw
1260 self
.assertEqual(self
._def
, raw
)
1262 def test_assign_false(self
):
1264 self
._def
.value
= raw
1265 self
.assertEqual(self
._def
, raw
)
1267 def test_assign_pos_int(self
):
1269 self
._def
.value
= raw
1270 self
.assertEqual(self
._def
, raw
)
1272 def test_assign_vint(self
):
1274 self
._def
.value
= bt2
.create_value(raw
)
1275 self
.assertEqual(self
._def
, raw
)
1278 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1279 _CLS
= bt2
.SignedIntegerValue
1284 self
._in
= self
._CLS
(self
._nv
)
1285 self
._def
_new
_value
= -101
1291 def test_create_neg(self
):
1292 self
.assertEqual(self
._in
, self
._nv
)
1294 def test_create_pos_too_big(self
):
1295 with self
._assert
_expecting
_int
64():
1298 def test_create_neg_too_big(self
):
1299 with self
._assert
_expecting
_int
64():
1300 self
._CLS
(-(2 ** 63) - 1)
1302 def test_assign_neg_int(self
):
1304 self
._def
.value
= raw
1305 self
.assertEqual(self
._def
, raw
)
1307 def test_compare_big_int(self
):
1308 # Larger than the IEEE 754 double-precision exact representation of
1311 v
= bt2
.create_value(raw
)
1312 self
.assertEqual(v
, raw
)
1315 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
1318 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1319 _CLS
= bt2
.UnsignedIntegerValue
1321 def test_create_pos_too_big(self
):
1322 with self
._assert
_expecting
_uint
64():
1325 def test_create_neg(self
):
1326 with self
._assert
_expecting
_uint
64():
1330 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
)
1333 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1337 self
._fp
= bt2
.RealValue(self
._pv
)
1338 self
._fn
= bt2
.RealValue(self
._nv
)
1339 self
._def
= self
._fp
1340 self
._def
_value
= self
._pv
1341 self
._def
_new
_value
= -101.88
1349 def _assert_expecting_float(self
):
1350 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
1352 def _test_invalid_op(self
, cb
):
1353 with self
.assertRaises(TypeError):
1356 def test_create_default(self
):
1358 self
.assertEqual(f
, 0.0)
1360 def test_create_pos(self
):
1361 self
.assertEqual(self
._fp
, self
._pv
)
1363 def test_create_neg(self
):
1364 self
.assertEqual(self
._fn
, self
._nv
)
1366 def test_create_from_false(self
):
1367 f
= bt2
.RealValue(False)
1370 def test_create_from_true(self
):
1371 f
= bt2
.RealValue(True)
1374 def test_create_from_int(self
):
1376 f
= bt2
.RealValue(raw
)
1377 self
.assertEqual(f
, float(raw
))
1379 def test_create_from_vint(self
):
1381 f
= bt2
.RealValue(bt2
.create_value(raw
))
1382 self
.assertEqual(f
, float(raw
))
1384 def test_create_from_vfloat(self
):
1386 f
= bt2
.RealValue(bt2
.create_value(raw
))
1387 self
.assertEqual(f
, raw
)
1389 def test_create_from_unknown(self
):
1393 with self
._assert
_expecting
_float
():
1396 def test_create_from_varray(self
):
1397 with self
._assert
_expecting
_float
():
1398 bt2
.RealValue(bt2
.ArrayValue())
1400 def test_assign_true(self
):
1401 self
._def
.value
= True
1402 self
.assertTrue(self
._def
)
1404 def test_assign_false(self
):
1405 self
._def
.value
= False
1406 self
.assertFalse(self
._def
)
1408 def test_assign_pos_int(self
):
1410 self
._def
.value
= raw
1411 self
.assertEqual(self
._def
, float(raw
))
1413 def test_assign_neg_int(self
):
1415 self
._def
.value
= raw
1416 self
.assertEqual(self
._def
, float(raw
))
1418 def test_assign_vint(self
):
1420 self
._def
.value
= bt2
.create_value(raw
)
1421 self
.assertEqual(self
._def
, float(raw
))
1423 def test_assign_float(self
):
1425 self
._def
.value
= raw
1426 self
.assertEqual(self
._def
, raw
)
1428 def test_assign_vfloat(self
):
1430 self
._def
.value
= bt2
.create_value(raw
)
1431 self
.assertEqual(self
._def
, raw
)
1433 def test_invalid_lshift(self
):
1434 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1436 def test_invalid_rshift(self
):
1437 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1439 def test_invalid_and(self
):
1440 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1442 def test_invalid_or(self
):
1443 self
._test
_invalid
_op
(lambda: self
._def |
23)
1445 def test_invalid_xor(self
):
1446 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1448 def test_invalid_invert(self
):
1449 self
._test
_invalid
_op
(lambda: ~self
._def
)
1452 _inject_numeric_testing_methods(RealValueTestCase
)
1455 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1457 self
._def
_value
= 'Hello, World!'
1458 self
._def
= bt2
.StringValue(self
._def
_value
)
1459 self
._def
_const
= _create_const_value(self
._def
_value
)
1460 self
._def
_new
_value
= 'Yes!'
1465 def _assert_expecting_str(self
):
1466 return self
.assertRaises(TypeError)
1468 def test_create_default(self
):
1469 s
= bt2
.StringValue()
1470 self
.assertEqual(s
, '')
1472 def test_create_from_str(self
):
1474 s
= bt2
.StringValue(raw
)
1475 self
.assertEqual(s
, raw
)
1477 def test_create_from_vstr(self
):
1479 s
= bt2
.StringValue(bt2
.create_value(raw
))
1480 self
.assertEqual(s
, raw
)
1482 def test_create_from_unknown(self
):
1486 with self
._assert
_expecting
_str
():
1487 bt2
.StringValue(A())
1489 def test_create_from_varray(self
):
1490 with self
._assert
_expecting
_str
():
1491 bt2
.StringValue(bt2
.ArrayValue())
1493 def test_assign_int(self
):
1494 with self
._assert
_expecting
_str
():
1495 self
._def
.value
= 283
1497 def test_assign_str(self
):
1500 self
.assertEqual(self
._def
, raw
)
1502 def test_assign_vstr(self
):
1504 self
._def
= bt2
.create_value(raw
)
1505 self
.assertEqual(self
._def
, raw
)
1508 self
.assertEqual(self
._def
, self
._def
_value
)
1510 def test_const_eq(self
):
1511 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1513 def test_eq_raw(self
):
1514 self
.assertNotEqual(self
._def
, 23)
1516 def test_lt_vstring(self
):
1517 s1
= bt2
.StringValue('allo')
1518 s2
= bt2
.StringValue('bateau')
1519 self
.assertLess(s1
, s2
)
1521 def test_lt_string(self
):
1522 s1
= bt2
.StringValue('allo')
1523 self
.assertLess(s1
, 'bateau')
1525 def test_le_vstring(self
):
1526 s1
= bt2
.StringValue('allo')
1527 s2
= bt2
.StringValue('bateau')
1528 self
.assertLessEqual(s1
, s2
)
1530 def test_le_string(self
):
1531 s1
= bt2
.StringValue('allo')
1532 self
.assertLessEqual(s1
, 'bateau')
1534 def test_gt_vstring(self
):
1535 s1
= bt2
.StringValue('allo')
1536 s2
= bt2
.StringValue('bateau')
1537 self
.assertGreater(s2
, s1
)
1539 def test_gt_string(self
):
1540 s1
= bt2
.StringValue('allo')
1541 self
.assertGreater('bateau', s1
)
1543 def test_ge_vstring(self
):
1544 s1
= bt2
.StringValue('allo')
1545 s2
= bt2
.StringValue('bateau')
1546 self
.assertGreaterEqual(s2
, s1
)
1548 def test_ge_string(self
):
1549 s1
= bt2
.StringValue('allo')
1550 self
.assertGreaterEqual('bateau', s1
)
1552 def test_in_string(self
):
1553 s1
= bt2
.StringValue('beau grand bateau')
1554 self
.assertIn('bateau', s1
)
1556 def test_in_vstring(self
):
1557 s1
= bt2
.StringValue('beau grand bateau')
1558 s2
= bt2
.StringValue('bateau')
1559 self
.assertIn(s2
, s1
)
1561 def test_bool_op(self
):
1562 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1564 def test_str_op(self
):
1565 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1568 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1570 def test_getitem(self
):
1571 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1573 def test_const_getitem(self
):
1574 self
.assertEqual(self
._def
_const
[5], self
._def
_value
[5])
1576 def test_iadd_str(self
):
1577 to_append
= 'meow meow meow'
1578 self
._def
+= to_append
1579 self
._def
_value
+= to_append
1580 self
.assertEqual(self
._def
, self
._def
_value
)
1582 def test_const_iadd_str(self
):
1583 to_append
= 'meow meow meow'
1584 with self
.assertRaises(TypeError):
1585 self
._def
_const
+= to_append
1587 self
.assertEqual(self
._def
_const
, self
._def
_value
)
1589 def test_append_vstr(self
):
1590 to_append
= 'meow meow meow'
1591 self
._def
+= bt2
.create_value(to_append
)
1592 self
._def
_value
+= to_append
1593 self
.assertEqual(self
._def
, self
._def
_value
)
1596 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1598 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1599 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1600 self
._def
_const
= _create_const_value(copy
.deepcopy(self
._def
_value
))
1605 def _modify_def(self
):
1606 self
._def
[2] = 'xyz'
1608 def _assert_type_error(self
):
1609 return self
.assertRaises(TypeError)
1611 def test_create_default(self
):
1612 a
= bt2
.ArrayValue()
1613 self
.assertEqual(len(a
), 0)
1615 def test_create_from_array(self
):
1616 self
.assertEqual(self
._def
, self
._def
_value
)
1618 def test_create_from_tuple(self
):
1619 t
= 1, 2, False, None
1620 a
= bt2
.ArrayValue(t
)
1621 self
.assertEqual(a
, t
)
1623 def test_create_from_varray(self
):
1624 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1625 a
= bt2
.ArrayValue(va
)
1626 self
.assertEqual(va
, a
)
1628 def test_create_from_unknown(self
):
1632 with self
._assert
_type
_error
():
1635 def test_bool_op_true(self
):
1636 self
.assertTrue(bool(self
._def
))
1638 def test_bool_op_false(self
):
1639 self
.assertFalse(bool(bt2
.ArrayValue()))
1642 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1644 def test_eq_int(self
):
1645 self
.assertNotEqual(self
._def
, 23)
1647 def test_const_eq(self
):
1648 a1
= _create_const_value([1, 2, 3])
1650 self
.assertEqual(a1
, a2
)
1652 def test_eq_diff_len(self
):
1653 a1
= bt2
.create_value([1, 2, 3])
1654 a2
= bt2
.create_value([1, 2])
1655 self
.assertIs(type(a1
), bt2
.ArrayValue
)
1656 self
.assertIs(type(a2
), bt2
.ArrayValue
)
1657 self
.assertNotEqual(a1
, a2
)
1659 def test_eq_diff_content_same_len(self
):
1660 a1
= bt2
.create_value([1, 2, 3])
1661 a2
= bt2
.create_value([4, 5, 6])
1662 self
.assertNotEqual(a1
, a2
)
1664 def test_eq_same_content_same_len(self
):
1665 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1666 a1
= bt2
.ArrayValue(raw
)
1667 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1668 self
.assertEqual(a1
, a2
)
1670 def test_eq_non_sequence_iterable(self
):
1671 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1672 a
= bt2
.ArrayValue((1, 3, 5))
1673 self
.assertEqual(a
, list(dct
.keys()))
1674 self
.assertNotEqual(a
, dct
)
1676 def test_setitem_int(self
):
1679 self
.assertEqual(self
._def
[2], raw
)
1681 def test_setitem_vint(self
):
1683 self
._def
[2] = bt2
.create_value(raw
)
1684 self
.assertEqual(self
._def
[2], raw
)
1686 def test_setitem_none(self
):
1688 self
.assertIsNone(self
._def
[2])
1690 def test_setitem_index_wrong_type(self
):
1691 with self
._assert
_type
_error
():
1692 self
._def
['yes'] = 23
1694 def test_setitem_index_neg(self
):
1695 with self
.assertRaises(IndexError):
1698 def test_setitem_index_out_of_range(self
):
1699 with self
.assertRaises(IndexError):
1700 self
._def
[len(self
._def
)] = 23
1702 def test_const_setitem(self
):
1703 with self
.assertRaises(TypeError):
1704 self
._def
_const
[2] = 19
1706 def test_append_none(self
):
1707 self
._def
.append(None)
1708 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1710 def test_append_int(self
):
1712 self
._def
.append(raw
)
1713 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1715 def test_const_append(self
):
1716 with self
.assertRaises(AttributeError):
1717 self
._def
_const
.append(12194)
1719 def test_append_vint(self
):
1721 self
._def
.append(bt2
.create_value(raw
))
1722 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1724 def test_append_unknown(self
):
1728 with self
._assert
_type
_error
():
1729 self
._def
.append(A())
1731 def test_iadd(self
):
1734 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1735 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1736 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1738 def test_const_iadd(self
):
1739 with self
.assertRaises(TypeError):
1740 self
._def
_const
+= 12194
1742 def test_iadd_unknown(self
):
1746 with self
._assert
_type
_error
():
1749 def test_iadd_list_unknown(self
):
1753 with self
._assert
_type
_error
():
1756 def test_iter(self
):
1757 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1758 self
.assertEqual(velem
, elem
)
1760 def test_const_iter(self
):
1761 for velem
, elem
in zip(self
._def
_const
, self
._def
_value
):
1762 self
.assertEqual(velem
, elem
)
1764 def test_const_get_item(self
):
1765 item1
= self
._def
_const
[0]
1766 item2
= self
._def
_const
[2]
1767 item3
= self
._def
_const
[5]
1768 item4
= self
._def
_const
[7]
1769 item5
= self
._def
_const
[8]
1771 self
.assertEqual(item1
, None)
1773 self
.assertIs(type(item2
), bt2
._BoolValueConst
)
1774 self
.assertEqual(item2
, True)
1776 self
.assertIs(type(item3
), bt2
._SignedIntegerValueConst
)
1777 self
.assertEqual(item3
, 42)
1779 self
.assertIs(type(item4
), bt2
._RealValueConst
)
1780 self
.assertEqual(item4
, 23.17)
1782 self
.assertIs(type(item5
), bt2
._StringValueConst
)
1783 self
.assertEqual(item5
, 'yes')
1786 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1799 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1800 self
._def
_const
= _create_const_value(self
._def
_value
)
1805 def _modify_def(self
):
1806 self
._def
['zero'] = 1
1808 def test_create_default(self
):
1810 self
.assertEqual(len(m
), 0)
1812 def test_create_from_dict(self
):
1813 self
.assertEqual(self
._def
, self
._def
_value
)
1815 def test_create_from_vmap(self
):
1816 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1817 m
= bt2
.MapValue(vm
)
1818 self
.assertEqual(vm
, m
)
1820 def test_create_from_unknown(self
):
1824 with self
.assertRaises(AttributeError):
1827 def test_bool_op_true(self
):
1828 self
.assertTrue(bool(self
._def
))
1830 def test_bool_op_false(self
):
1831 self
.assertFalse(bool(bt2
.MapValue()))
1834 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1836 def test_const_eq(self
):
1837 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1838 a2
= {'a': 1, 'b': 2, 'c': 3}
1839 self
.assertEqual(a1
, a2
)
1841 def test_eq_int(self
):
1842 self
.assertNotEqual(self
._def
, 23)
1844 def test_eq_diff_len(self
):
1845 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1846 a2
= bt2
.create_value({'a': 1, 'b': 2})
1847 self
.assertNotEqual(a1
, a2
)
1849 def test_const_eq_diff_len(self
):
1850 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1851 a2
= _create_const_value({'a': 1, 'b': 2})
1852 self
.assertNotEqual(a1
, a2
)
1854 def test_eq_diff_content_same_len(self
):
1855 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1856 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1857 self
.assertNotEqual(a1
, a2
)
1859 def test_const_eq_diff_content_same_len(self
):
1860 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1861 a2
= _create_const_value({'a': 4, 'b': 2, 'c': 3})
1862 self
.assertNotEqual(a1
, a2
)
1864 def test_eq_same_content_diff_keys(self
):
1865 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1866 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1867 self
.assertNotEqual(a1
, a2
)
1869 def test_const_eq_same_content_diff_keys(self
):
1870 a1
= _create_const_value({'a': 1, 'b': 2, 'c': 3})
1871 a2
= _create_const_value({'a': 1, 'k': 2, 'c': 3})
1872 self
.assertNotEqual(a1
, a2
)
1874 def test_eq_same_content_same_len(self
):
1875 raw
= {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1876 a1
= bt2
.MapValue(raw
)
1877 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1878 self
.assertEqual(a1
, a2
)
1879 self
.assertEqual(a1
, raw
)
1881 def test_const_eq_same_content_same_len(self
):
1882 raw
= {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1883 a1
= _create_const_value(raw
)
1884 a2
= _create_const_value(copy
.deepcopy(raw
))
1885 self
.assertEqual(a1
, a2
)
1886 self
.assertEqual(a1
, raw
)
1888 def test_setitem_int(self
):
1890 self
._def
['pos-int'] = raw
1891 self
.assertEqual(self
._def
['pos-int'], raw
)
1893 def test_const_setitem_int(self
):
1894 with self
.assertRaises(TypeError):
1895 self
._def
_const
['pos-int'] = 19
1897 def test_setitem_vint(self
):
1899 self
._def
['pos-int'] = bt2
.create_value(raw
)
1900 self
.assertEqual(self
._def
['pos-int'], raw
)
1902 def test_setitem_none(self
):
1903 self
._def
['none'] = None
1904 self
.assertIsNone(self
._def
['none'])
1906 def test_setitem_new_int(self
):
1907 old_len
= len(self
._def
)
1908 self
._def
['new-int'] = 23
1909 self
.assertEqual(self
._def
['new-int'], 23)
1910 self
.assertEqual(len(self
._def
), old_len
+ 1)
1912 def test_setitem_index_wrong_type(self
):
1913 with self
.assertRaises(TypeError):
1916 def test_iter(self
):
1917 for vkey
, vval
in self
._def
.items():
1918 val
= self
._def
_value
[vkey
]
1919 self
.assertEqual(vval
, val
)
1921 def test_const_iter(self
):
1922 for vkey
, vval
in self
._def
_const
.items():
1923 val
= self
._def
_value
[vkey
]
1924 self
.assertEqual(vval
, val
)
1926 def test_get_item(self
):
1927 i
= self
._def
['pos-float']
1928 self
.assertIs(type(i
), bt2
.RealValue
)
1929 self
.assertEqual(i
, 23.17)
1931 def test_const_get_item(self
):
1932 item1
= self
._def
_const
['none']
1933 item2
= self
._def
_const
['true']
1934 item3
= self
._def
_const
['pos-int']
1935 item4
= self
._def
_const
['pos-float']
1936 item5
= self
._def
_const
['str']
1938 self
.assertEqual(item1
, None)
1940 self
.assertIs(type(item2
), bt2
._BoolValueConst
)
1941 self
.assertEqual(item2
, True)
1943 self
.assertIs(type(item3
), bt2
._SignedIntegerValueConst
)
1944 self
.assertEqual(item3
, 42)
1946 self
.assertIs(type(item4
), bt2
._RealValueConst
)
1947 self
.assertEqual(item4
, 23.17)
1949 self
.assertIs(type(item5
), bt2
._StringValueConst
)
1950 self
.assertEqual(item5
, 'yes')
1952 def test_getitem_wrong_key(self
):
1953 with self
.assertRaises(KeyError):
1954 self
._def
['kilojoule']
1957 if __name__
== '__main__':