diff --git a/histogram.cpp b/histogram.cpp index 5d85960..641db08 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -3,8 +3,14 @@ using namespace std; void find_minmax(const vector& numbers, double& min, double& max) { + if (numbers.empty()) + { + min = 0; + max = 0; + return; + } max = numbers[0]; - min = numbers[1]; + min = numbers[0]; for (double x : numbers) { if (x < min) min = x; else if (x > max) max = x; diff --git a/unittest.cpp b/unittest.cpp index a9ba7e5..bf37c5e 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -10,3 +10,35 @@ TEST_CASE("distinct positive numbers") { CHECK(min == 1); CHECK(max == 2); } + + +TEST_CASE("distinct negative numbers"){ + double min = 0; + double max = 0; + find_minmax({-1, -2}, min, max); + CHECK(min == -2); + CHECK(max == -1); +} +TEST_CASE("vector of the same elements"){ + double min = 0; + double max = 0; + find_minmax({3,3,3}, min, max); + CHECK(min == 3); + CHECK(max == 3); +} + +TEST_CASE("single element vector") { + double min = 0; + double max = 0; + find_minmax({7}, min, max); + CHECK(min == 7); + CHECK(max == 7); +} + +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + find_minmax({}, min, max); + CHECK(min == 0); + CHECK(max == 0); +}