code: реализован unittest с большим количеством случаев
Этот коммит содержится в:
@@ -1,7 +1,13 @@
|
|||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
void find_minmax(const std::vector <double> &numbers, double &min, double &max){
|
void find_minmax(const std::vector <double> &numbers, double &min, double &max, bool &res ){
|
||||||
|
|
||||||
|
if (numbers.size()==0){
|
||||||
|
res = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
min = numbers[0];
|
min = numbers[0];
|
||||||
max = numbers[0];
|
max = numbers[0];
|
||||||
for ( double x : numbers ){
|
for ( double x : numbers ){
|
||||||
@@ -14,9 +20,9 @@ void find_minmax(const std::vector <double> &numbers, double &min, double &max){
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::size_t> make_histogram(const std::vector<double> &numbers, std::size_t bin_count) {
|
std::vector<std::size_t> make_histogram(const std::vector<double> &numbers, std::size_t bin_count) {
|
||||||
double min = numbers[0];
|
double min, max;
|
||||||
double max = numbers[0];
|
bool res = false;
|
||||||
find_minmax(numbers, min, max);
|
find_minmax(numbers, min, max, res);
|
||||||
double bin_size = ( max - min ) / bin_count;
|
double bin_size = ( max - min ) / bin_count;
|
||||||
std::vector<std::size_t> bins ( bin_count );
|
std::vector<std::size_t> bins ( bin_count );
|
||||||
for (std::size_t i=0; i < numbers.size(); i++ ){
|
for (std::size_t i=0; i < numbers.size(); i++ ){
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
void find_minmax(const std::vector <double> &numbers, double &min, double &max)
|
void find_minmax(const std::vector <double> &numbers, double &min, double &max, bool &res);
|
||||||
|
|
||||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
|||||||
1
text.cpp
1
text.cpp
@@ -6,6 +6,7 @@ const std::size_t SCREEN_WIDTH = 80;
|
|||||||
const std::size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
const std::size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
void show_histogram_text(const std::vector<std::size_t> &bins){
|
void show_histogram_text(const std::vector<std::size_t> &bins){
|
||||||
|
std::cout<<std::endl;
|
||||||
std::size_t maxbin = bins[0];
|
std::size_t maxbin = bins[0];
|
||||||
for (std::size_t i=1; i < bins.size(); i++){
|
for (std::size_t i=1; i < bins.size(); i++){
|
||||||
if (maxbin < bins[i]){
|
if (maxbin < bins[i]){
|
||||||
|
|||||||
67
unittest.cpp
67
unittest.cpp
@@ -0,0 +1,67 @@
|
|||||||
|
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||||
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
|
#include "doctest.h"
|
||||||
|
#include "histogram_internal.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
bool res = false;
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1, 2}, min, max, res);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 2);
|
||||||
|
CHECK(res==0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("negative numbers") {
|
||||||
|
bool res = false;
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({-1, -2}, min, max, res);
|
||||||
|
CHECK(min == -2);
|
||||||
|
CHECK(max == -1);
|
||||||
|
CHECK(res==0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("empty array") {
|
||||||
|
bool res = false;
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
std::vector<double> numbers;
|
||||||
|
find_minmax(numbers, min, max, res);
|
||||||
|
CHECK(min == 0);
|
||||||
|
CHECK(max == 0);
|
||||||
|
CHECK(res==1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("1 number") {
|
||||||
|
bool res = false;
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({-1}, min, max, res);
|
||||||
|
CHECK(min == -1);
|
||||||
|
CHECK(max == -1);
|
||||||
|
CHECK(res==0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("same number") {
|
||||||
|
bool res = false;
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1,1,1,1}, min, max, res);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 1);
|
||||||
|
CHECK(res==0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("same number but zero") {
|
||||||
|
bool res = false;
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({0,0,0,0}, min, max, res);
|
||||||
|
CHECK(min == 0);
|
||||||
|
CHECK(max == 0);
|
||||||
|
CHECK(res==0);
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user