File: for_each.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (100 lines) | stat: -rw-r--r-- 2,826 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <algorithm>

// template<InputIterator Iter, Callable<auto, Iter::reference> Function>
//   constexpr Function   // constexpr since C++20
//   for_each(Iter first, Iter last, Function f);

#include <algorithm>
#include <cassert>
#include <deque>
#if __has_include(<ranges>)
#  include <ranges>
#endif
#include <vector>

#include "test_macros.h"
#include "test_iterators.h"

struct for_each_test {
  TEST_CONSTEXPR for_each_test(int c) : count(c) {}

  // for_each functors only have to be move constructible
  for_each_test(const for_each_test&)            = delete;
  for_each_test(for_each_test&&)                 = default;
  for_each_test& operator=(const for_each_test&) = delete;
  for_each_test& operator=(for_each_test&&)      = delete;

  int count;
  TEST_CONSTEXPR_CXX14 void operator()(int& i) {
    ++i;
    ++count;
  }
};

struct Test {
  template <class Iter>
  TEST_CONSTEXPR_CXX20 void operator()() {
    int sizes[] = {0, 1, 6};
    for (const int size : sizes) {
      int ia[]        = {0, 1, 2, 3, 4, 5};
      for_each_test f = std::for_each(Iter(ia), Iter(ia + size), for_each_test(0));
      assert(f.count == size);
      for (int i = 0; i < size; ++i)
        assert(ia[i] == static_cast<int>(i + 1));
    }
  }
};

TEST_CONSTEXPR_CXX20 bool test() {
  types::for_each(types::cpp17_input_iterator_list<int*>(), Test());

  // TODO: Remove the `_LIBCPP_ENABLE_EXPERIMENTAL` check once we have the FTM guarded or views::join isn't
  // experimental anymore
#if TEST_STD_VER >= 20 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
  { // Make sure that the segmented iterator optimization works during constant evaluation
    std::vector<std::vector<int>> vecs = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    auto v                             = std::views::join(vecs);
    std::for_each(v.begin(), v.end(), [i = 0](int& a) mutable { assert(a == ++i); });
  }
#endif

  return true;
}

struct deque_test {
  std::deque<int>* d_;
  int* i_;

  deque_test(std::deque<int>& d, int& i) : d_(&d), i_(&i) {}

  void operator()(int& v) {
    assert(&(*d_)[*i_] == &v);
    ++*i_;
  }
};

int main(int, char**) {
  test();
#if TEST_STD_VER >= 20
  static_assert(test());
#endif

  // check that segmented iterators work properly
  int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};
  for (const int size : sizes) {
    std::deque<int> d(size);
    int index = 0;

    std::for_each(d.begin(), d.end(), deque_test(d, index));
  }

  return 0;
}