bt2: ignore comparison to None flake8 warnings in test_value.py
[babeltrace.git] / tests / bindings / python / bt2 / test_value.py
CommitLineData
32d2d479
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
54495cd0 21import collections
9cf643d1 22import unittest
9cf643d1
PP
23import math
24import copy
25import bt2
26
27
d1198f86
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):
77b70d39
SM
33 with self.assertRaises(NotImplementedError):
34 copy.copy(self._def)
9cf643d1
PP
35
36 def test_deepcopy(self):
77b70d39
SM
37 with self.assertRaises(NotImplementedError):
38 copy.deepcopy(self._def)
9cf643d1
PP
39
40
61d96b89 41_COMP_BINOPS = (operator.eq, operator.ne)
9cf643d1
PP
42
43
d1198f86
PP
44# Base class for numeric value test cases.
45#
46# To be compatible with this base class, a derived class must, in its
47# setUp() method:
48#
49# * Set `self._def` to a value object with an arbitrary raw value.
50# * Set `self._def_value` to the equivalent raw value of `self._def`.
77b70d39 51class _TestNumericValue(_TestCopySimple):
d1198f86
PP
52 # Tries the binary operation `op`:
53 #
54 # 1. Between `self._def`, which is a value object, and `rhs`.
55 # 2. Between `self._def_value`, which is the raw value of
56 # `self._def`, and `rhs`.
57 #
58 # Returns the results of 1. and 2.
59 #
60 # If there's an exception while performing 1. or 2., asserts that
61 # both operations raised exceptions, that both exceptions have the
62 # same type, and returns `None` for both results.
9cf643d1 63 def _binop(self, op, rhs):
d1198f86
PP
64 type_rexc = None
65 type_rvexc = None
9cf643d1
PP
66 comp_value = rhs
67
d1198f86 68 # try with value object
9cf643d1
PP
69 try:
70 r = op(self._def, rhs)
71 except Exception as e:
d1198f86 72 type_rexc = type(e)
9cf643d1 73
d1198f86 74 # try with raw value
9cf643d1
PP
75 try:
76 rv = op(self._def_value, comp_value)
77 except Exception as e:
d1198f86 78 type_rvexc = type(e)
9cf643d1 79
d1198f86 80 if type_rexc is not None or type_rvexc is not None:
9cf643d1
PP
81 # at least one of the operations raised an exception: in
82 # this case both operations should have raised the same
83 # type of exception (division by zero, bit shift with a
84 # floating point number operand, etc.)
d1198f86 85 self.assertIs(type_rexc, type_rvexc)
9cf643d1
PP
86 return None, None
87
88 return r, rv
89
d1198f86
PP
90 # Tries the unary operation `op`:
91 #
92 # 1. On `self._def`, which is a value object.
93 # 2. On `self._def_value`, which is the raw value of `self._def`.
94 #
95 # Returns the results of 1. and 2.
96 #
97 # If there's an exception while performing 1. or 2., asserts that
98 # both operations raised exceptions, that both exceptions have the
99 # same type, and returns `None` for both results.
9cf643d1 100 def _unaryop(self, op):
d1198f86
PP
101 type_rexc = None
102 type_rvexc = None
9cf643d1 103
d1198f86 104 # try with value object
9cf643d1
PP
105 try:
106 r = op(self._def)
107 except Exception as e:
d1198f86 108 type_rexc = type(e)
9cf643d1 109
d1198f86 110 # try with raw value
9cf643d1
PP
111 try:
112 rv = op(self._def_value)
113 except Exception as e:
d1198f86 114 type_rvexc = type(e)
9cf643d1 115
d1198f86 116 if type_rexc is not None or type_rvexc is not None:
9cf643d1
PP
117 # at least one of the operations raised an exception: in
118 # this case both operations should have raised the same
119 # type of exception (division by zero, bit shift with a
120 # floating point number operand, etc.)
d1198f86 121 self.assertIs(type_rexc, type_rvexc)
9cf643d1
PP
122 return None, None
123
124 return r, rv
125
d1198f86
PP
126 # Tests that the unary operation `op` gives results with the same
127 # type for both `self._def` and `self._def_value`.
9cf643d1
PP
128 def _test_unaryop_type(self, op):
129 r, rv = self._unaryop(op)
130
131 if r is None:
132 return
133
134 self.assertIsInstance(r, type(rv))
135
d1198f86
PP
136 # Tests that the unary operation `op` gives results with the same
137 # value for both `self._def` and `self._def_value`. This uses the
138 # __eq__() operator of `self._def`.
9cf643d1
PP
139 def _test_unaryop_value(self, op):
140 r, rv = self._unaryop(op)
141
142 if r is None:
143 return
144
145 self.assertEqual(r, rv)
146
d1198f86
PP
147 # Tests that the unary operation `op`, when applied to `self._def`,
148 # does not change its underlying BT object address.
9cf643d1
PP
149 def _test_unaryop_addr_same(self, op):
150 addr_before = self._def.addr
151 self._unaryop(op)
152 self.assertEqual(self._def.addr, addr_before)
153
d1198f86
PP
154 # Tests that the unary operation `op`, when applied to `self._def`,
155 # does not change its value.
9cf643d1 156 def _test_unaryop_value_same(self, op):
77b70d39 157 value_before = self._def.__class__(self._def)
9cf643d1 158 self._unaryop(op)
72bd7054 159 self.assertEqual(self._def, value_before)
9cf643d1 160
d1198f86
PP
161 # Tests that the binary operation `op` gives results with the same
162 # type for both `self._def` and `self._def_value`.
9cf643d1
PP
163 def _test_binop_type(self, op, rhs):
164 r, rv = self._binop(op, rhs)
165
166 if r is None:
167 return
168
169 if op in _COMP_BINOPS:
170 # __eq__() and __ne__() always return a 'bool' object
171 self.assertIsInstance(r, bool)
172 else:
173 self.assertIsInstance(r, type(rv))
174
d1198f86
PP
175 # Tests that the binary operation `op` gives results with the same
176 # value for both `self._def` and `self._def_value`. This uses the
177 # __eq__() operator of `self._def`.
9cf643d1
PP
178 def _test_binop_value(self, op, rhs):
179 r, rv = self._binop(op, rhs)
180
181 if r is None:
182 return
183
184 self.assertEqual(r, rv)
185
d1198f86
PP
186 # Tests that the binary operation `op`, when applied to `self._def`,
187 # does not change its underlying BT object address.
9cf643d1
PP
188 def _test_binop_lhs_addr_same(self, op, rhs):
189 addr_before = self._def.addr
190 r, rv = self._binop(op, rhs)
191 self.assertEqual(self._def.addr, addr_before)
192
d1198f86
PP
193 # Tests that the binary operation `op`, when applied to `self._def`,
194 # does not change its value.
9cf643d1 195 def _test_binop_lhs_value_same(self, op, rhs):
77b70d39 196 value_before = self._def.__class__(self._def)
9cf643d1 197 r, rv = self._binop(op, rhs)
72bd7054 198 self.assertEqual(self._def, value_before)
9cf643d1 199
d1198f86
PP
200 # The methods below which take the `test_cb` and `op` parameters
201 # are meant to be used with one of the _test_binop_*() functions
202 # above as `test_cb` and a binary operator function as `op`.
203 #
204 # For example:
205 #
206 # self._test_binop_rhs_pos_int(self._test_binop_value,
207 # operator.add)
208 #
209 # This tests that a numeric value object added to a positive integer
210 # raw value gives a result with the expected value.
211 #
212 # `vint` and `vfloat` mean a signed integer value object and a real
213 # value object.
214
9cf643d1
PP
215 def _test_binop_invalid_unknown(self, op):
216 if op in _COMP_BINOPS:
217 self.skipTest('not testing')
218
9cf643d1 219 with self.assertRaises(TypeError):
d1198f86 220 op(self._def, object())
9cf643d1
PP
221
222 def _test_binop_invalid_none(self, op):
223 if op in _COMP_BINOPS:
224 self.skipTest('not testing')
225
226 with self.assertRaises(TypeError):
227 op(self._def, None)
228
9cf643d1
PP
229 def _test_binop_rhs_false(self, test_cb, op):
230 test_cb(op, False)
231
232 def _test_binop_rhs_true(self, test_cb, op):
233 test_cb(op, True)
234
235 def _test_binop_rhs_pos_int(self, test_cb, op):
236 test_cb(op, 2)
237
238 def _test_binop_rhs_neg_int(self, test_cb, op):
239 test_cb(op, -23)
240
241 def _test_binop_rhs_zero_int(self, test_cb, op):
242 test_cb(op, 0)
243
244 def _test_binop_rhs_pos_vint(self, test_cb, op):
245 test_cb(op, bt2.create_value(2))
246
247 def _test_binop_rhs_neg_vint(self, test_cb, op):
248 test_cb(op, bt2.create_value(-23))
249
250 def _test_binop_rhs_zero_vint(self, test_cb, op):
251 test_cb(op, bt2.create_value(0))
252
253 def _test_binop_rhs_pos_float(self, test_cb, op):
254 test_cb(op, 2.2)
255
256 def _test_binop_rhs_neg_float(self, test_cb, op):
257 test_cb(op, -23.4)
258
259 def _test_binop_rhs_zero_float(self, test_cb, op):
260 test_cb(op, 0.0)
261
229e2227 262 def _test_binop_rhs_complex(self, test_cb, op):
61d96b89 263 test_cb(op, -23 + 19j)
229e2227
PP
264
265 def _test_binop_rhs_zero_complex(self, test_cb, op):
266 test_cb(op, 0j)
267
9cf643d1
PP
268 def _test_binop_rhs_pos_vfloat(self, test_cb, op):
269 test_cb(op, bt2.create_value(2.2))
270
271 def _test_binop_rhs_neg_vfloat(self, test_cb, op):
272 test_cb(op, bt2.create_value(-23.4))
273
274 def _test_binop_rhs_zero_vfloat(self, test_cb, op):
275 test_cb(op, bt2.create_value(0.0))
276
277 def _test_binop_type_false(self, op):
278 self._test_binop_rhs_false(self._test_binop_type, op)
279
280 def _test_binop_type_true(self, op):
281 self._test_binop_rhs_true(self._test_binop_type, op)
282
283 def _test_binop_type_pos_int(self, op):
284 self._test_binop_rhs_pos_int(self._test_binop_type, op)
285
286 def _test_binop_type_neg_int(self, op):
287 self._test_binop_rhs_neg_int(self._test_binop_type, op)
288
289 def _test_binop_type_zero_int(self, op):
290 self._test_binop_rhs_zero_int(self._test_binop_type, op)
291
292 def _test_binop_type_pos_vint(self, op):
293 self._test_binop_rhs_pos_vint(self._test_binop_type, op)
294
295 def _test_binop_type_neg_vint(self, op):
296 self._test_binop_rhs_neg_vint(self._test_binop_type, op)
297
298 def _test_binop_type_zero_vint(self, op):
299 self._test_binop_rhs_zero_vint(self._test_binop_type, op)
300
301 def _test_binop_type_pos_float(self, op):
302 self._test_binop_rhs_pos_float(self._test_binop_type, op)
303
304 def _test_binop_type_neg_float(self, op):
305 self._test_binop_rhs_neg_float(self._test_binop_type, op)
306
307 def _test_binop_type_zero_float(self, op):
308 self._test_binop_rhs_zero_float(self._test_binop_type, op)
309
310 def _test_binop_type_pos_vfloat(self, op):
311 self._test_binop_rhs_pos_vfloat(self._test_binop_type, op)
312
313 def _test_binop_type_neg_vfloat(self, op):
314 self._test_binop_rhs_neg_vfloat(self._test_binop_type, op)
315
316 def _test_binop_type_zero_vfloat(self, op):
317 self._test_binop_rhs_zero_vfloat(self._test_binop_type, op)
318
229e2227
PP
319 def _test_binop_type_complex(self, op):
320 self._test_binop_rhs_complex(self._test_binop_type, op)
321
322 def _test_binop_type_zero_complex(self, op):
323 self._test_binop_rhs_zero_complex(self._test_binop_type, op)
324
9cf643d1
PP
325 def _test_binop_value_false(self, op):
326 self._test_binop_rhs_false(self._test_binop_value, op)
327
328 def _test_binop_value_true(self, op):
329 self._test_binop_rhs_true(self._test_binop_value, op)
330
331 def _test_binop_value_pos_int(self, op):
332 self._test_binop_rhs_pos_int(self._test_binop_value, op)
333
334 def _test_binop_value_neg_int(self, op):
335 self._test_binop_rhs_neg_int(self._test_binop_value, op)
336
337 def _test_binop_value_zero_int(self, op):
338 self._test_binop_rhs_zero_int(self._test_binop_value, op)
339
340 def _test_binop_value_pos_vint(self, op):
341 self._test_binop_rhs_pos_vint(self._test_binop_value, op)
342
343 def _test_binop_value_neg_vint(self, op):
344 self._test_binop_rhs_neg_vint(self._test_binop_value, op)
345
346 def _test_binop_value_zero_vint(self, op):
347 self._test_binop_rhs_zero_vint(self._test_binop_value, op)
348
349 def _test_binop_value_pos_float(self, op):
350 self._test_binop_rhs_pos_float(self._test_binop_value, op)
351
352 def _test_binop_value_neg_float(self, op):
353 self._test_binop_rhs_neg_float(self._test_binop_value, op)
354
355 def _test_binop_value_zero_float(self, op):
356 self._test_binop_rhs_zero_float(self._test_binop_value, op)
357
358 def _test_binop_value_pos_vfloat(self, op):
359 self._test_binop_rhs_pos_vfloat(self._test_binop_value, op)
360
361 def _test_binop_value_neg_vfloat(self, op):
362 self._test_binop_rhs_neg_vfloat(self._test_binop_value, op)
363
364 def _test_binop_value_zero_vfloat(self, op):
365 self._test_binop_rhs_zero_vfloat(self._test_binop_value, op)
366
229e2227
PP
367 def _test_binop_value_complex(self, op):
368 self._test_binop_rhs_complex(self._test_binop_value, op)
369
370 def _test_binop_value_zero_complex(self, op):
371 self._test_binop_rhs_zero_complex(self._test_binop_value, op)
372
9cf643d1
PP
373 def _test_binop_lhs_addr_same_false(self, op):
374 self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op)
375
376 def _test_binop_lhs_addr_same_true(self, op):
377 self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op)
378
379 def _test_binop_lhs_addr_same_pos_int(self, op):
380 self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op)
381
382 def _test_binop_lhs_addr_same_neg_int(self, op):
383 self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op)
384
385 def _test_binop_lhs_addr_same_zero_int(self, op):
386 self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op)
387
388 def _test_binop_lhs_addr_same_pos_vint(self, op):
389 self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op)
390
391 def _test_binop_lhs_addr_same_neg_vint(self, op):
392 self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op)
393
394 def _test_binop_lhs_addr_same_zero_vint(self, op):
395 self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op)
396
397 def _test_binop_lhs_addr_same_pos_float(self, op):
398 self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op)
399
400 def _test_binop_lhs_addr_same_neg_float(self, op):
401 self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op)
402
403 def _test_binop_lhs_addr_same_zero_float(self, op):
404 self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op)
405
406 def _test_binop_lhs_addr_same_pos_vfloat(self, op):
407 self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op)
408
409 def _test_binop_lhs_addr_same_neg_vfloat(self, op):
410 self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op)
411
412 def _test_binop_lhs_addr_same_zero_vfloat(self, op):
413 self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op)
414
229e2227
PP
415 def _test_binop_lhs_addr_same_complex(self, op):
416 self._test_binop_rhs_complex(self._test_binop_lhs_addr_same, op)
417
418 def _test_binop_lhs_addr_same_zero_complex(self, op):
419 self._test_binop_rhs_zero_complex(self._test_binop_lhs_addr_same, op)
420
9cf643d1
PP
421 def _test_binop_lhs_value_same_false(self, op):
422 self._test_binop_rhs_false(self._test_binop_lhs_value_same, op)
423
424 def _test_binop_lhs_value_same_true(self, op):
425 self._test_binop_rhs_true(self._test_binop_lhs_value_same, op)
426
427 def _test_binop_lhs_value_same_pos_int(self, op):
428 self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op)
429
430 def _test_binop_lhs_value_same_neg_int(self, op):
431 self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op)
432
433 def _test_binop_lhs_value_same_zero_int(self, op):
434 self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op)
435
436 def _test_binop_lhs_value_same_pos_vint(self, op):
437 self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op)
438
439 def _test_binop_lhs_value_same_neg_vint(self, op):
440 self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op)
441
442 def _test_binop_lhs_value_same_zero_vint(self, op):
443 self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op)
444
445 def _test_binop_lhs_value_same_pos_float(self, op):
446 self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op)
447
448 def _test_binop_lhs_value_same_neg_float(self, op):
449 self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op)
450
451 def _test_binop_lhs_value_same_zero_float(self, op):
452 self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op)
453
454 def _test_binop_lhs_value_same_pos_vfloat(self, op):
455 self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op)
456
457 def _test_binop_lhs_value_same_neg_vfloat(self, op):
458 self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op)
459
460 def _test_binop_lhs_value_same_zero_vfloat(self, op):
461 self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op)
462
229e2227
PP
463 def _test_binop_lhs_value_same_complex(self, op):
464 self._test_binop_rhs_complex(self._test_binop_lhs_value_same, op)
465
466 def _test_binop_lhs_value_same_zero_complex(self, op):
467 self._test_binop_rhs_zero_complex(self._test_binop_lhs_value_same, op)
468
9cf643d1
PP
469 def test_bool_op(self):
470 self.assertEqual(bool(self._def), bool(self._def_value))
471
472 def test_int_op(self):
473 self.assertEqual(int(self._def), int(self._def_value))
474
475 def test_float_op(self):
476 self.assertEqual(float(self._def), float(self._def_value))
477
478 def test_complex_op(self):
479 self.assertEqual(complex(self._def), complex(self._def_value))
480
481 def test_str_op(self):
482 self.assertEqual(str(self._def), str(self._def_value))
483
484 def test_eq_none(self):
13b81b5d
SM
485 # Disable the "comparison to None" warning, as this is precisely what
486 # we want to test here.
487 self.assertFalse(self._def == None) # noqa: E711
9cf643d1
PP
488
489 def test_ne_none(self):
13b81b5d
SM
490 # Disable the "comparison to None" warning, as this is precisely what
491 # we want to test here.
492 self.assertTrue(self._def != None) # noqa: E711
9cf643d1
PP
493
494
d1198f86
PP
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.
9cf643d1
PP
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
d1198f86
PP
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.
9cf643d1
PP
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
d1198f86
PP
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#
d1198f86
PP
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.
a4a14cd0 569def _inject_numeric_testing_methods(cls):
9cf643d1
PP
570 def test_binop_name(suffix):
571 return 'test_binop_{}_{}'.format(name, suffix)
572
9cf643d1
PP
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:
61d96b89
FD
578 setattr(
579 cls,
580 test_binop_name('invalid_unknown'),
581 partialmethod(_TestNumericValue._test_binop_invalid_unknown, op=binop),
582 )
583 setattr(
584 cls,
585 test_binop_name('invalid_none'),
586 partialmethod(_TestNumericValue._test_binop_invalid_none, op=binop),
587 )
588 setattr(
589 cls,
590 test_binop_name('type_true'),
591 partialmethod(_TestNumericValue._test_binop_type_true, op=binop),
592 )
593 setattr(
594 cls,
595 test_binop_name('type_pos_int'),
596 partialmethod(_TestNumericValue._test_binop_type_pos_int, op=binop),
597 )
598 setattr(
599 cls,
600 test_binop_name('type_pos_vint'),
601 partialmethod(_TestNumericValue._test_binop_type_pos_vint, op=binop),
602 )
603 setattr(
604 cls,
605 test_binop_name('value_true'),
606 partialmethod(_TestNumericValue._test_binop_value_true, op=binop),
607 )
608 setattr(
609 cls,
610 test_binop_name('value_pos_int'),
611 partialmethod(_TestNumericValue._test_binop_value_pos_int, op=binop),
612 )
613 setattr(
614 cls,
615 test_binop_name('value_pos_vint'),
616 partialmethod(_TestNumericValue._test_binop_value_pos_vint, op=binop),
617 )
618 setattr(
619 cls,
620 test_binop_name('lhs_addr_same_true'),
621 partialmethod(_TestNumericValue._test_binop_lhs_addr_same_true, op=binop),
622 )
623 setattr(
624 cls,
625 test_binop_name('lhs_addr_same_pos_int'),
626 partialmethod(
627 _TestNumericValue._test_binop_lhs_addr_same_pos_int, op=binop
628 ),
629 )
630 setattr(
631 cls,
632 test_binop_name('lhs_addr_same_pos_vint'),
633 partialmethod(
634 _TestNumericValue._test_binop_lhs_addr_same_pos_vint, op=binop
635 ),
636 )
637 setattr(
638 cls,
639 test_binop_name('lhs_value_same_true'),
640 partialmethod(_TestNumericValue._test_binop_lhs_value_same_true, op=binop),
641 )
642 setattr(
643 cls,
644 test_binop_name('lhs_value_same_pos_int'),
645 partialmethod(
646 _TestNumericValue._test_binop_lhs_value_same_pos_int, op=binop
647 ),
648 )
649 setattr(
650 cls,
651 test_binop_name('lhs_value_same_pos_vint'),
652 partialmethod(
653 _TestNumericValue._test_binop_lhs_value_same_pos_vint, op=binop
654 ),
655 )
656 setattr(
657 cls,
658 test_binop_name('type_neg_int'),
659 partialmethod(_TestNumericValue._test_binop_type_neg_int, op=binop),
660 )
661 setattr(
662 cls,
663 test_binop_name('type_neg_vint'),
664 partialmethod(_TestNumericValue._test_binop_type_neg_vint, op=binop),
665 )
666 setattr(
667 cls,
668 test_binop_name('value_neg_int'),
669 partialmethod(_TestNumericValue._test_binop_value_neg_int, op=binop),
670 )
671 setattr(
672 cls,
673 test_binop_name('value_neg_vint'),
674 partialmethod(_TestNumericValue._test_binop_value_neg_vint, op=binop),
675 )
676 setattr(
677 cls,
678 test_binop_name('lhs_addr_same_neg_int'),
679 partialmethod(
680 _TestNumericValue._test_binop_lhs_addr_same_neg_int, op=binop
681 ),
682 )
683 setattr(
684 cls,
685 test_binop_name('lhs_addr_same_neg_vint'),
686 partialmethod(
687 _TestNumericValue._test_binop_lhs_addr_same_neg_vint, op=binop
688 ),
689 )
690 setattr(
691 cls,
692 test_binop_name('lhs_value_same_neg_int'),
693 partialmethod(
694 _TestNumericValue._test_binop_lhs_value_same_neg_int, op=binop
695 ),
696 )
697 setattr(
698 cls,
699 test_binop_name('lhs_value_same_neg_vint'),
700 partialmethod(
701 _TestNumericValue._test_binop_lhs_value_same_neg_vint, op=binop
702 ),
703 )
704 setattr(
705 cls,
706 test_binop_name('type_false'),
707 partialmethod(_TestNumericValue._test_binop_type_false, op=binop),
708 )
709 setattr(
710 cls,
711 test_binop_name('type_zero_int'),
712 partialmethod(_TestNumericValue._test_binop_type_zero_int, op=binop),
713 )
714 setattr(
715 cls,
716 test_binop_name('type_zero_vint'),
717 partialmethod(_TestNumericValue._test_binop_type_zero_vint, op=binop),
718 )
719 setattr(
720 cls,
721 test_binop_name('value_false'),
722 partialmethod(_TestNumericValue._test_binop_value_false, op=binop),
723 )
724 setattr(
725 cls,
726 test_binop_name('value_zero_int'),
727 partialmethod(_TestNumericValue._test_binop_value_zero_int, op=binop),
728 )
729 setattr(
730 cls,
731 test_binop_name('value_zero_vint'),
732 partialmethod(_TestNumericValue._test_binop_value_zero_vint, op=binop),
733 )
734 setattr(
735 cls,
736 test_binop_name('lhs_addr_same_false'),
737 partialmethod(_TestNumericValue._test_binop_lhs_addr_same_false, op=binop),
738 )
739 setattr(
740 cls,
741 test_binop_name('lhs_addr_same_zero_int'),
742 partialmethod(
743 _TestNumericValue._test_binop_lhs_addr_same_zero_int, op=binop
744 ),
745 )
746 setattr(
747 cls,
748 test_binop_name('lhs_addr_same_zero_vint'),
749 partialmethod(
750 _TestNumericValue._test_binop_lhs_addr_same_zero_vint, op=binop
751 ),
752 )
753 setattr(
754 cls,
755 test_binop_name('lhs_value_same_false'),
756 partialmethod(_TestNumericValue._test_binop_lhs_value_same_false, op=binop),
757 )
758 setattr(
759 cls,
760 test_binop_name('lhs_value_same_zero_int'),
761 partialmethod(
762 _TestNumericValue._test_binop_lhs_value_same_zero_int, op=binop
763 ),
764 )
765 setattr(
766 cls,
767 test_binop_name('lhs_value_same_zero_vint'),
768 partialmethod(
769 _TestNumericValue._test_binop_lhs_value_same_zero_vint, op=binop
770 ),
771 )
772 setattr(
773 cls,
774 test_binop_name('type_neg_float'),
775 partialmethod(_TestNumericValue._test_binop_type_neg_float, op=binop),
776 )
777 setattr(
778 cls,
779 test_binop_name('type_neg_vfloat'),
780 partialmethod(_TestNumericValue._test_binop_type_neg_vfloat, op=binop),
781 )
782 setattr(
783 cls,
784 test_binop_name('value_neg_float'),
785 partialmethod(_TestNumericValue._test_binop_value_neg_float, op=binop),
786 )
787 setattr(
788 cls,
789 test_binop_name('value_neg_vfloat'),
790 partialmethod(_TestNumericValue._test_binop_value_neg_vfloat, op=binop),
791 )
792 setattr(
793 cls,
794 test_binop_name('lhs_addr_same_neg_float'),
795 partialmethod(
796 _TestNumericValue._test_binop_lhs_addr_same_neg_float, op=binop
797 ),
798 )
799 setattr(
800 cls,
801 test_binop_name('lhs_addr_same_neg_vfloat'),
802 partialmethod(
803 _TestNumericValue._test_binop_lhs_addr_same_neg_vfloat, op=binop
804 ),
805 )
806 setattr(
807 cls,
808 test_binop_name('lhs_value_same_neg_float'),
809 partialmethod(
810 _TestNumericValue._test_binop_lhs_value_same_neg_float, op=binop
811 ),
812 )
813 setattr(
814 cls,
815 test_binop_name('lhs_value_same_neg_vfloat'),
816 partialmethod(
817 _TestNumericValue._test_binop_lhs_value_same_neg_vfloat, op=binop
818 ),
819 )
820 setattr(
821 cls,
822 test_binop_name('type_pos_float'),
823 partialmethod(_TestNumericValue._test_binop_type_pos_float, op=binop),
824 )
825 setattr(
826 cls,
827 test_binop_name('type_pos_vfloat'),
828 partialmethod(_TestNumericValue._test_binop_type_pos_vfloat, op=binop),
829 )
830 setattr(
831 cls,
832 test_binop_name('value_pos_float'),
833 partialmethod(_TestNumericValue._test_binop_value_pos_float, op=binop),
834 )
835 setattr(
836 cls,
837 test_binop_name('value_pos_vfloat'),
838 partialmethod(_TestNumericValue._test_binop_value_pos_vfloat, op=binop),
839 )
840 setattr(
841 cls,
842 test_binop_name('lhs_addr_same_pos_float'),
843 partialmethod(
844 _TestNumericValue._test_binop_lhs_addr_same_pos_float, op=binop
845 ),
846 )
847 setattr(
848 cls,
849 test_binop_name('lhs_addr_same_pos_vfloat'),
850 partialmethod(
851 _TestNumericValue._test_binop_lhs_addr_same_pos_vfloat, op=binop
852 ),
853 )
854 setattr(
855 cls,
856 test_binop_name('lhs_value_same_pos_float'),
857 partialmethod(
858 _TestNumericValue._test_binop_lhs_value_same_pos_float, op=binop
859 ),
860 )
861 setattr(
862 cls,
863 test_binop_name('lhs_value_same_pos_vfloat'),
864 partialmethod(
865 _TestNumericValue._test_binop_lhs_value_same_pos_vfloat, op=binop
866 ),
867 )
868 setattr(
869 cls,
870 test_binop_name('type_zero_float'),
871 partialmethod(_TestNumericValue._test_binop_type_zero_float, op=binop),
872 )
873 setattr(
874 cls,
875 test_binop_name('type_zero_vfloat'),
876 partialmethod(_TestNumericValue._test_binop_type_zero_vfloat, op=binop),
877 )
878 setattr(
879 cls,
880 test_binop_name('value_zero_float'),
881 partialmethod(_TestNumericValue._test_binop_value_zero_float, op=binop),
882 )
883 setattr(
884 cls,
885 test_binop_name('value_zero_vfloat'),
886 partialmethod(_TestNumericValue._test_binop_value_zero_vfloat, op=binop),
887 )
888 setattr(
889 cls,
890 test_binop_name('lhs_addr_same_zero_float'),
891 partialmethod(
892 _TestNumericValue._test_binop_lhs_addr_same_zero_float, op=binop
893 ),
894 )
895 setattr(
896 cls,
897 test_binop_name('lhs_addr_same_zero_vfloat'),
898 partialmethod(
899 _TestNumericValue._test_binop_lhs_addr_same_zero_vfloat, op=binop
900 ),
901 )
902 setattr(
903 cls,
904 test_binop_name('lhs_value_same_zero_float'),
905 partialmethod(
906 _TestNumericValue._test_binop_lhs_value_same_zero_float, op=binop
907 ),
908 )
909 setattr(
910 cls,
911 test_binop_name('lhs_value_same_zero_vfloat'),
912 partialmethod(
913 _TestNumericValue._test_binop_lhs_value_same_zero_vfloat, op=binop
914 ),
915 )
916 setattr(
917 cls,
918 test_binop_name('type_complex'),
919 partialmethod(_TestNumericValue._test_binop_type_complex, op=binop),
920 )
921 setattr(
922 cls,
923 test_binop_name('type_zero_complex'),
924 partialmethod(_TestNumericValue._test_binop_type_zero_complex, op=binop),
925 )
926 setattr(
927 cls,
928 test_binop_name('value_complex'),
929 partialmethod(_TestNumericValue._test_binop_value_complex, op=binop),
930 )
931 setattr(
932 cls,
933 test_binop_name('value_zero_complex'),
934 partialmethod(_TestNumericValue._test_binop_value_zero_complex, op=binop),
935 )
936 setattr(
937 cls,
938 test_binop_name('lhs_addr_same_complex'),
939 partialmethod(
940 _TestNumericValue._test_binop_lhs_addr_same_complex, op=binop
941 ),
942 )
943 setattr(
944 cls,
945 test_binop_name('lhs_addr_same_zero_complex'),
946 partialmethod(
947 _TestNumericValue._test_binop_lhs_addr_same_zero_complex, op=binop
948 ),
949 )
950 setattr(
951 cls,
952 test_binop_name('lhs_value_same_complex'),
953 partialmethod(
954 _TestNumericValue._test_binop_lhs_value_same_complex, op=binop
955 ),
956 )
957 setattr(
958 cls,
959 test_binop_name('lhs_value_same_zero_complex'),
960 partialmethod(
961 _TestNumericValue._test_binop_lhs_value_same_zero_complex, op=binop
962 ),
963 )
9cf643d1
PP
964
965 # inject testing methods for each unary operation
966 for name, unaryop in _UNARYOPS:
61d96b89
FD
967 setattr(
968 cls,
969 test_unaryop_name('type'),
970 partialmethod(_TestNumericValue._test_unaryop_type, op=unaryop),
971 )
972 setattr(
973 cls,
974 test_unaryop_name('value'),
975 partialmethod(_TestNumericValue._test_unaryop_value, op=unaryop),
976 )
977 setattr(
978 cls,
979 test_unaryop_name('addr_same'),
980 partialmethod(_TestNumericValue._test_unaryop_addr_same, op=unaryop),
981 )
982 setattr(
983 cls,
984 test_unaryop_name('value_same'),
985 partialmethod(_TestNumericValue._test_unaryop_value_same, op=unaryop),
986 )
9cf643d1 987
9cf643d1
PP
988
989class CreateValueFuncTestCase(unittest.TestCase):
990 def test_create_none(self):
991 v = bt2.create_value(None)
992 self.assertIsNone(v)
993
994 def test_create_bool_false(self):
995 v = bt2.create_value(False)
996 self.assertIsInstance(v, bt2.BoolValue)
997 self.assertFalse(v)
998
999 def test_create_bool_true(self):
1000 v = bt2.create_value(True)
1001 self.assertIsInstance(v, bt2.BoolValue)
1002 self.assertTrue(v)
1003
1004 def test_create_int_pos(self):
1005 raw = 23
1006 v = bt2.create_value(raw)
68d9d039 1007 self.assertIsInstance(v, bt2.SignedIntegerValue)
9cf643d1
PP
1008 self.assertEqual(v, raw)
1009
1010 def test_create_int_neg(self):
1011 raw = -23
1012 v = bt2.create_value(raw)
68d9d039 1013 self.assertIsInstance(v, bt2.SignedIntegerValue)
9cf643d1
PP
1014 self.assertEqual(v, raw)
1015
1016 def test_create_float_pos(self):
1017 raw = 17.5
1018 v = bt2.create_value(raw)
77b70d39 1019 self.assertIsInstance(v, bt2.RealValue)
9cf643d1
PP
1020 self.assertEqual(v, raw)
1021
1022 def test_create_float_neg(self):
1023 raw = -17.5
1024 v = bt2.create_value(raw)
77b70d39 1025 self.assertIsInstance(v, bt2.RealValue)
9cf643d1
PP
1026 self.assertEqual(v, raw)
1027
1028 def test_create_string(self):
1029 raw = 'salut'
1030 v = bt2.create_value(raw)
1031 self.assertIsInstance(v, bt2.StringValue)
1032 self.assertEqual(v, raw)
1033
1034 def test_create_string_empty(self):
1035 raw = ''
1036 v = bt2.create_value(raw)
1037 self.assertIsInstance(v, bt2.StringValue)
1038 self.assertEqual(v, raw)
1039
1040 def test_create_array_from_list(self):
1041 raw = [1, 2, 3]
1042 v = bt2.create_value(raw)
1043 self.assertIsInstance(v, bt2.ArrayValue)
1044 self.assertEqual(v, raw)
1045
1046 def test_create_array_from_tuple(self):
1047 raw = 4, 5, 6
1048 v = bt2.create_value(raw)
1049 self.assertIsInstance(v, bt2.ArrayValue)
1050 self.assertEqual(v, raw)
1051
1052 def test_create_array_from_empty_list(self):
1053 raw = []
1054 v = bt2.create_value(raw)
1055 self.assertIsInstance(v, bt2.ArrayValue)
1056 self.assertEqual(v, raw)
1057
1058 def test_create_array_from_empty_tuple(self):
1059 raw = ()
1060 v = bt2.create_value(raw)
1061 self.assertIsInstance(v, bt2.ArrayValue)
1062 self.assertEqual(v, raw)
1063
1064 def test_create_map(self):
1065 raw = {'salut': 23}
1066 v = bt2.create_value(raw)
1067 self.assertIsInstance(v, bt2.MapValue)
1068 self.assertEqual(v, raw)
1069
1070 def test_create_map_empty(self):
1071 raw = {}
1072 v = bt2.create_value(raw)
1073 self.assertIsInstance(v, bt2.MapValue)
1074 self.assertEqual(v, raw)
1075
1076 def test_create_vfalse(self):
1077 v = bt2.create_value(bt2.create_value(False))
1078 self.assertIsInstance(v, bt2.BoolValue)
1079 self.assertFalse(v)
1080
1081 def test_create_invalid(self):
1082 class A:
1083 pass
1084
1085 a = A()
1086
61d96b89
FD
1087 with self.assertRaisesRegex(
1088 TypeError, "cannot create value object from 'A' object"
36153ada
SM
1089 ):
1090 bt2.create_value(a)
9cf643d1
PP
1091
1092
f19e40a1
FD
1093def _create_const_value(value):
1094 class MySink(bt2._UserSinkComponent):
1095 def _user_consume(self):
1096 pass
1097
1098 @classmethod
1099 def _user_query(cls, priv_query_exec, obj, params, method_obj):
1100 nonlocal value
1101 return {'my_value': value}
1102
1103 res = bt2.QueryExecutor(MySink, 'obj', None).query()
1104 return res['my_value']
1105
1106
a19825c6 1107class BoolValueTestCase(_TestNumericValue, unittest.TestCase):
9cf643d1
PP
1108 def setUp(self):
1109 self._f = bt2.BoolValue(False)
1110 self._t = bt2.BoolValue(True)
1111 self._def = self._f
1112 self._def_value = False
1113 self._def_new_value = True
1114
f6a5e476
PP
1115 def tearDown(self):
1116 del self._f
1117 del self._t
1118 del self._def
1119
9cf643d1
PP
1120 def _assert_expecting_bool(self):
1121 return self.assertRaisesRegex(TypeError, r"expecting a 'bool' object")
1122
1123 def test_create_default(self):
1124 b = bt2.BoolValue()
1125 self.assertFalse(b)
1126
1127 def test_create_false(self):
9cf643d1
PP
1128 self.assertFalse(self._f)
1129
1130 def test_create_true(self):
9cf643d1
PP
1131 self.assertTrue(self._t)
1132
1133 def test_create_from_vfalse(self):
1134 b = bt2.BoolValue(self._f)
9cf643d1
PP
1135 self.assertFalse(b)
1136
1137 def test_create_from_vtrue(self):
1138 b = bt2.BoolValue(self._t)
9cf643d1
PP
1139 self.assertTrue(b)
1140
1141 def test_create_from_int_non_zero(self):
1142 with self.assertRaises(TypeError):
36153ada 1143 bt2.BoolValue(23)
9cf643d1
PP
1144
1145 def test_create_from_int_zero(self):
1146 with self.assertRaises(TypeError):
36153ada 1147 bt2.BoolValue(0)
9cf643d1
PP
1148
1149 def test_assign_true(self):
1150 b = bt2.BoolValue()
1151 b.value = True
1152 self.assertTrue(b)
1153
1154 def test_assign_false(self):
1155 b = bt2.BoolValue()
1156 b.value = False
1157 self.assertFalse(b)
1158
1159 def test_assign_vtrue(self):
1160 b = bt2.BoolValue()
1161 b.value = self._t
1162 self.assertTrue(b)
1163
1164 def test_assign_vfalse(self):
1165 b = bt2.BoolValue()
1166 b.value = False
1167 self.assertFalse(b)
1168
1169 def test_assign_int(self):
1170 with self.assertRaises(TypeError):
1171 b = bt2.BoolValue()
1172 b.value = 23
1173
1174 def test_bool_op(self):
1175 self.assertEqual(bool(self._def), bool(self._def_value))
1176
1177 def test_str_op(self):
1178 self.assertEqual(str(self._def), str(self._def_value))
1179
1180 def test_eq_none(self):
13b81b5d
SM
1181 # Disable the "comparison to None" warning, as this is precisely what
1182 # we want to test here.
1183 self.assertFalse(self._def == None) # noqa: E711
9cf643d1
PP
1184
1185 def test_ne_none(self):
13b81b5d
SM
1186 # Disable the "comparison to None" warning, as this is precisely what
1187 # we want to test here.
1188 self.assertTrue(self._def != None) # noqa: E711
9cf643d1
PP
1189
1190 def test_vfalse_eq_false(self):
1191 self.assertEqual(self._f, False)
1192
1193 def test_vfalse_ne_true(self):
1194 self.assertNotEqual(self._f, True)
1195
1196 def test_vtrue_eq_true(self):
1197 self.assertEqual(self._t, True)
1198
1199 def test_vtrue_ne_false(self):
1200 self.assertNotEqual(self._t, False)
1201
1202
a19825c6
PP
1203_inject_numeric_testing_methods(BoolValueTestCase)
1204
1205
68d9d039 1206class _TestIntegerValue(_TestNumericValue):
9cf643d1
PP
1207 def setUp(self):
1208 self._pv = 23
68d9d039 1209 self._ip = self._CLS(self._pv)
9cf643d1
PP
1210 self._def = self._ip
1211 self._def_value = self._pv
68d9d039 1212 self._def_new_value = 101
9cf643d1 1213
f6a5e476
PP
1214 def tearDown(self):
1215 del self._ip
f6a5e476
PP
1216 del self._def
1217 del self._def_value
1218
9cf643d1 1219 def _assert_expecting_int(self):
5100844d 1220 return self.assertRaisesRegex(TypeError, r'expecting an integral number object')
9cf643d1
PP
1221
1222 def _assert_expecting_int64(self):
61d96b89
FD
1223 return self.assertRaisesRegex(
1224 ValueError, r"expecting a signed 64-bit integral value"
1225 )
9cf643d1
PP
1226
1227 def _assert_expecting_uint64(self):
61d96b89
FD
1228 return self.assertRaisesRegex(
1229 ValueError, r"expecting an unsigned 64-bit integral value"
1230 )
9cf643d1
PP
1231
1232 def test_create_default(self):
68d9d039 1233 i = self._CLS()
72bd7054 1234 self.assertEqual(i, 0)
9cf643d1
PP
1235
1236 def test_create_pos(self):
9cf643d1
PP
1237 self.assertEqual(self._ip, self._pv)
1238
1239 def test_create_neg(self):
9cf643d1
PP
1240 self.assertEqual(self._in, self._nv)
1241
9cf643d1 1242 def test_create_from_vint(self):
68d9d039 1243 i = self._CLS(self._ip)
9cf643d1
PP
1244 self.assertEqual(i, self._pv)
1245
1246 def test_create_from_false(self):
68d9d039 1247 i = self._CLS(False)
9cf643d1
PP
1248 self.assertFalse(i)
1249
1250 def test_create_from_true(self):
68d9d039 1251 i = self._CLS(True)
9cf643d1
PP
1252 self.assertTrue(i)
1253
9cf643d1
PP
1254 def test_create_from_unknown(self):
1255 class A:
1256 pass
1257
1258 with self._assert_expecting_int():
36153ada 1259 self._CLS(A())
9cf643d1
PP
1260
1261 def test_create_from_varray(self):
1262 with self._assert_expecting_int():
36153ada 1263 self._CLS(bt2.ArrayValue())
9cf643d1
PP
1264
1265 def test_assign_true(self):
1266 raw = True
1267 self._def.value = raw
1268 self.assertEqual(self._def, raw)
9cf643d1
PP
1269
1270 def test_assign_false(self):
1271 raw = False
1272 self._def.value = raw
1273 self.assertEqual(self._def, raw)
9cf643d1
PP
1274
1275 def test_assign_pos_int(self):
1276 raw = 477
1277 self._def.value = raw
1278 self.assertEqual(self._def, raw)
9cf643d1 1279
9cf643d1
PP
1280 def test_assign_vint(self):
1281 raw = 999
1282 self._def.value = bt2.create_value(raw)
1283 self.assertEqual(self._def, raw)
9cf643d1 1284
9cf643d1 1285
68d9d039
PP
1286class SignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase):
1287 _CLS = bt2.SignedIntegerValue
1288
1289 def setUp(self):
1290 super().setUp()
1291 self._nv = -52
1292 self._in = self._CLS(self._nv)
1293 self._def_new_value = -101
1294
1295 def tearDown(self):
1296 super().tearDown()
1297 del self._in
1298
1299 def test_create_neg(self):
1300 self.assertEqual(self._in, self._nv)
1301
1302 def test_create_pos_too_big(self):
1303 with self._assert_expecting_int64():
36153ada 1304 self._CLS(2 ** 63)
68d9d039
PP
1305
1306 def test_create_neg_too_big(self):
1307 with self._assert_expecting_int64():
36153ada 1308 self._CLS(-(2 ** 63) - 1)
68d9d039
PP
1309
1310 def test_assign_neg_int(self):
1311 raw = -13
1312 self._def.value = raw
1313 self.assertEqual(self._def, raw)
1314
9eb7fb56
FD
1315 def test_compare_big_int(self):
1316 # Larger than the IEEE 754 double-precision exact representation of
1317 # integers.
61d96b89 1318 raw = (2 ** 53) + 1
9eb7fb56
FD
1319 v = bt2.create_value(raw)
1320 self.assertEqual(v, raw)
1321
68d9d039
PP
1322
1323_inject_numeric_testing_methods(SignedIntegerValueTestCase)
1324
1325
1326class UnsignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase):
1327 _CLS = bt2.UnsignedIntegerValue
1328
1329 def test_create_pos_too_big(self):
1330 with self._assert_expecting_uint64():
36153ada 1331 self._CLS(2 ** 64)
68d9d039
PP
1332
1333 def test_create_neg(self):
1334 with self._assert_expecting_uint64():
36153ada 1335 self._CLS(-1)
68d9d039
PP
1336
1337
a4a14cd0 1338_inject_numeric_testing_methods(UnsignedIntegerValueTestCase)
9cf643d1
PP
1339
1340
77b70d39 1341class RealValueTestCase(_TestNumericValue, unittest.TestCase):
9cf643d1
PP
1342 def setUp(self):
1343 self._pv = 23.4
1344 self._nv = -52.7
77b70d39
SM
1345 self._fp = bt2.RealValue(self._pv)
1346 self._fn = bt2.RealValue(self._nv)
9cf643d1
PP
1347 self._def = self._fp
1348 self._def_value = self._pv
1349 self._def_new_value = -101.88
1350
f6a5e476
PP
1351 def tearDown(self):
1352 del self._fp
1353 del self._fn
1354 del self._def
1355 del self._def_value
1356
9cf643d1
PP
1357 def _assert_expecting_float(self):
1358 return self.assertRaisesRegex(TypeError, r"expecting a real number object")
1359
1360 def _test_invalid_op(self, cb):
1361 with self.assertRaises(TypeError):
1362 cb()
1363
1364 def test_create_default(self):
77b70d39 1365 f = bt2.RealValue()
72bd7054 1366 self.assertEqual(f, 0.0)
9cf643d1
PP
1367
1368 def test_create_pos(self):
9cf643d1
PP
1369 self.assertEqual(self._fp, self._pv)
1370
1371 def test_create_neg(self):
9cf643d1
PP
1372 self.assertEqual(self._fn, self._nv)
1373
9cf643d1 1374 def test_create_from_false(self):
77b70d39 1375 f = bt2.RealValue(False)
9cf643d1
PP
1376 self.assertFalse(f)
1377
1378 def test_create_from_true(self):
77b70d39 1379 f = bt2.RealValue(True)
9cf643d1
PP
1380 self.assertTrue(f)
1381
1382 def test_create_from_int(self):
1383 raw = 17
77b70d39 1384 f = bt2.RealValue(raw)
72bd7054 1385 self.assertEqual(f, float(raw))
9cf643d1
PP
1386
1387 def test_create_from_vint(self):
1388 raw = 17
77b70d39 1389 f = bt2.RealValue(bt2.create_value(raw))
72bd7054 1390 self.assertEqual(f, float(raw))
9cf643d1
PP
1391
1392 def test_create_from_vfloat(self):
1393 raw = 17.17
77b70d39 1394 f = bt2.RealValue(bt2.create_value(raw))
72bd7054 1395 self.assertEqual(f, raw)
9cf643d1
PP
1396
1397 def test_create_from_unknown(self):
1398 class A:
1399 pass
1400
1401 with self._assert_expecting_float():
36153ada 1402 bt2.RealValue(A())
9cf643d1
PP
1403
1404 def test_create_from_varray(self):
1405 with self._assert_expecting_float():
36153ada 1406 bt2.RealValue(bt2.ArrayValue())
9cf643d1
PP
1407
1408 def test_assign_true(self):
1409 self._def.value = True
1410 self.assertTrue(self._def)
9cf643d1
PP
1411
1412 def test_assign_false(self):
1413 self._def.value = False
1414 self.assertFalse(self._def)
9cf643d1
PP
1415
1416 def test_assign_pos_int(self):
1417 raw = 477
1418 self._def.value = raw
1419 self.assertEqual(self._def, float(raw))
9cf643d1
PP
1420
1421 def test_assign_neg_int(self):
1422 raw = -13
1423 self._def.value = raw
1424 self.assertEqual(self._def, float(raw))
9cf643d1
PP
1425
1426 def test_assign_vint(self):
1427 raw = 999
1428 self._def.value = bt2.create_value(raw)
1429 self.assertEqual(self._def, float(raw))
9cf643d1
PP
1430
1431 def test_assign_float(self):
1432 raw = -19.23
1433 self._def.value = raw
1434 self.assertEqual(self._def, raw)
9cf643d1
PP
1435
1436 def test_assign_vfloat(self):
1437 raw = 101.32
1438 self._def.value = bt2.create_value(raw)
1439 self.assertEqual(self._def, raw)
9cf643d1
PP
1440
1441 def test_invalid_lshift(self):
1442 self._test_invalid_op(lambda: self._def << 23)
1443
1444 def test_invalid_rshift(self):
1445 self._test_invalid_op(lambda: self._def >> 23)
1446
1447 def test_invalid_and(self):
1448 self._test_invalid_op(lambda: self._def & 23)
1449
1450 def test_invalid_or(self):
1451 self._test_invalid_op(lambda: self._def | 23)
1452
1453 def test_invalid_xor(self):
1454 self._test_invalid_op(lambda: self._def ^ 23)
1455
1456 def test_invalid_invert(self):
1457 self._test_invalid_op(lambda: ~self._def)
1458
1459
77b70d39 1460_inject_numeric_testing_methods(RealValueTestCase)
9cf643d1
PP
1461
1462
77b70d39 1463class StringValueTestCase(_TestCopySimple, unittest.TestCase):
9cf643d1
PP
1464 def setUp(self):
1465 self._def_value = 'Hello, World!'
1466 self._def = bt2.StringValue(self._def_value)
f19e40a1 1467 self._def_const = _create_const_value(self._def_value)
9cf643d1
PP
1468 self._def_new_value = 'Yes!'
1469
f6a5e476
PP
1470 def tearDown(self):
1471 del self._def
1472
9cf643d1
PP
1473 def _assert_expecting_str(self):
1474 return self.assertRaises(TypeError)
1475
1476 def test_create_default(self):
1477 s = bt2.StringValue()
72bd7054 1478 self.assertEqual(s, '')
9cf643d1
PP
1479
1480 def test_create_from_str(self):
1481 raw = 'liberté'
1482 s = bt2.StringValue(raw)
72bd7054 1483 self.assertEqual(s, raw)
9cf643d1
PP
1484
1485 def test_create_from_vstr(self):
1486 raw = 'liberté'
1487 s = bt2.StringValue(bt2.create_value(raw))
72bd7054 1488 self.assertEqual(s, raw)
9cf643d1
PP
1489
1490 def test_create_from_unknown(self):
1491 class A:
1492 pass
1493
1494 with self._assert_expecting_str():
36153ada 1495 bt2.StringValue(A())
9cf643d1
PP
1496
1497 def test_create_from_varray(self):
1498 with self._assert_expecting_str():
36153ada 1499 bt2.StringValue(bt2.ArrayValue())
9cf643d1
PP
1500
1501 def test_assign_int(self):
1502 with self._assert_expecting_str():
1503 self._def.value = 283
1504
1505 def test_assign_str(self):
1506 raw = 'zorg'
1507 self._def = raw
1508 self.assertEqual(self._def, raw)
1509
1510 def test_assign_vstr(self):
1511 raw = 'zorg'
1512 self._def = bt2.create_value(raw)
1513 self.assertEqual(self._def, raw)
1514
1515 def test_eq(self):
1516 self.assertEqual(self._def, self._def_value)
1517
f19e40a1
FD
1518 def test_const_eq(self):
1519 self.assertEqual(self._def_const, self._def_value)
1520
1521 def test_eq_raw(self):
9cf643d1
PP
1522 self.assertNotEqual(self._def, 23)
1523
1524 def test_lt_vstring(self):
1525 s1 = bt2.StringValue('allo')
1526 s2 = bt2.StringValue('bateau')
1527 self.assertLess(s1, s2)
1528
1529 def test_lt_string(self):
1530 s1 = bt2.StringValue('allo')
1531 self.assertLess(s1, 'bateau')
1532
1533 def test_le_vstring(self):
1534 s1 = bt2.StringValue('allo')
1535 s2 = bt2.StringValue('bateau')
1536 self.assertLessEqual(s1, s2)
1537
1538 def test_le_string(self):
1539 s1 = bt2.StringValue('allo')
1540 self.assertLessEqual(s1, 'bateau')
1541
1542 def test_gt_vstring(self):
1543 s1 = bt2.StringValue('allo')
1544 s2 = bt2.StringValue('bateau')
1545 self.assertGreater(s2, s1)
1546
1547 def test_gt_string(self):
1548 s1 = bt2.StringValue('allo')
1549 self.assertGreater('bateau', s1)
1550
1551 def test_ge_vstring(self):
1552 s1 = bt2.StringValue('allo')
1553 s2 = bt2.StringValue('bateau')
1554 self.assertGreaterEqual(s2, s1)
1555
1556 def test_ge_string(self):
1557 s1 = bt2.StringValue('allo')
1558 self.assertGreaterEqual('bateau', s1)
1559
ef12398a
FD
1560 def test_in_string(self):
1561 s1 = bt2.StringValue('beau grand bateau')
1562 self.assertIn('bateau', s1)
1563
1564 def test_in_vstring(self):
1565 s1 = bt2.StringValue('beau grand bateau')
1566 s2 = bt2.StringValue('bateau')
1567 self.assertIn(s2, s1)
1568
9cf643d1
PP
1569 def test_bool_op(self):
1570 self.assertEqual(bool(self._def), bool(self._def_value))
1571
1572 def test_str_op(self):
1573 self.assertEqual(str(self._def), str(self._def_value))
1574
1575 def test_len(self):
1576 self.assertEqual(len(self._def), len(self._def_value))
1577
1578 def test_getitem(self):
1579 self.assertEqual(self._def[5], self._def_value[5])
1580
f19e40a1
FD
1581 def test_const_getitem(self):
1582 self.assertEqual(self._def_const[5], self._def_value[5])
1583
1584 def test_iadd_str(self):
9cf643d1
PP
1585 to_append = 'meow meow meow'
1586 self._def += to_append
1587 self._def_value += to_append
1588 self.assertEqual(self._def, self._def_value)
1589
f19e40a1
FD
1590 def test_const_iadd_str(self):
1591 to_append = 'meow meow meow'
1592 with self.assertRaises(TypeError):
1593 self._def_const += to_append
1594
1595 self.assertEqual(self._def_const, self._def_value)
1596
9cf643d1
PP
1597 def test_append_vstr(self):
1598 to_append = 'meow meow meow'
1599 self._def += bt2.create_value(to_append)
1600 self._def_value += to_append
1601 self.assertEqual(self._def, self._def_value)
1602
1603
77b70d39 1604class ArrayValueTestCase(_TestCopySimple, unittest.TestCase):
9cf643d1
PP
1605 def setUp(self):
1606 self._def_value = [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1607 self._def = bt2.ArrayValue(copy.deepcopy(self._def_value))
f19e40a1 1608 self._def_const = _create_const_value(copy.deepcopy(self._def_value))
9cf643d1 1609
f6a5e476
PP
1610 def tearDown(self):
1611 del self._def
1612
9cf643d1
PP
1613 def _modify_def(self):
1614 self._def[2] = 'xyz'
1615
1616 def _assert_type_error(self):
1617 return self.assertRaises(TypeError)
1618
1619 def test_create_default(self):
1620 a = bt2.ArrayValue()
1621 self.assertEqual(len(a), 0)
1622
1623 def test_create_from_array(self):
1624 self.assertEqual(self._def, self._def_value)
1625
1626 def test_create_from_tuple(self):
1627 t = 1, 2, False, None
1628 a = bt2.ArrayValue(t)
1629 self.assertEqual(a, t)
1630
1631 def test_create_from_varray(self):
1632 va = bt2.ArrayValue(copy.deepcopy(self._def_value))
1633 a = bt2.ArrayValue(va)
1634 self.assertEqual(va, a)
1635
1636 def test_create_from_unknown(self):
1637 class A:
1638 pass
1639
1640 with self._assert_type_error():
36153ada 1641 bt2.ArrayValue(A())
9cf643d1
PP
1642
1643 def test_bool_op_true(self):
1644 self.assertTrue(bool(self._def))
1645
1646 def test_bool_op_false(self):
1647 self.assertFalse(bool(bt2.ArrayValue()))
1648
1649 def test_len(self):
1650 self.assertEqual(len(self._def), len(self._def_value))
1651
9cf643d1
PP
1652 def test_eq_int(self):
1653 self.assertNotEqual(self._def, 23)
1654
f19e40a1
FD
1655 def test_const_eq(self):
1656 a1 = _create_const_value([1, 2, 3])
1657 a2 = [1, 2, 3]
1658 self.assertEqual(a1, a2)
1659
9cf643d1
PP
1660 def test_eq_diff_len(self):
1661 a1 = bt2.create_value([1, 2, 3])
1662 a2 = bt2.create_value([1, 2])
f19e40a1
FD
1663 self.assertIs(type(a1), bt2.ArrayValue)
1664 self.assertIs(type(a2), bt2.ArrayValue)
9cf643d1
PP
1665 self.assertNotEqual(a1, a2)
1666
1667 def test_eq_diff_content_same_len(self):
1668 a1 = bt2.create_value([1, 2, 3])
1669 a2 = bt2.create_value([4, 5, 6])
1670 self.assertNotEqual(a1, a2)
1671
1672 def test_eq_same_content_same_len(self):
1673 raw = (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1674 a1 = bt2.ArrayValue(raw)
1675 a2 = bt2.ArrayValue(copy.deepcopy(raw))
1676 self.assertEqual(a1, a2)
1677
54495cd0
PP
1678 def test_eq_non_sequence_iterable(self):
1679 dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)])
1680 a = bt2.ArrayValue((1, 3, 5))
1681 self.assertEqual(a, list(dct.keys()))
1682 self.assertNotEqual(a, dct)
1683
9cf643d1
PP
1684 def test_setitem_int(self):
1685 raw = 19
1686 self._def[2] = raw
1687 self.assertEqual(self._def[2], raw)
1688
1689 def test_setitem_vint(self):
1690 raw = 19
1691 self._def[2] = bt2.create_value(raw)
1692 self.assertEqual(self._def[2], raw)
1693
1694 def test_setitem_none(self):
1695 self._def[2] = None
1696 self.assertIsNone(self._def[2])
1697
1698 def test_setitem_index_wrong_type(self):
1699 with self._assert_type_error():
1700 self._def['yes'] = 23
1701
1702 def test_setitem_index_neg(self):
1703 with self.assertRaises(IndexError):
1704 self._def[-2] = 23
1705
1706 def test_setitem_index_out_of_range(self):
1707 with self.assertRaises(IndexError):
1708 self._def[len(self._def)] = 23
1709
f19e40a1
FD
1710 def test_const_setitem(self):
1711 with self.assertRaises(TypeError):
1712 self._def_const[2] = 19
1713
9cf643d1
PP
1714 def test_append_none(self):
1715 self._def.append(None)
1716 self.assertIsNone(self._def[len(self._def) - 1])
1717
1718 def test_append_int(self):
1719 raw = 145
1720 self._def.append(raw)
1721 self.assertEqual(self._def[len(self._def) - 1], raw)
1722
f19e40a1
FD
1723 def test_const_append(self):
1724 with self.assertRaises(AttributeError):
1725 self._def_const.append(12194)
1726
9cf643d1
PP
1727 def test_append_vint(self):
1728 raw = 145
1729 self._def.append(bt2.create_value(raw))
1730 self.assertEqual(self._def[len(self._def) - 1], raw)
1731
1732 def test_append_unknown(self):
1733 class A:
1734 pass
1735
1736 with self._assert_type_error():
1737 self._def.append(A())
1738
1739 def test_iadd(self):
1740 raw = 4, 5, True
1741 self._def += raw
1742 self.assertEqual(self._def[len(self._def) - 3], raw[0])
1743 self.assertEqual(self._def[len(self._def) - 2], raw[1])
1744 self.assertEqual(self._def[len(self._def) - 1], raw[2])
1745
f19e40a1
FD
1746 def test_const_iadd(self):
1747 with self.assertRaises(TypeError):
1748 self._def_const += 12194
1749
9cf643d1
PP
1750 def test_iadd_unknown(self):
1751 class A:
1752 pass
1753
1754 with self._assert_type_error():
1755 self._def += A()
1756
1757 def test_iadd_list_unknown(self):
1758 class A:
1759 pass
1760
1761 with self._assert_type_error():
1762 self._def += [A()]
1763
1764 def test_iter(self):
1765 for velem, elem in zip(self._def, self._def_value):
1766 self.assertEqual(velem, elem)
1767
f19e40a1
FD
1768 def test_const_iter(self):
1769 for velem, elem in zip(self._def_const, self._def_value):
1770 self.assertEqual(velem, elem)
1771
1772 def test_const_get_item(self):
1773 item1 = self._def_const[0]
1774 item2 = self._def_const[2]
1775 item3 = self._def_const[5]
1776 item4 = self._def_const[7]
1777 item5 = self._def_const[8]
1778
1779 self.assertEqual(item1, None)
1780
1781 self.assertIs(type(item2), bt2._BoolValueConst)
1782 self.assertEqual(item2, True)
1783
1784 self.assertIs(type(item3), bt2._SignedIntegerValueConst)
1785 self.assertEqual(item3, 42)
1786
1787 self.assertIs(type(item4), bt2._RealValueConst)
1788 self.assertEqual(item4, 23.17)
1789
1790 self.assertIs(type(item5), bt2._StringValueConst)
1791 self.assertEqual(item5, 'yes')
1792
9cf643d1 1793
77b70d39 1794class MapValueTestCase(_TestCopySimple, unittest.TestCase):
9cf643d1
PP
1795 def setUp(self):
1796 self._def_value = {
1797 'none': None,
1798 'false': False,
1799 'true': True,
1800 'neg-int': -23,
1801 'zero': 0,
1802 'pos-int': 42,
1803 'neg-float': -42.4,
1804 'pos-float': 23.17,
61d96b89 1805 'str': 'yes',
9cf643d1
PP
1806 }
1807 self._def = bt2.MapValue(copy.deepcopy(self._def_value))
f19e40a1 1808 self._def_const = _create_const_value(self._def_value)
9cf643d1 1809
f6a5e476
PP
1810 def tearDown(self):
1811 del self._def
1812
9cf643d1
PP
1813 def _modify_def(self):
1814 self._def['zero'] = 1
1815
1816 def test_create_default(self):
1817 m = bt2.MapValue()
1818 self.assertEqual(len(m), 0)
1819
1820 def test_create_from_dict(self):
1821 self.assertEqual(self._def, self._def_value)
1822
1823 def test_create_from_vmap(self):
1824 vm = bt2.MapValue(copy.deepcopy(self._def_value))
1825 m = bt2.MapValue(vm)
1826 self.assertEqual(vm, m)
1827
1828 def test_create_from_unknown(self):
1829 class A:
1830 pass
1831
1832 with self.assertRaises(AttributeError):
36153ada 1833 bt2.MapValue(A())
9cf643d1
PP
1834
1835 def test_bool_op_true(self):
1836 self.assertTrue(bool(self._def))
1837
1838 def test_bool_op_false(self):
1839 self.assertFalse(bool(bt2.MapValue()))
1840
1841 def test_len(self):
1842 self.assertEqual(len(self._def), len(self._def_value))
1843
f19e40a1
FD
1844 def test_const_eq(self):
1845 a1 = _create_const_value({'a': 1, 'b': 2, 'c': 3})
1846 a2 = {'a': 1, 'b': 2, 'c': 3}
1847 self.assertEqual(a1, a2)
1848
9cf643d1
PP
1849 def test_eq_int(self):
1850 self.assertNotEqual(self._def, 23)
1851
1852 def test_eq_diff_len(self):
1853 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1854 a2 = bt2.create_value({'a': 1, 'b': 2})
1855 self.assertNotEqual(a1, a2)
1856
f19e40a1
FD
1857 def test_const_eq_diff_len(self):
1858 a1 = _create_const_value({'a': 1, 'b': 2, 'c': 3})
1859 a2 = _create_const_value({'a': 1, 'b': 2})
1860 self.assertNotEqual(a1, a2)
1861
9cf643d1
PP
1862 def test_eq_diff_content_same_len(self):
1863 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1864 a2 = bt2.create_value({'a': 4, 'b': 2, 'c': 3})
1865 self.assertNotEqual(a1, a2)
1866
f19e40a1
FD
1867 def test_const_eq_diff_content_same_len(self):
1868 a1 = _create_const_value({'a': 1, 'b': 2, 'c': 3})
1869 a2 = _create_const_value({'a': 4, 'b': 2, 'c': 3})
1870 self.assertNotEqual(a1, a2)
1871
9cf643d1
PP
1872 def test_eq_same_content_diff_keys(self):
1873 a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3})
1874 a2 = bt2.create_value({'a': 1, 'k': 2, 'c': 3})
1875 self.assertNotEqual(a1, a2)
1876
f19e40a1
FD
1877 def test_const_eq_same_content_diff_keys(self):
1878 a1 = _create_const_value({'a': 1, 'b': 2, 'c': 3})
1879 a2 = _create_const_value({'a': 1, 'k': 2, 'c': 3})
1880 self.assertNotEqual(a1, a2)
1881
9cf643d1 1882 def test_eq_same_content_same_len(self):
61d96b89 1883 raw = {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
9cf643d1
PP
1884 a1 = bt2.MapValue(raw)
1885 a2 = bt2.MapValue(copy.deepcopy(raw))
1886 self.assertEqual(a1, a2)
1887 self.assertEqual(a1, raw)
1888
f19e40a1
FD
1889 def test_const_eq_same_content_same_len(self):
1890 raw = {'3': 3, 'True': True, 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]}
1891 a1 = _create_const_value(raw)
1892 a2 = _create_const_value(copy.deepcopy(raw))
1893 self.assertEqual(a1, a2)
1894 self.assertEqual(a1, raw)
1895
9cf643d1
PP
1896 def test_setitem_int(self):
1897 raw = 19
1898 self._def['pos-int'] = raw
1899 self.assertEqual(self._def['pos-int'], raw)
1900
f19e40a1
FD
1901 def test_const_setitem_int(self):
1902 with self.assertRaises(TypeError):
1903 self._def_const['pos-int'] = 19
1904
9cf643d1
PP
1905 def test_setitem_vint(self):
1906 raw = 19
1907 self._def['pos-int'] = bt2.create_value(raw)
1908 self.assertEqual(self._def['pos-int'], raw)
1909
1910 def test_setitem_none(self):
1911 self._def['none'] = None
1912 self.assertIsNone(self._def['none'])
1913
1914 def test_setitem_new_int(self):
1915 old_len = len(self._def)
1916 self._def['new-int'] = 23
1917 self.assertEqual(self._def['new-int'], 23)
1918 self.assertEqual(len(self._def), old_len + 1)
1919
1920 def test_setitem_index_wrong_type(self):
1921 with self.assertRaises(TypeError):
1922 self._def[18] = 23
1923
1924 def test_iter(self):
1925 for vkey, vval in self._def.items():
1926 val = self._def_value[vkey]
1927 self.assertEqual(vval, val)
1928
f19e40a1
FD
1929 def test_const_iter(self):
1930 for vkey, vval in self._def_const.items():
1931 val = self._def_value[vkey]
1932 self.assertEqual(vval, val)
1933
1934 def test_get_item(self):
1935 i = self._def['pos-float']
1936 self.assertIs(type(i), bt2.RealValue)
1937 self.assertEqual(i, 23.17)
1938
1939 def test_const_get_item(self):
1940 item1 = self._def_const['none']
1941 item2 = self._def_const['true']
1942 item3 = self._def_const['pos-int']
1943 item4 = self._def_const['pos-float']
1944 item5 = self._def_const['str']
1945
1946 self.assertEqual(item1, None)
1947
1948 self.assertIs(type(item2), bt2._BoolValueConst)
1949 self.assertEqual(item2, True)
1950
1951 self.assertIs(type(item3), bt2._SignedIntegerValueConst)
1952 self.assertEqual(item3, 42)
1953
1954 self.assertIs(type(item4), bt2._RealValueConst)
1955 self.assertEqual(item4, 23.17)
1956
1957 self.assertIs(type(item5), bt2._StringValueConst)
1958 self.assertEqual(item5, 'yes')
1959
9cf643d1
PP
1960 def test_getitem_wrong_key(self):
1961 with self.assertRaises(KeyError):
1962 self._def['kilojoule']
3db06b1d
SM
1963
1964
1965if __name__ == '__main__':
1966 unittest.main()
This page took 0.168131 seconds and 4 git commands to generate.