*/
str_t* str_new(const char* p_cstr);
+/**
+ * @brief Return the size of the string.
+ *
+ * @param p_str The string.
+ *
+ * @return The size of the string.
+ */
size_t str_size(str_t* p_str);
+/**
+ * @brief Returns the underlying C-style string.
+ *
+ * @param p_str The string.
+ *
+ * @return The underlying C string.
+ */
const char* str_cstr(str_t* p_str);
+/**
+ * @brief Return a new copy of the string.
+ *
+ * @param p_str The string to copy.
+ *
+ * @return The new copy of the string.
+ */
str_t* str_copy(str_t* p_str);
+/**
+ * @brief Return a character at a given index in the string.
+ *
+ * @param p_str The string.
+ * @param index The idnex of the character to fetch.
+ *
+ * @return The fetched character.
+ */
char str_at(str_t* p_str, size_t index);
+/**
+ * @brief Creates a new copy of the string with the character at the provided
+ * index changed to the given value.
+ *
+ * @param p_str The string to copy.
+ * @param index The index of the character to change.
+ * @param val The new value for the character.
+ *
+ * @return The newly created string.
+ */
str_t* str_set(str_t* p_str, size_t index, char val);
+/**
+ * @brief Creates a new string whose contents consist of the first input string
+ * followed by the second input string.
+ *
+ * @param p_str1 The first input string.
+ * @param p_str2 The second input string.
+ *
+ * @return A newly created string.
+ */
str_t* str_concat(str_t* p_str1, str_t* p_str2);
+/**
+ * @brief Creates a new string with the second string inserted at the given
+ * index of the first string.
+ *
+ * @param p_str1 The first input string.
+ * @param index The index where the string will be inserted.
+ * @param p_str2 The second input string.
+ *
+ * @return The newly created string.
+ */
str_t* str_insert(str_t* p_str1, size_t index, str_t* p_str2);
+/**
+ * @brief Creates a newly created string with the given range erased.
+ *
+ * The new string is created by copying the input string with the range
+ * specified erase. The range erased is from the start index up to, but not
+ * including the end index.
+ *
+ * @param p_str The input string.
+ * @param start The start index.
+ * @param end The end index.
+ *
+ * @return The newly created string.
+ */
str_t* str_erase(str_t* p_str, size_t start, size_t end);
+/**
+ * @brief Creates a new string by copying the range specifed from the input
+ * string.
+ *
+ * The new string is created by copying the specified range from the input
+ * string. The range is from the start index up to and not including the end
+ * index.
+ *
+ * @param p_str The input string.
+ * @param start The start index.
+ * @param end The end index.
+ *
+ * @return The newly created string.
+ */
str_t* str_substr(str_t* p_str, size_t start, size_t end);
+/**
+ * @brief Compares the two input strings
+ *
+ * Returns an int representing the comparison with the following semantics:
+ *
+ * <0 == The first character that does not match is lower in p_str1 than p_str2.
+ * 0 == The strings are identical.
+ * >0 == The first character that does not match is greater in p_str1 than p_str2.
+ *
+ * @param p_str1 The first input string.
+ * @param p_str2 The second input string.
+ *
+ * @return Integer representing the relationship.
+ */
int str_compare(str_t* p_str1, str_t* p_str2);
+/**
+ * @brief Find the first occurrence of p_str2 in p_str1.
+ *
+ * @param p_str1 The string to be searched.
+ * @param p_str2 The search string.
+ *
+ * @return The index of the first occurrence if a match is found and SIZE_MAX
+ * if no match is found.
+ */
size_t str_find(str_t* p_str1, str_t* p_str2);
+/**
+ * @brief Find the last occurrence of p_str2 in p_str1.
+ *
+ * @param p_str1 The string to be searched.
+ * @param p_str2 The search string.
+ *
+ * @return The index of the last occurrence if a match is found and SIZE_MAX
+ * if no match is found.
+ */
size_t str_rfind(str_t* p_str1, str_t* p_str2);
#ifdef __cplusplus
str_t* p_str2 = str_copy(p_str1);
CHECK(p_str1 != p_str2);
CHECK(0 == strcmp(str_cstr(p_str1), str_cstr(p_str2)));
- CHECK(3 == str_size(p_str));
+ CHECK(3 == str_size(p_str2));
mem_release(p_str1);
mem_release(p_str2);
}
mem_release(p_str);
}
+ TEST(Verify_str_at_should_return_null_char_if_index_WAY_out_of_range)
+ {
+ str_t* p_str = str_new("abc");
+ CHECK('\0' == str_at(p_str,42));
+ mem_release(p_str);
+ }
+
//-------------------------------------------------------------------------
// Test str_set function
//-------------------------------------------------------------------------
str_t* p_str1 = str_new("abc");
str_t* p_str2 = str_new("def");
str_t* p_str3 = str_concat(p_str1, p_str2);
- CHECK(p_str1 != p_str2);
CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
CHECK(0 == strcmp(str_cstr(p_str2), "def"));
CHECK(0 == strcmp(str_cstr(p_str3), "abcdef"));
str_t* p_str1 = str_new("abc");
str_t* p_str2 = str_new("def");
str_t* p_str3 = str_insert(p_str1, 0, p_str2);
- CHECK(p_str1 != p_str2);
CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
CHECK(0 == strcmp(str_cstr(p_str2), "def"));
CHECK(0 == strcmp(str_cstr(p_str3), "defabc"));
str_t* p_str1 = str_new("abc");
str_t* p_str2 = str_new("def");
str_t* p_str3 = str_insert(p_str1, 1, p_str2);
- CHECK(p_str1 != p_str2);
CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
CHECK(0 == strcmp(str_cstr(p_str2), "def"));
CHECK(0 == strcmp(str_cstr(p_str3), "adefbc"));
str_t* p_str1 = str_new("abc");
str_t* p_str2 = str_new("def");
str_t* p_str3 = str_insert(p_str1, 2, p_str2);
- CHECK(p_str1 != p_str2);
CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
CHECK(0 == strcmp(str_cstr(p_str2), "def"));
CHECK(0 == strcmp(str_cstr(p_str3), "abdefc"));
str_t* p_str1 = str_new("abc");
str_t* p_str2 = str_new("def");
str_t* p_str3 = str_insert(p_str1, 3, p_str2);
- CHECK(p_str1 != p_str2);
CHECK(0 == strcmp(str_cstr(p_str1), "abc"));
CHECK(0 == strcmp(str_cstr(p_str2), "def"));
CHECK(0 == strcmp(str_cstr(p_str3), "abcdef"));
mem_release(p_str2);
}
+ TEST(Verify_str_erase_2_chars)
+ {
+ str_t* p_str1 = str_new("abcdef");
+ str_t* p_str2 = str_erase(p_str1, 1, 3);
+ CHECK(p_str1 != p_str2);
+ CHECK(0 == strcmp(str_cstr(p_str2), "adef"));
+ CHECK(4 == str_size(p_str2));
+ mem_release(p_str1);
+ mem_release(p_str2);
+ }
+
+ TEST(Verify_str_erase_3_chars)
+ {
+ str_t* p_str1 = str_new("abcdef");
+ str_t* p_str2 = str_erase(p_str1, 1, 4);
+ CHECK(p_str1 != p_str2);
+ CHECK(0 == strcmp(str_cstr(p_str2), "aef"));
+ CHECK(3 == str_size(p_str2));
+ mem_release(p_str1);
+ mem_release(p_str2);
+ }
+
+ TEST(Verify_str_erase_4_chars)
+ {
+ str_t* p_str1 = str_new("abcdef");
+ str_t* p_str2 = str_erase(p_str1, 1, 5);
+ CHECK(p_str1 != p_str2);
+ CHECK(0 == strcmp(str_cstr(p_str2), "af"));
+ CHECK(2 == str_size(p_str2));
+ mem_release(p_str1);
+ mem_release(p_str2);
+ }
+
//-------------------------------------------------------------------------
// Test str_substr function
//-------------------------------------------------------------------------
{
str_t* p_str1 = str_new("abc");
str_t* p_str2 = str_new("abc");
- CHECK(0 == str_compare(p_str1, p_str2));
+ CHECK(str_compare(p_str1, p_str2) == 0);
mem_release(p_str1);
mem_release(p_str2);
}
- TEST(Verify_str_compare_should_return_negative_1_on_mismatch)
+ TEST(Verify_str_compare_should_return_less_than_zero_on_mismatch)
{
str_t* p_str1 = str_new("abc");
str_t* p_str2 = str_new("abd");
- CHECK(-1 == str_compare(p_str1, p_str2));
+ CHECK(str_compare(p_str1, p_str2) < 0);
mem_release(p_str1);
mem_release(p_str2);
}
- TEST(Verify_str_compare_should_return_positive_1_on_mismatch)
+ TEST(Verify_str_compare_should_return_greater_than_zero_on_mismatch)
{
str_t* p_str1 = str_new("abd");
str_t* p_str2 = str_new("abc");
- CHECK(1 == str_compare(p_str1, p_str2));
+ CHECK(str_compare(p_str1, p_str2) > 0);
+ mem_release(p_str1);
+ mem_release(p_str2);
+ }
+
+ TEST(Verify_str_compare_should_return_less_than_zero_if_case_is_different)
+ {
+ str_t* p_str1 = str_new("abC");
+ str_t* p_str2 = str_new("abc");
+ CHECK(str_compare(p_str1, p_str2) < 0);
+ mem_release(p_str1);
+ mem_release(p_str2);
+ }
+
+ TEST(Verify_str_compare_should_return_greater_than_zero_if_case_is_different)
+ {
+ str_t* p_str1 = str_new("abc");
+ str_t* p_str2 = str_new("abC");
+ CHECK(str_compare(p_str1, p_str2) > 0);
mem_release(p_str1);
mem_release(p_str2);
}
mem_release(p_str2);
}
+ TEST(Verify_str_find_should_find_first_occurrence)
+ {
+ str_t* p_str1 = str_new("_abcabc_");
+ str_t* p_str2 = str_new("abc");
+ CHECK(1 == str_find(p_str1, p_str2));
+ mem_release(p_str1);
+ mem_release(p_str2);
+ }
+
//-------------------------------------------------------------------------
// Test str_rfind function
//-------------------------------------------------------------------------
mem_release(p_str1);
mem_release(p_str2);
}
+
+ TEST(Verify_str_rfind_should_find_last_occurrence)
+ {
+ str_t* p_str1 = str_new("_abcabc_");
+ str_t* p_str2 = str_new("abc");
+ CHECK(4 == str_rfind(p_str1, p_str2));
+ mem_release(p_str1);
+ mem_release(p_str2);
+ }
}