bt2: update value.py, make test_value pass
[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 type(self._def) is 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):
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
592 setattr(cls, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue._test_binop_invalid_unknown, op=binop))
593 setattr(cls, test_binop_name('invalid_none'), partialmethod(_TestNumericValue._test_binop_invalid_none, op=binop))
594 setattr(cls, test_binop_name('type_true'), partialmethod(_TestNumericValue._test_binop_type_true, op=binop))
595 setattr(cls, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue._test_binop_type_pos_int, op=binop))
596 setattr(cls, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue._test_binop_type_pos_vint, op=binop))
597 setattr(cls, test_binop_name('value_true'), partialmethod(_TestNumericValue._test_binop_value_true, op=binop))
598 setattr(cls, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue._test_binop_value_pos_int, op=binop))
599 setattr(cls, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue._test_binop_value_pos_vint, op=binop))
600 setattr(cls, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_true, op=binop))
601 setattr(cls, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_int, op=binop))
602 setattr(cls, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_vint, op=binop))
603 setattr(cls, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_true, op=binop))
604 setattr(cls, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_int, op=binop))
605 setattr(cls, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_vint, op=binop))
606 setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue._test_binop_type_neg_int, op=binop))
607 setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue._test_binop_type_neg_vint, op=binop))
608 setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue._test_binop_value_neg_int, op=binop))
609 setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue._test_binop_value_neg_vint, op=binop))
610 setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_int, op=binop))
611 setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vint, op=binop))
612 setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_int, op=binop))
613 setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vint, op=binop))
614 setattr(cls, test_binop_name('type_false'), partialmethod(_TestNumericValue._test_binop_type_false, op=binop))
615 setattr(cls, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue._test_binop_type_zero_int, op=binop))
616 setattr(cls, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue._test_binop_type_zero_vint, op=binop))
617 setattr(cls, test_binop_name('value_false'), partialmethod(_TestNumericValue._test_binop_value_false, op=binop))
618 setattr(cls, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue._test_binop_value_zero_int, op=binop))
619 setattr(cls, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue._test_binop_value_zero_vint, op=binop))
620 setattr(cls, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_false, op=binop))
621 setattr(cls, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_int, op=binop))
622 setattr(cls, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_vint, op=binop))
623 setattr(cls, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_false, op=binop))
624 setattr(cls, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_int, op=binop))
625 setattr(cls, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_vint, op=binop))
626 setattr(cls, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue._test_binop_type_pos_float, op=binop))
627 setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue._test_binop_type_neg_float, op=binop))
628 setattr(cls, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_type_pos_vfloat, op=binop))
629 setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_type_neg_vfloat, op=binop))
630 setattr(cls, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue._test_binop_value_pos_float, op=binop))
631 setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue._test_binop_value_neg_float, op=binop))
632 setattr(cls, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_value_pos_vfloat, 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_pos_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_float, op=binop))
635 setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_float, op=binop))
636 setattr(cls, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_vfloat, op=binop))
637 setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vfloat, op=binop))
638 setattr(cls, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_float, op=binop))
639 setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_float, op=binop))
640 setattr(cls, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_vfloat, op=binop))
641 setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vfloat, op=binop))
642 setattr(cls, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue._test_binop_type_zero_float, op=binop))
643 setattr(cls, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_type_zero_vfloat, op=binop))
644 setattr(cls, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue._test_binop_value_zero_float, op=binop))
645 setattr(cls, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_value_zero_vfloat, op=binop))
646 setattr(cls, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_float, op=binop))
647 setattr(cls, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_vfloat, op=binop))
648 setattr(cls, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_float, op=binop))
649 setattr(cls, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_vfloat, op=binop))
650
651 # inject testing methods for each unary operation
652 for name, unaryop in _UNARYOPS:
653 setattr(cls, test_unaryop_name('type'), partialmethod(_TestNumericValue._test_unaryop_type, op=unaryop))
654 setattr(cls, test_unaryop_name('value'), partialmethod(_TestNumericValue._test_unaryop_value, op=unaryop))
655 setattr(cls, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue._test_unaryop_addr_same, op=unaryop))
656 setattr(cls, test_unaryop_name('value_same'), partialmethod(_TestNumericValue._test_unaryop_value_same, op=unaryop))
657
658 # inject testing methods for each inplace binary operation
659 for name, ibinop in _IBINOPS:
660 setattr(cls, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericValue._test_ibinop_invalid_unknown, op=ibinop))
661 setattr(cls, test_ibinop_name('invalid_none'), partialmethod(_TestNumericValue._test_ibinop_invalid_none, op=ibinop))
662 setattr(cls, test_ibinop_name('type_true'), partialmethod(_TestNumericValue._test_ibinop_type_true, op=ibinop))
663 setattr(cls, test_ibinop_name('value_true'), partialmethod(_TestNumericValue._test_ibinop_value_true, op=ibinop))
664 setattr(cls, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericValue._test_ibinop_type_pos_int, op=ibinop))
665 setattr(cls, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericValue._test_ibinop_type_pos_vint, op=ibinop))
666 setattr(cls, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericValue._test_ibinop_value_pos_int, op=ibinop))
667 setattr(cls, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericValue._test_ibinop_value_pos_vint, op=ibinop))
668 setattr(cls, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericValue._test_ibinop_type_neg_int, op=ibinop))
669 setattr(cls, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericValue._test_ibinop_type_neg_vint, op=ibinop))
670 setattr(cls, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericValue._test_ibinop_value_neg_int, op=ibinop))
671 setattr(cls, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericValue._test_ibinop_value_neg_vint, op=ibinop))
672 setattr(cls, test_ibinop_name('type_false'), partialmethod(_TestNumericValue._test_ibinop_type_false, op=ibinop))
673 setattr(cls, test_ibinop_name('value_false'), partialmethod(_TestNumericValue._test_ibinop_value_false, op=ibinop))
674 setattr(cls, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericValue._test_ibinop_type_zero_int, op=ibinop))
675 setattr(cls, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericValue._test_ibinop_type_zero_vint, op=ibinop))
676 setattr(cls, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericValue._test_ibinop_value_zero_int, op=ibinop))
677 setattr(cls, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericValue._test_ibinop_value_zero_vint, op=ibinop))
678 setattr(cls, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericValue._test_ibinop_type_pos_float, op=ibinop))
679 setattr(cls, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericValue._test_ibinop_type_neg_float, op=ibinop))
680 setattr(cls, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericValue._test_ibinop_type_pos_vfloat, op=ibinop))
681 setattr(cls, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericValue._test_ibinop_type_neg_vfloat, op=ibinop))
682 setattr(cls, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericValue._test_ibinop_value_pos_float, op=ibinop))
683 setattr(cls, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericValue._test_ibinop_value_neg_float, op=ibinop))
684 setattr(cls, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericValue._test_ibinop_value_pos_vfloat, op=ibinop))
685 setattr(cls, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericValue._test_ibinop_value_neg_vfloat, op=ibinop))
686 setattr(cls, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericValue._test_ibinop_type_zero_float, op=ibinop))
687 setattr(cls, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericValue._test_ibinop_type_zero_vfloat, op=ibinop))
688 setattr(cls, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericValue._test_ibinop_value_zero_float, op=ibinop))
689 setattr(cls, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericValue._test_ibinop_value_zero_vfloat, op=ibinop))
690
691
692 class CreateValueFuncTestCase(unittest.TestCase):
693 def test_create_none(self):
694 v = bt2.create_value(None)
695 self.assertIsNone(v)
696
697 def test_create_bool_false(self):
698 v = bt2.create_value(False)
699 self.assertIsInstance(v, bt2.BoolValue)
700 self.assertFalse(v)
701
702 def test_create_bool_true(self):
703 v = bt2.create_value(True)
704 self.assertIsInstance(v, bt2.BoolValue)
705 self.assertTrue(v)
706
707 def test_create_int_pos(self):
708 raw = 23
709 v = bt2.create_value(raw)
710 self.assertIsInstance(v, bt2.IntegerValue)
711 self.assertEqual(v, raw)
712
713 def test_create_int_neg(self):
714 raw = -23
715 v = bt2.create_value(raw)
716 self.assertIsInstance(v, bt2.IntegerValue)
717 self.assertEqual(v, raw)
718
719 def test_create_float_pos(self):
720 raw = 17.5
721 v = bt2.create_value(raw)
722 self.assertIsInstance(v, bt2.RealValue)
723 self.assertEqual(v, raw)
724
725 def test_create_float_neg(self):
726 raw = -17.5
727 v = bt2.create_value(raw)
728 self.assertIsInstance(v, bt2.RealValue)
729 self.assertEqual(v, raw)
730
731 def test_create_string(self):
732 raw = 'salut'
733 v = bt2.create_value(raw)
734 self.assertIsInstance(v, bt2.StringValue)
735 self.assertEqual(v, raw)
736
737 def test_create_string_empty(self):
738 raw = ''
739 v = bt2.create_value(raw)
740 self.assertIsInstance(v, bt2.StringValue)
741 self.assertEqual(v, raw)
742
743 def test_create_array_from_list(self):
744 raw = [1, 2, 3]
745 v = bt2.create_value(raw)
746 self.assertIsInstance(v, bt2.ArrayValue)
747 self.assertEqual(v, raw)
748
749 def test_create_array_from_tuple(self):
750 raw = 4, 5, 6
751 v = bt2.create_value(raw)
752 self.assertIsInstance(v, bt2.ArrayValue)
753 self.assertEqual(v, raw)
754
755 def test_create_array_from_empty_list(self):
756 raw = []
757 v = bt2.create_value(raw)
758 self.assertIsInstance(v, bt2.ArrayValue)
759 self.assertEqual(v, raw)
760
761 def test_create_array_from_empty_tuple(self):
762 raw = ()
763 v = bt2.create_value(raw)
764 self.assertIsInstance(v, bt2.ArrayValue)
765 self.assertEqual(v, raw)
766
767 def test_create_map(self):
768 raw = {'salut': 23}
769 v = bt2.create_value(raw)
770 self.assertIsInstance(v, bt2.MapValue)
771 self.assertEqual(v, raw)
772
773 def test_create_map_empty(self):
774 raw = {}
775 v = bt2.create_value(raw)
776 self.assertIsInstance(v, bt2.MapValue)
777 self.assertEqual(v, raw)
778
779 def test_create_vfalse(self):
780 v = bt2.create_value(bt2.create_value(False))
781 self.assertIsInstance(v, bt2.BoolValue)
782 self.assertFalse(v)
783
784 def test_create_invalid(self):
785 class A:
786 pass
787
788 a = A()
789
790 with self.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm:
791 v = bt2.create_value(a)
792
793
794 class BoolValueTestCase(_TestCopySimple, unittest.TestCase):
795 def setUp(self):
796 self._f = bt2.BoolValue(False)
797 self._t = bt2.BoolValue(True)
798 self._def = self._f
799 self._def_value = False
800 self._def_new_value = True
801
802 def tearDown(self):
803 del self._f
804 del self._t
805 del self._def
806
807 def _assert_expecting_bool(self):
808 return self.assertRaisesRegex(TypeError, r"expecting a 'bool' object")
809
810 def test_create_default(self):
811 b = bt2.BoolValue()
812 self.assertFalse(b)
813
814 def test_create_false(self):
815 self.assertFalse(self._f)
816
817 def test_create_true(self):
818 self.assertTrue(self._t)
819
820 def test_create_from_vfalse(self):
821 b = bt2.BoolValue(self._f)
822 self.assertFalse(b)
823
824 def test_create_from_vtrue(self):
825 b = bt2.BoolValue(self._t)
826 self.assertTrue(b)
827
828 def test_create_from_int_non_zero(self):
829 with self.assertRaises(TypeError):
830 b = bt2.BoolValue(23)
831
832 def test_create_from_int_zero(self):
833 with self.assertRaises(TypeError):
834 b = bt2.BoolValue(0)
835
836 def test_assign_true(self):
837 b = bt2.BoolValue()
838 b.value = True
839 self.assertTrue(b)
840
841 def test_assign_false(self):
842 b = bt2.BoolValue()
843 b.value = False
844 self.assertFalse(b)
845
846 def test_assign_vtrue(self):
847 b = bt2.BoolValue()
848 b.value = self._t
849 self.assertTrue(b)
850
851 def test_assign_vfalse(self):
852 b = bt2.BoolValue()
853 b.value = False
854 self.assertFalse(b)
855
856 def test_assign_int(self):
857 with self.assertRaises(TypeError):
858 b = bt2.BoolValue()
859 b.value = 23
860
861 def test_bool_op(self):
862 self.assertEqual(bool(self._def), bool(self._def_value))
863
864 def test_str_op(self):
865 self.assertEqual(str(self._def), str(self._def_value))
866
867 def test_eq_none(self):
868 self.assertFalse(self._def == None)
869
870 def test_ne_none(self):
871 self.assertTrue(self._def != None)
872
873 def test_vfalse_eq_false(self):
874 self.assertEqual(self._f, False)
875
876 def test_vfalse_ne_true(self):
877 self.assertNotEqual(self._f, True)
878
879 def test_vtrue_eq_true(self):
880 self.assertEqual(self._t, True)
881
882 def test_vtrue_ne_false(self):
883 self.assertNotEqual(self._t, False)
884
885
886 class IntegerValueTestCase(_TestNumericValue, unittest.TestCase):
887 def setUp(self):
888 self._pv = 23
889 self._nv = -52
890 self._ip = bt2.IntegerValue(self._pv)
891 self._in = bt2.IntegerValue(self._nv)
892 self._def = self._ip
893 self._def_value = self._pv
894 self._def_new_value = -101
895
896 def tearDown(self):
897 del self._ip
898 del self._in
899 del self._def
900 del self._def_value
901
902 def _assert_expecting_int(self):
903 return self.assertRaisesRegex(TypeError, r'expecting a number object')
904
905 def _assert_expecting_int64(self):
906 return self.assertRaisesRegex(ValueError, r"expecting a signed 64-bit integral value")
907
908 def _assert_expecting_uint64(self):
909 return self.assertRaisesRegex(ValueError, r"expecting an unsigned 64-bit integral value")
910
911 def test_create_default(self):
912 i = bt2.IntegerValue()
913 self.assertEqual(i, 0)
914
915 def test_create_pos(self):
916 self.assertEqual(self._ip, self._pv)
917
918 def test_create_neg(self):
919 self.assertEqual(self._in, self._nv)
920
921 def test_create_pos_too_big(self):
922 with self._assert_expecting_int64():
923 i = bt2.IntegerValue(2 ** 63)
924
925 def test_create_neg_too_big(self):
926 with self._assert_expecting_int64():
927 i = bt2.IntegerValue(-(2 ** 63) - 1)
928
929 def test_create_from_vint(self):
930 i = bt2.IntegerValue(self._ip)
931 self.assertEqual(i, self._pv)
932
933 def test_create_from_false(self):
934 i = bt2.IntegerValue(False)
935 self.assertFalse(i)
936
937 def test_create_from_true(self):
938 i = bt2.IntegerValue(True)
939 self.assertTrue(i)
940
941 def test_create_from_float(self):
942 i = bt2.IntegerValue(99.6)
943 self.assertEqual(i, 99)
944
945 def test_create_from_vfloat(self):
946 f = bt2.create_value(17.5)
947 i = bt2.IntegerValue(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 = bt2.IntegerValue(A())
956
957 def test_create_from_varray(self):
958 with self._assert_expecting_int():
959 i = bt2.IntegerValue(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_neg_int(self):
977 raw = -13
978 self._def.value = raw
979 self.assertEqual(self._def, raw)
980
981 def test_assign_vint(self):
982 raw = 999
983 self._def.value = bt2.create_value(raw)
984 self.assertEqual(self._def, raw)
985
986 def test_assign_vfloat(self):
987 raw = 123.456
988 self._def.value = bt2.create_value(raw)
989 self.assertEqual(self._def, int(raw))
990
991
992 _inject_numeric_testing_methods(IntegerValueTestCase)
993
994
995 class RealValueTestCase(_TestNumericValue, unittest.TestCase):
996 def setUp(self):
997 self._pv = 23.4
998 self._nv = -52.7
999 self._fp = bt2.RealValue(self._pv)
1000 self._fn = bt2.RealValue(self._nv)
1001 self._def = self._fp
1002 self._def_value = self._pv
1003 self._def_new_value = -101.88
1004
1005 def tearDown(self):
1006 del self._fp
1007 del self._fn
1008 del self._def
1009 del self._def_value
1010
1011 def _assert_expecting_float(self):
1012 return self.assertRaisesRegex(TypeError, r"expecting a real number object")
1013
1014 def _test_invalid_op(self, cb):
1015 with self.assertRaises(TypeError):
1016 cb()
1017
1018 def test_create_default(self):
1019 f = bt2.RealValue()
1020 self.assertEqual(f, 0.0)
1021
1022 def test_create_pos(self):
1023 self.assertEqual(self._fp, self._pv)
1024
1025 def test_create_neg(self):
1026 self.assertEqual(self._fn, self._nv)
1027
1028 def test_create_from_vint(self):
1029 f = bt2.RealValue(self._fp)
1030 self.assertEqual(f, self._pv)
1031
1032 def test_create_from_false(self):
1033 f = bt2.RealValue(False)
1034 self.assertFalse(f)
1035
1036 def test_create_from_true(self):
1037 f = bt2.RealValue(True)
1038 self.assertTrue(f)
1039
1040 def test_create_from_int(self):
1041 raw = 17
1042 f = bt2.RealValue(raw)
1043 self.assertEqual(f, float(raw))
1044
1045 def test_create_from_vint(self):
1046 raw = 17
1047 f = bt2.RealValue(bt2.create_value(raw))
1048 self.assertEqual(f, float(raw))
1049
1050 def test_create_from_vfloat(self):
1051 raw = 17.17
1052 f = bt2.RealValue(bt2.create_value(raw))
1053 self.assertEqual(f, raw)
1054
1055 def test_create_from_unknown(self):
1056 class A:
1057 pass
1058
1059 with self._assert_expecting_float():
1060 f = bt2.RealValue(A())
1061
1062 def test_create_from_varray(self):
1063 with self._assert_expecting_float():
1064 f = bt2.RealValue(bt2.ArrayValue())
1065
1066 def test_assign_true(self):
1067 self._def.value = True
1068 self.assertTrue(self._def)
1069
1070 def test_assign_false(self):
1071 self._def.value = False
1072 self.assertFalse(self._def)
1073
1074 def test_assign_pos_int(self):
1075 raw = 477
1076 self._def.value = raw
1077 self.assertEqual(self._def, float(raw))
1078
1079 def test_assign_neg_int(self):
1080 raw = -13
1081 self._def.value = raw
1082 self.assertEqual(self._def, float(raw))
1083
1084 def test_assign_vint(self):
1085 raw = 999
1086 self._def.value = bt2.create_value(raw)
1087 self.assertEqual(self._def, float(raw))
1088
1089 def test_assign_float(self):
1090 raw = -19.23
1091 self._def.value = raw
1092 self.assertEqual(self._def, raw)
1093
1094 def test_assign_vfloat(self):
1095 raw = 101.32
1096 self._def.value = bt2.create_value(raw)
1097 self.assertEqual(self._def, raw)
1098
1099 def test_invalid_lshift(self):
1100 self._test_invalid_op(lambda: self._def << 23)
1101
1102 def test_invalid_rshift(self):
1103 self._test_invalid_op(lambda: self._def >> 23)
1104
1105 def test_invalid_and(self):
1106 self._test_invalid_op(lambda: self._def & 23)
1107
1108 def test_invalid_or(self):
1109 self._test_invalid_op(lambda: self._def | 23)
1110
1111 def test_invalid_xor(self):
1112 self._test_invalid_op(lambda: self._def ^ 23)
1113
1114 def test_invalid_invert(self):
1115 self._test_invalid_op(lambda: ~self._def)
1116
1117
1118 _inject_numeric_testing_methods(RealValueTestCase)
1119
1120
1121 class StringValueTestCase(_TestCopySimple, unittest.TestCase):
1122 def setUp(self):
1123 self._def_value = 'Hello, World!'
1124 self._def = bt2.StringValue(self._def_value)
1125 self._def_new_value = 'Yes!'
1126
1127 def tearDown(self):
1128 del self._def
1129
1130 def _assert_expecting_str(self):
1131 return self.assertRaises(TypeError)
1132
1133 def test_create_default(self):
1134 s = bt2.StringValue()
1135 self.assertEqual(s, '')
1136
1137 def test_create_from_str(self):
1138 raw = 'liberté'
1139 s = bt2.StringValue(raw)
1140 self.assertEqual(s, raw)
1141
1142 def test_create_from_vstr(self):
1143 raw = 'liberté'
1144 s = bt2.StringValue(bt2.create_value(raw))
1145 self.assertEqual(s, raw)
1146
1147 def test_create_from_unknown(self):
1148 class A:
1149 pass
1150
1151 with self._assert_expecting_str():
1152 i = bt2.StringValue(A())
1153
1154 def test_create_from_varray(self):
1155 with self._assert_expecting_str():
1156 i = bt2.StringValue(bt2.ArrayValue())
1157
1158 def test_assign_int(self):
1159 with self._assert_expecting_str():
1160 self._def.value = 283
1161
1162 def test_assign_str(self):
1163 raw = 'zorg'
1164 self._def = raw
1165 self.assertEqual(self._def, raw)
1166
1167 def test_assign_vstr(self):
1168 raw = 'zorg'
1169 self._def = bt2.create_value(raw)
1170 self.assertEqual(self._def, raw)
1171
1172 def test_eq(self):
1173 self.assertEqual(self._def, self._def_value)
1174
1175 def test_eq(self):
1176 self.assertNotEqual(self._def, 23)
1177
1178 def test_lt_vstring(self):
1179 s1 = bt2.StringValue('allo')
1180 s2 = bt2.StringValue('bateau')
1181 self.assertLess(s1, s2)
1182
1183 def test_lt_string(self):
1184 s1 = bt2.StringValue('allo')
1185 self.assertLess(s1, 'bateau')
1186
1187 def test_le_vstring(self):
1188 s1 = bt2.StringValue('allo')
1189 s2 = bt2.StringValue('bateau')
1190 self.assertLessEqual(s1, s2)
1191
1192 def test_le_string(self):
1193 s1 = bt2.StringValue('allo')
1194 self.assertLessEqual(s1, 'bateau')
1195
1196 def test_gt_vstring(self):
1197 s1 = bt2.StringValue('allo')
1198 s2 = bt2.StringValue('bateau')
1199 self.assertGreater(s2, s1)
1200
1201 def test_gt_string(self):
1202 s1 = bt2.StringValue('allo')
1203 self.assertGreater('bateau', s1)
1204
1205 def test_ge_vstring(self):
1206 s1 = bt2.StringValue('allo')
1207 s2 = bt2.StringValue('bateau')
1208 self.assertGreaterEqual(s2, s1)
1209
1210 def test_ge_string(self):
1211 s1 = bt2.StringValue('allo')
1212 self.assertGreaterEqual('bateau', s1)
1213
1214 def test_bool_op(self):
1215 self.assertEqual(bool(self._def), bool(self._def_value))
1216
1217 def test_str_op(self):
1218 self.assertEqual(str(self._def), str(self._def_value))
1219
1220 def test_len(self):
1221 self.assertEqual(len(self._def), len(self._def_value))
1222
1223 def test_getitem(self):
1224 self.assertEqual(self._def[5], self._def_value[5])
1225
1226 def test_append_str(self):
1227 to_append = 'meow meow meow'
1228 self._def += to_append
1229 self._def_value += to_append
1230 self.assertEqual(self._def, self._def_value)
1231
1232 def test_append_vstr(self):
1233 to_append = 'meow meow meow'
1234 self._def += bt2.create_value(to_append)
1235 self._def_value += to_append
1236 self.assertEqual(self._def, self._def_value)
1237
1238
1239 class ArrayValueTestCase(_TestCopySimple, unittest.TestCase):
1240 def setUp(self):
1241 self._def_value = [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1242 self._def = bt2.ArrayValue(copy.deepcopy(self._def_value))
1243
1244 def tearDown(self):
1245 del self._def
1246
1247 def _modify_def(self):
1248 self._def[2] = 'xyz'
1249
1250 def _assert_type_error(self):
1251 return self.assertRaises(TypeError)
1252
1253 def test_create_default(self):
1254 a = bt2.ArrayValue()
1255 self.assertEqual(len(a), 0)
1256
1257 def test_create_from_array(self):
1258 self.assertEqual(self._def, self._def_value)
1259
1260 def test_create_from_tuple(self):
1261 t = 1, 2, False, None
1262 a = bt2.ArrayValue(t)
1263 self.assertEqual(a, t)
1264
1265 def test_create_from_varray(self):
1266 va = bt2.ArrayValue(copy.deepcopy(self._def_value))
1267 a = bt2.ArrayValue(va)
1268 self.assertEqual(va, a)
1269
1270 def test_create_from_unknown(self):
1271 class A:
1272 pass
1273
1274 with self._assert_type_error():
1275 a = bt2.ArrayValue(A())
1276
1277 def test_bool_op_true(self):
1278 self.assertTrue(bool(self._def))
1279
1280 def test_bool_op_false(self):
1281 self.assertFalse(bool(bt2.ArrayValue()))
1282
1283 def test_len(self):
1284 self.assertEqual(len(self._def), len(self._def_value))
1285
1286 def test_eq_int(self):
1287 self.assertNotEqual(self._def, 23)
1288
1289 def test_eq_diff_len(self):
1290 a1 = bt2.create_value([1, 2, 3])
1291 a2 = bt2.create_value([1, 2])
1292 self.assertNotEqual(a1, a2)
1293
1294 def test_eq_diff_content_same_len(self):
1295 a1 = bt2.create_value([1, 2, 3])
1296 a2 = bt2.create_value([4, 5, 6])
1297 self.assertNotEqual(a1, a2)
1298
1299 def test_eq_same_content_same_len(self):
1300 raw = (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1301 a1 = bt2.ArrayValue(raw)
1302 a2 = bt2.ArrayValue(copy.deepcopy(raw))
1303 self.assertEqual(a1, a2)
1304
1305 def test_setitem_int(self):
1306 raw = 19
1307 self._def[2] = raw
1308 self.assertEqual(self._def[2], raw)
1309
1310 def test_setitem_vint(self):
1311 raw = 19
1312 self._def[2] = bt2.create_value(raw)
1313 self.assertEqual(self._def[2], raw)
1314
1315 def test_setitem_none(self):
1316 self._def[2] = None
1317 self.assertIsNone(self._def[2])
1318
1319 def test_setitem_index_wrong_type(self):
1320 with self._assert_type_error():
1321 self._def['yes'] = 23
1322
1323 def test_setitem_index_neg(self):
1324 with self.assertRaises(IndexError):
1325 self._def[-2] = 23
1326
1327 def test_setitem_index_out_of_range(self):
1328 with self.assertRaises(IndexError):
1329 self._def[len(self._def)] = 23
1330
1331 def test_append_none(self):
1332 self._def.append(None)
1333 self.assertIsNone(self._def[len(self._def) - 1])
1334
1335 def test_append_int(self):
1336 raw = 145
1337 self._def.append(raw)
1338 self.assertEqual(self._def[len(self._def) - 1], raw)
1339
1340 def test_append_vint(self):
1341 raw = 145
1342 self._def.append(bt2.create_value(raw))
1343 self.assertEqual(self._def[len(self._def) - 1], raw)
1344
1345 def test_append_unknown(self):
1346 class A:
1347 pass
1348
1349 with self._assert_type_error():
1350 self._def.append(A())
1351
1352 def test_iadd(self):
1353 raw = 4, 5, True
1354 self._def += raw
1355 self.assertEqual(self._def[len(self._def) - 3], raw[0])
1356 self.assertEqual(self._def[len(self._def) - 2], raw[1])
1357 self.assertEqual(self._def[len(self._def) - 1], raw[2])
1358
1359 def test_iadd_unknown(self):
1360 class A:
1361 pass
1362
1363 with self._assert_type_error():
1364 self._def += A()
1365
1366 def test_iadd_list_unknown(self):
1367 class A:
1368 pass
1369
1370 with self._assert_type_error():
1371 self._def += [A()]
1372
1373 def test_iter(self):
1374 for velem, elem in zip(self._def, self._def_value):
1375 self.assertEqual(velem, elem)
1376
1377
1378 class MapValueTestCase(_TestCopySimple, unittest.TestCase):
1379 def setUp(self):
1380 self._def_value = {
1381 'none': None,
1382 'false': False,
1383 'true': True,
1384 'neg-int': -23,
1385 'zero': 0,
1386 'pos-int': 42,
1387 'neg-float': -42.4,
1388 'pos-float': 23.17,
1389 'str': 'yes'
1390 }
1391 self._def = bt2.MapValue(copy.deepcopy(self._def_value))
1392
1393 def tearDown(self):
1394 del self._def
1395
1396 def _modify_def(self):
1397 self._def['zero'] = 1
1398
1399 def test_create_default(self):
1400 m = bt2.MapValue()
1401 self.assertEqual(len(m), 0)
1402
1403 def test_create_from_dict(self):
1404 self.assertEqual(self._def, self._def_value)
1405
1406 def test_create_from_vmap(self):
1407 vm = bt2.MapValue(copy.deepcopy(self._def_value))
1408 m = bt2.MapValue(vm)
1409 self.assertEqual(vm, m)
1410
1411 def test_create_from_unknown(self):
1412 class A:
1413 pass
1414
1415 with self.assertRaises(AttributeError):
1416 m = bt2.MapValue(A())
1417
1418 def test_bool_op_true(self):
1419 self.assertTrue(bool(self._def))
1420
1421 def test_bool_op_false(self):
1422 self.assertFalse(bool(bt2.MapValue()))
1423
1424 def test_len(self):
1425 self.assertEqual(len(self._def), len(self._def_value))
1426
1427 def test_eq_int(self):
1428 self.assertNotEqual(self._def, 23)
1429
1430 def test_eq_diff_len(self):
1431 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1432 a2 = bt2.create_value({'a': 1, 'b': 2})
1433 self.assertNotEqual(a1, a2)
1434
1435 def test_eq_diff_content_same_len(self):
1436 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1437 a2 = bt2.create_value({'a': 4, 'b': 2, 'c': 3})
1438 self.assertNotEqual(a1, a2)
1439
1440 def test_eq_same_content_diff_keys(self):
1441 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1442 a2 = bt2.create_value({'a': 1, 'k': 2, 'c': 3})
1443 self.assertNotEqual(a1, a2)
1444
1445 def test_eq_same_content_same_len(self):
1446 raw = {
1447 '3': 3,
1448 'True': True,
1449 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]
1450 }
1451 a1 = bt2.MapValue(raw)
1452 a2 = bt2.MapValue(copy.deepcopy(raw))
1453 self.assertEqual(a1, a2)
1454 self.assertEqual(a1, raw)
1455
1456 def test_setitem_int(self):
1457 raw = 19
1458 self._def['pos-int'] = raw
1459 self.assertEqual(self._def['pos-int'], raw)
1460
1461 def test_setitem_vint(self):
1462 raw = 19
1463 self._def['pos-int'] = bt2.create_value(raw)
1464 self.assertEqual(self._def['pos-int'], raw)
1465
1466 def test_setitem_none(self):
1467 self._def['none'] = None
1468 self.assertIsNone(self._def['none'])
1469
1470 def test_setitem_new_int(self):
1471 old_len = len(self._def)
1472 self._def['new-int'] = 23
1473 self.assertEqual(self._def['new-int'], 23)
1474 self.assertEqual(len(self._def), old_len + 1)
1475
1476 def test_setitem_index_wrong_type(self):
1477 with self.assertRaises(TypeError):
1478 self._def[18] = 23
1479
1480 def test_iter(self):
1481 for vkey, vval in self._def.items():
1482 val = self._def_value[vkey]
1483 self.assertEqual(vval, val)
1484
1485 def test_getitem_wrong_key(self):
1486 with self.assertRaises(KeyError):
1487 self._def['kilojoule']
This page took 0.101434 seconds and 4 git commands to generate.