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