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 <errno.h>
17 #include <limits.h>
18 #include <locale.h>
19 #include <string.h>
20 #include <uchar.h>
21 #include "test.h"
22
23 /**
24 * @tc.name : c32rtomb_0100
25 * @tc.desc :
26 * @tc.leve : Level 0
27 */
c32rtomb_0100(void)28 void c32rtomb_0100(void)
29 {
30 size_t result = c32rtomb(NULL, L'\0', NULL);
31 if (result != 1U) {
32 t_error("%s c32rtomb failed\n", __func__);
33 }
34
35 result = c32rtomb(NULL, L'h', NULL);
36 if (result != 1U) {
37 t_error("%s c32rtomb failed\n", __func__);
38 }
39 }
40
41 /**
42 * @tc.name : c32rtomb_0200
43 * @tc.desc :
44 * @tc.leve : Level 0
45 */
c32rtomb_0200(void)46 void c32rtomb_0200(void)
47 {
48 char bytes[MB_LEN_MAX];
49 memset(bytes, 1, sizeof(bytes));
50
51 size_t result = c32rtomb(bytes, L'\0', NULL);
52 if (result != 1U) {
53 t_error("%s c32rtomb failed\n", __func__);
54 }
55 if (bytes[0] != '\0') {
56 t_error("%s bytes[0] failed\n", __func__);
57 }
58 if (bytes[1] != '\x01') {
59 t_error("%s bytes[1] failed\n", __func__);
60 }
61
62 memset(bytes, 0, sizeof(bytes));
63 result = c32rtomb(bytes, L'h', NULL);
64 if (result != 1U) {
65 t_error("%s c32rtomb failed\n", __func__);
66 }
67 if (bytes[0] != 'h') {
68 t_error("%s bytes[0] failed\n", __func__);
69 }
70 }
71
72 /**
73 * @tc.name : c32rtomb_0300
74 * @tc.desc :
75 * @tc.leve : Level 0
76 */
c32rtomb_0300(void)77 void c32rtomb_0300(void)
78 {
79 char bytes[MB_LEN_MAX];
80
81 char *ret = setlocale(LC_CTYPE, "C.UTF-8");
82 if (strcmp(ret, "C.UTF-8")) {
83 t_error("%s setlocale failed\n", __func__);
84 }
85 uselocale(LC_GLOBAL_LOCALE);
86
87 // 1-byte UTF-8.
88 memset(bytes, 0, sizeof(bytes));
89 size_t result = c32rtomb(bytes, L'h', NULL);
90 if (result != 1U) {
91 t_error("%s c32rtomb failed\n", __func__);
92 }
93 if (bytes[0] != 'h') {
94 t_error("%s bytes[0] failed\n", __func__);
95 }
96 // 2-byte UTF-8.
97 memset(bytes, 0, sizeof(bytes));
98 result = c32rtomb(bytes, 0x00a2, NULL);
99 if (result != 2U) {
100 t_error("%s c32rtomb failed\n", __func__);
101 }
102 if (bytes[0] != '\xc2') {
103 t_error("%s bytes[0] failed\n", __func__);
104 }
105 if (bytes[1] != '\xa2') {
106 t_error("%s bytes[1] failed\n", __func__);
107 }
108 // 3-byte UTF-8.
109 memset(bytes, 0, sizeof(bytes));
110 result = c32rtomb(bytes, 0x20ac, NULL);
111 if (result != 3U) {
112 t_error("%s c32rtomb failed\n", __func__);
113 }
114 if (bytes[0] != '\xe2') {
115 t_error("%s bytes[0] failed\n", __func__);
116 }
117 if (bytes[1] != '\x82') {
118 t_error("%s bytes[1] failed\n", __func__);
119 }
120 if (bytes[2] != '\xac') {
121 t_error("%s bytes[2] failed\n", __func__);
122 }
123 // 4-byte UTF-8.
124 memset(bytes, 0, sizeof(bytes));
125 result = c32rtomb(bytes, 0x24b62, NULL);
126 if (result != 4U) {
127 t_error("%s c32rtomb failed\n", __func__);
128 }
129 if (bytes[0] != '\xf0') {
130 t_error("%s bytes[0] failed\n", __func__);
131 }
132 if (bytes[1] != '\xa4') {
133 t_error("%s bytes[1] failed\n", __func__);
134 }
135 if (bytes[2] != '\xad') {
136 t_error("%s bytes[2] failed\n", __func__);
137 }
138 if (bytes[3] != '\xa2') {
139 t_error("%s bytes[3] failed\n", __func__);
140 }
141 }
142
143 /**
144 * @tc.name : c32rtomb_0400
145 * @tc.desc :
146 * @tc.leve : Level 2
147 */
c32rtomb_0400(void)148 void c32rtomb_0400(void)
149 {
150 char bytes[MB_LEN_MAX];
151 memset(bytes, 0, sizeof(bytes));
152 size_t result = c32rtomb(bytes, 0xffffffff, NULL);
153 if (result != (size_t)-1) {
154 t_error("%s c32rtomb should be failed\n", __func__);
155 }
156 }
157
main(int argc,char * argv[])158 int main(int argc, char *argv[])
159 {
160 c32rtomb_0100();
161 c32rtomb_0200();
162 c32rtomb_0300();
163 c32rtomb_0400();
164 return t_status;
165 }