Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkWeakPointerTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 // Testing
14 #include "mitkTestFixture.h"
15 #include "mitkTestingMacros.h"
16 
17 // std includes
18 #include <string>
19 
20 // MITK includes
21 #include <mitkWeakPointer.h>
22 
23 // ITK includes
24 #include <itkObject.h>
25 
26 class mitkWeakPointerTestSuite : public mitk::TestFixture
27 {
28  CPPUNIT_TEST_SUITE(mitkWeakPointerTestSuite);
29 
30  MITK_TEST(EqualPointers_Success);
31  MITK_TEST(ExpiredWeakPointerWithSmartPointerAssignment_Success);
32  MITK_TEST(ExpiredWeakPointerWithWeakPointerAssignment_Success);
33  MITK_TEST(ExpiredWeakPointerWithSmartPointerConstructor_Success);
34  MITK_TEST(ExpiredWeakPointerWithWeakPointerConstructor_Success);
35  MITK_TEST(DeleteEventCall_Success);
36 
37  CPPUNIT_TEST_SUITE_END();
38 
39 private:
40  mitk::WeakPointer<itk::Object> m_WeakPointer;
41  mitk::WeakPointer<itk::Object> m_WeakPointer2;
42  itk::Object::Pointer m_SmartPointer;
43 
44 public:
45  void setUp() override
46  {
47  m_SmartPointer = itk::Object::New();
48  m_WeakPointer = m_SmartPointer;
49  m_WeakPointer2 = m_WeakPointer;
50  }
51 
52  void tearDown() override
53  {
54  m_SmartPointer = nullptr;
55  m_WeakPointer = nullptr;
56  m_WeakPointer2 = nullptr;
57  }
58 
59  void EqualPointers_Success()
60  {
61  itk::Object::Pointer tmpSmartPointer(m_WeakPointer.Lock());
62  itk::Object::Pointer tmpSmartPointer2(m_WeakPointer2.Lock());
63  CPPUNIT_ASSERT_MESSAGE("Testing equal pointers", tmpSmartPointer.GetPointer() == tmpSmartPointer2.GetPointer());
64  }
65 
66  void ReferenceCountOfPointers_Success()
67  {
68  CPPUNIT_ASSERT_MESSAGE("Testing reference count", 1 == m_SmartPointer->GetReferenceCount());
69  }
70 
71  void ExpiredWeakPointerWithSmartPointerAssignment_Success()
72  {
73  m_SmartPointer = nullptr;
74  CPPUNIT_ASSERT_MESSAGE("Testing expired weak pointer (smart pointer assignment)", m_WeakPointer.IsExpired());
75  }
76 
77  void ExpiredWeakPointerWithWeakPointerAssignment_Success()
78  {
79  m_SmartPointer = nullptr;
80  CPPUNIT_ASSERT_MESSAGE("Testing expired weak pointer (weak pointer assignment)", m_WeakPointer2.IsExpired());
81  }
82 
83  void ExpiredWeakPointerWithSmartPointerConstructor_Success()
84  {
85  mitk::WeakPointer<itk::Object> weakPointer3(m_SmartPointer);
86  m_SmartPointer = nullptr;
87  CPPUNIT_ASSERT_MESSAGE("Testing expired weak pointer (smart pointer constructor)", weakPointer3.IsExpired());
88  }
89 
90  void ExpiredWeakPointerWithWeakPointerConstructor_Success()
91  {
92  mitk::WeakPointer<itk::Object> weakPointer4(m_WeakPointer);
93  m_WeakPointer = m_SmartPointer;
94  m_SmartPointer = nullptr;
95  CPPUNIT_ASSERT_MESSAGE("Testing expired weak pointer (copy constructor)", weakPointer4.IsExpired());
96  }
97 
98  void DeleteEventCall_Success()
99  {
100  int deleteEventCallbackCalled = 0;
101  m_WeakPointer.SetDeleteEventCallback([&deleteEventCallbackCalled]() { ++deleteEventCallbackCalled; });
102  m_WeakPointer = m_SmartPointer;
103  m_SmartPointer = nullptr;
104  CPPUNIT_ASSERT_MESSAGE("Testing call of delete event callback", 1 == deleteEventCallbackCalled);
105  }
106 };
107 MITK_TEST_SUITE_REGISTRATION(mitkWeakPointer)
itk::SmartPointer< T > Lock() const
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
bool IsExpired() const noexcept
Test fixture for parameterized tests.
void SetDeleteEventCallback(const DeleteEventCallbackType &callback)