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