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 <signal.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <wchar.h>
20
21#include "filepath_util.h"
22
23void handler(int sig)
24{
25    exit(t_status);
26}
27
28/**
29 * @tc.name      : ungetwc_0100
30 * @tc.desc      : push back a wide character
31 * @tc.level     : Level 0
32 */
33void ungetwc_0100(void)
34{
35    char path[PATH_MAX] = {0};
36    FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
37    FILE *file = fopen(path, "a+");
38    if (!file) {
39        t_error("%s failed: fopen\n", __func__);
40        return;
41    }
42
43    wint_t wc = L'a';
44    wint_t result = ungetwc(wc, file);
45    if (result != wc) {
46        remove(path);
47
48        t_error("%s failed: ungetwc\n", __func__);
49        return;
50    }
51
52    result = getwc(file);
53    if (result != wc) {
54        remove(path);
55
56        t_error("%s failed: getwc\n", __func__);
57        return;
58    }
59}
60
61/**
62 * @tc.name      : ungetwc_0200
63 * @tc.desc      : push back a wide character with an invalid FILE stream
64 * @tc.level     : Level 2
65 */
66void ungetwc_0200(void)
67{
68    signal(SIGSEGV, handler);
69
70    wint_t wc = L'a';
71    ungetwc(wc, NULL);
72}
73
74int main(int argc, char *argv[])
75{
76    ungetwc_0100();
77    ungetwc_0200();
78
79    return t_status;
80}
81