Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 1 | #include <assert.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | #include <stdio.h> |
| 5 | |
Andrew Jeffery | d0a8556 | 2023-04-18 06:22:39 +0930 | [diff] [blame^] | 6 | #ifndef SYSCONFDIR |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 7 | // Bypass compilation error due to -DSYSCONFDIR not provided |
| 8 | #define SYSCONFDIR |
Andrew Jeffery | d0a8556 | 2023-04-18 06:22:39 +0930 | [diff] [blame^] | 9 | #endif |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 10 | |
| 11 | #include "config.c" |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 12 | |
| 13 | struct test_parse_size_unit { |
| 14 | const char *test_str; |
| 15 | size_t expected_size; |
| 16 | int expected_rc; |
| 17 | }; |
| 18 | |
| 19 | void test_config_parse_logsize(void) |
| 20 | { |
| 21 | const struct test_parse_size_unit test_data[] = { |
| 22 | {"0", 0, -1}, |
| 23 | {"1", 1, 0}, |
| 24 | {"4k", 4*1024, 0}, |
| 25 | {"6M", (6ul << 20), 0}, |
| 26 | {"4095M", (4095ul << 20), 0}, |
| 27 | {"2G", (2ul << 30), 0}, |
| 28 | {"8M\n", (8ul << 20), 0}, /* Suffix ignored */ |
| 29 | {" 10k", 10*1024, 0}, /* Leading spaces trimmed */ |
| 30 | {"10k ", 10*1024, 0}, /* Trailing spaces trimmed */ |
| 31 | {"\r\t10k \r\t",10*1024, 0}, /* Spaces trimmed */ |
| 32 | {" 10 kB ", 10*1024, 0}, /* Spaces trimmed */ |
| 33 | {"11G", 0, -1}, /* Overflow */ |
| 34 | {"4294967296", 0, -1}, /* Overflow */ |
| 35 | {"4096M", 0, -1}, /* Overflow */ |
| 36 | {"65535G", 0, -1}, /* Overflow */ |
| 37 | {"xyz", 0, -1}, /* Invalid */ |
| 38 | {"000", 0, -1}, /* Invalid */ |
| 39 | {"0.1", 0, -1}, /* Invalid */ |
| 40 | {"9T", 0, -1}, /* Invalid suffix */ |
| 41 | }; |
| 42 | const size_t num_tests = sizeof(test_data) / |
| 43 | sizeof(struct test_parse_size_unit); |
| 44 | size_t size; |
Andrew Jeffery | 8f548f6 | 2023-04-18 11:48:49 +0930 | [diff] [blame] | 45 | size_t i; |
| 46 | int rc; |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 47 | |
| 48 | for (i = 0; i < num_tests; i++) { |
| 49 | rc = config_parse_logsize(test_data[i].test_str, &size); |
| 50 | |
| 51 | if ((rc == -1 && rc != test_data[i].expected_rc) || |
| 52 | (rc == 0 && test_data[i].expected_size != size)) { |
Andrew Jeffery | 8f548f6 | 2023-04-18 11:48:49 +0930 | [diff] [blame] | 53 | warn("[%zu] Str %s expected size %lu rc %d," |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 54 | " got size %lu rc %d\n", |
| 55 | i, |
| 56 | test_data[i].test_str, |
| 57 | test_data[i].expected_size, |
| 58 | test_data[i].expected_rc, |
| 59 | size, |
| 60 | rc); |
| 61 | } |
| 62 | assert(rc == test_data[i].expected_rc); |
| 63 | if (rc == 0) |
| 64 | assert(size == test_data[i].expected_size); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | int main(void) |
| 69 | { |
| 70 | test_config_parse_logsize(); |
| 71 | return EXIT_SUCCESS; |
| 72 | } |