[ATL][ATL_APITEST] Add GetEnvironmentVariable to CString. CORE-12581
[reactos.git] / rostests / apitests / atl / CString.inl
1
2 TEST_NAMEX(operators_init)
3 {
4 CStringX test;
5 ok(test.IsEmpty() == true, "Expected test to be empty\n");
6 ok(test.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", test.GetLength());
7 ok(test.GetAllocLength() == 0, "Expected GetAllocLength() to be 0, was: %i\n", test.GetAllocLength());
8
9 // Operator
10 const XCHAR* cstring = (const XCHAR*)test;
11 ok(cstring != NULL, "Expected a valid pointer\n");
12 if (cstring)
13 {
14 ok(cstring[0] == '\0', "Expected \\0, got: %c (%i)\n", cstring[0], (int)cstring[0]);
15 }
16
17 CStringX first(_X("First "));
18 ok(first.IsEmpty() != true, "Expected first to not be empty\n");
19 ok(first.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", first.GetLength());
20 ok(first.GetAllocLength() == ALLOC_EXPECT(7, 6), "Expected GetAllocLength() to be %i, was: %i\n", ALLOC_EXPECT(7, 6), first.GetAllocLength());
21
22 CStringX second(_X("Second"));
23 ok(second.IsEmpty() != true, "Expected second to not be empty\n");
24 ok(second.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", second.GetLength());
25 ok(second.GetAllocLength() == ALLOC_EXPECT(7, 6), "Expected GetAllocLength() to be %i, was: %i\n", ALLOC_EXPECT(7, 6), second.GetAllocLength());
26
27 test = first;
28 ok(test.IsEmpty() != true, "Expected test to not be empty\n");
29 ok(test.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", test.GetLength());
30 ok(test.GetAllocLength() == ALLOC_EXPECT(7, 6), "Expected GetAllocLength() to be %i, was: %i\n", ALLOC_EXPECT(7, 6), test.GetAllocLength());
31
32 test.Empty();
33 ok(test.IsEmpty() == true, "Expected test to be empty\n");
34 ok(test.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", test.GetLength());
35 ok(test.GetAllocLength() == 0, "Expected GetAllocLength() to be 0, was: %i\n", test.GetAllocLength());
36
37 test = _X("First ");
38 ok(test.IsEmpty() != true, "Expected test to not be empty\n");
39 ok(test.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", test.GetLength());
40 ok(test.GetAllocLength() == ALLOC_EXPECT(7, 6), "Expected GetAllocLength() to be %i, was: %i\n", ALLOC_EXPECT(7, 6), test.GetAllocLength());
41
42 test += second;
43 ok(test.IsEmpty() != true, "Expected test to not be empty\n");
44 ok(test.GetLength() == 12, "Expected GetLength() to be 12, was: %i\n", test.GetLength());
45 ok(test.GetAllocLength() == ALLOC_EXPECT(15, 12), "Expected GetAllocLength() to be %i, was: %i\n", ALLOC_EXPECT(15, 12), test.GetAllocLength());
46
47 test = first + second;
48 ok(test.IsEmpty() != true, "Expected test to not be empty\n");
49 ok(test.GetLength() == 12, "Expected GetLength() to be 12, was: %i\n", test.GetLength());
50 ok(test.GetAllocLength() == ALLOC_EXPECT(15, 12), "Expected GetAllocLength() to be %i, was: %i\n", ALLOC_EXPECT(15, 12), test.GetAllocLength());
51
52 test = first + second + _X(".");
53 ok(test.IsEmpty() != true, "Expected test to not be empty\n");
54 ok(test.GetLength() == 13, "Expected GetLength() to be 13, was: %i\n", test.GetLength());
55 ok(test.GetAllocLength() == ALLOC_EXPECT(15, 18), "Expected GetAllocLength() to be %i, was: %i\n", ALLOC_EXPECT(15, 18), test.GetAllocLength());
56
57 CStringX test2(test);
58 ok(test2.IsEmpty() != true, "Expected test2 to not be empty\n");
59 ok(test2.GetLength() == 13, "Expected GetLength() to be 13, was: %i\n", test2.GetLength());
60 ok(test2.GetAllocLength() == ALLOC_EXPECT(15, 18), "Expected GetAllocLength() to be %i, was: %i\n", ALLOC_EXPECT(15, 18), test2.GetAllocLength());
61
62 // Clear it again
63 test.Empty();
64 ok(test.IsEmpty() == true, "Expected test to be empty\n");
65 ok(test.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", test.GetLength());
66 ok(test.GetAllocLength() == 0, "Expected GetAllocLength() to be 0, was: %i\n", test.GetAllocLength());
67
68 // Assign string
69 test = "First ";
70 ok(test.IsEmpty() != true, "Expected test to not be empty\n");
71 ok(test.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", test.GetLength());
72 ok(test.GetAllocLength() == ALLOC_EXPECT(7, 6), "Expected GetAllocLength() to be 7, was: %i\n", test.GetAllocLength());
73
74 CStringA testA = test;
75 ok(testA.IsEmpty() != true, "Expected testA to not be empty\n");
76 ok(testA.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", testA.GetLength());
77 ok(testA.GetAllocLength() == ALLOC_EXPECT(7, 6), "Expected GetAllocLength() to be 7, was: %i\n", testA.GetAllocLength());
78
79 CStringW testW = test;
80 ok(testW.IsEmpty() != true, "Expected testW to not be empty\n");
81 ok(testW.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", testW.GetLength());
82 ok(testW.GetAllocLength() == ALLOC_EXPECT(7, 6), "Expected GetAllocLength() to be 7, was: %i\n", testW.GetAllocLength());
83
84 // Assign wstring
85 test = L"First ";
86 ok(test.IsEmpty() != true, "Expected test to not be empty\n");
87 ok(test.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", test.GetLength());
88 ok(test.GetAllocLength() == ALLOC_EXPECT(7, 6), "Expected GetAllocLength() to be 7, was: %i\n", test.GetAllocLength());
89 }
90
91
92 TEST_NAMEX(compare)
93 {
94 CStringX s1, s2;
95 s1 = s2 = _X("some text 1!");
96
97 int c = s1.Compare(s2);
98 ok(c == 0, "Expected c to be 1, was: %i\n", c);
99
100 c = s1.Compare(_X("r"));
101 ok(c == 1, "Expected c to be 1, was: %i\n", c);
102
103 c = s1.Compare(_X("s"));
104 ok(c == 1, "Expected c to be 1, was: %i\n", c);
105
106 c = s1.Compare(_X("t"));
107 ok(c == -1, "Expected c to be -1, was: %i\n", c);
108
109 c = s1.Compare(_X("R"));
110 ok(c == 1, "Expected c to be 1, was: %i\n", c);
111
112 c = s1.Compare(_X("S"));
113 ok(c == 1, "Expected c to be 1, was: %i\n", c);
114
115 c = s1.Compare(_X("T"));
116 ok(c == 1, "Expected c to be 1, was: %i\n", c);
117
118 ok(s1 == s2, "Expected s1 and s2 to be equal: '%s' == '%s'\n", dbgstrx(s1), dbgstrx(s2));
119
120 s1.MakeUpper(); // Does not modify s2
121 ok(s1[0] == _X('S'), "Expected s1[0] to be S, was: %c\n", (char)s1[0]);
122 ok(s2[0] == _X('s'), "Expected s2[0] to be s, was: %c\n", (char)s2[0]);
123
124 ok(s1 == _X("SOME TEXT 1!"), "Expected s1 to be 'SOME TEXT 1!', was: %s\n", dbgstrx(s1));
125
126 CStringX s3 = s1.MakeLower();
127 ok(s1 == _X("some text 1!"), "Expected s1 to be 'some text 1!', was: %s\n", dbgstrx(s1));
128 ok(s1 == s3, "Expected s1 and s3 to be equal: '%s' == '%s'\n", dbgstrx(s1), dbgstrx(s3));
129 }
130
131
132 TEST_NAMEX(find)
133 {
134 CStringX s(_X("adbcdef"));
135 int n = s.Find(_X('c'));
136 ok(n == 3, "Expected n to be 2, was %i\n", n);
137 n = s.Find(_X("de"));
138 ok(n == 4, "Expected n to be 4, was %i\n", n);
139
140 CStringX str(_X("The waves are still"));
141 n = str.Find(_X('e'), 5);
142 ok(n == 7, "Expected n to be 7, was %i\n", n);
143 n = str.Find(_X('e'), 7);
144 ok(n == 7, "Expected n to be 7, was %i\n", n);
145
146 s = _X("abcdefGHIJKLMNop");
147 n = s.FindOneOf(_X("Nd"));
148 ok(n == 3, "Expected n to be 3, was %i\n", n);
149 n = s.FindOneOf(_X("Np"));
150 ok(n == 13, "Expected n to be 13, was %i\n", n);
151
152 n = str.ReverseFind(_X('l'));
153 ok(n == 18, "Expected n to be 18, was %i\n", n);
154
155 n = str.ReverseFind(_X('e'));
156 ok(n == 12, "Expected n to be 12, was %i\n", n);
157 }
158
159
160 void WriteString(const XCHAR* pstrFormat, ...)
161 {
162 CStringX str;
163
164 va_list args;
165 va_start(args, pstrFormat);
166
167 str.FormatV(pstrFormat, args);
168 va_end(args);
169
170 ok(str == _X("10e 1351l"), "Expected str to be '10e 1351l', was: %s\n", dbgstrx(str));
171 }
172
173 TEST_NAMEX(format)
174 {
175 CStringX str;
176
177 str.Format(_X("FP: %.2f"), 12345.12345);
178 ok(str == _X("FP: 12345.12"), "Expected str to be 'FP: 12345.12', was: %s\n", dbgstrx(str));
179
180 str.Format(_X("int: %.6d"), 35);
181 ok(str == _X("int: 000035"), "Expected str to be 'int: 000035', was: %s\n", dbgstrx(str));
182
183 WriteString(_X("%de %dl"), 10, 1351);
184 }
185
186
187 TEST_NAMEX(substr)
188 {
189 CStringX s(_X("abcdef"));
190
191 CStringX m = s.Mid(2, 3);
192 ok(m == _X("cde"), "Expected str to be 'cde', was: %s\n", dbgstrx(m));
193 ok(m.GetLength() == 3, "Expected GetLength() to be 3, was: %i\n", m.GetLength());
194
195 m = s.Mid(-5, 3);
196 ok(m == _X("abc"), "Expected str to be 'abc', was: %s\n", dbgstrx(m));
197 ok(m.GetLength() == 3, "Expected GetLength() to be 3, was: %i\n", m.GetLength());
198
199 m = s.Mid(3, 20);
200 ok(m == _X("def"), "Expected str to be 'def', was: %s\n", dbgstrx(m));
201 ok(m.GetLength() == 3, "Expected GetLength() to be 3, was: %i\n", m.GetLength());
202
203 m = s.Mid(3, -1);
204 ok(m == _X(""), "Expected str to be '', was: %s\n", dbgstrx(m));
205 ok(m.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", m.GetLength());
206
207 m = s.Mid(2);
208 ok(m == _X("cdef"), "Expected str to be 'cdef', was: %s\n", dbgstrx(m));
209 ok(m.GetLength() == 4, "Expected GetLength() to be 4, was: %i\n", m.GetLength());
210
211 m = s.Mid(-3);
212 ok(m == _X("abcdef"), "Expected str to be 'abcdef', was: %s\n", dbgstrx(m));
213 ok(m.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", m.GetLength());
214
215 m = s.Mid(20);
216 ok(m == _X(""), "Expected str to be '', was: %s\n", dbgstrx(m));
217 ok(m.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", m.GetLength());
218
219 m = s.Left(2);
220 ok(m == _X("ab"), "Expected str to be 'ab', was: %s\n", dbgstrx(m));
221 ok(m.GetLength() == 2, "Expected GetLength() to be 2, was: %i\n", m.GetLength());
222
223 m = s.Left(40);
224 ok(m == _X("abcdef"), "Expected str to be 'abcdef', was: %s\n", dbgstrx(m));
225 ok(m.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", m.GetLength());
226
227 m = s.Left(-10);
228 ok(m == _X(""), "Expected str to be '', was: %s\n", dbgstrx(m));
229 ok(m.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", m.GetLength());
230
231 m = s.Right(2);
232 ok(m == _X("ef"), "Expected str to be 'ef', was: %s\n", dbgstrx(m));
233 ok(m.GetLength() == 2, "Expected GetLength() to be 2, was: %i\n", m.GetLength());
234
235 m = s.Right(-40);
236 ok(m == _X(""), "Expected str to be '', was: %s\n", dbgstrx(m));
237 ok(m.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", m.GetLength());
238
239 m = s.Right(99);
240 ok(m == _X("abcdef"), "Expected str to be 'abcdef', was: %s\n", dbgstrx(m));
241 ok(m.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", m.GetLength());
242 }
243
244
245 TEST_NAMEX(replace)
246 {
247 CStringX str(_X("abcde"));
248 int n = str.Replace(_X("b"), _X("bx"));
249 ok(n == 1, "Expected n to be 1, was %i\n", n);
250 ok(str == _X("abxcde"), "Expected str to be 'abxcde', was: %s\n", dbgstrx(str));
251 ok(str.GetLength() == 6, "Expected GetLength() to be 6, was: %i\n", str.GetLength());
252
253 CStringX strBang(_X("The quick brown fox is lazy today of all days"));
254
255 n = strBang.Replace(_X("is"), _X("was"));
256 ok(n == 1, "Expected n to be 1, was %i\n", n);
257 ok(strBang == _X("The quick brown fox was lazy today of all days"),
258 "Expected str to be 'The quick brown fox was lazy today of all days', was: %s\n", dbgstrx(strBang));
259 ok(strBang.GetLength() == 46, "Expected GetLength() to be 46, was: %i\n", strBang.GetLength());
260
261 n = strBang.Replace(_X("is"), _X("was"));
262 ok(n == 0, "Expected n to be 0, was %i\n", n);
263 ok(strBang == _X("The quick brown fox was lazy today of all days"),
264 "Expected str to be 'The quick brown fox was lazy today of all days', was: %s\n", dbgstrx(strBang));
265 ok(strBang.GetLength() == 46, "Expected GetLength() to be 46, was: %i\n", strBang.GetLength());
266
267 n = strBang.Replace(_X('o'), _X('0'));
268 ok(n == 4, "Expected n to be 4, was %i\n", n);
269 ok(strBang == _X("The quick br0wn f0x was lazy t0day 0f all days"),
270 "Expected str to be 'The quick br0wn f0x was lazy t0day 0f all days', was: %s\n", dbgstrx(strBang));
271 ok(strBang.GetLength() == 46, "Expected GetLength() to be 46, was: %i\n", strBang.GetLength());
272
273 n = strBang.Replace(_X('o'), _X('0'));
274 ok(n == 0, "Expected n to be 0, was %i\n", n);
275 ok(strBang == _X("The quick br0wn f0x was lazy t0day 0f all days"),
276 "Expected str to be 'The quick br0wn f0x was lazy t0day 0f all days', was: %s\n", dbgstrx(strBang));
277 ok(strBang.GetLength() == 46, "Expected GetLength() to be 46, was: %i\n", strBang.GetLength());
278
279 n = strBang.Replace(_X("y "), _X("y, "));
280 ok(n == 2, "Expected n to be 2, was %i\n", n);
281 ok(strBang == _X("The quick br0wn f0x was lazy, t0day, 0f all days"),
282 "Expected str to be 'The quick br0wn f0x was lazy, t0day, 0f all days', was: %s\n", dbgstrx(strBang));
283 ok(strBang.GetLength() == 48, "Expected GetLength() to be 48, was: %i\n", strBang.GetLength());
284
285 n = strBang.Replace(_X(", 0f all days"), _X(""));
286 ok(n == 1, "Expected n to be 1, was %i\n", n);
287 ok(strBang == _X("The quick br0wn f0x was lazy, t0day"),
288 "Expected str to be 'The quick br0wn f0x was lazy, t0day', was: %s\n", dbgstrx(strBang));
289 ok(strBang.GetLength() == 35, "Expected GetLength() to be 35, was: %i\n", strBang.GetLength());
290
291 n = strBang.Replace(_X(" lazy, "), _X(" fast "));
292 ok(n == 1, "Expected n to be 1, was %i\n", n);
293 ok(strBang == _X("The quick br0wn f0x was fast t0day"),
294 "Expected str to be 'The quick br0wn f0x was fast t0day', was: %s\n", dbgstrx(strBang));
295 ok(strBang.GetLength() == 34, "Expected GetLength() to be 34, was: %i\n", strBang.GetLength());
296
297 n = strBang.Replace(_X("The "), _X(""));
298 ok(n == 1, "Expected n to be 1, was %i\n", n);
299 ok(strBang == _X("quick br0wn f0x was fast t0day"),
300 "Expected str to be 'quick br0wn f0x was fast t0day', was: %s\n", dbgstrx(strBang));
301 ok(strBang.GetLength() == 30, "Expected GetLength() to be 30, was: %i\n", strBang.GetLength());
302
303 n = strBang.Replace(_X(" t0day"), _X(""));
304 ok(n == 1, "Expected n to be 1, was %i\n", n);
305 ok(strBang == _X("quick br0wn f0x was fast"),
306 "Expected str to be 'quick br0wn f0x was fast', was: %s\n", dbgstrx(strBang));
307 ok(strBang.GetLength() == 24, "Expected GetLength() to be 24, was: %i\n", strBang.GetLength());
308
309 n = strBang.Replace(_X("quick"), _X("The fast, quick"));
310 ok(n == 1, "Expected n to be 1, was %i\n", n);
311 ok(strBang == _X("The fast, quick br0wn f0x was fast"),
312 "Expected str to be 'The fast, quick br0wn f0x was fast', was: %s\n", dbgstrx(strBang));
313 ok(strBang.GetLength() == 34, "Expected GetLength() to be 34, was: %i\n", strBang.GetLength());
314 }
315
316
317 TEST_NAMEX(trim)
318 {
319 CStringX str;
320 str = _X(" \t\r\n******Trim some text!?!?!?!?!\n\r\t ");
321
322 str.TrimLeft();
323 ok(str == _X("******Trim some text!?!?!?!?!\n\r\t "), "Expected str to be '******Trim some text!?!?!?!?!\n\r\t ', was: %s\n", dbgstrx(str));
324 ok(str.GetLength() == 33, "Expected GetLength() to be 33, was: %i\n", str.GetLength());
325
326 str.TrimRight();
327 ok(str == _X("******Trim some text!?!?!?!?!"), "Expected str to be '******Trim some text!?!?!?!?!', was: %s\n", dbgstrx(str));
328 ok(str.GetLength() == 29, "Expected GetLength() to be 29, was: %i\n", str.GetLength());
329
330 CStringX str2 = str.Trim(_X("?!*"));
331 ok(str2 == _X("Trim some text"), "Expected str to be 'Trim some text', was: %s\n", dbgstrx(str2));
332 ok(str2.GetLength() == 14, "Expected GetLength() to be 14, was: %i\n", str2.GetLength());
333
334 str = _X("\t\t ****Trim some text!");
335 str2 = str.TrimLeft(_X("\t *"));
336 ok(str2 == _X("Trim some text!"), "Expected str to be 'Trim some text!', was: %s\n", dbgstrx(str2));
337 ok(str2.GetLength() == 15, "Expected GetLength() to be 15, was: %i\n", str2.GetLength());
338
339 str = _X("Trim some text!?!?!?!?!");
340 str2 = str.TrimRight(_X("?!"));
341 ok(str2 == _X("Trim some text"), "Expected str to be 'Trim some text', was: %s\n", dbgstrx(str2));
342 ok(str2.GetLength() == 14, "Expected GetLength() to be 14, was: %i\n", str2.GetLength());
343
344 str = _X("\t\t\t\t\t");
345 str2 = str.TrimLeft();
346 ok(str2 == _X(""), "Expected str2 to be '', was: %s\n", dbgstrx(str2));
347 ok(str2.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", str2.GetLength());
348
349 str = _X("\t\t\t\t\t");
350 str2 = str.TrimRight();
351 ok(str2 == _X(""), "Expected str2 to be '', was: %s\n", dbgstrx(str2));
352 ok(str2.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", str2.GetLength());
353
354 str = _X("\t\t\t\t\t");
355 str2 = str.Trim();
356 ok(str2 == _X(""), "Expected str2 to be '', was: %s\n", dbgstrx(str2));
357 ok(str2.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", str2.GetLength());
358 }
359
360 TEST_NAMEX(env)
361 {
362 CStringX test;
363 BOOL ret;
364 ok(test.IsEmpty() == true, "Expected test to be empty\n");
365 ok(test.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", test.GetLength());
366 ok(test.GetAllocLength() == 0, "Expected GetAllocLength() to be 0, was: %i\n", test.GetAllocLength());
367
368 XCHAR buf[512];
369 GetWindowsDirectoryX(buf, _countof(buf));
370
371 ret = test.GetEnvironmentVariable(_X("SystemDrive"));
372 ok(!!ret, "Expected %%SystemDrive%% to exist\n");
373 ok(test.IsEmpty() == false, "Expected test to have a valid result\n");
374 ok(test.GetLength() == 2, "Expected GetLength() to be 2, was: %i\n", test.GetLength());
375 if (test.GetLength() == 2)
376 {
377 ok(test[0] == buf[0], "Expected test[0] to equal buf[0], was: %c, %c\n", test[0], buf[0]);
378 ok(test[1] == buf[1], "Expected test[1] to equal buf[1], was: %c, %c\n", test[1], buf[1]);
379 }
380
381 ret = test.GetEnvironmentVariable(_X("SystemRoot"));
382 ok(!!ret, "Expected %%SystemRoot%% to exist\n");
383 ok(test.IsEmpty() == false, "Expected test to have a valid result\n");
384 ok(test == buf, "Expected test to be %s, was %s\n", dbgstrx(buf), dbgstrx(test));
385
386 ret = test.GetEnvironmentVariable(_X("some non existing env var"));
387 ok(!ret, "Expected %%some non existing env var%% to not exist\n");
388 ok(test.IsEmpty() == true, "Expected test to be empty\n");
389 ok(test.GetLength() == 0, "Expected GetLength() to be 0, was: %i\n", test.GetLength());
390 }