| 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 | |
| 11 | // General tests for join_view. This file does not test anything specifically. |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <cassert> |
| 15 | #include <ranges> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "types.h" |
| 20 | |
| 21 | template <class R, class I> |
| 22 | bool isEqual(R& r, I i) { |
| 23 | for (auto e : r) |
| 24 | if (e != *i++) |
| 25 | return false; |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | int main(int, char**) { |
| 30 | { |
| 31 | int buffer[4][4] = {{1111, 2222, 3333, 4444}, {555, 666, 777, 888}, {99, 1010, 1111, 1212}, {13, 14, 15, 16}}; |
| 32 | int* flattened = reinterpret_cast<int*>(buffer); |
| 33 | |
| 34 | ChildView children[4] = {ChildView(buffer[0]), ChildView(buffer[1]), ChildView(buffer[2]), ChildView(buffer[3])}; |
| 35 | auto jv = std::ranges::join_view(ParentView(children)); |
| 36 | assert(isEqual(jv, flattened)); |
| 37 | } |
| 38 | |
| 39 | { |
| 40 | std::vector<std::string> vec = {"Hello" , "," , " " , "World" , "!" }; |
| 41 | std::string check = "Hello, World!" ; |
| 42 | std::ranges::join_view jv(vec); |
| 43 | assert(isEqual(jv, check.begin())); |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | // P2328R1 join_view should join all views of ranges |
| 48 | // join a range of prvalue containers |
| 49 | std::vector x{1, 2, 3, 4}; |
| 50 | auto y = x | std::views::transform([](auto i) { |
| 51 | std::vector<int> v(i); |
| 52 | for (int& ii : v) { |
| 53 | ii = i; |
| 54 | } |
| 55 | return v; |
| 56 | }); |
| 57 | |
| 58 | std::ranges::join_view jv(y); |
| 59 | std::vector<int> check{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}; |
| 60 | assert(isEqual(jv, check.begin())); |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |