Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestCHIPMem.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *      This file implements a unit test suite for CHIP Memory Management
22  *      code functionality.
23  *
24  */
25
26 #include <inttypes.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <nlunit-test.h>
33 #include <support/CHIPMem.h>
34 #include <support/CodeUtils.h>
35 #include <support/UnitTestRegistration.h>
36
37 using namespace chip;
38 using namespace chip::Logging;
39 using namespace chip::Platform;
40
41 // =================================
42 //      Unit tests
43 // =================================
44
45 static void TestMemAlloc_Malloc(nlTestSuite * inSuite, void * inContext)
46 {
47     char * p1 = nullptr;
48     char * p2 = nullptr;
49     char * p3 = nullptr;
50
51     // Verify long-term allocation
52     p1 = static_cast<char *>(MemoryAlloc(64));
53     NL_TEST_ASSERT(inSuite, p1 != nullptr);
54
55     p2 = static_cast<char *>(MemoryAlloc(256));
56     NL_TEST_ASSERT(inSuite, p2 != nullptr);
57
58     chip::Platform::MemoryFree(p1);
59     chip::Platform::MemoryFree(p2);
60
61     // Verify short-term allocation
62     p1 = static_cast<char *>(MemoryAlloc(256));
63     NL_TEST_ASSERT(inSuite, p1 != nullptr);
64
65     p2 = static_cast<char *>(MemoryAlloc(256));
66     NL_TEST_ASSERT(inSuite, p2 != nullptr);
67
68     p3 = static_cast<char *>(MemoryAlloc(256));
69     NL_TEST_ASSERT(inSuite, p3 != nullptr);
70
71     chip::Platform::MemoryFree(p1);
72     chip::Platform::MemoryFree(p2);
73     chip::Platform::MemoryFree(p3);
74 }
75
76 static void TestMemAlloc_Calloc(nlTestSuite * inSuite, void * inContext)
77 {
78     char * p = static_cast<char *>(MemoryCalloc(128, true));
79     NL_TEST_ASSERT(inSuite, p != nullptr);
80
81     for (int i = 0; i < 128; i++)
82         NL_TEST_ASSERT(inSuite, p[i] == 0);
83
84     chip::Platform::MemoryFree(p);
85 }
86
87 static void TestMemAlloc_Realloc(nlTestSuite * inSuite, void * inContext)
88 {
89     char * pa = static_cast<char *>(MemoryAlloc(128));
90     NL_TEST_ASSERT(inSuite, pa != nullptr);
91
92     char * pb = static_cast<char *>(MemoryRealloc(pa, 256));
93     NL_TEST_ASSERT(inSuite, pb != nullptr);
94
95     chip::Platform::MemoryFree(pb);
96 }
97
98 /**
99  *   Test Suite. It lists all the test functions.
100  */
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() };
104
105 /**
106  *  Set up the test suite.
107  */
108 int TestMemAlloc_Setup(void * inContext)
109 {
110     CHIP_ERROR error = MemoryInit();
111     if (error != CHIP_NO_ERROR)
112         return (FAILURE);
113     return (SUCCESS);
114 }
115
116 /**
117  *  Tear down the test suite.
118  */
119 int TestMemAlloc_Teardown(void * inContext)
120 {
121     MemoryShutdown();
122     return (SUCCESS);
123 }
124
125 int TestMemAlloc()
126 {
127     nlTestSuite theSuite = { "CHIP Memory Allocation tests", &sTests[0], TestMemAlloc_Setup, TestMemAlloc_Teardown };
128
129     // Run test suit againt one context.
130     nlTestRunner(&theSuite, nullptr);
131     return nlTestRunnerStats(&theSuite);
132 }
133
134 CHIP_REGISTER_TEST_SUITE(TestMemAlloc)