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