blob: 165dab5170afc2a96bd0e6f7b0c1c41267c1aaae [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
Medicine Yehd6e8b642024-03-18 01:49:17 -070019void test_config_parse_bytesize(void)
Kun Yi9747d632018-06-22 15:32:05 -070020{
21 const struct test_parse_size_unit test_data[] = {
Medicine Yehd6e8b642024-03-18 01:49:17 -070022 { NULL, 0, -1 },
23 { "", 0, -1 },
Andrew Jefferya72711a2023-04-18 18:19:41 +093024 { "0", 0, -1 },
25 { "1", 1, 0 },
Andrew Jeffery5db8c792023-04-18 21:48:24 +093026 { "4k", 4ul * 1024ul, 0 },
Andrew Jefferya72711a2023-04-18 18:19:41 +093027 { "6M", (6ul << 20), 0 },
28 { "4095M", (4095ul << 20), 0 },
29 { "2G", (2ul << 30), 0 },
Andrew Jeffery4e44c792023-05-12 15:57:46 +093030 { "8M\n", (8ul << 20), 0 }, /* Suffix ignored */
Andrew Jeffery5db8c792023-04-18 21:48:24 +093031 { " 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 Jeffery4e44c792023-05-12 15:57:46 +093034 { " 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 Yi9747d632018-06-22 15:32:05 -070043 };
Andrew Jefferya72711a2023-04-18 18:19:41 +093044 const size_t num_tests =
45 sizeof(test_data) / sizeof(struct test_parse_size_unit);
Kun Yi9747d632018-06-22 15:32:05 -070046 size_t size;
Andrew Jeffery8f548f62023-04-18 11:48:49 +093047 size_t i;
48 int rc;
Kun Yi9747d632018-06-22 15:32:05 -070049
50 for (i = 0; i < num_tests; i++) {
Medicine Yehd6e8b642024-03-18 01:49:17 -070051 rc = config_parse_bytesize(test_data[i].test_str, &size);
Kun Yi9747d632018-06-22 15:32:05 -070052
Andrew Jeffery4f79c0a2023-04-18 16:58:16 +093053 if (rc == -1 && rc != test_data[i].expected_rc) {
Andrew Jefferya72711a2023-04-18 18:19:41 +093054 warn("[%zu] Str %s expected rc %d, got rc %d\n", i,
55 test_data[i].test_str, test_data[i].expected_rc,
Andrew Jeffery4f79c0a2023-04-18 16:58:16 +093056 rc);
57 } else if (rc == 0 && test_data[i].expected_size != size) {
Andrew Jefferydd4fa592023-04-21 11:48:56 +093058 warn("[%zu] Str %s expected size %zu, got size %zu\n",
Andrew Jefferya72711a2023-04-18 18:19:41 +093059 i, test_data[i].test_str,
60 test_data[i].expected_size, size);
Kun Yi9747d632018-06-22 15:32:05 -070061 }
62 assert(rc == test_data[i].expected_rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093063 if (rc == 0) {
Kun Yi9747d632018-06-22 15:32:05 -070064 assert(size == test_data[i].expected_size);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093065 }
Kun Yi9747d632018-06-22 15:32:05 -070066 }
67}
68
69int main(void)
70{
Medicine Yehd6e8b642024-03-18 01:49:17 -070071 test_config_parse_bytesize();
Kun Yi9747d632018-06-22 15:32:05 -070072 return EXIT_SUCCESS;
73}