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