1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: libcpp-no-concepts
11 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
12 
13 // Some basic examples of how transform_view might be used in the wild. This is a general
14 // collection of sample algorithms and functions that try to mock general usage of
15 // this view.
16 
17 #include <ranges>
18 
19 #include <cctype>
20 #include <functional>
21 #include <list>
22 #include <numeric>
23 #include <string>
24 #include <vector>
25 
26 #include <cassert>
27 #include "test_macros.h"
28 #include "test_iterators.h"
29 #include "types.h"
30 
31 template<class T, class F>
32 concept ValidTransformView = requires { typename std::ranges::transform_view<T, F>; };
33 
34 struct BadFunction { };
35 static_assert( ValidTransformView<MoveOnlyView, PlusOne>);
36 static_assert(!ValidTransformView<Range, PlusOne>);
37 static_assert(!ValidTransformView<MoveOnlyView, BadFunction>);
38 
39 template<std::ranges::range R>
40 auto toUpper(R range) {
41   return std::ranges::transform_view(range, [](char c) { return std::toupper(c); });
42 }
43 
44 template<class E1, class E2, size_t N, class Join = std::plus<E1>>
45 auto joinArrays(E1 (&a)[N], E2 (&b)[N], Join join = Join()) {
46   return std::ranges::transform_view(a, [&a, &b, join](auto& x) {
47     auto idx = (&x) - a;
48     return join(x, b[idx]);
49   });
50 }
51 
52 struct NonConstView : std::ranges::view_base {
53   explicit NonConstView(int *b, int *e) : b_(b), e_(e) {}
54   const int *begin() { return b_; }  // deliberately non-const
55   const int *end() { return e_; }    // deliberately non-const
56   const int *b_;
57   const int *e_;
58 };
59 
60 int main(int, char**) {
61   {
62     std::vector<int> vec = {1, 2, 3, 4};
63     auto transformed = std::ranges::transform_view(vec, [](int x) { return x + 42; });
64     int expected[] = {43, 44, 45, 46};
65     assert(std::equal(transformed.begin(), transformed.end(), expected, expected + 4));
66     const auto& ct = transformed;
67     assert(std::equal(ct.begin(), ct.end(), expected, expected + 4));
68   }
69 
70   {
71     // Test a view type that is not const-iterable.
72     int a[] = {1, 2, 3, 4};
73     auto transformed = NonConstView(a, a + 4) | std::views::transform([](int x) { return x + 42; });
74     int expected[4] = {43, 44, 45, 46};
75     assert(std::equal(transformed.begin(), transformed.end(), expected, expected + 4));
76   }
77 
78   {
79     int a[4] = {1, 2, 3, 4};
80     int b[4] = {4, 3, 2, 1};
81     auto out = joinArrays(a, b);
82     int check[4] = {5, 5, 5, 5};
83     assert(std::equal(out.begin(), out.end(), check, check + 4));
84   }
85 
86   {
87     std::string_view str = "Hello, World.";
88     auto upp = toUpper(str);
89     std::string_view check = "HELLO, WORLD.";
90     assert(std::equal(upp.begin(), upp.end(), check.begin(), check.end()));
91   }
92 
93   return 0;
94 }
95