2021-01-08 18:24:51 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
2018-01-18 16:08:24 +00:00
|
|
|
#include <assert.h>
|
2019-12-07 10:01:55 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
2018-01-18 16:08:24 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-11-24 10:53:00 +00:00
|
|
|
#include "util/str_util.h"
|
2018-01-18 16:08:24 +00:00
|
|
|
|
2018-10-04 15:03:24 +00:00
|
|
|
static void test_xstrncpy_simple(void) {
|
2018-01-18 16:08:24 +00:00
|
|
|
char s[] = "xxxxxxxxxx";
|
|
|
|
size_t w = xstrncpy(s, "abcdef", sizeof(s));
|
|
|
|
|
|
|
|
// returns strlen of copied string
|
|
|
|
assert(w == 6);
|
|
|
|
|
|
|
|
// is nul-terminated
|
|
|
|
assert(s[6] == '\0');
|
|
|
|
|
|
|
|
// does not write useless bytes
|
|
|
|
assert(s[7] == 'x');
|
|
|
|
|
|
|
|
// copies the content as expected
|
|
|
|
assert(!strcmp("abcdef", s));
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:03:24 +00:00
|
|
|
static void test_xstrncpy_just_fit(void) {
|
2018-01-18 16:08:24 +00:00
|
|
|
char s[] = "xxxxxx";
|
|
|
|
size_t w = xstrncpy(s, "abcdef", sizeof(s));
|
|
|
|
|
|
|
|
// returns strlen of copied string
|
|
|
|
assert(w == 6);
|
|
|
|
|
|
|
|
// is nul-terminated
|
|
|
|
assert(s[6] == '\0');
|
|
|
|
|
|
|
|
// copies the content as expected
|
|
|
|
assert(!strcmp("abcdef", s));
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:03:24 +00:00
|
|
|
static void test_xstrncpy_truncated(void) {
|
2018-01-18 16:08:24 +00:00
|
|
|
char s[] = "xxx";
|
|
|
|
size_t w = xstrncpy(s, "abcdef", sizeof(s));
|
|
|
|
|
|
|
|
// returns 'n' (sizeof(s))
|
|
|
|
assert(w == 4);
|
|
|
|
|
|
|
|
// is nul-terminated
|
|
|
|
assert(s[3] == '\0');
|
|
|
|
|
|
|
|
// copies the content as expected
|
|
|
|
assert(!strncmp("abcdef", s, 3));
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:03:24 +00:00
|
|
|
static void test_xstrjoin_simple(void) {
|
2018-01-18 16:08:24 +00:00
|
|
|
const char *const tokens[] = { "abc", "de", "fghi", NULL };
|
|
|
|
char s[] = "xxxxxxxxxxxxxx";
|
|
|
|
size_t w = xstrjoin(s, tokens, ' ', sizeof(s));
|
|
|
|
|
|
|
|
// returns strlen of concatenation
|
|
|
|
assert(w == 11);
|
|
|
|
|
|
|
|
// is nul-terminated
|
|
|
|
assert(s[11] == '\0');
|
|
|
|
|
|
|
|
// does not write useless bytes
|
|
|
|
assert(s[12] == 'x');
|
|
|
|
|
|
|
|
// copies the content as expected
|
|
|
|
assert(!strcmp("abc de fghi", s));
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:03:24 +00:00
|
|
|
static void test_xstrjoin_just_fit(void) {
|
2018-01-18 16:08:24 +00:00
|
|
|
const char *const tokens[] = { "abc", "de", "fghi", NULL };
|
|
|
|
char s[] = "xxxxxxxxxxx";
|
|
|
|
size_t w = xstrjoin(s, tokens, ' ', sizeof(s));
|
|
|
|
|
|
|
|
// returns strlen of concatenation
|
|
|
|
assert(w == 11);
|
|
|
|
|
|
|
|
// is nul-terminated
|
|
|
|
assert(s[11] == '\0');
|
|
|
|
|
|
|
|
// copies the content as expected
|
|
|
|
assert(!strcmp("abc de fghi", s));
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:03:24 +00:00
|
|
|
static void test_xstrjoin_truncated_in_token(void) {
|
2018-01-18 16:08:24 +00:00
|
|
|
const char *const tokens[] = { "abc", "de", "fghi", NULL };
|
|
|
|
char s[] = "xxxxx";
|
|
|
|
size_t w = xstrjoin(s, tokens, ' ', sizeof(s));
|
|
|
|
|
|
|
|
// returns 'n' (sizeof(s))
|
|
|
|
assert(w == 6);
|
|
|
|
|
|
|
|
// is nul-terminated
|
|
|
|
assert(s[5] == '\0');
|
|
|
|
|
|
|
|
// copies the content as expected
|
|
|
|
assert(!strcmp("abc d", s));
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:03:24 +00:00
|
|
|
static void test_xstrjoin_truncated_before_sep(void) {
|
2018-01-18 16:08:24 +00:00
|
|
|
const char *const tokens[] = { "abc", "de", "fghi", NULL };
|
|
|
|
char s[] = "xxxxxx";
|
|
|
|
size_t w = xstrjoin(s, tokens, ' ', sizeof(s));
|
|
|
|
|
|
|
|
// returns 'n' (sizeof(s))
|
|
|
|
assert(w == 7);
|
|
|
|
|
|
|
|
// is nul-terminated
|
|
|
|
assert(s[6] == '\0');
|
|
|
|
|
|
|
|
// copies the content as expected
|
|
|
|
assert(!strcmp("abc de", s));
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:03:24 +00:00
|
|
|
static void test_xstrjoin_truncated_after_sep(void) {
|
2018-01-18 16:08:24 +00:00
|
|
|
const char *const tokens[] = { "abc", "de", "fghi", NULL };
|
|
|
|
char s[] = "xxxxxxx";
|
|
|
|
size_t w = xstrjoin(s, tokens, ' ', sizeof(s));
|
|
|
|
|
|
|
|
// returns 'n' (sizeof(s))
|
|
|
|
assert(w == 8);
|
|
|
|
|
|
|
|
// is nul-terminated
|
|
|
|
assert(s[7] == '\0');
|
|
|
|
|
|
|
|
// copies the content as expected
|
|
|
|
assert(!strcmp("abc de ", s));
|
|
|
|
}
|
|
|
|
|
2019-11-30 04:15:58 +00:00
|
|
|
static void test_strquote(void) {
|
|
|
|
const char *s = "abcde";
|
|
|
|
char *out = strquote(s);
|
|
|
|
|
|
|
|
// add '"' at the beginning and the end
|
|
|
|
assert(!strcmp("\"abcde\"", out));
|
|
|
|
|
2021-01-24 14:14:53 +00:00
|
|
|
free(out);
|
2019-11-30 04:15:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 17:01:08 +00:00
|
|
|
static void test_utf8_truncate(void) {
|
|
|
|
const char *s = "aÉbÔc";
|
|
|
|
assert(strlen(s) == 7); // É and Ô are 2 bytes-wide
|
|
|
|
|
|
|
|
size_t count;
|
|
|
|
|
|
|
|
count = utf8_truncation_index(s, 1);
|
|
|
|
assert(count == 1);
|
|
|
|
|
|
|
|
count = utf8_truncation_index(s, 2);
|
|
|
|
assert(count == 1); // É is 2 bytes-wide
|
|
|
|
|
|
|
|
count = utf8_truncation_index(s, 3);
|
|
|
|
assert(count == 3);
|
|
|
|
|
|
|
|
count = utf8_truncation_index(s, 4);
|
|
|
|
assert(count == 4);
|
|
|
|
|
|
|
|
count = utf8_truncation_index(s, 5);
|
|
|
|
assert(count == 4); // Ô is 2 bytes-wide
|
|
|
|
|
|
|
|
count = utf8_truncation_index(s, 6);
|
|
|
|
assert(count == 6);
|
|
|
|
|
|
|
|
count = utf8_truncation_index(s, 7);
|
|
|
|
assert(count == 7);
|
|
|
|
|
|
|
|
count = utf8_truncation_index(s, 8);
|
|
|
|
assert(count == 7); // no more chars
|
|
|
|
}
|
|
|
|
|
2019-12-07 10:01:55 +00:00
|
|
|
static void test_parse_integer(void) {
|
|
|
|
long value;
|
|
|
|
bool ok = parse_integer("1234", &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == 1234);
|
|
|
|
|
|
|
|
ok = parse_integer("-1234", &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == -1234);
|
|
|
|
|
|
|
|
ok = parse_integer("1234k", &value);
|
|
|
|
assert(!ok);
|
|
|
|
|
|
|
|
ok = parse_integer("123456789876543212345678987654321", &value);
|
|
|
|
assert(!ok); // out-of-range
|
|
|
|
}
|
|
|
|
|
2019-12-09 13:32:59 +00:00
|
|
|
static void test_parse_integers(void) {
|
|
|
|
long values[5];
|
|
|
|
|
|
|
|
size_t count = parse_integers("1234", ':', 5, values);
|
|
|
|
assert(count == 1);
|
|
|
|
assert(values[0] == 1234);
|
|
|
|
|
|
|
|
count = parse_integers("1234:5678", ':', 5, values);
|
|
|
|
assert(count == 2);
|
|
|
|
assert(values[0] == 1234);
|
|
|
|
assert(values[1] == 5678);
|
|
|
|
|
|
|
|
count = parse_integers("1234:5678", ':', 2, values);
|
|
|
|
assert(count == 2);
|
|
|
|
assert(values[0] == 1234);
|
|
|
|
assert(values[1] == 5678);
|
|
|
|
|
|
|
|
count = parse_integers("1234:-5678", ':', 2, values);
|
|
|
|
assert(count == 2);
|
|
|
|
assert(values[0] == 1234);
|
|
|
|
assert(values[1] == -5678);
|
|
|
|
|
|
|
|
count = parse_integers("1:2:3:4:5", ':', 5, values);
|
|
|
|
assert(count == 5);
|
|
|
|
assert(values[0] == 1);
|
|
|
|
assert(values[1] == 2);
|
|
|
|
assert(values[2] == 3);
|
|
|
|
assert(values[3] == 4);
|
|
|
|
assert(values[4] == 5);
|
|
|
|
|
|
|
|
count = parse_integers("1234:5678", ':', 1, values);
|
|
|
|
assert(count == 0); // max_items == 1
|
|
|
|
|
|
|
|
count = parse_integers("1:2:3:4:5", ':', 3, values);
|
|
|
|
assert(count == 0); // max_items == 3
|
|
|
|
|
|
|
|
count = parse_integers(":1234", ':', 5, values);
|
|
|
|
assert(count == 0); // invalid
|
|
|
|
|
|
|
|
count = parse_integers("1234:", ':', 5, values);
|
|
|
|
assert(count == 0); // invalid
|
|
|
|
|
|
|
|
count = parse_integers("1234:", ':', 1, values);
|
|
|
|
assert(count == 0); // invalid, even when max_items == 1
|
|
|
|
|
|
|
|
count = parse_integers("1234::5678", ':', 5, values);
|
|
|
|
assert(count == 0); // invalid
|
|
|
|
}
|
|
|
|
|
2019-12-07 10:01:55 +00:00
|
|
|
static void test_parse_integer_with_suffix(void) {
|
|
|
|
long value;
|
|
|
|
bool ok = parse_integer_with_suffix("1234", &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == 1234);
|
|
|
|
|
|
|
|
ok = parse_integer_with_suffix("-1234", &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == -1234);
|
|
|
|
|
|
|
|
ok = parse_integer_with_suffix("1234k", &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == 1234000);
|
|
|
|
|
|
|
|
ok = parse_integer_with_suffix("1234m", &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == 1234000000);
|
|
|
|
|
|
|
|
ok = parse_integer_with_suffix("-1234k", &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == -1234000);
|
|
|
|
|
|
|
|
ok = parse_integer_with_suffix("-1234m", &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == -1234000000);
|
|
|
|
|
|
|
|
ok = parse_integer_with_suffix("123456789876543212345678987654321", &value);
|
|
|
|
assert(!ok); // out-of-range
|
|
|
|
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
sprintf(buf, "%ldk", LONG_MAX / 2000);
|
|
|
|
ok = parse_integer_with_suffix(buf, &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == LONG_MAX / 2000 * 1000);
|
|
|
|
|
|
|
|
sprintf(buf, "%ldm", LONG_MAX / 2000);
|
|
|
|
ok = parse_integer_with_suffix(buf, &value);
|
|
|
|
assert(!ok);
|
|
|
|
|
|
|
|
sprintf(buf, "%ldk", LONG_MIN / 2000);
|
|
|
|
ok = parse_integer_with_suffix(buf, &value);
|
|
|
|
assert(ok);
|
|
|
|
assert(value == LONG_MIN / 2000 * 1000);
|
|
|
|
|
|
|
|
sprintf(buf, "%ldm", LONG_MIN / 2000);
|
|
|
|
ok = parse_integer_with_suffix(buf, &value);
|
|
|
|
assert(!ok);
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:17:04 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
|
2018-01-18 16:08:24 +00:00
|
|
|
test_xstrncpy_simple();
|
|
|
|
test_xstrncpy_just_fit();
|
|
|
|
test_xstrncpy_truncated();
|
|
|
|
test_xstrjoin_simple();
|
|
|
|
test_xstrjoin_just_fit();
|
|
|
|
test_xstrjoin_truncated_in_token();
|
|
|
|
test_xstrjoin_truncated_before_sep();
|
|
|
|
test_xstrjoin_truncated_after_sep();
|
2019-11-30 04:15:58 +00:00
|
|
|
test_strquote();
|
2019-05-30 17:01:08 +00:00
|
|
|
test_utf8_truncate();
|
2019-12-07 10:01:55 +00:00
|
|
|
test_parse_integer();
|
2019-12-09 13:32:59 +00:00
|
|
|
test_parse_integers();
|
2019-12-07 10:01:55 +00:00
|
|
|
test_parse_integer_with_suffix();
|
2018-01-18 16:08:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|