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 : mbrtoc16_0100
28 * @tc.desc : Convert a single-byte string to a wide string
29 * @tc.level : Level 0
30 */
mbrtoc16_0100(void)31 void mbrtoc16_0100(void)
32 {
33 char16_t pc = 0;
34 int ret = -1;
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 = mbrtoc16(&pc, "abcdef", 1, NULL);
43 EXPECT_EQ("mbrtoc16_0100", ret, 1);
44 EXPECT_EQ("mbrtoc16_0100", pc, 'a');
45 }
46
47 /**
48 * @tc.name : mbrtoc16_0200
49 * @tc.desc : Convert a multibyte string to a wide-character string
50 * @tc.level : Level 0
51 */
mbrtoc16_0200(void)52 void mbrtoc16_0200(void)
53 {
54 char16_t out;
55 int ret = 0;
56 char str[] = "\u00a3";
57 char str1[] = "\u08a3";
58 mbstate_t ps;
59 memset(&ps, 0x0, sizeof(ps));
60
61 ret = mbrtoc16(&out, str, strlen(str), NULL);
62 EXPECT_EQ("mbrtoc16_0200", ret, TWO);
63 EXPECT_EQ("mbrtoc16_0200", out, TWO_BYTE_TEST_VALUE);
64
65 ret = mbrtoc16(&out, str1, strlen(str1), &ps);
66 EXPECT_EQ("mbrtoc16_0200", ret, THREE);
67 EXPECT_EQ("mbrtoc16_0200", out, THREE_BYTE_TEST_VALUE);
68 }
69
70 /**
71 * @tc.name : mbrtoc16_0300
72 * @tc.desc : Provide exception parameter data, convert the string to a wide string
73 * @tc.level : Level 2
74 */
mbrtoc16_0300(void)75 void mbrtoc16_0300(void)
76 {
77 char16_t out;
78 char str[] = "\xc2";
79 char strx[] = "\xc2\xa3";
80 int errorresult = -2;
81 mbstate_t ps;
82 memset(&ps, 0x0, sizeof(ps));
83
84 int ret = (int)(mbrtoc16(&out, str, sizeof(str), NULL));
85 EXPECT_EQ("mbrtoc16_0300", ret, ERREXPECT);
86 EXPECT_EQ("mbrtoc16_0300", errno, EILSEQ);
87 ret = (int)(mbrtoc16(&out, strx, 1, NULL));
88 EXPECT_EQ("mbrtoc16_0300", ret, errorresult);
89 ret = (int)(mbrtoc16(NULL, NULL, 0, &ps));
90 EXPECT_EQ("mbrtoc16_0300", ret, CMPFLAG);
91 }
92
main(void)93 int main(void)
94 {
95 mbrtoc16_0100();
96 mbrtoc16_0200();
97 mbrtoc16_0300();
98 return t_status;
99 }
100