summaryrefslogtreecommitdiffstats
path: root/src/test/test-exit-status.c
blob: 4dc197301d1124a084ab5c7be013ad0d69a9df9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "exit-status.h"
#include "string-util.h"
#include "tests.h"

static void test_exit_status_to_string(void) {
        log_info("/* %s */", __func__);

        for (int i = -1; i <= 256; i++) {
                const char *s, *class;

                s = exit_status_to_string(i, EXIT_STATUS_FULL);
                class = exit_status_class(i);
                log_info("%d: %s%s%s%s",
                         i, s ?: "-",
                         class ? " (" : "", strempty(class), class ? ")" : "");

                if (s)
                        assert_se(exit_status_from_string(s) == i);
        }
}

static void test_exit_status_from_string(void) {
        log_info("/* %s */", __func__);

        assert_se(exit_status_from_string("11") == 11);
        assert_se(exit_status_from_string("-1") == -ERANGE);
        assert_se(exit_status_from_string("256") == -ERANGE);
        assert_se(exit_status_from_string("foo") == -EINVAL);
        assert_se(exit_status_from_string("SUCCESS") == 0);
        assert_se(exit_status_from_string("FAILURE") == 1);
}

static void test_exit_status_NUMA_POLICY(void) {
        log_info("/* %s */", __func__);

        assert_se(streq(exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_FULL), "NUMA_POLICY"));
        assert_se(streq(exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_SYSTEMD), "NUMA_POLICY"));
        assert_se(!exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_BSD));
        assert_se(!exit_status_to_string(EXIT_NUMA_POLICY, EXIT_STATUS_LSB));
}

int main(int argc, char *argv[]) {
        test_setup_logging(LOG_DEBUG);

        test_exit_status_to_string();
        test_exit_status_from_string();
        test_exit_status_NUMA_POLICY();

        return 0;
}