Skip to content

Commit

Permalink
Add tests for untested functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dr8co committed Apr 6, 2024
1 parent 0f6d106 commit 75853de
Showing 1 changed file with 228 additions and 0 deletions.
228 changes: 228 additions & 0 deletions tests/testLiteString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,231 @@ TEST(LiteStringTest, ShrinkToFitDoesNothingWhenSizeIsCapacity) {
EXPECT_EQ(s->capacity, s->size);
string_free(s);
}

// Test copying a non-empty string to a buffer
TEST(LiteStringTest, CopyingStringToBufferStoresCorrectValue) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
char buf[50];
ASSERT_TRUE(string_copy_buffer(s, buf));
ASSERT_STREQ(buf, "Hello, World!");
string_free(s);
}

// Test copying an empty string to a buffer
TEST(LiteStringTest, CopyingEmptyStringToBufferStoresNullCharacter) {
lite_string *s = string_new();
char buf[50];
ASSERT_FALSE(string_copy_buffer(s, buf));
string_free(s);
}

// Test copying a string to a null buffer
TEST(LiteStringTest, CopyingStringToNullBufferFails) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_FALSE(string_copy_buffer(s, nullptr));
string_free(s);
}

// Test copying a null string to a buffer
TEST(LiteStringTest, CopyingNullStringToBufferFails) {
char buf[50];
ASSERT_FALSE(string_copy_buffer(nullptr, buf));
}

// Copying a non-empty string to another string
TEST(LiteStringTest, CopyingStringStoresCorrectValue) {
lite_string *src = string_new();
lite_string *dest = string_new();
string_append_cstr(src, "Hello, World!");
ASSERT_TRUE(string_copy(src, dest));
ASSERT_TRUE(string_compare(src, dest));
string_free(src);
string_free(dest);
}

// Copying an empty string to another string
TEST(LiteStringTest, CopyingEmptyStringStoresCorrectValue) {
lite_string *src = string_new();
lite_string *dest = string_new();
ASSERT_TRUE(string_copy(src, dest));
ASSERT_TRUE(string_compare(src, dest));
string_free(src);
string_free(dest);
}

// Copying a string to a null string
TEST(LiteStringTest, CopyingStringToNullStringFails) {
lite_string *src = string_new();
string_append_cstr(src, "Hello, World!");
ASSERT_FALSE(string_copy(src, nullptr));
string_free(src);
}

// Copying a null string to a string
TEST(LiteStringTest, CopyingNullStringFails) {
lite_string *dest = string_new();
ASSERT_FALSE(string_copy(nullptr, dest));
string_free(dest);
}

// Comparing a string with a C-string for equality, ignoring case
TEST(LiteStringTest, CaseCompareCstrReturnsCorrectValue) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_TRUE(string_case_compare_cstr(s, "HELLO, WORLD!"));
ASSERT_FALSE(string_case_compare_cstr(s, "HELLO, UNIVERSE!"));
string_free(s);
}

// Comparing a string with a null C-string
TEST(LiteStringTest, CaseCompareWithNullCStrReturnsFalse) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_FALSE(string_case_compare_cstr(s, nullptr));
string_free(s);
}

// Comparing a null string with a C-string
TEST(LiteStringTest, CaseCompareCstrNullStringReturnsFalse) {
ASSERT_FALSE(string_case_compare_cstr(nullptr, "HELLO, WORLD!"));
}

// String contains the C-string
TEST(LiteStringTest, ContainsCstrReturnsCorrectValue) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_TRUE(string_contains_cstr(s, "World"));
ASSERT_FALSE(string_contains_cstr(s, "Universe"));
string_free(s);
}

// String is null
TEST(LiteStringTest, ContainsCstrNullReturnsFalse) {
ASSERT_FALSE(string_contains_cstr(nullptr, "World"));
}

// C-string is null
TEST(LiteStringTest, ContainsCstrNullCStrReturnsFalse) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_FALSE(string_contains_cstr(s, nullptr));
string_free(s);
}

// String and C-string are both null
TEST(LiteStringTest, ContainsCstrNullStringAndCStrReturnsFalse) {
ASSERT_FALSE(string_contains_cstr(nullptr, nullptr));
}

// String is empty
TEST(LiteStringTest, ContainsCstrEmptyStringReturnsFalse) {
lite_string *s = string_new();
ASSERT_FALSE(string_contains_cstr(s, "World"));
string_free(s);
}

// C-string is empty
TEST(LiteStringTest, ContainsCstrEmptyCStrReturnsTrue) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_TRUE(string_contains_cstr(s, ""));
string_free(s);
}

// String and C-string are both empty
TEST(LiteStringTest, ContainsCstrEmptyStringAndCStrReturnsTrue) {
lite_string *s = string_new();
ASSERT_TRUE(string_contains_cstr(s, ""));
string_free(s);
}

// String starts with the C-string
TEST(LiteStringTest, StartsWithCstrReturnsCorrectValue) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_TRUE(string_starts_with_cstr(s, "Hello"));
ASSERT_FALSE(string_starts_with_cstr(s, "World"));
string_free(s);
}

// String is null
TEST(LiteStringTest, StartsWithCstrNullReturnsFalse) {
ASSERT_FALSE(string_starts_with_cstr(nullptr, "Hello"));
}

// C-string is null
TEST(LiteStringTest, StartsWithCstrNullCStrReturnsFalse) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_FALSE(string_starts_with_cstr(s, nullptr));
string_free(s);
}

// String and C-string are both null
TEST(LiteStringTest, StartsWithCstrNullStringAndCStrReturnsFalse) {
ASSERT_FALSE(string_starts_with_cstr(nullptr, nullptr));
}

// String is empty
TEST(LiteStringTest, StartsWithCstrEmptyStringReturnsFalse) {
lite_string *s = string_new();
ASSERT_FALSE(string_starts_with_cstr(s, "Hello"));
string_free(s);
}

// C-string is empty
TEST(LiteStringTest, StartsWithCstrEmptyCStrReturnsTrue) {
lite_string *s = string_new();
string_append_cstr(s, "Hello, World!");
ASSERT_TRUE(string_starts_with_cstr(s, ""));
string_free(s);
}

// String and C-string are both empty
TEST(LiteStringTest, StartsWithCstrEmptyStringAndCStrReturnsTrue) {
lite_string *s = string_new();
ASSERT_TRUE(string_starts_with_cstr(s, ""));
string_free(s);
}

// Comparing two identical strings, ignoring case
TEST(LiteStringTest, CaseCompareReturnsCorrectValue) {
lite_string *s1 = string_new();
lite_string *s2 = string_new();
lite_string *s3 = string_new();

string_append_cstr(s1, "Hello, World!");
string_append_cstr(s2, "HELLO, WORLD!");
string_append_cstr(s3, "HELLO, UNIVERSE!");

ASSERT_TRUE(string_case_compare(s1, s2));
ASSERT_FALSE(string_case_compare(s1, s3));
string_free(s1);
string_free(s2);
string_free(s3);
}

// Comparing a string with a null string, ignoring case
TEST(LiteStringTest, CaseCompareNullStringReturnsFalse) {
lite_string *s1 = string_new();
string_append_cstr(s1, "Hello, World!");
ASSERT_FALSE(string_case_compare(s1, nullptr));
ASSERT_FALSE(string_case_compare(nullptr, s1));
string_free(s1);
}

// Comparing two null strings, ignoring case
TEST(LiteStringTest, CaseCompareNullStringsReturnsFalse) {
ASSERT_FALSE(string_case_compare(nullptr, nullptr));
}

// Comparing two empty strings, ignoring case
TEST(LiteStringTest, CaseCompareEmptyStringsReturnsTrue) {
lite_string *s1 = string_new();
lite_string *s2 = string_new();
ASSERT_TRUE(string_case_compare(s1, s2));
string_free(s1);
string_free(s2);
}

0 comments on commit 75853de

Please sign in to comment.