Home | History | Annotate | Download | only in is.sorted
      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 //   Iter
     15 //   is_sorted_until(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
     24 TEST_CONSTEXPR bool test_constexpr() {
     25     int ia[] = {1, 0, 1};
     26     int ib[] = {1, 1, 0};
     27     return    (std::is_sorted_until(std::begin(ia), std::end(ia), std::greater<int>()) == ia+2)
     28            && (std::is_sorted_until(std::begin(ib), std::end(ib), std::greater<int>()) == ib+3);
     29     }
     30 #endif
     31 
     32 template <class Iter>
     33 void
     34 test()
     35 {
     36     {
     37     int a[] = {0};
     38     unsigned sa = sizeof(a) / sizeof(a[0]);
     39     assert(std::is_sorted_until(Iter(a), Iter(a), std::greater<int>()) == Iter(a));
     40     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
     41     }
     42 
     43     {
     44     int a[] = {0, 0};
     45     unsigned sa = sizeof(a) / sizeof(a[0]);
     46     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
     47     }
     48     {
     49     int a[] = {0, 1};
     50     unsigned sa = sizeof(a) / sizeof(a[0]);
     51     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+1));
     52     }
     53     {
     54     int a[] = {1, 0};
     55     unsigned sa = sizeof(a) / sizeof(a[0]);
     56     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
     57     }
     58     {
     59     int a[] = {1, 1};
     60     unsigned sa = sizeof(a) / sizeof(a[0]);
     61     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
     62     }
     63 
     64     {
     65     int a[] = {0, 0, 0};
     66     unsigned sa = sizeof(a) / sizeof(a[0]);
     67     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
     68     }
     69     {
     70     int a[] = {0, 0, 1};
     71     unsigned sa = sizeof(a) / sizeof(a[0]);
     72     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+2));
     73     }
     74     {
     75     int a[] = {0, 1, 0};
     76     unsigned sa = sizeof(a) / sizeof(a[0]);
     77     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+1));
     78     }
     79     {
     80     int a[] = {0, 1, 1};
     81     unsigned sa = sizeof(a) / sizeof(a[0]);
     82     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+1));
     83     }
     84     {
     85     int a[] = {1, 0, 0};
     86     unsigned sa = sizeof(a) / sizeof(a[0]);
     87     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
     88     }
     89     {
     90     int a[] = {1, 0, 1};
     91     unsigned sa = sizeof(a) / sizeof(a[0]);
     92     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+2));
     93     }
     94     {
     95     int a[] = {1, 1, 0};
     96     unsigned sa = sizeof(a) / sizeof(a[0]);
     97     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
     98     }
     99     {
    100     int a[] = {1, 1, 1};
    101     unsigned sa = sizeof(a) / sizeof(a[0]);
    102     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
    103     }
    104 
    105     {
    106     int a[] = {0, 0, 0, 0};
    107     unsigned sa = sizeof(a) / sizeof(a[0]);
    108     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
    109     }
    110     {
    111     int a[] = {0, 0, 0, 1};
    112     unsigned sa = sizeof(a) / sizeof(a[0]);
    113     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+3));
    114     }
    115     {
    116     int a[] = {0, 0, 1, 0};
    117     unsigned sa = sizeof(a) / sizeof(a[0]);
    118     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+2));
    119     }
    120     {
    121     int a[] = {0, 0, 1, 1};
    122     unsigned sa = sizeof(a) / sizeof(a[0]);
    123     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+2));
    124     }
    125     {
    126     int a[] = {0, 1, 0, 0};
    127     unsigned sa = sizeof(a) / sizeof(a[0]);
    128     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+1));
    129     }
    130     {
    131     int a[] = {0, 1, 0, 1};
    132     unsigned sa = sizeof(a) / sizeof(a[0]);
    133     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+1));
    134     }
    135     {
    136     int a[] = {0, 1, 1, 0};
    137     unsigned sa = sizeof(a) / sizeof(a[0]);
    138     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+1));
    139     }
    140     {
    141     int a[] = {0, 1, 1, 1};
    142     unsigned sa = sizeof(a) / sizeof(a[0]);
    143     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+1));
    144     }
    145     {
    146     int a[] = {1, 0, 0, 0};
    147     unsigned sa = sizeof(a) / sizeof(a[0]);
    148     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
    149     }
    150     {
    151     int a[] = {1, 0, 0, 1};
    152     unsigned sa = sizeof(a) / sizeof(a[0]);
    153     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+3));
    154     }
    155     {
    156     int a[] = {1, 0, 1, 0};
    157     unsigned sa = sizeof(a) / sizeof(a[0]);
    158     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+2));
    159     }
    160     {
    161     int a[] = {1, 0, 1, 1};
    162     unsigned sa = sizeof(a) / sizeof(a[0]);
    163     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+2));
    164     }
    165     {
    166     int a[] = {1, 1, 0, 0};
    167     unsigned sa = sizeof(a) / sizeof(a[0]);
    168     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
    169     }
    170     {
    171     int a[] = {1, 1, 0, 1};
    172     unsigned sa = sizeof(a) / sizeof(a[0]);
    173     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+3));
    174     }
    175     {
    176     int a[] = {1, 1, 1, 0};
    177     unsigned sa = sizeof(a) / sizeof(a[0]);
    178     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
    179     }
    180     {
    181     int a[] = {1, 1, 1, 1};
    182     unsigned sa = sizeof(a) / sizeof(a[0]);
    183     assert(std::is_sorted_until(Iter(a), Iter(a+sa), std::greater<int>()) == Iter(a+sa));
    184     }
    185 }
    186 
    187 int 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