From 2c2e4e74491be9f610c9fbeedcfb6a060b84d895 Mon Sep 17 00:00:00 2001 From: OgarkovIA Date: Sun, 29 Sep 2024 11:38:19 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- svg.h | 24 +++++++++++++++++++++++ text.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ text.h | 12 ++++++++++++ unittest.cbp | 43 ++++++++++++++++++++++++++++++++++++++++ unittest.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 svg.h create mode 100644 text.cpp create mode 100644 text.h create mode 100644 unittest.cbp create mode 100644 unittest.cpp diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..66a0f20 --- /dev/null +++ b/svg.h @@ -0,0 +1,24 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED +#include +#include + +void +svg_begin(double width, double height); + +void +svg_end(); + +void +show_histogram_svg(const std::vector& bins); + +void +svg_text(double left, double baseline, std::string text); + +std::string +bin_color(size_t bin, size_t max_count); + +void +svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fil = "black"); + +#endif // SVG_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..1a3dbc2 --- /dev/null +++ b/text.cpp @@ -0,0 +1,55 @@ +#include "text.h" +#include + +void +show_histogram_text(std::vector& bins){ + + size_t max_count = 0; + for (size_t x: bins) { + if (x > max_count) { + max_count = x; + } + } + + if (max_count > MAX_ASTERISK) { + for (size_t count: bins) { + size_t height = MAX_ASTERISK * (static_cast(count) / max_count); + if (count < 10) { + std::cout << " " << count << "|"; + } + else if (count >= 100) { + std::cout << count << "|"; + } + + else { + std::cout << " " << count << "|"; + } + + for (size_t i = 0; i < height; i++) { + std::cout << "*"; + } + std::cout << "\n"; + } + } + + else { + for (size_t x: bins) { + if (x < 10) { + std::cout << " " << x << "|"; + } + else if (x >= 100) { + std::cout << x << "|"; + } + + else { + std::cout << " " << x << "|"; + } + + for (size_t i = 0; i < x; i++) { + std::cout << "*"; + } + std::cout << "\n"; + } + } + +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..cf43159 --- /dev/null +++ b/text.h @@ -0,0 +1,12 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +#include + +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +void +show_histogram_text(std::vector& bins); + +#endif // TEXT_H_INCLUDED diff --git a/unittest.cbp b/unittest.cbp new file mode 100644 index 0000000..9085664 --- /dev/null +++ b/unittest.cbp @@ -0,0 +1,43 @@ + + + + + + diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..d7cb394 --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,55 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" + +using namespace std; + +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("one element vector") { + double min = 0; + double max = 0; + find_minmax({1}, min, max); + CHECK(min == max); +} + +TEST_CASE("distinct negative numbers") { + double min = 0; + double max = 0; + find_minmax({-1,-2}, min, max); + CHECK(min == -2); + CHECK(max == -1); +} + + +TEST_CASE("identical elements vector") { + double min = 0; + double max = 0; + find_minmax({3,3,3,3}, min, max); + CHECK(min == 3); + CHECK(max == 3); +} + +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + bool result = find_minmax({}, min, max); + CHECK(result == false); +} + +TEST_CASE("bin color") { + size_t max_bin = 10; + size_t min_bin = 3; + string rat = "#" + to_string(10 - (min_bin * 9) / max_bin) + to_string(10 - (min_bin * 9) / max_bin) + to_string(10 - (min_bin * 9) / max_bin); + auto test1 = bin_color(max_bin,max_bin); + auto test2 = bin_color(min_bin,max_bin); + CHECK(test1 == "#111"); + CHECK(test2 == rat); +}