Python bt2: value properties for sequence and struct
[babeltrace.git] / tests / bindings / python / bt2 / test_fields.py
1 from functools import partial, partialmethod
2 import operator
3 import unittest
4 import numbers
5 import math
6 import copy
7 import bt2
8
9
10 class _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
30 class _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
519
520 _BINOPS = (
521 ('lt', operator.lt),
522 ('le', operator.le),
523 ('eq', operator.eq),
524 ('ne', operator.ne),
525 ('ge', operator.ge),
526 ('gt', operator.gt),
527 ('add', operator.add),
528 ('radd', lambda a, b: operator.add(b, a)),
529 ('and', operator.and_),
530 ('rand', lambda a, b: operator.and_(b, a)),
531 ('floordiv', operator.floordiv),
532 ('rfloordiv', lambda a, b: operator.floordiv(b, a)),
533 ('lshift', operator.lshift),
534 ('rlshift', lambda a, b: operator.lshift(b, a)),
535 ('mod', operator.mod),
536 ('rmod', lambda a, b: operator.mod(b, a)),
537 ('mul', operator.mul),
538 ('rmul', lambda a, b: operator.mul(b, a)),
539 ('or', operator.or_),
540 ('ror', lambda a, b: operator.or_(b, a)),
541 ('pow', operator.pow),
542 ('rpow', lambda a, b: operator.pow(b, a)),
543 ('rshift', operator.rshift),
544 ('rrshift', lambda a, b: operator.rshift(b, a)),
545 ('sub', operator.sub),
546 ('rsub', lambda a, b: operator.sub(b, a)),
547 ('truediv', operator.truediv),
548 ('rtruediv', lambda a, b: operator.truediv(b, a)),
549 ('xor', operator.xor),
550 ('rxor', lambda a, b: operator.xor(b, a)),
551 )
552
553
554 _IBINOPS = (
555 ('iadd', operator.iadd),
556 ('iand', operator.iand),
557 ('ifloordiv', operator.ifloordiv),
558 ('ilshift', operator.ilshift),
559 ('imod', operator.imod),
560 ('imul', operator.imul),
561 ('ior', operator.ior),
562 ('ipow', operator.ipow),
563 ('irshift', operator.irshift),
564 ('isub', operator.isub),
565 ('itruediv', operator.itruediv),
566 ('ixor', operator.ixor),
567 )
568
569
570 _UNARYOPS = (
571 ('neg', operator.neg),
572 ('pos', operator.pos),
573 ('abs', operator.abs),
574 ('invert', operator.invert),
575 ('round', round),
576 ('round_0', partial(round, ndigits=0)),
577 ('round_1', partial(round, ndigits=1)),
578 ('round_2', partial(round, ndigits=2)),
579 ('round_3', partial(round, ndigits=3)),
580 ('ceil', math.ceil),
581 ('floor', math.floor),
582 ('trunc', math.trunc),
583 )
584
585
586 def _inject_numeric_testing_methods(cls):
587 def test_binop_name(suffix):
588 return 'test_binop_{}_{}'.format(name, suffix)
589
590 def test_ibinop_name(suffix):
591 return 'test_ibinop_{}_{}'.format(name, suffix)
592
593 def test_unaryop_name(suffix):
594 return 'test_unaryop_{}_{}'.format(name, suffix)
595
596 # inject testing methods for each binary operation
597 for name, binop in _BINOPS:
598 setattr(cls, test_binop_name('invalid_unknown'), partialmethod(_TestNumericField._test_binop_invalid_unknown, op=binop))
599 setattr(cls, test_binop_name('invalid_none'), partialmethod(_TestNumericField._test_binop_invalid_none, op=binop))
600 setattr(cls, test_binop_name('type_true'), partialmethod(_TestNumericField._test_binop_type_true, op=binop))
601 setattr(cls, test_binop_name('type_pos_int'), partialmethod(_TestNumericField._test_binop_type_pos_int, op=binop))
602 setattr(cls, test_binop_name('type_pos_vint'), partialmethod(_TestNumericField._test_binop_type_pos_vint, op=binop))
603 setattr(cls, test_binop_name('value_true'), partialmethod(_TestNumericField._test_binop_value_true, op=binop))
604 setattr(cls, test_binop_name('value_pos_int'), partialmethod(_TestNumericField._test_binop_value_pos_int, op=binop))
605 setattr(cls, test_binop_name('value_pos_vint'), partialmethod(_TestNumericField._test_binop_value_pos_vint, op=binop))
606 setattr(cls, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_true, op=binop))
607 setattr(cls, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_int, op=binop))
608 setattr(cls, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_vint, op=binop))
609 setattr(cls, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericField._test_binop_lhs_value_same_true, op=binop))
610 setattr(cls, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_int, op=binop))
611 setattr(cls, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_vint, op=binop))
612 setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericField._test_binop_type_neg_int, op=binop))
613 setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericField._test_binop_type_neg_vint, op=binop))
614 setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericField._test_binop_value_neg_int, op=binop))
615 setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericField._test_binop_value_neg_vint, op=binop))
616 setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_int, op=binop))
617 setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_vint, op=binop))
618 setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_int, op=binop))
619 setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_vint, op=binop))
620 setattr(cls, test_binop_name('type_false'), partialmethod(_TestNumericField._test_binop_type_false, op=binop))
621 setattr(cls, test_binop_name('type_zero_int'), partialmethod(_TestNumericField._test_binop_type_zero_int, op=binop))
622 setattr(cls, test_binop_name('type_zero_vint'), partialmethod(_TestNumericField._test_binop_type_zero_vint, op=binop))
623 setattr(cls, test_binop_name('value_false'), partialmethod(_TestNumericField._test_binop_value_false, op=binop))
624 setattr(cls, test_binop_name('value_zero_int'), partialmethod(_TestNumericField._test_binop_value_zero_int, op=binop))
625 setattr(cls, test_binop_name('value_zero_vint'), partialmethod(_TestNumericField._test_binop_value_zero_vint, op=binop))
626 setattr(cls, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_false, op=binop))
627 setattr(cls, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_int, op=binop))
628 setattr(cls, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_vint, op=binop))
629 setattr(cls, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericField._test_binop_lhs_value_same_false, op=binop))
630 setattr(cls, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_int, op=binop))
631 setattr(cls, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_vint, op=binop))
632 setattr(cls, test_binop_name('type_pos_float'), partialmethod(_TestNumericField._test_binop_type_pos_float, op=binop))
633 setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericField._test_binop_type_neg_float, op=binop))
634 setattr(cls, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericField._test_binop_type_pos_vfloat, op=binop))
635 setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericField._test_binop_type_neg_vfloat, op=binop))
636 setattr(cls, test_binop_name('value_pos_float'), partialmethod(_TestNumericField._test_binop_value_pos_float, op=binop))
637 setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericField._test_binop_value_neg_float, op=binop))
638 setattr(cls, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericField._test_binop_value_pos_vfloat, op=binop))
639 setattr(cls, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericField._test_binop_value_neg_vfloat, op=binop))
640 setattr(cls, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_float, op=binop))
641 setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_float, op=binop))
642 setattr(cls, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_pos_vfloat, op=binop))
643 setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_neg_vfloat, op=binop))
644 setattr(cls, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_float, op=binop))
645 setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_float, op=binop))
646 setattr(cls, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_pos_vfloat, op=binop))
647 setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_neg_vfloat, op=binop))
648 setattr(cls, test_binop_name('type_zero_float'), partialmethod(_TestNumericField._test_binop_type_zero_float, op=binop))
649 setattr(cls, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericField._test_binop_type_zero_vfloat, op=binop))
650 setattr(cls, test_binop_name('value_zero_float'), partialmethod(_TestNumericField._test_binop_value_zero_float, op=binop))
651 setattr(cls, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericField._test_binop_value_zero_vfloat, op=binop))
652 setattr(cls, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_float, op=binop))
653 setattr(cls, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_addr_same_zero_vfloat, op=binop))
654 setattr(cls, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_float, op=binop))
655 setattr(cls, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericField._test_binop_lhs_value_same_zero_vfloat, op=binop))
656
657 # inject testing methods for each unary operation
658 for name, unaryop in _UNARYOPS:
659 setattr(cls, test_unaryop_name('type'), partialmethod(_TestNumericField._test_unaryop_type, op=unaryop))
660 setattr(cls, test_unaryop_name('value'), partialmethod(_TestNumericField._test_unaryop_value, op=unaryop))
661 setattr(cls, test_unaryop_name('addr_same'), partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop))
662 setattr(cls, test_unaryop_name('value_same'), partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop))
663
664 # inject testing methods for each inplace binary operation
665 for name, ibinop in _IBINOPS:
666 setattr(cls, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericField._test_ibinop_invalid_unknown, op=ibinop))
667 setattr(cls, test_ibinop_name('invalid_none'), partialmethod(_TestNumericField._test_ibinop_invalid_none, op=ibinop))
668 setattr(cls, test_ibinop_name('type_true'), partialmethod(_TestNumericField._test_ibinop_type_true, op=ibinop))
669 setattr(cls, test_ibinop_name('value_true'), partialmethod(_TestNumericField._test_ibinop_value_true, op=ibinop))
670 setattr(cls, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericField._test_ibinop_type_pos_int, op=ibinop))
671 setattr(cls, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericField._test_ibinop_type_pos_vint, op=ibinop))
672 setattr(cls, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericField._test_ibinop_value_pos_int, op=ibinop))
673 setattr(cls, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericField._test_ibinop_value_pos_vint, op=ibinop))
674 setattr(cls, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericField._test_ibinop_type_neg_int, op=ibinop))
675 setattr(cls, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericField._test_ibinop_type_neg_vint, op=ibinop))
676 setattr(cls, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericField._test_ibinop_value_neg_int, op=ibinop))
677 setattr(cls, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericField._test_ibinop_value_neg_vint, op=ibinop))
678 setattr(cls, test_ibinop_name('type_false'), partialmethod(_TestNumericField._test_ibinop_type_false, op=ibinop))
679 setattr(cls, test_ibinop_name('value_false'), partialmethod(_TestNumericField._test_ibinop_value_false, op=ibinop))
680 setattr(cls, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericField._test_ibinop_type_zero_int, op=ibinop))
681 setattr(cls, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericField._test_ibinop_type_zero_vint, op=ibinop))
682 setattr(cls, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericField._test_ibinop_value_zero_int, op=ibinop))
683 setattr(cls, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericField._test_ibinop_value_zero_vint, op=ibinop))
684 setattr(cls, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericField._test_ibinop_type_pos_float, op=ibinop))
685 setattr(cls, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericField._test_ibinop_type_neg_float, op=ibinop))
686 setattr(cls, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_pos_vfloat, op=ibinop))
687 setattr(cls, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_neg_vfloat, op=ibinop))
688 setattr(cls, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericField._test_ibinop_value_pos_float, op=ibinop))
689 setattr(cls, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericField._test_ibinop_value_neg_float, op=ibinop))
690 setattr(cls, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_pos_vfloat, op=ibinop))
691 setattr(cls, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_neg_vfloat, op=ibinop))
692 setattr(cls, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericField._test_ibinop_type_zero_float, op=ibinop))
693 setattr(cls, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericField._test_ibinop_type_zero_vfloat, op=ibinop))
694 setattr(cls, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericField._test_ibinop_value_zero_float, op=ibinop))
695 setattr(cls, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericField._test_ibinop_value_zero_vfloat, op=ibinop))
696
697
698 class _TestIntegerFieldCommon(_TestNumericField):
699 def test_assign_true(self):
700 raw = True
701 self._def.value = raw
702 self.assertEqual(self._def, raw)
703 self.assertEqual(self._def.value, raw)
704
705 def test_assign_false(self):
706 raw = False
707 self._def.value = raw
708 self.assertEqual(self._def, raw)
709 self.assertEqual(self._def.value, raw)
710
711 def test_assign_pos_int(self):
712 raw = 477
713 self._def.value = raw
714 self.assertEqual(self._def, raw)
715 self.assertEqual(self._def.value, raw)
716
717 def test_assign_neg_int(self):
718 raw = -13
719 self._def.value = raw
720 self.assertEqual(self._def, raw)
721 self.assertEqual(self._def.value, raw)
722
723 def test_assign_int_field(self):
724 raw = 999
725 field = self._ft()
726 field.value = raw
727 self._def.value = field
728 self.assertEqual(self._def, raw)
729 self.assertEqual(self._def.value, raw)
730
731 def test_assign_float(self):
732 raw = 123.456
733 self._def.value = raw
734 self.assertEqual(self._def, int(raw))
735 self.assertEqual(self._def.value, int(raw))
736
737 def test_assign_invalid_type(self):
738 with self.assertRaises(TypeError):
739 self._def.value = 'yes'
740
741 def test_assign_uint(self):
742 ft = bt2.IntegerFieldType(size=32, is_signed=False)
743 field = ft()
744 raw = 1777
745 field.value = 1777
746 self.assertEqual(field, raw)
747 self.assertEqual(field.value, raw)
748
749 def test_assign_uint_invalid_neg(self):
750 ft = bt2.IntegerFieldType(size=32, is_signed=False)
751 field = ft()
752
753 with self.assertRaises(ValueError):
754 field.value = -23
755
756
757 _inject_numeric_testing_methods(_TestIntegerFieldCommon)
758
759
760 class IntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
761 def setUp(self):
762 self._ft = bt2.IntegerFieldType(25, is_signed=True)
763 self._field = self._ft()
764 self._def = self._ft()
765 self._def.value = 17
766 self._def_value = 17
767 self._def_new_value = -101
768
769 def tearDown(self):
770 del self._ft
771 del self._field
772 del self._def
773
774
775 class EnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
776 def setUp(self):
777 self._ft = bt2.EnumerationFieldType(size=32, is_signed=True)
778 self._ft.append_mapping('whole range', -(2 ** 31), (2 ** 31) - 1)
779 self._ft.append_mapping('something', 17)
780 self._ft.append_mapping('speaker', 12, 16)
781 self._ft.append_mapping('can', 18, 2540)
782 self._ft.append_mapping('zip', -45, 1001)
783 self._def = self._ft()
784 self._def.value = 17
785 self._def_value = 17
786 self._def_new_value = -101
787
788 def tearDown(self):
789 del self._ft
790 del self._def
791
792 def test_mappings(self):
793 mappings = (
794 ('whole range', -(2 ** 31), (2 ** 31) - 1),
795 ('something', 17, 17),
796 ('zip', -45, 1001),
797 )
798
799 total = 0
800 index_set = set()
801
802 for fm in self._def.mappings:
803 total += 1
804 for index, mapping in enumerate(mappings):
805 if fm.name == mapping[0] and fm.lower == mapping[1] and fm.upper == mapping[2]:
806 index_set.add(index)
807
808 self.assertEqual(total, 3)
809 self.assertTrue(0 in index_set and 1 in index_set and 2 in index_set)
810
811
812 class FloatingPointNumberFieldTestCase(_TestNumericField, unittest.TestCase):
813 def setUp(self):
814 self._ft = bt2.FloatingPointNumberFieldType()
815 self._field = self._ft()
816 self._def = self._ft()
817 self._def.value = 52.7
818 self._def_value = 52.7
819 self._def_new_value = -17.164857
820
821 def tearDown(self):
822 del self._ft
823 del self._field
824 del self._def
825
826 def _test_invalid_op(self, cb):
827 with self.assertRaises(TypeError):
828 cb()
829
830 def test_assign_true(self):
831 self._def.value = True
832 self.assertTrue(self._def)
833 self.assertTrue(self._def.value)
834
835 def test_assign_false(self):
836 self._def.value = False
837 self.assertFalse(self._def)
838 self.assertFalse(self._def.value)
839
840 def test_assign_pos_int(self):
841 raw = 477
842 self._def.value = raw
843 self.assertEqual(self._def, float(raw))
844 self.assertEqual(self._def.value, float(raw))
845
846 def test_assign_neg_int(self):
847 raw = -13
848 self._def.value = raw
849 self.assertEqual(self._def, float(raw))
850 self.assertEqual(self._def.value, float(raw))
851
852 def test_assign_int_field(self):
853 ft = bt2.IntegerFieldType(32)
854 field = ft()
855 raw = 999
856 field.value = raw
857 self._def.value = field
858 self.assertEqual(self._def, float(raw))
859 self.assertEqual(self._def.value, float(raw))
860
861 def test_assign_float(self):
862 raw = -19.23
863 self._def.value = raw
864 self.assertEqual(self._def, raw)
865 self.assertEqual(self._def.value, raw)
866
867 def test_assign_float_field(self):
868 ft = bt2.FloatingPointNumberFieldType(32)
869 field = ft()
870 raw = 101.32
871 field.value = raw
872 self._def.value = field
873 self.assertEqual(self._def, raw)
874 self.assertEqual(self._def.value, raw)
875
876 def test_assign_invalid_type(self):
877 with self.assertRaises(TypeError):
878 self._def.value = 'yes'
879
880 def test_invalid_lshift(self):
881 self._test_invalid_op(lambda: self._def << 23)
882
883 def test_invalid_rshift(self):
884 self._test_invalid_op(lambda: self._def >> 23)
885
886 def test_invalid_and(self):
887 self._test_invalid_op(lambda: self._def & 23)
888
889 def test_invalid_or(self):
890 self._test_invalid_op(lambda: self._def | 23)
891
892 def test_invalid_xor(self):
893 self._test_invalid_op(lambda: self._def ^ 23)
894
895 def test_invalid_invert(self):
896 self._test_invalid_op(lambda: ~self._def)
897
898
899 _inject_numeric_testing_methods(FloatingPointNumberFieldTestCase)
900
901
902 class StringFieldTestCase(_TestCopySimple, unittest.TestCase):
903 def setUp(self):
904 self._ft = bt2.StringFieldType()
905 self._def_value = 'Hello, World!'
906 self._def = self._ft()
907 self._def.value = self._def_value
908 self._def_new_value = 'Yes!'
909
910 def tearDown(self):
911 del self._ft
912 del self._def
913
914 def test_assign_int(self):
915 with self.assertRaises(TypeError):
916 self._def.value = 283
917
918 def test_assign_str(self):
919 raw = 'zorg'
920 self._def = raw
921 self.assertEqual(self._def, raw)
922
923 def test_assign_string_field(self):
924 ft = bt2.StringFieldType()
925 field = ft()
926 raw = 'zorg'
927 field.value = raw
928 self.assertEqual(field, raw)
929
930 def test_eq(self):
931 self.assertEqual(self._def, self._def_value)
932
933 def test_eq(self):
934 self.assertNotEqual(self._def, 23)
935
936 def test_lt_vstring(self):
937 s1 = self._ft()
938 s1.value = 'allo'
939 s2 = self._ft()
940 s2.value = 'bateau'
941 self.assertLess(s1, s2)
942
943 def test_lt_string(self):
944 s1 = self._ft()
945 s1.value = 'allo'
946 self.assertLess(s1, 'bateau')
947
948 def test_le_vstring(self):
949 s1 = self._ft()
950 s1.value = 'allo'
951 s2 = self._ft()
952 s2.value = 'bateau'
953 self.assertLessEqual(s1, s2)
954
955 def test_le_string(self):
956 s1 = self._ft()
957 s1.value = 'allo'
958 self.assertLessEqual(s1, 'bateau')
959
960 def test_gt_vstring(self):
961 s1 = self._ft()
962 s1.value = 'allo'
963 s2 = self._ft()
964 s2.value = 'bateau'
965 self.assertGreater(s2, s1)
966
967 def test_gt_string(self):
968 s1 = self._ft()
969 s1.value = 'allo'
970 self.assertGreater('bateau', s1)
971
972 def test_ge_vstring(self):
973 s1 = self._ft()
974 s1.value = 'allo'
975 s2 = self._ft()
976 s2.value = 'bateau'
977 self.assertGreaterEqual(s2, s1)
978
979 def test_ge_string(self):
980 s1 = self._ft()
981 s1.value = 'allo'
982 self.assertGreaterEqual('bateau', s1)
983
984 def test_bool_op(self):
985 self.assertEqual(bool(self._def), bool(self._def_value))
986
987 def test_str_op(self):
988 self.assertEqual(str(self._def), str(self._def_value))
989
990 def test_len(self):
991 self.assertEqual(len(self._def), len(self._def_value))
992
993 def test_getitem(self):
994 self.assertEqual(self._def[5], self._def_value[5])
995
996 def test_append_str(self):
997 to_append = 'meow meow meow'
998 self._def += to_append
999 self._def_value += to_append
1000 self.assertEqual(self._def, self._def_value)
1001
1002 def test_append_string_field(self):
1003 ft = bt2.StringFieldType()
1004 field = ft()
1005 to_append = 'meow meow meow'
1006 field.value = to_append
1007 self._def += field
1008 self._def_value += to_append
1009 self.assertEqual(self._def, self._def_value)
1010
1011
1012 class _TestArraySequenceFieldCommon(_TestCopySimple):
1013 def _modify_def(self):
1014 self._def[2] = 23
1015
1016 def test_bool_op_true(self):
1017 self.assertTrue(self._def)
1018
1019 def test_len(self):
1020 self.assertEqual(len(self._def), 3)
1021
1022 def test_getitem(self):
1023 field = self._def[1]
1024 self.assertIs(type(field), bt2.fields._IntegerField)
1025 self.assertEqual(field, 1847)
1026
1027 def test_eq(self):
1028 ft = bt2.ArrayFieldType(self._elem_ft, 3)
1029 field = ft()
1030 field[0] = 45
1031 field[1] = 1847
1032 field[2] = 1948754
1033 self.assertEqual(self._def, field)
1034
1035 def test_eq_invalid_type(self):
1036 self.assertNotEqual(self._def, 23)
1037
1038 def test_eq_diff_len(self):
1039 ft = bt2.ArrayFieldType(self._elem_ft, 2)
1040 field = ft()
1041 field[0] = 45
1042 field[1] = 1847
1043 self.assertNotEqual(self._def, field)
1044
1045 def test_eq_diff_content_same_len(self):
1046 ft = bt2.ArrayFieldType(self._elem_ft, 3)
1047 field = ft()
1048 field[0] = 45
1049 field[1] = 1846
1050 field[2] = 1948754
1051 self.assertNotEqual(self._def, field)
1052
1053 def test_setitem(self):
1054 self._def[2] = 24
1055 self.assertEqual(self._def[2], 24)
1056
1057 def test_setitem_int_field(self):
1058 int_field = self._elem_ft()
1059 int_field.value = 19487
1060 self._def[1] = int_field
1061 self.assertEqual(self._def[1], 19487)
1062
1063 def test_setitem_non_basic_field(self):
1064 elem_ft = bt2.StructureFieldType()
1065 array_ft = bt2.ArrayFieldType(elem_ft, 3)
1066 elem_field = elem_ft()
1067 array_field = array_ft()
1068
1069 with self.assertRaises(TypeError):
1070 array_field[1] = 23
1071
1072 def test_setitem_none(self):
1073 with self.assertRaises(TypeError):
1074 self._def[1] = None
1075
1076 def test_setitem_index_wrong_type(self):
1077 with self.assertRaises(TypeError):
1078 self._def['yes'] = 23
1079
1080 def test_setitem_index_neg(self):
1081 with self.assertRaises(IndexError):
1082 self._def[-2] = 23
1083
1084 def test_setitem_index_out_of_range(self):
1085 with self.assertRaises(IndexError):
1086 self._def[len(self._def)] = 134679
1087
1088 def test_iter(self):
1089 for field, value in zip(self._def, (45, 1847, 1948754)):
1090 self.assertEqual(field, value)
1091
1092 def test_value_int_field(self):
1093 values = [45646, 145, 12145]
1094 self._def.value = values
1095 self.assertEqual(values, self._def)
1096
1097 def test_value_unset(self):
1098 values = [45646, None, 12145]
1099 self._def.value = values
1100 self.assertFalse(self._def[1].is_set)
1101
1102 def test_value_rollback(self):
1103 values = [45, 1847, 1948754]
1104 # value is out of range, should not affect those we set previously
1105 with self.assertRaises(bt2.Error):
1106 self._def[2].value = 2**60
1107 self.assertEqual(values, self._def)
1108
1109 def test_value_check_sequence(self):
1110 values = 42
1111 with self.assertRaises(TypeError):
1112 self._def.value = values
1113
1114 def test_value_wrong_type_in_sequence(self):
1115 values = [32, 'hello', 11]
1116 with self.assertRaises(TypeError):
1117 self._def.value = values
1118
1119 def test_value_complex_type(self):
1120 struct_ft = bt2.StructureFieldType()
1121 int_ft = bt2.IntegerFieldType(32)
1122 str_ft = bt2.StringFieldType()
1123 struct_ft.append_field(field_type=int_ft, name='an_int')
1124 struct_ft.append_field(field_type=str_ft, name='a_string')
1125 struct_ft.append_field(field_type=int_ft, name='another_int')
1126 array_ft = bt2.ArrayFieldType(struct_ft, 3)
1127 values = [
1128 {
1129 'an_int': 42,
1130 'a_string': 'hello',
1131 'another_int': 66
1132 },
1133 {
1134 'an_int': 1,
1135 'a_string': 'goodbye',
1136 'another_int': 488
1137 },
1138 {
1139 'an_int': 156,
1140 'a_string': 'or not',
1141 'another_int': 4648
1142 },
1143 ]
1144
1145 array = array_ft()
1146 array.value = values
1147 self.assertEqual(values, array)
1148 values[0]['an_int'] = 'a string'
1149 with self.assertRaises(TypeError):
1150 array.value = values
1151
1152 class ArrayFieldTestCase(_TestArraySequenceFieldCommon, unittest.TestCase):
1153 def setUp(self):
1154 self._elem_ft = bt2.IntegerFieldType(32)
1155 self._ft = bt2.ArrayFieldType(self._elem_ft, 3)
1156 self._def = self._ft()
1157 self._def[0] = 45
1158 self._def[1] = 1847
1159 self._def[2] = 1948754
1160 self._def_value = [45, 1847, 1948754]
1161
1162 def tearDown(self):
1163 del self._elem_ft
1164 del self._ft
1165 del self._def
1166
1167 def test_value_wrong_len(self):
1168 values = [45, 1847]
1169 with self.assertRaises(ValueError):
1170 self._def.value = values
1171
1172
1173 class SequenceFieldTestCase(_TestArraySequenceFieldCommon, unittest.TestCase):
1174 def setUp(self):
1175 self._elem_ft = bt2.IntegerFieldType(32)
1176 self._ft = bt2.SequenceFieldType(self._elem_ft, 'the.length')
1177 self._def = self._ft()
1178 self._length_field = self._elem_ft(3)
1179 self._def.length_field = self._length_field
1180 self._def[0] = 45
1181 self._def[1] = 1847
1182 self._def[2] = 1948754
1183 self._def_value = [45, 1847, 1948754]
1184
1185 def tearDown(self):
1186 del self._elem_ft
1187 del self._ft
1188 del self._def
1189 del self._length_field
1190
1191 def test_value_resize(self):
1192 new_values = [1, 2, 3, 4]
1193 self._def.value = new_values
1194 self.assertCountEqual(self._def, new_values)
1195
1196 def test_value_resize_rollback(self):
1197 with self.assertRaises(TypeError):
1198 self._def.value = [1, 2, 3, 'unexpected string']
1199 self.assertEqual(self._def, self._def_value)
1200
1201 self._def.reset()
1202 with self.assertRaises(TypeError):
1203 self._def.value = [1, 2, 3, 'unexpected string']
1204 self.assertFalse(self._def.is_set)
1205
1206
1207 class StructureFieldTestCase(_TestCopySimple, unittest.TestCase):
1208 def setUp(self):
1209 self._ft0 = bt2.IntegerFieldType(32, is_signed=True)
1210 self._ft1 = bt2.StringFieldType()
1211 self._ft2 = bt2.FloatingPointNumberFieldType()
1212 self._ft3 = bt2.IntegerFieldType(17)
1213 self._ft = bt2.StructureFieldType()
1214 self._ft.append_field('A', self._ft0)
1215 self._ft.append_field('B', self._ft1)
1216 self._ft.append_field('C', self._ft2)
1217 self._ft.append_field('D', self._ft3)
1218 self._def = self._ft()
1219 self._def['A'] = -1872
1220 self._def['B'] = 'salut'
1221 self._def['C'] = 17.5
1222 self._def['D'] = 16497
1223
1224 def tearDown(self):
1225 del self._ft0
1226 del self._ft1
1227 del self._ft2
1228 del self._ft3
1229 del self._ft
1230 del self._def
1231
1232 def _modify_def(self):
1233 self._def['B'] = 'hola'
1234
1235 def test_bool_op_true(self):
1236 self.assertTrue(self._def)
1237
1238 def test_bool_op_false(self):
1239 ft = bt2.StructureFieldType()
1240 field = ft()
1241 self.assertFalse(field)
1242
1243 def test_len(self):
1244 self.assertEqual(len(self._def), 4)
1245
1246 def test_getitem(self):
1247 field = self._def['A']
1248 self.assertIs(type(field), bt2.fields._IntegerField)
1249 self.assertEqual(field, -1872)
1250
1251 def test_at_index_out_of_bounds_after(self):
1252 with self.assertRaises(IndexError):
1253 self._def.at_index(len(self._ft))
1254
1255 def test_eq(self):
1256 ft = bt2.StructureFieldType()
1257 ft.append_field('A', self._ft0)
1258 ft.append_field('B', self._ft1)
1259 ft.append_field('C', self._ft2)
1260 ft.append_field('D', self._ft3)
1261 field = ft()
1262 field['A'] = -1872
1263 field['B'] = 'salut'
1264 field['C'] = 17.5
1265 field['D'] = 16497
1266 self.assertEqual(self._def, field)
1267
1268 def test_eq_invalid_type(self):
1269 self.assertNotEqual(self._def, 23)
1270
1271 def test_eq_diff_len(self):
1272 ft = bt2.StructureFieldType()
1273 ft.append_field('A', self._ft0)
1274 ft.append_field('B', self._ft1)
1275 ft.append_field('C', self._ft2)
1276 field = ft()
1277 field['A'] = -1872
1278 field['B'] = 'salut'
1279 field['C'] = 17.5
1280 self.assertNotEqual(self._def, field)
1281
1282 def test_eq_diff_content_same_len(self):
1283 ft = bt2.StructureFieldType()
1284 ft.append_field('A', self._ft0)
1285 ft.append_field('B', self._ft1)
1286 ft.append_field('C', self._ft2)
1287 ft.append_field('D', self._ft3)
1288 field = ft()
1289 field['A'] = -1872
1290 field['B'] = 'salut'
1291 field['C'] = 17.4
1292 field['D'] = 16497
1293 self.assertNotEqual(self._def, field)
1294
1295 def test_eq_same_content_diff_keys(self):
1296 ft = bt2.StructureFieldType()
1297 ft.append_field('A', self._ft0)
1298 ft.append_field('B', self._ft1)
1299 ft.append_field('E', self._ft2)
1300 ft.append_field('D', self._ft3)
1301 field = ft()
1302 field['A'] = -1872
1303 field['B'] = 'salut'
1304 field['E'] = 17.4
1305 field['D'] = 16497
1306 self.assertNotEqual(self._def, field)
1307
1308 def test_setitem(self):
1309 self._def['C'] = -18.47
1310 self.assertEqual(self._def['C'], -18.47)
1311
1312 def test_setitem_int_field(self):
1313 int_ft = bt2.IntegerFieldType(16)
1314 int_field = int_ft()
1315 int_field.value = 19487
1316 self._def['D'] = int_field
1317 self.assertEqual(self._def['D'], 19487)
1318
1319 def test_setitem_non_basic_field(self):
1320 elem_ft = bt2.StructureFieldType()
1321 elem_field = elem_ft()
1322 struct_ft = bt2.StructureFieldType()
1323 struct_ft.append_field('A', elem_ft)
1324 struct_field = struct_ft()
1325
1326 with self.assertRaises(TypeError):
1327 struct_field['A'] = 23
1328
1329 def test_setitem_none(self):
1330 with self.assertRaises(TypeError):
1331 self._def['C'] = None
1332
1333 def test_setitem_key_wrong_type(self):
1334 with self.assertRaises(TypeError):
1335 self._def[3] = 23
1336
1337 def test_setitem_wrong_key(self):
1338 with self.assertRaises(KeyError):
1339 self._def['hi'] = 134679
1340
1341 def test_at_index(self):
1342 self.assertEqual(self._def.at_index(1), 'salut')
1343
1344 def test_iter(self):
1345 orig_values = {
1346 'A': -1872,
1347 'B': 'salut',
1348 'C': 17.5,
1349 'D': 16497,
1350 }
1351
1352 for vkey, vval in self._def.items():
1353 val = orig_values[vkey]
1354 self.assertEqual(vval, val)
1355
1356 def test_value(self):
1357 orig_values = {
1358 'A': -1872,
1359 'B': 'salut',
1360 'C': 17.5,
1361 'D': 16497,
1362 }
1363 self.assertEqual(self._def, orig_values)
1364
1365 def test_set_value(self):
1366 int_ft = bt2.IntegerFieldType(32)
1367 str_ft = bt2.StringFieldType()
1368 struct_ft = bt2.StructureFieldType()
1369 struct_ft.append_field(field_type=int_ft, name='an_int')
1370 struct_ft.append_field(field_type=str_ft, name='a_string')
1371 struct_ft.append_field(field_type=int_ft, name='another_int')
1372 values = {
1373 'an_int': 42,
1374 'a_string': 'hello',
1375 'another_int': 66
1376 }
1377
1378 struct = struct_ft()
1379 struct.value = values
1380 self.assertEqual(values, struct)
1381
1382 bad_type_values = copy.deepcopy(values)
1383 bad_type_values['an_int'] = 'a string'
1384 with self.assertRaises(TypeError):
1385 struct.value = bad_type_values
1386
1387 unknown_key_values = copy.deepcopy(values)
1388 unknown_key_values['unknown_key'] = 16546
1389 with self.assertRaises(KeyError):
1390 struct.value = unknown_key_values
1391
1392 def test_value_rollback(self):
1393 int_ft = bt2.IntegerFieldType(32)
1394 str_ft = bt2.StringFieldType()
1395 struct_ft = bt2.StructureFieldType()
1396 struct_ft.append_field(field_type=int_ft, name='an_int')
1397 struct_ft.append_field(field_type=str_ft, name='a_string')
1398 struct_ft.append_field(field_type=int_ft, name='another_int')
1399 values = {
1400 'an_int': 42,
1401 'a_string': 'hello',
1402 'another_int': 66
1403 }
1404
1405
1406
1407 class VariantFieldTestCase(_TestCopySimple, unittest.TestCase):
1408 def setUp(self):
1409 self._tag_ft = bt2.EnumerationFieldType(size=32)
1410 self._tag_ft.append_mapping('corner', 23)
1411 self._tag_ft.append_mapping('zoom', 17, 20)
1412 self._tag_ft.append_mapping('mellotron', 1001)
1413 self._tag_ft.append_mapping('giorgio', 2000, 3000)
1414 self._ft0 = bt2.IntegerFieldType(32, is_signed=True)
1415 self._ft1 = bt2.StringFieldType()
1416 self._ft2 = bt2.FloatingPointNumberFieldType()
1417 self._ft3 = bt2.IntegerFieldType(17)
1418 self._ft = bt2.VariantFieldType('salut', self._tag_ft)
1419 self._ft.append_field('corner', self._ft0)
1420 self._ft.append_field('zoom', self._ft1)
1421 self._ft.append_field('mellotron', self._ft2)
1422 self._ft.append_field('giorgio', self._ft3)
1423 self._def = self._ft()
1424
1425 def tearDown(self):
1426 del self._tag_ft
1427 del self._ft0
1428 del self._ft1
1429 del self._ft2
1430 del self._ft3
1431 del self._ft
1432 del self._def
1433
1434 def test_bool_op_true(self):
1435 tag_field = self._tag_ft(1001)
1436 self._def.field(tag_field).value = -17.34
1437 self.assertTrue(self._def)
1438
1439 def test_bool_op_false(self):
1440 self.assertFalse(self._def)
1441
1442 def test_tag_field_none(self):
1443 self.assertIsNone(self._def.tag_field)
1444
1445 def test_tag_field(self):
1446 tag_field = self._tag_ft(2800)
1447 self._def.field(tag_field).value = 1847
1448 self.assertEqual(self._def.tag_field, tag_field)
1449 self.assertEqual(self._def.tag_field.addr, tag_field.addr)
1450
1451 def test_selected_field_none(self):
1452 self.assertIsNone(self._def.selected_field)
1453
1454 def test_selected_field(self):
1455 var_field1 = self._ft()
1456 tag_field1 = self._tag_ft(1001)
1457 var_field1.field(tag_field1).value = -17.34
1458 self.assertEqual(var_field1.field(), -17.34)
1459 self.assertEqual(var_field1.selected_field, -17.34)
1460 var_field2 = self._ft()
1461 tag_field2 = self._tag_ft(2500)
1462 var_field2.field(tag_field2).value = 1921
1463 self.assertEqual(var_field2.field(), 1921)
1464 self.assertEqual(var_field2.selected_field, 1921)
1465
1466 def test_eq(self):
1467 tag_ft = bt2.EnumerationFieldType(size=32)
1468 tag_ft.append_mapping('corner', 23)
1469 tag_ft.append_mapping('zoom', 17, 20)
1470 tag_ft.append_mapping('mellotron', 1001)
1471 tag_ft.append_mapping('giorgio', 2000, 3000)
1472 ft0 = bt2.IntegerFieldType(32, is_signed=True)
1473 ft1 = bt2.StringFieldType()
1474 ft2 = bt2.FloatingPointNumberFieldType()
1475 ft3 = bt2.IntegerFieldType(17)
1476 ft = bt2.VariantFieldType('salut', tag_ft)
1477 ft.append_field('corner', ft0)
1478 ft.append_field('zoom', ft1)
1479 ft.append_field('mellotron', ft2)
1480 ft.append_field('giorgio', ft3)
1481 field = ft()
1482 field_tag = tag_ft(23)
1483 def_tag = self._tag_ft(23)
1484 field.field(field_tag).value = 1774
1485 self._def.field(def_tag).value = 1774
1486 self.assertEqual(self._def, field)
1487
1488 def test_eq_invalid_type(self):
1489 self.assertNotEqual(self._def, 23)
This page took 0.13041 seconds and 4 git commands to generate.