• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 
18 #include "text_converter.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace TextEngine {
26 namespace {
27 class TextConverterTest : public testing::Test {
28 };
29 
30 /**
31  * @tc.name: ToUTF8
32  * @tc.desc: Verify the ToUTF8
33  * @tc.type:FUNC
34  */
35 HWTEST_F(TextConverterTest, ToUTF8, TestSize.Level1)
36 {
37     UTF16String u16 = {0x00, 0x20, 0x41, 0x7f, 0x00, 0x80, 0x100, 0x601, 0x7ff, 0x800, 0xffff};
38     auto u8 = TextConverter::ToUTF8(u16);
39     EXPECT_EQ(u8[0], 0x00);
40     EXPECT_EQ(u8[1], 0x20);
41     EXPECT_EQ(u8[2], 0x41);
42     EXPECT_EQ(u8[3], 0x7f);
43     EXPECT_EQ(u8[4], 0x00);
44 
45     EXPECT_EQ(u8[5], 0b11000010);
46     EXPECT_EQ(u8[6], 0b10000000);
47 
48     EXPECT_EQ(u8[7], 0b11000100);
49     EXPECT_EQ(u8[8], 0b10000000);
50 
51     EXPECT_EQ(u8[9], 0b11011000);
52     EXPECT_EQ(u8[10], 0b10000001);
53 
54     EXPECT_EQ(u8[11], 0b11011111);
55     EXPECT_EQ(u8[12], 0b10111111);
56 
57     EXPECT_EQ(u8[13], 0b11100000);
58     EXPECT_EQ(u8[14], 0b10100000);
59     EXPECT_EQ(u8[15], 0b10000000);
60 
61     EXPECT_EQ(u8[16], 0b11101111);
62     EXPECT_EQ(u8[17], 0b10111111);
63     EXPECT_EQ(u8[18], 0b10111111);
64 }
65 
66 /**
67  * @tc.name: ToStr
68  * @tc.desc: Verify the ToStr
69  * @tc.type:FUNC
70  */
71 HWTEST_F(TextConverterTest, ToStr, TestSize.Level1)
72 {
73     UTF16String u16 = {0x20, 0x41, 0x7f, 0x80, 0x100, 0x601, 0x7ff, 0x800, 0xffff};
74     auto str = TextConverter::ToStr(u16);
75     EXPECT_EQ(str[0], static_cast<char>(0x20));
76     EXPECT_EQ(str[1], static_cast<char>(0x41));
77     EXPECT_EQ(str[2], static_cast<char>(0x7f));
78 
79     EXPECT_EQ(str[3], static_cast<char>(0b11000010));
80     EXPECT_EQ(str[4], static_cast<char>(0b10000000));
81 
82     EXPECT_EQ(str[5], static_cast<char>(0b11000100));
83     EXPECT_EQ(str[6], static_cast<char>(0b10000000));
84 
85     EXPECT_EQ(str[7], static_cast<char>(0b11011000));
86     EXPECT_EQ(str[8], static_cast<char>(0b10000001));
87 
88     EXPECT_EQ(str[9], static_cast<char>(0b11011111));
89     EXPECT_EQ(str[10], static_cast<char>(0b10111111));
90 
91     EXPECT_EQ(str[11], static_cast<char>(0b11100000));
92     EXPECT_EQ(str[12], static_cast<char>(0b10100000));
93     EXPECT_EQ(str[13], static_cast<char>(0b10000000));
94 
95     EXPECT_EQ(str[14], static_cast<char>(0b11101111));
96     EXPECT_EQ(str[15], static_cast<char>(0b10111111));
97     EXPECT_EQ(str[16], static_cast<char>(0b10111111));
98 }
99 
100 /**
101  * @tc.name: ToUTF16
102  * @tc.desc: Verify the ToUTF16
103  * @tc.type:FUNC
104  */
105 HWTEST_F(TextConverterTest, ToUTF16, TestSize.Level1)
106 {
107     auto u16 = TextConverter::ToUTF16("\U0001ffff\uffff a1");
108     EXPECT_EQ(u16[0], 0xd83f);
109     EXPECT_EQ(u16[1], 0xdfff);
110     EXPECT_EQ(u16[2], 0xffff);
111     EXPECT_EQ(u16[3], static_cast<uint16_t>(' '));
112     EXPECT_EQ(u16[4], static_cast<uint16_t>('a'));
113     EXPECT_EQ(u16[5], static_cast<uint16_t>('1'));
114 
115     u16 = TextConverter::ToUTF16(UTF8String{
116             0b11110000, 0b10011111, 0b10111111, 0b10111111, // 1ffff
117             0b11101111, 0b10111111, 0b10111111, // ffff
118             0x20, 0x61, 0x31
119     });
120     EXPECT_EQ(u16[0], 0xd83f);
121     EXPECT_EQ(u16[1], 0xdfff);
122     EXPECT_EQ(u16[2], 0xffff);
123     EXPECT_EQ(u16[3], static_cast<uint16_t>(' '));
124     EXPECT_EQ(u16[4], static_cast<uint16_t>('a'));
125     EXPECT_EQ(u16[5], static_cast<uint16_t>('1'));
126 
127     u16 = TextConverter::ToUTF16(UTF32String{0x1ffff, 0xffff, 0x20, 0x61, 0x31});
128     EXPECT_EQ(u16[0], 0xd83f);
129     EXPECT_EQ(u16[1], 0xdfff);
130     EXPECT_EQ(u16[2], 0xffff);
131     EXPECT_EQ(u16[3], static_cast<uint16_t>(' '));
132     EXPECT_EQ(u16[4], static_cast<uint16_t>('a'));
133     EXPECT_EQ(u16[5], static_cast<uint16_t>('1'));
134 }
135 
136 /**
137  * @tc.name: ToUTF32
138  * @tc.desc: Verify the ToUTF32
139  * @tc.type:FUNC
140  */
141 HWTEST_F(TextConverterTest, ToUTF32, TestSize.Level1)
142 {
143     auto u32 = TextConverter::ToUTF32(UTF16String{0xd83f, 0xdfff, 0xffff, 0x20, 0x61, 0x31});
144     EXPECT_EQ(u32[0], 0x1ffff);
145     EXPECT_EQ(u32[1], 0xffff);
146     EXPECT_EQ(u32[2], 0x20);
147     EXPECT_EQ(u32[3], 0x61);
148     EXPECT_EQ(u32[4], 0x31);
149 }
150 } // namespace
151 } // namespace TextEngine
152 } // namespace Rosen
153 } // namespace OHOS
154