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