aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2015-02-06 11:50:41 +0100
committerAndreas Schneider <asn@cryptomilk.org>2015-02-08 09:41:36 +0100
commit28f3315e86fb91926ec2ebd217af5a7a106e7d2a (patch)
tree1b021c388036fa1d76d558783cc8903480778ab3
parent5c61f539bb359770d54ce32413cd781b617485b3 (diff)
downloadcmocka-28f3315e86fb91926ec2ebd217af5a7a106e7d2a.tar.gz
cmocka-28f3315e86fb91926ec2ebd217af5a7a106e7d2a.tar.xz
cmocka-28f3315e86fb91926ec2ebd217af5a7a106e7d2a.zip
tests: Add tests for test_malloc() and test_realloc().
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/test_alloc.c71
2 files changed, 72 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 9c00846..a058b5f 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -7,6 +7,7 @@ include_directories(
)
set(CMOCKA_TESTS
+ test_alloc
test_fixtures
test_group_fixtures
test_assert_macros
diff --git a/tests/test_alloc.c b/tests/test_alloc.c
new file mode 100644
index 0000000..6008499
--- /dev/null
+++ b/tests/test_alloc.c
@@ -0,0 +1,71 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+static void torture_test_malloc(void **state)
+{
+ char *str;
+ size_t str_len;
+ size_t len;
+
+ (void)state; /* unsused */
+
+ str_len = 12;
+ str = (char *)test_malloc(str_len);
+ assert_non_null(str);
+
+ len = snprintf(str, str_len, "test string");
+ assert_int_equal(len, 11);
+
+ len = strlen(str);
+ assert_int_equal(len, 11);
+
+ test_free(str);
+}
+
+static void torture_test_realloc(void **state)
+{
+ char *str;
+ char *tmp;
+ size_t str_len;
+ size_t len;
+
+ (void)state; /* unsused */
+
+ str_len = 16;
+ str = (char *)test_malloc(str_len);
+ assert_non_null(str);
+
+ len = snprintf(str, str_len, "test string 123");
+ assert_int_equal(len, 15);
+
+ len = strlen(str);
+ assert_int_equal(len, 15);
+
+ str_len = 20;
+ tmp = test_realloc(str, str_len);
+ assert_non_null(tmp);
+
+ str = tmp;
+ len = strlen(str);
+ assert_string_equal(tmp, "test string 123");
+
+ snprintf(str + len, str_len - len, "4567");
+ assert_string_equal(tmp, "test string 1234567");
+
+ test_free(str);
+}
+
+int main(void) {
+ const struct CMUnitTest alloc_tests[] = {
+ cmocka_unit_test(torture_test_malloc),
+ cmocka_unit_test(torture_test_realloc),
+ };
+
+ return cmocka_run_group_tests(alloc_tests, NULL, NULL);
+}