29
29
#include < stdlib.h>
30
30
#include < string.h>
31
31
32
+ #include < gtest/gtest.h>
33
+
32
34
#include < lib/support/CHIPMem.h>
33
35
#include < lib/support/CodeUtils.h>
34
- #include < lib/support/UnitTestContext.h>
35
- #include < lib/support/UnitTestRegistration.h>
36
- #include < nlunit-test.h>
37
36
38
37
using namespace chip ;
39
38
using namespace chip ::Logging;
@@ -43,60 +42,67 @@ using namespace chip::Platform;
43
42
// Unit tests
44
43
// =================================
45
44
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)
47
53
{
48
54
char * p1 = nullptr ;
49
55
char * p2 = nullptr ;
50
56
char * p3 = nullptr ;
51
57
52
58
// Verify long-term allocation
53
59
p1 = static_cast <char *>(MemoryAlloc (64 ));
54
- NL_TEST_ASSERT (inSuite, p1 != nullptr );
60
+ EXPECT_NE (p1, nullptr );
55
61
56
62
p2 = static_cast <char *>(MemoryAlloc (256 ));
57
- NL_TEST_ASSERT (inSuite, p2 != nullptr );
63
+ EXPECT_NE (p2, nullptr );
58
64
59
65
chip::Platform::MemoryFree (p1);
60
66
chip::Platform::MemoryFree (p2);
61
67
62
68
// Verify short-term allocation
63
69
p1 = static_cast <char *>(MemoryAlloc (256 ));
64
- NL_TEST_ASSERT (inSuite, p1 != nullptr );
70
+ EXPECT_NE (p1, nullptr );
65
71
66
72
p2 = static_cast <char *>(MemoryAlloc (256 ));
67
- NL_TEST_ASSERT (inSuite, p2 != nullptr );
73
+ EXPECT_NE (p2, nullptr );
68
74
69
75
p3 = static_cast <char *>(MemoryAlloc (256 ));
70
- NL_TEST_ASSERT (inSuite, p3 != nullptr );
76
+ EXPECT_NE (p3, nullptr );
71
77
72
78
chip::Platform::MemoryFree (p1);
73
79
chip::Platform::MemoryFree (p2);
74
80
chip::Platform::MemoryFree (p3);
75
81
}
76
82
77
- static void TestMemAlloc_Calloc (nlTestSuite * inSuite, void * inContext )
83
+ TEST_F (TestCHIPMem, TestMemAlloc_Calloc )
78
84
{
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 );
81
87
82
88
for (int i = 0 ; i < 128 ; i++)
83
- NL_TEST_ASSERT (inSuite, p[i] == 0 );
89
+ EXPECT_EQ ( p[i], 0 );
84
90
85
91
chip::Platform::MemoryFree (p);
86
92
}
87
93
88
- static void TestMemAlloc_Realloc (nlTestSuite * inSuite, void * inContext )
94
+ TEST_F (TestCHIPMem, TestMemAlloc_Realloc )
89
95
{
90
96
char * pa = static_cast <char *>(MemoryAlloc (128 ));
91
- NL_TEST_ASSERT (inSuite, pa != nullptr );
97
+ EXPECT_NE (pa, nullptr );
92
98
93
99
char * pb = static_cast <char *>(MemoryRealloc (pa, 256 ));
94
- NL_TEST_ASSERT (inSuite, pb != nullptr );
100
+ EXPECT_NE (pb, nullptr );
95
101
96
102
chip::Platform::MemoryFree (pb);
97
103
}
98
104
99
- static void TestMemAlloc_UniquePtr (nlTestSuite * inSuite, void * inContext )
105
+ TEST_F (TestCHIPMem, TestMemAlloc_UniquePtr )
100
106
{
101
107
// UniquePtr is a wrapper of std::unique_ptr for platform allocators, we just check if we created a correct wrapper here.
102
108
int constructorCalled = 0 ;
@@ -114,15 +120,15 @@ static void TestMemAlloc_UniquePtr(nlTestSuite * inSuite, void * inContext)
114
120
115
121
{
116
122
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 );
119
125
IgnoreUnusedVariable (ptr);
120
126
}
121
127
122
- NL_TEST_ASSERT (inSuite, destructorCalled == 1 );
128
+ EXPECT_TRUE ( destructorCalled);
123
129
}
124
130
125
- static void TestMemAlloc_SharedPtr (nlTestSuite * inSuite, void * inContext )
131
+ TEST_F (TestCHIPMem, TestMemAlloc_SharedPtr )
126
132
{
127
133
// SharedPtr is a wrapper of std::shared_ptr for platform allocators.
128
134
int instanceConstructorCalled = 0 ;
@@ -145,62 +151,21 @@ static void TestMemAlloc_SharedPtr(nlTestSuite * inSuite, void * inContext)
145
151
SharedPtr<Cls> otherReference;
146
152
{
147
153
auto ptr = MakeShared<Cls>(&instanceConstructorCalled, &instanceDestructorCalled);
148
- NL_TEST_ASSERT (inSuite, instanceConstructorCalled == 1 );
154
+ EXPECT_EQ ( instanceConstructorCalled, 1 );
149
155
// Capture a shared reference so we aren't destructed when we leave this scope.
150
156
otherReference = ptr;
151
157
}
152
158
153
159
// Verify that by sharing to otherReference, we weren't destructed.
154
- NL_TEST_ASSERT (inSuite, instanceDestructorCalled == 0 );
160
+ EXPECT_EQ ( instanceDestructorCalled, 0 );
155
161
156
162
// Now drop our reference.
157
163
otherReference = MakeShared<Cls>(&otherInstanceConstructorCalled, &otherInstanceDestructorCalled);
158
164
159
165
// Verify that the new instance was constructed and the first instance was
160
166
// 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 );
186
171
}
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