3 * Copyright (c) 2020 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file implements a unit test suite for CHIP Memory Management
32 #include <nlunit-test.h>
33 #include <support/CHIPMem.h>
34 #include <support/CodeUtils.h>
35 #include <support/UnitTestRegistration.h>
38 using namespace chip::Logging;
39 using namespace chip::Platform;
41 // =================================
43 // =================================
45 static void TestMemAlloc_Malloc(nlTestSuite * inSuite, void * inContext)
51 // Verify long-term allocation
52 p1 = static_cast<char *>(MemoryAlloc(64));
53 NL_TEST_ASSERT(inSuite, p1 != nullptr);
55 p2 = static_cast<char *>(MemoryAlloc(256));
56 NL_TEST_ASSERT(inSuite, p2 != nullptr);
58 chip::Platform::MemoryFree(p1);
59 chip::Platform::MemoryFree(p2);
61 // Verify short-term allocation
62 p1 = static_cast<char *>(MemoryAlloc(256));
63 NL_TEST_ASSERT(inSuite, p1 != nullptr);
65 p2 = static_cast<char *>(MemoryAlloc(256));
66 NL_TEST_ASSERT(inSuite, p2 != nullptr);
68 p3 = static_cast<char *>(MemoryAlloc(256));
69 NL_TEST_ASSERT(inSuite, p3 != nullptr);
71 chip::Platform::MemoryFree(p1);
72 chip::Platform::MemoryFree(p2);
73 chip::Platform::MemoryFree(p3);
76 static void TestMemAlloc_Calloc(nlTestSuite * inSuite, void * inContext)
78 char * p = static_cast<char *>(MemoryCalloc(128, true));
79 NL_TEST_ASSERT(inSuite, p != nullptr);
81 for (int i = 0; i < 128; i++)
82 NL_TEST_ASSERT(inSuite, p[i] == 0);
84 chip::Platform::MemoryFree(p);
87 static void TestMemAlloc_Realloc(nlTestSuite * inSuite, void * inContext)
89 char * pa = static_cast<char *>(MemoryAlloc(128));
90 NL_TEST_ASSERT(inSuite, pa != nullptr);
92 char * pb = static_cast<char *>(MemoryRealloc(pa, 256));
93 NL_TEST_ASSERT(inSuite, pb != nullptr);
95 chip::Platform::MemoryFree(pb);
99 * Test Suite. It lists all the test functions.
101 static const nlTest sTests[] = { NL_TEST_DEF("Test MemAlloc::Malloc", TestMemAlloc_Malloc),
102 NL_TEST_DEF("Test MemAlloc::Calloc", TestMemAlloc_Calloc),
103 NL_TEST_DEF("Test MemAlloc::Realloc", TestMemAlloc_Realloc), NL_TEST_SENTINEL() };
106 * Set up the test suite.
108 int TestMemAlloc_Setup(void * inContext)
110 CHIP_ERROR error = MemoryInit();
111 if (error != CHIP_NO_ERROR)
117 * Tear down the test suite.
119 int TestMemAlloc_Teardown(void * inContext)
127 nlTestSuite theSuite = { "CHIP Memory Allocation tests", &sTests[0], TestMemAlloc_Setup, TestMemAlloc_Teardown };
129 // Run test suit againt one context.
130 nlTestRunner(&theSuite, nullptr);
131 return nlTestRunnerStats(&theSuite);
134 CHIP_REGISTER_TEST_SUITE(TestMemAlloc)