From b3e696cc70724876e8545113d6d627bdd553902d Mon Sep 17 00:00:00 2001 From: u113-02 Date: Wed, 24 Apr 2024 15:13:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=83=D0=BD=D0=BA=D1=82=205(=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D1=8B=20=D0=B8=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8=20find=5Fminmax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 14 +++++++---- histogram_internal.h | 2 +- main.cpp | 2 ++ text.cpp | 1 + unittest.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 70 insertions(+), 6 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 a4eddaa..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/main.cpp b/main.cpp index 7f736cf..b02a3dd 100644 --- a/main.cpp +++ b/main.cpp @@ -38,6 +38,8 @@ Input input_data() { int main(){ + bool res = false; + Input in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); 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); }