Fixed MacAddress toString function for different formats; Introduced even more MacAdd...
[public/netxms.git] / tests / test-libnetxms / test-libnetxms.cpp
1 #include <nms_common.h>
2 #include <nms_util.h>
3 #include <nxqueue.h>
4 #include <nxcpapi.h>
5 #include <testtools.h>
6
7 void TestMsgWaitQueue();
8 void TestMessageClass();
9 void TestMutexWrapper();
10 void TestRWLockWrapper();
11 void TestConditionWrapper();
12
13 static char mbText[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
14 static WCHAR wcText[] = L"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
15 static char mbTextShort[] = "Lorem ipsum";
16 static UCS2CHAR ucs2TextShort[] = { 'L', 'o', 'r', 'e', 'm', ' ', 'i', 'p', 's', 'u', 'm', 0 };
17
18 /**
19 * Test string conversion
20 */
21 static void TestStringConversion()
22 {
23 StartTest(_T("ANSI to UCS-2 conversion"));
24 UCS2CHAR ucs2buffer[1024];
25 mb_to_ucs2(mbTextShort, -1, ucs2buffer, 1024);
26 AssertTrue(!memcmp(ucs2buffer, ucs2TextShort, sizeof(UCS2CHAR) * 12));
27 EndTest();
28
29 StartTest(_T("UCS-2 to ANSI conversion"));
30 char mbBuffer[1024];
31 ucs2_to_mb(ucs2TextShort, -1, mbBuffer, 1024);
32 AssertTrue(!strcmp(mbBuffer, mbTextShort));
33 EndTest();
34
35 StartTest(_T("ANSI to UCS-2 conversion performance"));
36 INT64 start = GetCurrentTimeMs();
37 for(int i = 0; i < 10000; i++)
38 {
39 UCS2CHAR buffer[1024];
40 mb_to_ucs2(mbText, -1, buffer, 1024);
41 }
42 EndTest(GetCurrentTimeMs() - start);
43
44 StartTest(_T("UCS-2 to ANSI conversion performance"));
45 mb_to_ucs2(mbText, -1, ucs2buffer, 1024);
46 start = GetCurrentTimeMs();
47 for(int i = 0; i < 10000; i++)
48 {
49 char buffer[1024];
50 ucs2_to_mb(ucs2buffer, -1, buffer, 1024);
51 }
52 EndTest(GetCurrentTimeMs() - start);
53
54 #ifdef UNICODE_UCS4
55 StartTest(_T("ANSI to UCS-4 conversion performance"));
56 start = GetCurrentTimeMs();
57 for(int i = 0; i < 10000; i++)
58 {
59 WCHAR buffer[1024];
60 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mbText, -1, buffer, 1024);
61 }
62 EndTest(GetCurrentTimeMs() - start);
63
64 StartTest(_T("UCS-4 to ANSI conversion performance"));
65 start = GetCurrentTimeMs();
66 for(int i = 0; i < 10000; i++)
67 {
68 char buffer[1024];
69 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcText, -1, buffer, 1024, NULL, NULL);
70 }
71 EndTest(GetCurrentTimeMs() - start);
72
73 StartTest(_T("UCS-2 to UCS-4 conversion performance"));
74 mb_to_ucs2(mbText, -1, ucs2buffer, 1024);
75 start = GetCurrentTimeMs();
76 for(int i = 0; i < 10000; i++)
77 {
78 WCHAR buffer[1024];
79 ucs2_to_ucs4(ucs2buffer, -1, buffer, 1024);
80 }
81 EndTest(GetCurrentTimeMs() - start);
82
83 StartTest(_T("UCS-4 to UCS-2 conversion performance"));
84 start = GetCurrentTimeMs();
85 for(int i = 0; i < 10000; i++)
86 {
87 UCS2CHAR buffer[1024];
88 ucs4_to_ucs2(wcText, -1, buffer, 1024);
89 }
90 EndTest(GetCurrentTimeMs() - start);
91 #endif
92 }
93
94 /**
95 * Test string map
96 */
97 static void TestStringMap()
98 {
99 StringMap *m = new StringMap();
100
101 StartTest(_T("String map - insert"));
102 INT64 start = GetCurrentTimeMs();
103 for(int i = 0; i < 10000; i++)
104 {
105 TCHAR key[64];
106 _sntprintf(key, 64, _T("key-%d"), i);
107 m->set(key, _T("Lorem ipsum dolor sit amet"));
108 }
109 AssertEquals(m->size(), 10000);
110 const TCHAR *v = m->get(_T("key-42"));
111 AssertNotNull(v);
112 AssertTrue(!_tcscmp(v, _T("Lorem ipsum dolor sit amet")));
113 EndTest(GetCurrentTimeMs() - start);
114
115 StartTest(_T("String map - replace"));
116 start = GetCurrentTimeMs();
117 for(int i = 0; i < 10000; i++)
118 {
119 TCHAR key[64];
120 _sntprintf(key, 64, _T("key-%d"), i);
121 m->set(key, _T("consectetur adipiscing elit"));
122 }
123 AssertEquals(m->size(), 10000);
124 v = m->get(_T("key-42"));
125 AssertNotNull(v);
126 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
127 EndTest(GetCurrentTimeMs() - start);
128
129 StartTest(_T("String map - get"));
130 start = GetCurrentTimeMs();
131 v = m->get(_T("key-888"));
132 AssertNotNull(v);
133 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
134 EndTest(GetCurrentTimeMs() - start);
135
136 StartTest(_T("String map - clear"));
137 start = GetCurrentTimeMs();
138 m->clear();
139 AssertEquals(m->size(), 0);
140 EndTest(GetCurrentTimeMs() - start);
141
142 delete m;
143 }
144
145 /**
146 * Test string set
147 */
148 static void TestStringSet()
149 {
150 StringSet *s = new StringSet();
151
152 StartTest(_T("String set - insert"));
153 INT64 start = GetCurrentTimeMs();
154 for(int i = 0; i < 10000; i++)
155 {
156 TCHAR key[64];
157 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
158 s->add(key);
159 }
160 AssertEquals(s->size(), 10000);
161 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
162 EndTest(GetCurrentTimeMs() - start);
163
164 StartTest(_T("String set - replace"));
165 start = GetCurrentTimeMs();
166 for(int i = 0; i < 10000; i++)
167 {
168 TCHAR key[64];
169 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
170 s->add(key);
171 }
172 AssertEquals(s->size(), 10000);
173 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
174 EndTest(GetCurrentTimeMs() - start);
175
176 StartTest(_T("String set - contains"));
177 start = GetCurrentTimeMs();
178 AssertTrue(s->contains(_T("key-888 lorem ipsum")));
179 EndTest(GetCurrentTimeMs() - start);
180
181 StartTest(_T("String set - iterator"));
182 Iterator<const TCHAR> *it = s->iterator();
183 AssertTrue(it->hasNext());
184 bool found = false;
185 while(it->hasNext())
186 {
187 const TCHAR *v = it->next();
188 AssertNotNull(v);
189 if (!_tcscmp(v, _T("key-42 lorem ipsum")))
190 {
191 found = true;
192 break;
193 }
194 }
195 AssertTrue(found);
196 it->remove();
197 AssertEquals(s->size(), 9999);
198 AssertFalse(s->contains(_T("key-42 lorem ipsum")));
199 delete it;
200 EndTest();
201
202 StartTest(_T("String set - clear"));
203 start = GetCurrentTimeMs();
204 s->clear();
205 AssertEquals(s->size(), 0);
206 EndTest(GetCurrentTimeMs() - start);
207
208 delete s;
209 }
210
211 /**
212 * Test string class
213 */
214 static void TestString()
215 {
216 String s;
217
218 StartTest(_T("String - append"));
219 for(int i = 0; i < 256; i++)
220 s.append(_T("ABC "));
221 AssertEquals(s.length(), 1024);
222 AssertTrue(!_tcsncmp(s.getBuffer(), _T("ABC ABC ABC ABC "), 16));
223 EndTest();
224
225 StartTest(_T("String - assign #1"));
226 s = _T("alpha");
227 AssertEquals(s.length(), 5);
228 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha")));
229 EndTest();
230
231 StartTest(_T("String - assign #2"));
232 String t(_T("init string"));
233 s = t;
234 AssertEquals(s.length(), 11);
235 AssertTrue(!_tcscmp(s.getBuffer(), _T("init string")));
236 EndTest();
237
238 StartTest(_T("String - shrink"));
239 s.shrink();
240 AssertEquals(s.length(), 10);
241 AssertTrue(!_tcscmp(s.getBuffer(), _T("init strin")));
242 EndTest();
243
244 StartTest(_T("String - escape"));
245 s.escapeCharacter('i', '+');
246 AssertEquals(s.length(), 13);
247 AssertTrue(!_tcscmp(s.getBuffer(), _T("+in+it str+in")));
248 EndTest();
249
250 StartTest(_T("String - replace #1"));
251 s = _T("alpha beta gamma");
252 s.replace(_T("beta"), _T("epsilon"));
253 AssertEquals(s.length(), 19);
254 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha epsilon gamma")));
255 EndTest();
256
257 StartTest(_T("String - replace #2"));
258 s = _T("alpha beta gamma");
259 s.replace(_T("beta"), _T("xxxx"));
260 AssertEquals(s.length(), 16);
261 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha xxxx gamma")));
262 EndTest();
263
264 StartTest(_T("String - replace #3"));
265 s = _T("alpha beta gamma alpha omega");
266 s.replace(_T("alpha"), _T("Z"));
267 AssertEquals(s.length(), 20);
268 AssertTrue(!_tcscmp(s.getBuffer(), _T("Z beta gamma Z omega")));
269 EndTest();
270
271 StartTest(_T("String - substring #1"));
272 s = _T("alpha beta gamma");
273 TCHAR *str = s.substring(0, 5, NULL);
274 AssertTrue(!_tcscmp(str, _T("alpha")));
275 free(str);
276 EndTest();
277
278 StartTest(_T("String - substring #2"));
279 s = _T("alpha beta gamma");
280 str = s.substring(5, -1, NULL);
281 AssertTrue(!_tcscmp(str, _T(" beta gamma")));
282 free(str);
283 EndTest();
284
285 StartTest(_T("String - substring #3"));
286 s = _T("alpha beta gamma");
287 str = s.substring(14, 4, NULL);
288 AssertTrue(!_tcscmp(str, _T("ma")));
289 free(str);
290 EndTest();
291
292 StartTest(_T("String - left #1"));
293 s = _T("alpha beta gamma");
294 String ls = s.left(5);
295 AssertTrue(ls.equals(_T("alpha")));
296 EndTest();
297
298 StartTest(_T("String - left #2"));
299 s = _T("alpha");
300 ls = s.left(15);
301 AssertTrue(ls.equals(_T("alpha")));
302 EndTest();
303
304 StartTest(_T("String - right #1"));
305 s = _T("alpha beta gamma");
306 String rs = s.right(5);
307 AssertTrue(rs.equals(_T("gamma")));
308 EndTest();
309
310 StartTest(_T("String - right #2"));
311 s = _T("alpha");
312 rs = s.right(15);
313 AssertTrue(rs.equals(_T("alpha")));
314 EndTest();
315 }
316
317 /**
318 * Test MacAddress class
319 */
320 static void TestMacAddress()
321 {
322 MacAddress a, b, c, d, e, f;
323
324 StartTest(_T("MacAddress - parse()"));
325 a = MacAddress::parse("0180C2300100");
326 AssertTrue(_tcscmp(_T("0180C2300100"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
327 AssertTrue(_tcscmp(_T("01:80:C2:30:01:00"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
328 AssertTrue(_tcscmp(_T("01-80-C2-30-01-00"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
329 AssertTrue(_tcscmp(_T("018.0C2.300.100"), (const TCHAR*)a.toString(MAC_ADDR_DOT_SEPARATED)) == 0);
330 AssertTrue(_tcscmp(_T("0180.C230.0100"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
331 AssertTrue(_tcscmp(_T("0180:C230:0100"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
332
333 a = MacAddress::parse("01:80:C2:00:00:00");
334 AssertTrue(_tcscmp(_T("0180C2000000"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
335 AssertTrue(_tcscmp(_T("01:80:C2:00:00:00"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
336 AssertTrue(_tcscmp(_T("01-80-C2-00-00-00"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
337 AssertTrue(_tcscmp(_T("018.0C2.000.000"), (const TCHAR*)a.toString(MAC_ADDR_DOT_SEPARATED)) == 0);
338 AssertTrue(_tcscmp(_T("0180.C200.0000"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
339 AssertTrue(_tcscmp(_T("0180:C200:0000"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
340
341 a = MacAddress::parse("09-80-C2-FF-FF-FF");
342 AssertTrue(_tcscmp(_T("0980C2FFFFFF"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
343 AssertTrue(_tcscmp(_T("09:80:C2:FF:FF:FF"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
344 AssertTrue(_tcscmp(_T("09-80-C2-FF-FF-FF"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
345 AssertTrue(_tcscmp(_T("098.0C2.FFF.FFF"), (const TCHAR*)a.toString(MAC_ADDR_DOT_SEPARATED)) == 0);
346 AssertTrue(_tcscmp(_T("0980.C2FF.FFFF"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
347 AssertTrue(_tcscmp(_T("0980:C2FF:FFFF"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
348
349 a = MacAddress::parse("482.C6A.1E5.93D");
350 AssertTrue(_tcscmp(_T("482C6A1E593D"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
351 AssertTrue(_tcscmp(_T("48:2C:6A:1E:59:3D"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
352 AssertTrue(_tcscmp(_T("48-2C-6A-1E-59-3D"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
353 AssertTrue(_tcscmp(_T("482.C6A.1E5.93D"), (const TCHAR*)a.toString(MAC_ADDR_DOT_SEPARATED)) == 0);
354 AssertTrue(_tcscmp(_T("482C.6A1E.593D"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
355 AssertTrue(_tcscmp(_T("482C:6A1E:593D"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
356
357 a = MacAddress::parse("48AC.6B1A.F6FD");
358 AssertTrue(_tcscmp(_T("48AC6B1AF6FD"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
359 AssertTrue(_tcscmp(_T("48:AC:6B:1A:F6:FD"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
360 AssertTrue(_tcscmp(_T("48-AC-6B-1A-F6-FD"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
361 AssertTrue(_tcscmp(_T("48A.C6B.1AF.6FD"), (const TCHAR*)a.toString(MAC_ADDR_DOT_SEPARATED)) == 0);
362 AssertTrue(_tcscmp(_T("48AC.6B1A.F6FD"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
363 AssertTrue(_tcscmp(_T("48AC:6B1A:F6FD"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
364
365 a = MacAddress::parse("482C:6A1E:593D");
366 AssertTrue(_tcscmp(_T("482C6A1E593D"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
367 AssertTrue(_tcscmp(_T("48:2C:6A:1E:59:3D"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
368 AssertTrue(_tcscmp(_T("48-2C-6A-1E-59-3D"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
369 AssertTrue(_tcscmp(_T("482.C6A.1E5.93D"), (const TCHAR*)a.toString(MAC_ADDR_DOT_SEPARATED)) == 0);
370 AssertTrue(_tcscmp(_T("482C.6A1E.593D"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
371 AssertTrue(_tcscmp(_T("482C:6A1E:593D"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
372
373 a = MacAddress::parse("70B3D5B020035F11");
374 AssertTrue(_tcscmp(_T("70B3D5B020035F11"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
375 AssertTrue(_tcscmp(_T("70:B3:D5:B0:20:03:5F:11"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
376 AssertTrue(_tcscmp(_T("70-B3-D5-B0-20-03-5F-11"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
377 AssertTrue(_tcscmp(_T("70B3.D5B0.2003.5F11"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
378 AssertTrue(_tcscmp(_T("70B3:D5B0:2003:5F11"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
379
380 a = MacAddress::parse("70:B3:D5:B0:20:03:5F:16");
381 AssertTrue(_tcscmp(_T("70B3D5B020035F16"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
382 AssertTrue(_tcscmp(_T("70:B3:D5:B0:20:03:5F:16"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
383 AssertTrue(_tcscmp(_T("70-B3-D5-B0-20-03-5F-16"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
384 AssertTrue(_tcscmp(_T("70B3.D5B0.2003.5F16"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
385 AssertTrue(_tcscmp(_T("70B3:D5B0:2003:5F16"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
386
387 a = MacAddress::parse("70-B3-D5-B0-20-00-00-39");
388 AssertTrue(_tcscmp(_T("70B3D5B020000039"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
389 AssertTrue(_tcscmp(_T("70:B3:D5:B0:20:00:00:39"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
390 AssertTrue(_tcscmp(_T("70-B3-D5-B0-20-00-00-39"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
391 AssertTrue(_tcscmp(_T("70B3.D5B0.2000.0039"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
392 AssertTrue(_tcscmp(_T("70B3:D5B0:2000:0039"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
393
394 a = MacAddress::parse("70B3.D5B0.2003.5D5E");
395 AssertTrue(_tcscmp(_T("70B3D5B020035D5E"), (const TCHAR*)a.toString(MAC_ADDR_FLAT_STRING)) == 0);
396 AssertTrue(_tcscmp(_T("70:B3:D5:B0:20:03:5D:5E"), (const TCHAR*)a.toString(MAC_ADDR_COLON_SEPARATED)) == 0);
397 AssertTrue(_tcscmp(_T("70-B3-D5-B0-20-03-5D-5E"), (const TCHAR*)a.toString(MAC_ADDR_HYPHEN_SEPARATED)) == 0);
398 AssertTrue(_tcscmp(_T("70B3.D5B0.2003.5D5E"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_DOT_SEPARATED)) == 0);
399 AssertTrue(_tcscmp(_T("70B3:D5B0:2003:5D5E"), (const TCHAR*)a.toString(MAC_ADDR_BYTEPAIR_COLON_SEPARATED)) == 0);
400 EndTest();
401
402 StartTest(_T("MacAddress - isMulticast()"));
403 a = MacAddress::parse("01:80:C2:00:00:00");
404 AssertTrue(a.isMulticast());
405 b = MacAddress::parse("09-80-C2-FF-FF-FF");
406 AssertTrue(b.isMulticast());
407 c = MacAddress::parse("482.C6A.1E5.93D");
408 AssertFalse(c.isMulticast());
409 EndTest();
410
411 StartTest(_T("MacAddress - isBroadcast()"));
412 a = MacAddress::parse("FF:FF:FF:FF:FF:FF");
413 AssertTrue(a.isBroadcast());
414 b = MacAddress::parse("FF-2C-6A-1E-59-3D");
415 AssertFalse(b.isBroadcast());
416 c = MacAddress::parse("FFF.FC2.FFF.FFF");
417 AssertFalse(c.isBroadcast());
418 EndTest();
419
420 StartTest(_T("MacAddress - equals()"));
421 a = MacAddress::parse("09-80-C2-FF-FF-FF");
422 b = MacAddress::parse("48:2C:6A:1E:59:3D");
423 c = MacAddress::parse("098.0C2.FFF.FFF");
424 d = MacAddress::parse("70B3D5B020035F11");
425 e = MacAddress::parse("70B3:D5B0:2003:5F11");
426 f = MacAddress::parse("70B3.D5B0.2003.5D5E");
427 AssertFalse(a.equals(b));
428 AssertFalse(b.equals(c));
429 AssertFalse(e.equals(f));
430 AssertFalse(d.equals(b));
431 AssertTrue(c.equals(a));
432 AssertTrue(d.equals(e));
433 EndTest();
434 }
435
436 /**
437 * Test InetAddress class
438 */
439 static void TestInetAddress()
440 {
441 InetAddress a, b, c;
442
443 StartTest(_T("InetAddress - isLoopback() - IPv4"));
444 a = InetAddress::parse("127.0.0.1");
445 AssertTrue(a.isLoopback());
446 a = InetAddress::parse("192.168.1.1");
447 AssertFalse(a.isLoopback());
448 EndTest();
449
450 StartTest(_T("InetAddress - isLoopback() - IPv6"));
451 a = InetAddress::parse("::1");
452 AssertTrue(a.isLoopback());
453 a = InetAddress::parse("2000:1234::1");
454 AssertFalse(a.isLoopback());
455 EndTest();
456
457 StartTest(_T("InetAddress - isSubnetBroadcast() - IPv4"));
458 a = InetAddress::parse("192.168.0.255");
459 AssertTrue(a.isSubnetBroadcast(24));
460 AssertFalse(a.isSubnetBroadcast(23));
461 EndTest();
462
463 StartTest(_T("InetAddress - isSubnetBroadcast() - IPv6"));
464 a = InetAddress::parse("fe80::ffff:ffff:ffff:ffff");
465 AssertFalse(a.isSubnetBroadcast(64));
466 AssertFalse(a.isSubnetBroadcast(63));
467 EndTest();
468
469 StartTest(_T("InetAddress - isLinkLocal() - IPv4"));
470 a = InetAddress::parse("169.254.17.198");
471 AssertTrue(a.isLinkLocal());
472 a = InetAddress::parse("192.168.1.1");
473 AssertFalse(a.isLinkLocal());
474 EndTest();
475
476 StartTest(_T("InetAddress - isLinkLocal() - IPv6"));
477 a = InetAddress::parse("fe80::1");
478 AssertTrue(a.isLinkLocal());
479 a = InetAddress::parse("2000:1234::1");
480 AssertFalse(a.isLinkLocal());
481 EndTest();
482
483 StartTest(_T("InetAddress - sameSubnet() - IPv4"));
484 a = InetAddress::parse("192.168.1.43");
485 a.setMaskBits(23);
486 b = InetAddress::parse("192.168.0.180");
487 b.setMaskBits(23);
488 c = InetAddress::parse("192.168.2.22");
489 c.setMaskBits(23);
490 AssertTrue(a.sameSubnet(b));
491 AssertFalse(a.sameSubnet(c));
492 EndTest();
493
494 StartTest(_T("InetAddress - sameSubnet() - IPv6"));
495 a = InetAddress::parse("2000:1234:1000:1000::1");
496 a.setMaskBits(62);
497 b = InetAddress::parse("2000:1234:1000:1001::cdef:1");
498 b.setMaskBits(62);
499 c = InetAddress::parse("2000:1234:1000:1007::1");
500 c.setMaskBits(62);
501 AssertTrue(a.sameSubnet(b));
502 AssertFalse(a.sameSubnet(c));
503 EndTest();
504
505 StartTest(_T("InetAddress - buildHashKey() - IPv4"));
506 a = InetAddress::parse("10.3.1.91");
507 BYTE key[18];
508 a.buildHashKey(key);
509 #if WORDS_BIGENDIAN
510 static BYTE keyIPv4[] = { 0x06, AF_INET, 0x0A, 0x03, 0x01, 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
511 AssertTrue(memcmp(key, keyIPv4, 18) == 0);
512 #else
513 static BYTE keyIPv4[] = { 0x06, AF_INET, 0x5B, 0x01, 0x03, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
514 AssertTrue(memcmp(key, keyIPv4, 18) == 0);
515 #endif
516 EndTest();
517
518 StartTest(_T("InetAddress - buildHashKey() - IPv6"));
519 a = InetAddress::parse("fe80:1234::6e88:14ff:fec4:b8f8");
520 a.buildHashKey(key);
521 static BYTE keyIPv6[] = { 0x12, AF_INET6, 0xFE, 0x80, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x6E, 0x88, 0x14, 0xFF, 0xFE, 0xC4, 0xB8, 0xF8 };
522 AssertTrue(memcmp(key, keyIPv6, 18) == 0);
523 EndTest();
524 }
525
526 /**
527 * Test itoa/itow
528 */
529 static void TestItoa()
530 {
531 char buffer[64];
532 WCHAR wbuffer[64];
533
534 StartTest(_T("itoa"));
535 AssertTrue(!strcmp(_itoa(127, buffer, 10), "127"));
536 AssertTrue(!strcmp(_itoa(0, buffer, 10), "0"));
537 AssertTrue(!strcmp(_itoa(-3, buffer, 10), "-3"));
538 AssertTrue(!strcmp(_itoa(0555, buffer, 8), "555"));
539 AssertTrue(!strcmp(_itoa(0xFA48, buffer, 16), "fa48"));
540 EndTest();
541
542 StartTest(_T("itow"));
543 AssertTrue(!wcscmp(_itow(127, wbuffer, 10), L"127"));
544 AssertTrue(!wcscmp(_itow(0, wbuffer, 10), L"0"));
545 AssertTrue(!wcscmp(_itow(-3, wbuffer, 10), L"-3"));
546 AssertTrue(!wcscmp(_itow(0555, wbuffer, 8), L"555"));
547 AssertTrue(!wcscmp(_itow(0xFA48, wbuffer, 16), L"fa48"));
548 EndTest();
549 }
550
551 /**
552 * Test queue
553 */
554 static void TestQueue()
555 {
556 Queue *q = new Queue(16, 16);
557
558 StartTest(_T("Queue: put/get"));
559 for(int i = 0; i < 40; i++)
560 q->put(CAST_TO_POINTER(i + 1, void *));
561 AssertEquals(q->size(), 40);
562 AssertEquals(q->allocated(), 48);
563 for(int i = 0; i < 40; i++)
564 {
565 void *p = q->get();
566 AssertNotNull(p);
567 AssertEquals(CAST_FROM_POINTER(p, int), i + 1);
568 }
569 EndTest();
570
571 StartTest(_T("Queue: shrink"));
572 for(int i = 0; i < 60; i++)
573 q->put(CAST_TO_POINTER(i + 1, void *));
574 AssertEquals(q->size(), 60);
575 AssertEquals(q->allocated(), 64);
576 for(int i = 0; i < 55; i++)
577 {
578 void *p = q->get();
579 AssertNotNull(p);
580 AssertEquals(CAST_FROM_POINTER(p, int), i + 1);
581 }
582 AssertEquals(q->size(), 5);
583 AssertEquals(q->allocated(), 16);
584 EndTest();
585
586 delete q;
587 }
588
589 /**
590 * Key for hash map
591 */
592 typedef char HASH_KEY[6];
593
594 /**
595 * Test hash map
596 */
597 static void TestHashMap()
598 {
599 StartTest(_T("HashMap: create"));
600 HashMap<HASH_KEY, String> *hashMap = new HashMap<HASH_KEY, String>(true);
601 AssertEquals(hashMap->size(), 0);
602 EndTest();
603
604 HASH_KEY k1 = { '1', '2', '3', '4', '5', '6' };
605 HASH_KEY k2 = { '0', '0', 'a', 'b', 'c', 'd' };
606 HASH_KEY k3 = { '0', '0', '3', 'X', '1', '1' };
607
608 StartTest(_T("HashMap: set/get"));
609
610 hashMap->set(k1, new String(_T("String 1")));
611 hashMap->set(k2, new String(_T("String 2")));
612 hashMap->set(k3, new String(_T("String 3")));
613
614 String *s = hashMap->get(k1);
615 AssertNotNull(s);
616 AssertTrue(!_tcscmp(s->getBuffer(), _T("String 1")));
617
618 s = hashMap->get(k2);
619 AssertNotNull(s);
620 AssertTrue(!_tcscmp(s->getBuffer(), _T("String 2")));
621
622 s = hashMap->get(k3);
623 AssertNotNull(s);
624 AssertTrue(!_tcscmp(s->getBuffer(), _T("String 3")));
625
626 EndTest();
627
628 StartTest(_T("HashMap: replace"));
629 hashMap->set(k2, new String(_T("REPLACE")));
630 s = hashMap->get(k2);
631 AssertNotNull(s);
632 AssertTrue(!_tcscmp(s->getBuffer(), _T("REPLACE")));
633 EndTest();
634
635 StartTest(_T("HashMap: iterator"));
636 Iterator<String> *it = hashMap->iterator();
637 AssertTrue(it->hasNext());
638 s = it->next();
639 AssertNotNull(s);
640 AssertNotNull(it->next());
641 AssertNotNull(it->next());
642 AssertFalse(it->hasNext());
643 AssertNull(it->next());
644 AssertFalse(it->hasNext());
645 delete it;
646 EndTest();
647
648 StartTest(_T("HashMap: iterator remove"));
649 it = hashMap->iterator();
650 AssertTrue(it->hasNext());
651 AssertNotNull(it->next());
652 s = it->next();
653 AssertNotNull(s);
654 it->remove();
655 AssertTrue(it->hasNext());
656 AssertNotNull(it->next());
657 AssertFalse(it->hasNext());
658 AssertNull(it->next());
659 delete it;
660 AssertNotNull(hashMap->get(k1));
661 AssertNull(hashMap->get(k2));
662 AssertNotNull(hashMap->get(k3));
663 EndTest();
664
665 StartTest(_T("HashMap: remove"));
666 hashMap->remove(k3);
667 AssertNull(hashMap->get(k3));
668 EndTest();
669
670 StartTest(_T("HashMap: clear"));
671 hashMap->clear();
672 AssertEquals(hashMap->size(), 0);
673 it = hashMap->iterator();
674 AssertFalse(it->hasNext());
675 AssertNull(it->next());
676 delete it;
677 EndTest();
678
679 delete hashMap;
680 }
681
682 /**
683 * Test array
684 */
685 static void TestObjectArray()
686 {
687 StartTest(_T("ObjectArray: create"));
688 ObjectArray<String> *array = new ObjectArray<String>(16, 16, true);
689 AssertEquals(array->size(), 0);
690 EndTest();
691
692 StartTest(_T("ObjectArray: add/get"));
693 array->add(new String(_T("value 1")));
694 array->add(new String(_T("value 2")));
695 array->add(new String(_T("value 3")));
696 array->add(new String(_T("value 4")));
697 AssertEquals(array->size(), 4);
698 AssertNull(array->get(4));
699 AssertNotNull(array->get(1));
700 AssertTrue(!_tcscmp(array->get(1)->getBuffer(), _T("value 2")));
701 EndTest();
702
703 StartTest(_T("ObjectArray: replace"));
704 array->replace(0, new String(_T("replace")));
705 AssertEquals(array->size(), 4);
706 AssertTrue(!_tcscmp(array->get(0)->getBuffer(), _T("replace")));
707 EndTest();
708
709 StartTest(_T("ObjectArray: remove"));
710 array->remove(0);
711 AssertEquals(array->size(), 3);
712 AssertTrue(!_tcscmp(array->get(0)->getBuffer(), _T("value 2")));
713 EndTest();
714
715 StartTest(_T("ObjectArray: iterator"));
716 Iterator<String> *it = array->iterator();
717 AssertTrue(it->hasNext());
718 String *s = it->next();
719 AssertTrue(!_tcscmp(s->getBuffer(), _T("value 2")));
720 s = it->next();
721 AssertTrue(!_tcscmp(s->getBuffer(), _T("value 3")));
722 s = it->next();
723 AssertTrue(!_tcscmp(s->getBuffer(), _T("value 4")));
724 s = it->next();
725 AssertNull(s);
726 delete it;
727 EndTest();
728
729 delete array;
730 }
731
732 /**
733 * Table tests
734 */
735 static void TestTable()
736 {
737 StartTest(_T("Table: create"));
738 Table *table = new Table();
739 AssertEquals(table->getNumRows(), 0);
740 EndTest();
741
742 StartTest(_T("Table: set on empty table"));
743 table->set(0, 1.0);
744 table->set(1, _T("test"));
745 table->setPreallocated(1, _tcsdup(_T("test")));
746 AssertEquals(table->getNumRows(), 0);
747 EndTest();
748
749 StartTest(_T("Table: add row"));
750 table->addRow();
751 AssertEquals(table->getNumRows(), 1);
752 AssertEquals(table->getNumColumns(), 0);
753 EndTest();
754
755 StartTest(_T("Table: set on empty row"));
756 table->set(0, _T("test"));
757 table->setPreallocated(1, _tcsdup(_T("test")));
758 AssertEquals(table->getNumRows(), 1);
759 AssertEquals(table->getNumColumns(), 0);
760 EndTest();
761
762 table->addColumn(_T("NAME"));
763 table->addColumn(_T("VALUE"));
764 table->addColumn(_T("DATA1"));
765 table->addColumn(_T("DATA2"));
766 table->addColumn(_T("DATA3"));
767 table->addColumn(_T("DATA4"));
768 for(int i = 0; i < 50; i++)
769 {
770 table->addRow();
771 TCHAR b[64];
772 _sntprintf(b, 64, _T("Process #%d"), i);
773 table->set(0, b);
774 table->set(1, i);
775 table->set(2, i * 100);
776 table->set(3, i * 100001);
777 table->set(4, _T("/some/long/path/on/file/system"));
778 table->set(5, _T("constant"));
779 }
780
781 StartTest(_T("Table: pack"));
782 INT64 start = GetCurrentTimeMs();
783 char *packedTable = table->createPackedXML();
784 AssertNotNull(packedTable);
785 EndTest(GetCurrentTimeMs() - start);
786
787 StartTest(_T("Table: unpack"));
788 start = GetCurrentTimeMs();
789 Table *table2 = Table::createFromPackedXML(packedTable);
790 free(packedTable);
791 AssertNotNull(table2);
792 AssertEquals(table2->getNumColumns(), table->getNumColumns());
793 AssertEquals(table2->getNumRows(), table->getNumRows());
794 AssertEquals(table2->getAsInt(10, 1), table->getAsInt(10, 1));
795 EndTest(GetCurrentTimeMs() - start);
796
797 delete table;
798 delete table2;
799 }
800
801 /**
802 * Test byte swap
803 */
804 static void TestByteSwap()
805 {
806 StartTest(_T("bswap_16"));
807 AssertEquals(bswap_16(0xABCD), 0xCDAB);
808 EndTest();
809
810 StartTest(_T("bswap_32"));
811 AssertEquals(bswap_32(0x0102ABCD), 0xCDAB0201);
812 EndTest();
813
814 StartTest(_T("bswap_64"));
815 AssertEquals(bswap_64(_ULL(0x01020304A1A2A3A4)), _ULL(0xA4A3A2A104030201));
816 EndTest();
817
818 StartTest(_T("bswap_array_16"));
819 UINT16 s16[] = { 0xABCD, 0x1020, 0x2233, 0x0102 };
820 UINT16 d16[] = { 0xCDAB, 0x2010, 0x3322, 0x0201 };
821 bswap_array_16(s16, 4);
822 AssertTrue(!memcmp(s16, d16, 8));
823 EndTest();
824
825 StartTest(_T("bswap_array_16 (string)"));
826 UINT16 ss16[] = { 0xABCD, 0x1020, 0x2233, 0x0102, 0 };
827 UINT16 sd16[] = { 0xCDAB, 0x2010, 0x3322, 0x0201, 0 };
828 bswap_array_16(ss16, -1);
829 AssertTrue(!memcmp(ss16, sd16, 10));
830 EndTest();
831
832 StartTest(_T("bswap_array_32"));
833 UINT32 s32[] = { 0xABCDEF01, 0x10203040, 0x22334455, 0x01020304 };
834 UINT32 d32[] = { 0x01EFCDAB, 0x40302010, 0x55443322, 0x04030201 };
835 bswap_array_32(s32, 4);
836 AssertTrue(!memcmp(s32, d32, 16));
837 EndTest();
838
839 StartTest(_T("bswap_array_32 (string)"));
840 UINT32 ss32[] = { 0xABCDEF01, 0x10203040, 0x22334455, 0x01020304, 0 };
841 UINT32 sd32[] = { 0x01EFCDAB, 0x40302010, 0x55443322, 0x04030201, 0 };
842 bswap_array_32(ss32, -1);
843 AssertTrue(!memcmp(ss32, sd32, 20));
844 EndTest();
845 }
846
847 /**
848 * Test diff
849 */
850 static void TestDiff()
851 {
852 static const TCHAR *diffLeft = _T("line 1\nline 2\nline3\nFIXED TEXT\nalpha\n");
853 static const TCHAR *diffRight = _T("line 1\nline 3\nline 4\nFIXED TEXT\nbeta\n");
854 static const TCHAR *expectedDiff = _T("-line 2\n-line3\n+line 3\n+line 4\n-alpha\n+beta\n");
855
856 StartTest(_T("GenerateLineDiff (multiple lines)"));
857 String diff = GenerateLineDiff(diffLeft, diffRight);
858 AssertTrue(diff.equals(expectedDiff));
859 EndTest();
860
861 StartTest(_T("GenerateLineDiff (single line)"));
862 diff = GenerateLineDiff(_T("prefix-alpha"), _T("prefix-beta"));
863 AssertTrue(diff.equals(_T("-prefix-alpha\n+prefix-beta\n")));
864 EndTest();
865 }
866
867 /**
868 * Test ring buffer
869 */
870 static void TestRingBuffer()
871 {
872 RingBuffer rb(32, 32);
873 BYTE buffer[256];
874
875 StartTest(_T("RingBuffer: write #1"));
876 rb.write((const BYTE *)"short data", 10);
877 AssertEquals(rb.size(), 10);
878 EndTest();
879
880 StartTest(_T("RingBuffer: read #1"));
881 size_t bytes = rb.read(buffer, 256);
882 AssertEquals(bytes, 10);
883 AssertTrue(!memcmp(buffer, "short data", 10));
884 AssertEquals(rb.size(), 0);
885 EndTest();
886
887 StartTest(_T("RingBuffer: write #2"));
888 rb.write((const BYTE *)"short data", 10);
889 AssertEquals(rb.size(), 10);
890 EndTest();
891
892 StartTest(_T("RingBuffer: read #2"));
893 memset(buffer, 0, 256);
894 bytes = rb.read(buffer, 4);
895 AssertEquals(bytes, 4);
896 AssertTrue(!memcmp(buffer, "shor", 4));
897 AssertEquals(rb.size(), 6);
898 EndTest();
899
900 StartTest(_T("RingBuffer: write #3"));
901 rb.write((const BYTE *)"long data: 123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.", 111);
902 AssertEquals(rb.size(), 117);
903 EndTest();
904
905 StartTest(_T("RingBuffer: read #3"));
906 memset(buffer, 0, 256);
907 bytes = rb.read(buffer, 17);
908 AssertEquals(bytes, 17);
909 AssertTrue(!memcmp(buffer, "t datalong data: ", 17));
910 AssertEquals(rb.size(), 100);
911 EndTest();
912
913 StartTest(_T("RingBuffer: write #4"));
914 rb.write((const BYTE *)"short data", 10);
915 AssertEquals(rb.size(), 110);
916 EndTest();
917
918 StartTest(_T("RingBuffer: read #4"));
919 memset(buffer, 0, 256);
920 bytes = rb.read(buffer, 108);
921 AssertEquals(bytes, 108);
922 AssertTrue(!memcmp(buffer, "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.short da", 108));
923 AssertEquals(rb.size(), 2);
924 EndTest();
925
926 StartTest(_T("RingBuffer: write #5"));
927 rb.write((const BYTE *)"test", 4);
928 AssertEquals(rb.size(), 6);
929 EndTest();
930
931 StartTest(_T("RingBuffer: read #5"));
932 memset(buffer, 0, 256);
933 bytes = rb.read(buffer, 256);
934 AssertEquals(bytes, 6);
935 AssertTrue(!memcmp(buffer, "tatest", 6));
936 AssertEquals(rb.size(), 0);
937 EndTest();
938
939 StartTest(_T("RingBuffer: random read/write"));
940 AssertTrue(rb.isEmpty());
941 for(int i = 0; i < 10000; i++)
942 {
943 int writes = rand() % 10;
944 int reads = rand() % 10;
945 for(int j = 0; j < writes; j++)
946 {
947 rb.write((const BYTE *)"---------/---------/---------/---------/---------/---------/---------/---------/---------/---------/", 100);
948 }
949 for(int j = 0; j < reads; j++)
950 {
951 memset(buffer, 0, 256);
952 bytes = rb.read(buffer, 100);
953 AssertTrue(((bytes == 100) && !memcmp(buffer, "---------/---------/---------/---------/---------/---------/---------/---------/---------/---------/", 100)) || (bytes == 0));
954 }
955 }
956 while(!rb.isEmpty())
957 {
958 memset(buffer, 0, 256);
959 bytes = rb.read(buffer, 100);
960 AssertTrue((bytes == 100) && !memcmp(buffer, "---------/---------/---------/---------/---------/---------/---------/---------/---------/---------/", 100));
961 }
962 EndTest();
963 }
964
965 /**
966 * main()
967 */
968 int main(int argc, char *argv[])
969 {
970 #ifdef _WIN32
971 WSADATA wsaData;
972 WSAStartup(MAKEWORD(2, 2), &wsaData);
973 #endif
974
975 TestString();
976 TestStringConversion();
977 TestStringMap();
978 TestStringSet();
979 TestMessageClass();
980 TestMsgWaitQueue();
981 TestMacAddress();
982 TestInetAddress();
983 TestItoa();
984 TestQueue();
985 TestHashMap();
986 TestObjectArray();
987 TestTable();
988 TestMutexWrapper();
989 TestRWLockWrapper();
990 TestConditionWrapper();
991 TestByteSwap();
992 TestDiff();
993 TestRingBuffer();
994
995 MsgWaitQueue::shutdown();
996 return 0;
997 }