Commit | Line | Data |
---|---|---|
c4239792 | 1 | import bt2.field |
b4f45851 SM |
2 | import unittest |
3 | import copy | |
4 | import bt2 | |
5 | ||
6 | ||
7 | class _TestCopySimple: | |
8 | def _test_copy(self, cpy): | |
9 | self.assertIsNot(cpy, self._fc) | |
10 | self.assertNotEqual(cpy.addr, self._fc.addr) | |
11 | self.assertEqual(cpy, self._fc) | |
12 | ||
13 | def test_copy(self): | |
14 | cpy = copy.copy(self._fc) | |
15 | self._test_copy(cpy) | |
16 | ||
17 | def test_deepcopy(self): | |
18 | cpy = copy.deepcopy(self._fc) | |
19 | self._test_copy(cpy) | |
20 | ||
21 | ||
22 | class _TestAlignmentProp: | |
23 | def test_assign_alignment(self): | |
24 | self._fc.alignment = 32 | |
25 | self.assertEqual(self._fc.alignment, 32) | |
26 | ||
27 | def test_assign_invalid_alignment(self): | |
28 | with self.assertRaises(ValueError): | |
29 | self._fc.alignment = 23 | |
30 | ||
31 | ||
32 | class _TestByteOrderProp: | |
33 | def test_assign_byte_order(self): | |
34 | self._fc.byte_order = bt2.ByteOrder.LITTLE_ENDIAN | |
35 | self.assertEqual(self._fc.byte_order, bt2.ByteOrder.LITTLE_ENDIAN) | |
36 | ||
37 | def test_assign_invalid_byte_order(self): | |
38 | with self.assertRaises(TypeError): | |
39 | self._fc.byte_order = 'hey' | |
40 | ||
41 | ||
42 | class _TestInvalidEq: | |
43 | def test_eq_invalid(self): | |
44 | self.assertFalse(self._fc == 23) | |
45 | ||
46 | ||
47 | class _TestIntegerFieldClassProps: | |
48 | def test_size_prop(self): | |
49 | self.assertEqual(self._fc.size, 35) | |
50 | ||
51 | def test_assign_signed(self): | |
52 | self._fc.is_signed = True | |
53 | self.assertTrue(self._fc.is_signed) | |
54 | ||
55 | def test_assign_invalid_signed(self): | |
56 | with self.assertRaises(TypeError): | |
57 | self._fc.is_signed = 23 | |
58 | ||
59 | def test_assign_base(self): | |
60 | self._fc.base = bt2.Base.HEXADECIMAL | |
61 | self.assertEqual(self._fc.base, bt2.Base.HEXADECIMAL) | |
62 | ||
63 | def test_assign_invalid_base(self): | |
64 | with self.assertRaises(TypeError): | |
65 | self._fc.base = 'hey' | |
66 | ||
67 | def test_assign_encoding(self): | |
68 | self._fc.encoding = bt2.Encoding.UTF8 | |
69 | self.assertEqual(self._fc.encoding, bt2.Encoding.UTF8) | |
70 | ||
71 | def test_assign_invalid_encoding(self): | |
72 | with self.assertRaises(TypeError): | |
73 | self._fc.encoding = 'hey' | |
74 | ||
75 | def test_assign_mapped_clock_class(self): | |
76 | cc = bt2.ClockClass('name', 1000) | |
77 | self._fc.mapped_clock_class = cc | |
78 | self.assertEqual(self._fc.mapped_clock_class, cc) | |
79 | ||
80 | def test_assign_invalid_mapped_clock_class(self): | |
81 | with self.assertRaises(TypeError): | |
82 | self._fc.mapped_clock_class = object() | |
83 | ||
84 | ||
85 | @unittest.skip("this is broken") | |
86 | class IntegerFieldClassTestCase(_TestIntegerFieldClassProps, _TestCopySimple, | |
87 | _TestAlignmentProp, _TestByteOrderProp, | |
88 | _TestInvalidEq, unittest.TestCase): | |
89 | def setUp(self): | |
90 | self._fc = bt2.IntegerFieldClass(35) | |
91 | ||
92 | def tearDown(self): | |
93 | del self._fc | |
94 | ||
95 | def test_create_default(self): | |
96 | self.assertEqual(self._fc.size, 35) | |
97 | self.assertIsNone(self._fc.mapped_clock_class) | |
98 | ||
99 | def test_create_invalid_size(self): | |
100 | with self.assertRaises(TypeError): | |
101 | fc = bt2.IntegerFieldClass('yes') | |
102 | ||
103 | def test_create_neg_size(self): | |
104 | with self.assertRaises(ValueError): | |
105 | fc = bt2.IntegerFieldClass(-2) | |
106 | ||
107 | def test_create_neg_zero(self): | |
108 | with self.assertRaises(ValueError): | |
109 | fc = bt2.IntegerFieldClass(0) | |
110 | ||
111 | def test_create_full(self): | |
112 | cc = bt2.ClockClass('name', 1000) | |
113 | fc = bt2.IntegerFieldClass(24, alignment=16, | |
114 | byte_order=bt2.ByteOrder.BIG_ENDIAN, | |
115 | is_signed=True, base=bt2.Base.OCTAL, | |
116 | encoding=bt2.Encoding.NONE, | |
117 | mapped_clock_class=cc) | |
118 | self.assertEqual(fc.size, 24) | |
119 | self.assertEqual(fc.alignment, 16) | |
120 | self.assertEqual(fc.byte_order, bt2.ByteOrder.BIG_ENDIAN) | |
121 | self.assertTrue(fc.is_signed) | |
122 | self.assertEqual(fc.base, bt2.Base.OCTAL) | |
123 | self.assertEqual(fc.encoding, bt2.Encoding.NONE) | |
124 | self.assertEqual(fc.mapped_clock_class, cc) | |
125 | ||
126 | def test_create_field(self): | |
127 | field = self._fc() | |
c4239792 | 128 | self.assertIsInstance(field, bt2.field._IntegerField) |
b4f45851 SM |
129 | |
130 | def test_create_field_init(self): | |
131 | field = self._fc(23) | |
132 | self.assertEqual(field, 23) | |
133 | ||
134 | ||
135 | @unittest.skip("this is broken") | |
136 | class FloatingPointNumberFieldClassTestCase(_TestCopySimple, _TestAlignmentProp, | |
137 | _TestByteOrderProp, _TestInvalidEq, | |
138 | unittest.TestCase): | |
139 | def setUp(self): | |
140 | self._fc = bt2.FloatingPointNumberFieldClass() | |
141 | ||
142 | def tearDown(self): | |
143 | del self._fc | |
144 | ||
145 | def test_create_default(self): | |
146 | pass | |
147 | ||
148 | def test_create_full(self): | |
149 | fc = bt2.FloatingPointNumberFieldClass(alignment=16, | |
150 | byte_order=bt2.ByteOrder.BIG_ENDIAN, | |
151 | exponent_size=11, | |
152 | mantissa_size=53) | |
153 | self.assertEqual(fc.alignment, 16) | |
154 | self.assertEqual(fc.byte_order, bt2.ByteOrder.BIG_ENDIAN) | |
155 | self.assertEqual(fc.exponent_size, 11) | |
156 | self.assertEqual(fc.mantissa_size, 53) | |
157 | ||
158 | def test_assign_exponent_size(self): | |
159 | self._fc.exponent_size = 8 | |
160 | self.assertEqual(self._fc.exponent_size, 8) | |
161 | ||
162 | def test_assign_invalid_exponent_size(self): | |
163 | with self.assertRaises(TypeError): | |
164 | self._fc.exponent_size = 'yes' | |
165 | ||
166 | def test_assign_mantissa_size(self): | |
167 | self._fc.mantissa_size = 24 | |
168 | self.assertEqual(self._fc.mantissa_size, 24) | |
169 | ||
170 | def test_assign_invalid_mantissa_size(self): | |
171 | with self.assertRaises(TypeError): | |
172 | self._fc.mantissa_size = 'no' | |
173 | ||
174 | def test_create_field(self): | |
175 | field = self._fc() | |
c4239792 | 176 | self.assertIsInstance(field, bt2.field._FloatingPointNumberField) |
b4f45851 SM |
177 | |
178 | def test_create_field_init(self): | |
179 | field = self._fc(17.5) | |
180 | self.assertEqual(field, 17.5) | |
181 | ||
182 | ||
183 | @unittest.skip("this is broken") | |
184 | class EnumerationFieldClassTestCase(_TestIntegerFieldClassProps, _TestInvalidEq, | |
185 | _TestCopySimple, _TestAlignmentProp, | |
186 | _TestByteOrderProp, unittest.TestCase): | |
187 | def setUp(self): | |
188 | self._fc = bt2.EnumerationFieldClass(size=35) | |
189 | ||
190 | def tearDown(self): | |
191 | del self._fc | |
192 | ||
193 | def test_create_from_int_fc(self): | |
194 | int_fc = bt2.IntegerFieldClass(23) | |
195 | self._fc = bt2.EnumerationFieldClass(int_fc) | |
196 | ||
197 | def test_create_from_invalid_type(self): | |
198 | with self.assertRaises(TypeError): | |
199 | self._fc = bt2.EnumerationFieldClass('coucou') | |
200 | ||
201 | def test_create_from_invalid_fc(self): | |
202 | with self.assertRaises(TypeError): | |
203 | fc = bt2.FloatingPointNumberFieldClass() | |
204 | self._fc = bt2.EnumerationFieldClass(fc) | |
205 | ||
206 | def test_create_full(self): | |
207 | fc = bt2.EnumerationFieldClass(size=24, alignment=16, | |
208 | byte_order=bt2.ByteOrder.BIG_ENDIAN, | |
209 | is_signed=True, base=bt2.Base.OCTAL, | |
210 | encoding=bt2.Encoding.NONE, | |
211 | mapped_clock_class=None) | |
212 | self.assertEqual(fc.size, 24) | |
213 | self.assertEqual(fc.alignment, 16) | |
214 | self.assertEqual(fc.byte_order, bt2.ByteOrder.BIG_ENDIAN) | |
215 | self.assertTrue(fc.is_signed) | |
216 | self.assertEqual(fc.base, bt2.Base.OCTAL) | |
217 | self.assertEqual(fc.encoding, bt2.Encoding.NONE) | |
218 | #self.assertIsNone(fc.mapped_clock_class) | |
219 | ||
220 | def test_integer_field_class_prop(self): | |
221 | int_fc = bt2.IntegerFieldClass(23) | |
222 | enum_fc = bt2.EnumerationFieldClass(int_fc) | |
223 | self.assertEqual(enum_fc.integer_field_class.addr, int_fc.addr) | |
224 | ||
225 | def test_add_mapping_simple(self): | |
226 | self._fc.add_mapping('hello', 24) | |
227 | mapping = self._fc[0] | |
228 | self.assertEqual(mapping.name, 'hello') | |
229 | self.assertEqual(mapping.lower, 24) | |
230 | self.assertEqual(mapping.upper, 24) | |
231 | ||
232 | def test_add_mapping_simple_kwargs(self): | |
233 | self._fc.add_mapping(name='hello', lower=17, upper=23) | |
234 | mapping = self._fc[0] | |
235 | self.assertEqual(mapping.name, 'hello') | |
236 | self.assertEqual(mapping.lower, 17) | |
237 | self.assertEqual(mapping.upper, 23) | |
238 | ||
239 | def test_add_mapping_range(self): | |
240 | self._fc.add_mapping('hello', 21, 199) | |
241 | mapping = self._fc[0] | |
242 | self.assertEqual(mapping.name, 'hello') | |
243 | self.assertEqual(mapping.lower, 21) | |
244 | self.assertEqual(mapping.upper, 199) | |
245 | ||
246 | def test_add_mapping_invalid_name(self): | |
247 | with self.assertRaises(TypeError): | |
248 | self._fc.add_mapping(17, 21, 199) | |
249 | ||
250 | def test_add_mapping_invalid_signedness_lower(self): | |
251 | with self.assertRaises(ValueError): | |
252 | self._fc.add_mapping('hello', -21, 199) | |
253 | ||
254 | def test_add_mapping_invalid_signedness_upper(self): | |
255 | with self.assertRaises(ValueError): | |
256 | self._fc.add_mapping('hello', 21, -199) | |
257 | ||
258 | def test_add_mapping_simple_signed(self): | |
259 | self._fc.is_signed = True | |
260 | self._fc.add_mapping('hello', -24) | |
261 | mapping = self._fc[0] | |
262 | self.assertEqual(mapping.name, 'hello') | |
263 | self.assertEqual(mapping.lower, -24) | |
264 | self.assertEqual(mapping.upper, -24) | |
265 | ||
266 | def test_add_mapping_range_signed(self): | |
267 | self._fc.is_signed = True | |
268 | self._fc.add_mapping('hello', -21, 199) | |
269 | mapping = self._fc[0] | |
270 | self.assertEqual(mapping.name, 'hello') | |
271 | self.assertEqual(mapping.lower, -21) | |
272 | self.assertEqual(mapping.upper, 199) | |
273 | ||
274 | def test_iadd(self): | |
275 | enum_fc = bt2.EnumerationFieldClass(size=16) | |
276 | enum_fc.add_mapping('c', 4, 5) | |
277 | enum_fc.add_mapping('d', 6, 18) | |
278 | enum_fc.add_mapping('e', 20, 27) | |
279 | self._fc.add_mapping('a', 0, 2) | |
280 | self._fc.add_mapping('b', 3) | |
281 | self._fc += enum_fc | |
282 | self.assertEqual(self._fc[0].name, 'a') | |
283 | self.assertEqual(self._fc[0].lower, 0) | |
284 | self.assertEqual(self._fc[0].upper, 2) | |
285 | self.assertEqual(self._fc[1].name, 'b') | |
286 | self.assertEqual(self._fc[1].lower, 3) | |
287 | self.assertEqual(self._fc[1].upper, 3) | |
288 | self.assertEqual(self._fc[2].name, 'c') | |
289 | self.assertEqual(self._fc[2].lower, 4) | |
290 | self.assertEqual(self._fc[2].upper, 5) | |
291 | self.assertEqual(self._fc[3].name, 'd') | |
292 | self.assertEqual(self._fc[3].lower, 6) | |
293 | self.assertEqual(self._fc[3].upper, 18) | |
294 | self.assertEqual(self._fc[4].name, 'e') | |
295 | self.assertEqual(self._fc[4].lower, 20) | |
296 | self.assertEqual(self._fc[4].upper, 27) | |
297 | ||
298 | def test_bool_op(self): | |
299 | self.assertFalse(self._fc) | |
300 | self._fc.add_mapping('a', 0) | |
301 | self.assertTrue(self._fc) | |
302 | ||
303 | def test_len(self): | |
304 | self._fc.add_mapping('a', 0) | |
305 | self._fc.add_mapping('b', 1) | |
306 | self._fc.add_mapping('c', 2) | |
307 | self.assertEqual(len(self._fc), 3) | |
308 | ||
309 | def test_getitem(self): | |
310 | self._fc.add_mapping('a', 0) | |
311 | self._fc.add_mapping('b', 1, 3) | |
312 | self._fc.add_mapping('c', 5) | |
313 | mapping = self._fc[1] | |
314 | self.assertEqual(mapping.name, 'b') | |
315 | self.assertEqual(mapping.lower, 1) | |
316 | self.assertEqual(mapping.upper, 3) | |
317 | ||
318 | def test_iter(self): | |
319 | mappings = ( | |
320 | ('a', 1, 5), | |
321 | ('b', 10, 17), | |
322 | ('c', 20, 1504), | |
323 | ('d', 22510, 99999), | |
324 | ) | |
325 | ||
326 | for mapping in mappings: | |
327 | self._fc.add_mapping(*mapping) | |
328 | ||
329 | for fc_mapping, mapping in zip(self._fc, mappings): | |
330 | self.assertEqual(fc_mapping.name, mapping[0]) | |
331 | self.assertEqual(fc_mapping.lower, mapping[1]) | |
332 | self.assertEqual(fc_mapping.upper, mapping[2]) | |
333 | ||
334 | def test_mapping_eq(self): | |
335 | enum1 = bt2.EnumerationFieldClass(size=32) | |
336 | enum2 = bt2.EnumerationFieldClass(size=16) | |
337 | enum1.add_mapping('b', 1, 3) | |
338 | enum2.add_mapping('b', 1, 3) | |
339 | self.assertEqual(enum1[0], enum2[0]) | |
340 | ||
341 | def test_mapping_eq_invalid(self): | |
342 | enum1 = bt2.EnumerationFieldClass(size=32) | |
343 | enum1.add_mapping('b', 1, 3) | |
344 | self.assertNotEqual(enum1[0], 23) | |
345 | ||
346 | def _test_find_by_name(self, fc): | |
347 | fc.add_mapping('a', 0) | |
348 | fc.add_mapping('b', 1, 3) | |
349 | fc.add_mapping('a', 5) | |
350 | fc.add_mapping('a', 17, 144) | |
351 | fc.add_mapping('C', 5) | |
352 | mapping_iter = fc.mappings_by_name('a') | |
353 | mappings = list(mapping_iter) | |
354 | a0 = False | |
355 | a5 = False | |
356 | a17_144 = False | |
357 | i = 0 | |
358 | ||
359 | for mapping in mappings: | |
360 | i += 1 | |
361 | self.assertEqual(mapping.name, 'a') | |
362 | ||
363 | if mapping.lower == 0 and mapping.upper == 0: | |
364 | a0 = True | |
365 | elif mapping.lower == 5 and mapping.upper == 5: | |
366 | a5 = True | |
367 | elif mapping.lower == 17 and mapping.upper == 144: | |
368 | a17_144 = True | |
369 | ||
370 | self.assertEqual(i, 3) | |
371 | self.assertTrue(a0) | |
372 | self.assertTrue(a5) | |
373 | self.assertTrue(a17_144) | |
374 | ||
375 | def test_find_by_name_signed(self): | |
376 | self._test_find_by_name(bt2.EnumerationFieldClass(size=8, is_signed=True)) | |
377 | ||
378 | def test_find_by_name_unsigned(self): | |
379 | self._test_find_by_name(bt2.EnumerationFieldClass(size=8)) | |
380 | ||
381 | def _test_find_by_value(self, fc): | |
382 | fc.add_mapping('a', 0) | |
383 | fc.add_mapping('b', 1, 3) | |
384 | fc.add_mapping('c', 5, 19) | |
385 | fc.add_mapping('d', 8, 15) | |
386 | fc.add_mapping('e', 10, 21) | |
387 | fc.add_mapping('f', 0) | |
388 | fc.add_mapping('g', 14) | |
389 | mapping_iter = fc.mappings_by_value(14) | |
390 | mappings = list(mapping_iter) | |
391 | c = False | |
392 | d = False | |
393 | e = False | |
394 | g = False | |
395 | i = 0 | |
396 | ||
397 | for mapping in mappings: | |
398 | i += 1 | |
399 | ||
400 | if mapping.name == 'c': | |
401 | c = True | |
402 | elif mapping.name == 'd': | |
403 | d = True | |
404 | elif mapping.name == 'e': | |
405 | e = True | |
406 | elif mapping.name == 'g': | |
407 | g = True | |
408 | ||
409 | self.assertEqual(i, 4) | |
410 | self.assertTrue(c) | |
411 | self.assertTrue(d) | |
412 | self.assertTrue(e) | |
413 | self.assertTrue(g) | |
414 | ||
415 | def test_find_by_value_signed(self): | |
416 | self._test_find_by_value(bt2.EnumerationFieldClass(size=8, is_signed=True)) | |
417 | ||
418 | def test_find_by_value_unsigned(self): | |
419 | self._test_find_by_value(bt2.EnumerationFieldClass(size=8)) | |
420 | ||
421 | def test_create_field(self): | |
422 | self._fc.add_mapping('c', 4, 5) | |
423 | field = self._fc() | |
c4239792 | 424 | self.assertIsInstance(field, bt2.field._EnumerationField) |
b4f45851 SM |
425 | |
426 | def test_create_field_init(self): | |
427 | self._fc.add_mapping('c', 4, 5) | |
428 | field = self._fc(4) | |
429 | self.assertEqual(field, 4) | |
430 | ||
431 | ||
432 | @unittest.skip("this is broken") | |
433 | class StringFieldClassTestCase(_TestCopySimple, _TestInvalidEq, | |
434 | unittest.TestCase): | |
435 | def setUp(self): | |
436 | self._fc = bt2.StringFieldClass() | |
437 | ||
438 | def tearDown(self): | |
439 | del self._fc | |
440 | ||
441 | def test_create_default(self): | |
442 | pass | |
443 | ||
444 | def test_create_full(self): | |
445 | fc = bt2.StringFieldClass(encoding=bt2.Encoding.UTF8) | |
446 | self.assertEqual(fc.encoding, bt2.Encoding.UTF8) | |
447 | ||
448 | def test_assign_encoding(self): | |
449 | self._fc.encoding = bt2.Encoding.UTF8 | |
450 | self.assertEqual(self._fc.encoding, bt2.Encoding.UTF8) | |
451 | ||
452 | def test_assign_invalid_encoding(self): | |
453 | with self.assertRaises(TypeError): | |
454 | self._fc.encoding = 'yes' | |
455 | ||
456 | def test_create_field(self): | |
457 | field = self._fc() | |
c4239792 | 458 | self.assertIsInstance(field, bt2.field._StringField) |
b4f45851 SM |
459 | |
460 | def test_create_field_init(self): | |
461 | field = self._fc('hola') | |
462 | self.assertEqual(field, 'hola') | |
463 | ||
464 | ||
465 | class _TestFieldContainer(_TestInvalidEq, _TestCopySimple): | |
466 | def test_append_field(self): | |
467 | int_field_class = bt2.IntegerFieldClass(32) | |
468 | self._fc.append_field('int32', int_field_class) | |
469 | field_class = self._fc['int32'] | |
470 | self.assertEqual(field_class, int_field_class) | |
471 | ||
472 | def test_append_field_kwargs(self): | |
473 | int_field_class = bt2.IntegerFieldClass(32) | |
474 | self._fc.append_field(name='int32', field_class=int_field_class) | |
475 | field_class = self._fc['int32'] | |
476 | self.assertEqual(field_class, int_field_class) | |
477 | ||
478 | def test_append_field_invalid_name(self): | |
479 | with self.assertRaises(TypeError): | |
480 | self._fc.append_field(23, bt2.StringFieldClass()) | |
481 | ||
482 | def test_append_field_invalid_field_class(self): | |
483 | with self.assertRaises(TypeError): | |
484 | self._fc.append_field('yes', object()) | |
485 | ||
486 | def test_iadd(self): | |
487 | struct_fc = bt2.StructureFieldClass() | |
488 | c_field_class = bt2.StringFieldClass() | |
489 | d_field_class = bt2.EnumerationFieldClass(size=32) | |
490 | e_field_class = bt2.StructureFieldClass() | |
491 | struct_fc.append_field('c_string', c_field_class) | |
492 | struct_fc.append_field('d_enum', d_field_class) | |
493 | struct_fc.append_field('e_struct', e_field_class) | |
494 | a_field_class = bt2.FloatingPointNumberFieldClass() | |
495 | b_field_class = bt2.IntegerFieldClass(17) | |
496 | self._fc.append_field('a_float', a_field_class) | |
497 | self._fc.append_field('b_int', b_field_class) | |
498 | self._fc += struct_fc | |
499 | self.assertEqual(self._fc['a_float'], a_field_class) | |
500 | self.assertEqual(self._fc['b_int'], b_field_class) | |
501 | self.assertEqual(self._fc['c_string'], c_field_class) | |
502 | self.assertEqual(self._fc['d_enum'], d_field_class) | |
503 | self.assertEqual(self._fc['e_struct'], e_field_class) | |
504 | ||
505 | def test_bool_op(self): | |
506 | self.assertFalse(self._fc) | |
507 | self._fc.append_field('a', bt2.StringFieldClass()) | |
508 | self.assertTrue(self._fc) | |
509 | ||
510 | def test_len(self): | |
511 | fc = bt2.StringFieldClass() | |
512 | self._fc.append_field('a', fc) | |
513 | self._fc.append_field('b', fc) | |
514 | self._fc.append_field('c', fc) | |
515 | self.assertEqual(len(self._fc), 3) | |
516 | ||
517 | def test_getitem(self): | |
518 | a_fc = bt2.IntegerFieldClass(32) | |
519 | b_fc = bt2.StringFieldClass() | |
520 | c_fc = bt2.FloatingPointNumberFieldClass() | |
521 | self._fc.append_field('a', a_fc) | |
522 | self._fc.append_field('b', b_fc) | |
523 | self._fc.append_field('c', c_fc) | |
524 | self.assertEqual(self._fc['b'], b_fc) | |
525 | ||
526 | def test_getitem_invalid_key_type(self): | |
527 | with self.assertRaises(TypeError): | |
528 | self._fc[0] | |
529 | ||
530 | def test_getitem_invalid_key(self): | |
531 | with self.assertRaises(KeyError): | |
532 | self._fc['no way'] | |
533 | ||
534 | def test_contains(self): | |
535 | self.assertFalse('a' in self._fc) | |
536 | self._fc.append_field('a', bt2.StringFieldClass()) | |
537 | self.assertTrue('a' in self._fc) | |
538 | ||
539 | def test_iter(self): | |
540 | a_fc = bt2.IntegerFieldClass(32) | |
541 | b_fc = bt2.StringFieldClass() | |
542 | c_fc = bt2.FloatingPointNumberFieldClass() | |
543 | fields = ( | |
544 | ('a', a_fc), | |
545 | ('b', b_fc), | |
546 | ('c', c_fc), | |
547 | ) | |
548 | ||
549 | for field in fields: | |
550 | self._fc.append_field(*field) | |
551 | ||
552 | for (name, fc_field_class), field in zip(self._fc.items(), fields): | |
553 | self.assertEqual(name, field[0]) | |
554 | self.assertEqual(fc_field_class, field[1]) | |
555 | ||
556 | def test_at_index(self): | |
557 | a_fc = bt2.IntegerFieldClass(32) | |
558 | b_fc = bt2.StringFieldClass() | |
559 | c_fc = bt2.FloatingPointNumberFieldClass() | |
560 | self._fc.append_field('c', c_fc) | |
561 | self._fc.append_field('a', a_fc) | |
562 | self._fc.append_field('b', b_fc) | |
563 | self.assertEqual(self._fc.at_index(1), a_fc) | |
564 | ||
565 | def test_at_index_invalid(self): | |
566 | self._fc.append_field('c', bt2.IntegerFieldClass(32)) | |
567 | ||
568 | with self.assertRaises(TypeError): | |
569 | self._fc.at_index('yes') | |
570 | ||
571 | def test_at_index_out_of_bounds_after(self): | |
572 | self._fc.append_field('c', bt2.IntegerFieldClass(32)) | |
573 | ||
574 | with self.assertRaises(IndexError): | |
575 | self._fc.at_index(len(self._fc)) | |
576 | ||
577 | ||
578 | @unittest.skip("this is broken") | |
579 | class StructureFieldClassTestCase(_TestFieldContainer, unittest.TestCase): | |
580 | def setUp(self): | |
581 | self._fc = bt2.StructureFieldClass() | |
582 | ||
583 | def tearDown(self): | |
584 | del self._fc | |
585 | ||
586 | def test_create_default(self): | |
587 | self.assertEqual(self._fc.alignment, 1) | |
588 | ||
589 | def test_create_with_min_alignment(self): | |
590 | fc = bt2.StructureFieldClass(8) | |
591 | self.assertEqual(fc.alignment, 8) | |
592 | ||
593 | def test_assign_alignment(self): | |
594 | with self.assertRaises(AttributeError): | |
595 | self._fc.alignment = 32 | |
596 | ||
597 | def test_assign_min_alignment(self): | |
598 | self._fc.min_alignment = 64 | |
599 | self.assertTrue(self._fc.alignment >= 64) | |
600 | ||
601 | def test_assign_invalid_min_alignment(self): | |
602 | with self.assertRaises(ValueError): | |
603 | self._fc.min_alignment = 23 | |
604 | ||
605 | def test_assign_get_min_alignment(self): | |
606 | with self.assertRaises(AttributeError): | |
607 | self._fc.min_alignment | |
608 | ||
609 | def test_create_field(self): | |
610 | field = self._fc() | |
c4239792 | 611 | self.assertIsInstance(field, bt2.field._StructureField) |
b4f45851 SM |
612 | |
613 | def test_create_field_init_invalid(self): | |
614 | with self.assertRaises(bt2.Error): | |
615 | field = self._fc(23) | |
616 | ||
617 | ||
618 | @unittest.skip("this is broken") | |
619 | class VariantFieldClassTestCase(_TestFieldContainer, unittest.TestCase): | |
620 | def setUp(self): | |
621 | self._fc = bt2.VariantFieldClass('path.to.tag') | |
622 | ||
623 | def tearDown(self): | |
624 | del self._fc | |
625 | ||
626 | def test_create_default(self): | |
627 | self.assertEqual(self._fc.tag_name, 'path.to.tag') | |
628 | ||
629 | def test_create_invalid_tag_name(self): | |
630 | with self.assertRaises(TypeError): | |
631 | self._fc = bt2.VariantFieldClass(23) | |
632 | ||
633 | def test_assign_tag_name(self): | |
634 | self._fc.tag_name = 'a.different.tag' | |
635 | self.assertEqual(self._fc.tag_name, 'a.different.tag') | |
636 | ||
637 | def test_assign_invalid_tag_name(self): | |
638 | with self.assertRaises(TypeError): | |
639 | self._fc.tag_name = -17 | |
640 | ||
641 | ||
642 | @unittest.skip("this is broken") | |
643 | class ArrayFieldClassTestCase(_TestInvalidEq, _TestCopySimple, | |
644 | unittest.TestCase): | |
645 | def setUp(self): | |
646 | self._elem_fc = bt2.IntegerFieldClass(23) | |
647 | self._fc = bt2.ArrayFieldClass(self._elem_fc, 45) | |
648 | ||
649 | def tearDown(self): | |
650 | del self._fc | |
651 | del self._elem_fc | |
652 | ||
653 | def test_create_default(self): | |
654 | self.assertEqual(self._fc.element_field_class, self._elem_fc) | |
655 | self.assertEqual(self._fc.length, 45) | |
656 | ||
657 | def test_create_invalid_field_class(self): | |
658 | with self.assertRaises(TypeError): | |
659 | self._fc = bt2.ArrayFieldClass(object(), 45) | |
660 | ||
661 | def test_create_invalid_length(self): | |
662 | with self.assertRaises(ValueError): | |
663 | self._fc = bt2.ArrayFieldClass(bt2.StringFieldClass(), -17) | |
664 | ||
665 | def test_create_invalid_length_type(self): | |
666 | with self.assertRaises(TypeError): | |
667 | self._fc = bt2.ArrayFieldClass(bt2.StringFieldClass(), 'the length') | |
668 | ||
669 | def test_create_field(self): | |
670 | field = self._fc() | |
c4239792 | 671 | self.assertIsInstance(field, bt2.field._ArrayField) |
b4f45851 SM |
672 | |
673 | def test_create_field_init_invalid(self): | |
674 | with self.assertRaises(bt2.Error): | |
675 | field = self._fc(23) | |
676 | ||
677 | ||
678 | @unittest.skip("this is broken") | |
679 | class SequenceFieldClassTestCase(_TestInvalidEq, _TestCopySimple, | |
680 | unittest.TestCase): | |
681 | def setUp(self): | |
682 | self._elem_fc = bt2.IntegerFieldClass(23) | |
683 | self._fc = bt2.SequenceFieldClass(self._elem_fc, 'the.length') | |
684 | ||
685 | def tearDown(self): | |
686 | del self._fc | |
687 | del self._elem_fc | |
688 | ||
689 | def test_create_default(self): | |
690 | self.assertEqual(self._fc.element_field_class, self._elem_fc) | |
691 | self.assertEqual(self._fc.length_name, 'the.length') | |
692 | ||
693 | def test_create_invalid_field_class(self): | |
694 | with self.assertRaises(TypeError): | |
695 | self._fc = bt2.ArrayFieldClass(object(), 'the.length') | |
696 | ||
697 | def test_create_invalid_length_type(self): | |
698 | with self.assertRaises(TypeError): | |
699 | self._fc = bt2.SequenceFieldClass(bt2.StringFieldClass(), 17) | |
700 | ||
701 | def test_create_field(self): | |
702 | field = self._fc() | |
c4239792 | 703 | self.assertIsInstance(field, bt2.field._SequenceField) |
b4f45851 SM |
704 | |
705 | def test_create_field_init_invalid(self): | |
706 | with self.assertRaises(bt2.Error): | |
707 | field = self._fc(23) |