diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index 3929a57d8..5dc231bb3 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -1449,16 +1449,20 @@ namespace etl TCompare compare) { TIterator minimum = begin; - ++begin; - while (begin != end) + if (begin != end) { - if (compare(*begin, *minimum)) + ++begin; + + while (begin != end) { - minimum = begin; - } + if (compare(*begin, *minimum)) + { + minimum = begin; + } - ++begin; + ++begin; + } } return minimum; @@ -1493,16 +1497,20 @@ namespace etl TCompare compare) { TIterator maximum = begin; - ++begin; - while (begin != end) + if (begin != end) { - if (!compare(*begin, *maximum)) + ++begin; + + while (begin != end) { - maximum = begin; - } + if (!compare(*begin, *maximum)) + { + maximum = begin; + } - ++begin; + ++begin; + } } return maximum; @@ -1538,21 +1546,25 @@ namespace etl { TIterator minimum = begin; TIterator maximum = begin; - ++begin; - while (begin != end) + if (begin != end) { - if (compare(*begin, *minimum)) - { - minimum = begin; - } + ++begin; - if (compare(*maximum, *begin)) + while (begin != end) { - maximum = begin; - } + if (compare(*begin, *minimum)) + { + minimum = begin; + } - ++begin; + if (compare(*maximum, *begin)) + { + maximum = begin; + } + + ++begin; + } } return ETL_OR_STD::pair(minimum, maximum); diff --git a/test/test_algorithm.cpp b/test/test_algorithm.cpp index 43ff2379c..b1224614c 100644 --- a/test/test_algorithm.cpp +++ b/test/test_algorithm.cpp @@ -190,6 +190,15 @@ namespace CHECK_EQUAL(std::distance(data.begin(), expected), std::distance(data.begin(), result)); } + //************************************************************************* + TEST(min_element_empty) + { + Vector dataEmpty; + Vector::iterator expected = std::min_element(dataEmpty.begin(), dataEmpty.end()); + Vector::iterator result = etl::min_element(dataEmpty.begin(), dataEmpty.end()); + CHECK_EQUAL(std::distance(dataEmpty.end(), expected), std::distance(dataEmpty.end(), result)); + } + //************************************************************************* TEST(max_element) { @@ -206,6 +215,15 @@ namespace CHECK_EQUAL(std::distance(data.begin(), expected), std::distance(data.begin(), result)); } + //************************************************************************* + TEST(max_element_empty) + { + Vector dataEmpty; + Vector::iterator expected = std::max_element(dataEmpty.begin(), dataEmpty.end()); + Vector::iterator result = etl::max_element(dataEmpty.begin(), dataEmpty.end()); + CHECK_EQUAL(std::distance(dataEmpty.end(), expected), std::distance(dataEmpty.end(), result)); + } + //************************************************************************* TEST(minmax_element) { @@ -224,6 +242,16 @@ namespace CHECK_EQUAL(std::distance(data.begin(), expected.second), std::distance(data.begin(), result.second)); } + //************************************************************************* + TEST(minmax_element_empty) + { + Vector dataEmpty; + std::pair expected = std::minmax_element(dataEmpty.begin(), dataEmpty.end()); + std::pair result = etl::minmax_element(dataEmpty.begin(), dataEmpty.end()); + CHECK_EQUAL(std::distance(dataEmpty.begin(), expected.first), std::distance(dataEmpty.begin(), result.first)); + CHECK_EQUAL(std::distance(dataEmpty.begin(), expected.second), std::distance(dataEmpty.begin(), result.second)); + } + //************************************************************************* TEST(minmax) {