Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / core / tests / TestReferenceCounted.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *      This file implements a test for  CHIP core library reference counted object.
23  *
24  */
25
26 #include <inttypes.h>
27 #include <stdint.h>
28 #include <string.h>
29
30 #include <lib/core/ReferenceCounted.h>
31 #include <support/UnitTestRegistration.h>
32
33 #include <nlunit-test.h>
34
35 using namespace chip;
36
37 class TestClass : public ReferenceCounted<TestClass>
38 {
39 };
40
41 static void TestRetainRelease(nlTestSuite * inSuite, void * inContext)
42 {
43     TestClass * testObj = chip::Platform::New<TestClass>();
44     NL_TEST_ASSERT(inSuite, testObj->GetReferenceCount() == 1);
45     testObj->Retain();
46     NL_TEST_ASSERT(inSuite, testObj->GetReferenceCount() == 2);
47     testObj->Release();
48     NL_TEST_ASSERT(inSuite, testObj->GetReferenceCount() == 1);
49     testObj->Release();
50 }
51
52 class TestClassNonHeap;
53 class Deletor
54 {
55 public:
56     static void Release(TestClassNonHeap * obj);
57 };
58
59 class TestClassNonHeap : public ReferenceCounted<TestClassNonHeap, Deletor>
60 {
61 public:
62     bool deleted;
63 };
64
65 void Deletor::Release(TestClassNonHeap * obj)
66 {
67     obj->deleted = true;
68 }
69
70 static void TestRetainReleaseNonHeap(nlTestSuite * inSuite, void * inContext)
71 {
72     TestClassNonHeap testObj;
73     testObj.deleted = false;
74     NL_TEST_ASSERT(inSuite, testObj.GetReferenceCount() == 1);
75     NL_TEST_ASSERT(inSuite, testObj.deleted == false);
76     testObj.Retain();
77     NL_TEST_ASSERT(inSuite, testObj.GetReferenceCount() == 2);
78     NL_TEST_ASSERT(inSuite, testObj.deleted == false);
79     testObj.Release();
80     NL_TEST_ASSERT(inSuite, testObj.GetReferenceCount() == 1);
81     NL_TEST_ASSERT(inSuite, testObj.deleted == false);
82     testObj.Release();
83     NL_TEST_ASSERT(inSuite, testObj.GetReferenceCount() == 0);
84     NL_TEST_ASSERT(inSuite, testObj.deleted == true);
85 }
86
87 /**
88  *   Test Suite. It lists all the test functions.
89  */
90
91 // clang-format off
92 static const nlTest sTests[] =
93 {
94     NL_TEST_DEF("ReferenceCountedRetain", TestRetainRelease),
95     NL_TEST_DEF("ReferenceCountedRetainNonHeap", TestRetainReleaseNonHeap),
96
97     NL_TEST_SENTINEL()
98 };
99 // clang-format on
100
101 int TestReferenceCounted_Setup(void * inContext)
102 {
103     CHIP_ERROR error = chip::Platform::MemoryInit();
104     if (error != CHIP_NO_ERROR)
105         return FAILURE;
106     return SUCCESS;
107 }
108
109 int TestReferenceCounted_Teardown(void * inContext)
110 {
111     chip::Platform::MemoryShutdown();
112     return SUCCESS;
113 }
114
115 int TestReferenceCounted(void)
116 {
117     // clang-format off
118     nlTestSuite theSuite =
119     {
120         "Reference-Counted",
121         &sTests[0],
122         TestReferenceCounted_Setup,
123         TestReferenceCounted_Teardown
124     };
125     // clang-format on
126
127     nlTestRunner(&theSuite, nullptr);
128
129     return (nlTestRunnerStats(&theSuite));
130 }
131
132 CHIP_REGISTER_TEST_SUITE(TestReferenceCounted)