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