|
20 | 20 | #include "itkOffset.h" |
21 | 21 | #include "gtest/gtest.h" |
22 | 22 | #include <array> |
| 23 | +#include <complex> |
23 | 24 | #include <sstream> |
24 | 25 | #include <vector> |
25 | 26 | #include <list> |
@@ -130,3 +131,55 @@ TEST(PrintHelper, ArrayOfVector) |
130 | 131 | oss << a; |
131 | 132 | EXPECT_EQ(oss.str(), "([1, 2], [3])"); |
132 | 133 | } |
| 134 | + |
| 135 | +TEST(PrintHelper, PrintNumericTraitDouble) |
| 136 | +{ |
| 137 | + std::ostringstream oss; |
| 138 | + itk::print_helper::PrintNumericTrait(oss, itk::Indent{}, "Value", 3.5); |
| 139 | + EXPECT_EQ(oss.str(), "Value: 3.5\n"); |
| 140 | +} |
| 141 | + |
| 142 | +TEST(PrintHelper, PrintNumericTraitIntIsIdentityCast) |
| 143 | +{ |
| 144 | + // PrintType<int> == int, so the constexpr branch skips static_cast and |
| 145 | + // streams the value directly. |
| 146 | + std::ostringstream oss; |
| 147 | + itk::print_helper::PrintNumericTrait(oss, itk::Indent{}, "Count", 42); |
| 148 | + EXPECT_EQ(oss.str(), "Count: 42\n"); |
| 149 | +} |
| 150 | + |
| 151 | +TEST(PrintHelper, PrintNumericTraitCharRendersNumerically) |
| 152 | +{ |
| 153 | + // PrintType<unsigned char> == int. Without the cast the value would be |
| 154 | + // emitted as the ASCII character; the helper must produce the integer. |
| 155 | + std::ostringstream oss; |
| 156 | + const unsigned char ch = 65; |
| 157 | + itk::print_helper::PrintNumericTrait(oss, itk::Indent{}, "Byte", ch); |
| 158 | + EXPECT_EQ(oss.str(), "Byte: 65\n"); |
| 159 | +} |
| 160 | + |
| 161 | +TEST(PrintHelper, PrintNumericTraitSignedCharRendersNumerically) |
| 162 | +{ |
| 163 | + std::ostringstream oss; |
| 164 | + const signed char sc = -7; |
| 165 | + itk::print_helper::PrintNumericTrait(oss, itk::Indent{}, "Offset", sc); |
| 166 | + EXPECT_EQ(oss.str(), "Offset: -7\n"); |
| 167 | +} |
| 168 | + |
| 169 | +TEST(PrintHelper, PrintNumericTraitComplexIsIdentityCast) |
| 170 | +{ |
| 171 | + // NumericTraits<std::complex<T>>::PrintType is Self, so PrintNumericTrait |
| 172 | + // forwards to the value's own ostream insertion overload unchanged. |
| 173 | + std::ostringstream oss; |
| 174 | + std::complex<double> z{ 1.0, -2.5 }; |
| 175 | + itk::print_helper::PrintNumericTrait(oss, itk::Indent{}, "Z", z); |
| 176 | + EXPECT_EQ(oss.str(), "Z: (1,-2.5)\n"); |
| 177 | +} |
| 178 | + |
| 179 | +TEST(PrintHelper, PrintNumericTraitIndentation) |
| 180 | +{ |
| 181 | + std::ostringstream oss; |
| 182 | + itk::Indent indent{ 4 }; |
| 183 | + itk::print_helper::PrintNumericTrait(oss, indent, "Threshold", 0.125); |
| 184 | + EXPECT_EQ(oss.str(), " Threshold: 0.125\n"); |
| 185 | +} |
0 commit comments