test for StringSet
[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 AssertTrue(!_tcscmp(m->get(_T("key-42")), _T("Lorem ipsum dolor sit amet")));
103 EndTest(GetCurrentTimeMs() - start);
104
105 StartTest(_T("String map - replace"));
106 start = GetCurrentTimeMs();
107 for(int i = 0; i < 10000; i++)
108 {
109 TCHAR key[64];
110 _sntprintf(key, 64, _T("key-%d"), i);
111 m->set(key, _T("consectetur adipiscing elit"));
112 }
113 AssertEquals(m->size(), 10000);
114 AssertTrue(!_tcscmp(m->get(_T("key-42")), _T("consectetur adipiscing elit")));
115 EndTest(GetCurrentTimeMs() - start);
116
117 StartTest(_T("String map - get"));
118 start = GetCurrentTimeMs();
119 const TCHAR *v = m->get(_T("key-888"));
120 AssertNotNull(v);
121 AssertTrue(!_tcscmp(v, _T("consectetur adipiscing elit")));
122 EndTest(GetCurrentTimeMs() - start);
123
124 StartTest(_T("String map - clear"));
125 start = GetCurrentTimeMs();
126 m->clear();
127 AssertEquals(m->size(), 0);
128 EndTest(GetCurrentTimeMs() - start);
129
130 delete m;
131 }
132
133 /**
134 * Test string set
135 */
136 static void TestStringSet()
137 {
138 StringSet *s = new StringSet();
139
140 StartTest(_T("String set - insert"));
141 INT64 start = GetCurrentTimeMs();
142 for(int i = 0; i < 10000; i++)
143 {
144 TCHAR key[64];
145 _sntprintf(key, 64, _T("key-%d lorem ipsum"), i);
146 s->add(key);
147 }
148 AssertEquals(s->size(), 10000);
149 AssertTrue(s->contains(_T("key-42 lorem ipsum")));
150 EndTest(GetCurrentTimeMs() - start);
151
152 StartTest(_T("String set - replace"));
153 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 - contains"));
165 start = GetCurrentTimeMs();
166 AssertTrue(s->contains(_T("key-888 lorem ipsum")));
167 EndTest(GetCurrentTimeMs() - start);
168
169 StartTest(_T("String set - clear"));
170 start = GetCurrentTimeMs();
171 s->clear();
172 AssertEquals(s->size(), 0);
173 EndTest(GetCurrentTimeMs() - start);
174
175 delete s;
176 }
177
178 /**
179 * main()
180 */
181 int main(int argc, char *argv)
182 {
183 TestStringConversion();
184 TestStringMap();
185 TestStringSet();
186 return 0;
187 }