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