Skip to content

Commit b34e3a6

Browse files
#1017 Fix empty range handling of min/max_element (#1018)
1 parent 4cbef7f commit b34e3a6

File tree

2 files changed

+62
-22
lines changed

2 files changed

+62
-22
lines changed

include/etl/algorithm.h

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,16 +1449,20 @@ namespace etl
14491449
TCompare compare)
14501450
{
14511451
TIterator minimum = begin;
1452-
++begin;
14531452

1454-
while (begin != end)
1453+
if (begin != end)
14551454
{
1456-
if (compare(*begin, *minimum))
1455+
++begin;
1456+
1457+
while (begin != end)
14571458
{
1458-
minimum = begin;
1459-
}
1459+
if (compare(*begin, *minimum))
1460+
{
1461+
minimum = begin;
1462+
}
14601463

1461-
++begin;
1464+
++begin;
1465+
}
14621466
}
14631467

14641468
return minimum;
@@ -1493,16 +1497,20 @@ namespace etl
14931497
TCompare compare)
14941498
{
14951499
TIterator maximum = begin;
1496-
++begin;
14971500

1498-
while (begin != end)
1501+
if (begin != end)
14991502
{
1500-
if (!compare(*begin, *maximum))
1503+
++begin;
1504+
1505+
while (begin != end)
15011506
{
1502-
maximum = begin;
1503-
}
1507+
if (!compare(*begin, *maximum))
1508+
{
1509+
maximum = begin;
1510+
}
15041511

1505-
++begin;
1512+
++begin;
1513+
}
15061514
}
15071515

15081516
return maximum;
@@ -1538,21 +1546,25 @@ namespace etl
15381546
{
15391547
TIterator minimum = begin;
15401548
TIterator maximum = begin;
1541-
++begin;
15421549

1543-
while (begin != end)
1550+
if (begin != end)
15441551
{
1545-
if (compare(*begin, *minimum))
1546-
{
1547-
minimum = begin;
1548-
}
1552+
++begin;
15491553

1550-
if (compare(*maximum, *begin))
1554+
while (begin != end)
15511555
{
1552-
maximum = begin;
1553-
}
1556+
if (compare(*begin, *minimum))
1557+
{
1558+
minimum = begin;
1559+
}
15541560

1555-
++begin;
1561+
if (compare(*maximum, *begin))
1562+
{
1563+
maximum = begin;
1564+
}
1565+
1566+
++begin;
1567+
}
15561568
}
15571569

15581570
return ETL_OR_STD::pair<TIterator, TIterator>(minimum, maximum);

test/test_algorithm.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ namespace
190190
CHECK_EQUAL(std::distance(data.begin(), expected), std::distance(data.begin(), result));
191191
}
192192

193+
//*************************************************************************
194+
TEST(min_element_empty)
195+
{
196+
Vector dataEmpty;
197+
Vector::iterator expected = std::min_element(dataEmpty.begin(), dataEmpty.end());
198+
Vector::iterator result = etl::min_element(dataEmpty.begin(), dataEmpty.end());
199+
CHECK_EQUAL(std::distance(dataEmpty.end(), expected), std::distance(dataEmpty.end(), result));
200+
}
201+
193202
//*************************************************************************
194203
TEST(max_element)
195204
{
@@ -206,6 +215,15 @@ namespace
206215
CHECK_EQUAL(std::distance(data.begin(), expected), std::distance(data.begin(), result));
207216
}
208217

218+
//*************************************************************************
219+
TEST(max_element_empty)
220+
{
221+
Vector dataEmpty;
222+
Vector::iterator expected = std::max_element(dataEmpty.begin(), dataEmpty.end());
223+
Vector::iterator result = etl::max_element(dataEmpty.begin(), dataEmpty.end());
224+
CHECK_EQUAL(std::distance(dataEmpty.end(), expected), std::distance(dataEmpty.end(), result));
225+
}
226+
209227
//*************************************************************************
210228
TEST(minmax_element)
211229
{
@@ -224,6 +242,16 @@ namespace
224242
CHECK_EQUAL(std::distance(data.begin(), expected.second), std::distance(data.begin(), result.second));
225243
}
226244

245+
//*************************************************************************
246+
TEST(minmax_element_empty)
247+
{
248+
Vector dataEmpty;
249+
std::pair<Vector::iterator, Vector::iterator> expected = std::minmax_element(dataEmpty.begin(), dataEmpty.end());
250+
std::pair<Vector::iterator, Vector::iterator> result = etl::minmax_element(dataEmpty.begin(), dataEmpty.end());
251+
CHECK_EQUAL(std::distance(dataEmpty.begin(), expected.first), std::distance(dataEmpty.begin(), result.first));
252+
CHECK_EQUAL(std::distance(dataEmpty.begin(), expected.second), std::distance(dataEmpty.begin(), result.second));
253+
}
254+
227255
//*************************************************************************
228256
TEST(minmax)
229257
{

0 commit comments

Comments
 (0)