0672be6e73adbd510e5d8af40d3eaac18e5fd39b
[pdfium.git] / core / src / fxcrt / fx_basic_bstring_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, ByteStringOperatorSubscript) {
10     // CFX_ByteString includes the NUL terminator for non-empty strings.
11     CFX_ByteString abc("abc");
12     EXPECT_EQ('a', abc[0]);
13     EXPECT_EQ('b', abc[1]);
14     EXPECT_EQ('c', abc[2]);
15     EXPECT_EQ(0, abc[3]);
16 }
17
18 TEST(fxcrt, ByteStringOperatorLT) {
19     CFX_ByteString empty;
20     CFX_ByteString a("a");
21     CFX_ByteString abc("abc");
22     CFX_ByteString def("def");
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, ByteStringOperatorEQ) {
49     CFX_ByteString byte_string("hello");
50     ASSERT_TRUE(byte_string == byte_string);
51
52     CFX_ByteString byte_string_same1("hello");
53     ASSERT_TRUE(byte_string == byte_string_same1);
54     ASSERT_TRUE(byte_string_same1 == byte_string);
55
56     CFX_ByteString byte_string_same2(byte_string);
57     ASSERT_TRUE(byte_string == byte_string_same2);
58     ASSERT_TRUE(byte_string_same2 == byte_string);
59
60     CFX_ByteString byte_string1("he");
61     CFX_ByteString byte_string2("hellp");
62     CFX_ByteString byte_string3("hellod");
63     ASSERT_FALSE(byte_string == byte_string1);
64     ASSERT_FALSE(byte_string == byte_string2);
65     ASSERT_FALSE(byte_string == byte_string3);
66     ASSERT_FALSE(byte_string1 == byte_string);
67     ASSERT_FALSE(byte_string2 == byte_string);
68     ASSERT_FALSE(byte_string3 == byte_string);
69
70     CFX_ByteStringC byte_string_c_same1("hello");
71     ASSERT_TRUE(byte_string == byte_string_c_same1);
72     ASSERT_TRUE(byte_string_c_same1 == byte_string);
73
74     CFX_ByteStringC byte_string_c1("he");
75     CFX_ByteStringC byte_string_c2("hellp");
76     CFX_ByteStringC byte_string_c3("hellod");
77     ASSERT_FALSE(byte_string == byte_string_c1);
78     ASSERT_FALSE(byte_string == byte_string_c2);
79     ASSERT_FALSE(byte_string == byte_string_c3);
80     ASSERT_FALSE(byte_string_c1 == byte_string);
81     ASSERT_FALSE(byte_string_c2 == byte_string);
82     ASSERT_FALSE(byte_string_c3 == byte_string);
83
84     const char* c_string_same1 = "hello";
85     ASSERT_TRUE(byte_string == c_string_same1);
86 #if 0
87     // TODO(tsepez): See, you don't want implicit c_str() casting.
88     // This degrades to a pointer comparision, which flunks.
89     ASSERT_TRUE(c_string_same1 == byte_string);
90 #endif
91
92     const char* c_string1 = "he";
93     const char* c_string2 = "hellp";
94     const char* c_string3 = "hellod";
95     ASSERT_FALSE(byte_string == c_string1);
96     ASSERT_FALSE(byte_string == c_string2);
97     ASSERT_FALSE(byte_string == c_string3);
98     ASSERT_FALSE(c_string1 == byte_string);
99     ASSERT_FALSE(c_string2 == byte_string);
100     ASSERT_FALSE(c_string3 == byte_string);
101 }
102
103 TEST(fxcrt, ByteStringOperatorNE) {
104     CFX_ByteString byte_string("hello");
105     ASSERT_FALSE(byte_string != byte_string);
106
107     CFX_ByteString byte_string_same1("hello");
108     ASSERT_FALSE(byte_string != byte_string_same1);
109     ASSERT_FALSE(byte_string_same1 != byte_string);
110
111     CFX_ByteString byte_string_same2(byte_string);
112     ASSERT_FALSE(byte_string != byte_string_same2);
113     ASSERT_FALSE(byte_string_same2 != byte_string);
114
115     CFX_ByteString byte_string1("he");
116     CFX_ByteString byte_string2("hellp");
117     CFX_ByteString byte_string3("hellod");
118     ASSERT_TRUE(byte_string != byte_string1);
119     ASSERT_TRUE(byte_string != byte_string2);
120     ASSERT_TRUE(byte_string != byte_string3);
121     ASSERT_TRUE(byte_string1 != byte_string);
122     ASSERT_TRUE(byte_string2 != byte_string);
123     ASSERT_TRUE(byte_string3 != byte_string);
124
125     CFX_ByteStringC byte_string_c_same1("hello");
126     ASSERT_FALSE(byte_string != byte_string_c_same1);
127     ASSERT_FALSE(byte_string_c_same1 != byte_string);
128
129     CFX_ByteStringC byte_string_c1("he");
130     CFX_ByteStringC byte_string_c2("hellp");
131     CFX_ByteStringC byte_string_c3("hellod");
132     ASSERT_TRUE(byte_string != byte_string_c1);
133     ASSERT_TRUE(byte_string != byte_string_c2);
134     ASSERT_TRUE(byte_string != byte_string_c3);
135     ASSERT_TRUE(byte_string_c1 != byte_string);
136     ASSERT_TRUE(byte_string_c2 != byte_string);
137     ASSERT_TRUE(byte_string_c3 != byte_string);
138
139     const char* c_string_same1 = "hello";
140     ASSERT_FALSE(byte_string != c_string_same1);
141 #if 0
142     // See above TODO.
143     ASSERT_FALSE(c_string_same1 != byte_string);
144 #endif
145     const char* c_string1 = "he";
146     const char* c_string2 = "hellp";
147     const char* c_string3 = "hellod";
148     ASSERT_TRUE(byte_string != c_string1);
149     ASSERT_TRUE(byte_string != c_string2);
150     ASSERT_TRUE(byte_string != c_string3);
151     ASSERT_TRUE(c_string1 != byte_string);
152     ASSERT_TRUE(c_string2 != byte_string);
153     ASSERT_TRUE(c_string3 != byte_string);
154 }
155
156 TEST(fxcrt, ByteStringCNull) {
157   CFX_ByteStringC null_string;
158   EXPECT_EQ(null_string.GetPtr(), nullptr);
159   EXPECT_EQ(null_string.GetLength(), 0);
160   EXPECT_TRUE(null_string.IsEmpty());
161
162   CFX_ByteStringC another_null_string;
163   EXPECT_EQ(null_string, another_null_string);
164
165   CFX_ByteStringC copied_null_string(null_string);
166   EXPECT_EQ(copied_null_string.GetPtr(), nullptr);
167   EXPECT_EQ(copied_null_string.GetLength(), 0);
168   EXPECT_TRUE(copied_null_string.IsEmpty());
169   EXPECT_EQ(null_string, copied_null_string);
170
171   CFX_ByteStringC empty_string("");  // Pointer to NUL, not NULL pointer.
172   EXPECT_NE(empty_string.GetPtr(), nullptr);
173   EXPECT_EQ(empty_string.GetLength(), 0);
174   EXPECT_TRUE(empty_string.IsEmpty());
175   EXPECT_EQ(null_string, empty_string);
176
177   CFX_ByteStringC assigned_null_string("initially not NULL");
178   assigned_null_string = null_string;
179   EXPECT_EQ(assigned_null_string.GetPtr(), nullptr);
180   EXPECT_EQ(assigned_null_string.GetLength(), 0);
181   EXPECT_TRUE(assigned_null_string.IsEmpty());
182   EXPECT_EQ(null_string, assigned_null_string);
183
184   CFX_ByteStringC assigned_nullptr_string("initially not NULL");
185   assigned_nullptr_string = (FX_LPCSTR)nullptr;
186   EXPECT_EQ(assigned_nullptr_string.GetPtr(), nullptr);
187   EXPECT_EQ(assigned_nullptr_string.GetLength(), 0);
188   EXPECT_TRUE(assigned_nullptr_string.IsEmpty());
189   EXPECT_EQ(null_string, assigned_nullptr_string);
190
191   CFX_ByteStringC non_null_string("a");
192   EXPECT_NE(null_string, non_null_string);
193 }
194
195 TEST(fxcrt, ByteStringCNotNull) {
196   CFX_ByteStringC string3("abc");
197   CFX_ByteStringC string6("abcdef");
198   CFX_ByteStringC alternate_string3("abcdef", 3);
199   CFX_ByteStringC embedded_nul_string7("abc\0def", 7);
200   CFX_ByteStringC illegal_string7("abcdef", 7);
201
202   EXPECT_EQ(3, string3.GetLength());
203   EXPECT_EQ(6, string6.GetLength());
204   EXPECT_EQ(3, alternate_string3.GetLength());
205   EXPECT_EQ(7, embedded_nul_string7.GetLength());
206   EXPECT_EQ(7, illegal_string7.GetLength());
207
208   EXPECT_NE(string3, string6);
209   EXPECT_EQ(string3, alternate_string3);
210   EXPECT_NE(string3, embedded_nul_string7);
211   EXPECT_NE(string3, illegal_string7);
212   EXPECT_NE(string6, alternate_string3);
213   EXPECT_NE(string6, embedded_nul_string7);
214   EXPECT_NE(string6, illegal_string7);
215   EXPECT_NE(alternate_string3, embedded_nul_string7);
216   EXPECT_NE(alternate_string3, illegal_string7);
217   EXPECT_NE(embedded_nul_string7, illegal_string7);
218
219   CFX_ByteStringC copied_string3(string3);
220   CFX_ByteStringC copied_alternate_string3(alternate_string3);
221   CFX_ByteStringC copied_embedded_nul_string7(embedded_nul_string7);
222
223   EXPECT_EQ(string3, copied_string3);
224   EXPECT_EQ(alternate_string3, copied_alternate_string3);
225   EXPECT_EQ(embedded_nul_string7, copied_embedded_nul_string7);
226
227   CFX_ByteStringC assigned_string3("intially something else");
228   CFX_ByteStringC assigned_alternate_string3("initally something else");
229   CFX_ByteStringC assigned_ptr_string3("initially something else");
230   CFX_ByteStringC assigned_embedded_nul_string7("initially something else");
231
232   assigned_string3 = string3;
233   assigned_alternate_string3 = alternate_string3;
234   assigned_ptr_string3 = "abc";
235   assigned_embedded_nul_string7 = embedded_nul_string7;
236   EXPECT_EQ(string3, assigned_string3);
237   EXPECT_EQ(alternate_string3, assigned_alternate_string3);
238   EXPECT_EQ(alternate_string3, assigned_ptr_string3);
239   EXPECT_EQ(embedded_nul_string7, assigned_embedded_nul_string7);
240 }
241
242 TEST(fxcrt, ByteStringCFromChar) {
243   CFX_ByteStringC null_string;
244   CFX_ByteStringC lower_a_string("a");
245
246   // Must have lvalues that outlive the corresponding ByteStringC.
247   char nul = '\0';
248   char lower_a = 'a';
249   CFX_ByteStringC nul_string_from_char(nul);
250   CFX_ByteStringC lower_a_string_from_char(lower_a);
251
252   // Pointer to nul, not NULL ptr, hence length 1 ...
253   EXPECT_EQ(1, nul_string_from_char.GetLength());
254   EXPECT_NE(null_string, nul_string_from_char);
255
256   EXPECT_EQ(1, lower_a_string_from_char.GetLength());
257   EXPECT_EQ(lower_a_string, lower_a_string_from_char);
258   EXPECT_NE(nul_string_from_char, lower_a_string_from_char);
259
260   CFX_ByteStringC longer_string("ab");
261   EXPECT_NE(longer_string, lower_a_string_from_char);
262 }
263
264 TEST(fxcrt, ByteStringCGetID) {
265   CFX_ByteStringC null_string;
266   EXPECT_EQ(0u, null_string.GetID());
267   EXPECT_EQ(0u, null_string.GetID(1));
268   EXPECT_EQ(0u, null_string.GetID(-1));
269   EXPECT_EQ(0u, null_string.GetID(-1000000));
270
271   CFX_ByteStringC empty_string("");
272   EXPECT_EQ(0u, empty_string.GetID());
273   EXPECT_EQ(0u, empty_string.GetID(1));
274   EXPECT_EQ(0u, empty_string.GetID(-1));
275   EXPECT_EQ(0u, empty_string.GetID(-1000000));
276
277   CFX_ByteStringC short_string("ab");
278   EXPECT_EQ(FXBSTR_ID('a', 'b', 0, 0), short_string.GetID());
279   EXPECT_EQ(FXBSTR_ID('b', 0, 0, 0), short_string.GetID(1));
280   EXPECT_EQ(0u, short_string.GetID(2));
281   EXPECT_EQ(0u, short_string.GetID(-1));
282   EXPECT_EQ(0u, short_string.GetID(-1000000));
283
284   CFX_ByteStringC longer_string("abcdef");
285   EXPECT_EQ(FXBSTR_ID('a', 'b', 'c', 'd'), longer_string.GetID());
286   EXPECT_EQ(FXBSTR_ID('b', 'c', 'd', 'e'), longer_string.GetID(1));
287   EXPECT_EQ(FXBSTR_ID('c', 'd', 'e', 'f'), longer_string.GetID(2));
288   EXPECT_EQ(FXBSTR_ID('d', 'e', 'f', 0), longer_string.GetID(3));
289   EXPECT_EQ(FXBSTR_ID('e', 'f', 0 , 0), longer_string.GetID(4));
290   EXPECT_EQ(FXBSTR_ID('f', 0 , 0, 0), longer_string.GetID(5));
291   EXPECT_EQ(0u, longer_string.GetID(6));
292   EXPECT_EQ(0u, longer_string.GetID(-1));
293   EXPECT_EQ(0u, longer_string.GetID(-1000000));
294 }
295
296 TEST(fxcrt, ByteStringCMid) {
297   CFX_ByteStringC null_string;
298   EXPECT_EQ(null_string, null_string.Mid(0, 1));
299   EXPECT_EQ(null_string, null_string.Mid(1, 1));
300
301   CFX_ByteStringC empty_string("");
302   EXPECT_EQ(empty_string, empty_string.Mid(0, 1));
303   EXPECT_EQ(empty_string, empty_string.Mid(1, 1));
304
305   CFX_ByteStringC single_character("a");
306   EXPECT_EQ(empty_string, single_character.Mid(0, 0));
307   EXPECT_EQ(single_character, single_character.Mid(0, 1));
308   EXPECT_EQ(empty_string, single_character.Mid(1, 0));
309   EXPECT_EQ(empty_string, single_character.Mid(1, 1));
310
311   CFX_ByteStringC longer_string("abcdef");
312   EXPECT_EQ(longer_string, longer_string.Mid(0, 6));
313   EXPECT_EQ(longer_string, longer_string.Mid(0, 187));
314   EXPECT_EQ(longer_string, longer_string.Mid(-42, 6));
315   EXPECT_EQ(longer_string, longer_string.Mid(-42, 187));
316
317   CFX_ByteStringC leading_substring("ab");
318   EXPECT_EQ(leading_substring, longer_string.Mid(0, 2));
319   EXPECT_EQ(leading_substring, longer_string.Mid(-1, 2));
320
321   CFX_ByteStringC middle_substring("bcde");
322   EXPECT_EQ(middle_substring, longer_string.Mid(1, 4));
323
324   CFX_ByteStringC trailing_substring("ef");
325   EXPECT_EQ(trailing_substring, longer_string.Mid(4, 2));
326   EXPECT_EQ(trailing_substring, longer_string.Mid(4, 3));
327 }
328
329 TEST(fxcrt, ByteStringCGetAt) {
330   CFX_ByteString short_string("a");
331   CFX_ByteString longer_string("abc");
332   CFX_ByteString embedded_nul_string("ab\0c", 4);
333
334   EXPECT_EQ('a', short_string.GetAt(0));
335   EXPECT_EQ('c', longer_string.GetAt(2));
336   EXPECT_EQ('b', embedded_nul_string.GetAt(1));
337   EXPECT_EQ('\0', embedded_nul_string.GetAt(2));
338   EXPECT_EQ('c', embedded_nul_string.GetAt(3));
339 }
340
341 TEST(fxcrt, ByteStringCOperatorSubscript) {
342     // CFX_ByteStringC includes the NUL terminator for non-empty strings.
343     CFX_ByteStringC abc("abc");
344     EXPECT_EQ('a', abc[0]);
345     EXPECT_EQ('b', abc[1]);
346     EXPECT_EQ('c', abc[2]);
347     EXPECT_EQ(0, abc[3]);
348 }
349
350 TEST(fxcrt, ByteStringCOperatorLT) {
351     CFX_ByteStringC empty;
352     CFX_ByteStringC a("a");
353     CFX_ByteStringC abc("abc");
354     CFX_ByteStringC def("def");
355
356     EXPECT_FALSE(empty < empty);
357     EXPECT_FALSE(a < a);
358     EXPECT_FALSE(abc < abc);
359     EXPECT_FALSE(def < def);
360
361     EXPECT_TRUE(empty < a);
362     EXPECT_FALSE(a < empty);
363
364     EXPECT_TRUE(empty < abc);
365     EXPECT_FALSE(abc < empty);
366
367     EXPECT_TRUE(empty < def);
368     EXPECT_FALSE(def < empty);
369
370     EXPECT_TRUE(a < abc);
371     EXPECT_FALSE(abc < a);
372
373     EXPECT_TRUE(a < def);
374     EXPECT_FALSE(def < a);
375
376     EXPECT_TRUE(abc < def);
377     EXPECT_FALSE(def < abc);
378 }