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