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