| 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', 1000000) |
| 53 | |
| 54 | def tearDown(self): |
| 55 | del self._cc |
| 56 | |
| 57 | def test_create_default(self): |
| 58 | self.assertEqual(self._cc.name, 'salut') |
| 59 | |
| 60 | def test_create_invalid_no_name(self): |
| 61 | with self.assertRaises(TypeError): |
| 62 | bt2.ClockClass() |
| 63 | |
| 64 | def test_create_full(self): |
| 65 | my_uuid = uuid.uuid1() |
| 66 | cc = bt2.ClockClass(name='name', description='some description', |
| 67 | frequency=1001, precision=176, |
| 68 | offset=bt2.ClockClassOffset(45, 3003), |
| 69 | is_absolute=True, uuid=my_uuid) |
| 70 | self.assertEqual(cc.name, 'name') |
| 71 | self.assertEqual(cc.description, 'some description') |
| 72 | self.assertEqual(cc.frequency, 1001) |
| 73 | self.assertEqual(cc.precision, 176) |
| 74 | self.assertEqual(cc.offset, bt2.ClockClassOffset(45, 3003)) |
| 75 | self.assertEqual(cc.is_absolute, True) |
| 76 | self.assertEqual(cc.uuid, copy.deepcopy(my_uuid)) |
| 77 | |
| 78 | def test_assign_name(self): |
| 79 | self._cc.name = 'the_clock' |
| 80 | self.assertEqual(self._cc.name, 'the_clock') |
| 81 | |
| 82 | def test_assign_invalid_name(self): |
| 83 | with self.assertRaises(TypeError): |
| 84 | self._cc.name = 23 |
| 85 | |
| 86 | def test_assign_description(self): |
| 87 | self._cc.description = 'hi people' |
| 88 | self.assertEqual(self._cc.description, 'hi people') |
| 89 | |
| 90 | def test_assign_invalid_description(self): |
| 91 | with self.assertRaises(TypeError): |
| 92 | self._cc.description = 23 |
| 93 | |
| 94 | def test_assign_frequency(self): |
| 95 | self._cc.frequency = 987654321 |
| 96 | self.assertEqual(self._cc.frequency, 987654321) |
| 97 | |
| 98 | def test_assign_invalid_frequency(self): |
| 99 | with self.assertRaises(TypeError): |
| 100 | self._cc.frequency = 'lel' |
| 101 | |
| 102 | def test_assign_precision(self): |
| 103 | self._cc.precision = 12 |
| 104 | self.assertEqual(self._cc.precision, 12) |
| 105 | |
| 106 | def test_assign_invalid_precision(self): |
| 107 | with self.assertRaises(TypeError): |
| 108 | self._cc.precision = 'lel' |
| 109 | |
| 110 | def test_assign_offset(self): |
| 111 | self._cc.offset = bt2.ClockClassOffset(12, 56) |
| 112 | self.assertEqual(self._cc.offset, bt2.ClockClassOffset(12, 56)) |
| 113 | |
| 114 | def test_assign_invalid_offset(self): |
| 115 | with self.assertRaises(TypeError): |
| 116 | self._cc.offset = object() |
| 117 | |
| 118 | def test_assign_absolute(self): |
| 119 | self._cc.is_absolute = True |
| 120 | self.assertTrue(self._cc.is_absolute) |
| 121 | |
| 122 | def test_assign_invalid_absolute(self): |
| 123 | with self.assertRaises(TypeError): |
| 124 | self._cc.is_absolute = 23 |
| 125 | |
| 126 | def test_assign_uuid(self): |
| 127 | the_uuid = uuid.uuid1() |
| 128 | self._cc.uuid = the_uuid |
| 129 | self.assertEqual(self._cc.uuid, the_uuid) |
| 130 | |
| 131 | def test_assign_invalid_uuid(self): |
| 132 | with self.assertRaises(TypeError): |
| 133 | self._cc.uuid = object() |
| 134 | |
| 135 | def test_create_clock_value(self): |
| 136 | cv = self._cc(756) |
| 137 | self.assertEqual(cv.clock_class.addr, self._cc.addr) |
| 138 | |
| 139 | def _test_copy(self, cpy): |
| 140 | self.assertIsNot(cpy, self._cc) |
| 141 | self.assertNotEqual(cpy.addr, self._cc.addr) |
| 142 | self.assertEqual(cpy, self._cc) |
| 143 | |
| 144 | def test_copy(self): |
| 145 | cpy = copy.copy(self._cc) |
| 146 | self._test_copy(cpy) |
| 147 | |
| 148 | def test_deepcopy(self): |
| 149 | cpy = copy.deepcopy(self._cc) |
| 150 | self._test_copy(cpy) |
| 151 | |
| 152 | def test_eq(self): |
| 153 | my_uuid = uuid.uuid1() |
| 154 | cc1 = bt2.ClockClass(name='name', description='some description', |
| 155 | frequency=1001, precision=176, |
| 156 | offset=bt2.ClockClassOffset(45, 3003), |
| 157 | is_absolute=True, uuid=my_uuid) |
| 158 | cc2 = bt2.ClockClass(name='name', description='some description', |
| 159 | frequency=1001, precision=176, |
| 160 | offset=bt2.ClockClassOffset(45, 3003), |
| 161 | is_absolute=True, uuid=my_uuid) |
| 162 | self.assertEqual(cc1, cc2) |
| 163 | |
| 164 | def test_ne_name(self): |
| 165 | my_uuid = uuid.uuid1() |
| 166 | cc1 = bt2.ClockClass(name='mane', description='some description', |
| 167 | frequency=1001, precision=176, |
| 168 | offset=bt2.ClockClassOffset(45, 3003), |
| 169 | is_absolute=True, uuid=my_uuid) |
| 170 | cc2 = bt2.ClockClass(name='name', description='some description', |
| 171 | frequency=1001, precision=176, |
| 172 | offset=bt2.ClockClassOffset(45, 3003), |
| 173 | is_absolute=True, uuid=my_uuid) |
| 174 | self.assertNotEqual(cc1, cc2) |
| 175 | |
| 176 | def test_ne_description(self): |
| 177 | my_uuid = uuid.uuid1() |
| 178 | cc1 = bt2.ClockClass(name='name', description='some descripti2', |
| 179 | frequency=1001, precision=176, |
| 180 | offset=bt2.ClockClassOffset(45, 3003), |
| 181 | is_absolute=True, uuid=my_uuid) |
| 182 | cc2 = bt2.ClockClass(name='name', description='some description', |
| 183 | frequency=1001, precision=176, |
| 184 | offset=bt2.ClockClassOffset(45, 3003), |
| 185 | is_absolute=True, uuid=my_uuid) |
| 186 | self.assertNotEqual(cc1, cc2) |
| 187 | |
| 188 | def test_ne_frequency(self): |
| 189 | my_uuid = uuid.uuid1() |
| 190 | cc1 = bt2.ClockClass(name='name', description='some description', |
| 191 | frequency=1003, precision=176, |
| 192 | offset=bt2.ClockClassOffset(45, 3003), |
| 193 | is_absolute=True, uuid=my_uuid) |
| 194 | cc2 = bt2.ClockClass(name='name', description='some description', |
| 195 | frequency=1001, precision=176, |
| 196 | offset=bt2.ClockClassOffset(45, 3003), |
| 197 | is_absolute=True, uuid=my_uuid) |
| 198 | self.assertNotEqual(cc1, cc2) |
| 199 | |
| 200 | def test_ne_precision(self): |
| 201 | my_uuid = uuid.uuid1() |
| 202 | cc1 = bt2.ClockClass(name='name', description='some description', |
| 203 | frequency=1001, precision=171, |
| 204 | offset=bt2.ClockClassOffset(45, 3003), |
| 205 | is_absolute=True, uuid=my_uuid) |
| 206 | cc2 = bt2.ClockClass(name='name', description='some description', |
| 207 | frequency=1001, precision=176, |
| 208 | offset=bt2.ClockClassOffset(45, 3003), |
| 209 | is_absolute=True, uuid=my_uuid) |
| 210 | self.assertNotEqual(cc1, cc2) |
| 211 | |
| 212 | def test_ne_offset(self): |
| 213 | my_uuid = uuid.uuid1() |
| 214 | cc1 = bt2.ClockClass(name='name', description='some description', |
| 215 | frequency=1001, precision=176, |
| 216 | offset=bt2.ClockClassOffset(45, 3001), |
| 217 | is_absolute=True, uuid=my_uuid) |
| 218 | cc2 = bt2.ClockClass(name='name', description='some description', |
| 219 | frequency=1001, precision=176, |
| 220 | offset=bt2.ClockClassOffset(45, 3003), |
| 221 | is_absolute=True, uuid=my_uuid) |
| 222 | self.assertNotEqual(cc1, cc2) |
| 223 | |
| 224 | def test_ne_absolute(self): |
| 225 | my_uuid = uuid.uuid1() |
| 226 | cc1 = bt2.ClockClass(name='name', description='some description', |
| 227 | frequency=1001, precision=176, |
| 228 | offset=bt2.ClockClassOffset(45, 3003), |
| 229 | is_absolute=True, uuid=my_uuid) |
| 230 | cc2 = bt2.ClockClass(name='name', description='some description', |
| 231 | frequency=1001, precision=176, |
| 232 | offset=bt2.ClockClassOffset(45, 3003), |
| 233 | is_absolute=False, uuid=my_uuid) |
| 234 | self.assertNotEqual(cc1, cc2) |
| 235 | |
| 236 | def test_ne_uuid(self): |
| 237 | cc1 = bt2.ClockClass(name='name', description='some description', |
| 238 | frequency=1001, precision=176, |
| 239 | offset=bt2.ClockClassOffset(45, 3003), |
| 240 | is_absolute=True, uuid=uuid.uuid1()) |
| 241 | cc2 = bt2.ClockClass(name='name', description='some description', |
| 242 | frequency=1001, precision=176, |
| 243 | offset=bt2.ClockClassOffset(45, 3003), |
| 244 | is_absolute=True, uuid=uuid.uuid1()) |
| 245 | self.assertNotEqual(cc1, cc2) |
| 246 | |
| 247 | def test_eq_invalid(self): |
| 248 | self.assertFalse(self._cc == 23) |
| 249 | |
| 250 | |
| 251 | class ClockValueTestCase(unittest.TestCase): |
| 252 | def setUp(self): |
| 253 | self._cc = bt2.ClockClass('salut', 1000, |
| 254 | offset=bt2.ClockClassOffset(45, 354)) |
| 255 | self._cv = self._cc(123) |
| 256 | |
| 257 | def tearDown(self): |
| 258 | del self._cc |
| 259 | del self._cv |
| 260 | |
| 261 | def test_create_default(self): |
| 262 | self.assertEqual(self._cv.clock_class.addr, self._cc.addr) |
| 263 | self.assertEqual(self._cv.cycles, 123) |
| 264 | |
| 265 | def test_create_invalid_cycles_type(self): |
| 266 | with self.assertRaises(TypeError): |
| 267 | self._cc('yes') |
| 268 | |
| 269 | def test_ns_from_epoch(self): |
| 270 | s_from_epoch = 45 + ((354 + 123) / 1000) |
| 271 | ns_from_epoch = int(s_from_epoch * 1e9) |
| 272 | self.assertEqual(self._cv.ns_from_epoch, ns_from_epoch) |
| 273 | |
| 274 | def test_eq(self): |
| 275 | cv1 = self._cc(123) |
| 276 | cv2 = self._cc(123) |
| 277 | self.assertEqual(cv1, cv2) |
| 278 | |
| 279 | def test_eq_int(self): |
| 280 | cv1 = self._cc(123) |
| 281 | self.assertEqual(cv1, 123) |
| 282 | |
| 283 | def test_ne_clock_class(self): |
| 284 | cc1 = bt2.ClockClass('yes', 1500) |
| 285 | cc2 = bt2.ClockClass('yes', 1501) |
| 286 | cv1 = cc1(123) |
| 287 | cv2 = cc2(123) |
| 288 | self.assertNotEqual(cv1, cv2) |
| 289 | |
| 290 | def test_ne_cycles(self): |
| 291 | cv1 = self._cc(123) |
| 292 | cv2 = self._cc(125) |
| 293 | self.assertNotEqual(cv1, cv2) |
| 294 | |
| 295 | def test_eq_invalid(self): |
| 296 | self.assertFalse(self._cv == 23) |
| 297 | |
| 298 | def _test_copy(self, cpy): |
| 299 | self.assertIsNot(cpy, self._cv) |
| 300 | self.assertNotEqual(cpy.addr, self._cv.addr) |
| 301 | self.assertEqual(cpy, self._cv) |
| 302 | |
| 303 | def test_copy(self): |
| 304 | cpy = copy.copy(self._cv) |
| 305 | self._test_copy(cpy) |
| 306 | |
| 307 | def test_deepcopy(self): |
| 308 | cpy = copy.deepcopy(self._cv) |
| 309 | self._test_copy(cpy) |