code: модульные тесты; улучшены функции расчета и печати гистограммы для случая с пустым вектором
Этот коммит содержится в:
6
Lab1.cbp
6
Lab1.cbp
@@ -32,8 +32,14 @@
|
|||||||
<Add option="-Wall" />
|
<Add option="-Wall" />
|
||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
|
<Unit filename=".gitignore" />
|
||||||
|
<Unit filename="histogram.cpp" />
|
||||||
|
<Unit filename="histogram.h" />
|
||||||
|
<Unit filename="histogram_internal.h" />
|
||||||
<Unit filename="main.cpp" />
|
<Unit filename="main.cpp" />
|
||||||
<Unit filename="main.h" />
|
<Unit filename="main.h" />
|
||||||
|
<Unit filename="text.cpp" />
|
||||||
|
<Unit filename="text.h" />
|
||||||
<Extensions />
|
<Extensions />
|
||||||
</Project>
|
</Project>
|
||||||
</CodeBlocks_project_file>
|
</CodeBlocks_project_file>
|
||||||
|
|||||||
@@ -2,16 +2,28 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
static void
|
void
|
||||||
find_minmax(const vector<double>& numbers, double& min, double& max)
|
find_minmax(const vector<double>& numbers, double& min, double& max, bool& empty_vector)
|
||||||
|
{
|
||||||
|
if (numbers.empty())
|
||||||
|
{
|
||||||
|
empty_vector = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
min = numbers[0];
|
min = numbers[0];
|
||||||
max = numbers[0];
|
max = numbers[0];
|
||||||
// (çäåñü êîä ïîèñêà ìèíèìóìà è ìàêñèìóìà)
|
for (double x : numbers)
|
||||||
for (double value : numbers)
|
|
||||||
{
|
{
|
||||||
if (value < min) {min = value;}
|
if (x > max )
|
||||||
else if (value > max) {max = value;}
|
{
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
if (x < min)
|
||||||
|
{
|
||||||
|
min = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,10 +31,22 @@ vector<size_t>
|
|||||||
make_histogram(const vector<double>& numbers, size_t& bin_count)
|
make_histogram(const vector<double>& numbers, size_t& bin_count)
|
||||||
{
|
{
|
||||||
double min, max;
|
double min, max;
|
||||||
find_minmax(numbers, min, max);
|
bool empty_vector = false;
|
||||||
|
find_minmax(numbers, min, max, empty_vector);
|
||||||
|
|
||||||
|
|
||||||
double bin_size = (max - min) / bin_count;
|
double bin_size = (max - min) / bin_count;
|
||||||
vector<size_t> bins(bin_count);
|
vector<size_t> bins(bin_count);
|
||||||
|
|
||||||
|
if (empty_vector)
|
||||||
|
{
|
||||||
|
for (size_t y : bins)
|
||||||
|
{
|
||||||
|
y = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for (size_t i = 0; i < numbers.size(); i++)
|
for (size_t i = 0; i < numbers.size(); i++)
|
||||||
{
|
{
|
||||||
for (size_t j = i + 1; j < numbers.size(); j++)
|
for (size_t j = i + 1; j < numbers.size(); j++)
|
||||||
|
|||||||
9
histogram_internal.h
Обычный файл
9
histogram_internal.h
Обычный файл
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const std::vector<double>& numbers, double& min, double& max, bool& empty_vector);
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
5
text.cpp
5
text.cpp
@@ -17,6 +17,11 @@ show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (max_count == 0)
|
||||||
|
{
|
||||||
|
max_count = 1;
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t bin : bins)
|
for (size_t bin : bins)
|
||||||
{
|
{
|
||||||
if (bin < 100) cout << " ";
|
if (bin < 100) cout << " ";
|
||||||
|
|||||||
54
unittest.cpp
Обычный файл
54
unittest.cpp
Обычный файл
@@ -0,0 +1,54 @@
|
|||||||
|
#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;
|
||||||
|
bool empty_vector = false;
|
||||||
|
find_minmax({1, 2}, min, max, empty_vector);
|
||||||
|
CHECK(empty_vector == false);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("single number") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
bool empty_vector = false;
|
||||||
|
find_minmax({1}, min, max, empty_vector);
|
||||||
|
CHECK(empty_vector == false);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("negative numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
bool empty_vector = false;
|
||||||
|
find_minmax({-1, -2}, min, max, empty_vector);
|
||||||
|
CHECK(empty_vector == false);
|
||||||
|
CHECK(min == -2);
|
||||||
|
CHECK(max == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("identical numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
bool empty_vector = false;
|
||||||
|
find_minmax({2, 2}, min, max, empty_vector);
|
||||||
|
CHECK(empty_vector == false);
|
||||||
|
CHECK(min == 2);
|
||||||
|
CHECK(max == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("empty vector") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
bool empty_vector = false;
|
||||||
|
find_minmax({}, min, max, empty_vector);
|
||||||
|
CHECK(empty_vector == true);
|
||||||
|
CHECK(min == 0);
|
||||||
|
CHECK(max == 0);
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user