summaryrefslogtreecommitdiff
path: root/tests/test_util.c
blob: cbd4e1b3f23a9ef1ff9b9fda5ff16418fac3c044 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "hybbx/limits.h"
#include "hybbx/log.h"
#include "hybbx/util.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static unsigned g_failures;

static void check_int(const char *label, int got, int want)
{
    if (got != want) {
        fprintf(stderr, "FAIL %s: got %d want %d\n", label, got, want);
        g_failures++;
    }
}

static void check_str(const char *label, const char *got, const char *want)
{
    if (got == NULL || want == NULL || strcmp(got, want) != 0) {
        fprintf(stderr, "FAIL %s: got '%s' want '%s'\n",
                label, got != NULL ? got : "(null)", want);
        g_failures++;
    }
}

int main(void)
{
    check_int("true yes", hybbx_bool_is_true("yes"), 1);
    check_int("true enable", hybbx_bool_is_true("enable"), 1);
    check_int("false no", hybbx_bool_is_false("no"), 1);
    check_int("false disable", hybbx_bool_is_false("disable"), 1);

    check_int("parse yes", hybbx_parse_bool("yes", 0), 1);
    check_int("parse false", hybbx_parse_bool("false", 1), 0);
    check_int("parse empty", hybbx_parse_bool("", 1), 1);
    check_int("parse garbage", hybbx_parse_bool("maybe", 0), 0);

    check_str("bool yes", hybbx_bool_to_string(1), HYBBX_BOOL_YES);
    check_str("bool no", hybbx_bool_to_string(0), HYBBX_BOOL_NO);

    check_str("result ok", hybbx_result_name(HYBBX_OK), "ok");
    check_str("result invalid", hybbx_result_name(HYBBX_ERR_INVALID), "invalid");
    check_str("result unknown", hybbx_result_name((hybbx_result_t)-99), "unknown");

    check_int("log parse debug", (int)hybbx_log_parse_level("debug"),
              (int)HYBBX_LOG_DEBUG);
    check_int("log parse stats", (int)hybbx_log_parse_level("stats"),
              (int)HYBBX_LOG_STATS);
    check_int("log parse info", (int)hybbx_log_parse_level("info"),
              (int)HYBBX_LOG_INFO);
    check_int("log parse warn", (int)hybbx_log_parse_level("warn"),
              (int)HYBBX_LOG_WARN);
    check_int("log parse unknown", (int)hybbx_log_parse_level("verbose"),
              (int)HYBBX_LOG_WARN);
    check_str("log name stats", hybbx_log_level_name(HYBBX_LOG_STATS), "stats");

    {
        char path[HYBBX_PATH_MAX];
        const char *home = getenv("HOME");

        if (hybbx_path_expand(path, sizeof(path), NULL) == HYBBX_OK) {
            check_str("path expand null", path, HYBBX_DIR_DATA);
        }
        if (home != NULL && home[0] != '\0') {
            char want[HYBBX_PATH_MAX];

            snprintf(want, sizeof(want), "%s/custom", home);
            if (hybbx_path_expand(path, sizeof(path), "~/custom") == HYBBX_OK) {
                check_str("path expand tilde", path, want);
            }
            if (hybbx_path_expand(path, sizeof(path), "~") == HYBBX_OK) {
                check_str("path expand home only", path, home);
            }
        }
    }

    {
        char path[HYBBX_PATH_MAX];
        char want[HYBBX_PATH_MAX];

        hybbx_install_root_set("/opt/hybbx");
        if (hybbx_path_resolve(path, sizeof(path), HYBBX_DIR_DATA) == HYBBX_OK) {
            snprintf(want, sizeof(want), "/opt/hybbx/%s", HYBBX_DIR_DATA);
            check_str("path resolve data", path, want);
        }
        if (hybbx_path_resolve(path, sizeof(path), HYBBX_DIR_LOGS) == HYBBX_OK) {
            snprintf(want, sizeof(want), "/opt/hybbx/%s", HYBBX_DIR_LOGS);
            check_str("path resolve logs", path, want);
        }
        hybbx_install_root_set(NULL);
    }

    {
        char os_name[HYBBX_PATH_MAX];

        if (hybbx_platform_os_name(os_name, sizeof(os_name)) != HYBBX_OK) {
            fprintf(stderr, "FAIL platform os name: lookup failed\n");
            g_failures++;
        } else if (os_name[0] == '\0') {
            fprintf(stderr, "FAIL platform os name: empty\n");
            g_failures++;
#if defined(__linux__)
        } else if (strcmp(os_name, "Linux") != 0) {
            fprintf(stderr, "FAIL platform os name: got '%s' want 'Linux'\n",
                    os_name);
            g_failures++;
#endif
        }
    }

    {
        struct tm tm_ref = {0};
        char time_buf[32];
        char date_buf[32];
        hybbx_time_format_t fmt;

        tm_ref.tm_year = 126;
        tm_ref.tm_mon = 6;
        tm_ref.tm_mday = 8;
        tm_ref.tm_hour = 15;
        tm_ref.tm_min = 4;
        tm_ref.tm_sec = 5;

        hybbx_time_format_defaults(&fmt);
        if (hybbx_time_format_time(time_buf, sizeof(time_buf), &tm_ref,
                                   &fmt) == HYBBX_OK) {
            check_str("time 24h seconds", time_buf, "15:04:05");
        }

        fmt.date_format = HYBBX_DATE_US;
        if (hybbx_time_format_date(date_buf, sizeof(date_buf), &tm_ref,
                                   &fmt) == HYBBX_OK) {
            check_str("date us", date_buf, "07/08/2026");
        }

        fmt.date_format = HYBBX_DATE_ISO;
        if (hybbx_time_format_date(date_buf, sizeof(date_buf), &tm_ref,
                                   &fmt) == HYBBX_OK) {
            check_str("date iso", date_buf, "2026/07/08");
        }
    }

    if (g_failures != 0) {
        fprintf(stderr, "%u test(s) failed\n", g_failures);
        return EXIT_FAILURE;
    }

    puts("test_util: ok");
    return EXIT_SUCCESS;
}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com