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
29 # The value object classes explicitly do not implement the copy methods,
30 # raising `NotImplementedError`, just in case we decide to implement
32 class _TestCopySimple
:
34 with self
.assertRaises(NotImplementedError):
37 def test_deepcopy(self
):
38 with self
.assertRaises(NotImplementedError):
39 copy
.deepcopy(self
._def
)
48 # Base class for numeric value test cases.
50 # To be compatible with this base class, a derived class must, in its
53 # * Set `self._def` to a value object with an arbitrary raw value.
54 # * Set `self._def_value` to the equivalent raw value of `self._def`.
55 class _TestNumericValue(_TestCopySimple
):
56 # Tries the binary operation `op`:
58 # 1. Between `self._def`, which is a value object, and `rhs`.
59 # 2. Between `self._def_value`, which is the raw value of
60 # `self._def`, and `rhs`.
62 # Returns the results of 1. and 2.
64 # If there's an exception while performing 1. or 2., asserts that
65 # both operations raised exceptions, that both exceptions have the
66 # same type, and returns `None` for both results.
67 def _binop(self
, op
, rhs
):
72 # try with value object
74 r
= op(self
._def
, rhs
)
75 except Exception as e
:
80 rv
= op(self
._def
_value
, comp_value
)
81 except Exception as e
:
84 if type_rexc
is not None or type_rvexc
is not None:
85 # at least one of the operations raised an exception: in
86 # this case both operations should have raised the same
87 # type of exception (division by zero, bit shift with a
88 # floating point number operand, etc.)
89 self
.assertIs(type_rexc
, type_rvexc
)
94 # Tries the unary operation `op`:
96 # 1. On `self._def`, which is a value object.
97 # 2. On `self._def_value`, which is the raw value of `self._def`.
99 # Returns the results of 1. and 2.
101 # If there's an exception while performing 1. or 2., asserts that
102 # both operations raised exceptions, that both exceptions have the
103 # same type, and returns `None` for both results.
104 def _unaryop(self
, op
):
108 # try with value object
111 except Exception as e
:
116 rv
= op(self
._def
_value
)
117 except Exception as e
:
120 if type_rexc
is not None or type_rvexc
is not None:
121 # at least one of the operations raised an exception: in
122 # this case both operations should have raised the same
123 # type of exception (division by zero, bit shift with a
124 # floating point number operand, etc.)
125 self
.assertIs(type_rexc
, type_rvexc
)
130 # Tests that the unary operation `op` gives results with the same
131 # type for both `self._def` and `self._def_value`.
132 def _test_unaryop_type(self
, op
):
133 r
, rv
= self
._unaryop
(op
)
138 self
.assertIsInstance(r
, type(rv
))
140 # Tests that the unary operation `op` gives results with the same
141 # value for both `self._def` and `self._def_value`. This uses the
142 # __eq__() operator of `self._def`.
143 def _test_unaryop_value(self
, op
):
144 r
, rv
= self
._unaryop
(op
)
149 self
.assertEqual(r
, rv
)
151 # Tests that the unary operation `op`, when applied to `self._def`,
152 # does not change its underlying BT object address.
153 def _test_unaryop_addr_same(self
, op
):
154 addr_before
= self
._def
.addr
156 self
.assertEqual(self
._def
.addr
, addr_before
)
158 # Tests that the unary operation `op`, when applied to `self._def`,
159 # does not change its value.
160 def _test_unaryop_value_same(self
, op
):
161 value_before
= self
._def
.__class
__(self
._def
)
163 self
.assertEqual(self
._def
, value_before
)
165 # Tests that the binary operation `op` gives results with the same
166 # type for both `self._def` and `self._def_value`.
167 def _test_binop_type(self
, op
, rhs
):
168 r
, rv
= self
._binop
(op
, rhs
)
173 if op
in _COMP_BINOPS
:
174 # __eq__() and __ne__() always return a 'bool' object
175 self
.assertIsInstance(r
, bool)
177 self
.assertIsInstance(r
, type(rv
))
179 # Tests that the binary operation `op` gives results with the same
180 # value for both `self._def` and `self._def_value`. This uses the
181 # __eq__() operator of `self._def`.
182 def _test_binop_value(self
, op
, rhs
):
183 r
, rv
= self
._binop
(op
, rhs
)
188 self
.assertEqual(r
, rv
)
190 # Tests that the binary operation `op`, when applied to `self._def`,
191 # does not change its underlying BT object address.
192 def _test_binop_lhs_addr_same(self
, op
, rhs
):
193 addr_before
= self
._def
.addr
194 r
, rv
= self
._binop
(op
, rhs
)
195 self
.assertEqual(self
._def
.addr
, addr_before
)
197 # Tests that the binary operation `op`, when applied to `self._def`,
198 # does not change its value.
199 def _test_binop_lhs_value_same(self
, op
, rhs
):
200 value_before
= self
._def
.__class
__(self
._def
)
201 r
, rv
= self
._binop
(op
, rhs
)
202 self
.assertEqual(self
._def
, value_before
)
204 # The methods below which take the `test_cb` and `op` parameters
205 # are meant to be used with one of the _test_binop_*() functions
206 # above as `test_cb` and a binary operator function as `op`.
210 # self._test_binop_rhs_pos_int(self._test_binop_value,
213 # This tests that a numeric value object added to a positive integer
214 # raw value gives a result with the expected value.
216 # `vint` and `vfloat` mean a signed integer value object and a real
219 def _test_binop_invalid_unknown(self
, op
):
220 if op
in _COMP_BINOPS
:
221 self
.skipTest('not testing')
223 with self
.assertRaises(TypeError):
224 op(self
._def
, object())
226 def _test_binop_invalid_none(self
, op
):
227 if op
in _COMP_BINOPS
:
228 self
.skipTest('not testing')
230 with self
.assertRaises(TypeError):
233 def _test_binop_rhs_false(self
, test_cb
, op
):
236 def _test_binop_rhs_true(self
, test_cb
, op
):
239 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
242 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
245 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
248 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
249 test_cb(op
, bt2
.create_value(2))
251 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
252 test_cb(op
, bt2
.create_value(-23))
254 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
255 test_cb(op
, bt2
.create_value(0))
257 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
260 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
263 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
266 def _test_binop_rhs_complex(self
, test_cb
, op
):
269 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
272 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
273 test_cb(op
, bt2
.create_value(2.2))
275 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
276 test_cb(op
, bt2
.create_value(-23.4))
278 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
279 test_cb(op
, bt2
.create_value(0.0))
281 def _test_binop_type_false(self
, op
):
282 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
284 def _test_binop_type_true(self
, op
):
285 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
287 def _test_binop_type_pos_int(self
, op
):
288 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
290 def _test_binop_type_neg_int(self
, op
):
291 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
293 def _test_binop_type_zero_int(self
, op
):
294 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
296 def _test_binop_type_pos_vint(self
, op
):
297 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
299 def _test_binop_type_neg_vint(self
, op
):
300 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
302 def _test_binop_type_zero_vint(self
, op
):
303 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
305 def _test_binop_type_pos_float(self
, op
):
306 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
308 def _test_binop_type_neg_float(self
, op
):
309 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
311 def _test_binop_type_zero_float(self
, op
):
312 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
314 def _test_binop_type_pos_vfloat(self
, op
):
315 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
317 def _test_binop_type_neg_vfloat(self
, op
):
318 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
320 def _test_binop_type_zero_vfloat(self
, op
):
321 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
323 def _test_binop_type_complex(self
, op
):
324 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
326 def _test_binop_type_zero_complex(self
, op
):
327 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
329 def _test_binop_value_false(self
, op
):
330 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
332 def _test_binop_value_true(self
, op
):
333 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
335 def _test_binop_value_pos_int(self
, op
):
336 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
338 def _test_binop_value_neg_int(self
, op
):
339 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
341 def _test_binop_value_zero_int(self
, op
):
342 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
344 def _test_binop_value_pos_vint(self
, op
):
345 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
347 def _test_binop_value_neg_vint(self
, op
):
348 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
350 def _test_binop_value_zero_vint(self
, op
):
351 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
353 def _test_binop_value_pos_float(self
, op
):
354 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
356 def _test_binop_value_neg_float(self
, op
):
357 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
359 def _test_binop_value_zero_float(self
, op
):
360 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
362 def _test_binop_value_pos_vfloat(self
, op
):
363 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
365 def _test_binop_value_neg_vfloat(self
, op
):
366 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
368 def _test_binop_value_zero_vfloat(self
, op
):
369 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
371 def _test_binop_value_complex(self
, op
):
372 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
374 def _test_binop_value_zero_complex(self
, op
):
375 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
377 def _test_binop_lhs_addr_same_false(self
, op
):
378 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
380 def _test_binop_lhs_addr_same_true(self
, op
):
381 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
383 def _test_binop_lhs_addr_same_pos_int(self
, op
):
384 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
386 def _test_binop_lhs_addr_same_neg_int(self
, op
):
387 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
389 def _test_binop_lhs_addr_same_zero_int(self
, op
):
390 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
392 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
393 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
395 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
396 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
398 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
399 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
401 def _test_binop_lhs_addr_same_pos_float(self
, op
):
402 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
404 def _test_binop_lhs_addr_same_neg_float(self
, op
):
405 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
407 def _test_binop_lhs_addr_same_zero_float(self
, op
):
408 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
410 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
411 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
413 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
414 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
416 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
417 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
419 def _test_binop_lhs_addr_same_complex(self
, op
):
420 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
422 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
423 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
425 def _test_binop_lhs_value_same_false(self
, op
):
426 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
428 def _test_binop_lhs_value_same_true(self
, op
):
429 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
431 def _test_binop_lhs_value_same_pos_int(self
, op
):
432 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
434 def _test_binop_lhs_value_same_neg_int(self
, op
):
435 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
437 def _test_binop_lhs_value_same_zero_int(self
, op
):
438 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
440 def _test_binop_lhs_value_same_pos_vint(self
, op
):
441 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
443 def _test_binop_lhs_value_same_neg_vint(self
, op
):
444 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
446 def _test_binop_lhs_value_same_zero_vint(self
, op
):
447 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
449 def _test_binop_lhs_value_same_pos_float(self
, op
):
450 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
452 def _test_binop_lhs_value_same_neg_float(self
, op
):
453 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
455 def _test_binop_lhs_value_same_zero_float(self
, op
):
456 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
458 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
459 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
461 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
462 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
464 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
465 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
467 def _test_binop_lhs_value_same_complex(self
, op
):
468 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
470 def _test_binop_lhs_value_same_zero_complex(self
, op
):
471 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
473 def test_bool_op(self
):
474 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
476 def test_int_op(self
):
477 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
479 def test_float_op(self
):
480 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
482 def test_complex_op(self
):
483 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
485 def test_str_op(self
):
486 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
488 def test_eq_none(self
):
489 self
.assertFalse(self
._def
== None)
491 def test_ne_none(self
):
492 self
.assertTrue(self
._def
!= None)
495 # This is a list of binary operators used for
496 # _inject_numeric_testing_methods().
498 # Each entry is a pair of binary operator name (used as part of the
499 # created testing method's name) and operator function.
507 ('add', operator
.add
),
508 ('radd', lambda a
, b
: operator
.add(b
, a
)),
509 ('and', operator
.and_
),
510 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
511 ('floordiv', operator
.floordiv
),
512 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
513 ('lshift', operator
.lshift
),
514 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
515 ('mod', operator
.mod
),
516 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
517 ('mul', operator
.mul
),
518 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
519 ('or', operator
.or_
),
520 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
521 ('pow', operator
.pow),
522 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
523 ('rshift', operator
.rshift
),
524 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
525 ('sub', operator
.sub
),
526 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
527 ('truediv', operator
.truediv
),
528 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
529 ('xor', operator
.xor
),
530 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
534 # This is a list of unary operators used for
535 # _inject_numeric_testing_methods().
537 # Each entry is a pair of unary operator name (used as part of the
538 # created testing method's name) and operator function.
540 ('neg', operator
.neg
),
541 ('pos', operator
.pos
),
542 ('abs', operator
.abs),
543 ('invert', operator
.invert
),
545 ('round_0', partial(round, ndigits
=0)),
546 ('round_1', partial(round, ndigits
=1)),
547 ('round_2', partial(round, ndigits
=2)),
548 ('round_3', partial(round, ndigits
=3)),
550 ('floor', math
.floor
),
551 ('trunc', math
.trunc
),
555 # This function injects a bunch of testing methods to a numeric
558 # It is meant to be used like this:
560 # _inject_numeric_testing_methods(MyNumericValueTestCase)
562 # This function injects:
564 # * One testing method for each _TestNumericValue._test_binop_*()
565 # method, for each binary operator in the _BINOPS tuple.
567 # * One testing method for each _TestNumericValue._test_unaryop*()
568 # method, for each unary operator in the _UNARYOPS tuple.
569 def _inject_numeric_testing_methods(cls
):
570 def test_binop_name(suffix
):
571 return 'test_binop_{}_{}'.format(name
, suffix
)
573 def test_unaryop_name(suffix
):
574 return 'test_unaryop_{}_{}'.format(name
, suffix
)
576 # inject testing methods for each binary operation
577 for name
, binop
in _BINOPS
:
578 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
))
579 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
))
580 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
))
581 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
))
582 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
))
583 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
))
584 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
))
585 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
))
586 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
587 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
588 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
589 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
590 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
591 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
592 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
))
593 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
))
594 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
))
595 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
))
596 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
597 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
598 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
599 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
600 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
))
601 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
))
602 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
))
603 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
))
604 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
))
605 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
))
606 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
607 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
608 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
609 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
610 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
611 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
612 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
))
613 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
614 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
))
615 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
616 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
617 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
618 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
619 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
620 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
))
621 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
622 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
))
623 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
624 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
625 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
626 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
627 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
628 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
))
629 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
630 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
))
631 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
632 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
633 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
634 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
635 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
636 setattr(cls
, test_binop_name('type_complex'), partialmethod(_TestNumericValue
._test
_binop
_type
_complex
, op
=binop
))
637 setattr(cls
, test_binop_name('type_zero_complex'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_complex
, op
=binop
))
638 setattr(cls
, test_binop_name('value_complex'), partialmethod(_TestNumericValue
._test
_binop
_value
_complex
, op
=binop
))
639 setattr(cls
, test_binop_name('value_zero_complex'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_complex
, op
=binop
))
640 setattr(cls
, test_binop_name('lhs_addr_same_complex'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
))
641 setattr(cls
, test_binop_name('lhs_addr_same_zero_complex'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
))
642 setattr(cls
, test_binop_name('lhs_value_same_complex'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
))
643 setattr(cls
, test_binop_name('lhs_value_same_zero_complex'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
))
645 # inject testing methods for each unary operation
646 for name
, unaryop
in _UNARYOPS
:
647 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
))
648 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
))
649 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
))
650 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
))
653 class CreateValueFuncTestCase(unittest
.TestCase
):
654 def test_create_none(self
):
655 v
= bt2
.create_value(None)
658 def test_create_bool_false(self
):
659 v
= bt2
.create_value(False)
660 self
.assertIsInstance(v
, bt2
.BoolValue
)
663 def test_create_bool_true(self
):
664 v
= bt2
.create_value(True)
665 self
.assertIsInstance(v
, bt2
.BoolValue
)
668 def test_create_int_pos(self
):
670 v
= bt2
.create_value(raw
)
671 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
672 self
.assertEqual(v
, raw
)
674 def test_create_int_neg(self
):
676 v
= bt2
.create_value(raw
)
677 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
678 self
.assertEqual(v
, raw
)
680 def test_create_float_pos(self
):
682 v
= bt2
.create_value(raw
)
683 self
.assertIsInstance(v
, bt2
.RealValue
)
684 self
.assertEqual(v
, raw
)
686 def test_create_float_neg(self
):
688 v
= bt2
.create_value(raw
)
689 self
.assertIsInstance(v
, bt2
.RealValue
)
690 self
.assertEqual(v
, raw
)
692 def test_create_string(self
):
694 v
= bt2
.create_value(raw
)
695 self
.assertIsInstance(v
, bt2
.StringValue
)
696 self
.assertEqual(v
, raw
)
698 def test_create_string_empty(self
):
700 v
= bt2
.create_value(raw
)
701 self
.assertIsInstance(v
, bt2
.StringValue
)
702 self
.assertEqual(v
, raw
)
704 def test_create_array_from_list(self
):
706 v
= bt2
.create_value(raw
)
707 self
.assertIsInstance(v
, bt2
.ArrayValue
)
708 self
.assertEqual(v
, raw
)
710 def test_create_array_from_tuple(self
):
712 v
= bt2
.create_value(raw
)
713 self
.assertIsInstance(v
, bt2
.ArrayValue
)
714 self
.assertEqual(v
, raw
)
716 def test_create_array_from_empty_list(self
):
718 v
= bt2
.create_value(raw
)
719 self
.assertIsInstance(v
, bt2
.ArrayValue
)
720 self
.assertEqual(v
, raw
)
722 def test_create_array_from_empty_tuple(self
):
724 v
= bt2
.create_value(raw
)
725 self
.assertIsInstance(v
, bt2
.ArrayValue
)
726 self
.assertEqual(v
, raw
)
728 def test_create_map(self
):
730 v
= bt2
.create_value(raw
)
731 self
.assertIsInstance(v
, bt2
.MapValue
)
732 self
.assertEqual(v
, raw
)
734 def test_create_map_empty(self
):
736 v
= bt2
.create_value(raw
)
737 self
.assertIsInstance(v
, bt2
.MapValue
)
738 self
.assertEqual(v
, raw
)
740 def test_create_vfalse(self
):
741 v
= bt2
.create_value(bt2
.create_value(False))
742 self
.assertIsInstance(v
, bt2
.BoolValue
)
745 def test_create_invalid(self
):
751 with self
.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm
:
752 v
= bt2
.create_value(a
)
755 class BoolValueTestCase(_TestNumericValue
, unittest
.TestCase
):
757 self
._f
= bt2
.BoolValue(False)
758 self
._t
= bt2
.BoolValue(True)
760 self
._def
_value
= False
761 self
._def
_new
_value
= True
768 def _assert_expecting_bool(self
):
769 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
771 def test_create_default(self
):
775 def test_create_false(self
):
776 self
.assertFalse(self
._f
)
778 def test_create_true(self
):
779 self
.assertTrue(self
._t
)
781 def test_create_from_vfalse(self
):
782 b
= bt2
.BoolValue(self
._f
)
785 def test_create_from_vtrue(self
):
786 b
= bt2
.BoolValue(self
._t
)
789 def test_create_from_int_non_zero(self
):
790 with self
.assertRaises(TypeError):
791 b
= bt2
.BoolValue(23)
793 def test_create_from_int_zero(self
):
794 with self
.assertRaises(TypeError):
797 def test_assign_true(self
):
802 def test_assign_false(self
):
807 def test_assign_vtrue(self
):
812 def test_assign_vfalse(self
):
817 def test_assign_int(self
):
818 with self
.assertRaises(TypeError):
822 def test_bool_op(self
):
823 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
825 def test_str_op(self
):
826 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
828 def test_eq_none(self
):
829 self
.assertFalse(self
._def
== None)
831 def test_ne_none(self
):
832 self
.assertTrue(self
._def
!= None)
834 def test_vfalse_eq_false(self
):
835 self
.assertEqual(self
._f
, False)
837 def test_vfalse_ne_true(self
):
838 self
.assertNotEqual(self
._f
, True)
840 def test_vtrue_eq_true(self
):
841 self
.assertEqual(self
._t
, True)
843 def test_vtrue_ne_false(self
):
844 self
.assertNotEqual(self
._t
, False)
847 _inject_numeric_testing_methods(BoolValueTestCase
)
850 class _TestIntegerValue(_TestNumericValue
):
853 self
._ip
= self
._CLS
(self
._pv
)
855 self
._def
_value
= self
._pv
856 self
._def
_new
_value
= 101
863 def _assert_expecting_int(self
):
864 return self
.assertRaisesRegex(TypeError, r
'expecting an integral number object')
866 def _assert_expecting_int64(self
):
867 return self
.assertRaisesRegex(ValueError, r
"expecting a signed 64-bit integral value")
869 def _assert_expecting_uint64(self
):
870 return self
.assertRaisesRegex(ValueError, r
"expecting an unsigned 64-bit integral value")
872 def test_create_default(self
):
874 self
.assertEqual(i
, 0)
876 def test_create_pos(self
):
877 self
.assertEqual(self
._ip
, self
._pv
)
879 def test_create_neg(self
):
880 self
.assertEqual(self
._in
, self
._nv
)
882 def test_create_from_vint(self
):
883 i
= self
._CLS
(self
._ip
)
884 self
.assertEqual(i
, self
._pv
)
886 def test_create_from_false(self
):
890 def test_create_from_true(self
):
894 def test_create_from_unknown(self
):
898 with self
._assert
_expecting
_int
():
901 def test_create_from_varray(self
):
902 with self
._assert
_expecting
_int
():
903 i
= self
._CLS
(bt2
.ArrayValue())
905 def test_assign_true(self
):
907 self
._def
.value
= raw
908 self
.assertEqual(self
._def
, raw
)
910 def test_assign_false(self
):
912 self
._def
.value
= raw
913 self
.assertEqual(self
._def
, raw
)
915 def test_assign_pos_int(self
):
917 self
._def
.value
= raw
918 self
.assertEqual(self
._def
, raw
)
920 def test_assign_vint(self
):
922 self
._def
.value
= bt2
.create_value(raw
)
923 self
.assertEqual(self
._def
, raw
)
926 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
927 _CLS
= bt2
.SignedIntegerValue
932 self
._in
= self
._CLS
(self
._nv
)
933 self
._def
_new
_value
= -101
939 def test_create_neg(self
):
940 self
.assertEqual(self
._in
, self
._nv
)
942 def test_create_pos_too_big(self
):
943 with self
._assert
_expecting
_int
64():
944 i
= self
._CLS
(2 ** 63)
946 def test_create_neg_too_big(self
):
947 with self
._assert
_expecting
_int
64():
948 i
= self
._CLS
(-(2 ** 63) - 1)
950 def test_assign_neg_int(self
):
952 self
._def
.value
= raw
953 self
.assertEqual(self
._def
, raw
)
955 def test_compare_big_int(self
):
956 # Larger than the IEEE 754 double-precision exact representation of
959 v
= bt2
.create_value(raw
)
960 self
.assertEqual(v
, raw
)
963 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
966 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
967 _CLS
= bt2
.UnsignedIntegerValue
969 def test_create_pos_too_big(self
):
970 with self
._assert
_expecting
_uint
64():
971 i
= self
._CLS
(2 ** 64)
973 def test_create_neg(self
):
974 with self
._assert
_expecting
_uint
64():
978 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
)
981 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
985 self
._fp
= bt2
.RealValue(self
._pv
)
986 self
._fn
= bt2
.RealValue(self
._nv
)
988 self
._def
_value
= self
._pv
989 self
._def
_new
_value
= -101.88
997 def _assert_expecting_float(self
):
998 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
1000 def _test_invalid_op(self
, cb
):
1001 with self
.assertRaises(TypeError):
1004 def test_create_default(self
):
1006 self
.assertEqual(f
, 0.0)
1008 def test_create_pos(self
):
1009 self
.assertEqual(self
._fp
, self
._pv
)
1011 def test_create_neg(self
):
1012 self
.assertEqual(self
._fn
, self
._nv
)
1014 def test_create_from_vint(self
):
1015 f
= bt2
.RealValue(self
._fp
)
1016 self
.assertEqual(f
, self
._pv
)
1018 def test_create_from_false(self
):
1019 f
= bt2
.RealValue(False)
1022 def test_create_from_true(self
):
1023 f
= bt2
.RealValue(True)
1026 def test_create_from_int(self
):
1028 f
= bt2
.RealValue(raw
)
1029 self
.assertEqual(f
, float(raw
))
1031 def test_create_from_vint(self
):
1033 f
= bt2
.RealValue(bt2
.create_value(raw
))
1034 self
.assertEqual(f
, float(raw
))
1036 def test_create_from_vfloat(self
):
1038 f
= bt2
.RealValue(bt2
.create_value(raw
))
1039 self
.assertEqual(f
, raw
)
1041 def test_create_from_unknown(self
):
1045 with self
._assert
_expecting
_float
():
1046 f
= bt2
.RealValue(A())
1048 def test_create_from_varray(self
):
1049 with self
._assert
_expecting
_float
():
1050 f
= bt2
.RealValue(bt2
.ArrayValue())
1052 def test_assign_true(self
):
1053 self
._def
.value
= True
1054 self
.assertTrue(self
._def
)
1056 def test_assign_false(self
):
1057 self
._def
.value
= False
1058 self
.assertFalse(self
._def
)
1060 def test_assign_pos_int(self
):
1062 self
._def
.value
= raw
1063 self
.assertEqual(self
._def
, float(raw
))
1065 def test_assign_neg_int(self
):
1067 self
._def
.value
= raw
1068 self
.assertEqual(self
._def
, float(raw
))
1070 def test_assign_vint(self
):
1072 self
._def
.value
= bt2
.create_value(raw
)
1073 self
.assertEqual(self
._def
, float(raw
))
1075 def test_assign_float(self
):
1077 self
._def
.value
= raw
1078 self
.assertEqual(self
._def
, raw
)
1080 def test_assign_vfloat(self
):
1082 self
._def
.value
= bt2
.create_value(raw
)
1083 self
.assertEqual(self
._def
, raw
)
1085 def test_invalid_lshift(self
):
1086 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1088 def test_invalid_rshift(self
):
1089 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1091 def test_invalid_and(self
):
1092 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1094 def test_invalid_or(self
):
1095 self
._test
_invalid
_op
(lambda: self
._def |
23)
1097 def test_invalid_xor(self
):
1098 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1100 def test_invalid_invert(self
):
1101 self
._test
_invalid
_op
(lambda: ~self
._def
)
1104 _inject_numeric_testing_methods(RealValueTestCase
)
1107 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1109 self
._def
_value
= 'Hello, World!'
1110 self
._def
= bt2
.StringValue(self
._def
_value
)
1111 self
._def
_new
_value
= 'Yes!'
1116 def _assert_expecting_str(self
):
1117 return self
.assertRaises(TypeError)
1119 def test_create_default(self
):
1120 s
= bt2
.StringValue()
1121 self
.assertEqual(s
, '')
1123 def test_create_from_str(self
):
1125 s
= bt2
.StringValue(raw
)
1126 self
.assertEqual(s
, raw
)
1128 def test_create_from_vstr(self
):
1130 s
= bt2
.StringValue(bt2
.create_value(raw
))
1131 self
.assertEqual(s
, raw
)
1133 def test_create_from_unknown(self
):
1137 with self
._assert
_expecting
_str
():
1138 i
= bt2
.StringValue(A())
1140 def test_create_from_varray(self
):
1141 with self
._assert
_expecting
_str
():
1142 i
= bt2
.StringValue(bt2
.ArrayValue())
1144 def test_assign_int(self
):
1145 with self
._assert
_expecting
_str
():
1146 self
._def
.value
= 283
1148 def test_assign_str(self
):
1151 self
.assertEqual(self
._def
, raw
)
1153 def test_assign_vstr(self
):
1155 self
._def
= bt2
.create_value(raw
)
1156 self
.assertEqual(self
._def
, raw
)
1159 self
.assertEqual(self
._def
, self
._def
_value
)
1162 self
.assertNotEqual(self
._def
, 23)
1164 def test_lt_vstring(self
):
1165 s1
= bt2
.StringValue('allo')
1166 s2
= bt2
.StringValue('bateau')
1167 self
.assertLess(s1
, s2
)
1169 def test_lt_string(self
):
1170 s1
= bt2
.StringValue('allo')
1171 self
.assertLess(s1
, 'bateau')
1173 def test_le_vstring(self
):
1174 s1
= bt2
.StringValue('allo')
1175 s2
= bt2
.StringValue('bateau')
1176 self
.assertLessEqual(s1
, s2
)
1178 def test_le_string(self
):
1179 s1
= bt2
.StringValue('allo')
1180 self
.assertLessEqual(s1
, 'bateau')
1182 def test_gt_vstring(self
):
1183 s1
= bt2
.StringValue('allo')
1184 s2
= bt2
.StringValue('bateau')
1185 self
.assertGreater(s2
, s1
)
1187 def test_gt_string(self
):
1188 s1
= bt2
.StringValue('allo')
1189 self
.assertGreater('bateau', s1
)
1191 def test_ge_vstring(self
):
1192 s1
= bt2
.StringValue('allo')
1193 s2
= bt2
.StringValue('bateau')
1194 self
.assertGreaterEqual(s2
, s1
)
1196 def test_ge_string(self
):
1197 s1
= bt2
.StringValue('allo')
1198 self
.assertGreaterEqual('bateau', s1
)
1200 def test_bool_op(self
):
1201 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1203 def test_str_op(self
):
1204 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1207 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1209 def test_getitem(self
):
1210 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1212 def test_append_str(self
):
1213 to_append
= 'meow meow meow'
1214 self
._def
+= to_append
1215 self
._def
_value
+= to_append
1216 self
.assertEqual(self
._def
, self
._def
_value
)
1218 def test_append_vstr(self
):
1219 to_append
= 'meow meow meow'
1220 self
._def
+= bt2
.create_value(to_append
)
1221 self
._def
_value
+= to_append
1222 self
.assertEqual(self
._def
, self
._def
_value
)
1225 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1227 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1228 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1233 def _modify_def(self
):
1234 self
._def
[2] = 'xyz'
1236 def _assert_type_error(self
):
1237 return self
.assertRaises(TypeError)
1239 def test_create_default(self
):
1240 a
= bt2
.ArrayValue()
1241 self
.assertEqual(len(a
), 0)
1243 def test_create_from_array(self
):
1244 self
.assertEqual(self
._def
, self
._def
_value
)
1246 def test_create_from_tuple(self
):
1247 t
= 1, 2, False, None
1248 a
= bt2
.ArrayValue(t
)
1249 self
.assertEqual(a
, t
)
1251 def test_create_from_varray(self
):
1252 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1253 a
= bt2
.ArrayValue(va
)
1254 self
.assertEqual(va
, a
)
1256 def test_create_from_unknown(self
):
1260 with self
._assert
_type
_error
():
1261 a
= bt2
.ArrayValue(A())
1263 def test_bool_op_true(self
):
1264 self
.assertTrue(bool(self
._def
))
1266 def test_bool_op_false(self
):
1267 self
.assertFalse(bool(bt2
.ArrayValue()))
1270 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1272 def test_eq_int(self
):
1273 self
.assertNotEqual(self
._def
, 23)
1275 def test_eq_diff_len(self
):
1276 a1
= bt2
.create_value([1, 2, 3])
1277 a2
= bt2
.create_value([1, 2])
1278 self
.assertNotEqual(a1
, a2
)
1280 def test_eq_diff_content_same_len(self
):
1281 a1
= bt2
.create_value([1, 2, 3])
1282 a2
= bt2
.create_value([4, 5, 6])
1283 self
.assertNotEqual(a1
, a2
)
1285 def test_eq_same_content_same_len(self
):
1286 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1287 a1
= bt2
.ArrayValue(raw
)
1288 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1289 self
.assertEqual(a1
, a2
)
1291 def test_eq_non_sequence_iterable(self
):
1292 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1293 a
= bt2
.ArrayValue((1, 3, 5))
1294 self
.assertEqual(a
, list(dct
.keys()))
1295 self
.assertNotEqual(a
, dct
)
1297 def test_setitem_int(self
):
1300 self
.assertEqual(self
._def
[2], raw
)
1302 def test_setitem_vint(self
):
1304 self
._def
[2] = bt2
.create_value(raw
)
1305 self
.assertEqual(self
._def
[2], raw
)
1307 def test_setitem_none(self
):
1309 self
.assertIsNone(self
._def
[2])
1311 def test_setitem_index_wrong_type(self
):
1312 with self
._assert
_type
_error
():
1313 self
._def
['yes'] = 23
1315 def test_setitem_index_neg(self
):
1316 with self
.assertRaises(IndexError):
1319 def test_setitem_index_out_of_range(self
):
1320 with self
.assertRaises(IndexError):
1321 self
._def
[len(self
._def
)] = 23
1323 def test_append_none(self
):
1324 self
._def
.append(None)
1325 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1327 def test_append_int(self
):
1329 self
._def
.append(raw
)
1330 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1332 def test_append_vint(self
):
1334 self
._def
.append(bt2
.create_value(raw
))
1335 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1337 def test_append_unknown(self
):
1341 with self
._assert
_type
_error
():
1342 self
._def
.append(A())
1344 def test_iadd(self
):
1347 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1348 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1349 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1351 def test_iadd_unknown(self
):
1355 with self
._assert
_type
_error
():
1358 def test_iadd_list_unknown(self
):
1362 with self
._assert
_type
_error
():
1365 def test_iter(self
):
1366 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1367 self
.assertEqual(velem
, elem
)
1370 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1383 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1388 def _modify_def(self
):
1389 self
._def
['zero'] = 1
1391 def test_create_default(self
):
1393 self
.assertEqual(len(m
), 0)
1395 def test_create_from_dict(self
):
1396 self
.assertEqual(self
._def
, self
._def
_value
)
1398 def test_create_from_vmap(self
):
1399 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1400 m
= bt2
.MapValue(vm
)
1401 self
.assertEqual(vm
, m
)
1403 def test_create_from_unknown(self
):
1407 with self
.assertRaises(AttributeError):
1408 m
= bt2
.MapValue(A())
1410 def test_bool_op_true(self
):
1411 self
.assertTrue(bool(self
._def
))
1413 def test_bool_op_false(self
):
1414 self
.assertFalse(bool(bt2
.MapValue()))
1417 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1419 def test_eq_int(self
):
1420 self
.assertNotEqual(self
._def
, 23)
1422 def test_eq_diff_len(self
):
1423 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1424 a2
= bt2
.create_value({'a': 1, 'b': 2})
1425 self
.assertNotEqual(a1
, a2
)
1427 def test_eq_diff_content_same_len(self
):
1428 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1429 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1430 self
.assertNotEqual(a1
, a2
)
1432 def test_eq_same_content_diff_keys(self
):
1433 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1434 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1435 self
.assertNotEqual(a1
, a2
)
1437 def test_eq_same_content_same_len(self
):
1441 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]
1443 a1
= bt2
.MapValue(raw
)
1444 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1445 self
.assertEqual(a1
, a2
)
1446 self
.assertEqual(a1
, raw
)
1448 def test_setitem_int(self
):
1450 self
._def
['pos-int'] = raw
1451 self
.assertEqual(self
._def
['pos-int'], raw
)
1453 def test_setitem_vint(self
):
1455 self
._def
['pos-int'] = bt2
.create_value(raw
)
1456 self
.assertEqual(self
._def
['pos-int'], raw
)
1458 def test_setitem_none(self
):
1459 self
._def
['none'] = None
1460 self
.assertIsNone(self
._def
['none'])
1462 def test_setitem_new_int(self
):
1463 old_len
= len(self
._def
)
1464 self
._def
['new-int'] = 23
1465 self
.assertEqual(self
._def
['new-int'], 23)
1466 self
.assertEqual(len(self
._def
), old_len
+ 1)
1468 def test_setitem_index_wrong_type(self
):
1469 with self
.assertRaises(TypeError):
1472 def test_iter(self
):
1473 for vkey
, vval
in self
._def
.items():
1474 val
= self
._def
_value
[vkey
]
1475 self
.assertEqual(vval
, val
)
1477 def test_getitem_wrong_key(self
):
1478 with self
.assertRaises(KeyError):
1479 self
._def
['kilojoule']