summaryrefslogtreecommitdiffstats
path: root/tests/util/alloc.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:47:08 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:47:08 +0000
commit29b5ab554790bb57337a3b6ab9dcd963cf69d22e (patch)
treebe1456d2bc6c1fb078695fad7bc8f6b212062d3c /tests/util/alloc.c
parentInitial commit. (diff)
downloadlibgit2-upstream/1.7.2+ds.tar.xz
libgit2-upstream/1.7.2+ds.zip
Adding upstream version 1.7.2+ds.upstream/1.7.2+ds
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/util/alloc.c')
-rw-r--r--tests/util/alloc.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/util/alloc.c b/tests/util/alloc.c
new file mode 100644
index 0000000..492394a
--- /dev/null
+++ b/tests/util/alloc.c
@@ -0,0 +1,68 @@
+#include "clar_libgit2.h"
+#include "clar_libgit2_alloc.h"
+#include "alloc.h"
+
+void test_alloc__cleanup(void)
+{
+ cl_alloc_reset();
+}
+
+void test_alloc__oom(void)
+{
+ void *ptr = NULL;
+
+ cl_alloc_limit(0);
+
+ cl_assert(git__malloc(1) == NULL);
+ cl_assert(git__calloc(1, 1) == NULL);
+ cl_assert(git__realloc(ptr, 1) == NULL);
+ cl_assert(git__strdup("test") == NULL);
+ cl_assert(git__strndup("test", 4) == NULL);
+}
+
+void test_alloc__single_byte_is_exhausted(void)
+{
+ void *ptr;
+
+ cl_alloc_limit(1);
+
+ cl_assert(ptr = git__malloc(1));
+ cl_assert(git__malloc(1) == NULL);
+ git__free(ptr);
+}
+
+void test_alloc__free_replenishes_byte(void)
+{
+ void *ptr;
+
+ cl_alloc_limit(1);
+
+ cl_assert(ptr = git__malloc(1));
+ cl_assert(git__malloc(1) == NULL);
+ git__free(ptr);
+ cl_assert(ptr = git__malloc(1));
+ git__free(ptr);
+}
+
+void test_alloc__realloc(void)
+{
+ char *ptr = NULL;
+
+ cl_alloc_limit(3);
+
+ cl_assert(ptr = git__realloc(ptr, 1));
+ *ptr = 'x';
+
+ cl_assert(ptr = git__realloc(ptr, 1));
+ cl_assert_equal_i(*ptr, 'x');
+
+ cl_assert(ptr = git__realloc(ptr, 2));
+ cl_assert_equal_i(*ptr, 'x');
+
+ cl_assert(git__realloc(ptr, 2) == NULL);
+
+ cl_assert(ptr = git__realloc(ptr, 1));
+ cl_assert_equal_i(*ptr, 'x');
+
+ git__free(ptr);
+}