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