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<ForwardIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare> 13// requires CopyConstructible<Compare> 14// bool 15// is_sorted(Iter first, Iter last, Compare comp); 16 17#include <algorithm> 18#include <functional> 19#include <cassert> 20 21#include "test_iterators.h" 22 23#if TEST_STD_VER > 17 24TEST_CONSTEXPR bool test_constexpr() { 25 int ia[] = {1, 1, 0, 0}; 26 int ib[] = {0, 0, 1, 1}; 27 return std::is_sorted(std::begin(ia), std::end(ia), std::greater<int>()) 28 && !std::is_sorted(std::begin(ib), std::end(ib), std::greater<int>()); 29 } 30#endif 31 32template <class Iter> 33void 34test() 35{ 36 { 37 int a[] = {0}; 38 unsigned sa = sizeof(a) / sizeof(a[0]); 39 assert(std::is_sorted(Iter(a), Iter(a))); 40 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 41 } 42 43 { 44 int a[] = {0, 0}; 45 unsigned sa = sizeof(a) / sizeof(a[0]); 46 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 47 } 48 { 49 int a[] = {0, 1}; 50 unsigned sa = sizeof(a) / sizeof(a[0]); 51 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 52 } 53 { 54 int a[] = {1, 0}; 55 unsigned sa = sizeof(a) / sizeof(a[0]); 56 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 57 } 58 { 59 int a[] = {1, 1}; 60 unsigned sa = sizeof(a) / sizeof(a[0]); 61 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 62 } 63 64 { 65 int a[] = {0, 0, 0}; 66 unsigned sa = sizeof(a) / sizeof(a[0]); 67 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 68 } 69 { 70 int a[] = {0, 0, 1}; 71 unsigned sa = sizeof(a) / sizeof(a[0]); 72 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 73 } 74 { 75 int a[] = {0, 1, 0}; 76 unsigned sa = sizeof(a) / sizeof(a[0]); 77 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 78 } 79 { 80 int a[] = {0, 1, 1}; 81 unsigned sa = sizeof(a) / sizeof(a[0]); 82 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 83 } 84 { 85 int a[] = {1, 0, 0}; 86 unsigned sa = sizeof(a) / sizeof(a[0]); 87 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 88 } 89 { 90 int a[] = {1, 0, 1}; 91 unsigned sa = sizeof(a) / sizeof(a[0]); 92 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 93 } 94 { 95 int a[] = {1, 1, 0}; 96 unsigned sa = sizeof(a) / sizeof(a[0]); 97 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 98 } 99 { 100 int a[] = {1, 1, 1}; 101 unsigned sa = sizeof(a) / sizeof(a[0]); 102 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 103 } 104 105 { 106 int a[] = {0, 0, 0, 0}; 107 unsigned sa = sizeof(a) / sizeof(a[0]); 108 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 109 } 110 { 111 int a[] = {0, 0, 0, 1}; 112 unsigned sa = sizeof(a) / sizeof(a[0]); 113 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 114 } 115 { 116 int a[] = {0, 0, 1, 0}; 117 unsigned sa = sizeof(a) / sizeof(a[0]); 118 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 119 } 120 { 121 int a[] = {0, 0, 1, 1}; 122 unsigned sa = sizeof(a) / sizeof(a[0]); 123 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 124 } 125 { 126 int a[] = {0, 1, 0, 0}; 127 unsigned sa = sizeof(a) / sizeof(a[0]); 128 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 129 } 130 { 131 int a[] = {0, 1, 0, 1}; 132 unsigned sa = sizeof(a) / sizeof(a[0]); 133 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 134 } 135 { 136 int a[] = {0, 1, 1, 0}; 137 unsigned sa = sizeof(a) / sizeof(a[0]); 138 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 139 } 140 { 141 int a[] = {0, 1, 1, 1}; 142 unsigned sa = sizeof(a) / sizeof(a[0]); 143 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 144 } 145 { 146 int a[] = {1, 0, 0, 0}; 147 unsigned sa = sizeof(a) / sizeof(a[0]); 148 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 149 } 150 { 151 int a[] = {1, 0, 0, 1}; 152 unsigned sa = sizeof(a) / sizeof(a[0]); 153 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 154 } 155 { 156 int a[] = {1, 0, 1, 0}; 157 unsigned sa = sizeof(a) / sizeof(a[0]); 158 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 159 } 160 { 161 int a[] = {1, 0, 1, 1}; 162 unsigned sa = sizeof(a) / sizeof(a[0]); 163 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 164 } 165 { 166 int a[] = {1, 1, 0, 0}; 167 unsigned sa = sizeof(a) / sizeof(a[0]); 168 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 169 } 170 { 171 int a[] = {1, 1, 0, 1}; 172 unsigned sa = sizeof(a) / sizeof(a[0]); 173 assert(!std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 174 } 175 { 176 int a[] = {1, 1, 1, 0}; 177 unsigned sa = sizeof(a) / sizeof(a[0]); 178 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 179 } 180 { 181 int a[] = {1, 1, 1, 1}; 182 unsigned sa = sizeof(a) / sizeof(a[0]); 183 assert(std::is_sorted(Iter(a), Iter(a+sa), std::greater<int>())); 184 } 185} 186 187int main() 188{ 189 test<forward_iterator<const int*> >(); 190 test<bidirectional_iterator<const int*> >(); 191 test<random_access_iterator<const int*> >(); 192 test<const int*>(); 193 194#if TEST_STD_VER > 17 195 static_assert(test_constexpr()); 196#endif 197} 198