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