164 lines
5.1 KiB
C
164 lines
5.1 KiB
C
#include "apfl.h"
|
|
#include "test.h"
|
|
|
|
static void
|
|
search_testcase(testctx t, const char *haystack, const char *needle, ptrdiff_t want)
|
|
{
|
|
ptrdiff_t have = apfl_string_view_search(
|
|
apfl_string_view_from(haystack),
|
|
apfl_string_view_from(needle)
|
|
);
|
|
|
|
if (have != want) {
|
|
test_failf(t, "search \"%s\" in \"%s\" failed: want=%d, have=%d", needle, haystack, (int)want, (int)have);
|
|
}
|
|
}
|
|
|
|
TEST(search, t) {
|
|
search_testcase(t, "", "", 0);
|
|
search_testcase(t, "", "foo", -1);
|
|
search_testcase(t, "foo", "foo", 0);
|
|
search_testcase(t, "foofoo", "foo", 0);
|
|
search_testcase(t, "hoofoo", "foo", 3);
|
|
search_testcase(t, "hoofooo", "foo", 3);
|
|
search_testcase(t, "ababab", "a", 0);
|
|
search_testcase(t, "ababab", "b", 1);
|
|
search_testcase(t, "ababab", "ab", 0);
|
|
search_testcase(t, "ababab", "ababab", 0);
|
|
}
|
|
|
|
typedef struct apfl_string_view (*trimmer)(struct apfl_string_view);
|
|
|
|
static void
|
|
generic_trim_testcase(testctx t, const char *name, trimmer trim, const char *in, const char *want)
|
|
{
|
|
struct apfl_string_view have = trim(apfl_string_view_from(in));
|
|
if (!apfl_string_eq(want, have)) {
|
|
test_failf(t, "%s of \"%s\" failed: want=\"%s\", have=\"" APFL_STR_FMT "\"", name, in, want, APFL_STR_FMT_ARGS(have));
|
|
}
|
|
}
|
|
|
|
static void
|
|
ltrim_testcase(testctx t, const char *in, const char *want)
|
|
{
|
|
generic_trim_testcase(t, "ltrim", apfl_string_view_ltrim, in, want);
|
|
}
|
|
|
|
static void
|
|
rtrim_testcase(testctx t, const char *in, const char *want)
|
|
{
|
|
generic_trim_testcase(t, "rtrim", apfl_string_view_rtrim, in, want);
|
|
}
|
|
|
|
static void
|
|
trim_testcase(testctx t, const char *in, const char *want)
|
|
{
|
|
generic_trim_testcase(t, "trim", apfl_string_view_trim, in, want);
|
|
}
|
|
|
|
TEST(ltrim, t) {
|
|
ltrim_testcase(t, "", "");
|
|
ltrim_testcase(t, " ", "");
|
|
ltrim_testcase(t, " ", "");
|
|
ltrim_testcase(t, " \n", "");
|
|
ltrim_testcase(t, " \t\n", "");
|
|
ltrim_testcase(t, " foo", "foo");
|
|
ltrim_testcase(t, " foo ", "foo ");
|
|
ltrim_testcase(t, "foo ", "foo ");
|
|
ltrim_testcase(t, "foo\t", "foo\t");
|
|
ltrim_testcase(t, " f o o ", "f o o ");
|
|
}
|
|
|
|
TEST(rtrim, t) {
|
|
rtrim_testcase(t, "", "");
|
|
rtrim_testcase(t, " ", "");
|
|
rtrim_testcase(t, " ", "");
|
|
rtrim_testcase(t, " \n", "");
|
|
rtrim_testcase(t, " \t\n", "");
|
|
rtrim_testcase(t, " foo", " foo");
|
|
rtrim_testcase(t, " foo ", " foo");
|
|
rtrim_testcase(t, "foo ", "foo");
|
|
rtrim_testcase(t, "foo\t", "foo");
|
|
rtrim_testcase(t, " f o o ", " f o o");
|
|
}
|
|
|
|
TEST(trim, t) {
|
|
trim_testcase(t, "", "");
|
|
trim_testcase(t, " ", "");
|
|
trim_testcase(t, " ", "");
|
|
trim_testcase(t, " \n", "");
|
|
trim_testcase(t, " \t\n", "");
|
|
trim_testcase(t, " foo", "foo");
|
|
trim_testcase(t, " foo ", "foo");
|
|
trim_testcase(t, "foo ", "foo");
|
|
trim_testcase(t, "foo\t", "foo");
|
|
trim_testcase(t, " f o o ", "f o o");
|
|
}
|
|
|
|
static void
|
|
offset_testcase(testctx t, const char *in, size_t off, const char *want)
|
|
{
|
|
struct apfl_string_view have = apfl_string_view_offset(apfl_string_view_from(in), off);
|
|
if (!apfl_string_eq(want, have)) {
|
|
test_failf(t, "offset(\"%s\", %d) failed: want=\"%s\", have=\"" APFL_STR_FMT "\"", in, (int)off, want, APFL_STR_FMT_ARGS(have));
|
|
}
|
|
}
|
|
|
|
TEST(offset, t) {
|
|
offset_testcase(t, "", 0, "");
|
|
offset_testcase(t, "", 10, "");
|
|
offset_testcase(t, "foobar", 0, "foobar");
|
|
offset_testcase(t, "foobar", 1, "oobar");
|
|
offset_testcase(t, "foobar", 3, "bar");
|
|
offset_testcase(t, "foobar", 6, "");
|
|
offset_testcase(t, "foobar", 1000, "");
|
|
}
|
|
|
|
static void
|
|
trunc_testcase(testctx t, const char *in, size_t newlen, const char *want)
|
|
{
|
|
struct apfl_string_view have = apfl_string_view_trunc(apfl_string_view_from(in), newlen);
|
|
if (!apfl_string_eq(want, have)) {
|
|
test_failf(t, "trunc(\"%s\", %d) failed: want=\"%s\", have=\"" APFL_STR_FMT "\"", in, (int)newlen, want, APFL_STR_FMT_ARGS(have));
|
|
}
|
|
}
|
|
|
|
TEST(trunc, t) {
|
|
trunc_testcase(t, "", 0, "");
|
|
trunc_testcase(t, "", 10, "");
|
|
trunc_testcase(t, "foobar", 6, "foobar");
|
|
trunc_testcase(t, "foobar", 10000, "foobar");
|
|
trunc_testcase(t, "foobar", 0, "");
|
|
trunc_testcase(t, "foobar", 5, "fooba");
|
|
trunc_testcase(t, "foobar", 3, "foo");
|
|
}
|
|
|
|
static void
|
|
substr_testcase(testctx t, const char *in, size_t off, size_t newlen, const char *want)
|
|
{
|
|
struct apfl_string_view have = apfl_string_view_substr(apfl_string_view_from(in), off, newlen);
|
|
if (!apfl_string_eq(want, have)) {
|
|
test_failf(t, "substr(\"%s\", %d, %d) failed: want=\"%s\", have=\"" APFL_STR_FMT "\"", in, (int)off, (int)newlen, want, APFL_STR_FMT_ARGS(have));
|
|
}
|
|
}
|
|
|
|
TEST(substr, t) {
|
|
substr_testcase(t, "", 0, 0, "");
|
|
substr_testcase(t, "", 10, 0, "");
|
|
substr_testcase(t, "", 0, 10, "");
|
|
substr_testcase(t, "foobar", 0, 10, "foobar");
|
|
substr_testcase(t, "foobar", 0, 6, "foobar");
|
|
substr_testcase(t, "foobar", 0, 5, "fooba");
|
|
substr_testcase(t, "foobar", 1, 5, "oobar");
|
|
substr_testcase(t, "foobar", 100, 5, "");
|
|
}
|
|
|
|
TESTS_BEGIN
|
|
ADDTEST(search),
|
|
ADDTEST(ltrim),
|
|
ADDTEST(rtrim),
|
|
ADDTEST(trim),
|
|
ADDTEST(offset),
|
|
ADDTEST(trunc),
|
|
ADDTEST(substr),
|
|
TESTS_END
|