From c944a978e0d64230f326c6cbe3016d8a4b46b230 Mon Sep 17 00:00:00 2001 From: Will Sciaroni Date: Wed, 18 Mar 2026 00:05:50 +0000 Subject: [PATCH] Add test for next_permutation where begin == end --- test/test_algorithm.cpp | 64 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/test/test_algorithm.cpp b/test/test_algorithm.cpp index 88a88654..accddb4a 100644 --- a/test/test_algorithm.cpp +++ b/test/test_algorithm.cpp @@ -1341,14 +1341,26 @@ namespace CHECK_ARRAY_EQUAL(expected.data(), result.data(), result.size()); } + // Check one past the end. + bool expected_has_next = std::next_permutation(expected.begin(), expected.end()); + bool result_has_next = etl::next_permutation(result.begin(), result.end()); + CHECK_EQUAL(expected_has_next, result_has_next); + CHECK_ARRAY_EQUAL(expected.data(), result.data(), result.size()); + int single_expected[] = { 1 }; int single_result[] = { 1 }; - bool expected_has_next = std::next_permutation(std::begin(single_expected), std::end(single_expected)); - bool result_has_next = etl::next_permutation(std::begin(single_result), std::end(single_result)); + expected_has_next = std::next_permutation(std::begin(single_expected), std::end(single_expected)); + result_has_next = etl::next_permutation(std::begin(single_result), std::end(single_result)); CHECK_EQUAL(expected_has_next, result_has_next); CHECK_ARRAY_EQUAL(single_expected, single_result, 1U); + + // Check for what happens if the beginning and end are the same. + expected_has_next = std::next_permutation(std::begin(single_expected), std::begin(single_expected)); + result_has_next = etl::next_permutation(std::begin(single_result), std::begin(single_result)); + + CHECK_EQUAL(expected_has_next, result_has_next); } //************************************************************************* @@ -1365,6 +1377,22 @@ namespace CHECK_EQUAL(expected_has_next, result_has_next); CHECK_ARRAY_EQUAL(expected.data(), result.data(), result.size()); } + + // Check one past the end. + bool expected_has_next = std::next_permutation(expected.begin(), expected.end(), std::greater()); + bool result_has_next = etl::next_permutation(result.begin(), result.end(), std::greater()); + CHECK_EQUAL(expected_has_next, result_has_next); + CHECK_ARRAY_EQUAL(expected.data(), result.data(), result.size()); + + int single_expected[] = { 1 }; + int single_result[] = { 1 }; + + // Check for what happens if the beginning and end are the same. + expected_has_next = std::next_permutation(std::begin(single_expected), std::begin(single_expected), std::greater()); + result_has_next = etl::next_permutation(std::begin(single_result), std::begin(single_result), std::greater()); + + CHECK_EQUAL(expected_has_next, result_has_next); + CHECK_ARRAY_EQUAL(single_expected, single_result, 1U); } //************************************************************************* @@ -1382,14 +1410,26 @@ namespace CHECK_ARRAY_EQUAL(expected.data(), result.data(), result.size()); } + // Check one past the end. + bool expected_has_prev = std::prev_permutation(expected.begin(), expected.end()); + bool result_has_prev = etl::prev_permutation(result.begin(), result.end()); + CHECK_EQUAL(expected_has_prev, result_has_prev); + CHECK_ARRAY_EQUAL(expected.data(), result.data(), result.size()); + int single_expected[] = { 1 }; int single_result[] = { 1 }; - bool expected_has_prev = std::prev_permutation(std::begin(single_expected), std::end(single_expected)); - bool result_has_prev = etl::prev_permutation(std::begin(single_result), std::end(single_result)); + expected_has_prev = std::prev_permutation(std::begin(single_expected), std::end(single_expected)); + result_has_prev = etl::prev_permutation(std::begin(single_result), std::end(single_result)); CHECK_EQUAL(expected_has_prev, result_has_prev); CHECK_ARRAY_EQUAL(single_expected, single_result, 1U); + + // Check for what happens if the beginning and end are the same. + expected_has_prev = std::prev_permutation(std::begin(single_expected), std::begin(single_expected)); + result_has_prev = etl::prev_permutation(std::begin(single_result), std::begin(single_result)); + + CHECK_EQUAL(expected_has_prev, result_has_prev); } //************************************************************************* @@ -1406,6 +1446,22 @@ namespace CHECK_EQUAL(expected_has_prev, result_has_prev); CHECK_ARRAY_EQUAL(expected.data(), result.data(), result.size()); } + + // Check one past the end. + bool expected_has_prev = std::prev_permutation(expected.begin(), expected.end(), std::greater()); + bool result_has_prev = etl::prev_permutation(result.begin(), result.end(), std::greater()); + CHECK_EQUAL(expected_has_prev, result_has_prev); + CHECK_ARRAY_EQUAL(expected.data(), result.data(), result.size()); + + int single_expected[] = { 1 }; + int single_result[] = { 1 }; + + // Check for what happens if the beginning and end are the same. + expected_has_prev = std::prev_permutation(std::begin(single_expected), std::begin(single_expected), std::greater()); + result_has_prev = etl::prev_permutation(std::begin(single_result), std::begin(single_result), std::greater()); + + CHECK_EQUAL(expected_has_prev, result_has_prev); + CHECK_ARRAY_EQUAL(single_expected, single_result, 1U); } //*************************************************************************