|
| 1 | +#include "hist_test.hxx" |
| 2 | + |
| 3 | +#include <limits> |
| 4 | + |
| 5 | +TEST(RRegularAxis, Constructor) |
| 6 | +{ |
| 7 | + static constexpr std::size_t Bins = 20; |
| 8 | + RRegularAxis axis(Bins, 0, Bins); |
| 9 | + EXPECT_EQ(axis.GetNumNormalBins(), Bins); |
| 10 | + EXPECT_EQ(axis.GetTotalNumBins(), Bins + 2); |
| 11 | + EXPECT_EQ(axis.GetLow(), 0); |
| 12 | + EXPECT_EQ(axis.GetHigh(), Bins); |
| 13 | + EXPECT_TRUE(axis.HasFlowBins()); |
| 14 | + |
| 15 | + axis = RRegularAxis(Bins, 0, Bins, /*enableFlowBins=*/false); |
| 16 | + EXPECT_EQ(axis.GetNumNormalBins(), Bins); |
| 17 | + EXPECT_EQ(axis.GetTotalNumBins(), Bins); |
| 18 | + EXPECT_FALSE(axis.HasFlowBins()); |
| 19 | +} |
| 20 | + |
| 21 | +TEST(RRegularAxis, Equality) |
| 22 | +{ |
| 23 | + static constexpr std::size_t Bins = 20; |
| 24 | + const RRegularAxis axisA(Bins, 0, Bins); |
| 25 | + const RRegularAxis axisANoFlowBins(Bins, 0, Bins, /*enableFlowBins=*/false); |
| 26 | + const RRegularAxis axisA2(Bins, 0, Bins); |
| 27 | + const RRegularAxis axisB(Bins / 2, 0, Bins); |
| 28 | + const RRegularAxis axisC(Bins, 0, Bins / 2); |
| 29 | + const RRegularAxis axisD(Bins, Bins / 2, Bins); |
| 30 | + |
| 31 | + EXPECT_TRUE(axisA == axisA); |
| 32 | + EXPECT_TRUE(axisA == axisA2); |
| 33 | + EXPECT_TRUE(axisA2 == axisA); |
| 34 | + |
| 35 | + EXPECT_FALSE(axisA == axisANoFlowBins); |
| 36 | + |
| 37 | + EXPECT_FALSE(axisA == axisB); |
| 38 | + EXPECT_FALSE(axisA == axisC); |
| 39 | + EXPECT_FALSE(axisA == axisD); |
| 40 | + |
| 41 | + EXPECT_FALSE(axisB == axisC); |
| 42 | + EXPECT_FALSE(axisB == axisD); |
| 43 | + |
| 44 | + EXPECT_FALSE(axisC == axisD); |
| 45 | + EXPECT_FALSE(axisD == axisC); |
| 46 | +} |
| 47 | + |
| 48 | +TEST(RRegularAxis, ComputeLinearizedIndex) |
| 49 | +{ |
| 50 | + static constexpr std::size_t Bins = 20; |
| 51 | + const RRegularAxis axis(Bins, 0, Bins); |
| 52 | + const RRegularAxis axisNoFlowBins(Bins, 0, Bins, /*enableFlowBins=*/false); |
| 53 | + |
| 54 | + // Underflow |
| 55 | + static constexpr double NegativeInfinity = -std::numeric_limits<double>::infinity(); |
| 56 | + static constexpr double UnderflowLarge = -static_cast<double>(Bins); |
| 57 | + static constexpr double UnderflowSmall = -0.1; |
| 58 | + for (double underflow : {NegativeInfinity, UnderflowLarge, UnderflowSmall}) { |
| 59 | + auto linIndex = axis.ComputeLinearizedIndex(underflow); |
| 60 | + EXPECT_EQ(linIndex.fIndex, Bins); |
| 61 | + EXPECT_TRUE(linIndex.fValid); |
| 62 | + linIndex = axisNoFlowBins.ComputeLinearizedIndex(underflow); |
| 63 | + EXPECT_EQ(linIndex.fIndex, Bins); |
| 64 | + EXPECT_FALSE(linIndex.fValid); |
| 65 | + } |
| 66 | + |
| 67 | + for (std::size_t i = 0; i < Bins; i++) { |
| 68 | + auto linIndex = axis.ComputeLinearizedIndex(i + 0.5); |
| 69 | + EXPECT_EQ(linIndex.fIndex, i); |
| 70 | + EXPECT_TRUE(linIndex.fValid); |
| 71 | + linIndex = axisNoFlowBins.ComputeLinearizedIndex(i + 0.5); |
| 72 | + EXPECT_EQ(linIndex.fIndex, i); |
| 73 | + EXPECT_TRUE(linIndex.fValid); |
| 74 | + } |
| 75 | + |
| 76 | + // Overflow |
| 77 | + static constexpr double PositiveInfinity = std::numeric_limits<double>::infinity(); |
| 78 | + static constexpr double NaN = std::numeric_limits<double>::quiet_NaN(); |
| 79 | + static constexpr double OverflowLarge = static_cast<double>(Bins * 2); |
| 80 | + static constexpr double OverflowSmall = Bins + 0.1; |
| 81 | + for (double overflow : {PositiveInfinity, NaN, OverflowLarge, OverflowSmall}) { |
| 82 | + auto linIndex = axis.ComputeLinearizedIndex(overflow); |
| 83 | + EXPECT_EQ(linIndex.fIndex, Bins + 1); |
| 84 | + EXPECT_TRUE(linIndex.fValid); |
| 85 | + linIndex = axisNoFlowBins.ComputeLinearizedIndex(overflow); |
| 86 | + EXPECT_EQ(linIndex.fIndex, Bins + 1); |
| 87 | + EXPECT_FALSE(linIndex.fValid); |
| 88 | + } |
| 89 | +} |
0 commit comments