79dc3873163c272c330e25cbc6d9c9bb8c617a9f
[reactos.git] / rostests / apitests / atl / CString.cpp
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for CString
5 * PROGRAMMER: Mark Jansen
6 */
7
8 #include <atlstr.h>
9 #include <apitest.h>
10
11
12 struct traits_test
13 {
14 const char* strA;
15 const wchar_t* strW;
16 int str_len;
17 int exp_1, exp_2, exp_3, exp_4;
18 };
19
20 traits_test g_Tests[] = {
21 // inputs outputs
22 { NULL, NULL, 0, 0, 0, -1, 0 },
23 { NULL, NULL, -1, 0, -1, -1, 0 },
24 { NULL, NULL, 1, 0, 1, -1, 0 },
25
26 { "", L"", 0, 0, 0, 0, 0 },
27 { "", L"", -1, 0, -1, 0, 1 },
28 { "", L"", 1, 0, 1, 0, 1 },
29
30 { "AAABBB", L"AAABBB", 0, 6, 0, 6, 0 },
31 { "AAABBB", L"AAABBB", 3, 6, 3, 6, 3 },
32 { "AAABBB", L"AAABBB", -1, 6, -1, 6, 7 },
33 };
34
35 static void test_basetypes()
36 {
37 int len;
38 char bufA[10];
39 wchar_t bufW[10];
40
41 for (size_t n = 0; n < _countof(g_Tests); ++n)
42 {
43 len = ChTraitsCRT<char>::GetBaseTypeLength(g_Tests[n].strA);
44 ok(len == g_Tests[n].exp_1, "Expected len to be %i, was %i for %u (A)\n", g_Tests[n].exp_1, len, n);
45
46 len = ChTraitsCRT<char>::GetBaseTypeLength(g_Tests[n].strA, g_Tests[n].str_len);
47 ok(len == g_Tests[n].exp_2, "Expected len to be %i, was %i for %u (A,len)\n", g_Tests[n].exp_2, len, n);
48
49 len = ChTraitsCRT<char>::GetBaseTypeLength(g_Tests[n].strW);
50 ok(len == g_Tests[n].exp_3, "Expected len to be %i, was %i for %u (W)\n", g_Tests[n].exp_3, len, n);
51
52 len = ChTraitsCRT<char>::GetBaseTypeLength(g_Tests[n].strW, g_Tests[n].str_len);
53 ok(len == g_Tests[n].exp_4, "Expected len to be %i, was %i for %u (W,len)\n", g_Tests[n].exp_4, len, n);
54
55 if (g_Tests[n].strA && g_Tests[n].strW)
56 {
57 memset(bufA, 'x', sizeof(bufA));
58 ChTraitsCRT<char>::ConvertToBaseType(bufA, g_Tests[n].exp_1+1, g_Tests[n].strA);
59 char ch = bufA[g_Tests[n].exp_1];
60 ok(ch == '\0', "Expected %i to be \\0, was: %c (%i) for %u\n", g_Tests[n].exp_1, ch, (int)ch, n);
61 ok(!strcmp(bufA, g_Tests[n].strA), "Expected bufA to be %s, was: %s for %u\n", g_Tests[n].strA, bufA, n);
62 ch = bufA[g_Tests[n].exp_1+1];
63 ok(ch == 'x', "Expected %i to be 'x', was: %c (%i) for %u\n", g_Tests[n].exp_1+1, ch, (int)ch, n);
64 }
65
66 if (g_Tests[n].strA && g_Tests[n].strW)
67 {
68 memset(bufA, 'x', sizeof(bufA));
69 ChTraitsCRT<char>::ConvertToBaseType(bufA, g_Tests[n].exp_1+1, g_Tests[n].strW);
70 char ch = bufA[g_Tests[n].exp_1];
71 ok(ch == '\0', "Expected %i to be \\0, was: %c (%i) for %u\n", g_Tests[n].exp_1, ch, (int)ch, n);
72 ok(!strcmp(bufA, g_Tests[n].strA), "Expected bufA to be %s, was: %s for %u\n", g_Tests[n].strA, bufA, n);
73 ch = bufA[g_Tests[n].exp_1+1];
74 ok(ch == 'x', "Expected %i to be 'x', was: %c (%i) for %u\n", g_Tests[n].exp_1+1, ch, (int)ch, n);
75 }
76
77 // wchar_t --> please note, swapped the expectations from 2 and 4 !
78 len = ChTraitsCRT<wchar_t>::GetBaseTypeLength(g_Tests[n].strA);
79 ok(len == g_Tests[n].exp_1, "Expected len to be %i, was %i for %u (A)\n", g_Tests[n].exp_1, len, n);
80
81 len = ChTraitsCRT<wchar_t>::GetBaseTypeLength(g_Tests[n].strA, g_Tests[n].str_len);
82 ok(len == g_Tests[n].exp_4, "Expected len to be %i, was %i for %u (A,len)\n", g_Tests[n].exp_4, len, n);
83
84 len = ChTraitsCRT<wchar_t>::GetBaseTypeLength(g_Tests[n].strW);
85 ok(len == g_Tests[n].exp_3, "Expected len to be %i, was %i for %u (W)\n", g_Tests[n].exp_3, len, n);
86
87 len = ChTraitsCRT<wchar_t>::GetBaseTypeLength(g_Tests[n].strW, g_Tests[n].str_len);
88 ok(len == g_Tests[n].exp_2, "Expected len to be %i, was %i for %u (W,len)\n", g_Tests[n].exp_2, len, n);
89
90 if (g_Tests[n].strA && g_Tests[n].strW)
91 {
92 memset(bufW, 'x', sizeof(bufW));
93 ChTraitsCRT<wchar_t>::ConvertToBaseType(bufW, g_Tests[n].exp_1+1, g_Tests[n].strA);
94 wchar_t ch = bufW[g_Tests[n].exp_1];
95 ok(ch == L'\0', "Expected %i to be \\0, was: %c (%i) for %u\n", g_Tests[n].exp_1, ch, (int)ch, n);
96 ok(!wcscmp(bufW, g_Tests[n].strW), "Expected bufW to be %s, was: %s for %u\n", wine_dbgstr_w(g_Tests[n].strW), wine_dbgstr_w(bufW), n);
97 ch = bufW[g_Tests[n].exp_1+1];
98 ok(ch == 30840, "Expected %i to be %i for %u\n", g_Tests[n].exp_1+1, (int)ch, n);
99 }
100
101 if (g_Tests[n].strA && g_Tests[n].strW)
102 {
103 memset(bufW, 'x', sizeof(bufW));
104 ChTraitsCRT<wchar_t>::ConvertToBaseType(bufW, g_Tests[n].exp_1+1, g_Tests[n].strW);
105 wchar_t ch = bufW[g_Tests[n].exp_1];
106 ok(ch == '\0', "Expected %i to be \\0, was: %c (%i) for %u\n", g_Tests[n].exp_1, ch, (int)ch, n);
107 ok(!wcscmp(bufW, g_Tests[n].strW), "Expected bufW to be %s, was: %s for %u\n", wine_dbgstr_w(g_Tests[n].strW), wine_dbgstr_w(bufW), n);
108 ch = bufW[g_Tests[n].exp_1+1];
109 ok(ch == 30840, "Expected %i to be %i for %u\n", g_Tests[n].exp_1+1, (int)ch, n);
110 }
111 }
112 }
113
114 // Allocation strategy seems to differ a bit between us and MS's atl.
115 // if someone cares enough to find out why, feel free to change the macro below.
116 #define ALLOC_EXPECT(a, b) b
117
118
119 #undef ok
120 #undef _T
121
122 #define TEST_NAMEX(name) void test_##name##W()
123 #define CStringX CStringW
124 #define _X(x) L ## x
125 #define XCHAR WCHAR
126 #define dbgstrx(x) wine_dbgstr_w(x)
127 #define ok ok_("CStringW:\n" __FILE__, __LINE__)
128 #define GetWindowsDirectoryX GetWindowsDirectoryW
129 #include "CString.inl"
130
131
132 #undef CStringX
133 #undef TEST_NAMEX
134 #undef _X
135 #undef XCHAR
136 #undef dbgstrx
137 #undef ok
138 #undef GetWindowsDirectoryX
139
140 #define TEST_NAMEX(name) void test_##name##A()
141 #define CStringX CStringA
142 #define _X(x) x
143 #define XCHAR CHAR
144 #define dbgstrx(x) (const char*)x
145 #define ok ok_("CStringA:\n" __FILE__, __LINE__)
146 #define GetWindowsDirectoryX GetWindowsDirectoryA
147 #include "CString.inl"
148
149
150 START_TEST(CString)
151 {
152 test_basetypes();
153
154 if ((ALLOC_EXPECT(1, 2)) == 2)
155 {
156 skip("Ignoring real GetAllocLength() lenght\n");
157 }
158
159 test_operators_initW();
160 test_operators_initA();
161
162 test_compareW();
163 test_compareA();
164
165 test_findW();
166 test_findA();
167
168 test_formatW();
169 test_formatA();
170
171 test_substrW();
172 test_substrA();
173
174 test_replaceW();
175 test_replaceA();
176
177 test_trimW();
178 test_trimA();
179
180 test_envW();
181 test_envA();
182 }