Add missing operators for CFX_ByteStringC.
[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 #define ByteStringLiteral(str) CFX_ByteString(FX_BSTRC(str))
49
50 TEST(fxcrt, WideStringUTF16LE_Encode) {
51   struct UTF16LEEncodeCase {
52     CFX_WideString ws;
53     CFX_ByteString bs;
54   } utf16le_encode_cases[] = {
55     { L"", ByteStringLiteral("\0\0") },
56     { L"abc", ByteStringLiteral("a\0b\0c\0\0\0") },
57     { L"abcdef", ByteStringLiteral("a\0b\0c\0d\0e\0f\0\0\0") },
58     { L"abc\0def", ByteStringLiteral("a\0b\0c\0\0\0") },
59     { L"\xaabb\xccdd", ByteStringLiteral("\xbb\xaa\xdd\xcc\0\0") },
60     { L"\x3132\x6162", ByteStringLiteral("\x32\x31\x62\x61\0\0") },
61   };
62
63   for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) {
64     EXPECT_EQ(utf16le_encode_cases[i].bs,
65         utf16le_encode_cases[i].ws.UTF16LE_Encode())
66         << " for case number " << i;
67   }
68 }