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 <uchar.h>
17 #include <string.h>
18 #include <locale.h>
19 #include "functionalext.h"
20
21 #define TWO_BYTE_TEST_VALUE 0x00a3
22 #define THREE_BYTE_TEST_VALUE 0x08a3
23 #define TWO 2
24 #define THREE 3
25
26 /**
27 * @tc.name : mbrtoc32_0100
28 * @tc.desc : Convert a single-byte string to a wide string
29 * @tc.level : Level 0
30 */
mbrtoc32_0100(void)31 void mbrtoc32_0100(void)
32 {
33 char32_t pc = 0;
34 int ret = 0;
35 char *str = setlocale(LC_CTYPE, "C.UTF-8");
36 if (str == NULL) {
37 t_error("setlocale failed\n");
38 return;
39 }
40 uselocale(LC_GLOBAL_LOCALE);
41
42 ret = mbrtoc32(&pc, "abcdef", 1, NULL);
43 EXPECT_EQ("mbrtoc32_0100", ret, 1);
44 EXPECT_EQ("mbrtoc32_0100", pc, 'a');
45 }
46
47 /**
48 * @tc.name : mbrtoc32_0200
49 * @tc.desc : Convert a multibyte string to a wide-character string
50 * @tc.level : Level 0
51 */
mbrtoc32_0200(void)52 void mbrtoc32_0200(void)
53 {
54 char32_t out;
55 char str[] = "\u00a3";
56 char str1[] = "\u08a3";
57 mbstate_t ps;
58 int ret = 0;
59 memset(&ps, 0x0, sizeof(ps));
60
61 ret = mbrtoc32(&out, str, strlen(str), NULL);
62 EXPECT_EQ("mbrtoc32_0200", ret, TWO);
63 EXPECT_EQ("mbrtoc32_0200", out, TWO_BYTE_TEST_VALUE);
64
65 ret = mbrtoc32(&out, str1, strlen(str1), &ps);
66 EXPECT_EQ("mbrtoc32_0200", ret, THREE);
67 EXPECT_EQ("mbrtoc32_0200", out, THREE_BYTE_TEST_VALUE);
68 }
69
70 /**
71 * @tc.name : mbrtoc32_0300
72 * @tc.desc : Provide exception parameter data, convert the string to a wide string
73 * @tc.level : Level 2
74 */
mbrtoc32_0300(void)75 void mbrtoc32_0300(void)
76 {
77 char32_t out;
78 char str[] = "\xc2";
79 char strx[] = "\xc2\xa3";
80 int errorresult = -2;
81 mbstate_t ps;
82 int ret = 0;
83 memset(&ps, 0x0, sizeof(ps));
84
85 ret = (int)(mbrtoc32(&out, str, sizeof(str), NULL));
86 EXPECT_EQ("mbrtoc32_0300", ret, ERREXPECT);
87 EXPECT_EQ("mbrtoc32_0300", errno, EILSEQ);
88 ret = (int)(mbrtoc32(&out, strx, 1, NULL));
89 EXPECT_EQ("mbrtoc32_0300", ret, errorresult);
90 ret = (int)(mbrtoc32(NULL, NULL, 0, &ps));
91 EXPECT_EQ("mbrtoc32_0300", ret, CMPFLAG);
92 }
93
main(void)94 int main(void)
95 {
96 mbrtoc32_0100();
97 mbrtoc32_0200();
98 mbrtoc32_0300();
99 return t_status;
100 }
101