libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

fill.pass.cpp (1771B)


      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, class T>
     13 //   requires OutputIterator<Iter, const T&>
     14 //   constexpr void      // constexpr after C++17
     15 //   fill(Iter first, Iter last, const T& value);
     16 
     17 #include <algorithm>
     18 #include <cassert>
     19 
     20 #include "test_macros.h"
     21 #include "test_iterators.h"
     22 
     23 #if TEST_STD_VER > 17
     24 TEST_CONSTEXPR bool test_constexpr() {
     25     int ia[] = {0, 1, 2, 3, 4};
     26 
     27     std::fill(std::begin(ia), std::end(ia), 5);
     28 
     29     return std::all_of(std::begin(ia), std::end(ia), [](int a) {return a == 5; })
     30         ;
     31     }
     32 #endif
     33 
     34 template <class Iter>
     35 void
     36 test_char()
     37 {
     38     const unsigned n = 4;
     39     char ca[n] = {0};
     40     std::fill(Iter(ca), Iter(ca+n), char(1));
     41     assert(ca[0] == 1);
     42     assert(ca[1] == 1);
     43     assert(ca[2] == 1);
     44     assert(ca[3] == 1);
     45 }
     46 
     47 template <class Iter>
     48 void
     49 test_int()
     50 {
     51     const unsigned n = 4;
     52     int ia[n] = {0};
     53     std::fill(Iter(ia), Iter(ia+n), 1);
     54     assert(ia[0] == 1);
     55     assert(ia[1] == 1);
     56     assert(ia[2] == 1);
     57     assert(ia[3] == 1);
     58 }
     59 
     60 int main()
     61 {
     62     test_char<forward_iterator<char*> >();
     63     test_char<bidirectional_iterator<char*> >();
     64     test_char<random_access_iterator<char*> >();
     65     test_char<char*>();
     66 
     67     test_int<forward_iterator<int*> >();
     68     test_int<bidirectional_iterator<int*> >();
     69     test_int<random_access_iterator<int*> >();
     70     test_int<int*>();
     71 
     72 #if TEST_STD_VER > 17
     73     static_assert(test_constexpr());
     74 #endif
     75 }