Commit | Line | Data |
---|---|---|
b016b06b JG |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
4 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
5 | # | |
6 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | # of this software and associated documentation files (the "Software"), to deal | |
8 | # in the Software without restriction, including without limitation the rights | |
9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | # copies of the Software, and to permit persons to whom the Software is | |
11 | # furnished to do so, subject to the following conditions: | |
12 | # | |
13 | # The above copyright notice and this permission notice shall be included in | |
14 | # all copies or substantial portions of the Software. | |
15 | # | |
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | # THE SOFTWARE. | |
23 | ||
24 | import collections | |
25 | import unittest | |
26 | import copy | |
27 | import bt2 | |
28 | import babeltrace | |
29 | import babeltrace.reader_field_declaration as field_declaration | |
30 | import datetime | |
31 | import random | |
32 | ||
33 | ||
34 | class FieldDeclarationTestCase(unittest.TestCase): | |
35 | def setUp(self): | |
36 | pass | |
37 | ||
38 | def tearDown(self): | |
39 | pass | |
40 | ||
41 | @staticmethod | |
42 | def _get_declaration(field_type): | |
43 | return field_declaration._create_field_declaration( | |
44 | field_type, 'a_name', babeltrace.CTFScope.TRACE_PACKET_HEADER) | |
45 | ||
46 | def test_type(self): | |
47 | int_ft = bt2.IntegerFieldType(32) | |
48 | ||
49 | enum_ft = bt2.EnumerationFieldType(int_ft) | |
50 | enum_ft.append_mapping('corner', 23) | |
51 | enum_ft.append_mapping('zoom', 17, 20) | |
52 | enum_ft.append_mapping('mellotron', 1001) | |
53 | enum_ft.append_mapping('giorgio', 2000, 3000) | |
54 | ||
55 | array_ft = bt2.ArrayFieldType(int_ft, 5) | |
56 | seq_ft = bt2.SequenceFieldType(int_ft, 'the_len_field') | |
57 | float_ft = bt2.FloatingPointNumberFieldType() | |
58 | ||
59 | struct_ft = bt2.StructureFieldType() | |
60 | struct_ft.append_field('a', int_ft) | |
61 | struct_ft.append_field('b', int_ft) | |
62 | struct_ft.append_field('c', int_ft) | |
63 | ||
64 | _string_ft = bt2.StringFieldType() | |
65 | ||
66 | variant_ft = bt2.VariantFieldType('tag', enum_ft) | |
67 | variant_ft.append_field('corner', int_ft) | |
68 | variant_ft.append_field('zoom', array_ft) | |
69 | variant_ft.append_field('mellotron', float_ft) | |
70 | variant_ft.append_field('giorgio', struct_ft) | |
71 | ||
72 | expected_types = { | |
73 | babeltrace.CTFTypeId.INTEGER: int_ft, | |
74 | babeltrace.CTFTypeId.FLOAT: float_ft, | |
75 | babeltrace.CTFTypeId.ENUM: enum_ft, | |
76 | babeltrace.CTFTypeId.STRING: _string_ft, | |
77 | babeltrace.CTFTypeId.STRUCT: struct_ft, | |
78 | babeltrace.CTFTypeId.VARIANT: variant_ft, | |
79 | babeltrace.CTFTypeId.ARRAY: array_ft, | |
80 | babeltrace.CTFTypeId.SEQUENCE: seq_ft, | |
81 | } | |
82 | ||
83 | for type_id, ft in expected_types.items(): | |
84 | declaration = self._get_declaration(ft) | |
85 | self.assertIsNotNone(declaration) | |
86 | self.assertEqual(declaration.type, type_id) | |
87 | ||
88 | def test_int_signedness(self): | |
89 | int_ft = bt2.IntegerFieldType(size=32, is_signed=True) | |
90 | declaration = self._get_declaration(int_ft) | |
91 | self.assertEqual(declaration.signedness, 1) | |
92 | ||
93 | uint_ft = bt2.IntegerFieldType(size=32, is_signed=False) | |
94 | declaration = self._get_declaration(uint_ft) | |
95 | self.assertEqual(declaration.signedness, 0) | |
96 | ||
97 | def test_int_base(self): | |
98 | int_ft = bt2.IntegerFieldType(size=32, base=8) | |
99 | declaration = self._get_declaration(int_ft) | |
100 | self.assertEqual(declaration.base, 8) | |
101 | ||
102 | int_ft = bt2.IntegerFieldType(size=32, base=16) | |
103 | declaration = self._get_declaration(int_ft) | |
104 | self.assertEqual(declaration.base, 16) | |
105 | ||
106 | int_ft = bt2.IntegerFieldType(size=32, base=10) | |
107 | declaration = self._get_declaration(int_ft) | |
108 | self.assertEqual(declaration.base, 10) | |
109 | ||
110 | int_ft = bt2.IntegerFieldType(size=32, base=2) | |
111 | declaration = self._get_declaration(int_ft) | |
112 | self.assertEqual(declaration.base, 2) | |
113 | ||
114 | def test_int_byte_order(self): | |
115 | expected_byte_orders = { | |
116 | bt2.ByteOrder.NATIVE: babeltrace.ByteOrder.BYTE_ORDER_NATIVE, | |
117 | bt2.ByteOrder.LITTLE_ENDIAN: babeltrace.ByteOrder.BYTE_ORDER_LITTLE_ENDIAN, | |
118 | bt2.ByteOrder.BIG_ENDIAN: babeltrace.ByteOrder.BYTE_ORDER_BIG_ENDIAN, | |
119 | bt2.ByteOrder.NETWORK: babeltrace.ByteOrder.BYTE_ORDER_NETWORK, | |
120 | } | |
121 | ||
122 | for bt2_bo, bt_bo in expected_byte_orders.items(): | |
123 | int_ft = bt2.IntegerFieldType(size=32, byte_order=bt2_bo) | |
124 | declaration = self._get_declaration(int_ft) | |
125 | self.assertEqual(declaration.byte_order, bt_bo) | |
126 | ||
127 | def test_int_size(self): | |
128 | int_ft = bt2.IntegerFieldType(size=32, base=8) | |
129 | declaration = self._get_declaration(int_ft) | |
130 | self.assertEqual(declaration.size, 32) | |
131 | self.assertEqual(declaration.length, 32) | |
132 | ||
133 | int_ft = bt2.IntegerFieldType(size=12, base=8) | |
134 | declaration = self._get_declaration(int_ft) | |
135 | self.assertEqual(declaration.size, 12) | |
136 | self.assertEqual(declaration.length, 12) | |
137 | ||
138 | def test_int_encoding(self): | |
139 | expected_encodings = { | |
140 | bt2.Encoding.NONE: babeltrace.CTFStringEncoding.NONE, | |
141 | bt2.Encoding.ASCII: babeltrace.CTFStringEncoding.ASCII, | |
142 | bt2.Encoding.UTF8: babeltrace.CTFStringEncoding.UTF8, | |
143 | } | |
144 | ||
145 | for bt2_encoding, bt_encoding in expected_encodings.items(): | |
146 | int_ft = bt2.IntegerFieldType(size=32, encoding=bt2_encoding) | |
147 | declaration = self._get_declaration(int_ft) | |
148 | self.assertEqual(declaration.encoding, bt_encoding) | |
149 | ||
150 | def test_array_length(self): | |
151 | int_ft = bt2.IntegerFieldType(32) | |
152 | array_ft = bt2.ArrayFieldType(int_ft, 5) | |
153 | declaration = self._get_declaration(array_ft) | |
154 | self.assertEqual(declaration.length, 5) | |
155 | ||
156 | def test_array_element_declaration(self): | |
157 | int_ft = bt2.IntegerFieldType(size=32, is_signed=True, base=8) | |
158 | array_ft = bt2.ArrayFieldType(int_ft, 5) | |
159 | declaration = self._get_declaration(array_ft) | |
160 | element_declaration = declaration.element_declaration | |
161 | self.assertEqual(element_declaration.type, babeltrace.CTFTypeId.INTEGER) | |
162 | self.assertEqual(element_declaration.size, 32) | |
163 | self.assertEqual(element_declaration.base, 8) | |
164 | ||
165 | def test_sequence_element_declaration(self): | |
166 | int_ft = bt2.IntegerFieldType(size=32, is_signed=True, base=8) | |
167 | seq_ft = bt2.SequenceFieldType(int_ft, 'len_field') | |
168 | declaration = self._get_declaration(seq_ft) | |
169 | element_declaration = declaration.element_declaration | |
170 | self.assertEqual(element_declaration.type, babeltrace.CTFTypeId.INTEGER) | |
171 | self.assertEqual(element_declaration.size, 32) | |
172 | self.assertEqual(element_declaration.base, 8) |