Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alxbilger authored and fredroy committed Jan 8, 2025
1 parent 18a16e5 commit a80189e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Sofa/framework/Core/src/sofa/core/IntrusiveObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class SOFA_CORE_API IntrusiveObject
{
p->release();
}

protected:
virtual ~IntrusiveObject() = default;
};

}
73 changes: 73 additions & 0 deletions Sofa/framework/Core/test/IntrusiveObject_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#include <sofa/core/IntrusiveObject.h>
#include <gtest/gtest.h>
#include <sofa/core/sptr.h>


class DummyIntrusiveObject : public sofa::core::IntrusiveObject
{
public:
DummyIntrusiveObject() = default;
explicit DummyIntrusiveObject(const std::function<void()>& _destructorCallback)
: destructorCallback(_destructorCallback) {}

~DummyIntrusiveObject() override
{
destructorCallback();
}

private:
std::function<void()> destructorCallback;
};



TEST(IntrusiveObject, IsDestructorCalled)
{
std::size_t nbTimesDestructorCalled = 0;
{
sofa::core::sptr<DummyIntrusiveObject> dummy(new DummyIntrusiveObject([&nbTimesDestructorCalled]()
{
nbTimesDestructorCalled++;
}));
}
EXPECT_EQ(1, nbTimesDestructorCalled);
}


TEST(IntrusiveObject, Copy)
{
std::size_t nbTimesDestructorCalled = 0;
{
sofa::core::sptr<DummyIntrusiveObject> dummy0;
{
sofa::core::sptr<DummyIntrusiveObject> dummy(new DummyIntrusiveObject([&nbTimesDestructorCalled]()
{
nbTimesDestructorCalled++;
}));

dummy0 = dummy;
}
}
EXPECT_EQ(1, nbTimesDestructorCalled);
}

0 comments on commit a80189e

Please sign in to comment.