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