Commit | Line | Data |
---|---|---|
9cf643d1 PP |
1 | import unittest |
2 | import uuid | |
3 | import copy | |
4 | import bt2 | |
5 | ||
6 | ||
7 | class ClockClassOffsetTestCase(unittest.TestCase): | |
8 | def test_create_default(self): | |
9 | cco = bt2.ClockClassOffset() | |
10 | self.assertEqual(cco.seconds, 0) | |
11 | self.assertEqual(cco.cycles, 0) | |
12 | ||
13 | def test_create(self): | |
14 | cco = bt2.ClockClassOffset(23, 4871232) | |
15 | self.assertEqual(cco.seconds, 23) | |
16 | self.assertEqual(cco.cycles, 4871232) | |
17 | ||
18 | def test_create_kwargs(self): | |
19 | cco = bt2.ClockClassOffset(seconds=23, cycles=4871232) | |
20 | self.assertEqual(cco.seconds, 23) | |
21 | self.assertEqual(cco.cycles, 4871232) | |
22 | ||
23 | def test_create_invalid_seconds(self): | |
24 | with self.assertRaises(TypeError): | |
25 | bt2.ClockClassOffset('hello', 4871232) | |
26 | ||
27 | def test_create_invalid_cycles(self): | |
28 | with self.assertRaises(TypeError): | |
29 | bt2.ClockClassOffset(23, 'hello') | |
30 | ||
31 | def test_eq(self): | |
32 | cco1 = bt2.ClockClassOffset(23, 42) | |
33 | cco2 = bt2.ClockClassOffset(23, 42) | |
34 | self.assertEqual(cco1, cco2) | |
35 | ||
36 | def test_ne_seconds(self): | |
37 | cco1 = bt2.ClockClassOffset(23, 42) | |
38 | cco2 = bt2.ClockClassOffset(24, 42) | |
39 | self.assertNotEqual(cco1, cco2) | |
40 | ||
41 | def test_ne_cycles(self): | |
42 | cco1 = bt2.ClockClassOffset(23, 42) | |
43 | cco2 = bt2.ClockClassOffset(23, 43) | |
44 | self.assertNotEqual(cco1, cco2) | |
45 | ||
46 | def test_eq_invalid(self): | |
47 | self.assertFalse(bt2.ClockClassOffset() == 23) | |
48 | ||
49 | ||
50 | class ClockClassTestCase(unittest.TestCase): | |
51 | def setUp(self): | |
52 | self._cc = bt2.ClockClass('salut') | |
53 | ||
54 | def test_create_default(self): | |
55 | self.assertEqual(self._cc.name, 'salut') | |
56 | ||
57 | def test_create_invalid_no_name(self): | |
58 | with self.assertRaises(TypeError): | |
59 | bt2.ClockClass() | |
60 | ||
61 | def test_create_full(self): | |
62 | my_uuid = uuid.uuid1() | |
63 | cc = bt2.ClockClass(name='name', description='some description', | |
64 | frequency=1001, precision=176, | |
65 | offset=bt2.ClockClassOffset(45, 3003), | |
66 | is_absolute=True, uuid=my_uuid) | |
67 | self.assertEqual(cc.name, 'name') | |
68 | self.assertEqual(cc.description, 'some description') | |
69 | self.assertEqual(cc.frequency, 1001) | |
70 | self.assertEqual(cc.precision, 176) | |
71 | self.assertEqual(cc.offset, bt2.ClockClassOffset(45, 3003)) | |
72 | self.assertEqual(cc.is_absolute, True) | |
73 | self.assertEqual(cc.uuid, copy.deepcopy(my_uuid)) | |
74 | ||
75 | def test_assign_name(self): | |
76 | self._cc.name = 'the_clock' | |
77 | self.assertEqual(self._cc.name, 'the_clock') | |
78 | ||
79 | def test_assign_invalid_name(self): | |
80 | with self.assertRaises(TypeError): | |
81 | self._cc.name = 23 | |
82 | ||
83 | def test_assign_description(self): | |
84 | self._cc.description = 'hi people' | |
85 | self.assertEqual(self._cc.description, 'hi people') | |
86 | ||
87 | def test_assign_invalid_description(self): | |
88 | with self.assertRaises(TypeError): | |
89 | self._cc.description = 23 | |
90 | ||
91 | def test_assign_frequency(self): | |
92 | self._cc.frequency = 987654321 | |
93 | self.assertEqual(self._cc.frequency, 987654321) | |
94 | ||
95 | def test_assign_invalid_frequency(self): | |
96 | with self.assertRaises(TypeError): | |
97 | self._cc.frequency = 'lel' | |
98 | ||
99 | def test_assign_precision(self): | |
100 | self._cc.precision = 12 | |
101 | self.assertEqual(self._cc.precision, 12) | |
102 | ||
103 | def test_assign_invalid_precision(self): | |
104 | with self.assertRaises(TypeError): | |
105 | self._cc.precision = 'lel' | |
106 | ||
107 | def test_assign_offset(self): | |
108 | self._cc.offset = bt2.ClockClassOffset(12, 56) | |
109 | self.assertEqual(self._cc.offset, bt2.ClockClassOffset(12, 56)) | |
110 | ||
111 | def test_assign_invalid_offset(self): | |
112 | with self.assertRaises(TypeError): | |
113 | self._cc.offset = object() | |
114 | ||
115 | def test_assign_absolute(self): | |
116 | self._cc.is_absolute = True | |
117 | self.assertTrue(self._cc.is_absolute) | |
118 | ||
119 | def test_assign_invalid_absolute(self): | |
120 | with self.assertRaises(TypeError): | |
121 | self._cc.is_absolute = 23 | |
122 | ||
123 | def test_assign_uuid(self): | |
124 | the_uuid = uuid.uuid1() | |
125 | self._cc.uuid = the_uuid | |
126 | self.assertEqual(self._cc.uuid, the_uuid) | |
127 | ||
128 | def test_assign_invalid_uuid(self): | |
129 | with self.assertRaises(TypeError): | |
130 | self._cc.uuid = object() | |
131 | ||
132 | def test_create_clock_value(self): | |
133 | cv = self._cc.create_clock_value(756) | |
134 | self.assertEqual(cv.clock_class.addr, self._cc.addr) | |
135 | ||
136 | def _test_copy(self, cpy): | |
137 | self.assertIsNot(cpy, self._cc) | |
138 | self.assertNotEqual(cpy.addr, self._cc.addr) | |
139 | self.assertEqual(cpy, self._cc) | |
140 | ||
141 | def test_copy(self): | |
142 | cpy = copy.copy(self._cc) | |
143 | self._test_copy(cpy) | |
144 | ||
145 | def test_deepcopy(self): | |
146 | cpy = copy.deepcopy(self._cc) | |
147 | self._test_copy(cpy) | |
148 | ||
149 | def test_eq(self): | |
150 | my_uuid = uuid.uuid1() | |
151 | cc1 = bt2.ClockClass(name='name', description='some description', | |
152 | frequency=1001, precision=176, | |
153 | offset=bt2.ClockClassOffset(45, 3003), | |
154 | is_absolute=True, uuid=my_uuid) | |
155 | cc2 = bt2.ClockClass(name='name', description='some description', | |
156 | frequency=1001, precision=176, | |
157 | offset=bt2.ClockClassOffset(45, 3003), | |
158 | is_absolute=True, uuid=my_uuid) | |
159 | self.assertEqual(cc1, cc2) | |
160 | ||
161 | def test_ne_name(self): | |
162 | my_uuid = uuid.uuid1() | |
163 | cc1 = bt2.ClockClass(name='mane', description='some description', | |
164 | frequency=1001, precision=176, | |
165 | offset=bt2.ClockClassOffset(45, 3003), | |
166 | is_absolute=True, uuid=my_uuid) | |
167 | cc2 = bt2.ClockClass(name='name', description='some description', | |
168 | frequency=1001, precision=176, | |
169 | offset=bt2.ClockClassOffset(45, 3003), | |
170 | is_absolute=True, uuid=my_uuid) | |
171 | self.assertNotEqual(cc1, cc2) | |
172 | ||
173 | def test_ne_description(self): | |
174 | my_uuid = uuid.uuid1() | |
175 | cc1 = bt2.ClockClass(name='name', description='some descripti2', | |
176 | frequency=1001, precision=176, | |
177 | offset=bt2.ClockClassOffset(45, 3003), | |
178 | is_absolute=True, uuid=my_uuid) | |
179 | cc2 = bt2.ClockClass(name='name', description='some description', | |
180 | frequency=1001, precision=176, | |
181 | offset=bt2.ClockClassOffset(45, 3003), | |
182 | is_absolute=True, uuid=my_uuid) | |
183 | self.assertNotEqual(cc1, cc2) | |
184 | ||
185 | def test_ne_frequency(self): | |
186 | my_uuid = uuid.uuid1() | |
187 | cc1 = bt2.ClockClass(name='name', description='some description', | |
188 | frequency=1003, precision=176, | |
189 | offset=bt2.ClockClassOffset(45, 3003), | |
190 | is_absolute=True, uuid=my_uuid) | |
191 | cc2 = bt2.ClockClass(name='name', description='some description', | |
192 | frequency=1001, precision=176, | |
193 | offset=bt2.ClockClassOffset(45, 3003), | |
194 | is_absolute=True, uuid=my_uuid) | |
195 | self.assertNotEqual(cc1, cc2) | |
196 | ||
197 | def test_ne_precision(self): | |
198 | my_uuid = uuid.uuid1() | |
199 | cc1 = bt2.ClockClass(name='name', description='some description', | |
200 | frequency=1001, precision=171, | |
201 | offset=bt2.ClockClassOffset(45, 3003), | |
202 | is_absolute=True, uuid=my_uuid) | |
203 | cc2 = bt2.ClockClass(name='name', description='some description', | |
204 | frequency=1001, precision=176, | |
205 | offset=bt2.ClockClassOffset(45, 3003), | |
206 | is_absolute=True, uuid=my_uuid) | |
207 | self.assertNotEqual(cc1, cc2) | |
208 | ||
209 | def test_ne_offset(self): | |
210 | my_uuid = uuid.uuid1() | |
211 | cc1 = bt2.ClockClass(name='name', description='some description', | |
212 | frequency=1001, precision=176, | |
213 | offset=bt2.ClockClassOffset(45, 3001), | |
214 | is_absolute=True, uuid=my_uuid) | |
215 | cc2 = bt2.ClockClass(name='name', description='some description', | |
216 | frequency=1001, precision=176, | |
217 | offset=bt2.ClockClassOffset(45, 3003), | |
218 | is_absolute=True, uuid=my_uuid) | |
219 | self.assertNotEqual(cc1, cc2) | |
220 | ||
221 | def test_ne_absolute(self): | |
222 | my_uuid = uuid.uuid1() | |
223 | cc1 = bt2.ClockClass(name='name', description='some description', | |
224 | frequency=1001, precision=176, | |
225 | offset=bt2.ClockClassOffset(45, 3003), | |
226 | is_absolute=True, uuid=my_uuid) | |
227 | cc2 = bt2.ClockClass(name='name', description='some description', | |
228 | frequency=1001, precision=176, | |
229 | offset=bt2.ClockClassOffset(45, 3003), | |
230 | is_absolute=False, uuid=my_uuid) | |
231 | self.assertNotEqual(cc1, cc2) | |
232 | ||
233 | def test_ne_uuid(self): | |
234 | cc1 = bt2.ClockClass(name='name', description='some description', | |
235 | frequency=1001, precision=176, | |
236 | offset=bt2.ClockClassOffset(45, 3003), | |
237 | is_absolute=True, uuid=uuid.uuid1()) | |
238 | cc2 = bt2.ClockClass(name='name', description='some description', | |
239 | frequency=1001, precision=176, | |
240 | offset=bt2.ClockClassOffset(45, 3003), | |
241 | is_absolute=True, uuid=uuid.uuid1()) | |
242 | self.assertNotEqual(cc1, cc2) | |
243 | ||
244 | def test_eq_invalid(self): | |
245 | self.assertFalse(self._cc == 23) | |
246 | ||
247 | ||
248 | class ClockClassValueTestCase(unittest.TestCase): | |
249 | def setUp(self): | |
250 | self._cc = bt2.ClockClass('salut') | |
251 | self._cv = self._cc.create_clock_value(123) | |
252 | ||
253 | def test_create_default(self): | |
254 | self.assertEqual(self._cv.clock_class.addr, self._cc.addr) | |
255 | self.assertEqual(self._cv.cycles, 123) | |
256 | ||
257 | def test_create_invalid_cycles_type(self): | |
258 | with self.assertRaises(TypeError): | |
259 | self._cc.create_clock_value('yes') | |
260 | ||
261 | def test_ns_from_epoch(self): | |
262 | self._cv.clock_class.frequency = 1000 | |
263 | self._cv.clock_class.offset = bt2.ClockClassOffset(45, 354) | |
264 | s_from_epoch = 45 + ((354 + 123) / 1000) | |
265 | ns_from_epoch = int(s_from_epoch * 1e9) | |
266 | self.assertEqual(self._cv.ns_from_epoch, ns_from_epoch) | |
267 | ||
268 | def test_eq(self): | |
269 | cv1 = self._cc.create_clock_value(123) | |
270 | cv2 = self._cc.create_clock_value(123) | |
271 | self.assertEqual(cv1, cv2) | |
272 | ||
273 | def test_ne_clock_class(self): | |
274 | cc1 = bt2.ClockClass('yes') | |
275 | cc2 = bt2.ClockClass('yes') | |
276 | cv1 = cc1.create_clock_value(123) | |
277 | cv2 = cc2.create_clock_value(123) | |
278 | self.assertNotEqual(cv1, cv2) | |
279 | ||
280 | def test_ne_cycles(self): | |
281 | cv1 = self._cc.create_clock_value(123) | |
282 | cv2 = self._cc.create_clock_value(125) | |
283 | self.assertNotEqual(cv1, cv2) | |
284 | ||
285 | def test_eq_invalid(self): | |
286 | self.assertFalse(self._cv == 23) | |
287 | ||
288 | def _test_copy(self, cpy): | |
289 | self.assertIsNot(cpy, self._cv) | |
290 | self.assertNotEqual(cpy.addr, self._cv.addr) | |
291 | self.assertEqual(cpy, self._cv) | |
292 | ||
293 | def test_copy(self): | |
294 | cpy = copy.copy(self._cv) | |
295 | self._test_copy(cpy) | |
296 | ||
297 | def test_deepcopy(self): | |
298 | cpy = copy.deepcopy(self._cv) | |
299 | self._test_copy(cpy) |