-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc++][test] Refactor tests for std::{copy, move, fill} algorithms #120909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,75 +19,48 @@ | |
|
||
#include "test_macros.h" | ||
#include "test_iterators.h" | ||
#include "type_algorithms.h" | ||
|
||
struct Pred | ||
{ | ||
TEST_CONSTEXPR_CXX14 bool operator()(int i) {return i % 3 == 0;} | ||
struct Pred { | ||
TEST_CONSTEXPR_CXX14 bool operator()(int i) { return i % 3 == 0; } | ||
}; | ||
|
||
template <class InIter, class OutIter> | ||
TEST_CONSTEXPR_CXX20 void | ||
test_copy_if() | ||
{ | ||
template <class InIter> | ||
struct TestOutIters { | ||
template <class OutIter> | ||
TEST_CONSTEXPR_CXX20 void operator()() { | ||
const unsigned N = 1000; | ||
int ia[N] = {}; | ||
int ia[N] = {}; | ||
for (unsigned i = 0; i < N; ++i) | ||
ia[i] = i; | ||
ia[i] = i; | ||
int ib[N] = {0}; | ||
|
||
OutIter r = std::copy_if(InIter(ia), InIter(ia+N), OutIter(ib), Pred()); | ||
assert(base(r) == ib+N/3+1); | ||
for (unsigned i = 0; i < N/3+1; ++i) | ||
assert(ib[i] % 3 == 0); | ||
} | ||
|
||
TEST_CONSTEXPR_CXX20 bool | ||
test() | ||
{ | ||
test_copy_if<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >(); | ||
test_copy_if<cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >(); | ||
test_copy_if<cpp17_input_iterator<const int*>, forward_iterator<int*> >(); | ||
test_copy_if<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >(); | ||
test_copy_if<cpp17_input_iterator<const int*>, random_access_iterator<int*> >(); | ||
test_copy_if<cpp17_input_iterator<const int*>, int*>(); | ||
|
||
test_copy_if<forward_iterator<const int*>, cpp17_output_iterator<int*> >(); | ||
test_copy_if<forward_iterator<const int*>, cpp17_input_iterator<int*> >(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a pre-existing condition in the test, but I think it's not valid to instantiate this test with In other words, this input iterator "archetype" is too specific, and that has allowed us to write code that depends on more than the strict requirements of an input iterator. This would be worth trying to fix in a follow-up patch. Maybe returning a prvalue from |
||
test_copy_if<forward_iterator<const int*>, forward_iterator<int*> >(); | ||
test_copy_if<forward_iterator<const int*>, bidirectional_iterator<int*> >(); | ||
test_copy_if<forward_iterator<const int*>, random_access_iterator<int*> >(); | ||
test_copy_if<forward_iterator<const int*>, int*>(); | ||
|
||
test_copy_if<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >(); | ||
test_copy_if<bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >(); | ||
test_copy_if<bidirectional_iterator<const int*>, forward_iterator<int*> >(); | ||
test_copy_if<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >(); | ||
test_copy_if<bidirectional_iterator<const int*>, random_access_iterator<int*> >(); | ||
test_copy_if<bidirectional_iterator<const int*>, int*>(); | ||
|
||
test_copy_if<random_access_iterator<const int*>, cpp17_output_iterator<int*> >(); | ||
test_copy_if<random_access_iterator<const int*>, cpp17_input_iterator<int*> >(); | ||
test_copy_if<random_access_iterator<const int*>, forward_iterator<int*> >(); | ||
test_copy_if<random_access_iterator<const int*>, bidirectional_iterator<int*> >(); | ||
test_copy_if<random_access_iterator<const int*>, random_access_iterator<int*> >(); | ||
test_copy_if<random_access_iterator<const int*>, int*>(); | ||
OutIter r = std::copy_if(InIter(ia), InIter(ia + N), OutIter(ib), Pred()); | ||
assert(base(r) == ib + N / 3 + 1); | ||
for (unsigned i = 0; i < N / 3 + 1; ++i) | ||
assert(ib[i] % 3 == 0); | ||
} | ||
}; | ||
|
||
test_copy_if<const int*, cpp17_output_iterator<int*> >(); | ||
test_copy_if<const int*, cpp17_input_iterator<int*> >(); | ||
test_copy_if<const int*, forward_iterator<int*> >(); | ||
test_copy_if<const int*, bidirectional_iterator<int*> >(); | ||
test_copy_if<const int*, random_access_iterator<int*> >(); | ||
test_copy_if<const int*, int*>(); | ||
struct TestInIters { | ||
template <class InIter> | ||
TEST_CONSTEXPR_CXX20 void operator()() { | ||
types::for_each( | ||
types::concatenate_t<types::cpp17_input_iterator_list<int*>, types::type_list<cpp17_output_iterator<int*> > >(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. types::type_list<int*, cpp17_output_iterator<int*> >(), I think that's the only thing we're really allowed to test here. This makes me realize that we probably have a similar problem in almost all of our tests? Since this seems to be so widespread, I actually suggest not making any change as part of this patch. That way, we'll keep our tests incorrect but at least they will be consistently incorrect. We can fix this issue across all of our tests in a separate patch. |
||
TestOutIters<InIter>()); | ||
} | ||
}; | ||
|
||
TEST_CONSTEXPR_CXX20 bool test() { | ||
types::for_each(types::cpp17_input_iterator_list<const int*>(), TestInIters()); | ||
return true; | ||
} | ||
|
||
int main(int, char**) | ||
{ | ||
test(); | ||
int main(int, char**) { | ||
test(); | ||
|
||
#if TEST_STD_VER > 17 | ||
static_assert(test()); | ||
static_assert(test()); | ||
#endif | ||
|
||
return 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, but this newline should probably stay.