148 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++17, c++2010 11// zip_transform_view() = default;12 13#include <ranges>14 15#include <cassert>16#include <type_traits>17 18#include "types.h"19 20constexpr int buff[] = {1, 2, 3};21 22struct DefaultConstructibleView : std::ranges::view_base {23 constexpr DefaultConstructibleView() : begin_(buff), end_(buff + 3) {}24 constexpr int const* begin() const { return begin_; }25 constexpr int const* end() const { return end_; }26 27private:28 int const* begin_;29 int const* end_;30};31 32struct NonDefaultConstructibleView : std::ranges::view_base {33 NonDefaultConstructibleView() = delete;34 int* begin() const;35 int* end() const;36};37 38struct DefaultConstructibleFn {39 constexpr int operator()(const auto&... x) const { return (x + ...); }40};41 42struct NonDefaultConstructibleFn {43 NonDefaultConstructibleFn() = delete;44 constexpr int operator()(const auto&... x) const;45};46 47// The default constructor requires all underlying views to be default constructible.48// It is implicitly required by the zip_view's constructor.49static_assert(std::is_default_constructible_v<std::ranges::zip_transform_view< //50 DefaultConstructibleFn, //51 DefaultConstructibleView>>);52static_assert(std::is_default_constructible_v<std::ranges::zip_transform_view< //53 DefaultConstructibleFn, //54 DefaultConstructibleView,55 DefaultConstructibleView>>);56static_assert(!std::is_default_constructible_v<std::ranges::zip_transform_view< //57 NonDefaultConstructibleFn, //58 DefaultConstructibleView>>);59static_assert(!std::is_default_constructible_v<std::ranges::zip_transform_view< //60 DefaultConstructibleFn, //61 NonDefaultConstructibleView>>);62static_assert(!std::is_default_constructible_v<std::ranges::zip_transform_view< //63 DefaultConstructibleFn, //64 DefaultConstructibleView,65 NonDefaultConstructibleView>>);66 67constexpr bool test() {68 {69 using View =70 std::ranges::zip_transform_view<DefaultConstructibleFn, DefaultConstructibleView, DefaultConstructibleView>;71 View v = View(); // the default constructor is not explicit72 assert(v.size() == 3);73 auto it = v.begin();74 assert(*it++ == 2);75 assert(*it++ == 4);76 assert(*it == 6);77 }78 79 {80 // one range81 using View = std::ranges::zip_transform_view<MakeTuple, DefaultConstructibleView>;82 View v = View(); // the default constructor is not explicit83 auto it = v.begin();84 assert(*it == std::make_tuple(1));85 }86 87 {88 // two ranges89 using View = std::ranges::zip_transform_view<MakeTuple, DefaultConstructibleView, std::ranges::iota_view<int>>;90 View v = View(); // the default constructor is not explicit91 auto it = v.begin();92 assert(*it == std::tuple(1, 0));93 }94 95 {96 // three ranges97 using View = std::ranges::98 zip_transform_view<MakeTuple, DefaultConstructibleView, DefaultConstructibleView, std::ranges::iota_view<int>>;99 View v = View(); // the default constructor is not explicit100 auto it = v.begin();101 assert(*it == std::tuple(1, 1, 0));102 }103 104 {105 // single empty range106 std::ranges::zip_transform_view v(MakeTuple{}, std::ranges::empty_view<int>());107 assert(v.begin() == v.end());108 assert(std::as_const(v).begin() == std::as_const(v).end());109 }110 111 {112 // empty range at the beginning113 using View = std::ranges::114 zip_transform_view<MakeTuple, std::ranges::empty_view<int>, DefaultConstructibleView, DefaultConstructibleView>;115 View v = View(); // the default constructor is not explicit116 assert(v.empty());117 }118 119 {120 // empty range in the middle121 using View =122 std::ranges::zip_transform_view<MakeTuple,123 DefaultConstructibleView,124 std::ranges::empty_view<int>,125 DefaultConstructibleView,126 DefaultConstructibleView>;127 View v = View(); // the default constructor is not explicit128 assert(v.empty());129 }130 131 {132 // empty range at the end133 using View = std::ranges::134 zip_transform_view<MakeTuple, DefaultConstructibleView, DefaultConstructibleView, std::ranges::empty_view<int>>;135 View v = View(); // the default constructor is not explicit136 assert(v.empty());137 }138 139 return true;140}141 142int main(int, char**) {143 test();144 static_assert(test());145 146 return 0;147}148