246cdaa02f5d441a2139a7f41ebdd19e7c86a456
[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;
323
324 StartTest(_T("MacAddress - isMulticast()"));
325 a = MacAddress::parse("01:80:C2:00:00:00");
326 AssertTrue(a.isMulticast());
327 b = MacAddress::parse("09-80-C2-FF-FF-FF");
328 AssertTrue(b.isMulticast());
329 c = MacAddress::parse("48.2C.6A.1E.59.3D");
330 AssertFalse(c.isMulticast());
331 EndTest();
332
333 StartTest(_T("MacAddress - isBroadcast()"));
334 a = MacAddress::parse("FF:FF:FF:FF:FF:FF");
335 AssertTrue(a.isBroadcast());
336 b = MacAddress::parse("FF-2C-6A-1E-59-3D");
337 AssertFalse(b.isBroadcast());
338 c = MacAddress::parse("FF.FF.C2.FF.FF.FF");
339 AssertFalse(c.isBroadcast());
340 EndTest();
341
342 StartTest(_T("MacAddress - equals()"));
343 a = MacAddress::parse("09-80-C2-FF-FF-FF");
344 b = MacAddress::parse("48:2C:6A:1E:59:3D");
345 c = MacAddress::parse("09.80.C2.FF.FF.FF");
346 AssertFalse(a.equals(b));
347 AssertFalse(b.equals(c));
348 AssertTrue(c.equals(a));
349 EndTest();
350 }
351
352 /**
353 * Test InetAddress class
354 */
355 static void TestInetAddress()
356 {
357 InetAddress a, b, c;
358
359 StartTest(_T("InetAddress - isLoopback() - IPv4"));
360 a = InetAddress::parse("127.0.0.1");
361 AssertTrue(a.isLoopback());
362 a = InetAddress::parse("192.168.1.1");
363 AssertFalse(a.isLoopback());
364 EndTest();
365
366 StartTest(_T("InetAddress - isLoopback() - IPv6"));
367 a = InetAddress::parse("::1");
368 AssertTrue(a.isLoopback());
369 a = InetAddress::parse("2000:1234::1");
370 AssertFalse(a.isLoopback());
371 EndTest();
372
373 StartTest(_T("InetAddress - isSubnetBroadcast() - IPv4"));
374 a = InetAddress::parse("192.168.0.255");
375 AssertTrue(a.isSubnetBroadcast(24));
376 AssertFalse(a.isSubnetBroadcast(23));
377 EndTest();
378
379 StartTest(_T("InetAddress - isSubnetBroadcast() - IPv6"));
380 a = InetAddress::parse("fe80::ffff:ffff:ffff:ffff");
381 AssertFalse(a.isSubnetBroadcast(64));
382 AssertFalse(a.isSubnetBroadcast(63));
383 EndTest();
384
385 StartTest(_T("InetAddress - isLinkLocal() - IPv4"));
386 a = InetAddress::parse("169.254.17.198");
387 AssertTrue(a.isLinkLocal());
388 a = InetAddress::parse("192.168.1.1");
389 AssertFalse(a.isLinkLocal());
390 EndTest();
391
392 StartTest(_T("InetAddress - isLinkLocal() - IPv6"));
393 a = InetAddress::parse("fe80::1");
394 AssertTrue(a.isLinkLocal());
395 a = InetAddress::parse("2000:1234::1");
396 AssertFalse(a.isLinkLocal());
397 EndTest();
398
399 StartTest(_T("InetAddress - sameSubnet() - IPv4"));
400 a = InetAddress::parse("192.168.1.43");
401 a.setMaskBits(23);
402 b = InetAddress::parse("192.168.0.180");
403 b.setMaskBits(23);
404 c = InetAddress::parse("192.168.2.22");
405 c.setMaskBits(23);
406 AssertTrue(a.sameSubnet(b));
407 AssertFalse(a.sameSubnet(c));
408 EndTest();
409
410 StartTest(_T("InetAddress - sameSubnet() - IPv6"));
411 a = InetAddress::parse("2000:1234:1000:1000::1");
412 a.setMaskBits(62);
413 b = InetAddress::parse("2000:1234:1000:1001::cdef:1");
414 b.setMaskBits(62);
415 c = InetAddress::parse("2000:1234:1000:1007::1");
416 c.setMaskBits(62);
417 AssertTrue(a.sameSubnet(b));
418 AssertFalse(a.sameSubnet(c));
419 EndTest();
420
421 StartTest(_T("InetAddress - buildHashKey() - IPv4"));
422 a = InetAddress::parse("10.3.1.91");
423 BYTE key[18];
424 a.buildHashKey(key);
425 #if WORDS_BIGENDIAN
426 static BYTE keyIPv4[] = { 0x06, AF_INET, 0x0A, 0x03, 0x01, 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
427 AssertTrue(memcmp(key, keyIPv4, 18) == 0);
428 #else
429 static BYTE keyIPv4[] = { 0x06, AF_INET, 0x5B, 0x01, 0x03, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
430 AssertTrue(memcmp(key, keyIPv4, 18) == 0);
431 #endif
432 EndTest();
433
434 StartTest(_T("InetAddress - buildHashKey() - IPv6"));
435 a = InetAddress::parse("fe80:1234::6e88:14ff:fec4:b8f8");
436 a.buildHashKey(key);
437 static BYTE keyIPv6[] = { 0x12, AF_INET6, 0xFE, 0x80, 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x6E, 0x88, 0x14, 0xFF, 0xFE, 0xC4, 0xB8, 0xF8 };
438 AssertTrue(memcmp(key, keyIPv6, 18) == 0);
439 EndTest();
440 }
441
442 /**
443 * Test itoa/itow
444 */
445 static void TestItoa()
446 {
447 char buffer[64];
448 WCHAR wbuffer[64];
449
450 StartTest(_T("itoa"));
451 AssertTrue(!strcmp(_itoa(127, buffer, 10), "127"));
452 AssertTrue(!strcmp(_itoa(0, buffer, 10), "0"));
453 AssertTrue(!strcmp(_itoa(-3, buffer, 10), "-3"));
454 AssertTrue(!strcmp(_itoa(0555, buffer, 8), "555"));
455 AssertTrue(!strcmp(_itoa(0xFA48, buffer, 16), "fa48"));
456 EndTest();
457
458 StartTest(_T("itow"));
459 AssertTrue(!wcscmp(_itow(127, wbuffer, 10), L"127"));
460 AssertTrue(!wcscmp(_itow(0, wbuffer, 10), L"0"));
461 AssertTrue(!wcscmp(_itow(-3, wbuffer, 10), L"-3"));
462 AssertTrue(!wcscmp(_itow(0555, wbuffer, 8), L"555"));
463 AssertTrue(!wcscmp(_itow(0xFA48, wbuffer, 16), L"fa48"));
464 EndTest();
465 }
466
467 /**
468 * Test queue
469 */
470 static void TestQueue()
471 {
472 Queue *q = new Queue(16, 16);
473
474 StartTest(_T("Queue: put/get"));
475 for(int i = 0; i < 40; i++)
476 q->put(CAST_TO_POINTER(i + 1, void *));
477 AssertEquals(q->size(), 40);
478 AssertEquals(q->allocated(), 48);
479 for(int i = 0; i < 40; i++)
480 {
481 void *p = q->get();
482 AssertNotNull(p);
483 AssertEquals(CAST_FROM_POINTER(p, int), i + 1);
484 }
485 EndTest();
486
487 StartTest(_T("Queue: shrink"));
488 for(int i = 0; i < 60; i++)
489 q->put(CAST_TO_POINTER(i + 1, void *));
490 AssertEquals(q->size(), 60);
491 AssertEquals(q->allocated(), 64);
492 for(int i = 0; i < 55; i++)
493 {
494 void *p = q->get();
495 AssertNotNull(p);
496 AssertEquals(CAST_FROM_POINTER(p, int), i + 1);
497 }
498 AssertEquals(q->size(), 5);
499 AssertEquals(q->allocated(), 16);
500 EndTest();
501
502 delete q;
503 }
504
505 /**
506 * Key for hash map
507 */
508 typedef char HASH_KEY[6];
509
510 /**
511 * Test hash map
512 */
513 static void TestHashMap()
514 {
515 StartTest(_T("HashMap: create"));
516 HashMap<HASH_KEY, String> *hashMap = new HashMap<HASH_KEY, String>(true);
517 AssertEquals(hashMap->size(), 0);
518 EndTest();
519
520 HASH_KEY k1 = { '1', '2', '3', '4', '5', '6' };
521 HASH_KEY k2 = { '0', '0', 'a', 'b', 'c', 'd' };
522 HASH_KEY k3 = { '0', '0', '3', 'X', '1', '1' };
523
524 StartTest(_T("HashMap: set/get"));
525
526 hashMap->set(k1, new String(_T("String 1")));
527 hashMap->set(k2, new String(_T("String 2")));
528 hashMap->set(k3, new String(_T("String 3")));
529
530 String *s = hashMap->get(k1);
531 AssertNotNull(s);
532 AssertTrue(!_tcscmp(s->getBuffer(), _T("String 1")));
533
534 s = hashMap->get(k2);
535 AssertNotNull(s);
536 AssertTrue(!_tcscmp(s->getBuffer(), _T("String 2")));
537
538 s = hashMap->get(k3);
539 AssertNotNull(s);
540 AssertTrue(!_tcscmp(s->getBuffer(), _T("String 3")));
541
542 EndTest();
543
544 StartTest(_T("HashMap: replace"));
545 hashMap->set(k2, new String(_T("REPLACE")));
546 s = hashMap->get(k2);
547 AssertNotNull(s);
548 AssertTrue(!_tcscmp(s->getBuffer(), _T("REPLACE")));
549 EndTest();
550
551 StartTest(_T("HashMap: iterator"));
552 Iterator<String> *it = hashMap->iterator();
553 AssertTrue(it->hasNext());
554 s = it->next();
555 AssertNotNull(s);
556 AssertNotNull(it->next());
557 AssertNotNull(it->next());
558 AssertFalse(it->hasNext());
559 AssertNull(it->next());
560 AssertFalse(it->hasNext());
561 delete it;
562 EndTest();
563
564 StartTest(_T("HashMap: iterator remove"));
565 it = hashMap->iterator();
566 AssertTrue(it->hasNext());
567 AssertNotNull(it->next());
568 s = it->next();
569 AssertNotNull(s);
570 it->remove();
571 AssertTrue(it->hasNext());
572 AssertNotNull(it->next());
573 AssertFalse(it->hasNext());
574 AssertNull(it->next());
575 delete it;
576 AssertNotNull(hashMap->get(k1));
577 AssertNull(hashMap->get(k2));
578 AssertNotNull(hashMap->get(k3));
579 EndTest();
580
581 StartTest(_T("HashMap: remove"));
582 hashMap->remove(k3);
583 AssertNull(hashMap->get(k3));
584 EndTest();
585
586 StartTest(_T("HashMap: clear"));
587 hashMap->clear();
588 AssertEquals(hashMap->size(), 0);
589 it = hashMap->iterator();
590 AssertFalse(it->hasNext());
591 AssertNull(it->next());
592 delete it;
593 EndTest();
594
595 delete hashMap;
596 }
597
598 /**
599 * Test array
600 */
601 static void TestObjectArray()
602 {
603 StartTest(_T("ObjectArray: create"));
604 ObjectArray<String> *array = new ObjectArray<String>(16, 16, true);
605 AssertEquals(array->size(), 0);
606 EndTest();
607
608 StartTest(_T("ObjectArray: add/get"));
609 array->add(new String(_T("value 1")));
610 array->add(new String(_T("value 2")));
611 array->add(new String(_T("value 3")));
612 array->add(new String(_T("value 4")));
613 AssertEquals(array->size(), 4);
614 AssertNull(array->get(4));
615 AssertNotNull(array->get(1));
616 AssertTrue(!_tcscmp(array->get(1)->getBuffer(), _T("value 2")));
617 EndTest();
618
619 StartTest(_T("ObjectArray: replace"));
620 array->replace(0, new String(_T("replace")));
621 AssertEquals(array->size(), 4);
622 AssertTrue(!_tcscmp(array->get(0)->getBuffer(), _T("replace")));
623 EndTest();
624
625 StartTest(_T("ObjectArray: remove"));
626 array->remove(0);
627 AssertEquals(array->size(), 3);
628 AssertTrue(!_tcscmp(array->get(0)->getBuffer(), _T("value 2")));
629 EndTest();
630
631 StartTest(_T("ObjectArray: iterator"));
632 Iterator<String> *it = array->iterator();
633 AssertTrue(it->hasNext());
634 String *s = it->next();
635 AssertTrue(!_tcscmp(s->getBuffer(), _T("value 2")));
636 s = it->next();
637 AssertTrue(!_tcscmp(s->getBuffer(), _T("value 3")));
638 s = it->next();
639 AssertTrue(!_tcscmp(s->getBuffer(), _T("value 4")));
640 s = it->next();
641 AssertNull(s);
642 delete it;
643 EndTest();
644
645 delete array;
646 }
647
648 /**
649 * Table tests
650 */
651 static void TestTable()
652 {
653 StartTest(_T("Table: create"));
654 Table *table = new Table();
655 AssertEquals(table->getNumRows(), 0);
656 EndTest();
657
658 StartTest(_T("Table: set on empty table"));
659 table->set(0, 1.0);
660 table->set(1, _T("test"));
661 table->setPreallocated(1, _tcsdup(_T("test")));
662 AssertEquals(table->getNumRows(), 0);
663 EndTest();
664
665 StartTest(_T("Table: add row"));
666 table->addRow();
667 AssertEquals(table->getNumRows(), 1);
668 AssertEquals(table->getNumColumns(), 0);
669 EndTest();
670
671 StartTest(_T("Table: set on empty row"));
672 table->set(0, _T("test"));
673 table->setPreallocated(1, _tcsdup(_T("test")));
674 AssertEquals(table->getNumRows(), 1);
675 AssertEquals(table->getNumColumns(), 0);
676 EndTest();
677
678 table->addColumn(_T("NAME"));
679 table->addColumn(_T("VALUE"));
680 table->addColumn(_T("DATA1"));
681 table->addColumn(_T("DATA2"));
682 table->addColumn(_T("DATA3"));
683 table->addColumn(_T("DATA4"));
684 for(int i = 0; i < 50; i++)
685 {
686 table->addRow();
687 TCHAR b[64];
688 _sntprintf(b, 64, _T("Process #%d"), i);
689 table->set(0, b);
690 table->set(1, i);
691 table->set(2, i * 100);
692 table->set(3, i * 100001);
693 table->set(4, _T("/some/long/path/on/file/system"));
694 table->set(5, _T("constant"));
695 }
696
697 StartTest(_T("Table: pack"));
698 INT64 start = GetCurrentTimeMs();
699 char *packedTable = table->createPackedXML();
700 AssertNotNull(packedTable);
701 EndTest(GetCurrentTimeMs() - start);
702
703 StartTest(_T("Table: unpack"));
704 start = GetCurrentTimeMs();
705 Table *table2 = Table::createFromPackedXML(packedTable);
706 free(packedTable);
707 AssertNotNull(table2);
708 AssertEquals(table2->getNumColumns(), table->getNumColumns());
709 AssertEquals(table2->getNumRows(), table->getNumRows());
710 AssertEquals(table2->getAsInt(10, 1), table->getAsInt(10, 1));
711 EndTest(GetCurrentTimeMs() - start);
712
713 delete table;
714 delete table2;
715 }
716
717 /**
718 * Test byte swap
719 */
720 static void TestByteSwap()
721 {
722 StartTest(_T("bswap_16"));
723 AssertEquals(bswap_16(0xABCD), 0xCDAB);
724 EndTest();
725
726 StartTest(_T("bswap_32"));
727 AssertEquals(bswap_32(0x0102ABCD), 0xCDAB0201);
728 EndTest();
729
730 StartTest(_T("bswap_64"));
731 AssertEquals(bswap_64(_ULL(0x01020304A1A2A3A4)), _ULL(0xA4A3A2A104030201));
732 EndTest();
733
734 StartTest(_T("bswap_array_16"));
735 UINT16 s16[] = { 0xABCD, 0x1020, 0x2233, 0x0102 };
736 UINT16 d16[] = { 0xCDAB, 0x2010, 0x3322, 0x0201 };
737 bswap_array_16(s16, 4);
738 AssertTrue(!memcmp(s16, d16, 8));
739 EndTest();
740
741 StartTest(_T("bswap_array_16 (string)"));
742 UINT16 ss16[] = { 0xABCD, 0x1020, 0x2233, 0x0102, 0 };
743 UINT16 sd16[] = { 0xCDAB, 0x2010, 0x3322, 0x0201, 0 };
744 bswap_array_16(ss16, -1);
745 AssertTrue(!memcmp(ss16, sd16, 10));
746 EndTest();
747
748 StartTest(_T("bswap_array_32"));
749 UINT32 s32[] = { 0xABCDEF01, 0x10203040, 0x22334455, 0x01020304 };
750 UINT32 d32[] = { 0x01EFCDAB, 0x40302010, 0x55443322, 0x04030201 };
751 bswap_array_32(s32, 4);
752 AssertTrue(!memcmp(s32, d32, 16));
753 EndTest();
754
755 StartTest(_T("bswap_array_32 (string)"));
756 UINT32 ss32[] = { 0xABCDEF01, 0x10203040, 0x22334455, 0x01020304, 0 };
757 UINT32 sd32[] = { 0x01EFCDAB, 0x40302010, 0x55443322, 0x04030201, 0 };
758 bswap_array_32(ss32, -1);
759 AssertTrue(!memcmp(ss32, sd32, 20));
760 EndTest();
761 }
762
763 /**
764 * Test diff
765 */
766 static void TestDiff()
767 {
768 static const TCHAR *diffLeft = _T("line 1\nline 2\nline3\nFIXED TEXT\nalpha\n");
769 static const TCHAR *diffRight = _T("line 1\nline 3\nline 4\nFIXED TEXT\nbeta\n");
770 static const TCHAR *expectedDiff = _T("-line 2\n-line3\n+line 3\n+line 4\n-alpha\n+beta\n");
771
772 StartTest(_T("GenerateLineDiff (multiple lines)"));
773 String diff = GenerateLineDiff(diffLeft, diffRight);
774 AssertTrue(diff.equals(expectedDiff));
775 EndTest();
776
777 StartTest(_T("GenerateLineDiff (single line)"));
778 diff = GenerateLineDiff(_T("prefix-alpha"), _T("prefix-beta"));
779 AssertTrue(diff.equals(_T("-prefix-alpha\n+prefix-beta\n")));
780 EndTest();
781 }
782
783 /**
784 * Test ring buffer
785 */
786 static void TestRingBuffer()
787 {
788 RingBuffer rb(32, 32);
789 BYTE buffer[256];
790
791 StartTest(_T("RingBuffer: write #1"));
792 rb.write((const BYTE *)"short data", 10);
793 AssertEquals(rb.size(), 10);
794 EndTest();
795
796 StartTest(_T("RingBuffer: read #1"));
797 size_t bytes = rb.read(buffer, 256);
798 AssertEquals(bytes, 10);
799 AssertTrue(!memcmp(buffer, "short data", 10));
800 AssertEquals(rb.size(), 0);
801 EndTest();
802
803 StartTest(_T("RingBuffer: write #2"));
804 rb.write((const BYTE *)"short data", 10);
805 AssertEquals(rb.size(), 10);
806 EndTest();
807
808 StartTest(_T("RingBuffer: read #2"));
809 memset(buffer, 0, 256);
810 bytes = rb.read(buffer, 4);
811 AssertEquals(bytes, 4);
812 AssertTrue(!memcmp(buffer, "shor", 4));
813 AssertEquals(rb.size(), 6);
814 EndTest();
815
816 StartTest(_T("RingBuffer: write #3"));
817 rb.write((const BYTE *)"long data: 123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.", 111);
818 AssertEquals(rb.size(), 117);
819 EndTest();
820
821 StartTest(_T("RingBuffer: read #3"));
822 memset(buffer, 0, 256);
823 bytes = rb.read(buffer, 17);
824 AssertEquals(bytes, 17);
825 AssertTrue(!memcmp(buffer, "t datalong data: ", 17));
826 AssertEquals(rb.size(), 100);
827 EndTest();
828
829 StartTest(_T("RingBuffer: write #4"));
830 rb.write((const BYTE *)"short data", 10);
831 AssertEquals(rb.size(), 110);
832 EndTest();
833
834 StartTest(_T("RingBuffer: read #4"));
835 memset(buffer, 0, 256);
836 bytes = rb.read(buffer, 108);
837 AssertEquals(bytes, 108);
838 AssertTrue(!memcmp(buffer, "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.short da", 108));
839 AssertEquals(rb.size(), 2);
840 EndTest();
841
842 StartTest(_T("RingBuffer: write #5"));
843 rb.write((const BYTE *)"test", 4);
844 AssertEquals(rb.size(), 6);
845 EndTest();
846
847 StartTest(_T("RingBuffer: read #5"));
848 memset(buffer, 0, 256);
849 bytes = rb.read(buffer, 256);
850 AssertEquals(bytes, 6);
851 AssertTrue(!memcmp(buffer, "tatest", 6));
852 AssertEquals(rb.size(), 0);
853 EndTest();
854
855 StartTest(_T("RingBuffer: random read/write"));
856 AssertTrue(rb.isEmpty());
857 for(int i = 0; i < 10000; i++)
858 {
859 int writes = rand() % 10;
860 int reads = rand() % 10;
861 for(int j = 0; j < writes; j++)
862 {
863 rb.write((const BYTE *)"---------/---------/---------/---------/---------/---------/---------/---------/---------/---------/", 100);
864 }
865 for(int j = 0; j < reads; j++)
866 {
867 memset(buffer, 0, 256);
868 bytes = rb.read(buffer, 100);
869 AssertTrue(((bytes == 100) && !memcmp(buffer, "---------/---------/---------/---------/---------/---------/---------/---------/---------/---------/", 100)) || (bytes == 0));
870 }
871 }
872 while(!rb.isEmpty())
873 {
874 memset(buffer, 0, 256);
875 bytes = rb.read(buffer, 100);
876 AssertTrue((bytes == 100) && !memcmp(buffer, "---------/---------/---------/---------/---------/---------/---------/---------/---------/---------/", 100));
877 }
878 EndTest();
879 }
880
881 /**
882 * main()
883 */
884 int main(int argc, char *argv[])
885 {
886 #ifdef _WIN32
887 WSADATA wsaData;
888 WSAStartup(MAKEWORD(2, 2), &wsaData);
889 #endif
890
891 TestString();
892 TestStringConversion();
893 TestStringMap();
894 TestStringSet();
895 TestMessageClass();
896 TestMsgWaitQueue();
897 TestMacAddress();
898 TestInetAddress();
899 TestItoa();
900 TestQueue();
901 TestHashMap();
902 TestObjectArray();
903 TestTable();
904 TestMutexWrapper();
905 TestRWLockWrapper();
906 TestConditionWrapper();
907 TestByteSwap();
908 TestDiff();
909 TestRingBuffer();
910
911 MsgWaitQueue::shutdown();
912 return 0;
913 }