start adding tests for C part
[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
5static char mbText[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
6static WCHAR wcText[] = L"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
7static char mbTextShort[] = "Lorem ipsum";
8static UCS2CHAR ucs2TextShort[] = { 'L', 'o', 'r', 'e', 'm', ' ', 'i', 'p', 's', 'u', 'm', 0 };
9
10/**
11 * Test string conversion
12 */
13static 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 */
89static 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
131/**
132 * main()
133 */
134int main(int argc, char *argv)
135{
136 TestStringConversion();
137 TestStringMap();
138 return 0;
139}