-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathUIDTest.cpp
More file actions
443 lines (378 loc) · 13.9 KB
/
UIDTest.cpp
File metadata and controls
443 lines (378 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* UIDTest.cpp
* Test fixture for the UID classes
* Copyright (C) 2005 Simon Newton
*/
#include <cppunit/extensions/HelperMacros.h>
#include <string.h>
#include <string>
#include "ola/rdm/UID.h"
#include "ola/rdm/UIDSet.h"
#include "ola/testing/TestUtils.h"
using std::string;
using ola::rdm::UID;
using ola::rdm::UIDSet;
class UIDTest: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(UIDTest);
CPPUNIT_TEST(testUID);
CPPUNIT_TEST(testRPTUID);
CPPUNIT_TEST(testUIDInequalities);
CPPUNIT_TEST(testUIDSet);
CPPUNIT_TEST(testUIDSetUnion);
CPPUNIT_TEST(testUIDParse);
CPPUNIT_TEST(testDirectedToUID);
CPPUNIT_TEST_SUITE_END();
public:
void testUID();
void testRPTUID();
void testUIDInequalities();
void testUIDSet();
void testUIDSetUnion();
void testUIDParse();
void testDirectedToUID();
};
CPPUNIT_TEST_SUITE_REGISTRATION(UIDTest);
/*
* Test the UIDs work.
*/
void UIDTest::testUID() {
UID uid(1, 2);
OLA_ASSERT_FALSE(uid.IsBroadcast());
OLA_ASSERT_FALSE(uid.IsVendorcast());
UID uid2 = uid;
OLA_ASSERT_EQ(uid, uid2);
OLA_ASSERT_FALSE(uid != uid2);
OLA_ASSERT_EQ((uint16_t) 1, uid.ManufacturerId());
OLA_ASSERT_EQ((uint32_t) 2, uid.DeviceId());
UID uid3(2, 10);
OLA_ASSERT_NE(uid, uid3);
OLA_ASSERT_LT(uid, uid3);
OLA_ASSERT_EQ((uint16_t) 2, uid3.ManufacturerId());
OLA_ASSERT_EQ((uint32_t) 10, uid3.DeviceId());
UID uid4(0x0000000400000002ULL);
OLA_ASSERT_EQ((uint16_t) 4, uid4.ManufacturerId());
OLA_ASSERT_EQ((uint32_t) 2, uid4.DeviceId());
// ToUInt64
OLA_ASSERT_EQ((uint64_t) 0x000100000002ULL, uid.ToUInt64());
OLA_ASSERT_EQ((uint64_t) 0x00020000000aULL, uid3.ToUInt64());
// ToString
OLA_ASSERT_EQ(string("0001:00000002"), uid.ToString());
OLA_ASSERT_EQ(string("0002:0000000a"), uid3.ToString());
UID all_devices = UID::AllDevices();
UID manufacturer_devices = UID::VendorcastAddress(0x52);
UID manufacturer_devices2 = UID::VendorcastAddress(uid);
OLA_ASSERT_EQ(string("ffff:ffffffff"), all_devices.ToString());
OLA_ASSERT_EQ(string("0052:ffffffff"),
manufacturer_devices.ToString());
OLA_ASSERT_EQ(string("0001:ffffffff"),
manufacturer_devices2.ToString());
OLA_ASSERT_EQ(all_devices.ManufacturerId(),
static_cast<uint16_t>(0xffff));
OLA_ASSERT_EQ(all_devices.DeviceId(),
static_cast<uint32_t>(0xffffffff));
OLA_ASSERT_EQ(manufacturer_devices.ManufacturerId(),
static_cast<uint16_t>(0x0052));
OLA_ASSERT_EQ(manufacturer_devices.DeviceId(),
static_cast<uint32_t>(0xffffffff));
OLA_ASSERT_EQ(manufacturer_devices2.ManufacturerId(),
static_cast<uint16_t>(0x0001));
OLA_ASSERT_EQ(manufacturer_devices2.DeviceId(),
static_cast<uint32_t>(0xffffffff));
OLA_ASSERT_TRUE(all_devices.IsBroadcast());
OLA_ASSERT_FALSE(all_devices.IsVendorcast());
OLA_ASSERT_TRUE(manufacturer_devices.IsBroadcast());
OLA_ASSERT_TRUE(manufacturer_devices.IsVendorcast());
OLA_ASSERT_TRUE(manufacturer_devices2.IsBroadcast());
OLA_ASSERT_TRUE(manufacturer_devices2.IsVendorcast());
// now test the packing & unpacking
unsigned int buffer_size = UID::UID_SIZE;
uint8_t *buffer = new uint8_t[buffer_size];
OLA_ASSERT_TRUE(uid.Pack(buffer, buffer_size));
uint8_t expected[] = {0, 1, 0, 0, 0, 2};
OLA_ASSERT_DATA_EQUALS(expected, sizeof(expected), buffer, buffer_size);
UID unpacked_uid1(buffer);
OLA_ASSERT_EQ(uid, unpacked_uid1);
OLA_ASSERT_TRUE(uid3.Pack(buffer, buffer_size));
uint8_t expected2[] = {0, 2, 0, 0, 0, 0x0a};
OLA_ASSERT_DATA_EQUALS(expected2, sizeof(expected2), buffer, buffer_size);
UID unpacked_uid2(buffer);
OLA_ASSERT_EQ(uid3, unpacked_uid2);
delete[] buffer;
}
/*
* Test the RPT UIDs work.
*/
void UIDTest::testRPTUID() {
UID uid(1, 2);
UID rpt_all_controllers = UID::RPTAllControllers();
UID rpt_all_devices = UID::RPTAllDevices();
UID rpt_manufacturer_devices = UID::RPTVendorcastAddressDevices(0x52);
UID rpt_manufacturer_devices2 = UID::RPTVendorcastAddressDevices(uid);
OLA_ASSERT_EQ(string("fffc:ffffffff"), rpt_all_controllers.ToString());
OLA_ASSERT_EQ(string("fffd:ffffffff"), rpt_all_devices.ToString());
OLA_ASSERT_EQ(string("fffd:0052ffff"),
rpt_manufacturer_devices.ToString());
OLA_ASSERT_EQ(string("fffd:0001ffff"),
rpt_manufacturer_devices2.ToString());
OLA_ASSERT_EQ(rpt_all_controllers.ManufacturerId(),
static_cast<uint16_t>(0xfffc));
OLA_ASSERT_EQ(rpt_all_controllers.DeviceId(),
static_cast<uint32_t>(0xffffffff));
OLA_ASSERT_EQ(rpt_all_devices.ManufacturerId(),
static_cast<uint16_t>(0xfffd));
OLA_ASSERT_EQ(rpt_all_devices.DeviceId(),
static_cast<uint32_t>(0xffffffff));
OLA_ASSERT_EQ(rpt_manufacturer_devices.ManufacturerId(),
static_cast<uint16_t>(0xfffd));
OLA_ASSERT_EQ(rpt_manufacturer_devices.DeviceId(),
static_cast<uint32_t>(0x0052ffff));
OLA_ASSERT_EQ(rpt_manufacturer_devices2.ManufacturerId(),
static_cast<uint16_t>(0xfffd));
OLA_ASSERT_EQ(rpt_manufacturer_devices2.DeviceId(),
static_cast<uint32_t>(0x0001ffff));
// TODO(Peter): Handle the more complicated RPT vendorcast tests
OLA_ASSERT_TRUE(rpt_all_controllers.IsBroadcast());
OLA_ASSERT_FALSE(rpt_all_controllers.IsVendorcast());
OLA_ASSERT_TRUE(rpt_all_devices.IsBroadcast());
OLA_ASSERT_FALSE(rpt_all_devices.IsVendorcast());
OLA_ASSERT_TRUE(rpt_manufacturer_devices.IsBroadcast());
OLA_ASSERT_TRUE(rpt_manufacturer_devices.IsVendorcast());
OLA_ASSERT_TRUE(rpt_manufacturer_devices2.IsBroadcast());
OLA_ASSERT_TRUE(rpt_manufacturer_devices2.IsVendorcast());
}
/*
* Test the UIDs inequalities work
*/
void UIDTest::testUIDInequalities() {
uint16_t MOCK_ESTA_ID = 0x7a70;
// check comparisons on the device id
UID uid1(MOCK_ESTA_ID, 0);
UID uid2(MOCK_ESTA_ID, 1);
UID uid3(MOCK_ESTA_ID, 2);
OLA_ASSERT_TRUE(uid1 < uid2);
OLA_ASSERT_TRUE(uid1 < uid3);
OLA_ASSERT_TRUE(uid2 < uid3);
OLA_ASSERT_TRUE(uid3 > uid1);
OLA_ASSERT_TRUE(uid2 > uid1);
OLA_ASSERT_TRUE(uid3 > uid2);
// check we're using unsigned ints for the device id
UID uid4(MOCK_ESTA_ID, 0x80000000);
UID uid5(MOCK_ESTA_ID, 0xffffffff);
OLA_ASSERT_LT(uid1, uid4);
OLA_ASSERT_LT(uid2, uid4);
OLA_ASSERT_LT(uid3, uid4);
OLA_ASSERT_LT(uid1, uid5);
OLA_ASSERT_LT(uid2, uid5);
OLA_ASSERT_LT(uid3, uid5);
OLA_ASSERT_LT(uid4, uid5);
OLA_ASSERT_GT(uid4, uid1);
OLA_ASSERT_GT(uid4, uid2);
OLA_ASSERT_GT(uid4, uid3);
OLA_ASSERT_GT(uid5, uid1);
OLA_ASSERT_GT(uid5, uid2);
OLA_ASSERT_GT(uid5, uid3);
OLA_ASSERT_GT(uid5, uid4);
// test the manufacturer ID
UID uid6(MOCK_ESTA_ID - 1, 0xffffffff);
OLA_ASSERT_LT(uid6, uid1);
OLA_ASSERT_LT(uid6, uid4);
OLA_ASSERT_LT(uid6, uid5);
OLA_ASSERT_GT(uid1, uid6);
OLA_ASSERT_GT(uid4, uid6);
OLA_ASSERT_GT(uid5, uid6);
UID uid7(MOCK_ESTA_ID + 1, 0);
OLA_ASSERT_LT(uid1, uid7);
OLA_ASSERT_LT(uid4, uid7);
OLA_ASSERT_LT(uid5, uid7);
OLA_ASSERT_LT(uid6, uid7);
OLA_ASSERT_GT(uid7, uid1);
OLA_ASSERT_GT(uid7, uid4);
OLA_ASSERT_GT(uid7, uid5);
OLA_ASSERT_GT(uid7, uid6);
// now some tests that would expose problems if we used signed ints
UID uid8(0x8000, 0);
OLA_ASSERT_LT(uid1, uid8);
OLA_ASSERT_LT(uid2, uid8);
OLA_ASSERT_LT(uid3, uid8);
OLA_ASSERT_LT(uid4, uid8);
OLA_ASSERT_LT(uid5, uid8);
OLA_ASSERT_LT(uid6, uid8);
OLA_ASSERT_GT(uid8, uid1);
OLA_ASSERT_GT(uid8, uid4);
OLA_ASSERT_GT(uid8, uid5);
OLA_ASSERT_GT(uid8, uid6);
OLA_ASSERT_GT(uid8, uid7);
}
/*
* Test the UIDSet
*/
void UIDTest::testUIDSet() {
UIDSet set1;
OLA_ASSERT_EQ(0u, set1.Size());
UID uid(1, 2);
UID uid2(2, 10);
set1.AddUID(uid);
OLA_ASSERT_EQ(1u, set1.Size());
OLA_ASSERT_EQ(string("0001:00000002"), set1.ToString());
OLA_ASSERT_TRUE(set1.Contains(uid));
OLA_ASSERT_FALSE(set1.Contains(uid2));
set1.AddUID(uid);
OLA_ASSERT_EQ(1u, set1.Size());
set1.AddUID(uid2);
OLA_ASSERT_EQ(2u, set1.Size());
OLA_ASSERT_EQ(string("0001:00000002,0002:0000000a"), set1.ToString());
OLA_ASSERT_TRUE(set1.Contains(uid));
OLA_ASSERT_TRUE(set1.Contains(uid2));
UIDSet set2(set1);
OLA_ASSERT_EQ(set1, set2);
UIDSet set3;
OLA_ASSERT_EQ(0u, set3.Size());
set3 = set2;
OLA_ASSERT_EQ(set1, set2);
set3.RemoveUID(uid2);
OLA_ASSERT_EQ(1u, set3.Size());
OLA_ASSERT_EQ(string("0001:00000002"), set3.ToString());
UIDSet difference = set1.SetDifference(set3);
OLA_ASSERT_EQ(1u, difference.Size());
OLA_ASSERT_TRUE(set1.Contains(uid));
OLA_ASSERT_TRUE(set1.Contains(uid2));
difference = set3.SetDifference(set1);
OLA_ASSERT_EQ(0u, difference.Size());
// now test the packing & unpacking
UID uid3(3, 4);
UIDSet set4(set1);
set4.AddUID(uid3);
unsigned int buffer_size = UID::UID_SIZE * 3;
uint8_t *buffer = new uint8_t[buffer_size];
OLA_ASSERT_TRUE(set4.Pack(buffer, buffer_size));
uint8_t expected[] = {0, 1, 0, 0, 0, 2, 0, 2, 0, 0, 0, 10, 0, 3, 0, 0, 0, 4};
OLA_ASSERT_DATA_EQUALS(expected, sizeof(expected), buffer, buffer_size);
// Creating UIDSets from binary data
uint8_t empty[] = {};
unsigned int data_size = sizeof(empty);
UIDSet set5(empty, &data_size);
UIDSet empty_set = UIDSet();
OLA_ASSERT_EQ(set5, empty_set);
uint8_t raw_short[] = {0, 1};
data_size = sizeof(raw_short);
UIDSet set6(raw_short, &data_size);
OLA_ASSERT_EQ(set6, empty_set);
UIDSet set7;
set7.AddUID(UID(1, 2));
uint8_t raw_one[] = {0, 1, 0, 0, 0, 2};
data_size = sizeof(raw_one);
UIDSet set8(raw_one, &data_size);
OLA_ASSERT_EQ(6u, data_size);
OLA_ASSERT_EQ(set7, set8);
uint8_t raw_one_extra[] = {0, 1, 0, 0, 0, 2, 3, 4};
data_size = sizeof(raw_one_extra);
UIDSet set9(raw_one_extra, &data_size);
OLA_ASSERT_EQ(6u, data_size);
OLA_ASSERT_EQ(set7, set9);
set7.AddUID(UID(2, 10));
set7.AddUID(UID(3, 4));
uint8_t raw_three[] =
{0, 1, 0, 0, 0, 2, 0, 2, 0, 0, 0, 10, 0, 3, 0, 0, 0, 4};
data_size = sizeof(raw_three);
UIDSet set10(raw_three, &data_size);
OLA_ASSERT_EQ(18u, data_size);
OLA_ASSERT_EQ(set7, set10);
uint8_t raw_three_extra[] =
{0, 1, 0, 0, 0, 2, 0, 2, 0, 0, 0, 10, 0, 3, 0, 0, 0, 4, 5, 6};
data_size = sizeof(raw_three_extra);
UIDSet set11(raw_three_extra, &data_size);
OLA_ASSERT_EQ(18u, data_size);
OLA_ASSERT_EQ(set7, set11);
}
/*
* Test the UIDSet Union method.
*/
void UIDTest::testUIDSetUnion() {
UIDSet set1, set2, expected;
UID uid(1, 2);
UID uid2(2, 10);
UID uid3(3, 10);
UID uid4(4, 10);
set1.AddUID(uid);
set2.AddUID(uid2);
set2.AddUID(uid3);
set2.AddUID(uid4);
UIDSet union_set = set1.Union(set2);
OLA_ASSERT_EQ(4u, union_set.Size());
OLA_ASSERT_TRUE(union_set.Contains(uid));
OLA_ASSERT_TRUE(union_set.Contains(uid2));
OLA_ASSERT_TRUE(union_set.Contains(uid3));
OLA_ASSERT_TRUE(union_set.Contains(uid4));
}
/*
* Test UID parsing
*/
void UIDTest::testUIDParse() {
UID *uid = UID::FromString("ffff:00000000");
OLA_ASSERT_NOT_NULL(uid);
OLA_ASSERT_EQ(uid->ManufacturerId(), static_cast<uint16_t>(0xffff));
OLA_ASSERT_EQ(uid->DeviceId(), static_cast<uint32_t>(0x00));
OLA_ASSERT_EQ(uid->ToString(), string("ffff:00000000"));
delete uid;
uid = UID::FromString("1234:567890ab");
OLA_ASSERT_NOT_NULL(uid);
OLA_ASSERT_EQ(uid->ManufacturerId(), static_cast<uint16_t>(0x1234));
OLA_ASSERT_EQ(uid->DeviceId(), static_cast<uint32_t>(0x567890ab));
OLA_ASSERT_EQ(uid->ToString(), string("1234:567890ab"));
delete uid;
uid = UID::FromString("abcd:ef123456");
OLA_ASSERT_NOT_NULL(uid);
OLA_ASSERT_EQ(uid->ManufacturerId(), static_cast<uint16_t>(0xabcd));
OLA_ASSERT_EQ(uid->DeviceId(), static_cast<uint32_t>(0xef123456));
OLA_ASSERT_EQ(uid->ToString(), string("abcd:ef123456"));
delete uid;
OLA_ASSERT_FALSE(UID::FromString(""));
OLA_ASSERT_FALSE(UID::FromString(":"));
OLA_ASSERT_FALSE(UID::FromString("0:0"));
OLA_ASSERT_FALSE(UID::FromString(":123456"));
OLA_ASSERT_FALSE(UID::FromString(":123456"));
OLA_ASSERT_FALSE(UID::FromString("abcd:123456"));
}
/**
* Test DirectedToUID()
*/
void UIDTest::testDirectedToUID() {
const uint16_t MANUFACTURER_ID = 0x7a70;
UID device_uid(MANUFACTURER_ID, 10);
// test a direct match
OLA_ASSERT_TRUE(device_uid.DirectedToUID(device_uid));
// test a different device
UID other_device(MANUFACTURER_ID, 9);
OLA_ASSERT_FALSE(other_device.DirectedToUID(device_uid));
// test broadcast
UID broadcast_uid = UID::AllDevices();
OLA_ASSERT_TRUE(broadcast_uid.DirectedToUID(device_uid));
// test vendorcast passing manufacturer ID
UID vendorcast_uid = UID::VendorcastAddress(MANUFACTURER_ID);
OLA_ASSERT_TRUE(vendorcast_uid.DirectedToUID(device_uid));
// test vendorcast passing UID
UID other_device_uid(MANUFACTURER_ID, 11);
UID vendorcast_uid_2 = UID::VendorcastAddress(other_device_uid);
OLA_ASSERT_TRUE(vendorcast_uid_2.DirectedToUID(device_uid));
// test another vendor passing manufacturer ID
UID other_vendorcast_uid = UID::VendorcastAddress(MANUFACTURER_ID - 1);
OLA_ASSERT_FALSE(other_vendorcast_uid.DirectedToUID(device_uid));
// test another vendor passing UID
UID other_manufacturer_uid(MANUFACTURER_ID - 1, 10);
UID other_vendorcast_uid_2 = UID::VendorcastAddress(other_manufacturer_uid);
OLA_ASSERT_FALSE(other_vendorcast_uid_2.DirectedToUID(device_uid));
}