1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "utils/strings/utf8.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21
22 namespace libtextclassifier3 {
23 namespace {
24
TEST(Utf8Test,ComputesUtf8LengthOfUnicodeCharacters)25 TEST(Utf8Test, ComputesUtf8LengthOfUnicodeCharacters) {
26 EXPECT_EQ(GetNumBytesForUTF8Char("\x00"), 1);
27 EXPECT_EQ(GetNumBytesForUTF8Char("h"), 1);
28 EXPECT_EQ(GetNumBytesForUTF8Char(""), 4);
29 EXPECT_EQ(GetNumBytesForUTF8Char("㍿"), 3);
30 }
31
TEST(Utf8Test,IsValidUTF8)32 TEST(Utf8Test, IsValidUTF8) {
33 EXPECT_TRUE(IsValidUTF8("1234hello", 13));
34 EXPECT_TRUE(IsValidUTF8("\u304A\u00B0\u106B", 8));
35 EXPECT_TRUE(IsValidUTF8("this is a test", 26));
36 EXPECT_TRUE(IsValidUTF8("\xf0\x9f\x98\x8b", 4));
37 // Too short (string is too short).
38 EXPECT_FALSE(IsValidUTF8("\xf0\x9f", 2));
39 // Too long (too many trailing bytes).
40 EXPECT_FALSE(IsValidUTF8("\xf0\x9f\x98\x8b\x8b", 5));
41 // Too short (too few trailing bytes).
42 EXPECT_FALSE(IsValidUTF8("\xf0\x9f\x98\x61\x61", 5));
43 }
44
TEST(Utf8Test,ValidUTF8CharLength)45 TEST(Utf8Test, ValidUTF8CharLength) {
46 EXPECT_EQ(ValidUTF8CharLength("1234hello", 13), 1);
47 EXPECT_EQ(ValidUTF8CharLength("\u304A\u00B0\u106B", 8), 3);
48 EXPECT_EQ(ValidUTF8CharLength("this is a test", 26), 1);
49 EXPECT_EQ(ValidUTF8CharLength("\xf0\x9f\x98\x8b", 4), 4);
50 // Too short (string is too short).
51 EXPECT_EQ(ValidUTF8CharLength("\xf0\x9f", 2), -1);
52 // Too long (too many trailing bytes). First character is valid.
53 EXPECT_EQ(ValidUTF8CharLength("\xf0\x9f\x98\x8b\x8b", 5), 4);
54 // Too short (too few trailing bytes).
55 EXPECT_EQ(ValidUTF8CharLength("\xf0\x9f\x98\x61\x61", 5), -1);
56 }
57
TEST(Utf8Test,CorrectlyTruncatesStrings)58 TEST(Utf8Test, CorrectlyTruncatesStrings) {
59 EXPECT_EQ(SafeTruncateLength("FooBar", 3), 3);
60 EXPECT_EQ(SafeTruncateLength("früh", 3), 2);
61 EXPECT_EQ(SafeTruncateLength("مَمِمّمَّمِّ", 5), 4);
62 }
63
TEST(Utf8Test,CorrectlyConvertsFromUtf8)64 TEST(Utf8Test, CorrectlyConvertsFromUtf8) {
65 EXPECT_EQ(ValidCharToRune("a"), 97);
66 EXPECT_EQ(ValidCharToRune("\0"), 0);
67 EXPECT_EQ(ValidCharToRune("\u304A"), 0x304a);
68 EXPECT_EQ(ValidCharToRune("\xe3\x81\x8a"), 0x304a);
69 }
70
TEST(Utf8Test,CorrectlyConvertsToUtf8)71 TEST(Utf8Test, CorrectlyConvertsToUtf8) {
72 char utf8_encoding[4];
73 EXPECT_EQ(ValidRuneToChar(97, utf8_encoding), 1);
74 EXPECT_EQ(ValidRuneToChar(0, utf8_encoding), 1);
75 EXPECT_EQ(ValidRuneToChar(0x304a, utf8_encoding), 3);
76 }
77
78 } // namespace
79 } // namespace libtextclassifier3
80