1 /*
2  * Copyright (c) 2021-2022 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 #define private public
19 #define protected public
20 #include "zchar_wrapper.h"
21 #undef private
22 #undef protected
23 
24 using namespace OHOS;
25 using namespace OHOS::AAFwk;
26 using testing::ext::TestSize;
27 
28 namespace OHOS {
29 namespace AAFwk {
30 class AAfWKZcharWrapperTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase() {};
TearDownTestCase()33     static void TearDownTestCase() {};
SetUp()34     void SetUp() {};
TearDown()35     void TearDown() {};
36 };
37 
38 /**
39  * @tc.number: ZcharWrapperTest_GetValue_001
40  * @tc.name: GetValue
41  * @tc.desc:
42  */
43 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetValue_001, TestSize.Level1)
44 {
45   zchar value = 0;
46   Char zcharValue(value);
47   ErrCode result = zcharValue.GetValue(value);
48   EXPECT_EQ(ERR_OK, result);
49 }
50 
51 /**
52  * @tc.number: ZcharWrapperTest_Box_001
53  * @tc.name: Box
54  * @tc.desc:
55  */
56 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_Box_001, TestSize.Level1)
57 {
58   char value = 0;
59   Char charValue(value);
60   char result = charValue.Unbox(charValue.Box(value));
61   EXPECT_EQ(0, result);
62 }
63 
64 /**
65  * @tc.number: ZcharWrapperTest_GetByteSize_001
66  * @tc.name: GetByteSize
67  * @tc.desc:
68  */
69 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetByteSize_001, TestSize.Level1)
70 {
71     zchar value = 0xD801;
72     Char zcharValue(value);
73     EXPECT_EQ(0, zcharValue.GetByteSize(value));
74 }
75 
76 /**
77  * @tc.number: ZcharWrapperTest_GetByteSize_002
78  * @tc.name: GetByteSize
79  * @tc.desc:
80  */
81 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetByteSize_002, TestSize.Level1)
82 {
83     zchar value = 0x10FFFF + 1;
84     Char zcharValue(value);
85     EXPECT_EQ(0, zcharValue.GetByteSize(value));
86 }
87 
88 /**
89  * @tc.number: ZcharWrapperTest_GetByteSize_003
90  * @tc.name: GetByteSize
91  * @tc.desc:
92  */
93 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetByteSize_003, TestSize.Level1)
94 {
95     zchar value = 0x00000800 - 1;
96     Char zcharValue(value);
97     EXPECT_EQ(2, zcharValue.GetByteSize(value));
98 }
99 
100 /**
101  * @tc.number: ZcharWrapperTest_GetByteSize_004
102  * @tc.name: GetByteSize
103  * @tc.desc:
104  */
105 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetByteSize_004, TestSize.Level1)
106 {
107     zchar value = 0x00010000 - 1;
108     Char zcharValue(value);
109     EXPECT_EQ(3, zcharValue.GetByteSize(value));
110 }
111 
112 /**
113  * @tc.number: ZcharWrapperTest_GetByteSize_005
114  * @tc.name: GetByteSize
115  * @tc.desc:
116  */
117 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetByteSize_005, TestSize.Level1)
118 {
119     zchar value = 0x00010000 + 1;
120     Char zcharValue(value);
121     EXPECT_EQ(4, zcharValue.GetByteSize(value));
122 }
123 
124 /**
125  * @tc.number: ZcharWrapperTest_WriteUTF8Bytes_001
126  * @tc.name: WriteUTF8Bytes
127  * @tc.desc:
128  */
129 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_WriteUTF8Bytes_001, TestSize.Level1)
130 {
131     zchar value = 0;
132     Char zcharValue(value);
133     char *dst = new char [4];
134     int32_t size = 4;
135     zcharValue.WriteUTF8Bytes(dst, value, size);
136 }
137 
138 /**
139  * @tc.number: ZcharWrapperTest_WriteUTF8Bytes_002
140  * @tc.name: WriteUTF8Bytes
141  * @tc.desc:
142  */
143 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_WriteUTF8Bytes_002, TestSize.Level1)
144 {
145     zchar value = 0;
146     Char zcharValue(value);
147     char *dst = new char [4];
148     int32_t size = 3;
149     zcharValue.WriteUTF8Bytes(dst, value, size);
150 }
151 
152 /**
153  * @tc.number: ZcharWrapperTest_WriteUTF8Bytes_003
154  * @tc.name: WriteUTF8Bytes
155  * @tc.desc:
156  */
157 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_WriteUTF8Bytes_003, TestSize.Level1)
158 {
159     zchar value = 0;
160     Char zcharValue(value);
161     char *dst = new char [4];
162     int32_t size = 2;
163     zcharValue.WriteUTF8Bytes(dst, value, size);
164 }
165 
166 /**
167  * @tc.number: ZcharWrapperTest_GetChar_001
168  * @tc.name: GetChar
169  * @tc.desc:
170  */
171 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetChar_001, TestSize.Level1)
172 {
173     zchar value = 0;
174     Char zcharValue(value);
175     std::string str = "";
176     int32_t index = 2;
177     EXPECT_EQ(Char::INVALID_CHAR, zcharValue.GetChar(str, index));
178 }
179 
180 /**
181  * @tc.number: ZcharWrapperTest_GetChar_002
182  * @tc.name: GetChar
183  * @tc.desc:
184  */
185 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetChar_002, TestSize.Level1)
186 {
187     zchar value = 0;
188     Char zcharValue(value);
189     std::string str = "string";
190     int32_t index = -1;
191     EXPECT_EQ(Char::INVALID_CHAR, zcharValue.GetChar(str, index));
192 }
193 
194 /**
195  * @tc.number: ZcharWrapperTest_GetChar_003
196  * @tc.name: GetChar
197  * @tc.desc:
198  */
199 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetChar_003, TestSize.Level1)
200 {
201     zchar value = 0;
202     Char zcharValue(value);
203     std::string str = "string";
204     int32_t index = 10;
205     EXPECT_EQ(Char::INVALID_CHAR, zcharValue.GetChar(str, index));
206 }
207 
208 /**
209  * @tc.number: ZcharWrapperTest_GetCharInternal_001
210  * @tc.name: GetCharInternal
211  * @tc.desc:
212  */
213 HWTEST_F(AAfWKZcharWrapperTest, ZcharWrapperTest_GetCharInternal_001, TestSize.Level1)
214 {
215     zchar value = 0;
216     Char zcharValue(value);
217     const char* str = u8"你好";
218     unsigned char* cur = reinterpret_cast<unsigned char*>(const_cast<char*>(str));
219     int32_t size = 0;
220     zcharValue.GetCharInternal(cur, size);
221     EXPECT_EQ(3, size);
222 }
223 }
224 }