Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 1 | |
| 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 | |
| 14 | static 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 | |
| 34 | static void test_config_parse_basic(void) |
| 35 | { |
| 36 | execute_test("tty = ttyS0", "tty", "ttyS0"); |
| 37 | } |
| 38 | |
| 39 | static void test_config_parse_no_key(void) |
| 40 | { |
| 41 | execute_test("= ttyS0", "tty", NULL); |
| 42 | } |
| 43 | |
| 44 | static void test_config_parse_no_value(void) |
| 45 | { |
| 46 | execute_test("tty =", "tty", NULL); |
| 47 | } |
| 48 | |
| 49 | static void test_config_parse_no_operator(void) |
| 50 | { |
| 51 | execute_test("tty ttyS0", "tty", NULL); |
| 52 | } |
| 53 | |
| 54 | static void test_config_parse_no_spaces(void) |
| 55 | { |
| 56 | execute_test("tty=ttyS0", "tty", "ttyS0"); |
| 57 | } |
| 58 | |
| 59 | static void test_config_parse_empty(void) |
| 60 | { |
| 61 | execute_test("", "tty", NULL); |
| 62 | } |
| 63 | |
| 64 | int 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 | } |