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