bt2: field.py: raise ValueError when setting out of range value to Integer
[babeltrace.git] / tests / bindings / python / bt2 / test_field.py
... / ...
CommitLineData
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
19from functools import partial, partialmethod
20import operator
21import unittest
22import math
23import copy
24import itertools
25import collections
26import bt2
27from 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
39def _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
59def _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
72def _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, 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, 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
116def _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
130def _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
146def _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
164def _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
173class 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`.
226class _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.
762def _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
1182class 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
1239class _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
1324class 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
1345class 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
1393class RealFieldTestCase(_TestNumericField, unittest.TestCase):
1394 @staticmethod
1395 def _const_value_setter(field):
1396 field.value = 52.7
1397
1398 def _create_fc(self, tc):
1399 return tc.create_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, self._tc.create_real_field_class(), self._const_value_setter
1407 )
1408 self._def.value = 52.7
1409 self._def_value = 52.7
1410 self._def_new_value = -17.164857
1411
1412 def _test_invalid_op(self, cb):
1413 with self.assertRaises(TypeError):
1414 cb()
1415
1416 def test_assign_true(self):
1417 self._def.value = True
1418 self.assertTrue(self._def)
1419
1420 def test_assign_false(self):
1421 self._def.value = False
1422 self.assertFalse(self._def)
1423
1424 def test_assign_pos_int(self):
1425 raw = 477
1426 self._def.value = raw
1427 self.assertEqual(self._def, float(raw))
1428
1429 def test_assign_neg_int(self):
1430 raw = -13
1431 self._def.value = raw
1432 self.assertEqual(self._def, float(raw))
1433
1434 def test_assign_int_field(self):
1435 int_fc = self._tc.create_signed_integer_field_class(32)
1436 int_field = _create_field(self._tc, int_fc)
1437 raw = 999
1438 int_field.value = raw
1439 self._def.value = int_field
1440 self.assertEqual(self._def, float(raw))
1441
1442 def test_assign_float(self):
1443 raw = -19.23
1444 self._def.value = raw
1445 self.assertEqual(self._def, raw)
1446
1447 def test_assign_float_field(self):
1448 field = _create_field(self._tc, self._create_fc(self._tc))
1449 raw = 101.32
1450 field.value = raw
1451 self._def.value = field
1452 self.assertEqual(self._def, raw)
1453
1454 def test_assign_invalid_type(self):
1455 with self.assertRaises(TypeError):
1456 self._def.value = 'yes'
1457
1458 def test_invalid_lshift(self):
1459 self._test_invalid_op(lambda: self._def << 23)
1460
1461 def test_invalid_rshift(self):
1462 self._test_invalid_op(lambda: self._def >> 23)
1463
1464 def test_invalid_and(self):
1465 self._test_invalid_op(lambda: self._def & 23)
1466
1467 def test_invalid_or(self):
1468 self._test_invalid_op(lambda: self._def | 23)
1469
1470 def test_invalid_xor(self):
1471 self._test_invalid_op(lambda: self._def ^ 23)
1472
1473 def test_invalid_invert(self):
1474 self._test_invalid_op(lambda: ~self._def)
1475
1476 def test_str_op(self):
1477 self.assertEqual(str(self._def), str(self._def_value))
1478
1479
1480_inject_numeric_testing_methods(RealFieldTestCase)
1481
1482
1483class StringFieldTestCase(unittest.TestCase):
1484 @staticmethod
1485 def _const_value_setter(field):
1486 field.value = 'Hello, World!'
1487
1488 def setUp(self):
1489 self._tc = get_default_trace_class()
1490 self._def_value = 'Hello, World!'
1491 self._def = _create_string_field(self._tc)
1492 self._def_const = _create_const_field(
1493 self._tc, self._tc.create_string_field_class(), self._const_value_setter
1494 )
1495 self._def.value = self._def_value
1496 self._def_new_value = 'Yes!'
1497
1498 def test_assign_int(self):
1499 with self.assertRaises(TypeError):
1500 self._def.value = 283
1501
1502 def test_assign_string_field(self):
1503 field = _create_string_field(self._tc)
1504 raw = 'zorg'
1505 field.value = raw
1506 self.assertEqual(field, raw)
1507
1508 def test_eq(self):
1509 self.assertEqual(self._def, self._def_value)
1510
1511 def test_const_eq(self):
1512 self.assertEqual(self._def_const, self._def_value)
1513
1514 def test_not_eq(self):
1515 self.assertNotEqual(self._def, 23)
1516
1517 def test_lt_vstring(self):
1518 s1 = _create_string_field(self._tc)
1519 s1.value = 'allo'
1520 s2 = _create_string_field(self._tc)
1521 s2.value = 'bateau'
1522 self.assertLess(s1, s2)
1523
1524 def test_lt_string(self):
1525 s1 = _create_string_field(self._tc)
1526 s1.value = 'allo'
1527 self.assertLess(s1, 'bateau')
1528
1529 def test_le_vstring(self):
1530 s1 = _create_string_field(self._tc)
1531 s1.value = 'allo'
1532 s2 = _create_string_field(self._tc)
1533 s2.value = 'bateau'
1534 self.assertLessEqual(s1, s2)
1535
1536 def test_le_string(self):
1537 s1 = _create_string_field(self._tc)
1538 s1.value = 'allo'
1539 self.assertLessEqual(s1, 'bateau')
1540
1541 def test_gt_vstring(self):
1542 s1 = _create_string_field(self._tc)
1543 s1.value = 'allo'
1544 s2 = _create_string_field(self._tc)
1545 s2.value = 'bateau'
1546 self.assertGreater(s2, s1)
1547
1548 def test_gt_string(self):
1549 s1 = _create_string_field(self._tc)
1550 s1.value = 'allo'
1551 self.assertGreater('bateau', s1)
1552
1553 def test_ge_vstring(self):
1554 s1 = _create_string_field(self._tc)
1555 s1.value = 'allo'
1556 s2 = _create_string_field(self._tc)
1557 s2.value = 'bateau'
1558 self.assertGreaterEqual(s2, s1)
1559
1560 def test_ge_string(self):
1561 s1 = _create_string_field(self._tc)
1562 s1.value = 'allo'
1563 self.assertGreaterEqual('bateau', s1)
1564
1565 def test_bool_op(self):
1566 self.assertEqual(bool(self._def), bool(self._def_value))
1567
1568 def test_str_op(self):
1569 self.assertEqual(str(self._def), str(self._def_value))
1570
1571 def test_len(self):
1572 self.assertEqual(len(self._def), len(self._def_value))
1573
1574 def test_getitem(self):
1575 self.assertEqual(self._def[5], self._def_value[5])
1576
1577 def test_const_getitem(self):
1578 self.assertEqual(self._def_const[5], self._def_value[5])
1579
1580 def test_append_str(self):
1581 to_append = 'meow meow meow'
1582 self._def += to_append
1583 self._def_value += to_append
1584 self.assertEqual(self._def, self._def_value)
1585
1586 def test_const_append_str(self):
1587 to_append = 'meow meow meow'
1588 with self.assertRaises(TypeError):
1589 self._def_const += to_append
1590 self.assertEqual(self._def_const, self._def_value)
1591
1592 def test_append_string_field(self):
1593 field = _create_string_field(self._tc)
1594 to_append = 'meow meow meow'
1595 field.value = to_append
1596 self._def += field
1597 self._def_value += to_append
1598 self.assertEqual(self._def, self._def_value)
1599
1600 def test_hash_op(self):
1601 with self.assertRaises(TypeError):
1602 hash(self._def)
1603
1604 def test_const_hash_op(self):
1605 self.assertEqual(hash(self._def_const), hash(self._def_value))
1606
1607 def test_const_hash_dict(self):
1608 my_dict = {}
1609 my_dict[self._def_const] = 'my_value'
1610 self.assertEqual(my_dict[self._def_value], 'my_value')
1611
1612
1613class _TestArrayFieldCommon:
1614 def _modify_def(self):
1615 self._def[2] = 23
1616
1617 def test_bool_op_true(self):
1618 self.assertTrue(self._def)
1619
1620 def test_len(self):
1621 self.assertEqual(len(self._def), 3)
1622
1623 def test_length(self):
1624 self.assertEqual(self._def.length, 3)
1625
1626 def test_getitem(self):
1627 field = self._def[1]
1628 self.assertIs(type(field), bt2._SignedIntegerField)
1629 self.assertEqual(field, 1847)
1630
1631 def test_const_getitem(self):
1632 field = self._def_const[1]
1633 self.assertIs(type(field), bt2._SignedIntegerFieldConst)
1634 self.assertEqual(field, 1847)
1635
1636 def test_eq(self):
1637 field = _create_int_array_field(self._tc, 3)
1638 field[0] = 45
1639 field[1] = 1847
1640 field[2] = 1948754
1641 self.assertEqual(self._def, field)
1642
1643 def test_eq_invalid_type(self):
1644 self.assertNotEqual(self._def, 23)
1645
1646 def test_eq_diff_len(self):
1647 field = _create_int_array_field(self._tc, 2)
1648 field[0] = 45
1649 field[1] = 1847
1650 self.assertNotEqual(self._def, field)
1651
1652 def test_eq_diff_content_same_len(self):
1653 field = _create_int_array_field(self._tc, 3)
1654 field[0] = 45
1655 field[1] = 1846
1656 field[2] = 1948754
1657 self.assertNotEqual(self._def, field)
1658
1659 def test_eq_non_sequence_iterable(self):
1660 dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)])
1661 field = _create_int_array_field(self._tc, 3)
1662 field[0] = 1
1663 field[1] = 3
1664 field[2] = 5
1665 self.assertEqual(field, list(dct.keys()))
1666 self.assertNotEqual(field, dct)
1667
1668 def test_setitem(self):
1669 self._def[2] = 24
1670 self.assertEqual(self._def[2], 24)
1671
1672 def test_setitem_int_field(self):
1673 int_fc = self._tc.create_signed_integer_field_class(32)
1674 int_field = _create_field(self._tc, int_fc)
1675 int_field.value = 19487
1676 self._def[1] = int_field
1677 self.assertEqual(self._def[1], 19487)
1678
1679 def test_setitem_non_basic_field(self):
1680 array_field = _create_struct_array_field(self._tc, 2)
1681 with self.assertRaises(TypeError):
1682 array_field[1] = 23
1683
1684 def test_setitem_none(self):
1685 with self.assertRaises(TypeError):
1686 self._def[1] = None
1687
1688 def test_setitem_index_wrong_type(self):
1689 with self.assertRaises(TypeError):
1690 self._def['yes'] = 23
1691
1692 def test_setitem_index_neg(self):
1693 with self.assertRaises(IndexError):
1694 self._def[-2] = 23
1695
1696 def test_setitem_index_out_of_range(self):
1697 with self.assertRaises(IndexError):
1698 self._def[len(self._def)] = 134679
1699
1700 def test_const_setitem(self):
1701 with self.assertRaises(TypeError):
1702 self._def_const[0] = 134679
1703
1704 def test_iter(self):
1705 for field, value in zip(self._def, (45, 1847, 1948754)):
1706 self.assertEqual(field, value)
1707
1708 def test_const_iter(self):
1709 for field, value in zip(self._def_const, (45, 1847, 1948754)):
1710 self.assertEqual(field, value)
1711
1712 def test_value_int_field(self):
1713 values = [45646, 145, 12145]
1714 self._def.value = values
1715 self.assertEqual(values, self._def)
1716
1717 def test_value_check_sequence(self):
1718 values = 42
1719 with self.assertRaises(TypeError):
1720 self._def.value = values
1721
1722 def test_value_wrong_type_in_sequence(self):
1723 values = [32, 'hello', 11]
1724 with self.assertRaises(TypeError):
1725 self._def.value = values
1726
1727 def test_value_complex_type(self):
1728 struct_fc = self._tc.create_structure_field_class()
1729 int_fc = self._tc.create_signed_integer_field_class(32)
1730 another_int_fc = self._tc.create_signed_integer_field_class(32)
1731 str_fc = self._tc.create_string_field_class()
1732 struct_fc.append_member(field_class=int_fc, name='an_int')
1733 struct_fc.append_member(field_class=str_fc, name='a_string')
1734 struct_fc.append_member(field_class=another_int_fc, name='another_int')
1735 array_fc = self._tc.create_static_array_field_class(struct_fc, 3)
1736 stream = _create_stream(self._tc, [('array_field', array_fc)])
1737 values = [
1738 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1739 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1740 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
1741 ]
1742
1743 array = stream.create_packet().context_field['array_field']
1744 array.value = values
1745 self.assertEqual(values, array)
1746 values[0]['an_int'] = 'a string'
1747 with self.assertRaises(TypeError):
1748 array.value = values
1749
1750 def test_str_op(self):
1751 s = str(self._def)
1752 expected_string = '[{}]'.format(', '.join([repr(v) for v in self._def_value]))
1753 self.assertEqual(expected_string, s)
1754
1755
1756class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase):
1757 @staticmethod
1758 def _const_value_setter(field):
1759 field.value = [45, 1847, 1948754]
1760
1761 def setUp(self):
1762 self._tc = get_default_trace_class()
1763 self._def = _create_int_array_field(self._tc, 3)
1764 self._def[0] = 45
1765 self._def[1] = 1847
1766 self._def[2] = 1948754
1767 self._def_value = [45, 1847, 1948754]
1768 self._def_const = _create_const_field(
1769 self._tc,
1770 self._tc.create_static_array_field_class(
1771 self._tc.create_signed_integer_field_class(32), 3
1772 ),
1773 self._const_value_setter,
1774 )
1775
1776 def test_value_wrong_len(self):
1777 values = [45, 1847]
1778 with self.assertRaises(ValueError):
1779 self._def.value = values
1780
1781
1782class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase):
1783 @staticmethod
1784 def _const_value_setter(field):
1785 field.value = [45, 1847, 1948754]
1786
1787 def setUp(self):
1788 self._tc = get_default_trace_class()
1789 self._def = _create_dynamic_array(self._tc)
1790 self._def[0] = 45
1791 self._def[1] = 1847
1792 self._def[2] = 1948754
1793 self._def_value = [45, 1847, 1948754]
1794 self._def_const = _create_const_field(
1795 self._tc,
1796 self._tc.create_dynamic_array_field_class(
1797 self._tc.create_signed_integer_field_class(32)
1798 ),
1799 self._const_value_setter,
1800 )
1801
1802 def test_value_resize(self):
1803 new_values = [1, 2, 3, 4]
1804 self._def.value = new_values
1805 self.assertCountEqual(self._def, new_values)
1806
1807 def test_set_length(self):
1808 self._def.length = 4
1809 self._def[3] = 0
1810 self.assertEqual(len(self._def), 4)
1811
1812 def test_const_set_length(self):
1813 with self.assertRaises(AttributeError):
1814 self._def_const.length = 4
1815 self.assertEqual(len(self._def), 3)
1816
1817 def test_set_invalid_length(self):
1818 with self.assertRaises(TypeError):
1819 self._def.length = 'cheval'
1820
1821
1822class StructureFieldTestCase(unittest.TestCase):
1823 @staticmethod
1824 def _const_value_setter(field):
1825 field.value = {
1826 'A': -1872,
1827 'B': 'salut',
1828 'C': 17.5,
1829 'D': 16497,
1830 'E': {},
1831 'F': {'F_1': 52},
1832 }
1833
1834 def _create_fc(self, tc):
1835 fc = tc.create_structure_field_class()
1836 fc.append_member('A', self._fc0_fn())
1837 fc.append_member('B', self._fc1_fn())
1838 fc.append_member('C', self._fc2_fn())
1839 fc.append_member('D', self._fc3_fn())
1840 fc.append_member('E', self._fc4_fn())
1841 fc5 = self._fc5_fn()
1842 fc5.append_member('F_1', self._fc5_inner_fn())
1843 fc.append_member('F', fc5)
1844 return fc
1845
1846 def setUp(self):
1847 self._tc = get_default_trace_class()
1848 self._fc0_fn = self._tc.create_signed_integer_field_class
1849 self._fc1_fn = self._tc.create_string_field_class
1850 self._fc2_fn = self._tc.create_real_field_class
1851 self._fc3_fn = self._tc.create_signed_integer_field_class
1852 self._fc4_fn = self._tc.create_structure_field_class
1853 self._fc5_fn = self._tc.create_structure_field_class
1854 self._fc5_inner_fn = self._tc.create_signed_integer_field_class
1855
1856 self._fc = self._create_fc(self._tc)
1857 self._def = _create_field(self._tc, self._fc)
1858 self._def['A'] = -1872
1859 self._def['B'] = 'salut'
1860 self._def['C'] = 17.5
1861 self._def['D'] = 16497
1862 self._def['E'] = {}
1863 self._def['F'] = {'F_1': 52}
1864 self._def_value = {
1865 'A': -1872,
1866 'B': 'salut',
1867 'C': 17.5,
1868 'D': 16497,
1869 'E': {},
1870 'F': {'F_1': 52},
1871 }
1872
1873 self._def_const = _create_const_field(
1874 self._tc, self._create_fc(self._tc), self._const_value_setter
1875 )
1876
1877 def _modify_def(self):
1878 self._def['B'] = 'hola'
1879
1880 def test_bool_op_true(self):
1881 self.assertTrue(self._def)
1882
1883 def test_bool_op_false(self):
1884 field = self._def['E']
1885 self.assertFalse(field)
1886
1887 def test_len(self):
1888 self.assertEqual(len(self._def), len(self._def_value))
1889
1890 def test_getitem(self):
1891 field1 = self._def['A']
1892 field2 = self._def['B']
1893 field3 = self._def['C']
1894 field4 = self._def['D']
1895 field5 = self._def['E']
1896 field6 = self._def['F']
1897
1898 self.assertIs(type(field1), bt2._SignedIntegerField)
1899 self.assertEqual(field1, -1872)
1900
1901 self.assertIs(type(field2), bt2._StringField)
1902 self.assertEqual(field2, 'salut')
1903
1904 self.assertIs(type(field3), bt2._RealField)
1905 self.assertEqual(field3, 17.5)
1906
1907 self.assertIs(type(field4), bt2._SignedIntegerField)
1908 self.assertEqual(field4, 16497)
1909
1910 self.assertIs(type(field5), bt2._StructureField)
1911 self.assertEqual(field5, {})
1912
1913 self.assertIs(type(field6), bt2._StructureField)
1914 self.assertEqual(field6, {'F_1': 52})
1915
1916 def test_const_getitem(self):
1917 field1 = self._def_const['A']
1918 field2 = self._def_const['B']
1919 field3 = self._def_const['C']
1920 field4 = self._def_const['D']
1921 field5 = self._def_const['E']
1922 field6 = self._def_const['F']
1923
1924 self.assertIs(type(field1), bt2._SignedIntegerFieldConst)
1925 self.assertEqual(field1, -1872)
1926
1927 self.assertIs(type(field2), bt2._StringFieldConst)
1928 self.assertEqual(field2, 'salut')
1929
1930 self.assertIs(type(field3), bt2._RealFieldConst)
1931 self.assertEqual(field3, 17.5)
1932
1933 self.assertIs(type(field4), bt2._SignedIntegerFieldConst)
1934 self.assertEqual(field4, 16497)
1935
1936 self.assertIs(type(field5), bt2._StructureFieldConst)
1937 self.assertEqual(field5, {})
1938
1939 self.assertIs(type(field6), bt2._StructureFieldConst)
1940 self.assertEqual(field6, {'F_1': 52})
1941
1942 def test_member_at_index_out_of_bounds_after(self):
1943 with self.assertRaises(IndexError):
1944 self._def.member_at_index(len(self._def_value))
1945
1946 def test_eq(self):
1947 field = _create_field(self._tc, self._create_fc(self._tc))
1948 field['A'] = -1872
1949 field['B'] = 'salut'
1950 field['C'] = 17.5
1951 field['D'] = 16497
1952 field['E'] = {}
1953 field['F'] = {'F_1': 52}
1954 self.assertEqual(self._def, field)
1955
1956 def test_const_eq(self):
1957 field = _create_field(self._tc, self._create_fc(self._tc))
1958 field['A'] = -1872
1959 field['B'] = 'salut'
1960 field['C'] = 17.5
1961 field['D'] = 16497
1962 field['E'] = {}
1963 field['F'] = {'F_1': 52}
1964 self.assertEqual(self._def_const, field)
1965
1966 def test_eq_invalid_type(self):
1967 self.assertNotEqual(self._def, 23)
1968
1969 def test_eq_diff_len(self):
1970 fc = self._tc.create_structure_field_class()
1971 fc.append_member('A', self._fc0_fn())
1972 fc.append_member('B', self._fc1_fn())
1973 fc.append_member('C', self._fc2_fn())
1974
1975 field = _create_field(self._tc, fc)
1976 field['A'] = -1872
1977 field['B'] = 'salut'
1978 field['C'] = 17.5
1979 self.assertNotEqual(self._def, field)
1980
1981 def test_eq_diff_keys(self):
1982 fc = self._tc.create_structure_field_class()
1983 fc.append_member('U', self._fc0_fn())
1984 fc.append_member('V', self._fc1_fn())
1985 fc.append_member('W', self._fc2_fn())
1986 fc.append_member('X', self._fc3_fn())
1987 fc.append_member('Y', self._fc4_fn())
1988 fc.append_member('Z', self._fc5_fn())
1989 field = _create_field(self._tc, fc)
1990 field['U'] = -1871
1991 field['V'] = "gerry"
1992 field['W'] = 18.19
1993 field['X'] = 16497
1994 field['Y'] = {}
1995 field['Z'] = {}
1996 self.assertNotEqual(self._def, field)
1997
1998 def test_eq_diff_content_same_len(self):
1999 field = _create_field(self._tc, self._create_fc(self._tc))
2000 field['A'] = -1872
2001 field['B'] = 'salut'
2002 field['C'] = 17.4
2003 field['D'] = 16497
2004 field['E'] = {}
2005 field['F'] = {'F_1': 0}
2006 self.assertNotEqual(self._def, field)
2007
2008 def test_eq_same_content_diff_keys(self):
2009 fc = self._tc.create_structure_field_class()
2010 fc.append_member('A', self._fc0_fn())
2011 fc.append_member('B', self._fc1_fn())
2012 fc.append_member('E', self._fc2_fn())
2013 fc.append_member('D', self._fc3_fn())
2014 fc.append_member('C', self._fc4_fn())
2015 fc.append_member('F', self._fc5_fn())
2016 field = _create_field(self._tc, fc)
2017 field['A'] = -1872
2018 field['B'] = 'salut'
2019 field['E'] = 17.5
2020 field['D'] = 16497
2021 field['C'] = {}
2022 field['F'] = {}
2023 self.assertNotEqual(self._def, field)
2024
2025 def test_setitem(self):
2026 self._def['C'] = -18.47
2027 self.assertEqual(self._def['C'], -18.47)
2028
2029 def test_const_setitem(self):
2030 with self.assertRaises(TypeError):
2031 self._def_const['A'] = 134679
2032
2033 def test_setitem_int_field(self):
2034 int_fc = self._tc.create_signed_integer_field_class(32)
2035 int_field = _create_field(self._tc, int_fc)
2036 int_field.value = 19487
2037 self._def['D'] = int_field
2038 self.assertEqual(self._def['D'], 19487)
2039
2040 def test_setitem_non_basic_field(self):
2041 elem_fc = self._tc.create_structure_field_class()
2042 struct_fc = self._tc.create_structure_field_class()
2043 struct_fc.append_member('A', elem_fc)
2044 struct_field = _create_field(self._tc, struct_fc)
2045
2046 # Will fail on access to .items() of the value
2047 with self.assertRaises(AttributeError):
2048 struct_field['A'] = 23
2049
2050 def test_setitem_none(self):
2051 with self.assertRaises(TypeError):
2052 self._def['C'] = None
2053
2054 def test_setitem_key_wrong_type(self):
2055 with self.assertRaises(TypeError):
2056 self._def[3] = 23
2057
2058 def test_setitem_wrong_key(self):
2059 with self.assertRaises(KeyError):
2060 self._def['hi'] = 134679
2061
2062 def test_member_at_index(self):
2063 self.assertEqual(self._def.member_at_index(1), 'salut')
2064
2065 def test_const_member_at_index(self):
2066 self.assertEqual(self._def_const.member_at_index(1), 'salut')
2067
2068 def test_iter(self):
2069 orig_values = {
2070 'A': -1872,
2071 'B': 'salut',
2072 'C': 17.5,
2073 'D': 16497,
2074 'E': {},
2075 'F': {'F_1': 52},
2076 }
2077
2078 for vkey, vval in self._def.items():
2079 val = orig_values[vkey]
2080 self.assertEqual(vval, val)
2081
2082 def test_value(self):
2083 orig_values = {
2084 'A': -1872,
2085 'B': 'salut',
2086 'C': 17.5,
2087 'D': 16497,
2088 'E': {},
2089 'F': {'F_1': 52},
2090 }
2091 self.assertEqual(self._def, orig_values)
2092
2093 def test_set_value(self):
2094 int_fc = self._tc.create_signed_integer_field_class(32)
2095 another_int_fc = self._tc.create_signed_integer_field_class(32)
2096 str_fc = self._tc.create_string_field_class()
2097 struct_fc = self._tc.create_structure_field_class()
2098 struct_fc.append_member(field_class=int_fc, name='an_int')
2099 struct_fc.append_member(field_class=str_fc, name='a_string')
2100 struct_fc.append_member(field_class=another_int_fc, name='another_int')
2101 values = {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
2102
2103 struct = _create_field(self._tc, struct_fc)
2104 struct.value = values
2105 self.assertEqual(values, struct)
2106
2107 bad_type_values = copy.deepcopy(values)
2108 bad_type_values['an_int'] = 'a string'
2109 with self.assertRaises(TypeError):
2110 struct.value = bad_type_values
2111
2112 unknown_key_values = copy.deepcopy(values)
2113 unknown_key_values['unknown_key'] = 16546
2114 with self.assertRaises(KeyError):
2115 struct.value = unknown_key_values
2116
2117 def test_str_op(self):
2118 expected_string_found = False
2119 s = str(self._def)
2120 # Establish all permutations of the three expected matches since
2121 # the order in which mappings are enumerated is not explicitly part of
2122 # the API.
2123 for p in itertools.permutations([(k, v) for k, v in self._def.items()]):
2124 items = ['{}: {}'.format(repr(k), repr(v)) for k, v in p]
2125 candidate = '{{{}}}'.format(', '.join(items))
2126 if candidate == s:
2127 expected_string_found = True
2128 break
2129
2130 self.assertTrue(expected_string_found)
2131
2132
2133class OptionFieldTestCase(unittest.TestCase):
2134 @staticmethod
2135 def _const_value_setter(field):
2136 field.value = {'opt_field': 'hiboux'}
2137
2138 def _create_fc(self, tc):
2139 fc = tc.create_option_field_class(tc.create_string_field_class())
2140 top_fc = tc.create_structure_field_class()
2141 top_fc.append_member('opt_field', fc)
2142 return top_fc
2143
2144 def setUp(self):
2145 self._tc = get_default_trace_class()
2146 fld = _create_field(self._tc, self._create_fc(self._tc))
2147 self._def = fld['opt_field']
2148 self._def_value = 'hiboux'
2149 self._def_const = _create_const_field(
2150 self._tc, self._create_fc(self._tc), self._const_value_setter
2151 )['opt_field']
2152
2153 def test_value_prop(self):
2154 self._def.value = 'hiboux'
2155 self.assertEqual(self._def.field, 'hiboux')
2156 self.assertIs(type(self._def), bt2._OptionField)
2157 self.assertIs(type(self._def.field), bt2._StringField)
2158 self.assertTrue(self._def.has_field)
2159
2160 def test_const_value_prop(self):
2161 self.assertEqual(self._def_const.field, 'hiboux')
2162 self.assertIs(type(self._def_const), bt2._OptionFieldConst)
2163 self.assertIs(type(self._def_const.field), bt2._StringFieldConst)
2164 self.assertTrue(self._def_const.has_field)
2165
2166 def test_has_field_prop_true(self):
2167 self._def.has_field = True
2168 self.assertTrue(self._def.has_field)
2169
2170 def test_has_field_prop_true(self):
2171 self._def.has_field = False
2172 self.assertFalse(self._def.has_field)
2173
2174 def test_bool_op_true(self):
2175 self._def.value = 'allo'
2176 self.assertTrue(self._def)
2177
2178 def test_bool_op_true(self):
2179 self._def.has_field = False
2180 self.assertFalse(self._def)
2181
2182 def test_field_prop_existing(self):
2183 self._def.value = 'meow'
2184 field = self._def.field
2185 self.assertEqual(field, 'meow')
2186
2187 def test_field_prop_none(self):
2188 self._def.has_field = False
2189 field = self._def.field
2190 self.assertIsNone(field)
2191
2192 def test_const_field_prop(self):
2193 with self.assertRaises(AttributeError):
2194 self._def_const.has_field = False
2195
2196 self.assertEqual(self._def_const, self._def_value)
2197 self.assertTrue(self._def_const.has_field)
2198
2199 def test_field_prop_existing_then_none(self):
2200 self._def.value = 'meow'
2201 field = self._def.field
2202 self.assertEqual(field, 'meow')
2203 self._def.has_field = False
2204 field = self._def.field
2205 self.assertIsNone(field)
2206
2207 def test_eq(self):
2208 field = _create_field(self._tc, self._create_fc(self._tc))
2209 field = field['opt_field']
2210 field.value = 'walk'
2211 self._def.value = 'walk'
2212 self.assertEqual(self._def, field)
2213
2214 def test_const_eq(self):
2215 field = _create_field(self._tc, self._create_fc(self._tc))
2216 field = field['opt_field']
2217 field.value = 'hiboux'
2218 self.assertEqual(self._def_const, field)
2219 self.assertEqual(self._def_const, self._def_value)
2220
2221 def test_eq_invalid_type(self):
2222 self._def.value = 'gerry'
2223 self.assertNotEqual(self._def, 23)
2224
2225 def test_str_op(self):
2226 self._def.value = 'marcel'
2227 self.assertEqual(str(self._def), str(self._def.field))
2228
2229 def test_repr_op(self):
2230 self._def.value = 'mireille'
2231 self.assertEqual(repr(self._def), repr(self._def.field))
2232
2233
2234class VariantFieldTestCase(unittest.TestCase):
2235 @staticmethod
2236 def _const_value_setter(field):
2237 field.selected_option_index = 3
2238 field.value = 1334
2239
2240 def _create_fc(self, tc):
2241 ft0 = tc.create_signed_integer_field_class(32)
2242 ft1 = tc.create_string_field_class()
2243 ft2 = tc.create_real_field_class()
2244 ft3 = tc.create_signed_integer_field_class(17)
2245 fc = tc.create_variant_field_class()
2246 fc.append_option('corner', ft0)
2247 fc.append_option('zoom', ft1)
2248 fc.append_option('mellotron', ft2)
2249 fc.append_option('giorgio', ft3)
2250 top_fc = tc.create_structure_field_class()
2251 top_fc.append_member('variant_field', fc)
2252 return top_fc
2253
2254 def setUp(self):
2255 self._tc = get_default_trace_class()
2256 fld = _create_field(self._tc, self._create_fc(self._tc))
2257 self._def = fld['variant_field']
2258
2259 self._def_value = 1334
2260 self._def_selected_index = 3
2261 const_fc = self._create_fc(self._tc)['variant_field']
2262
2263 fld_const = _create_const_field(
2264 self._tc, const_fc.field_class, self._const_value_setter
2265 )
2266 self._def_const = fld_const
2267
2268 def test_bool_op(self):
2269 self._def.selected_option_index = 2
2270 self._def.value = -17.34
2271 with self.assertRaises(NotImplementedError):
2272 bool(self._def)
2273
2274 def test_selected_option_index(self):
2275 self._def.selected_option_index = 2
2276 self.assertEqual(self._def.selected_option_index, 2)
2277
2278 def test_selected_option_index_above_range(self):
2279 with self.assertRaises(IndexError):
2280 self._def.selected_option_index = 4
2281
2282 def test_selected_option_index_below_range(self):
2283 with self.assertRaises(IndexError):
2284 self._def.selected_option_index = -1
2285
2286 def test_const_selected_option_index(self):
2287 with self.assertRaises(AttributeError):
2288 self._def_const.selected_option_index = 2
2289 self.assertEqual(self._def_const.selected_option_index, 3)
2290
2291 def test_selected_option(self):
2292 self._def.selected_option_index = 2
2293 self._def.value = -17.34
2294 self.assertEqual(self._def.selected_option, -17.34)
2295 self.assertEqual(type(self._def.selected_option), bt2._RealField)
2296
2297 self._def.selected_option_index = 3
2298 self._def.value = 1921
2299 self.assertEqual(self._def.selected_option, 1921)
2300 self.assertEqual(type(self._def.selected_option), bt2._SignedIntegerField)
2301
2302 def test_const_selected_option(self):
2303 self.assertEqual(self._def_const.selected_option, 1334)
2304 self.assertEqual(
2305 type(self._def_const.selected_option), bt2._SignedIntegerFieldConst
2306 )
2307
2308 def test_eq(self):
2309 field = _create_field(self._tc, self._create_fc(self._tc))
2310 field = field['variant_field']
2311 field.selected_option_index = 0
2312 field.value = 1774
2313 self._def.selected_option_index = 0
2314 self._def.value = 1774
2315 self.assertEqual(self._def, field)
2316
2317 def test_const_eq(self):
2318 field = _create_field(self._tc, self._create_fc(self._tc))
2319 field = field['variant_field']
2320 field.selected_option_index = 3
2321 field.value = 1334
2322 self.assertEqual(self._def_const, field)
2323
2324 def test_len(self):
2325 self.assertEqual(len(self._def), 4)
2326
2327 def test_eq_invalid_type(self):
2328 self._def.selected_option_index = 1
2329 self._def.value = 'gerry'
2330 self.assertNotEqual(self._def, 23)
2331
2332 def test_str_op_int(self):
2333 field = _create_field(self._tc, self._create_fc(self._tc))
2334 field = field['variant_field']
2335 field.selected_option_index = 0
2336 field.value = 1774
2337 other_field = _create_field(self._tc, self._create_fc(self._tc))
2338 other_field = other_field['variant_field']
2339 other_field.selected_option_index = 0
2340 other_field.value = 1774
2341 self.assertEqual(str(field), str(other_field))
2342
2343 def test_str_op_str(self):
2344 field = _create_field(self._tc, self._create_fc(self._tc))
2345 field = field['variant_field']
2346 field.selected_option_index = 1
2347 field.value = 'un beau grand bateau'
2348 other_field = _create_field(self._tc, self._create_fc(self._tc))
2349 other_field = other_field['variant_field']
2350 other_field.selected_option_index = 1
2351 other_field.value = 'un beau grand bateau'
2352 self.assertEqual(str(field), str(other_field))
2353
2354 def test_str_op_float(self):
2355 field = _create_field(self._tc, self._create_fc(self._tc))
2356 field = field['variant_field']
2357 field.selected_option_index = 2
2358 field.value = 14.4245
2359 other_field = _create_field(self._tc, self._create_fc(self._tc))
2360 other_field = other_field['variant_field']
2361 other_field.selected_option_index = 2
2362 other_field.value = 14.4245
2363 self.assertEqual(str(field), str(other_field))
2364
2365
2366if __name__ == '__main__':
2367 unittest.main()
This page took 0.050394 seconds and 4 git commands to generate.