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