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
29VerifyMetaDataPrivateTestingUtility(const MetaDataDictionary & metaDict, const std::string & key, const T & knownValue)
30{
31 int status = EXIT_SUCCESS;
32 T exposedValue{};
33
34#if defined ITK_FUTURE_LEGACY_REMOVE
35 static_assert(
36 !std::is_same_v<itk::Array<char>, T>,
37 "Should not use the ambiguous 'char' stored in meta data, because it is not-cross platform consistent.");
38 static_assert(
39 !std::is_same_v<char, T>,
40 "Should not use the ambiguous 'char' stored in meta data, because it is not-cross platform consistent.");
41 if (!itk::ExposeMetaData<T>(metaDict, key, exposedValue))
42 {
43 std::cerr << "Failure ExposeMetaData for key '" << key << "'" << std::endl;
44 status = EXIT_FAILURE;
45 }
46#else
47 if constexpr (std::is_same_v<itk::Array<char>, T>)
48 {
49 // If Encapsulate and Expose Metadata is all in core memory operations,
50 // the type of char may be preserved.
51 if (!itk::ExposeMetaData<itk::Array<char>>(metaDict, key, exposedValue))
52 {
53 // If Encapsulate and Expose Metadata is written to disk and
54 // possibly shared across platforms, then 'char' may have been
55 // stored in an intermediate format (aka file on disk) with explicit
56 // signed or unsigned characteristics
57 if constexpr (std::is_signed_v<char>)
58 {
59 itk::Array<signed char> temp_value{};
60 if (!itk::ExposeMetaData<itk::Array<signed char>>(metaDict, key, temp_value))
61 {
62 std::cerr << "Failure ExposeMetaData '" << key << "'" << std::endl;
63 status = EXIT_FAILURE;
64 }
65 exposedValue = temp_value;
66 }
67 else
68 {
69 itk::Array<unsigned char> temp_value{};
70 if (!itk::ExposeMetaData<itk::Array<unsigned char>>(metaDict, key, temp_value))
71 {
72 std::cerr << "Failure ExposeMetaData '" << key << "'" << std::endl;
73 status = EXIT_FAILURE;
74 }
75 exposedValue = temp_value;
76 }
77 }
78 }
79 else if constexpr (std::is_same_v<char, T>)
80 {
81 // If Encapsulate and Expose Metadata is all in core memory operations,
82 // the type of char may be preserved.
83 if (!itk::ExposeMetaData<char>(metaDict, key, exposedValue))
84 {
85 // If Encapsulate and Expose Metadata is written to disk and
86 // possibly shared across platforms, then 'char' may have been
87 // stored in an intermediate format (aka file on disk) with explicit
88 // signed or unsigned characteristics
89 if constexpr (std::is_signed_v<char>)
90 {
91 signed char temp_value{};
92
93 if (!itk::ExposeMetaData<signed char>(metaDict, key, temp_value))
94 {
95 std::cerr << "Failure ExposeMetaData '" << key << "'" << std::endl;
96 status = EXIT_FAILURE;
97 }
98 exposedValue = static_cast<T>(temp_value);
99 }
100 else
101 {
102 unsigned char temp_value{};
103 if (!itk::ExposeMetaData<unsigned char>(metaDict, key, temp_value))
104 {
105 std::cerr << "Failure ExposeMetaData '" << key << "'" << std::endl;
106 status = EXIT_FAILURE;
107 }
108 exposedValue = static_cast<T>(temp_value);
109 }
110 }
111 }
112 else if (!itk::ExposeMetaData<T>(metaDict, key, exposedValue))
113 {
114 std::cerr << "Failure ExposeMetaData of boolean type '" << key << "'" << std::endl;
115 status = EXIT_FAILURE;
116 }
117#endif
118
119
120 if constexpr (std::is_floating_point_v<T>)
121 {
122 if (itk::Math::NotAlmostEquals(exposedValue, knownValue))
123 {
124 std::cerr << "Incorrect meta value read in for " << key << " '" << exposedValue << "' != '" << knownValue << "'"
125 << std::endl;
126 status = EXIT_FAILURE;
127 }
128 }
129 else
130 {
131 if (exposedValue != knownValue)
132 {
133 std::cerr << "Incorrect meta value read in for " << key << " '" << exposedValue << "' != '" << knownValue << "'"
134 << std::endl;
135 status = EXIT_FAILURE;
136 }
137 }
138 if (status == EXIT_FAILURE)
139 {
140 std::cerr << "========================================" << std::endl;
141 metaDict.Print(std::cerr);
142 std::cerr << "========================================" << std::endl;
143 }
144 return status;
145}
146} // namespace itk
147
148#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:689
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 MetaDataDictionary &metaDict, const std::string &key, const T &knownValue)