030b6aea10a60330aee5bc66a136a68f803de497
[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, ByteStringCNull) {
10   CFX_ByteStringC null_string;
11   EXPECT_EQ(null_string.GetPtr(), nullptr);
12   EXPECT_EQ(null_string.GetLength(), 0);
13   EXPECT_TRUE(null_string.IsEmpty());
14
15   CFX_ByteStringC another_null_string;
16   EXPECT_EQ(null_string, another_null_string);
17
18   CFX_ByteStringC copied_null_string(null_string);
19   EXPECT_EQ(copied_null_string.GetPtr(), nullptr);
20   EXPECT_EQ(copied_null_string.GetLength(), 0);
21   EXPECT_TRUE(copied_null_string.IsEmpty());
22   EXPECT_EQ(null_string, copied_null_string);
23
24   CFX_ByteStringC empty_string("");  // Pointer to NUL, not NULL pointer.
25   EXPECT_NE(empty_string.GetPtr(), nullptr);
26   EXPECT_EQ(empty_string.GetLength(), 0);
27   EXPECT_TRUE(empty_string.IsEmpty());
28   EXPECT_EQ(null_string, empty_string);
29
30   CFX_ByteStringC assigned_null_string("initially not NULL");
31   assigned_null_string = null_string;
32   EXPECT_EQ(assigned_null_string.GetPtr(), nullptr);
33   EXPECT_EQ(assigned_null_string.GetLength(), 0);
34   EXPECT_TRUE(assigned_null_string.IsEmpty());
35   EXPECT_EQ(null_string, assigned_null_string);
36
37   CFX_ByteStringC assigned_nullptr_string("initially not NULL");
38   assigned_nullptr_string = (FX_LPCSTR)nullptr;
39   EXPECT_EQ(assigned_nullptr_string.GetPtr(), nullptr);
40   EXPECT_EQ(assigned_nullptr_string.GetLength(), 0);
41   EXPECT_TRUE(assigned_nullptr_string.IsEmpty());
42   EXPECT_EQ(null_string, assigned_nullptr_string);
43
44   CFX_ByteStringC non_null_string("a");
45   EXPECT_NE(null_string, non_null_string);
46 }
47
48 TEST(fxcrt, ByteStringCNotNull) {
49   CFX_ByteStringC string3("abc");
50   CFX_ByteStringC string6("abcdef");
51   CFX_ByteStringC alternate_string3("abcdef", 3);
52   CFX_ByteStringC embedded_nul_string7("abc\0def", 7);
53   CFX_ByteStringC illegal_string7("abcdef", 7);
54
55   EXPECT_EQ(3, string3.GetLength());
56   EXPECT_EQ(6, string6.GetLength());
57   EXPECT_EQ(3, alternate_string3.GetLength());
58   EXPECT_EQ(7, embedded_nul_string7.GetLength());
59   EXPECT_EQ(7, illegal_string7.GetLength());
60
61   EXPECT_NE(string3, string6);
62   EXPECT_EQ(string3, alternate_string3);
63   EXPECT_NE(string3, embedded_nul_string7);
64   EXPECT_NE(string3, illegal_string7);
65   EXPECT_NE(string6, alternate_string3);
66   EXPECT_NE(string6, embedded_nul_string7);
67   EXPECT_NE(string6, illegal_string7);
68   EXPECT_NE(alternate_string3, embedded_nul_string7);
69   EXPECT_NE(alternate_string3, illegal_string7);
70   EXPECT_NE(embedded_nul_string7, illegal_string7);
71
72   CFX_ByteStringC copied_string3(string3);
73   CFX_ByteStringC copied_alternate_string3(alternate_string3);
74   CFX_ByteStringC copied_embedded_nul_string7(embedded_nul_string7);
75
76   EXPECT_EQ(string3, copied_string3);
77   EXPECT_EQ(alternate_string3, copied_alternate_string3);
78   EXPECT_EQ(embedded_nul_string7, copied_embedded_nul_string7);
79
80   CFX_ByteStringC assigned_string3("intially something else");
81   CFX_ByteStringC assigned_alternate_string3("initally something else");
82   CFX_ByteStringC assigned_ptr_string3("initially something else");
83   CFX_ByteStringC assigned_embedded_nul_string7("initially something else");
84
85   assigned_string3 = string3;
86   assigned_alternate_string3 = alternate_string3;
87   assigned_ptr_string3 = "abc";
88   assigned_embedded_nul_string7 = embedded_nul_string7;
89   EXPECT_EQ(string3, assigned_string3);
90   EXPECT_EQ(alternate_string3, assigned_alternate_string3);
91   EXPECT_EQ(alternate_string3, assigned_ptr_string3);
92   EXPECT_EQ(embedded_nul_string7, assigned_embedded_nul_string7);
93 }
94
95 TEST(fxcrt, ByteStringCFromChar) {
96   CFX_ByteStringC null_string;
97   CFX_ByteStringC lower_a_string("a");
98
99   // Must have lvalues that outlive the corresponding ByteStringC.
100   char nul = '\0';
101   char lower_a = 'a';
102   CFX_ByteStringC nul_string_from_char(nul);
103   CFX_ByteStringC lower_a_string_from_char(lower_a);
104
105   // Pointer to nul, not NULL ptr, hence length 1 ...
106   EXPECT_EQ(1, nul_string_from_char.GetLength());
107   EXPECT_NE(null_string, nul_string_from_char);
108
109   EXPECT_EQ(1, lower_a_string_from_char.GetLength());
110   EXPECT_EQ(lower_a_string, lower_a_string_from_char);
111   EXPECT_NE(nul_string_from_char, lower_a_string_from_char);
112
113   CFX_ByteStringC longer_string("ab");
114   EXPECT_NE(longer_string, lower_a_string_from_char);
115 }
116
117 TEST(fxcrt, ByteStringCGetID) {
118   CFX_ByteStringC null_string;
119   EXPECT_EQ(0u, null_string.GetID());
120   EXPECT_EQ(0u, null_string.GetID(1));
121   EXPECT_EQ(0u, null_string.GetID(-1));
122   EXPECT_EQ(0u, null_string.GetID(-1000000));
123
124   CFX_ByteStringC empty_string("");
125   EXPECT_EQ(0u, empty_string.GetID());
126   EXPECT_EQ(0u, empty_string.GetID(1));
127   EXPECT_EQ(0u, empty_string.GetID(-1));
128   EXPECT_EQ(0u, empty_string.GetID(-1000000));
129
130   CFX_ByteStringC short_string("ab");
131   EXPECT_EQ(FXBSTR_ID('a', 'b', 0, 0), short_string.GetID());
132   EXPECT_EQ(FXBSTR_ID('b', 0, 0, 0), short_string.GetID(1));
133   EXPECT_EQ(0u, short_string.GetID(2));
134   EXPECT_EQ(0u, short_string.GetID(-1));
135   EXPECT_EQ(0u, short_string.GetID(-1000000));
136
137   CFX_ByteStringC longer_string("abcdef");
138   EXPECT_EQ(FXBSTR_ID('a', 'b', 'c', 'd'), longer_string.GetID());
139   EXPECT_EQ(FXBSTR_ID('b', 'c', 'd', 'e'), longer_string.GetID(1));
140   EXPECT_EQ(FXBSTR_ID('c', 'd', 'e', 'f'), longer_string.GetID(2));
141   EXPECT_EQ(FXBSTR_ID('d', 'e', 'f', 0), longer_string.GetID(3));
142   EXPECT_EQ(FXBSTR_ID('e', 'f', 0 , 0), longer_string.GetID(4));
143   EXPECT_EQ(FXBSTR_ID('f', 0 , 0, 0), longer_string.GetID(5));
144   EXPECT_EQ(0u, longer_string.GetID(6));
145   EXPECT_EQ(0u, longer_string.GetID(-1));
146   EXPECT_EQ(0u, longer_string.GetID(-1000000));
147 }
148
149 TEST(fxcrt, ByteStringCMid) {
150   CFX_ByteStringC null_string;
151   EXPECT_EQ(null_string, null_string.Mid(0, 1));
152   EXPECT_EQ(null_string, null_string.Mid(1, 1));
153
154   CFX_ByteStringC empty_string("");
155   EXPECT_EQ(empty_string, empty_string.Mid(0, 1));
156   EXPECT_EQ(empty_string, empty_string.Mid(1, 1));
157
158   CFX_ByteStringC single_character("a");
159   EXPECT_EQ(empty_string, single_character.Mid(0, 0));
160   EXPECT_EQ(single_character, single_character.Mid(0, 1));
161   EXPECT_EQ(empty_string, single_character.Mid(1, 0));
162   EXPECT_EQ(empty_string, single_character.Mid(1, 1));
163
164   CFX_ByteStringC longer_string("abcdef");
165   EXPECT_EQ(longer_string, longer_string.Mid(0, 6));
166   EXPECT_EQ(longer_string, longer_string.Mid(0, 187));
167   EXPECT_EQ(longer_string, longer_string.Mid(-42, 6));
168   EXPECT_EQ(longer_string, longer_string.Mid(-42, 187));
169
170   CFX_ByteStringC leading_substring("ab");
171   EXPECT_EQ(leading_substring, longer_string.Mid(0, 2));
172   EXPECT_EQ(leading_substring, longer_string.Mid(-1, 2));
173
174   CFX_ByteStringC middle_substring("bcde");
175   EXPECT_EQ(middle_substring, longer_string.Mid(1, 4));
176
177   CFX_ByteStringC trailing_substring("ef");
178   EXPECT_EQ(trailing_substring, longer_string.Mid(4, 2));
179   EXPECT_EQ(trailing_substring, longer_string.Mid(4, 3));
180 }
181
182 TEST(fxcrt, ByteStringCGetAt) {
183   CFX_ByteString short_string("a");
184   CFX_ByteString longer_string("abc");
185   CFX_ByteString embedded_nul_string("ab\0c", 4);
186
187   EXPECT_EQ('a', short_string.GetAt(0));
188   EXPECT_EQ('c', longer_string.GetAt(2));
189   EXPECT_EQ('b', embedded_nul_string.GetAt(1));
190   EXPECT_EQ('\0', embedded_nul_string.GetAt(2));
191   EXPECT_EQ('c', embedded_nul_string.GetAt(3));
192 }