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