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 { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 14 | const char *test_str; |
| 15 | size_t expected_size; |
| 16 | int expected_rc; |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 17 | }; |
| 18 | |
Medicine Yeh | d6e8b64 | 2024-03-18 01:49:17 -0700 | [diff] [blame] | 19 | void test_config_parse_bytesize(void) |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 20 | { |
| 21 | const struct test_parse_size_unit test_data[] = { |
Medicine Yeh | d6e8b64 | 2024-03-18 01:49:17 -0700 | [diff] [blame] | 22 | { NULL, 0, -1 }, |
| 23 | { "", 0, -1 }, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 24 | { "0", 0, -1 }, |
| 25 | { "1", 1, 0 }, |
Andrew Jeffery | 5db8c79 | 2023-04-18 21:48:24 +0930 | [diff] [blame] | 26 | { "4k", 4ul * 1024ul, 0 }, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 27 | { "6M", (6ul << 20), 0 }, |
| 28 | { "4095M", (4095ul << 20), 0 }, |
| 29 | { "2G", (2ul << 30), 0 }, |
Andrew Jeffery | 4e44c79 | 2023-05-12 15:57:46 +0930 | [diff] [blame] | 30 | { "8M\n", (8ul << 20), 0 }, /* Suffix ignored */ |
Andrew Jeffery | 5db8c79 | 2023-04-18 21:48:24 +0930 | [diff] [blame] | 31 | { " 10k", 10ul * 1024ul, 0 }, /* Leading spaces trimmed */ |
| 32 | { "10k ", 10ul * 1024ul, 0 }, /* Trailing spaces trimmed */ |
| 33 | { "\r\t10k \r\t", 10ul * 1024ul, 0 }, /* Spaces trimmed */ |
Andrew Jeffery | 4e44c79 | 2023-05-12 15:57:46 +0930 | [diff] [blame] | 34 | { " 10 kB ", 10ul * 1024ul, 0 }, /* Spaces trimmed */ |
| 35 | { "11G", 0, -1 }, /* Overflow */ |
| 36 | { "4294967296", 0, -1 }, /* Overflow */ |
| 37 | { "4096M", 0, -1 }, /* Overflow */ |
| 38 | { "65535G", 0, -1 }, /* Overflow */ |
| 39 | { "xyz", 0, -1 }, /* Invalid */ |
| 40 | { "000", 0, -1 }, /* Invalid */ |
| 41 | { "0.1", 0, -1 }, /* Invalid */ |
| 42 | { "9T", 0, -1 }, /* Invalid suffix */ |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 43 | }; |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 44 | const size_t num_tests = |
| 45 | sizeof(test_data) / sizeof(struct test_parse_size_unit); |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 46 | size_t size; |
Andrew Jeffery | 8f548f6 | 2023-04-18 11:48:49 +0930 | [diff] [blame] | 47 | size_t i; |
| 48 | int rc; |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 49 | |
| 50 | for (i = 0; i < num_tests; i++) { |
Medicine Yeh | d6e8b64 | 2024-03-18 01:49:17 -0700 | [diff] [blame] | 51 | rc = config_parse_bytesize(test_data[i].test_str, &size); |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 52 | |
Andrew Jeffery | 4f79c0a | 2023-04-18 16:58:16 +0930 | [diff] [blame] | 53 | if (rc == -1 && rc != test_data[i].expected_rc) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 54 | warn("[%zu] Str %s expected rc %d, got rc %d\n", i, |
| 55 | test_data[i].test_str, test_data[i].expected_rc, |
Andrew Jeffery | 4f79c0a | 2023-04-18 16:58:16 +0930 | [diff] [blame] | 56 | rc); |
| 57 | } else if (rc == 0 && test_data[i].expected_size != size) { |
Andrew Jeffery | dd4fa59 | 2023-04-21 11:48:56 +0930 | [diff] [blame] | 58 | warn("[%zu] Str %s expected size %zu, got size %zu\n", |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 59 | i, test_data[i].test_str, |
| 60 | test_data[i].expected_size, size); |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 61 | } |
| 62 | assert(rc == test_data[i].expected_rc); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 63 | if (rc == 0) { |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 64 | assert(size == test_data[i].expected_size); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 65 | } |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | int main(void) |
| 70 | { |
Medicine Yeh | d6e8b64 | 2024-03-18 01:49:17 -0700 | [diff] [blame] | 71 | test_config_parse_bytesize(); |
Kun Yi | 9747d63 | 2018-06-22 15:32:05 -0700 | [diff] [blame] | 72 | return EXIT_SUCCESS; |
| 73 | } |