blob: 6392bd9988cea630711942c6d5ac868b5ec23a6b [file] [log] [blame]
Andrew Jefferyd30d7572023-04-18 14:51:51 +09301
2#include <assert.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <stdio.h>
6
7#ifndef SYSCONFDIR
8// Bypass compilation error due to -DSYSCONFDIR not provided
9#define SYSCONFDIR
10#endif
11
12#include "config.c"
13
14static void execute_test(const char *input, const char *key, const char *expected)
15{
16 struct config *ctx;
17 const char *found;
18 char *buf;
19
20 ctx = calloc(1, sizeof(*ctx));
21 buf = strdup(input);
22 config_parse(ctx, buf);
23 free(buf);
24 found = config_get_value(ctx, key);
25 if (!expected)
26 assert(!found);
27 if (expected) {
28 assert(found);
29 assert(!strcmp(expected, found));
30 }
31 config_fini(ctx);
32}
33
34static void test_config_parse_basic(void)
35{
36 execute_test("tty = ttyS0", "tty", "ttyS0");
37}
38
39static void test_config_parse_no_key(void)
40{
41 execute_test("= ttyS0", "tty", NULL);
42}
43
44static void test_config_parse_no_value(void)
45{
46 execute_test("tty =", "tty", NULL);
47}
48
49static void test_config_parse_no_operator(void)
50{
51 execute_test("tty ttyS0", "tty", NULL);
52}
53
54static void test_config_parse_no_spaces(void)
55{
56 execute_test("tty=ttyS0", "tty", "ttyS0");
57}
58
59static void test_config_parse_empty(void)
60{
61 execute_test("", "tty", NULL);
62}
63
64int main(void)
65{
66 test_config_parse_basic();
67 test_config_parse_no_key();
68 test_config_parse_no_value();
69 test_config_parse_no_operator();
70 test_config_parse_no_spaces();
71 test_config_parse_empty();
72
73 return EXIT_SUCCESS;
74}