additional utility methods in InetAddress class
[public/netxms.git] / tests / test-libnetxms / test-libnetxms.cpp
CommitLineData
ee2d686f
VK
1#include <nms_common.h>
2#include <nms_util.h>
3#include <testtools.h>
4
7a70e9de
VK
5void TestMsgWaitQueue();
6
ee2d686f
VK
7static char mbText[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
8static WCHAR wcText[] = L"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
9static char mbTextShort[] = "Lorem ipsum";
10static UCS2CHAR ucs2TextShort[] = { 'L', 'o', 'r', 'e', 'm', ' ', 'i', 'p', 's', 'u', 'm', 0 };
11
12/**
13 * Test string conversion
14 */
15static void TestStringConversion()
16{
17 StartTest(_T("ANSI to UCS-2 conversion"));
18 UCS2CHAR ucs2buffer[1024];
19 mb_to_ucs2(mbTextShort, -1, ucs2buffer, 1024);
20 AssertTrue(!memcmp(ucs2buffer, ucs2TextShort, sizeof(UCS2CHAR) * 12));
21 EndTest();
22
23 StartTest(_T("UCS-2 to ANSI conversion"));
24 char mbBuffer[1024];
25 ucs2_to_mb(ucs2TextShort, -1, mbBuffer, 1024);
26 AssertTrue(!strcmp(mbBuffer, mbTextShort));
27 EndTest();
28
29 StartTest(_T("ANSI to UCS-2 conversion performance"));
30 INT64 start = GetCurrentTimeMs();
31 for(int i = 0; i < 10000; i++)
32 {
33 UCS2CHAR buffer[1024];
34 mb_to_ucs2(mbText, -1, buffer, 1024);
35 }
36 EndTest(GetCurrentTimeMs() - start);
37
38 StartTest(_T("UCS-2 to ANSI conversion performance"));
39 mb_to_ucs2(mbText, -1, ucs2buffer, 1024);
40 start = GetCurrentTimeMs();
41 for(int i = 0; i < 10000; i++)
42 {
43 char buffer[1024];
44 ucs2_to_mb(ucs2buffer, -1, buffer, 1024);
45 }
46 EndTest(GetCurrentTimeMs() - start);
47
48#ifdef UNICODE_UCS4
49 StartTest(_T("ANSI to UCS-4 conversion performance"));
50 start = GetCurrentTimeMs();
51 for(int i = 0; i < 10000; i++)
52 {
53 WCHAR buffer[1024];
54 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mbText, -1, buffer, 1024);
55 }
56 EndTest(GetCurrentTimeMs() - start);
57
58 StartTest(_T("UCS-4 to ANSI conversion performance"));
59 start = GetCurrentTimeMs();
60 for(int i = 0; i < 10000; i++)
61 {
62 char buffer[1024];
63 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcText, -1, buffer, 1024, NULL, NULL);
64 }
65 EndTest(GetCurrentTimeMs() - start);
66
67 StartTest(_T("UCS-2 to UCS-4 conversion performance"));
68 mb_to_ucs2(mbText, -1, ucs2buffer, 1024);
69 start = GetCurrentTimeMs();
70 for(int i = 0; i < 10000; i++)
71 {
72 WCHAR buffer[1024];
73 ucs2_to_ucs4(ucs2buffer, -1, buffer, 1024);
74 }
75 EndTest(GetCurrentTimeMs() - start);
76
77 StartTest(_T("UCS-4 to UCS-2 conversion performance"));
78 start = GetCurrentTimeMs();
79 for(int i = 0; i < 10000; i++)
80 {
81 UCS2CHAR buffer[1024];
82 ucs4_to_ucs2(wcText, -1, buffer, 1024);
83 }
84 EndTest(GetCurrentTimeMs() - start);
85#endif
86}
87
88/**
89 * Test string map
90 */
91static void TestStringMap()
92{
93 StringMap *m = new StringMap();
94
95 StartTest(_T("String map - insert"));
96 INT64 start = GetCurrentTimeMs();
97 for(int i = 0; i < 10000; i++)
98 {
99 TCHAR key[64];
100 _sntprintf(key, 64, _T("key-%d"), i);
101 m->set(key, _T("Lorem ipsum dolor sit amet"));
102 }
103 AssertEquals(m->size(), 10000);
400e55c4
VK
104 const TCHAR *v = m->get(_T("key-42"));
105 AssertNotNull(v);
106 AssertTrue(!_tcscmp(v, _T("Lorem ipsum dolor sit amet")));
ee2d686f
VK
107 EndTest(GetCurrentTimeMs() - start);
108
109 StartTest(_T("String map - replace"));
110 start = GetCurrentTimeMs();
111 for(int i = 0; i < 10000; i++)
112 {
113 TCHAR key[64];
114 _sntprintf(key, 64, _T("key-%d"), i);
115 m->set(key, _T("consectetur adipiscing elit"));
116 }
117 AssertEquals(m->size(), 10000);
400e55c4
VK
118 v = m->get(_T("key-42"));
119 AssertNotNull(v);
120 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
ee2d686f
VK
121 EndTest(GetCurrentTimeMs() - start);
122
123 StartTest(_T("String map - get"));
124 start = GetCurrentTimeMs();
400e55c4 125 v = m->get(_T("key-888"));
ee2d686f
VK
126 AssertNotNull(v);
127 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
128 EndTest(GetCurrentTimeMs() - start);
129
130 StartTest(_T("String map - clear"));
131 start = GetCurrentTimeMs();
132 m->clear();
133 AssertEquals(m->size(), 0);
134 EndTest(GetCurrentTimeMs() - start);
4b6e5e27
VK
135
136 delete m;
137}
138
139/**
140 * Test string set
141 */
142static void TestStringSet()
143{
144 StringSet *s = new StringSet();
145
146 StartTest(_T("String set - insert"));
147 INT64 start = GetCurrentTimeMs();
148 for(int i = 0; i < 10000; i++)
149 {
150 TCHAR key[64];
151 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
152 s->add(key);
153 }
154 AssertEquals(s->size(), 10000);
155 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
156 EndTest(GetCurrentTimeMs() - start);
157
158 StartTest(_T("String set - replace"));
159 start = GetCurrentTimeMs();
160 for(int i = 0; i < 10000; i++)
161 {
162 TCHAR key[64];
163 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
164 s->add(key);
165 }
166 AssertEquals(s->size(), 10000);
167 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
168 EndTest(GetCurrentTimeMs() - start);
169
170 StartTest(_T("String set - contains"));
171 start = GetCurrentTimeMs();
172 AssertTrue(s->contains(_T("key-888 lorem ipsum")));
173 EndTest(GetCurrentTimeMs() - start);
174
175 StartTest(_T("String set - clear"));
176 start = GetCurrentTimeMs();
177 s->clear();
178 AssertEquals(s->size(), 0);
179 EndTest(GetCurrentTimeMs() - start);
180
181 delete s;
ee2d686f
VK
182}
183
4e0e77e6
VK
184/**
185 * Test string class
186 */
187static void TestString()
188{
189 String s;
190
191 StartTest(_T("String - append"));
192 for(int i = 0; i < 256; i++)
193 s.append(_T("ABC "));
194 AssertEquals(s.length(), 1024);
195 AssertTrue(!_tcsncmp(s.getBuffer(), _T("ABC ABC ABC ABC "), 16));
196 EndTest();
197
198 StartTest(_T("String - assign #1"));
199 s = _T("alpha");
200 AssertEquals(s.length(), 5);
201 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha")));
202 EndTest();
203
204 StartTest(_T("String - assign #2"));
205 String t(_T("init string"));
206 s = t;
207 AssertEquals(s.length(), 11);
208 AssertTrue(!_tcscmp(s.getBuffer(), _T("init string")));
209 EndTest();
210
211 StartTest(_T("String - shrink"));
212 s.shrink();
213 AssertEquals(s.length(), 10);
214 AssertTrue(!_tcscmp(s.getBuffer(), _T("init strin")));
215 EndTest();
216
217 StartTest(_T("String - escape"));
218 s.escapeCharacter('i', '+');
219 AssertEquals(s.length(), 13);
220 AssertTrue(!_tcscmp(s.getBuffer(), _T("+in+it str+in")));
221 EndTest();
222
223 StartTest(_T("String - replace #1"));
224 s = _T("alpha beta gamma");
225 s.replace(_T("beta"), _T("epsilon"));
226 AssertEquals(s.length(), 19);
227 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha epsilon gamma")));
228 EndTest();
229
230 StartTest(_T("String - replace #2"));
231 s = _T("alpha beta gamma");
232 s.replace(_T("beta"), _T("xxxx"));
233 AssertEquals(s.length(), 16);
234 AssertTrue(!_tcscmp(s.getBuffer(), _T("alpha xxxx gamma")));
235 EndTest();
236
237 StartTest(_T("String - replace #3"));
238 s = _T("alpha beta gamma alpha omega");
239 s.replace(_T("alpha"), _T("Z"));
240 AssertEquals(s.length(), 20);
241 AssertTrue(!_tcscmp(s.getBuffer(), _T("Z beta gamma Z omega")));
242 EndTest();
243
244 StartTest(_T("String - substring #1"));
245 s = _T("alpha beta gamma");
246 TCHAR *str = s.substring(0, 5);
247 AssertTrue(!_tcscmp(str, _T("alpha")));
248 free(str);
249 EndTest();
250
251 StartTest(_T("String - substring #2"));
252 s = _T("alpha beta gamma");
253 str = s.substring(5, -1);
254 AssertTrue(!_tcscmp(str, _T(" beta gamma")));
255 free(str);
256 EndTest();
257
258 StartTest(_T("String - substring #3"));
259 s = _T("alpha beta gamma");
260 str = s.substring(14, 4);
261 AssertTrue(!_tcscmp(str, _T("ma")));
262 free(str);
263 EndTest();
264}
265
8e8ade0e
VK
266/**
267 * Test InetAddress class
268 */
269static void TestInetAddress()
270{
271 InetAddress a;
272
273 StartTest(_T("InetAddress - isSubnetBroadcast() - IPv4"));
274 a = InetAddress::parse("192.168.0.255");
275 AssertTrue(a.isSubnetBroadcast(24));
276 AssertFalse(a.isSubnetBroadcast(23));
277 EndTest();
278
279 StartTest(_T("InetAddress - isSubnetBroadcast() - IPv6"));
280 a = InetAddress::parse("fe80::ffff:ffff:ffff:ffff");
281 AssertTrue(a.isSubnetBroadcast(64));
282 AssertFalse(a.isSubnetBroadcast(63));
283 EndTest();
284}
285
ee2d686f
VK
286/**
287 * main()
288 */
bd96aa53 289int main(int argc, char *argv[])
ee2d686f 290{
8e8ade0e
VK
291#ifdef _WIN32
292 WSADATA wsaData;
293 WSAStartup(MAKEWORD(2, 2), &wsaData);
294#endif
295
4e0e77e6 296 TestString();
ee2d686f
VK
297 TestStringConversion();
298 TestStringMap();
4b6e5e27 299 TestStringSet();
7a70e9de 300 TestMsgWaitQueue();
8e8ade0e 301 TestInetAddress();
ee2d686f
VK
302 return 0;
303}