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