1/*
2 * Copyright (C) 2024 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 <stdlib.h>
18#include "test.h"
19
20static int exit_num = 0;
21static int is_register = 0;
22
23/**
24 * @tc.name      : atexit_0100
25 * @tc.desc      : Construct repeated call
26 * @tc.level     : Level 0
27 */
28void atexit_0100(void)
29{
30    printf("%s is registered\n", __func__);
31}
32
33/**
34 * @tc.name      : atexit_0200
35 * @tc.desc      : Register atexit_0100 when execute atexit_0200
36 * @tc.level     : Level 0
37 */
38void atexit_0200(void)
39{
40    exit_num++;
41    if (!is_register) {
42        int result = atexit(atexit_0100);
43        if (result != 0) {
44            t_error("atexit_0200 error get result is not 0\n");
45        }
46        is_register = 1;
47    }
48}
49
50/**
51 * @tc.name      : atexit_0300
52 * @tc.desc      : Execute atexit_0300 when exit to check whether atexit_0200 is double-call
53 * @tc.level     : Level 0
54 */
55void atexit_0300(void)
56{
57    if (exit_num > 1) {
58        t_error("exit_num from atexit_0200 error get result is not 1, now is %d\n", exit_num);
59    }
60}
61
62int main(int argc, char *argv[])
63{
64    int result = 0;
65    result = atexit(atexit_0300);
66    if (result != 0) {
67        t_error("atexit_0300 error get result is not 0\n");
68    }
69    result = atexit(atexit_0200);
70    if (result != 0) {
71        t_error("atexit_0200 error get result is not 0\n");
72    }
73    return t_status;
74}