blob: dcfb9c8c053ecb17ad565834f6c914f603567932 [file] [log] [blame]
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/containers/span.h"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "starboard/types.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::Pointwise;
namespace base {
TEST(SpanTest, DefaultConstructor) {
span<int> dynamic_span;
EXPECT_EQ(nullptr, dynamic_span.data());
EXPECT_EQ(0u, dynamic_span.size());
constexpr span<int, 0> static_span;
static_assert(nullptr == static_span.data(), "");
static_assert(0u == static_span.size(), "");
}
TEST(SpanTest, ConstructFromDataAndSize) {
constexpr span<int> empty_span(nullptr, 0);
EXPECT_TRUE(empty_span.empty());
EXPECT_EQ(nullptr, empty_span.data());
std::vector<int> vector = {1, 1, 2, 3, 5, 8};
span<int> dynamic_span(vector.data(), vector.size());
EXPECT_EQ(vector.data(), dynamic_span.data());
EXPECT_EQ(vector.size(), dynamic_span.size());
for (size_t i = 0; i < dynamic_span.size(); ++i)
EXPECT_EQ(vector[i], dynamic_span[i]);
span<int, 6> static_span(vector.data(), vector.size());
EXPECT_EQ(vector.data(), static_span.data());
EXPECT_EQ(vector.size(), static_span.size());
for (size_t i = 0; i < static_span.size(); ++i)
EXPECT_EQ(vector[i], static_span[i]);
}
TEST(SpanTest, ConstructFromPointerPair) {
constexpr span<int> empty_span(nullptr, nullptr);
EXPECT_TRUE(empty_span.empty());
EXPECT_EQ(nullptr, empty_span.data());
std::vector<int> vector = {1, 1, 2, 3, 5, 8};
span<int> dynamic_span(vector.data(), vector.data() + vector.size() / 2);
EXPECT_EQ(vector.data(), dynamic_span.data());
EXPECT_EQ(vector.size() / 2, dynamic_span.size());
for (size_t i = 0; i < dynamic_span.size(); ++i)
EXPECT_EQ(vector[i], dynamic_span[i]);
span<int, 3> static_span(vector.data(), vector.data() + vector.size() / 2);
EXPECT_EQ(vector.data(), static_span.data());
EXPECT_EQ(vector.size() / 2, static_span.size());
for (size_t i = 0; i < static_span.size(); ++i)
EXPECT_EQ(vector[i], static_span[i]);
}
TEST(SpanTest, ConstructFromConstexprArray) {
static constexpr int kArray[] = {5, 4, 3, 2, 1};
constexpr span<const int> dynamic_span(kArray);
static_assert(kArray == dynamic_span.data(), "");
static_assert(base::size(kArray) == dynamic_span.size(), "");
static_assert(kArray[0] == dynamic_span[0], "");
static_assert(kArray[1] == dynamic_span[1], "");
static_assert(kArray[2] == dynamic_span[2], "");
static_assert(kArray[3] == dynamic_span[3], "");
static_assert(kArray[4] == dynamic_span[4], "");
constexpr span<const int, base::size(kArray)> static_span(kArray);
static_assert(kArray == static_span.data(), "");
static_assert(base::size(kArray) == static_span.size(), "");
static_assert(kArray[0] == static_span[0], "");
static_assert(kArray[1] == static_span[1], "");
static_assert(kArray[2] == static_span[2], "");
static_assert(kArray[3] == static_span[3], "");
static_assert(kArray[4] == static_span[4], "");
}
TEST(SpanTest, ConstructFromArray) {
int array[] = {5, 4, 3, 2, 1};
span<const int> const_span(array);
EXPECT_EQ(array, const_span.data());
EXPECT_EQ(arraysize(array), const_span.size());
for (size_t i = 0; i < const_span.size(); ++i)
EXPECT_EQ(array[i], const_span[i]);
span<int> dynamic_span(array);
EXPECT_EQ(array, dynamic_span.data());
EXPECT_EQ(base::size(array), dynamic_span.size());
for (size_t i = 0; i < dynamic_span.size(); ++i)
EXPECT_EQ(array[i], dynamic_span[i]);
span<int, base::size(array)> static_span(array);
EXPECT_EQ(array, static_span.data());
EXPECT_EQ(base::size(array), static_span.size());
for (size_t i = 0; i < static_span.size(); ++i)
EXPECT_EQ(array[i], static_span[i]);
}
TEST(SpanTest, ConstructFromStdArray) {
// Note: Constructing a constexpr span from a constexpr std::array does not
// work prior to C++17 due to non-constexpr std::array::data.
std::array<int, 5> array = {{5, 4, 3, 2, 1}};
span<const int> const_span(array);
EXPECT_EQ(array.data(), const_span.data());
EXPECT_EQ(array.size(), const_span.size());
for (size_t i = 0; i < const_span.size(); ++i)
EXPECT_EQ(array[i], const_span[i]);
span<int> dynamic_span(array);
EXPECT_EQ(array.data(), dynamic_span.data());
EXPECT_EQ(array.size(), dynamic_span.size());
for (size_t i = 0; i < dynamic_span.size(); ++i)
EXPECT_EQ(array[i], dynamic_span[i]);
span<int, base::size(array)> static_span(array);
EXPECT_EQ(array.data(), static_span.data());
EXPECT_EQ(array.size(), static_span.size());
for (size_t i = 0; i < static_span.size(); ++i)
EXPECT_EQ(array[i], static_span[i]);
}
TEST(SpanTest, ConstructFromInitializerList) {
std::initializer_list<int> il = {1, 1, 2, 3, 5, 8};
span<const int> const_span(il);
EXPECT_EQ(il.begin(), const_span.data());
EXPECT_EQ(il.size(), const_span.size());
for (size_t i = 0; i < const_span.size(); ++i)
EXPECT_EQ(il.begin()[i], const_span[i]);
span<const int, 6> static_span(il);
EXPECT_EQ(il.begin(), static_span.data());
EXPECT_EQ(il.size(), static_span.size());
for (size_t i = 0; i < static_span.size(); ++i)
EXPECT_EQ(il.begin()[i], static_span[i]);
}
TEST(SpanTest, ConstructFromStdString) {
std::string str = "foobar";
span<const char> const_span(str);
EXPECT_EQ(str.data(), const_span.data());
EXPECT_EQ(str.size(), const_span.size());
for (size_t i = 0; i < const_span.size(); ++i)
EXPECT_EQ(str[i], const_span[i]);
span<char> dynamic_span(str);
EXPECT_EQ(str.data(), dynamic_span.data());
EXPECT_EQ(str.size(), dynamic_span.size());
for (size_t i = 0; i < dynamic_span.size(); ++i)
EXPECT_EQ(str[i], dynamic_span[i]);
span<char, 6> static_span(str);
EXPECT_EQ(str.data(), static_span.data());
EXPECT_EQ(str.size(), static_span.size());
for (size_t i = 0; i < static_span.size(); ++i)
EXPECT_EQ(str[i], static_span[i]);
}
TEST(SpanTest, ConstructFromConstContainer) {
const std::vector<int> vector = {1, 1, 2, 3, 5, 8};
span<const int> const_span(vector);
EXPECT_EQ(vector.data(), const_span.data());
EXPECT_EQ(vector.size(), const_span.size());
for (size_t i = 0; i < const_span.size(); ++i)
EXPECT_EQ(vector[i], const_span[i]);
span<const int, 6> static_span(vector);
EXPECT_EQ(vector.data(), static_span.data());
EXPECT_EQ(vector.size(), static_span.size());
for (size_t i = 0; i < static_span.size(); ++i)
EXPECT_EQ(vector[i], static_span[i]);
}
TEST(SpanTest, ConstructFromContainer) {
std::vector<int> vector = {1, 1, 2, 3, 5, 8};
span<const int> const_span(vector);
EXPECT_EQ(vector.data(), const_span.data());
EXPECT_EQ(vector.size(), const_span.size());
for (size_t i = 0; i < const_span.size(); ++i)
EXPECT_EQ(vector[i], const_span[i]);
span<int> dynamic_span(vector);
EXPECT_EQ(vector.data(), dynamic_span.data());
EXPECT_EQ(vector.size(), dynamic_span.size());
for (size_t i = 0; i < dynamic_span.size(); ++i)
EXPECT_EQ(vector[i], dynamic_span[i]);
span<int, 6> static_span(vector);
EXPECT_EQ(vector.data(), static_span.data());
EXPECT_EQ(vector.size(), static_span.size());
for (size_t i = 0; i < static_span.size(); ++i)
EXPECT_EQ(vector[i], static_span[i]);
}
TEST(SpanTest, ConvertNonConstIntegralToConst) {
std::vector<int> vector = {1, 1, 2, 3, 5, 8};
span<int> int_span(vector.data(), vector.size());
span<const int> const_span(int_span);
EXPECT_EQ(int_span.size(), const_span.size());
EXPECT_THAT(const_span, Pointwise(Eq(), int_span));
span<int, 6> static_int_span(vector.data(), vector.size());
span<const int, 6> static_const_span(static_int_span);
EXPECT_THAT(static_const_span, Pointwise(Eq(), static_int_span));
}
TEST(SpanTest, ConvertNonConstPointerToConst) {
auto a = std::make_unique<int>(11);
auto b = std::make_unique<int>(22);
auto c = std::make_unique<int>(33);
std::vector<int*> vector = {a.get(), b.get(), c.get()};
span<int*> non_const_pointer_span(vector);
EXPECT_THAT(non_const_pointer_span, Pointwise(Eq(), vector));
span<int* const> const_pointer_span(non_const_pointer_span);
EXPECT_THAT(const_pointer_span, Pointwise(Eq(), non_const_pointer_span));
// Note: no test for conversion from span<int> to span<const int*>, since that
// would imply a conversion from int** to const int**, which is unsafe.
//
// Note: no test for conversion from span<int*> to span<const int* const>,
// due to CWG Defect 330:
// http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#330
span<int*, 3> static_non_const_pointer_span(vector);
EXPECT_THAT(static_non_const_pointer_span, Pointwise(Eq(), vector));
span<int* const, 3> static_const_pointer_span(static_non_const_pointer_span);
EXPECT_THAT(static_const_pointer_span,
Pointwise(Eq(), static_non_const_pointer_span));
}
TEST(SpanTest, ConvertBetweenEquivalentTypes) {
std::vector<int32_t> vector = {2, 4, 8, 16, 32};
span<int32_t> int32_t_span(vector);
span<int> converted_span(int32_t_span);
EXPECT_EQ(int32_t_span, converted_span);
span<int32_t, 5> static_int32_t_span(vector);
span<int, 5> static_converted_span(static_int32_t_span);
EXPECT_EQ(static_int32_t_span, static_converted_span);
}
TEST(SpanTest, TemplatedFirst) {
static constexpr int array[] = {1, 2, 3};
constexpr span<const int, 3> span(array);
{
constexpr auto subspan = span.first<0>();
static_assert(span.data() == subspan.data(), "");
static_assert(0u == subspan.size(), "");
static_assert(0u == decltype(subspan)::extent, "");
}
{
constexpr auto subspan = span.first<1>();
static_assert(span.data() == subspan.data(), "");
static_assert(1u == subspan.size(), "");
static_assert(1u == decltype(subspan)::extent, "");
static_assert(1 == subspan[0], "");
}
{
constexpr auto subspan = span.first<2>();
static_assert(span.data() == subspan.data(), "");
static_assert(2u == subspan.size(), "");
static_assert(2u == decltype(subspan)::extent, "");
static_assert(1 == subspan[0], "");
static_assert(2 == subspan[1], "");
}
{
constexpr auto subspan = span.first<3>();
static_assert(span.data() == subspan.data(), "");
static_assert(3u == subspan.size(), "");
static_assert(3u == decltype(subspan)::extent, "");
static_assert(1 == subspan[0], "");
static_assert(2 == subspan[1], "");
static_assert(3 == subspan[2], "");
}
}
TEST(SpanTest, TemplatedLast) {
static constexpr int array[] = {1, 2, 3};
constexpr span<const int, 3> span(array);
{
constexpr auto subspan = span.last<0>();
static_assert(span.data() + 3 == subspan.data(), "");
static_assert(0u == subspan.size(), "");
static_assert(0u == decltype(subspan)::extent, "");
}
{
constexpr auto subspan = span.last<1>();
static_assert(span.data() + 2 == subspan.data(), "");
static_assert(1u == subspan.size(), "");
static_assert(1u == decltype(subspan)::extent, "");
static_assert(3 == subspan[0], "");
}
{
constexpr auto subspan = span.last<2>();
static_assert(span.data() + 1 == subspan.data(), "");
static_assert(2u == subspan.size(), "");
static_assert(2u == decltype(subspan)::extent, "");
static_assert(2 == subspan[0], "");
static_assert(3 == subspan[1], "");
}
{
constexpr auto subspan = span.last<3>();
static_assert(span.data() == subspan.data(), "");
static_assert(3u == subspan.size(), "");
static_assert(3u == decltype(subspan)::extent, "");
static_assert(1 == subspan[0], "");
static_assert(2 == subspan[1], "");
static_assert(3 == subspan[2], "");
}
}
TEST(SpanTest, TemplatedSubspan) {
static constexpr int array[] = {1, 2, 3};
constexpr span<const int, 3> span(array);
{
constexpr auto subspan = span.subspan<0>();
static_assert(span.data() == subspan.data(), "");
static_assert(3u == subspan.size(), "");
static_assert(3u == decltype(subspan)::extent, "");
static_assert(1 == subspan[0], "");
static_assert(2 == subspan[1], "");
static_assert(3 == subspan[2], "");
}
{
constexpr auto subspan = span.subspan<1>();
static_assert(span.data() + 1 == subspan.data(), "");
static_assert(2u == subspan.size(), "");
static_assert(2u == decltype(subspan)::extent, "");
static_assert(2 == subspan[0], "");
static_assert(3 == subspan[1], "");
}
{
constexpr auto subspan = span.subspan<2>();
static_assert(span.data() + 2 == subspan.data(), "");
static_assert(1u == subspan.size(), "");
static_assert(1u == decltype(subspan)::extent, "");
static_assert(3 == subspan[0], "");
}
{
constexpr auto subspan = span.subspan<3>();
static_assert(span.data() + 3 == subspan.data(), "");
static_assert(0u == subspan.size(), "");
static_assert(0u == decltype(subspan)::extent, "");
}
{
constexpr auto subspan = span.subspan<0, 0>();
static_assert(span.data() == subspan.data(), "");
static_assert(0u == subspan.size(), "");
static_assert(0u == decltype(subspan)::extent, "");
}
{
constexpr auto subspan = span.subspan<1, 0>();
static_assert(span.data() + 1 == subspan.data(), "");
static_assert(0u == subspan.size(), "");
static_assert(0u == decltype(subspan)::extent, "");
}
{
constexpr auto subspan = span.subspan<2, 0>();
static_assert(span.data() + 2 == subspan.data(), "");
static_assert(0u == subspan.size(), "");
static_assert(0u == decltype(subspan)::extent, "");
}
{
constexpr auto subspan = span.subspan<0, 1>();
static_assert(span.data() == subspan.data(), "");
static_assert(1u == subspan.size(), "");
static_assert(1u == decltype(subspan)::extent, "");
static_assert(1 == subspan[0], "");
}
{
constexpr auto subspan = span.subspan<1, 1>();
static_assert(span.data() + 1 == subspan.data(), "");
static_assert(1u == subspan.size(), "");
static_assert(1u == decltype(subspan)::extent, "");
static_assert(2 == subspan[0], "");
}
{
constexpr auto subspan = span.subspan<2, 1>();
static_assert(span.data() + 2 == subspan.data(), "");
static_assert(1u == subspan.size(), "");
static_assert(1u == decltype(subspan)::extent, "");
static_assert(3 == subspan[0], "");
}
{
constexpr auto subspan = span.subspan<0, 2>();
static_assert(span.data() == subspan.data(), "");
static_assert(2u == subspan.size(), "");
static_assert(2u == decltype(subspan)::extent, "");
static_assert(1 == subspan[0], "");
static_assert(2 == subspan[1], "");
}
{
constexpr auto subspan = span.subspan<1, 2>();
static_assert(span.data() + 1 == subspan.data(), "");
static_assert(2u == subspan.size(), "");
static_assert(2u == decltype(subspan)::extent, "");
static_assert(2 == subspan[0], "");
static_assert(3 == subspan[1], "");
}
{
constexpr auto subspan = span.subspan<0, 3>();
static_assert(span.data() == subspan.data(), "");
static_assert(3u == subspan.size(), "");
static_assert(3u == decltype(subspan)::extent, "");
static_assert(1 == subspan[0], "");
static_assert(2 == subspan[1], "");
static_assert(3 == subspan[2], "");
}
}
TEST(SpanTest, TemplatedFirstOnDynamicSpan) {
int array[] = {1, 2, 3};
span<const int> span(array);
{
auto subspan = span.first<0>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(0u, subspan.size());
static_assert(0u == decltype(subspan)::extent, "");
}
{
auto subspan = span.first<1>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(1u, subspan.size());
static_assert(1u == decltype(subspan)::extent, "");
EXPECT_EQ(1, subspan[0]);
}
{
auto subspan = span.first<2>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(2u, subspan.size());
static_assert(2u == decltype(subspan)::extent, "");
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
}
{
auto subspan = span.first<3>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(3u, subspan.size());
static_assert(3u == decltype(subspan)::extent, "");
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
EXPECT_EQ(3, subspan[2]);
}
}
TEST(SpanTest, TemplatedLastOnDynamicSpan) {
int array[] = {1, 2, 3};
span<int> span(array);
{
auto subspan = span.last<0>();
EXPECT_EQ(span.data() + 3, subspan.data());
EXPECT_EQ(0u, subspan.size());
static_assert(0u == decltype(subspan)::extent, "");
}
{
auto subspan = span.last<1>();
EXPECT_EQ(span.data() + 2, subspan.data());
EXPECT_EQ(1u, subspan.size());
static_assert(1u == decltype(subspan)::extent, "");
EXPECT_EQ(3, subspan[0]);
}
{
auto subspan = span.last<2>();
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(2u, subspan.size());
static_assert(2u == decltype(subspan)::extent, "");
EXPECT_EQ(2, subspan[0]);
EXPECT_EQ(3, subspan[1]);
}
{
auto subspan = span.last<3>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(3u, subspan.size());
static_assert(3u == decltype(subspan)::extent, "");
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
EXPECT_EQ(3, subspan[2]);
}
}
TEST(SpanTest, TemplatedSubspanFromDynamicSpan) {
int array[] = {1, 2, 3};
span<int, 3> span(array);
{
auto subspan = span.subspan<0>();
EXPECT_EQ(span.data(), subspan.data());
static_assert(3u == decltype(subspan)::extent, "");
EXPECT_EQ(3u, subspan.size());
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
EXPECT_EQ(3, subspan[2]);
}
{
auto subspan = span.subspan<1>();
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(2u, subspan.size());
static_assert(2u == decltype(subspan)::extent, "");
EXPECT_EQ(2, subspan[0]);
EXPECT_EQ(3, subspan[1]);
}
{
auto subspan = span.subspan<2>();
EXPECT_EQ(span.data() + 2, subspan.data());
EXPECT_EQ(1u, subspan.size());
static_assert(1u == decltype(subspan)::extent, "");
EXPECT_EQ(3, subspan[0]);
}
{
auto subspan = span.subspan<3>();
EXPECT_EQ(span.data() + 3, subspan.data());
EXPECT_EQ(0u, subspan.size());
static_assert(0u == decltype(subspan)::extent, "");
}
{
auto subspan = span.subspan<0, 0>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(0u, subspan.size());
static_assert(0u == decltype(subspan)::extent, "");
}
{
auto subspan = span.subspan<1, 0>();
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(0u, subspan.size());
static_assert(0u == decltype(subspan)::extent, "");
}
{
auto subspan = span.subspan<2, 0>();
EXPECT_EQ(span.data() + 2, subspan.data());
EXPECT_EQ(0u, subspan.size());
static_assert(0u == decltype(subspan)::extent, "");
}
{
auto subspan = span.subspan<0, 1>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(1u, subspan.size());
static_assert(1u == decltype(subspan)::extent, "");
EXPECT_EQ(1, subspan[0]);
}
{
auto subspan = span.subspan<1, 1>();
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(1u, subspan.size());
static_assert(1u == decltype(subspan)::extent, "");
EXPECT_EQ(2, subspan[0]);
}
{
auto subspan = span.subspan<2, 1>();
EXPECT_EQ(span.data() + 2, subspan.data());
EXPECT_EQ(1u, subspan.size());
static_assert(1u == decltype(subspan)::extent, "");
EXPECT_EQ(3, subspan[0]);
}
{
auto subspan = span.subspan<0, 2>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(2u, subspan.size());
static_assert(2u == decltype(subspan)::extent, "");
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
}
{
auto subspan = span.subspan<1, 2>();
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(2u, subspan.size());
static_assert(2u == decltype(subspan)::extent, "");
EXPECT_EQ(2, subspan[0]);
EXPECT_EQ(3, subspan[1]);
}
{
auto subspan = span.subspan<0, 3>();
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(3u, subspan.size());
static_assert(3u == decltype(subspan)::extent, "");
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
EXPECT_EQ(3, subspan[2]);
}
}
TEST(SpanTest, First) {
int array[] = {1, 2, 3};
span<int> span(array);
{
auto subspan = span.first(0);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(0u, subspan.size());
}
{
auto subspan = span.first(1);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(1u, subspan.size());
EXPECT_EQ(1, subspan[0]);
}
{
auto subspan = span.first(2);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(2u, subspan.size());
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
}
{
auto subspan = span.first(3);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(3u, subspan.size());
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
EXPECT_EQ(3, subspan[2]);
}
}
TEST(SpanTest, Last) {
int array[] = {1, 2, 3};
span<int> span(array);
{
auto subspan = span.last(0);
EXPECT_EQ(span.data() + 3, subspan.data());
EXPECT_EQ(0u, subspan.size());
}
{
auto subspan = span.last(1);
EXPECT_EQ(span.data() + 2, subspan.data());
EXPECT_EQ(1u, subspan.size());
EXPECT_EQ(3, subspan[0]);
}
{
auto subspan = span.last(2);
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(2u, subspan.size());
EXPECT_EQ(2, subspan[0]);
EXPECT_EQ(3, subspan[1]);
}
{
auto subspan = span.last(3);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(3u, subspan.size());
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
EXPECT_EQ(3, subspan[2]);
}
}
TEST(SpanTest, Subspan) {
int array[] = {1, 2, 3};
span<int> span(array);
{
auto subspan = span.subspan(0);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(3u, subspan.size());
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
EXPECT_EQ(3, subspan[2]);
}
{
auto subspan = span.subspan(1);
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(2u, subspan.size());
EXPECT_EQ(2, subspan[0]);
EXPECT_EQ(3, subspan[1]);
}
{
auto subspan = span.subspan(2);
EXPECT_EQ(span.data() + 2, subspan.data());
EXPECT_EQ(1u, subspan.size());
EXPECT_EQ(3, subspan[0]);
}
{
auto subspan = span.subspan(3);
EXPECT_EQ(span.data() + 3, subspan.data());
EXPECT_EQ(0u, subspan.size());
}
{
auto subspan = span.subspan(0, 0);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(0u, subspan.size());
}
{
auto subspan = span.subspan(1, 0);
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(0u, subspan.size());
}
{
auto subspan = span.subspan(2, 0);
EXPECT_EQ(span.data() + 2, subspan.data());
EXPECT_EQ(0u, subspan.size());
}
{
auto subspan = span.subspan(0, 1);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(1u, subspan.size());
EXPECT_EQ(1, subspan[0]);
}
{
auto subspan = span.subspan(1, 1);
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(1u, subspan.size());
EXPECT_EQ(2, subspan[0]);
}
{
auto subspan = span.subspan(2, 1);
EXPECT_EQ(span.data() + 2, subspan.data());
EXPECT_EQ(1u, subspan.size());
EXPECT_EQ(3, subspan[0]);
}
{
auto subspan = span.subspan(0, 2);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(2u, subspan.size());
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
}
{
auto subspan = span.subspan(1, 2);
EXPECT_EQ(span.data() + 1, subspan.data());
EXPECT_EQ(2u, subspan.size());
EXPECT_EQ(2, subspan[0]);
EXPECT_EQ(3, subspan[1]);
}
{
auto subspan = span.subspan(0, 3);
EXPECT_EQ(span.data(), subspan.data());
EXPECT_EQ(span.size(), subspan.size());
EXPECT_EQ(1, subspan[0]);
EXPECT_EQ(2, subspan[1]);
EXPECT_EQ(3, subspan[2]);
}
}
TEST(SpanTest, Size) {
{
span<int> span;
EXPECT_EQ(0u, span.size());
}
{
int array[] = {1, 2, 3};
span<int> span(array);
EXPECT_EQ(3u, span.size());
}
}
TEST(SpanTest, SizeBytes) {
{
span<int> span;
EXPECT_EQ(0u, span.size_bytes());
}
{
int array[] = {1, 2, 3};
span<int> span(array);
EXPECT_EQ(3u * sizeof(int), span.size_bytes());
}
}
TEST(SpanTest, Empty) {
{
span<int> span;
EXPECT_TRUE(span.empty());
}
{
int array[] = {1, 2, 3};
span<int> span(array);
EXPECT_FALSE(span.empty());
}
}
TEST(SpanTest, OperatorAt) {
static constexpr int kArray[] = {1, 6, 1, 8, 0};
constexpr span<const int> span(kArray);
static_assert(kArray[0] == span[0], "span[0] does not equal kArray[0]");
static_assert(kArray[1] == span[1], "span[1] does not equal kArray[1]");
static_assert(kArray[2] == span[2], "span[2] does not equal kArray[2]");
static_assert(kArray[3] == span[3], "span[3] does not equal kArray[3]");
static_assert(kArray[4] == span[4], "span[4] does not equal kArray[4]");
static_assert(kArray[0] == span(0), "span(0) does not equal kArray[0]");
static_assert(kArray[1] == span(1), "span(1) does not equal kArray[1]");
static_assert(kArray[2] == span(2), "span(2) does not equal kArray[2]");
static_assert(kArray[3] == span(3), "span(3) does not equal kArray[3]");
static_assert(kArray[4] == span(4), "span(4) does not equal kArray[4]");
}
TEST(SpanTest, Iterator) {
static constexpr int kArray[] = {1, 6, 1, 8, 0};
constexpr span<const int> span(kArray);
std::vector<int> results;
for (int i : span)
results.emplace_back(i);
EXPECT_THAT(results, ElementsAre(1, 6, 1, 8, 0));
}
TEST(SpanTest, ReverseIterator) {
static constexpr int kArray[] = {1, 6, 1, 8, 0};
constexpr span<const int> span(kArray);
#if __cplusplus < 201402L
// With GNU at least, there exists an implementation of std::equal that is
// replaced in C++14.
{
auto a = std::rbegin(kArray);
auto b = span.rbegin();
while (a != std::rend(kArray) && b != span.rend()) {
EXPECT_EQ(*a, *b);
++a;
++b;
}
EXPECT_EQ(std::rend(kArray), a);
EXPECT_EQ(span.rend(), b);
}
{
auto a = std::crbegin(kArray);
auto b = span.crbegin();
while (a != std::crend(kArray) && b != span.crend()) {
EXPECT_EQ(*a, *b);
++a;
++b;
}
EXPECT_EQ(std::crend(kArray), a);
EXPECT_EQ(span.crend(), b);
}
#else
EXPECT_TRUE(std::equal(std::rbegin(kArray), std::rend(kArray), span.rbegin(),
span.rend()));
EXPECT_TRUE(std::equal(std::crbegin(kArray), std::crend(kArray),
span.crbegin(), span.crend()));
#endif
}
TEST(SpanTest, Equality) {
static constexpr int kArray1[] = {3, 1, 4, 1, 5};
static constexpr int kArray2[] = {3, 1, 4, 1, 5};
constexpr span<const int> span1(kArray1);
constexpr span<const int, 5> span2(kArray2);
EXPECT_EQ(span1, span2);
static constexpr int kArray3[] = {2, 7, 1, 8, 3};
constexpr span<const int> span3(kArray3);
EXPECT_FALSE(span1 == span3);
static double kArray4[] = {2.0, 7.0, 1.0, 8.0, 3.0};
span<double, 5> span4(kArray4);
EXPECT_EQ(span3, span4);
}
TEST(SpanTest, Inequality) {
static constexpr int kArray1[] = {2, 3, 5, 7, 11};
static constexpr int kArray2[] = {1, 4, 6, 8, 9};
constexpr span<const int> span1(kArray1);
constexpr span<const int, 5> span2(kArray2);
EXPECT_NE(span1, span2);
static constexpr int kArray3[] = {2, 3, 5, 7, 11};
constexpr span<const int> span3(kArray3);
EXPECT_FALSE(span1 != span3);
static double kArray4[] = {1.0, 4.0, 6.0, 8.0, 9.0};
span<double, 5> span4(kArray4);
EXPECT_NE(span3, span4);
}
TEST(SpanTest, LessThan) {
static constexpr int kArray1[] = {2, 3, 5, 7, 11};
static constexpr int kArray2[] = {2, 3, 5, 7, 11, 13};
constexpr span<const int> span1(kArray1);
constexpr span<const int, 6> span2(kArray2);
EXPECT_LT(span1, span2);
static constexpr int kArray3[] = {2, 3, 5, 7, 11};
constexpr span<const int> span3(kArray3);
EXPECT_FALSE(span1 < span3);
static double kArray4[] = {2.0, 3.0, 5.0, 7.0, 11.0, 13.0};
span<double, 6> span4(kArray4);
EXPECT_LT(span3, span4);
}
TEST(SpanTest, LessEqual) {
static constexpr int kArray1[] = {2, 3, 5, 7, 11};
static constexpr int kArray2[] = {2, 3, 5, 7, 11, 13};
constexpr span<const int> span1(kArray1);
constexpr span<const int, 6> span2(kArray2);
EXPECT_LE(span1, span1);
EXPECT_LE(span1, span2);
static constexpr int kArray3[] = {2, 3, 5, 7, 10};
constexpr span<const int> span3(kArray3);
EXPECT_FALSE(span1 <= span3);
static double kArray4[] = {2.0, 3.0, 5.0, 7.0, 11.0, 13.0};
span<double, 6> span4(kArray4);
EXPECT_LE(span3, span4);
}
TEST(SpanTest, GreaterThan) {
static constexpr int kArray1[] = {2, 3, 5, 7, 11, 13};
static constexpr int kArray2[] = {2, 3, 5, 7, 11};
constexpr span<const int> span1(kArray1);
constexpr span<const int, 5> span2(kArray2);
EXPECT_GT(span1, span2);
static constexpr int kArray3[] = {2, 3, 5, 7, 11, 13};
constexpr span<const int> span3(kArray3);
EXPECT_FALSE(span1 > span3);
static double kArray4[] = {2.0, 3.0, 5.0, 7.0, 11.0};
span<double, 5> span4(kArray4);
EXPECT_GT(span3, span4);
}
TEST(SpanTest, GreaterEqual) {
static constexpr int kArray1[] = {2, 3, 5, 7, 11, 13};
static constexpr int kArray2[] = {2, 3, 5, 7, 11};
constexpr span<const int> span1(kArray1);
constexpr span<const int, 5> span2(kArray2);
EXPECT_GE(span1, span1);
EXPECT_GE(span1, span2);
static constexpr int kArray3[] = {2, 3, 5, 7, 12};
constexpr span<const int> span3(kArray3);
EXPECT_FALSE(span1 >= span3);
static double kArray4[] = {2.0, 3.0, 5.0, 7.0, 11.0};
span<double, 5> span4(kArray4);
EXPECT_GE(span3, span4);
}
TEST(SpanTest, AsBytes) {
{
constexpr int kArray[] = {2, 3, 5, 7, 11, 13};
span<const uint8_t, sizeof(kArray)> bytes_span =
as_bytes(make_span(kArray));
EXPECT_EQ(reinterpret_cast<const uint8_t*>(kArray), bytes_span.data());
EXPECT_EQ(sizeof(kArray), bytes_span.size());
EXPECT_EQ(bytes_span.size(), bytes_span.size_bytes());
}
{
std::vector<int> vec = {1, 1, 2, 3, 5, 8};
span<int> mutable_span(vec);
span<const uint8_t> bytes_span = as_bytes(mutable_span);
EXPECT_EQ(reinterpret_cast<const uint8_t*>(vec.data()), bytes_span.data());
EXPECT_EQ(sizeof(int) * vec.size(), bytes_span.size());
EXPECT_EQ(bytes_span.size(), bytes_span.size_bytes());
}
}
TEST(SpanTest, AsWritableBytes) {
std::vector<int> vec = {1, 1, 2, 3, 5, 8};
span<int> mutable_span(vec);
span<uint8_t> writable_bytes_span = as_writable_bytes(mutable_span);
EXPECT_EQ(reinterpret_cast<uint8_t*>(vec.data()), writable_bytes_span.data());
EXPECT_EQ(sizeof(int) * vec.size(), writable_bytes_span.size());
EXPECT_EQ(writable_bytes_span.size(), writable_bytes_span.size_bytes());
// Set the first entry of vec to zero while writing through the span.
std::fill(writable_bytes_span.data(),
writable_bytes_span.data() + sizeof(int), 0);
EXPECT_EQ(0, vec[0]);
}
TEST(SpanTest, MakeSpanFromDataAndSize) {
int* nullint = nullptr;
auto empty_span = make_span(nullint, 0);
EXPECT_TRUE(empty_span.empty());
EXPECT_EQ(nullptr, empty_span.data());
std::vector<int> vector = {1, 1, 2, 3, 5, 8};
span<int> span(vector.data(), vector.size());
auto made_span = make_span(vector.data(), vector.size());
EXPECT_EQ(span, made_span);
static_assert(decltype(made_span)::extent == dynamic_extent, "");
}
TEST(SpanTest, MakeSpanFromPointerPair) {
int* nullint = nullptr;
auto empty_span = make_span(nullint, nullint);
EXPECT_TRUE(empty_span.empty());
EXPECT_EQ(nullptr, empty_span.data());
std::vector<int> vector = {1, 1, 2, 3, 5, 8};
span<int> span(vector.data(), vector.size());
auto made_span = make_span(vector.data(), vector.data() + vector.size());
EXPECT_EQ(span, made_span);
static_assert(decltype(made_span)::extent == dynamic_extent, "");
}
TEST(SpanTest, MakeSpanFromConstexprArray) {
static constexpr int kArray[] = {1, 2, 3, 4, 5};
constexpr span<const int> span(kArray);
EXPECT_EQ(span, make_span(kArray));
static_assert(decltype(make_span(kArray))::extent == 5, "");
}
TEST(SpanTest, MakeSpanFromStdArray) {
const std::array<int, 5> kArray = {{1, 2, 3, 4, 5}};
span<const int> span(kArray);
EXPECT_EQ(span, make_span(kArray));
static_assert(decltype(make_span(kArray))::extent == 5, "");
}
TEST(SpanTest, MakeSpanFromConstContainer) {
const std::vector<int> vector = {-1, -2, -3, -4, -5};
span<const int> span(vector);
EXPECT_EQ(span, make_span(vector));
static_assert(decltype(make_span(vector))::extent == dynamic_extent, "");
}
#ifndef STARBOARD
// Unable to do these conversions.
TEST(SpanTest, MakeStaticSpanFromConstContainer) {
const std::vector<int> vector = {-1, -2, -3, -4, -5};
span<const int, 5> span(vector);
EXPECT_EQ(span, make_span<5>(vector));
static_assert(decltype(make_span<5>(vector))::extent == 5, "");
}
#endif
TEST(SpanTest, MakeSpanFromContainer) {
std::vector<int> vector = {-1, -2, -3, -4, -5};
span<int> span(vector);
EXPECT_EQ(span, make_span(vector));
static_assert(decltype(make_span(vector))::extent == dynamic_extent, "");
}
#ifndef STARBOARD
// Unable to do these conversions.
TEST(SpanTest, MakeStaticSpanFromContainer) {
std::vector<int> vector = {-1, -2, -3, -4, -5};
span<int, 5> span(vector);
EXPECT_EQ(span, make_span<5>(vector));
static_assert(decltype(make_span<5>(vector))::extent == 5, "");
}
#endif
TEST(SpanTest, MakeSpanFromDynamicSpan) {
static constexpr int kArray[] = {1, 2, 3, 4, 5};
constexpr span<const int> span(kArray);
static_assert(std::is_same<decltype(span)::element_type,
decltype(make_span(span))::element_type>::value,
"make_span(span) should have the same element_type as span");
static_assert(span.data() == make_span(span).data(),
"make_span(span) should have the same data() as span");
static_assert(span.size() == make_span(span).size(),
"make_span(span) should have the same size() as span");
static_assert(decltype(make_span(span))::extent == decltype(span)::extent,
"make_span(span) should have the same extent as span");
}
TEST(SpanTest, MakeSpanFromStaticSpan) {
static constexpr int kArray[] = {1, 2, 3, 4, 5};
constexpr span<const int, 5> span(kArray);
static_assert(std::is_same<decltype(span)::element_type,
decltype(make_span(span))::element_type>::value,
"make_span(span) should have the same element_type as span");
static_assert(span.data() == make_span(span).data(),
"make_span(span) should have the same data() as span");
static_assert(span.size() == make_span(span).size(),
"make_span(span) should have the same size() as span");
static_assert(decltype(make_span(span))::extent == decltype(span)::extent,
"make_span(span) should have the same extent as span");
}
TEST(SpanTest, EnsureConstexprGoodness) {
static constexpr int kArray[] = {5, 4, 3, 2, 1};
constexpr span<const int> constexpr_span(kArray);
const size_t size = 2;
const size_t start = 1;
constexpr span<const int> subspan =
constexpr_span.subspan(start, start + size);
for (size_t i = 0; i < subspan.size(); ++i)
EXPECT_EQ(kArray[start + i], subspan[i]);
constexpr span<const int> firsts = constexpr_span.first(size);
for (size_t i = 0; i < firsts.size(); ++i)
EXPECT_EQ(kArray[i], firsts[i]);
constexpr span<const int> lasts = constexpr_span.last(size);
for (size_t i = 0; i < lasts.size(); ++i) {
const size_t j = (arraysize(kArray) - size) + i;
EXPECT_EQ(kArray[j], lasts[i]);
}
constexpr int item = constexpr_span[size];
EXPECT_EQ(kArray[size], item);
}
} // namespace base