blob: 157401a78dc6d2d24f9d5ac07b6e8cef30520080 [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;
43 int i, rc;
44
45 for (i = 0; i < num_tests; i++) {
46 rc = config_parse_logsize(test_data[i].test_str, &size);
47
48 if ((rc == -1 && rc != test_data[i].expected_rc) ||
49 (rc == 0 && test_data[i].expected_size != size)) {
50 warn("[%d] Str %s expected size %lu rc %d,"
51 " got size %lu rc %d\n",
52 i,
53 test_data[i].test_str,
54 test_data[i].expected_size,
55 test_data[i].expected_rc,
56 size,
57 rc);
58 }
59 assert(rc == test_data[i].expected_rc);
60 if (rc == 0)
61 assert(size == test_data[i].expected_size);
62 }
63}
64
65int main(void)
66{
67 test_config_parse_logsize();
68 return EXIT_SUCCESS;
69}