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