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