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