blob: 4946cf0d1af925de50a3f42e4ab06122b4d05618 [file] [log] [blame]
Kun Yi9747d632018-06-22 15:32:05 -07001#include <assert.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <stdio.h>
5
Andrew Jefferyd0a85562023-04-18 06:22:39 +09306#ifndef SYSCONFDIR
Kun Yi9747d632018-06-22 15:32:05 -07007// Bypass compilation error due to -DSYSCONFDIR not provided
8#define SYSCONFDIR
Andrew Jefferyd0a85562023-04-18 06:22:39 +09309#endif
Kun Yi9747d632018-06-22 15:32:05 -070010
11#include "config.c"
Kun Yi9747d632018-06-22 15:32:05 -070012
13struct test_parse_size_unit {
Andrew Jefferya72711a2023-04-18 18:19:41 +093014 const char *test_str;
15 size_t expected_size;
16 int expected_rc;
Kun Yi9747d632018-06-22 15:32:05 -070017};
18
19void test_config_parse_logsize(void)
20{
21 const struct test_parse_size_unit test_data[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +093022 { "0", 0, -1 },
23 { "1", 1, 0 },
Andrew Jeffery5db8c792023-04-18 21:48:24 +093024 { "4k", 4ul * 1024ul, 0 },
Andrew Jefferya72711a2023-04-18 18:19:41 +093025 { "6M", (6ul << 20), 0 },
26 { "4095M", (4095ul << 20), 0 },
27 { "2G", (2ul << 30), 0 },
28 { "8M\n", (8ul << 20), 0 }, /* Suffix ignored */
Andrew Jeffery5db8c792023-04-18 21:48:24 +093029 { " 10k", 10ul * 1024ul, 0 }, /* Leading spaces trimmed */
30 { "10k ", 10ul * 1024ul, 0 }, /* Trailing spaces trimmed */
31 { "\r\t10k \r\t", 10ul * 1024ul, 0 }, /* Spaces trimmed */
32 { " 10 kB ", 10ul * 1024ul, 0 }, /* Spaces trimmed */
Andrew Jefferya72711a2023-04-18 18:19:41 +093033 { "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 */
Kun Yi9747d632018-06-22 15:32:05 -070041 };
Andrew Jefferya72711a2023-04-18 18:19:41 +093042 const size_t num_tests =
43 sizeof(test_data) / sizeof(struct test_parse_size_unit);
Kun Yi9747d632018-06-22 15:32:05 -070044 size_t size;
Andrew Jeffery8f548f62023-04-18 11:48:49 +093045 size_t i;
46 int rc;
Kun Yi9747d632018-06-22 15:32:05 -070047
48 for (i = 0; i < num_tests; i++) {
49 rc = config_parse_logsize(test_data[i].test_str, &size);
50
Andrew Jeffery4f79c0a2023-04-18 16:58:16 +093051 if (rc == -1 && rc != test_data[i].expected_rc) {
Andrew Jefferya72711a2023-04-18 18:19:41 +093052 warn("[%zu] Str %s expected rc %d, got rc %d\n", i,
53 test_data[i].test_str, test_data[i].expected_rc,
Andrew Jeffery4f79c0a2023-04-18 16:58:16 +093054 rc);
55 } else if (rc == 0 && test_data[i].expected_size != size) {
56 warn("[%zu] Str %s expected size %lu, got size %lu\n",
Andrew Jefferya72711a2023-04-18 18:19:41 +093057 i, test_data[i].test_str,
58 test_data[i].expected_size, size);
Kun Yi9747d632018-06-22 15:32:05 -070059 }
60 assert(rc == test_data[i].expected_rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093061 if (rc == 0) {
Kun Yi9747d632018-06-22 15:32:05 -070062 assert(size == test_data[i].expected_size);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093063 }
Kun Yi9747d632018-06-22 15:32:05 -070064 }
65}
66
67int main(void)
68{
69 test_config_parse_logsize();
70 return EXIT_SUCCESS;
71}