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
)
42 _COMP_BINOPS
= (operator
.eq
, operator
.ne
)
45 # Base class for numeric value test cases.
47 # To be compatible with this base class, a derived class must, in its
50 # * Set `self._def` to a value object with an arbitrary raw value.
51 # * Set `self._def_value` to the equivalent raw value of `self._def`.
52 class _TestNumericValue(_TestCopySimple
):
53 # Tries the binary operation `op`:
55 # 1. Between `self._def`, which is a value object, and `rhs`.
56 # 2. Between `self._def_value`, which is the raw value of
57 # `self._def`, and `rhs`.
59 # Returns the results of 1. and 2.
61 # If there's an exception while performing 1. or 2., asserts that
62 # both operations raised exceptions, that both exceptions have the
63 # same type, and returns `None` for both results.
64 def _binop(self
, op
, rhs
):
69 # try with value object
71 r
= op(self
._def
, rhs
)
72 except Exception as e
:
77 rv
= op(self
._def
_value
, comp_value
)
78 except Exception as e
:
81 if type_rexc
is not None or type_rvexc
is not None:
82 # at least one of the operations raised an exception: in
83 # this case both operations should have raised the same
84 # type of exception (division by zero, bit shift with a
85 # floating point number operand, etc.)
86 self
.assertIs(type_rexc
, type_rvexc
)
91 # Tries the unary operation `op`:
93 # 1. On `self._def`, which is a value object.
94 # 2. On `self._def_value`, which is the raw value of `self._def`.
96 # Returns the results of 1. and 2.
98 # If there's an exception while performing 1. or 2., asserts that
99 # both operations raised exceptions, that both exceptions have the
100 # same type, and returns `None` for both results.
101 def _unaryop(self
, op
):
105 # try with value object
108 except Exception as e
:
113 rv
= op(self
._def
_value
)
114 except Exception as e
:
117 if type_rexc
is not None or type_rvexc
is not None:
118 # at least one of the operations raised an exception: in
119 # this case both operations should have raised the same
120 # type of exception (division by zero, bit shift with a
121 # floating point number operand, etc.)
122 self
.assertIs(type_rexc
, type_rvexc
)
127 # Tests that the unary operation `op` gives results with the same
128 # type for both `self._def` and `self._def_value`.
129 def _test_unaryop_type(self
, op
):
130 r
, rv
= self
._unaryop
(op
)
135 self
.assertIsInstance(r
, type(rv
))
137 # Tests that the unary operation `op` gives results with the same
138 # value for both `self._def` and `self._def_value`. This uses the
139 # __eq__() operator of `self._def`.
140 def _test_unaryop_value(self
, op
):
141 r
, rv
= self
._unaryop
(op
)
146 self
.assertEqual(r
, rv
)
148 # Tests that the unary operation `op`, when applied to `self._def`,
149 # does not change its underlying BT object address.
150 def _test_unaryop_addr_same(self
, op
):
151 addr_before
= self
._def
.addr
153 self
.assertEqual(self
._def
.addr
, addr_before
)
155 # Tests that the unary operation `op`, when applied to `self._def`,
156 # does not change its value.
157 def _test_unaryop_value_same(self
, op
):
158 value_before
= self
._def
.__class
__(self
._def
)
160 self
.assertEqual(self
._def
, value_before
)
162 # Tests that the binary operation `op` gives results with the same
163 # type for both `self._def` and `self._def_value`.
164 def _test_binop_type(self
, op
, rhs
):
165 r
, rv
= self
._binop
(op
, rhs
)
170 if op
in _COMP_BINOPS
:
171 # __eq__() and __ne__() always return a 'bool' object
172 self
.assertIsInstance(r
, bool)
174 self
.assertIsInstance(r
, type(rv
))
176 # Tests that the binary operation `op` gives results with the same
177 # value for both `self._def` and `self._def_value`. This uses the
178 # __eq__() operator of `self._def`.
179 def _test_binop_value(self
, op
, rhs
):
180 r
, rv
= self
._binop
(op
, rhs
)
185 self
.assertEqual(r
, rv
)
187 # Tests that the binary operation `op`, when applied to `self._def`,
188 # does not change its underlying BT object address.
189 def _test_binop_lhs_addr_same(self
, op
, rhs
):
190 addr_before
= self
._def
.addr
191 r
, rv
= self
._binop
(op
, rhs
)
192 self
.assertEqual(self
._def
.addr
, addr_before
)
194 # Tests that the binary operation `op`, when applied to `self._def`,
195 # does not change its value.
196 def _test_binop_lhs_value_same(self
, op
, rhs
):
197 value_before
= self
._def
.__class
__(self
._def
)
198 r
, rv
= self
._binop
(op
, rhs
)
199 self
.assertEqual(self
._def
, value_before
)
201 # The methods below which take the `test_cb` and `op` parameters
202 # are meant to be used with one of the _test_binop_*() functions
203 # above as `test_cb` and a binary operator function as `op`.
207 # self._test_binop_rhs_pos_int(self._test_binop_value,
210 # This tests that a numeric value object added to a positive integer
211 # raw value gives a result with the expected value.
213 # `vint` and `vfloat` mean a signed integer value object and a real
216 def _test_binop_invalid_unknown(self
, op
):
217 if op
in _COMP_BINOPS
:
218 self
.skipTest('not testing')
220 with self
.assertRaises(TypeError):
221 op(self
._def
, object())
223 def _test_binop_invalid_none(self
, op
):
224 if op
in _COMP_BINOPS
:
225 self
.skipTest('not testing')
227 with self
.assertRaises(TypeError):
230 def _test_binop_rhs_false(self
, test_cb
, op
):
233 def _test_binop_rhs_true(self
, test_cb
, op
):
236 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
239 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
242 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
245 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
246 test_cb(op
, bt2
.create_value(2))
248 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
249 test_cb(op
, bt2
.create_value(-23))
251 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
252 test_cb(op
, bt2
.create_value(0))
254 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
257 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
260 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
263 def _test_binop_rhs_complex(self
, test_cb
, op
):
264 test_cb(op
, -23 + 19j
)
266 def _test_binop_rhs_zero_complex(self
, test_cb
, op
):
269 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
270 test_cb(op
, bt2
.create_value(2.2))
272 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
273 test_cb(op
, bt2
.create_value(-23.4))
275 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
276 test_cb(op
, bt2
.create_value(0.0))
278 def _test_binop_type_false(self
, op
):
279 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
281 def _test_binop_type_true(self
, op
):
282 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
284 def _test_binop_type_pos_int(self
, op
):
285 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
287 def _test_binop_type_neg_int(self
, op
):
288 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
290 def _test_binop_type_zero_int(self
, op
):
291 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
293 def _test_binop_type_pos_vint(self
, op
):
294 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
296 def _test_binop_type_neg_vint(self
, op
):
297 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
299 def _test_binop_type_zero_vint(self
, op
):
300 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
302 def _test_binop_type_pos_float(self
, op
):
303 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
305 def _test_binop_type_neg_float(self
, op
):
306 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
308 def _test_binop_type_zero_float(self
, op
):
309 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
311 def _test_binop_type_pos_vfloat(self
, op
):
312 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
314 def _test_binop_type_neg_vfloat(self
, op
):
315 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
317 def _test_binop_type_zero_vfloat(self
, op
):
318 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
320 def _test_binop_type_complex(self
, op
):
321 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_type
, op
)
323 def _test_binop_type_zero_complex(self
, op
):
324 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_type
, op
)
326 def _test_binop_value_false(self
, op
):
327 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
329 def _test_binop_value_true(self
, op
):
330 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
332 def _test_binop_value_pos_int(self
, op
):
333 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
335 def _test_binop_value_neg_int(self
, op
):
336 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
338 def _test_binop_value_zero_int(self
, op
):
339 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
341 def _test_binop_value_pos_vint(self
, op
):
342 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
344 def _test_binop_value_neg_vint(self
, op
):
345 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
347 def _test_binop_value_zero_vint(self
, op
):
348 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
350 def _test_binop_value_pos_float(self
, op
):
351 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
353 def _test_binop_value_neg_float(self
, op
):
354 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
356 def _test_binop_value_zero_float(self
, op
):
357 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
359 def _test_binop_value_pos_vfloat(self
, op
):
360 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
362 def _test_binop_value_neg_vfloat(self
, op
):
363 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
365 def _test_binop_value_zero_vfloat(self
, op
):
366 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
368 def _test_binop_value_complex(self
, op
):
369 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_value
, op
)
371 def _test_binop_value_zero_complex(self
, op
):
372 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_value
, op
)
374 def _test_binop_lhs_addr_same_false(self
, op
):
375 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
377 def _test_binop_lhs_addr_same_true(self
, op
):
378 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
380 def _test_binop_lhs_addr_same_pos_int(self
, op
):
381 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
383 def _test_binop_lhs_addr_same_neg_int(self
, op
):
384 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
386 def _test_binop_lhs_addr_same_zero_int(self
, op
):
387 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
389 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
390 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
392 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
393 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
395 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
396 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
398 def _test_binop_lhs_addr_same_pos_float(self
, op
):
399 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
401 def _test_binop_lhs_addr_same_neg_float(self
, op
):
402 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
404 def _test_binop_lhs_addr_same_zero_float(self
, op
):
405 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
407 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
408 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
410 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
411 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
413 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
414 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
416 def _test_binop_lhs_addr_same_complex(self
, op
):
417 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
419 def _test_binop_lhs_addr_same_zero_complex(self
, op
):
420 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_addr
_same
, op
)
422 def _test_binop_lhs_value_same_false(self
, op
):
423 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
425 def _test_binop_lhs_value_same_true(self
, op
):
426 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
428 def _test_binop_lhs_value_same_pos_int(self
, op
):
429 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
431 def _test_binop_lhs_value_same_neg_int(self
, op
):
432 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
434 def _test_binop_lhs_value_same_zero_int(self
, op
):
435 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
437 def _test_binop_lhs_value_same_pos_vint(self
, op
):
438 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
440 def _test_binop_lhs_value_same_neg_vint(self
, op
):
441 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
443 def _test_binop_lhs_value_same_zero_vint(self
, op
):
444 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
446 def _test_binop_lhs_value_same_pos_float(self
, op
):
447 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
449 def _test_binop_lhs_value_same_neg_float(self
, op
):
450 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
452 def _test_binop_lhs_value_same_zero_float(self
, op
):
453 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
455 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
456 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
458 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
459 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
461 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
462 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
464 def _test_binop_lhs_value_same_complex(self
, op
):
465 self
._test
_binop
_rhs
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
467 def _test_binop_lhs_value_same_zero_complex(self
, op
):
468 self
._test
_binop
_rhs
_zero
_complex
(self
._test
_binop
_lhs
_value
_same
, op
)
470 def test_bool_op(self
):
471 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
473 def test_int_op(self
):
474 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
476 def test_float_op(self
):
477 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
479 def test_complex_op(self
):
480 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
482 def test_str_op(self
):
483 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
485 def test_eq_none(self
):
486 self
.assertFalse(self
._def
== None)
488 def test_ne_none(self
):
489 self
.assertTrue(self
._def
!= None)
492 # This is a list of binary operators used for
493 # _inject_numeric_testing_methods().
495 # Each entry is a pair of binary operator name (used as part of the
496 # created testing method's name) and operator function.
504 ('add', operator
.add
),
505 ('radd', lambda a
, b
: operator
.add(b
, a
)),
506 ('and', operator
.and_
),
507 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
508 ('floordiv', operator
.floordiv
),
509 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
510 ('lshift', operator
.lshift
),
511 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
512 ('mod', operator
.mod
),
513 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
514 ('mul', operator
.mul
),
515 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
516 ('or', operator
.or_
),
517 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
518 ('pow', operator
.pow),
519 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
520 ('rshift', operator
.rshift
),
521 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
522 ('sub', operator
.sub
),
523 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
524 ('truediv', operator
.truediv
),
525 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
526 ('xor', operator
.xor
),
527 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
531 # This is a list of unary operators used for
532 # _inject_numeric_testing_methods().
534 # Each entry is a pair of unary operator name (used as part of the
535 # created testing method's name) and operator function.
537 ('neg', operator
.neg
),
538 ('pos', operator
.pos
),
539 ('abs', operator
.abs),
540 ('invert', operator
.invert
),
542 ('round_0', partial(round, ndigits
=0)),
543 ('round_1', partial(round, ndigits
=1)),
544 ('round_2', partial(round, ndigits
=2)),
545 ('round_3', partial(round, ndigits
=3)),
547 ('floor', math
.floor
),
548 ('trunc', math
.trunc
),
552 # This function injects a bunch of testing methods to a numeric
555 # It is meant to be used like this:
557 # _inject_numeric_testing_methods(MyNumericValueTestCase)
559 # This function injects:
561 # * One testing method for each _TestNumericValue._test_binop_*()
562 # method, for each binary operator in the _BINOPS tuple.
564 # * One testing method for each _TestNumericValue._test_unaryop*()
565 # method, for each unary operator in the _UNARYOPS tuple.
566 def _inject_numeric_testing_methods(cls
):
567 def test_binop_name(suffix
):
568 return 'test_binop_{}_{}'.format(name
, suffix
)
570 def test_unaryop_name(suffix
):
571 return 'test_unaryop_{}_{}'.format(name
, suffix
)
573 # inject testing methods for each binary operation
574 for name
, binop
in _BINOPS
:
577 test_binop_name('invalid_unknown'),
578 partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
),
582 test_binop_name('invalid_none'),
583 partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
),
587 test_binop_name('type_true'),
588 partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
),
592 test_binop_name('type_pos_int'),
593 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
),
597 test_binop_name('type_pos_vint'),
598 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
),
602 test_binop_name('value_true'),
603 partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
),
607 test_binop_name('value_pos_int'),
608 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
),
612 test_binop_name('value_pos_vint'),
613 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
),
617 test_binop_name('lhs_addr_same_true'),
618 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
),
622 test_binop_name('lhs_addr_same_pos_int'),
624 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
629 test_binop_name('lhs_addr_same_pos_vint'),
631 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
636 test_binop_name('lhs_value_same_true'),
637 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
),
641 test_binop_name('lhs_value_same_pos_int'),
643 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
648 test_binop_name('lhs_value_same_pos_vint'),
650 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
655 test_binop_name('type_neg_int'),
656 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
),
660 test_binop_name('type_neg_vint'),
661 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
),
665 test_binop_name('value_neg_int'),
666 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
),
670 test_binop_name('value_neg_vint'),
671 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
),
675 test_binop_name('lhs_addr_same_neg_int'),
677 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
682 test_binop_name('lhs_addr_same_neg_vint'),
684 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
689 test_binop_name('lhs_value_same_neg_int'),
691 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
696 test_binop_name('lhs_value_same_neg_vint'),
698 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
703 test_binop_name('type_false'),
704 partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
),
708 test_binop_name('type_zero_int'),
709 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
),
713 test_binop_name('type_zero_vint'),
714 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
),
718 test_binop_name('value_false'),
719 partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
),
723 test_binop_name('value_zero_int'),
724 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
),
728 test_binop_name('value_zero_vint'),
729 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
),
733 test_binop_name('lhs_addr_same_false'),
734 partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
),
738 test_binop_name('lhs_addr_same_zero_int'),
740 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
745 test_binop_name('lhs_addr_same_zero_vint'),
747 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
752 test_binop_name('lhs_value_same_false'),
753 partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
),
757 test_binop_name('lhs_value_same_zero_int'),
759 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
764 test_binop_name('lhs_value_same_zero_vint'),
766 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
771 test_binop_name('type_neg_float'),
772 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
),
776 test_binop_name('type_neg_vfloat'),
777 partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
),
781 test_binop_name('value_neg_float'),
782 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
),
786 test_binop_name('value_neg_vfloat'),
787 partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
),
791 test_binop_name('lhs_addr_same_neg_float'),
793 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
798 test_binop_name('lhs_addr_same_neg_vfloat'),
800 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
805 test_binop_name('lhs_value_same_neg_float'),
807 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
812 test_binop_name('lhs_value_same_neg_vfloat'),
814 _TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
819 test_binop_name('type_pos_float'),
820 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
),
824 test_binop_name('type_pos_vfloat'),
825 partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
),
829 test_binop_name('value_pos_float'),
830 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
),
834 test_binop_name('value_pos_vfloat'),
835 partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
),
839 test_binop_name('lhs_addr_same_pos_float'),
841 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
846 test_binop_name('lhs_addr_same_pos_vfloat'),
848 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
853 test_binop_name('lhs_value_same_pos_float'),
855 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
860 test_binop_name('lhs_value_same_pos_vfloat'),
862 _TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
867 test_binop_name('type_zero_float'),
868 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
),
872 test_binop_name('type_zero_vfloat'),
873 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
),
877 test_binop_name('value_zero_float'),
878 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
),
882 test_binop_name('value_zero_vfloat'),
883 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
),
887 test_binop_name('lhs_addr_same_zero_float'),
889 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
894 test_binop_name('lhs_addr_same_zero_vfloat'),
896 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
901 test_binop_name('lhs_value_same_zero_float'),
903 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
908 test_binop_name('lhs_value_same_zero_vfloat'),
910 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
915 test_binop_name('type_complex'),
916 partialmethod(_TestNumericValue
._test
_binop
_type
_complex
, op
=binop
),
920 test_binop_name('type_zero_complex'),
921 partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_complex
, op
=binop
),
925 test_binop_name('value_complex'),
926 partialmethod(_TestNumericValue
._test
_binop
_value
_complex
, op
=binop
),
930 test_binop_name('value_zero_complex'),
931 partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_complex
, op
=binop
),
935 test_binop_name('lhs_addr_same_complex'),
937 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_complex
, op
=binop
942 test_binop_name('lhs_addr_same_zero_complex'),
944 _TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_complex
, op
=binop
949 test_binop_name('lhs_value_same_complex'),
951 _TestNumericValue
._test
_binop
_lhs
_value
_same
_complex
, op
=binop
956 test_binop_name('lhs_value_same_zero_complex'),
958 _TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_complex
, op
=binop
962 # inject testing methods for each unary operation
963 for name
, unaryop
in _UNARYOPS
:
966 test_unaryop_name('type'),
967 partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
),
971 test_unaryop_name('value'),
972 partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
),
976 test_unaryop_name('addr_same'),
977 partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
),
981 test_unaryop_name('value_same'),
982 partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
),
986 class CreateValueFuncTestCase(unittest
.TestCase
):
987 def test_create_none(self
):
988 v
= bt2
.create_value(None)
991 def test_create_bool_false(self
):
992 v
= bt2
.create_value(False)
993 self
.assertIsInstance(v
, bt2
.BoolValue
)
996 def test_create_bool_true(self
):
997 v
= bt2
.create_value(True)
998 self
.assertIsInstance(v
, bt2
.BoolValue
)
1001 def test_create_int_pos(self
):
1003 v
= bt2
.create_value(raw
)
1004 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1005 self
.assertEqual(v
, raw
)
1007 def test_create_int_neg(self
):
1009 v
= bt2
.create_value(raw
)
1010 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
1011 self
.assertEqual(v
, raw
)
1013 def test_create_float_pos(self
):
1015 v
= bt2
.create_value(raw
)
1016 self
.assertIsInstance(v
, bt2
.RealValue
)
1017 self
.assertEqual(v
, raw
)
1019 def test_create_float_neg(self
):
1021 v
= bt2
.create_value(raw
)
1022 self
.assertIsInstance(v
, bt2
.RealValue
)
1023 self
.assertEqual(v
, raw
)
1025 def test_create_string(self
):
1027 v
= bt2
.create_value(raw
)
1028 self
.assertIsInstance(v
, bt2
.StringValue
)
1029 self
.assertEqual(v
, raw
)
1031 def test_create_string_empty(self
):
1033 v
= bt2
.create_value(raw
)
1034 self
.assertIsInstance(v
, bt2
.StringValue
)
1035 self
.assertEqual(v
, raw
)
1037 def test_create_array_from_list(self
):
1039 v
= bt2
.create_value(raw
)
1040 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1041 self
.assertEqual(v
, raw
)
1043 def test_create_array_from_tuple(self
):
1045 v
= bt2
.create_value(raw
)
1046 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1047 self
.assertEqual(v
, raw
)
1049 def test_create_array_from_empty_list(self
):
1051 v
= bt2
.create_value(raw
)
1052 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1053 self
.assertEqual(v
, raw
)
1055 def test_create_array_from_empty_tuple(self
):
1057 v
= bt2
.create_value(raw
)
1058 self
.assertIsInstance(v
, bt2
.ArrayValue
)
1059 self
.assertEqual(v
, raw
)
1061 def test_create_map(self
):
1063 v
= bt2
.create_value(raw
)
1064 self
.assertIsInstance(v
, bt2
.MapValue
)
1065 self
.assertEqual(v
, raw
)
1067 def test_create_map_empty(self
):
1069 v
= bt2
.create_value(raw
)
1070 self
.assertIsInstance(v
, bt2
.MapValue
)
1071 self
.assertEqual(v
, raw
)
1073 def test_create_vfalse(self
):
1074 v
= bt2
.create_value(bt2
.create_value(False))
1075 self
.assertIsInstance(v
, bt2
.BoolValue
)
1078 def test_create_invalid(self
):
1084 with self
.assertRaisesRegex(
1085 TypeError, "cannot create value object from 'A' object"
1087 v
= bt2
.create_value(a
)
1090 class BoolValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1092 self
._f
= bt2
.BoolValue(False)
1093 self
._t
= bt2
.BoolValue(True)
1095 self
._def
_value
= False
1096 self
._def
_new
_value
= True
1103 def _assert_expecting_bool(self
):
1104 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
1106 def test_create_default(self
):
1110 def test_create_false(self
):
1111 self
.assertFalse(self
._f
)
1113 def test_create_true(self
):
1114 self
.assertTrue(self
._t
)
1116 def test_create_from_vfalse(self
):
1117 b
= bt2
.BoolValue(self
._f
)
1120 def test_create_from_vtrue(self
):
1121 b
= bt2
.BoolValue(self
._t
)
1124 def test_create_from_int_non_zero(self
):
1125 with self
.assertRaises(TypeError):
1126 b
= bt2
.BoolValue(23)
1128 def test_create_from_int_zero(self
):
1129 with self
.assertRaises(TypeError):
1130 b
= bt2
.BoolValue(0)
1132 def test_assign_true(self
):
1137 def test_assign_false(self
):
1142 def test_assign_vtrue(self
):
1147 def test_assign_vfalse(self
):
1152 def test_assign_int(self
):
1153 with self
.assertRaises(TypeError):
1157 def test_bool_op(self
):
1158 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1160 def test_str_op(self
):
1161 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1163 def test_eq_none(self
):
1164 self
.assertFalse(self
._def
== None)
1166 def test_ne_none(self
):
1167 self
.assertTrue(self
._def
!= None)
1169 def test_vfalse_eq_false(self
):
1170 self
.assertEqual(self
._f
, False)
1172 def test_vfalse_ne_true(self
):
1173 self
.assertNotEqual(self
._f
, True)
1175 def test_vtrue_eq_true(self
):
1176 self
.assertEqual(self
._t
, True)
1178 def test_vtrue_ne_false(self
):
1179 self
.assertNotEqual(self
._t
, False)
1182 _inject_numeric_testing_methods(BoolValueTestCase
)
1185 class _TestIntegerValue(_TestNumericValue
):
1188 self
._ip
= self
._CLS
(self
._pv
)
1189 self
._def
= self
._ip
1190 self
._def
_value
= self
._pv
1191 self
._def
_new
_value
= 101
1198 def _assert_expecting_int(self
):
1199 return self
.assertRaisesRegex(TypeError, r
'expecting an integral number object')
1201 def _assert_expecting_int64(self
):
1202 return self
.assertRaisesRegex(
1203 ValueError, r
"expecting a signed 64-bit integral value"
1206 def _assert_expecting_uint64(self
):
1207 return self
.assertRaisesRegex(
1208 ValueError, r
"expecting an unsigned 64-bit integral value"
1211 def test_create_default(self
):
1213 self
.assertEqual(i
, 0)
1215 def test_create_pos(self
):
1216 self
.assertEqual(self
._ip
, self
._pv
)
1218 def test_create_neg(self
):
1219 self
.assertEqual(self
._in
, self
._nv
)
1221 def test_create_from_vint(self
):
1222 i
= self
._CLS
(self
._ip
)
1223 self
.assertEqual(i
, self
._pv
)
1225 def test_create_from_false(self
):
1226 i
= self
._CLS
(False)
1229 def test_create_from_true(self
):
1233 def test_create_from_unknown(self
):
1237 with self
._assert
_expecting
_int
():
1240 def test_create_from_varray(self
):
1241 with self
._assert
_expecting
_int
():
1242 i
= self
._CLS
(bt2
.ArrayValue())
1244 def test_assign_true(self
):
1246 self
._def
.value
= raw
1247 self
.assertEqual(self
._def
, raw
)
1249 def test_assign_false(self
):
1251 self
._def
.value
= raw
1252 self
.assertEqual(self
._def
, raw
)
1254 def test_assign_pos_int(self
):
1256 self
._def
.value
= raw
1257 self
.assertEqual(self
._def
, raw
)
1259 def test_assign_vint(self
):
1261 self
._def
.value
= bt2
.create_value(raw
)
1262 self
.assertEqual(self
._def
, raw
)
1265 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1266 _CLS
= bt2
.SignedIntegerValue
1271 self
._in
= self
._CLS
(self
._nv
)
1272 self
._def
_new
_value
= -101
1278 def test_create_neg(self
):
1279 self
.assertEqual(self
._in
, self
._nv
)
1281 def test_create_pos_too_big(self
):
1282 with self
._assert
_expecting
_int
64():
1283 i
= self
._CLS
(2 ** 63)
1285 def test_create_neg_too_big(self
):
1286 with self
._assert
_expecting
_int
64():
1287 i
= self
._CLS
(-(2 ** 63) - 1)
1289 def test_assign_neg_int(self
):
1291 self
._def
.value
= raw
1292 self
.assertEqual(self
._def
, raw
)
1294 def test_compare_big_int(self
):
1295 # Larger than the IEEE 754 double-precision exact representation of
1298 v
= bt2
.create_value(raw
)
1299 self
.assertEqual(v
, raw
)
1302 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
1305 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
1306 _CLS
= bt2
.UnsignedIntegerValue
1308 def test_create_pos_too_big(self
):
1309 with self
._assert
_expecting
_uint
64():
1310 i
= self
._CLS
(2 ** 64)
1312 def test_create_neg(self
):
1313 with self
._assert
_expecting
_uint
64():
1317 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
)
1320 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
1324 self
._fp
= bt2
.RealValue(self
._pv
)
1325 self
._fn
= bt2
.RealValue(self
._nv
)
1326 self
._def
= self
._fp
1327 self
._def
_value
= self
._pv
1328 self
._def
_new
_value
= -101.88
1336 def _assert_expecting_float(self
):
1337 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
1339 def _test_invalid_op(self
, cb
):
1340 with self
.assertRaises(TypeError):
1343 def test_create_default(self
):
1345 self
.assertEqual(f
, 0.0)
1347 def test_create_pos(self
):
1348 self
.assertEqual(self
._fp
, self
._pv
)
1350 def test_create_neg(self
):
1351 self
.assertEqual(self
._fn
, self
._nv
)
1353 def test_create_from_vint(self
):
1354 f
= bt2
.RealValue(self
._fp
)
1355 self
.assertEqual(f
, self
._pv
)
1357 def test_create_from_false(self
):
1358 f
= bt2
.RealValue(False)
1361 def test_create_from_true(self
):
1362 f
= bt2
.RealValue(True)
1365 def test_create_from_int(self
):
1367 f
= bt2
.RealValue(raw
)
1368 self
.assertEqual(f
, float(raw
))
1370 def test_create_from_vint(self
):
1372 f
= bt2
.RealValue(bt2
.create_value(raw
))
1373 self
.assertEqual(f
, float(raw
))
1375 def test_create_from_vfloat(self
):
1377 f
= bt2
.RealValue(bt2
.create_value(raw
))
1378 self
.assertEqual(f
, raw
)
1380 def test_create_from_unknown(self
):
1384 with self
._assert
_expecting
_float
():
1385 f
= bt2
.RealValue(A())
1387 def test_create_from_varray(self
):
1388 with self
._assert
_expecting
_float
():
1389 f
= bt2
.RealValue(bt2
.ArrayValue())
1391 def test_assign_true(self
):
1392 self
._def
.value
= True
1393 self
.assertTrue(self
._def
)
1395 def test_assign_false(self
):
1396 self
._def
.value
= False
1397 self
.assertFalse(self
._def
)
1399 def test_assign_pos_int(self
):
1401 self
._def
.value
= raw
1402 self
.assertEqual(self
._def
, float(raw
))
1404 def test_assign_neg_int(self
):
1406 self
._def
.value
= raw
1407 self
.assertEqual(self
._def
, float(raw
))
1409 def test_assign_vint(self
):
1411 self
._def
.value
= bt2
.create_value(raw
)
1412 self
.assertEqual(self
._def
, float(raw
))
1414 def test_assign_float(self
):
1416 self
._def
.value
= raw
1417 self
.assertEqual(self
._def
, raw
)
1419 def test_assign_vfloat(self
):
1421 self
._def
.value
= bt2
.create_value(raw
)
1422 self
.assertEqual(self
._def
, raw
)
1424 def test_invalid_lshift(self
):
1425 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1427 def test_invalid_rshift(self
):
1428 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1430 def test_invalid_and(self
):
1431 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1433 def test_invalid_or(self
):
1434 self
._test
_invalid
_op
(lambda: self
._def |
23)
1436 def test_invalid_xor(self
):
1437 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1439 def test_invalid_invert(self
):
1440 self
._test
_invalid
_op
(lambda: ~self
._def
)
1443 _inject_numeric_testing_methods(RealValueTestCase
)
1446 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1448 self
._def
_value
= 'Hello, World!'
1449 self
._def
= bt2
.StringValue(self
._def
_value
)
1450 self
._def
_new
_value
= 'Yes!'
1455 def _assert_expecting_str(self
):
1456 return self
.assertRaises(TypeError)
1458 def test_create_default(self
):
1459 s
= bt2
.StringValue()
1460 self
.assertEqual(s
, '')
1462 def test_create_from_str(self
):
1464 s
= bt2
.StringValue(raw
)
1465 self
.assertEqual(s
, raw
)
1467 def test_create_from_vstr(self
):
1469 s
= bt2
.StringValue(bt2
.create_value(raw
))
1470 self
.assertEqual(s
, raw
)
1472 def test_create_from_unknown(self
):
1476 with self
._assert
_expecting
_str
():
1477 i
= bt2
.StringValue(A())
1479 def test_create_from_varray(self
):
1480 with self
._assert
_expecting
_str
():
1481 i
= bt2
.StringValue(bt2
.ArrayValue())
1483 def test_assign_int(self
):
1484 with self
._assert
_expecting
_str
():
1485 self
._def
.value
= 283
1487 def test_assign_str(self
):
1490 self
.assertEqual(self
._def
, raw
)
1492 def test_assign_vstr(self
):
1494 self
._def
= bt2
.create_value(raw
)
1495 self
.assertEqual(self
._def
, raw
)
1498 self
.assertEqual(self
._def
, self
._def
_value
)
1501 self
.assertNotEqual(self
._def
, 23)
1503 def test_lt_vstring(self
):
1504 s1
= bt2
.StringValue('allo')
1505 s2
= bt2
.StringValue('bateau')
1506 self
.assertLess(s1
, s2
)
1508 def test_lt_string(self
):
1509 s1
= bt2
.StringValue('allo')
1510 self
.assertLess(s1
, 'bateau')
1512 def test_le_vstring(self
):
1513 s1
= bt2
.StringValue('allo')
1514 s2
= bt2
.StringValue('bateau')
1515 self
.assertLessEqual(s1
, s2
)
1517 def test_le_string(self
):
1518 s1
= bt2
.StringValue('allo')
1519 self
.assertLessEqual(s1
, 'bateau')
1521 def test_gt_vstring(self
):
1522 s1
= bt2
.StringValue('allo')
1523 s2
= bt2
.StringValue('bateau')
1524 self
.assertGreater(s2
, s1
)
1526 def test_gt_string(self
):
1527 s1
= bt2
.StringValue('allo')
1528 self
.assertGreater('bateau', s1
)
1530 def test_ge_vstring(self
):
1531 s1
= bt2
.StringValue('allo')
1532 s2
= bt2
.StringValue('bateau')
1533 self
.assertGreaterEqual(s2
, s1
)
1535 def test_ge_string(self
):
1536 s1
= bt2
.StringValue('allo')
1537 self
.assertGreaterEqual('bateau', s1
)
1539 def test_bool_op(self
):
1540 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1542 def test_str_op(self
):
1543 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1546 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1548 def test_getitem(self
):
1549 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1551 def test_append_str(self
):
1552 to_append
= 'meow meow meow'
1553 self
._def
+= to_append
1554 self
._def
_value
+= to_append
1555 self
.assertEqual(self
._def
, self
._def
_value
)
1557 def test_append_vstr(self
):
1558 to_append
= 'meow meow meow'
1559 self
._def
+= bt2
.create_value(to_append
)
1560 self
._def
_value
+= to_append
1561 self
.assertEqual(self
._def
, self
._def
_value
)
1564 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1566 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1567 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1572 def _modify_def(self
):
1573 self
._def
[2] = 'xyz'
1575 def _assert_type_error(self
):
1576 return self
.assertRaises(TypeError)
1578 def test_create_default(self
):
1579 a
= bt2
.ArrayValue()
1580 self
.assertEqual(len(a
), 0)
1582 def test_create_from_array(self
):
1583 self
.assertEqual(self
._def
, self
._def
_value
)
1585 def test_create_from_tuple(self
):
1586 t
= 1, 2, False, None
1587 a
= bt2
.ArrayValue(t
)
1588 self
.assertEqual(a
, t
)
1590 def test_create_from_varray(self
):
1591 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1592 a
= bt2
.ArrayValue(va
)
1593 self
.assertEqual(va
, a
)
1595 def test_create_from_unknown(self
):
1599 with self
._assert
_type
_error
():
1600 a
= bt2
.ArrayValue(A())
1602 def test_bool_op_true(self
):
1603 self
.assertTrue(bool(self
._def
))
1605 def test_bool_op_false(self
):
1606 self
.assertFalse(bool(bt2
.ArrayValue()))
1609 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1611 def test_eq_int(self
):
1612 self
.assertNotEqual(self
._def
, 23)
1614 def test_eq_diff_len(self
):
1615 a1
= bt2
.create_value([1, 2, 3])
1616 a2
= bt2
.create_value([1, 2])
1617 self
.assertNotEqual(a1
, a2
)
1619 def test_eq_diff_content_same_len(self
):
1620 a1
= bt2
.create_value([1, 2, 3])
1621 a2
= bt2
.create_value([4, 5, 6])
1622 self
.assertNotEqual(a1
, a2
)
1624 def test_eq_same_content_same_len(self
):
1625 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1626 a1
= bt2
.ArrayValue(raw
)
1627 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1628 self
.assertEqual(a1
, a2
)
1630 def test_eq_non_sequence_iterable(self
):
1631 dct
= collections
.OrderedDict([(1, 2), (3, 4), (5, 6)])
1632 a
= bt2
.ArrayValue((1, 3, 5))
1633 self
.assertEqual(a
, list(dct
.keys()))
1634 self
.assertNotEqual(a
, dct
)
1636 def test_setitem_int(self
):
1639 self
.assertEqual(self
._def
[2], raw
)
1641 def test_setitem_vint(self
):
1643 self
._def
[2] = bt2
.create_value(raw
)
1644 self
.assertEqual(self
._def
[2], raw
)
1646 def test_setitem_none(self
):
1648 self
.assertIsNone(self
._def
[2])
1650 def test_setitem_index_wrong_type(self
):
1651 with self
._assert
_type
_error
():
1652 self
._def
['yes'] = 23
1654 def test_setitem_index_neg(self
):
1655 with self
.assertRaises(IndexError):
1658 def test_setitem_index_out_of_range(self
):
1659 with self
.assertRaises(IndexError):
1660 self
._def
[len(self
._def
)] = 23
1662 def test_append_none(self
):
1663 self
._def
.append(None)
1664 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1666 def test_append_int(self
):
1668 self
._def
.append(raw
)
1669 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1671 def test_append_vint(self
):
1673 self
._def
.append(bt2
.create_value(raw
))
1674 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1676 def test_append_unknown(self
):
1680 with self
._assert
_type
_error
():
1681 self
._def
.append(A())
1683 def test_iadd(self
):
1686 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1687 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1688 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1690 def test_iadd_unknown(self
):
1694 with self
._assert
_type
_error
():
1697 def test_iadd_list_unknown(self
):
1701 with self
._assert
_type
_error
():
1704 def test_iter(self
):
1705 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1706 self
.assertEqual(velem
, elem
)
1709 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1722 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1727 def _modify_def(self
):
1728 self
._def
['zero'] = 1
1730 def test_create_default(self
):
1732 self
.assertEqual(len(m
), 0)
1734 def test_create_from_dict(self
):
1735 self
.assertEqual(self
._def
, self
._def
_value
)
1737 def test_create_from_vmap(self
):
1738 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1739 m
= bt2
.MapValue(vm
)
1740 self
.assertEqual(vm
, m
)
1742 def test_create_from_unknown(self
):
1746 with self
.assertRaises(AttributeError):
1747 m
= bt2
.MapValue(A())
1749 def test_bool_op_true(self
):
1750 self
.assertTrue(bool(self
._def
))
1752 def test_bool_op_false(self
):
1753 self
.assertFalse(bool(bt2
.MapValue()))
1756 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1758 def test_eq_int(self
):
1759 self
.assertNotEqual(self
._def
, 23)
1761 def test_eq_diff_len(self
):
1762 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1763 a2
= bt2
.create_value({'a': 1, 'b': 2})
1764 self
.assertNotEqual(a1
, a2
)
1766 def test_eq_diff_content_same_len(self
):
1767 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1768 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1769 self
.assertNotEqual(a1
, a2
)
1771 def test_eq_same_content_diff_keys(self
):
1772 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1773 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1774 self
.assertNotEqual(a1
, a2
)
1776 def test_eq_same_content_same_len(self
):
1777 raw
= {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1778 a1
= bt2
.MapValue(raw
)
1779 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1780 self
.assertEqual(a1
, a2
)
1781 self
.assertEqual(a1
, raw
)
1783 def test_setitem_int(self
):
1785 self
._def
['pos-int'] = raw
1786 self
.assertEqual(self
._def
['pos-int'], raw
)
1788 def test_setitem_vint(self
):
1790 self
._def
['pos-int'] = bt2
.create_value(raw
)
1791 self
.assertEqual(self
._def
['pos-int'], raw
)
1793 def test_setitem_none(self
):
1794 self
._def
['none'] = None
1795 self
.assertIsNone(self
._def
['none'])
1797 def test_setitem_new_int(self
):
1798 old_len
= len(self
._def
)
1799 self
._def
['new-int'] = 23
1800 self
.assertEqual(self
._def
['new-int'], 23)
1801 self
.assertEqual(len(self
._def
), old_len
+ 1)
1803 def test_setitem_index_wrong_type(self
):
1804 with self
.assertRaises(TypeError):
1807 def test_iter(self
):
1808 for vkey
, vval
in self
._def
.items():
1809 val
= self
._def
_value
[vkey
]
1810 self
.assertEqual(vval
, val
)
1812 def test_getitem_wrong_key(self
):
1813 with self
.assertRaises(KeyError):
1814 self
._def
['kilojoule']