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 <stdio.h>
17#include <string.h>
18#include <locale.h>
19#include <wchar.h>
20#include "test.h"
21
22/**
23 * @tc.name      : wcrtomb_0100
24 * @tc.desc      : call wcrtomb to convert wide characters to narrow characters
25 * @tc.level     : Level 0
26 */
27void wcrtomb_0100(void)
28{
29    wchar_t wcs = L'A';
30    char s[20];
31    mbstate_t pr;
32    memset(&pr, 0, sizeof pr);
33    int result = wcrtomb(s, wcs, &pr);
34    if (result != 1) {
35        t_error("%s wcrtomb get result is %d are not 1\n", __func__, result);
36    }
37    if (strcmp(s, "A") != 0) {
38        t_error("%s wcrtomb error get s is '%s' are not 'A'\n", __func__, s);
39    }
40}
41
42/**
43 * @tc.name      : wcrtomb_0200
44 * @tc.desc      : Wide character length exceeds 0x80
45 * @tc.level     : Level 1
46 */
47void wcrtomb_0200(void)
48{
49    wchar_t wcs = L'\u00df';
50    int want = 2;
51    char s[20];
52    mbstate_t pr;
53    memset(&pr, 0, sizeof pr);
54    int result = wcrtomb(s, wcs, &pr);
55    if (result != want) {
56        t_error("%s wcrtomb get result is %d are not 2\n", __func__, result);
57    }
58    if (strcmp(s, "ß") != 0) {
59        t_error("%s wcrtomb error get s is '%s' are not 'ß'\n", __func__, s);
60    }
61}
62
63/**
64 * @tc.name      : wcrtomb_0300
65 * @tc.desc      : Wide character length exceeds 0x800
66 * @tc.level     : Level 1
67 */
68void wcrtomb_0300(void)
69{
70    wchar_t wcs = L'\ud10f';
71    int want = 3;
72    char s[20];
73    mbstate_t pr;
74    memset(&pr, 0, sizeof pr);
75    int result = wcrtomb(s, wcs, &pr);
76    if (result != want) {
77        t_error("%s wcrtomb get result is %d are not 3\n", __func__, result);
78    }
79    if (strcmp(s, "턏") != 0) {
80        t_error("%s wcrtomb error get s is '%s' are not '턏'\n", __func__, s);
81    }
82}
83
84int main(int argc, char *argv[])
85{
86    setlocale(LC_ALL, "en_US.utf8");
87
88    wcrtomb_0100();
89    wcrtomb_0200();
90    wcrtomb_0300();
91    return t_status;
92}