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