remove unnecessary string clone

This commit is contained in:
Dustin J. Mitchell
2022-01-24 04:12:58 +00:00
parent 65082c26e7
commit 40f30c6d89
4 changed files with 104 additions and 16 deletions

View File

@@ -10,20 +10,52 @@ TEST_CASE("creating borrowed strings does not crash") {
TEST_CASE("creating cloned strings does not crash") {
char *abcdef = strdup("abcdef");
TCString *s = tc_string_clone(abcdef);
REQUIRE(s != NULL);
free(abcdef);
CHECK(strcmp(tc_string_content(s), "abcdef") == 0);
tc_string_free(s);
}
TEST_CASE("strings echo back their content") {
TEST_CASE("borrowed strings echo back their content") {
TCString *s = tc_string_new("abcdef");
REQUIRE(s != NULL);
CHECK(strcmp(tc_string_content(s), "abcdef") == 0);
size_t len;
const char *buf = tc_string_content_with_len(s, &len);
REQUIRE(buf != NULL);
CHECK(len == 6);
CHECK(strncmp(buf, "abcdef", len) == 0);
tc_string_free(s);
}
TEST_CASE("cloned strings echo back their content") {
char *orig = strdup("abcdef");
TCString *s = tc_string_clone(orig);
REQUIRE(s != NULL);
free(orig);
CHECK(strcmp(tc_string_content(s), "abcdef") == 0);
size_t len;
const char *buf = tc_string_content_with_len(s, &len);
REQUIRE(buf != NULL);
CHECK(len == 6);
CHECK(strncmp(buf, "abcdef", len) == 0);
tc_string_free(s);
}
TEST_CASE("tc_string_content returns NULL for strings containing embedded NULs") {
TCString *s = tc_string_clone_with_len("ab\0de", 5);
REQUIRE(s != NULL);
CHECK(tc_string_content(s) == NULL);
size_t len;
const char *buf = tc_string_content_with_len(s, &len);
REQUIRE(buf != NULL);
CHECK(len == 5);
CHECK(strncmp(buf, "ab\0de", len) == 0);
tc_string_free(s);
}