blob: d6b4873d88c6f8c3406e6d2caf83c62bae5600a6 [file] [log] [blame]
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#ifndef SYSCONFDIR
// Bypass compilation error due to -DSYSCONFDIR not provided
#define SYSCONFDIR
#endif
#include "config.c"
struct test_parse_size_unit {
const char *test_str;
size_t expected_size;
int expected_rc;
};
void test_config_parse_logsize(void)
{
const struct test_parse_size_unit test_data[] = {
{"0", 0, -1},
{"1", 1, 0},
{"4k", 4*1024, 0},
{"6M", (6ul << 20), 0},
{"4095M", (4095ul << 20), 0},
{"2G", (2ul << 30), 0},
{"8M\n", (8ul << 20), 0}, /* Suffix ignored */
{" 10k", 10*1024, 0}, /* Leading spaces trimmed */
{"10k ", 10*1024, 0}, /* Trailing spaces trimmed */
{"\r\t10k \r\t",10*1024, 0}, /* Spaces trimmed */
{" 10 kB ", 10*1024, 0}, /* Spaces trimmed */
{"11G", 0, -1}, /* Overflow */
{"4294967296", 0, -1}, /* Overflow */
{"4096M", 0, -1}, /* Overflow */
{"65535G", 0, -1}, /* Overflow */
{"xyz", 0, -1}, /* Invalid */
{"000", 0, -1}, /* Invalid */
{"0.1", 0, -1}, /* Invalid */
{"9T", 0, -1}, /* Invalid suffix */
};
const size_t num_tests = sizeof(test_data) /
sizeof(struct test_parse_size_unit);
size_t size;
size_t i;
int rc;
for (i = 0; i < num_tests; i++) {
rc = config_parse_logsize(test_data[i].test_str, &size);
if (rc == -1 && rc != test_data[i].expected_rc) {
warn("[%zu] Str %s expected rc %d, got rc %d\n",
i,
test_data[i].test_str,
test_data[i].expected_rc,
rc);
} else if (rc == 0 && test_data[i].expected_size != size) {
warn("[%zu] Str %s expected size %lu, got size %lu\n",
i,
test_data[i].test_str,
test_data[i].expected_size,
size);
}
assert(rc == test_data[i].expected_rc);
if (rc == 0)
assert(size == test_data[i].expected_size);
}
}
int main(void)
{
test_config_parse_logsize();
return EXIT_SUCCESS;
}