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

Extend GeoMaterial Test

parent df04ac83
No related branches found
No related tags found
1 merge request!435Extend GeoMaterial Test
Pipeline #11908587 passed
...@@ -6,14 +6,14 @@ ...@@ -6,14 +6,14 @@
* @class RCBase * @class RCBase
* *
* @brief This is a base class for objects whose memory is managed * @brief This is a base class for objects whose memory is managed
* through reference counting. Reference-counted objects * through reference counting. Reference-counted objects
* should only be created using * should only be created using
* operator new, they should not be created on the stack. * operator new, they should not be created on the stack.
* *
* The methods ref() and unref() can be called to increase * The methods ref() and unref() can be called to increase
* and decrease the reference count of an object. When * and decrease the reference count of an object. When
* the reference count decreases to zero, the object deletes * the reference count decreases to zero, the object deletes
* itself * itself
*/ */
#ifndef GEOMODELKERNEL_RCBASE_H #ifndef GEOMODELKERNEL_RCBASE_H
...@@ -25,20 +25,20 @@ class RCBase { ...@@ -25,20 +25,20 @@ class RCBase {
public: public:
RCBase() = default; RCBase() = default;
// Increase the reference count // Increase the reference count
void ref() const noexcept { void ref() const noexcept {
++m_count; ++m_count;
} }
// Decreases the reference count. When the reference count // Decreases the reference count. When the reference count
// falls to zero, the object deletes itself. // falls to zero, the object deletes itself.
void unref () const noexcept{ void unref () const noexcept{
if (--m_count == 0) { if (--m_count == 0) {
delete this; delete this;
} }
} }
// Return the reference count. // Return the reference count.
unsigned int refCount () const noexcept { unsigned int refCount () const noexcept {
return m_count.load(); return m_count.load();
} }
...@@ -51,7 +51,7 @@ class RCBase { ...@@ -51,7 +51,7 @@ class RCBase {
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,18 +4,68 @@ ...@@ -4,18 +4,68 @@
#include "GeoModelKernel/GeoMaterial.h" #include "GeoModelKernel/GeoMaterial.h"
#include "GeoModelKernel/GeoIntrusivePtr.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <stdexcept>
#define GTEST_COUT std::cerr << "[ MESSAGE ] "
using namespace GeoModelKernelUnits; using namespace GeoModelKernelUnits;
class SampleMaterial{
private:
const std::string m_name{"Rhodium"};
const double m_dense{13.0*gram/centimeter3};
GeoIntrusivePtr<GeoMaterial> m_ptr{};
public:
GeoMaterial * ptr() const {return m_ptr.get();}
SampleMaterial():m_ptr(make_intrusive<GeoMaterial>(m_name, m_dense)){
//
};
};
TEST(GeoMaterial, CanBeConstructedOnHeap) { TEST(GeoMaterial, CanBeConstructedOnHeap) {
GeoMaterial * pG{}; GeoIntrusivePtr<GeoMaterial> pG{};
const std::string name{"Shaun"}; const std::string name{"Shaun"};
const double dense{13.0*gram/centimeter3}; const double dense{13.0*gram/centimeter3};
EXPECT_NO_THROW(pG = new GeoMaterial(name, dense));//Shaun is dense EXPECT_NO_THROW(pG = make_intrusive<GeoMaterial>(name, dense));
pG->ref(); }
pG->unref(); //should be destroyed here
TEST(GeoMaterial, CannotLockAMaterialWithNoElements) {
SampleMaterial s;
auto * pMat = s.ptr();
EXPECT_THROW(pMat->lock(), std::runtime_error);
}
TEST(GeoMaterial, CannotAccessUnlockedMaterialProperties) {
SampleMaterial s;
auto * pMat = s.ptr();
EXPECT_THROW(pMat->getNumElements(), std::runtime_error);
EXPECT_THROW(pMat->getDeDxConstant(), std::runtime_error);
EXPECT_THROW(pMat->getDeDxI0(),std::runtime_error);
EXPECT_THROW(pMat->getDeDxMin(),std::runtime_error);
EXPECT_THROW(pMat->getRadLength(),std::runtime_error);
EXPECT_THROW(pMat->getIntLength(),std::runtime_error);
EXPECT_THROW(pMat->getNumElements(),std::runtime_error);
EXPECT_THROW(pMat->getElement(0),std::runtime_error);
EXPECT_THROW(pMat->getFraction(0),std::runtime_error);
} }
TEST(GeoMaterial, CanAccessTrivialUnlockedMaterialProperties) {
SampleMaterial s;
const double expectedDensity{13.0*gram/centimeter3};
auto * pMat = s.ptr();
EXPECT_EQ(pMat->getName(), "Rhodium");
EXPECT_EQ(pMat->getDensity(), expectedDensity);
int id{};
//id seems to vary run to run
EXPECT_NO_THROW(id = pMat->getID());
GTEST_COUT <<"id is "<< id << std::endl;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment