3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2016-2017 Nest Labs, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * This file implements a test for CHIP core library reference counted object.
30 #include <lib/core/ReferenceCounted.h>
31 #include <support/UnitTestRegistration.h>
33 #include <nlunit-test.h>
37 class TestClass : public ReferenceCounted<TestClass>
41 static void TestRetainRelease(nlTestSuite * inSuite, void * inContext)
43 TestClass * testObj = chip::Platform::New<TestClass>();
44 NL_TEST_ASSERT(inSuite, testObj->GetReferenceCount() == 1);
46 NL_TEST_ASSERT(inSuite, testObj->GetReferenceCount() == 2);
48 NL_TEST_ASSERT(inSuite, testObj->GetReferenceCount() == 1);
52 class TestClassNonHeap;
56 static void Release(TestClassNonHeap * obj);
59 class TestClassNonHeap : public ReferenceCounted<TestClassNonHeap, Deletor>
65 void Deletor::Release(TestClassNonHeap * obj)
70 static void TestRetainReleaseNonHeap(nlTestSuite * inSuite, void * inContext)
72 TestClassNonHeap testObj;
73 testObj.deleted = false;
74 NL_TEST_ASSERT(inSuite, testObj.GetReferenceCount() == 1);
75 NL_TEST_ASSERT(inSuite, testObj.deleted == false);
77 NL_TEST_ASSERT(inSuite, testObj.GetReferenceCount() == 2);
78 NL_TEST_ASSERT(inSuite, testObj.deleted == false);
80 NL_TEST_ASSERT(inSuite, testObj.GetReferenceCount() == 1);
81 NL_TEST_ASSERT(inSuite, testObj.deleted == false);
83 NL_TEST_ASSERT(inSuite, testObj.GetReferenceCount() == 0);
84 NL_TEST_ASSERT(inSuite, testObj.deleted == true);
88 * Test Suite. It lists all the test functions.
92 static const nlTest sTests[] =
94 NL_TEST_DEF("ReferenceCountedRetain", TestRetainRelease),
95 NL_TEST_DEF("ReferenceCountedRetainNonHeap", TestRetainReleaseNonHeap),
101 int TestReferenceCounted_Setup(void * inContext)
103 CHIP_ERROR error = chip::Platform::MemoryInit();
104 if (error != CHIP_NO_ERROR)
109 int TestReferenceCounted_Teardown(void * inContext)
111 chip::Platform::MemoryShutdown();
115 int TestReferenceCounted(void)
118 nlTestSuite theSuite =
122 TestReferenceCounted_Setup,
123 TestReferenceCounted_Teardown
127 nlTestRunner(&theSuite, nullptr);
129 return (nlTestRunnerStats(&theSuite));
132 CHIP_REGISTER_TEST_SUITE(TestReferenceCounted)