brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.0 KiB · bbe08be Raw
159 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// template <input_range Range>12//   requires constructible_from<View, views::all_t<Range>> &&13//             constructible_from<Pattern, single_view<range_value_t<Range>>>14// constexpr split_view(Range&& r, range_value_t<Range> e); // explicit since C++2315 16#include <algorithm>17#include <cassert>18#include <ranges>19#include <string>20#include <string_view>21#include <type_traits>22#include <utility>23 24#include "test_convertible.h"25#include "test_macros.h"26 27struct Counting {28  int* times_copied = nullptr;29  int* times_moved  = nullptr;30 31  constexpr Counting(int& copies_ctr, int& moves_ctr) : times_copied(&copies_ctr), times_moved(&moves_ctr) {}32 33  constexpr Counting(const Counting& rhs) : times_copied(rhs.times_copied), times_moved(rhs.times_moved) {34    ++(*times_copied);35  }36  constexpr Counting(Counting&& rhs) : times_copied(rhs.times_copied), times_moved(rhs.times_moved) {37    ++(*times_moved);38  }39 40  constexpr Counting& operator=(const Counting&) = default;41  constexpr Counting& operator=(Counting&&)      = default;42};43 44struct ElementWithCounting : Counting {45  using Counting::Counting;46 47  constexpr bool operator==(const ElementWithCounting&) const { return true; }48};49 50struct RangeWithCounting : Counting {51  using Counting::Counting;52 53  constexpr const ElementWithCounting* begin() const { return nullptr; }54  constexpr const ElementWithCounting* end() const { return nullptr; }55 56  constexpr bool operator==(const RangeWithCounting&) const { return true; }57};58static_assert(std::ranges::forward_range<RangeWithCounting>);59static_assert(!std::ranges::view<RangeWithCounting>);60 61struct StrView : std::ranges::view_base {62  std::string buffer_;63 64  template <std::ranges::range R>65  constexpr StrView(R&& r) : buffer_(std::ranges::begin(r), std::ranges::end(r)) {}66  constexpr const char* begin() const { return buffer_.data(); }67  constexpr const char* end() const { return buffer_.data() + buffer_.size(); }68  constexpr bool operator==(const StrView& rhs) const { return buffer_ == rhs.buffer_; }69};70static_assert(std::ranges::random_access_range<StrView>);71static_assert(std::ranges::view<StrView>);72static_assert(std::is_copy_constructible_v<StrView>);73 74// SFINAE tests.75 76#if TEST_STD_VER >= 2377 78static_assert(79    !test_convertible<std::ranges::split_view<StrView, StrView>, StrView, std::ranges::range_value_t<StrView>>(),80    "This constructor must be explicit");81 82# else83 84static_assert(85    test_convertible<std::ranges::split_view<StrView, StrView>, StrView, std::ranges::range_value_t<StrView>>(),86    "This constructor must not be explicit");87 88#endif // TEST_STD_VER >= 2389 90constexpr bool test() {91  {92    using V = std::ranges::split_view<StrView, StrView>;93 94    // Calling the constructor with `(std::string, range_value_t)`.95    {96      std::string input("abc def");97      V v(input, ' ');98      assert(v.base() == input);99      assert(std::ranges::equal(*v.begin(), std::string_view("abc")));100    }101 102    // Calling the constructor with `(StrView, range_value_t)`.103    {104      StrView input("abc def");105      V v(input, ' ');106      assert(v.base() == input);107      assert(std::ranges::equal(*v.begin(), std::string_view("abc")));108    }109 110    struct Empty {};111    static_assert(!std::is_constructible_v<V, Empty, std::string_view>);112    static_assert(!std::is_constructible_v<V, std::string_view, Empty>);113  }114 115  // Make sure the arguments are moved, not copied.116  {117    using Range   = RangeWithCounting;118    using Element = ElementWithCounting;119    using Pattern = std::ranges::single_view<Element>;120 121    // Arguments are lvalues.122    {123      using View = std::ranges::ref_view<Range>;124 125      int range_copied = 0, range_moved = 0, element_copied = 0, element_moved = 0;126      Range range(range_copied, range_moved);127      Element element(element_copied, element_moved);128 129      std::ranges::split_view<View, Pattern> v(range, element);130      assert(range_copied == 0);   // `ref_view` does neither copy...131      assert(range_moved == 0);    // ...nor move the element.132      assert(element_copied == 1); // The element is copied into the argument...133      assert(element_moved == 1);  // ...and moved into the member variable.134    }135 136    // Arguments are rvalues.137    {138      using View = std::ranges::owning_view<Range>;139 140      int range_copied = 0, range_moved = 0, element_copied = 0, element_moved = 0;141      std::ranges::split_view<View, Pattern> v(142          Range(range_copied, range_moved), Element(element_copied, element_moved));143      assert(range_copied == 0);144      assert(range_moved == 1); // `owning_view` moves the given argument.145      assert(element_copied == 0);146      assert(element_moved == 1);147    }148  }149 150  return true;151}152 153int main(int, char**) {154  test();155  static_assert(test());156 157  return 0;158}159