Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkDICOMTagPath.h"
18 
19 #include "mitkTestFixture.h"
20 #include "mitkTestingMacros.h"
21 
22 #include <regex>
23 
24 class mitkDICOMTagPathTestSuite : public mitk::TestFixture
25 {
26  CPPUNIT_TEST_SUITE(mitkDICOMTagPathTestSuite);
27 
35  MITK_TEST(ExecutePropertyRegEx);
36 
37  CPPUNIT_TEST_SUITE_END();
38 
39 private:
40 
41  mitk::DICOMTagPath simplePath;
42  mitk::DICOMTagPath deepPath;
43  mitk::DICOMTagPath deepPath_withAnyElement;
44  mitk::DICOMTagPath deepPath_withAnySelection;
45  mitk::DICOMTagPath deepPath_withSelection;
46  mitk::DICOMTagPath verydeepPath;
47 
48  mitk::DICOMTagPath emptyPath;
49 
50 public:
51 
52  void setUp() override
53  {
54  simplePath.AddElement(0x0010, 0x0011);
55 
56  deepPath.AddElement(0x0010, 0x0011);
57  deepPath.AddElement(0x0020, 0x0022);
58  deepPath.AddElement(0x003A, 0x0033);
59 
60  deepPath_withAnyElement.AddElement(0x0010, 0x0011);
61  deepPath_withAnyElement.AddAnyElement();
62  deepPath_withAnyElement.AddElement(0x003a, 0x003f);
63 
64  deepPath_withAnySelection.AddElement(0x0010, 0x0011);
65  deepPath_withAnySelection.AddAnySelection(0x002B, 0x002E);
66  deepPath_withAnySelection.AddElement(0x0030, 0x0033);
67 
68  deepPath_withSelection.AddElement(0x0010, 0x0011);
69  deepPath_withSelection.AddSelection(0x0020, 0x0022, 6);
70  deepPath_withSelection.AddElement(0x003b, 0x003e);
71 
72  verydeepPath.AddAnySelection(0x0010, 0x0011);
73  verydeepPath.AddAnyElement();
74  verydeepPath.AddElement(0x0030, 0x0033);
75  verydeepPath.AddSelection(0x004c, 0x004d, 4);
76  verydeepPath.AddElement(0x0050, 0x0055);
77  }
78 
79  void tearDown() override
80  {
81  }
82 
84  {
85  std::string result = mitk::DICOMTagPathToPropertyRegEx(simplePath);
86  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyRegEx() with '(0010,0011)'", std::string("DICOM\\.0010\\.0011"), result);
87  result = mitk::DICOMTagPathToPropertyRegEx(deepPath);
88  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyRegEx() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("DICOM\\.0010\\.0011\\.0020\\.0022\\.(003a|003A)\\.0033"), result);
89  result = mitk::DICOMTagPathToPropertyRegEx(deepPath_withAnyElement);
90  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);
91  result = mitk::DICOMTagPathToPropertyRegEx(deepPath_withAnySelection);
92  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);
93  result = mitk::DICOMTagPathToPropertyRegEx(deepPath_withSelection);
94  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);
95  result = mitk::DICOMTagPathToPropertyRegEx(verydeepPath);
96  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);
97  }
98 
100  {
101  std::string result = mitk::DICOMTagPathToPersistenceKeyRegEx(simplePath);
102  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyRegEx() with '(0010,0011)'", std::string("DICOM_0010_0011"), result);
103  result = mitk::DICOMTagPathToPersistenceKeyRegEx(deepPath);
104  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyRegEx() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("DICOM_0010_0011_0020_0022_(003a|003A)_0033"), result);
105  result = mitk::DICOMTagPathToPersistenceKeyRegEx(deepPath_withAnyElement);
106  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);
107  result = mitk::DICOMTagPathToPersistenceKeyRegEx(deepPath_withAnySelection);
108  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);
109  result = mitk::DICOMTagPathToPersistenceKeyRegEx(deepPath_withSelection);
110  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);
111  result = mitk::DICOMTagPathToPersistenceKeyRegEx(verydeepPath);
112  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);
113  }
114 
116  {
117  std::string result = mitk::DICOMTagPathToPersistenceKeyTemplate(simplePath);
118  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011)'", std::string("DICOM_0010_0011"), result);
120  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("DICOM_0010_0011_0020_0022_003A_0033"), result);
121  result = mitk::DICOMTagPathToPersistenceKeyTemplate(deepPath_withAnyElement);
122  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011).*.(003a,003f)'", std::string("DICOM_0010_0011_$1_$2_003A_003F"), result);
123  result = mitk::DICOMTagPathToPersistenceKeyTemplate(deepPath_withAnySelection);
124  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceKeyTemplate() with '(0010,0011).(002B,002E)[*].(0030,0033)'", std::string("DICOM_0010_0011_002B_002E_[$1]_0030_0033"), result);
125  result = mitk::DICOMTagPathToPersistenceKeyTemplate(deepPath_withSelection);
126  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);
127  result = mitk::DICOMTagPathToPersistenceKeyTemplate(verydeepPath);
128  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);
129  }
130 
132  {
133  std::string result = mitk::DICOMTagPathToPersistenceNameTemplate(simplePath);
134  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011)'", std::string("DICOM.0010.0011"), result);
136  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("DICOM.0010.0011.0020.0022.003A.0033"), result);
137  result = mitk::DICOMTagPathToPersistenceNameTemplate(deepPath_withAnyElement);
138  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011).*.(003a,003f)'", std::string("DICOM.0010.0011.$1.$2.003A.003F"), result);
139  result = mitk::DICOMTagPathToPersistenceNameTemplate(deepPath_withAnySelection);
140  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPersistenceNameTemplate() with '(0010,0011).(002B,002E)[*].(0030,0033)'", std::string("DICOM.0010.0011.002B.002E.[$1].0030.0033"), result);
141  result = mitk::DICOMTagPathToPersistenceNameTemplate(deepPath_withSelection);
142  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);
143  result = mitk::DICOMTagPathToPersistenceNameTemplate(verydeepPath);
144  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);
145  }
146 
148  {
149  std::string result = mitk::DICOMTagPathToDCMTKSearchPath(simplePath);
150  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToDCMTKSearchPath() with '(0010,0011)'", std::string("(0010,0011)"), result);
151  result = mitk::DICOMTagPathToDCMTKSearchPath(deepPath);
152  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToDCMTKSearchPath() with '(0010,0011).(0020,0022).(003A,0033)'", std::string("(0010,0011).(0020,0022).(003A,0033)"), result);
153  CPPUNIT_ASSERT_THROW(mitk::DICOMTagPathToDCMTKSearchPath(deepPath_withAnyElement), mitk::Exception);
154  result = mitk::DICOMTagPathToDCMTKSearchPath(deepPath_withAnySelection);
155  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToDCMTKSearchPath() with '(0010,0011).(002B,002E)[*].(0030,0033)'", std::string("(0010,0011).(002B,002E)[*].(0030,0033)"), result);
156  result = mitk::DICOMTagPathToDCMTKSearchPath(deepPath_withSelection);
157  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToDCMTKSearchPath() with '(0010,0011).(0020,0022)[6].(003b,003e)'", std::string("(0010,0011).(0020,0022)[6].(003B,003E)"), result);
158  CPPUNIT_ASSERT_THROW(mitk::DICOMTagPathToDCMTKSearchPath(verydeepPath), mitk::Exception);
159  }
160 
162  {
163  mitk::DICOMTagPath result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011");
164  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011)'", simplePath, result);
165  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.0020.0022.003A.0033");
166  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011).(0020,0022).(003A,0033)'", deepPath, result);
167  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.*.003a.003f");
168  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011).*.(003a,003f)'", deepPath_withAnyElement, result);
169  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.002B.002E.[*].0030.0033");
170  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011).(002B,002E)[*].(0030,0033)'", deepPath_withAnySelection, result);
171  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.0020.0022.[6].003b.003e");
172  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011).(0020,0022)[6].(003b,003e)'", deepPath_withSelection, result);
173  result = mitk::PropertyNameToDICOMTagPath("DICOM.0010.0011.[*].*.0030.0033.004c.004d.[4].0050.0055");
174  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with '(0010,0011)[*].*.(0030,0033).(004c,004d)[4].(0050,0055)'", verydeepPath, result);
175 
176  result = mitk::PropertyNameToDICOMTagPath("WRONG.0010.0011.0020.0022.0030.0033");
177  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing PropertyNameToDICOMTagPath() with wrong path", emptyPath, result);
178  }
179 
181  {
182  std::string result = mitk::DICOMTagPathToPropertyName(simplePath);
183  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011)'", result, std::string("DICOM.0010.0011"));
184  result = mitk::DICOMTagPathToPropertyName(deepPath);
185  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011).(0020,0022).(003A,0033)'", result, std::string("DICOM.0010.0011.0020.0022.003A.0033"));
186  result = mitk::DICOMTagPathToPropertyName(deepPath_withAnyElement);
187  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011).*.(003a,003f)'", result, std::string("DICOM.0010.0011.*.003A.003F"));
188  result = mitk::DICOMTagPathToPropertyName(deepPath_withAnySelection);
189  CPPUNIT_ASSERT_EQUAL_MESSAGE("Testing DICOMTagPathToPropertyName() with '(0010,0011).(002B,002E)[*].(0030,0033)'", result, std::string("DICOM.0010.0011.002B.002E.[*].0030.0033"));
190  result = mitk::DICOMTagPathToPropertyName(deepPath_withSelection);
191  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"));
192  result = mitk::DICOMTagPathToPropertyName(verydeepPath);
193  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"));
194  }
195 
196  void ExecutePropertyRegEx()
197  {
198  std::regex regEx(mitk::DICOMTagPathToPropertyRegEx(simplePath));
199  std::string result = mitk::DICOMTagPathToPropertyName(simplePath);
200  CPPUNIT_ASSERT(std::regex_match(result, regEx));
201  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(deepPath));
202  result = mitk::DICOMTagPathToPropertyName(deepPath);
203  CPPUNIT_ASSERT(std::regex_match(result, regEx));
204  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(deepPath_withAnyElement));
205  result = mitk::DICOMTagPathToPropertyName(deepPath_withAnyElement);
206  int position = result.find("*");
207  if (std::string::npos != position)
208  {
209  result.replace(position, 1, "1234.ABCD");
210  CPPUNIT_ASSERT(std::regex_match(result, regEx));
211  }
212  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(deepPath_withAnySelection));
213  result = mitk::DICOMTagPathToPropertyName(deepPath_withAnySelection);
214  position = result.find("[*]");
215  if (std::string::npos != position)
216  {
217  result.replace(position, 3, "[10]");
218  CPPUNIT_ASSERT(std::regex_match(result, regEx));
219  }
220  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(deepPath_withSelection));
221  result = mitk::DICOMTagPathToPropertyName(deepPath_withSelection);
222  CPPUNIT_ASSERT(std::regex_match(result, regEx));
223  regEx = std::regex(mitk::DICOMTagPathToPropertyRegEx(verydeepPath));
224  result = mitk::DICOMTagPathToPropertyName(verydeepPath);
225  position = result.find("[*]");
226  if (std::string::npos != position)
227  {
228  result.replace(position, 3, "[1]");
229  position = result.find("*");
230  if (std::string::npos != position)
231  {
232  result.replace(position, 1, "abcd.1234");
233  CPPUNIT_ASSERT(std::regex_match(result, regEx));
234  }
235  }
236  }
237 
238 };
239 
240 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't instantiate exceptions manually...
Definition: mitkException.h:49
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)