File: ParameterObjectGTest.cxx

package info (click to toggle)
elastix 5.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 45,644 kB
  • sloc: cpp: 85,720; lisp: 4,118; python: 1,045; sh: 200; xml: 182; makefile: 33
file content (265 lines) | stat: -rw-r--r-- 10,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*=========================================================================
 *
 *  Copyright UMC Utrecht and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

// First include the header file to be tested:
#include "elxParameterObject.h"

#include "GTesting/elxGTestUtilities.h"

#include "elxCoreMainGTestUtilities.h"
#include "elxDefaultConstruct.h"

// ITK header file:
#include <itkFileTools.h>
#include <itksys/SystemTools.hxx>

// GoogleTest header file:
#include <gtest/gtest.h>

#include <string>

// Type aliases:
using ParameterMapType = elx::ParameterObject::ParameterMapType;
using ParameterMapVectorType = elx::ParameterObject::ParameterMapVectorType;
using ParameterValueVectorType = elx::ParameterObject::ParameterValueVectorType;
using ParameterFileNameVectorType = elx::ParameterObject::ParameterFileNameVectorType;

// Using-declarations:
using elx::CoreMainGTestUtilities::GetCurrentBinaryDirectoryPath;
using elx::CoreMainGTestUtilities::GetNameOfTest;
using elx::CoreMainGTestUtilities::TypeHolder;

// Tests that ParameterObject::WriteParameterFiles writes all the specified files.
GTEST_TEST(ParameterObject, WriteParameterFiles)
{
  const std::string rootOutputDirectoryPath = GetCurrentBinaryDirectoryPath() + '/' + GetNameOfTest(*this);
  itk::FileTools::CreateDirectory(rootOutputDirectoryPath);

  elx::DefaultConstruct<elx::ParameterObject> parameterObject{};

  for (const std::size_t numberOfMaps : { 0, 1, 2 })
  {
    const std::string outputDirectoryPath = rootOutputDirectoryPath + '/' + std::to_string(numberOfMaps);
    itk::FileTools::CreateDirectory(outputDirectoryPath);

    parameterObject.SetParameterMaps(ParameterMapVectorType(numberOfMaps));

    ParameterFileNameVectorType fileNames{};

    for (std::size_t i{}; i < numberOfMaps; ++i)
    {
      fileNames.push_back(outputDirectoryPath + '/' + "ParameterFile." + std::to_string(i) + ".txt");
    }

    parameterObject.WriteParameterFiles(fileNames);

    // Check that each of the specified files is written to disk.
    for (const auto & fileName : fileNames)
    {
      EXPECT_TRUE(itksys::SystemTools::FileExists(fileName.c_str(), true));
    }
  }
}


// Tests that any numeric parameter value is printed (by `<<` into an output stream) literally as it was originally
// placed into the ParameterMap.
GTEST_TEST(ParameterObject, PrintOriginalNumericValues)
{
  static constexpr auto check = [](const std::string & parameterValue) {
    elx::DefaultConstruct<elx::ParameterObject> parameterObject{};

    const std::string parameterName = "ParameterName";

    parameterObject.SetParameterMap(elx::ParameterObject::ParameterMapType{ { parameterName, { parameterValue } } });
    std::ostringstream outputStringStream;

    outputStringStream << parameterObject;
    const auto        printedString = outputStringStream.str();
    const std::string expectedParameter = '(' + parameterName + ' ' + parameterValue + ")\n";
    ASSERT_GT(printedString.size(), expectedParameter.size());
    EXPECT_EQ(std::string_view(printedString).substr(printedString.size() - expectedParameter.size()),
              expectedParameter);
  };

  for (const std::string parameterValue : { "0", "1", "1.23456789", "0.1", "-0.1", "3e+38", "4e+38" })
  {
    check(parameterValue);
  }

  check(std::to_string(std::numeric_limits<std::int32_t>::min()));
  check(std::to_string(std::numeric_limits<std::int32_t>::max()));
  check(std::to_string(std::numeric_limits<std::uint32_t>::max()));
  check(std::to_string(std::numeric_limits<std::int64_t>::min()));
  check(std::to_string(std::numeric_limits<std::int64_t>::max()));
  check(std::to_string(std::numeric_limits<std::uint64_t>::max()));

  {
    // Test +/- 100000000000000000000
    constexpr auto power_of_ten = 10.0 * static_cast<double>(itk::Math::UnsignedPower(10, 19));
    check(elx::Conversion::ToString(power_of_ten));
    check(elx::Conversion::ToString(-power_of_ten));
  }
  {
    // Test +/- 999999999999999
    constexpr auto power_of_ten = static_cast<double>(itk::Math::UnsignedPower(10, 15));
    check(elx::Conversion::ToString(power_of_ten - 1.0));
    check(elx::Conversion::ToString(1.0 - power_of_ten));
  }
  {
    // Test +/- 0.000001
    constexpr auto power_of_ten = 1.0 / static_cast<double>(itk::Math::UnsignedPower(10, 6));
    check(elx::Conversion::ToString(power_of_ten));
    check(elx::Conversion::ToString(-power_of_ten));
  }

  const auto checkNumericLimits = [](const auto typeHolder) {
    (void)typeHolder;
    using FloatingPointType = typename decltype(typeHolder)::Type;
    using NumericLimits = std::numeric_limits<FloatingPointType>;
    for (const auto value : { NumericLimits::epsilon(),
                              NumericLimits::min(),
                              NumericLimits::lowest(),
                              NumericLimits::max(),
                              NumericLimits::denorm_min(),
                              -NumericLimits::denorm_min() })
    {
      check(elx::Conversion::ToString(value));
    }
  };

  checkNumericLimits(TypeHolder<float>{});
  checkNumericLimits(TypeHolder<double>{});
}


// Tests HasParameter method with index parameter
GTEST_TEST(ParameterObject, HasParameterWithIndex)
{
  elx::DefaultConstruct<elx::ParameterObject> parameterObject{};

  // Test with empty parameter object
  EXPECT_FALSE(parameterObject.HasParameter(0, "NonExistentParameter"));

  // Create parameter maps
  ParameterMapType parameterMap1;
  parameterMap1["ExistingParameter1"] = { "value1" };
  parameterMap1["ExistingParameter2"] = { "value2a", "value2b" };

  ParameterMapType parameterMap2;
  parameterMap2["ExistingParameter1"] = { "differentValue" };
  parameterMap2["ExistingParameter3"] = { "value3" };

  ParameterMapVectorType parameterMapVector = { parameterMap1, parameterMap2 };
  parameterObject.SetParameterMaps(parameterMapVector);

  // Test existing parameters in first map (index 0)
  EXPECT_TRUE(parameterObject.HasParameter(0, "ExistingParameter1"));
  EXPECT_TRUE(parameterObject.HasParameter(0, "ExistingParameter2"));
  EXPECT_FALSE(parameterObject.HasParameter(0, "ExistingParameter3"));
  EXPECT_FALSE(parameterObject.HasParameter(0, "NonExistentParameter"));

  // Test existing parameters in second map (index 1)
  EXPECT_TRUE(parameterObject.HasParameter(1, "ExistingParameter1"));
  EXPECT_FALSE(parameterObject.HasParameter(1, "ExistingParameter2"));
  EXPECT_TRUE(parameterObject.HasParameter(1, "ExistingParameter3"));
  EXPECT_FALSE(parameterObject.HasParameter(1, "NonExistentParameter"));

  // Test with out-of-bounds index
  EXPECT_FALSE(parameterObject.HasParameter(2, "ExistingParameter1"));
  EXPECT_FALSE(parameterObject.HasParameter(10, "ExistingParameter1"));
}


// Tests HasParameter method without index parameter (searches all maps)
GTEST_TEST(ParameterObject, HasParameterWithoutIndex)
{
  elx::DefaultConstruct<elx::ParameterObject> parameterObject{};

  // Test with empty parameter object
  EXPECT_FALSE(parameterObject.HasParameter("NonExistentParameter"));

  // Create parameter maps
  ParameterMapType parameterMap1;
  parameterMap1["OnlyInMap1"] = { "value1" };
  parameterMap1["InBothMaps"] = { "value2" };

  ParameterMapType parameterMap2;
  parameterMap2["OnlyInMap2"] = { "value3" };
  parameterMap2["InBothMaps"] = { "differentValue" };

  ParameterMapVectorType parameterMapVector = { parameterMap1, parameterMap2 };
  parameterObject.SetParameterMaps(parameterMapVector);

  // Test parameters that exist in specific maps
  EXPECT_TRUE(parameterObject.HasParameter("OnlyInMap1")); // Only in first map
  EXPECT_TRUE(parameterObject.HasParameter("OnlyInMap2")); // Only in second map
  EXPECT_TRUE(parameterObject.HasParameter("InBothMaps")); // In both maps

  // Test non-existent parameter
  EXPECT_FALSE(parameterObject.HasParameter("NonExistentParameter"));

  // Test with single parameter map
  ParameterMapVectorType singleMapVector = { parameterMap1 };
  parameterObject.SetParameterMaps(singleMapVector);

  EXPECT_TRUE(parameterObject.HasParameter("OnlyInMap1"));
  EXPECT_TRUE(parameterObject.HasParameter("InBothMaps"));
  EXPECT_FALSE(parameterObject.HasParameter("OnlyInMap2"));
  EXPECT_FALSE(parameterObject.HasParameter("NonExistentParameter"));
}


// Tests that ParameterObject member functions throw an `itk::ExceptionObject` when the index specified as function
// argument is out of range. Note: HasParameter(index, key) does not throw, when the index is out of range.
GTEST_TEST(ParameterObject, ThrowsExceptionWhenIndexIsOutOfRange)
{
  const std::string              parameterName = "parameterName";
  const std::string              parameterValue = "value";
  const ParameterValueVectorType parameterValues = { "value1", "value2", "value3" };
  const ParameterMapType         parameterMap{ { parameterName, parameterValues } };

  for (const unsigned int numberOfParameterMaps : { 0, 1 })
  {
    elx::DefaultConstruct<elx::ParameterObject> parameterObject{};

    parameterObject.SetParameterMaps(ParameterMapVectorType(numberOfParameterMaps, parameterMap));

    for (const unsigned int index :
         { numberOfParameterMaps, numberOfParameterMaps + 1, std::numeric_limits<unsigned int>::max() })
    {
      EXPECT_THROW(parameterObject.GetParameterMap(index), itk::ExceptionObject);
      EXPECT_THROW(parameterObject.SetParameterMap(index, parameterMap), itk::ExceptionObject);
      EXPECT_THROW(parameterObject.SetParameter(index, parameterName, parameterValue), itk::ExceptionObject);
      EXPECT_THROW(parameterObject.SetParameter(index, parameterName, parameterValues), itk::ExceptionObject);
      EXPECT_THROW(parameterObject.GetParameter(index, parameterName), itk::ExceptionObject);
      EXPECT_THROW(parameterObject.RemoveParameter(index, parameterName), itk::ExceptionObject);
    }
  }
}


// Tests that `parameterObject.GetParameter(index, parameterName)` throw an `itk::ExceptionObject` when the parameter
// name is not there, in the specified parameter map.
GTEST_TEST(ParameterObject, GetParameterThrowsExceptionWhenParameterNameIsNotThere)
{
  elx::DefaultConstruct<elx::ParameterObject> parameterObject{};
  parameterObject.SetParameterMaps(ParameterMapVectorType(1));

  EXPECT_THROW(parameterObject.GetParameter(0, "nonExistingParameterName"), itk::ExceptionObject);
}