Skip to content

Commit

Permalink
Merge 55191f5 into d2c9d38
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Mar 29, 2022
2 parents d2c9d38 + 55191f5 commit 026f9dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions core/src/histogram.cc
@@ -1,8 +1,8 @@
#include "prometheus/histogram.h"

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <functional>
#include <iterator>
#include <limits>
#include <memory>
Expand All @@ -13,8 +13,10 @@ namespace prometheus {

Histogram::Histogram(const BucketBoundaries& buckets)
: bucket_boundaries_{buckets}, bucket_counts_{buckets.size() + 1}, sum_{} {
assert(std::is_sorted(std::begin(bucket_boundaries_),
std::end(bucket_boundaries_)));
if (!std::is_sorted(begin(bucket_boundaries_), end(bucket_boundaries_),
std::less_equal<BucketBoundaries::value_type>())) {
throw std::invalid_argument("Bucket Boundaries must be incrementing");
}
}

void Histogram::Observe(const double value) {
Expand Down
8 changes: 8 additions & 0 deletions core/tests/histogram_test.cc
Expand Up @@ -53,6 +53,14 @@ TEST(HistogramTest, bucket_bounds) {
std::numeric_limits<double>::infinity());
}

TEST(HistogramTest, reject_unsorted_bucket_bounds) {
EXPECT_ANY_THROW(Histogram({2, 1}));
}

TEST(HistogramTest, reject_non_incrementing_bucket_bounds) {
EXPECT_ANY_THROW(Histogram({1, 2, 2, 3}));
}

TEST(HistogramTest, bucket_counts_not_reset_by_collection) {
Histogram histogram{{1, 2}};
histogram.Observe(1.5);
Expand Down

0 comments on commit 026f9dd

Please sign in to comment.