From d5dded25e25b63fa2c1c86b2bcc970a26fc4259b Mon Sep 17 00:00:00 2001 From: ShevchukDS <ShevchukDS@mpei.ru> Date: Mon, 2 Jun 2025 00:24:58 +0300 Subject: [PATCH] =?UTF-8?q?4.=20=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +++ histogram.cpp | 4 ++-- histogram_internal.h | 8 ++++++++ lab01.cbp | 12 ++++++++++++ unittest.cbp | 38 ++++++++++++++++++++++++++++++++++++++ unittest.cpp | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 histogram_internal.h create mode 100644 unittest.cbp create mode 100644 unittest.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d85abef --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/bin +/obj +/*.layout diff --git a/histogram.cpp b/histogram.cpp index a03bed2..d099d32 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,10 +1,10 @@ #include "histogram.h" +#include "histogram_internal.h" #include <vector> using namespace std; -static void find_minmax(const vector<double>& numbers, double& min, double& max) { +void find_minmax(const vector<double>& numbers, double& min, double& max) { if (numbers.empty()) return; - min = numbers[0]; max = numbers[0]; for (double x : numbers) { diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..430454e --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED + +#include <vector> + +void find_minmax(const std::vector<double>& numbers, double& min, double& max); + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/lab01.cbp b/lab01.cbp index d541ad7..bad2e84 100644 --- a/lab01.cbp +++ b/lab01.cbp @@ -32,7 +32,19 @@ <Add option="-Wall" /> <Add option="-fexceptions" /> </Compiler> + <Unit filename=".gitignore" /> + <Unit filename="histogram.cpp" /> + <Unit filename="histogram.h"> + <Option target="<{~None~}>" /> + </Unit> + <Unit filename="histogram_internal.h"> + <Option target="<{~None~}>" /> + </Unit> <Unit filename="main.cpp" /> + <Unit filename="text.cpp" /> + <Unit filename="text.h"> + <Option target="<{~None~}>" /> + </Unit> <Extensions> <lib_finder disable_auto="1" /> </Extensions> diff --git a/unittest.cbp b/unittest.cbp new file mode 100644 index 0000000..7f9621b --- /dev/null +++ b/unittest.cbp @@ -0,0 +1,38 @@ +<?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> + <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..51863c8 --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,36 @@ +#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("identical numbers") { + double min = 0; + double max = 0; + find_minmax({5, 5}, min, max); + CHECK(min == 5); + CHECK(max == 5); +} + +TEST_CASE("single element") { + double min = 0; + double max = 0; + find_minmax({-3}, min, max); + CHECK(min == -3); + CHECK(max == -3); +} + +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + find_minmax({}, min, max); + CHECK(min == 0); + CHECK(max == 0); +}