Skip to content
Snippets Groups Projects
Commit dd1edb99 authored by Shaun Roe's avatar Shaun Roe Committed by Johannes Junggeburth
Browse files

Refine and document RCBaseTests

parent 4be29757
No related branches found
No related tags found
1 merge request!427Refine and document RCBaseTests
Pipeline #11783400 passed
...@@ -49,6 +49,7 @@ class RCBase { ...@@ -49,6 +49,7 @@ class RCBase {
private: private:
RCBase(const RCBase &right) = delete; RCBase(const RCBase &right) = delete;
RCBase & operator=(const RCBase &right) = delete; RCBase & operator=(const RCBase &right) = delete;
// The reference count // The reference count
mutable std::atomic<unsigned> m_count{0}; mutable std::atomic<unsigned> m_count{0};
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "GeoModelKernel/RCBase.h" #include "GeoModelKernel/RCBase.h"
#include <exception>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <limits> #include <limits>
...@@ -13,7 +14,10 @@ class Stub:public RCBase{ ...@@ -13,7 +14,10 @@ class Stub:public RCBase{
virtual ~Stub() = default; virtual ~Stub() = default;
}; };
TEST(GeoModelKernel, RCBaseStub_CanBeConstructedOnStack) { //Making the destructor of RCBase private would cause all the defaulted destructors
//of derived classes (e.g the 'Stub' above, and many others) no longer compile.
//However, keeping it protected results in this undesirable behaviour:
TEST(RCBase, StubCanBeConstructedOnStack) {
EXPECT_NO_THROW([[maybe_unused]] Stub stub); EXPECT_NO_THROW([[maybe_unused]] Stub stub);
//Stub stub1; (unused here if the following are commented out) //Stub stub1; (unused here if the following are commented out)
//none of the following compile (correctly so) //none of the following compile (correctly so)
...@@ -23,14 +27,14 @@ TEST(GeoModelKernel, RCBaseStub_CanBeConstructedOnStack) { ...@@ -23,14 +27,14 @@ TEST(GeoModelKernel, RCBaseStub_CanBeConstructedOnStack) {
//Stub stub5 = std::move(stub3); //Stub stub5 = std::move(stub3);
} }
TEST(GeoModelKernel, RCBase_CanBeConstructedOnHeap) { TEST(RCBase, CanBeConstructedOnHeap) {
RCBase * p{}; RCBase * p{};
EXPECT_NO_THROW(p = new RCBase); EXPECT_NO_THROW(p = new RCBase);
EXPECT_NE(p, nullptr); EXPECT_NE(p, nullptr);
//deliberate leak here, RCBase d'tor is protected, so cannot delete //deliberate leak here, RCBase d'tor is protected, so cannot delete
} }
TEST(GeoModelKernel, RCBaseStub_CanBeConstructedOnHeap) { TEST(RCBase, StubCanBeConstructedOnHeap) {
Stub * p{}; Stub * p{};
EXPECT_NO_THROW(p = new Stub); EXPECT_NO_THROW(p = new Stub);
EXPECT_NE(p, nullptr); EXPECT_NE(p, nullptr);
...@@ -38,20 +42,29 @@ TEST(GeoModelKernel, RCBaseStub_CanBeConstructedOnHeap) { ...@@ -38,20 +42,29 @@ TEST(GeoModelKernel, RCBaseStub_CanBeConstructedOnHeap) {
EXPECT_NO_THROW(delete p); EXPECT_NO_THROW(delete p);
} }
TEST(GeoModelKernel, RCBaseStub_ReferenceCountOk){ TEST(RCBase, CanIncrementAndDecrementReference){
//do on heap, as is supposed to be //do on heap, as is supposed to be
Stub * pStub = new Stub; Stub * pStub = new Stub;
EXPECT_EQ(pStub->refCount(), 0); EXPECT_EQ(pStub->refCount(), 0);
EXPECT_NO_THROW(pStub->ref()); EXPECT_NO_THROW(pStub->ref());
EXPECT_EQ(pStub->refCount(), 1); EXPECT_EQ(pStub->refCount(), 1);
EXPECT_NO_THROW(pStub->unref()); EXPECT_NO_THROW(pStub->unref());
//now, where is the object? The pointer has not been set to zero.. so... }
//the following line would execute (not crash) and return a nonsense value, //undesirable behaviours
//EXPECT_EQ(pStub->refCount(), 0); //returns 2043 on my machine TEST(RCBase, DISABLED_CannotDecrementReferenceBelowZero){
//segfault : delete pStub; Stub * pStub = new Stub;
Stub * pStub2 = new Stub; pStub->unref(); //should this throw?
EXPECT_NO_THROW(pStub2->unref()); EXPECT_EQ(pStub->refCount(), 0);//fails
EXPECT_EQ(pStub2->refCount(), std::numeric_limits<unsigned int>::max());
//now it's cursed, so euthanise it //now it's cursed, so euthanise it
delete pStub2; delete pStub;
}
TEST(RCBase, DISABLED_CannotBeUsedAfterRefCountBecomesZero){
Stub * pStub = new Stub;
EXPECT_NO_THROW(pStub->ref());
EXPECT_NO_THROW(pStub->unref());//object destroyed, but pointer still valid
EXPECT_THROW(pStub->ref(), std::exception);//compiles, doesn't throw
//now the refcount is some random value (returns 2043 on my machine)
//deleting would cause segfault
//delete pStub;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment