added test for InetAddress::buildHashKey()
[public/netxms.git] / tests / test-libnetxms / test-libnetxms.cpp
CommitLineData
ee2d686f
VK
1#include <nms_common.h>
2#include <nms_util.h>
19dbc8ef 3#include <nxqueue.h>
ee2d686f
VK
4#include <testtools.h>
5
7a70e9de 6void TestMsgWaitQueue();
de4af576 7void TestMessageClass();
7a70e9de 8
ee2d686f
VK
9static char mbText[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
10static WCHAR wcText[] = L"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
11static char mbTextShort[] = "Lorem ipsum";
12static UCS2CHAR ucs2TextShort[] = { 'L', 'o', 'r', 'e', 'm', ' ', 'i', 'p', 's', 'u', 'm', 0 };
13
14/**
15 * Test string conversion
16 */
17static void TestStringConversion()
18{
19 StartTest(_T("ANSI to UCS-2 conversion"));
20 UCS2CHAR ucs2buffer[1024];
21 mb_to_ucs2(mbTextShort, -1, ucs2buffer, 1024);
22 AssertTrue(!memcmp(ucs2buffer, ucs2TextShort, sizeof(UCS2CHAR) * 12));
23 EndTest();
24
25 StartTest(_T("UCS-2 to ANSI conversion"));
26 char mbBuffer[1024];
27 ucs2_to_mb(ucs2TextShort, -1, mbBuffer, 1024);
28 AssertTrue(!strcmp(mbBuffer, mbTextShort));
29 EndTest();
30
31 StartTest(_T("ANSI to UCS-2 conversion performance"));
32 INT64 start = GetCurrentTimeMs();
33 for(int i = 0; i < 10000; i++)
34 {
35 UCS2CHAR buffer[1024];
36 mb_to_ucs2(mbText, -1, buffer, 1024);
37 }
38 EndTest(GetCurrentTimeMs() - start);
39
40 StartTest(_T("UCS-2 to ANSI conversion performance"));
41 mb_to_ucs2(mbText, -1, ucs2buffer, 1024);
42 start = GetCurrentTimeMs();
43 for(int i = 0; i < 10000; i++)
44 {
45 char buffer[1024];
46 ucs2_to_mb(ucs2buffer, -1, buffer, 1024);
47 }
48 EndTest(GetCurrentTimeMs() - start);
49
50#ifdef UNICODE_UCS4
51 StartTest(_T("ANSI to UCS-4 conversion performance"));
52 start = GetCurrentTimeMs();
53 for(int i = 0; i < 10000; i++)
54 {
55 WCHAR buffer[1024];
56 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mbText, -1, buffer, 1024);
57 }
58 EndTest(GetCurrentTimeMs() - start);
59
60 StartTest(_T("UCS-4 to ANSI conversion performance"));
61 start = GetCurrentTimeMs();
62 for(int i = 0; i < 10000; i++)
63 {
64 char buffer[1024];
65 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcText, -1, buffer, 1024, NULL, NULL);
66 }
67 EndTest(GetCurrentTimeMs() - start);
68
69 StartTest(_T("UCS-2 to UCS-4 conversion performance"));
70 mb_to_ucs2(mbText, -1, ucs2buffer, 1024);
71 start = GetCurrentTimeMs();
72 for(int i = 0; i < 10000; i++)
73 {
74 WCHAR buffer[1024];
75 ucs2_to_ucs4(ucs2buffer, -1, buffer, 1024);
76 }
77 EndTest(GetCurrentTimeMs() - start);
78
79 StartTest(_T("UCS-4 to UCS-2 conversion performance"));
80 start = GetCurrentTimeMs();
81 for(int i = 0; i < 10000; i++)
82 {
83 UCS2CHAR buffer[1024];
84 ucs4_to_ucs2(wcText, -1, buffer, 1024);
85 }
86 EndTest(GetCurrentTimeMs() - start);
87#endif
88}
89
90/**
91 * Test string map
92 */
93static void TestStringMap()
94{
95 StringMap *m = new StringMap();
96
97 StartTest(_T("String map - insert"));
98 INT64 start = GetCurrentTimeMs();
99 for(int i = 0; i < 10000; i++)
100 {
101 TCHAR key[64];
102 _sntprintf(key, 64, _T("key-%d"), i);
103 m->set(key, _T("Lorem ipsum dolor sit amet"));
104 }
105 AssertEquals(m->size(), 10000);
400e55c4
VK
106 const TCHAR *v = m->get(_T("key-42"));
107 AssertNotNull(v);
108 AssertTrue(!_tcscmp(v, _T("Lorem ipsum dolor sit amet")));
ee2d686f
VK
109 EndTest(GetCurrentTimeMs() - start);
110
111 StartTest(_T("String map - replace"));
112 start = GetCurrentTimeMs();
113 for(int i = 0; i < 10000; i++)
114 {
115 TCHAR key[64];
116 _sntprintf(key, 64, _T("key-%d"), i);
117 m->set(key, _T("consectetur adipiscing elit"));
118 }
119 AssertEquals(m->size(), 10000);
400e55c4
VK
120 v = m->get(_T("key-42"));
121 AssertNotNull(v);
122 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
ee2d686f
VK
123 EndTest(GetCurrentTimeMs() - start);
124
125 StartTest(_T("String map - get"));
126 start = GetCurrentTimeMs();
400e55c4 127 v = m->get(_T("key-888"));
ee2d686f
VK
128 AssertNotNull(v);
129 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
130 EndTest(GetCurrentTimeMs() - start);
131
132 StartTest(_T("String map - clear"));
133 start = GetCurrentTimeMs();
134 m->clear();
135 AssertEquals(m->size(), 0);
136 EndTest(GetCurrentTimeMs() - start);
4b6e5e27
VK
137
138 delete m;
139}
140
141/**
142 * Test string set
143 */
144static void TestStringSet()
145{
146 StringSet *s = new StringSet();
147
148 StartTest(_T("String set - insert"));
149 INT64 start = GetCurrentTimeMs();
150 for(int i = 0; i < 10000; i++)
151 {
152 TCHAR key[64];
153 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
154 s->add(key);
155 }
156 AssertEquals(s->size(), 10000);
157 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
158 EndTest(GetCurrentTimeMs() - start);
159
160 StartTest(_T("String set - replace"));
161 start = GetCurrentTimeMs();
162 for(int i = 0; i < 10000; i++)
163 {
164 TCHAR key[64];
165 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
166 s->add(key);
167 }
168 AssertEquals(s->size(), 10000);
169 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
170 EndTest(GetCurrentTimeMs() - start);
171
172 StartTest(_T("String set - contains"));
173 start = GetCurrentTimeMs();
174 AssertTrue(s->contains(_T("key-888 lorem ipsum")));
175 EndTest(GetCurrentTimeMs() - start);
176
177 StartTest(_T("String set - clear"));
178 start = GetCurrentTimeMs();
179 s->clear();
180 AssertEquals(s->size(), 0);
181 EndTest(GetCurrentTimeMs() - start);
182
183 delete s;
ee2d686f
VK
184}
185
4e0e77e6
VK
186/**
187 * Test string class
188 */
189static void TestString()
190{
191 String s;
192
193 StartTest(_T("String - append"));
194 for(int i = 0; i < 256; i++)
195 s.append(_T("ABC "));
196 AssertEquals(s.length(), 1024);
197 AssertTrue(!_tcsncmp(s.getBuffer(), _T("ABC ABC ABC ABC "), 16));
198 EndTest();
199
200 StartTest(_T("String - assign #1"));
201 s = _T("alpha");
202 AssertEquals(s.length(), 5);
203 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha")));
204 EndTest();
205
206 StartTest(_T("String - assign #2"));
207 String t(_T("init string"));
208 s = t;
209 AssertEquals(s.length(), 11);
210 AssertTrue(!_tcscmp(s.getBuffer(), _T("init string")));
211 EndTest();
212
213 StartTest(_T("String - shrink"));
214 s.shrink();
215 AssertEquals(s.length(), 10);
216 AssertTrue(!_tcscmp(s.getBuffer(), _T("init strin")));
217 EndTest();
218
219 StartTest(_T("String - escape"));
220 s.escapeCharacter('i', '+');
221 AssertEquals(s.length(), 13);
222 AssertTrue(!_tcscmp(s.getBuffer(), _T("+in+it str+in")));
223 EndTest();
224
225 StartTest(_T("String - replace #1"));
226 s = _T("alpha beta gamma");
227 s.replace(_T("beta"), _T("epsilon"));
228 AssertEquals(s.length(), 19);
229 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha epsilon gamma")));
230 EndTest();
231
232 StartTest(_T("String - replace #2"));
233 s = _T("alpha beta gamma");
234 s.replace(_T("beta"), _T("xxxx"));
235 AssertEquals(s.length(), 16);
236 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha xxxx gamma")));
237 EndTest();
238
239 StartTest(_T("String - replace #3"));
240 s = _T("alpha beta gamma alpha omega");
241 s.replace(_T("alpha"), _T("Z"));
242 AssertEquals(s.length(), 20);
243 AssertTrue(!_tcscmp(s.getBuffer(), _T("Z beta gamma Z omega")));
244 EndTest();
245
246 StartTest(_T("String - substring #1"));
247 s = _T("alpha beta gamma");
248 TCHAR *str = s.substring(0, 5);
249 AssertTrue(!_tcscmp(str, _T("alpha")));
250 free(str);
251 EndTest();
252
253 StartTest(_T("String - substring #2"));
254 s = _T("alpha beta gamma");
255 str = s.substring(5, -1);
256 AssertTrue(!_tcscmp(str, _T(" beta gamma")));
257 free(str);
258 EndTest();
259
260 StartTest(_T("String - substring #3"));
261 s = _T("alpha beta gamma");
262 str = s.substring(14, 4);
263 AssertTrue(!_tcscmp(str, _T("ma")));
264 free(str);
265 EndTest();
266}
267
8e8ade0e
VK
268/**
269 * Test InetAddress class
270 */
271static void TestInetAddress()
272{
4203dcb5 273 InetAddress a, b, c;
8e8ade0e
VK
274
275 StartTest(_T("InetAddress - isSubnetBroadcast() - IPv4"));
276 a = InetAddress::parse("192.168.0.255");
277 AssertTrue(a.isSubnetBroadcast(24));
278 AssertFalse(a.isSubnetBroadcast(23));
279 EndTest();
280
281 StartTest(_T("InetAddress - isSubnetBroadcast() - IPv6"));
282 a = InetAddress::parse("fe80::ffff:ffff:ffff:ffff");
4203dcb5 283 AssertFalse(a.isSubnetBroadcast(64));
8e8ade0e
VK
284 AssertFalse(a.isSubnetBroadcast(63));
285 EndTest();
4203dcb5
VK
286
287 StartTest(_T("InetAddress - isLinkLocal() - IPv4"));
288 a = InetAddress::parse("169.254.17.198");
289 AssertTrue(a.isLinkLocal());
290 a = InetAddress::parse("192.168.1.1");
291 AssertFalse(a.isLinkLocal());
292 EndTest();
293
294 StartTest(_T("InetAddress - isLinkLocal() - IPv6"));
295 a = InetAddress::parse("fe80::1");
296 AssertTrue(a.isLinkLocal());
297 a = InetAddress::parse("2000:1234::1");
298 AssertFalse(a.isLinkLocal());
299 EndTest();
300
301 StartTest(_T("InetAddress - sameSubnet() - IPv4"));
302 a = InetAddress::parse("192.168.1.43");
303 a.setMaskBits(23);
304 b = InetAddress::parse("192.168.0.180");
305 b.setMaskBits(23);
306 c = InetAddress::parse("192.168.2.22");
307 c.setMaskBits(23);
308 AssertTrue(a.sameSubnet(b));
309 AssertFalse(a.sameSubnet(c));
310 EndTest();
311
312 StartTest(_T("InetAddress - sameSubnet() - IPv6"));
313 a = InetAddress::parse("2000:1234:1000:1000::1");
314 a.setMaskBits(62);
315 b = InetAddress::parse("2000:1234:1000:1001::cdef:1");
316 b.setMaskBits(62);
317 c = InetAddress::parse("2000:1234:1000:1007::1");
318 c.setMaskBits(62);
319 AssertTrue(a.sameSubnet(b));
320 AssertFalse(a.sameSubnet(c));
321 EndTest();
d7bbe226
VK
322
323 StartTest(_T("InetAddress - buildHashKey() - IPv4"));
324 a = InetAddress::parse("10.3.1.91");
325 BYTE key[18];
326 a.buildHashKey(key);
327#if WORDS_BIGENDIAN
328 AssertTrue(memcmp(key, "\x06\x02\x0A\x03\x01\x5B\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 18) == 0);
329#else
330 AssertTrue(memcmp(key, "\x06\x02\x5B\x01\x03\x0A\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 18) == 0);
331#endif
332 EndTest();
333
334 StartTest(_T("InetAddress - buildHashKey() - IPv6"));
335 a = InetAddress::parse("fe80:1234::6e88:14ff:fec4:b8f8");
336 a.buildHashKey(key);
337 AssertTrue(memcmp(key, "\x12\x0A\xFE\x80\x12\x34\x00\x00\x00\x00\x6E\x88\x14\xFF\xFE\xC4\xB8\xF8", 18) == 0);
338 EndTest();
8e8ade0e
VK
339}
340
9138dfe2
VK
341/**
342 * Test itoa/itow
343 */
344static void TestItoa()
345{
346 char buffer[64];
347 WCHAR wbuffer[64];
348
349 StartTest(_T("itoa"));
350 AssertTrue(!strcmp(_itoa(127, buffer, 10), "127"));
351 AssertTrue(!strcmp(_itoa(0, buffer, 10), "0"));
352 AssertTrue(!strcmp(_itoa(-3, buffer, 10), "-3"));
353 AssertTrue(!strcmp(_itoa(0555, buffer, 8), "555"));
354 AssertTrue(!strcmp(_itoa(0xFA48, buffer, 16), "fa48"));
355 EndTest();
356
357 StartTest(_T("itow"));
358 AssertTrue(!wcscmp(_itow(127, wbuffer, 10), L"127"));
359 AssertTrue(!wcscmp(_itow(0, wbuffer, 10), L"0"));
360 AssertTrue(!wcscmp(_itow(-3, wbuffer, 10), L"-3"));
361 AssertTrue(!wcscmp(_itow(0555, wbuffer, 8), L"555"));
362 AssertTrue(!wcscmp(_itow(0xFA48, wbuffer, 16), L"fa48"));
363 EndTest();
364}
365
19dbc8ef
VK
366/**
367 * Test queue
368 */
369static void TestQueue()
370{
371 Queue *q = new Queue(16, 16);
372
373 StartTest(_T("Queue: put/get"));
374 for(int i = 0; i < 40; i++)
375 q->put(CAST_TO_POINTER(i + 1, void *));
376 AssertEquals(q->size(), 40);
377 AssertEquals(q->allocated(), 48);
378 for(int i = 0; i < 40; i++)
379 {
380 void *p = q->get();
381 AssertNotNull(p);
382 AssertEquals(CAST_FROM_POINTER(p, int), i + 1);
383 }
384 EndTest();
385
386 StartTest(_T("Queue: shrink"));
387 for(int i = 0; i < 60; i++)
388 q->put(CAST_TO_POINTER(i + 1, void *));
389 AssertEquals(q->size(), 60);
390 AssertEquals(q->allocated(), 64);
391 for(int i = 0; i < 55; i++)
392 {
393 void *p = q->get();
394 AssertNotNull(p);
395 AssertEquals(CAST_FROM_POINTER(p, int), i + 1);
396 }
397 AssertEquals(q->size(), 5);
398 AssertEquals(q->allocated(), 16);
399 EndTest();
400}
401
ee2d686f
VK
402/**
403 * main()
404 */
bd96aa53 405int main(int argc, char *argv[])
ee2d686f 406{
8e8ade0e
VK
407#ifdef _WIN32
408 WSADATA wsaData;
409 WSAStartup(MAKEWORD(2, 2), &wsaData);
410#endif
411
4e0e77e6 412 TestString();
ee2d686f
VK
413 TestStringConversion();
414 TestStringMap();
4b6e5e27 415 TestStringSet();
de4af576 416 TestMessageClass();
7a70e9de 417 TestMsgWaitQueue();
8e8ade0e 418 TestInetAddress();
9138dfe2 419 TestItoa();
19dbc8ef 420 TestQueue();
ee2d686f
VK
421 return 0;
422}