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
)
47 # Base class for numeric value test cases.
49 # To be compatible with this base class, a derived class must, in its
52 # * Set `self._def` to a value object with an arbitrary raw value.
53 # * Set `self._def_value` to the equivalent raw value of `self._def`.
54 class _TestNumericValue(_TestCopySimple
):
55 # Tries the binary operation `op`:
57 # 1. Between `self._def`, which is a value object, and `rhs`.
58 # 2. Between `self._def_value`, which is the raw value of
59 # `self._def`, and `rhs`.
61 # Returns the results of 1. and 2.
63 # If there's an exception while performing 1. or 2., asserts that
64 # both operations raised exceptions, that both exceptions have the
65 # same type, and returns `None` for both results.
66 def _binop(self
, op
, rhs
):
71 # try with value object
73 r
= op(self
._def
, rhs
)
74 except Exception as e
:
79 rv
= op(self
._def
_value
, comp_value
)
80 except Exception as e
:
83 if type_rexc
is not None or type_rvexc
is not None:
84 # at least one of the operations raised an exception: in
85 # this case both operations should have raised the same
86 # type of exception (division by zero, bit shift with a
87 # floating point number operand, etc.)
88 self
.assertIs(type_rexc
, type_rvexc
)
93 # Tries the unary operation `op`:
95 # 1. On `self._def`, which is a value object.
96 # 2. On `self._def_value`, which is the raw value of `self._def`.
98 # Returns the results of 1. and 2.
100 # If there's an exception while performing 1. or 2., asserts that
101 # both operations raised exceptions, that both exceptions have the
102 # same type, and returns `None` for both results.
103 def _unaryop(self
, op
):
107 # try with value object
110 except Exception as e
:
115 rv
= op(self
._def
_value
)
116 except Exception as e
:
119 if type_rexc
is not None or type_rvexc
is not None:
120 # at least one of the operations raised an exception: in
121 # this case both operations should have raised the same
122 # type of exception (division by zero, bit shift with a
123 # floating point number operand, etc.)
124 self
.assertIs(type_rexc
, type_rvexc
)
129 # Tests that the unary operation `op` gives results with the same
130 # type for both `self._def` and `self._def_value`.
131 def _test_unaryop_type(self
, op
):
132 r
, rv
= self
._unaryop
(op
)
137 self
.assertIsInstance(r
, type(rv
))
139 # Tests that the unary operation `op` gives results with the same
140 # value for both `self._def` and `self._def_value`. This uses the
141 # __eq__() operator of `self._def`.
142 def _test_unaryop_value(self
, op
):
143 r
, rv
= self
._unaryop
(op
)
148 self
.assertEqual(r
, rv
)
150 # Tests that the unary operation `op`, when applied to `self._def`,
151 # does not change its underlying BT object address.
152 def _test_unaryop_addr_same(self
, op
):
153 addr_before
= self
._def
.addr
155 self
.assertEqual(self
._def
.addr
, addr_before
)
157 # Tests that the unary operation `op`, when applied to `self._def`,
158 # does not change its value.
159 def _test_unaryop_value_same(self
, op
):
160 value_before
= self
._def
.__class
__(self
._def
)
162 self
.assertEqual(self
._def
, value_before
)
164 # Tests that the binary operation `op` gives results with the same
165 # type for both `self._def` and `self._def_value`.
166 def _test_binop_type(self
, op
, rhs
):
167 r
, rv
= self
._binop
(op
, rhs
)
172 if op
in _COMP_BINOPS
:
173 # __eq__() and __ne__() always return a 'bool' object
174 self
.assertIsInstance(r
, bool)
176 self
.assertIsInstance(r
, type(rv
))
178 # Tests that the binary operation `op` gives results with the same
179 # value for both `self._def` and `self._def_value`. This uses the
180 # __eq__() operator of `self._def`.
181 def _test_binop_value(self
, op
, rhs
):
182 r
, rv
= self
._binop
(op
, rhs
)
187 self
.assertEqual(r
, rv
)
189 # Tests that the binary operation `op`, when applied to `self._def`,
190 # does not change its underlying BT object address.
191 def _test_binop_lhs_addr_same(self
, op
, rhs
):
192 addr_before
= self
._def
.addr
193 r
, rv
= self
._binop
(op
, rhs
)
194 self
.assertEqual(self
._def
.addr
, addr_before
)
196 # Tests that the binary operation `op`, when applied to `self._def`,
197 # does not change its value.
198 def _test_binop_lhs_value_same(self
, op
, rhs
):
199 value_before
= self
._def
.__class
__(self
._def
)
200 r
, rv
= self
._binop
(op
, rhs
)
201 self
.assertEqual(self
._def
, value_before
)
203 # The methods below which take the `test_cb` and `op` parameters
204 # are meant to be used with one of the _test_binop_*() functions
205 # above as `test_cb` and a binary operator function as `op`.
209 # self._test_binop_rhs_pos_int(self._test_binop_value,
212 # This tests that a numeric value object added to a positive integer
213 # raw value gives a result with the expected value.
215 # `vint` and `vfloat` mean a signed integer value object and a real
218 def _test_binop_invalid_unknown(self
, op
):
219 if op
in _COMP_BINOPS
:
220 self
.skipTest('not testing')
222 with self
.assertRaises(TypeError):
223 op(self
._def
, object())
225 def _test_binop_invalid_none(self
, op
):
226 if op
in _COMP_BINOPS
:
227 self
.skipTest('not testing')
229 with self
.assertRaises(TypeError):
232 def _test_binop_rhs_false(self
, test_cb
, op
):
235 def _test_binop_rhs_true(self
, test_cb
, op
):
238 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
241 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
244 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
247 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
248 test_cb(op
, bt2
.create_value(2))
250 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
251 test_cb(op
, bt2
.create_value(-23))
253 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
254 test_cb(op
, bt2
.create_value(0))
256 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
259 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
262 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
265 def _test_binop_rhs_complex(self
, test_cb
, op
):
268 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
271 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
272 test_cb(op
, bt2
.create_value(2.2))
274 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
275 test_cb(op
, bt2
.create_value(-23.4))
277 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
278 test_cb(op
, bt2
.create_value(0.0))
280 def _test_binop_type_false(self
, op
):
281 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
283 def _test_binop_type_true(self
, op
):
284 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
286 def _test_binop_type_pos_int(self
, op
):
287 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
289 def _test_binop_type_neg_int(self
, op
):
290 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
292 def _test_binop_type_zero_int(self
, op
):
293 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
295 def _test_binop_type_pos_vint(self
, op
):
296 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
298 def _test_binop_type_neg_vint(self
, op
):
299 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
301 def _test_binop_type_zero_vint(self
, op
):
302 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
304 def _test_binop_type_pos_float(self
, op
):
305 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
307 def _test_binop_type_neg_float(self
, op
):
308 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
310 def _test_binop_type_zero_float(self
, op
):
311 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
313 def _test_binop_type_pos_vfloat(self
, op
):
314 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
316 def _test_binop_type_neg_vfloat(self
, op
):
317 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
319 def _test_binop_type_zero_vfloat(self
, op
):
320 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
322 def _test_binop_type_complex(self
, op
):
323 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
325 def _test_binop_type_zero_complex(self
, op
):
326 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
328 def _test_binop_value_false(self
, op
):
329 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
331 def _test_binop_value_true(self
, op
):
332 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
334 def _test_binop_value_pos_int(self
, op
):
335 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
337 def _test_binop_value_neg_int(self
, op
):
338 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
340 def _test_binop_value_zero_int(self
, op
):
341 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
343 def _test_binop_value_pos_vint(self
, op
):
344 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
346 def _test_binop_value_neg_vint(self
, op
):
347 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
349 def _test_binop_value_zero_vint(self
, op
):
350 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
352 def _test_binop_value_pos_float(self
, op
):
353 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
355 def _test_binop_value_neg_float(self
, op
):
356 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
358 def _test_binop_value_zero_float(self
, op
):
359 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
361 def _test_binop_value_pos_vfloat(self
, op
):
362 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
364 def _test_binop_value_neg_vfloat(self
, op
):
365 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
367 def _test_binop_value_zero_vfloat(self
, op
):
368 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
370 def _test_binop_value_complex(self
, op
):
371 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
373 def _test_binop_value_zero_complex(self
, op
):
374 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
376 def _test_binop_lhs_addr_same_false(self
, op
):
377 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
379 def _test_binop_lhs_addr_same_true(self
, op
):
380 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
382 def _test_binop_lhs_addr_same_pos_int(self
, op
):
383 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
385 def _test_binop_lhs_addr_same_neg_int(self
, op
):
386 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
388 def _test_binop_lhs_addr_same_zero_int(self
, op
):
389 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
391 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
392 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
394 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
395 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
397 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
398 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
400 def _test_binop_lhs_addr_same_pos_float(self
, op
):
401 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
403 def _test_binop_lhs_addr_same_neg_float(self
, op
):
404 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
406 def _test_binop_lhs_addr_same_zero_float(self
, op
):
407 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
409 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
410 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
412 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
413 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
415 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
416 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
418 def _test_binop_lhs_addr_same_complex(self
, op
):
419 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
421 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
422 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
424 def _test_binop_lhs_value_same_false(self
, op
):
425 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
427 def _test_binop_lhs_value_same_true(self
, op
):
428 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
430 def _test_binop_lhs_value_same_pos_int(self
, op
):
431 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
433 def _test_binop_lhs_value_same_neg_int(self
, op
):
434 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
436 def _test_binop_lhs_value_same_zero_int(self
, op
):
437 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
439 def _test_binop_lhs_value_same_pos_vint(self
, op
):
440 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
442 def _test_binop_lhs_value_same_neg_vint(self
, op
):
443 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
445 def _test_binop_lhs_value_same_zero_vint(self
, op
):
446 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
448 def _test_binop_lhs_value_same_pos_float(self
, op
):
449 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
451 def _test_binop_lhs_value_same_neg_float(self
, op
):
452 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
454 def _test_binop_lhs_value_same_zero_float(self
, op
):
455 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
457 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
458 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
460 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
461 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
463 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
464 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
466 def _test_binop_lhs_value_same_complex(self
, op
):
467 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
469 def _test_binop_lhs_value_same_zero_complex(self
, op
):
470 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
472 def test_bool_op(self
):
473 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
475 def test_int_op(self
):
476 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
478 def test_float_op(self
):
479 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
481 def test_complex_op(self
):
482 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
484 def test_str_op(self
):
485 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
487 def test_eq_none(self
):
488 self
.assertFalse(self
._def
== None)
490 def test_ne_none(self
):
491 self
.assertTrue(self
._def
!= None)
494 # This is a list of binary operators used for
495 # _inject_numeric_testing_methods().
497 # Each entry is a pair of binary operator name (used as part of the
498 # created testing method's name) and operator function.
506 ('add', operator
.add
),
507 ('radd', lambda a
, b
: operator
.add(b
, a
)),
508 ('and', operator
.and_
),
509 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
510 ('floordiv', operator
.floordiv
),
511 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
512 ('lshift', operator
.lshift
),
513 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
514 ('mod', operator
.mod
),
515 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
516 ('mul', operator
.mul
),
517 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
518 ('or', operator
.or_
),
519 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
520 ('pow', operator
.pow),
521 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
522 ('rshift', operator
.rshift
),
523 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
524 ('sub', operator
.sub
),
525 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
526 ('truediv', operator
.truediv
),
527 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
528 ('xor', operator
.xor
),
529 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
533 # This is a list of unary operators used for
534 # _inject_numeric_testing_methods().
536 # Each entry is a pair of unary operator name (used as part of the
537 # created testing method's name) and operator function.
539 ('neg', operator
.neg
),
540 ('pos', operator
.pos
),
541 ('abs', operator
.abs),
542 ('invert', operator
.invert
),
544 ('round_0', partial(round, ndigits
=0)),
545 ('round_1', partial(round, ndigits
=1)),
546 ('round_2', partial(round, ndigits
=2)),
547 ('round_3', partial(round, ndigits
=3)),
549 ('floor', math
.floor
),
550 ('trunc', math
.trunc
),
554 # This function injects a bunch of testing methods to a numeric
557 # It is meant to be used like this:
559 # _inject_numeric_testing_methods(MyNumericValueTestCase)
561 # This function injects:
563 # * One testing method for each _TestNumericValue._test_binop_*()
564 # method, for each binary operator in the _BINOPS tuple.
566 # * One testing method for each _TestNumericValue._test_unaryop*()
567 # method, for each unary operator in the _UNARYOPS tuple.
568 def _inject_numeric_testing_methods(cls
):
569 def test_binop_name(suffix
):
570 return 'test_binop_{}_{}'.format(name
, suffix
)
572 def test_unaryop_name(suffix
):
573 return 'test_unaryop_{}_{}'.format(name
, suffix
)
575 # inject testing methods for each binary operation
576 for name
, binop
in _BINOPS
:
577 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
))
578 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
))
579 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
))
580 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
))
581 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
))
582 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
))
583 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
))
584 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
))
585 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
586 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
587 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
588 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
589 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
590 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
591 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
))
592 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
))
593 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
))
594 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
))
595 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
596 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
597 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
598 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
599 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
))
600 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
))
601 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
))
602 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
))
603 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
))
604 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
))
605 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
606 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
607 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
608 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
609 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
610 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
611 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
))
612 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
613 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
))
614 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
615 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
616 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
617 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
618 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
619 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
))
620 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
621 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
))
622 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
623 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
624 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
625 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
626 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
627 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
))
628 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
629 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
))
630 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
631 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
632 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
633 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
634 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
635 setattr(cls
, test_binop_name('type_complex'), partialmethod(_TestNumericValue
._test
_binop
_type
_complex
, op
=binop
))
636 setattr(cls
, test_binop_name('type_zero_complex'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_complex
, op
=binop
))
637 setattr(cls
, test_binop_name('value_complex'), partialmethod(_TestNumericValue
._test
_binop
_value
_complex
, op
=binop
))
638 setattr(cls
, test_binop_name('value_zero_complex'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_complex
, op
=binop
))
639 setattr(cls
, test_binop_name('lhs_addr_same_complex'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
))
640 setattr(cls
, test_binop_name('lhs_addr_same_zero_complex'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
))
641 setattr(cls
, test_binop_name('lhs_value_same_complex'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
))
642 setattr(cls
, test_binop_name('lhs_value_same_zero_complex'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
))
644 # inject testing methods for each unary operation
645 for name
, unaryop
in _UNARYOPS
:
646 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
))
647 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
))
648 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
))
649 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
))
652 class CreateValueFuncTestCase(unittest
.TestCase
):
653 def test_create_none(self
):
654 v
= bt2
.create_value(None)
657 def test_create_bool_false(self
):
658 v
= bt2
.create_value(False)
659 self
.assertIsInstance(v
, bt2
.BoolValue
)
662 def test_create_bool_true(self
):
663 v
= bt2
.create_value(True)
664 self
.assertIsInstance(v
, bt2
.BoolValue
)
667 def test_create_int_pos(self
):
669 v
= bt2
.create_value(raw
)
670 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
671 self
.assertEqual(v
, raw
)
673 def test_create_int_neg(self
):
675 v
= bt2
.create_value(raw
)
676 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
677 self
.assertEqual(v
, raw
)
679 def test_create_float_pos(self
):
681 v
= bt2
.create_value(raw
)
682 self
.assertIsInstance(v
, bt2
.RealValue
)
683 self
.assertEqual(v
, raw
)
685 def test_create_float_neg(self
):
687 v
= bt2
.create_value(raw
)
688 self
.assertIsInstance(v
, bt2
.RealValue
)
689 self
.assertEqual(v
, raw
)
691 def test_create_string(self
):
693 v
= bt2
.create_value(raw
)
694 self
.assertIsInstance(v
, bt2
.StringValue
)
695 self
.assertEqual(v
, raw
)
697 def test_create_string_empty(self
):
699 v
= bt2
.create_value(raw
)
700 self
.assertIsInstance(v
, bt2
.StringValue
)
701 self
.assertEqual(v
, raw
)
703 def test_create_array_from_list(self
):
705 v
= bt2
.create_value(raw
)
706 self
.assertIsInstance(v
, bt2
.ArrayValue
)
707 self
.assertEqual(v
, raw
)
709 def test_create_array_from_tuple(self
):
711 v
= bt2
.create_value(raw
)
712 self
.assertIsInstance(v
, bt2
.ArrayValue
)
713 self
.assertEqual(v
, raw
)
715 def test_create_array_from_empty_list(self
):
717 v
= bt2
.create_value(raw
)
718 self
.assertIsInstance(v
, bt2
.ArrayValue
)
719 self
.assertEqual(v
, raw
)
721 def test_create_array_from_empty_tuple(self
):
723 v
= bt2
.create_value(raw
)
724 self
.assertIsInstance(v
, bt2
.ArrayValue
)
725 self
.assertEqual(v
, raw
)
727 def test_create_map(self
):
729 v
= bt2
.create_value(raw
)
730 self
.assertIsInstance(v
, bt2
.MapValue
)
731 self
.assertEqual(v
, raw
)
733 def test_create_map_empty(self
):
735 v
= bt2
.create_value(raw
)
736 self
.assertIsInstance(v
, bt2
.MapValue
)
737 self
.assertEqual(v
, raw
)
739 def test_create_vfalse(self
):
740 v
= bt2
.create_value(bt2
.create_value(False))
741 self
.assertIsInstance(v
, bt2
.BoolValue
)
744 def test_create_invalid(self
):
750 with self
.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm
:
751 v
= bt2
.create_value(a
)
754 class BoolValueTestCase(_TestNumericValue
, unittest
.TestCase
):
756 self
._f
= bt2
.BoolValue(False)
757 self
._t
= bt2
.BoolValue(True)
759 self
._def
_value
= False
760 self
._def
_new
_value
= True
767 def _assert_expecting_bool(self
):
768 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
770 def test_create_default(self
):
774 def test_create_false(self
):
775 self
.assertFalse(self
._f
)
777 def test_create_true(self
):
778 self
.assertTrue(self
._t
)
780 def test_create_from_vfalse(self
):
781 b
= bt2
.BoolValue(self
._f
)
784 def test_create_from_vtrue(self
):
785 b
= bt2
.BoolValue(self
._t
)
788 def test_create_from_int_non_zero(self
):
789 with self
.assertRaises(TypeError):
790 b
= bt2
.BoolValue(23)
792 def test_create_from_int_zero(self
):
793 with self
.assertRaises(TypeError):
796 def test_assign_true(self
):
801 def test_assign_false(self
):
806 def test_assign_vtrue(self
):
811 def test_assign_vfalse(self
):
816 def test_assign_int(self
):
817 with self
.assertRaises(TypeError):
821 def test_bool_op(self
):
822 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
824 def test_str_op(self
):
825 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
827 def test_eq_none(self
):
828 self
.assertFalse(self
._def
== None)
830 def test_ne_none(self
):
831 self
.assertTrue(self
._def
!= None)
833 def test_vfalse_eq_false(self
):
834 self
.assertEqual(self
._f
, False)
836 def test_vfalse_ne_true(self
):
837 self
.assertNotEqual(self
._f
, True)
839 def test_vtrue_eq_true(self
):
840 self
.assertEqual(self
._t
, True)
842 def test_vtrue_ne_false(self
):
843 self
.assertNotEqual(self
._t
, False)
846 _inject_numeric_testing_methods(BoolValueTestCase
)
849 class _TestIntegerValue(_TestNumericValue
):
852 self
._ip
= self
._CLS
(self
._pv
)
854 self
._def
_value
= self
._pv
855 self
._def
_new
_value
= 101
862 def _assert_expecting_int(self
):
863 return self
.assertRaisesRegex(TypeError, r
'expecting an integral number object')
865 def _assert_expecting_int64(self
):
866 return self
.assertRaisesRegex(ValueError, r
"expecting a signed 64-bit integral value")
868 def _assert_expecting_uint64(self
):
869 return self
.assertRaisesRegex(ValueError, r
"expecting an unsigned 64-bit integral value")
871 def test_create_default(self
):
873 self
.assertEqual(i
, 0)
875 def test_create_pos(self
):
876 self
.assertEqual(self
._ip
, self
._pv
)
878 def test_create_neg(self
):
879 self
.assertEqual(self
._in
, self
._nv
)
881 def test_create_from_vint(self
):
882 i
= self
._CLS
(self
._ip
)
883 self
.assertEqual(i
, self
._pv
)
885 def test_create_from_false(self
):
889 def test_create_from_true(self
):
893 def test_create_from_unknown(self
):
897 with self
._assert
_expecting
_int
():
900 def test_create_from_varray(self
):
901 with self
._assert
_expecting
_int
():
902 i
= self
._CLS
(bt2
.ArrayValue())
904 def test_assign_true(self
):
906 self
._def
.value
= raw
907 self
.assertEqual(self
._def
, raw
)
909 def test_assign_false(self
):
911 self
._def
.value
= raw
912 self
.assertEqual(self
._def
, raw
)
914 def test_assign_pos_int(self
):
916 self
._def
.value
= raw
917 self
.assertEqual(self
._def
, raw
)
919 def test_assign_vint(self
):
921 self
._def
.value
= bt2
.create_value(raw
)
922 self
.assertEqual(self
._def
, raw
)
925 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
926 _CLS
= bt2
.SignedIntegerValue
931 self
._in
= self
._CLS
(self
._nv
)
932 self
._def
_new
_value
= -101
938 def test_create_neg(self
):
939 self
.assertEqual(self
._in
, self
._nv
)
941 def test_create_pos_too_big(self
):
942 with self
._assert
_expecting
_int
64():
943 i
= self
._CLS
(2 ** 63)
945 def test_create_neg_too_big(self
):
946 with self
._assert
_expecting
_int
64():
947 i
= self
._CLS
(-(2 ** 63) - 1)
949 def test_assign_neg_int(self
):
951 self
._def
.value
= raw
952 self
.assertEqual(self
._def
, raw
)
954 def test_compare_big_int(self
):
955 # Larger than the IEEE 754 double-precision exact representation of
958 v
= bt2
.create_value(raw
)
959 self
.assertEqual(v
, raw
)
962 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
965 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
966 _CLS
= bt2
.UnsignedIntegerValue
968 def test_create_pos_too_big(self
):
969 with self
._assert
_expecting
_uint
64():
970 i
= self
._CLS
(2 ** 64)
972 def test_create_neg(self
):
973 with self
._assert
_expecting
_uint
64():
977 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
)
980 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
984 self
._fp
= bt2
.RealValue(self
._pv
)
985 self
._fn
= bt2
.RealValue(self
._nv
)
987 self
._def
_value
= self
._pv
988 self
._def
_new
_value
= -101.88
996 def _assert_expecting_float(self
):
997 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
999 def _test_invalid_op(self
, cb
):
1000 with self
.assertRaises(TypeError):
1003 def test_create_default(self
):
1005 self
.assertEqual(f
, 0.0)
1007 def test_create_pos(self
):
1008 self
.assertEqual(self
._fp
, self
._pv
)
1010 def test_create_neg(self
):
1011 self
.assertEqual(self
._fn
, self
._nv
)
1013 def test_create_from_vint(self
):
1014 f
= bt2
.RealValue(self
._fp
)
1015 self
.assertEqual(f
, self
._pv
)
1017 def test_create_from_false(self
):
1018 f
= bt2
.RealValue(False)
1021 def test_create_from_true(self
):
1022 f
= bt2
.RealValue(True)
1025 def test_create_from_int(self
):
1027 f
= bt2
.RealValue(raw
)
1028 self
.assertEqual(f
, float(raw
))
1030 def test_create_from_vint(self
):
1032 f
= bt2
.RealValue(bt2
.create_value(raw
))
1033 self
.assertEqual(f
, float(raw
))
1035 def test_create_from_vfloat(self
):
1037 f
= bt2
.RealValue(bt2
.create_value(raw
))
1038 self
.assertEqual(f
, raw
)
1040 def test_create_from_unknown(self
):
1044 with self
._assert
_expecting
_float
():
1045 f
= bt2
.RealValue(A())
1047 def test_create_from_varray(self
):
1048 with self
._assert
_expecting
_float
():
1049 f
= bt2
.RealValue(bt2
.ArrayValue())
1051 def test_assign_true(self
):
1052 self
._def
.value
= True
1053 self
.assertTrue(self
._def
)
1055 def test_assign_false(self
):
1056 self
._def
.value
= False
1057 self
.assertFalse(self
._def
)
1059 def test_assign_pos_int(self
):
1061 self
._def
.value
= raw
1062 self
.assertEqual(self
._def
, float(raw
))
1064 def test_assign_neg_int(self
):
1066 self
._def
.value
= raw
1067 self
.assertEqual(self
._def
, float(raw
))
1069 def test_assign_vint(self
):
1071 self
._def
.value
= bt2
.create_value(raw
)
1072 self
.assertEqual(self
._def
, float(raw
))
1074 def test_assign_float(self
):
1076 self
._def
.value
= raw
1077 self
.assertEqual(self
._def
, raw
)
1079 def test_assign_vfloat(self
):
1081 self
._def
.value
= bt2
.create_value(raw
)
1082 self
.assertEqual(self
._def
, raw
)
1084 def test_invalid_lshift(self
):
1085 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1087 def test_invalid_rshift(self
):
1088 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1090 def test_invalid_and(self
):
1091 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1093 def test_invalid_or(self
):
1094 self
._test
_invalid
_op
(lambda: self
._def |
23)
1096 def test_invalid_xor(self
):
1097 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1099 def test_invalid_invert(self
):
1100 self
._test
_invalid
_op
(lambda: ~self
._def
)
1103 _inject_numeric_testing_methods(RealValueTestCase
)
1106 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1108 self
._def
_value
= 'Hello, World!'
1109 self
._def
= bt2
.StringValue(self
._def
_value
)
1110 self
._def
_new
_value
= 'Yes!'
1115 def _assert_expecting_str(self
):
1116 return self
.assertRaises(TypeError)
1118 def test_create_default(self
):
1119 s
= bt2
.StringValue()
1120 self
.assertEqual(s
, '')
1122 def test_create_from_str(self
):
1124 s
= bt2
.StringValue(raw
)
1125 self
.assertEqual(s
, raw
)
1127 def test_create_from_vstr(self
):
1129 s
= bt2
.StringValue(bt2
.create_value(raw
))
1130 self
.assertEqual(s
, raw
)
1132 def test_create_from_unknown(self
):
1136 with self
._assert
_expecting
_str
():
1137 i
= bt2
.StringValue(A())
1139 def test_create_from_varray(self
):
1140 with self
._assert
_expecting
_str
():
1141 i
= bt2
.StringValue(bt2
.ArrayValue())
1143 def test_assign_int(self
):
1144 with self
._assert
_expecting
_str
():
1145 self
._def
.value
= 283
1147 def test_assign_str(self
):
1150 self
.assertEqual(self
._def
, raw
)
1152 def test_assign_vstr(self
):
1154 self
._def
= bt2
.create_value(raw
)
1155 self
.assertEqual(self
._def
, raw
)
1158 self
.assertEqual(self
._def
, self
._def
_value
)
1161 self
.assertNotEqual(self
._def
, 23)
1163 def test_lt_vstring(self
):
1164 s1
= bt2
.StringValue('allo')
1165 s2
= bt2
.StringValue('bateau')
1166 self
.assertLess(s1
, s2
)
1168 def test_lt_string(self
):
1169 s1
= bt2
.StringValue('allo')
1170 self
.assertLess(s1
, 'bateau')
1172 def test_le_vstring(self
):
1173 s1
= bt2
.StringValue('allo')
1174 s2
= bt2
.StringValue('bateau')
1175 self
.assertLessEqual(s1
, s2
)
1177 def test_le_string(self
):
1178 s1
= bt2
.StringValue('allo')
1179 self
.assertLessEqual(s1
, 'bateau')
1181 def test_gt_vstring(self
):
1182 s1
= bt2
.StringValue('allo')
1183 s2
= bt2
.StringValue('bateau')
1184 self
.assertGreater(s2
, s1
)
1186 def test_gt_string(self
):
1187 s1
= bt2
.StringValue('allo')
1188 self
.assertGreater('bateau', s1
)
1190 def test_ge_vstring(self
):
1191 s1
= bt2
.StringValue('allo')
1192 s2
= bt2
.StringValue('bateau')
1193 self
.assertGreaterEqual(s2
, s1
)
1195 def test_ge_string(self
):
1196 s1
= bt2
.StringValue('allo')
1197 self
.assertGreaterEqual('bateau', s1
)
1199 def test_bool_op(self
):
1200 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1202 def test_str_op(self
):
1203 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1206 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1208 def test_getitem(self
):
1209 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1211 def test_append_str(self
):
1212 to_append
= 'meow meow meow'
1213 self
._def
+= to_append
1214 self
._def
_value
+= to_append
1215 self
.assertEqual(self
._def
, self
._def
_value
)
1217 def test_append_vstr(self
):
1218 to_append
= 'meow meow meow'
1219 self
._def
+= bt2
.create_value(to_append
)
1220 self
._def
_value
+= to_append
1221 self
.assertEqual(self
._def
, self
._def
_value
)
1224 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1226 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1227 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1232 def _modify_def(self
):
1233 self
._def
[2] = 'xyz'
1235 def _assert_type_error(self
):
1236 return self
.assertRaises(TypeError)
1238 def test_create_default(self
):
1239 a
= bt2
.ArrayValue()
1240 self
.assertEqual(len(a
), 0)
1242 def test_create_from_array(self
):
1243 self
.assertEqual(self
._def
, self
._def
_value
)
1245 def test_create_from_tuple(self
):
1246 t
= 1, 2, False, None
1247 a
= bt2
.ArrayValue(t
)
1248 self
.assertEqual(a
, t
)
1250 def test_create_from_varray(self
):
1251 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1252 a
= bt2
.ArrayValue(va
)
1253 self
.assertEqual(va
, a
)
1255 def test_create_from_unknown(self
):
1259 with self
._assert
_type
_error
():
1260 a
= bt2
.ArrayValue(A())
1262 def test_bool_op_true(self
):
1263 self
.assertTrue(bool(self
._def
))
1265 def test_bool_op_false(self
):
1266 self
.assertFalse(bool(bt2
.ArrayValue()))
1269 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1271 def test_eq_int(self
):
1272 self
.assertNotEqual(self
._def
, 23)
1274 def test_eq_diff_len(self
):
1275 a1
= bt2
.create_value([1, 2, 3])
1276 a2
= bt2
.create_value([1, 2])
1277 self
.assertNotEqual(a1
, a2
)
1279 def test_eq_diff_content_same_len(self
):
1280 a1
= bt2
.create_value([1, 2, 3])
1281 a2
= bt2
.create_value([4, 5, 6])
1282 self
.assertNotEqual(a1
, a2
)
1284 def test_eq_same_content_same_len(self
):
1285 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1286 a1
= bt2
.ArrayValue(raw
)
1287 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1288 self
.assertEqual(a1
, a2
)
1290 def test_setitem_int(self
):
1293 self
.assertEqual(self
._def
[2], raw
)
1295 def test_setitem_vint(self
):
1297 self
._def
[2] = bt2
.create_value(raw
)
1298 self
.assertEqual(self
._def
[2], raw
)
1300 def test_setitem_none(self
):
1302 self
.assertIsNone(self
._def
[2])
1304 def test_setitem_index_wrong_type(self
):
1305 with self
._assert
_type
_error
():
1306 self
._def
['yes'] = 23
1308 def test_setitem_index_neg(self
):
1309 with self
.assertRaises(IndexError):
1312 def test_setitem_index_out_of_range(self
):
1313 with self
.assertRaises(IndexError):
1314 self
._def
[len(self
._def
)] = 23
1316 def test_append_none(self
):
1317 self
._def
.append(None)
1318 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1320 def test_append_int(self
):
1322 self
._def
.append(raw
)
1323 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1325 def test_append_vint(self
):
1327 self
._def
.append(bt2
.create_value(raw
))
1328 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1330 def test_append_unknown(self
):
1334 with self
._assert
_type
_error
():
1335 self
._def
.append(A())
1337 def test_iadd(self
):
1340 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1341 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1342 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1344 def test_iadd_unknown(self
):
1348 with self
._assert
_type
_error
():
1351 def test_iadd_list_unknown(self
):
1355 with self
._assert
_type
_error
():
1358 def test_iter(self
):
1359 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1360 self
.assertEqual(velem
, elem
)
1363 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1376 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1381 def _modify_def(self
):
1382 self
._def
['zero'] = 1
1384 def test_create_default(self
):
1386 self
.assertEqual(len(m
), 0)
1388 def test_create_from_dict(self
):
1389 self
.assertEqual(self
._def
, self
._def
_value
)
1391 def test_create_from_vmap(self
):
1392 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1393 m
= bt2
.MapValue(vm
)
1394 self
.assertEqual(vm
, m
)
1396 def test_create_from_unknown(self
):
1400 with self
.assertRaises(AttributeError):
1401 m
= bt2
.MapValue(A())
1403 def test_bool_op_true(self
):
1404 self
.assertTrue(bool(self
._def
))
1406 def test_bool_op_false(self
):
1407 self
.assertFalse(bool(bt2
.MapValue()))
1410 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1412 def test_eq_int(self
):
1413 self
.assertNotEqual(self
._def
, 23)
1415 def test_eq_diff_len(self
):
1416 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1417 a2
= bt2
.create_value({'a': 1, 'b': 2})
1418 self
.assertNotEqual(a1
, a2
)
1420 def test_eq_diff_content_same_len(self
):
1421 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1422 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1423 self
.assertNotEqual(a1
, a2
)
1425 def test_eq_same_content_diff_keys(self
):
1426 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1427 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1428 self
.assertNotEqual(a1
, a2
)
1430 def test_eq_same_content_same_len(self
):
1434 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]
1436 a1
= bt2
.MapValue(raw
)
1437 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1438 self
.assertEqual(a1
, a2
)
1439 self
.assertEqual(a1
, raw
)
1441 def test_setitem_int(self
):
1443 self
._def
['pos-int'] = raw
1444 self
.assertEqual(self
._def
['pos-int'], raw
)
1446 def test_setitem_vint(self
):
1448 self
._def
['pos-int'] = bt2
.create_value(raw
)
1449 self
.assertEqual(self
._def
['pos-int'], raw
)
1451 def test_setitem_none(self
):
1452 self
._def
['none'] = None
1453 self
.assertIsNone(self
._def
['none'])
1455 def test_setitem_new_int(self
):
1456 old_len
= len(self
._def
)
1457 self
._def
['new-int'] = 23
1458 self
.assertEqual(self
._def
['new-int'], 23)
1459 self
.assertEqual(len(self
._def
), old_len
+ 1)
1461 def test_setitem_index_wrong_type(self
):
1462 with self
.assertRaises(TypeError):
1465 def test_iter(self
):
1466 for vkey
, vval
in self
._def
.items():
1467 val
= self
._def
_value
[vkey
]
1468 self
.assertEqual(vval
, val
)
1470 def test_getitem_wrong_key(self
):
1471 with self
.assertRaises(KeyError):
1472 self
._def
['kilojoule']