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