130 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11// std::views::lazy_split12 13#include <ranges>14 15#include <array>16#include <cassert>17#include <concepts>18#include <string_view>19#include <utility>20 21#include "test_iterators.h"22#include "test_range.h"23#include "types.h"24 25struct SomeView : std::ranges::view_base {26 const std::string_view* v_;27 constexpr SomeView(const std::string_view& v) : v_(&v) {}28 constexpr auto begin() const { return v_->begin(); }29 constexpr auto end() const { return v_->end(); }30};31 32struct NotAView { };33 34static_assert(!std::is_invocable_v<decltype(std::views::lazy_split)>);35static_assert(!std::is_invocable_v<decltype(std::views::lazy_split), SomeView, NotAView>);36static_assert(!std::is_invocable_v<decltype(std::views::lazy_split), NotAView, SomeView>);37static_assert( std::is_invocable_v<decltype(std::views::lazy_split), SomeView, SomeView>);38 39// Regression test for #75002, views::lazy_split shouldn't be a range adaptor closure40static_assert(!CanBePiped<SomeView&, decltype(std::views::lazy_split)>);41static_assert(!CanBePiped<char (&)[10], decltype(std::views::lazy_split)>);42static_assert(!CanBePiped<char (&&)[10], decltype(std::views::lazy_split)>);43static_assert(!CanBePiped<NotAView, decltype(std::views::lazy_split)>);44 45static_assert(CanBePiped<SomeView&, decltype(std::views::lazy_split('x'))>);46static_assert(CanBePiped<char (&)[10], decltype(std::views::lazy_split('x'))>);47static_assert(!CanBePiped<char (&&)[10], decltype(std::views::lazy_split('x'))>);48static_assert(!CanBePiped<NotAView, decltype(std::views::lazy_split('x'))>);49 50static_assert(std::same_as<decltype(std::views::lazy_split), decltype(std::ranges::views::lazy_split)>);51 52constexpr bool test() {53 std::string_view input = "abc";54 std::string_view sep = "a";55 56 // Test that `std::views::lazy_split` is a range adaptor.57 58 // Test `views::lazy_split(input, sep)`.59 {60 SomeView view(input);61 62 using Result = std::ranges::lazy_split_view<SomeView, std::string_view>;63 std::same_as<Result> decltype(auto) result = std::views::lazy_split(view, sep);64 assert(result.base().begin() == input.begin());65 assert(result.base().end() == input.end());66 }67 68 // Test `views::lazy_split(sep)(input)`.69 {70 SomeView view(input);71 72 using Result = std::ranges::lazy_split_view<SomeView, std::string_view>;73 std::same_as<Result> decltype(auto) result = std::views::lazy_split(sep)(view);74 assert(result.base().begin() == input.begin());75 assert(result.base().end() == input.end());76 }77 78 // Test `view | views::lazy_split`.79 {80 SomeView view(input);81 82 using Result = std::ranges::lazy_split_view<SomeView, std::string_view>;83 std::same_as<Result> decltype(auto) result = view | std::views::lazy_split(sep);84 assert(result.base().begin() == input.begin());85 assert(result.base().end() == input.end());86 }87 88 // Test `adaptor | views::lazy_split`.89 {90 SomeView view(input);91 auto f = [](char c) { return c; };92 auto partial = std::views::transform(f) | std::views::lazy_split(sep);93 94 using Result = std::ranges::lazy_split_view<std::ranges::transform_view<SomeView, decltype(f)>, std::string_view>;95 std::same_as<Result> decltype(auto) result = partial(view);96 assert(result.base().base().begin() == input.begin());97 assert(result.base().base().end() == input.end());98 }99 100 // Test `views::lazy_split | adaptor`.101 {102 SomeView view(input);103 auto f = [](auto v) { return v; };104 auto partial = std::views::lazy_split(sep) | std::views::transform(f);105 106 using Result = std::ranges::transform_view<std::ranges::lazy_split_view<SomeView, std::string_view>, decltype(f)>;107 std::same_as<Result> decltype(auto) result = partial(view);108 assert(result.base().base().begin() == input.begin());109 assert(result.base().base().end() == input.end());110 }111 112 // Test that one can call `std::views::lazy_split` with arbitrary stuff, as long as we113 // don't try to actually complete the call by passing it a range.114 //115 // That makes no sense and we can't do anything with the result, but it's valid.116 {117 struct X { };118 [[maybe_unused]] auto partial = std::views::lazy_split(X{});119 }120 121 return true;122}123 124int main(int, char**) {125 test();126 static_assert(test());127 128 return 0;129}130