1010#include " std_ranges_test.h"
1111
1212#if _ENABLE_STD_RANGES_TESTING
13+ #include < initializer_list>
14+
1315struct
1416{
1517 template <std::ranges::random_access_range InRange, std::ranges::random_access_range OutRange,
@@ -33,6 +35,59 @@ struct
3335 }
3436 return ret_type{in + i, out + j};
3537 }
38+
39+ void test_self ()
40+ {
41+ #if TEST_CPP20_SPAN_PRESENT
42+ int input[10 ] = {0 ,0 , 1 , 2 ,2 , 8 , 1 ,1 ,1 , 8 };
43+ int output[9 ] = {-9 , -8 , -7 , -6 , -5 , -4 , -3 , -2 , -1 };
44+
45+ // Define test cases with expected outputs and expected end positions
46+ struct TestCase {
47+ int in_size;
48+ int out_size;
49+ std::initializer_list<int > expected_output;
50+ int expected_in_end;
51+ int expected_out_end;
52+ } test_cases[] = {
53+ // insz, outsz, expected, instop, outstop
54+ {0 , 0 , {}, 0 , 0 }, // Empty ranges
55+ {10 , 0 , {}, 0 , 0 }, // Empty output range
56+ {1 , 1 , {0 }, 1 , 1 }, // One element ranges
57+ {10 , 1 , {0 }, 1 , 1 }, // One element output range
58+ {10 , 5 , {0 , 0 , 2 , 2 , 8 }, 9 , 5 }, // Output range is not big enough
59+ {10 , 6 , {0 , 0 , 2 , 2 , 8 , 8 }, 10 , 6 }, // Output range is just enough
60+ {10 , 7 , {0 , 0 , 2 , 2 , 8 , 8 }, 10 , 6 }, // Output range is bigger than needed
61+ };
62+
63+ auto & self = *this ;
64+ for (const TestCase& test_case : test_cases) {
65+ constexpr int shift = 1 ;
66+ std::span<int > in_span (input, test_case.in_size );
67+ std::span<int > out_span (output + shift, test_case.out_size );
68+
69+ auto result = self (in_span, out_span, [](int v){ return v == 1 ; });
70+
71+ // Verify the returned iterators point to the correct end positions
72+ EXPECT_EQ (in_span.begin () + test_case.expected_in_end , result.in , " Checker problem: wrong input stop" );
73+ EXPECT_EQ (out_span.begin () + test_case.expected_out_end , result.out , " Checker problem: wrong output stop" );
74+
75+ // Verify the output matches the expected result and nothing is overwritten
76+ for (int i = 0 ; i < 9 ; ++i)
77+ {
78+ if (i < shift || i >= shift + test_case.expected_out_end )
79+ {
80+ EXPECT_EQ (i - 9 , output[i], " Checker problem: out of range modification" );
81+ }
82+ else
83+ {
84+ EXPECT_EQ (test_case.expected_output .begin ()[i - shift], output[i], " Checker problem: wrong output" );
85+ output[i] = i - 9 ; // Restore the original output data
86+ }
87+ }
88+ }
89+ #endif // TEST_CPP20_SPAN_PRESENT
90+ }
3691} remove_copy_if_checker;
3792#endif // _ENABLE_STD_RANGES_TESTING
3893
@@ -55,6 +110,8 @@ main()
55110 using repeating_gen = decltype (repeat_sometimes);
56111 auto modulo_3_is_1 = [](int val) { return (val % 3 ) == 1 ; };
57112
113+ remove_copy_if_checker.test_self ();
114+
58115 test_range_algo<0 , int , data_in_out_lim>{239 }(dpl_ranges::remove_copy_if, remove_copy_if_checker, pred);
59116 test_range_algo<1 , int , data_in_out_lim>{1471 }(dpl_ranges::remove_copy_if, remove_copy_if_checker, select_many);
60117 test_range_algo<2 , int , data_in_out_lim>{}(dpl_ranges::remove_copy_if, remove_copy_if_checker, select_many, proj);
0 commit comments