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 // <list> 11 12 // template <class Compare> sort(Compare comp); 13 14 #include <list> 15 #include <functional> 16 #include <algorithm> // for is_permutation 17 #include <cassert> 18 19 #include "min_allocator.h" 20 21 22 #ifndef TEST_HAS_NO_EXCEPTIONS 23 template <typename T> 24 struct throwingLess { 25 throwingLess() : num_(1) {} 26 throwingLess(int num) : num_(num) {} 27 28 bool operator() (const T& lhs, const T& rhs) const 29 { 30 if ( --num_ == 0) throw 1; 31 return lhs < rhs; 32 } 33 34 mutable int num_; 35 }; 36 #endif 37 38 39 int main() 40 { 41 { 42 int a1[] = {4, 8, 1, 0, 5, 7, 2, 3, 6, 11, 10, 9}; 43 int a2[] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; 44 std::list<int> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 45 c1.sort(std::greater<int>()); 46 assert(c1 == std::list<int>(a2, a2+sizeof(a2)/sizeof(a2[0]))); 47 } 48 49 // Test with throwing comparison; make sure that nothing is lost. 50 // This is (sort of) LWG #2824 51 #ifndef TEST_HAS_NO_EXCEPTIONS 52 { 53 int a1[] = {4, 8, 1, 0, 5, 7, 2, 3, 6, 11, 10, 9}; 54 const int sz = sizeof(a1)/sizeof(a1[0]); 55 for (int i = 0; i < 10; ++i) 56 { 57 std::list<int> c1(a1, a1 + sz); 58 try 59 { 60 throwingLess<int> comp(i); 61 c1.sort(std::cref(comp)); 62 } 63 catch (int) {} 64 assert((c1.size() == sz)); 65 assert((std::is_permutation(c1.begin(), c1.end(), a1))); 66 } 67 } 68 #endif 69 70 #if TEST_STD_VER >= 11 71 { 72 int a1[] = {4, 8, 1, 0, 5, 7, 2, 3, 6, 11, 10, 9}; 73 int a2[] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; 74 std::list<int, min_allocator<int>> c1(a1, a1+sizeof(a1)/sizeof(a1[0])); 75 c1.sort(std::greater<int>()); 76 assert((c1 == std::list<int, min_allocator<int>>(a2, a2+sizeof(a2)/sizeof(a2[0])))); 77 } 78 #endif 79 } 80