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