Move to kernel style SPDX license identifiers
[babeltrace.git] / tests / bindings / python / bt2 / test_value.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # Copyright (C) 2019 EfficiOS Inc.
4 #
5
6 from functools import partial, partialmethod
7 import operator
8 import collections
9 import unittest
10 import math
11 import copy
12 import bt2
13
14
15 # The value object classes explicitly do not implement the copy methods,
16 # raising `NotImplementedError`, just in case we decide to implement
17 # them someday.
18 class _TestCopySimple:
19 def test_copy(self):
20 with self.assertRaises(NotImplementedError):
21 copy.copy(self._def)
22
23 def test_deepcopy(self):
24 with self.assertRaises(NotImplementedError):
25 copy.deepcopy(self._def)
26
27
28 _COMP_BINOPS = (operator.eq, operator.ne)
29
30
31 # Base class for numeric value test cases.
32 #
33 # To be compatible with this base class, a derived class must, in its
34 # setUp() method:
35 #
36 # * Set `self._def` to a value object with an arbitrary raw value.
37 # * Set `self._def_value` to the equivalent raw value of `self._def`.
38 class _TestNumericValue(_TestCopySimple):
39 # Tries the binary operation `op`:
40 #
41 # 1. Between `self._def`, which is a value object, and `rhs`.
42 # 2. Between `self._def_value`, which is the raw value of
43 # `self._def`, and `rhs`.
44 #
45 # Returns the results of 1. and 2.
46 #
47 # If there's an exception while performing 1. or 2., asserts that
48 # both operations raised exceptions, that both exceptions have the
49 # same type, and returns `None` for both results.
50 def _binop(self, op, rhs):
51 type_rexc = None
52 type_rvexc = None
53 comp_value = rhs
54
55 # try with value object
56 try:
57 r = op(self._def, rhs)
58 except Exception as e:
59 type_rexc = type(e)
60
61 # try with raw value
62 try:
63 rv = op(self._def_value, comp_value)
64 except Exception as e:
65 type_rvexc = type(e)
66
67 if type_rexc is not None or type_rvexc is not None:
68 # at least one of the operations raised an exception: in
69 # this case both operations should have raised the same
70 # type of exception (division by zero, bit shift with a
71 # floating point number operand, etc.)
72 self.assertIs(type_rexc, type_rvexc)
73 return None, None
74
75 return r, rv
76
77 # Tries the unary operation `op`:
78 #
79 # 1. On `self._def`, which is a value object.
80 # 2. On `self._def_value`, which is the raw value of `self._def`.
81 #
82 # Returns the results of 1. and 2.
83 #
84 # If there's an exception while performing 1. or 2., asserts that
85 # both operations raised exceptions, that both exceptions have the
86 # same type, and returns `None` for both results.
87 def _unaryop(self, op):
88 type_rexc = None
89 type_rvexc = None
90
91 # try with value object
92 try:
93 r = op(self._def)
94 except Exception as e:
95 type_rexc = type(e)
96
97 # try with raw value
98 try:
99 rv = op(self._def_value)
100 except Exception as e:
101 type_rvexc = type(e)
102
103 if type_rexc is not None or type_rvexc is not None:
104 # at least one of the operations raised an exception: in
105 # this case both operations should have raised the same
106 # type of exception (division by zero, bit shift with a
107 # floating point number operand, etc.)
108 self.assertIs(type_rexc, type_rvexc)
109 return None, None
110
111 return r, rv
112
113 # Tests that the unary operation `op` gives results with the same
114 # type for both `self._def` and `self._def_value`.
115 def _test_unaryop_type(self, op):
116 r, rv = self._unaryop(op)
117
118 if r is None:
119 return
120
121 self.assertIsInstance(r, type(rv))
122
123 # Tests that the unary operation `op` gives results with the same
124 # value for both `self._def` and `self._def_value`. This uses the
125 # __eq__() operator of `self._def`.
126 def _test_unaryop_value(self, op):
127 r, rv = self._unaryop(op)
128
129 if r is None:
130 return
131
132 self.assertEqual(r, rv)
133
134 # Tests that the unary operation `op`, when applied to `self._def`,
135 # does not change its underlying BT object address.
136 def _test_unaryop_addr_same(self, op):
137 addr_before = self._def.addr
138 self._unaryop(op)
139 self.assertEqual(self._def.addr, addr_before)
140
141 # Tests that the unary operation `op`, when applied to `self._def`,
142 # does not change its value.
143 def _test_unaryop_value_same(self, op):
144 value_before = self._def.__class__(self._def)
145 self._unaryop(op)
146 self.assertEqual(self._def, value_before)
147
148 # Tests that the binary operation `op` gives results with the same
149 # type for both `self._def` and `self._def_value`.
150 def _test_binop_type(self, op, rhs):
151 r, rv = self._binop(op, rhs)
152
153 if r is None:
154 return
155
156 if op in _COMP_BINOPS:
157 # __eq__() and __ne__() always return a 'bool' object
158 self.assertIsInstance(r, bool)
159 else:
160 self.assertIsInstance(r, type(rv))
161
162 # Tests that the binary operation `op` gives results with the same
163 # value for both `self._def` and `self._def_value`. This uses the
164 # __eq__() operator of `self._def`.
165 def _test_binop_value(self, op, rhs):
166 r, rv = self._binop(op, rhs)
167
168 if r is None:
169 return
170
171 self.assertEqual(r, rv)
172
173 # Tests that the binary operation `op`, when applied to `self._def`,
174 # does not change its underlying BT object address.
175 def _test_binop_lhs_addr_same(self, op, rhs):
176 addr_before = self._def.addr
177 r, rv = self._binop(op, rhs)
178 self.assertEqual(self._def.addr, addr_before)
179
180 # Tests that the binary operation `op`, when applied to `self._def`,
181 # does not change its value.
182 def _test_binop_lhs_value_same(self, op, rhs):
183 value_before = self._def.__class__(self._def)
184 r, rv = self._binop(op, rhs)
185 self.assertEqual(self._def, value_before)
186
187 # The methods below which take the `test_cb` and `op` parameters
188 # are meant to be used with one of the _test_binop_*() functions
189 # above as `test_cb` and a binary operator function as `op`.
190 #
191 # For example:
192 #
193 # self._test_binop_rhs_pos_int(self._test_binop_value,
194 # operator.add)
195 #
196 # This tests that a numeric value object added to a positive integer
197 # raw value gives a result with the expected value.
198 #
199 # `vint` and `vfloat` mean a signed integer value object and a real
200 # value object.
201
202 def _test_binop_unknown(self, op):
203 if op is operator.eq:
204 self.assertIs(op(self._def, object()), False)
205 elif op is operator.ne:
206 self.assertIs(op(self._def, object()), True)
207 else:
208 with self.assertRaises(TypeError):
209 op(self._def, object())
210
211 def _test_binop_none(self, op):
212 if op is operator.eq:
213 self.assertIs(op(self._def, None), False)
214 elif op is operator.ne:
215 self.assertIs(op(self._def, None), True)
216 else:
217 with self.assertRaises(TypeError):
218 op(self._def, None)
219
220 def _test_binop_rhs_false(self, test_cb, op):
221 test_cb(op, False)
222
223 def _test_binop_rhs_true(self, test_cb, op):
224 test_cb(op, True)
225
226 def _test_binop_rhs_pos_int(self, test_cb, op):
227 test_cb(op, 2)
228
229 def _test_binop_rhs_neg_int(self, test_cb, op):
230 test_cb(op, -23)
231
232 def _test_binop_rhs_zero_int(self, test_cb, op):
233 test_cb(op, 0)
234
235 def _test_binop_rhs_pos_vint(self, test_cb, op):
236 test_cb(op, bt2.create_value(2))
237
238 def _test_binop_rhs_neg_vint(self, test_cb, op):
239 test_cb(op, bt2.create_value(-23))
240
241 def _test_binop_rhs_zero_vint(self, test_cb, op):
242 test_cb(op, bt2.create_value(0))
243
244 def _test_binop_rhs_pos_float(self, test_cb, op):
245 test_cb(op, 2.2)
246
247 def _test_binop_rhs_neg_float(self, test_cb, op):
248 test_cb(op, -23.4)
249
250 def _test_binop_rhs_zero_float(self, test_cb, op):
251 test_cb(op, 0.0)
252
253 def _test_binop_rhs_complex(self, test_cb, op):
254 test_cb(op, -23 + 19j)
255
256 def _test_binop_rhs_zero_complex(self, test_cb, op):
257 test_cb(op, 0j)
258
259 def _test_binop_rhs_pos_vfloat(self, test_cb, op):
260 test_cb(op, bt2.create_value(2.2))
261
262 def _test_binop_rhs_neg_vfloat(self, test_cb, op):
263 test_cb(op, bt2.create_value(-23.4))
264
265 def _test_binop_rhs_zero_vfloat(self, test_cb, op):
266 test_cb(op, bt2.create_value(0.0))
267
268 def _test_binop_type_false(self, op):
269 self._test_binop_rhs_false(self._test_binop_type, op)
270
271 def _test_binop_type_true(self, op):
272 self._test_binop_rhs_true(self._test_binop_type, op)
273
274 def _test_binop_type_pos_int(self, op):
275 self._test_binop_rhs_pos_int(self._test_binop_type, op)
276
277 def _test_binop_type_neg_int(self, op):
278 self._test_binop_rhs_neg_int(self._test_binop_type, op)
279
280 def _test_binop_type_zero_int(self, op):
281 self._test_binop_rhs_zero_int(self._test_binop_type, op)
282
283 def _test_binop_type_pos_vint(self, op):
284 self._test_binop_rhs_pos_vint(self._test_binop_type, op)
285
286 def _test_binop_type_neg_vint(self, op):
287 self._test_binop_rhs_neg_vint(self._test_binop_type, op)
288
289 def _test_binop_type_zero_vint(self, op):
290 self._test_binop_rhs_zero_vint(self._test_binop_type, op)
291
292 def _test_binop_type_pos_float(self, op):
293 self._test_binop_rhs_pos_float(self._test_binop_type, op)
294
295 def _test_binop_type_neg_float(self, op):
296 self._test_binop_rhs_neg_float(self._test_binop_type, op)
297
298 def _test_binop_type_zero_float(self, op):
299 self._test_binop_rhs_zero_float(self._test_binop_type, op)
300
301 def _test_binop_type_pos_vfloat(self, op):
302 self._test_binop_rhs_pos_vfloat(self._test_binop_type, op)
303
304 def _test_binop_type_neg_vfloat(self, op):
305 self._test_binop_rhs_neg_vfloat(self._test_binop_type, op)
306
307 def _test_binop_type_zero_vfloat(self, op):
308 self._test_binop_rhs_zero_vfloat(self._test_binop_type, op)
309
310 def _test_binop_type_complex(self, op):
311 self._test_binop_rhs_complex(self._test_binop_type, op)
312
313 def _test_binop_type_zero_complex(self, op):
314 self._test_binop_rhs_zero_complex(self._test_binop_type, op)
315
316 def _test_binop_value_false(self, op):
317 self._test_binop_rhs_false(self._test_binop_value, op)
318
319 def _test_binop_value_true(self, op):
320 self._test_binop_rhs_true(self._test_binop_value, op)
321
322 def _test_binop_value_pos_int(self, op):
323 self._test_binop_rhs_pos_int(self._test_binop_value, op)
324
325 def _test_binop_value_neg_int(self, op):
326 self._test_binop_rhs_neg_int(self._test_binop_value, op)
327
328 def _test_binop_value_zero_int(self, op):
329 self._test_binop_rhs_zero_int(self._test_binop_value, op)
330
331 def _test_binop_value_pos_vint(self, op):
332 self._test_binop_rhs_pos_vint(self._test_binop_value, op)
333
334 def _test_binop_value_neg_vint(self, op):
335 self._test_binop_rhs_neg_vint(self._test_binop_value, op)
336
337 def _test_binop_value_zero_vint(self, op):
338 self._test_binop_rhs_zero_vint(self._test_binop_value, op)
339
340 def _test_binop_value_pos_float(self, op):
341 self._test_binop_rhs_pos_float(self._test_binop_value, op)
342
343 def _test_binop_value_neg_float(self, op):
344 self._test_binop_rhs_neg_float(self._test_binop_value, op)
345
346 def _test_binop_value_zero_float(self, op):
347 self._test_binop_rhs_zero_float(self._test_binop_value, op)
348
349 def _test_binop_value_pos_vfloat(self, op):
350 self._test_binop_rhs_pos_vfloat(self._test_binop_value, op)
351
352 def _test_binop_value_neg_vfloat(self, op):
353 self._test_binop_rhs_neg_vfloat(self._test_binop_value, op)
354
355 def _test_binop_value_zero_vfloat(self, op):
356 self._test_binop_rhs_zero_vfloat(self._test_binop_value, op)
357
358 def _test_binop_value_complex(self, op):
359 self._test_binop_rhs_complex(self._test_binop_value, op)
360
361 def _test_binop_value_zero_complex(self, op):
362 self._test_binop_rhs_zero_complex(self._test_binop_value, op)
363
364 def _test_binop_lhs_addr_same_false(self, op):
365 self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op)
366
367 def _test_binop_lhs_addr_same_true(self, op):
368 self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op)
369
370 def _test_binop_lhs_addr_same_pos_int(self, op):
371 self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op)
372
373 def _test_binop_lhs_addr_same_neg_int(self, op):
374 self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op)
375
376 def _test_binop_lhs_addr_same_zero_int(self, op):
377 self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op)
378
379 def _test_binop_lhs_addr_same_pos_vint(self, op):
380 self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op)
381
382 def _test_binop_lhs_addr_same_neg_vint(self, op):
383 self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op)
384
385 def _test_binop_lhs_addr_same_zero_vint(self, op):
386 self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op)
387
388 def _test_binop_lhs_addr_same_pos_float(self, op):
389 self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op)
390
391 def _test_binop_lhs_addr_same_neg_float(self, op):
392 self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op)
393
394 def _test_binop_lhs_addr_same_zero_float(self, op):
395 self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op)
396
397 def _test_binop_lhs_addr_same_pos_vfloat(self, op):
398 self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op)
399
400 def _test_binop_lhs_addr_same_neg_vfloat(self, op):
401 self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op)
402
403 def _test_binop_lhs_addr_same_zero_vfloat(self, op):
404 self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op)
405
406 def _test_binop_lhs_addr_same_complex(self, op):
407 self._test_binop_rhs_complex(self._test_binop_lhs_addr_same, op)
408
409 def _test_binop_lhs_addr_same_zero_complex(self, op):
410 self._test_binop_rhs_zero_complex(self._test_binop_lhs_addr_same, op)
411
412 def _test_binop_lhs_value_same_false(self, op):
413 self._test_binop_rhs_false(self._test_binop_lhs_value_same, op)
414
415 def _test_binop_lhs_value_same_true(self, op):
416 self._test_binop_rhs_true(self._test_binop_lhs_value_same, op)
417
418 def _test_binop_lhs_value_same_pos_int(self, op):
419 self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op)
420
421 def _test_binop_lhs_value_same_neg_int(self, op):
422 self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op)
423
424 def _test_binop_lhs_value_same_zero_int(self, op):
425 self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op)
426
427 def _test_binop_lhs_value_same_pos_vint(self, op):
428 self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op)
429
430 def _test_binop_lhs_value_same_neg_vint(self, op):
431 self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op)
432
433 def _test_binop_lhs_value_same_zero_vint(self, op):
434 self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op)
435
436 def _test_binop_lhs_value_same_pos_float(self, op):
437 self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op)
438
439 def _test_binop_lhs_value_same_neg_float(self, op):
440 self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op)
441
442 def _test_binop_lhs_value_same_zero_float(self, op):
443 self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op)
444
445 def _test_binop_lhs_value_same_pos_vfloat(self, op):
446 self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op)
447
448 def _test_binop_lhs_value_same_neg_vfloat(self, op):
449 self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op)
450
451 def _test_binop_lhs_value_same_zero_vfloat(self, op):
452 self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op)
453
454 def _test_binop_lhs_value_same_complex(self, op):
455 self._test_binop_rhs_complex(self._test_binop_lhs_value_same, op)
456
457 def _test_binop_lhs_value_same_zero_complex(self, op):
458 self._test_binop_rhs_zero_complex(self._test_binop_lhs_value_same, op)
459
460 def test_bool_op(self):
461 self.assertEqual(bool(self._def), bool(self._def_value))
462
463 def test_int_op(self):
464 self.assertEqual(int(self._def), int(self._def_value))
465
466 def test_float_op(self):
467 self.assertEqual(float(self._def), float(self._def_value))
468
469 def test_complex_op(self):
470 self.assertEqual(complex(self._def), complex(self._def_value))
471
472 def test_str_op(self):
473 self.assertEqual(str(self._def), str(self._def_value))
474
475 def test_eq_none(self):
476 # Disable the "comparison to None" warning, as this is precisely what
477 # we want to test here.
478 self.assertFalse(self._def == None) # noqa: E711
479
480 def test_ne_none(self):
481 # Disable the "comparison to None" warning, as this is precisely what
482 # we want to test here.
483 self.assertTrue(self._def != None) # noqa: E711
484
485
486 # This is a list of binary operators used for
487 # _inject_numeric_testing_methods().
488 #
489 # Each entry is a pair of binary operator name (used as part of the
490 # created testing method's name) and operator function.
491 _BINOPS = (
492 ('lt', operator.lt),
493 ('le', operator.le),
494 ('eq', operator.eq),
495 ('ne', operator.ne),
496 ('ge', operator.ge),
497 ('gt', operator.gt),
498 ('add', operator.add),
499 ('radd', lambda a, b: operator.add(b, a)),
500 ('and', operator.and_),
501 ('rand', lambda a, b: operator.and_(b, a)),
502 ('floordiv', operator.floordiv),
503 ('rfloordiv', lambda a, b: operator.floordiv(b, a)),
504 ('lshift', operator.lshift),
505 ('rlshift', lambda a, b: operator.lshift(b, a)),
506 ('mod', operator.mod),
507 ('rmod', lambda a, b: operator.mod(b, a)),
508 ('mul', operator.mul),
509 ('rmul', lambda a, b: operator.mul(b, a)),
510 ('or', operator.or_),
511 ('ror', lambda a, b: operator.or_(b, a)),
512 ('pow', operator.pow),
513 ('rpow', lambda a, b: operator.pow(b, a)),
514 ('rshift', operator.rshift),
515 ('rrshift', lambda a, b: operator.rshift(b, a)),
516 ('sub', operator.sub),
517 ('rsub', lambda a, b: operator.sub(b, a)),
518 ('truediv', operator.truediv),
519 ('rtruediv', lambda a, b: operator.truediv(b, a)),
520 ('xor', operator.xor),
521 ('rxor', lambda a, b: operator.xor(b, a)),
522 )
523
524
525 # This is a list of unary operators used for
526 # _inject_numeric_testing_methods().
527 #
528 # Each entry is a pair of unary operator name (used as part of the
529 # created testing method's name) and operator function.
530 _UNARYOPS = (
531 ('neg', operator.neg),
532 ('pos', operator.pos),
533 ('abs', operator.abs),
534 ('invert', operator.invert),
535 ('round', round),
536 ('round_0', partial(round, ndigits=0)),
537 ('round_1', partial(round, ndigits=1)),
538 ('round_2', partial(round, ndigits=2)),
539 ('round_3', partial(round, ndigits=3)),
540 ('ceil', math.ceil),
541 ('floor', math.floor),
542 ('trunc', math.trunc),
543 )
544
545
546 # This function injects a bunch of testing methods to a numeric
547 # value test case.
548 #
549 # It is meant to be used like this:
550 #
551 # _inject_numeric_testing_methods(MyNumericValueTestCase)
552 #
553 # This function injects:
554 #
555 # * One testing method for each _TestNumericValue._test_binop_*()
556 # method, for each binary operator in the _BINOPS tuple.
557 #
558 # * One testing method for each _TestNumericValue._test_unaryop*()
559 # method, for each unary operator in the _UNARYOPS tuple.
560 def _inject_numeric_testing_methods(cls):
561 def test_binop_name(suffix):
562 return 'test_binop_{}_{}'.format(name, suffix)
563
564 def test_unaryop_name(suffix):
565 return 'test_unaryop_{}_{}'.format(name, suffix)
566
567 # inject testing methods for each binary operation
568 for name, binop in _BINOPS:
569 setattr(
570 cls,
571 test_binop_name('unknown'),
572 partialmethod(_TestNumericValue._test_binop_unknown, op=binop),
573 )
574 setattr(
575 cls,
576 test_binop_name('none'),
577 partialmethod(_TestNumericValue._test_binop_none, op=binop),
578 )
579 setattr(
580 cls,
581 test_binop_name('type_true'),
582 partialmethod(_TestNumericValue._test_binop_type_true, op=binop),
583 )
584 setattr(
585 cls,
586 test_binop_name('type_pos_int'),
587 partialmethod(_TestNumericValue._test_binop_type_pos_int, op=binop),
588 )
589 setattr(
590 cls,
591 test_binop_name('type_pos_vint'),
592 partialmethod(_TestNumericValue._test_binop_type_pos_vint, op=binop),
593 )
594 setattr(
595 cls,
596 test_binop_name('value_true'),
597 partialmethod(_TestNumericValue._test_binop_value_true, op=binop),
598 )
599 setattr(
600 cls,
601 test_binop_name('value_pos_int'),
602 partialmethod(_TestNumericValue._test_binop_value_pos_int, op=binop),
603 )
604 setattr(
605 cls,
606 test_binop_name('value_pos_vint'),
607 partialmethod(_TestNumericValue._test_binop_value_pos_vint, op=binop),
608 )
609 setattr(
610 cls,
611 test_binop_name('lhs_addr_same_true'),
612 partialmethod(_TestNumericValue._test_binop_lhs_addr_same_true, op=binop),
613 )
614 setattr(
615 cls,
616 test_binop_name('lhs_addr_same_pos_int'),
617 partialmethod(
618 _TestNumericValue._test_binop_lhs_addr_same_pos_int, op=binop
619 ),
620 )
621 setattr(
622 cls,
623 test_binop_name('lhs_addr_same_pos_vint'),
624 partialmethod(
625 _TestNumericValue._test_binop_lhs_addr_same_pos_vint, op=binop
626 ),
627 )
628 setattr(
629 cls,
630 test_binop_name('lhs_value_same_true'),
631 partialmethod(_TestNumericValue._test_binop_lhs_value_same_true, op=binop),
632 )
633 setattr(
634 cls,
635 test_binop_name('lhs_value_same_pos_int'),
636 partialmethod(
637 _TestNumericValue._test_binop_lhs_value_same_pos_int, op=binop
638 ),
639 )
640 setattr(
641 cls,
642 test_binop_name('lhs_value_same_pos_vint'),
643 partialmethod(
644 _TestNumericValue._test_binop_lhs_value_same_pos_vint, op=binop
645 ),
646 )
647 setattr(
648 cls,
649 test_binop_name('type_neg_int'),
650 partialmethod(_TestNumericValue._test_binop_type_neg_int, op=binop),
651 )
652 setattr(
653 cls,
654 test_binop_name('type_neg_vint'),
655 partialmethod(_TestNumericValue._test_binop_type_neg_vint, op=binop),
656 )
657 setattr(
658 cls,
659 test_binop_name('value_neg_int'),
660 partialmethod(_TestNumericValue._test_binop_value_neg_int, op=binop),
661 )
662 setattr(
663 cls,
664 test_binop_name('value_neg_vint'),
665 partialmethod(_TestNumericValue._test_binop_value_neg_vint, op=binop),
666 )
667 setattr(
668 cls,
669 test_binop_name('lhs_addr_same_neg_int'),
670 partialmethod(
671 _TestNumericValue._test_binop_lhs_addr_same_neg_int, op=binop
672 ),
673 )
674 setattr(
675 cls,
676 test_binop_name('lhs_addr_same_neg_vint'),
677 partialmethod(
678 _TestNumericValue._test_binop_lhs_addr_same_neg_vint, op=binop
679 ),
680 )
681 setattr(
682 cls,
683 test_binop_name('lhs_value_same_neg_int'),
684 partialmethod(
685 _TestNumericValue._test_binop_lhs_value_same_neg_int, op=binop
686 ),
687 )
688 setattr(
689 cls,
690 test_binop_name('lhs_value_same_neg_vint'),
691 partialmethod(
692 _TestNumericValue._test_binop_lhs_value_same_neg_vint, op=binop
693 ),
694 )
695 setattr(
696 cls,
697 test_binop_name('type_false'),
698 partialmethod(_TestNumericValue._test_binop_type_false, op=binop),
699 )
700 setattr(
701 cls,
702 test_binop_name('type_zero_int'),
703 partialmethod(_TestNumericValue._test_binop_type_zero_int, op=binop),
704 )
705 setattr(
706 cls,
707 test_binop_name('type_zero_vint'),
708 partialmethod(_TestNumericValue._test_binop_type_zero_vint, op=binop),
709 )
710 setattr(
711 cls,
712 test_binop_name('value_false'),
713 partialmethod(_TestNumericValue._test_binop_value_false, op=binop),
714 )
715 setattr(
716 cls,
717 test_binop_name('value_zero_int'),
718 partialmethod(_TestNumericValue._test_binop_value_zero_int, op=binop),
719 )
720 setattr(
721 cls,
722 test_binop_name('value_zero_vint'),
723 partialmethod(_TestNumericValue._test_binop_value_zero_vint, op=binop),
724 )
725 setattr(
726 cls,
727 test_binop_name('lhs_addr_same_false'),
728 partialmethod(_TestNumericValue._test_binop_lhs_addr_same_false, op=binop),
729 )
730 setattr(
731 cls,
732 test_binop_name('lhs_addr_same_zero_int'),
733 partialmethod(
734 _TestNumericValue._test_binop_lhs_addr_same_zero_int, op=binop
735 ),
736 )
737 setattr(
738 cls,
739 test_binop_name('lhs_addr_same_zero_vint'),
740 partialmethod(
741 _TestNumericValue._test_binop_lhs_addr_same_zero_vint, op=binop
742 ),
743 )
744 setattr(
745 cls,
746 test_binop_name('lhs_value_same_false'),
747 partialmethod(_TestNumericValue._test_binop_lhs_value_same_false, op=binop),
748 )
749 setattr(
750 cls,
751 test_binop_name('lhs_value_same_zero_int'),
752 partialmethod(
753 _TestNumericValue._test_binop_lhs_value_same_zero_int, op=binop
754 ),
755 )
756 setattr(
757 cls,
758 test_binop_name('lhs_value_same_zero_vint'),
759 partialmethod(
760 _TestNumericValue._test_binop_lhs_value_same_zero_vint, op=binop
761 ),
762 )
763 setattr(
764 cls,
765 test_binop_name('type_neg_float'),
766 partialmethod(_TestNumericValue._test_binop_type_neg_float, op=binop),
767 )
768 setattr(
769 cls,
770 test_binop_name('type_neg_vfloat'),
771 partialmethod(_TestNumericValue._test_binop_type_neg_vfloat, op=binop),
772 )
773 setattr(
774 cls,
775 test_binop_name('value_neg_float'),
776 partialmethod(_TestNumericValue._test_binop_value_neg_float, op=binop),
777 )
778 setattr(
779 cls,
780 test_binop_name('value_neg_vfloat'),
781 partialmethod(_TestNumericValue._test_binop_value_neg_vfloat, op=binop),
782 )
783 setattr(
784 cls,
785 test_binop_name('lhs_addr_same_neg_float'),
786 partialmethod(
787 _TestNumericValue._test_binop_lhs_addr_same_neg_float, op=binop
788 ),
789 )
790 setattr(
791 cls,
792 test_binop_name('lhs_addr_same_neg_vfloat'),
793 partialmethod(
794 _TestNumericValue._test_binop_lhs_addr_same_neg_vfloat, op=binop
795 ),
796 )
797 setattr(
798 cls,
799 test_binop_name('lhs_value_same_neg_float'),
800 partialmethod(
801 _TestNumericValue._test_binop_lhs_value_same_neg_float, op=binop
802 ),
803 )
804 setattr(
805 cls,
806 test_binop_name('lhs_value_same_neg_vfloat'),
807 partialmethod(
808 _TestNumericValue._test_binop_lhs_value_same_neg_vfloat, op=binop
809 ),
810 )
811 setattr(
812 cls,
813 test_binop_name('type_pos_float'),
814 partialmethod(_TestNumericValue._test_binop_type_pos_float, op=binop),
815 )
816 setattr(
817 cls,
818 test_binop_name('type_pos_vfloat'),
819 partialmethod(_TestNumericValue._test_binop_type_pos_vfloat, op=binop),
820 )
821 setattr(
822 cls,
823 test_binop_name('value_pos_float'),
824 partialmethod(_TestNumericValue._test_binop_value_pos_float, op=binop),
825 )
826 setattr(
827 cls,
828 test_binop_name('value_pos_vfloat'),
829 partialmethod(_TestNumericValue._test_binop_value_pos_vfloat, op=binop),
830 )
831 setattr(
832 cls,
833 test_binop_name('lhs_addr_same_pos_float'),
834 partialmethod(
835 _TestNumericValue._test_binop_lhs_addr_same_pos_float, op=binop
836 ),
837 )
838 setattr(
839 cls,
840 test_binop_name('lhs_addr_same_pos_vfloat'),
841 partialmethod(
842 _TestNumericValue._test_binop_lhs_addr_same_pos_vfloat, op=binop
843 ),
844 )
845 setattr(
846 cls,
847 test_binop_name('lhs_value_same_pos_float'),
848 partialmethod(
849 _TestNumericValue._test_binop_lhs_value_same_pos_float, op=binop
850 ),
851 )
852 setattr(
853 cls,
854 test_binop_name('lhs_value_same_pos_vfloat'),
855 partialmethod(
856 _TestNumericValue._test_binop_lhs_value_same_pos_vfloat, op=binop
857 ),
858 )
859 setattr(
860 cls,
861 test_binop_name('type_zero_float'),
862 partialmethod(_TestNumericValue._test_binop_type_zero_float, op=binop),
863 )
864 setattr(
865 cls,
866 test_binop_name('type_zero_vfloat'),
867 partialmethod(_TestNumericValue._test_binop_type_zero_vfloat, op=binop),
868 )
869 setattr(
870 cls,
871 test_binop_name('value_zero_float'),
872 partialmethod(_TestNumericValue._test_binop_value_zero_float, op=binop),
873 )
874 setattr(
875 cls,
876 test_binop_name('value_zero_vfloat'),
877 partialmethod(_TestNumericValue._test_binop_value_zero_vfloat, op=binop),
878 )
879 setattr(
880 cls,
881 test_binop_name('lhs_addr_same_zero_float'),
882 partialmethod(
883 _TestNumericValue._test_binop_lhs_addr_same_zero_float, op=binop
884 ),
885 )
886 setattr(
887 cls,
888 test_binop_name('lhs_addr_same_zero_vfloat'),
889 partialmethod(
890 _TestNumericValue._test_binop_lhs_addr_same_zero_vfloat, op=binop
891 ),
892 )
893 setattr(
894 cls,
895 test_binop_name('lhs_value_same_zero_float'),
896 partialmethod(
897 _TestNumericValue._test_binop_lhs_value_same_zero_float, op=binop
898 ),
899 )
900 setattr(
901 cls,
902 test_binop_name('lhs_value_same_zero_vfloat'),
903 partialmethod(
904 _TestNumericValue._test_binop_lhs_value_same_zero_vfloat, op=binop
905 ),
906 )
907 setattr(
908 cls,
909 test_binop_name('type_complex'),
910 partialmethod(_TestNumericValue._test_binop_type_complex, op=binop),
911 )
912 setattr(
913 cls,
914 test_binop_name('type_zero_complex'),
915 partialmethod(_TestNumericValue._test_binop_type_zero_complex, op=binop),
916 )
917 setattr(
918 cls,
919 test_binop_name('value_complex'),
920 partialmethod(_TestNumericValue._test_binop_value_complex, op=binop),
921 )
922 setattr(
923 cls,
924 test_binop_name('value_zero_complex'),
925 partialmethod(_TestNumericValue._test_binop_value_zero_complex, op=binop),
926 )
927 setattr(
928 cls,
929 test_binop_name('lhs_addr_same_complex'),
930 partialmethod(
931 _TestNumericValue._test_binop_lhs_addr_same_complex, op=binop
932 ),
933 )
934 setattr(
935 cls,
936 test_binop_name('lhs_addr_same_zero_complex'),
937 partialmethod(
938 _TestNumericValue._test_binop_lhs_addr_same_zero_complex, op=binop
939 ),
940 )
941 setattr(
942 cls,
943 test_binop_name('lhs_value_same_complex'),
944 partialmethod(
945 _TestNumericValue._test_binop_lhs_value_same_complex, op=binop
946 ),
947 )
948 setattr(
949 cls,
950 test_binop_name('lhs_value_same_zero_complex'),
951 partialmethod(
952 _TestNumericValue._test_binop_lhs_value_same_zero_complex, op=binop
953 ),
954 )
955
956 # inject testing methods for each unary operation
957 for name, unaryop in _UNARYOPS:
958 setattr(
959 cls,
960 test_unaryop_name('type'),
961 partialmethod(_TestNumericValue._test_unaryop_type, op=unaryop),
962 )
963 setattr(
964 cls,
965 test_unaryop_name('value'),
966 partialmethod(_TestNumericValue._test_unaryop_value, op=unaryop),
967 )
968 setattr(
969 cls,
970 test_unaryop_name('addr_same'),
971 partialmethod(_TestNumericValue._test_unaryop_addr_same, op=unaryop),
972 )
973 setattr(
974 cls,
975 test_unaryop_name('value_same'),
976 partialmethod(_TestNumericValue._test_unaryop_value_same, op=unaryop),
977 )
978
979
980 class CreateValueFuncTestCase(unittest.TestCase):
981 def test_create_none(self):
982 v = bt2.create_value(None)
983 self.assertIsNone(v)
984
985 def test_create_bool_false(self):
986 v = bt2.create_value(False)
987 self.assertIsInstance(v, bt2.BoolValue)
988 self.assertFalse(v)
989
990 def test_create_bool_true(self):
991 v = bt2.create_value(True)
992 self.assertIsInstance(v, bt2.BoolValue)
993 self.assertTrue(v)
994
995 def test_create_int_pos(self):
996 raw = 23
997 v = bt2.create_value(raw)
998 self.assertIsInstance(v, bt2.SignedIntegerValue)
999 self.assertEqual(v, raw)
1000
1001 def test_create_int_neg(self):
1002 raw = -23
1003 v = bt2.create_value(raw)
1004 self.assertIsInstance(v, bt2.SignedIntegerValue)
1005 self.assertEqual(v, raw)
1006
1007 def test_create_float_pos(self):
1008 raw = 17.5
1009 v = bt2.create_value(raw)
1010 self.assertIsInstance(v, bt2.RealValue)
1011 self.assertEqual(v, raw)
1012
1013 def test_create_float_neg(self):
1014 raw = -17.5
1015 v = bt2.create_value(raw)
1016 self.assertIsInstance(v, bt2.RealValue)
1017 self.assertEqual(v, raw)
1018
1019 def test_create_string(self):
1020 raw = 'salut'
1021 v = bt2.create_value(raw)
1022 self.assertIsInstance(v, bt2.StringValue)
1023 self.assertEqual(v, raw)
1024
1025 def test_create_string_empty(self):
1026 raw = ''
1027 v = bt2.create_value(raw)
1028 self.assertIsInstance(v, bt2.StringValue)
1029 self.assertEqual(v, raw)
1030
1031 def test_create_array_from_list(self):
1032 raw = [1, 2, 3]
1033 v = bt2.create_value(raw)
1034 self.assertIsInstance(v, bt2.ArrayValue)
1035 self.assertEqual(v, raw)
1036
1037 def test_create_array_from_tuple(self):
1038 raw = 4, 5, 6
1039 v = bt2.create_value(raw)
1040 self.assertIsInstance(v, bt2.ArrayValue)
1041 self.assertEqual(v, raw)
1042
1043 def test_create_array_from_empty_list(self):
1044 raw = []
1045 v = bt2.create_value(raw)
1046 self.assertIsInstance(v, bt2.ArrayValue)
1047 self.assertEqual(v, raw)
1048
1049 def test_create_array_from_empty_tuple(self):
1050 raw = ()
1051 v = bt2.create_value(raw)
1052 self.assertIsInstance(v, bt2.ArrayValue)
1053 self.assertEqual(v, raw)
1054
1055 def test_create_map(self):
1056 raw = {'salut': 23}
1057 v = bt2.create_value(raw)
1058 self.assertIsInstance(v, bt2.MapValue)
1059 self.assertEqual(v, raw)
1060
1061 def test_create_map_empty(self):
1062 raw = {}
1063 v = bt2.create_value(raw)
1064 self.assertIsInstance(v, bt2.MapValue)
1065 self.assertEqual(v, raw)
1066
1067 def test_create_vfalse(self):
1068 v = bt2.create_value(bt2.create_value(False))
1069 self.assertIsInstance(v, bt2.BoolValue)
1070 self.assertFalse(v)
1071
1072 def test_create_invalid(self):
1073 class A:
1074 pass
1075
1076 a = A()
1077
1078 with self.assertRaisesRegex(
1079 TypeError, "cannot create value object from 'A' object"
1080 ):
1081 bt2.create_value(a)
1082
1083
1084 def _create_const_value(value):
1085 class MySink(bt2._UserSinkComponent):
1086 def _user_consume(self):
1087 pass
1088
1089 @classmethod
1090 def _user_query(cls, priv_query_exec, obj, params, method_obj):
1091 nonlocal value
1092 return {'my_value': value}
1093
1094 res = bt2.QueryExecutor(MySink, 'obj', None).query()
1095 return res['my_value']
1096
1097
1098 class BoolValueTestCase(_TestNumericValue, unittest.TestCase):
1099 def setUp(self):
1100 self._f = bt2.BoolValue(False)
1101 self._t = bt2.BoolValue(True)
1102 self._def = self._f
1103 self._def_value = False
1104 self._def_new_value = True
1105
1106 def tearDown(self):
1107 del self._f
1108 del self._t
1109 del self._def
1110
1111 def _assert_expecting_bool(self):
1112 return self.assertRaisesRegex(TypeError, r"expecting a 'bool' object")
1113
1114 def test_create_default(self):
1115 b = bt2.BoolValue()
1116 self.assertFalse(b)
1117
1118 def test_create_false(self):
1119 self.assertFalse(self._f)
1120
1121 def test_create_true(self):
1122 self.assertTrue(self._t)
1123
1124 def test_create_from_vfalse(self):
1125 b = bt2.BoolValue(self._f)
1126 self.assertFalse(b)
1127
1128 def test_create_from_vtrue(self):
1129 b = bt2.BoolValue(self._t)
1130 self.assertTrue(b)
1131
1132 def test_create_from_int_non_zero(self):
1133 with self.assertRaises(TypeError):
1134 bt2.BoolValue(23)
1135
1136 def test_create_from_int_zero(self):
1137 with self.assertRaises(TypeError):
1138 bt2.BoolValue(0)
1139
1140 def test_assign_true(self):
1141 b = bt2.BoolValue()
1142 b.value = True
1143 self.assertTrue(b)
1144
1145 def test_assign_false(self):
1146 b = bt2.BoolValue()
1147 b.value = False
1148 self.assertFalse(b)
1149
1150 def test_assign_vtrue(self):
1151 b = bt2.BoolValue()
1152 b.value = self._t
1153 self.assertTrue(b)
1154
1155 def test_assign_vfalse(self):
1156 b = bt2.BoolValue()
1157 b.value = False
1158 self.assertFalse(b)
1159
1160 def test_assign_int(self):
1161 with self.assertRaises(TypeError):
1162 b = bt2.BoolValue()
1163 b.value = 23
1164
1165 def test_bool_op(self):
1166 self.assertEqual(bool(self._def), bool(self._def_value))
1167
1168 def test_str_op(self):
1169 self.assertEqual(str(self._def), str(self._def_value))
1170
1171 def test_eq_none(self):
1172 # Disable the "comparison to None" warning, as this is precisely what
1173 # we want to test here.
1174 self.assertFalse(self._def == None) # noqa: E711
1175
1176 def test_ne_none(self):
1177 # Disable the "comparison to None" warning, as this is precisely what
1178 # we want to test here.
1179 self.assertTrue(self._def != None) # noqa: E711
1180
1181 def test_vfalse_eq_false(self):
1182 self.assertEqual(self._f, False)
1183
1184 def test_vfalse_ne_true(self):
1185 self.assertNotEqual(self._f, True)
1186
1187 def test_vtrue_eq_true(self):
1188 self.assertEqual(self._t, True)
1189
1190 def test_vtrue_ne_false(self):
1191 self.assertNotEqual(self._t, False)
1192
1193
1194 _inject_numeric_testing_methods(BoolValueTestCase)
1195
1196
1197 class _TestIntegerValue(_TestNumericValue):
1198 def setUp(self):
1199 self._pv = 23
1200 self._ip = self._CLS(self._pv)
1201 self._def = self._ip
1202 self._def_value = self._pv
1203 self._def_new_value = 101
1204
1205 def tearDown(self):
1206 del self._ip
1207 del self._def
1208 del self._def_value
1209
1210 def _assert_expecting_int(self):
1211 return self.assertRaisesRegex(TypeError, r'expecting an integral number object')
1212
1213 def _assert_expecting_int64(self):
1214 return self.assertRaisesRegex(
1215 ValueError, r"expecting a signed 64-bit integral value"
1216 )
1217
1218 def _assert_expecting_uint64(self):
1219 return self.assertRaisesRegex(
1220 ValueError, r"expecting an unsigned 64-bit integral value"
1221 )
1222
1223 def test_create_default(self):
1224 i = self._CLS()
1225 self.assertEqual(i, 0)
1226
1227 def test_create_pos(self):
1228 self.assertEqual(self._ip, self._pv)
1229
1230 def test_create_neg(self):
1231 self.assertEqual(self._in, self._nv)
1232
1233 def test_create_from_vint(self):
1234 i = self._CLS(self._ip)
1235 self.assertEqual(i, self._pv)
1236
1237 def test_create_from_false(self):
1238 i = self._CLS(False)
1239 self.assertFalse(i)
1240
1241 def test_create_from_true(self):
1242 i = self._CLS(True)
1243 self.assertTrue(i)
1244
1245 def test_create_from_unknown(self):
1246 class A:
1247 pass
1248
1249 with self._assert_expecting_int():
1250 self._CLS(A())
1251
1252 def test_create_from_varray(self):
1253 with self._assert_expecting_int():
1254 self._CLS(bt2.ArrayValue())
1255
1256 def test_assign_true(self):
1257 raw = True
1258 self._def.value = raw
1259 self.assertEqual(self._def, raw)
1260
1261 def test_assign_false(self):
1262 raw = False
1263 self._def.value = raw
1264 self.assertEqual(self._def, raw)
1265
1266 def test_assign_pos_int(self):
1267 raw = 477
1268 self._def.value = raw
1269 self.assertEqual(self._def, raw)
1270
1271 def test_assign_vint(self):
1272 raw = 999
1273 self._def.value = bt2.create_value(raw)
1274 self.assertEqual(self._def, raw)
1275
1276
1277 class SignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase):
1278 _CLS = bt2.SignedIntegerValue
1279
1280 def setUp(self):
1281 super().setUp()
1282 self._nv = -52
1283 self._in = self._CLS(self._nv)
1284 self._def_new_value = -101
1285
1286 def tearDown(self):
1287 super().tearDown()
1288 del self._in
1289
1290 def test_create_neg(self):
1291 self.assertEqual(self._in, self._nv)
1292
1293 def test_create_pos_too_big(self):
1294 with self._assert_expecting_int64():
1295 self._CLS(2 ** 63)
1296
1297 def test_create_neg_too_big(self):
1298 with self._assert_expecting_int64():
1299 self._CLS(-(2 ** 63) - 1)
1300
1301 def test_assign_neg_int(self):
1302 raw = -13
1303 self._def.value = raw
1304 self.assertEqual(self._def, raw)
1305
1306 def test_compare_big_int(self):
1307 # Larger than the IEEE 754 double-precision exact representation of
1308 # integers.
1309 raw = (2 ** 53) + 1
1310 v = bt2.create_value(raw)
1311 self.assertEqual(v, raw)
1312
1313
1314 _inject_numeric_testing_methods(SignedIntegerValueTestCase)
1315
1316
1317 class UnsignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase):
1318 _CLS = bt2.UnsignedIntegerValue
1319
1320 def test_create_pos_too_big(self):
1321 with self._assert_expecting_uint64():
1322 self._CLS(2 ** 64)
1323
1324 def test_create_neg(self):
1325 with self._assert_expecting_uint64():
1326 self._CLS(-1)
1327
1328
1329 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase)
1330
1331
1332 class RealValueTestCase(_TestNumericValue, unittest.TestCase):
1333 def setUp(self):
1334 self._pv = 23.4
1335 self._nv = -52.7
1336 self._fp = bt2.RealValue(self._pv)
1337 self._fn = bt2.RealValue(self._nv)
1338 self._def = self._fp
1339 self._def_value = self._pv
1340 self._def_new_value = -101.88
1341
1342 def tearDown(self):
1343 del self._fp
1344 del self._fn
1345 del self._def
1346 del self._def_value
1347
1348 def _assert_expecting_float(self):
1349 return self.assertRaisesRegex(TypeError, r"expecting a real number object")
1350
1351 def _test_invalid_op(self, cb):
1352 with self.assertRaises(TypeError):
1353 cb()
1354
1355 def test_create_default(self):
1356 f = bt2.RealValue()
1357 self.assertEqual(f, 0.0)
1358
1359 def test_create_pos(self):
1360 self.assertEqual(self._fp, self._pv)
1361
1362 def test_create_neg(self):
1363 self.assertEqual(self._fn, self._nv)
1364
1365 def test_create_from_false(self):
1366 f = bt2.RealValue(False)
1367 self.assertFalse(f)
1368
1369 def test_create_from_true(self):
1370 f = bt2.RealValue(True)
1371 self.assertTrue(f)
1372
1373 def test_create_from_int(self):
1374 raw = 17
1375 f = bt2.RealValue(raw)
1376 self.assertEqual(f, float(raw))
1377
1378 def test_create_from_vint(self):
1379 raw = 17
1380 f = bt2.RealValue(bt2.create_value(raw))
1381 self.assertEqual(f, float(raw))
1382
1383 def test_create_from_vfloat(self):
1384 raw = 17.17
1385 f = bt2.RealValue(bt2.create_value(raw))
1386 self.assertEqual(f, raw)
1387
1388 def test_create_from_unknown(self):
1389 class A:
1390 pass
1391
1392 with self._assert_expecting_float():
1393 bt2.RealValue(A())
1394
1395 def test_create_from_varray(self):
1396 with self._assert_expecting_float():
1397 bt2.RealValue(bt2.ArrayValue())
1398
1399 def test_assign_true(self):
1400 self._def.value = True
1401 self.assertTrue(self._def)
1402
1403 def test_assign_false(self):
1404 self._def.value = False
1405 self.assertFalse(self._def)
1406
1407 def test_assign_pos_int(self):
1408 raw = 477
1409 self._def.value = raw
1410 self.assertEqual(self._def, float(raw))
1411
1412 def test_assign_neg_int(self):
1413 raw = -13
1414 self._def.value = raw
1415 self.assertEqual(self._def, float(raw))
1416
1417 def test_assign_vint(self):
1418 raw = 999
1419 self._def.value = bt2.create_value(raw)
1420 self.assertEqual(self._def, float(raw))
1421
1422 def test_assign_float(self):
1423 raw = -19.23
1424 self._def.value = raw
1425 self.assertEqual(self._def, raw)
1426
1427 def test_assign_vfloat(self):
1428 raw = 101.32
1429 self._def.value = bt2.create_value(raw)
1430 self.assertEqual(self._def, raw)
1431
1432 def test_invalid_lshift(self):
1433 self._test_invalid_op(lambda: self._def << 23)
1434
1435 def test_invalid_rshift(self):
1436 self._test_invalid_op(lambda: self._def >> 23)
1437
1438 def test_invalid_and(self):
1439 self._test_invalid_op(lambda: self._def & 23)
1440
1441 def test_invalid_or(self):
1442 self._test_invalid_op(lambda: self._def | 23)
1443
1444 def test_invalid_xor(self):
1445 self._test_invalid_op(lambda: self._def ^ 23)
1446
1447 def test_invalid_invert(self):
1448 self._test_invalid_op(lambda: ~self._def)
1449
1450
1451 _inject_numeric_testing_methods(RealValueTestCase)
1452
1453
1454 class StringValueTestCase(_TestCopySimple, unittest.TestCase):
1455 def setUp(self):
1456 self._def_value = 'Hello, World!'
1457 self._def = bt2.StringValue(self._def_value)
1458 self._def_const = _create_const_value(self._def_value)
1459 self._def_new_value = 'Yes!'
1460
1461 def tearDown(self):
1462 del self._def
1463
1464 def _assert_expecting_str(self):
1465 return self.assertRaises(TypeError)
1466
1467 def test_create_default(self):
1468 s = bt2.StringValue()
1469 self.assertEqual(s, '')
1470
1471 def test_create_from_str(self):
1472 raw = 'liberté'
1473 s = bt2.StringValue(raw)
1474 self.assertEqual(s, raw)
1475
1476 def test_create_from_vstr(self):
1477 raw = 'liberté'
1478 s = bt2.StringValue(bt2.create_value(raw))
1479 self.assertEqual(s, raw)
1480
1481 def test_create_from_unknown(self):
1482 class A:
1483 pass
1484
1485 with self._assert_expecting_str():
1486 bt2.StringValue(A())
1487
1488 def test_create_from_varray(self):
1489 with self._assert_expecting_str():
1490 bt2.StringValue(bt2.ArrayValue())
1491
1492 def test_assign_int(self):
1493 with self._assert_expecting_str():
1494 self._def.value = 283
1495
1496 def test_assign_str(self):
1497 raw = 'zorg'
1498 self._def = raw
1499 self.assertEqual(self._def, raw)
1500
1501 def test_assign_vstr(self):
1502 raw = 'zorg'
1503 self._def = bt2.create_value(raw)
1504 self.assertEqual(self._def, raw)
1505
1506 def test_eq(self):
1507 self.assertEqual(self._def, self._def_value)
1508
1509 def test_const_eq(self):
1510 self.assertEqual(self._def_const, self._def_value)
1511
1512 def test_eq_raw(self):
1513 self.assertNotEqual(self._def, 23)
1514
1515 def test_lt_vstring(self):
1516 s1 = bt2.StringValue('allo')
1517 s2 = bt2.StringValue('bateau')
1518 self.assertLess(s1, s2)
1519
1520 def test_lt_string(self):
1521 s1 = bt2.StringValue('allo')
1522 self.assertLess(s1, 'bateau')
1523
1524 def test_le_vstring(self):
1525 s1 = bt2.StringValue('allo')
1526 s2 = bt2.StringValue('bateau')
1527 self.assertLessEqual(s1, s2)
1528
1529 def test_le_string(self):
1530 s1 = bt2.StringValue('allo')
1531 self.assertLessEqual(s1, 'bateau')
1532
1533 def test_gt_vstring(self):
1534 s1 = bt2.StringValue('allo')
1535 s2 = bt2.StringValue('bateau')
1536 self.assertGreater(s2, s1)
1537
1538 def test_gt_string(self):
1539 s1 = bt2.StringValue('allo')
1540 self.assertGreater('bateau', s1)
1541
1542 def test_ge_vstring(self):
1543 s1 = bt2.StringValue('allo')
1544 s2 = bt2.StringValue('bateau')
1545 self.assertGreaterEqual(s2, s1)
1546
1547 def test_ge_string(self):
1548 s1 = bt2.StringValue('allo')
1549 self.assertGreaterEqual('bateau', s1)
1550
1551 def test_in_string(self):
1552 s1 = bt2.StringValue('beau grand bateau')
1553 self.assertIn('bateau', s1)
1554
1555 def test_in_vstring(self):
1556 s1 = bt2.StringValue('beau grand bateau')
1557 s2 = bt2.StringValue('bateau')
1558 self.assertIn(s2, s1)
1559
1560 def test_bool_op(self):
1561 self.assertEqual(bool(self._def), bool(self._def_value))
1562
1563 def test_str_op(self):
1564 self.assertEqual(str(self._def), str(self._def_value))
1565
1566 def test_len(self):
1567 self.assertEqual(len(self._def), len(self._def_value))
1568
1569 def test_getitem(self):
1570 self.assertEqual(self._def[5], self._def_value[5])
1571
1572 def test_const_getitem(self):
1573 self.assertEqual(self._def_const[5], self._def_value[5])
1574
1575 def test_iadd_str(self):
1576 to_append = 'meow meow meow'
1577 self._def += to_append
1578 self._def_value += to_append
1579 self.assertEqual(self._def, self._def_value)
1580
1581 def test_const_iadd_str(self):
1582 to_append = 'meow meow meow'
1583 with self.assertRaises(TypeError):
1584 self._def_const += to_append
1585
1586 self.assertEqual(self._def_const, self._def_value)
1587
1588 def test_append_vstr(self):
1589 to_append = 'meow meow meow'
1590 self._def += bt2.create_value(to_append)
1591 self._def_value += to_append
1592 self.assertEqual(self._def, self._def_value)
1593
1594
1595 class ArrayValueTestCase(_TestCopySimple, unittest.TestCase):
1596 def setUp(self):
1597 self._def_value = [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1598 self._def = bt2.ArrayValue(copy.deepcopy(self._def_value))
1599 self._def_const = _create_const_value(copy.deepcopy(self._def_value))
1600
1601 def tearDown(self):
1602 del self._def
1603
1604 def _modify_def(self):
1605 self._def[2] = 'xyz'
1606
1607 def _assert_type_error(self):
1608 return self.assertRaises(TypeError)
1609
1610 def test_create_default(self):
1611 a = bt2.ArrayValue()
1612 self.assertEqual(len(a), 0)
1613
1614 def test_create_from_array(self):
1615 self.assertEqual(self._def, self._def_value)
1616
1617 def test_create_from_tuple(self):
1618 t = 1, 2, False, None
1619 a = bt2.ArrayValue(t)
1620 self.assertEqual(a, t)
1621
1622 def test_create_from_varray(self):
1623 va = bt2.ArrayValue(copy.deepcopy(self._def_value))
1624 a = bt2.ArrayValue(va)
1625 self.assertEqual(va, a)
1626
1627 def test_create_from_unknown(self):
1628 class A:
1629 pass
1630
1631 with self._assert_type_error():
1632 bt2.ArrayValue(A())
1633
1634 def test_bool_op_true(self):
1635 self.assertTrue(bool(self._def))
1636
1637 def test_bool_op_false(self):
1638 self.assertFalse(bool(bt2.ArrayValue()))
1639
1640 def test_len(self):
1641 self.assertEqual(len(self._def), len(self._def_value))
1642
1643 def test_eq_int(self):
1644 self.assertNotEqual(self._def, 23)
1645
1646 def test_const_eq(self):
1647 a1 = _create_const_value([1, 2, 3])
1648 a2 = [1, 2, 3]
1649 self.assertEqual(a1, a2)
1650
1651 def test_eq_diff_len(self):
1652 a1 = bt2.create_value([1, 2, 3])
1653 a2 = bt2.create_value([1, 2])
1654 self.assertIs(type(a1), bt2.ArrayValue)
1655 self.assertIs(type(a2), bt2.ArrayValue)
1656 self.assertNotEqual(a1, a2)
1657
1658 def test_eq_diff_content_same_len(self):
1659 a1 = bt2.create_value([1, 2, 3])
1660 a2 = bt2.create_value([4, 5, 6])
1661 self.assertNotEqual(a1, a2)
1662
1663 def test_eq_same_content_same_len(self):
1664 raw = (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1665 a1 = bt2.ArrayValue(raw)
1666 a2 = bt2.ArrayValue(copy.deepcopy(raw))
1667 self.assertEqual(a1, a2)
1668
1669 def test_eq_non_sequence_iterable(self):
1670 dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)])
1671 a = bt2.ArrayValue((1, 3, 5))
1672 self.assertEqual(a, list(dct.keys()))
1673 self.assertNotEqual(a, dct)
1674
1675 def test_setitem_int(self):
1676 raw = 19
1677 self._def[2] = raw
1678 self.assertEqual(self._def[2], raw)
1679
1680 def test_setitem_vint(self):
1681 raw = 19
1682 self._def[2] = bt2.create_value(raw)
1683 self.assertEqual(self._def[2], raw)
1684
1685 def test_setitem_none(self):
1686 self._def[2] = None
1687 self.assertIsNone(self._def[2])
1688
1689 def test_setitem_index_wrong_type(self):
1690 with self._assert_type_error():
1691 self._def['yes'] = 23
1692
1693 def test_setitem_index_neg(self):
1694 with self.assertRaises(IndexError):
1695 self._def[-2] = 23
1696
1697 def test_setitem_index_out_of_range(self):
1698 with self.assertRaises(IndexError):
1699 self._def[len(self._def)] = 23
1700
1701 def test_const_setitem(self):
1702 with self.assertRaises(TypeError):
1703 self._def_const[2] = 19
1704
1705 def test_append_none(self):
1706 self._def.append(None)
1707 self.assertIsNone(self._def[len(self._def) - 1])
1708
1709 def test_append_int(self):
1710 raw = 145
1711 self._def.append(raw)
1712 self.assertEqual(self._def[len(self._def) - 1], raw)
1713
1714 def test_const_append(self):
1715 with self.assertRaises(AttributeError):
1716 self._def_const.append(12194)
1717
1718 def test_append_vint(self):
1719 raw = 145
1720 self._def.append(bt2.create_value(raw))
1721 self.assertEqual(self._def[len(self._def) - 1], raw)
1722
1723 def test_append_unknown(self):
1724 class A:
1725 pass
1726
1727 with self._assert_type_error():
1728 self._def.append(A())
1729
1730 def test_iadd(self):
1731 raw = 4, 5, True
1732 self._def += raw
1733 self.assertEqual(self._def[len(self._def) - 3], raw[0])
1734 self.assertEqual(self._def[len(self._def) - 2], raw[1])
1735 self.assertEqual(self._def[len(self._def) - 1], raw[2])
1736
1737 def test_const_iadd(self):
1738 with self.assertRaises(TypeError):
1739 self._def_const += 12194
1740
1741 def test_iadd_unknown(self):
1742 class A:
1743 pass
1744
1745 with self._assert_type_error():
1746 self._def += A()
1747
1748 def test_iadd_list_unknown(self):
1749 class A:
1750 pass
1751
1752 with self._assert_type_error():
1753 self._def += [A()]
1754
1755 def test_iter(self):
1756 for velem, elem in zip(self._def, self._def_value):
1757 self.assertEqual(velem, elem)
1758
1759 def test_const_iter(self):
1760 for velem, elem in zip(self._def_const, self._def_value):
1761 self.assertEqual(velem, elem)
1762
1763 def test_const_get_item(self):
1764 item1 = self._def_const[0]
1765 item2 = self._def_const[2]
1766 item3 = self._def_const[5]
1767 item4 = self._def_const[7]
1768 item5 = self._def_const[8]
1769
1770 self.assertEqual(item1, None)
1771
1772 self.assertIs(type(item2), bt2._BoolValueConst)
1773 self.assertEqual(item2, True)
1774
1775 self.assertIs(type(item3), bt2._SignedIntegerValueConst)
1776 self.assertEqual(item3, 42)
1777
1778 self.assertIs(type(item4), bt2._RealValueConst)
1779 self.assertEqual(item4, 23.17)
1780
1781 self.assertIs(type(item5), bt2._StringValueConst)
1782 self.assertEqual(item5, 'yes')
1783
1784
1785 class MapValueTestCase(_TestCopySimple, unittest.TestCase):
1786 def setUp(self):
1787 self._def_value = {
1788 'none': None,
1789 'false': False,
1790 'true': True,
1791 'neg-int': -23,
1792 'zero': 0,
1793 'pos-int': 42,
1794 'neg-float': -42.4,
1795 'pos-float': 23.17,
1796 'str': 'yes',
1797 }
1798 self._def = bt2.MapValue(copy.deepcopy(self._def_value))
1799 self._def_const = _create_const_value(self._def_value)
1800
1801 def tearDown(self):
1802 del self._def
1803
1804 def _modify_def(self):
1805 self._def['zero'] = 1
1806
1807 def test_create_default(self):
1808 m = bt2.MapValue()
1809 self.assertEqual(len(m), 0)
1810
1811 def test_create_from_dict(self):
1812 self.assertEqual(self._def, self._def_value)
1813
1814 def test_create_from_vmap(self):
1815 vm = bt2.MapValue(copy.deepcopy(self._def_value))
1816 m = bt2.MapValue(vm)
1817 self.assertEqual(vm, m)
1818
1819 def test_create_from_unknown(self):
1820 class A:
1821 pass
1822
1823 with self.assertRaises(AttributeError):
1824 bt2.MapValue(A())
1825
1826 def test_bool_op_true(self):
1827 self.assertTrue(bool(self._def))
1828
1829 def test_bool_op_false(self):
1830 self.assertFalse(bool(bt2.MapValue()))
1831
1832 def test_len(self):
1833 self.assertEqual(len(self._def), len(self._def_value))
1834
1835 def test_const_eq(self):
1836 a1 = _create_const_value({'a': 1, 'b': 2, 'c': 3})
1837 a2 = {'a': 1, 'b': 2, 'c': 3}
1838 self.assertEqual(a1, a2)
1839
1840 def test_eq_int(self):
1841 self.assertNotEqual(self._def, 23)
1842
1843 def test_eq_diff_len(self):
1844 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1845 a2 = bt2.create_value({'a': 1, 'b': 2})
1846 self.assertNotEqual(a1, a2)
1847
1848 def test_const_eq_diff_len(self):
1849 a1 = _create_const_value({'a': 1, 'b': 2, 'c': 3})
1850 a2 = _create_const_value({'a': 1, 'b': 2})
1851 self.assertNotEqual(a1, a2)
1852
1853 def test_eq_diff_content_same_len(self):
1854 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1855 a2 = bt2.create_value({'a': 4, 'b': 2, 'c': 3})
1856 self.assertNotEqual(a1, a2)
1857
1858 def test_const_eq_diff_content_same_len(self):
1859 a1 = _create_const_value({'a': 1, 'b': 2, 'c': 3})
1860 a2 = _create_const_value({'a': 4, 'b': 2, 'c': 3})
1861 self.assertNotEqual(a1, a2)
1862
1863 def test_eq_same_content_diff_keys(self):
1864 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1865 a2 = bt2.create_value({'a': 1, 'k': 2, 'c': 3})
1866 self.assertNotEqual(a1, a2)
1867
1868 def test_const_eq_same_content_diff_keys(self):
1869 a1 = _create_const_value({'a': 1, 'b': 2, 'c': 3})
1870 a2 = _create_const_value({'a': 1, 'k': 2, 'c': 3})
1871 self.assertNotEqual(a1, a2)
1872
1873 def test_eq_same_content_same_len(self):
1874 raw = {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1875 a1 = bt2.MapValue(raw)
1876 a2 = bt2.MapValue(copy.deepcopy(raw))
1877 self.assertEqual(a1, a2)
1878 self.assertEqual(a1, raw)
1879
1880 def test_const_eq_same_content_same_len(self):
1881 raw = {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1882 a1 = _create_const_value(raw)
1883 a2 = _create_const_value(copy.deepcopy(raw))
1884 self.assertEqual(a1, a2)
1885 self.assertEqual(a1, raw)
1886
1887 def test_setitem_int(self):
1888 raw = 19
1889 self._def['pos-int'] = raw
1890 self.assertEqual(self._def['pos-int'], raw)
1891
1892 def test_const_setitem_int(self):
1893 with self.assertRaises(TypeError):
1894 self._def_const['pos-int'] = 19
1895
1896 def test_setitem_vint(self):
1897 raw = 19
1898 self._def['pos-int'] = bt2.create_value(raw)
1899 self.assertEqual(self._def['pos-int'], raw)
1900
1901 def test_setitem_none(self):
1902 self._def['none'] = None
1903 self.assertIsNone(self._def['none'])
1904
1905 def test_setitem_new_int(self):
1906 old_len = len(self._def)
1907 self._def['new-int'] = 23
1908 self.assertEqual(self._def['new-int'], 23)
1909 self.assertEqual(len(self._def), old_len + 1)
1910
1911 def test_setitem_index_wrong_type(self):
1912 with self.assertRaises(TypeError):
1913 self._def[18] = 23
1914
1915 def test_iter(self):
1916 for vkey, vval in self._def.items():
1917 val = self._def_value[vkey]
1918 self.assertEqual(vval, val)
1919
1920 def test_const_iter(self):
1921 for vkey, vval in self._def_const.items():
1922 val = self._def_value[vkey]
1923 self.assertEqual(vval, val)
1924
1925 def test_get_item(self):
1926 i = self._def['pos-float']
1927 self.assertIs(type(i), bt2.RealValue)
1928 self.assertEqual(i, 23.17)
1929
1930 def test_const_get_item(self):
1931 item1 = self._def_const['none']
1932 item2 = self._def_const['true']
1933 item3 = self._def_const['pos-int']
1934 item4 = self._def_const['pos-float']
1935 item5 = self._def_const['str']
1936
1937 self.assertEqual(item1, None)
1938
1939 self.assertIs(type(item2), bt2._BoolValueConst)
1940 self.assertEqual(item2, True)
1941
1942 self.assertIs(type(item3), bt2._SignedIntegerValueConst)
1943 self.assertEqual(item3, 42)
1944
1945 self.assertIs(type(item4), bt2._RealValueConst)
1946 self.assertEqual(item4, 23.17)
1947
1948 self.assertIs(type(item5), bt2._StringValueConst)
1949 self.assertEqual(item5, 'yes')
1950
1951 def test_getitem_wrong_key(self):
1952 with self.assertRaises(KeyError):
1953 self._def['kilojoule']
1954
1955
1956 if __name__ == '__main__':
1957 unittest.main()
This page took 0.113913 seconds and 4 git commands to generate.