Fix some clang warnings with -Wmissing-braces in pdfium.
[pdfium.git] / core / src / fxcrt / fx_basic_wstring_unittest.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "../../../testing/fx_string_testhelpers.h"
7 #include "../../include/fxcrt/fx_basic.h"
8
9 TEST(fxcrt, WideStringOperatorSubscript) {
10     // CFX_WideString includes the NUL terminator for non-empty strings.
11     CFX_WideString abc(L"abc");
12     EXPECT_EQ(L'a', abc[0]);
13     EXPECT_EQ(L'b', abc[1]);
14     EXPECT_EQ(L'c', abc[2]);
15     EXPECT_EQ(L'\0', abc[3]);
16 }
17
18 TEST(fxcrt, WideStringOperatorLT) {
19     CFX_WideString empty;
20     CFX_WideString a(L"a");
21     CFX_WideString abc(L"\x0110qq");  // Comes before despite endianness.
22     CFX_WideString def(L"\x1001qq");  // Comes after despite endianness.
23
24     EXPECT_FALSE(empty < empty);
25     EXPECT_FALSE(a < a);
26     EXPECT_FALSE(abc < abc);
27     EXPECT_FALSE(def < def);
28
29     EXPECT_TRUE(empty < a);
30     EXPECT_FALSE(a < empty);
31
32     EXPECT_TRUE(empty < abc);
33     EXPECT_FALSE(abc < empty);
34
35     EXPECT_TRUE(empty < def);
36     EXPECT_FALSE(def < empty);
37
38     EXPECT_TRUE(a < abc);
39     EXPECT_FALSE(abc < a);
40
41     EXPECT_TRUE(a < def);
42     EXPECT_FALSE(def < a);
43
44     EXPECT_TRUE(abc < def);
45     EXPECT_FALSE(def < abc);
46 }
47
48 TEST(fxcrt, WideStringOperatorEQ) {
49     CFX_WideString null_string;
50     EXPECT_TRUE(null_string == null_string);
51
52     CFX_WideString empty_string(L"");
53     EXPECT_TRUE(empty_string == empty_string);
54     EXPECT_TRUE(empty_string == null_string);
55     EXPECT_TRUE(null_string == empty_string);
56
57     CFX_WideString deleted_string(L"hello");
58     deleted_string.Delete(0, 5);
59     EXPECT_TRUE(deleted_string == deleted_string);
60     EXPECT_TRUE(deleted_string == null_string);
61     EXPECT_TRUE(deleted_string == empty_string);
62     EXPECT_TRUE(null_string == deleted_string);
63     EXPECT_TRUE(null_string == empty_string);
64
65     CFX_WideString wide_string(L"hello");
66     EXPECT_TRUE(wide_string == wide_string);
67     EXPECT_FALSE(wide_string == null_string);
68     EXPECT_FALSE(wide_string == empty_string);
69     EXPECT_FALSE(wide_string == deleted_string);
70     EXPECT_FALSE(null_string == wide_string);
71     EXPECT_FALSE(empty_string == wide_string);
72     EXPECT_FALSE(deleted_string == wide_string);
73
74     CFX_WideString wide_string_same1(L"hello");
75     EXPECT_TRUE(wide_string == wide_string_same1);
76     EXPECT_TRUE(wide_string_same1 == wide_string);
77
78     CFX_WideString wide_string_same2(wide_string);
79     EXPECT_TRUE(wide_string == wide_string_same2);
80     EXPECT_TRUE(wide_string_same2 == wide_string);
81
82     CFX_WideString wide_string1(L"he");
83     CFX_WideString wide_string2(L"hellp");
84     CFX_WideString wide_string3(L"hellod");
85     EXPECT_FALSE(wide_string == wide_string1);
86     EXPECT_FALSE(wide_string == wide_string2);
87     EXPECT_FALSE(wide_string == wide_string3);
88     EXPECT_FALSE(wide_string1 == wide_string);
89     EXPECT_FALSE(wide_string2 == wide_string);
90     EXPECT_FALSE(wide_string3 == wide_string);
91
92     CFX_WideStringC null_string_c;
93     CFX_WideStringC empty_string_c(L"");
94     EXPECT_TRUE(null_string == null_string_c);
95     EXPECT_TRUE(null_string == empty_string_c);
96     EXPECT_TRUE(empty_string == null_string_c);
97     EXPECT_TRUE(empty_string == empty_string_c);
98     EXPECT_TRUE(deleted_string == null_string_c);
99     EXPECT_TRUE(deleted_string == empty_string_c);
100     EXPECT_TRUE(null_string_c == null_string);
101     EXPECT_TRUE(empty_string_c == null_string);
102     EXPECT_TRUE(null_string_c == empty_string);
103     EXPECT_TRUE(empty_string_c == empty_string);
104     EXPECT_TRUE(null_string_c == deleted_string);
105     EXPECT_TRUE(empty_string_c == deleted_string);
106
107     CFX_WideStringC wide_string_c_same1(L"hello");
108     EXPECT_TRUE(wide_string == wide_string_c_same1);
109     EXPECT_TRUE(wide_string_c_same1 == wide_string);
110
111     CFX_WideStringC wide_string_c1(L"he");
112     CFX_WideStringC wide_string_c2(L"hellp");
113     CFX_WideStringC wide_string_c3(L"hellod");
114     EXPECT_FALSE(wide_string == wide_string_c1);
115     EXPECT_FALSE(wide_string == wide_string_c2);
116     EXPECT_FALSE(wide_string == wide_string_c3);
117     EXPECT_FALSE(wide_string_c1 == wide_string);
118     EXPECT_FALSE(wide_string_c2 == wide_string);
119     EXPECT_FALSE(wide_string_c3 == wide_string);
120
121     const wchar_t* c_null_string = nullptr;
122     const wchar_t* c_empty_string = L"";
123     EXPECT_TRUE(null_string == c_null_string);
124     EXPECT_TRUE(null_string == c_empty_string);
125     EXPECT_TRUE(empty_string == c_null_string);
126     EXPECT_TRUE(empty_string == c_empty_string);
127     EXPECT_TRUE(deleted_string == c_null_string);
128     EXPECT_TRUE(deleted_string == c_empty_string);
129     EXPECT_TRUE(c_null_string == null_string);
130     EXPECT_TRUE(c_empty_string == null_string);
131     EXPECT_TRUE(c_null_string == empty_string);
132     EXPECT_TRUE(c_empty_string == empty_string);
133     EXPECT_TRUE(c_null_string == deleted_string);
134     EXPECT_TRUE(c_empty_string == deleted_string);
135
136     const wchar_t* c_string_same1 = L"hello";
137     EXPECT_TRUE(wide_string == c_string_same1);
138     EXPECT_TRUE(c_string_same1 == wide_string);
139
140     const wchar_t* c_string1 = L"he";
141     const wchar_t* c_string2 = L"hellp";
142     const wchar_t* c_string3 = L"hellod";
143     EXPECT_FALSE(wide_string == c_string1);
144     EXPECT_FALSE(wide_string == c_string2);
145     EXPECT_FALSE(wide_string == c_string3);
146     EXPECT_FALSE(c_string1 == wide_string);
147     EXPECT_FALSE(c_string2 == wide_string);
148     EXPECT_FALSE(c_string3 == wide_string);
149 }
150
151 TEST(fxcrt, WideStringOperatorNE) {
152     CFX_WideString null_string;
153     EXPECT_FALSE(null_string != null_string);
154
155     CFX_WideString empty_string(L"");
156     EXPECT_FALSE(empty_string != empty_string);
157     EXPECT_FALSE(empty_string != null_string);
158     EXPECT_FALSE(null_string != empty_string);
159
160     CFX_WideString deleted_string(L"hello");
161     deleted_string.Delete(0, 5);
162     EXPECT_FALSE(deleted_string != deleted_string);
163     EXPECT_FALSE(deleted_string != null_string);
164     EXPECT_FALSE(deleted_string != empty_string);
165     EXPECT_FALSE(null_string != deleted_string);
166     EXPECT_FALSE(null_string != empty_string);
167
168     CFX_WideString wide_string(L"hello");
169     EXPECT_FALSE(wide_string != wide_string);
170     EXPECT_TRUE(wide_string != null_string);
171     EXPECT_TRUE(wide_string != empty_string);
172     EXPECT_TRUE(wide_string != deleted_string);
173     EXPECT_TRUE(null_string != wide_string);
174     EXPECT_TRUE(empty_string != wide_string);
175     EXPECT_TRUE(deleted_string != wide_string);
176
177     CFX_WideString wide_string_same1(L"hello");
178     EXPECT_FALSE(wide_string != wide_string_same1);
179     EXPECT_FALSE(wide_string_same1 != wide_string);
180
181     CFX_WideString wide_string_same2(wide_string);
182     EXPECT_FALSE(wide_string != wide_string_same2);
183     EXPECT_FALSE(wide_string_same2 != wide_string);
184
185     CFX_WideString wide_string1(L"he");
186     CFX_WideString wide_string2(L"hellp");
187     CFX_WideString wide_string3(L"hellod");
188     EXPECT_TRUE(wide_string != wide_string1);
189     EXPECT_TRUE(wide_string != wide_string2);
190     EXPECT_TRUE(wide_string != wide_string3);
191     EXPECT_TRUE(wide_string1 != wide_string);
192     EXPECT_TRUE(wide_string2 != wide_string);
193     EXPECT_TRUE(wide_string3 != wide_string);
194
195     CFX_WideStringC null_string_c;
196     CFX_WideStringC empty_string_c(L"");
197     EXPECT_FALSE(null_string != null_string_c);
198     EXPECT_FALSE(null_string != empty_string_c);
199     EXPECT_FALSE(empty_string != null_string_c);
200     EXPECT_FALSE(empty_string != empty_string_c);
201     EXPECT_FALSE(deleted_string != null_string_c);
202     EXPECT_FALSE(deleted_string != empty_string_c);
203     EXPECT_FALSE(null_string_c != null_string);
204     EXPECT_FALSE(empty_string_c != null_string);
205     EXPECT_FALSE(null_string_c != empty_string);
206     EXPECT_FALSE(empty_string_c != empty_string);
207
208     CFX_WideStringC wide_string_c_same1(L"hello");
209     EXPECT_FALSE(wide_string != wide_string_c_same1);
210     EXPECT_FALSE(wide_string_c_same1 != wide_string);
211
212     CFX_WideStringC wide_string_c1(L"he");
213     CFX_WideStringC wide_string_c2(L"hellp");
214     CFX_WideStringC wide_string_c3(L"hellod");
215     EXPECT_TRUE(wide_string != wide_string_c1);
216     EXPECT_TRUE(wide_string != wide_string_c2);
217     EXPECT_TRUE(wide_string != wide_string_c3);
218     EXPECT_TRUE(wide_string_c1 != wide_string);
219     EXPECT_TRUE(wide_string_c2 != wide_string);
220     EXPECT_TRUE(wide_string_c3 != wide_string);
221
222     const wchar_t* c_null_string = nullptr;
223     const wchar_t* c_empty_string = L"";
224     EXPECT_FALSE(null_string != c_null_string);
225     EXPECT_FALSE(null_string != c_empty_string);
226     EXPECT_FALSE(empty_string != c_null_string);
227     EXPECT_FALSE(empty_string != c_empty_string);
228     EXPECT_FALSE(deleted_string != c_null_string);
229     EXPECT_FALSE(deleted_string != c_empty_string);
230     EXPECT_FALSE(c_null_string != null_string);
231     EXPECT_FALSE(c_empty_string != null_string);
232     EXPECT_FALSE(c_null_string != empty_string);
233     EXPECT_FALSE(c_empty_string != empty_string);
234     EXPECT_FALSE(c_null_string != deleted_string);
235     EXPECT_FALSE(c_empty_string != deleted_string);
236
237     const wchar_t* c_string_same1 = L"hello";
238     EXPECT_FALSE(wide_string != c_string_same1);
239     EXPECT_FALSE(c_string_same1 != wide_string);
240
241     const wchar_t* c_string1 = L"he";
242     const wchar_t* c_string2 = L"hellp";
243     const wchar_t* c_string3 = L"hellod";
244     EXPECT_TRUE(wide_string != c_string1);
245     EXPECT_TRUE(wide_string != c_string2);
246     EXPECT_TRUE(wide_string != c_string3);
247     EXPECT_TRUE(c_string1 != wide_string);
248     EXPECT_TRUE(c_string2 != wide_string);
249     EXPECT_TRUE(c_string3 != wide_string);
250 }
251
252 TEST(fxcrt, WideStringConcatInPlace) {
253     CFX_WideString fred;
254     fred.ConcatInPlace(4, L"FRED");
255     EXPECT_EQ(L"FRED", fred);
256
257     fred.ConcatInPlace(2, L"DY");
258     EXPECT_EQ(L"FREDDY", fred);
259
260     fred.Delete(3, 3);
261     EXPECT_EQ(L"FRE", fred);
262
263     fred.ConcatInPlace(1, L"D");
264     EXPECT_EQ(L"FRED", fred);
265
266     CFX_WideString copy = fred;
267     fred.ConcatInPlace(2, L"DY");
268     EXPECT_EQ(L"FREDDY", fred);
269     EXPECT_EQ(L"FRED", copy);
270
271     // Test invalid arguments.
272     copy = fred;
273     fred.ConcatInPlace(-6, L"freddy");
274     CFX_WideString not_aliased(L"xxxxxx");
275     EXPECT_EQ(L"FREDDY", fred);
276     EXPECT_EQ(L"xxxxxx", not_aliased);
277 }
278
279 #define ByteStringLiteral(str) CFX_ByteString(FX_BSTRC(str))
280
281 TEST(fxcrt, WideStringUTF16LE_Encode) {
282   struct UTF16LEEncodeCase {
283     CFX_WideString ws;
284     CFX_ByteString bs;
285   } utf16le_encode_cases[] = {
286     { L"", ByteStringLiteral("\0\0") },
287     { L"abc", ByteStringLiteral("a\0b\0c\0\0\0") },
288     { L"abcdef", ByteStringLiteral("a\0b\0c\0d\0e\0f\0\0\0") },
289     { L"abc\0def", ByteStringLiteral("a\0b\0c\0\0\0") },
290     { L"\xaabb\xccdd", ByteStringLiteral("\xbb\xaa\xdd\xcc\0\0") },
291     { L"\x3132\x6162", ByteStringLiteral("\x32\x31\x62\x61\0\0") },
292   };
293
294   for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) {
295     EXPECT_EQ(utf16le_encode_cases[i].bs,
296         utf16le_encode_cases[i].ws.UTF16LE_Encode())
297         << " for case number " << i;
298   }
299 }
300
301 TEST(fxcrt, WideStringCOperatorSubscript) {
302     // CFX_WideStringC includes the NUL terminator for non-empty strings.
303     CFX_WideStringC abc(L"abc");
304     EXPECT_EQ(L'a', abc[0]);
305     EXPECT_EQ(L'b', abc[1]);
306     EXPECT_EQ(L'c', abc[2]);
307     EXPECT_EQ(L'\0', abc[3]);
308 }
309
310 TEST(fxcrt, WideStringCOperatorLT) {
311     CFX_WideStringC empty;
312     CFX_WideStringC a(L"a");
313     CFX_WideStringC abc(L"\x0110qq");  // Comes before despite endianness.
314     CFX_WideStringC def(L"\x1001qq");  // Comes after despite endianness.
315
316     EXPECT_FALSE(empty < empty);
317     EXPECT_FALSE(a < a);
318     EXPECT_FALSE(abc < abc);
319     EXPECT_FALSE(def < def);
320
321     EXPECT_TRUE(empty < a);
322     EXPECT_FALSE(a < empty);
323
324     EXPECT_TRUE(empty < abc);
325     EXPECT_FALSE(abc < empty);
326
327     EXPECT_TRUE(empty < def);
328     EXPECT_FALSE(def < empty);
329
330     EXPECT_TRUE(a < abc);
331     EXPECT_FALSE(abc < a);
332
333     EXPECT_TRUE(a < def);
334     EXPECT_FALSE(def < a);
335
336     EXPECT_TRUE(abc < def);
337     EXPECT_FALSE(def < abc);
338 }
339
340 TEST(fxcrt, WideStringCOperatorEQ) {
341     CFX_WideStringC wide_string_c(L"hello");
342     EXPECT_TRUE(wide_string_c == wide_string_c);
343
344     CFX_WideStringC wide_string_c_same1(L"hello");
345     EXPECT_TRUE(wide_string_c == wide_string_c_same1);
346     EXPECT_TRUE(wide_string_c_same1 == wide_string_c);
347
348     CFX_WideStringC wide_string_c_same2(wide_string_c);
349     EXPECT_TRUE(wide_string_c == wide_string_c_same2);
350     EXPECT_TRUE(wide_string_c_same2 == wide_string_c);
351
352     CFX_WideStringC wide_string_c1(L"he");
353     CFX_WideStringC wide_string_c2(L"hellp");
354     CFX_WideStringC wide_string_c3(L"hellod");
355     EXPECT_FALSE(wide_string_c == wide_string_c1);
356     EXPECT_FALSE(wide_string_c == wide_string_c2);
357     EXPECT_FALSE(wide_string_c == wide_string_c3);
358     EXPECT_FALSE(wide_string_c1 == wide_string_c);
359     EXPECT_FALSE(wide_string_c2 == wide_string_c);
360     EXPECT_FALSE(wide_string_c3 == wide_string_c);
361
362     CFX_WideString wide_string_same1(L"hello");
363     EXPECT_TRUE(wide_string_c == wide_string_same1);
364     EXPECT_TRUE(wide_string_same1 == wide_string_c);
365
366     CFX_WideString wide_string1(L"he");
367     CFX_WideString wide_string2(L"hellp");
368     CFX_WideString wide_string3(L"hellod");
369     EXPECT_FALSE(wide_string_c == wide_string1);
370     EXPECT_FALSE(wide_string_c == wide_string2);
371     EXPECT_FALSE(wide_string_c == wide_string3);
372     EXPECT_FALSE(wide_string1 == wide_string_c);
373     EXPECT_FALSE(wide_string2 == wide_string_c);
374     EXPECT_FALSE(wide_string3 == wide_string_c);
375
376     const wchar_t* c_string_same1 = L"hello";
377     EXPECT_TRUE(wide_string_c == c_string_same1);
378     EXPECT_TRUE(c_string_same1 == wide_string_c);
379
380     const wchar_t* c_string1 = L"he";
381     const wchar_t* c_string2 = L"hellp";
382     const wchar_t* c_string3 = L"hellod";
383     EXPECT_FALSE(wide_string_c == c_string1);
384     EXPECT_FALSE(wide_string_c == c_string2);
385     EXPECT_FALSE(wide_string_c == c_string3);
386
387     EXPECT_FALSE(c_string1 == wide_string_c);
388     EXPECT_FALSE(c_string2 == wide_string_c);
389     EXPECT_FALSE(c_string3 == wide_string_c);
390 }
391
392 TEST(fxcrt, WideStringCOperatorNE) {
393     CFX_WideStringC wide_string_c(L"hello");
394     EXPECT_FALSE(wide_string_c != wide_string_c);
395
396     CFX_WideStringC wide_string_c_same1(L"hello");
397     EXPECT_FALSE(wide_string_c != wide_string_c_same1);
398     EXPECT_FALSE(wide_string_c_same1 != wide_string_c);
399
400     CFX_WideStringC wide_string_c_same2(wide_string_c);
401     EXPECT_FALSE(wide_string_c != wide_string_c_same2);
402     EXPECT_FALSE(wide_string_c_same2 != wide_string_c);
403
404     CFX_WideStringC wide_string_c1(L"he");
405     CFX_WideStringC wide_string_c2(L"hellp");
406     CFX_WideStringC wide_string_c3(L"hellod");
407     EXPECT_TRUE(wide_string_c != wide_string_c1);
408     EXPECT_TRUE(wide_string_c != wide_string_c2);
409     EXPECT_TRUE(wide_string_c != wide_string_c3);
410     EXPECT_TRUE(wide_string_c1 != wide_string_c);
411     EXPECT_TRUE(wide_string_c2 != wide_string_c);
412     EXPECT_TRUE(wide_string_c3 != wide_string_c);
413
414     CFX_WideString wide_string_same1(L"hello");
415     EXPECT_FALSE(wide_string_c != wide_string_same1);
416     EXPECT_FALSE(wide_string_same1 != wide_string_c);
417
418     CFX_WideString wide_string1(L"he");
419     CFX_WideString wide_string2(L"hellp");
420     CFX_WideString wide_string3(L"hellod");
421     EXPECT_TRUE(wide_string_c != wide_string1);
422     EXPECT_TRUE(wide_string_c != wide_string2);
423     EXPECT_TRUE(wide_string_c != wide_string3);
424     EXPECT_TRUE(wide_string1 != wide_string_c);
425     EXPECT_TRUE(wide_string2 != wide_string_c);
426     EXPECT_TRUE(wide_string3 != wide_string_c);
427
428     const wchar_t* c_string_same1 = L"hello";
429     EXPECT_FALSE(wide_string_c != c_string_same1);
430     EXPECT_FALSE(c_string_same1 != wide_string_c);
431
432     const wchar_t* c_string1 = L"he";
433     const wchar_t* c_string2 = L"hellp";
434     const wchar_t* c_string3 = L"hellod";
435     EXPECT_TRUE(wide_string_c != c_string1);
436     EXPECT_TRUE(wide_string_c != c_string2);
437     EXPECT_TRUE(wide_string_c != c_string3);
438
439     EXPECT_TRUE(c_string1 != wide_string_c);
440     EXPECT_TRUE(c_string2 != wide_string_c);
441     EXPECT_TRUE(c_string3 != wide_string_c);
442 }