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