diff --git a/histogram.cpp b/histogram.cpp index 52c06d3..e3503d5 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -3,12 +3,18 @@ #include using namespace std; -void find_minmax( vector numbers, double &min, double &max) +bool find_minmax( vector numbers, double &min, double &max) { + if (numbers.empty()) + { + return false; + } + min = numbers[0]; max = numbers[0]; - for (double x : numbers) + for (double x : numbers) { + max = x; if (x < min) { min = x; @@ -18,7 +24,7 @@ void find_minmax( vector numbers, double &min, double &max) max = x; } } - return; + return true; } vector make_histogram (vector numbers, size_t bin_count) diff --git a/unittest.cpp b/unittest.cpp index 62f954d..083153a 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -2,6 +2,7 @@ #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "doctest.h" #include "histogram_internal.h" +#include "sr.h" TEST_CASE("distinct positive numbers") { @@ -38,3 +39,11 @@ TEST_CASE("vector with same elements") { CHECK(min == 2); CHECK(max == 2); } +TEST_CASE("Empty vector"){ + double min = 0; + double max = 0; + std::vector numbers {}; + CHECK(find_minmax(numbers, min, max) == false); + +} +