StringMap implementation now uses hash
[public/netxms.git] / tests / test-libnetxms / test-libnetxms.cpp
1 #include <nms_common.h>
2 #include <nms_util.h>
3 #include <testtools.h>
4
5 static char mbText[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
6 static WCHAR wcText[] = L"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
7 static char mbTextShort[] = "Lorem ipsum";
8 static UCS2CHAR ucs2TextShort[] = { 'L', 'o', 'r', 'e', 'm', ' ', 'i', 'p', 's', 'u', 'm', 0 };
9
10 /**
11 * Test string conversion
12 */
13 static void TestStringConversion()
14 {
15 StartTest(_T("ANSI to UCS-2 conversion"));
16 UCS2CHAR ucs2buffer[1024];
17 mb_to_ucs2(mbTextShort, -1, ucs2buffer, 1024);
18 AssertTrue(!memcmp(ucs2buffer, ucs2TextShort, sizeof(UCS2CHAR) * 12));
19 EndTest();
20
21 StartTest(_T("UCS-2 to ANSI conversion"));
22 char mbBuffer[1024];
23 ucs2_to_mb(ucs2TextShort, -1, mbBuffer, 1024);
24 AssertTrue(!strcmp(mbBuffer, mbTextShort));
25 EndTest();
26
27 StartTest(_T("ANSI to UCS-2 conversion performance"));
28 INT64 start = GetCurrentTimeMs();
29 for(int i = 0; i < 10000; i++)
30 {
31 UCS2CHAR buffer[1024];
32 mb_to_ucs2(mbText, -1, buffer, 1024);
33 }
34 EndTest(GetCurrentTimeMs() - start);
35
36 StartTest(_T("UCS-2 to ANSI conversion performance"));
37 mb_to_ucs2(mbText, -1, ucs2buffer, 1024);
38 start = GetCurrentTimeMs();
39 for(int i = 0; i < 10000; i++)
40 {
41 char buffer[1024];
42 ucs2_to_mb(ucs2buffer, -1, buffer, 1024);
43 }
44 EndTest(GetCurrentTimeMs() - start);
45
46 #ifdef UNICODE_UCS4
47 StartTest(_T("ANSI to UCS-4 conversion performance"));
48 start = GetCurrentTimeMs();
49 for(int i = 0; i < 10000; i++)
50 {
51 WCHAR buffer[1024];
52 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mbText, -1, buffer, 1024);
53 }
54 EndTest(GetCurrentTimeMs() - start);
55
56 StartTest(_T("UCS-4 to ANSI conversion performance"));
57 start = GetCurrentTimeMs();
58 for(int i = 0; i < 10000; i++)
59 {
60 char buffer[1024];
61 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, wcText, -1, buffer, 1024, NULL, NULL);
62 }
63 EndTest(GetCurrentTimeMs() - start);
64
65 StartTest(_T("UCS-2 to UCS-4 conversion performance"));
66 mb_to_ucs2(mbText, -1, ucs2buffer, 1024);
67 start = GetCurrentTimeMs();
68 for(int i = 0; i < 10000; i++)
69 {
70 WCHAR buffer[1024];
71 ucs2_to_ucs4(ucs2buffer, -1, buffer, 1024);
72 }
73 EndTest(GetCurrentTimeMs() - start);
74
75 StartTest(_T("UCS-4 to UCS-2 conversion performance"));
76 start = GetCurrentTimeMs();
77 for(int i = 0; i < 10000; i++)
78 {
79 UCS2CHAR buffer[1024];
80 ucs4_to_ucs2(wcText, -1, buffer, 1024);
81 }
82 EndTest(GetCurrentTimeMs() - start);
83 #endif
84 }
85
86 /**
87 * Test string map
88 */
89 static void TestStringMap()
90 {
91 StringMap *m = new StringMap();
92
93 StartTest(_T("String map - insert"));
94 INT64 start = GetCurrentTimeMs();
95 for(int i = 0; i < 10000; i++)
96 {
97 TCHAR key[64];
98 _sntprintf(key, 64, _T("key-%d"), i);
99 m->set(key, _T("Lorem ipsum dolor sit amet"));
100 }
101 AssertEquals(m->size(), 10000);
102 const TCHAR *v = m->get(_T("key-42"));
103 AssertNotNull(v);
104 AssertTrue(!_tcscmp(v, _T("Lorem ipsum dolor sit amet")));
105 EndTest(GetCurrentTimeMs() - start);
106
107 StartTest(_T("String map - replace"));
108 start = GetCurrentTimeMs();
109 for(int i = 0; i < 10000; i++)
110 {
111 TCHAR key[64];
112 _sntprintf(key, 64, _T("key-%d"), i);
113 m->set(key, _T("consectetur adipiscing elit"));
114 }
115 AssertEquals(m->size(), 10000);
116 v = m->get(_T("key-42"));
117 AssertNotNull(v);
118 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
119 EndTest(GetCurrentTimeMs() - start);
120
121 StartTest(_T("String map - get"));
122 start = GetCurrentTimeMs();
123 v = m->get(_T("key-888"));
124 AssertNotNull(v);
125 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
126 EndTest(GetCurrentTimeMs() - start);
127
128 StartTest(_T("String map - clear"));
129 start = GetCurrentTimeMs();
130 m->clear();
131 AssertEquals(m->size(), 0);
132 EndTest(GetCurrentTimeMs() - start);
133
134 delete m;
135 }
136
137 /**
138 * Test string set
139 */
140 static void TestStringSet()
141 {
142 StringSet *s = new StringSet();
143
144 StartTest(_T("String set - insert"));
145 INT64 start = GetCurrentTimeMs();
146 for(int i = 0; i < 10000; i++)
147 {
148 TCHAR key[64];
149 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
150 s->add(key);
151 }
152 AssertEquals(s->size(), 10000);
153 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
154 EndTest(GetCurrentTimeMs() - start);
155
156 StartTest(_T("String set - replace"));
157 start = GetCurrentTimeMs();
158 for(int i = 0; i < 10000; i++)
159 {
160 TCHAR key[64];
161 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
162 s->add(key);
163 }
164 AssertEquals(s->size(), 10000);
165 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
166 EndTest(GetCurrentTimeMs() - start);
167
168 StartTest(_T("String set - contains"));
169 start = GetCurrentTimeMs();
170 AssertTrue(s->contains(_T("key-888 lorem ipsum")));
171 EndTest(GetCurrentTimeMs() - start);
172
173 StartTest(_T("String set - clear"));
174 start = GetCurrentTimeMs();
175 s->clear();
176 AssertEquals(s->size(), 0);
177 EndTest(GetCurrentTimeMs() - start);
178
179 delete s;
180 }
181
182 /**
183 * main()
184 */
185 int main(int argc, char *argv)
186 {
187 TestStringConversion();
188 TestStringMap();
189 TestStringSet();
190 return 0;
191 }