diff --git a/src/models/search.h b/src/models/search.h index 93c5f2580..6d706e6fd 100644 --- a/src/models/search.h +++ b/src/models/search.h @@ -29,7 +29,13 @@ int search_impl(iter begin, iter end, iter current, SearchFunc searchFunc, EndRe if (begin == end) return -1; - auto start = current + 1; + iter start; + if (current != end) { + start = current + 1; + } else { + start = begin; + } + auto found = std::find_if(start, end, searchFunc); if (found != end) { diff --git a/tests/modeltests/tst_search.cpp b/tests/modeltests/tst_search.cpp index 931790693..307978fdf 100644 --- a/tests/modeltests/tst_search.cpp +++ b/tests/modeltests/tst_search.cpp @@ -82,6 +82,26 @@ private slots: 3); } } + + void testArrayIsEmpty() + { + const std::array testArray; + + for (int i = 0; i < 2; i++) { + QCOMPARE(search( + testArray, i, Direction::Forward, [](int) { return true; }, [] {}), + -1); + } + } + + void testOutOfRangeIfCurrentIsEnd() + { + const std::array testArray = {0}; + + QCOMPARE(search( + testArray, 1, Direction::Forward, [](int i) { return i == 0; }, [] {}), + 0); + } }; QTEST_GUILESS_MAIN(TestSearch)