Catch nullptr in STATS::pile_count (fix isse #3694)

Add also a test case for this issue.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2021-12-29 17:15:31 +01:00
parent 7277963e11
commit d754593a31
2 changed files with 10 additions and 0 deletions

View File

@ -73,6 +73,9 @@ public:
double median() const; // get median of samples
// Returns the count of the given value.
int32_t pile_count(int32_t value) const {
if (buckets_ == nullptr) {
return 0;
}
if (value <= rangemin_) {
return buckets_[0];
}

View File

@ -40,6 +40,13 @@ TEST_F(STATSTest, BasicStats) {
EXPECT_EQ(12, stats_.pile_count(2));
}
TEST_F(STATSTest, InitStats) {
STATS stats;
EXPECT_EQ(0, stats.get_total());
EXPECT_EQ(0, stats.mode());
EXPECT_EQ(0, stats.pile_count(2));
}
// Tests the top_n_modes function.
TEST_F(STATSTest, TopNModes) {
std::vector<tesseract::KDPairInc<float, int> > modes;