brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · 27fc4b8 Raw
104 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 split_view(View base, Pattern pattern); // explicit since C++2312 13#include <algorithm>14#include <cassert>15#include <ranges>16#include <string_view>17#include <utility>18 19#include "test_convertible.h"20#include "test_macros.h"21 22struct ViewWithCounting : std::ranges::view_base {23  int* times_copied = nullptr;24  int* times_moved  = nullptr;25 26  constexpr ViewWithCounting(int& copies_ctr, int& moves_ctr) : times_copied(&copies_ctr), times_moved(&moves_ctr) {}27 28  constexpr ViewWithCounting(const ViewWithCounting& rhs)29      : times_copied(rhs.times_copied), times_moved(rhs.times_moved) {30    ++(*times_copied);31  }32  constexpr ViewWithCounting(ViewWithCounting&& rhs) : times_copied(rhs.times_copied), times_moved(rhs.times_moved) {33    ++(*times_moved);34  }35 36  constexpr const char* begin() const { return nullptr; }37  constexpr const char* end() const { return nullptr; }38 39  constexpr ViewWithCounting& operator=(const ViewWithCounting&) = default;40  constexpr ViewWithCounting& operator=(ViewWithCounting&&)      = default;41  constexpr bool operator==(const ViewWithCounting&) const { return true; }42};43 44using View    = ViewWithCounting;45using Pattern = ViewWithCounting;46 47// SFINAE tests.48 49#if TEST_STD_VER >= 2350 51static_assert(!test_convertible<std::ranges::split_view<View, Pattern>, View, Pattern>(),52              "This constructor must be explicit");53 54#else55 56static_assert( test_convertible<std::ranges::split_view<View, Pattern>, View, Pattern>(),57              "This constructor must not be explicit");58 59#endif // TEST_STD_VER >= 2360 61constexpr bool test() {62  {63    std::string_view input = "abc def";64    std::ranges::split_view<std::string_view, std::string_view> v(input, " ");65    assert(v.base() == input);66    assert(std::ranges::equal(*v.begin(), std::string_view{"abc"}));67  }68 69  // Make sure the arguments are moved, not copied.70  {71    // Arguments are lvalues.72    {73      int view_copied = 0, view_moved = 0, pattern_copied = 0, pattern_moved = 0;74      View view(view_copied, view_moved);75      Pattern pattern(pattern_copied, pattern_moved);76 77      std::ranges::split_view<View, Pattern> v(view, pattern);78      assert(view_copied == 1); // The local variable is copied into the argument.79      assert(view_moved == 1);80      assert(pattern_copied == 1);81      assert(pattern_moved == 1);82    }83 84    // Arguments are rvalues.85    {86      int view_copied = 0, view_moved = 0, pattern_copied = 0, pattern_moved = 0;87      std::ranges::split_view<View, Pattern> v(View(view_copied, view_moved), Pattern(pattern_copied, pattern_moved));88      assert(view_copied == 0);89      assert(view_moved == 1);90      assert(pattern_copied == 0);91      assert(pattern_moved == 1);92    }93  }94 95  return true;96}97 98int main(int, char**) {99  test();100  static_assert(test());101 102  return 0;103}104