Merge to XFA: Fix comparison of CFX_ByteString and CFX_WideString.
[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 #define ByteStringLiteral(str) CFX_ByteString(FX_BSTRC(str))
253
254 TEST(fxcrt, WideStringUTF16LE_Encode) {
255   struct UTF16LEEncodeCase {
256     CFX_WideString ws;
257     CFX_ByteString bs;
258   } utf16le_encode_cases[] = {
259     { L"", ByteStringLiteral("\0\0") },
260     { L"abc", ByteStringLiteral("a\0b\0c\0\0\0") },
261     { L"abcdef", ByteStringLiteral("a\0b\0c\0d\0e\0f\0\0\0") },
262     { L"abc\0def", ByteStringLiteral("a\0b\0c\0\0\0") },
263     { L"\xaabb\xccdd", ByteStringLiteral("\xbb\xaa\xdd\xcc\0\0") },
264     { L"\x3132\x6162", ByteStringLiteral("\x32\x31\x62\x61\0\0") },
265   };
266
267   for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) {
268     EXPECT_EQ(utf16le_encode_cases[i].bs,
269         utf16le_encode_cases[i].ws.UTF16LE_Encode())
270         << " for case number " << i;
271   }
272 }
273
274 TEST(fxcrt, WideStringCOperatorSubscript) {
275     // CFX_WideStringC includes the NUL terminator for non-empty strings.
276     CFX_WideStringC abc(L"abc");
277     EXPECT_EQ(L'a', abc[0]);
278     EXPECT_EQ(L'b', abc[1]);
279     EXPECT_EQ(L'c', abc[2]);
280     EXPECT_EQ(L'\0', abc[3]);
281 }
282
283 TEST(fxcrt, WideStringCOperatorLT) {
284     CFX_WideStringC empty;
285     CFX_WideStringC a(L"a");
286     CFX_WideStringC abc(L"\x0110qq");  // Comes before despite endianness.
287     CFX_WideStringC def(L"\x1001qq");  // Comes after despite endianness.
288
289     EXPECT_FALSE(empty < empty);
290     EXPECT_FALSE(a < a);
291     EXPECT_FALSE(abc < abc);
292     EXPECT_FALSE(def < def);
293
294     EXPECT_TRUE(empty < a);
295     EXPECT_FALSE(a < empty);
296
297     EXPECT_TRUE(empty < abc);
298     EXPECT_FALSE(abc < empty);
299
300     EXPECT_TRUE(empty < def);
301     EXPECT_FALSE(def < empty);
302
303     EXPECT_TRUE(a < abc);
304     EXPECT_FALSE(abc < a);
305
306     EXPECT_TRUE(a < def);
307     EXPECT_FALSE(def < a);
308
309     EXPECT_TRUE(abc < def);
310     EXPECT_FALSE(def < abc);
311 }
312
313 TEST(fxcrt, WideStringCOperatorEQ) {
314     CFX_WideStringC wide_string_c(L"hello");
315     EXPECT_TRUE(wide_string_c == wide_string_c);
316
317     CFX_WideStringC wide_string_c_same1(L"hello");
318     EXPECT_TRUE(wide_string_c == wide_string_c_same1);
319     EXPECT_TRUE(wide_string_c_same1 == wide_string_c);
320
321     CFX_WideStringC wide_string_c_same2(wide_string_c);
322     EXPECT_TRUE(wide_string_c == wide_string_c_same2);
323     EXPECT_TRUE(wide_string_c_same2 == wide_string_c);
324
325     CFX_WideStringC wide_string_c1(L"he");
326     CFX_WideStringC wide_string_c2(L"hellp");
327     CFX_WideStringC wide_string_c3(L"hellod");
328     EXPECT_FALSE(wide_string_c == wide_string_c1);
329     EXPECT_FALSE(wide_string_c == wide_string_c2);
330     EXPECT_FALSE(wide_string_c == wide_string_c3);
331     EXPECT_FALSE(wide_string_c1 == wide_string_c);
332     EXPECT_FALSE(wide_string_c2 == wide_string_c);
333     EXPECT_FALSE(wide_string_c3 == wide_string_c);
334
335     CFX_WideString wide_string_same1(L"hello");
336     EXPECT_TRUE(wide_string_c == wide_string_same1);
337     EXPECT_TRUE(wide_string_same1 == wide_string_c);
338
339     CFX_WideString wide_string1(L"he");
340     CFX_WideString wide_string2(L"hellp");
341     CFX_WideString wide_string3(L"hellod");
342     EXPECT_FALSE(wide_string_c == wide_string1);
343     EXPECT_FALSE(wide_string_c == wide_string2);
344     EXPECT_FALSE(wide_string_c == wide_string3);
345     EXPECT_FALSE(wide_string1 == wide_string_c);
346     EXPECT_FALSE(wide_string2 == wide_string_c);
347     EXPECT_FALSE(wide_string3 == wide_string_c);
348
349     const wchar_t* c_string_same1 = L"hello";
350     EXPECT_TRUE(wide_string_c == c_string_same1);
351     EXPECT_TRUE(c_string_same1 == wide_string_c);
352
353     const wchar_t* c_string1 = L"he";
354     const wchar_t* c_string2 = L"hellp";
355     const wchar_t* c_string3 = L"hellod";
356     EXPECT_FALSE(wide_string_c == c_string1);
357     EXPECT_FALSE(wide_string_c == c_string2);
358     EXPECT_FALSE(wide_string_c == c_string3);
359
360     EXPECT_FALSE(c_string1 == wide_string_c);
361     EXPECT_FALSE(c_string2 == wide_string_c);
362     EXPECT_FALSE(c_string3 == wide_string_c);
363 }
364
365 TEST(fxcrt, WideStringCOperatorNE) {
366     CFX_WideStringC wide_string_c(L"hello");
367     EXPECT_FALSE(wide_string_c != wide_string_c);
368
369     CFX_WideStringC wide_string_c_same1(L"hello");
370     EXPECT_FALSE(wide_string_c != wide_string_c_same1);
371     EXPECT_FALSE(wide_string_c_same1 != wide_string_c);
372
373     CFX_WideStringC wide_string_c_same2(wide_string_c);
374     EXPECT_FALSE(wide_string_c != wide_string_c_same2);
375     EXPECT_FALSE(wide_string_c_same2 != wide_string_c);
376
377     CFX_WideStringC wide_string_c1(L"he");
378     CFX_WideStringC wide_string_c2(L"hellp");
379     CFX_WideStringC wide_string_c3(L"hellod");
380     EXPECT_TRUE(wide_string_c != wide_string_c1);
381     EXPECT_TRUE(wide_string_c != wide_string_c2);
382     EXPECT_TRUE(wide_string_c != wide_string_c3);
383     EXPECT_TRUE(wide_string_c1 != wide_string_c);
384     EXPECT_TRUE(wide_string_c2 != wide_string_c);
385     EXPECT_TRUE(wide_string_c3 != wide_string_c);
386
387     CFX_WideString wide_string_same1(L"hello");
388     EXPECT_FALSE(wide_string_c != wide_string_same1);
389     EXPECT_FALSE(wide_string_same1 != wide_string_c);
390
391     CFX_WideString wide_string1(L"he");
392     CFX_WideString wide_string2(L"hellp");
393     CFX_WideString wide_string3(L"hellod");
394     EXPECT_TRUE(wide_string_c != wide_string1);
395     EXPECT_TRUE(wide_string_c != wide_string2);
396     EXPECT_TRUE(wide_string_c != wide_string3);
397     EXPECT_TRUE(wide_string1 != wide_string_c);
398     EXPECT_TRUE(wide_string2 != wide_string_c);
399     EXPECT_TRUE(wide_string3 != wide_string_c);
400
401     const wchar_t* c_string_same1 = L"hello";
402     EXPECT_FALSE(wide_string_c != c_string_same1);
403     EXPECT_FALSE(c_string_same1 != wide_string_c);
404
405     const wchar_t* c_string1 = L"he";
406     const wchar_t* c_string2 = L"hellp";
407     const wchar_t* c_string3 = L"hellod";
408     EXPECT_TRUE(wide_string_c != c_string1);
409     EXPECT_TRUE(wide_string_c != c_string2);
410     EXPECT_TRUE(wide_string_c != c_string3);
411
412     EXPECT_TRUE(c_string1 != wide_string_c);
413     EXPECT_TRUE(c_string2 != wide_string_c);
414     EXPECT_TRUE(c_string3 != wide_string_c);
415 }