code: добавлен unittest и подгружены недостающие файлы

main
Dmitry (KolomeytsevDA) 12 месяцев назад
Родитель 5f528767f2
Сommit 0a815d481d

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

@ -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

@ -0,0 +1,12 @@
#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);
}
Загрузка…
Отмена
Сохранить