1 /*
2 * Copyright (c) 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 <limits.h>
17 #include <locale.h>
18 #include <string.h>
19 #include <uchar.h>
20 #include "test.h"
21
22 /**
23 * @tc.name : c16rtomb_0100
24 * @tc.desc : Converts a variable-length 16-bit wide character representation to its narrow multibyte character
25 * representation.
26 * @tc.level : Level 0
27 */
c16rtomb_0100(void)28 void c16rtomb_0100(void)
29 {
30 char bytes[MB_LEN_MAX];
31 memset(bytes, 0, sizeof(bytes));
32 size_t result = c16rtomb(bytes, L'h', NULL);
33 if (result != 1U) {
34 t_error("%s c16rtomb failed, result is %d\n", __func__, result);
35 }
36 if (bytes[0] != 'h') {
37 t_error("%s bytes[0] is %c, not \'h\'\n", bytes[0]);
38 }
39
40 char *ret = setlocale(LC_CTYPE, "C.UTF-8");
41 if (strcmp(ret, "C.UTF-8")) {
42 t_error("%s setlocale failed\n", __func__);
43 }
44 uselocale(LC_GLOBAL_LOCALE);
45
46 // 1-byte UTF-8.
47 memset(bytes, 0, sizeof(bytes));
48 result = c16rtomb(bytes, L'h', NULL);
49 if (result != 1U) {
50 t_error("%s c16rtomb failed, result is %d\n", __func__, result);
51 }
52 if (bytes[0] != 'h') {
53 t_error("%s bytes[0] is %c, not \'h\'\n", bytes[0]);
54 }
55
56 // 2-byte UTF-8.
57 memset(bytes, 0, sizeof(bytes));
58 result = c16rtomb(bytes, 0x00a2, NULL);
59 if (result != 2U) {
60 t_error("%s c16rtomb failed, result is %d\n", __func__, result);
61 }
62 if (bytes[0] != '\xc2') {
63 t_error("%s bytes[0] is %c, not \'\\xc2\'\n", bytes[0]);
64 }
65 if (bytes[1] != '\xa2') {
66 t_error("%s bytes[0] is %c, not \'\\xa2\'\n", bytes[1]);
67 }
68
69 // 3-byte UTF-8.
70 memset(bytes, 0, sizeof(bytes));
71 result = c16rtomb(bytes, 0x20ac, NULL);
72 if (result != 3U) {
73 t_error("%s c16rtomb failed, result is %d\n", __func__, result);
74 }
75 if (bytes[0] != '\xe2') {
76 t_error("%s bytes[0] is %c, not \'\\xe2\'\n", bytes[0]);
77 }
78 if (bytes[1] != '\x82') {
79 t_error("%s bytes[0] is %c, not \'\\x82\'\n", bytes[1]);
80 }
81 if (bytes[2] != '\xac') {
82 t_error("%s bytes[0] is %c, not \'\\xac\'\n", bytes[2]);
83 }
84 }
85
86 /**
87 * @tc.name : c16rtomb_0200
88 * @tc.desc : When the s parameter is invalid, test the return value of the function.
89 * @tc.level : Level 1
90 */
c16rtomb_0200(void)91 void c16rtomb_0200(void)
92 {
93 size_t result = c16rtomb(NULL, L'\0', NULL);
94 if (result != 1U) {
95 t_error("%s c16rtomb failed, result is %d\n", __func__, result);
96 }
97
98 result = c16rtomb(NULL, L'h', NULL);
99 if (result != 1U) {
100 t_error("%s c16rtomb failed, result is %d\n", __func__, result);
101 }
102 }
103
104 /**
105 * @tc.name : c16rtomb_0300
106 * @tc.desc : Test the return value of this function when c16 is equal to 0xdbea.
107 * @tc.leve : Level 1
108 */
c16rtomb_0300(void)109 void c16rtomb_0300(void)
110 {
111 char bytes[MB_LEN_MAX];
112 memset(bytes, 0, sizeof(bytes));
113 size_t result = c16rtomb(bytes, 0xdbea, NULL);
114 if (result != 0U) {
115 t_error("%s c16rtomb failed\n", __func__);
116 }
117 }
118
119 /**
120 * @tc.name : c16rtomb_0400
121 * @tc.desc : Test the return value of this function when c16 is equal to 0xdfcd.
122 * @tc.leve : Level 1
123 */
c16rtomb_0400(void)124 void c16rtomb_0400(void)
125 {
126 char bytes[MB_LEN_MAX];
127 memset(bytes, 0, sizeof(bytes));
128 size_t result = c16rtomb(bytes, 0xdfcd, NULL);
129 if (result != 4U) {
130 t_error("%s c16rtomb failed\n", __func__);
131 }
132
133 if (bytes[0] != '\xf4') {
134 t_error("%s bytes[0] is %c, not \'\\xf4\'\n", bytes[0]);
135 }
136 if (bytes[1] != '\x8a') {
137 t_error("%s bytes[0] is %c, not \'\\x8a\'\n", bytes[1]);
138 }
139 if (bytes[2] != '\xaf') {
140 t_error("%s bytes[0] is %c, not \'\\xaf\'\n", bytes[2]);
141 }
142 if (bytes[3] != '\x8d') {
143 t_error("%s bytes[0] is %c, not \'\\x8d\'\n", bytes[3]);
144 }
145 }
146
147 /**
148 * @tc.name : c16rtomb_0500
149 * @tc.desc : Test the return value of this function when c16 is equal to 0xdfcd.
150 * @tc.leve : Level 2
151 */
c16rtomb_0500(void)152 void c16rtomb_0500(void)
153 {
154 char bytes[MB_LEN_MAX];
155 memset(bytes, 0, sizeof(bytes));
156 size_t result = c16rtomb(bytes, 0xdfcd, NULL);
157 if (result != (size_t)-1) {
158 t_error("%s c16rtomb failed\n", __func__);
159 }
160 }
161
main(int argc,char * argv[])162 int main(int argc, char *argv[])
163 {
164 c16rtomb_0100();
165 c16rtomb_0200();
166 c16rtomb_0300();
167 c16rtomb_0400();
168 c16rtomb_0500();
169 return t_status;
170 }