1 /* 2 * Copyright (C) 2010 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 #define LOG_TAG "Unicode_test" 18 #include <utils/Log.h> 19 #include <utils/Unicode.h> 20 21 #include <gtest/gtest.h> 22 23 namespace android { 24 25 class UnicodeTest : public testing::Test { 26 protected: 27 virtual void SetUp() { 28 } 29 30 virtual void TearDown() { 31 } 32 }; 33 34 TEST_F(UnicodeTest, UTF8toUTF16ZeroLength) { 35 ssize_t measured; 36 37 const uint8_t str[] = { }; 38 39 measured = utf8_to_utf16_length(str, 0); 40 EXPECT_EQ(0, measured) 41 << "Zero length input should return zero length output."; 42 } 43 44 TEST_F(UnicodeTest, UTF8toUTF16ASCIILength) { 45 ssize_t measured; 46 47 // U+0030 or ASCII '0' 48 const uint8_t str[] = { 0x30 }; 49 50 measured = utf8_to_utf16_length(str, sizeof(str)); 51 EXPECT_EQ(1, measured) 52 << "ASCII glyphs should have a length of 1 char16_t"; 53 } 54 55 TEST_F(UnicodeTest, UTF8toUTF16Plane1Length) { 56 ssize_t measured; 57 58 // U+2323 SMILE 59 const uint8_t str[] = { 0xE2, 0x8C, 0xA3 }; 60 61 measured = utf8_to_utf16_length(str, sizeof(str)); 62 EXPECT_EQ(1, measured) 63 << "Plane 1 glyphs should have a length of 1 char16_t"; 64 } 65 66 TEST_F(UnicodeTest, UTF8toUTF16SurrogateLength) { 67 ssize_t measured; 68 69 // U+10000 70 const uint8_t str[] = { 0xF0, 0x90, 0x80, 0x80 }; 71 72 measured = utf8_to_utf16_length(str, sizeof(str)); 73 EXPECT_EQ(2, measured) 74 << "Surrogate pairs should have a length of 2 char16_t"; 75 } 76 77 TEST_F(UnicodeTest, UTF8toUTF16TruncatedUTF8) { 78 ssize_t measured; 79 80 // Truncated U+2323 SMILE 81 // U+2323 SMILE 82 const uint8_t str[] = { 0xE2, 0x8C }; 83 84 measured = utf8_to_utf16_length(str, sizeof(str)); 85 EXPECT_EQ(-1, measured) 86 << "Truncated UTF-8 should return -1 to indicate invalid"; 87 } 88 89 TEST_F(UnicodeTest, UTF8toUTF16Normal) { 90 const uint8_t str[] = { 91 0x30, // U+0030, 1 UTF-16 character 92 0xC4, 0x80, // U+0100, 1 UTF-16 character 93 0xE2, 0x8C, 0xA3, // U+2323, 1 UTF-16 character 94 0xF0, 0x90, 0x80, 0x80, // U+10000, 2 UTF-16 character 95 }; 96 97 char16_t output[1 + 1 + 1 + 2 + 1]; // Room for NULL 98 99 utf8_to_utf16(str, sizeof(str), output); 100 101 EXPECT_EQ(0x0030, output[0]) 102 << "should be U+0030"; 103 EXPECT_EQ(0x0100, output[1]) 104 << "should be U+0100"; 105 EXPECT_EQ(0x2323, output[2]) 106 << "should be U+2323"; 107 EXPECT_EQ(0xD800, output[3]) 108 << "should be first half of surrogate U+10000"; 109 EXPECT_EQ(0xDC00, output[4]) 110 << "should be second half of surrogate U+10000"; 111 EXPECT_EQ(NULL, output[5]) 112 << "should be NULL terminated"; 113 } 114 115 } 116