Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

449 lines
13 KiB
C++
Raw Normal View History

2010-05-11 19:42:16 +00:00
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2010-05-11 19:42:16 +00:00
//
//===----------------------------------------------------------------------===//
// <algorithm>
// template<ShuffleIterator Iter>
2010-05-11 19:42:16 +00:00
// Iter
// rotate(Iter first, Iter middle, Iter last);
#include <algorithm>
#include <cassert>
#include <memory>
#include "test_macros.h"
#include "test_iterators.h"
2010-05-11 19:42:16 +00:00
template <class Iter>
_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
2010-05-11 19:42:16 +00:00
test()
{
int ia[] = {0};
const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
2010-05-11 19:42:16 +00:00
Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
assert(base(r) == ia);
assert(ia[0] == 0);
r = std::rotate(Iter(ia), Iter(ia), Iter(ia+sa));
assert(base(r) == ia+sa);
assert(ia[0] == 0);
r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
assert(base(r) == ia);
assert(ia[0] == 0);
int ib[] = {0, 1};
const int sb = static_cast<int>(sizeof(ib)/sizeof(ib[0]));
2010-05-11 19:42:16 +00:00
r = std::rotate(Iter(ib), Iter(ib), Iter(ib+sb));
assert(base(r) == ib+sb);
assert(ib[0] == 0);
assert(ib[1] == 1);
r = std::rotate(Iter(ib), Iter(ib+1), Iter(ib+sb));
assert(base(r) == ib+1);
assert(ib[0] == 1);
assert(ib[1] == 0);
r = std::rotate(Iter(ib), Iter(ib+sb), Iter(ib+sb));
assert(base(r) == ib);
assert(ib[0] == 1);
assert(ib[1] == 0);
int ic[] = {0, 1, 2};
const int sc = static_cast<int>(sizeof(ic)/sizeof(ic[0]));
2010-05-11 19:42:16 +00:00
r = std::rotate(Iter(ic), Iter(ic), Iter(ic+sc));
assert(base(r) == ic+sc);
assert(ic[0] == 0);
assert(ic[1] == 1);
assert(ic[2] == 2);
r = std::rotate(Iter(ic), Iter(ic+1), Iter(ic+sc));
assert(base(r) == ic+2);
assert(ic[0] == 1);
assert(ic[1] == 2);
assert(ic[2] == 0);
r = std::rotate(Iter(ic), Iter(ic+2), Iter(ic+sc));
assert(base(r) == ic+1);
assert(ic[0] == 0);
assert(ic[1] == 1);
assert(ic[2] == 2);
r = std::rotate(Iter(ic), Iter(ic+sc), Iter(ic+sc));
assert(base(r) == ic);
assert(ic[0] == 0);
assert(ic[1] == 1);
assert(ic[2] == 2);
int id[] = {0, 1, 2, 3};
const int sd = static_cast<int>(sizeof(id)/sizeof(id[0]));
2010-05-11 19:42:16 +00:00
r = std::rotate(Iter(id), Iter(id), Iter(id+sd));
assert(base(r) == id+sd);
assert(id[0] == 0);
assert(id[1] == 1);
assert(id[2] == 2);
assert(id[3] == 3);
r = std::rotate(Iter(id), Iter(id+1), Iter(id+sd));
assert(base(r) == id+3);
assert(id[0] == 1);
assert(id[1] == 2);
assert(id[2] == 3);
assert(id[3] == 0);
r = std::rotate(Iter(id), Iter(id+2), Iter(id+sd));
assert(base(r) == id+2);
assert(id[0] == 3);
assert(id[1] == 0);
assert(id[2] == 1);
assert(id[3] == 2);
r = std::rotate(Iter(id), Iter(id+3), Iter(id+sd));
assert(base(r) == id+1);
assert(id[0] == 2);
assert(id[1] == 3);
assert(id[2] == 0);
assert(id[3] == 1);
r = std::rotate(Iter(id), Iter(id+sd), Iter(id+sd));
assert(base(r) == id);
assert(id[0] == 2);
assert(id[1] == 3);
assert(id[2] == 0);
assert(id[3] == 1);
int ie[] = {0, 1, 2, 3, 4};
const int se = static_cast<int>(sizeof(ie)/sizeof(ie[0]));
2010-05-11 19:42:16 +00:00
r = std::rotate(Iter(ie), Iter(ie), Iter(ie+se));
assert(base(r) == ie+se);
assert(ie[0] == 0);
assert(ie[1] == 1);
assert(ie[2] == 2);
assert(ie[3] == 3);
assert(ie[4] == 4);
r = std::rotate(Iter(ie), Iter(ie+1), Iter(ie+se));
assert(base(r) == ie+4);
assert(ie[0] == 1);
assert(ie[1] == 2);
assert(ie[2] == 3);
assert(ie[3] == 4);
assert(ie[4] == 0);
r = std::rotate(Iter(ie), Iter(ie+2), Iter(ie+se));
assert(base(r) == ie+3);
assert(ie[0] == 3);
assert(ie[1] == 4);
assert(ie[2] == 0);
assert(ie[3] == 1);
assert(ie[4] == 2);
r = std::rotate(Iter(ie), Iter(ie+3), Iter(ie+se));
assert(base(r) == ie+2);
assert(ie[0] == 1);
assert(ie[1] == 2);
assert(ie[2] == 3);
assert(ie[3] == 4);
assert(ie[4] == 0);
r = std::rotate(Iter(ie), Iter(ie+4), Iter(ie+se));
assert(base(r) == ie+1);
assert(ie[0] == 0);
assert(ie[1] == 1);
assert(ie[2] == 2);
assert(ie[3] == 3);
assert(ie[4] == 4);
r = std::rotate(Iter(ie), Iter(ie+se), Iter(ie+se));
assert(base(r) == ie);
assert(ie[0] == 0);
assert(ie[1] == 1);
assert(ie[2] == 2);
assert(ie[3] == 3);
assert(ie[4] == 4);
int ig[] = {0, 1, 2, 3, 4, 5};
const int sg = static_cast<int>(sizeof(ig)/sizeof(ig[0]));
2010-05-11 19:42:16 +00:00
r = std::rotate(Iter(ig), Iter(ig), Iter(ig+sg));
assert(base(r) == ig+sg);
assert(ig[0] == 0);
assert(ig[1] == 1);
assert(ig[2] == 2);
assert(ig[3] == 3);
assert(ig[4] == 4);
assert(ig[5] == 5);
r = std::rotate(Iter(ig), Iter(ig+1), Iter(ig+sg));
assert(base(r) == ig+5);
assert(ig[0] == 1);
assert(ig[1] == 2);
assert(ig[2] == 3);
assert(ig[3] == 4);
assert(ig[4] == 5);
assert(ig[5] == 0);
r = std::rotate(Iter(ig), Iter(ig+2), Iter(ig+sg));
assert(base(r) == ig+4);
assert(ig[0] == 3);
assert(ig[1] == 4);
assert(ig[2] == 5);
assert(ig[3] == 0);
assert(ig[4] == 1);
assert(ig[5] == 2);
r = std::rotate(Iter(ig), Iter(ig+3), Iter(ig+sg));
assert(base(r) == ig+3);
assert(ig[0] == 0);
assert(ig[1] == 1);
assert(ig[2] == 2);
assert(ig[3] == 3);
assert(ig[4] == 4);
assert(ig[5] == 5);
r = std::rotate(Iter(ig), Iter(ig+4), Iter(ig+sg));
assert(base(r) == ig+2);
assert(ig[0] == 4);
assert(ig[1] == 5);
assert(ig[2] == 0);
assert(ig[3] == 1);
assert(ig[4] == 2);
assert(ig[5] == 3);
r = std::rotate(Iter(ig), Iter(ig+5), Iter(ig+sg));
assert(base(r) == ig+1);
assert(ig[0] == 3);
assert(ig[1] == 4);
assert(ig[2] == 5);
assert(ig[3] == 0);
assert(ig[4] == 1);
assert(ig[5] == 2);
r = std::rotate(Iter(ig), Iter(ig+sg), Iter(ig+sg));
assert(base(r) == ig);
assert(ig[0] == 3);
assert(ig[1] == 4);
assert(ig[2] == 5);
assert(ig[3] == 0);
assert(ig[4] == 1);
assert(ig[5] == 2);
return true;
2010-05-11 19:42:16 +00:00
}
#if TEST_STD_VER >= 11
2010-05-11 19:42:16 +00:00
template <class Iter>
void
test1()
{
std::unique_ptr<int> ia[1];
const int sa = static_cast<int>(sizeof(ia)/sizeof(ia[0]));
2010-05-11 19:42:16 +00:00
for (int i = 0; i < sa; ++i)
ia[i].reset(new int(i));
Iter r = std::rotate(Iter(ia), Iter(ia), Iter(ia));
assert(base(r) == ia);
assert(*ia[0] == 0);
r = std::rotate(Iter(ia), Iter(ia), Iter(ia+sa));
assert(base(r) == ia+sa);
assert(*ia[0] == 0);
r = std::rotate(Iter(ia), Iter(ia+sa), Iter(ia+sa));
assert(base(r) == ia);
assert(*ia[0] == 0);
std::unique_ptr<int> ib[2];
const int sb = static_cast<int>(sizeof(ib)/sizeof(ib[0]));
2010-05-11 19:42:16 +00:00
for (int i = 0; i < sb; ++i)
ib[i].reset(new int(i));
r = std::rotate(Iter(ib), Iter(ib), Iter(ib+sb));
assert(base(r) == ib+sb);
assert(*ib[0] == 0);
assert(*ib[1] == 1);
r = std::rotate(Iter(ib), Iter(ib+1), Iter(ib+sb));
assert(base(r) == ib+1);
assert(*ib[0] == 1);
assert(*ib[1] == 0);
r = std::rotate(Iter(ib), Iter(ib+sb), Iter(ib+sb));
assert(base(r) == ib);
assert(*ib[0] == 1);
assert(*ib[1] == 0);
std::unique_ptr<int> ic[3];
const int sc = static_cast<int>(sizeof(ic)/sizeof(ic[0]));
2010-05-11 19:42:16 +00:00
for (int i = 0; i < sc; ++i)
ic[i].reset(new int(i));
r = std::rotate(Iter(ic), Iter(ic), Iter(ic+sc));
assert(base(r) == ic+sc);
assert(*ic[0] == 0);
assert(*ic[1] == 1);
assert(*ic[2] == 2);
r = std::rotate(Iter(ic), Iter(ic+1), Iter(ic+sc));
assert(base(r) == ic+2);
assert(*ic[0] == 1);
assert(*ic[1] == 2);
assert(*ic[2] == 0);
r = std::rotate(Iter(ic), Iter(ic+2), Iter(ic+sc));
assert(base(r) == ic+1);
assert(*ic[0] == 0);
assert(*ic[1] == 1);
assert(*ic[2] == 2);
r = std::rotate(Iter(ic), Iter(ic+sc), Iter(ic+sc));
assert(base(r) == ic);
assert(*ic[0] == 0);
assert(*ic[1] == 1);
assert(*ic[2] == 2);
std::unique_ptr<int> id[4];
const int sd = static_cast<int>(sizeof(id)/sizeof(id[0]));
2010-05-11 19:42:16 +00:00
for (int i = 0; i < sd; ++i)
id[i].reset(new int(i));
r = std::rotate(Iter(id), Iter(id), Iter(id+sd));
assert(base(r) == id+sd);
assert(*id[0] == 0);
assert(*id[1] == 1);
assert(*id[2] == 2);
assert(*id[3] == 3);
r = std::rotate(Iter(id), Iter(id+1), Iter(id+sd));
assert(base(r) == id+3);
assert(*id[0] == 1);
assert(*id[1] == 2);
assert(*id[2] == 3);
assert(*id[3] == 0);
r = std::rotate(Iter(id), Iter(id+2), Iter(id+sd));
assert(base(r) == id+2);
assert(*id[0] == 3);
assert(*id[1] == 0);
assert(*id[2] == 1);
assert(*id[3] == 2);
r = std::rotate(Iter(id), Iter(id+3), Iter(id+sd));
assert(base(r) == id+1);
assert(*id[0] == 2);
assert(*id[1] == 3);
assert(*id[2] == 0);
assert(*id[3] == 1);
r = std::rotate(Iter(id), Iter(id+sd), Iter(id+sd));
assert(base(r) == id);
assert(*id[0] == 2);
assert(*id[1] == 3);
assert(*id[2] == 0);
assert(*id[3] == 1);
std::unique_ptr<int> ie[5];
const int se = static_cast<int>(sizeof(ie)/sizeof(ie[0]));
2010-05-11 19:42:16 +00:00
for (int i = 0; i < se; ++i)
ie[i].reset(new int(i));
r = std::rotate(Iter(ie), Iter(ie), Iter(ie+se));
assert(base(r) == ie+se);
assert(*ie[0] == 0);
assert(*ie[1] == 1);
assert(*ie[2] == 2);
assert(*ie[3] == 3);
assert(*ie[4] == 4);
r = std::rotate(Iter(ie), Iter(ie+1), Iter(ie+se));
assert(base(r) == ie+4);
assert(*ie[0] == 1);
assert(*ie[1] == 2);
assert(*ie[2] == 3);
assert(*ie[3] == 4);
assert(*ie[4] == 0);
r = std::rotate(Iter(ie), Iter(ie+2), Iter(ie+se));
assert(base(r) == ie+3);
assert(*ie[0] == 3);
assert(*ie[1] == 4);
assert(*ie[2] == 0);
assert(*ie[3] == 1);
assert(*ie[4] == 2);
r = std::rotate(Iter(ie), Iter(ie+3), Iter(ie+se));
assert(base(r) == ie+2);
assert(*ie[0] == 1);
assert(*ie[1] == 2);
assert(*ie[2] == 3);
assert(*ie[3] == 4);
assert(*ie[4] == 0);
r = std::rotate(Iter(ie), Iter(ie+4), Iter(ie+se));
assert(base(r) == ie+1);
assert(*ie[0] == 0);
assert(*ie[1] == 1);
assert(*ie[2] == 2);
assert(*ie[3] == 3);
assert(*ie[4] == 4);
r = std::rotate(Iter(ie), Iter(ie+se), Iter(ie+se));
assert(base(r) == ie);
assert(*ie[0] == 0);
assert(*ie[1] == 1);
assert(*ie[2] == 2);
assert(*ie[3] == 3);
assert(*ie[4] == 4);
std::unique_ptr<int> ig[6];
const int sg = static_cast<int>(sizeof(ig)/sizeof(ig[0]));
2010-05-11 19:42:16 +00:00
for (int i = 0; i < sg; ++i)
ig[i].reset(new int(i));
r = std::rotate(Iter(ig), Iter(ig), Iter(ig+sg));
assert(base(r) == ig+sg);
assert(*ig[0] == 0);
assert(*ig[1] == 1);
assert(*ig[2] == 2);
assert(*ig[3] == 3);
assert(*ig[4] == 4);
assert(*ig[5] == 5);
r = std::rotate(Iter(ig), Iter(ig+1), Iter(ig+sg));
assert(base(r) == ig+5);
assert(*ig[0] == 1);
assert(*ig[1] == 2);
assert(*ig[2] == 3);
assert(*ig[3] == 4);
assert(*ig[4] == 5);
assert(*ig[5] == 0);
r = std::rotate(Iter(ig), Iter(ig+2), Iter(ig+sg));
assert(base(r) == ig+4);
assert(*ig[0] == 3);
assert(*ig[1] == 4);
assert(*ig[2] == 5);
assert(*ig[3] == 0);
assert(*ig[4] == 1);
assert(*ig[5] == 2);
r = std::rotate(Iter(ig), Iter(ig+3), Iter(ig+sg));
assert(base(r) == ig+3);
assert(*ig[0] == 0);
assert(*ig[1] == 1);
assert(*ig[2] == 2);
assert(*ig[3] == 3);
assert(*ig[4] == 4);
assert(*ig[5] == 5);
r = std::rotate(Iter(ig), Iter(ig+4), Iter(ig+sg));
assert(base(r) == ig+2);
assert(*ig[0] == 4);
assert(*ig[1] == 5);
assert(*ig[2] == 0);
assert(*ig[3] == 1);
assert(*ig[4] == 2);
assert(*ig[5] == 3);
r = std::rotate(Iter(ig), Iter(ig+5), Iter(ig+sg));
assert(base(r) == ig+1);
assert(*ig[0] == 3);
assert(*ig[1] == 4);
assert(*ig[2] == 5);
assert(*ig[3] == 0);
assert(*ig[4] == 1);
assert(*ig[5] == 2);
r = std::rotate(Iter(ig), Iter(ig+sg), Iter(ig+sg));
assert(base(r) == ig);
assert(*ig[0] == 3);
assert(*ig[1] == 4);
assert(*ig[2] == 5);
assert(*ig[3] == 0);
assert(*ig[4] == 1);
assert(*ig[5] == 2);
}
#endif // TEST_STD_VER >= 11
2010-05-11 19:42:16 +00:00
Support tests in freestanding Summary: Freestanding is *weird*. The standard allows it to differ in a bunch of odd manners from regular C++, and the committee would like to improve that situation. I'd like to make libc++ behave better with what freestanding should be, so that it can be a tool we use in improving the standard. To do that we need to try stuff out, both with "freestanding the language mode" and "freestanding the library subset". Let's start with the super basic: run the libc++ tests in freestanding, using clang as the compiler, and see what works. The easiest hack to do this: In utils/libcxx/test/config.py add: self.cxx.compile_flags += ['-ffreestanding'] Run the tests and they all fail. Why? Because in freestanding `main` isn't special. This "not special" property has two effects: main doesn't get mangled, and main isn't allowed to omit its `return` statement. The first means main gets mangled and the linker can't create a valid executable for us to test. The second means we spew out warnings (ew) and the compiler doesn't insert the `return` we omitted, and main just falls of the end and does whatever undefined behavior (if you're luck, ud2 leading to non-zero return code). Let's start my work with the basics. This patch changes all libc++ tests to declare `main` as `int main(int, char**` so it mangles consistently (enabling us to declare another `extern "C"` main for freestanding which calls the mangled one), and adds `return 0;` to all places where it was missing. This touches 6124 files, and I apologize. The former was done with The Magic Of Sed. The later was done with a (not quite correct but decent) clang tool: https://gist.github.com/jfbastien/793819ff360baa845483dde81170feed This works for most tests, though I did have to adjust a few places when e.g. the test runs with `-x c`, macros are used for main (such as for the filesystem tests), etc. Once this is in we can create a freestanding bot which will prevent further regressions. After that, we can start the real work of supporting C++ freestanding fairly well in libc++. <rdar://problem/47754795> Reviewers: ldionne, mclow.lists, EricWF Subscribers: christof, jkorous, dexonsmith, arphaman, miyuki, libcxx-commits Differential Revision: https://reviews.llvm.org/D57624 llvm-svn: 353086
2019-02-04 20:31:13 +00:00
int main(int, char**)
2010-05-11 19:42:16 +00:00
{
test<forward_iterator<int*> >();
test<bidirectional_iterator<int*> >();
test<random_access_iterator<int*> >();
test<int*>();
#if TEST_STD_VER >= 11
2010-05-11 19:42:16 +00:00
test1<forward_iterator<std::unique_ptr<int>*> >();
test1<bidirectional_iterator<std::unique_ptr<int>*> >();
test1<random_access_iterator<std::unique_ptr<int>*> >();
test1<std::unique_ptr<int>*>();
#endif
Support tests in freestanding Summary: Freestanding is *weird*. The standard allows it to differ in a bunch of odd manners from regular C++, and the committee would like to improve that situation. I'd like to make libc++ behave better with what freestanding should be, so that it can be a tool we use in improving the standard. To do that we need to try stuff out, both with "freestanding the language mode" and "freestanding the library subset". Let's start with the super basic: run the libc++ tests in freestanding, using clang as the compiler, and see what works. The easiest hack to do this: In utils/libcxx/test/config.py add: self.cxx.compile_flags += ['-ffreestanding'] Run the tests and they all fail. Why? Because in freestanding `main` isn't special. This "not special" property has two effects: main doesn't get mangled, and main isn't allowed to omit its `return` statement. The first means main gets mangled and the linker can't create a valid executable for us to test. The second means we spew out warnings (ew) and the compiler doesn't insert the `return` we omitted, and main just falls of the end and does whatever undefined behavior (if you're luck, ud2 leading to non-zero return code). Let's start my work with the basics. This patch changes all libc++ tests to declare `main` as `int main(int, char**` so it mangles consistently (enabling us to declare another `extern "C"` main for freestanding which calls the mangled one), and adds `return 0;` to all places where it was missing. This touches 6124 files, and I apologize. The former was done with The Magic Of Sed. The later was done with a (not quite correct but decent) clang tool: https://gist.github.com/jfbastien/793819ff360baa845483dde81170feed This works for most tests, though I did have to adjust a few places when e.g. the test runs with `-x c`, macros are used for main (such as for the filesystem tests), etc. Once this is in we can create a freestanding bot which will prevent further regressions. After that, we can start the real work of supporting C++ freestanding fairly well in libc++. <rdar://problem/47754795> Reviewers: ldionne, mclow.lists, EricWF Subscribers: christof, jkorous, dexonsmith, arphaman, miyuki, libcxx-commits Differential Revision: https://reviews.llvm.org/D57624 llvm-svn: 353086
2019-02-04 20:31:13 +00:00
#if TEST_STD_VER > 17
static_assert(test<forward_iterator<int*> >());
static_assert(test<bidirectional_iterator<int*> >());
static_assert(test<random_access_iterator<int*> >());
static_assert(test<int*>());
#endif // TEST_STD_VER > 17
Support tests in freestanding Summary: Freestanding is *weird*. The standard allows it to differ in a bunch of odd manners from regular C++, and the committee would like to improve that situation. I'd like to make libc++ behave better with what freestanding should be, so that it can be a tool we use in improving the standard. To do that we need to try stuff out, both with "freestanding the language mode" and "freestanding the library subset". Let's start with the super basic: run the libc++ tests in freestanding, using clang as the compiler, and see what works. The easiest hack to do this: In utils/libcxx/test/config.py add: self.cxx.compile_flags += ['-ffreestanding'] Run the tests and they all fail. Why? Because in freestanding `main` isn't special. This "not special" property has two effects: main doesn't get mangled, and main isn't allowed to omit its `return` statement. The first means main gets mangled and the linker can't create a valid executable for us to test. The second means we spew out warnings (ew) and the compiler doesn't insert the `return` we omitted, and main just falls of the end and does whatever undefined behavior (if you're luck, ud2 leading to non-zero return code). Let's start my work with the basics. This patch changes all libc++ tests to declare `main` as `int main(int, char**` so it mangles consistently (enabling us to declare another `extern "C"` main for freestanding which calls the mangled one), and adds `return 0;` to all places where it was missing. This touches 6124 files, and I apologize. The former was done with The Magic Of Sed. The later was done with a (not quite correct but decent) clang tool: https://gist.github.com/jfbastien/793819ff360baa845483dde81170feed This works for most tests, though I did have to adjust a few places when e.g. the test runs with `-x c`, macros are used for main (such as for the filesystem tests), etc. Once this is in we can create a freestanding bot which will prevent further regressions. After that, we can start the real work of supporting C++ freestanding fairly well in libc++. <rdar://problem/47754795> Reviewers: ldionne, mclow.lists, EricWF Subscribers: christof, jkorous, dexonsmith, arphaman, miyuki, libcxx-commits Differential Revision: https://reviews.llvm.org/D57624 llvm-svn: 353086
2019-02-04 20:31:13 +00:00
return 0;
2010-05-11 19:42:16 +00:00
}