Skip to content

Commit e90f573

Browse files
committed
[hist] Check arguments in axis constructors
1 parent b5cd4fc commit e90f573

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

hist/histv7/inc/ROOT/RRegularAxis.hxx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <cstddef>
1111
#include <stdexcept>
12+
#include <string>
1213

1314
class TBuffer;
1415

@@ -44,14 +45,20 @@ class RRegularAxis final {
4445
public:
4546
/// Construct a regular axis object.
4647
///
47-
/// \param[in] numNormalBins the number of normal bins
48+
/// \param[in] numNormalBins the number of normal bins, must be > 0
4849
/// \param[in] low the lower end of the axis interval (inclusive)
49-
/// \param[in] high the upper end of the axis interval (exclusive)
50+
/// \param[in] high the upper end of the axis interval (exclusive), must be > low
5051
/// \param[in] enableFlowBins whether to enable underflow and overflow bins
5152
RRegularAxis(std::size_t numNormalBins, double low, double high, bool enableFlowBins = true)
5253
: fNumNormalBins(numNormalBins), fLow(low), fHigh(high), fEnableFlowBins(enableFlowBins)
5354
{
54-
// FIXME: should validate numNormalBins > 0 and low < high
55+
if (numNormalBins == 0) {
56+
throw std::invalid_argument("numNormalBins must be > 0");
57+
}
58+
if (low >= high) {
59+
std::string msg = "high must be > low, but " + std::to_string(low) + " >= " + std::to_string(high);
60+
throw std::invalid_argument(msg);
61+
}
5562
fInvBinWidth = numNormalBins / (high - low);
5663
}
5764

hist/histv7/inc/ROOT/RVariableBinAxis.hxx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <cstddef>
1111
#include <stdexcept>
12+
#include <string>
1213
#include <utility>
1314
#include <vector>
1415

@@ -41,12 +42,21 @@ class RVariableBinAxis final {
4142
public:
4243
/// Construct an axis object with variable bins.
4344
///
44-
/// \param[in] binEdges the (ordered) edges of the normal bins
45+
/// \param[in] binEdges the (ordered) edges of the normal bins, must define at least one bin (i.e. size >= 2)
4546
/// \param[in] enableFlowBins whether to enable underflow and overflow bins
4647
RVariableBinAxis(std::vector<double> binEdges, bool enableFlowBins = true)
4748
: fBinEdges(std::move(binEdges)), fEnableFlowBins(enableFlowBins)
4849
{
49-
// FIXME: should validate that fBinEdges is sorted
50+
if (fBinEdges.size() < 2) {
51+
throw std::invalid_argument("must have >= 2 bin edges");
52+
}
53+
for (std::size_t i = 1; i < fBinEdges.size(); i++) {
54+
if (fBinEdges[i - 1] >= fBinEdges[i]) {
55+
std::string msg = "binEdges must be sorted, but for bin " + std::to_string(i - 1) + ": ";
56+
msg += std::to_string(fBinEdges[i - 1]) + " >= " + std::to_string(fBinEdges[i]);
57+
throw std::invalid_argument(msg);
58+
}
59+
}
5060
}
5161

5262
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)