1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <algorithm> 11 12 // template<BidirectionalIterator Iter> 13 // requires ShuffleIterator<Iter> 14 // && LessThanComparable<Iter::value_type> 15 // void 16 // inplace_merge(Iter first, Iter middle, Iter last); 17 18 #include <algorithm> 19 #include <cassert> 20 21 #include "test_iterators.h" 22 23 template <class Iter> 24 void 25 test_one(unsigned N, unsigned M) 26 { 27 assert(M <= N); 28 int* ia = new int[N]; 29 for (unsigned i = 0; i < N; ++i) 30 ia[i] = i; 31 std::random_shuffle(ia, ia+N); 32 std::sort(ia, ia+M); 33 std::sort(ia+M, ia+N); 34 std::inplace_merge(Iter(ia), Iter(ia+M), Iter(ia+N)); 35 if(N > 0) 36 { 37 assert(ia[0] == 0); 38 assert(ia[N-1] == N-1); 39 assert(std::is_sorted(ia, ia+N)); 40 } 41 delete [] ia; 42 } 43 44 template <class Iter> 45 void 46 test(unsigned N) 47 { 48 test_one<Iter>(N, 0); 49 test_one<Iter>(N, N/4); 50 test_one<Iter>(N, N/2); 51 test_one<Iter>(N, 3*N/4); 52 test_one<Iter>(N, N); 53 } 54 55 template <class Iter> 56 void 57 test() 58 { 59 test_one<Iter>(0, 0); 60 test_one<Iter>(1, 0); 61 test_one<Iter>(1, 1); 62 test_one<Iter>(2, 0); 63 test_one<Iter>(2, 1); 64 test_one<Iter>(2, 2); 65 test_one<Iter>(3, 0); 66 test_one<Iter>(3, 1); 67 test_one<Iter>(3, 2); 68 test_one<Iter>(3, 3); 69 test<Iter>(4); 70 test<Iter>(100); 71 test<Iter>(1000); 72 } 73 74 int main() 75 { 76 test<bidirectional_iterator<int*> >(); 77 test<random_access_iterator<int*> >(); 78 test<int*>(); 79 } 80