Debian Codegraph @

src/UTF8UtilTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2015-2026 Carbo Kuo and contributors
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "UTF8Util.hpp"
20 #include "TestUtils.hpp"
21
22 namespace opencc {
23
24 class UTF8UtilTest : public ::testing::Test {
25 protected:
26 UTF8UtilTest() : text("東菄鶇䍶𠍀倲𩜍𢘐"), length(strlen(text)){};
27 const char* text;
28 const size_t length;
29 };
30
31 TEST_F(UTF8UtilTest, NextCharLength) {
32 EXPECT_EQ(3, UTF8Util::NextCharLength(text));
33 EXPECT_EQ(3, UTF8Util::NextCharLength(text + 3));
34 EXPECT_EQ(3, UTF8Util::NextCharLength(text + 6));
35 EXPECT_EQ(3, UTF8Util::NextCharLength(text + 9));
36 EXPECT_EQ(4, UTF8Util::NextCharLength(text + 12));
37 EXPECT_EQ(3, UTF8Util::NextCharLength(text + 16));
38 EXPECT_EQ(4, UTF8Util::NextCharLength(text + 19));
39 EXPECT_EQ(4, UTF8Util::NextCharLength(text + 23));
40 EXPECT_THROW(EXPECT_EQ(3, UTF8Util::NextCharLength(text + 1)), InvalidUTF8);
41 EXPECT_THROW(EXPECT_EQ(3, UTF8Util::NextCharLength(text + 2)), InvalidUTF8);
42 }
43
44 TEST_F(UTF8UtilTest, PrevCharLength) {
45 EXPECT_EQ(4, UTF8Util::PrevCharLength(text + length));
46 EXPECT_EQ(4, UTF8Util::PrevCharLength(text + length - 4));
47 EXPECT_EQ(3, UTF8Util::PrevCharLength(text + length - 8));
48 EXPECT_THROW(EXPECT_EQ(3, UTF8Util::PrevCharLength(text + 1)), InvalidUTF8);
49 }
50
51 TEST(UTF8UtilASCIITest, PrevCharLengthASCII) {
52 // Heap-allocated ASCII string to catch issue #794 (OOB read with ASCII input)
53 const std::string asciiStr = "abc";
54 EXPECT_EQ(1,
55 UTF8Util::PrevCharLength(asciiStr.c_str() + asciiStr.size()));
56 EXPECT_EQ(1, UTF8Util::PrevCharLength(asciiStr.c_str() + 2));
57 EXPECT_EQ(1, UTF8Util::PrevCharLength(asciiStr.c_str() + 1));
58 }
59
60 TEST(UTF8UtilTruncatedTest, LengthTruncatedSequence) {
61 // Length() must throw InvalidUTF8 for truncated sequences instead of
62 // reading past the null terminator (issue #799 fix).
63 EXPECT_THROW(UTF8Util::Length("\xE0"), InvalidUTF8); // incomplete 3-byte sequence
64 EXPECT_THROW(UTF8Util::Length("\xF0"), InvalidUTF8); // incomplete 4-byte sequence
65 EXPECT_THROW(UTF8Util::Length("\xE0\xBF"), InvalidUTF8); // still incomplete 3-byte
66 }
67
68 TEST_F(UTF8UtilTest, Length) {
69 EXPECT_EQ(0, UTF8Util::Length(""));
70 EXPECT_EQ(8, UTF8Util::Length(text));
71 }
72
73 TEST_F(UTF8UtilTest, NotShorterThan) {
74 EXPECT_TRUE(UTF8Util::NotShorterThan(text, 0));
75 EXPECT_TRUE(UTF8Util::NotShorterThan(text, length));
76 EXPECT_FALSE(UTF8Util::NotShorterThan(text, length + 1));
77 }
78
79 TEST_F(UTF8UtilTest, TruncateUTF8) {
80 EXPECT_EQ("", UTF8Util::TruncateUTF8(text, 0));
81 EXPECT_EQ("", UTF8Util::TruncateUTF8(text, 1));
82 EXPECT_EQ("東", UTF8Util::TruncateUTF8(text, 3));
83 EXPECT_EQ("東", UTF8Util::TruncateUTF8(text, 4));
84 EXPECT_EQ("東菄鶇䍶𠍀", UTF8Util::TruncateUTF8(text, 16));
85 EXPECT_EQ(text, UTF8Util::TruncateUTF8(text, length));
86 EXPECT_EQ(text, UTF8Util::TruncateUTF8(text, length + 1));
87 }
88
89 TEST(UTF8UtilIDSTest, IdeographicDescriptionSequenceLength) {
90 const std::string sequence = "⿰钅只只";
91 EXPECT_EQ(strlen("⿰钅只"),
92 UTF8Util::NextIdeographicDescriptionSequenceLength(
93 sequence.c_str(), sequence.length()));
94 }
95
96 TEST(UTF8UtilIDSTest, NestedIdeographicDescriptionSequenceLength) {
97 const std::string sequence = "⿱艹⿰钅只干";
98 EXPECT_EQ(strlen("⿱艹⿰钅只"),
99 UTF8Util::NextIdeographicDescriptionSequenceLength(
100 sequence.c_str(), sequence.length()));
101 }
102
103 TEST(UTF8UtilIDSTest, ComplexIdeographicDescriptionSequenceLength) {
104 const std::string sequence = "⿺⻍⿳穴⿲月⿱⿲幺言幺⿲長馬長刂心長";
105 EXPECT_EQ(strlen("⿺⻍⿳穴⿲月⿱⿲幺言幺⿲長馬長刂心"),
106 UTF8Util::NextIdeographicDescriptionSequenceLength(
107 sequence.c_str(), sequence.length()));
108 }
109
110 TEST(UTF8UtilIDSTest, OperatorOnlyIdeographicDescriptionSequenceReturnsZero) {
111 const std::string sequence = "⿰⿱⿲⿳⿴⿵⿶⿷⿸⿹⿺⿻⿼⿽⿾⿿";
112 EXPECT_EQ(0, UTF8Util::NextIdeographicDescriptionSequenceLength(
113 sequence.c_str(), sequence.length()));
114 }
115
116 TEST(UTF8UtilIDSTest, IsolatedIdeographicDescriptionCharactersAreNotSequences) {
117 const std::vector<std::string> operators = {
118 "⿰", "⿱", "⿲", "⿳", "⿴", "⿵", "⿶", "⿷",
119 "⿸", "⿹", "⿺", "⿻", "⿼", "⿽", "⿾", "⿿"};
120 for (const std::string& op : operators) {
121 EXPECT_EQ(0, UTF8Util::NextIdeographicDescriptionSequenceLength(
122 op.c_str(), op.length()))
123 << op;
124 }
125 }
126
127 TEST(UTF8UtilIDSTest, IncompleteIdeographicDescriptionSequenceReturnsZero) {
128 const std::string sequence = "⿰钅";
129 EXPECT_EQ(0, UTF8Util::NextIdeographicDescriptionSequenceLength(
130 sequence.c_str(), sequence.length()));
131 }
132
133 TEST(UTF8UtilIDSTest, OverlyDeepIdeographicDescriptionSequenceReturnsZero) {
134 const std::string sequence = "⿰⿰⿰⿰⿰⿰⿰⿰⿰⿰⿰⿰⿰⿰⿰⿰⿰木木木木木木木木木木木木木木木木木木";
135 EXPECT_EQ(0, UTF8Util::NextIdeographicDescriptionSequenceLength(
136 sequence.c_str(), sequence.length()));
137 }
138
139 TEST(UTF8UtilIDSTest, OverlyLongIdeographicDescriptionSequenceReturnsZero) {
140 std::string sequence;
141 for (size_t i = 0; i < 64; i++) {
142 sequence += "⿰";
143 }
144 for (size_t i = 0; i < 65; i++) {
145 sequence += "木";
146 }
147 EXPECT_EQ(0, UTF8Util::NextIdeographicDescriptionSequenceLength(
148 sequence.c_str(), sequence.length()));
149 }
150
151 TEST(UTF8UtilVariationSelectorTest, ContainsVariationSelector) {
152 const std::string bmpVariationSelector = std::string("禰") + "\xEF\xB8\x80";
153 const std::string supplementaryVariationSelector =
154 std::string("禰") + "\xF3\xA0\x84\x80";
155 EXPECT_TRUE(UTF8Util::ContainsVariationSelector(
156 bmpVariationSelector.c_str(), bmpVariationSelector.length()));
157 EXPECT_TRUE(UTF8Util::ContainsVariationSelector(
158 supplementaryVariationSelector.c_str(),
159 supplementaryVariationSelector.length()));
160 const std::string textWithoutVariationSelector = "東菄鶇䍶𠍀倲𩜍𢘐";
161 EXPECT_FALSE(UTF8Util::ContainsVariationSelector(
162 textWithoutVariationSelector.c_str(),
163 textWithoutVariationSelector.length()));
164 }
165
166 TEST_F(UTF8UtilTest, GetByteMap) {
167 std::vector<size_t> byteMap;
168 UTF8Util::GetByteMap(text, 6, &byteMap);
169 EXPECT_EQ(std::vector<size_t>({0, 3, 6, 9, 12, 16}), byteMap);
170 }
171
172 } // namespace opencc