bt2: add `if __name__ == '__main__'` snippet to all tests
[babeltrace.git] / tests / bindings / python / bt2 / test_field.py
CommitLineData
d2d857a8
MJ
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
9cf643d1
PP
19from functools import partial, partialmethod
20import operator
21import unittest
9cf643d1
PP
22import math
23import copy
b0bdda42 24import itertools
73051d46 25import collections
9cf643d1 26import bt2
f0a42b33 27from utils import get_default_trace_class, TestOutputPortMessageIterator
9cf643d1
PP
28
29
cfbd7cf3 30_COMP_BINOPS = (operator.eq, operator.ne)
9cf643d1
PP
31
32
1eccc498
SM
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
cfbd7cf3 38
1eccc498
SM
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()
cfbd7cf3
FD
45 stream_class = tc.create_stream_class(
46 packet_context_field_class=packet_context_fc, supports_packets=True
47 )
1eccc498
SM
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
cfbd7cf3 58
1eccc498
SM
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
f0a42b33
FD
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
1eccc498
SM
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
cfbd7cf3 115
1eccc498
SM
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
cfbd7cf3 129
1eccc498
SM
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
cfbd7cf3 145
1eccc498
SM
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
cfbd7cf3 163
1eccc498
SM
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
ead8c3d4
PP
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
21368027
PP
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`.
1eccc498 226class _TestNumericField:
21368027
PP
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.
9cf643d1 238 def _binop(self, op, rhs):
21368027
PP
239 type_rexc = None
240 type_rvexc = None
9cf643d1
PP
241 comp_value = rhs
242
21368027 243 # try with field object
9cf643d1
PP
244 try:
245 r = op(self._def, rhs)
246 except Exception as e:
21368027 247 type_rexc = type(e)
9cf643d1 248
21368027 249 # try with value
9cf643d1
PP
250 try:
251 rv = op(self._def_value, comp_value)
252 except Exception as e:
21368027 253 type_rvexc = type(e)
9cf643d1 254
21368027 255 if type_rexc is not None or type_rvexc is not None:
9cf643d1
PP
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.)
21368027 260 self.assertIs(type_rexc, type_rvexc)
9cf643d1
PP
261 return None, None
262
263 return r, rv
264
21368027
PP
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.
9cf643d1 275 def _unaryop(self, op):
21368027
PP
276 type_rexc = None
277 type_rvexc = None
9cf643d1 278
21368027 279 # try with field object
9cf643d1
PP
280 try:
281 r = op(self._def)
282 except Exception as e:
21368027 283 type_rexc = type(e)
9cf643d1 284
21368027 285 # try with value
9cf643d1
PP
286 try:
287 rv = op(self._def_value)
288 except Exception as e:
21368027 289 type_rvexc = type(e)
9cf643d1 290
21368027 291 if type_rexc is not None or type_rvexc is not None:
9cf643d1
PP
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.)
21368027 296 self.assertIs(type_rexc, type_rvexc)
9cf643d1
PP
297 return None, None
298
299 return r, rv
300
21368027
PP
301 # Tests that the unary operation `op` gives results with the same
302 # type for both `self._def` and `self._def_value`.
9cf643d1
PP
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
21368027
PP
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`.
9cf643d1
PP
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
21368027
PP
322 # Tests that the unary operation `op`, when applied to `self._def`,
323 # does not change its underlying BT object address.
9cf643d1
PP
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
21368027
PP
329 # Tests that the unary operation `op`, when applied to `self._def`,
330 # does not change its value.
9cf643d1 331 def _test_unaryop_value_same(self, op):
e1c6bebd 332 value_before = copy.copy(self._def_value)
9cf643d1 333 self._unaryop(op)
e1c6bebd 334 self.assertEqual(self._def, value_before)
9cf643d1 335
21368027
PP
336 # Tests that the binary operation `op` gives results with the same
337 # type for both `self._def` and `self._def_value`.
9cf643d1
PP
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
21368027
PP
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`.
9cf643d1
PP
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
21368027
PP
361 # Tests that the binary operation `op`, when applied to `self._def`,
362 # does not change its underlying BT object address.
9cf643d1
PP
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
21368027
PP
368 # Tests that the binary operation `op`, when applied to `self._def`,
369 # does not change its value.
1eccc498 370 @unittest.skip('copy is not implemented')
9cf643d1 371 def _test_binop_lhs_value_same(self, op, rhs):
e1c6bebd 372 value_before = copy.copy(self._def)
9cf643d1 373 r, rv = self._binop(op, rhs)
e1c6bebd 374 self.assertEqual(self._def, value_before)
9cf643d1 375
21368027
PP
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
9cf643d1
PP
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
9cf643d1
PP
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
00512c97 450 def _test_binop_rhs_complex(self, test_cb, op):
cfbd7cf3 451 test_cb(op, -23 + 19j)
00512c97
PP
452
453 def _test_binop_rhs_zero_complex(self, test_cb, op):
454 test_cb(op, 0j)
455
9cf643d1
PP
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
00512c97
PP
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
9cf643d1
PP
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
00512c97
PP
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
9cf643d1
PP
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
00512c97
PP
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
9cf643d1
PP
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
00512c97
PP
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
9cf643d1
PP
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
dda659b3
FD
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
9cf643d1 675 def test_eq_none(self):
1eccc498
SM
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).
cfbd7cf3 679 self.assertFalse(self._def == None) # noqa: E711
9cf643d1
PP
680
681 def test_ne_none(self):
1eccc498
SM
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).
cfbd7cf3 685 self.assertTrue(self._def != None) # noqa: E711
742e4747 686
9cf643d1 687
21368027
PP
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.
9cf643d1
PP
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
21368027
PP
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.
9cf643d1
PP
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
21368027
PP
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.
9cf643d1
PP
762def _inject_numeric_testing_methods(cls):
763 def test_binop_name(suffix):
764 return 'test_binop_{}_{}'.format(name, suffix)
765
9cf643d1
PP
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:
cfbd7cf3
FD
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 )
9cf643d1
PP
1157
1158 # inject testing methods for each unary operation
1159 for name, unaryop in _UNARYOPS:
cfbd7cf3
FD
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 )
9cf643d1 1180
9cf643d1 1181
aae30e61 1182class BoolFieldTestCase(_TestNumericField, unittest.TestCase):
f0a42b33
FD
1183 @staticmethod
1184 def _const_value_setter(field):
1185 field.value = True
1186
aae30e61
PP
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
f0a42b33
FD
1195 self._def_const = _create_const_field(
1196 self._tc, self._tc.create_bool_field_class(), self._const_value_setter
1197 )
aae30e61
PP
1198 self._def_new_value = False
1199
f0a42b33
FD
1200 def test_classes(self):
1201 self.assertIs(type(self._def), bt2._BoolField)
1202 self.assertIs(type(self._def_const), bt2._BoolFieldConst)
1203
aae30e61
PP
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
9cf643d1
PP
1239class _TestIntegerFieldCommon(_TestNumericField):
1240 def test_assign_true(self):
1241 raw = True
1242 self._def.value = raw
1243 self.assertEqual(self._def, raw)
9cf643d1
PP
1244
1245 def test_assign_false(self):
1246 raw = False
1247 self._def.value = raw
1248 self.assertEqual(self._def, raw)
9cf643d1
PP
1249
1250 def test_assign_pos_int(self):
1251 raw = 477
1252 self._def.value = raw
1253 self.assertEqual(self._def, raw)
9cf643d1
PP
1254
1255 def test_assign_neg_int(self):
1256 raw = -13
1257 self._def.value = raw
1258 self.assertEqual(self._def, raw)
9cf643d1
PP
1259
1260 def test_assign_int_field(self):
1261 raw = 999
1eccc498 1262 field = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
1263 field.value = raw
1264 self._def.value = field
1265 self.assertEqual(self._def, raw)
9cf643d1 1266
9cf643d1
PP
1267 def test_assign_invalid_type(self):
1268 with self.assertRaises(TypeError):
1269 self._def.value = 'yes'
1270
1271 def test_assign_uint(self):
1eccc498
SM
1272 uint_fc = self._tc.create_unsigned_integer_field_class(32)
1273 field = _create_field(self._tc, uint_fc)
9cf643d1
PP
1274 raw = 1777
1275 field.value = 1777
1276 self.assertEqual(field, raw)
9cf643d1 1277
7bb4180f
FD
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.
cfbd7cf3
FD
1283 raw = (2 ** 53) + 1
1284 field.value = (2 ** 53) + 1
7bb4180f
FD
1285 self.assertEqual(field, raw)
1286
9cf643d1 1287 def test_assign_uint_invalid_neg(self):
1eccc498
SM
1288 uint_fc = self._tc.create_unsigned_integer_field_class(32)
1289 field = _create_field(self._tc, uint_fc)
9cf643d1
PP
1290
1291 with self.assertRaises(ValueError):
1292 field.value = -23
1293
b0bdda42
JG
1294 def test_str_op(self):
1295 self.assertEqual(str(self._def), str(self._def_value))
1296
9cf643d1
PP
1297
1298_inject_numeric_testing_methods(_TestIntegerFieldCommon)
1299
1300
1eccc498 1301class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
dda659b3
FD
1302 @staticmethod
1303 def _const_value_setter(field):
1304 field.value = 17
1305
1eccc498
SM
1306 def _create_fc(self, tc):
1307 return tc.create_signed_integer_field_class(25)
1308
9cf643d1 1309 def setUp(self):
1eccc498
SM
1310 self._tc = get_default_trace_class()
1311 self._field = _create_field(self._tc, self._create_fc(self._tc))
1312 self._field.value = 17
1313 self._def = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
1314 self._def.value = 17
1315 self._def_value = 17
dda659b3
FD
1316 self._def_const = _create_const_field(
1317 self._tc, self._create_fc(self._tc), self._const_value_setter
1318 )
9cf643d1
PP
1319 self._def_new_value = -101
1320
811644b8 1321
1eccc498 1322class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
dda659b3
FD
1323 @staticmethod
1324 def _const_value_setter(field):
1325 field.value = 17
1326
1eccc498
SM
1327 def _create_fc(self, tc):
1328 fc = tc.create_signed_enumeration_field_class(32)
45c51519
PP
1329 fc.add_mapping('something', bt2.SignedIntegerRangeSet([(17, 17)]))
1330 fc.add_mapping('speaker', bt2.SignedIntegerRangeSet([(12, 16)]))
1331 fc.add_mapping('can', bt2.SignedIntegerRangeSet([(18, 2540)]))
cfbd7cf3
FD
1332 fc.add_mapping(
1333 'whole range', bt2.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)])
1334 )
45c51519 1335 fc.add_mapping('zip', bt2.SignedIntegerRangeSet([(-45, 1001)]))
1eccc498 1336 return fc
9cf643d1 1337
9cf643d1 1338 def setUp(self):
1eccc498
SM
1339 self._tc = get_default_trace_class()
1340 self._field = _create_field(self._tc, self._create_fc(self._tc))
1341 self._def = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
1342 self._def.value = 17
1343 self._def_value = 17
dda659b3
FD
1344 self._def_const = _create_const_field(
1345 self._tc, self._create_fc(self._tc), self._const_value_setter
1346 )
9cf643d1
PP
1347 self._def_new_value = -101
1348
b0bdda42
JG
1349 def test_str_op(self):
1350 expected_string_found = False
1351 s = str(self._def)
1352
1353 # Establish all permutations of the three expected matches since
1354 # the order in which mappings are enumerated is not explicitly part of
1355 # the API.
cfbd7cf3 1356 for p in itertools.permutations(['whole range', 'something', 'zip']):
b0bdda42
JG
1357 candidate = '{} ({})'.format(self._def_value, ', '.join(p))
1358 if candidate == s:
1359 expected_string_found = True
1360 break
1361
1362 self.assertTrue(expected_string_found)
1363
1eccc498
SM
1364 def test_labels(self):
1365 self._field.value = 17
1366 labels = sorted(self._field.labels)
1367 self.assertEqual(labels, ['something', 'whole range', 'zip'])
1368
b0bdda42 1369
1eccc498 1370class RealFieldTestCase(_TestNumericField, unittest.TestCase):
dda659b3
FD
1371 @staticmethod
1372 def _const_value_setter(field):
1373 field.value = 52.7
1374
1eccc498
SM
1375 def _create_fc(self, tc):
1376 return tc.create_real_field_class()
9cf643d1 1377
9cf643d1 1378 def setUp(self):
1eccc498
SM
1379 self._tc = get_default_trace_class()
1380 self._field = _create_field(self._tc, self._create_fc(self._tc))
1381 self._def = _create_field(self._tc, self._create_fc(self._tc))
dda659b3
FD
1382 self._def_const = _create_const_field(
1383 self._tc, self._tc.create_real_field_class(), self._const_value_setter
1384 )
9cf643d1
PP
1385 self._def.value = 52.7
1386 self._def_value = 52.7
1387 self._def_new_value = -17.164857
1388
1389 def _test_invalid_op(self, cb):
1390 with self.assertRaises(TypeError):
1391 cb()
1392
1393 def test_assign_true(self):
1394 self._def.value = True
1395 self.assertTrue(self._def)
9cf643d1
PP
1396
1397 def test_assign_false(self):
1398 self._def.value = False
1399 self.assertFalse(self._def)
9cf643d1
PP
1400
1401 def test_assign_pos_int(self):
1402 raw = 477
1403 self._def.value = raw
1404 self.assertEqual(self._def, float(raw))
9cf643d1
PP
1405
1406 def test_assign_neg_int(self):
1407 raw = -13
1408 self._def.value = raw
1409 self.assertEqual(self._def, float(raw))
9cf643d1
PP
1410
1411 def test_assign_int_field(self):
1eccc498
SM
1412 int_fc = self._tc.create_signed_integer_field_class(32)
1413 int_field = _create_field(self._tc, int_fc)
9cf643d1 1414 raw = 999
1eccc498
SM
1415 int_field.value = raw
1416 self._def.value = int_field
9cf643d1 1417 self.assertEqual(self._def, float(raw))
9cf643d1
PP
1418
1419 def test_assign_float(self):
1420 raw = -19.23
1421 self._def.value = raw
1422 self.assertEqual(self._def, raw)
9cf643d1
PP
1423
1424 def test_assign_float_field(self):
1eccc498 1425 field = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
1426 raw = 101.32
1427 field.value = raw
1428 self._def.value = field
1429 self.assertEqual(self._def, raw)
9cf643d1
PP
1430
1431 def test_assign_invalid_type(self):
1432 with self.assertRaises(TypeError):
1433 self._def.value = 'yes'
1434
1435 def test_invalid_lshift(self):
1436 self._test_invalid_op(lambda: self._def << 23)
1437
1438 def test_invalid_rshift(self):
1439 self._test_invalid_op(lambda: self._def >> 23)
1440
1441 def test_invalid_and(self):
1442 self._test_invalid_op(lambda: self._def & 23)
1443
1444 def test_invalid_or(self):
1445 self._test_invalid_op(lambda: self._def | 23)
1446
1447 def test_invalid_xor(self):
1448 self._test_invalid_op(lambda: self._def ^ 23)
1449
1450 def test_invalid_invert(self):
1451 self._test_invalid_op(lambda: ~self._def)
1452
b0bdda42
JG
1453 def test_str_op(self):
1454 self.assertEqual(str(self._def), str(self._def_value))
1455
9cf643d1 1456
1eccc498 1457_inject_numeric_testing_methods(RealFieldTestCase)
9cf643d1
PP
1458
1459
1eccc498 1460class StringFieldTestCase(unittest.TestCase):
f0a42b33
FD
1461 @staticmethod
1462 def _const_value_setter(field):
1463 field.value = 'Hello, World!'
1464
9cf643d1 1465 def setUp(self):
1eccc498 1466 self._tc = get_default_trace_class()
9cf643d1 1467 self._def_value = 'Hello, World!'
1eccc498 1468 self._def = _create_string_field(self._tc)
f0a42b33
FD
1469 self._def_const = _create_const_field(
1470 self._tc, self._tc.create_string_field_class(), self._const_value_setter
1471 )
9cf643d1
PP
1472 self._def.value = self._def_value
1473 self._def_new_value = 'Yes!'
1474
1475 def test_assign_int(self):
1476 with self.assertRaises(TypeError):
1477 self._def.value = 283
1478
9cf643d1 1479 def test_assign_string_field(self):
1eccc498 1480 field = _create_string_field(self._tc)
9cf643d1
PP
1481 raw = 'zorg'
1482 field.value = raw
1483 self.assertEqual(field, raw)
1484
1485 def test_eq(self):
1486 self.assertEqual(self._def, self._def_value)
1487
f0a42b33
FD
1488 def test_const_eq(self):
1489 self.assertEqual(self._def_const, self._def_value)
1490
1eccc498 1491 def test_not_eq(self):
9cf643d1
PP
1492 self.assertNotEqual(self._def, 23)
1493
1494 def test_lt_vstring(self):
1eccc498 1495 s1 = _create_string_field(self._tc)
9cf643d1 1496 s1.value = 'allo'
1eccc498 1497 s2 = _create_string_field(self._tc)
9cf643d1
PP
1498 s2.value = 'bateau'
1499 self.assertLess(s1, s2)
1500
1501 def test_lt_string(self):
1eccc498 1502 s1 = _create_string_field(self._tc)
9cf643d1
PP
1503 s1.value = 'allo'
1504 self.assertLess(s1, 'bateau')
1505
1506 def test_le_vstring(self):
1eccc498 1507 s1 = _create_string_field(self._tc)
9cf643d1 1508 s1.value = 'allo'
1eccc498 1509 s2 = _create_string_field(self._tc)
9cf643d1
PP
1510 s2.value = 'bateau'
1511 self.assertLessEqual(s1, s2)
1512
1513 def test_le_string(self):
1eccc498 1514 s1 = _create_string_field(self._tc)
9cf643d1
PP
1515 s1.value = 'allo'
1516 self.assertLessEqual(s1, 'bateau')
1517
1518 def test_gt_vstring(self):
1eccc498 1519 s1 = _create_string_field(self._tc)
9cf643d1 1520 s1.value = 'allo'
1eccc498 1521 s2 = _create_string_field(self._tc)
9cf643d1
PP
1522 s2.value = 'bateau'
1523 self.assertGreater(s2, s1)
1524
1525 def test_gt_string(self):
1eccc498 1526 s1 = _create_string_field(self._tc)
9cf643d1
PP
1527 s1.value = 'allo'
1528 self.assertGreater('bateau', s1)
1529
1530 def test_ge_vstring(self):
1eccc498 1531 s1 = _create_string_field(self._tc)
9cf643d1 1532 s1.value = 'allo'
1eccc498 1533 s2 = _create_string_field(self._tc)
9cf643d1
PP
1534 s2.value = 'bateau'
1535 self.assertGreaterEqual(s2, s1)
1536
1537 def test_ge_string(self):
1eccc498 1538 s1 = _create_string_field(self._tc)
9cf643d1
PP
1539 s1.value = 'allo'
1540 self.assertGreaterEqual('bateau', s1)
1541
1542 def test_bool_op(self):
1543 self.assertEqual(bool(self._def), bool(self._def_value))
1544
1545 def test_str_op(self):
1546 self.assertEqual(str(self._def), str(self._def_value))
1547
1548 def test_len(self):
1549 self.assertEqual(len(self._def), len(self._def_value))
1550
1551 def test_getitem(self):
1552 self.assertEqual(self._def[5], self._def_value[5])
1553
f0a42b33
FD
1554 def test_const_getitem(self):
1555 self.assertEqual(self._def_const[5], self._def_value[5])
1556
9cf643d1
PP
1557 def test_append_str(self):
1558 to_append = 'meow meow meow'
1559 self._def += to_append
1560 self._def_value += to_append
1561 self.assertEqual(self._def, self._def_value)
1562
f0a42b33
FD
1563 def test_const_append_str(self):
1564 to_append = 'meow meow meow'
1565 with self.assertRaises(TypeError):
1566 self._def_const += to_append
1567 self.assertEqual(self._def_const, self._def_value)
1568
9cf643d1 1569 def test_append_string_field(self):
1eccc498 1570 field = _create_string_field(self._tc)
9cf643d1
PP
1571 to_append = 'meow meow meow'
1572 field.value = to_append
1573 self._def += field
1574 self._def_value += to_append
1575 self.assertEqual(self._def, self._def_value)
1576
dda659b3
FD
1577 def test_hash_op(self):
1578 with self.assertRaises(TypeError):
1579 hash(self._def)
1580
1581 def test_const_hash_op(self):
1582 self.assertEqual(hash(self._def_const), hash(self._def_value))
1583
1584 def test_const_hash_dict(self):
1585 my_dict = {}
1586 my_dict[self._def_const] = 'my_value'
1587 self.assertEqual(my_dict[self._def_value], 'my_value')
1588
742e4747 1589
1eccc498 1590class _TestArrayFieldCommon:
9cf643d1
PP
1591 def _modify_def(self):
1592 self._def[2] = 23
1593
1594 def test_bool_op_true(self):
1595 self.assertTrue(self._def)
1596
1597 def test_len(self):
1598 self.assertEqual(len(self._def), 3)
1599
1eccc498
SM
1600 def test_length(self):
1601 self.assertEqual(self._def.length, 3)
1602
9cf643d1
PP
1603 def test_getitem(self):
1604 field = self._def[1]
3fb99a22 1605 self.assertIs(type(field), bt2._SignedIntegerField)
9cf643d1
PP
1606 self.assertEqual(field, 1847)
1607
f0a42b33
FD
1608 def test_const_getitem(self):
1609 field = self._def_const[1]
1610 self.assertIs(type(field), bt2._SignedIntegerFieldConst)
1611 self.assertEqual(field, 1847)
1612
9cf643d1 1613 def test_eq(self):
1eccc498 1614 field = _create_int_array_field(self._tc, 3)
9cf643d1
PP
1615 field[0] = 45
1616 field[1] = 1847
1617 field[2] = 1948754
1618 self.assertEqual(self._def, field)
1619
1620 def test_eq_invalid_type(self):
1621 self.assertNotEqual(self._def, 23)
1622
1623 def test_eq_diff_len(self):
1eccc498 1624 field = _create_int_array_field(self._tc, 2)
9cf643d1
PP
1625 field[0] = 45
1626 field[1] = 1847
1627 self.assertNotEqual(self._def, field)
1628
1629 def test_eq_diff_content_same_len(self):
1eccc498 1630 field = _create_int_array_field(self._tc, 3)
9cf643d1
PP
1631 field[0] = 45
1632 field[1] = 1846
1633 field[2] = 1948754
1634 self.assertNotEqual(self._def, field)
1635
73051d46
PP
1636 def test_eq_non_sequence_iterable(self):
1637 dct = collections.OrderedDict([(1, 2), (3, 4), (5, 6)])
1638 field = _create_int_array_field(self._tc, 3)
1639 field[0] = 1
1640 field[1] = 3
1641 field[2] = 5
1642 self.assertEqual(field, list(dct.keys()))
1643 self.assertNotEqual(field, dct)
1644
9cf643d1
PP
1645 def test_setitem(self):
1646 self._def[2] = 24
1647 self.assertEqual(self._def[2], 24)
1648
1649 def test_setitem_int_field(self):
1eccc498
SM
1650 int_fc = self._tc.create_signed_integer_field_class(32)
1651 int_field = _create_field(self._tc, int_fc)
9cf643d1
PP
1652 int_field.value = 19487
1653 self._def[1] = int_field
1654 self.assertEqual(self._def[1], 19487)
1655
1656 def test_setitem_non_basic_field(self):
1eccc498 1657 array_field = _create_struct_array_field(self._tc, 2)
9cf643d1
PP
1658 with self.assertRaises(TypeError):
1659 array_field[1] = 23
1660
1661 def test_setitem_none(self):
1662 with self.assertRaises(TypeError):
1663 self._def[1] = None
1664
1665 def test_setitem_index_wrong_type(self):
1666 with self.assertRaises(TypeError):
1667 self._def['yes'] = 23
1668
1669 def test_setitem_index_neg(self):
1670 with self.assertRaises(IndexError):
1671 self._def[-2] = 23
1672
1673 def test_setitem_index_out_of_range(self):
1674 with self.assertRaises(IndexError):
1675 self._def[len(self._def)] = 134679
1676
f0a42b33
FD
1677 def test_const_setitem(self):
1678 with self.assertRaises(TypeError):
1679 self._def_const[0] = 134679
1680
9cf643d1
PP
1681 def test_iter(self):
1682 for field, value in zip(self._def, (45, 1847, 1948754)):
1683 self.assertEqual(field, value)
1684
f0a42b33
FD
1685 def test_const_iter(self):
1686 for field, value in zip(self._def_const, (45, 1847, 1948754)):
1687 self.assertEqual(field, value)
1688
7c54e2e7
JG
1689 def test_value_int_field(self):
1690 values = [45646, 145, 12145]
1691 self._def.value = values
1692 self.assertEqual(values, self._def)
1693
7c54e2e7
JG
1694 def test_value_check_sequence(self):
1695 values = 42
1696 with self.assertRaises(TypeError):
1697 self._def.value = values
1698
1699 def test_value_wrong_type_in_sequence(self):
1700 values = [32, 'hello', 11]
1701 with self.assertRaises(TypeError):
1702 self._def.value = values
1703
1704 def test_value_complex_type(self):
1eccc498
SM
1705 struct_fc = self._tc.create_structure_field_class()
1706 int_fc = self._tc.create_signed_integer_field_class(32)
1707 another_int_fc = self._tc.create_signed_integer_field_class(32)
1708 str_fc = self._tc.create_string_field_class()
1709 struct_fc.append_member(field_class=int_fc, name='an_int')
1710 struct_fc.append_member(field_class=str_fc, name='a_string')
1711 struct_fc.append_member(field_class=another_int_fc, name='another_int')
1712 array_fc = self._tc.create_static_array_field_class(struct_fc, 3)
1713 stream = _create_stream(self._tc, [('array_field', array_fc)])
7c54e2e7 1714 values = [
cfbd7cf3
FD
1715 {'an_int': 42, 'a_string': 'hello', 'another_int': 66},
1716 {'an_int': 1, 'a_string': 'goodbye', 'another_int': 488},
1717 {'an_int': 156, 'a_string': 'or not', 'another_int': 4648},
7c54e2e7
JG
1718 ]
1719
1eccc498 1720 array = stream.create_packet().context_field['array_field']
7c54e2e7
JG
1721 array.value = values
1722 self.assertEqual(values, array)
1723 values[0]['an_int'] = 'a string'
1724 with self.assertRaises(TypeError):
1725 array.value = values
9cf643d1 1726
b0bdda42
JG
1727 def test_str_op(self):
1728 s = str(self._def)
cfbd7cf3 1729 expected_string = '[{}]'.format(', '.join([repr(v) for v in self._def_value]))
b0bdda42
JG
1730 self.assertEqual(expected_string, s)
1731
742e4747 1732
1eccc498 1733class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase):
f0a42b33
FD
1734 @staticmethod
1735 def _const_value_setter(field):
1736 field.value = [45, 1847, 1948754]
1737
9cf643d1 1738 def setUp(self):
1eccc498
SM
1739 self._tc = get_default_trace_class()
1740 self._def = _create_int_array_field(self._tc, 3)
9cf643d1
PP
1741 self._def[0] = 45
1742 self._def[1] = 1847
1743 self._def[2] = 1948754
7c54e2e7 1744 self._def_value = [45, 1847, 1948754]
f0a42b33
FD
1745 self._def_const = _create_const_field(
1746 self._tc,
1747 self._tc.create_static_array_field_class(
1748 self._tc.create_signed_integer_field_class(32), 3
1749 ),
1750 self._const_value_setter,
1751 )
9cf643d1 1752
7c54e2e7
JG
1753 def test_value_wrong_len(self):
1754 values = [45, 1847]
1755 with self.assertRaises(ValueError):
1756 self._def.value = values
1757
9cf643d1 1758
1eccc498 1759class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase):
f0a42b33
FD
1760 @staticmethod
1761 def _const_value_setter(field):
1762 field.value = [45, 1847, 1948754]
1763
9cf643d1 1764 def setUp(self):
1eccc498
SM
1765 self._tc = get_default_trace_class()
1766 self._def = _create_dynamic_array(self._tc)
9cf643d1
PP
1767 self._def[0] = 45
1768 self._def[1] = 1847
1769 self._def[2] = 1948754
7c54e2e7 1770 self._def_value = [45, 1847, 1948754]
f0a42b33
FD
1771 self._def_const = _create_const_field(
1772 self._tc,
1773 self._tc.create_dynamic_array_field_class(
1774 self._tc.create_signed_integer_field_class(32)
1775 ),
1776 self._const_value_setter,
1777 )
9cf643d1 1778
7c54e2e7
JG
1779 def test_value_resize(self):
1780 new_values = [1, 2, 3, 4]
1781 self._def.value = new_values
1782 self.assertCountEqual(self._def, new_values)
1783
1eccc498
SM
1784 def test_set_length(self):
1785 self._def.length = 4
1786 self._def[3] = 0
1787 self.assertEqual(len(self._def), 4)
7c54e2e7 1788
f0a42b33
FD
1789 def test_const_set_length(self):
1790 with self.assertRaises(AttributeError):
1791 self._def_const.length = 4
1792 self.assertEqual(len(self._def), 3)
1793
1eccc498 1794 def test_set_invalid_length(self):
7c54e2e7 1795 with self.assertRaises(TypeError):
1eccc498
SM
1796 self._def.length = 'cheval'
1797
1798
1799class StructureFieldTestCase(unittest.TestCase):
f0a42b33
FD
1800 @staticmethod
1801 def _const_value_setter(field):
1802 field.value = {
1803 'A': -1872,
1804 'B': 'salut',
1805 'C': 17.5,
1806 'D': 16497,
1807 'E': {},
1808 'F': {'F_1': 52},
1809 }
1810
1eccc498
SM
1811 def _create_fc(self, tc):
1812 fc = tc.create_structure_field_class()
1813 fc.append_member('A', self._fc0_fn())
1814 fc.append_member('B', self._fc1_fn())
1815 fc.append_member('C', self._fc2_fn())
1816 fc.append_member('D', self._fc3_fn())
1817 fc.append_member('E', self._fc4_fn())
1818 fc5 = self._fc5_fn()
1819 fc5.append_member('F_1', self._fc5_inner_fn())
1820 fc.append_member('F', fc5)
1821 return fc
7c54e2e7 1822
9cf643d1 1823 def setUp(self):
1eccc498
SM
1824 self._tc = get_default_trace_class()
1825 self._fc0_fn = self._tc.create_signed_integer_field_class
1826 self._fc1_fn = self._tc.create_string_field_class
1827 self._fc2_fn = self._tc.create_real_field_class
1828 self._fc3_fn = self._tc.create_signed_integer_field_class
1829 self._fc4_fn = self._tc.create_structure_field_class
1830 self._fc5_fn = self._tc.create_structure_field_class
1831 self._fc5_inner_fn = self._tc.create_signed_integer_field_class
1832
1833 self._fc = self._create_fc(self._tc)
1834 self._def = _create_field(self._tc, self._fc)
9cf643d1
PP
1835 self._def['A'] = -1872
1836 self._def['B'] = 'salut'
1837 self._def['C'] = 17.5
1838 self._def['D'] = 16497
1eccc498
SM
1839 self._def['E'] = {}
1840 self._def['F'] = {'F_1': 52}
b0bdda42
JG
1841 self._def_value = {
1842 'A': -1872,
1843 'B': 'salut',
1844 'C': 17.5,
1eccc498
SM
1845 'D': 16497,
1846 'E': {},
cfbd7cf3 1847 'F': {'F_1': 52},
b0bdda42 1848 }
9cf643d1 1849
f0a42b33
FD
1850 self._def_const = _create_const_field(
1851 self._tc, self._create_fc(self._tc), self._const_value_setter
1852 )
1853
9cf643d1
PP
1854 def _modify_def(self):
1855 self._def['B'] = 'hola'
1856
1857 def test_bool_op_true(self):
1858 self.assertTrue(self._def)
1859
1860 def test_bool_op_false(self):
1eccc498 1861 field = self._def['E']
9cf643d1
PP
1862 self.assertFalse(field)
1863
1864 def test_len(self):
1eccc498 1865 self.assertEqual(len(self._def), len(self._def_value))
9cf643d1
PP
1866
1867 def test_getitem(self):
f0a42b33
FD
1868 field1 = self._def['A']
1869 field2 = self._def['B']
1870 field3 = self._def['C']
1871 field4 = self._def['D']
1872 field5 = self._def['E']
1873 field6 = self._def['F']
1874
1875 self.assertIs(type(field1), bt2._SignedIntegerField)
1876 self.assertEqual(field1, -1872)
1877
1878 self.assertIs(type(field2), bt2._StringField)
1879 self.assertEqual(field2, 'salut')
1880
1881 self.assertIs(type(field3), bt2._RealField)
1882 self.assertEqual(field3, 17.5)
1883
1884 self.assertIs(type(field4), bt2._SignedIntegerField)
1885 self.assertEqual(field4, 16497)
1886
1887 self.assertIs(type(field5), bt2._StructureField)
1888 self.assertEqual(field5, {})
1889
1890 self.assertIs(type(field6), bt2._StructureField)
1891 self.assertEqual(field6, {'F_1': 52})
1892
1893 def test_const_getitem(self):
1894 field1 = self._def_const['A']
1895 field2 = self._def_const['B']
1896 field3 = self._def_const['C']
1897 field4 = self._def_const['D']
1898 field5 = self._def_const['E']
1899 field6 = self._def_const['F']
1900
1901 self.assertIs(type(field1), bt2._SignedIntegerFieldConst)
1902 self.assertEqual(field1, -1872)
1903
1904 self.assertIs(type(field2), bt2._StringFieldConst)
1905 self.assertEqual(field2, 'salut')
1906
1907 self.assertIs(type(field3), bt2._RealFieldConst)
1908 self.assertEqual(field3, 17.5)
1909
1910 self.assertIs(type(field4), bt2._SignedIntegerFieldConst)
1911 self.assertEqual(field4, 16497)
1912
1913 self.assertIs(type(field5), bt2._StructureFieldConst)
1914 self.assertEqual(field5, {})
1915
1916 self.assertIs(type(field6), bt2._StructureFieldConst)
1917 self.assertEqual(field6, {'F_1': 52})
9cf643d1 1918
1eccc498 1919 def test_member_at_index_out_of_bounds_after(self):
811644b8 1920 with self.assertRaises(IndexError):
1eccc498 1921 self._def.member_at_index(len(self._def_value))
811644b8 1922
9cf643d1 1923 def test_eq(self):
cfbd7cf3 1924 field = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
1925 field['A'] = -1872
1926 field['B'] = 'salut'
1927 field['C'] = 17.5
1928 field['D'] = 16497
1eccc498
SM
1929 field['E'] = {}
1930 field['F'] = {'F_1': 52}
9cf643d1
PP
1931 self.assertEqual(self._def, field)
1932
f0a42b33
FD
1933 def test_const_eq(self):
1934 field = _create_field(self._tc, self._create_fc(self._tc))
1935 field['A'] = -1872
1936 field['B'] = 'salut'
1937 field['C'] = 17.5
1938 field['D'] = 16497
1939 field['E'] = {}
1940 field['F'] = {'F_1': 52}
1941 self.assertEqual(self._def_const, field)
1942
9cf643d1
PP
1943 def test_eq_invalid_type(self):
1944 self.assertNotEqual(self._def, 23)
1945
1946 def test_eq_diff_len(self):
1eccc498
SM
1947 fc = self._tc.create_structure_field_class()
1948 fc.append_member('A', self._fc0_fn())
1949 fc.append_member('B', self._fc1_fn())
1950 fc.append_member('C', self._fc2_fn())
1951
1952 field = _create_field(self._tc, fc)
9cf643d1
PP
1953 field['A'] = -1872
1954 field['B'] = 'salut'
1955 field['C'] = 17.5
1956 self.assertNotEqual(self._def, field)
1957
1eccc498
SM
1958 def test_eq_diff_keys(self):
1959 fc = self._tc.create_structure_field_class()
1960 fc.append_member('U', self._fc0_fn())
1961 fc.append_member('V', self._fc1_fn())
1962 fc.append_member('W', self._fc2_fn())
1963 fc.append_member('X', self._fc3_fn())
1964 fc.append_member('Y', self._fc4_fn())
1965 fc.append_member('Z', self._fc5_fn())
1966 field = _create_field(self._tc, fc)
1967 field['U'] = -1871
1968 field['V'] = "gerry"
1969 field['W'] = 18.19
1970 field['X'] = 16497
1971 field['Y'] = {}
1972 field['Z'] = {}
1973 self.assertNotEqual(self._def, field)
1974
9cf643d1 1975 def test_eq_diff_content_same_len(self):
1eccc498 1976 field = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
1977 field['A'] = -1872
1978 field['B'] = 'salut'
1979 field['C'] = 17.4
1980 field['D'] = 16497
1eccc498
SM
1981 field['E'] = {}
1982 field['F'] = {'F_1': 0}
9cf643d1
PP
1983 self.assertNotEqual(self._def, field)
1984
1985 def test_eq_same_content_diff_keys(self):
1eccc498
SM
1986 fc = self._tc.create_structure_field_class()
1987 fc.append_member('A', self._fc0_fn())
1988 fc.append_member('B', self._fc1_fn())
1989 fc.append_member('E', self._fc2_fn())
1990 fc.append_member('D', self._fc3_fn())
1991 fc.append_member('C', self._fc4_fn())
1992 fc.append_member('F', self._fc5_fn())
1993 field = _create_field(self._tc, fc)
9cf643d1
PP
1994 field['A'] = -1872
1995 field['B'] = 'salut'
1eccc498 1996 field['E'] = 17.5
9cf643d1 1997 field['D'] = 16497
1eccc498
SM
1998 field['C'] = {}
1999 field['F'] = {}
9cf643d1
PP
2000 self.assertNotEqual(self._def, field)
2001
2002 def test_setitem(self):
2003 self._def['C'] = -18.47
2004 self.assertEqual(self._def['C'], -18.47)
2005
f0a42b33
FD
2006 def test_const_setitem(self):
2007 with self.assertRaises(TypeError):
2008 self._def_const['A'] = 134679
2009
9cf643d1 2010 def test_setitem_int_field(self):
1eccc498
SM
2011 int_fc = self._tc.create_signed_integer_field_class(32)
2012 int_field = _create_field(self._tc, int_fc)
9cf643d1
PP
2013 int_field.value = 19487
2014 self._def['D'] = int_field
2015 self.assertEqual(self._def['D'], 19487)
2016
2017 def test_setitem_non_basic_field(self):
1eccc498
SM
2018 elem_fc = self._tc.create_structure_field_class()
2019 struct_fc = self._tc.create_structure_field_class()
2020 struct_fc.append_member('A', elem_fc)
2021 struct_field = _create_field(self._tc, struct_fc)
9cf643d1 2022
236355c2
JG
2023 # Will fail on access to .items() of the value
2024 with self.assertRaises(AttributeError):
9cf643d1
PP
2025 struct_field['A'] = 23
2026
2027 def test_setitem_none(self):
2028 with self.assertRaises(TypeError):
2029 self._def['C'] = None
2030
2031 def test_setitem_key_wrong_type(self):
2032 with self.assertRaises(TypeError):
2033 self._def[3] = 23
2034
2035 def test_setitem_wrong_key(self):
2036 with self.assertRaises(KeyError):
2037 self._def['hi'] = 134679
2038
1eccc498
SM
2039 def test_member_at_index(self):
2040 self.assertEqual(self._def.member_at_index(1), 'salut')
9cf643d1 2041
f0a42b33
FD
2042 def test_const_member_at_index(self):
2043 self.assertEqual(self._def_const.member_at_index(1), 'salut')
2044
9cf643d1
PP
2045 def test_iter(self):
2046 orig_values = {
2047 'A': -1872,
2048 'B': 'salut',
2049 'C': 17.5,
2050 'D': 16497,
1eccc498 2051 'E': {},
cfbd7cf3 2052 'F': {'F_1': 52},
9cf643d1
PP
2053 }
2054
2055 for vkey, vval in self._def.items():
2056 val = orig_values[vkey]
2057 self.assertEqual(vval, val)
811644b8 2058
7c54e2e7
JG
2059 def test_value(self):
2060 orig_values = {
2061 'A': -1872,
2062 'B': 'salut',
2063 'C': 17.5,
2064 'D': 16497,
1eccc498 2065 'E': {},
cfbd7cf3 2066 'F': {'F_1': 52},
7c54e2e7
JG
2067 }
2068 self.assertEqual(self._def, orig_values)
2069
2070 def test_set_value(self):
1eccc498
SM
2071 int_fc = self._tc.create_signed_integer_field_class(32)
2072 another_int_fc = self._tc.create_signed_integer_field_class(32)
2073 str_fc = self._tc.create_string_field_class()
2074 struct_fc = self._tc.create_structure_field_class()
2075 struct_fc.append_member(field_class=int_fc, name='an_int')
2076 struct_fc.append_member(field_class=str_fc, name='a_string')
2077 struct_fc.append_member(field_class=another_int_fc, name='another_int')
cfbd7cf3 2078 values = {'an_int': 42, 'a_string': 'hello', 'another_int': 66}
7c54e2e7 2079
1eccc498 2080 struct = _create_field(self._tc, struct_fc)
7c54e2e7
JG
2081 struct.value = values
2082 self.assertEqual(values, struct)
2083
2084 bad_type_values = copy.deepcopy(values)
2085 bad_type_values['an_int'] = 'a string'
2086 with self.assertRaises(TypeError):
2087 struct.value = bad_type_values
2088
2089 unknown_key_values = copy.deepcopy(values)
2090 unknown_key_values['unknown_key'] = 16546
2091 with self.assertRaises(KeyError):
2092 struct.value = unknown_key_values
2093
b0bdda42
JG
2094 def test_str_op(self):
2095 expected_string_found = False
2096 s = str(self._def)
2097 # Establish all permutations of the three expected matches since
2098 # the order in which mappings are enumerated is not explicitly part of
2099 # the API.
2100 for p in itertools.permutations([(k, v) for k, v in self._def.items()]):
2101 items = ['{}: {}'.format(repr(k), repr(v)) for k, v in p]
2102 candidate = '{{{}}}'.format(', '.join(items))
2103 if candidate == s:
2104 expected_string_found = True
2105 break
2106
2107 self.assertTrue(expected_string_found)
2108
b0bdda42 2109
cec0261d 2110class OptionFieldTestCase(unittest.TestCase):
f0a42b33
FD
2111 @staticmethod
2112 def _const_value_setter(field):
2113 field.value = {'opt_field': 'hiboux'}
2114
cec0261d
PP
2115 def _create_fc(self, tc):
2116 fc = tc.create_option_field_class(tc.create_string_field_class())
2117 top_fc = tc.create_structure_field_class()
2118 top_fc.append_member('opt_field', fc)
2119 return top_fc
2120
2121 def setUp(self):
2122 self._tc = get_default_trace_class()
2123 fld = _create_field(self._tc, self._create_fc(self._tc))
2124 self._def = fld['opt_field']
f0a42b33
FD
2125 self._def_value = 'hiboux'
2126 self._def_const = _create_const_field(
2127 self._tc, self._create_fc(self._tc), self._const_value_setter
2128 )['opt_field']
cec0261d
PP
2129
2130 def test_value_prop(self):
2131 self._def.value = 'hiboux'
2132 self.assertEqual(self._def.field, 'hiboux')
f0a42b33
FD
2133 self.assertIs(type(self._def), bt2._OptionField)
2134 self.assertIs(type(self._def.field), bt2._StringField)
cec0261d
PP
2135 self.assertTrue(self._def.has_field)
2136
f0a42b33
FD
2137 def test_const_value_prop(self):
2138 self.assertEqual(self._def_const.field, 'hiboux')
2139 self.assertIs(type(self._def_const), bt2._OptionFieldConst)
2140 self.assertIs(type(self._def_const.field), bt2._StringFieldConst)
2141 self.assertTrue(self._def_const.has_field)
2142
cec0261d
PP
2143 def test_has_field_prop_true(self):
2144 self._def.has_field = True
2145 self.assertTrue(self._def.has_field)
2146
2147 def test_has_field_prop_true(self):
2148 self._def.has_field = False
2149 self.assertFalse(self._def.has_field)
2150
2151 def test_bool_op_true(self):
2152 self._def.value = 'allo'
2153 self.assertTrue(self._def)
2154
2155 def test_bool_op_true(self):
2156 self._def.has_field = False
2157 self.assertFalse(self._def)
2158
2159 def test_field_prop_existing(self):
2160 self._def.value = 'meow'
2161 field = self._def.field
2162 self.assertEqual(field, 'meow')
2163
2164 def test_field_prop_none(self):
2165 self._def.has_field = False
2166 field = self._def.field
2167 self.assertIsNone(field)
2168
f0a42b33
FD
2169 def test_const_field_prop(self):
2170 with self.assertRaises(AttributeError):
2171 self._def_const.has_field = False
2172
2173 self.assertEqual(self._def_const, self._def_value)
2174 self.assertTrue(self._def_const.has_field)
2175
cec0261d
PP
2176 def test_field_prop_existing_then_none(self):
2177 self._def.value = 'meow'
2178 field = self._def.field
2179 self.assertEqual(field, 'meow')
2180 self._def.has_field = False
2181 field = self._def.field
2182 self.assertIsNone(field)
2183
2184 def test_eq(self):
2185 field = _create_field(self._tc, self._create_fc(self._tc))
2186 field = field['opt_field']
2187 field.value = 'walk'
2188 self._def.value = 'walk'
2189 self.assertEqual(self._def, field)
2190
f0a42b33
FD
2191 def test_const_eq(self):
2192 field = _create_field(self._tc, self._create_fc(self._tc))
2193 field = field['opt_field']
2194 field.value = 'hiboux'
2195 self.assertEqual(self._def_const, field)
2196 self.assertEqual(self._def_const, self._def_value)
2197
cec0261d
PP
2198 def test_eq_invalid_type(self):
2199 self._def.value = 'gerry'
2200 self.assertNotEqual(self._def, 23)
2201
2202 def test_str_op(self):
2203 self._def.value = 'marcel'
2204 self.assertEqual(str(self._def), str(self._def.field))
2205
2206 def test_repr_op(self):
2207 self._def.value = 'mireille'
2208 self.assertEqual(repr(self._def), repr(self._def.field))
2209
2210
1eccc498 2211class VariantFieldTestCase(unittest.TestCase):
f0a42b33
FD
2212 @staticmethod
2213 def _const_value_setter(field):
2214 field.selected_option_index = 3
2215 field.value = 1334
2216
1eccc498 2217 def _create_fc(self, tc):
1eccc498
SM
2218 ft0 = tc.create_signed_integer_field_class(32)
2219 ft1 = tc.create_string_field_class()
2220 ft2 = tc.create_real_field_class()
2221 ft3 = tc.create_signed_integer_field_class(17)
1eccc498
SM
2222 fc = tc.create_variant_field_class()
2223 fc.append_option('corner', ft0)
2224 fc.append_option('zoom', ft1)
2225 fc.append_option('mellotron', ft2)
2226 fc.append_option('giorgio', ft3)
1eccc498 2227 top_fc = tc.create_structure_field_class()
1eccc498
SM
2228 top_fc.append_member('variant_field', fc)
2229 return top_fc
811644b8 2230
811644b8 2231 def setUp(self):
1eccc498
SM
2232 self._tc = get_default_trace_class()
2233 fld = _create_field(self._tc, self._create_fc(self._tc))
2234 self._def = fld['variant_field']
811644b8 2235
f0a42b33
FD
2236 self._def_value = 1334
2237 self._def_selected_index = 3
2238 const_fc = self._create_fc(self._tc)['variant_field']
2239
2240 fld_const = _create_const_field(
2241 self._tc, const_fc.field_class, self._const_value_setter
2242 )
2243 self._def_const = fld_const
2244
1eccc498
SM
2245 def test_bool_op(self):
2246 self._def.selected_option_index = 2
2247 self._def.value = -17.34
2248 with self.assertRaises(NotImplementedError):
2249 bool(self._def)
811644b8 2250
1eccc498
SM
2251 def test_selected_option_index(self):
2252 self._def.selected_option_index = 2
2253 self.assertEqual(self._def.selected_option_index, 2)
811644b8 2254
d431b4de
FD
2255 def test_selected_option_index_above_range(self):
2256 with self.assertRaises(IndexError):
2257 self._def.selected_option_index = 4
2258
2259 def test_selected_option_index_below_range(self):
2260 with self.assertRaises(IndexError):
2261 self._def.selected_option_index = -1
2262
f0a42b33
FD
2263 def test_const_selected_option_index(self):
2264 with self.assertRaises(AttributeError):
2265 self._def_const.selected_option_index = 2
2266 self.assertEqual(self._def_const.selected_option_index, 3)
2267
1eccc498
SM
2268 def test_selected_option(self):
2269 self._def.selected_option_index = 2
2270 self._def.value = -17.34
2271 self.assertEqual(self._def.selected_option, -17.34)
f0a42b33 2272 self.assertEqual(type(self._def.selected_option), bt2._RealField)
1eccc498
SM
2273
2274 self._def.selected_option_index = 3
2275 self._def.value = 1921
2276 self.assertEqual(self._def.selected_option, 1921)
f0a42b33
FD
2277 self.assertEqual(type(self._def.selected_option), bt2._SignedIntegerField)
2278
2279 def test_const_selected_option(self):
2280 self.assertEqual(self._def_const.selected_option, 1334)
2281 self.assertEqual(
2282 type(self._def_const.selected_option), bt2._SignedIntegerFieldConst
2283 )
811644b8
PP
2284
2285 def test_eq(self):
1eccc498
SM
2286 field = _create_field(self._tc, self._create_fc(self._tc))
2287 field = field['variant_field']
2288 field.selected_option_index = 0
2289 field.value = 1774
2290 self._def.selected_option_index = 0
2291 self._def.value = 1774
811644b8
PP
2292 self.assertEqual(self._def, field)
2293
f0a42b33
FD
2294 def test_const_eq(self):
2295 field = _create_field(self._tc, self._create_fc(self._tc))
2296 field = field['variant_field']
2297 field.selected_option_index = 3
2298 field.value = 1334
2299 self.assertEqual(self._def_const, field)
2300
2b9aa00b
FD
2301 def test_len(self):
2302 self.assertEqual(len(self._def), 4)
2303
811644b8 2304 def test_eq_invalid_type(self):
1eccc498
SM
2305 self._def.selected_option_index = 1
2306 self._def.value = 'gerry'
811644b8 2307 self.assertNotEqual(self._def, 23)
742e4747 2308
b0bdda42 2309 def test_str_op_int(self):
1eccc498
SM
2310 field = _create_field(self._tc, self._create_fc(self._tc))
2311 field = field['variant_field']
2312 field.selected_option_index = 0
2313 field.value = 1774
2314 other_field = _create_field(self._tc, self._create_fc(self._tc))
2315 other_field = other_field['variant_field']
2316 other_field.selected_option_index = 0
2317 other_field.value = 1774
2318 self.assertEqual(str(field), str(other_field))
b0bdda42
JG
2319
2320 def test_str_op_str(self):
1eccc498
SM
2321 field = _create_field(self._tc, self._create_fc(self._tc))
2322 field = field['variant_field']
2323 field.selected_option_index = 1
2324 field.value = 'un beau grand bateau'
2325 other_field = _create_field(self._tc, self._create_fc(self._tc))
2326 other_field = other_field['variant_field']
2327 other_field.selected_option_index = 1
2328 other_field.value = 'un beau grand bateau'
2329 self.assertEqual(str(field), str(other_field))
2330
2331 def test_str_op_float(self):
2332 field = _create_field(self._tc, self._create_fc(self._tc))
2333 field = field['variant_field']
2334 field.selected_option_index = 2
2335 field.value = 14.4245
2336 other_field = _create_field(self._tc, self._create_fc(self._tc))
2337 other_field = other_field['variant_field']
2338 other_field.selected_option_index = 2
2339 other_field.value = 14.4245
2340 self.assertEqual(str(field), str(other_field))
d14ddbba
SM
2341
2342
2343if __name__ == '__main__':
2344 unittest.main()
This page took 0.150353 seconds and 4 git commands to generate.