summaryrefslogtreecommitdiff
path: root/tests/unit/core/test-string.cpp
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2017-03-03 22:35:09 +0100
committerSébastien Helleu <flashcode@flashtux.org>2017-03-25 14:18:19 +0100
commit07d16903f3c4f8be84e777a2067085f305e8d2f0 (patch)
tree964063edee2de816033db0ceae55984f4d595b12 /tests/unit/core/test-string.cpp
parenta5b00ec9793b0a8a20074ffb801615c347e66709 (diff)
downloadweechat-07d16903f3c4f8be84e777a2067085f305e8d2f0.zip
api: add dynamic string functions (string_dyn_*)
New functions: - string_dyn_alloc - string_dyn_copy - string_dyn_concat - string_dyn_free
Diffstat (limited to 'tests/unit/core/test-string.cpp')
-rw-r--r--tests/unit/core/test-string.cpp115
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/unit/core/test-string.cpp b/tests/unit/core/test-string.cpp
index 748aff2fe..4a9cab548 100644
--- a/tests/unit/core/test-string.cpp
+++ b/tests/unit/core/test-string.cpp
@@ -1345,3 +1345,118 @@ TEST(String, Shared)
string_shared_free (str3);
LONGS_EQUAL(count + 0, string_hashtable_shared->items_count);
}
+
+/*
+ * Tests functions:
+ * string_dyn_alloc
+ * string_dyn_copy
+ * string_dyn_concat
+ * string_dyn_free
+ */
+
+TEST(String, Dyn)
+{
+ char **str, *str_ptr;
+ struct t_string_dyn *ptr_string_dyn;
+
+ POINTERS_EQUAL(NULL, string_dyn_alloc (-1));
+ POINTERS_EQUAL(NULL, string_dyn_alloc (0));
+
+ str = string_dyn_alloc (1);
+ CHECK(str);
+ CHECK(*str);
+ STRCMP_EQUAL("", *str);
+
+ /* check internal structure content */
+ ptr_string_dyn = (struct t_string_dyn *)str;
+ LONGS_EQUAL(1, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(1, ptr_string_dyn->size);
+ STRCMP_EQUAL("", ptr_string_dyn->string);
+
+ /* check copy with NULL */
+ LONGS_EQUAL(1, string_dyn_copy (str, NULL));
+ LONGS_EQUAL(1, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(1, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("", ptr_string_dyn->string);
+ STRCMP_EQUAL("", *str);
+
+ /* check copy with an empty string */
+ LONGS_EQUAL(1, string_dyn_copy (str, ""));
+ LONGS_EQUAL(1, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(1, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("", ptr_string_dyn->string);
+ STRCMP_EQUAL("", *str);
+
+ /* check copy with some strings */
+ LONGS_EQUAL(1, string_dyn_copy (str, "a"));
+ LONGS_EQUAL(2, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(2, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("a", ptr_string_dyn->string);
+ STRCMP_EQUAL("a", *str);
+
+ LONGS_EQUAL(1, string_dyn_copy (str, "abcd"));
+ LONGS_EQUAL(5, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(5, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("abcd", ptr_string_dyn->string);
+ STRCMP_EQUAL("abcd", *str);
+
+ string_dyn_free (str, 1);
+
+ str = string_dyn_alloc (1);
+
+ /* check concat with NULL */
+ LONGS_EQUAL(1, string_dyn_concat (str, NULL));
+ LONGS_EQUAL(1, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(1, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("", ptr_string_dyn->string);
+ STRCMP_EQUAL("", *str);
+
+ /* check concat with an empty string */
+ LONGS_EQUAL(1, string_dyn_concat (str, ""));
+ LONGS_EQUAL(1, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(1, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("", ptr_string_dyn->string);
+ STRCMP_EQUAL("", *str);
+
+ /* check concat with some strings */
+ LONGS_EQUAL(1, string_dyn_concat (str, "a"));
+ LONGS_EQUAL(2, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(2, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("a", ptr_string_dyn->string);
+ STRCMP_EQUAL("a", *str);
+
+ LONGS_EQUAL(1, string_dyn_concat (str, "bcd"));
+ LONGS_EQUAL(5, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(5, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("abcd", ptr_string_dyn->string);
+ STRCMP_EQUAL("abcd", *str);
+
+ LONGS_EQUAL(1, string_dyn_concat (str, "e"));
+ LONGS_EQUAL(7, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(6, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("abcde", ptr_string_dyn->string);
+ STRCMP_EQUAL("abcde", *str);
+
+ LONGS_EQUAL(1, string_dyn_concat (str, "fg"));
+ LONGS_EQUAL(10, ptr_string_dyn->size_alloc);
+ LONGS_EQUAL(8, ptr_string_dyn->size);
+ POINTERS_EQUAL(ptr_string_dyn->string, *str);
+ STRCMP_EQUAL("abcdefg", ptr_string_dyn->string);
+ STRCMP_EQUAL("abcdefg", *str);
+
+ str_ptr = *str;
+ string_dyn_free (str, 0);
+
+ STRCMP_EQUAL("abcdefg", str_ptr);
+
+ free (str_ptr);
+}