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