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