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 <locale.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <wchar.h>
20 #include "test.h"
21
22 /**
23 * @tc.name : wcsnrtombs_0100
24 * @tc.desc : Convert wn wide characters in a wide string to a multibyte string
25 * @tc.level : Level 0
26 */
wcsnrtombs_0100(void)27 void wcsnrtombs_0100(void)
28 {
29 const wchar_t src[] = L"test wcsnrtombs";
30 const wchar_t *p;
31 mbstate_t mbs;
32 char buffer[32];
33 int wn = 4;
34 int n = 4;
35 mbrlen(NULL, 0, &mbs);
36 memset(buffer, 0, sizeof(buffer));
37 p = src;
38 int result = wcsnrtombs(buffer, &p, wn, n, &mbs);
39 if (result < 0) {
40 t_error("%s, get result failed", __func__);
41 }
42 if (strcmp(buffer, "test")) {
43 t_error("%s string in buffer is not right", __func__);
44 }
45 }
46
47 /**
48 * @tc.name : wcsnrtombs_0200
49 * @tc.desc : Conversion of wide characters to multibyte characters when wn < n
50 * @tc.level : Level 1
51 */
wcsnrtombs_0200(void)52 void wcsnrtombs_0200(void)
53 {
54 const wchar_t src[] = L"test wcsnrtombs";
55 const wchar_t *p;
56 mbstate_t mbs;
57 char buffer[32];
58 int wn = 4;
59 int n = 5;
60 mbrlen(NULL, 0, &mbs);
61 memset(buffer, 0, sizeof(buffer));
62 p = src;
63 int result = wcsnrtombs(buffer, &p, wn, n, &mbs);
64 if (result < 0) {
65 t_error("%s get result failed", __func__);
66 }
67 if (strcmp(buffer, "test")) {
68 t_error("%s string in buffer is not right", __func__);
69 }
70 }
71
72 /**
73 * @tc.name : wcsnrtombs_0300
74 * @tc.desc : Conversion of wide characters to multibyte characters when dst=NULL
75 * @tc.level : Level 2
76 */
wcsnrtombs_0300(void)77 void wcsnrtombs_0300(void)
78 {
79 const wchar_t src[] = L"test wcsnrtombs";
80 const wchar_t *p;
81 mbstate_t mbs;
82 char *buffer = NULL;
83 int wn = 4;
84 int n = 4;
85 mbrlen(NULL, 0, &mbs);
86 p = src;
87 int result = wcsnrtombs(buffer, &p, wn, n, &mbs);
88 if (result < 0) {
89 t_error("%s get result failed", __func__);
90 }
91 if (result != wn) {
92 t_error("%s get result is not equal to %d", __func__, wn);
93 }
94 }
95
main(int argc,char * argv[])96 int main(int argc, char *argv[])
97 {
98 wcsnrtombs_0100();
99 wcsnrtombs_0200();
100 wcsnrtombs_0300();
101 return t_status;
102 }