Use Black stable to format python code
[babeltrace.git] / tests / bindings / python / bt2 / test_field.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # Copyright (C) 2019 EfficiOS Inc.
4 #
5
6 from functools import partial, partialmethod
7 import operator
8 import unittest
9 import math
10 import copy
11 import itertools
12 import collections
13 import bt2
14 from utils import get_default_trace_class, create_const_field
15
16
17 _COMP_BINOPS = (operator.eq, operator.ne)
18
19
20 # Create and return a stream with the field classes part of its stream packet
21 # context.
22 #
23 # The stream is part of a dummy trace created from trace class `tc`.
24
25
26 def _create_stream(tc, ctx_field_classes):
27 packet_context_fc = tc.create_structure_field_class()
28 for name, fc in ctx_field_classes:
29 packet_context_fc.append_member(name, fc)
30
31 trace = tc()
32 stream_class = tc.create_stream_class(
33 packet_context_field_class=packet_context_fc, supports_packets=True
34 )
35
36 stream = trace.create_stream(stream_class)
37 return stream
38
39
40 # Create a field of the given field class.
41 #
42 # The field is part of a dummy stream, itself part of a dummy trace created
43 # from trace class `tc`.
44
45
46 def _create_field(tc, field_class):
47 field_name = 'field'
48 stream = _create_stream(tc, [(field_name, field_class)])
49 packet = stream.create_packet()
50 return packet.context_field[field_name]
51
52
53 # Create a field of type string.
54 #
55 # The field is part of a dummy stream, itself part of a dummy trace created
56 # from trace class `tc`. It is made out of a dummy string field class.
57
58
59 def _create_string_field(tc):
60 field_name = 'string_field'
61 stream = _create_stream(tc, [(field_name, tc.create_string_field_class())])
62 packet = stream.create_packet()
63 return packet.context_field[field_name]
64
65
66 # Create a field of type static array of ints.
67 #
68 # The field is part of a dummy stream, itself part of a dummy trace created
69 # from trace class `tc`. It is made out of a dummy static array field class,
70 # with a dummy integer field class as element class.
71
72
73 def _create_int_array_field(tc, length):
74 elem_fc = tc.create_signed_integer_field_class(32)
75 fc = tc.create_static_array_field_class(elem_fc, length)
76 field_name = 'int_array'
77 stream = _create_stream(tc, [(field_name, fc)])
78 packet = stream.create_packet()
79 return packet.context_field[field_name]
80
81
82 # Create a field of type dynamic array of ints.
83 #
84 # The field is part of a dummy stream, itself part of a dummy trace created
85 # from trace class `tc`. It is made out of a dummy static array field class,
86 # with a dummy integer field class as element and length classes.
87
88
89 def _create_dynamic_array(tc):
90 elem_fc = tc.create_signed_integer_field_class(32)
91 len_fc = tc.create_signed_integer_field_class(32)
92 fc = tc.create_dynamic_array_field_class(elem_fc)
93 field_name = 'int_dyn_array'
94 stream = _create_stream(tc, [('thelength', len_fc), (field_name, fc)])
95 packet = stream.create_packet()
96 packet.context_field[field_name].length = 3
97 return packet.context_field[field_name]
98
99
100 # Create a field of type array of (empty) structures.
101 #
102 # The field is part of a dummy stream, itself part of a dummy trace created
103 # from trace class `tc`. It is made out of a dummy static array field class,
104 # with a dummy struct field class as element class.
105
106
107 def _create_struct_array_field(tc, length):
108 elem_fc = tc.create_structure_field_class()
109 fc = tc.create_static_array_field_class(elem_fc, length)
110 field_name = 'struct_array'
111 stream = _create_stream(tc, [(field_name, fc)])
112 packet = stream.create_packet()
113 return packet.context_field[field_name]
114
115
116 class BitArrayFieldTestCase(unittest.TestCase):
117 def _create_field(self):
118 return _create_field(self._tc, self._tc.create_bit_array_field_class(24))
119
120 def setUp(self):
121 self._tc = get_default_trace_class()
122 self._def_value = 15497
123 self._def = self._create_field()
124 self._def.value_as_integer = self._def_value
125 self._def_new_value = 101542
126
127 def test_assign_invalid_type(self):
128 with self.assertRaises(TypeError):
129 self._def.value_as_integer = 'onze'
130
131 def test_assign(self):
132 self._def.value_as_integer = 199
133 self.assertEqual(self._def.value_as_integer, 199)
134
135 def test_assign_masked(self):
136 self._def.value_as_integer = 0xE1549BB
137 self.assertEqual(self._def.value_as_integer, 0xE1549BB & ((1 << 24) - 1))
138
139 def test_eq(self):
140 other = self._create_field()
141 other.value_as_integer = self._def_value
142 self.assertEqual(self._def, other)
143
144 def test_ne_same_type(self):
145 other = self._create_field()
146 other.value_as_integer = self._def_value - 1
147 self.assertNotEqual(self._def, other)
148
149 def test_ne_diff_type(self):
150 self.assertNotEqual(self._def, self._def_value)
151
152 def test_len(self):
153 self.assertEqual(len(self._def), 24)
154
155 def test_str(self):
156 self.assertEqual(str(self._def), str(self._def_value))
157
158 def test_repr(self):
159 self.assertEqual(repr(self._def), repr(self._def_value))
160
161
162 # Base class for numeric field test cases.
163 #
164 # To be compatible with this base class, a derived class must, in its
165 # setUp() method:
166 #
167 # * Set `self._def` to a field object with an arbitrary value.
168 # * Set `self._def_value` to the equivalent value of `self._def`.
169 class _TestNumericField:
170 # Tries the binary operation `op`:
171 #
172 # 1. Between `self._def`, which is a field object, and `rhs`.
173 # 2. Between `self._def_value`, which is the raw value of
174 # `self._def`, and `rhs`.
175 #
176 # Returns the results of 1. and 2.
177 #
178 # If there's an exception while performing 1. or 2., asserts that
179 # both operations raised exceptions, that both exceptions have the
180 # same type, and returns `None` for both results.
181 def _binop(self, op, rhs):
182 type_rexc = None
183 type_rvexc = None
184 comp_value = rhs
185
186 # try with field object
187 try:
188 r = op(self._def, rhs)
189 except Exception as e:
190 type_rexc = type(e)
191
192 # try with value
193 try:
194 rv = op(self._def_value, comp_value)
195 except Exception as e:
196 type_rvexc = type(e)
197
198 if type_rexc is not None or type_rvexc is not None:
199 # at least one of the operations raised an exception: in
200 # this case both operations should have raised the same
201 # type of exception (division by zero, bit shift with a
202 # floating point number operand, etc.)
203 self.assertIs(type_rexc, type_rvexc)
204 return None, None
205
206 return r, rv
207
208 # Tries the unary operation `op`:
209 #
210 # 1. On `self._def`, which is a field object.
211 # 2. On `self._def_value`, which is the value of `self._def`.
212 #
213 # Returns the results of 1. and 2.
214 #
215 # If there's an exception while performing 1. or 2., asserts that
216 # both operations raised exceptions, that both exceptions have the
217 # same type, and returns `None` for both results.
218 def _unaryop(self, op):
219 type_rexc = None
220 type_rvexc = None
221
222 # try with field object
223 try:
224 r = op(self._def)
225 except Exception as e:
226 type_rexc = type(e)
227
228 # try with value
229 try:
230 rv = op(self._def_value)
231 except Exception as e:
232 type_rvexc = type(e)
233
234 if type_rexc is not None or type_rvexc is not None:
235 # at least one of the operations raised an exception: in
236 # this case both operations should have raised the same
237 # type of exception (division by zero, bit shift with a
238 # floating point number operand, etc.)
239 self.assertIs(type_rexc, type_rvexc)
240 return None, None
241
242 return r, rv
243
244 # Tests that the unary operation `op` gives results with the same
245 # type for both `self._def` and `self._def_value`.
246 def _test_unaryop_type(self, op):
247 r, rv = self._unaryop(op)
248
249 if r is None:
250 return
251
252 self.assertIsInstance(r, type(rv))
253
254 # Tests that the unary operation `op` gives results with the same
255 # value for both `self._def` and `self._def_value`. This uses the
256 # __eq__() operator of `self._def`.
257 def _test_unaryop_value(self, op):
258 r, rv = self._unaryop(op)
259
260 if r is None:
261 return
262
263 self.assertEqual(r, rv)
264
265 # Tests that the unary operation `op`, when applied to `self._def`,
266 # does not change its underlying BT object address.
267 def _test_unaryop_addr_same(self, op):
268 addr_before = self._def.addr
269 self._unaryop(op)
270 self.assertEqual(self._def.addr, addr_before)
271
272 # Tests that the unary operation `op`, when applied to `self._def`,
273 # does not change its value.
274 def _test_unaryop_value_same(self, op):
275 value_before = copy.copy(self._def_value)
276 self._unaryop(op)
277 self.assertEqual(self._def, value_before)
278
279 # Tests that the binary operation `op` gives results with the same
280 # type for both `self._def` and `self._def_value`.
281 def _test_binop_type(self, op, rhs):
282 r, rv = self._binop(op, rhs)
283
284 if r is None:
285 return
286
287 if op in _COMP_BINOPS:
288 # __eq__() and __ne__() always return a 'bool' object
289 self.assertIsInstance(r, bool)
290 else:
291 self.assertIsInstance(r, type(rv))
292
293 # Tests that the binary operation `op` gives results with the same
294 # value for both `self._def` and `self._def_value`. This uses the
295 # __eq__() operator of `self._def`.
296 def _test_binop_value(self, op, rhs):
297 r, rv = self._binop(op, rhs)
298
299 if r is None:
300 return
301
302 self.assertEqual(r, rv)
303
304 # Tests that the binary operation `op`, when applied to `self._def`,
305 # does not change its underlying BT object address.
306 def _test_binop_lhs_addr_same(self, op, rhs):
307 addr_before = self._def.addr
308 r, rv = self._binop(op, rhs)
309 self.assertEqual(self._def.addr, addr_before)
310
311 # Tests that the binary operation `op`, when applied to `self._def`,
312 # does not change its value.
313 def _test_binop_lhs_value_same(self, op, rhs):
314 value_before = copy.copy(self._def_value)
315 r, rv = self._binop(op, rhs)
316 self.assertEqual(self._def, value_before)
317
318 # The methods below which take the `test_cb` and/or `op` parameters
319 # are meant to be used with one of the _test_binop_*() functions
320 # above as `test_cb` and a binary operator function as `op`.
321 #
322 # For example:
323 #
324 # self._test_binop_rhs_pos_int(self._test_binop_value,
325 # operator.add)
326 #
327 # This tests that a numeric field object added to a positive integer
328 # value gives a result with the expected value.
329 #
330 # `vint` and `vfloat` mean a signed integer value object and a real
331 # value object.
332
333 def _test_binop_unknown(self, op):
334 class A:
335 pass
336
337 # Operators == and != are defined when comparing the field to an
338 # arbitrary object.
339 if op is operator.eq:
340 self.assertIs(op(self._def, A()), False)
341 elif op is operator.ne:
342 self.assertIs(op(self._def, A()), True)
343 else:
344 # But not other operators.
345 with self.assertRaises(TypeError):
346 op(self._def, A())
347
348 def _test_binop_none(self, op):
349 # Operators == and != are defined when comparing the field to None.
350 if op is operator.eq:
351 self.assertIs(op(self._def, None), False)
352 elif op is operator.ne:
353 self.assertIs(op(self._def, None), True)
354 else:
355 # But not other operators.
356 with self.assertRaises(TypeError):
357 op(self._def, None)
358
359 def _test_binop_rhs_false(self, test_cb, op):
360 test_cb(op, False)
361
362 def _test_binop_rhs_true(self, test_cb, op):
363 test_cb(op, True)
364
365 def _test_binop_rhs_pos_int(self, test_cb, op):
366 test_cb(op, 2)
367
368 def _test_binop_rhs_neg_int(self, test_cb, op):
369 test_cb(op, -23)
370
371 def _test_binop_rhs_zero_int(self, test_cb, op):
372 test_cb(op, 0)
373
374 def _test_binop_rhs_pos_vint(self, test_cb, op):
375 test_cb(op, bt2.create_value(2))
376
377 def _test_binop_rhs_neg_vint(self, test_cb, op):
378 test_cb(op, bt2.create_value(-23))
379
380 def _test_binop_rhs_zero_vint(self, test_cb, op):
381 test_cb(op, bt2.create_value(0))
382
383 def _test_binop_rhs_pos_float(self, test_cb, op):
384 test_cb(op, 2.2)
385
386 def _test_binop_rhs_neg_float(self, test_cb, op):
387 test_cb(op, -23.4)
388
389 def _test_binop_rhs_zero_float(self, test_cb, op):
390 test_cb(op, 0.0)
391
392 def _test_binop_rhs_pos_vfloat(self, test_cb, op):
393 test_cb(op, bt2.create_value(2.2))
394
395 def _test_binop_rhs_neg_vfloat(self, test_cb, op):
396 test_cb(op, bt2.create_value(-23.4))
397
398 def _test_binop_rhs_zero_vfloat(self, test_cb, op):
399 test_cb(op, bt2.create_value(0.0))
400
401 def _test_binop_rhs_complex(self, test_cb, op):
402 test_cb(op, -23 + 19j)
403
404 def _test_binop_rhs_zero_complex(self, test_cb, op):
405 test_cb(op, 0j)
406
407 def _test_binop_type_false(self, op):
408 self._test_binop_rhs_false(self._test_binop_type, op)
409
410 def _test_binop_type_true(self, op):
411 self._test_binop_rhs_true(self._test_binop_type, op)
412
413 def _test_binop_type_pos_int(self, op):
414 self._test_binop_rhs_pos_int(self._test_binop_type, op)
415
416 def _test_binop_type_neg_int(self, op):
417 self._test_binop_rhs_neg_int(self._test_binop_type, op)
418
419 def _test_binop_type_zero_int(self, op):
420 self._test_binop_rhs_zero_int(self._test_binop_type, op)
421
422 def _test_binop_type_pos_vint(self, op):
423 self._test_binop_rhs_pos_vint(self._test_binop_type, op)
424
425 def _test_binop_type_neg_vint(self, op):
426 self._test_binop_rhs_neg_vint(self._test_binop_type, op)
427
428 def _test_binop_type_zero_vint(self, op):
429 self._test_binop_rhs_zero_vint(self._test_binop_type, op)
430
431 def _test_binop_type_pos_float(self, op):
432 self._test_binop_rhs_pos_float(self._test_binop_type, op)
433
434 def _test_binop_type_neg_float(self, op):
435 self._test_binop_rhs_neg_float(self._test_binop_type, op)
436
437 def _test_binop_type_zero_float(self, op):
438 self._test_binop_rhs_zero_float(self._test_binop_type, op)
439
440 def _test_binop_type_pos_vfloat(self, op):
441 self._test_binop_rhs_pos_vfloat(self._test_binop_type, op)
442
443 def _test_binop_type_neg_vfloat(self, op):
444 self._test_binop_rhs_neg_vfloat(self._test_binop_type, op)
445
446 def _test_binop_type_zero_vfloat(self, op):
447 self._test_binop_rhs_zero_vfloat(self._test_binop_type, op)
448
449 def _test_binop_type_complex(self, op):
450 self._test_binop_rhs_complex(self._test_binop_type, op)
451
452 def _test_binop_type_zero_complex(self, op):
453 self._test_binop_rhs_zero_complex(self._test_binop_type, op)
454
455 def _test_binop_value_false(self, op):
456 self._test_binop_rhs_false(self._test_binop_value, op)
457
458 def _test_binop_value_true(self, op):
459 self._test_binop_rhs_true(self._test_binop_value, op)
460
461 def _test_binop_value_pos_int(self, op):
462 self._test_binop_rhs_pos_int(self._test_binop_value, op)
463
464 def _test_binop_value_neg_int(self, op):
465 self._test_binop_rhs_neg_int(self._test_binop_value, op)
466
467 def _test_binop_value_zero_int(self, op):
468 self._test_binop_rhs_zero_int(self._test_binop_value, op)
469
470 def _test_binop_value_pos_vint(self, op):
471 self._test_binop_rhs_pos_vint(self._test_binop_value, op)
472
473 def _test_binop_value_neg_vint(self, op):
474 self._test_binop_rhs_neg_vint(self._test_binop_value, op)
475
476 def _test_binop_value_zero_vint(self, op):
477 self._test_binop_rhs_zero_vint(self._test_binop_value, op)
478
479 def _test_binop_value_pos_float(self, op):
480 self._test_binop_rhs_pos_float(self._test_binop_value, op)
481
482 def _test_binop_value_neg_float(self, op):
483 self._test_binop_rhs_neg_float(self._test_binop_value, op)
484
485 def _test_binop_value_zero_float(self, op):
486 self._test_binop_rhs_zero_float(self._test_binop_value, op)
487
488 def _test_binop_value_pos_vfloat(self, op):
489 self._test_binop_rhs_pos_vfloat(self._test_binop_value, op)
490
491 def _test_binop_value_neg_vfloat(self, op):
492 self._test_binop_rhs_neg_vfloat(self._test_binop_value, op)
493
494 def _test_binop_value_zero_vfloat(self, op):
495 self._test_binop_rhs_zero_vfloat(self._test_binop_value, op)
496
497 def _test_binop_value_complex(self, op):
498 self._test_binop_rhs_complex(self._test_binop_value, op)
499
500 def _test_binop_value_zero_complex(self, op):
501 self._test_binop_rhs_zero_complex(self._test_binop_value, op)
502
503 def _test_binop_lhs_addr_same_false(self, op):
504 self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op)
505
506 def _test_binop_lhs_addr_same_true(self, op):
507 self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op)
508
509 def _test_binop_lhs_addr_same_pos_int(self, op):
510 self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op)
511
512 def _test_binop_lhs_addr_same_neg_int(self, op):
513 self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op)
514
515 def _test_binop_lhs_addr_same_zero_int(self, op):
516 self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op)
517
518 def _test_binop_lhs_addr_same_pos_vint(self, op):
519 self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op)
520
521 def _test_binop_lhs_addr_same_neg_vint(self, op):
522 self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op)
523
524 def _test_binop_lhs_addr_same_zero_vint(self, op):
525 self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op)
526
527 def _test_binop_lhs_addr_same_pos_float(self, op):
528 self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op)
529
530 def _test_binop_lhs_addr_same_neg_float(self, op):
531 self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op)
532
533 def _test_binop_lhs_addr_same_zero_float(self, op):
534 self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op)
535
536 def _test_binop_lhs_addr_same_pos_vfloat(self, op):
537 self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op)
538
539 def _test_binop_lhs_addr_same_neg_vfloat(self, op):
540 self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op)
541
542 def _test_binop_lhs_addr_same_zero_vfloat(self, op):
543 self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op)
544
545 def _test_binop_lhs_addr_same_complex(self, op):
546 self._test_binop_rhs_complex(self._test_binop_lhs_addr_same, op)
547
548 def _test_binop_lhs_addr_same_zero_complex(self, op):
549 self._test_binop_rhs_zero_complex(self._test_binop_lhs_addr_same, op)
550
551 def _test_binop_lhs_value_same_false(self, op):
552 self._test_binop_rhs_false(self._test_binop_lhs_value_same, op)
553
554 def _test_binop_lhs_value_same_true(self, op):
555 self._test_binop_rhs_true(self._test_binop_lhs_value_same, op)
556
557 def _test_binop_lhs_value_same_pos_int(self, op):
558 self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op)
559
560 def _test_binop_lhs_value_same_neg_int(self, op):
561 self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op)
562
563 def _test_binop_lhs_value_same_zero_int(self, op):
564 self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op)
565
566 def _test_binop_lhs_value_same_pos_vint(self, op):
567 self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op)
568
569 def _test_binop_lhs_value_same_neg_vint(self, op):
570 self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op)
571
572 def _test_binop_lhs_value_same_zero_vint(self, op):
573 self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op)
574
575 def _test_binop_lhs_value_same_pos_float(self, op):
576 self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op)
577
578 def _test_binop_lhs_value_same_neg_float(self, op):
579 self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op)
580
581 def _test_binop_lhs_value_same_zero_float(self, op):
582 self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op)
583
584 def _test_binop_lhs_value_same_pos_vfloat(self, op):
585 self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op)
586
587 def _test_binop_lhs_value_same_neg_vfloat(self, op):
588 self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op)
589
590 def _test_binop_lhs_value_same_zero_vfloat(self, op):
591 self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op)
592
593 def _test_binop_lhs_value_same_complex(self, op):
594 self._test_binop_rhs_complex(self._test_binop_lhs_value_same, op)
595
596 def _test_binop_lhs_value_same_zero_complex(self, op):
597 self._test_binop_rhs_zero_complex(self._test_binop_lhs_value_same, op)
598
599 def test_bool_op(self):
600 self.assertEqual(bool(self._def), bool(self._def_value))
601
602 def test_int_op(self):
603 self.assertEqual(int(self._def), int(self._def_value))
604
605 def test_float_op(self):
606 self.assertEqual(float(self._def), float(self._def_value))
607
608 def test_complex_op(self):
609 self.assertEqual(complex(self._def), complex(self._def_value))
610
611 def test_str_op(self):
612 self.assertEqual(str(self._def), str(self._def_value))
613
614 def test_hash_op(self):
615 with self.assertRaises(TypeError):
616 hash(self._def)
617
618 def test_const_hash_op(self):
619 self.assertEqual(hash(self._def_const), hash(self._def_value))
620
621 def test_const_hash_dict(self):
622 my_dict = {}
623 my_dict[self._def_const] = 'my_value'
624 self.assertEqual(my_dict[self._def_value], 'my_value')
625
626 def test_eq_none(self):
627 # Ignore this lint error:
628 # E711 comparison to None should be 'if cond is None:'
629 # since this is what we want to test (even though not good practice).
630 self.assertFalse(self._def == None) # noqa: E711
631
632 def test_ne_none(self):
633 # Ignore this lint error:
634 # E711 comparison to None should be 'if cond is not None:'
635 # since this is what we want to test (even though not good practice).
636 self.assertTrue(self._def != None) # noqa: E711
637
638
639 # This is a list of binary operators used for
640 # _inject_numeric_testing_methods().
641 #
642 # Each entry is a pair of binary operator name (used as part of the
643 # created testing method's name) and operator function.
644 _BINOPS = (
645 ('lt', operator.lt),
646 ('le', operator.le),
647 ('eq', operator.eq),
648 ('ne', operator.ne),
649 ('ge', operator.ge),
650 ('gt', operator.gt),
651 ('add', operator.add),
652 ('radd', lambda a, b: operator.add(b, a)),
653 ('and', operator.and_),
654 ('rand', lambda a, b: operator.and_(b, a)),
655 ('floordiv', operator.floordiv),
656 ('rfloordiv', lambda a, b: operator.floordiv(b, a)),
657 ('lshift', operator.lshift),
658 ('rlshift', lambda a, b: operator.lshift(b, a)),
659 ('mod', operator.mod),
660 ('rmod', lambda a, b: operator.mod(b, a)),
661 ('mul', operator.mul),
662 ('rmul', lambda a, b: operator.mul(b, a)),
663 ('or', operator.or_),
664 ('ror', lambda a, b: operator.or_(b, a)),
665 ('pow', operator.pow),
666 ('rpow', lambda a, b: operator.pow(b, a)),
667 ('rshift', operator.rshift),
668 ('rrshift', lambda a, b: operator.rshift(b, a)),
669 ('sub', operator.sub),
670 ('rsub', lambda a, b: operator.sub(b, a)),
671 ('truediv', operator.truediv),
672 ('rtruediv', lambda a, b: operator.truediv(b, a)),
673 ('xor', operator.xor),
674 ('rxor', lambda a, b: operator.xor(b, a)),
675 )
676
677
678 # This is a list of unary operators used for
679 # _inject_numeric_testing_methods().
680 #
681 # Each entry is a pair of unary operator name (used as part of the
682 # created testing method's name) and operator function.
683 _UNARYOPS = (
684 ('neg', operator.neg),
685 ('pos', operator.pos),
686 ('abs', operator.abs),
687 ('invert', operator.invert),
688 ('round', round),
689 ('round_0', partial(round, ndigits=0)),
690 ('round_1', partial(round, ndigits=1)),
691 ('round_2', partial(round, ndigits=2)),
692 ('round_3', partial(round, ndigits=3)),
693 ('ceil', math.ceil),
694 ('floor', math.floor),
695 ('trunc', math.trunc),
696 )
697
698
699 # This function injects a bunch of testing methods to a numeric
700 # field test case.
701 #
702 # It is meant to be used like this:
703 #
704 # _inject_numeric_testing_methods(MyNumericFieldTestCase)
705 #
706 # This function injects:
707 #
708 # * One testing method for each _TestNumericField._test_binop_*()
709 # method, for each binary operator in the _BINOPS tuple.
710 #
711 # * One testing method for each _TestNumericField._test_unaryop*()
712 # method, for each unary operator in the _UNARYOPS tuple.
713 def _inject_numeric_testing_methods(cls):
714 def test_binop_name(suffix):
715 return 'test_binop_{}_{}'.format(name, suffix)
716
717 def test_unaryop_name(suffix):
718 return 'test_unaryop_{}_{}'.format(name, suffix)
719
720 # inject testing methods for each binary operation
721 for name, binop in _BINOPS:
722 setattr(
723 cls,
724 test_binop_name('unknown'),
725 partialmethod(_TestNumericField._test_binop_unknown, op=binop),
726 )
727 setattr(
728 cls,
729 test_binop_name('none'),
730 partialmethod(_TestNumericField._test_binop_none, op=binop),
731 )
732 setattr(
733 cls,
734 test_binop_name('type_true'),
735 partialmethod(_TestNumericField._test_binop_type_true, op=binop),
736 )
737 setattr(
738 cls,
739 test_binop_name('type_pos_int'),
740 partialmethod(_TestNumericField._test_binop_type_pos_int, op=binop),
741 )
742 setattr(
743 cls,
744 test_binop_name('type_pos_vint'),
745 partialmethod(_TestNumericField._test_binop_type_pos_vint, op=binop),
746 )
747 setattr(
748 cls,
749 test_binop_name('value_true'),
750 partialmethod(_TestNumericField._test_binop_value_true, op=binop),
751 )
752 setattr(
753 cls,
754 test_binop_name('value_pos_int'),
755 partialmethod(_TestNumericField._test_binop_value_pos_int, op=binop),
756 )
757 setattr(
758 cls,
759 test_binop_name('value_pos_vint'),
760 partialmethod(_TestNumericField._test_binop_value_pos_vint, op=binop),
761 )
762 setattr(
763 cls,
764 test_binop_name('lhs_addr_same_true'),
765 partialmethod(_TestNumericField._test_binop_lhs_addr_same_true, op=binop),
766 )
767 setattr(
768 cls,
769 test_binop_name('lhs_addr_same_pos_int'),
770 partialmethod(
771 _TestNumericField._test_binop_lhs_addr_same_pos_int, op=binop
772 ),
773 )
774 setattr(
775 cls,
776 test_binop_name('lhs_addr_same_pos_vint'),
777 partialmethod(
778 _TestNumericField._test_binop_lhs_addr_same_pos_vint, op=binop
779 ),
780 )
781 setattr(
782 cls,
783 test_binop_name('lhs_value_same_true'),
784 partialmethod(_TestNumericField._test_binop_lhs_value_same_true, op=binop),
785 )
786 setattr(
787 cls,
788 test_binop_name('lhs_value_same_pos_int'),
789 partialmethod(
790 _TestNumericField._test_binop_lhs_value_same_pos_int, op=binop
791 ),
792 )
793 setattr(
794 cls,
795 test_binop_name('lhs_value_same_pos_vint'),
796 partialmethod(
797 _TestNumericField._test_binop_lhs_value_same_pos_vint, op=binop
798 ),
799 )
800 setattr(
801 cls,
802 test_binop_name('type_neg_int'),
803 partialmethod(_TestNumericField._test_binop_type_neg_int, op=binop),
804 )
805 setattr(
806 cls,
807 test_binop_name('type_neg_vint'),
808 partialmethod(_TestNumericField._test_binop_type_neg_vint, op=binop),
809 )
810 setattr(
811 cls,
812 test_binop_name('value_neg_int'),
813 partialmethod(_TestNumericField._test_binop_value_neg_int, op=binop),
814 )
815 setattr(
816 cls,
817 test_binop_name('value_neg_vint'),
818 partialmethod(_TestNumericField._test_binop_value_neg_vint, op=binop),
819 )
820 setattr(
821 cls,
822 test_binop_name('lhs_addr_same_neg_int'),
823 partialmethod(
824 _TestNumericField._test_binop_lhs_addr_same_neg_int, op=binop
825 ),
826 )
827 setattr(
828 cls,
829 test_binop_name('lhs_addr_same_neg_vint'),
830 partialmethod(
831 _TestNumericField._test_binop_lhs_addr_same_neg_vint, op=binop
832 ),
833 )
834 setattr(
835 cls,
836 test_binop_name('lhs_value_same_neg_int'),
837 partialmethod(
838 _TestNumericField._test_binop_lhs_value_same_neg_int, op=binop
839 ),
840 )
841 setattr(
842 cls,
843 test_binop_name('lhs_value_same_neg_vint'),
844 partialmethod(
845 _TestNumericField._test_binop_lhs_value_same_neg_vint, op=binop
846 ),
847 )
848 setattr(
849 cls,
850 test_binop_name('type_false'),
851 partialmethod(_TestNumericField._test_binop_type_false, op=binop),
852 )
853 setattr(
854 cls,
855 test_binop_name('type_zero_int'),
856 partialmethod(_TestNumericField._test_binop_type_zero_int, op=binop),
857 )
858 setattr(
859 cls,
860 test_binop_name('type_zero_vint'),
861 partialmethod(_TestNumericField._test_binop_type_zero_vint, op=binop),
862 )
863 setattr(
864 cls,
865 test_binop_name('value_false'),
866 partialmethod(_TestNumericField._test_binop_value_false, op=binop),
867 )
868 setattr(
869 cls,
870 test_binop_name('value_zero_int'),
871 partialmethod(_TestNumericField._test_binop_value_zero_int, op=binop),
872 )
873 setattr(
874 cls,
875 test_binop_name('value_zero_vint'),
876 partialmethod(_TestNumericField._test_binop_value_zero_vint, op=binop),
877 )
878 setattr(
879 cls,
880 test_binop_name('lhs_addr_same_false'),
881 partialmethod(_TestNumericField._test_binop_lhs_addr_same_false, op=binop),
882 )
883 setattr(
884 cls,
885 test_binop_name('lhs_addr_same_zero_int'),
886 partialmethod(
887 _TestNumericField._test_binop_lhs_addr_same_zero_int, op=binop
888 ),
889 )
890 setattr(
891 cls,
892 test_binop_name('lhs_addr_same_zero_vint'),
893 partialmethod(
894 _TestNumericField._test_binop_lhs_addr_same_zero_vint, op=binop
895 ),
896 )
897 setattr(
898 cls,
899 test_binop_name('lhs_value_same_false'),
900 partialmethod(_TestNumericField._test_binop_lhs_value_same_false, op=binop),
901 )
902 setattr(
903 cls,
904 test_binop_name('lhs_value_same_zero_int'),
905 partialmethod(
906 _TestNumericField._test_binop_lhs_value_same_zero_int, op=binop
907 ),
908 )
909 setattr(
910 cls,
911 test_binop_name('lhs_value_same_zero_vint'),
912 partialmethod(
913 _TestNumericField._test_binop_lhs_value_same_zero_vint, op=binop
914 ),
915 )
916 setattr(
917 cls,
918 test_binop_name('type_pos_float'),
919 partialmethod(_TestNumericField._test_binop_type_pos_float, op=binop),
920 )
921 setattr(
922 cls,
923 test_binop_name('type_neg_float'),
924 partialmethod(_TestNumericField._test_binop_type_neg_float, op=binop),
925 )
926 setattr(
927 cls,
928 test_binop_name('type_pos_vfloat'),
929 partialmethod(_TestNumericField._test_binop_type_pos_vfloat, op=binop),
930 )
931 setattr(
932 cls,
933 test_binop_name('type_neg_vfloat'),
934 partialmethod(_TestNumericField._test_binop_type_neg_vfloat, op=binop),
935 )
936 setattr(
937 cls,
938 test_binop_name('value_pos_float'),
939 partialmethod(_TestNumericField._test_binop_value_pos_float, op=binop),
940 )
941 setattr(
942 cls,
943 test_binop_name('value_neg_float'),
944 partialmethod(_TestNumericField._test_binop_value_neg_float, op=binop),
945 )
946 setattr(
947 cls,
948 test_binop_name('value_pos_vfloat'),
949 partialmethod(_TestNumericField._test_binop_value_pos_vfloat, op=binop),
950 )
951 setattr(
952 cls,
953 test_binop_name('value_neg_vfloat'),
954 partialmethod(_TestNumericField._test_binop_value_neg_vfloat, op=binop),
955 )
956 setattr(
957 cls,
958 test_binop_name('lhs_addr_same_pos_float'),
959 partialmethod(
960 _TestNumericField._test_binop_lhs_addr_same_pos_float, op=binop
961 ),
962 )
963 setattr(
964 cls,
965 test_binop_name('lhs_addr_same_neg_float'),
966 partialmethod(
967 _TestNumericField._test_binop_lhs_addr_same_neg_float, op=binop
968 ),
969 )
970 setattr(
971 cls,
972 test_binop_name('lhs_addr_same_pos_vfloat'),
973 partialmethod(
974 _TestNumericField._test_binop_lhs_addr_same_pos_vfloat, op=binop
975 ),
976 )
977 setattr(
978 cls,
979 test_binop_name('lhs_addr_same_neg_vfloat'),
980 partialmethod(
981 _TestNumericField._test_binop_lhs_addr_same_neg_vfloat, op=binop
982 ),
983 )
984 setattr(
985 cls,
986 test_binop_name('lhs_value_same_pos_float'),
987 partialmethod(
988 _TestNumericField._test_binop_lhs_value_same_pos_float, op=binop
989 ),
990 )
991 setattr(
992 cls,
993 test_binop_name('lhs_value_same_neg_float'),
994 partialmethod(
995 _TestNumericField._test_binop_lhs_value_same_neg_float, op=binop
996 ),
997 )
998 setattr(
999 cls,
1000 test_binop_name('lhs_value_same_pos_vfloat'),
1001 partialmethod(
1002 _TestNumericField._test_binop_lhs_value_same_pos_vfloat, op=binop
1003 ),
1004 )
1005 setattr(
1006 cls,
1007 test_binop_name('lhs_value_same_neg_vfloat'),
1008 partialmethod(
1009 _TestNumericField._test_binop_lhs_value_same_neg_vfloat, op=binop
1010 ),
1011 )
1012 setattr(
1013 cls,
1014 test_binop_name('type_zero_float'),
1015 partialmethod(_TestNumericField._test_binop_type_zero_float, op=binop),
1016 )
1017 setattr(
1018 cls,
1019 test_binop_name('type_zero_vfloat'),
1020 partialmethod(_TestNumericField._test_binop_type_zero_vfloat, op=binop),
1021 )
1022 setattr(
1023 cls,
1024 test_binop_name('value_zero_float'),
1025 partialmethod(_TestNumericField._test_binop_value_zero_float, op=binop),
1026 )
1027 setattr(
1028 cls,
1029 test_binop_name('value_zero_vfloat'),
1030 partialmethod(_TestNumericField._test_binop_value_zero_vfloat, op=binop),
1031 )
1032 setattr(
1033 cls,
1034 test_binop_name('lhs_addr_same_zero_float'),
1035 partialmethod(
1036 _TestNumericField._test_binop_lhs_addr_same_zero_float, op=binop
1037 ),
1038 )
1039 setattr(
1040 cls,
1041 test_binop_name('lhs_addr_same_zero_vfloat'),
1042 partialmethod(
1043 _TestNumericField._test_binop_lhs_addr_same_zero_vfloat, op=binop
1044 ),
1045 )
1046 setattr(
1047 cls,
1048 test_binop_name('lhs_value_same_zero_float'),
1049 partialmethod(
1050 _TestNumericField._test_binop_lhs_value_same_zero_float, op=binop
1051 ),
1052 )
1053 setattr(
1054 cls,
1055 test_binop_name('lhs_value_same_zero_vfloat'),
1056 partialmethod(
1057 _TestNumericField._test_binop_lhs_value_same_zero_vfloat, op=binop
1058 ),
1059 )
1060 setattr(
1061 cls,
1062 test_binop_name('type_complex'),
1063 partialmethod(_TestNumericField._test_binop_type_complex, op=binop),
1064 )
1065 setattr(
1066 cls,
1067 test_binop_name('type_zero_complex'),
1068 partialmethod(_TestNumericField._test_binop_type_zero_complex, op=binop),
1069 )
1070 setattr(
1071 cls,
1072 test_binop_name('value_complex'),
1073 partialmethod(_TestNumericField._test_binop_value_complex, op=binop),
1074 )
1075 setattr(
1076 cls,
1077 test_binop_name('value_zero_complex'),
1078 partialmethod(_TestNumericField._test_binop_value_zero_complex, op=binop),
1079 )
1080 setattr(
1081 cls,
1082 test_binop_name('lhs_addr_same_complex'),
1083 partialmethod(
1084 _TestNumericField._test_binop_lhs_addr_same_complex, op=binop
1085 ),
1086 )
1087 setattr(
1088 cls,
1089 test_binop_name('lhs_addr_same_zero_complex'),
1090 partialmethod(
1091 _TestNumericField._test_binop_lhs_addr_same_zero_complex, op=binop
1092 ),
1093 )
1094 setattr(
1095 cls,
1096 test_binop_name('lhs_value_same_complex'),
1097 partialmethod(
1098 _TestNumericField._test_binop_lhs_value_same_complex, op=binop
1099 ),
1100 )
1101 setattr(
1102 cls,
1103 test_binop_name('lhs_value_same_zero_complex'),
1104 partialmethod(
1105 _TestNumericField._test_binop_lhs_value_same_zero_complex, op=binop
1106 ),
1107 )
1108
1109 # inject testing methods for each unary operation
1110 for name, unaryop in _UNARYOPS:
1111 setattr(
1112 cls,
1113 test_unaryop_name('type'),
1114 partialmethod(_TestNumericField._test_unaryop_type, op=unaryop),
1115 )
1116 setattr(
1117 cls,
1118 test_unaryop_name('value'),
1119 partialmethod(_TestNumericField._test_unaryop_value, op=unaryop),
1120 )
1121 setattr(
1122 cls,
1123 test_unaryop_name('addr_same'),
1124 partialmethod(_TestNumericField._test_unaryop_addr_same, op=unaryop),
1125 )
1126 setattr(
1127 cls,
1128 test_unaryop_name('value_same'),
1129 partialmethod(_TestNumericField._test_unaryop_value_same, op=unaryop),
1130 )
1131
1132
1133 class BoolFieldTestCase(_TestNumericField, unittest.TestCase):
1134 @staticmethod
1135 def _const_value_setter(field):
1136 field.value = True
1137
1138 def _create_fc(self, tc):
1139 return tc.create_bool_field_class()
1140
1141 def setUp(self):
1142 self._tc = get_default_trace_class()
1143 self._def = _create_field(self._tc, self._create_fc(self._tc))
1144 self._def.value = True
1145 self._def_value = True
1146 self._def_const = create_const_field(
1147 self._tc, self._tc.create_bool_field_class(), self._const_value_setter
1148 )
1149 self._def_new_value = False
1150
1151 def test_classes(self):
1152 self.assertIs(type(self._def), bt2._BoolField)
1153 self.assertIs(type(self._def_const), bt2._BoolFieldConst)
1154
1155 def test_assign_true(self):
1156 raw = True
1157 self._def.value = raw
1158 self.assertEqual(self._def, raw)
1159
1160 def test_assign_false(self):
1161 raw = False
1162 self._def.value = raw
1163 self.assertEqual(self._def, raw)
1164
1165 def test_assign_field_true(self):
1166 field = _create_field(self._tc, self._create_fc(self._tc))
1167 raw = True
1168 field.value = raw
1169 self._def.value = field
1170 self.assertEqual(self._def, raw)
1171
1172 def test_assign_field_false(self):
1173 field = _create_field(self._tc, self._create_fc(self._tc))
1174 raw = False
1175 field.value = raw
1176 self._def.value = field
1177 self.assertEqual(self._def, raw)
1178
1179 def test_assign_invalid_type(self):
1180 with self.assertRaises(TypeError):
1181 self._def.value = 17
1182
1183 def test_str_op(self):
1184 self.assertEqual(str(self._def), str(self._def_value))
1185
1186
1187 _inject_numeric_testing_methods(BoolFieldTestCase)
1188
1189
1190 class _TestIntegerFieldCommon(_TestNumericField):
1191 def test_assign_true(self):
1192 raw = True
1193 self._def.value = raw
1194 self.assertEqual(self._def, raw)
1195
1196 def test_assign_false(self):
1197 raw = False
1198 self._def.value = raw
1199 self.assertEqual(self._def, raw)
1200
1201 def test_assign_pos_int(self):
1202 raw = 477
1203 self._def.value = raw
1204 self.assertEqual(self._def, raw)
1205
1206 def test_assign_neg_int(self):
1207 raw = -13
1208 self._def.value = raw
1209 self.assertEqual(self._def, raw)
1210
1211 def test_assign_int_field(self):
1212 raw = 999
1213 field = _create_field(self._tc, self._create_fc(self._tc))
1214 field.value = raw
1215 self._def.value = field
1216 self.assertEqual(self._def, raw)
1217
1218 def test_assign_invalid_type(self):
1219 with self.assertRaises(TypeError):
1220 self._def.value = 'yes'
1221
1222 def test_assign_uint(self):
1223 uint_fc = self._tc.create_unsigned_integer_field_class(32)
1224 field = _create_field(self._tc, uint_fc)
1225 raw = 1777
1226 field.value = 1777
1227 self.assertEqual(field, raw)
1228
1229 def test_assign_big_uint(self):
1230 uint_fc = self._tc.create_unsigned_integer_field_class(64)
1231 field = _create_field(self._tc, uint_fc)
1232 # Larger than the IEEE 754 double-precision exact representation of
1233 # integers.
1234 raw = (2**53) + 1
1235 field.value = (2**53) + 1
1236 self.assertEqual(field, raw)
1237
1238 def test_assign_uint_out_of_range(self):
1239 uint_fc = self._tc.create_unsigned_integer_field_class(8)
1240 field = _create_field(self._tc, uint_fc)
1241
1242 with self.assertRaises(ValueError) as ctx:
1243 field.value = 256
1244 self.assertEqual(
1245 str(ctx.exception), 'Value 256 is outside valid range [0, 255]'
1246 )
1247
1248 with self.assertRaises(ValueError) as ctx:
1249 field.value = -1
1250 self.assertEqual(str(ctx.exception), 'Value -1 is outside valid range [0, 255]')
1251
1252 def test_assign_int_out_of_range(self):
1253 int_fc = self._tc.create_signed_integer_field_class(8)
1254 field = _create_field(self._tc, int_fc)
1255
1256 with self.assertRaises(ValueError) as ctx:
1257 field.value = 128
1258 self.assertEqual(
1259 str(ctx.exception), 'Value 128 is outside valid range [-128, 127]'
1260 )
1261
1262 with self.assertRaises(ValueError) as ctx:
1263 field.value = -129
1264 self.assertEqual(
1265 str(ctx.exception), 'Value -129 is outside valid range [-128, 127]'
1266 )
1267
1268 def test_str_op(self):
1269 self.assertEqual(str(self._def), str(self._def_value))
1270
1271
1272 _inject_numeric_testing_methods(_TestIntegerFieldCommon)
1273
1274
1275 class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
1276 @staticmethod
1277 def _const_value_setter(field):
1278 field.value = 17
1279
1280 def _create_fc(self, tc):
1281 return tc.create_signed_integer_field_class(25)
1282
1283 def setUp(self):
1284 self._tc = get_default_trace_class()
1285 self._field = _create_field(self._tc, self._create_fc(self._tc))
1286 self._field.value = 17
1287 self._def = _create_field(self._tc, self._create_fc(self._tc))
1288 self._def.value = 17
1289 self._def_value = 17
1290 self._def_const = create_const_field(
1291 self._tc, self._create_fc(self._tc), self._const_value_setter
1292 )
1293 self._def_new_value = -101
1294
1295
1296 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
1297 @staticmethod
1298 def _const_value_setter(field):
1299 field.value = 17
1300
1301 def _create_fc(self, tc):
1302 fc = tc.create_signed_enumeration_field_class(32)
1303 fc.add_mapping('something', bt2.SignedIntegerRangeSet([(17, 17)]))
1304 fc.add_mapping('speaker', bt2.SignedIntegerRangeSet([(12, 16)]))
1305 fc.add_mapping('can', bt2.SignedIntegerRangeSet([(18, 2540)]))
1306 fc.add_mapping(
1307 'whole range', bt2.SignedIntegerRangeSet([(-(2**31), (2**31) - 1)])
1308 )
1309 fc.add_mapping('zip', bt2.SignedIntegerRangeSet([(-45, 1001)]))
1310 return fc
1311
1312 def setUp(self):
1313 self._tc = get_default_trace_class()
1314 self._field = _create_field(self._tc, self._create_fc(self._tc))
1315 self._def = _create_field(self._tc, self._create_fc(self._tc))
1316 self._def.value = 17
1317 self._def_value = 17
1318 self._def_const = create_const_field(
1319 self._tc, self._create_fc(self._tc), self._const_value_setter
1320 )
1321 self._def_new_value = -101
1322
1323 def test_str_op(self):
1324 expected_string_found = False
1325 s = str(self._def)
1326
1327 # Establish all permutations of the three expected matches since
1328 # the order in which mappings are enumerated is not explicitly part of
1329 # the API.
1330 for p in itertools.permutations(['whole range', 'something', 'zip']):
1331 candidate = '{} ({})'.format(self._def_value, ', '.join(p))
1332 if candidate == s:
1333 expected_string_found = True
1334 break
1335
1336 self.assertTrue(expected_string_found)
1337
1338 def test_labels(self):
1339 self._field.value = 17
1340 labels = sorted(self._field.labels)
1341 self.assertEqual(labels, ['something', 'whole range', 'zip'])
1342
1343
1344 class SingleRealFieldTestCase(_TestNumericField, unittest.TestCase):
1345 @staticmethod
1346 def _const_value_setter(field):
1347 field.value = 52.0
1348
1349 def _create_fc(self, tc):
1350 return tc.create_single_precision_real_field_class()
1351
1352 def setUp(self):
1353 self._tc = get_default_trace_class()
1354 self._field = _create_field(self._tc, self._create_fc(self._tc))
1355 self._def = _create_field(self._tc, self._create_fc(self._tc))
1356 self._def_const = create_const_field(
1357 self._tc,
1358 self._tc.create_single_precision_real_field_class(),
1359 self._const_value_setter,
1360 )
1361 self._def.value = 52.0
1362 self._def_value = 52.0
1363 self._def_new_value = -17.0
1364
1365 def _test_invalid_op(self, cb):
1366 with self.assertRaises(TypeError):
1367 cb()
1368
1369 def test_assign_true(self):
1370 self._def.value = True
1371 self.assertTrue(self._def)
1372
1373 def test_assign_false(self):
1374 self._def.value = False
1375 self.assertFalse(self._def)
1376
1377 def test_assign_pos_int(self):
1378 raw = 477
1379 self._def.value = raw
1380 self.assertEqual(self._def, float(raw))
1381
1382 def test_assign_neg_int(self):
1383 raw = -13
1384 self._def.value = raw
1385 self.assertEqual(self._def, float(raw))
1386
1387 def test_assign_int_field(self):
1388 int_fc = self._tc.create_signed_integer_field_class(32)
1389 int_field = _create_field(self._tc, int_fc)
1390 raw = 999
1391 int_field.value = raw
1392 self._def.value = int_field
1393 self.assertEqual(self._def, float(raw))
1394
1395 def test_assign_float(self):
1396 raw = -19.23
1397 self._def.value = raw
1398 # It's expected to have some lost of precision because of the field
1399 # that is in single precision.
1400 self.assertEqual(round(self._def, 5), raw)
1401
1402 def test_assign_float_field(self):
1403 field = _create_field(self._tc, self._create_fc(self._tc))
1404 raw = 101.32
1405 field.value = raw
1406 self._def.value = field
1407 # It's expected to have some lost of precision because of the field
1408 # that is in single precision.
1409 self.assertEqual(round(self._def, 5), raw)
1410
1411 def test_assign_invalid_type(self):
1412 with self.assertRaises(TypeError):
1413 self._def.value = 'yes'
1414
1415 def test_invalid_lshift(self):
1416 self._test_invalid_op(lambda: self._def << 23)
1417
1418 def test_invalid_rshift(self):
1419 self._test_invalid_op(lambda: self._def >> 23)
1420
1421 def test_invalid_and(self):
1422 self._test_invalid_op(lambda: self._def & 23)
1423
1424 def test_invalid_or(self):
1425 self._test_invalid_op(lambda: self._def | 23)
1426
1427 def test_invalid_xor(self):
1428 self._test_invalid_op(lambda: self._def ^ 23)
1429
1430 def test_invalid_invert(self):
1431 self._test_invalid_op(lambda: ~self._def)
1432
1433 def test_str_op(self):
1434 self.assertEqual(str(round(self._def, 5)), str(self._def_value))
1435
1436
1437 _inject_numeric_testing_methods(SingleRealFieldTestCase)
1438
1439
1440 class DoubleRealFieldTestCase(_TestNumericField, unittest.TestCase):
1441 @staticmethod
1442 def _const_value_setter(field):
1443 field.value = 52.7
1444
1445 def _create_fc(self, tc):
1446 return tc.create_double_precision_real_field_class()
1447
1448 def setUp(self):
1449 self._tc = get_default_trace_class()
1450 self._field = _create_field(self._tc, self._create_fc(self._tc))
1451 self._def = _create_field(self._tc, self._create_fc(self._tc))
1452 self._def_const = create_const_field(
1453 self._tc,
1454 self._tc.create_double_precision_real_field_class(),
1455 self._const_value_setter,
1456 )
1457 self._def.value = 52.7
1458 self._def_value = 52.7
1459 self._def_new_value = -17.164857
1460
1461 def _test_invalid_op(self, cb):
1462 with self.assertRaises(TypeError):
1463 cb()
1464
1465 def test_assign_true(self):
1466 self._def.value = True
1467 self.assertTrue(self._def)
1468
1469 def test_assign_false(self):
1470 self._def.value = False
1471 self.assertFalse(self._def)
1472
1473 def test_assign_pos_int(self):
1474 raw = 477
1475 self._def.value = raw
1476 self.assertEqual(self._def, float(raw))
1477
1478 def test_assign_neg_int(self):
1479 raw = -13
1480 self._def.value = raw
1481 self.assertEqual(self._def, float(raw))
1482
1483 def test_assign_int_field(self):
1484 int_fc = self._tc.create_signed_integer_field_class(32)
1485 int_field = _create_field(self._tc, int_fc)
1486 raw = 999
1487 int_field.value = raw
1488 self._def.value = int_field
1489 self.assertEqual(self._def, float(raw))
1490
1491 def test_assign_float(self):
1492 raw = -19.23
1493 self._def.value = raw
1494 self.assertEqual(self._def, raw)
1495
1496 def test_assign_float_field(self):
1497 field = _create_field(self._tc, self._create_fc(self._tc))
1498 raw = 101.32
1499 field.value = raw
1500 self._def.value = field
1501 self.assertEqual(self._def, raw)
1502
1503 def test_assign_invalid_type(self):
1504 with self.assertRaises(TypeError):
1505 self._def.value = 'yes'
1506
1507 def test_invalid_lshift(self):
1508 self._test_invalid_op(lambda: self._def << 23)
1509
1510 def test_invalid_rshift(self):
1511 self._test_invalid_op(lambda: self._def >> 23)
1512
1513 def test_invalid_and(self):
1514 self._test_invalid_op(lambda: self._def & 23)
1515
1516 def test_invalid_or(self):
1517 self._test_invalid_op(lambda: self._def | 23)
1518
1519 def test_invalid_xor(self):
1520 self._test_invalid_op(lambda: self._def ^ 23)
1521
1522 def test_invalid_invert(self):
1523 self._test_invalid_op(lambda: ~self._def)
1524
1525 def test_str_op(self):
1526 self.assertEqual(str(self._def), str(self._def_value))
1527
1528
1529 _inject_numeric_testing_methods(DoubleRealFieldTestCase)
1530
1531
1532 class StringFieldTestCase(unittest.TestCase):
1533 @staticmethod
1534 def _const_value_setter(field):
1535 field.value = 'Hello, World!'
1536
1537 def setUp(self):
1538 self._tc = get_default_trace_class()
1539 self._def_value = 'Hello, World!'
1540 self._def = _create_string_field(self._tc)
1541 self._def_const = create_const_field(
1542 self._tc, self._tc.create_string_field_class(), self._const_value_setter
1543 )
1544 self._def.value = self._def_value
1545 self._def_new_value = 'Yes!'
1546
1547 def test_assign_int(self):
1548 with self.assertRaises(TypeError):
1549 self._def.value = 283
1550
1551 def test_assign_string_field(self):
1552 field = _create_string_field(self._tc)
1553 raw = 'zorg'
1554 field.value = raw
1555 self.assertEqual(field, raw)
1556
1557 def test_eq(self):
1558 self.assertEqual(self._def, self._def_value)
1559
1560 def test_const_eq(self):
1561 self.assertEqual(self._def_const, self._def_value)
1562
1563 def test_not_eq(self):
1564 self.assertNotEqual(self._def, 23)
1565
1566 def test_lt_vstring(self):
1567 s1 = _create_string_field(self._tc)
1568 s1.value = 'allo'
1569 s2 = _create_string_field(self._tc)
1570 s2.value = 'bateau'
1571 self.assertLess(s1, s2)
1572
1573 def test_lt_string(self):
1574 s1 = _create_string_field(self._tc)
1575 s1.value = 'allo'
1576 self.assertLess(s1, 'bateau')
1577
1578 def test_le_vstring(self):
1579 s1 = _create_string_field(self._tc)
1580 s1.value = 'allo'
1581 s2 = _create_string_field(self._tc)
1582 s2.value = 'bateau'
1583 self.assertLessEqual(s1, s2)
1584
1585 def test_le_string(self):
1586 s1 = _create_string_field(self._tc)
1587 s1.value = 'allo'
1588 self.assertLessEqual(s1, 'bateau')
1589
1590 def test_gt_vstring(self):
1591 s1 = _create_string_field(self._tc)
1592 s1.value = 'allo'
1593 s2 = _create_string_field(self._tc)
1594 s2.value = 'bateau'
1595 self.assertGreater(s2, s1)
1596
1597 def test_gt_string(self):
1598 s1 = _create_string_field(self._tc)
1599 s1.value = 'allo'
1600 self.assertGreater('bateau', s1)
1601
1602 def test_ge_vstring(self):
1603 s1 = _create_string_field(self._tc)
1604 s1.value = 'allo'
1605 s2 = _create_string_field(self._tc)
1606 s2.value = 'bateau'
1607 self.assertGreaterEqual(s2, s1)
1608
1609 def test_ge_string(self):
1610 s1 = _create_string_field(self._tc)
1611 s1.value = 'allo'
1612 self.assertGreaterEqual('bateau', s1)
1613
1614 def test_bool_op(self):
1615 self.assertEqual(bool(self._def), bool(self._def_value))
1616
1617 def test_str_op(self):
1618 self.assertEqual(str(self._def), str(self._def_value))
1619
1620 def test_len(self):
1621 self.assertEqual(len(self._def), len(self._def_value))
1622
1623 def test_getitem(self):
1624 self.assertEqual(self._def[5], self._def_value[5])
1625
1626 def test_const_getitem(self):
1627 self.assertEqual(self._def_const[5], self._def_value[5])
1628
1629 def test_append_str(self):
1630 to_append = 'meow meow meow'
1631 self._def += to_append
1632 self._def_value += to_append
1633 self.assertEqual(self._def, self._def_value)
1634
1635 def test_const_append_str(self):
1636 to_append = 'meow meow meow'
1637 with self.assertRaises(TypeError):
1638 self._def_const += to_append
1639 self.assertEqual(self._def_const, self._def_value)
1640
1641 def test_append_string_field(self):
1642 field = _create_string_field(self._tc)
1643 to_append = 'meow meow meow'
1644 field.value = to_append
1645 self._def += field
1646 self._def_value += to_append
1647 self.assertEqual(self._def, self._def_value)
1648
1649 def test_hash_op(self):
1650 with self.assertRaises(TypeError):
1651 hash(self._def)
1652
1653 def test_const_hash_op(self):
1654 self.assertEqual(hash(self._def_const), hash(self._def_value))
1655
1656 def test_const_hash_dict(self):
1657 my_dict = {}
1658 my_dict[self._def_const] = 'my_value'
1659 self.assertEqual(my_dict[self._def_value], 'my_value')
1660
1661
1662 class _TestArrayFieldCommon:
1663 def _modify_def(self):
1664 self._def[2] = 23
1665
1666 def test_bool_op_true(self):
1667 self.assertTrue(self._def)
1668
1669 def test_len(self):
1670 self.assertEqual(len(self._def), 3)
1671
1672 def test_length(self):
1673 self.assertEqual(self._def.length, 3)
1674
1675 def test_getitem(self):
1676 field = self._def[1]
1677 self.assertIs(type(field), bt2._SignedIntegerField)
1678 self.assertEqual(field, 1847)
1679
1680 def test_const_getitem(self):
1681 field = self._def_const[1]
1682 self.assertIs(type(field), bt2._SignedIntegerFieldConst)
1683 self.assertEqual(field, 1847)
1684
1685 def test_eq(self):
1686 field = _create_int_array_field(self._tc, 3)
1687 field[0] = 45
1688 field[1] = 1847
1689 field[2] = 1948754
1690 self.assertEqual(self._def, field)
1691
1692 def test_eq_invalid_type(self):
1693 self.assertNotEqual(self._def, 23)
1694
1695 def test_eq_diff_len(self):
1696 field = _create_int_array_field(self._tc, 2)
1697 field[0] = 45
1698 field[1] = 1847
1699 self.assertNotEqual(self._def, field)
1700
1701 def test_eq_diff_content_same_len(self):
1702 field = _create_int_array_field(self._tc, 3)
1703 field[0] = 45
1704 field[1] = 1846
1705 field[2] = 1948754
1706 self.assertNotEqual(self._def, field)
1707
1708 def test_eq_non_sequence_iterable(self):
1709 dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)])
1710 field = _create_int_array_field(self._tc, 3)
1711 field[0] = 1
1712 field[1] = 3
1713 field[2] = 5
1714 self.assertEqual(field, list(dct.keys()))
1715 self.assertNotEqual(field, dct)
1716
1717 def test_setitem(self):
1718 self._def[2] = 24
1719 self.assertEqual(self._def[2], 24)
1720
1721 def test_setitem_int_field(self):
1722 int_fc = self._tc.create_signed_integer_field_class(32)
1723 int_field = _create_field(self._tc, int_fc)
1724 int_field.value = 19487
1725 self._def[1] = int_field
1726 self.assertEqual(self._def[1], 19487)
1727
1728 def test_setitem_non_basic_field(self):
1729 array_field = _create_struct_array_field(self._tc, 2)
1730 with self.assertRaises(TypeError):
1731 array_field[1] = 23
1732
1733 def test_setitem_none(self):
1734 with self.assertRaises(TypeError):
1735 self._def[1] = None
1736
1737 def test_setitem_index_wrong_type(self):
1738 with self.assertRaises(TypeError):
1739 self._def['yes'] = 23
1740
1741 def test_setitem_index_neg(self):
1742 with self.assertRaises(IndexError):
1743 self._def[-2] = 23
1744
1745 def test_setitem_index_out_of_range(self):
1746 with self.assertRaises(IndexError):
1747 self._def[len(self._def)] = 134679
1748
1749 def test_const_setitem(self):
1750 with self.assertRaises(TypeError):
1751 self._def_const[0] = 134679
1752
1753 def test_iter(self):
1754 for field, value in zip(self._def, (45, 1847, 1948754)):
1755 self.assertEqual(field, value)
1756
1757 def test_const_iter(self):
1758 for field, value in zip(self._def_const, (45, 1847, 1948754)):
1759 self.assertEqual(field, value)
1760
1761 def test_value_int_field(self):
1762 values = [45646, 145, 12145]
1763 self._def.value = values
1764 self.assertEqual(values, self._def)
1765
1766 def test_value_check_sequence(self):
1767 values = 42
1768 with self.assertRaises(TypeError):
1769 self._def.value = values
1770
1771 def test_value_wrong_type_in_sequence(self):
1772 values = [32, 'hello', 11]
1773 with self.assertRaises(TypeError):
1774 self._def.value = values
1775
1776 def test_value_complex_type(self):
1777 struct_fc = self._tc.create_structure_field_class()
1778 int_fc = self._tc.create_signed_integer_field_class(32)
1779 another_int_fc = self._tc.create_signed_integer_field_class(32)
1780 str_fc = self._tc.create_string_field_class()
1781 struct_fc.append_member(field_class=int_fc, name='an_int')
1782 struct_fc.append_member(field_class=str_fc, name='a_string')
1783 struct_fc.append_member(field_class=another_int_fc, name='another_int')
1784 array_fc = self._tc.create_static_array_field_class(struct_fc, 3)
1785 stream = _create_stream(self._tc, [('array_field', array_fc)])
1786 values = [
1787 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1788 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1789 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1790 ]
1791
1792 array = stream.create_packet().context_field['array_field']
1793 array.value = values
1794 self.assertEqual(values, array)
1795 values[0]['an_int'] = 'a string'
1796 with self.assertRaises(TypeError):
1797 array.value = values
1798
1799 def test_str_op(self):
1800 s = str(self._def)
1801 expected_string = '[{}]'.format(', '.join([repr(v) for v in self._def_value]))
1802 self.assertEqual(expected_string, s)
1803
1804
1805 class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase):
1806 @staticmethod
1807 def _const_value_setter(field):
1808 field.value = [45, 1847, 1948754]
1809
1810 def setUp(self):
1811 self._tc = get_default_trace_class()
1812 self._def = _create_int_array_field(self._tc, 3)
1813 self._def[0] = 45
1814 self._def[1] = 1847
1815 self._def[2] = 1948754
1816 self._def_value = [45, 1847, 1948754]
1817 self._def_const = create_const_field(
1818 self._tc,
1819 self._tc.create_static_array_field_class(
1820 self._tc.create_signed_integer_field_class(32), 3
1821 ),
1822 self._const_value_setter,
1823 )
1824
1825 def test_value_wrong_len(self):
1826 values = [45, 1847]
1827 with self.assertRaises(ValueError):
1828 self._def.value = values
1829
1830
1831 class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase):
1832 @staticmethod
1833 def _const_value_setter(field):
1834 field.value = [45, 1847, 1948754]
1835
1836 def setUp(self):
1837 self._tc = get_default_trace_class()
1838 self._def = _create_dynamic_array(self._tc)
1839 self._def[0] = 45
1840 self._def[1] = 1847
1841 self._def[2] = 1948754
1842 self._def_value = [45, 1847, 1948754]
1843 self._def_const = create_const_field(
1844 self._tc,
1845 self._tc.create_dynamic_array_field_class(
1846 self._tc.create_signed_integer_field_class(32)
1847 ),
1848 self._const_value_setter,
1849 )
1850
1851 def test_value_resize(self):
1852 new_values = [1, 2, 3, 4]
1853 self._def.value = new_values
1854 self.assertCountEqual(self._def, new_values)
1855
1856 def test_set_length(self):
1857 self._def.length = 4
1858 self._def[3] = 0
1859 self.assertEqual(len(self._def), 4)
1860
1861 def test_const_set_length(self):
1862 with self.assertRaises(AttributeError):
1863 self._def_const.length = 4
1864 self.assertEqual(len(self._def), 3)
1865
1866 def test_set_invalid_length(self):
1867 with self.assertRaises(TypeError):
1868 self._def.length = 'cheval'
1869
1870
1871 class StructureFieldTestCase(unittest.TestCase):
1872 @staticmethod
1873 def _const_value_setter(field):
1874 field.value = {
1875 'A': -1872,
1876 'B': 'salut',
1877 'C': 17.5,
1878 'D': 16497,
1879 'E': {},
1880 'F': {'F_1': 52},
1881 }
1882
1883 def _create_fc(self, tc):
1884 fc = tc.create_structure_field_class()
1885 fc.append_member('A', self._fc0_fn())
1886 fc.append_member('B', self._fc1_fn())
1887 fc.append_member('C', self._fc2_fn())
1888 fc.append_member('D', self._fc3_fn())
1889 fc.append_member('E', self._fc4_fn())
1890 fc5 = self._fc5_fn()
1891 fc5.append_member('F_1', self._fc5_inner_fn())
1892 fc.append_member('F', fc5)
1893 return fc
1894
1895 def setUp(self):
1896 self._tc = get_default_trace_class()
1897 self._fc0_fn = self._tc.create_signed_integer_field_class
1898 self._fc1_fn = self._tc.create_string_field_class
1899 self._fc2_fn = self._tc.create_double_precision_real_field_class
1900 self._fc3_fn = self._tc.create_signed_integer_field_class
1901 self._fc4_fn = self._tc.create_structure_field_class
1902 self._fc5_fn = self._tc.create_structure_field_class
1903 self._fc5_inner_fn = self._tc.create_signed_integer_field_class
1904
1905 self._fc = self._create_fc(self._tc)
1906 self._def = _create_field(self._tc, self._fc)
1907 self._def['A'] = -1872
1908 self._def['B'] = 'salut'
1909 self._def['C'] = 17.5
1910 self._def['D'] = 16497
1911 self._def['E'] = {}
1912 self._def['F'] = {'F_1': 52}
1913 self._def_value = {
1914 'A': -1872,
1915 'B': 'salut',
1916 'C': 17.5,
1917 'D': 16497,
1918 'E': {},
1919 'F': {'F_1': 52},
1920 }
1921
1922 self._def_const = create_const_field(
1923 self._tc, self._create_fc(self._tc), self._const_value_setter
1924 )
1925
1926 def _modify_def(self):
1927 self._def['B'] = 'hola'
1928
1929 def test_bool_op_true(self):
1930 self.assertTrue(self._def)
1931
1932 def test_bool_op_false(self):
1933 field = self._def['E']
1934 self.assertFalse(field)
1935
1936 def test_len(self):
1937 self.assertEqual(len(self._def), len(self._def_value))
1938
1939 def test_getitem(self):
1940 field1 = self._def['A']
1941 field2 = self._def['B']
1942 field3 = self._def['C']
1943 field4 = self._def['D']
1944 field5 = self._def['E']
1945 field6 = self._def['F']
1946
1947 self.assertIs(type(field1), bt2._SignedIntegerField)
1948 self.assertEqual(field1, -1872)
1949
1950 self.assertIs(type(field2), bt2._StringField)
1951 self.assertEqual(field2, 'salut')
1952
1953 self.assertIs(type(field3), bt2._DoublePrecisionRealField)
1954 self.assertEqual(field3, 17.5)
1955
1956 self.assertIs(type(field4), bt2._SignedIntegerField)
1957 self.assertEqual(field4, 16497)
1958
1959 self.assertIs(type(field5), bt2._StructureField)
1960 self.assertEqual(field5, {})
1961
1962 self.assertIs(type(field6), bt2._StructureField)
1963 self.assertEqual(field6, {'F_1': 52})
1964
1965 def test_const_getitem(self):
1966 field1 = self._def_const['A']
1967 field2 = self._def_const['B']
1968 field3 = self._def_const['C']
1969 field4 = self._def_const['D']
1970 field5 = self._def_const['E']
1971 field6 = self._def_const['F']
1972
1973 self.assertIs(type(field1), bt2._SignedIntegerFieldConst)
1974 self.assertEqual(field1, -1872)
1975
1976 self.assertIs(type(field2), bt2._StringFieldConst)
1977 self.assertEqual(field2, 'salut')
1978
1979 self.assertIs(type(field3), bt2._DoublePrecisionRealFieldConst)
1980 self.assertEqual(field3, 17.5)
1981
1982 self.assertIs(type(field4), bt2._SignedIntegerFieldConst)
1983 self.assertEqual(field4, 16497)
1984
1985 self.assertIs(type(field5), bt2._StructureFieldConst)
1986 self.assertEqual(field5, {})
1987
1988 self.assertIs(type(field6), bt2._StructureFieldConst)
1989 self.assertEqual(field6, {'F_1': 52})
1990
1991 def test_member_at_index_out_of_bounds_after(self):
1992 with self.assertRaises(IndexError):
1993 self._def.member_at_index(len(self._def_value))
1994
1995 def test_eq(self):
1996 field = _create_field(self._tc, self._create_fc(self._tc))
1997 field['A'] = -1872
1998 field['B'] = 'salut'
1999 field['C'] = 17.5
2000 field['D'] = 16497
2001 field['E'] = {}
2002 field['F'] = {'F_1': 52}
2003 self.assertEqual(self._def, field)
2004
2005 def test_const_eq(self):
2006 field = _create_field(self._tc, self._create_fc(self._tc))
2007 field['A'] = -1872
2008 field['B'] = 'salut'
2009 field['C'] = 17.5
2010 field['D'] = 16497
2011 field['E'] = {}
2012 field['F'] = {'F_1': 52}
2013 self.assertEqual(self._def_const, field)
2014
2015 def test_eq_invalid_type(self):
2016 self.assertNotEqual(self._def, 23)
2017
2018 def test_eq_diff_len(self):
2019 fc = self._tc.create_structure_field_class()
2020 fc.append_member('A', self._fc0_fn())
2021 fc.append_member('B', self._fc1_fn())
2022 fc.append_member('C', self._fc2_fn())
2023
2024 field = _create_field(self._tc, fc)
2025 field['A'] = -1872
2026 field['B'] = 'salut'
2027 field['C'] = 17.5
2028 self.assertNotEqual(self._def, field)
2029
2030 def test_eq_diff_keys(self):
2031 fc = self._tc.create_structure_field_class()
2032 fc.append_member('U', self._fc0_fn())
2033 fc.append_member('V', self._fc1_fn())
2034 fc.append_member('W', self._fc2_fn())
2035 fc.append_member('X', self._fc3_fn())
2036 fc.append_member('Y', self._fc4_fn())
2037 fc.append_member('Z', self._fc5_fn())
2038 field = _create_field(self._tc, fc)
2039 field['U'] = -1871
2040 field['V'] = "gerry"
2041 field['W'] = 18.19
2042 field['X'] = 16497
2043 field['Y'] = {}
2044 field['Z'] = {}
2045 self.assertNotEqual(self._def, field)
2046
2047 def test_eq_diff_content_same_len(self):
2048 field = _create_field(self._tc, self._create_fc(self._tc))
2049 field['A'] = -1872
2050 field['B'] = 'salut'
2051 field['C'] = 17.4
2052 field['D'] = 16497
2053 field['E'] = {}
2054 field['F'] = {'F_1': 0}
2055 self.assertNotEqual(self._def, field)
2056
2057 def test_eq_same_content_diff_keys(self):
2058 fc = self._tc.create_structure_field_class()
2059 fc.append_member('A', self._fc0_fn())
2060 fc.append_member('B', self._fc1_fn())
2061 fc.append_member('E', self._fc2_fn())
2062 fc.append_member('D', self._fc3_fn())
2063 fc.append_member('C', self._fc4_fn())
2064 fc.append_member('F', self._fc5_fn())
2065 field = _create_field(self._tc, fc)
2066 field['A'] = -1872
2067 field['B'] = 'salut'
2068 field['E'] = 17.5
2069 field['D'] = 16497
2070 field['C'] = {}
2071 field['F'] = {}
2072 self.assertNotEqual(self._def, field)
2073
2074 def test_setitem(self):
2075 self._def['C'] = -18.47
2076 self.assertEqual(self._def['C'], -18.47)
2077
2078 def test_const_setitem(self):
2079 with self.assertRaises(TypeError):
2080 self._def_const['A'] = 134679
2081
2082 def test_setitem_int_field(self):
2083 int_fc = self._tc.create_signed_integer_field_class(32)
2084 int_field = _create_field(self._tc, int_fc)
2085 int_field.value = 19487
2086 self._def['D'] = int_field
2087 self.assertEqual(self._def['D'], 19487)
2088
2089 def test_setitem_non_basic_field(self):
2090 elem_fc = self._tc.create_structure_field_class()
2091 struct_fc = self._tc.create_structure_field_class()
2092 struct_fc.append_member('A', elem_fc)
2093 struct_field = _create_field(self._tc, struct_fc)
2094
2095 # Will fail on access to .items() of the value
2096 with self.assertRaises(AttributeError):
2097 struct_field['A'] = 23
2098
2099 def test_setitem_none(self):
2100 with self.assertRaises(TypeError):
2101 self._def['C'] = None
2102
2103 def test_setitem_key_wrong_type(self):
2104 with self.assertRaises(TypeError):
2105 self._def[3] = 23
2106
2107 def test_setitem_wrong_key(self):
2108 with self.assertRaises(KeyError):
2109 self._def['hi'] = 134679
2110
2111 def test_member_at_index(self):
2112 self.assertEqual(self._def.member_at_index(1), 'salut')
2113
2114 def test_const_member_at_index(self):
2115 self.assertEqual(self._def_const.member_at_index(1), 'salut')
2116
2117 def test_iter(self):
2118 orig_values = {
2119 'A': -1872,
2120 'B': 'salut',
2121 'C': 17.5,
2122 'D': 16497,
2123 'E': {},
2124 'F': {'F_1': 52},
2125 }
2126
2127 for vkey, vval in self._def.items():
2128 val = orig_values[vkey]
2129 self.assertEqual(vval, val)
2130
2131 def test_value(self):
2132 orig_values = {
2133 'A': -1872,
2134 'B': 'salut',
2135 'C': 17.5,
2136 'D': 16497,
2137 'E': {},
2138 'F': {'F_1': 52},
2139 }
2140 self.assertEqual(self._def, orig_values)
2141
2142 def test_set_value(self):
2143 int_fc = self._tc.create_signed_integer_field_class(32)
2144 another_int_fc = self._tc.create_signed_integer_field_class(32)
2145 str_fc = self._tc.create_string_field_class()
2146 struct_fc = self._tc.create_structure_field_class()
2147 struct_fc.append_member(field_class=int_fc, name='an_int')
2148 struct_fc.append_member(field_class=str_fc, name='a_string')
2149 struct_fc.append_member(field_class=another_int_fc, name='another_int')
2150 values = {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2151
2152 struct = _create_field(self._tc, struct_fc)
2153 struct.value = values
2154 self.assertEqual(values, struct)
2155
2156 bad_type_values = copy.deepcopy(values)
2157 bad_type_values['an_int'] = 'a string'
2158 with self.assertRaises(TypeError):
2159 struct.value = bad_type_values
2160
2161 unknown_key_values = copy.deepcopy(values)
2162 unknown_key_values['unknown_key'] = 16546
2163 with self.assertRaises(KeyError):
2164 struct.value = unknown_key_values
2165
2166 def test_str_op(self):
2167 expected_string_found = False
2168 s = str(self._def)
2169 # Establish all permutations of the three expected matches since
2170 # the order in which mappings are enumerated is not explicitly part of
2171 # the API.
2172 for p in itertools.permutations([(k, v) for k, v in self._def.items()]):
2173 items = ['{}: {}'.format(repr(k), repr(v)) for k, v in p]
2174 candidate = '{{{}}}'.format(', '.join(items))
2175 if candidate == s:
2176 expected_string_found = True
2177 break
2178
2179 self.assertTrue(expected_string_found)
2180
2181
2182 class OptionFieldTestCase(unittest.TestCase):
2183 @staticmethod
2184 def _const_value_setter(field):
2185 field.value = {'opt_field': 'hiboux'}
2186
2187 def _create_fc(self, tc):
2188 fc = tc.create_option_without_selector_field_class(
2189 tc.create_string_field_class()
2190 )
2191 top_fc = tc.create_structure_field_class()
2192 top_fc.append_member('opt_field', fc)
2193 return top_fc
2194
2195 def setUp(self):
2196 self._tc = get_default_trace_class()
2197 fld = _create_field(self._tc, self._create_fc(self._tc))
2198 self._def = fld['opt_field']
2199 self._def_value = 'hiboux'
2200 self._def_const = create_const_field(
2201 self._tc, self._create_fc(self._tc), self._const_value_setter
2202 )['opt_field']
2203
2204 def test_value_prop(self):
2205 self._def.value = 'hiboux'
2206 self.assertEqual(self._def.field, 'hiboux')
2207 self.assertIs(type(self._def), bt2._OptionField)
2208 self.assertIs(type(self._def.field), bt2._StringField)
2209 self.assertTrue(self._def.has_field)
2210
2211 def test_const_value_prop(self):
2212 self.assertEqual(self._def_const.field, 'hiboux')
2213 self.assertIs(type(self._def_const), bt2._OptionFieldConst)
2214 self.assertIs(type(self._def_const.field), bt2._StringFieldConst)
2215 self.assertTrue(self._def_const.has_field)
2216
2217 def test_has_field_prop_true(self):
2218 self._def.has_field = True
2219 self.assertTrue(self._def.has_field)
2220
2221 def test_has_field_prop_false(self):
2222 self._def.has_field = False
2223 self.assertFalse(self._def.has_field)
2224
2225 def test_bool_op_true(self):
2226 self._def.value = 'allo'
2227 self.assertTrue(self._def)
2228
2229 def test_bool_op_false(self):
2230 self._def.has_field = False
2231 self.assertFalse(self._def)
2232
2233 def test_field_prop_existing(self):
2234 self._def.value = 'meow'
2235 field = self._def.field
2236 self.assertEqual(field, 'meow')
2237
2238 def test_field_prop_none(self):
2239 self._def.has_field = False
2240 field = self._def.field
2241 self.assertIsNone(field)
2242
2243 def test_const_field_prop(self):
2244 with self.assertRaises(AttributeError):
2245 self._def_const.has_field = False
2246
2247 self.assertEqual(self._def_const, self._def_value)
2248 self.assertTrue(self._def_const.has_field)
2249
2250 def test_field_prop_existing_then_none(self):
2251 self._def.value = 'meow'
2252 field = self._def.field
2253 self.assertEqual(field, 'meow')
2254 self._def.has_field = False
2255 field = self._def.field
2256 self.assertIsNone(field)
2257
2258 def test_eq(self):
2259 field = _create_field(self._tc, self._create_fc(self._tc))
2260 field = field['opt_field']
2261 field.value = 'walk'
2262 self._def.value = 'walk'
2263 self.assertEqual(self._def, field)
2264
2265 def test_const_eq(self):
2266 field = _create_field(self._tc, self._create_fc(self._tc))
2267 field = field['opt_field']
2268 field.value = 'hiboux'
2269 self.assertEqual(self._def_const, field)
2270 self.assertEqual(self._def_const, self._def_value)
2271
2272 def test_eq_invalid_type(self):
2273 self._def.value = 'gerry'
2274 self.assertNotEqual(self._def, 23)
2275
2276 def test_str_op(self):
2277 self._def.value = 'marcel'
2278 self.assertEqual(str(self._def), str(self._def.field))
2279
2280 def test_repr_op(self):
2281 self._def.value = 'mireille'
2282 self.assertEqual(repr(self._def), repr(self._def.field))
2283
2284
2285 class VariantFieldTestCase(unittest.TestCase):
2286 @staticmethod
2287 def _const_value_setter(field):
2288 field.selected_option_index = 3
2289 field.value = 1334
2290
2291 def _create_fc(self, tc):
2292 ft0 = tc.create_signed_integer_field_class(32)
2293 ft1 = tc.create_string_field_class()
2294 ft2 = tc.create_double_precision_real_field_class()
2295 ft3 = tc.create_signed_integer_field_class(17)
2296 fc = tc.create_variant_field_class()
2297 fc.append_option('corner', ft0)
2298 fc.append_option('zoom', ft1)
2299 fc.append_option('mellotron', ft2)
2300 fc.append_option('giorgio', ft3)
2301 top_fc = tc.create_structure_field_class()
2302 top_fc.append_member('variant_field', fc)
2303 return top_fc
2304
2305 def setUp(self):
2306 self._tc = get_default_trace_class()
2307 fld = _create_field(self._tc, self._create_fc(self._tc))
2308 self._def = fld['variant_field']
2309
2310 self._def_value = 1334
2311 self._def_selected_index = 3
2312 const_fc = self._create_fc(self._tc)['variant_field']
2313
2314 fld_const = create_const_field(
2315 self._tc, const_fc.field_class, self._const_value_setter
2316 )
2317 self._def_const = fld_const
2318
2319 def test_bool_op(self):
2320 self._def.selected_option_index = 2
2321 self._def.value = -17.34
2322 with self.assertRaises(NotImplementedError):
2323 bool(self._def)
2324
2325 def test_selected_option_index(self):
2326 self._def.selected_option_index = 2
2327 self.assertEqual(self._def.selected_option_index, 2)
2328
2329 def test_selected_option_index_above_range(self):
2330 with self.assertRaises(IndexError):
2331 self._def.selected_option_index = 4
2332
2333 def test_selected_option_index_below_range(self):
2334 with self.assertRaises(IndexError):
2335 self._def.selected_option_index = -1
2336
2337 def test_const_selected_option_index(self):
2338 with self.assertRaises(AttributeError):
2339 self._def_const.selected_option_index = 2
2340 self.assertEqual(self._def_const.selected_option_index, 3)
2341
2342 def test_selected_option(self):
2343 self._def.selected_option_index = 2
2344 self._def.value = -17.34
2345 self.assertEqual(self._def.selected_option, -17.34)
2346 self.assertEqual(type(self._def.selected_option), bt2._DoublePrecisionRealField)
2347
2348 self._def.selected_option_index = 3
2349 self._def.value = 1921
2350 self.assertEqual(self._def.selected_option, 1921)
2351 self.assertEqual(type(self._def.selected_option), bt2._SignedIntegerField)
2352
2353 def test_const_selected_option(self):
2354 self.assertEqual(self._def_const.selected_option, 1334)
2355 self.assertEqual(
2356 type(self._def_const.selected_option), bt2._SignedIntegerFieldConst
2357 )
2358
2359 def test_eq(self):
2360 field = _create_field(self._tc, self._create_fc(self._tc))
2361 field = field['variant_field']
2362 field.selected_option_index = 0
2363 field.value = 1774
2364 self._def.selected_option_index = 0
2365 self._def.value = 1774
2366 self.assertEqual(self._def, field)
2367
2368 def test_const_eq(self):
2369 field = _create_field(self._tc, self._create_fc(self._tc))
2370 field = field['variant_field']
2371 field.selected_option_index = 3
2372 field.value = 1334
2373 self.assertEqual(self._def_const, field)
2374
2375 def test_len(self):
2376 self.assertEqual(len(self._def), 4)
2377
2378 def test_eq_invalid_type(self):
2379 self._def.selected_option_index = 1
2380 self._def.value = 'gerry'
2381 self.assertNotEqual(self._def, 23)
2382
2383 def test_str_op_int(self):
2384 field = _create_field(self._tc, self._create_fc(self._tc))
2385 field = field['variant_field']
2386 field.selected_option_index = 0
2387 field.value = 1774
2388 other_field = _create_field(self._tc, self._create_fc(self._tc))
2389 other_field = other_field['variant_field']
2390 other_field.selected_option_index = 0
2391 other_field.value = 1774
2392 self.assertEqual(str(field), str(other_field))
2393
2394 def test_str_op_str(self):
2395 field = _create_field(self._tc, self._create_fc(self._tc))
2396 field = field['variant_field']
2397 field.selected_option_index = 1
2398 field.value = 'un beau grand bateau'
2399 other_field = _create_field(self._tc, self._create_fc(self._tc))
2400 other_field = other_field['variant_field']
2401 other_field.selected_option_index = 1
2402 other_field.value = 'un beau grand bateau'
2403 self.assertEqual(str(field), str(other_field))
2404
2405 def test_str_op_float(self):
2406 field = _create_field(self._tc, self._create_fc(self._tc))
2407 field = field['variant_field']
2408 field.selected_option_index = 2
2409 field.value = 14.4245
2410 other_field = _create_field(self._tc, self._create_fc(self._tc))
2411 other_field = other_field['variant_field']
2412 other_field.selected_option_index = 2
2413 other_field.value = 14.4245
2414 self.assertEqual(str(field), str(other_field))
2415
2416
2417 if __name__ == '__main__':
2418 unittest.main()
This page took 0.121574 seconds and 4 git commands to generate.