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