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