summaryrefslogtreecommitdiff
path: root/core/src/fxcrt/fx_basic_wstring_unittest.cpp
blob: 16d71cb5b8e700f6a4c4643e6b245186dd31a2c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "../../../testing/fx_string_testhelpers.h"
#include "../../include/fxcrt/fx_basic.h"

TEST(fxcrt, WideStringOperatorSubscript) {
    // CFX_WideString includes the NUL terminator for non-empty strings.
    CFX_WideString abc(L"abc");
    EXPECT_EQ(L'a', abc[0]);
    EXPECT_EQ(L'b', abc[1]);
    EXPECT_EQ(L'c', abc[2]);
    EXPECT_EQ(L'\0', abc[3]);
}

TEST(fxcrt, WideStringOperatorLT) {
    CFX_WideString empty;
    CFX_WideString a(L"a");
    CFX_WideString abc(L"\x0110qq");  // Comes before despite endianness.
    CFX_WideString def(L"\x1001qq");  // Comes after despite endianness.

    EXPECT_FALSE(empty < empty);
    EXPECT_FALSE(a < a);
    EXPECT_FALSE(abc < abc);
    EXPECT_FALSE(def < def);

    EXPECT_TRUE(empty < a);
    EXPECT_FALSE(a < empty);

    EXPECT_TRUE(empty < abc);
    EXPECT_FALSE(abc < empty);

    EXPECT_TRUE(empty < def);
    EXPECT_FALSE(def < empty);

    EXPECT_TRUE(a < abc);
    EXPECT_FALSE(abc < a);

    EXPECT_TRUE(a < def);
    EXPECT_FALSE(def < a);

    EXPECT_TRUE(abc < def);
    EXPECT_FALSE(def < abc);
}

TEST(fxcrt, WideStringOperatorEQ) {
    CFX_WideString wide_string(L"hello");
    ASSERT_TRUE(wide_string == wide_string);

    CFX_WideString wide_string_same1(L"hello");
    ASSERT_TRUE(wide_string == wide_string_same1);
    ASSERT_TRUE(wide_string_same1 == wide_string);

    CFX_WideString wide_string_same2(wide_string);
    ASSERT_TRUE(wide_string == wide_string_same2);
    ASSERT_TRUE(wide_string_same2 == wide_string);

    CFX_WideString wide_string1(L"he");
    CFX_WideString wide_string2(L"hellp");
    CFX_WideString wide_string3(L"hellod");
    ASSERT_FALSE(wide_string == wide_string1);
    ASSERT_FALSE(wide_string == wide_string2);
    ASSERT_FALSE(wide_string == wide_string3);
    ASSERT_FALSE(wide_string1 == wide_string);
    ASSERT_FALSE(wide_string2 == wide_string);
    ASSERT_FALSE(wide_string3 == wide_string);

    CFX_WideStringC wide_string_c_same1(L"hello");
    ASSERT_TRUE(wide_string == wide_string_c_same1);
    ASSERT_TRUE(wide_string_c_same1 == wide_string);

    CFX_WideStringC wide_string_c1(L"he");
    CFX_WideStringC wide_string_c2(L"hellp");
    CFX_WideStringC wide_string_c3(L"hellod");
    ASSERT_FALSE(wide_string == wide_string_c1);
    ASSERT_FALSE(wide_string == wide_string_c2);
    ASSERT_FALSE(wide_string == wide_string_c3);
    ASSERT_FALSE(wide_string_c1 == wide_string);
    ASSERT_FALSE(wide_string_c2 == wide_string);
    ASSERT_FALSE(wide_string_c3 == wide_string);

    const wchar_t* c_string_same1 = L"hello";
    ASSERT_TRUE(wide_string == c_string_same1);
#if 0
    // TODO(tsepez): Missing operator - there's a prototype but no actual
    // implementation (at least we already removed implicit c_str() casting).
    ASSERT_TRUE(c_string_same1 == wide_string);
#endif

    const wchar_t* c_string1 = L"he";
    const wchar_t* c_string2 = L"hellp";
    const wchar_t* c_string3 = L"hellod";
    ASSERT_FALSE(wide_string == c_string1);
    ASSERT_FALSE(wide_string == c_string2);
    ASSERT_FALSE(wide_string == c_string3);
#if 0
    // See above TODO.
    ASSERT_FALSE(c_string1 == wide_string);
    ASSERT_FALSE(c_string2 == wide_string);
    ASSERT_FALSE(c_string3 == wide_string);
#endif
}

TEST(fxcrt, WideStringOperatorNE) {
    CFX_WideString wide_string(L"hello");
    ASSERT_FALSE(wide_string != wide_string);

    CFX_WideString wide_string_same1(L"hello");
    ASSERT_FALSE(wide_string != wide_string_same1);
    ASSERT_FALSE(wide_string_same1 != wide_string);

    CFX_WideString wide_string_same2(wide_string);
    ASSERT_FALSE(wide_string != wide_string_same2);
    ASSERT_FALSE(wide_string_same2 != wide_string);

    CFX_WideString wide_string1(L"he");
    CFX_WideString wide_string2(L"hellp");
    CFX_WideString wide_string3(L"hellod");
    ASSERT_TRUE(wide_string != wide_string1);
    ASSERT_TRUE(wide_string != wide_string2);
    ASSERT_TRUE(wide_string != wide_string3);
    ASSERT_TRUE(wide_string1 != wide_string);
    ASSERT_TRUE(wide_string2 != wide_string);
    ASSERT_TRUE(wide_string3 != wide_string);

    CFX_WideStringC wide_string_c_same1(L"hello");
    ASSERT_FALSE(wide_string != wide_string_c_same1);
    ASSERT_FALSE(wide_string_c_same1 != wide_string);

    CFX_WideStringC wide_string_c1(L"he");
    CFX_WideStringC wide_string_c2(L"hellp");
    CFX_WideStringC wide_string_c3(L"hellod");
    ASSERT_TRUE(wide_string != wide_string_c1);
    ASSERT_TRUE(wide_string != wide_string_c2);
    ASSERT_TRUE(wide_string != wide_string_c3);
    ASSERT_TRUE(wide_string_c1 != wide_string);
    ASSERT_TRUE(wide_string_c2 != wide_string);
    ASSERT_TRUE(wide_string_c3 != wide_string);

    const wchar_t* c_string_same1 = L"hello";
    ASSERT_FALSE(wide_string != c_string_same1);
#if 0
    // See above TODO.
    ASSERT_FALSE(c_string_same1 != wide_string);
#endif
    const wchar_t* c_string1 = L"he";
    const wchar_t* c_string2 = L"hellp";
    const wchar_t* c_string3 = L"hellod";
    ASSERT_TRUE(wide_string != c_string1);
    ASSERT_TRUE(wide_string != c_string2);
    ASSERT_TRUE(wide_string != c_string3);
#if 0
    // See above TODO.
    ASSERT_TRUE(c_string1 != wide_string);
    ASSERT_TRUE(c_string2 != wide_string);
    ASSERT_TRUE(c_string3 != wide_string);
#endif
}

#define ByteStringLiteral(str) CFX_ByteString(FX_BSTRC(str))

TEST(fxcrt, WideStringUTF16LE_Encode) {
  struct UTF16LEEncodeCase {
    CFX_WideString ws;
    CFX_ByteString bs;
  } utf16le_encode_cases[] = {
    { L"", ByteStringLiteral("\0\0") },
    { L"abc", ByteStringLiteral("a\0b\0c\0\0\0") },
    { L"abcdef", ByteStringLiteral("a\0b\0c\0d\0e\0f\0\0\0") },
    { L"abc\0def", ByteStringLiteral("a\0b\0c\0\0\0") },
    { L"\xaabb\xccdd", ByteStringLiteral("\xbb\xaa\xdd\xcc\0\0") },
    { L"\x3132\x6162", ByteStringLiteral("\x32\x31\x62\x61\0\0") },
  };

  for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) {
    EXPECT_EQ(utf16le_encode_cases[i].bs,
        utf16le_encode_cases[i].ws.UTF16LE_Encode())
        << " for case number " << i;
  }
}

TEST(fxcrt, WideStringCOperatorSubscript) {
    // CFX_WideStringC includes the NUL terminator for non-empty strings.
    CFX_WideStringC abc(L"abc");
    EXPECT_EQ(L'a', abc[0]);
    EXPECT_EQ(L'b', abc[1]);
    EXPECT_EQ(L'c', abc[2]);
    EXPECT_EQ(L'\0', abc[3]);
}

TEST(fxcrt, WideStringCOperatorLT) {
    CFX_WideStringC empty;
    CFX_WideStringC a(L"a");
    CFX_WideStringC abc(L"\x0110qq");  // Comes before despite endianness.
    CFX_WideStringC def(L"\x1001qq");  // Comes after despite endianness.

    EXPECT_FALSE(empty < empty);
    EXPECT_FALSE(a < a);
    EXPECT_FALSE(abc < abc);
    EXPECT_FALSE(def < def);

    EXPECT_TRUE(empty < a);
    EXPECT_FALSE(a < empty);

    EXPECT_TRUE(empty < abc);
    EXPECT_FALSE(abc < empty);

    EXPECT_TRUE(empty < def);
    EXPECT_FALSE(def < empty);

    EXPECT_TRUE(a < abc);
    EXPECT_FALSE(abc < a);

    EXPECT_TRUE(a < def);
    EXPECT_FALSE(def < a);

    EXPECT_TRUE(abc < def);
    EXPECT_FALSE(def < abc);
}

TEST(fxcrt, WideStringCOperatorEQ) {
    CFX_WideStringC wide_string_c(L"hello");
    ASSERT_TRUE(wide_string_c == wide_string_c);

    CFX_WideStringC wide_string_c_same1(L"hello");
    ASSERT_TRUE(wide_string_c == wide_string_c_same1);
    ASSERT_TRUE(wide_string_c_same1 == wide_string_c);

    CFX_WideStringC wide_string_c_same2(wide_string_c);
    ASSERT_TRUE(wide_string_c == wide_string_c_same2);
    ASSERT_TRUE(wide_string_c_same2 == wide_string_c);

    CFX_WideStringC wide_string_c1(L"he");
    CFX_WideStringC wide_string_c2(L"hellp");
    CFX_WideStringC wide_string_c3(L"hellod");
    ASSERT_FALSE(wide_string_c == wide_string_c1);
    ASSERT_FALSE(wide_string_c == wide_string_c2);
    ASSERT_FALSE(wide_string_c == wide_string_c3);
    ASSERT_FALSE(wide_string_c1 == wide_string_c);
    ASSERT_FALSE(wide_string_c2 == wide_string_c);
    ASSERT_FALSE(wide_string_c3 == wide_string_c);

    CFX_WideString wide_string_same1(L"hello");
    ASSERT_TRUE(wide_string_c == wide_string_same1);
    ASSERT_TRUE(wide_string_same1 == wide_string_c);

    CFX_WideString wide_string1(L"he");
    CFX_WideString wide_string2(L"hellp");
    CFX_WideString wide_string3(L"hellod");
    ASSERT_FALSE(wide_string_c == wide_string1);
    ASSERT_FALSE(wide_string_c == wide_string2);
    ASSERT_FALSE(wide_string_c == wide_string3);
    ASSERT_FALSE(wide_string1 == wide_string_c);
    ASSERT_FALSE(wide_string2 == wide_string_c);
    ASSERT_FALSE(wide_string3 == wide_string_c);

#if 0
    // TODO(tsepez): ambiguos overload prevents compilation
    const wchar_t* c_string_same1 = L"hello";
    ASSERT_TRUE(wide_string_c == c_string_same1);
    ASSERT_TRUE(c_string_same1 == wide_string_c);

    const wchar_t* c_string1 = L"he";
    const wchar_t* c_string2 = L"hellp";
    const wchar_t* c_string3 = L"hellod";
    ASSERT_FALSE(wide_string_c == c_string1);
    ASSERT_FALSE(wide_string_c == c_string2);
    ASSERT_FALSE(wide_string_c == c_string3);

    ASSERT_FALSE(c_string1 == wide_string_c);
    ASSERT_FALSE(c_string2 == wide_string_c);
    ASSERT_FALSE(c_string3 == wide_string_c);
#endif
}

TEST(fxcrt, WideStringCOperatorNE) {
    CFX_WideStringC wide_string_c(L"hello");
    ASSERT_FALSE(wide_string_c != wide_string_c);

    CFX_WideStringC wide_string_c_same1(L"hello");
    ASSERT_FALSE(wide_string_c != wide_string_c_same1);
    ASSERT_FALSE(wide_string_c_same1 != wide_string_c);

    CFX_WideStringC wide_string_c_same2(wide_string_c);
    ASSERT_FALSE(wide_string_c != wide_string_c_same2);
    ASSERT_FALSE(wide_string_c_same2 != wide_string_c);

    CFX_WideStringC wide_string_c1(L"he");
    CFX_WideStringC wide_string_c2(L"hellp");
    CFX_WideStringC wide_string_c3(L"hellod");
    ASSERT_TRUE(wide_string_c != wide_string_c1);
    ASSERT_TRUE(wide_string_c != wide_string_c2);
    ASSERT_TRUE(wide_string_c != wide_string_c3);
    ASSERT_TRUE(wide_string_c1 != wide_string_c);
    ASSERT_TRUE(wide_string_c2 != wide_string_c);
    ASSERT_TRUE(wide_string_c3 != wide_string_c);

    CFX_WideString wide_string_same1(L"hello");
    ASSERT_FALSE(wide_string_c != wide_string_same1);
    ASSERT_FALSE(wide_string_same1 != wide_string_c);

    CFX_WideString wide_string1(L"he");
    CFX_WideString wide_string2(L"hellp");
    CFX_WideString wide_string3(L"hellod");
    ASSERT_TRUE(wide_string_c != wide_string1);
    ASSERT_TRUE(wide_string_c != wide_string2);
    ASSERT_TRUE(wide_string_c != wide_string3);
    ASSERT_TRUE(wide_string1 != wide_string_c);
    ASSERT_TRUE(wide_string2 != wide_string_c);
    ASSERT_TRUE(wide_string3 != wide_string_c);

#if 0
    // See above TODO.
    const wchar_t* c_string_same1 = L"hello";
    ASSERT_FALSE(wide_string_c != c_string_same1);
    ASSERT_FALSE(c_string_same1 != wide_string_c);

    const wchar_t* c_string1 = L"he";
    const wchar_t* c_string2 = L"hellp";
    const wchar_t* c_string3 = L"hellod";
    ASSERT_TRUE(wide_string_c != c_string1);
    ASSERT_TRUE(wide_string_c != c_string2);
    ASSERT_TRUE(wide_string_c != c_string3);

    ASSERT_TRUE(c_string1 != wide_string_c);
    ASSERT_TRUE(c_string2 != wide_string_c);
    ASSERT_TRUE(c_string3 != wide_string_c);
#endif
}