//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // UNSUPPORTED: c++03, c++11, c++14, c++17 // std::views::common #include #include #include #include #include #include "test_iterators.h" #include "test_range.h" #include "types.h" constexpr bool test() { int buf[] = {1, 2, 3}; // views::common(r) is equivalent to views::all(r) if r is a common_range { { CommonView view(buf, buf + 3); std::same_as auto result = std::views::common(view); assert(result.begin_ == buf); assert(result.end_ == buf + 3); } { using NotAView = std::array; NotAView arr = {1, 2, 3}; std::same_as> auto result = std::views::common(arr); assert(result.begin() == arr.begin()); assert(result.end() == arr.end()); } } // Otherwise, views::common(r) is equivalent to ranges::common_view{r} { NonCommonView view(buf, buf + 3); std::same_as> auto result = std::views::common(view); assert(result.base().begin_ == buf); assert(result.base().end_ == buf + 3); } // Test that std::views::common is a range adaptor { using SomeView = NonCommonView; // Test `v | views::common` { SomeView view(buf, buf + 3); std::same_as> auto result = view | std::views::common; assert(result.base().begin_ == buf); assert(result.base().end_ == buf + 3); } // Test `adaptor | views::common` { SomeView view(buf, buf + 3); auto f = [](int i) { return i; }; auto const partial = std::views::transform(f) | std::views::common; using Result = std::ranges::common_view>; std::same_as auto result = partial(view); assert(result.base().base().begin_ == buf); assert(result.base().base().end_ == buf + 3); } // Test `views::common | adaptor` { SomeView view(buf, buf + 3); auto f = [](int i) { return i; }; auto const partial = std::views::common | std::views::transform(f); using Result = std::ranges::transform_view, decltype(f)>; std::same_as auto result = partial(view); assert(result.base().base().begin_ == buf); assert(result.base().base().end_ == buf + 3); } // Check SFINAE friendliness { struct NotAView { }; static_assert(!std::is_invocable_v); static_assert(!std::is_invocable_v); static_assert( CanBePiped); static_assert( CanBePiped); static_assert(!CanBePiped); static_assert(!CanBePiped); } } { static_assert(std::same_as); } return true; } int main(int, char**) { test(); static_assert(test()); return 0; }