diff --git a/main.cpp b/main.cpp index e904c60..6c9aa03 100644 --- a/main.cpp +++ b/main.cpp @@ -10,25 +10,31 @@ struct Input { size_t bin_count; }; -Input input_data(istream& in) { +Input input_data(istream& in, bool prompt) { Input inp; size_t number_count; - cerr << "Enter number count: "; + if (prompt){ + cerr << "Enter number count: "; + } cin >> number_count; inp.numbers.resize(number_count); - cerr << "Enter numbers: "; + if (prompt){ + cerr << "Enter numbers: "; + } for (size_t i = 0; i < number_count; i++) { cin >> inp.numbers[i]; } - cerr << "Enter bin count: "; + if (prompt){ + cerr << "Enter bin count: "; + } cin >> inp.bin_count; return inp; } int main() { - auto in = input_data(cin); + auto in = input_data(cin, true); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_svg(bins); return 0; diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..bf37c5e --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,44 @@ +#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); +} + + +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("vector of the same elements"){ + double min = 0; + double max = 0; + find_minmax({3,3,3}, min, max); + CHECK(min == 3); + CHECK(max == 3); +} + +TEST_CASE("single element vector") { + double min = 0; + double max = 0; + find_minmax({7}, min, max); + CHECK(min == 7); + CHECK(max == 7); +} + +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + find_minmax({}, min, max); + CHECK(min == 0); + CHECK(max == 0); +}