//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // template Compare> // requires ShuffleIterator // && CopyConstructible // void // inplace_merge(Iter first, Iter middle, Iter last, Compare comp); #include #include #include #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES #include struct indirect_less { template bool operator()(const P& x, const P& y) {return *x < *y;} }; #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES #include "test_iterators.h" template void test_one(unsigned N, unsigned M) { assert(M <= N); int* ia = new int[N]; for (unsigned i = 0; i < N; ++i) ia[i] = i; std::random_shuffle(ia, ia+N); std::sort(ia, ia+M, std::greater()); std::sort(ia+M, ia+N, std::greater()); std::inplace_merge(Iter(ia), Iter(ia+M), Iter(ia+N), std::greater()); if(N > 0) { assert(ia[0] == N-1); assert(ia[N-1] == 0); assert(std::is_sorted(ia, ia+N, std::greater())); } delete [] ia; } template void test(unsigned N) { test_one(N, 0); test_one(N, N/4); test_one(N, N/2); test_one(N, 3*N/4); test_one(N, N); } template void test() { test_one(0, 0); test_one(1, 0); test_one(1, 1); test_one(2, 0); test_one(2, 1); test_one(2, 2); test_one(3, 0); test_one(3, 1); test_one(3, 2); test_one(3, 3); test(4); test(100); test(1000); } int main() { test >(); test >(); test(); #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { unsigned N = 100; unsigned M = 50; std::unique_ptr* ia = new std::unique_ptr[N]; for (unsigned i = 0; i < N; ++i) ia[i].reset(new int(i)); std::random_shuffle(ia, ia+N); std::sort(ia, ia+M, indirect_less()); std::sort(ia+M, ia+N, indirect_less()); std::inplace_merge(ia, ia+M, ia+N, indirect_less()); if(N > 0) { assert(*ia[0] == 0); assert(*ia[N-1] == N-1); assert(std::is_sorted(ia, ia+N, indirect_less())); } delete [] ia; } #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES }