Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix memory leaks in malloc cctests #9667

Merged
merged 1 commit into from
Nov 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions test/cctest/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,34 +94,41 @@ namespace node {
void LowMemoryNotification() {}
}

#define TEST_AND_FREE(expression) \
do { \
auto pointer = expression; \
EXPECT_NE(nullptr, pointer); \
free(pointer); \
} while (0)

TEST(UtilTest, Malloc) {
using node::Malloc;
EXPECT_NE(nullptr, Malloc<char>(0));
EXPECT_NE(nullptr, Malloc<char>(1));
EXPECT_NE(nullptr, Malloc(0));
EXPECT_NE(nullptr, Malloc(1));
TEST_AND_FREE(Malloc<char>(0));
TEST_AND_FREE(Malloc<char>(1));
TEST_AND_FREE(Malloc(0));
TEST_AND_FREE(Malloc(1));
}

TEST(UtilTest, Calloc) {
using node::Calloc;
EXPECT_NE(nullptr, Calloc<char>(0));
EXPECT_NE(nullptr, Calloc<char>(1));
EXPECT_NE(nullptr, Calloc(0));
EXPECT_NE(nullptr, Calloc(1));
TEST_AND_FREE(Calloc<char>(0));
TEST_AND_FREE(Calloc<char>(1));
TEST_AND_FREE(Calloc(0));
TEST_AND_FREE(Calloc(1));
}

TEST(UtilTest, UncheckedMalloc) {
using node::UncheckedMalloc;
EXPECT_NE(nullptr, UncheckedMalloc<char>(0));
EXPECT_NE(nullptr, UncheckedMalloc<char>(1));
EXPECT_NE(nullptr, UncheckedMalloc(0));
EXPECT_NE(nullptr, UncheckedMalloc(1));
TEST_AND_FREE(UncheckedMalloc<char>(0));
TEST_AND_FREE(UncheckedMalloc<char>(1));
TEST_AND_FREE(UncheckedMalloc(0));
TEST_AND_FREE(UncheckedMalloc(1));
}

TEST(UtilTest, UncheckedCalloc) {
using node::UncheckedCalloc;
EXPECT_NE(nullptr, UncheckedCalloc<char>(0));
EXPECT_NE(nullptr, UncheckedCalloc<char>(1));
EXPECT_NE(nullptr, UncheckedCalloc(0));
EXPECT_NE(nullptr, UncheckedCalloc(1));
TEST_AND_FREE(UncheckedCalloc<char>(0));
TEST_AND_FREE(UncheckedCalloc<char>(1));
TEST_AND_FREE(UncheckedCalloc(0));
TEST_AND_FREE(UncheckedCalloc(1));
}