Skip to content

Commit 7bd2409

Browse files
authored
pw_unit_test migration: lib support batch 4 (#33199)
* pw_unit_test migration: lib support batch 4 * fix merge error BUILD.gn
1 parent 33bfb4e commit 7bd2409

10 files changed

+752
-1080
lines changed

src/lib/support/tests/BUILD.gn

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ chip_test_suite("tests") {
2929
"TestBytesCircularBuffer.cpp",
3030
"TestBytesToHex.cpp",
3131
"TestCHIPCounter.cpp",
32+
"TestCHIPMem.cpp",
33+
"TestCHIPMemString.cpp",
3234
"TestDefer.cpp",
3335
"TestErrorStr.cpp",
3436
"TestFixedBufferAllocator.cpp",
3537
"TestFold.cpp",
3638
"TestIniEscaping.cpp",
39+
"TestIntrusiveList.cpp",
40+
"TestJsonToTlv.cpp",
41+
"TestJsonToTlvToJson.cpp",
3742
"TestPersistedCounter.cpp",
3843
"TestPrivateHeap.cpp",
3944
"TestSafeInt.cpp",
@@ -47,8 +52,11 @@ chip_test_suite("tests") {
4752
"TestStringSplitter.cpp",
4853
"TestTestPersistentStorageDelegate.cpp",
4954
"TestTimeUtils.cpp",
55+
"TestTlvJson.cpp",
56+
"TestTlvToJson.cpp",
5057
"TestUtf8.cpp",
5158
"TestVariant.cpp",
59+
"TestZclString.cpp",
5260
]
5361
sources = []
5462

@@ -76,17 +84,9 @@ chip_test_suite_using_nltest("tests_nltest") {
7684
output_name = "libSupportTestsNL"
7785

7886
test_sources = [
79-
"TestCHIPMem.cpp",
80-
"TestCHIPMemString.cpp",
81-
"TestIntrusiveList.cpp",
82-
"TestJsonToTlv.cpp",
83-
"TestJsonToTlvToJson.cpp",
8487
"TestPool.cpp",
8588
"TestStateMachine.cpp",
8689
"TestThreadOperationalDataset.cpp",
87-
"TestTlvJson.cpp",
88-
"TestTlvToJson.cpp",
89-
"TestZclString.cpp",
9090
]
9191
sources = []
9292

src/lib/support/tests/TestCHIPCounter.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
* limitations under the License.
1717
*/
1818

19+
#include <stdint.h>
20+
1921
#include <gtest/gtest.h>
22+
2023
#include <lib/support/CHIPCounter.h>
21-
#include <stdint.h>
2224

2325
using namespace chip;
2426

src/lib/support/tests/TestCHIPMem.cpp

+33-68
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@
2929
#include <stdlib.h>
3030
#include <string.h>
3131

32+
#include <gtest/gtest.h>
33+
3234
#include <lib/support/CHIPMem.h>
3335
#include <lib/support/CodeUtils.h>
34-
#include <lib/support/UnitTestContext.h>
35-
#include <lib/support/UnitTestRegistration.h>
36-
#include <nlunit-test.h>
3736

3837
using namespace chip;
3938
using namespace chip::Logging;
@@ -43,60 +42,67 @@ using namespace chip::Platform;
4342
// Unit tests
4443
// =================================
4544

46-
static void TestMemAlloc_Malloc(nlTestSuite * inSuite, void * inContext)
45+
class TestCHIPMem : public ::testing::Test
46+
{
47+
public:
48+
static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); }
49+
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
50+
};
51+
52+
TEST_F(TestCHIPMem, TestMemAlloc_Malloc)
4753
{
4854
char * p1 = nullptr;
4955
char * p2 = nullptr;
5056
char * p3 = nullptr;
5157

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

5662
p2 = static_cast<char *>(MemoryAlloc(256));
57-
NL_TEST_ASSERT(inSuite, p2 != nullptr);
63+
EXPECT_NE(p2, nullptr);
5864

5965
chip::Platform::MemoryFree(p1);
6066
chip::Platform::MemoryFree(p2);
6167

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

6672
p2 = static_cast<char *>(MemoryAlloc(256));
67-
NL_TEST_ASSERT(inSuite, p2 != nullptr);
73+
EXPECT_NE(p2, nullptr);
6874

6975
p3 = static_cast<char *>(MemoryAlloc(256));
70-
NL_TEST_ASSERT(inSuite, p3 != nullptr);
76+
EXPECT_NE(p3, nullptr);
7177

7278
chip::Platform::MemoryFree(p1);
7379
chip::Platform::MemoryFree(p2);
7480
chip::Platform::MemoryFree(p3);
7581
}
7682

77-
static void TestMemAlloc_Calloc(nlTestSuite * inSuite, void * inContext)
83+
TEST_F(TestCHIPMem, TestMemAlloc_Calloc)
7884
{
79-
char * p = static_cast<char *>(MemoryCalloc(128, true));
80-
NL_TEST_EXIT_ON_FAILED_ASSERT(inSuite, p != nullptr);
85+
char * p = static_cast<char *>(MemoryCalloc(128, sizeof(char)));
86+
ASSERT_NE(p, nullptr);
8187

8288
for (int i = 0; i < 128; i++)
83-
NL_TEST_ASSERT(inSuite, p[i] == 0);
89+
EXPECT_EQ(p[i], 0);
8490

8591
chip::Platform::MemoryFree(p);
8692
}
8793

88-
static void TestMemAlloc_Realloc(nlTestSuite * inSuite, void * inContext)
94+
TEST_F(TestCHIPMem, TestMemAlloc_Realloc)
8995
{
9096
char * pa = static_cast<char *>(MemoryAlloc(128));
91-
NL_TEST_ASSERT(inSuite, pa != nullptr);
97+
EXPECT_NE(pa, nullptr);
9298

9399
char * pb = static_cast<char *>(MemoryRealloc(pa, 256));
94-
NL_TEST_ASSERT(inSuite, pb != nullptr);
100+
EXPECT_NE(pb, nullptr);
95101

96102
chip::Platform::MemoryFree(pb);
97103
}
98104

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

115121
{
116122
auto ptr = MakeUnique<Cls>(&constructorCalled, &destructorCalled);
117-
NL_TEST_ASSERT(inSuite, constructorCalled == 1);
118-
NL_TEST_ASSERT(inSuite, destructorCalled == 0);
123+
EXPECT_EQ(constructorCalled, 1);
124+
EXPECT_EQ(destructorCalled, 0);
119125
IgnoreUnusedVariable(ptr);
120126
}
121127

122-
NL_TEST_ASSERT(inSuite, destructorCalled == 1);
128+
EXPECT_TRUE(destructorCalled);
123129
}
124130

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

153159
// Verify that by sharing to otherReference, we weren't destructed.
154-
NL_TEST_ASSERT(inSuite, instanceDestructorCalled == 0);
160+
EXPECT_EQ(instanceDestructorCalled, 0);
155161

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

159165
// Verify that the new instance was constructed and the first instance was
160166
// destructed, and that we retain a reference to the new instance.
161-
NL_TEST_ASSERT(inSuite, instanceConstructorCalled == 1);
162-
NL_TEST_ASSERT(inSuite, instanceDestructorCalled == 1);
163-
NL_TEST_ASSERT(inSuite, otherInstanceConstructorCalled == 1);
164-
NL_TEST_ASSERT(inSuite, otherInstanceDestructorCalled == 0);
165-
}
166-
167-
/**
168-
* Test Suite. It lists all the test functions.
169-
*/
170-
static const nlTest sTests[] = { NL_TEST_DEF("Test MemAlloc::Malloc", TestMemAlloc_Malloc),
171-
NL_TEST_DEF("Test MemAlloc::Calloc", TestMemAlloc_Calloc),
172-
NL_TEST_DEF("Test MemAlloc::Realloc", TestMemAlloc_Realloc),
173-
NL_TEST_DEF("Test MemAlloc::UniquePtr", TestMemAlloc_UniquePtr),
174-
NL_TEST_DEF("Test MemAlloc::SharedPtr", TestMemAlloc_SharedPtr),
175-
NL_TEST_SENTINEL() };
176-
177-
/**
178-
* Set up the test suite.
179-
*/
180-
int TestMemAlloc_Setup(void * inContext)
181-
{
182-
CHIP_ERROR error = MemoryInit();
183-
if (error != CHIP_NO_ERROR)
184-
return (FAILURE);
185-
return (SUCCESS);
167+
EXPECT_EQ(instanceConstructorCalled, 1);
168+
EXPECT_EQ(instanceDestructorCalled, 1);
169+
EXPECT_EQ(otherInstanceConstructorCalled, 1);
170+
EXPECT_EQ(otherInstanceDestructorCalled, 0);
186171
}
187-
188-
/**
189-
* Tear down the test suite.
190-
*/
191-
int TestMemAlloc_Teardown(void * inContext)
192-
{
193-
MemoryShutdown();
194-
return (SUCCESS);
195-
}
196-
197-
int TestMemAlloc()
198-
{
199-
nlTestSuite theSuite = { "CHIP Memory Allocation tests", &sTests[0], TestMemAlloc_Setup, TestMemAlloc_Teardown };
200-
201-
// Run test suite against one context.
202-
nlTestRunner(&theSuite, nullptr);
203-
return nlTestRunnerStats(&theSuite);
204-
}
205-
206-
CHIP_REGISTER_TEST_SUITE(TestMemAlloc)

0 commit comments

Comments
 (0)