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