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