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

pw_unit_test migration: lib support batch 4 #33199

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 9 additions & 9 deletions src/lib/support/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,26 @@ chip_test_suite("tests") {
"TestBitMask.cpp",
"TestBufferReader.cpp",
"TestBytesToHex.cpp",
"TestCHIPCounter.cpp",
"TestCHIPMem.cpp",
"TestCHIPMemString.cpp",
"TestDefer.cpp",
"TestFixedBufferAllocator.cpp",
"TestFold.cpp",
"TestIniEscaping.cpp",
"TestIntrusiveList.cpp",
"TestJsonToTlv.cpp",
"TestJsonToTlvToJson.cpp",
"TestSafeInt.cpp",
"TestSafeString.cpp",
"TestScoped.cpp",
"TestSpan.cpp",
"TestStaticSupportSmartPtr.cpp",
"TestTestPersistentStorageDelegate.cpp",
"TestTlvJson.cpp",
"TestTlvToJson.cpp",
"TestUtf8.cpp",
"TestZclString.cpp",
]
sources = []

Expand Down Expand Up @@ -66,13 +75,7 @@ chip_test_suite_using_nltest("tests_nltest") {
test_sources = [
"TestBufferWriter.cpp",
"TestBytesCircularBuffer.cpp",
"TestCHIPCounter.cpp",
"TestCHIPMem.cpp",
"TestCHIPMemString.cpp",
"TestErrorStr.cpp",
"TestIntrusiveList.cpp",
"TestJsonToTlv.cpp",
"TestJsonToTlvToJson.cpp",
"TestPersistedCounter.cpp",
"TestPool.cpp",
"TestPrivateHeap.cpp",
Expand All @@ -83,10 +86,7 @@ chip_test_suite_using_nltest("tests_nltest") {
"TestStringSplitter.cpp",
"TestThreadOperationalDataset.cpp",
"TestTimeUtils.cpp",
"TestTlvJson.cpp",
"TestTlvToJson.cpp",
"TestVariant.cpp",
"TestZclString.cpp",
]
sources = []

Expand Down
76 changes: 13 additions & 63 deletions src/lib/support/tests/TestCHIPCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,82 +16,32 @@
* limitations under the License.
*/

#include <nlunit-test.h>
#include <gtest/gtest.h>

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

static void CheckStartWithZero(nlTestSuite * inSuite, void * inContext)
TEST(TestCHIPCounter, TestCheckStartWithZero)
{
chip::MonotonicallyIncreasingCounter<uint64_t> counter;
NL_TEST_ASSERT(inSuite, counter.GetValue() == 0);
EXPECT_EQ(counter.GetValue(), 0u);
}

static void CheckInitialize(nlTestSuite * inSuite, void * inContext)
TEST(TestCHIPCounter, TestCheckInitialize)
{
chip::MonotonicallyIncreasingCounter<uint64_t> counter;

NL_TEST_ASSERT(inSuite, counter.Init(4321) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, counter.GetValue() == 4321);
EXPECT_EQ(counter.Init(4321), CHIP_NO_ERROR);
EXPECT_EQ(counter.GetValue(), 4321u);
}

static void CheckAdvance(nlTestSuite * inSuite, void * inContext)
TEST(TestCHIPCounter, TestCheckAdvance)
{
chip::MonotonicallyIncreasingCounter<uint64_t> counter;

NL_TEST_ASSERT(inSuite, counter.Init(22) == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, counter.GetValue() == 22);
NL_TEST_ASSERT(inSuite, counter.Advance() == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, counter.GetValue() == 23);
NL_TEST_ASSERT(inSuite, counter.Advance() == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, counter.GetValue() == 24);
EXPECT_EQ(counter.Init(22), CHIP_NO_ERROR);
EXPECT_EQ(counter.GetValue(), 22u);
EXPECT_EQ(counter.Advance(), CHIP_NO_ERROR);
EXPECT_EQ(counter.GetValue(), 23u);
EXPECT_EQ(counter.Advance(), CHIP_NO_ERROR);
EXPECT_EQ(counter.GetValue(), 24u);
}

/**
* Test Suite. It lists all the test functions.
*/

// clang-format off
static const nlTest sTests[] =
{
NL_TEST_DEF("Start with zero", CheckStartWithZero),
NL_TEST_DEF("Can initialize", CheckInitialize),
NL_TEST_DEF("Can Advance", CheckAdvance),
NL_TEST_SENTINEL()
};
// clang-format on

/**
* Set up the test suite.
*/
static int TestSetup(void * inContext)
{
return (SUCCESS);
}

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

int TestCHIPCounter()
{
// clang-format off
nlTestSuite theSuite = {
"chip-counter",
&sTests[0],
TestSetup,
TestTeardown
};
// clang-format on

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

return nlTestRunnerStats(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestCHIPCounter)
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
Loading