diff --git a/histogram.cpp b/histogram.cpp index 41fad77..498dc66 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,7 +1,13 @@ #include "histogram.h" #include -void find_minmax(const std::vector &numbers, double &min, double &max){ +void find_minmax(const std::vector &numbers, double &min, double &max, bool &res ){ + + if (numbers.size()==0){ + res = true; + return; + } + min = numbers[0]; max = numbers[0]; for ( double x : numbers ){ @@ -14,9 +20,9 @@ void find_minmax(const std::vector &numbers, double &min, double &max){ } std::vector make_histogram(const std::vector &numbers, std::size_t bin_count) { - double min = numbers[0]; - double max = numbers[0]; - find_minmax(numbers, min, max); + double min, max; + bool res = false; + find_minmax(numbers, min, max, res); double bin_size = ( max - min ) / bin_count; std::vector bins ( bin_count ); for (std::size_t i=0; i < numbers.size(); i++ ){ diff --git a/histogram_internal.h b/histogram_internal.h index 616921d..44594cb 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -3,6 +3,6 @@ #include -void find_minmax(const std::vector &numbers, double &min, double &max) +void find_minmax(const std::vector &numbers, double &min, double &max, bool &res); #endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/text.cpp b/text.cpp index 1ca3e67..50c04f0 100644 --- a/text.cpp +++ b/text.cpp @@ -6,6 +6,7 @@ const std::size_t SCREEN_WIDTH = 80; const std::size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; void show_histogram_text(const std::vector &bins){ + std::cout< numbers; + find_minmax(numbers, min, max, res); + CHECK(min == 0); + CHECK(max == 0); + CHECK(res==1); +} + +TEST_CASE("1 number") { + bool res = false; + double min = 0; + double max = 0; + find_minmax({-1}, min, max, res); + CHECK(min == -1); + CHECK(max == -1); + CHECK(res==0); +} + +TEST_CASE("same number") { + bool res = false; + double min = 0; + double max = 0; + find_minmax({1,1,1,1}, min, max, res); + CHECK(min == 1); + CHECK(max == 1); + CHECK(res==0); +} + +TEST_CASE("same number but zero") { + bool res = false; + double min = 0; + double max = 0; + find_minmax({0,0,0,0}, min, max, res); + CHECK(min == 0); + CHECK(max == 0); + CHECK(res==0); +}