Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestScopedBuffer.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 #include <support/ScopedBuffer.h>
20 #include <support/UnitTestRegistration.h>
21
22 #include <nlunit-test.h>
23
24 namespace {
25
26 class TestCounterMemoryManagement
27 {
28 public:
29     static int Counter() { return mAllocCount; }
30
31     static void MemoryFree(void * p)
32     {
33         mAllocCount--;
34         chip::Platform::MemoryFree(p);
35     }
36     static void * MemoryAlloc(size_t size)
37     {
38         mAllocCount++;
39         return chip::Platform::MemoryAlloc(size);
40     }
41     static void * MemoryCalloc(size_t num, size_t size)
42     {
43
44         mAllocCount++;
45         return chip::Platform::MemoryCalloc(num, size);
46     }
47
48 private:
49     static int mAllocCount;
50 };
51 int TestCounterMemoryManagement::mAllocCount = 0;
52
53 using TestCounterScopedBuffer = chip::Platform::ScopedMemoryBuffer<char, TestCounterMemoryManagement>;
54
55 void TestAutoFree(nlTestSuite * inSuite, void * inContext)
56 {
57     NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
58
59     {
60         TestCounterScopedBuffer buffer;
61
62         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
63         NL_TEST_ASSERT(inSuite, buffer.Alloc(128));
64         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 1);
65     }
66     NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
67 }
68
69 void TestFreeDuringAllocs(nlTestSuite * inSuite, void * inContext)
70 {
71     NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
72
73     {
74         TestCounterScopedBuffer buffer;
75
76         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
77         NL_TEST_ASSERT(inSuite, buffer.Alloc(128));
78         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 1);
79         NL_TEST_ASSERT(inSuite, buffer.Alloc(64));
80         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 1);
81         NL_TEST_ASSERT(inSuite, buffer.Calloc(10));
82         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 1);
83     }
84     NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
85 }
86
87 void TestRelease(nlTestSuite * inSuite, void * inContext)
88 {
89     NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
90     void * ptr = nullptr;
91
92     {
93         TestCounterScopedBuffer buffer;
94
95         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
96         NL_TEST_ASSERT(inSuite, buffer.Alloc(128));
97         NL_TEST_ASSERT(inSuite, buffer.Get() != nullptr);
98
99         ptr = buffer.Release();
100         NL_TEST_ASSERT(inSuite, ptr != nullptr);
101         NL_TEST_ASSERT(inSuite, buffer.Get() == nullptr);
102     }
103
104     NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 1);
105
106     {
107         TestCounterScopedBuffer buffer;
108         NL_TEST_ASSERT(inSuite, buffer.Alloc(128));
109         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 2);
110         TestCounterMemoryManagement::MemoryFree(ptr);
111         NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 1);
112     }
113
114     NL_TEST_ASSERT(inSuite, TestCounterMemoryManagement::Counter() == 0);
115 }
116
117 int Setup(void * inContext)
118 {
119     CHIP_ERROR error = chip::Platform::MemoryInit();
120     if (error != CHIP_NO_ERROR)
121         return FAILURE;
122     return SUCCESS;
123 }
124
125 int Teardown(void * inContext)
126 {
127     chip::Platform::MemoryShutdown();
128     return SUCCESS;
129 }
130
131 } // namespace
132
133 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
134 /**
135  *   Test Suite. It lists all the test functions.
136  */
137 static const nlTest sTests[] = {
138     NL_TEST_DEF_FN(TestAutoFree),         //
139     NL_TEST_DEF_FN(TestFreeDuringAllocs), //
140     NL_TEST_DEF_FN(TestRelease),          //
141     NL_TEST_SENTINEL()                    //
142 };
143
144 int TestScopedBuffer(void)
145 {
146     nlTestSuite theSuite = { "CHIP ScopedBuffer tests", &sTests[0], Setup, Teardown };
147
148     // Run test suit againt one context.
149     nlTestRunner(&theSuite, nullptr);
150     return nlTestRunnerStats(&theSuite);
151 }
152
153 CHIP_REGISTER_TEST_SUITE(TestScopedBuffer)