/* * Copyright (C) 2018 The Android Open Source Project * * 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 "utils/strings/utf8.h" #include "gmock/gmock.h" #include "gtest/gtest.h" namespace libtextclassifier3 { namespace { TEST(Utf8Test, ComputesUtf8LengthOfUnicodeCharacters) { EXPECT_EQ(GetNumBytesForUTF8Char("\x00"), 1); EXPECT_EQ(GetNumBytesForUTF8Char("h"), 1); EXPECT_EQ(GetNumBytesForUTF8Char("๐Ÿ˜‹"), 4); EXPECT_EQ(GetNumBytesForUTF8Char("ใฟ"), 3); } TEST(Utf8Test, IsValidUTF8) { EXPECT_TRUE(IsValidUTF8("1234๐Ÿ˜‹hello", 13)); EXPECT_TRUE(IsValidUTF8("\u304A\u00B0\u106B", 8)); EXPECT_TRUE(IsValidUTF8("this is a test๐Ÿ˜‹๐Ÿ˜‹๐Ÿ˜‹", 26)); EXPECT_TRUE(IsValidUTF8("\xf0\x9f\x98\x8b", 4)); // Example with first byte payload of zero. EXPECT_TRUE(IsValidUTF8("\xf0\x90\x80\x80", 4)); // Too short (string is too short). EXPECT_FALSE(IsValidUTF8("\xf0\x9f", 2)); // Too long (too many trailing bytes). EXPECT_FALSE(IsValidUTF8("\xf0\x9f\x98\x8b\x8b", 5)); // Too short (too few trailing bytes). EXPECT_FALSE(IsValidUTF8("\xf0\x9f\x98\x61\x61", 5)); // Invalid continuation byte (can be encoded in less bytes). EXPECT_FALSE(IsValidUTF8("\xc0\x81", 2)); // Invalid continuation byte (can be encoded in less bytes). EXPECT_FALSE(IsValidUTF8("\xf0\x8a\x85\x8f", 4)); } TEST(Utf8Test, CorrectlyTruncatesStrings) { EXPECT_EQ(SafeTruncateLength("FooBar", 3), 3); EXPECT_EQ(SafeTruncateLength("frรผh", 3), 2); EXPECT_EQ(SafeTruncateLength("ู…ูŽู…ูู…ู‘ู…ูŽู‘ู…ูู‘", 5), 4); } TEST(Utf8Test, CorrectlyConvertsFromUtf8) { EXPECT_EQ(ValidCharToRune("a"), 97); EXPECT_EQ(ValidCharToRune("\0"), 0); EXPECT_EQ(ValidCharToRune("\u304A"), 0x304a); EXPECT_EQ(ValidCharToRune("\xe3\x81\x8a"), 0x304a); } TEST(Utf8Test, CorrectlyConvertsToUtf8) { char utf8_encoding[4]; EXPECT_EQ(ValidRuneToChar(97, utf8_encoding), 1); EXPECT_EQ(ValidRuneToChar(0, utf8_encoding), 1); EXPECT_EQ(ValidRuneToChar(0x304a, utf8_encoding), 3); } } // namespace } // namespace libtextclassifier3