Skip to content

Commit 82d4f0e

Browse files
committed
Add self-testing to the custom checkers
1 parent fbcee8f commit 82d4f0e

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

test/parallel_api/ranges/std_ranges_remove_copy.pass.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "std_ranges_test.h"
1111

1212
#if _ENABLE_STD_RANGES_TESTING
13+
#include <initializer_list>
14+
1315
struct
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, 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_checker;
3792
#endif // _ENABLE_STD_RANGES_TESTING
3893

@@ -50,6 +105,8 @@ main()
50105
};
51106
using many_twos = decltype(almost_always_two);
52107

108+
remove_copy_if_checker.test_self();
109+
53110
test_range_algo<0, int, data_in_out_lim>{179}(dpl_ranges::remove_copy, remove_copy_checker, 0);
54111
test_range_algo<1, int, data_in_out_lim, many_twos>{1127}(dpl_ranges::remove_copy, remove_copy_checker, 2);
55112
test_range_algo<2, int, data_in_out_lim>{}(dpl_ranges::remove_copy, remove_copy_checker, 1, proj);

test/parallel_api/ranges/std_ranges_remove_copy_if.pass.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "std_ranges_test.h"
1111

1212
#if _ENABLE_STD_RANGES_TESTING
13+
#include <initializer_list>
14+
1315
struct
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);

test/parallel_api/ranges/std_ranges_unique_copy.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "std_ranges_test.h"
1717

1818
#if _ENABLE_STD_RANGES_TESTING
19-
#include <span>
2019
#include <initializer_list>
2120

2221
struct
@@ -45,6 +44,7 @@ struct
4544

4645
void test_self()
4746
{
47+
#if TEST_CPP20_SPAN_PRESENT
4848
int input[10] = {0,0, 1, 2,2, 8, 1,1,1, 8};
4949
int output[9] = {-9, -8, -7, -6, -5, -4, -3, -2, -1};
5050

@@ -92,6 +92,7 @@ struct
9292
}
9393
}
9494
}
95+
#endif // TEST_CPP20_SPAN_PRESENT
9596
}
9697
} unique_copy_checker;
9798
#endif // _ENABLE_STD_RANGES_TESTING

0 commit comments

Comments
 (0)