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