Commit | Line | Data |
---|---|---|
32d2d479 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 | ||
32656995 | 19 | import unittest |
32656995 | 20 | import bt2 |
c8820b76 | 21 | from utils import get_default_trace_class |
32656995 SM |
22 | |
23 | ||
a07f15cb PP |
24 | class BoolFieldClassTestCase(unittest.TestCase): |
25 | def setUp(self): | |
26 | tc = get_default_trace_class() | |
27 | self._fc = tc.create_bool_field_class() | |
28 | ||
29 | def test_create_default(self): | |
30 | self.assertIsNotNone(self._fc) | |
31 | ||
32 | ||
6b29f2d4 PP |
33 | class BitArrayFieldClassTestCase(unittest.TestCase): |
34 | def setUp(self): | |
35 | self._tc = get_default_trace_class() | |
36 | self._fc = self._tc.create_bit_array_field_class(17) | |
37 | ||
38 | def test_create_default(self): | |
39 | self.assertIsNotNone(self._fc) | |
40 | ||
41 | def test_create_length_out_of_range(self): | |
42 | with self.assertRaises(ValueError): | |
43 | self._tc.create_bit_array_field_class(65) | |
44 | ||
45 | def test_create_length_zero(self): | |
46 | with self.assertRaises(ValueError): | |
47 | self._tc.create_bit_array_field_class(0) | |
48 | ||
49 | def test_create_length_invalid_type(self): | |
50 | with self.assertRaises(TypeError): | |
51 | self._tc.create_bit_array_field_class('lel') | |
52 | ||
53 | def test_length_prop(self): | |
54 | self.assertEqual(self._fc.length, 17) | |
55 | ||
56 | ||
c8820b76 SM |
57 | class _TestIntegerFieldClassProps: |
58 | def test_create_default(self): | |
59 | fc = self._create_func() | |
60 | self.assertEqual(fc.field_value_range, 64) | |
61 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.DECIMAL) | |
32656995 | 62 | |
c8820b76 SM |
63 | def test_create_range(self): |
64 | fc = self._create_func(field_value_range=35) | |
65 | self.assertEqual(fc.field_value_range, 35) | |
32656995 | 66 | |
c8820b76 SM |
67 | fc = self._create_func(36) |
68 | self.assertEqual(fc.field_value_range, 36) | |
32656995 | 69 | |
c8820b76 SM |
70 | def test_create_invalid_range(self): |
71 | with self.assertRaises(TypeError): | |
72 | self._create_func('yes') | |
32656995 | 73 | |
c8820b76 SM |
74 | with self.assertRaises(TypeError): |
75 | self._create_func(field_value_range='yes') | |
32656995 | 76 | |
32656995 | 77 | with self.assertRaises(ValueError): |
c8820b76 | 78 | self._create_func(field_value_range=-2) |
32656995 | 79 | |
c8820b76 SM |
80 | with self.assertRaises(ValueError): |
81 | self._create_func(field_value_range=0) | |
32656995 | 82 | |
c8820b76 | 83 | def test_create_base(self): |
61d96b89 FD |
84 | fc = self._create_func( |
85 | preferred_display_base=bt2.IntegerDisplayBase.HEXADECIMAL | |
86 | ) | |
c8820b76 | 87 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.HEXADECIMAL) |
32656995 | 88 | |
c8820b76 | 89 | def test_create_invalid_base_type(self): |
32656995 | 90 | with self.assertRaises(TypeError): |
c8820b76 | 91 | self._create_func(preferred_display_base='yes') |
32656995 | 92 | |
c8820b76 SM |
93 | def test_create_invalid_base_value(self): |
94 | with self.assertRaises(ValueError): | |
95 | self._create_func(preferred_display_base=444) | |
32656995 | 96 | |
c8820b76 SM |
97 | def test_create_full(self): |
98 | fc = self._create_func(24, preferred_display_base=bt2.IntegerDisplayBase.OCTAL) | |
99 | self.assertEqual(fc.field_value_range, 24) | |
100 | self.assertEqual(fc.preferred_display_base, bt2.IntegerDisplayBase.OCTAL) | |
32656995 SM |
101 | |
102 | ||
c8820b76 SM |
103 | class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, unittest.TestCase): |
104 | def setUp(self): | |
105 | self._tc = get_default_trace_class() | |
106 | self._create_func = self._tc.create_signed_integer_field_class | |
32656995 | 107 | |
32656995 | 108 | |
c8820b76 SM |
109 | class RealFieldClassTestCase(unittest.TestCase): |
110 | def setUp(self): | |
111 | self._tc = get_default_trace_class() | |
32656995 | 112 | |
c8820b76 SM |
113 | def test_create_default(self): |
114 | fc = self._tc.create_real_field_class() | |
115 | self.assertFalse(fc.is_single_precision) | |
32656995 | 116 | |
c8820b76 SM |
117 | def test_create_is_single_precision(self): |
118 | fc = self._tc.create_real_field_class(is_single_precision=True) | |
119 | self.assertTrue(fc.is_single_precision) | |
120 | ||
121 | def test_create_invalid_is_single_precision(self): | |
32656995 | 122 | with self.assertRaises(TypeError): |
c8820b76 | 123 | self._tc.create_real_field_class(is_single_precision='hohoho') |
32656995 | 124 | |
32656995 | 125 | |
c8820b76 SM |
126 | # Converts an _EnumerationFieldClassMapping to a list of ranges: |
127 | # | |
128 | # [(lower0, upper0), (lower1, upper1), ...] | |
129 | ||
61d96b89 | 130 | |
02b61fe0 PP |
131 | def enum_mapping_to_set(mapping): |
132 | return {(x.lower, x.upper) for x in mapping.ranges} | |
32656995 | 133 | |
32656995 | 134 | |
02b61fe0 | 135 | class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps): |
c8820b76 SM |
136 | def setUp(self): |
137 | self._tc = get_default_trace_class() | |
02b61fe0 PP |
138 | self._spec_set_up() |
139 | self._fc = self._create_func() | |
c8820b76 SM |
140 | |
141 | def test_create_from_invalid_type(self): | |
32656995 | 142 | with self.assertRaises(TypeError): |
c8820b76 | 143 | self._create_func('coucou') |
32656995 | 144 | |
c8820b76 | 145 | def test_add_mapping_simple(self): |
02b61fe0 | 146 | self._fc.add_mapping('hello', self._ranges1) |
c8820b76 SM |
147 | mapping = self._fc['hello'] |
148 | self.assertEqual(mapping.label, 'hello') | |
02b61fe0 | 149 | self.assertEqual(mapping.ranges, self._ranges1) |
32656995 | 150 | |
c8820b76 | 151 | def test_add_mapping_simple_kwargs(self): |
02b61fe0 | 152 | self._fc.add_mapping(label='hello', ranges=self._ranges1) |
c8820b76 SM |
153 | mapping = self._fc['hello'] |
154 | self.assertEqual(mapping.label, 'hello') | |
02b61fe0 | 155 | self.assertEqual(mapping.ranges, self._ranges1) |
32656995 | 156 | |
02b61fe0 PP |
157 | def test_add_mapping_invalid_name(self): |
158 | with self.assertRaises(TypeError): | |
159 | self._fc.add_mapping(17, self._ranges1) | |
32656995 | 160 | |
02b61fe0 PP |
161 | def test_add_mapping_invalid_range(self): |
162 | with self.assertRaises(TypeError): | |
163 | self._fc.add_mapping('allo', 'meow') | |
c8820b76 | 164 | |
02b61fe0 | 165 | def test_add_mapping_dup_label(self): |
3b2be708 | 166 | with self.assertRaises(ValueError): |
02b61fe0 PP |
167 | self._fc.add_mapping('a', self._ranges1) |
168 | self._fc.add_mapping('a', self._ranges2) | |
c8820b76 | 169 | |
02b61fe0 | 170 | def test_add_mapping_invalid_ranges_signedness(self): |
32656995 | 171 | with self.assertRaises(TypeError): |
02b61fe0 | 172 | self._fc.add_mapping('allo', self._inval_ranges) |
32656995 | 173 | |
c8820b76 | 174 | def test_iadd(self): |
02b61fe0 | 175 | self._fc.add_mapping('c', self._ranges1) |
32656995 | 176 | |
61d96b89 | 177 | self._fc += [('d', self._ranges2), ('e', self._ranges3)] |
32656995 | 178 | |
02b61fe0 | 179 | self.assertEqual(len(self._fc), 3) |
c8820b76 | 180 | self.assertEqual(self._fc['c'].label, 'c') |
02b61fe0 | 181 | self.assertEqual(self._fc['c'].ranges, self._ranges1) |
c8820b76 | 182 | self.assertEqual(self._fc['d'].label, 'd') |
02b61fe0 | 183 | self.assertEqual(self._fc['d'].ranges, self._ranges2) |
c8820b76 | 184 | self.assertEqual(self._fc['e'].label, 'e') |
02b61fe0 | 185 | self.assertEqual(self._fc['e'].ranges, self._ranges3) |
32656995 | 186 | |
c8820b76 SM |
187 | def test_bool_op(self): |
188 | self.assertFalse(self._fc) | |
02b61fe0 | 189 | self._fc.add_mapping('a', self._ranges1) |
c8820b76 | 190 | self.assertTrue(self._fc) |
32656995 | 191 | |
c8820b76 | 192 | def test_len(self): |
02b61fe0 PP |
193 | self._fc.add_mapping('a', self._ranges1) |
194 | self._fc.add_mapping('b', self._ranges2) | |
195 | self._fc.add_mapping('c', self._ranges3) | |
c8820b76 | 196 | self.assertEqual(len(self._fc), 3) |
32656995 | 197 | |
c8820b76 | 198 | def test_getitem(self): |
02b61fe0 PP |
199 | self._fc.add_mapping('a', self._ranges1) |
200 | self._fc.add_mapping('b', self._ranges2) | |
201 | self._fc.add_mapping('c', self._ranges3) | |
c8820b76 | 202 | mapping = self._fc['a'] |
c8820b76 | 203 | self.assertEqual(mapping.label, 'a') |
02b61fe0 | 204 | self.assertEqual(mapping.ranges, self._ranges1) |
32656995 | 205 | |
02b61fe0 | 206 | def test_getitem_nonexistent(self): |
c8820b76 SM |
207 | with self.assertRaises(KeyError): |
208 | self._fc['doesnotexist'] | |
32656995 | 209 | |
c8820b76 | 210 | def test_iter(self): |
02b61fe0 PP |
211 | self._fc.add_mapping('a', self._ranges1) |
212 | self._fc.add_mapping('b', self._ranges2) | |
213 | self._fc.add_mapping('c', self._ranges3) | |
32656995 | 214 | |
c8820b76 SM |
215 | # This exercises iteration. |
216 | labels = sorted(self._fc) | |
32656995 | 217 | |
02b61fe0 | 218 | self.assertEqual(labels, ['a', 'b', 'c']) |
32656995 | 219 | |
c8820b76 | 220 | def test_find_by_value(self): |
02b61fe0 PP |
221 | self._fc.add_mapping('a', self._ranges1) |
222 | self._fc.add_mapping('b', self._ranges2) | |
223 | self._fc.add_mapping('c', self._ranges3) | |
224 | mappings = self._fc.mappings_for_value(self._value_in_range_1_and_3) | |
225 | labels = set([mapping.label for mapping in mappings]) | |
226 | expected_labels = set(['a', 'c']) | |
227 | self.assertEqual(labels, expected_labels) | |
228 | ||
229 | ||
61d96b89 FD |
230 | class UnsignedEnumerationFieldClassTestCase( |
231 | _EnumerationFieldClassTestCase, unittest.TestCase | |
232 | ): | |
02b61fe0 PP |
233 | def _spec_set_up(self): |
234 | self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) | |
235 | self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) | |
236 | self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 22), (48, 99)]) | |
237 | self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, -5), (48, 1928)]) | |
238 | self._value_in_range_1_and_3 = 20 | |
c8820b76 | 239 | self._create_func = self._tc.create_unsigned_enumeration_field_class |
32656995 | 240 | |
32656995 | 241 | |
61d96b89 FD |
242 | class SignedEnumerationFieldClassTestCase( |
243 | _EnumerationFieldClassTestCase, unittest.TestCase | |
244 | ): | |
02b61fe0 PP |
245 | def _spec_set_up(self): |
246 | self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) | |
247 | self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) | |
248 | self._ranges3 = bt2.SignedIntegerRangeSet([(-100, -1), (8, 16), (48, 99)]) | |
249 | self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
250 | self._value_in_range_1_and_3 = -7 | |
c8820b76 | 251 | self._create_func = self._tc.create_signed_enumeration_field_class |
32656995 | 252 | |
32656995 | 253 | |
c8820b76 | 254 | class StringFieldClassTestCase(unittest.TestCase): |
32656995 | 255 | def setUp(self): |
c8820b76 SM |
256 | tc = get_default_trace_class() |
257 | self._fc = tc.create_string_field_class() | |
32656995 SM |
258 | |
259 | def test_create_default(self): | |
c8820b76 | 260 | self.assertIsNotNone(self._fc) |
32656995 | 261 | |
32656995 | 262 | |
61d96b89 | 263 | class _TestElementContainer: |
02b61fe0 PP |
264 | def setUp(self): |
265 | self._tc = get_default_trace_class() | |
266 | self._fc = self._create_default_fc() | |
267 | ||
268 | def test_create_default(self): | |
269 | self.assertIsNotNone(self._fc) | |
270 | ||
c8820b76 SM |
271 | def test_append_element(self): |
272 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
273 | self._append_element_method(self._fc, 'int32', int_field_class) | |
02b61fe0 | 274 | field_class = self._fc['int32'].field_class |
c8820b76 | 275 | self.assertEqual(field_class.addr, int_field_class.addr) |
32656995 | 276 | |
02b61fe0 | 277 | def test_append_element_kwargs(self): |
c8820b76 SM |
278 | int_field_class = self._tc.create_signed_integer_field_class(32) |
279 | self._append_element_method(self._fc, name='int32', field_class=int_field_class) | |
02b61fe0 | 280 | field_class = self._fc['int32'].field_class |
c8820b76 SM |
281 | self.assertEqual(field_class.addr, int_field_class.addr) |
282 | ||
283 | def test_append_element_invalid_name(self): | |
284 | sub_fc = self._tc.create_string_field_class() | |
32656995 | 285 | |
32656995 | 286 | with self.assertRaises(TypeError): |
c8820b76 | 287 | self._append_element_method(self._fc, 23, sub_fc) |
32656995 | 288 | |
c8820b76 | 289 | def test_append_element_invalid_field_class(self): |
32656995 | 290 | with self.assertRaises(TypeError): |
c8820b76 | 291 | self._append_element_method(self._fc, 'yes', object()) |
32656995 | 292 | |
02b61fe0 PP |
293 | def test_append_element_dup_name(self): |
294 | sub_fc1 = self._tc.create_string_field_class() | |
295 | sub_fc2 = self._tc.create_string_field_class() | |
296 | ||
3b2be708 | 297 | with self.assertRaises(ValueError): |
02b61fe0 PP |
298 | self._append_element_method(self._fc, 'yes', sub_fc1) |
299 | self._append_element_method(self._fc, 'yes', sub_fc2) | |
300 | ||
32656995 | 301 | def test_iadd(self): |
02b61fe0 | 302 | other_fc = self._create_default_fc() |
c8820b76 SM |
303 | a_field_class = self._tc.create_real_field_class() |
304 | b_field_class = self._tc.create_signed_integer_field_class(17) | |
305 | self._append_element_method(self._fc, 'a_float', a_field_class) | |
306 | self._append_element_method(self._fc, 'b_int', b_field_class) | |
02b61fe0 | 307 | c_field_class = self._tc.create_string_field_class() |
61d96b89 FD |
308 | d_field_class = self._tc.create_signed_enumeration_field_class( |
309 | field_value_range=32 | |
310 | ) | |
02b61fe0 PP |
311 | e_field_class = self._tc.create_structure_field_class() |
312 | self._fc += [ | |
313 | ('c_string', c_field_class), | |
314 | ('d_enum', d_field_class), | |
315 | ('e_struct', e_field_class), | |
316 | ] | |
317 | self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr) | |
318 | self.assertEqual(self._fc['a_float'].name, 'a_float') | |
319 | self.assertEqual(self._fc['b_int'].field_class.addr, b_field_class.addr) | |
320 | self.assertEqual(self._fc['b_int'].name, 'b_int') | |
321 | self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr) | |
322 | self.assertEqual(self._fc['c_string'].name, 'c_string') | |
323 | self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr) | |
324 | self.assertEqual(self._fc['d_enum'].name, 'd_enum') | |
325 | self.assertEqual(self._fc['e_struct'].field_class.addr, e_field_class.addr) | |
326 | self.assertEqual(self._fc['e_struct'].name, 'e_struct') | |
32656995 SM |
327 | |
328 | def test_bool_op(self): | |
329 | self.assertFalse(self._fc) | |
c8820b76 | 330 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
32656995 SM |
331 | self.assertTrue(self._fc) |
332 | ||
333 | def test_len(self): | |
02b61fe0 PP |
334 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
335 | self._append_element_method(self._fc, 'b', self._tc.create_string_field_class()) | |
336 | self._append_element_method(self._fc, 'c', self._tc.create_string_field_class()) | |
32656995 SM |
337 | self.assertEqual(len(self._fc), 3) |
338 | ||
339 | def test_getitem(self): | |
c8820b76 SM |
340 | a_fc = self._tc.create_signed_integer_field_class(32) |
341 | b_fc = self._tc.create_string_field_class() | |
342 | c_fc = self._tc.create_real_field_class() | |
343 | self._append_element_method(self._fc, 'a', a_fc) | |
344 | self._append_element_method(self._fc, 'b', b_fc) | |
345 | self._append_element_method(self._fc, 'c', c_fc) | |
02b61fe0 PP |
346 | self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr) |
347 | self.assertEqual(self._fc['b'].name, 'b') | |
32656995 SM |
348 | |
349 | def test_getitem_invalid_key_type(self): | |
350 | with self.assertRaises(TypeError): | |
351 | self._fc[0] | |
352 | ||
353 | def test_getitem_invalid_key(self): | |
354 | with self.assertRaises(KeyError): | |
355 | self._fc['no way'] | |
356 | ||
357 | def test_contains(self): | |
358 | self.assertFalse('a' in self._fc) | |
c8820b76 | 359 | self._append_element_method(self._fc, 'a', self._tc.create_string_field_class()) |
32656995 SM |
360 | self.assertTrue('a' in self._fc) |
361 | ||
362 | def test_iter(self): | |
c8820b76 SM |
363 | a_fc = self._tc.create_signed_integer_field_class(32) |
364 | b_fc = self._tc.create_string_field_class() | |
365 | c_fc = self._tc.create_real_field_class() | |
61d96b89 | 366 | elements = (('a', a_fc), ('b', b_fc), ('c', c_fc)) |
32656995 | 367 | |
02b61fe0 PP |
368 | for elem in elements: |
369 | self._append_element_method(self._fc, *elem) | |
32656995 | 370 | |
02b61fe0 PP |
371 | for (name, element), test_elem in zip(self._fc.items(), elements): |
372 | self.assertEqual(element.name, test_elem[0]) | |
373 | self.assertEqual(name, element.name) | |
374 | self.assertEqual(element.field_class.addr, test_elem[1].addr) | |
32656995 SM |
375 | |
376 | def test_at_index(self): | |
c8820b76 SM |
377 | a_fc = self._tc.create_signed_integer_field_class(32) |
378 | b_fc = self._tc.create_string_field_class() | |
379 | c_fc = self._tc.create_real_field_class() | |
380 | self._append_element_method(self._fc, 'c', c_fc) | |
381 | self._append_element_method(self._fc, 'a', a_fc) | |
382 | self._append_element_method(self._fc, 'b', b_fc) | |
02b61fe0 PP |
383 | elem = self._at_index_method(self._fc, 1) |
384 | self.assertEqual(elem.field_class.addr, a_fc.addr) | |
385 | self.assertEqual(elem.name, 'a') | |
32656995 SM |
386 | |
387 | def test_at_index_invalid(self): | |
61d96b89 FD |
388 | self._append_element_method( |
389 | self._fc, 'c', self._tc.create_signed_integer_field_class(32) | |
390 | ) | |
32656995 SM |
391 | |
392 | with self.assertRaises(TypeError): | |
c8820b76 | 393 | self._at_index_method(self._fc, 'yes') |
32656995 SM |
394 | |
395 | def test_at_index_out_of_bounds_after(self): | |
61d96b89 FD |
396 | self._append_element_method( |
397 | self._fc, 'c', self._tc.create_signed_integer_field_class(32) | |
398 | ) | |
32656995 SM |
399 | |
400 | with self.assertRaises(IndexError): | |
c8820b76 | 401 | self._at_index_method(self._fc, len(self._fc)) |
32656995 SM |
402 | |
403 | ||
02b61fe0 | 404 | class StructureFieldClassTestCase(_TestElementContainer, unittest.TestCase): |
c946c9de PP |
405 | _append_element_method = staticmethod(bt2._StructureFieldClass.append_member) |
406 | _at_index_method = staticmethod(bt2._StructureFieldClass.member_at_index) | |
02b61fe0 PP |
407 | |
408 | def _create_default_fc(self): | |
409 | return self._tc.create_structure_field_class() | |
410 | ||
411 | ||
84eba0d9 PP |
412 | class OptionFieldClassTestCase(unittest.TestCase): |
413 | def setUp(self): | |
414 | self._tc = get_default_trace_class() | |
415 | self._content_fc = self._tc.create_signed_integer_field_class(23) | |
416 | self._tag_fc = self._tc.create_bool_field_class() | |
417 | ||
418 | def test_create_default(self): | |
419 | fc = self._tc.create_option_field_class(self._content_fc) | |
420 | self.assertEqual(fc.field_class.addr, self._content_fc.addr) | |
421 | self.assertIsNone(fc.selector_field_path, None) | |
422 | ||
423 | def _create_field_class_for_field_path_test(self): | |
424 | fc = self._tc.create_option_field_class(self._content_fc, self._tag_fc) | |
425 | ||
426 | foo_fc = self._tc.create_real_field_class() | |
427 | bar_fc = self._tc.create_string_field_class() | |
428 | baz_fc = self._tc.create_string_field_class() | |
429 | ||
430 | inner_struct_fc = self._tc.create_structure_field_class() | |
431 | inner_struct_fc.append_member('bar', bar_fc) | |
432 | inner_struct_fc.append_member('baz', baz_fc) | |
433 | inner_struct_fc.append_member('tag', self._tag_fc) | |
434 | inner_struct_fc.append_member('opt', fc) | |
435 | ||
436 | opt_struct_array_fc = self._tc.create_option_field_class(inner_struct_fc) | |
437 | ||
438 | outer_struct_fc = self._tc.create_structure_field_class() | |
439 | outer_struct_fc.append_member('foo', foo_fc) | |
440 | outer_struct_fc.append_member('inner_opt', opt_struct_array_fc) | |
441 | ||
442 | # The path to the selector field class is resolved when the | |
443 | # option field class is actually used, for example in a packet | |
444 | # context. | |
445 | self._tc.create_stream_class( | |
446 | packet_context_field_class=outer_struct_fc, supports_packets=True | |
447 | ) | |
448 | ||
449 | return fc | |
450 | ||
451 | def test_field_path_len(self): | |
452 | fc = self._create_field_class_for_field_path_test() | |
453 | self.assertEqual(len(fc.selector_field_path), 3) | |
454 | ||
455 | def test_field_path_iter(self): | |
456 | fc = self._create_field_class_for_field_path_test() | |
457 | path_items = list(fc.selector_field_path) | |
458 | ||
459 | self.assertEqual(len(path_items), 3) | |
460 | ||
461 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) | |
462 | self.assertEqual(path_items[0].index, 1) | |
463 | ||
464 | self.assertIsInstance(path_items[1], bt2._CurrentOptionContentFieldPathItem) | |
465 | ||
466 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) | |
467 | self.assertEqual(path_items[2].index, 2) | |
468 | ||
469 | def test_field_path_root_scope(self): | |
470 | fc = self._create_field_class_for_field_path_test() | |
471 | self.assertEqual( | |
472 | fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT | |
473 | ) | |
474 | ||
475 | def test_create_invalid_field_class(self): | |
476 | with self.assertRaises(TypeError): | |
477 | self._tc.create_option_field_class(object()) | |
478 | ||
479 | def test_create_invalid_selector_type(self): | |
480 | with self.assertRaises(TypeError): | |
481 | self._tc.create_option_field_class(self._content_fc, 17) | |
482 | ||
483 | ||
61d96b89 FD |
484 | class VariantFieldClassWithoutSelectorTestCase( |
485 | _TestElementContainer, unittest.TestCase | |
486 | ): | |
487 | _append_element_method = staticmethod( | |
c946c9de | 488 | bt2._VariantFieldClassWithoutSelector.append_option |
61d96b89 FD |
489 | ) |
490 | _at_index_method = staticmethod( | |
c946c9de | 491 | bt2._VariantFieldClassWithoutSelector.option_at_index |
61d96b89 | 492 | ) |
02b61fe0 PP |
493 | |
494 | def _create_default_fc(self): | |
495 | return self._tc.create_variant_field_class() | |
496 | ||
497 | ||
498 | class _VariantFieldClassWithSelectorTestCase: | |
32656995 | 499 | def setUp(self): |
c8820b76 | 500 | self._tc = get_default_trace_class() |
02b61fe0 PP |
501 | self._spec_set_up() |
502 | self._fc = self._create_default_fc() | |
503 | ||
504 | def _create_default_fc(self): | |
505 | return self._tc.create_variant_field_class(self._selector_fc) | |
c8820b76 SM |
506 | |
507 | def test_create_default(self): | |
508 | self.assertIsNotNone(self._fc) | |
32656995 | 509 | |
02b61fe0 PP |
510 | def test_append_element(self): |
511 | str_field_class = self._tc.create_string_field_class() | |
512 | self._fc.append_option('str', str_field_class, self._ranges1) | |
513 | opt = self._fc['str'] | |
514 | self.assertEqual(opt.field_class.addr, str_field_class.addr) | |
515 | self.assertEqual(opt.name, 'str') | |
516 | self.assertEqual(opt.ranges.addr, self._ranges1.addr) | |
517 | ||
518 | def test_append_element_kwargs(self): | |
519 | int_field_class = self._tc.create_signed_integer_field_class(32) | |
61d96b89 FD |
520 | self._fc.append_option( |
521 | name='int32', field_class=int_field_class, ranges=self._ranges1 | |
522 | ) | |
02b61fe0 PP |
523 | opt = self._fc['int32'] |
524 | self.assertEqual(opt.field_class.addr, int_field_class.addr) | |
525 | self.assertEqual(opt.name, 'int32') | |
526 | self.assertEqual(opt.ranges.addr, self._ranges1.addr) | |
c8820b76 | 527 | |
02b61fe0 PP |
528 | def test_append_element_invalid_name(self): |
529 | sub_fc = self._tc.create_string_field_class() | |
32656995 | 530 | |
02b61fe0 PP |
531 | with self.assertRaises(TypeError): |
532 | self._fc.append_option(self._fc, 23, sub_fc) | |
32656995 | 533 | |
02b61fe0 PP |
534 | def test_append_element_invalid_field_class(self): |
535 | with self.assertRaises(TypeError): | |
536 | self._fc.append_option(self._fc, 'yes', object()) | |
537 | ||
538 | def test_append_element_invalid_ranges(self): | |
539 | sub_fc = self._tc.create_string_field_class() | |
540 | ||
541 | with self.assertRaises(TypeError): | |
542 | self._fc.append_option(self._fc, sub_fc, 'lel') | |
543 | ||
544 | def test_append_element_dup_name(self): | |
545 | sub_fc1 = self._tc.create_string_field_class() | |
546 | sub_fc2 = self._tc.create_string_field_class() | |
547 | ||
3b2be708 | 548 | with self.assertRaises(ValueError): |
02b61fe0 PP |
549 | self._fc.append_option('yes', sub_fc1, self._ranges1) |
550 | self._fc.append_option('yes', sub_fc2, self._ranges2) | |
551 | ||
552 | def test_append_element_invalid_ranges_signedness(self): | |
553 | sub_fc = self._tc.create_string_field_class() | |
554 | ||
555 | with self.assertRaises(TypeError): | |
556 | self._fc.append_option(self._fc, sub_fc, self._inval_ranges) | |
557 | ||
558 | def test_iadd(self): | |
559 | other_fc = self._create_default_fc() | |
560 | a_field_class = self._tc.create_real_field_class() | |
561 | self._fc.append_option('a_float', a_field_class, self._ranges1) | |
562 | c_field_class = self._tc.create_string_field_class() | |
61d96b89 FD |
563 | d_field_class = self._tc.create_signed_enumeration_field_class( |
564 | field_value_range=32 | |
565 | ) | |
02b61fe0 PP |
566 | self._fc += [ |
567 | ('c_string', c_field_class, self._ranges2), | |
568 | ('d_enum', d_field_class, self._ranges3), | |
569 | ] | |
570 | self.assertEqual(self._fc['a_float'].field_class.addr, a_field_class.addr) | |
571 | self.assertEqual(self._fc['a_float'].name, 'a_float') | |
572 | self.assertEqual(self._fc['a_float'].ranges, self._ranges1) | |
573 | self.assertEqual(self._fc['c_string'].field_class.addr, c_field_class.addr) | |
574 | self.assertEqual(self._fc['c_string'].name, 'c_string') | |
575 | self.assertEqual(self._fc['c_string'].ranges, self._ranges2) | |
576 | self.assertEqual(self._fc['d_enum'].field_class.addr, d_field_class.addr) | |
577 | self.assertEqual(self._fc['d_enum'].name, 'd_enum') | |
578 | self.assertEqual(self._fc['d_enum'].ranges, self._ranges3) | |
579 | ||
580 | def test_bool_op(self): | |
581 | self.assertFalse(self._fc) | |
582 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
583 | self.assertTrue(self._fc) | |
584 | ||
585 | def test_len(self): | |
586 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
587 | self._fc.append_option('b', self._tc.create_string_field_class(), self._ranges2) | |
588 | self._fc.append_option('c', self._tc.create_string_field_class(), self._ranges3) | |
589 | self.assertEqual(len(self._fc), 3) | |
590 | ||
591 | def test_getitem(self): | |
592 | a_fc = self._tc.create_signed_integer_field_class(32) | |
593 | b_fc = self._tc.create_string_field_class() | |
594 | c_fc = self._tc.create_real_field_class() | |
595 | self._fc.append_option('a', a_fc, self._ranges1) | |
596 | self._fc.append_option('b', b_fc, self._ranges2) | |
597 | self._fc.append_option('c', c_fc, self._ranges3) | |
598 | self.assertEqual(self._fc['b'].field_class.addr, b_fc.addr) | |
599 | self.assertEqual(self._fc['b'].name, 'b') | |
600 | self.assertEqual(self._fc['b'].ranges.addr, self._ranges2.addr) | |
601 | ||
602 | def test_getitem_invalid_key_type(self): | |
603 | with self.assertRaises(TypeError): | |
604 | self._fc[0] | |
605 | ||
606 | def test_getitem_invalid_key(self): | |
607 | with self.assertRaises(KeyError): | |
608 | self._fc['no way'] | |
609 | ||
610 | def test_contains(self): | |
611 | self.assertFalse('a' in self._fc) | |
612 | self._fc.append_option('a', self._tc.create_string_field_class(), self._ranges1) | |
613 | self.assertTrue('a' in self._fc) | |
614 | ||
615 | def test_iter(self): | |
616 | a_fc = self._tc.create_signed_integer_field_class(32) | |
617 | b_fc = self._tc.create_string_field_class() | |
618 | c_fc = self._tc.create_real_field_class() | |
619 | opts = ( | |
620 | ('a', a_fc, self._ranges1), | |
621 | ('b', b_fc, self._ranges2), | |
622 | ('c', c_fc, self._ranges3), | |
623 | ) | |
624 | ||
625 | for opt in opts: | |
626 | self._fc.append_option(*opt) | |
32656995 | 627 | |
02b61fe0 PP |
628 | for (name, opt), test_opt in zip(self._fc.items(), opts): |
629 | self.assertEqual(opt.name, test_opt[0]) | |
630 | self.assertEqual(name, opt.name) | |
631 | self.assertEqual(opt.field_class.addr, test_opt[1].addr) | |
632 | self.assertEqual(opt.ranges.addr, test_opt[2].addr) | |
633 | ||
634 | def test_at_index(self): | |
635 | a_fc = self._tc.create_signed_integer_field_class(32) | |
636 | b_fc = self._tc.create_string_field_class() | |
637 | c_fc = self._tc.create_real_field_class() | |
638 | self._fc.append_option('c', c_fc, self._ranges1) | |
639 | self._fc.append_option('a', a_fc, self._ranges2) | |
640 | self._fc.append_option('b', b_fc, self._ranges3) | |
641 | self.assertEqual(self._fc.option_at_index(1).field_class.addr, a_fc.addr) | |
642 | self.assertEqual(self._fc.option_at_index(1).name, 'a') | |
643 | self.assertEqual(self._fc.option_at_index(1).ranges.addr, self._ranges2.addr) | |
644 | ||
645 | def test_at_index_invalid(self): | |
61d96b89 FD |
646 | self._fc.append_option( |
647 | 'c', self._tc.create_signed_integer_field_class(32), self._ranges3 | |
648 | ) | |
02b61fe0 PP |
649 | |
650 | with self.assertRaises(TypeError): | |
651 | self._fc.option_at_index('yes') | |
652 | ||
653 | def test_at_index_out_of_bounds_after(self): | |
61d96b89 FD |
654 | self._fc.append_option( |
655 | 'c', self._tc.create_signed_integer_field_class(32), self._ranges3 | |
656 | ) | |
02b61fe0 PP |
657 | |
658 | with self.assertRaises(IndexError): | |
659 | self._fc.option_at_index(len(self._fc)) | |
660 | ||
661 | def _fill_default_fc_for_field_path_test(self): | |
c8820b76 SM |
662 | # Create something equivalent to: |
663 | # | |
664 | # struct outer_struct_fc { | |
665 | # real foo; | |
666 | # struct inner_struct_fc { | |
02b61fe0 | 667 | # [u]int64_t selector; |
c8820b76 SM |
668 | # string bar; |
669 | # string baz; | |
02b61fe0 PP |
670 | # variant <selector> { |
671 | # real a; // selected with self._ranges1 | |
672 | # int21_t b; // selected with self._ranges2 | |
673 | # uint34_t c; // selected with self._ranges3 | |
c8820b76 SM |
674 | # } variant; |
675 | # } inner_struct[2]; | |
676 | # }; | |
02b61fe0 | 677 | self._fc.append_option('a', self._tc.create_real_field_class(), self._ranges1) |
61d96b89 FD |
678 | self._fc.append_option( |
679 | 'b', self._tc.create_signed_integer_field_class(21), self._ranges2 | |
680 | ) | |
681 | self._fc.append_option( | |
682 | 'c', self._tc.create_unsigned_integer_field_class(34), self._ranges3 | |
683 | ) | |
32656995 | 684 | |
c8820b76 SM |
685 | foo_fc = self._tc.create_real_field_class() |
686 | bar_fc = self._tc.create_string_field_class() | |
687 | baz_fc = self._tc.create_string_field_class() | |
32656995 | 688 | |
c8820b76 | 689 | inner_struct_fc = self._tc.create_structure_field_class() |
02b61fe0 | 690 | inner_struct_fc.append_member('selector', self._selector_fc) |
c8820b76 SM |
691 | inner_struct_fc.append_member('bar', bar_fc) |
692 | inner_struct_fc.append_member('baz', baz_fc) | |
02b61fe0 | 693 | inner_struct_fc.append_member('variant', self._fc) |
32656995 | 694 | |
61d96b89 FD |
695 | inner_struct_array_fc = self._tc.create_static_array_field_class( |
696 | inner_struct_fc, 2 | |
697 | ) | |
32656995 | 698 | |
c8820b76 SM |
699 | outer_struct_fc = self._tc.create_structure_field_class() |
700 | outer_struct_fc.append_member('foo', foo_fc) | |
701 | outer_struct_fc.append_member('inner_struct', inner_struct_array_fc) | |
32656995 | 702 | |
c8820b76 SM |
703 | # The path to the selector field is resolved when the sequence is |
704 | # actually used, for example in a packet context. | |
61d96b89 FD |
705 | self._tc.create_stream_class( |
706 | supports_packets=True, packet_context_field_class=outer_struct_fc | |
707 | ) | |
32656995 | 708 | |
c8820b76 | 709 | def test_selector_field_path_length(self): |
02b61fe0 PP |
710 | self._fill_default_fc_for_field_path_test() |
711 | self.assertEqual(len(self._fc.selector_field_path), 3) | |
32656995 | 712 | |
c8820b76 | 713 | def test_selector_field_path_iter(self): |
02b61fe0 PP |
714 | self._fill_default_fc_for_field_path_test() |
715 | path_items = list(self._fc.selector_field_path) | |
32656995 | 716 | |
c8820b76 | 717 | self.assertEqual(len(path_items), 3) |
32656995 | 718 | |
c946c9de | 719 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
c8820b76 | 720 | self.assertEqual(path_items[0].index, 1) |
32656995 | 721 | |
c946c9de | 722 | self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem) |
32656995 | 723 | |
c946c9de | 724 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
c8820b76 | 725 | self.assertEqual(path_items[2].index, 0) |
32656995 | 726 | |
c8820b76 | 727 | def test_selector_field_path_root_scope(self): |
02b61fe0 | 728 | self._fill_default_fc_for_field_path_test() |
61d96b89 | 729 | self.assertEqual( |
056995e5 | 730 | self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT |
61d96b89 | 731 | ) |
02b61fe0 PP |
732 | |
733 | ||
61d96b89 FD |
734 | class VariantFieldClassWithUnsignedSelectorTestCase( |
735 | _VariantFieldClassWithSelectorTestCase, unittest.TestCase | |
736 | ): | |
02b61fe0 PP |
737 | def _spec_set_up(self): |
738 | self._ranges1 = bt2.UnsignedIntegerRangeSet([(1, 4), (18, 47)]) | |
739 | self._ranges2 = bt2.UnsignedIntegerRangeSet([(5, 5)]) | |
740 | self._ranges3 = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
741 | self._inval_ranges = bt2.SignedIntegerRangeSet([(-8, 16), (48, 99)]) | |
742 | self._selector_fc = self._tc.create_unsigned_integer_field_class() | |
743 | ||
744 | ||
61d96b89 FD |
745 | class VariantFieldClassWithSignedSelectorTestCase( |
746 | _VariantFieldClassWithSelectorTestCase, unittest.TestCase | |
747 | ): | |
02b61fe0 PP |
748 | def _spec_set_up(self): |
749 | self._ranges1 = bt2.SignedIntegerRangeSet([(-10, -4), (18, 47)]) | |
750 | self._ranges2 = bt2.SignedIntegerRangeSet([(-3, -3)]) | |
751 | self._ranges3 = bt2.SignedIntegerRangeSet([(8, 16), (48, 99)]) | |
752 | self._inval_ranges = bt2.UnsignedIntegerRangeSet([(8, 16), (48, 99)]) | |
753 | self._selector_fc = self._tc.create_signed_integer_field_class() | |
32656995 | 754 | |
c8820b76 SM |
755 | |
756 | class StaticArrayFieldClassTestCase(unittest.TestCase): | |
757 | def setUp(self): | |
758 | self._tc = get_default_trace_class() | |
759 | self._elem_fc = self._tc.create_signed_integer_field_class(23) | |
32656995 SM |
760 | |
761 | def test_create_default(self): | |
c8820b76 SM |
762 | fc = self._tc.create_static_array_field_class(self._elem_fc, 45) |
763 | self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) | |
764 | self.assertEqual(fc.length, 45) | |
32656995 | 765 | |
c8820b76 | 766 | def test_create_invalid_elem_field_class(self): |
32656995 | 767 | with self.assertRaises(TypeError): |
c8820b76 | 768 | self._tc.create_static_array_field_class(object(), 45) |
32656995 SM |
769 | |
770 | def test_create_invalid_length(self): | |
771 | with self.assertRaises(ValueError): | |
61d96b89 FD |
772 | self._tc.create_static_array_field_class( |
773 | self._tc.create_string_field_class(), -17 | |
774 | ) | |
32656995 SM |
775 | |
776 | def test_create_invalid_length_type(self): | |
777 | with self.assertRaises(TypeError): | |
61d96b89 FD |
778 | self._tc.create_static_array_field_class( |
779 | self._tc.create_string_field_class(), 'the length' | |
780 | ) | |
c8820b76 SM |
781 | |
782 | ||
783 | class DynamicArrayFieldClassTestCase(unittest.TestCase): | |
784 | def setUp(self): | |
785 | self._tc = get_default_trace_class() | |
786 | self._elem_fc = self._tc.create_signed_integer_field_class(23) | |
787 | self._len_fc = self._tc.create_unsigned_integer_field_class(12) | |
32656995 | 788 | |
c8820b76 SM |
789 | def test_create_default(self): |
790 | fc = self._tc.create_dynamic_array_field_class(self._elem_fc) | |
791 | self.assertEqual(fc.element_field_class.addr, self._elem_fc.addr) | |
792 | self.assertIsNone(fc.length_field_path, None) | |
32656995 | 793 | |
c8820b76 SM |
794 | def _create_field_class_for_field_path_test(self): |
795 | # Create something a field class that is equivalent to: | |
796 | # | |
797 | # struct outer_struct_fc { | |
798 | # real foo; | |
799 | # struct inner_struct_fc { | |
800 | # string bar; | |
801 | # string baz; | |
802 | # uint12_t len; | |
803 | # uint23_t dyn_array[len]; | |
804 | # } inner_struct[2]; | |
805 | # }; | |
32656995 | 806 | |
c8820b76 | 807 | fc = self._tc.create_dynamic_array_field_class(self._elem_fc, self._len_fc) |
32656995 | 808 | |
c8820b76 SM |
809 | foo_fc = self._tc.create_real_field_class() |
810 | bar_fc = self._tc.create_string_field_class() | |
811 | baz_fc = self._tc.create_string_field_class() | |
32656995 | 812 | |
c8820b76 SM |
813 | inner_struct_fc = self._tc.create_structure_field_class() |
814 | inner_struct_fc.append_member('bar', bar_fc) | |
815 | inner_struct_fc.append_member('baz', baz_fc) | |
816 | inner_struct_fc.append_member('len', self._len_fc) | |
817 | inner_struct_fc.append_member('dyn_array', fc) | |
32656995 | 818 | |
61d96b89 FD |
819 | inner_struct_array_fc = self._tc.create_static_array_field_class( |
820 | inner_struct_fc, 2 | |
821 | ) | |
c8820b76 SM |
822 | |
823 | outer_struct_fc = self._tc.create_structure_field_class() | |
824 | outer_struct_fc.append_member('foo', foo_fc) | |
825 | outer_struct_fc.append_member('inner_struct', inner_struct_array_fc) | |
826 | ||
827 | # The path to the length field is resolved when the sequence is | |
828 | # actually used, for example in a packet context. | |
61d96b89 FD |
829 | self._tc.create_stream_class( |
830 | packet_context_field_class=outer_struct_fc, supports_packets=True | |
831 | ) | |
c8820b76 SM |
832 | |
833 | return fc | |
834 | ||
835 | def test_field_path_len(self): | |
836 | fc = self._create_field_class_for_field_path_test() | |
837 | self.assertEqual(len(fc.length_field_path), 3) | |
838 | ||
839 | def test_field_path_iter(self): | |
840 | fc = self._create_field_class_for_field_path_test() | |
841 | path_items = list(fc.length_field_path) | |
842 | ||
843 | self.assertEqual(len(path_items), 3) | |
844 | ||
c946c9de | 845 | self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem) |
c8820b76 SM |
846 | self.assertEqual(path_items[0].index, 1) |
847 | ||
c946c9de | 848 | self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem) |
c8820b76 | 849 | |
c946c9de | 850 | self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem) |
c8820b76 SM |
851 | self.assertEqual(path_items[2].index, 2) |
852 | ||
853 | def test_field_path_root_scope(self): | |
854 | fc = self._create_field_class_for_field_path_test() | |
056995e5 PP |
855 | self.assertEqual( |
856 | fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT | |
857 | ) | |
32656995 SM |
858 | |
859 | def test_create_invalid_field_class(self): | |
860 | with self.assertRaises(TypeError): | |
c8820b76 | 861 | self._tc.create_dynamic_array_field_class(object()) |
32656995 SM |
862 | |
863 | def test_create_invalid_length_type(self): | |
864 | with self.assertRaises(TypeError): | |
61d96b89 FD |
865 | self._tc.create_dynamic_array_field_class( |
866 | self._tc.create_string_field_class(), 17 | |
867 | ) | |
32656995 | 868 | |
32656995 | 869 | |
c8820b76 SM |
870 | if __name__ == "__main__": |
871 | unittest.main() |