3. Записаны модульные тесты

master
VeretennikovMA 1 год назад
Сommit 0a7e18c978

6
.gitignore поставляемый

@ -0,0 +1,6 @@
/bin
/obj
/lab01.depend
/lab01.layout
/unittest.depend
/unittest.layout

Разница между файлами не показана из-за своего большого размера Загрузить разницу

@ -0,0 +1,44 @@
#include "histogram.h"
using namespace std;
void
find_minmax(const vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
}
vector<size_t>
make_histogram(const vector<double>& numbers, size_t bin_count){
size_t number_count = numbers.size();
vector<size_t> bins(bin_count);
double min, max;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
for (size_t i = 0; i < number_count; i++) {
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
auto lo = min + j * bin_size;
auto hi = min + (j + 1) * bin_size;
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}

@ -0,0 +1,5 @@
#pragma once
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);

@ -0,0 +1,5 @@
#pragma once
#include <vector>
void
find_minmax(const std::vector<double>& numbers, double& min, double& max);

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="lab01" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/lab01" 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/lab01" 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" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename=".gitignore" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="histogram_internal.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" />
<Unit filename="text.cpp" />
<Unit filename="text.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
struct Input {
vector<double> numbers;
size_t bin_count{};
};
Input
input_data() {
size_t number_count;
cerr << "Enter number count: ";
cin >> number_count;
Input in;
in.numbers.resize(number_count);
cerr << "Enter numbers: ";
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
cerr << "Enter bin count: ";
cin >> in.bin_count;
return in;
}
int
main() {
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
}

@ -0,0 +1,53 @@
#include "text.h"
#include <iostream>
using namespace std;
void
show_histogram_text(const vector<size_t>& bins){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
auto bin_count = bins.size();
size_t max_count = 0;
for (size_t i = 0; i < bin_count; i++){
size_t Count = bins[i];
if (max_count < Count){
max_count = Count;
}
}
if (max_count <= MAX_ASTERISK){
for (size_t i = 0; i < bin_count; i++){
size_t Count = bins[i];
if (Count < 100){
cout << " ";
}
if (Count < 10){
cout << " ";
}
cout << Count << "|";
for (size_t j = 0; j < Count; j++){
cout << "*";
}
cout << "\n";
}
}
else{
for (size_t i = 0; i < bin_count; i++){
size_t Count = bins[i];
size_t height = 76 * (static_cast<double>(Count) / max_count);
if (Count < 100){
cout << " ";
}
if (Count < 10){
cout << " ";
}
cout << Count << "|";
for (size_t j = 0; j < height; j++){
cout << "*";
}
cout << "\n";
}
}
}

@ -0,0 +1,5 @@
#pragma once
#include <vector>
void
show_histogram_text(const std::vector<size_t>& bins);

@ -0,0 +1,44 @@
<?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">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -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("blank vector") {
double min = 0;
double max = 0;
find_minmax({NULL}, min, max);
CHECK(min == 0);
CHECK(max == 0);
}
TEST_CASE("one element vector"){
double min = 0;
double max = 0;
find_minmax({5}, min, max);
CHECK(min == 5);
CHECK(max == 5);
}
TEST_CASE("negative elements vector"){
double min = 0;
double max = 0;
find_minmax({-3, -7, -24}, min, max);
CHECK(min == -24);
CHECK(max == -3);
}
TEST_CASE("same elements vector"){
double min = 0;
double max = 0;
find_minmax({8, 8, 8}, min, max);
CHECK(min == 8);
CHECK(max == 8);
}
Загрузка…
Отмена
Сохранить