|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | + |
| 19 | + |
| 20 | +// First include the header file to be tested: |
| 21 | +#include "itkObjectFactoryBase.h" |
| 22 | + |
| 23 | +#include "itkLightObject.h" |
| 24 | +#include "itkSmartPointer.h" |
| 25 | +#include "itkVersion.h" |
| 26 | + |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | + |
| 30 | +namespace |
| 31 | +{ |
| 32 | + |
| 33 | +// The name of the test object type, as stored by the test object factory. |
| 34 | +constexpr const char * testObjectTypeName = "Test Object"; |
| 35 | + |
| 36 | +// A "dummy" object type, just for unit test purposes. |
| 37 | +class TestObject : public itk::LightObject |
| 38 | +{}; |
| 39 | + |
| 40 | + |
| 41 | +// A factory of test objects. |
| 42 | +class TestObjectFactory : public itk::ObjectFactoryBase |
| 43 | +{ |
| 44 | +public: |
| 45 | + ITK_DISALLOW_COPY_AND_MOVE(TestObjectFactory); |
| 46 | + |
| 47 | + using Self = TestObjectFactory; |
| 48 | + using Superclass = ObjectFactoryBase; |
| 49 | + using Pointer = itk::SmartPointer<Self>; |
| 50 | + using ConstPointer = itk::SmartPointer<const Self>; |
| 51 | + |
| 52 | + const char * |
| 53 | + GetITKSourceVersion() const override |
| 54 | + { |
| 55 | + return ITK_SOURCE_VERSION; |
| 56 | + } |
| 57 | + |
| 58 | + const char * |
| 59 | + GetDescription() const override |
| 60 | + { |
| 61 | + return "Test Object Factory"; |
| 62 | + } |
| 63 | + |
| 64 | + itkTypeMacro(TestObjectFactory, itk::ObjectFactoryBase); |
| 65 | + |
| 66 | + itkFactorylessNewMacro(Self); |
| 67 | + |
| 68 | +protected: |
| 69 | + TestObjectFactory() |
| 70 | + { |
| 71 | + this->RegisterOverride( |
| 72 | + testObjectTypeName, testObjectTypeName, testObjectTypeName, true, itk::CreateObjectFunction<TestObject>::New()); |
| 73 | + } |
| 74 | + ~TestObjectFactory() override = default; |
| 75 | +}; |
| 76 | + |
| 77 | + |
| 78 | +// Ensures that a TestObjectFactory is registered and unregistered, following the C++ RAII idiom (Resource Acquisition |
| 79 | +// Is Initialization). |
| 80 | +class TestObjectFactoryRegistration |
| 81 | +{ |
| 82 | +public: |
| 83 | + ITK_DISALLOW_COPY_AND_MOVE(TestObjectFactoryRegistration); |
| 84 | + |
| 85 | + TestObjectFactoryRegistration() { itk::ObjectFactoryBase::RegisterFactory(m_Factory); } |
| 86 | + ~TestObjectFactoryRegistration() { itk::ObjectFactoryBase::UnRegisterFactory(m_Factory); } |
| 87 | + |
| 88 | +private: |
| 89 | + TestObjectFactory::Pointer m_Factory{ TestObjectFactory::New() }; |
| 90 | +}; |
| 91 | +} // namespace |
| 92 | + |
| 93 | + |
| 94 | +// Tests that ObjectFactoryBase::CreateInstance creates an instance that has a reference count of 2. |
| 95 | +TEST(ObjectFactoryBase, CreatedInstanceHasReferenceCountOfTwo) |
| 96 | +{ |
| 97 | + const TestObjectFactoryRegistration registration{}; |
| 98 | + |
| 99 | + const auto instance = itk::ObjectFactoryBase::CreateInstance(testObjectTypeName); |
| 100 | + |
| 101 | + // The type specified by testObjectTypeName is registered, so the created instance may not be null. |
| 102 | + ASSERT_NE(instance, nullptr); |
| 103 | + |
| 104 | + // The main check: expect the reference count to be 2, not 1, and certainly not zero! |
| 105 | + EXPECT_EQ(instance->GetReferenceCount(), 2); |
| 106 | + |
| 107 | + // Avoid a memory leak. |
| 108 | + instance->UnRegister(); |
| 109 | +} |
0 commit comments