Skip to content

Commit 97736dc

Browse files
committed
[hist] Check arguments in axis constructors
1 parent 62664b1 commit 97736dc

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

hist/histv7/inc/ROOT/RRegularAxis.hxx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@ class RRegularAxis final {
4444
public:
4545
/// Construct a regular axis object.
4646
///
47-
/// \param[in] numNormalBins the number of normal bins
47+
/// \param[in] numNormalBins the number of normal bins, must be > 0
4848
/// \param[in] low the lower end of the axis interval (inclusive)
49-
/// \param[in] high the upper end of the axis interval (exclusive)
49+
/// \param[in] high the upper end of the axis interval (exclusive), must be > low
5050
/// \param[in] enableFlowBins whether to enable underflow and overflow bins
5151
RRegularAxis(std::size_t numNormalBins, double low, double high, bool enableFlowBins = true)
5252
: fNumNormalBins(numNormalBins), fLow(low), fHigh(high), fEnableFlowBins(enableFlowBins)
5353
{
54-
// FIXME: should validate numNormalBins > 0 and low < high
54+
if (numNormalBins == 0) {
55+
throw std::invalid_argument("numNormalBins must be > 0");
56+
}
57+
if (low >= high) {
58+
throw std::invalid_argument("high must be > low");
59+
}
5560
fInvBinWidth = numNormalBins / (high - low);
5661
}
5762

hist/histv7/inc/ROOT/RVariableBinAxis.hxx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ public:
4646
RVariableBinAxis(std::vector<double> binEdges, bool enableFlowBins = true)
4747
: fBinEdges(std::move(binEdges)), fEnableFlowBins(enableFlowBins)
4848
{
49-
// FIXME: should validate that fBinEdges is sorted
49+
if (fBinEdges.size() < 2) {
50+
throw std::invalid_argument("must have >= 2 edges");
51+
}
52+
for (std::size_t i = 1; i < fBinEdges.size(); i++) {
53+
if (fBinEdges[i - 1] >= fBinEdges[i]) {
54+
throw std::invalid_argument("binEdges must be sorted");
55+
}
56+
}
5057
}
5158

5259
std::size_t GetNumNormalBins() const { return fBinEdges.size() - 1; }

hist/histv7/test/hist_regular.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ TEST(RRegularAxis, Constructor)
1616
EXPECT_EQ(axis.GetNumNormalBins(), Bins);
1717
EXPECT_EQ(axis.GetTotalNumBins(), Bins);
1818
EXPECT_FALSE(axis.HasFlowBins());
19+
20+
EXPECT_THROW(RRegularAxis(0, 0, Bins), std::invalid_argument);
21+
EXPECT_THROW(RRegularAxis(Bins, 1, 1), std::invalid_argument);
1922
}
2023

2124
TEST(RRegularAxis, Equality)

hist/histv7/test/hist_variable.cxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ TEST(RVariableBinAxis, Constructor)
2121
EXPECT_EQ(axis.GetNumNormalBins(), Bins);
2222
EXPECT_EQ(axis.GetTotalNumBins(), Bins);
2323
EXPECT_FALSE(axis.HasFlowBins());
24+
25+
EXPECT_THROW(RVariableBinAxis({}), std::invalid_argument);
26+
EXPECT_THROW(RVariableBinAxis({0}), std::invalid_argument);
27+
EXPECT_THROW(RVariableBinAxis({0, 0}), std::invalid_argument);
28+
EXPECT_THROW(RVariableBinAxis({0, 1, 0}), std::invalid_argument);
29+
EXPECT_THROW(RVariableBinAxis({0, 1, 1}), std::invalid_argument);
2430
}
2531

2632
TEST(RVariableBinAxis, Equality)

0 commit comments

Comments
 (0)