Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkPhotoacousticMotionCorrectionFilterTest.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 #include <mitkTestFixture.h>
14 #include <mitkTestingMacros.h>
15 
17 
18 class mitkPhotoacousticMotionCorrectionFilterTestSuite : public mitk::TestFixture
19 {
20  CPPUNIT_TEST_SUITE(mitkPhotoacousticMotionCorrectionFilterTestSuite);
21  MITK_TEST(testSettingFirstInput);
22  MITK_TEST(testSettingSecondInput);
23  // MITK_TEST(testNoThirdInput);
24  MITK_TEST(testGetFirstEmptyOutput);
25  MITK_TEST(testGetSecondEmptyOutput);
26  MITK_TEST(testSameInOutDimensions);
27  MITK_TEST(testNo3DError);
28  MITK_TEST(testSameInputDimension1);
29  MITK_TEST(testSameInputDimension2);
30  MITK_TEST(testNullPtr1);
31  MITK_TEST(testNullPtr2);
32  MITK_TEST(testNullPtr3);
33  MITK_TEST(testSameInputDimensions);
34  MITK_TEST(testStaticSliceCorrection);
35  CPPUNIT_TEST_SUITE_END();
36 
37 
38 private:
40  mitk::Image::Pointer image, image4d, image2d, image3d1slice;
41  float * data2d;
42 
43 public:
44  void setUp() override {
45  // get the filter I need
47  // get a 3d mitk image
48  image = mitk::Image::New();
49  image2d = mitk::Image::New();
50  image4d = mitk::Image::New();
51  image3d1slice = mitk::Image::New();
52  unsigned int * dimensions = new unsigned int[3] {2, 2, 2};
53  unsigned int * dimensions2 = new unsigned int[3] {2, 2, 1};
54  unsigned int * dim2 = new unsigned int[4] {2, 2, 2, 2};
55  mitk::PixelType pt = mitk::MakeScalarPixelType<float>();
56  float * data = new float[8] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
57  float * data4d = new float[16];
58  data2d = new float[4] {500.0, -500.5, 1000.0, 0.5};
59  image->Initialize(pt, 3, dimensions);
60  image->SetVolume(data);
61  image4d->Initialize(pt, 4, dim2);
62  image4d->SetVolume(data4d);
63  image2d->Initialize(pt, 2, dimensions);
64  image2d->SetVolume(data2d);
65  image3d1slice->Initialize(pt, 3, dimensions2);
66  image3d1slice->SetVolume(data);
67  delete[] dimensions;
68  delete[] dimensions2;
69  delete[] data;
70  delete[] data4d;
71  delete[] dim2;
72  }
73 
74  void tearDown() override {
75  delete[] data2d;
76  }
77 
78  void testSettingFirstInput() {
79  filter->SetInput(0, image);
80  mitk::Image::Pointer out = filter->GetInput(0);
81  CPPUNIT_ASSERT_EQUAL(image, out);
82  }
83 
84  void testSettingSecondInput() {
85  filter->SetInput(1, image);
86  mitk::Image::Pointer out = filter->GetInput(1);
87  CPPUNIT_ASSERT_EQUAL(image, out);
88  }
89 
90  // void testNoThirdInput() {
91  // CPPUNIT_ASSERT_NO_THROW(filter->SetInput(2, image));
92  // // CPPUNIT_ASSERT(true);
93  // }
94 
95  void testGetFirstEmptyOutput() {
96  mitk::Image::Pointer out = filter->GetOutput(0);
97  unsigned int dim = 0;
98  CPPUNIT_ASSERT_EQUAL(dim, out->GetDimension());
99  }
100 
101  void testGetSecondEmptyOutput() {
102  mitk::Image::Pointer out = filter->GetOutput(1);
103  unsigned int dim = 0;
104  CPPUNIT_ASSERT_EQUAL(dim, out->GetDimension());
105  }
106 
107  void testSameInOutDimensions() {
108  filter->SetInput(0, image);
109  filter->SetInput(1, image);
110  filter->Update();
111  mitk::Image::Pointer out = filter->GetOutput(0);
112  CPPUNIT_ASSERT_EQUAL(image->GetDimension(), out->GetDimension());
113  out = filter->GetOutput(1);
114  CPPUNIT_ASSERT_EQUAL(image->GetDimension(), out->GetDimension());
115  }
116 
117  void testNo3DError() {
118  filter->SetInput(0, image4d);
119  filter->SetInput(1, image4d);
120  CPPUNIT_ASSERT_THROW(filter->Update(), std::invalid_argument);
121  }
122 
123  // I only test for dim input0 <= dim input1, because otherwise
124  // itk will throw an error before the program even reaches the
125  // parts I wrote.
126  // Same for testSameInputDimension2
127  void testSameInputDimension1() {
128  filter->SetInput(0, image);
129  filter->SetInput(1, image4d);
130  CPPUNIT_ASSERT_THROW(filter->Update(), std::invalid_argument);
131  }
132 
133  // See previous comment
134  void testSameInputDimension2() {
135  filter->SetInput(0, image2d);
136  filter->SetInput(1, image);
137  CPPUNIT_ASSERT_THROW(filter->Update(), std::invalid_argument);
138  }
139 
140  // I tried to catch the error myself in the filter, but itk does some magic beforehand
141  void testNullPtr1() {
142  filter->SetInput(0, nullptr);
143  filter->SetInput(1, image);
144  CPPUNIT_ASSERT_THROW(filter->Update(), itk::ExceptionObject);
145  }
146 
147  // Now I am allowed to catch it myself, because the first input is fine -.-
148  void testNullPtr2() {
149  filter->SetInput(0, image);
150  filter->SetInput(1, nullptr);
151  CPPUNIT_ASSERT_THROW(filter->Update(), std::invalid_argument);
152  }
153 
154  void testNullPtr3() {
155  filter->SetInput(0, nullptr);
156  filter->SetInput(1, nullptr);
157  CPPUNIT_ASSERT_THROW(filter->Update(), itk::ExceptionObject);
158  }
159 
160  void testSameInputDimensions() {
161  filter->SetInput(0, image3d1slice);
162  filter->SetInput(1, image);
163  CPPUNIT_ASSERT_THROW(filter->Update(), std::invalid_argument);
164  }
165 
166  void testStaticSliceCorrection() {
167  image->SetSlice(data2d, 0);
168  image->SetSlice(data2d, 1);
169  filter->SetInput(0, image);
170  filter->SetInput(1, image);
171  filter->Update();
172  mitk::Image::Pointer out1 = filter->GetOutput(0);
173  mitk::Image::Pointer out2 = filter->GetOutput(1);
174  MITK_ASSERT_EQUAL(image, out1, "Check that static image does not get changed.");
175  }
176 
177 };
178 
179 MITK_TEST_SUITE_REGISTRATION(mitkPhotoacousticMotionCorrectionFilter)
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Test fixture for parameterized tests.
#define MITK_ASSERT_EQUAL(EXPECTED, ACTUAL, MSG)
Testing macro to test if two objects are equal.
mitk::Image::Pointer image
static Pointer New()
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51