| 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 "UTF8StringSlice.hpp" |
| 20 | #include "TestUtils.hpp" |
| 21 | |
| 22 | namespace opencc { |
| 23 | |
| 24 | class UTF8StringSliceTest : public ::testing::Test { |
| 25 | protected: |
| 26 | UTF8StringSliceTest() |
| 27 | : text("天行健,君子以自強不息。地勢坤,君子以厚德載物。"), empty(""){}; |
| 28 | |
| 29 | UTF8StringSlice text; |
| 30 | const UTF8StringSlice empty; |
| 31 | }; |
| 32 | |
| 33 | TEST_F(UTF8StringSliceTest, UTF8Length) { |
| 34 | EXPECT_EQ(0, empty.UTF8Length()); |
| 35 | EXPECT_EQ(24, text.UTF8Length()); |
| 36 | } |
| 37 | |
| 38 | TEST_F(UTF8StringSliceTest, ByteLength) { |
| 39 | EXPECT_EQ(0, empty.ByteLength()); |
| 40 | EXPECT_EQ(72, text.ByteLength()); |
| 41 | } |
| 42 | |
| 43 | TEST_F(UTF8StringSliceTest, Left) { |
| 44 | EXPECT_EQ(UTF8StringSlice("天行健"), text.Left(3)); |
| 45 | } |
| 46 | |
| 47 | TEST_F(UTF8StringSliceTest, Right) { |
| 48 | EXPECT_EQ(UTF8StringSlice("厚德載物。"), text.Right(5)); |
| 49 | } |
| 50 | |
| 51 | TEST_F(UTF8StringSliceTest, SubString) { |
| 52 | EXPECT_EQ(UTF8StringSlice("自強不息"), text.SubString(7, 4)); |
| 53 | } |
| 54 | |
| 55 | TEST_F(UTF8StringSliceTest, ToString) { |
| 56 | EXPECT_EQ("地勢坤", text.SubString(12, 3).ToString()); |
| 57 | } |
| 58 | |
| 59 | TEST_F(UTF8StringSliceTest, Compare) { |
| 60 | EXPECT_TRUE(text.SubString(12, 3) > UTF8StringSlice("一")); |
| 61 | EXPECT_TRUE(text.SubString(12, 3) == UTF8StringSlice("地勢坤")); |
| 62 | } |
| 63 | |
| 64 | TEST_F(UTF8StringSliceTest, MoveRight) { |
| 65 | text.MoveRight(); |
| 66 | EXPECT_EQ(UTF8StringSlice("行健,君子以自強不息。地勢坤,君子以厚德載物。"), |
| 67 | text); |
| 68 | for (size_t i = 0; i < 23; i++) { |
| 69 | text.MoveRight(); |
| 70 | } |
| 71 | EXPECT_EQ(UTF8StringSlice(""), text); |
| 72 | text.MoveRight(); // No effect, because it's already empty |
| 73 | EXPECT_EQ(UTF8StringSlice(""), text); |
| 74 | } |
| 75 | |
| 76 | TEST_F(UTF8StringSliceTest, MoveLeft) { |
| 77 | text.MoveLeft(); |
| 78 | EXPECT_EQ(UTF8StringSlice("天行健,君子以自強不息。地勢坤,君子以厚德載物"), |
| 79 | text); |
| 80 | for (size_t i = 0; i < 22; i++) { |
| 81 | text.MoveLeft(); |
| 82 | } |
| 83 | EXPECT_EQ(UTF8StringSlice("天"), text); |
| 84 | text.MoveLeft(); |
| 85 | text.MoveLeft(); // No effect, because it's already empty |
| 86 | EXPECT_EQ(UTF8StringSlice(""), text); |
| 87 | } |
| 88 | |
| 89 | TEST_F(UTF8StringSliceTest, ReverseCompare) { |
| 90 | EXPECT_EQ(0, UTF8StringSlice("").ReverseCompare(UTF8StringSlice(""))); |
| 91 | EXPECT_EQ(-1, UTF8StringSlice("").ReverseCompare(UTF8StringSlice("大"))); |
| 92 | EXPECT_EQ(-1, UTF8StringSlice("一").ReverseCompare(UTF8StringSlice("二"))); |
| 93 | EXPECT_EQ(-1, UTF8StringSlice("z一").ReverseCompare(UTF8StringSlice("a二"))); |
| 94 | EXPECT_EQ(1, UTF8StringSlice("一一").ReverseCompare(UTF8StringSlice("一"))); |
| 95 | } |
| 96 | |
| 97 | TEST_F(UTF8StringSliceTest, FindBytePosition) { |
| 98 | EXPECT_EQ(0, text.FindBytePosition("")); |
| 99 | EXPECT_EQ(0, text.FindBytePosition("天")); |
| 100 | EXPECT_EQ(9, text.FindBytePosition(",")); |
| 101 | EXPECT_EQ(static_cast<UTF8StringSlice::LengthType>(-1), |
| 102 | text.FindBytePosition("a")); |
| 103 | EXPECT_EQ(static_cast<UTF8StringSlice::LengthType>(-1), |
| 104 | text.FindBytePosition("\n")); |
| 105 | EXPECT_EQ(3, UTF8StringSlice("了。").FindBytePosition("。")); |
| 106 | } |
| 107 | |
| 108 | } // namespace opencc |