Commit | Line | Data |
---|---|---|
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 |
19 | from functools import partial, partialmethod |
20 | import operator | |
21 | import unittest | |
22 | import numbers | |
23 | import math | |
24 | import copy | |
25 | import 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 |
31 | class _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 | 54 | class _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 | 538 | def _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 | |
614 | class 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 | ||
88be7fa4 | 716 | class BoolValueTestCase(_TestNumericValue, 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 | ||
88be7fa4 PP |
808 | _inject_numeric_testing_methods(BoolValueTestCase) |
809 | ||
810 | ||
fdd3a2da | 811 | class _TestIntegerValue(_TestNumericValue): |
9cf643d1 PP |
812 | def setUp(self): |
813 | self._pv = 23 | |
fdd3a2da | 814 | self._ip = self._CLS(self._pv) |
9cf643d1 PP |
815 | self._def = self._ip |
816 | self._def_value = self._pv | |
fdd3a2da | 817 | self._def_new_value = 101 |
9cf643d1 | 818 | |
811644b8 PP |
819 | def tearDown(self): |
820 | del self._ip | |
811644b8 PP |
821 | del self._def |
822 | del self._def_value | |
823 | ||
9cf643d1 | 824 | def _assert_expecting_int(self): |
e502b15a | 825 | return self.assertRaisesRegex(TypeError, r'expecting an integral number object') |
9cf643d1 PP |
826 | |
827 | def _assert_expecting_int64(self): | |
828 | return self.assertRaisesRegex(ValueError, r"expecting a signed 64-bit integral value") | |
829 | ||
830 | def _assert_expecting_uint64(self): | |
831 | return self.assertRaisesRegex(ValueError, r"expecting an unsigned 64-bit integral value") | |
832 | ||
833 | def test_create_default(self): | |
fdd3a2da | 834 | i = self._CLS() |
9b6cd4a7 | 835 | self.assertEqual(i, 0) |
9cf643d1 PP |
836 | |
837 | def test_create_pos(self): | |
9cf643d1 PP |
838 | self.assertEqual(self._ip, self._pv) |
839 | ||
840 | def test_create_neg(self): | |
9cf643d1 PP |
841 | self.assertEqual(self._in, self._nv) |
842 | ||
9cf643d1 | 843 | def test_create_from_vint(self): |
fdd3a2da | 844 | i = self._CLS(self._ip) |
9cf643d1 PP |
845 | self.assertEqual(i, self._pv) |
846 | ||
847 | def test_create_from_false(self): | |
fdd3a2da | 848 | i = self._CLS(False) |
9cf643d1 PP |
849 | self.assertFalse(i) |
850 | ||
851 | def test_create_from_true(self): | |
fdd3a2da | 852 | i = self._CLS(True) |
9cf643d1 PP |
853 | self.assertTrue(i) |
854 | ||
9cf643d1 PP |
855 | def test_create_from_unknown(self): |
856 | class A: | |
857 | pass | |
858 | ||
859 | with self._assert_expecting_int(): | |
fdd3a2da | 860 | i = self._CLS(A()) |
9cf643d1 PP |
861 | |
862 | def test_create_from_varray(self): | |
863 | with self._assert_expecting_int(): | |
fdd3a2da | 864 | i = self._CLS(bt2.ArrayValue()) |
9cf643d1 PP |
865 | |
866 | def test_assign_true(self): | |
867 | raw = True | |
868 | self._def.value = raw | |
869 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
870 | |
871 | def test_assign_false(self): | |
872 | raw = False | |
873 | self._def.value = raw | |
874 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
875 | |
876 | def test_assign_pos_int(self): | |
877 | raw = 477 | |
878 | self._def.value = raw | |
879 | self.assertEqual(self._def, raw) | |
9cf643d1 | 880 | |
9cf643d1 PP |
881 | def test_assign_vint(self): |
882 | raw = 999 | |
883 | self._def.value = bt2.create_value(raw) | |
884 | self.assertEqual(self._def, raw) | |
9cf643d1 | 885 | |
9cf643d1 | 886 | |
fdd3a2da PP |
887 | class SignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase): |
888 | _CLS = bt2.SignedIntegerValue | |
889 | ||
890 | def setUp(self): | |
891 | super().setUp() | |
892 | self._nv = -52 | |
893 | self._in = self._CLS(self._nv) | |
894 | self._def_new_value = -101 | |
895 | ||
896 | def tearDown(self): | |
897 | super().tearDown() | |
898 | del self._in | |
899 | ||
900 | def test_create_neg(self): | |
901 | self.assertEqual(self._in, self._nv) | |
902 | ||
903 | def test_create_pos_too_big(self): | |
904 | with self._assert_expecting_int64(): | |
905 | i = self._CLS(2 ** 63) | |
906 | ||
907 | def test_create_neg_too_big(self): | |
908 | with self._assert_expecting_int64(): | |
909 | i = self._CLS(-(2 ** 63) - 1) | |
910 | ||
911 | def test_assign_neg_int(self): | |
912 | raw = -13 | |
913 | self._def.value = raw | |
914 | self.assertEqual(self._def, raw) | |
915 | ||
7bb4180f FD |
916 | def test_compare_big_int(self): |
917 | # Larger than the IEEE 754 double-precision exact representation of | |
918 | # integers. | |
919 | raw = (2**53) + 1 | |
920 | v = bt2.create_value(raw) | |
921 | self.assertEqual(v, raw) | |
922 | ||
fdd3a2da PP |
923 | |
924 | _inject_numeric_testing_methods(SignedIntegerValueTestCase) | |
925 | ||
926 | ||
927 | class UnsignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase): | |
928 | _CLS = bt2.UnsignedIntegerValue | |
929 | ||
930 | def test_create_pos_too_big(self): | |
931 | with self._assert_expecting_uint64(): | |
932 | i = self._CLS(2 ** 64) | |
933 | ||
934 | def test_create_neg(self): | |
935 | with self._assert_expecting_uint64(): | |
936 | i = self._CLS(-1) | |
937 | ||
938 | ||
ad24a7ac | 939 | _inject_numeric_testing_methods(UnsignedIntegerValueTestCase) |
9cf643d1 PP |
940 | |
941 | ||
10a19b49 | 942 | class RealValueTestCase(_TestNumericValue, unittest.TestCase): |
9cf643d1 PP |
943 | def setUp(self): |
944 | self._pv = 23.4 | |
945 | self._nv = -52.7 | |
10a19b49 SM |
946 | self._fp = bt2.RealValue(self._pv) |
947 | self._fn = bt2.RealValue(self._nv) | |
9cf643d1 PP |
948 | self._def = self._fp |
949 | self._def_value = self._pv | |
950 | self._def_new_value = -101.88 | |
951 | ||
811644b8 PP |
952 | def tearDown(self): |
953 | del self._fp | |
954 | del self._fn | |
955 | del self._def | |
956 | del self._def_value | |
957 | ||
9cf643d1 PP |
958 | def _assert_expecting_float(self): |
959 | return self.assertRaisesRegex(TypeError, r"expecting a real number object") | |
960 | ||
961 | def _test_invalid_op(self, cb): | |
962 | with self.assertRaises(TypeError): | |
963 | cb() | |
964 | ||
965 | def test_create_default(self): | |
10a19b49 | 966 | f = bt2.RealValue() |
9b6cd4a7 | 967 | self.assertEqual(f, 0.0) |
9cf643d1 PP |
968 | |
969 | def test_create_pos(self): | |
9cf643d1 PP |
970 | self.assertEqual(self._fp, self._pv) |
971 | ||
972 | def test_create_neg(self): | |
9cf643d1 PP |
973 | self.assertEqual(self._fn, self._nv) |
974 | ||
975 | def test_create_from_vint(self): | |
10a19b49 | 976 | f = bt2.RealValue(self._fp) |
9cf643d1 PP |
977 | self.assertEqual(f, self._pv) |
978 | ||
979 | def test_create_from_false(self): | |
10a19b49 | 980 | f = bt2.RealValue(False) |
9cf643d1 PP |
981 | self.assertFalse(f) |
982 | ||
983 | def test_create_from_true(self): | |
10a19b49 | 984 | f = bt2.RealValue(True) |
9cf643d1 PP |
985 | self.assertTrue(f) |
986 | ||
987 | def test_create_from_int(self): | |
988 | raw = 17 | |
10a19b49 | 989 | f = bt2.RealValue(raw) |
9b6cd4a7 | 990 | self.assertEqual(f, float(raw)) |
9cf643d1 PP |
991 | |
992 | def test_create_from_vint(self): | |
993 | raw = 17 | |
10a19b49 | 994 | f = bt2.RealValue(bt2.create_value(raw)) |
9b6cd4a7 | 995 | self.assertEqual(f, float(raw)) |
9cf643d1 PP |
996 | |
997 | def test_create_from_vfloat(self): | |
998 | raw = 17.17 | |
10a19b49 | 999 | f = bt2.RealValue(bt2.create_value(raw)) |
9b6cd4a7 | 1000 | self.assertEqual(f, raw) |
9cf643d1 PP |
1001 | |
1002 | def test_create_from_unknown(self): | |
1003 | class A: | |
1004 | pass | |
1005 | ||
1006 | with self._assert_expecting_float(): | |
10a19b49 | 1007 | f = bt2.RealValue(A()) |
9cf643d1 PP |
1008 | |
1009 | def test_create_from_varray(self): | |
1010 | with self._assert_expecting_float(): | |
10a19b49 | 1011 | f = bt2.RealValue(bt2.ArrayValue()) |
9cf643d1 PP |
1012 | |
1013 | def test_assign_true(self): | |
1014 | self._def.value = True | |
1015 | self.assertTrue(self._def) | |
9cf643d1 PP |
1016 | |
1017 | def test_assign_false(self): | |
1018 | self._def.value = False | |
1019 | self.assertFalse(self._def) | |
9cf643d1 PP |
1020 | |
1021 | def test_assign_pos_int(self): | |
1022 | raw = 477 | |
1023 | self._def.value = raw | |
1024 | self.assertEqual(self._def, float(raw)) | |
9cf643d1 PP |
1025 | |
1026 | def test_assign_neg_int(self): | |
1027 | raw = -13 | |
1028 | self._def.value = raw | |
1029 | self.assertEqual(self._def, float(raw)) | |
9cf643d1 PP |
1030 | |
1031 | def test_assign_vint(self): | |
1032 | raw = 999 | |
1033 | self._def.value = bt2.create_value(raw) | |
1034 | self.assertEqual(self._def, float(raw)) | |
9cf643d1 PP |
1035 | |
1036 | def test_assign_float(self): | |
1037 | raw = -19.23 | |
1038 | self._def.value = raw | |
1039 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
1040 | |
1041 | def test_assign_vfloat(self): | |
1042 | raw = 101.32 | |
1043 | self._def.value = bt2.create_value(raw) | |
1044 | self.assertEqual(self._def, raw) | |
9cf643d1 PP |
1045 | |
1046 | def test_invalid_lshift(self): | |
1047 | self._test_invalid_op(lambda: self._def << 23) | |
1048 | ||
1049 | def test_invalid_rshift(self): | |
1050 | self._test_invalid_op(lambda: self._def >> 23) | |
1051 | ||
1052 | def test_invalid_and(self): | |
1053 | self._test_invalid_op(lambda: self._def & 23) | |
1054 | ||
1055 | def test_invalid_or(self): | |
1056 | self._test_invalid_op(lambda: self._def | 23) | |
1057 | ||
1058 | def test_invalid_xor(self): | |
1059 | self._test_invalid_op(lambda: self._def ^ 23) | |
1060 | ||
1061 | def test_invalid_invert(self): | |
1062 | self._test_invalid_op(lambda: ~self._def) | |
1063 | ||
1064 | ||
10a19b49 | 1065 | _inject_numeric_testing_methods(RealValueTestCase) |
9cf643d1 PP |
1066 | |
1067 | ||
10a19b49 | 1068 | class StringValueTestCase(_TestCopySimple, unittest.TestCase): |
9cf643d1 PP |
1069 | def setUp(self): |
1070 | self._def_value = 'Hello, World!' | |
1071 | self._def = bt2.StringValue(self._def_value) | |
1072 | self._def_new_value = 'Yes!' | |
1073 | ||
811644b8 PP |
1074 | def tearDown(self): |
1075 | del self._def | |
1076 | ||
9cf643d1 PP |
1077 | def _assert_expecting_str(self): |
1078 | return self.assertRaises(TypeError) | |
1079 | ||
1080 | def test_create_default(self): | |
1081 | s = bt2.StringValue() | |
9b6cd4a7 | 1082 | self.assertEqual(s, '') |
9cf643d1 PP |
1083 | |
1084 | def test_create_from_str(self): | |
1085 | raw = 'liberté' | |
1086 | s = bt2.StringValue(raw) | |
9b6cd4a7 | 1087 | self.assertEqual(s, raw) |
9cf643d1 PP |
1088 | |
1089 | def test_create_from_vstr(self): | |
1090 | raw = 'liberté' | |
1091 | s = bt2.StringValue(bt2.create_value(raw)) | |
9b6cd4a7 | 1092 | self.assertEqual(s, raw) |
9cf643d1 PP |
1093 | |
1094 | def test_create_from_unknown(self): | |
1095 | class A: | |
1096 | pass | |
1097 | ||
1098 | with self._assert_expecting_str(): | |
1099 | i = bt2.StringValue(A()) | |
1100 | ||
1101 | def test_create_from_varray(self): | |
1102 | with self._assert_expecting_str(): | |
1103 | i = bt2.StringValue(bt2.ArrayValue()) | |
1104 | ||
1105 | def test_assign_int(self): | |
1106 | with self._assert_expecting_str(): | |
1107 | self._def.value = 283 | |
1108 | ||
1109 | def test_assign_str(self): | |
1110 | raw = 'zorg' | |
1111 | self._def = raw | |
1112 | self.assertEqual(self._def, raw) | |
1113 | ||
1114 | def test_assign_vstr(self): | |
1115 | raw = 'zorg' | |
1116 | self._def = bt2.create_value(raw) | |
1117 | self.assertEqual(self._def, raw) | |
1118 | ||
1119 | def test_eq(self): | |
1120 | self.assertEqual(self._def, self._def_value) | |
1121 | ||
1122 | def test_eq(self): | |
1123 | self.assertNotEqual(self._def, 23) | |
1124 | ||
1125 | def test_lt_vstring(self): | |
1126 | s1 = bt2.StringValue('allo') | |
1127 | s2 = bt2.StringValue('bateau') | |
1128 | self.assertLess(s1, s2) | |
1129 | ||
1130 | def test_lt_string(self): | |
1131 | s1 = bt2.StringValue('allo') | |
1132 | self.assertLess(s1, 'bateau') | |
1133 | ||
1134 | def test_le_vstring(self): | |
1135 | s1 = bt2.StringValue('allo') | |
1136 | s2 = bt2.StringValue('bateau') | |
1137 | self.assertLessEqual(s1, s2) | |
1138 | ||
1139 | def test_le_string(self): | |
1140 | s1 = bt2.StringValue('allo') | |
1141 | self.assertLessEqual(s1, 'bateau') | |
1142 | ||
1143 | def test_gt_vstring(self): | |
1144 | s1 = bt2.StringValue('allo') | |
1145 | s2 = bt2.StringValue('bateau') | |
1146 | self.assertGreater(s2, s1) | |
1147 | ||
1148 | def test_gt_string(self): | |
1149 | s1 = bt2.StringValue('allo') | |
1150 | self.assertGreater('bateau', s1) | |
1151 | ||
1152 | def test_ge_vstring(self): | |
1153 | s1 = bt2.StringValue('allo') | |
1154 | s2 = bt2.StringValue('bateau') | |
1155 | self.assertGreaterEqual(s2, s1) | |
1156 | ||
1157 | def test_ge_string(self): | |
1158 | s1 = bt2.StringValue('allo') | |
1159 | self.assertGreaterEqual('bateau', s1) | |
1160 | ||
1161 | def test_bool_op(self): | |
1162 | self.assertEqual(bool(self._def), bool(self._def_value)) | |
1163 | ||
1164 | def test_str_op(self): | |
1165 | self.assertEqual(str(self._def), str(self._def_value)) | |
1166 | ||
1167 | def test_len(self): | |
1168 | self.assertEqual(len(self._def), len(self._def_value)) | |
1169 | ||
1170 | def test_getitem(self): | |
1171 | self.assertEqual(self._def[5], self._def_value[5]) | |
1172 | ||
1173 | def test_append_str(self): | |
1174 | to_append = 'meow meow meow' | |
1175 | self._def += to_append | |
1176 | self._def_value += to_append | |
1177 | self.assertEqual(self._def, self._def_value) | |
1178 | ||
1179 | def test_append_vstr(self): | |
1180 | to_append = 'meow meow meow' | |
1181 | self._def += bt2.create_value(to_append) | |
1182 | self._def_value += to_append | |
1183 | self.assertEqual(self._def, self._def_value) | |
1184 | ||
1185 | ||
10a19b49 | 1186 | class ArrayValueTestCase(_TestCopySimple, unittest.TestCase): |
9cf643d1 PP |
1187 | def setUp(self): |
1188 | self._def_value = [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes'] | |
1189 | self._def = bt2.ArrayValue(copy.deepcopy(self._def_value)) | |
1190 | ||
811644b8 PP |
1191 | def tearDown(self): |
1192 | del self._def | |
1193 | ||
9cf643d1 PP |
1194 | def _modify_def(self): |
1195 | self._def[2] = 'xyz' | |
1196 | ||
1197 | def _assert_type_error(self): | |
1198 | return self.assertRaises(TypeError) | |
1199 | ||
1200 | def test_create_default(self): | |
1201 | a = bt2.ArrayValue() | |
1202 | self.assertEqual(len(a), 0) | |
1203 | ||
1204 | def test_create_from_array(self): | |
1205 | self.assertEqual(self._def, self._def_value) | |
1206 | ||
1207 | def test_create_from_tuple(self): | |
1208 | t = 1, 2, False, None | |
1209 | a = bt2.ArrayValue(t) | |
1210 | self.assertEqual(a, t) | |
1211 | ||
1212 | def test_create_from_varray(self): | |
1213 | va = bt2.ArrayValue(copy.deepcopy(self._def_value)) | |
1214 | a = bt2.ArrayValue(va) | |
1215 | self.assertEqual(va, a) | |
1216 | ||
1217 | def test_create_from_unknown(self): | |
1218 | class A: | |
1219 | pass | |
1220 | ||
1221 | with self._assert_type_error(): | |
1222 | a = bt2.ArrayValue(A()) | |
1223 | ||
1224 | def test_bool_op_true(self): | |
1225 | self.assertTrue(bool(self._def)) | |
1226 | ||
1227 | def test_bool_op_false(self): | |
1228 | self.assertFalse(bool(bt2.ArrayValue())) | |
1229 | ||
1230 | def test_len(self): | |
1231 | self.assertEqual(len(self._def), len(self._def_value)) | |
1232 | ||
9cf643d1 PP |
1233 | def test_eq_int(self): |
1234 | self.assertNotEqual(self._def, 23) | |
1235 | ||
1236 | def test_eq_diff_len(self): | |
1237 | a1 = bt2.create_value([1, 2, 3]) | |
1238 | a2 = bt2.create_value([1, 2]) | |
1239 | self.assertNotEqual(a1, a2) | |
1240 | ||
1241 | def test_eq_diff_content_same_len(self): | |
1242 | a1 = bt2.create_value([1, 2, 3]) | |
1243 | a2 = bt2.create_value([4, 5, 6]) | |
1244 | self.assertNotEqual(a1, a2) | |
1245 | ||
1246 | def test_eq_same_content_same_len(self): | |
1247 | raw = (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}]) | |
1248 | a1 = bt2.ArrayValue(raw) | |
1249 | a2 = bt2.ArrayValue(copy.deepcopy(raw)) | |
1250 | self.assertEqual(a1, a2) | |
1251 | ||
1252 | def test_setitem_int(self): | |
1253 | raw = 19 | |
1254 | self._def[2] = raw | |
1255 | self.assertEqual(self._def[2], raw) | |
1256 | ||
1257 | def test_setitem_vint(self): | |
1258 | raw = 19 | |
1259 | self._def[2] = bt2.create_value(raw) | |
1260 | self.assertEqual(self._def[2], raw) | |
1261 | ||
1262 | def test_setitem_none(self): | |
1263 | self._def[2] = None | |
1264 | self.assertIsNone(self._def[2]) | |
1265 | ||
1266 | def test_setitem_index_wrong_type(self): | |
1267 | with self._assert_type_error(): | |
1268 | self._def['yes'] = 23 | |
1269 | ||
1270 | def test_setitem_index_neg(self): | |
1271 | with self.assertRaises(IndexError): | |
1272 | self._def[-2] = 23 | |
1273 | ||
1274 | def test_setitem_index_out_of_range(self): | |
1275 | with self.assertRaises(IndexError): | |
1276 | self._def[len(self._def)] = 23 | |
1277 | ||
1278 | def test_append_none(self): | |
1279 | self._def.append(None) | |
1280 | self.assertIsNone(self._def[len(self._def) - 1]) | |
1281 | ||
1282 | def test_append_int(self): | |
1283 | raw = 145 | |
1284 | self._def.append(raw) | |
1285 | self.assertEqual(self._def[len(self._def) - 1], raw) | |
1286 | ||
1287 | def test_append_vint(self): | |
1288 | raw = 145 | |
1289 | self._def.append(bt2.create_value(raw)) | |
1290 | self.assertEqual(self._def[len(self._def) - 1], raw) | |
1291 | ||
1292 | def test_append_unknown(self): | |
1293 | class A: | |
1294 | pass | |
1295 | ||
1296 | with self._assert_type_error(): | |
1297 | self._def.append(A()) | |
1298 | ||
1299 | def test_iadd(self): | |
1300 | raw = 4, 5, True | |
1301 | self._def += raw | |
1302 | self.assertEqual(self._def[len(self._def) - 3], raw[0]) | |
1303 | self.assertEqual(self._def[len(self._def) - 2], raw[1]) | |
1304 | self.assertEqual(self._def[len(self._def) - 1], raw[2]) | |
1305 | ||
1306 | def test_iadd_unknown(self): | |
1307 | class A: | |
1308 | pass | |
1309 | ||
1310 | with self._assert_type_error(): | |
1311 | self._def += A() | |
1312 | ||
1313 | def test_iadd_list_unknown(self): | |
1314 | class A: | |
1315 | pass | |
1316 | ||
1317 | with self._assert_type_error(): | |
1318 | self._def += [A()] | |
1319 | ||
1320 | def test_iter(self): | |
1321 | for velem, elem in zip(self._def, self._def_value): | |
1322 | self.assertEqual(velem, elem) | |
1323 | ||
1324 | ||
10a19b49 | 1325 | class MapValueTestCase(_TestCopySimple, unittest.TestCase): |
9cf643d1 PP |
1326 | def setUp(self): |
1327 | self._def_value = { | |
1328 | 'none': None, | |
1329 | 'false': False, | |
1330 | 'true': True, | |
1331 | 'neg-int': -23, | |
1332 | 'zero': 0, | |
1333 | 'pos-int': 42, | |
1334 | 'neg-float': -42.4, | |
1335 | 'pos-float': 23.17, | |
1336 | 'str': 'yes' | |
1337 | } | |
1338 | self._def = bt2.MapValue(copy.deepcopy(self._def_value)) | |
1339 | ||
811644b8 PP |
1340 | def tearDown(self): |
1341 | del self._def | |
1342 | ||
9cf643d1 PP |
1343 | def _modify_def(self): |
1344 | self._def['zero'] = 1 | |
1345 | ||
1346 | def test_create_default(self): | |
1347 | m = bt2.MapValue() | |
1348 | self.assertEqual(len(m), 0) | |
1349 | ||
1350 | def test_create_from_dict(self): | |
1351 | self.assertEqual(self._def, self._def_value) | |
1352 | ||
1353 | def test_create_from_vmap(self): | |
1354 | vm = bt2.MapValue(copy.deepcopy(self._def_value)) | |
1355 | m = bt2.MapValue(vm) | |
1356 | self.assertEqual(vm, m) | |
1357 | ||
1358 | def test_create_from_unknown(self): | |
1359 | class A: | |
1360 | pass | |
1361 | ||
1362 | with self.assertRaises(AttributeError): | |
1363 | m = bt2.MapValue(A()) | |
1364 | ||
1365 | def test_bool_op_true(self): | |
1366 | self.assertTrue(bool(self._def)) | |
1367 | ||
1368 | def test_bool_op_false(self): | |
1369 | self.assertFalse(bool(bt2.MapValue())) | |
1370 | ||
1371 | def test_len(self): | |
1372 | self.assertEqual(len(self._def), len(self._def_value)) | |
1373 | ||
9cf643d1 PP |
1374 | def test_eq_int(self): |
1375 | self.assertNotEqual(self._def, 23) | |
1376 | ||
1377 | def test_eq_diff_len(self): | |
1378 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) | |
1379 | a2 = bt2.create_value({'a': 1, 'b': 2}) | |
1380 | self.assertNotEqual(a1, a2) | |
1381 | ||
1382 | def test_eq_diff_content_same_len(self): | |
1383 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) | |
1384 | a2 = bt2.create_value({'a': 4, 'b': 2, 'c': 3}) | |
1385 | self.assertNotEqual(a1, a2) | |
1386 | ||
1387 | def test_eq_same_content_diff_keys(self): | |
1388 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) | |
1389 | a2 = bt2.create_value({'a': 1, 'k': 2, 'c': 3}) | |
1390 | self.assertNotEqual(a1, a2) | |
1391 | ||
1392 | def test_eq_same_content_same_len(self): | |
1393 | raw = { | |
1394 | '3': 3, | |
1395 | 'True': True, | |
1396 | 'array': [1, 2.5, None, {'a': 17.6, 'b': None}] | |
1397 | } | |
1398 | a1 = bt2.MapValue(raw) | |
1399 | a2 = bt2.MapValue(copy.deepcopy(raw)) | |
1400 | self.assertEqual(a1, a2) | |
1401 | self.assertEqual(a1, raw) | |
1402 | ||
1403 | def test_setitem_int(self): | |
1404 | raw = 19 | |
1405 | self._def['pos-int'] = raw | |
1406 | self.assertEqual(self._def['pos-int'], raw) | |
1407 | ||
1408 | def test_setitem_vint(self): | |
1409 | raw = 19 | |
1410 | self._def['pos-int'] = bt2.create_value(raw) | |
1411 | self.assertEqual(self._def['pos-int'], raw) | |
1412 | ||
1413 | def test_setitem_none(self): | |
1414 | self._def['none'] = None | |
1415 | self.assertIsNone(self._def['none']) | |
1416 | ||
1417 | def test_setitem_new_int(self): | |
1418 | old_len = len(self._def) | |
1419 | self._def['new-int'] = 23 | |
1420 | self.assertEqual(self._def['new-int'], 23) | |
1421 | self.assertEqual(len(self._def), old_len + 1) | |
1422 | ||
1423 | def test_setitem_index_wrong_type(self): | |
1424 | with self.assertRaises(TypeError): | |
1425 | self._def[18] = 23 | |
1426 | ||
1427 | def test_iter(self): | |
1428 | for vkey, vval in self._def.items(): | |
1429 | val = self._def_value[vkey] | |
1430 | self.assertEqual(vval, val) | |
1431 | ||
1432 | def test_getitem_wrong_key(self): | |
1433 | with self.assertRaises(KeyError): | |
1434 | self._def['kilojoule'] |