bt2: create_value(): check `numbers` and `collections.abc` types
[babeltrace.git] / tests / bindings / python / bt2 / test_field.py
CommitLineData
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
19from functools import partial, partialmethod
20import operator
21import unittest
9cf643d1
PP
22import math
23import copy
b0bdda42 24import itertools
9cf643d1 25import bt2
1eccc498 26from utils import get_default_trace_class
9cf643d1
PP
27
28
29_COMP_BINOPS = (
30 operator.eq,
31 operator.ne,
32)
33
34
1eccc498
SM
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
40def _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
57def _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
69def _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
82def _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
97def _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
114def _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
123class _TestNumericField:
9cf643d1
PP
124 def _binop(self, op, rhs):
125 rexc = None
126 rvexc = None
127 comp_value = rhs
128
9cf643d1
PP
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):
e1c6bebd 195 value_before = copy.copy(self._def_value)
9cf643d1 196 self._unaryop(op)
e1c6bebd 197 self.assertEqual(self._def, value_before)
9cf643d1
PP
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
1eccc498 224 @unittest.skip('copy is not implemented')
9cf643d1 225 def _test_binop_lhs_value_same(self, op, rhs):
e1c6bebd 226 value_before = copy.copy(self._def)
9cf643d1 227 r, rv = self._binop(op, rhs)
e1c6bebd 228 self.assertEqual(self._def, value_before)
9cf643d1
PP
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
9cf643d1
PP
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
9cf643d1
PP
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):
1eccc498
SM
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
9cf643d1
PP
477
478 def test_ne_none(self):
1eccc498
SM
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
742e4747 483
9cf643d1
PP
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
9cf643d1
PP
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
535def _inject_numeric_testing_methods(cls):
536 def test_binop_name(suffix):
537 return 'test_binop_{}_{}'.format(name, suffix)
538
9cf643d1
PP
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:
9cf643d1
PP
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
9cf643d1
PP
610
611class _TestIntegerFieldCommon(_TestNumericField):
612 def test_assign_true(self):
613 raw = True
614 self._def.value = raw
615 self.assertEqual(self._def, raw)
9cf643d1
PP
616
617 def test_assign_false(self):
618 raw = False
619 self._def.value = raw
620 self.assertEqual(self._def, raw)
9cf643d1
PP
621
622 def test_assign_pos_int(self):
623 raw = 477
624 self._def.value = raw
625 self.assertEqual(self._def, raw)
9cf643d1
PP
626
627 def test_assign_neg_int(self):
628 raw = -13
629 self._def.value = raw
630 self.assertEqual(self._def, raw)
9cf643d1
PP
631
632 def test_assign_int_field(self):
633 raw = 999
1eccc498 634 field = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
635 field.value = raw
636 self._def.value = field
637 self.assertEqual(self._def, raw)
9cf643d1 638
9cf643d1
PP
639 def test_assign_invalid_type(self):
640 with self.assertRaises(TypeError):
641 self._def.value = 'yes'
642
643 def test_assign_uint(self):
1eccc498
SM
644 uint_fc = self._tc.create_unsigned_integer_field_class(32)
645 field = _create_field(self._tc, uint_fc)
9cf643d1
PP
646 raw = 1777
647 field.value = 1777
648 self.assertEqual(field, raw)
9cf643d1 649
7bb4180f
FD
650 def test_assign_big_uint(self):
651 uint_fc = self._tc.create_unsigned_integer_field_class(64)
652 field = _create_field(self._tc, uint_fc)
653 # Larger than the IEEE 754 double-precision exact representation of
654 # integers.
655 raw = (2**53) + 1
656 field.value = (2**53) + 1
657 self.assertEqual(field, raw)
658
9cf643d1 659 def test_assign_uint_invalid_neg(self):
1eccc498
SM
660 uint_fc = self._tc.create_unsigned_integer_field_class(32)
661 field = _create_field(self._tc, uint_fc)
9cf643d1
PP
662
663 with self.assertRaises(ValueError):
664 field.value = -23
665
b0bdda42
JG
666 def test_str_op(self):
667 self.assertEqual(str(self._def), str(self._def_value))
668
9cf643d1
PP
669
670_inject_numeric_testing_methods(_TestIntegerFieldCommon)
671
672
1eccc498
SM
673class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
674 def _create_fc(self, tc):
675 return tc.create_signed_integer_field_class(25)
676
9cf643d1 677 def setUp(self):
1eccc498
SM
678 self._tc = get_default_trace_class()
679 self._field = _create_field(self._tc, self._create_fc(self._tc))
680 self._field.value = 17
681 self._def = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
682 self._def.value = 17
683 self._def_value = 17
684 self._def_new_value = -101
685
811644b8 686
1eccc498
SM
687class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase):
688 def _create_fc(self, tc):
689 fc = tc.create_signed_enumeration_field_class(32)
690 fc.map_range('something', 17)
691 fc.map_range('speaker', 12, 16)
692 fc.map_range('can', 18, 2540)
693 fc.map_range('whole range', -(2 ** 31), (2 ** 31) - 1)
694 fc.map_range('zip', -45, 1001)
695 return fc
9cf643d1 696
9cf643d1 697 def setUp(self):
1eccc498
SM
698 self._tc = get_default_trace_class()
699 self._field = _create_field(self._tc, self._create_fc(self._tc))
700 self._def = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
701 self._def.value = 17
702 self._def_value = 17
703 self._def_new_value = -101
704
b0bdda42
JG
705 def test_str_op(self):
706 expected_string_found = False
707 s = str(self._def)
708
709 # Establish all permutations of the three expected matches since
710 # the order in which mappings are enumerated is not explicitly part of
711 # the API.
1eccc498
SM
712 for p in itertools.permutations(['whole range', 'something',
713 'zip']):
b0bdda42
JG
714 candidate = '{} ({})'.format(self._def_value, ', '.join(p))
715 if candidate == s:
716 expected_string_found = True
717 break
718
719 self.assertTrue(expected_string_found)
720
1eccc498
SM
721 def test_labels(self):
722 self._field.value = 17
723 labels = sorted(self._field.labels)
724 self.assertEqual(labels, ['something', 'whole range', 'zip'])
725
b0bdda42 726
1eccc498
SM
727class RealFieldTestCase(_TestNumericField, unittest.TestCase):
728 def _create_fc(self, tc):
729 return tc.create_real_field_class()
9cf643d1 730
9cf643d1 731 def setUp(self):
1eccc498
SM
732 self._tc = get_default_trace_class()
733 self._field = _create_field(self._tc, self._create_fc(self._tc))
734 self._def = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
735 self._def.value = 52.7
736 self._def_value = 52.7
737 self._def_new_value = -17.164857
738
739 def _test_invalid_op(self, cb):
740 with self.assertRaises(TypeError):
741 cb()
742
743 def test_assign_true(self):
744 self._def.value = True
745 self.assertTrue(self._def)
9cf643d1
PP
746
747 def test_assign_false(self):
748 self._def.value = False
749 self.assertFalse(self._def)
9cf643d1
PP
750
751 def test_assign_pos_int(self):
752 raw = 477
753 self._def.value = raw
754 self.assertEqual(self._def, float(raw))
9cf643d1
PP
755
756 def test_assign_neg_int(self):
757 raw = -13
758 self._def.value = raw
759 self.assertEqual(self._def, float(raw))
9cf643d1
PP
760
761 def test_assign_int_field(self):
1eccc498
SM
762 int_fc = self._tc.create_signed_integer_field_class(32)
763 int_field = _create_field(self._tc, int_fc)
9cf643d1 764 raw = 999
1eccc498
SM
765 int_field.value = raw
766 self._def.value = int_field
9cf643d1 767 self.assertEqual(self._def, float(raw))
9cf643d1
PP
768
769 def test_assign_float(self):
770 raw = -19.23
771 self._def.value = raw
772 self.assertEqual(self._def, raw)
9cf643d1
PP
773
774 def test_assign_float_field(self):
1eccc498 775 field = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
776 raw = 101.32
777 field.value = raw
778 self._def.value = field
779 self.assertEqual(self._def, raw)
9cf643d1
PP
780
781 def test_assign_invalid_type(self):
782 with self.assertRaises(TypeError):
783 self._def.value = 'yes'
784
785 def test_invalid_lshift(self):
786 self._test_invalid_op(lambda: self._def << 23)
787
788 def test_invalid_rshift(self):
789 self._test_invalid_op(lambda: self._def >> 23)
790
791 def test_invalid_and(self):
792 self._test_invalid_op(lambda: self._def & 23)
793
794 def test_invalid_or(self):
795 self._test_invalid_op(lambda: self._def | 23)
796
797 def test_invalid_xor(self):
798 self._test_invalid_op(lambda: self._def ^ 23)
799
800 def test_invalid_invert(self):
801 self._test_invalid_op(lambda: ~self._def)
802
b0bdda42
JG
803 def test_str_op(self):
804 self.assertEqual(str(self._def), str(self._def_value))
805
9cf643d1 806
1eccc498 807_inject_numeric_testing_methods(RealFieldTestCase)
9cf643d1
PP
808
809
1eccc498 810class StringFieldTestCase(unittest.TestCase):
9cf643d1 811 def setUp(self):
1eccc498 812 self._tc = get_default_trace_class()
9cf643d1 813 self._def_value = 'Hello, World!'
1eccc498 814 self._def = _create_string_field(self._tc)
9cf643d1
PP
815 self._def.value = self._def_value
816 self._def_new_value = 'Yes!'
817
818 def test_assign_int(self):
819 with self.assertRaises(TypeError):
820 self._def.value = 283
821
9cf643d1 822 def test_assign_string_field(self):
1eccc498 823 field = _create_string_field(self._tc)
9cf643d1
PP
824 raw = 'zorg'
825 field.value = raw
826 self.assertEqual(field, raw)
827
828 def test_eq(self):
829 self.assertEqual(self._def, self._def_value)
830
1eccc498 831 def test_not_eq(self):
9cf643d1
PP
832 self.assertNotEqual(self._def, 23)
833
834 def test_lt_vstring(self):
1eccc498 835 s1 = _create_string_field(self._tc)
9cf643d1 836 s1.value = 'allo'
1eccc498 837 s2 = _create_string_field(self._tc)
9cf643d1
PP
838 s2.value = 'bateau'
839 self.assertLess(s1, s2)
840
841 def test_lt_string(self):
1eccc498 842 s1 = _create_string_field(self._tc)
9cf643d1
PP
843 s1.value = 'allo'
844 self.assertLess(s1, 'bateau')
845
846 def test_le_vstring(self):
1eccc498 847 s1 = _create_string_field(self._tc)
9cf643d1 848 s1.value = 'allo'
1eccc498 849 s2 = _create_string_field(self._tc)
9cf643d1
PP
850 s2.value = 'bateau'
851 self.assertLessEqual(s1, s2)
852
853 def test_le_string(self):
1eccc498 854 s1 = _create_string_field(self._tc)
9cf643d1
PP
855 s1.value = 'allo'
856 self.assertLessEqual(s1, 'bateau')
857
858 def test_gt_vstring(self):
1eccc498 859 s1 = _create_string_field(self._tc)
9cf643d1 860 s1.value = 'allo'
1eccc498 861 s2 = _create_string_field(self._tc)
9cf643d1
PP
862 s2.value = 'bateau'
863 self.assertGreater(s2, s1)
864
865 def test_gt_string(self):
1eccc498 866 s1 = _create_string_field(self._tc)
9cf643d1
PP
867 s1.value = 'allo'
868 self.assertGreater('bateau', s1)
869
870 def test_ge_vstring(self):
1eccc498 871 s1 = _create_string_field(self._tc)
9cf643d1 872 s1.value = 'allo'
1eccc498 873 s2 = _create_string_field(self._tc)
9cf643d1
PP
874 s2.value = 'bateau'
875 self.assertGreaterEqual(s2, s1)
876
877 def test_ge_string(self):
1eccc498 878 s1 = _create_string_field(self._tc)
9cf643d1
PP
879 s1.value = 'allo'
880 self.assertGreaterEqual('bateau', s1)
881
882 def test_bool_op(self):
883 self.assertEqual(bool(self._def), bool(self._def_value))
884
885 def test_str_op(self):
886 self.assertEqual(str(self._def), str(self._def_value))
887
888 def test_len(self):
889 self.assertEqual(len(self._def), len(self._def_value))
890
891 def test_getitem(self):
892 self.assertEqual(self._def[5], self._def_value[5])
893
894 def test_append_str(self):
895 to_append = 'meow meow meow'
896 self._def += to_append
897 self._def_value += to_append
898 self.assertEqual(self._def, self._def_value)
899
900 def test_append_string_field(self):
1eccc498 901 field = _create_string_field(self._tc)
9cf643d1
PP
902 to_append = 'meow meow meow'
903 field.value = to_append
904 self._def += field
905 self._def_value += to_append
906 self.assertEqual(self._def, self._def_value)
907
742e4747 908
1eccc498 909class _TestArrayFieldCommon:
9cf643d1
PP
910 def _modify_def(self):
911 self._def[2] = 23
912
913 def test_bool_op_true(self):
914 self.assertTrue(self._def)
915
916 def test_len(self):
917 self.assertEqual(len(self._def), 3)
918
1eccc498
SM
919 def test_length(self):
920 self.assertEqual(self._def.length, 3)
921
9cf643d1
PP
922 def test_getitem(self):
923 field = self._def[1]
1eccc498 924 self.assertIs(type(field), bt2.field._SignedIntegerField)
9cf643d1
PP
925 self.assertEqual(field, 1847)
926
927 def test_eq(self):
1eccc498 928 field = _create_int_array_field(self._tc, 3)
9cf643d1
PP
929 field[0] = 45
930 field[1] = 1847
931 field[2] = 1948754
932 self.assertEqual(self._def, field)
933
934 def test_eq_invalid_type(self):
935 self.assertNotEqual(self._def, 23)
936
937 def test_eq_diff_len(self):
1eccc498 938 field = _create_int_array_field(self._tc, 2)
9cf643d1
PP
939 field[0] = 45
940 field[1] = 1847
941 self.assertNotEqual(self._def, field)
942
943 def test_eq_diff_content_same_len(self):
1eccc498 944 field = _create_int_array_field(self._tc, 3)
9cf643d1
PP
945 field[0] = 45
946 field[1] = 1846
947 field[2] = 1948754
948 self.assertNotEqual(self._def, field)
949
950 def test_setitem(self):
951 self._def[2] = 24
952 self.assertEqual(self._def[2], 24)
953
954 def test_setitem_int_field(self):
1eccc498
SM
955 int_fc = self._tc.create_signed_integer_field_class(32)
956 int_field = _create_field(self._tc, int_fc)
9cf643d1
PP
957 int_field.value = 19487
958 self._def[1] = int_field
959 self.assertEqual(self._def[1], 19487)
960
961 def test_setitem_non_basic_field(self):
1eccc498 962 array_field = _create_struct_array_field(self._tc, 2)
9cf643d1
PP
963 with self.assertRaises(TypeError):
964 array_field[1] = 23
965
966 def test_setitem_none(self):
967 with self.assertRaises(TypeError):
968 self._def[1] = None
969
970 def test_setitem_index_wrong_type(self):
971 with self.assertRaises(TypeError):
972 self._def['yes'] = 23
973
974 def test_setitem_index_neg(self):
975 with self.assertRaises(IndexError):
976 self._def[-2] = 23
977
978 def test_setitem_index_out_of_range(self):
979 with self.assertRaises(IndexError):
980 self._def[len(self._def)] = 134679
981
982 def test_iter(self):
983 for field, value in zip(self._def, (45, 1847, 1948754)):
984 self.assertEqual(field, value)
985
7c54e2e7
JG
986 def test_value_int_field(self):
987 values = [45646, 145, 12145]
988 self._def.value = values
989 self.assertEqual(values, self._def)
990
7c54e2e7
JG
991 def test_value_check_sequence(self):
992 values = 42
993 with self.assertRaises(TypeError):
994 self._def.value = values
995
996 def test_value_wrong_type_in_sequence(self):
997 values = [32, 'hello', 11]
998 with self.assertRaises(TypeError):
999 self._def.value = values
1000
1001 def test_value_complex_type(self):
1eccc498
SM
1002 struct_fc = self._tc.create_structure_field_class()
1003 int_fc = self._tc.create_signed_integer_field_class(32)
1004 another_int_fc = self._tc.create_signed_integer_field_class(32)
1005 str_fc = self._tc.create_string_field_class()
1006 struct_fc.append_member(field_class=int_fc, name='an_int')
1007 struct_fc.append_member(field_class=str_fc, name='a_string')
1008 struct_fc.append_member(field_class=another_int_fc, name='another_int')
1009 array_fc = self._tc.create_static_array_field_class(struct_fc, 3)
1010 stream = _create_stream(self._tc, [('array_field', array_fc)])
7c54e2e7
JG
1011 values = [
1012 {
1013 'an_int': 42,
1014 'a_string': 'hello',
1015 'another_int': 66
1016 },
1017 {
1018 'an_int': 1,
1019 'a_string': 'goodbye',
1020 'another_int': 488
1021 },
1022 {
1023 'an_int': 156,
1024 'a_string': 'or not',
1025 'another_int': 4648
1026 },
1027 ]
1028
1eccc498 1029 array = stream.create_packet().context_field['array_field']
7c54e2e7
JG
1030 array.value = values
1031 self.assertEqual(values, array)
1032 values[0]['an_int'] = 'a string'
1033 with self.assertRaises(TypeError):
1034 array.value = values
9cf643d1 1035
b0bdda42
JG
1036 def test_str_op(self):
1037 s = str(self._def)
1038 expected_string = '[{}]'.format(', '.join(
1039 [repr(v) for v in self._def_value]))
1040 self.assertEqual(expected_string, s)
1041
742e4747 1042
1eccc498 1043class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase):
9cf643d1 1044 def setUp(self):
1eccc498
SM
1045 self._tc = get_default_trace_class()
1046 self._def = _create_int_array_field(self._tc, 3)
9cf643d1
PP
1047 self._def[0] = 45
1048 self._def[1] = 1847
1049 self._def[2] = 1948754
7c54e2e7 1050 self._def_value = [45, 1847, 1948754]
9cf643d1 1051
7c54e2e7
JG
1052 def test_value_wrong_len(self):
1053 values = [45, 1847]
1054 with self.assertRaises(ValueError):
1055 self._def.value = values
1056
9cf643d1 1057
1eccc498 1058class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase):
9cf643d1 1059 def setUp(self):
1eccc498
SM
1060 self._tc = get_default_trace_class()
1061 self._def = _create_dynamic_array(self._tc)
9cf643d1
PP
1062 self._def[0] = 45
1063 self._def[1] = 1847
1064 self._def[2] = 1948754
7c54e2e7 1065 self._def_value = [45, 1847, 1948754]
9cf643d1 1066
7c54e2e7
JG
1067 def test_value_resize(self):
1068 new_values = [1, 2, 3, 4]
1069 self._def.value = new_values
1070 self.assertCountEqual(self._def, new_values)
1071
1eccc498
SM
1072 def test_set_length(self):
1073 self._def.length = 4
1074 self._def[3] = 0
1075 self.assertEqual(len(self._def), 4)
7c54e2e7 1076
1eccc498 1077 def test_set_invalid_length(self):
7c54e2e7 1078 with self.assertRaises(TypeError):
1eccc498
SM
1079 self._def.length = 'cheval'
1080
1081
1082class StructureFieldTestCase(unittest.TestCase):
1083 def _create_fc(self, tc):
1084 fc = tc.create_structure_field_class()
1085 fc.append_member('A', self._fc0_fn())
1086 fc.append_member('B', self._fc1_fn())
1087 fc.append_member('C', self._fc2_fn())
1088 fc.append_member('D', self._fc3_fn())
1089 fc.append_member('E', self._fc4_fn())
1090 fc5 = self._fc5_fn()
1091 fc5.append_member('F_1', self._fc5_inner_fn())
1092 fc.append_member('F', fc5)
1093 return fc
7c54e2e7 1094
9cf643d1 1095 def setUp(self):
1eccc498
SM
1096 self._tc = get_default_trace_class()
1097 self._fc0_fn = self._tc.create_signed_integer_field_class
1098 self._fc1_fn = self._tc.create_string_field_class
1099 self._fc2_fn = self._tc.create_real_field_class
1100 self._fc3_fn = self._tc.create_signed_integer_field_class
1101 self._fc4_fn = self._tc.create_structure_field_class
1102 self._fc5_fn = self._tc.create_structure_field_class
1103 self._fc5_inner_fn = self._tc.create_signed_integer_field_class
1104
1105 self._fc = self._create_fc(self._tc)
1106 self._def = _create_field(self._tc, self._fc)
9cf643d1
PP
1107 self._def['A'] = -1872
1108 self._def['B'] = 'salut'
1109 self._def['C'] = 17.5
1110 self._def['D'] = 16497
1eccc498
SM
1111 self._def['E'] = {}
1112 self._def['F'] = {'F_1': 52}
b0bdda42
JG
1113 self._def_value = {
1114 'A': -1872,
1115 'B': 'salut',
1116 'C': 17.5,
1eccc498
SM
1117 'D': 16497,
1118 'E': {},
1119 'F': {'F_1': 52}
b0bdda42 1120 }
9cf643d1
PP
1121
1122 def _modify_def(self):
1123 self._def['B'] = 'hola'
1124
1125 def test_bool_op_true(self):
1126 self.assertTrue(self._def)
1127
1128 def test_bool_op_false(self):
1eccc498 1129 field = self._def['E']
9cf643d1
PP
1130 self.assertFalse(field)
1131
1132 def test_len(self):
1eccc498 1133 self.assertEqual(len(self._def), len(self._def_value))
9cf643d1
PP
1134
1135 def test_getitem(self):
1136 field = self._def['A']
1eccc498 1137 self.assertIs(type(field), bt2.field._SignedIntegerField)
9cf643d1
PP
1138 self.assertEqual(field, -1872)
1139
1eccc498 1140 def test_member_at_index_out_of_bounds_after(self):
811644b8 1141 with self.assertRaises(IndexError):
1eccc498 1142 self._def.member_at_index(len(self._def_value))
811644b8 1143
9cf643d1 1144 def test_eq(self):
1eccc498 1145 field = _create_field(self._tc, self._create_fc(self._tc, ))
9cf643d1
PP
1146 field['A'] = -1872
1147 field['B'] = 'salut'
1148 field['C'] = 17.5
1149 field['D'] = 16497
1eccc498
SM
1150 field['E'] = {}
1151 field['F'] = {'F_1': 52}
9cf643d1
PP
1152 self.assertEqual(self._def, field)
1153
1154 def test_eq_invalid_type(self):
1155 self.assertNotEqual(self._def, 23)
1156
1157 def test_eq_diff_len(self):
1eccc498
SM
1158 fc = self._tc.create_structure_field_class()
1159 fc.append_member('A', self._fc0_fn())
1160 fc.append_member('B', self._fc1_fn())
1161 fc.append_member('C', self._fc2_fn())
1162
1163 field = _create_field(self._tc, fc)
9cf643d1
PP
1164 field['A'] = -1872
1165 field['B'] = 'salut'
1166 field['C'] = 17.5
1167 self.assertNotEqual(self._def, field)
1168
1eccc498
SM
1169 def test_eq_diff_keys(self):
1170 fc = self._tc.create_structure_field_class()
1171 fc.append_member('U', self._fc0_fn())
1172 fc.append_member('V', self._fc1_fn())
1173 fc.append_member('W', self._fc2_fn())
1174 fc.append_member('X', self._fc3_fn())
1175 fc.append_member('Y', self._fc4_fn())
1176 fc.append_member('Z', self._fc5_fn())
1177 field = _create_field(self._tc, fc)
1178 field['U'] = -1871
1179 field['V'] = "gerry"
1180 field['W'] = 18.19
1181 field['X'] = 16497
1182 field['Y'] = {}
1183 field['Z'] = {}
1184 self.assertNotEqual(self._def, field)
1185
9cf643d1 1186 def test_eq_diff_content_same_len(self):
1eccc498 1187 field = _create_field(self._tc, self._create_fc(self._tc))
9cf643d1
PP
1188 field['A'] = -1872
1189 field['B'] = 'salut'
1190 field['C'] = 17.4
1191 field['D'] = 16497
1eccc498
SM
1192 field['E'] = {}
1193 field['F'] = {'F_1': 0}
9cf643d1
PP
1194 self.assertNotEqual(self._def, field)
1195
1196 def test_eq_same_content_diff_keys(self):
1eccc498
SM
1197 fc = self._tc.create_structure_field_class()
1198 fc.append_member('A', self._fc0_fn())
1199 fc.append_member('B', self._fc1_fn())
1200 fc.append_member('E', self._fc2_fn())
1201 fc.append_member('D', self._fc3_fn())
1202 fc.append_member('C', self._fc4_fn())
1203 fc.append_member('F', self._fc5_fn())
1204 field = _create_field(self._tc, fc)
9cf643d1
PP
1205 field['A'] = -1872
1206 field['B'] = 'salut'
1eccc498 1207 field['E'] = 17.5
9cf643d1 1208 field['D'] = 16497
1eccc498
SM
1209 field['C'] = {}
1210 field['F'] = {}
9cf643d1
PP
1211 self.assertNotEqual(self._def, field)
1212
1213 def test_setitem(self):
1214 self._def['C'] = -18.47
1215 self.assertEqual(self._def['C'], -18.47)
1216
1217 def test_setitem_int_field(self):
1eccc498
SM
1218 int_fc = self._tc.create_signed_integer_field_class(32)
1219 int_field = _create_field(self._tc, int_fc)
9cf643d1
PP
1220 int_field.value = 19487
1221 self._def['D'] = int_field
1222 self.assertEqual(self._def['D'], 19487)
1223
1224 def test_setitem_non_basic_field(self):
1eccc498
SM
1225 elem_fc = self._tc.create_structure_field_class()
1226 struct_fc = self._tc.create_structure_field_class()
1227 struct_fc.append_member('A', elem_fc)
1228 struct_field = _create_field(self._tc, struct_fc)
9cf643d1 1229
236355c2
JG
1230 # Will fail on access to .items() of the value
1231 with self.assertRaises(AttributeError):
9cf643d1
PP
1232 struct_field['A'] = 23
1233
1234 def test_setitem_none(self):
1235 with self.assertRaises(TypeError):
1236 self._def['C'] = None
1237
1238 def test_setitem_key_wrong_type(self):
1239 with self.assertRaises(TypeError):
1240 self._def[3] = 23
1241
1242 def test_setitem_wrong_key(self):
1243 with self.assertRaises(KeyError):
1244 self._def['hi'] = 134679
1245
1eccc498
SM
1246 def test_member_at_index(self):
1247 self.assertEqual(self._def.member_at_index(1), 'salut')
9cf643d1
PP
1248
1249 def test_iter(self):
1250 orig_values = {
1251 'A': -1872,
1252 'B': 'salut',
1253 'C': 17.5,
1254 'D': 16497,
1eccc498
SM
1255 'E': {},
1256 'F': {'F_1': 52}
9cf643d1
PP
1257 }
1258
1259 for vkey, vval in self._def.items():
1260 val = orig_values[vkey]
1261 self.assertEqual(vval, val)
811644b8 1262
7c54e2e7
JG
1263 def test_value(self):
1264 orig_values = {
1265 'A': -1872,
1266 'B': 'salut',
1267 'C': 17.5,
1268 'D': 16497,
1eccc498
SM
1269 'E': {},
1270 'F': {'F_1': 52}
7c54e2e7
JG
1271 }
1272 self.assertEqual(self._def, orig_values)
1273
1274 def test_set_value(self):
1eccc498
SM
1275 int_fc = self._tc.create_signed_integer_field_class(32)
1276 another_int_fc = self._tc.create_signed_integer_field_class(32)
1277 str_fc = self._tc.create_string_field_class()
1278 struct_fc = self._tc.create_structure_field_class()
1279 struct_fc.append_member(field_class=int_fc, name='an_int')
1280 struct_fc.append_member(field_class=str_fc, name='a_string')
1281 struct_fc.append_member(field_class=another_int_fc, name='another_int')
7c54e2e7
JG
1282 values = {
1283 'an_int': 42,
1284 'a_string': 'hello',
1285 'another_int': 66
1286 }
1287
1eccc498 1288 struct = _create_field(self._tc, struct_fc)
7c54e2e7
JG
1289 struct.value = values
1290 self.assertEqual(values, struct)
1291
1292 bad_type_values = copy.deepcopy(values)
1293 bad_type_values['an_int'] = 'a string'
1294 with self.assertRaises(TypeError):
1295 struct.value = bad_type_values
1296
1297 unknown_key_values = copy.deepcopy(values)
1298 unknown_key_values['unknown_key'] = 16546
1299 with self.assertRaises(KeyError):
1300 struct.value = unknown_key_values
1301
b0bdda42
JG
1302 def test_str_op(self):
1303 expected_string_found = False
1304 s = str(self._def)
1305 # Establish all permutations of the three expected matches since
1306 # the order in which mappings are enumerated is not explicitly part of
1307 # the API.
1308 for p in itertools.permutations([(k, v) for k, v in self._def.items()]):
1309 items = ['{}: {}'.format(repr(k), repr(v)) for k, v in p]
1310 candidate = '{{{}}}'.format(', '.join(items))
1311 if candidate == s:
1312 expected_string_found = True
1313 break
1314
1315 self.assertTrue(expected_string_found)
1316
b0bdda42 1317
1eccc498
SM
1318class VariantFieldTestCase(unittest.TestCase):
1319 def _create_fc(self, tc):
1320 selector_fc = tc.create_signed_enumeration_field_class(field_value_range=32)
1321 selector_fc.map_range('corner', 23)
1322 selector_fc.map_range('zoom', 17, 20)
1323 selector_fc.map_range('mellotron', 1001)
1324 selector_fc.map_range('giorgio', 2000, 3000)
1325
1326 ft0 = tc.create_signed_integer_field_class(32)
1327 ft1 = tc.create_string_field_class()
1328 ft2 = tc.create_real_field_class()
1329 ft3 = tc.create_signed_integer_field_class(17)
1330
1331 fc = tc.create_variant_field_class()
1332 fc.append_option('corner', ft0)
1333 fc.append_option('zoom', ft1)
1334 fc.append_option('mellotron', ft2)
1335 fc.append_option('giorgio', ft3)
1336 fc.selector_field_class = selector_fc
1337
1338 top_fc = tc.create_structure_field_class()
1339 top_fc.append_member('selector_field', selector_fc)
1340 top_fc.append_member('variant_field', fc)
1341 return top_fc
811644b8 1342
811644b8 1343 def setUp(self):
1eccc498
SM
1344 self._tc = get_default_trace_class()
1345 fld = _create_field(self._tc, self._create_fc(self._tc))
1346 self._def = fld['variant_field']
811644b8 1347
1eccc498
SM
1348 def test_bool_op(self):
1349 self._def.selected_option_index = 2
1350 self._def.value = -17.34
1351 with self.assertRaises(NotImplementedError):
1352 bool(self._def)
811644b8 1353
1eccc498
SM
1354 def test_selected_option_index(self):
1355 self._def.selected_option_index = 2
1356 self.assertEqual(self._def.selected_option_index, 2)
811644b8 1357
1eccc498
SM
1358 def test_selected_option(self):
1359 self._def.selected_option_index = 2
1360 self._def.value = -17.34
1361 self.assertEqual(self._def.selected_option, -17.34)
1362
1363 self._def.selected_option_index = 3
1364 self._def.value = 1921
1365 self.assertEqual(self._def.selected_option, 1921)
811644b8
PP
1366
1367 def test_eq(self):
1eccc498
SM
1368 field = _create_field(self._tc, self._create_fc(self._tc))
1369 field = field['variant_field']
1370 field.selected_option_index = 0
1371 field.value = 1774
1372 self._def.selected_option_index = 0
1373 self._def.value = 1774
811644b8
PP
1374 self.assertEqual(self._def, field)
1375
1376 def test_eq_invalid_type(self):
1eccc498
SM
1377 self._def.selected_option_index = 1
1378 self._def.value = 'gerry'
811644b8 1379 self.assertNotEqual(self._def, 23)
742e4747 1380
b0bdda42 1381 def test_str_op_int(self):
1eccc498
SM
1382 field = _create_field(self._tc, self._create_fc(self._tc))
1383 field = field['variant_field']
1384 field.selected_option_index = 0
1385 field.value = 1774
1386 other_field = _create_field(self._tc, self._create_fc(self._tc))
1387 other_field = other_field['variant_field']
1388 other_field.selected_option_index = 0
1389 other_field.value = 1774
1390 self.assertEqual(str(field), str(other_field))
b0bdda42
JG
1391
1392 def test_str_op_str(self):
1eccc498
SM
1393 field = _create_field(self._tc, self._create_fc(self._tc))
1394 field = field['variant_field']
1395 field.selected_option_index = 1
1396 field.value = 'un beau grand bateau'
1397 other_field = _create_field(self._tc, self._create_fc(self._tc))
1398 other_field = other_field['variant_field']
1399 other_field.selected_option_index = 1
1400 other_field.value = 'un beau grand bateau'
1401 self.assertEqual(str(field), str(other_field))
1402
1403 def test_str_op_float(self):
1404 field = _create_field(self._tc, self._create_fc(self._tc))
1405 field = field['variant_field']
1406 field.selected_option_index = 2
1407 field.value = 14.4245
1408 other_field = _create_field(self._tc, self._create_fc(self._tc))
1409 other_field = other_field['variant_field']
1410 other_field.selected_option_index = 2
1411 other_field.value = 14.4245
1412 self.assertEqual(str(field), str(other_field))
This page took 0.099096 seconds and 4 git commands to generate.