brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · 45257f2 Raw
133 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// constexpr inner-iterator& inner-iterator::operator++();12//13// constexpr decltype(auto) inner-iterator::operator++(int);14 15#include <ranges>16 17#include <cassert>18#include <type_traits>19#include "test_macros.h"20#include "../types.h"21 22struct EmptyView : std::ranges::view_base {23  constexpr int* begin() const { return nullptr; }24  constexpr int* end() const { return nullptr; }25  constexpr static std::size_t size() { return 0; }26};27static_assert(std::ranges::forward_range<EmptyView>);28static_assert(std::ranges::view<EmptyView>);29LIBCPP_STATIC_ASSERT(std::ranges::__tiny_range<EmptyView>);30 31constexpr bool test() {32  // Can call `inner-iterator::operator++`; `View` is a forward range.33  {34    SplitViewForward v("abc def", " ");35    auto val = *v.begin();36 37    // ++i38    {39      auto i = val.begin();40      assert(*i == 'a');41 42      decltype(auto) i2 = ++i;43      static_assert(std::is_lvalue_reference_v<decltype(i2)>);44      assert(&i2 == &i);45      assert(*i2 == 'b');46    }47 48    // i++49    {50      auto i = val.begin();51      assert(*i == 'a');52 53      decltype(auto) i2 = i++;54      static_assert(!std::is_reference_v<decltype(i2)>);55      assert(*i2 == 'a');56      assert(*i == 'b');57    }58  }59 60  // Can call `inner-iterator::operator++`; `View` is an input range.61  {62    // ++i63    {64      SplitViewInput v("abc def", ' ');65      auto val = *v.begin();66 67      auto i = val.begin();68      assert(*i == 'a');69 70      decltype(auto) i2 = ++i;71      static_assert(std::is_lvalue_reference_v<decltype(i2)>);72      assert(&i2 == &i);73      assert(*i2 == 'b');74    }75 76    // i++77    {78      SplitViewInput v("abc def", ' ');79      auto val = *v.begin();80 81      auto i = val.begin();82      assert(*i == 'a');83 84      static_assert(std::is_void_v<decltype(i++)>);85      i++;86      assert(*i == 'b');87    }88  }89 90  // Can call `inner-iterator::operator++`; `View` is an input range and `Pattern` is an "empty" range.91  {92    // ++i93    {94      std::ranges::lazy_split_view<InputView, EmptyView> v("a", EmptyView());95      auto val = *v.begin();96 97      auto i = val.begin();98      assert(*i.base() == 'a');99      assert(i != std::default_sentinel);100 101      // The iterator doesn't move to the next character but is considered to point to the end.102      decltype(auto) i2 = ++i;103      assert(&i2 == &i);104      assert(*i2.base() == 'a');105      assert(i2 == std::default_sentinel);106    }107 108    // i++109    {110      std::ranges::lazy_split_view<InputView, EmptyView> v("a", EmptyView());111      auto val = *v.begin();112 113      auto i = val.begin();114      assert(*i.base() == 'a');115      assert(i != std::default_sentinel);116 117      // The iterator doesn't move to the next character but is considered to point to the end.118      i++;119      assert(*i.base() == 'a');120      assert(i == std::default_sentinel);121    }122  }123 124  return true;125}126 127int main(int, char**) {128  test();129  static_assert(test());130 131  return 0;132}133