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 // bool 16 // prev_permutation(Iter first, Iter last); 17 18 #include <algorithm> 19 #include <cassert> 20 21 #include "test_iterators.h" 22 23 #include <cstdio> 24 25 int factorial(int x) 26 { 27 int r = 1; 28 for (; x; --x) 29 r *= x; 30 return r; 31 } 32 33 template <class Iter> 34 void 35 test() 36 { 37 int ia[] = {6, 5, 4, 3, 2, 1}; 38 const int sa = sizeof(ia)/sizeof(ia[0]); 39 int prev[sa]; 40 for (int e = 0; e <= sa; ++e) 41 { 42 int count = 0; 43 bool x; 44 do 45 { 46 std::copy(ia, ia+e, prev); 47 x = std::prev_permutation(Iter(ia), Iter(ia+e)); 48 if (e > 1) 49 { 50 if (x) 51 assert(std::lexicographical_compare(ia, ia+e, prev, prev+e)); 52 else 53 assert(std::lexicographical_compare(prev, prev+e, ia, ia+e)); 54 } 55 ++count; 56 } while (x); 57 assert(count == factorial(e)); 58 } 59 } 60 61 int main() 62 { 63 test<bidirectional_iterator<int*> >(); 64 test<random_access_iterator<int*> >(); 65 test<int*>(); 66 } 67