]> git.mdlowis.com Git - projs/libcds.git/commitdiff
Doxygen comments and more tests string-module
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 22 Aug 2014 02:07:21 +0000 (22:07 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 22 Aug 2014 02:07:21 +0000 (22:07 -0400)
source/string/str.c
source/string/str.h
tests/test_str.c

index 9d6b0e6a4cc274156199c16106b77a240cfc0673..d95ba176c41c742d3ee6a43443773df482ae3b4e 100644 (file)
@@ -115,12 +115,16 @@ str_t* str_substr(str_t* p_str, size_t start, size_t end)
 
 int str_compare(str_t* p_str1, str_t* p_str2)
 {
+    assert(NULL != p_str1);
+    assert(NULL != p_str2);
     return strcmp(p_str1->data, p_str2->data);
 }
 
 size_t str_find(str_t* p_str1, str_t* p_str2)
 {
-    size_t idx = -1;
+    size_t idx = SIZE_MAX;
+    assert(NULL != p_str1);
+    assert(NULL != p_str2);
     for(size_t i = 0; i < p_str1->size; i++) {
         if(0 == strncmp(&(p_str1->data[i]), p_str2->data, p_str2->size))
         {
@@ -133,7 +137,9 @@ size_t str_find(str_t* p_str1, str_t* p_str2)
 
 size_t str_rfind(str_t* p_str1, str_t* p_str2)
 {
-    size_t idx = -1;
+    size_t idx = SIZE_MAX;
+    assert(NULL != p_str1);
+    assert(NULL != p_str2);
     for(size_t i = p_str1->size; i > 0; i--) {
         if(0 == strncmp(&(p_str1->data[i-1]), p_str2->data, p_str2->size))
         {
index 4d5354445cc2006729233333065281ae8d207e94..95d2c2e02599ab7259add2db6b927d29754b93f9 100644 (file)
@@ -30,28 +30,145 @@ typedef struct str_t str_t;
  */
 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
index 084a6ad348932630bababe7cc7a90abf1e38c7c9..9f3a2bb6fee79ca643956a07d314e928c59798f0 100644 (file)
@@ -40,7 +40,7 @@ TEST_SUITE(String) {
         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);
     }
@@ -62,6 +62,13 @@ TEST_SUITE(String) {
         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
     //-------------------------------------------------------------------------
@@ -93,7 +100,6 @@ TEST_SUITE(String) {
         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"));
@@ -111,7 +117,6 @@ TEST_SUITE(String) {
         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"));
@@ -126,7 +131,6 @@ TEST_SUITE(String) {
         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"));
@@ -141,7 +145,6 @@ TEST_SUITE(String) {
         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"));
@@ -156,7 +159,6 @@ TEST_SUITE(String) {
         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"));
@@ -247,6 +249,39 @@ TEST_SUITE(String) {
         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
     //-------------------------------------------------------------------------
@@ -334,25 +369,43 @@ TEST_SUITE(String) {
     {
         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);
     }
@@ -405,6 +458,15 @@ TEST_SUITE(String) {
         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
     //-------------------------------------------------------------------------
@@ -452,4 +514,13 @@ TEST_SUITE(String) {
         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);
+    }
 }