lander/lsm/test/str/str.c

92 lines
2.2 KiB
C

#include "test.h"
#include "lsm/str_internal.h"
void test_cmp() {
lsm_str s1, s2, s3;
lsm_str_overwrite_copy(&s1, "some_string");
lsm_str_overwrite_copy(&s2, "some");
lsm_str_overwrite_copy(&s3, "some_string_extra");
TEST_CHECK(lsm_str_cmp(&s1, 0, &s2, 0) == 4);
TEST_CHECK(lsm_str_cmp(&s1, 0, &s2, 1) == 0);
TEST_CHECK(lsm_str_cmp(&s1, 1, &s2, 1) == 3);
TEST_CHECK(lsm_str_cmp(&s1, 1, &s2, 0) == 0);
TEST_CHECK(lsm_str_cmp(&s1, 0, &s3, 0) == lsm_str_len(&s1));
}
void test_eq() {
lsm_str s1, s2;
lsm_str_overwrite_copy(&s1, "longerthan8");
lsm_str_overwrite_copy(&s2, "longerthan8");
TEST_CHECK(lsm_str_eq(&s1, &s2));
lsm_str_overwrite_copy(&s1, "longerthan8");
lsm_str_overwrite_copy(&s2, "lmaolongerthan8");
TEST_CHECK(!lsm_str_eq(&s1, &s2));
lsm_str_overwrite_copy(&s1, "short");
lsm_str_overwrite_copy(&s2, "short");
TEST_CHECK(lsm_str_eq(&s1, &s2));
lsm_str_overwrite_copy(&s1, "short");
lsm_str_overwrite_copy(&s1, "shorte");
TEST_CHECK(!lsm_str_eq(&s1, &s2));
lsm_str_overwrite_copy(&s1, "longerthan8");
lsm_str_overwrite_copy(&s2, "short");
TEST_CHECK(!lsm_str_eq(&s1, &s2));
}
void test_substr() {
lsm_str s1, s2, s3;
lsm_str_overwrite_copy(&s1, "some_string");
lsm_str_overwrite_copy(&s3, "string");
lsm_str_substr(&s2, &s1, 5, lsm_str_len(&s1));
TEST_CHECK(lsm_str_eq(&s2, &s3));
lsm_str_zero(&s2);
lsm_str_substr(&s2, &s1, 25, lsm_str_len(&s1));
TEST_CHECK(lsm_str_len(&s2) == 0);
}
void test_truncate() {
lsm_str s1, s2, s3;
lsm_str_overwrite_copy(&s1, "some_longer_string_thing");
lsm_str_overwrite_copy(&s2, "some_longer_string");
lsm_str_overwrite_copy(&s3, "some");
lsm_str_truncate(&s1, 18);
TEST_CHECK(lsm_str_eq(&s1, &s2));
lsm_str_truncate(&s1, 4);
TEST_CHECK(lsm_str_eq(&s1, &s3));
}
void test_init_copy() {
char orig[] = "some_string";
lsm_str *s;
lsm_str_init_copy(&s, orig);
TEST_CHECK(s->data.ptr != orig);
TEST_CHECK(strcmp(s->data.ptr, orig) == 0);
lsm_str_free(s);
}
TEST_LIST = {
{ "str init_copy", test_init_copy },
{ "str cmp", test_cmp },
{ "str eq", test_eq },
{ "str substr", test_substr },
{ "str truncate", test_truncate },
{ NULL, NULL }
};