-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathUTF8UtilTest.cpp
78 lines (68 loc) · 2.7 KB
/
UTF8UtilTest.cpp
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
/*
* Open Chinese Convert
*
* Copyright 2015 Carbo Kuo <byvoid@byvoid.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "UTF8Util.hpp"
#include "TestUtils.hpp"
namespace opencc {
class UTF8UtilTest : public ::testing::Test {
protected:
UTF8UtilTest() : text("東菄鶇䍶𠍀倲𩜍𢘐"), length(strlen(text)){};
const char* text;
const size_t length;
};
TEST_F(UTF8UtilTest, NextCharLength) {
EXPECT_EQ(3, UTF8Util::NextCharLength(text));
EXPECT_EQ(3, UTF8Util::NextCharLength(text + 3));
EXPECT_EQ(3, UTF8Util::NextCharLength(text + 6));
EXPECT_EQ(3, UTF8Util::NextCharLength(text + 9));
EXPECT_EQ(4, UTF8Util::NextCharLength(text + 12));
EXPECT_EQ(3, UTF8Util::NextCharLength(text + 16));
EXPECT_EQ(4, UTF8Util::NextCharLength(text + 19));
EXPECT_EQ(4, UTF8Util::NextCharLength(text + 23));
EXPECT_THROW(EXPECT_EQ(3, UTF8Util::NextCharLength(text + 1)), InvalidUTF8);
EXPECT_THROW(EXPECT_EQ(3, UTF8Util::NextCharLength(text + 2)), InvalidUTF8);
}
TEST_F(UTF8UtilTest, PrevCharLength) {
EXPECT_EQ(4, UTF8Util::PrevCharLength(text + length));
EXPECT_EQ(4, UTF8Util::PrevCharLength(text + length - 4));
EXPECT_EQ(3, UTF8Util::PrevCharLength(text + length - 8));
EXPECT_THROW(EXPECT_EQ(3, UTF8Util::PrevCharLength(text + 1)), InvalidUTF8);
}
TEST_F(UTF8UtilTest, Length) {
EXPECT_EQ(0, UTF8Util::Length(""));
EXPECT_EQ(8, UTF8Util::Length(text));
}
TEST_F(UTF8UtilTest, NotShorterThan) {
EXPECT_TRUE(UTF8Util::NotShorterThan(text, 0));
EXPECT_TRUE(UTF8Util::NotShorterThan(text, length));
EXPECT_FALSE(UTF8Util::NotShorterThan(text, length + 1));
}
TEST_F(UTF8UtilTest, TruncateUTF8) {
EXPECT_EQ("", UTF8Util::TruncateUTF8(text, 0));
EXPECT_EQ("", UTF8Util::TruncateUTF8(text, 1));
EXPECT_EQ("東", UTF8Util::TruncateUTF8(text, 3));
EXPECT_EQ("東", UTF8Util::TruncateUTF8(text, 4));
EXPECT_EQ("東菄鶇䍶𠍀", UTF8Util::TruncateUTF8(text, 16));
EXPECT_EQ(text, UTF8Util::TruncateUTF8(text, length));
EXPECT_EQ(text, UTF8Util::TruncateUTF8(text, length + 1));
}
TEST_F(UTF8UtilTest, GetByteMap) {
std::vector<size_t> byteMap;
UTF8Util::GetByteMap(text, 6, &byteMap);
EXPECT_EQ(std::vector<size_t>({0, 3, 6, 9, 12, 16}), byteMap);
}
} // namespace opencc