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 <string> +#include <vector> + +void +svg_begin(double width, double height); + +void +svg_end(); + +void +show_histogram_svg(const std::vector<size_t>& 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 <iostream> + +void +show_histogram_text(std::vector<size_t>& 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<double>(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 <vector> + +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +void +show_histogram_text(std::vector<size_t>& 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 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> +<CodeBlocks_project_file> + <FileVersion major="1" minor="6" /> + <Project> + <Option title="unittest" /> + <Option pch_mode="2" /> + <Option compiler="gcc" /> + <Build> + <Target title="Debug"> + <Option output="bin/Debug/unittest" prefix_auto="1" extension_auto="1" /> + <Option object_output="obj/Debug/" /> + <Option type="1" /> + <Option compiler="gcc" /> + <Compiler> + <Add option="-g" /> + </Compiler> + </Target> + <Target title="Release"> + <Option output="bin/Release/unittest" prefix_auto="1" extension_auto="1" /> + <Option object_output="obj/Release/" /> + <Option type="1" /> + <Option compiler="gcc" /> + <Compiler> + <Add option="-O2" /> + </Compiler> + <Linker> + <Add option="-s" /> + </Linker> + </Target> + </Build> + <Compiler> + <Add option="-Wall" /> + </Compiler> + <Unit filename="doctest.h" /> + <Unit filename="histogram.cpp" /> + <Unit filename="histogram_internal.h" /> + <Unit filename="svg.cpp" /> + <Unit filename="unittest.cpp" /> + <Extensions> + <lib_finder disable_auto="1" /> + </Extensions> + </Project> +</CodeBlocks_project_file> 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); +}