summaryrefslogtreecommitdiffstats
path: root/libutils/tests/Unicode_test.cpp
blob: 18c130c55474bcbbad5a8a5b2f30f99fb0dc539a (plain)
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
/*
 * 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() {
    }
};

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);

    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";
}

}