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