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