[libc++] [test] Add tests for std::span construction from initializer lists.

Differential Revision: https://reviews.llvm.org/D116481
This commit is contained in:
Arthur O'Dwyer 2021-10-03 00:54:18 -04:00
parent e88eb6443f
commit 6a6a80e88e
3 changed files with 79 additions and 35 deletions

View File

@ -74,50 +74,48 @@ void checkCV()
}
}
template <typename T>
constexpr bool testConstexprSpan()
{
constexpr T val[2] = {};
ASSERT_NOEXCEPT(std::span<const T> {val});
ASSERT_NOEXCEPT(std::span<const T, 2>{val});
std::span<const T> s1{val};
std::span<const T, 2> s2{val};
return
s1.data() == &val[0] && s1.size() == 2
&& s2.data() == &val[0] && s2.size() == 2;
}
template <typename T>
void testRuntimeSpan()
template<class T>
constexpr bool testSpan()
{
T val[2] = {};
ASSERT_NOEXCEPT(std::span<T> {val});
ASSERT_NOEXCEPT(std::span<T>{val});
ASSERT_NOEXCEPT(std::span<T, 2>{val});
std::span<T> s1{val};
std::span<T, 2> s2{val};
assert(s1.data() == &val[0] && s1.size() == 2);
assert(s2.data() == &val[0] && s2.size() == 2);
ASSERT_NOEXCEPT(std::span<const T>{val});
ASSERT_NOEXCEPT(std::span<const T, 2>{val});
std::span<T> s1 = val;
std::span<T, 2> s2 = val;
std::span<const T> s3 = val;
std::span<const T, 2> s4 = val;
assert(s1.data() == val && s1.size() == 2);
assert(s2.data() == val && s2.size() == 2);
assert(s3.data() == val && s3.size() == 2);
assert(s4.data() == val && s4.size() == 2);
std::span<const int> s5 = {{1,2}};
std::span<const int, 2> s6 = {{1,2}};
assert(s5.size() == 2); // and it dangles
assert(s6.size() == 2); // and it dangles
return true;
}
struct A{};
struct A {};
int main(int, char**)
{
static_assert(testConstexprSpan<int>(), "");
static_assert(testConstexprSpan<long>(), "");
static_assert(testConstexprSpan<double>(), "");
static_assert(testConstexprSpan<A>(), "");
testSpan<int>();
testSpan<double>();
testSpan<A>();
testSpan<std::string>();
testRuntimeSpan<int>();
testRuntimeSpan<long>();
testRuntimeSpan<double>();
testRuntimeSpan<std::string>();
testRuntimeSpan<A>();
static_assert(testSpan<int>());
static_assert(testSpan<double>());
static_assert(testSpan<A>());
checkCV();
return 0;
return 0;
}

View File

@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <span>
#include <span>
#include <cassert>
struct Sink {
constexpr Sink() = default;
constexpr Sink(Sink*) {}
};
constexpr int count(std::span<const Sink> sp) {
return sp.size();
}
template<int N>
constexpr int countn(std::span<const Sink, N> sp) {
return sp.size();
}
constexpr bool test() {
Sink a[10];
assert(count({a}) == 10);
assert(count({a, a+10}) == 10);
assert(countn<10>({a}) == 10);
return true;
}
int main(int, char**)
{
test();
static_assert(test());
return 0;
}

View File

@ -31,7 +31,10 @@ int main(int, char**) {
int arr[] = {1, 2, 3};
createImplicitSpan<int, 1>(arr, 3);
std::span<const int> sp = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
std::span<int> sp = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
std::span<int, 2> sp2 = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<int, 2>'}}
std::span<const int> csp = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
std::span<const int, 2> csp2 = {0, 0}; // expected-error {{no matching constructor for initialization of 'std::span<const int, 2>'}}
return 0;
}