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