summaryrefslogtreecommitdiff
path: root/tests/malloc_test.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2016-02-10 20:43:22 -0800
committerElliott Hughes <enh@google.com>2016-02-11 08:45:40 -0800
commit884f76e3aa081740f7cfe582b11af7446bf77bd9 (patch)
treeebd6b34509a2198628aa5a25b76a8ff4af4c2804 /tests/malloc_test.cpp
parent8fa00a5836b49bbacfd08151edb039ed7c6db21d (diff)
Add tests for zero-sized allocations.
POSIX lets us return null and set errno, but that would be annoying and surprising. Bug: http://b/27101951 Change-Id: I320a8a14884abb806a8d30e3e6cf1ede28b49335
Diffstat (limited to 'tests/malloc_test.cpp')
-rw-r--r--tests/malloc_test.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp
index 5af5a6fb5..d3a9d01e4 100644
--- a/tests/malloc_test.cpp
+++ b/tests/malloc_test.cpp
@@ -391,3 +391,42 @@ TEST(malloc, calloc_usable_size) {
free(zero_mem);
}
}
+
+TEST(malloc, malloc_0) {
+ void* p = malloc(0);
+ ASSERT_TRUE(p != nullptr);
+ free(p);
+}
+
+TEST(malloc, calloc_0_0) {
+ void* p = calloc(0, 0);
+ ASSERT_TRUE(p != nullptr);
+ free(p);
+}
+
+TEST(malloc, calloc_0_1) {
+ void* p = calloc(0, 1);
+ ASSERT_TRUE(p != nullptr);
+ free(p);
+}
+
+TEST(malloc, calloc_1_0) {
+ void* p = calloc(1, 0);
+ ASSERT_TRUE(p != nullptr);
+ free(p);
+}
+
+TEST(malloc, realloc_nullptr_0) {
+ // realloc(nullptr, size) is actually malloc(size).
+ void* p = realloc(nullptr, 0);
+ ASSERT_TRUE(p != nullptr);
+ free(p);
+}
+
+TEST(malloc, realloc_0) {
+ void* p = malloc(1024);
+ ASSERT_TRUE(p != nullptr);
+ // realloc(p, 0) is actually free(p).
+ void* p2 = realloc(p, 0);
+ ASSERT_TRUE(p2 == nullptr);
+}