Skip to content

Commit

Permalink
pw_unit_test migration: lib support batch 4 (project-chip#33199)
Browse files Browse the repository at this point in the history
* pw_unit_test migration: lib support batch 4

* fix merge error BUILD.gn
  • Loading branch information
Alami-Amine committed Apr 29, 2024
1 parent 344705b commit e418c80
Show file tree
Hide file tree
Showing 10 changed files with 752 additions and 1,080 deletions.
16 changes: 8 additions & 8 deletions src/lib/support/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ chip_test_suite("tests") {
"TestBytesCircularBuffer.cpp",
"TestBytesToHex.cpp",
"TestCHIPCounter.cpp",
"TestCHIPMem.cpp",
"TestCHIPMemString.cpp",
"TestDefer.cpp",
"TestErrorStr.cpp",
"TestFixedBufferAllocator.cpp",
"TestFold.cpp",
"TestIniEscaping.cpp",
"TestIntrusiveList.cpp",
"TestJsonToTlv.cpp",
"TestJsonToTlvToJson.cpp",
"TestPersistedCounter.cpp",
"TestPool.cpp",
"TestPrivateHeap.cpp",
Expand All @@ -48,8 +53,11 @@ chip_test_suite("tests") {
"TestStringSplitter.cpp",
"TestTestPersistentStorageDelegate.cpp",
"TestTimeUtils.cpp",
"TestTlvJson.cpp",
"TestTlvToJson.cpp",
"TestUtf8.cpp",
"TestVariant.cpp",
"TestZclString.cpp",
]
sources = []

Expand Down Expand Up @@ -77,16 +85,8 @@ chip_test_suite_using_nltest("tests_nltest") {
output_name = "libSupportTestsNL"

test_sources = [
"TestCHIPMem.cpp",
"TestCHIPMemString.cpp",
"TestIntrusiveList.cpp",
"TestJsonToTlv.cpp",
"TestJsonToTlvToJson.cpp",
"TestStateMachine.cpp",
"TestThreadOperationalDataset.cpp",
"TestTlvJson.cpp",
"TestTlvToJson.cpp",
"TestZclString.cpp",
]
sources = []

Expand Down
4 changes: 3 additions & 1 deletion src/lib/support/tests/TestCHIPCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
* limitations under the License.
*/

#include <stdint.h>

#include <gtest/gtest.h>

#include <lib/support/CHIPCounter.h>
#include <stdint.h>

using namespace chip;

Expand Down
101 changes: 33 additions & 68 deletions src/lib/support/tests/TestCHIPMem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
#include <stdlib.h>
#include <string.h>

#include <gtest/gtest.h>

#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>

using namespace chip;
using namespace chip::Logging;
Expand All @@ -43,60 +42,67 @@ using namespace chip::Platform;
// Unit tests
// =================================

static void TestMemAlloc_Malloc(nlTestSuite * inSuite, void * inContext)
class TestCHIPMem : public ::testing::Test
{
public:
static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); }
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
};

TEST_F(TestCHIPMem, TestMemAlloc_Malloc)
{
char * p1 = nullptr;
char * p2 = nullptr;
char * p3 = nullptr;

// Verify long-term allocation
p1 = static_cast<char *>(MemoryAlloc(64));
NL_TEST_ASSERT(inSuite, p1 != nullptr);
EXPECT_NE(p1, nullptr);

p2 = static_cast<char *>(MemoryAlloc(256));
NL_TEST_ASSERT(inSuite, p2 != nullptr);
EXPECT_NE(p2, nullptr);

chip::Platform::MemoryFree(p1);
chip::Platform::MemoryFree(p2);

// Verify short-term allocation
p1 = static_cast<char *>(MemoryAlloc(256));
NL_TEST_ASSERT(inSuite, p1 != nullptr);
EXPECT_NE(p1, nullptr);

p2 = static_cast<char *>(MemoryAlloc(256));
NL_TEST_ASSERT(inSuite, p2 != nullptr);
EXPECT_NE(p2, nullptr);

p3 = static_cast<char *>(MemoryAlloc(256));
NL_TEST_ASSERT(inSuite, p3 != nullptr);
EXPECT_NE(p3, nullptr);

chip::Platform::MemoryFree(p1);
chip::Platform::MemoryFree(p2);
chip::Platform::MemoryFree(p3);
}

static void TestMemAlloc_Calloc(nlTestSuite * inSuite, void * inContext)
TEST_F(TestCHIPMem, TestMemAlloc_Calloc)
{
char * p = static_cast<char *>(MemoryCalloc(128, true));
NL_TEST_EXIT_ON_FAILED_ASSERT(inSuite, p != nullptr);
char * p = static_cast<char *>(MemoryCalloc(128, sizeof(char)));
ASSERT_NE(p, nullptr);

for (int i = 0; i < 128; i++)
NL_TEST_ASSERT(inSuite, p[i] == 0);
EXPECT_EQ(p[i], 0);

chip::Platform::MemoryFree(p);
}

static void TestMemAlloc_Realloc(nlTestSuite * inSuite, void * inContext)
TEST_F(TestCHIPMem, TestMemAlloc_Realloc)
{
char * pa = static_cast<char *>(MemoryAlloc(128));
NL_TEST_ASSERT(inSuite, pa != nullptr);
EXPECT_NE(pa, nullptr);

char * pb = static_cast<char *>(MemoryRealloc(pa, 256));
NL_TEST_ASSERT(inSuite, pb != nullptr);
EXPECT_NE(pb, nullptr);

chip::Platform::MemoryFree(pb);
}

static void TestMemAlloc_UniquePtr(nlTestSuite * inSuite, void * inContext)
TEST_F(TestCHIPMem, TestMemAlloc_UniquePtr)
{
// UniquePtr is a wrapper of std::unique_ptr for platform allocators, we just check if we created a correct wrapper here.
int constructorCalled = 0;
Expand All @@ -114,15 +120,15 @@ static void TestMemAlloc_UniquePtr(nlTestSuite * inSuite, void * inContext)

{
auto ptr = MakeUnique<Cls>(&constructorCalled, &destructorCalled);
NL_TEST_ASSERT(inSuite, constructorCalled == 1);
NL_TEST_ASSERT(inSuite, destructorCalled == 0);
EXPECT_EQ(constructorCalled, 1);
EXPECT_EQ(destructorCalled, 0);
IgnoreUnusedVariable(ptr);
}

NL_TEST_ASSERT(inSuite, destructorCalled == 1);
EXPECT_TRUE(destructorCalled);
}

static void TestMemAlloc_SharedPtr(nlTestSuite * inSuite, void * inContext)
TEST_F(TestCHIPMem, TestMemAlloc_SharedPtr)
{
// SharedPtr is a wrapper of std::shared_ptr for platform allocators.
int instanceConstructorCalled = 0;
Expand All @@ -145,62 +151,21 @@ static void TestMemAlloc_SharedPtr(nlTestSuite * inSuite, void * inContext)
SharedPtr<Cls> otherReference;
{
auto ptr = MakeShared<Cls>(&instanceConstructorCalled, &instanceDestructorCalled);
NL_TEST_ASSERT(inSuite, instanceConstructorCalled == 1);
EXPECT_EQ(instanceConstructorCalled, 1);
// Capture a shared reference so we aren't destructed when we leave this scope.
otherReference = ptr;
}

// Verify that by sharing to otherReference, we weren't destructed.
NL_TEST_ASSERT(inSuite, instanceDestructorCalled == 0);
EXPECT_EQ(instanceDestructorCalled, 0);

// Now drop our reference.
otherReference = MakeShared<Cls>(&otherInstanceConstructorCalled, &otherInstanceDestructorCalled);

// Verify that the new instance was constructed and the first instance was
// destructed, and that we retain a reference to the new instance.
NL_TEST_ASSERT(inSuite, instanceConstructorCalled == 1);
NL_TEST_ASSERT(inSuite, instanceDestructorCalled == 1);
NL_TEST_ASSERT(inSuite, otherInstanceConstructorCalled == 1);
NL_TEST_ASSERT(inSuite, otherInstanceDestructorCalled == 0);
}

/**
* Test Suite. It lists all the test functions.
*/
static const nlTest sTests[] = { NL_TEST_DEF("Test MemAlloc::Malloc", TestMemAlloc_Malloc),
NL_TEST_DEF("Test MemAlloc::Calloc", TestMemAlloc_Calloc),
NL_TEST_DEF("Test MemAlloc::Realloc", TestMemAlloc_Realloc),
NL_TEST_DEF("Test MemAlloc::UniquePtr", TestMemAlloc_UniquePtr),
NL_TEST_DEF("Test MemAlloc::SharedPtr", TestMemAlloc_SharedPtr),
NL_TEST_SENTINEL() };

/**
* Set up the test suite.
*/
int TestMemAlloc_Setup(void * inContext)
{
CHIP_ERROR error = MemoryInit();
if (error != CHIP_NO_ERROR)
return (FAILURE);
return (SUCCESS);
EXPECT_EQ(instanceConstructorCalled, 1);
EXPECT_EQ(instanceDestructorCalled, 1);
EXPECT_EQ(otherInstanceConstructorCalled, 1);
EXPECT_EQ(otherInstanceDestructorCalled, 0);
}

/**
* Tear down the test suite.
*/
int TestMemAlloc_Teardown(void * inContext)
{
MemoryShutdown();
return (SUCCESS);
}

int TestMemAlloc()
{
nlTestSuite theSuite = { "CHIP Memory Allocation tests", &sTests[0], TestMemAlloc_Setup, TestMemAlloc_Teardown };

// Run test suite against one context.
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestMemAlloc)
Loading

0 comments on commit e418c80

Please sign in to comment.