diff --git a/histogram.cpp b/histogram.cpp index 9eec184..eb2de5e 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -5,17 +5,20 @@ using namespace std; void find_minmax(const vector& numbers, double& min, double& max) { - min = numbers[0]; - max = numbers[0]; - for (double x : numbers) + if (numbers.size() == 2) { - if (x < min) + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) { - min = x; - } - if (x > max) - { - max = x; + if (x < min) + { + min = x; + } + if (x > max) + { + max = x; + } } } } diff --git a/unittest.cpp b/unittest.cpp index e69de29..485d693 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -0,0 +1,27 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" + +TEST_CASE("distinct positive numbers") { + double min = 0; + double max = 0; + find_minmax({ 1, 2 }, min, max); + CHECK(min == 1); + CHECK(max == 2); +} + +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + find_minmax({}, min, max); + CHECK(min == 0); + CHECK(max == 0); +} +TEST_CASE("negative values") { + double min = 0; + double max = 0; + find_minmax({-2, -4}, min, max); + CHECK(min == -4); + CHECK(max == -2); +} \ No newline at end of file diff --git a/unittest.vcxproj b/unittest.vcxproj index aee391f..3e22914 100644 --- a/unittest.vcxproj +++ b/unittest.vcxproj @@ -17,7 +17,6 @@ Release x64 - 17.0 @@ -53,27 +52,24 @@ true Unicode - - + + + + + + + + + + + + + - - - - - - - - - - - - - - Level3 @@ -130,9 +126,15 @@ true - - + + + + + + + + - + \ No newline at end of file