aboutsummaryrefslogtreecommitdiff
path: root/test/source/TestAlgorithm.cpp
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2025-06-25 12:39:04 +0200
committerToni Uhlig <matzeton@googlemail.com>2025-06-25 12:39:04 +0200
commitda5ddb55fd888d6d5ef185acdb054deac335717b (patch)
treec6450033f6215ffb8d53a0806e7ecaf95b38c302 /test/source/TestAlgorithm.cpp
parent03b5c5b336ef8f5ff8b458b366b3560b224e7bcb (diff)
Squashed 'EASTL/' changes from e045e9d..1aa7846
1aa7846 EASTL version 3.20.02. 3cf1c4f Merge pull request #500 from electronicarts/bump_compiler_versions ad2478c Increment compiler version on the runners. clang-14 gcc-12. 7b2a478 Merge pull request #483 from electronicarts/test_ci_for_iterator_categories 0184a05 Don't yell, lowercase is fine for this. 125ae27 Make the names more expressive. cb0f08b Fix whitespace 2611d05 Fix syntax error. c5e80d5 Test adding std_iter_compatibility to the ci workflow. db16065 Merge pull request #493 from electronicarts/eastl-3.19.05 384db67 Update version macros. 2a48230 Update to 13.19.05 19a8869 Merge pull request #481 from eugeneko/ek/ctor 1bb9f62 Add non-explicit default ctor for hash_map and hash_set. 5eb9b1e Merge pull request #476 from eugeneko/ek/ref 00216eb Use invoke_result instead of result_of in reference_wrapper. Relax is_invocable_r check. 46b5321 Merge pull request #475 from eugeneko/ek/tuple-vec ae8cc01 Merge pull request #474 from StephanTLavavej/fix-floating-limits 713beb7 Merge pull request #453 from GrooveStomp/441-typo-in-tests 63125fc Merge pull request #477 from electronicarts/release-candidate-3.19.03 ab9db85 Fixes for gcc. a0b8a69 Release candidate 3.19.03 ec806cd Fix missing namespace qualifier in TupleVecIter. 6c6c546 MSVC's STL is removing `_FInf` etc. 29a805e Merge pull request #415 from mtnpke/km_assert 0700f63 Merge pull request #442 from Razakhel/operator_less_to_eastl_less 49f654e Merge pull request #419 from Wawha/std-basic-string-npos-constexpr cd6abfc Replaced some generic calls to operator< by eastl::less ca6b871 Merge pull request #411 from Razakhel/array_structured_binding d1c2a9c Merge pull request #458 from james-moran-ea/master 4ba652d Fix typo in unit test 8d5eb9e Added structured binding support for eastl::array 640e2a6 Fix compilation error with eastl::basic_string<>::npos and (clang 9.0). Need to be constexpr. 3743276 Add assertion failure variant for win kernel git-subtree-dir: EASTL git-subtree-split: 1aa784643081404783ce6494eb2fcaba99d8f6a5
Diffstat (limited to 'test/source/TestAlgorithm.cpp')
-rw-r--r--test/source/TestAlgorithm.cpp235
1 files changed, 234 insertions, 1 deletions
diff --git a/test/source/TestAlgorithm.cpp b/test/source/TestAlgorithm.cpp
index 142d45e..a0f64da 100644
--- a/test/source/TestAlgorithm.cpp
+++ b/test/source/TestAlgorithm.cpp
@@ -1509,6 +1509,82 @@ int TestAlgorithm()
EATEST_VERIFY( b);
}
+#if defined(EA_COMPILER_HAS_THREE_WAY_COMPARISON)
+ {
+ // <compairison_category> lexicographical_compare_three_way(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare compare)
+
+ int intArray1[6] = {0, 1, 2, 3, 4, 5};
+ int intArray2[6] = {0, 1, 2, 3, 4, 6};
+ int intArray3[5] = {0, 1, 2, 3, 4};
+ int intArray4[5] = {4, 3, 2, 1, 0};
+
+ // strong ordering
+ auto compare_strong = [](int first, int second)
+ {
+ return (first < second) ? std::strong_ordering::less :
+ (first > second) ? std::strong_ordering::greater :
+ std::strong_ordering::equal;
+ };
+
+ auto b = lexicographical_compare_three_way(intArray1, intArray1 + 6, intArray2, intArray2 + 6, compare_strong);
+ EATEST_VERIFY(b == std::strong_ordering::less);
+ b = lexicographical_compare_three_way(intArray3, intArray3 + 5, intArray2, intArray2 + 6, compare_strong);
+ EATEST_VERIFY(b == std::strong_ordering::less);
+ b = lexicographical_compare_three_way(intArray3, intArray3 + 5, intArray2, intArray2 + 6, synth_three_way{});
+ EATEST_VERIFY(b == std::strong_ordering::less);
+
+ b = lexicographical_compare_three_way(intArray2, intArray2 + 6, intArray1, intArray1 + 6, compare_strong);
+ EATEST_VERIFY(b == std::strong_ordering::greater);
+ b = lexicographical_compare_three_way(intArray2, intArray2 + 6, intArray1, intArray1 + 6, synth_three_way{});
+ EATEST_VERIFY(b == std::strong_ordering::greater);
+
+ b = lexicographical_compare_three_way(intArray1, intArray1 + 6, intArray3, intArray3 + 5, compare_strong);
+ EATEST_VERIFY(b == std::strong_ordering::greater);
+ b = lexicographical_compare_three_way(intArray1, intArray1 + 6, intArray3, intArray3 + 5, synth_three_way{});
+ EATEST_VERIFY(b == std::strong_ordering::greater);
+
+ b = lexicographical_compare_three_way(intArray1, intArray1, intArray2, intArray2, compare_strong); // Test empty range.
+ EATEST_VERIFY(b == std::strong_ordering::equal);
+ b = lexicographical_compare_three_way(intArray1, intArray1, intArray2, intArray2, synth_three_way{}); // Test empty range.
+ EATEST_VERIFY(b == std::strong_ordering::equal);
+
+ // weak ordering
+ auto compare_weak = [](int first, int second)
+ {
+ return (first < second) ? std::weak_ordering::less :
+ (first > second) ? std::weak_ordering::greater :
+ std::weak_ordering::equivalent;
+ };
+
+ auto c = lexicographical_compare_three_way(intArray3, intArray3 + 5, intArray4, intArray4 + 5, compare_weak);
+ EATEST_VERIFY(c == std::weak_ordering::less);
+ c = lexicographical_compare_three_way(intArray4, intArray4 + 5, intArray3, intArray3 + 5, compare_weak);
+ EATEST_VERIFY(c == std::weak_ordering::greater);
+ c = lexicographical_compare_three_way(intArray3, intArray3 + 5, intArray4, intArray4 + 5, synth_three_way{});
+ EATEST_VERIFY(c == std::weak_ordering::less);
+ c = lexicographical_compare_three_way(intArray4, intArray4 + 5, intArray3, intArray3 + 5, synth_three_way{});
+ EATEST_VERIFY(c == std::weak_ordering::greater);
+ }
+
+ {
+ EATEST_VERIFY(synth_three_way{}(1, 1) == std::strong_ordering::equal);
+ EATEST_VERIFY(synth_three_way{}(2, 1) == std::strong_ordering::greater);
+ EATEST_VERIFY(synth_three_way{}(1, 2) == std::strong_ordering::less);
+
+ struct weak_struct
+ {
+ int val;
+ inline std::weak_ordering operator<=>(const weak_struct& b) const
+ {
+ return val <=> b.val;
+ }
+ };
+
+ EATEST_VERIFY(synth_three_way{}(weak_struct{1}, weak_struct{2}) == std::weak_ordering::less);
+ EATEST_VERIFY(synth_three_way{}(weak_struct{2}, weak_struct{1}) == std::weak_ordering::greater);
+ EATEST_VERIFY(synth_three_way{}(weak_struct{1}, weak_struct{1}) == std::weak_ordering::equivalent);
+ }
+#endif
{
// ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value)
@@ -1815,7 +1891,164 @@ int TestAlgorithm()
}
- {
+ {
+ // ForwardIterator apply_and_remove(ForwardIterator first, ForwardIterator last, Function function, const T&
+ // value) ForwardIterator apply_and_remove_if(ForwardIterator first, ForwardIterator last, Function function,
+ // Predicate predicate)
+
+ // Test for empty range and full container range
+ {
+ int intArray[12] = {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ int* pInt = apply_and_remove(intArray, intArray, func, 1);
+ EATEST_VERIFY(pInt == intArray);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 12, int(), "apply_and_remove", 0, 0, 1, 1, 0, 0, 1, 1, 0,
+ 0, 1, 1, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove", -1));
+ pInt = apply_and_remove(intArray, intArray + 12, func, 1);
+ EATEST_VERIFY(pInt == intArray + 6);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 6, int(), "apply_and_remove", 0, 0, 0, 0, 0, 0, -1));
+ EATEST_VERIFY(
+ VerifySequence(output.begin(), output.end(), int(), "apply_and_remove", 1, 1, 1, 1, 1, 1, -1));
+ }
+
+ // Test for no match on empty range and full container range
+ {
+ int intArray[12] = {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ int* pInt = apply_and_remove(intArray, intArray, func, 1);
+ EATEST_VERIFY(pInt == intArray);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 12, int(), "apply_and_remove", 3, 3, 3, 3, 3, 3, 3, 3, 3,
+ 3, 3, 3, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove", -1));
+ pInt = apply_and_remove(intArray, intArray + 12, func, 1);
+ EATEST_VERIFY(pInt == intArray + 12);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 12, int(), "apply_and_remove", 3, 3, 3, 3, 3, 3, 3, 3, 3,
+ 3, 3, 3, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove", -1));
+ }
+
+ // Test for empty range and full container range
+ {
+ int intArray[12] = {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ int* pInt = apply_and_remove_if(intArray, intArray, func, bind2nd(equal_to<int>(), (int)1));
+ EATEST_VERIFY(pInt == intArray);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 12, int(), "apply_and_remove_if", 0, 0, 1, 1, 0, 0, 1, 1,
+ 0, 0, 1, 1, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove_if", -1));
+ pInt = apply_and_remove_if(intArray, intArray + 12, func, bind2nd(equal_to<int>(), (int)1));
+ EATEST_VERIFY(pInt == intArray + 6);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 6, int(), "apply_and_remove_if", 0, 0, 0, 0, 0, 0, -1));
+ EATEST_VERIFY(
+ VerifySequence(output.begin(), output.end(), int(), "apply_and_remove_if", 1, 1, 1, 1, 1, 1, -1));
+ }
+
+ // Test for no match on empty range and full container range
+ {
+ int intArray[12] = {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ int* pInt = apply_and_remove_if(intArray, intArray, func, bind2nd(equal_to<int>(), (int)1));
+ EATEST_VERIFY(pInt == intArray);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 12, int(), "apply_and_remove_if", 3, 3, 3, 3, 3, 3, 3, 3,
+ 3, 3, 3, 3, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove_if", -1));
+ pInt = apply_and_remove_if(intArray, intArray + 12, func, bind2nd(equal_to<int>(), (int)1));
+ EATEST_VERIFY(pInt == intArray + 12);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 12, int(), "apply_and_remove_if", 3, 3, 3, 3, 3, 3, 3, 3,
+ 3, 3, 3, 3, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove_if", -1));
+ }
+
+ auto even = [](int a) { return (a % 2) == 0; };
+ // Test to verify that the remaining element have stable ordering
+ {
+ int intArray[12] = {7, 8, 2, 3, 4, 5, 6, 0, 1, 9, 10, 11};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ int* pInt = apply_and_remove_if(intArray, intArray + 12, func, even);
+ EATEST_VERIFY(pInt == intArray + 6);
+ EATEST_VERIFY(VerifySequence(intArray, intArray + 6, int(), "apply_and_remove_if", 7, 3, 5, 1, 9, 11, -1));
+ EATEST_VERIFY(
+ VerifySequence(output.begin(), output.end(), int(), "apply_and_remove_if", 8, 2, 4, 6, 0, 10, -1));
+ }
+ {
+ int intArray[12] = {7, 8, 0, 0, 4, 5, 6, 0, 1, 9, 0, 11};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ int* pInt = apply_and_remove(intArray, intArray + 12, func, 0);
+ EATEST_VERIFY(pInt == intArray + 8);
+ EATEST_VERIFY(
+ VerifySequence(intArray, intArray + 8, int(), "apply_and_remove", 7, 8, 4, 5, 6, 1, 9, 11, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove", 0, 0, 0, 0, -1));
+ }
+
+ // Tests on a list (i.e. non-contiguous memory container)
+ {
+ list<int> intList = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ auto listIter = apply_and_remove_if(intList.begin(), intList.begin(), func, even);
+ EATEST_VERIFY(listIter == intList.begin());
+ EATEST_VERIFY(VerifySequence(intList.begin(), intList.end(), int(), "apply_and_remove_if", 0, 1, 2, 3, 4, 5,
+ 6, 7, 8, 9, 10, 11, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove_if", -1));
+ listIter = apply_and_remove_if(intList.begin(), intList.end(), func, even);
+ EATEST_VERIFY(listIter == next(intList.begin(), 6));
+ EATEST_VERIFY(
+ VerifySequence(intList.begin(), listIter, int(), "apply_and_remove_if", 1, 3, 5, 7, 9, 11, -1));
+ EATEST_VERIFY(
+ VerifySequence(output.begin(), output.end(), int(), "apply_and_remove_if", 0, 2, 4, 6, 8, 10, -1));
+ }
+ {
+ list<int> intList = {0, 4, 2, 3, 4, 5, 6, 4, 4, 4, 10, 11};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ auto listIter = apply_and_remove(intList.begin(), intList.begin(), func, 4);
+ EATEST_VERIFY(listIter == intList.begin());
+ EATEST_VERIFY(VerifySequence(intList.begin(), intList.end(), int(), "apply_and_remove", 0, 4, 2, 3, 4, 5, 6,
+ 4, 4, 4, 10, 11, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove", -1));
+ listIter = apply_and_remove(intList.begin(), intList.end(), func, 4);
+ EATEST_VERIFY(listIter == next(intList.begin(), 7));
+ EATEST_VERIFY(
+ VerifySequence(intList.begin(), listIter, int(), "apply_and_remove", 0, 2, 3, 5, 6, 10, 11, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove", 4, 4, 4, 4, 4, -1));
+ }
+
+ // Tests on a part of a container
+ {
+ vector<int> intVector = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ auto vectorIter = apply_and_remove_if(next(intVector.begin(), 3), prev(intVector.end(), 2), func, even);
+ EATEST_VERIFY(vectorIter == next(intVector.begin(), 7));
+ EATEST_VERIFY(
+ VerifySequence(intVector.begin(), vectorIter, int(), "apply_and_remove_if", 0, 1, 2, 3, 5, 7, 9, -1));
+ EATEST_VERIFY(
+ VerifySequence(prev(intVector.end(), 2), intVector.end(), int(), "apply_and_remove_if", 10, 11, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove_if", 4, 6, 8, -1));
+ }
+ {
+ vector<int> intVector = {5, 1, 5, 3, 4, 5, 5, 7, 8, 5, 10, 5};
+ vector<int> output;
+ auto func = [&output](int a) { output.push_back(a); };
+ auto vectorIter = apply_and_remove(next(intVector.begin(), 2), prev(intVector.end(), 3), func, 5);
+ EATEST_VERIFY(vectorIter == next(intVector.begin(), 6));
+ EATEST_VERIFY(
+ VerifySequence(intVector.begin(), vectorIter, int(), "apply_and_remove", 5, 1, 3, 4, 7, 8, -1));
+ EATEST_VERIFY(
+ VerifySequence(prev(intVector.end(), 3), intVector.end(), int(), "apply_and_remove", 5, 10, 5, -1));
+ EATEST_VERIFY(VerifySequence(output.begin(), output.end(), int(), "apply_and_remove", 5, 5, 5, -1));
+ }
+ }
+
+
+ {
// OutputIterator replace_copy(InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value)
// OutputIterator replace_copy_if(InputIterator first, InputIterator last, OutputIterator result, Predicate predicate, const T& new_value)