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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
* Copyright (C) 2010 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.
*/
#define LOG_TAG "Unicode_test"
#include <utils/Log.h>
#include <utils/Unicode.h>
#include <gtest/gtest.h>
namespace android {
class UnicodeTest : public testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
char16_t const * const kSearchString = u"I am a leaf on the wind.";
};
TEST_F(UnicodeTest, UTF8toUTF16ZeroLength) {
ssize_t measured;
const uint8_t str[] = { };
measured = utf8_to_utf16_length(str, 0);
EXPECT_EQ(0, measured)
<< "Zero length input should return zero length output.";
}
TEST_F(UnicodeTest, UTF8toUTF16ASCIILength) {
ssize_t measured;
// U+0030 or ASCII '0'
const uint8_t str[] = { 0x30 };
measured = utf8_to_utf16_length(str, sizeof(str));
EXPECT_EQ(1, measured)
<< "ASCII glyphs should have a length of 1 char16_t";
}
TEST_F(UnicodeTest, UTF8toUTF16Plane1Length) {
ssize_t measured;
// U+2323 SMILE
const uint8_t str[] = { 0xE2, 0x8C, 0xA3 };
measured = utf8_to_utf16_length(str, sizeof(str));
EXPECT_EQ(1, measured)
<< "Plane 1 glyphs should have a length of 1 char16_t";
}
TEST_F(UnicodeTest, UTF8toUTF16SurrogateLength) {
ssize_t measured;
// U+10000
const uint8_t str[] = { 0xF0, 0x90, 0x80, 0x80 };
measured = utf8_to_utf16_length(str, sizeof(str));
EXPECT_EQ(2, measured)
<< "Surrogate pairs should have a length of 2 char16_t";
}
TEST_F(UnicodeTest, UTF8toUTF16TruncatedUTF8) {
ssize_t measured;
// Truncated U+2323 SMILE
// U+2323 SMILE
const uint8_t str[] = { 0xE2, 0x8C };
measured = utf8_to_utf16_length(str, sizeof(str));
EXPECT_EQ(-1, measured)
<< "Truncated UTF-8 should return -1 to indicate invalid";
}
TEST_F(UnicodeTest, UTF8toUTF16Normal) {
const uint8_t str[] = {
0x30, // U+0030, 1 UTF-16 character
0xC4, 0x80, // U+0100, 1 UTF-16 character
0xE2, 0x8C, 0xA3, // U+2323, 1 UTF-16 character
0xF0, 0x90, 0x80, 0x80, // U+10000, 2 UTF-16 character
};
char16_t output[1 + 1 + 1 + 2 + 1]; // Room for NULL
utf8_to_utf16(str, sizeof(str), output, sizeof(output) / sizeof(output[0]));
EXPECT_EQ(0x0030, output[0])
<< "should be U+0030";
EXPECT_EQ(0x0100, output[1])
<< "should be U+0100";
EXPECT_EQ(0x2323, output[2])
<< "should be U+2323";
EXPECT_EQ(0xD800, output[3])
<< "should be first half of surrogate U+10000";
EXPECT_EQ(0xDC00, output[4])
<< "should be second half of surrogate U+10000";
EXPECT_EQ(NULL, output[5])
<< "should be NULL terminated";
}
TEST_F(UnicodeTest, strstr16EmptyTarget) {
EXPECT_EQ(strstr16(kSearchString, u""), kSearchString)
<< "should return the original pointer";
}
TEST_F(UnicodeTest, strstr16SameString) {
const char16_t* result = strstr16(kSearchString, kSearchString);
EXPECT_EQ(kSearchString, result)
<< "should return the original pointer";
}
TEST_F(UnicodeTest, strstr16TargetStartOfString) {
const char16_t* result = strstr16(kSearchString, u"I am");
EXPECT_EQ(kSearchString, result)
<< "should return the original pointer";
}
TEST_F(UnicodeTest, strstr16TargetEndOfString) {
const char16_t* result = strstr16(kSearchString, u"wind.");
EXPECT_EQ(kSearchString+19, result);
}
TEST_F(UnicodeTest, strstr16TargetWithinString) {
const char16_t* result = strstr16(kSearchString, u"leaf");
EXPECT_EQ(kSearchString+7, result);
}
TEST_F(UnicodeTest, strstr16TargetNotPresent) {
const char16_t* result = strstr16(kSearchString, u"soar");
EXPECT_EQ(nullptr, result);
}
// http://b/29267949
// Test that overreading in utf8_to_utf16_length is detected
TEST_F(UnicodeTest, InvalidUtf8OverreadDetected) {
// An utf8 char starting with \xc4 is two bytes long.
// Add extra zeros so no extra memory is read in case the code doesn't
// work as expected.
static char utf8[] = "\xc4\x00\x00\x00";
ASSERT_DEATH(utf8_to_utf16_length((uint8_t *) utf8, strlen(utf8),
true /* overreadIsFatal */), "" /* regex for ASSERT_DEATH */);
}
}
|