57cfc8047ee2f67fa82f064327acd2e5bf93c521
[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, ByteStringCNull) {
49   CFX_ByteStringC null_string;
50   EXPECT_EQ(null_string.GetPtr(), nullptr);
51   EXPECT_EQ(null_string.GetLength(), 0);
52   EXPECT_TRUE(null_string.IsEmpty());
53
54   CFX_ByteStringC another_null_string;
55   EXPECT_EQ(null_string, another_null_string);
56
57   CFX_ByteStringC copied_null_string(null_string);
58   EXPECT_EQ(copied_null_string.GetPtr(), nullptr);
59   EXPECT_EQ(copied_null_string.GetLength(), 0);
60   EXPECT_TRUE(copied_null_string.IsEmpty());
61   EXPECT_EQ(null_string, copied_null_string);
62
63   CFX_ByteStringC empty_string("");  // Pointer to NUL, not NULL pointer.
64   EXPECT_NE(empty_string.GetPtr(), nullptr);
65   EXPECT_EQ(empty_string.GetLength(), 0);
66   EXPECT_TRUE(empty_string.IsEmpty());
67   EXPECT_EQ(null_string, empty_string);
68
69   CFX_ByteStringC assigned_null_string("initially not NULL");
70   assigned_null_string = null_string;
71   EXPECT_EQ(assigned_null_string.GetPtr(), nullptr);
72   EXPECT_EQ(assigned_null_string.GetLength(), 0);
73   EXPECT_TRUE(assigned_null_string.IsEmpty());
74   EXPECT_EQ(null_string, assigned_null_string);
75
76   CFX_ByteStringC assigned_nullptr_string("initially not NULL");
77   assigned_nullptr_string = (FX_LPCSTR)nullptr;
78   EXPECT_EQ(assigned_nullptr_string.GetPtr(), nullptr);
79   EXPECT_EQ(assigned_nullptr_string.GetLength(), 0);
80   EXPECT_TRUE(assigned_nullptr_string.IsEmpty());
81   EXPECT_EQ(null_string, assigned_nullptr_string);
82
83   CFX_ByteStringC non_null_string("a");
84   EXPECT_NE(null_string, non_null_string);
85 }
86
87 TEST(fxcrt, ByteStringCNotNull) {
88   CFX_ByteStringC string3("abc");
89   CFX_ByteStringC string6("abcdef");
90   CFX_ByteStringC alternate_string3("abcdef", 3);
91   CFX_ByteStringC embedded_nul_string7("abc\0def", 7);
92   CFX_ByteStringC illegal_string7("abcdef", 7);
93
94   EXPECT_EQ(3, string3.GetLength());
95   EXPECT_EQ(6, string6.GetLength());
96   EXPECT_EQ(3, alternate_string3.GetLength());
97   EXPECT_EQ(7, embedded_nul_string7.GetLength());
98   EXPECT_EQ(7, illegal_string7.GetLength());
99
100   EXPECT_NE(string3, string6);
101   EXPECT_EQ(string3, alternate_string3);
102   EXPECT_NE(string3, embedded_nul_string7);
103   EXPECT_NE(string3, illegal_string7);
104   EXPECT_NE(string6, alternate_string3);
105   EXPECT_NE(string6, embedded_nul_string7);
106   EXPECT_NE(string6, illegal_string7);
107   EXPECT_NE(alternate_string3, embedded_nul_string7);
108   EXPECT_NE(alternate_string3, illegal_string7);
109   EXPECT_NE(embedded_nul_string7, illegal_string7);
110
111   CFX_ByteStringC copied_string3(string3);
112   CFX_ByteStringC copied_alternate_string3(alternate_string3);
113   CFX_ByteStringC copied_embedded_nul_string7(embedded_nul_string7);
114
115   EXPECT_EQ(string3, copied_string3);
116   EXPECT_EQ(alternate_string3, copied_alternate_string3);
117   EXPECT_EQ(embedded_nul_string7, copied_embedded_nul_string7);
118
119   CFX_ByteStringC assigned_string3("intially something else");
120   CFX_ByteStringC assigned_alternate_string3("initally something else");
121   CFX_ByteStringC assigned_ptr_string3("initially something else");
122   CFX_ByteStringC assigned_embedded_nul_string7("initially something else");
123
124   assigned_string3 = string3;
125   assigned_alternate_string3 = alternate_string3;
126   assigned_ptr_string3 = "abc";
127   assigned_embedded_nul_string7 = embedded_nul_string7;
128   EXPECT_EQ(string3, assigned_string3);
129   EXPECT_EQ(alternate_string3, assigned_alternate_string3);
130   EXPECT_EQ(alternate_string3, assigned_ptr_string3);
131   EXPECT_EQ(embedded_nul_string7, assigned_embedded_nul_string7);
132 }
133
134 TEST(fxcrt, ByteStringCFromChar) {
135   CFX_ByteStringC null_string;
136   CFX_ByteStringC lower_a_string("a");
137
138   // Must have lvalues that outlive the corresponding ByteStringC.
139   char nul = '\0';
140   char lower_a = 'a';
141   CFX_ByteStringC nul_string_from_char(nul);
142   CFX_ByteStringC lower_a_string_from_char(lower_a);
143
144   // Pointer to nul, not NULL ptr, hence length 1 ...
145   EXPECT_EQ(1, nul_string_from_char.GetLength());
146   EXPECT_NE(null_string, nul_string_from_char);
147
148   EXPECT_EQ(1, lower_a_string_from_char.GetLength());
149   EXPECT_EQ(lower_a_string, lower_a_string_from_char);
150   EXPECT_NE(nul_string_from_char, lower_a_string_from_char);
151
152   CFX_ByteStringC longer_string("ab");
153   EXPECT_NE(longer_string, lower_a_string_from_char);
154 }
155
156 TEST(fxcrt, ByteStringCGetID) {
157   CFX_ByteStringC null_string;
158   EXPECT_EQ(0u, null_string.GetID());
159   EXPECT_EQ(0u, null_string.GetID(1));
160   EXPECT_EQ(0u, null_string.GetID(-1));
161   EXPECT_EQ(0u, null_string.GetID(-1000000));
162
163   CFX_ByteStringC empty_string("");
164   EXPECT_EQ(0u, empty_string.GetID());
165   EXPECT_EQ(0u, empty_string.GetID(1));
166   EXPECT_EQ(0u, empty_string.GetID(-1));
167   EXPECT_EQ(0u, empty_string.GetID(-1000000));
168
169   CFX_ByteStringC short_string("ab");
170   EXPECT_EQ(FXBSTR_ID('a', 'b', 0, 0), short_string.GetID());
171   EXPECT_EQ(FXBSTR_ID('b', 0, 0, 0), short_string.GetID(1));
172   EXPECT_EQ(0u, short_string.GetID(2));
173   EXPECT_EQ(0u, short_string.GetID(-1));
174   EXPECT_EQ(0u, short_string.GetID(-1000000));
175
176   CFX_ByteStringC longer_string("abcdef");
177   EXPECT_EQ(FXBSTR_ID('a', 'b', 'c', 'd'), longer_string.GetID());
178   EXPECT_EQ(FXBSTR_ID('b', 'c', 'd', 'e'), longer_string.GetID(1));
179   EXPECT_EQ(FXBSTR_ID('c', 'd', 'e', 'f'), longer_string.GetID(2));
180   EXPECT_EQ(FXBSTR_ID('d', 'e', 'f', 0), longer_string.GetID(3));
181   EXPECT_EQ(FXBSTR_ID('e', 'f', 0 , 0), longer_string.GetID(4));
182   EXPECT_EQ(FXBSTR_ID('f', 0 , 0, 0), longer_string.GetID(5));
183   EXPECT_EQ(0u, longer_string.GetID(6));
184   EXPECT_EQ(0u, longer_string.GetID(-1));
185   EXPECT_EQ(0u, longer_string.GetID(-1000000));
186 }
187
188 TEST(fxcrt, ByteStringCMid) {
189   CFX_ByteStringC null_string;
190   EXPECT_EQ(null_string, null_string.Mid(0, 1));
191   EXPECT_EQ(null_string, null_string.Mid(1, 1));
192
193   CFX_ByteStringC empty_string("");
194   EXPECT_EQ(empty_string, empty_string.Mid(0, 1));
195   EXPECT_EQ(empty_string, empty_string.Mid(1, 1));
196
197   CFX_ByteStringC single_character("a");
198   EXPECT_EQ(empty_string, single_character.Mid(0, 0));
199   EXPECT_EQ(single_character, single_character.Mid(0, 1));
200   EXPECT_EQ(empty_string, single_character.Mid(1, 0));
201   EXPECT_EQ(empty_string, single_character.Mid(1, 1));
202
203   CFX_ByteStringC longer_string("abcdef");
204   EXPECT_EQ(longer_string, longer_string.Mid(0, 6));
205   EXPECT_EQ(longer_string, longer_string.Mid(0, 187));
206   EXPECT_EQ(longer_string, longer_string.Mid(-42, 6));
207   EXPECT_EQ(longer_string, longer_string.Mid(-42, 187));
208
209   CFX_ByteStringC leading_substring("ab");
210   EXPECT_EQ(leading_substring, longer_string.Mid(0, 2));
211   EXPECT_EQ(leading_substring, longer_string.Mid(-1, 2));
212
213   CFX_ByteStringC middle_substring("bcde");
214   EXPECT_EQ(middle_substring, longer_string.Mid(1, 4));
215
216   CFX_ByteStringC trailing_substring("ef");
217   EXPECT_EQ(trailing_substring, longer_string.Mid(4, 2));
218   EXPECT_EQ(trailing_substring, longer_string.Mid(4, 3));
219 }
220
221 TEST(fxcrt, ByteStringCGetAt) {
222   CFX_ByteString short_string("a");
223   CFX_ByteString longer_string("abc");
224   CFX_ByteString embedded_nul_string("ab\0c", 4);
225
226   EXPECT_EQ('a', short_string.GetAt(0));
227   EXPECT_EQ('c', longer_string.GetAt(2));
228   EXPECT_EQ('b', embedded_nul_string.GetAt(1));
229   EXPECT_EQ('\0', embedded_nul_string.GetAt(2));
230   EXPECT_EQ('c', embedded_nul_string.GetAt(3));
231 }
232
233 TEST(fxcrt, ByteStringCOperatorSubscript) {
234     // CFX_ByteStringC includes the NUL terminator for non-empty strings.
235     CFX_ByteStringC abc("abc");
236     EXPECT_EQ('a', abc[0]);
237     EXPECT_EQ('b', abc[1]);
238     EXPECT_EQ('c', abc[2]);
239     EXPECT_EQ(0, abc[3]);
240 }
241
242 TEST(fxcrt, ByteStringCOperatorLT) {
243     CFX_ByteStringC empty;
244     CFX_ByteStringC a("a");
245     CFX_ByteStringC abc("abc");
246     CFX_ByteStringC def("def");
247
248     EXPECT_FALSE(empty < empty);
249     EXPECT_FALSE(a < a);
250     EXPECT_FALSE(abc < abc);
251     EXPECT_FALSE(def < def);
252
253     EXPECT_TRUE(empty < a);
254     EXPECT_FALSE(a < empty);
255
256     EXPECT_TRUE(empty < abc);
257     EXPECT_FALSE(abc < empty);
258
259     EXPECT_TRUE(empty < def);
260     EXPECT_FALSE(def < empty);
261
262     EXPECT_TRUE(a < abc);
263     EXPECT_FALSE(abc < a);
264
265     EXPECT_TRUE(a < def);
266     EXPECT_FALSE(def < a);
267
268     EXPECT_TRUE(abc < def);
269     EXPECT_FALSE(def < abc);
270 }