code: проверка на нулевой массив

main
Станислав Князев 2 лет назад
Родитель 3661762f5a
Сommit c6251914a5

@ -3,12 +3,18 @@
#include <vector> #include <vector>
using namespace std; using namespace std;
void find_minmax( vector<double> numbers, double &min, double &max) bool find_minmax( vector<double> numbers, double &min, double &max)
{ {
if (numbers.empty())
{
return false;
}
min = numbers[0]; min = numbers[0];
max = numbers[0]; max = numbers[0];
for (double x : numbers) for (double x : numbers)
{ {
max = x;
if (x < min) if (x < min)
{ {
min = x; min = x;
@ -18,7 +24,7 @@ void find_minmax( vector<double> numbers, double &min, double &max)
max = x; max = x;
} }
} }
return; return true;
} }
vector<size_t> make_histogram (vector<double> numbers, size_t bin_count) vector<size_t> make_histogram (vector<double> numbers, size_t bin_count)

@ -2,6 +2,7 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h" #include "doctest.h"
#include "histogram_internal.h" #include "histogram_internal.h"
#include "sr.h"
TEST_CASE("distinct positive numbers") { TEST_CASE("distinct positive numbers") {
@ -38,3 +39,11 @@ TEST_CASE("vector with same elements") {
CHECK(min == 2); CHECK(min == 2);
CHECK(max == 2); CHECK(max == 2);
} }
TEST_CASE("Empty vector"){
double min = 0;
double max = 0;
std::vector<double> numbers {};
CHECK(find_minmax(numbers, min, max) == false);
}

Загрузка…
Отмена
Сохранить