From 9f4b4098f2531ce4beeb14410faae3bb2ca77de2 Mon Sep 17 00:00:00 2001 From: Zakhar Date: Wed, 25 Sep 2024 18:01:51 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=20unittest=20=D1=81=20=D0=B1=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D1=88=D0=B8=D0=BC=20=D0=BA=D0=BE=D0=BB=D0=B8=D1=87?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B2=D0=BE=D0=BC=20=D1=81=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B0=D0=B5=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 14 ++++++--- histogram_internal.h | 2 +- text.cpp | 1 + unittest.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) 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); +}