From 0a815d481d9f1aef4919f1c2dcbe3452e50ad746 Mon Sep 17 00:00:00 2001 From: "Dmitry (KolomeytsevDA)" Date: Mon, 6 May 2024 19:32:46 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20unittest=20=D0=B8=20=D0=BF=D0=BE=D0=B4=D0=B3?= =?UTF-8?q?=D1=80=D1=83=D0=B6=D0=B5=D0=BD=D1=8B=20=D0=BD=D0=B5=D0=B4=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D1=8E=D1=89=D0=B8=D0=B5=20=D1=84=D0=B0=D0=B9?= =?UTF-8?q?=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 47 ++++++++++++++------------------------------ histogram_internal.h | 8 ++++++++ unittest.cpp | 12 +++++++++++ 3 files changed, 35 insertions(+), 32 deletions(-) create mode 100644 histogram_internal.h create mode 100644 unittest.cpp diff --git a/histogram.cpp b/histogram.cpp index bac09a3..3372e75 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -2,53 +2,36 @@ #include #include "histogram.h" -void find_minmax(const std::vector &numbers, double &min, double &max) -{ - //min = numbers[0]; - //max = numbers[0]; - for (double x : numbers) - { - - if (x < min) - { +void find_minmax(const std::vector &numbers, double &min, double &max){ + min = numbers[0]; + max = numbers[0]; + for ( double x : numbers ){ + if ( x < min ){ min = x; - } - - else if (x > max) - { + } else if ( x > max ){ max = x; } } } -std::vector make_histogram(const std::vector& numbers, size_t bin_count) -{ - std::vector bins(bin_count); - int max_count = bins[0]; +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 bin_size = (max - min) / bin_count; - - for (size_t i = 0; i < numbers.size(); i++) - { + double bin_size = ( max - min ) / bin_count; + std::vector bins ( bin_count ); + for (std::size_t i=0; i < numbers.size(); i++ ){ bool found = false; - for (size_t j = 0; (j < bin_count - 1) && !found; j++) - { + for (std::size_t j = 0; ( j < bin_count - 1 ) && !found; j++ ){ auto lo = min + j * bin_size; - auto hi = min + (j + 1) * bin_size; - - if ((lo <= numbers[i]) && (numbers[i] < hi)) - { + auto hi = min + ( j + 1 ) * bin_size; + if (lo <= numbers[i] && ( numbers[i] < hi )){ bins[j]++; found = true; } } - - if (!found) - { - bins[bin_count - 1]++; + if ( !found ){ + bins[bin_count-1]++; } } return bins; diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..2b43395 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED + +#include + +void find_minmax(const std::vector &numbers, double &min, double &max) + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED \ No newline at end of file diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..a9ba7e5 --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,12 @@ +#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); +}