1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168

base / containers / adapters_unittest.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/containers/adapters.h"

#include <array>
#include <ranges>
#include <utility>
#include <vector>

#include "base/containers/span.h"
#include "base/containers/to_vector.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class UnsizedVector {
 public:
  UnsizedVector() = default;
  UnsizedVector(std::initializer_list<int> il) : v_(il) {}

  // Sentinel iterator type that wraps `end()` to ensure this type doesn't
  // satisfy the `sized_range` concept.
  template <typename Iterator>
  class Sentinel {
   public:
    Sentinel() = default;
    explicit Sentinel(Iterator it) : wrapped_it_(it) {}

    bool operator==(Iterator it) const { return wrapped_it_ == it; }

   private:
    Iterator wrapped_it_;
  };

  auto begin() const { return v_.begin(); }
  auto end() const { return Sentinel(v_.end()); }
  auto rbegin() const { return v_.rbegin(); }
  auto rend() const { return Sentinel(v_.rend()); }
  auto begin() { return v_.begin(); }
  auto end() { return Sentinel(v_.end()); }
  auto rbegin() { return v_.rbegin(); }
  auto rend() { return Sentinel(v_.rend()); }

  auto operator[](size_t pos) const { return v_[pos]; }
  auto operator[](size_t pos) { return v_[pos]; }

 private:
  std::vector<int> v_;
};

[[maybe_unused]] void StaticAsserts() {
  {
    // Named local variables are more readable than std::declval<T>().
    std::vector<int> v;
    static_assert(std::ranges::range<decltype(base::Reversed(v))>);
    static_assert(std::ranges::sized_range<decltype(base::Reversed(v))>);
    // `base::Reversed()` takes a const ref to the vector, which is, by
    // definition, a borrowed range.
    static_assert(std::ranges::borrowed_range<decltype(base::Reversed(v))>);

    auto make_vector = [] { return std::vector<int>(); };
    static_assert(std::ranges::range<decltype(base::Reversed(make_vector()))>);
    static_assert(
        std::ranges::sized_range<decltype(base::Reversed(make_vector()))>);
    static_assert(
        !std::ranges::borrowed_range<decltype(base::Reversed(make_vector()))>);
  }

  {
    base::span<int> s;
    static_assert(std::ranges::range<decltype(base::Reversed(s))>);
    static_assert(std::ranges::sized_range<decltype(base::Reversed(s))>);
    static_assert(std::ranges::borrowed_range<decltype(base::Reversed(s))>);

    auto rvalue_span = [] { return base::span<int>(); };
    static_assert(std::ranges::range<decltype(base::Reversed(rvalue_span()))>);
    static_assert(
        std::ranges::sized_range<decltype(base::Reversed(rvalue_span()))>);
    static_assert(
        std::ranges::borrowed_range<decltype(base::Reversed(rvalue_span()))>);
  }

  {
    // A named local variable is more readable than std::declval<T>().
    UnsizedVector v;
    static_assert(std::ranges::range<decltype(v)>);
    static_assert(!std::ranges::sized_range<decltype(v)>);

    static_assert(std::ranges::range<decltype(base::Reversed(v))>);
    static_assert(!std::ranges::sized_range<decltype(base::Reversed(v))>);
    // `base::Reversed()` takes a const ref to the vector, which is, by
    // definition, a borrowed range.
    static_assert(std::ranges::borrowed_range<decltype(base::Reversed(v))>);

    auto make_vector = [] { return UnsizedVector(); };
    static_assert(std::ranges::range<decltype(base::Reversed(make_vector()))>);
    static_assert(
        !std::ranges::sized_range<decltype(base::Reversed(make_vector()))>);
    static_assert(!std::ranges::borrowed_range<decltype(make_vector())>);
  }
}

TEST(AdaptersTest, Reversed) {
  std::vector<int> v = {3, 2, 1};
  int j = 0;
  for (int& i : base::Reversed(v)) {
    EXPECT_EQ(++j, i);
    i += 100;
  }
  EXPECT_EQ(103, v[0]);
  EXPECT_EQ(102, v[1]);
  EXPECT_EQ(101, v[2]);
}

TEST(AdaptersTest, ReversedUnsized) {
  UnsizedVector v = {3, 2, 1};
  int j = 0;
  for (int& i : base::Reversed(v)) {
    EXPECT_EQ(++j, i);
    i += 100;
  }
  EXPECT_EQ(103, v[0]);
  EXPECT_EQ(102, v[1]);
  EXPECT_EQ(101, v[2]);
}

TEST(AdaptersTest, ReversedArray) {
  std::array<int, 3> v = {3, 2, 1};
  int j = 0;
  for (int& i : base::Reversed(v)) {
    EXPECT_EQ(++j, i);
    i += 100;
  }
  EXPECT_EQ(103, v[0]);
  EXPECT_EQ(102, v[1]);
  EXPECT_EQ(101, v[2]);
}

TEST(AdaptersTest, ReversedConst) {
  std::vector<int> v = {3, 2, 1};
  const std::vector<int>& cv = v;
  int j = 0;
  for (int i : base::Reversed(cv)) {
    EXPECT_EQ(++j, i);
  }
}

TEST(AdaptersTest, RangeAsRvalues) {
  std::vector<std::unique_ptr<int>> v;
  v.push_back(std::make_unique<int>(1));
  v.push_back(std::make_unique<int>(2));
  v.push_back(std::make_unique<int>(3));

  auto v2 = base::ToVector(base::RangeAsRvalues(std::move(v)));
  EXPECT_EQ(1, *v2[0]);
  EXPECT_EQ(2, *v2[1]);
  EXPECT_EQ(3, *v2[2]);

  // The old vector should be consumed. The standard guarantees that a
  // moved-from std::unique_ptr will be null.
  EXPECT_EQ(nullptr, v[0]);  // NOLINT(bugprone-use-after-move)
  EXPECT_EQ(nullptr, v[1]);  // NOLINT(bugprone-use-after-move)
  EXPECT_EQ(nullptr, v[2]);  // NOLINT(bugprone-use-after-move)
}

}  // namespace