Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkDICOMTagPathTest.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 "mitkDICOMTagPath.h"
14 
15 #include "mitkTestFixture.h"
16 #include "mitkTestingMacros.h"
17 
18 #include <regex>
19 
20 class mitkDICOMTagPathTestSuite : public mitk::TestFixture
21 {
22  CPPUNIT_TEST_SUITE(mitkDICOMTagPathTestSuite);
23 
31  MITK_TEST(ExecutePropertyRegEx);
32 
33  CPPUNIT_TEST_SUITE_END();
34 
35 private:
36 
37  mitk::DICOMTagPath simplePath;
38  mitk::DICOMTagPath deepPath;
39  mitk::DICOMTagPath deepPath_withAnyElement;
40  mitk::DICOMTagPath deepPath_withAnySelection;
41  mitk::DICOMTagPath deepPath_withSelection;
42  mitk::DICOMTagPath verydeepPath;
43 
44  mitk::DICOMTagPath emptyPath;
45 
46 public:
47 
48  void setUp() override
49  {
50  simplePath.AddElement(0x0010, 0x0011);
51 
52  deepPath.AddElement(0x0010, 0x0011);
53  deepPath.AddElement(0x0020, 0x0022);
54  deepPath.AddElement(0x003A, 0x0033);
55 
56  deepPath_withAnyElement.AddElement(0x0010, 0x0011);
57  deepPath_withAnyElement.AddAnyElement();
58  deepPath_withAnyElement.AddElement(0x003a, 0x003f);
59 
60  deepPath_withAnySelection.AddElement(0x0010, 0x0011);
61  deepPath_withAnySelection.AddAnySelection(0x002B, 0x002E);
62  deepPath_withAnySelection.AddElement(0x0030, 0x0033);
63 
64  deepPath_withSelection.AddElement(0x0010, 0x0011);
65  deepPath_withSelection.AddSelection(0x0020, 0x0022, 6);
66  deepPath_withSelection.AddElement(0x003b, 0x003e);
67 
68  verydeepPath.AddAnySelection(0x0010, 0x0011);
69  verydeepPath.AddAnyElement();
70  verydeepPath.AddElement(0x0030, 0x0033);
71  verydeepPath.AddSelection(0x004c, 0x004d, 4);
72  verydeepPath.AddElement(0x0050, 0x0055);
73  }
74 
75  void tearDown() override
76  {
77  }
78 
80  {
81  std::string result = mitk::DICOMTagPathToPropertyRegEx(simplePath);
82  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyRegEx() with '(0010,0011)'", std::string("DICOM\\.0010\\.0011"), result);
83  result = mitk::DICOMTagPathToPropertyRegEx(deepPath);
84  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyRegEx() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("DICOM\\.0010\\.0011\\.0020\\.0022\\.(003a|003A)\\.0033"), result);
85  result = mitk::DICOMTagPathToPropertyRegEx(deepPath_withAnyElement);
86  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyRegEx() with '(0010,0011).*.(003a,003f)'", std::string("DICOM\\.0010\\.0011\\.([A-Fa-f\\d]{4})\\.([A-Fa-f\\d]{4})\\.(003a|003A)\\.(003f|003F)"), result);
87  result = mitk::DICOMTagPathToPropertyRegEx(deepPath_withAnySelection);
88  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyRegEx() with '(0010,0011).(002B,002E)[*].(0030,0033)'", std::string("DICOM\\.0010\\.0011\\.(002b|002B)\\.(002e|002E)\\.\\[(\\d*)\\]\\.0030\\.0033"), result);
89  result = mitk::DICOMTagPathToPropertyRegEx(deepPath_withSelection);
90  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyRegEx() with '(0010,0011).(0020,0022)[6].(003b,003e)'", std::string("DICOM\\.0010\\.0011\\.0020\\.0022\\.\\[6\\]\\.(003b|003B)\\.(003e|003E)"), result);
91  result = mitk::DICOMTagPathToPropertyRegEx(verydeepPath);
92  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyRegEx() with '(0010,0011)[*].*.(0030,0033).(004c,004d)[4].(0050,0055)'", std::string("DICOM\\.0010\\.0011\\.\\[(\\d*)\\]\\.([A-Fa-f\\d]{4})\\.([A-Fa-f\\d]{4})\\.0030\\.0033\\.(004c|004C)\\.(004d|004D)\\.\\[4\\]\\.0050\\.0055"), result);
93  }
94 
96  {
97  std::string result = mitk::DICOMTagPathToPersistenceKeyRegEx(simplePath);
98  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyRegEx() with '(0010,0011)'", std::string("DICOM_0010_0011"), result);
99  result = mitk::DICOMTagPathToPersistenceKeyRegEx(deepPath);
100  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyRegEx() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("DICOM_0010_0011_0020_0022_(003a|003A)_0033"), result);
101  result = mitk::DICOMTagPathToPersistenceKeyRegEx(deepPath_withAnyElement);
102  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyRegEx() with '(0010,0011).*.(003a,003f)'", std::string("DICOM_0010_0011_([A-Fa-f\\d]{4})_([A-Fa-f\\d]{4})_(003a|003A)_(003f|003F)"), result);
103  result = mitk::DICOMTagPathToPersistenceKeyRegEx(deepPath_withAnySelection);
104  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyRegEx() with '(0010,0011).(002B,002E)[*].(0030,0033)'", std::string("DICOM_0010_0011_(002b|002B)_(002e|002E)_\\[(\\d*)\\]_0030_0033"), result);
105  result = mitk::DICOMTagPathToPersistenceKeyRegEx(deepPath_withSelection);
106  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyRegEx() with '(0010,0011).(0020,0022)[6].(003b,003e)'", std::string("DICOM_0010_0011_0020_0022_\\[6\\]_(003b|003B)_(003e|003E)"), result);
107  result = mitk::DICOMTagPathToPersistenceKeyRegEx(verydeepPath);
108  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyRegEx() with '(0010,0011)[*].*.(0030,0033).(004c,004d)[4].(0050,0055)'", std::string("DICOM_0010_0011_\\[(\\d*)\\]_([A-Fa-f\\d]{4})_([A-Fa-f\\d]{4})_0030_0033_(004c|004C)_(004d|004D)_\\[4\\]_0050_0055"), result);
109  }
110 
112  {
113  std::string result = mitk::DICOMTagPathToPersistenceKeyTemplate(simplePath);
114  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011)'", std::string("DICOM_0010_0011"), result);
116  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("DICOM_0010_0011_0020_0022_003A_0033"), result);
117  result = mitk::DICOMTagPathToPersistenceKeyTemplate(deepPath_withAnyElement);
118  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011).*.(003a,003f)'", std::string("DICOM_0010_0011_$1_$2_003A_003F"), result);
119  result = mitk::DICOMTagPathToPersistenceKeyTemplate(deepPath_withAnySelection);
120  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011).(002B,002E)[*].(0030,0033)'", std::string("DICOM_0010_0011_002B_002E_[$1]_0030_0033"), result);
121  result = mitk::DICOMTagPathToPersistenceKeyTemplate(deepPath_withSelection);
122  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011).(0020,0022)[6].(003b,003e)'", std::string("DICOM_0010_0011_0020_0022_[6]_003B_003E"), result);
123  result = mitk::DICOMTagPathToPersistenceKeyTemplate(verydeepPath);
124  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011)[*].*.(0030,0033).(004c,004d)[4].(0050,0055)'", std::string("DICOM_0010_0011_[$1]_$2_$3_0030_0033_004C_004D_[4]_0050_0055"), result);
125  }
126 
128  {
129  std::string result = mitk::DICOMTagPathToPersistenceNameTemplate(simplePath);
130  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011)'", std::string("DICOM.0010.0011"), result);
132  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("DICOM.0010.0011.0020.0022.003A.0033"), result);
133  result = mitk::DICOMTagPathToPersistenceNameTemplate(deepPath_withAnyElement);
134  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011).*.(003a,003f)'", std::string("DICOM.0010.0011.$1.$2.003A.003F"), result);
135  result = mitk::DICOMTagPathToPersistenceNameTemplate(deepPath_withAnySelection);
136  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011).(002B,002E)[*].(0030,0033)'", std::string("DICOM.0010.0011.002B.002E.[$1].0030.0033"), result);
137  result = mitk::DICOMTagPathToPersistenceNameTemplate(deepPath_withSelection);
138  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011).(0020,0022)[6].(003b,003e)'", std::string("DICOM.0010.0011.0020.0022.[6].003B.003E"), result);
139  result = mitk::DICOMTagPathToPersistenceNameTemplate(verydeepPath);
140  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011)[*].*.(0030,0033).(004c,004d)[4].(0050,0055)'", std::string("DICOM.0010.0011.[$1].$2.$3.0030.0033.004C.004D.[4].0050.0055"), result);
141  }
142 
144  {
145  std::string result = mitk::DICOMTagPathToDCMTKSearchPath(simplePath);
146  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToDCMTKSearchPath() with '(0010,0011)'", std::string("(0010,0011)"), result);
147  result = mitk::DICOMTagPathToDCMTKSearchPath(deepPath);
148  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToDCMTKSearchPath() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("(0010,0011).(0020,0022).(003A,0033)"), result);
149  CPPUNIT_ASSERT_THROW(mitk::DICOMTagPathToDCMTKSearchPath(deepPath_withAnyElement), mitk::Exception);
150  result = mitk::DICOMTagPathToDCMTKSearchPath(deepPath_withAnySelection);
151  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToDCMTKSearchPath() with '(0010,0011).(002B,002E)[*].(0030,0033)'", std::string("(0010,0011).(002B,002E)[*].(0030,0033)"), result);
152  result = mitk::DICOMTagPathToDCMTKSearchPath(deepPath_withSelection);
153  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToDCMTKSearchPath() with '(0010,0011).(0020,0022)[6].(003b,003e)'", std::string("(0010,0011).(0020,0022)[6].(003B,003E)"), result);
154  CPPUNIT_ASSERT_THROW(mitk::DICOMTagPathToDCMTKSearchPath(verydeepPath), mitk::Exception);
155  }
156 
158  {
159  mitk::DICOMTagPath result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011");
160  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011)'", simplePath, result);
161  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.0020.0022.003A.0033");
162  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011).(0020,0022).(003A,0033)'", deepPath, result);
163  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.*.003a.003f");
164  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011).*.(003a,003f)'", deepPath_withAnyElement, result);
165  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.002B.002E.[*].0030.0033");
166  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011).(002B,002E)[*].(0030,0033)'", deepPath_withAnySelection, result);
167  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.0020.0022.[6].003b.003e");
168  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011).(0020,0022)[6].(003b,003e)'", deepPath_withSelection, result);
169  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.[*].*.0030.0033.004c.004d.[4].0050.0055");
170  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011)[*].*.(0030,0033).(004c,004d)[4].(0050,0055)'", verydeepPath, result);
171 
172  result = mitk::PropertyNameToDICOMTagPath("WRONG.0010.0011.0020.0022.0030.0033");
173  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with wrong path", emptyPath, result);
174  }
175 
177  {
178  std::string result = mitk::DICOMTagPathToPropertyName(simplePath);
179  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011)'", result, std::string("DICOM.0010.0011"));
180  result = mitk::DICOMTagPathToPropertyName(deepPath);
181  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011).(0020,0022).(003A,0033)'", result, std::string("DICOM.0010.0011.0020.0022.003A.0033"));
182  result = mitk::DICOMTagPathToPropertyName(deepPath_withAnyElement);
183  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011).*.(003a,003f)'", result, std::string("DICOM.0010.0011.*.003A.003F"));
184  result = mitk::DICOMTagPathToPropertyName(deepPath_withAnySelection);
185  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011).(002B,002E)[*].(0030,0033)'", result, std::string("DICOM.0010.0011.002B.002E.[*].0030.0033"));
186  result = mitk::DICOMTagPathToPropertyName(deepPath_withSelection);
187  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011).(0020,0022)[6].(003b,003e)'", result, std::string("DICOM.0010.0011.0020.0022.[6].003B.003E"));
188  result = mitk::DICOMTagPathToPropertyName(verydeepPath);
189  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011)[*].*.(0030,0033).(004c,004d)[4].(0050,0055)'", result, std::string("DICOM.0010.0011.[*].*.0030.0033.004C.004D.[4].0050.0055"));
190  }
191 
192  void ExecutePropertyRegEx()
193  {
194  std::regex regEx(mitk::DICOMTagPathToPropertyRegEx(simplePath));
195  std::string result = mitk::DICOMTagPathToPropertyName(simplePath);
196  CPPUNIT_ASSERT(std::regex_match(result, regEx));
197  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(deepPath));
198  result = mitk::DICOMTagPathToPropertyName(deepPath);
199  CPPUNIT_ASSERT(std::regex_match(result, regEx));
200  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(deepPath_withAnyElement));
201  result = mitk::DICOMTagPathToPropertyName(deepPath_withAnyElement);
202  auto position = result.find("*");
203  if (std::string::npos != position)
204  {
205  result.replace(position, 1, "1234.ABCD");
206  CPPUNIT_ASSERT(std::regex_match(result, regEx));
207  }
208  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(deepPath_withAnySelection));
209  result = mitk::DICOMTagPathToPropertyName(deepPath_withAnySelection);
210  position = result.find("[*]");
211  if (std::string::npos != position)
212  {
213  result.replace(position, 3, "[10]");
214  CPPUNIT_ASSERT(std::regex_match(result, regEx));
215  }
216  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(deepPath_withSelection));
217  result = mitk::DICOMTagPathToPropertyName(deepPath_withSelection);
218  CPPUNIT_ASSERT(std::regex_match(result, regEx));
219  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(verydeepPath));
220  result = mitk::DICOMTagPathToPropertyName(verydeepPath);
221  position = result.find("[*]");
222  if (std::string::npos != position)
223  {
224  result.replace(position, 3, "[1]");
225  position = result.find("*");
226  if (std::string::npos != position)
227  {
228  result.replace(position, 1, "abcd.1234");
229  CPPUNIT_ASSERT(std::regex_match(result, regEx));
230  }
231  }
232  }
233 
234 };
235 
236 MITK_TEST_SUITE_REGISTRATION(mitkDICOMTagPath)
DICOMTagPath & AddElement(unsigned int group, unsigned int element)
DICOMTagPath & AddAnySelection(unsigned int group, unsigned int element)
Class is used to identify (nested) attributes in a DICOM dataset. In contrast to the class DICOMTag...
MITK_TEST_SUITE_REGISTRATION(mitkImageToItk)
MITKDICOMREADER_EXPORT std::string DICOMTagPathToPersistenceKeyRegEx(const DICOMTagPath &tagPath)
DICOMTagPath & AddAnyElement()
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
MITKDICOMREADER_EXPORT std::string DICOMTagPathToPersistenceNameTemplate(const DICOMTagPath &tagPath)
MITKDICOMREADER_EXPORT std::string DICOMTagPathToPropertyName(const DICOMTagPath &tagPath)
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
Test fixture for parameterized tests.
MITKDICOMREADER_EXPORT std::string DICOMTagPathToPersistenceKeyTemplate(const DICOMTagPath &tagPath)
MITKDICOMREADER_EXPORT std::string DICOMTagPathToPropertyRegEx(const DICOMTagPath &tagPath)
DICOMTagPath & AddSelection(unsigned int group, unsigned int element, ItemSelectionIndex index)
MITKDICOMREADER_EXPORT std::string DICOMTagPathToDCMTKSearchPath(const DICOMTagPath &tagPath)
MITKDICOMREADER_EXPORT DICOMTagPath PropertyNameToDICOMTagPath(const std::string &propertyName)