ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkTestVerifyMetaData.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkTestVerifyMetaData_h
19#define itkTestVerifyMetaData_h
20
22#include "itkMetaDataObject.h"
23namespace itk
24{
25/* A utility function used for testing, this is not intended to be part of the public interface */
26/* Used only to avoid duplicate code in itkMetaDictionaryGTest.cxx and itkHDF5ImageIOTest.cxx */
27template <typename T>
28int
30 const std::string & key,
31 const T & knownValue)
32{
33 int status = EXIT_SUCCESS;
34 T exposedValue{};
35
36#if defined ITK_FUTURE_LEGACY_REMOVE
37 static_assert(
38 !std::is_same_v<itk::Array<char>, T>,
39 "Should not use the ambiguous 'char' stored in meta data, because it is not-cross platform consistent.");
40 static_assert(
41 !std::is_same_v<char, T>,
42 "Should not use the ambiguous 'char' stored in meta data, because it is not-cross platform consistent.");
43 if (!itk::ExposeMetaData<T>(metaDict, key, exposedValue))
44 {
45 std::cerr << "Failure ExposeMetaData for key '" << key << "'" << std::endl;
46 status = EXIT_FAILURE;
47 }
48#else
49 if constexpr (std::is_same_v<itk::Array<char>, T>)
50 {
51 // If Encapsulate and Expose Metadata is all in core memory operations,
52 // the type of char may be preserved.
53 if (!itk::ExposeMetaData<itk::Array<char>>(metaDict, key, exposedValue))
54 {
55 // If Encapsulate and Expose Metadata is written to disk and
56 // possibly shared across platforms, then 'char' may have been
57 // stored in an intermediate format (aka file on disk) with explicit
58 // signed or unsigned characteristics
59 if constexpr (std::is_signed_v<char>)
60 {
61 itk::Array<signed char> temp_value{};
62 if (!itk::ExposeMetaData<itk::Array<signed char>>(metaDict, key, temp_value))
63 {
64 std::cerr << "Failure ExposeMetaData '" << key << "'" << std::endl;
65 status = EXIT_FAILURE;
66 }
67 exposedValue = temp_value;
68 }
69 else
70 {
71 itk::Array<unsigned char> temp_value{};
72 if (!itk::ExposeMetaData<itk::Array<unsigned char>>(metaDict, key, temp_value))
73 {
74 std::cerr << "Failure ExposeMetaData '" << key << "'" << std::endl;
75 status = EXIT_FAILURE;
76 }
77 exposedValue = temp_value;
78 }
79 }
80 }
81 else if constexpr (std::is_same_v<char, T>)
82 {
83 // If Encapsulate and Expose Metadata is all in core memory operations,
84 // the type of char may be preserved.
85 if (!itk::ExposeMetaData<char>(metaDict, key, exposedValue))
86 {
87 // If Encapsulate and Expose Metadata is written to disk and
88 // possibly shared across platforms, then 'char' may have been
89 // stored in an intermediate format (aka file on disk) with explicit
90 // signed or unsigned characteristics
91 if constexpr (std::is_signed_v<char>)
92 {
93 signed char temp_value{};
94
95 if (!itk::ExposeMetaData<signed char>(metaDict, key, temp_value))
96 {
97 std::cerr << "Failure ExposeMetaData '" << key << "'" << std::endl;
98 status = EXIT_FAILURE;
99 }
100 exposedValue = static_cast<T>(temp_value);
101 }
102 else
103 {
104 unsigned char temp_value{};
105 if (!itk::ExposeMetaData<unsigned char>(metaDict, key, temp_value))
106 {
107 std::cerr << "Failure ExposeMetaData '" << key << "'" << std::endl;
108 status = EXIT_FAILURE;
109 }
110 exposedValue = static_cast<T>(temp_value);
111 }
112 }
113 }
114 else if (!itk::ExposeMetaData<T>(metaDict, key, exposedValue))
115 {
116 std::cerr << "Failure ExposeMetaData of boolean type '" << key << "'" << std::endl;
117 status = EXIT_FAILURE;
118 }
119#endif
120
121
122 if constexpr (std::is_floating_point_v<T>)
123 {
124 if (itk::Math::NotAlmostEquals(exposedValue, knownValue))
125 {
126 std::cerr << "Incorrect meta value read in for " << key << " '" << exposedValue << "' != '" << knownValue << "'"
127 << std::endl;
128 status = EXIT_FAILURE;
129 }
130 }
131 else
132 {
133 if (exposedValue != knownValue)
134 {
135 std::cerr << "Incorrect meta value read in for " << key << " '" << exposedValue << "' != '" << knownValue << "'"
136 << std::endl;
137 status = EXIT_FAILURE;
138 }
139 }
140 if (status == EXIT_FAILURE)
141 {
142 std::cerr << "========================================" << std::endl;
143 metaDict.Print(std::cerr);
144 std::cerr << "========================================" << std::endl;
145 }
146 return status;
147}
148} // namespace itk
149
150#endif // itkTestVerifyMetaData_h
Array class with size defined at construction time.
Definition itkArray.h:48
Provides a mechanism for storing a collection of arbitrary data types.
virtual void Print(std::ostream &os) const
bool NotAlmostEquals(T1 x1, T2 x2)
Definition itkMath.h:688
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
bool ExposeMetaData(const MetaDataDictionary &Dictionary, const std::string key, T &outval)
int VerifyMetaDataPrivateTestingUtility(const itk::MetaDataDictionary &metaDict, const std::string &key, const T &knownValue)