mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Upgrade to C++20 (#4239)
* Use explicit this capture required by C++20 * Use C++20 span * Replace wpi::numbers with std::numbers * Fix C++20 clang-tidy warning false positive in fmt * Remove ciso646 include since C++20 removed that header * Fix global-buffer-overflow asan warnings in ntcore tests * Add DIOSetProxy constructor to HAL * Upgrade MSVC compiler to 2022 * Bump native-utils to 2023.2.7 (changes to std=c++20) Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
This commit is contained in:
@@ -200,7 +200,7 @@ TEST(JsonReadmeTest, OtherContainer)
|
||||
{
|
||||
std::vector<int> c_vector {1, 2, 3, 4};
|
||||
json j_vec(c_vector);
|
||||
json j_vec2(wpi::span<const int>(c_vector.data(), c_vector.size()));
|
||||
json j_vec2(std::span<const int>(c_vector.data(), c_vector.size()));
|
||||
// [1, 2, 3, 4]
|
||||
|
||||
std::deque<float> c_deque {1.2f, 2.3f, 3.4f, 5.6f};
|
||||
|
||||
@@ -18,7 +18,7 @@ using namespace wpi;
|
||||
TEST(ConvertUTFTest, ConvertUTF16LittleEndianToUTF8String) {
|
||||
// Src is the look of disapproval.
|
||||
alignas(UTF16) static const char Src[] = "\xff\xfe\xa0\x0c_\x00\xa0\x0c";
|
||||
span<const char> Ref(Src, sizeof(Src) - 1);
|
||||
std::span<const char> Ref(Src, sizeof(Src) - 1);
|
||||
SmallString<20> Result;
|
||||
bool Success = convertUTF16ToUTF8String(Ref, Result);
|
||||
EXPECT_TRUE(Success);
|
||||
@@ -29,7 +29,7 @@ TEST(ConvertUTFTest, ConvertUTF16LittleEndianToUTF8String) {
|
||||
TEST(ConvertUTFTest, ConvertUTF16BigEndianToUTF8String) {
|
||||
// Src is the look of disapproval.
|
||||
alignas(UTF16) static const char Src[] = "\xfe\xff\x0c\xa0\x00_\x0c\xa0";
|
||||
span<const char> Ref(Src, sizeof(Src) - 1);
|
||||
std::span<const char> Ref(Src, sizeof(Src) - 1);
|
||||
SmallString<20> Result;
|
||||
bool Success = convertUTF16ToUTF8String(Ref, Result);
|
||||
EXPECT_TRUE(Success);
|
||||
@@ -52,13 +52,13 @@ TEST(ConvertUTFTest, ConvertUTF8ToUTF16String) {
|
||||
|
||||
TEST(ConvertUTFTest, OddLengthInput) {
|
||||
SmallString<20> Result;
|
||||
bool Success = convertUTF16ToUTF8String(span<const char>("xxxxx", 5), Result);
|
||||
bool Success = convertUTF16ToUTF8String(std::span<const char>("xxxxx", 5), Result);
|
||||
EXPECT_FALSE(Success);
|
||||
}
|
||||
|
||||
TEST(ConvertUTFTest, Empty) {
|
||||
SmallString<20> Result;
|
||||
bool Success = convertUTF16ToUTF8String(span<const char>(), Result);
|
||||
bool Success = convertUTF16ToUTF8String(std::span<const char>(), Result);
|
||||
EXPECT_TRUE(Success);
|
||||
EXPECT_TRUE(std::string{Result}.empty());
|
||||
}
|
||||
@@ -82,7 +82,7 @@ TEST(ConvertUTFTest, HasUTF16BOM) {
|
||||
TEST(ConvertUTFTest, UTF16WrappersForConvertUTF16ToUTF8String) {
|
||||
// Src is the look of disapproval.
|
||||
alignas(UTF16) static const char Src[] = "\xff\xfe\xa0\x0c_\x00\xa0\x0c";
|
||||
span<const UTF16> SrcRef((const UTF16 *)Src, 4);
|
||||
std::span<const UTF16> SrcRef((const UTF16 *)Src, 4);
|
||||
SmallString<20> Result;
|
||||
bool Success = convertUTF16ToUTF8String(SrcRef, Result);
|
||||
EXPECT_TRUE(Success);
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "wpi/SmallVector.h"
|
||||
#include "wpi/span.h"
|
||||
#include <span>
|
||||
#include "wpi/Compiler.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include <list>
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
#if __has_include(<span>)
|
||||
#include <span>
|
||||
#endif
|
||||
#include "wpi/span.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
void func1(wpi::span<const int> x) {}
|
||||
#ifdef __cpp_lib_span
|
||||
void func2(std::span<const int> x) {}
|
||||
#endif
|
||||
|
||||
void func4(wpi::span<int> x) {}
|
||||
#ifdef __cpp_lib_span
|
||||
void func5(std::span<int> x) {}
|
||||
#endif
|
||||
|
||||
TEST(Span, ConvertStdSpan) {
|
||||
func1(wpi::span<const int>{});
|
||||
func1(wpi::span<int>{});
|
||||
#ifdef __cpp_lib_span
|
||||
func1(std::span<const int>{});
|
||||
func1(std::span<int>{});
|
||||
#endif
|
||||
|
||||
#ifdef __cpp_lib_span
|
||||
func2(wpi::span<const int>{});
|
||||
func2(wpi::span<int>{});
|
||||
func2(std::span<const int>{});
|
||||
func2(std::span<int>{});
|
||||
#endif
|
||||
|
||||
//func4(wpi::span<const int>{});
|
||||
func4(wpi::span<int>{});
|
||||
#ifdef __cpp_lib_span
|
||||
//func4(std::span<const int>{});
|
||||
func4(std::span<int>{});
|
||||
#endif
|
||||
|
||||
#ifdef __cpp_lib_span
|
||||
//func5(wpi::span<const int>{});
|
||||
func5(wpi::span<int>{});
|
||||
//func5(std::span<const int>{});
|
||||
func5(std::span<int>{});
|
||||
#endif
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
|
||||
#include "wpi/span.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
using wpi::span;
|
||||
|
||||
namespace {
|
||||
|
||||
template <typename R1, typename R2>
|
||||
constexpr bool equal(R1&& r1, R2&& r2)
|
||||
{
|
||||
auto first1 = std::begin(r1);
|
||||
const auto last1 = std::end(r1);
|
||||
auto first2 = std::begin(r2);
|
||||
const auto last2 = std::end(r2);
|
||||
|
||||
while (first1 != last1 && first2 != last2) {
|
||||
if (*first1 != *first2) {
|
||||
return false;
|
||||
}
|
||||
++first1;
|
||||
++first2;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool test_raw_array()
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
auto s = span{arr};
|
||||
static_assert(std::is_same_v<decltype(s), span<int, 3>>);
|
||||
|
||||
return equal(arr, s);
|
||||
}
|
||||
|
||||
constexpr bool test_const_raw_array()
|
||||
{
|
||||
constexpr int arr[] = {1, 2, 3};
|
||||
auto s = span{arr};
|
||||
static_assert(std::is_same_v<decltype(s), span<const int, 3>>);
|
||||
|
||||
return equal(arr, s);
|
||||
}
|
||||
|
||||
constexpr bool test_std_array()
|
||||
{
|
||||
auto arr = std::array<int, 3>{1, 2, 3};
|
||||
auto s = span{arr};
|
||||
static_assert(std::is_same_v<decltype(s), span<int, 3>>);
|
||||
|
||||
return equal(arr, s);
|
||||
}
|
||||
|
||||
constexpr bool test_const_std_array()
|
||||
{
|
||||
const auto arr = std::array<int, 3>{1, 2, 3};
|
||||
auto s = span{arr};
|
||||
static_assert(std::is_same_v<decltype(s), span<const int, 3>>);
|
||||
|
||||
return equal(arr, s);
|
||||
}
|
||||
|
||||
bool test_vec()
|
||||
{
|
||||
auto arr = std::vector{1, 2, 3};
|
||||
auto s = span{arr};
|
||||
static_assert(std::is_same_v<decltype(s), span<int>>);
|
||||
|
||||
return equal(arr, s);
|
||||
}
|
||||
|
||||
bool test_const_vec()
|
||||
{
|
||||
const auto arr = std::vector{1, 2, 3};
|
||||
auto s = span{arr};
|
||||
static_assert(std::is_same_v<decltype(s), span<const int>>);
|
||||
|
||||
return equal(arr, s);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST(Deduction, FromRawArrays)
|
||||
{
|
||||
static_assert(test_raw_array());
|
||||
static_assert(test_const_raw_array());
|
||||
static_assert(test_std_array());
|
||||
static_assert(test_const_std_array());
|
||||
|
||||
ASSERT_TRUE(test_std_array());
|
||||
ASSERT_TRUE(test_const_std_array());
|
||||
ASSERT_TRUE(test_vec());
|
||||
ASSERT_TRUE(test_const_vec());
|
||||
}
|
||||
@@ -1,663 +0,0 @@
|
||||
|
||||
#include "wpi/span.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <deque>
|
||||
#include <initializer_list>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using wpi::span;
|
||||
|
||||
struct base {};
|
||||
struct derived : base {};
|
||||
|
||||
TEST(Span, DefaultConstruction)
|
||||
{
|
||||
static_assert(std::is_nothrow_default_constructible<span<int>>::value, "");
|
||||
static_assert(std::is_nothrow_default_constructible<span<int, 0>>::value,
|
||||
"");
|
||||
static_assert(!std::is_default_constructible<span<int, 42>>::value, "");
|
||||
|
||||
//SECTION("dynamic size")
|
||||
{
|
||||
constexpr span<int> s{};
|
||||
static_assert(s.size() == 0, "");
|
||||
static_assert(s.data() == nullptr, "");
|
||||
// This causes an ICE on MSVC
|
||||
#ifndef _MSC_VER
|
||||
static_assert(s.begin() == s.end(), "");
|
||||
#else
|
||||
ASSERT_EQ(s.begin(), s.end());
|
||||
#endif
|
||||
}
|
||||
|
||||
//SECTION("fixed size")
|
||||
{
|
||||
constexpr span<int, 0> s{};
|
||||
static_assert(s.size() == 0, "");
|
||||
static_assert(s.data() == nullptr, "");
|
||||
#ifndef _MSC_VER
|
||||
static_assert(s.begin() == s.end(), "");
|
||||
#else
|
||||
ASSERT_EQ(s.begin(), s.end());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Span, PointerLengthConstruction)
|
||||
{
|
||||
static_assert(std::is_constructible<span<int>, int*, int>::value, "");
|
||||
static_assert(std::is_constructible<span<const int>, int*, int>::value, "");
|
||||
static_assert(
|
||||
std::is_constructible<span<const int>, const int*, int>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<span<int, 42>, int*, int>::value, "");
|
||||
static_assert(std::is_constructible<span<const int, 42>, int*, int>::value,
|
||||
"");
|
||||
static_assert(
|
||||
std::is_constructible<span<const int, 42>, const int*, int>::value, "");
|
||||
|
||||
//SECTION("dynamic size")
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
span<int> s(arr, 3);
|
||||
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.begin(), std::begin(arr));
|
||||
ASSERT_EQ(s.end(), std::end(arr));
|
||||
}
|
||||
|
||||
//SECTION("fixed size")
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
span<int, 3> s(arr, 3);
|
||||
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.begin(), std::begin(arr));
|
||||
ASSERT_EQ(s.end(), std::end(arr));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Span, PointerPointerConstruction)
|
||||
{
|
||||
static_assert(std::is_constructible<span<int>, int*, int*>::value, "");
|
||||
static_assert(!std::is_constructible<span<int>, float*, float*>::value, "");
|
||||
static_assert(std::is_constructible<span<int, 42>, int*, int*>::value, "");
|
||||
static_assert(!std::is_constructible<span<int, 42>, float*, float*>::value,
|
||||
"");
|
||||
|
||||
//SECTION("dynamic size")
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
span<int> s{arr, arr + 3};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.begin(), std::begin(arr));
|
||||
ASSERT_EQ(s.end(), std::end(arr));
|
||||
}
|
||||
|
||||
//SECTION("fixed size")
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
span<int, 3> s{arr, arr + 3};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.begin(), std::begin(arr));
|
||||
ASSERT_EQ(s.end(), std::end(arr));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Span, CArrayConstruction)
|
||||
{
|
||||
using int_array_t = int[3];
|
||||
using float_array_t = float[3];
|
||||
|
||||
static_assert(std::is_nothrow_constructible<span<int>, int_array_t&>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<span<int>, int_array_t const&>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<span<int>, float_array_t>::value, "");
|
||||
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<span<const int>, int_array_t&>::value,
|
||||
"");
|
||||
static_assert(std::is_nothrow_constructible<span<const int>,
|
||||
int_array_t const&>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<span<const int>, float_array_t>::value,
|
||||
"");
|
||||
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<span<int, 3>, int_array_t&>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<span<int, 3>, int_array_t const&>::value, "");
|
||||
static_assert(!std::is_constructible<span<int, 3>, float_array_t&>::value,
|
||||
"");
|
||||
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<span<const int, 3>, int_array_t&>::value,
|
||||
"");
|
||||
static_assert(std::is_nothrow_constructible<span<const int, 3>,
|
||||
int_array_t const&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 3>, float_array_t>::value, "");
|
||||
|
||||
static_assert(!std::is_constructible<span<int, 42>, int_array_t&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<int, 42>, int_array_t const&>::value, "");
|
||||
static_assert(!std::is_constructible<span<int, 42>, float_array_t&>::value,
|
||||
"");
|
||||
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 42>, int_array_t&>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 42>, int_array_t const&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 42>, float_array_t&>::value, "");
|
||||
|
||||
//SECTION("non-const, dynamic size")
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
span<int> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.begin(), std::begin(arr));
|
||||
ASSERT_EQ(s.end(), std::end(arr));
|
||||
}
|
||||
|
||||
//SECTION("const, dynamic size")
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
span<int const> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.begin(), std::begin(arr));
|
||||
ASSERT_EQ(s.end(), std::end(arr));
|
||||
}
|
||||
|
||||
//SECTION("non-const, static size")
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
span<int, 3> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.begin(), std::begin(arr));
|
||||
ASSERT_EQ(s.end(), std::end(arr));
|
||||
}
|
||||
|
||||
//SECTION("const, dynamic size")
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
span<int const, 3> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.begin(), std::begin(arr));
|
||||
ASSERT_EQ(s.end(), std::end(arr));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Span, StdArrayConstruction)
|
||||
{
|
||||
using int_array_t = std::array<int, 3>;
|
||||
using float_array_t = std::array<float, 3>;
|
||||
using zero_array_t = std::array<int, 0>;
|
||||
|
||||
static_assert(std::is_nothrow_constructible<span<int>, int_array_t&>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<span<int>, int_array_t const&>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<span<int>, float_array_t>::value, "");
|
||||
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<span<const int>, int_array_t&>::value,
|
||||
"");
|
||||
static_assert(std::is_nothrow_constructible<span<const int>,
|
||||
int_array_t const&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int>, float_array_t const&>::value,
|
||||
"");
|
||||
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<span<int, 3>, int_array_t&>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<span<int, 3>, int_array_t const&>::value, "");
|
||||
static_assert(!std::is_constructible<span<int, 3>, float_array_t>::value,
|
||||
"");
|
||||
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<span<const int, 3>, int_array_t&>::value,
|
||||
"");
|
||||
static_assert(std::is_nothrow_constructible<span<const int, 3>,
|
||||
int_array_t const&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 3>, float_array_t const&>::value,
|
||||
"");
|
||||
|
||||
static_assert(!std::is_constructible<span<int, 42>, int_array_t&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<int, 42>, int_array_t const&>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<span<int, 42>, float_array_t const&>::value, "");
|
||||
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 42>, int_array_t&>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 42>, int_array_t const&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 42>, float_array_t&>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<span<int>, zero_array_t&>::value, "");
|
||||
static_assert(!std::is_constructible<span<int>, const zero_array_t&>::value,
|
||||
"");
|
||||
static_assert(std::is_constructible<span<const int>, zero_array_t&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
std::is_constructible<span<const int>, const zero_array_t&>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<span<int, 0>, zero_array_t&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<int, 0>, const zero_array_t&>::value, "");
|
||||
static_assert(
|
||||
std::is_constructible<span<const int, 0>, zero_array_t&>::value, "");
|
||||
static_assert(
|
||||
std::is_constructible<span<const int, 0>, const zero_array_t&>::value,
|
||||
"");
|
||||
|
||||
//SECTION("non-const, dynamic size")
|
||||
{
|
||||
int_array_t arr = {1, 2, 3};
|
||||
span<int> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.begin(), arr.data());
|
||||
ASSERT_EQ(s.end(), arr.data() + 3);
|
||||
}
|
||||
|
||||
//SECTION("const, dynamic size")
|
||||
{
|
||||
int_array_t arr = {1, 2, 3};
|
||||
span<int const> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.begin(), arr.data());
|
||||
ASSERT_EQ(s.end(), arr.data() + 3);
|
||||
}
|
||||
|
||||
//SECTION("non-const, static size")
|
||||
{
|
||||
int_array_t arr = {1, 2, 3};
|
||||
span<int, 3> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.begin(), arr.data());
|
||||
ASSERT_EQ(s.end(), arr.data() + 3);
|
||||
}
|
||||
|
||||
//SECTION("const, dynamic size")
|
||||
{
|
||||
int_array_t arr = {1, 2, 3};
|
||||
span<int const, 3> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.begin(), arr.data());
|
||||
ASSERT_EQ(s.end(), arr.data() + 3);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Span, ConstructionFromOtherContainers)
|
||||
{
|
||||
using vec_t = std::vector<int>;
|
||||
using deque_t = std::deque<int>;
|
||||
|
||||
static_assert(std::is_constructible<span<int>, vec_t&>::value, "");
|
||||
static_assert(!std::is_constructible<span<int>, const vec_t&>::value, "");
|
||||
static_assert(!std::is_constructible<span<int>, const deque_t&>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<span<const int>, vec_t&>::value, "");
|
||||
static_assert(std::is_constructible<span<const int>, const vec_t&>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int>, const deque_t&>::value, "");
|
||||
|
||||
static_assert(!std::is_constructible<span<int, 3>, vec_t&>::value, "");
|
||||
static_assert(!std::is_constructible<span<int, 3>, const vec_t&>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<span<int, 3>, const deque_t&>::value,
|
||||
"");
|
||||
|
||||
static_assert(!std::is_constructible<span<const int, 3>, vec_t&>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 3>, const vec_t&>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<span<const int, 3>, const deque_t&>::value, "");
|
||||
|
||||
// vector<bool> is not contiguous and cannot be converted to span<bool>
|
||||
// Regression test for https://github.com/tcbrindle/span/issues/24
|
||||
static_assert(
|
||||
!std::is_constructible<span<bool>, std::vector<bool>&>::value, "");
|
||||
static_assert(!std::is_constructible<span<const bool>,
|
||||
const std::vector<bool>&>::value, "");
|
||||
|
||||
//SECTION("non-const, dynamic size")
|
||||
{
|
||||
vec_t arr = {1, 2, 3};
|
||||
span<int> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.begin(), arr.data());
|
||||
ASSERT_EQ(s.end(), arr.data() + 3);
|
||||
}
|
||||
|
||||
//SECTION("const, dynamic size")
|
||||
{
|
||||
vec_t arr = {1, 2, 3};
|
||||
span<int const> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.begin(), arr.data());
|
||||
ASSERT_EQ(s.end(), arr.data() + 3);
|
||||
}
|
||||
|
||||
//SECTION("non-const, static size")
|
||||
{
|
||||
std::array<int, 3> arr = {1, 2, 3};
|
||||
span<int, 3> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.begin(), arr.data());
|
||||
ASSERT_EQ(s.end(), arr.data() + 3);
|
||||
}
|
||||
|
||||
//SECTION("const, dynamic size")
|
||||
{
|
||||
std::array<int, 3> arr = {1, 2, 3};
|
||||
span<int const, 3> s{arr};
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.begin(), arr.data());
|
||||
ASSERT_EQ(s.end(), arr.data() + 3);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Span, ConstructionFromSpansOfDifferentSize)
|
||||
{
|
||||
using zero_span = span<int, 0>;
|
||||
using zero_const_span = span<const int, 0>;
|
||||
using big_span = span<int, 1000000>;
|
||||
using big_const_span = span<const int, 1000000>;
|
||||
using dynamic_span = span<int>;
|
||||
using dynamic_const_span = span<const int>;
|
||||
|
||||
static_assert(std::is_trivially_copyable<zero_span>::value, "");
|
||||
static_assert(std::is_trivially_move_constructible<zero_span>::value, "");
|
||||
static_assert(!std::is_constructible<zero_span, zero_const_span>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<zero_span, big_span>::value, "");
|
||||
static_assert(!std::is_constructible<zero_span, big_const_span>::value, "");
|
||||
static_assert(!std::is_constructible<zero_span, dynamic_span>::value, "");
|
||||
static_assert(!std::is_constructible<zero_span, dynamic_const_span>::value,
|
||||
"");
|
||||
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<zero_const_span, zero_span>::value, "");
|
||||
static_assert(std::is_trivially_copyable<zero_const_span>::value, "");
|
||||
static_assert(std::is_trivially_move_constructible<zero_const_span>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<zero_const_span, big_span>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<zero_const_span, big_const_span>::value, "");
|
||||
static_assert(!std::is_constructible<zero_const_span, dynamic_span>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<zero_const_span, dynamic_const_span>::value, "");
|
||||
|
||||
static_assert(!std::is_constructible<big_span, zero_span>::value, "");
|
||||
static_assert(!std::is_constructible<big_span, zero_const_span>::value, "");
|
||||
static_assert(std::is_trivially_copyable<big_span>::value, "");
|
||||
static_assert(std::is_trivially_move_constructible<big_span>::value, "");
|
||||
static_assert(!std::is_constructible<big_span, big_const_span>::value, "");
|
||||
static_assert(!std::is_constructible<big_span, dynamic_span>::value, "");
|
||||
static_assert(!std::is_constructible<big_span, dynamic_const_span>::value,
|
||||
"");
|
||||
|
||||
static_assert(!std::is_constructible<big_const_span, zero_span>::value, "");
|
||||
static_assert(
|
||||
!std::is_constructible<big_const_span, zero_const_span>::value, "");
|
||||
static_assert(std::is_trivially_copyable<big_const_span>::value, "");
|
||||
static_assert(std::is_trivially_move_constructible<big_const_span>::value,
|
||||
"");
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<big_const_span, big_span>::value, "");
|
||||
static_assert(!std::is_constructible<big_const_span, dynamic_span>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<big_const_span, dynamic_const_span>::value, "");
|
||||
|
||||
static_assert(std::is_nothrow_constructible<dynamic_span, zero_span>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<dynamic_span, zero_const_span>::value,
|
||||
"");
|
||||
static_assert(std::is_nothrow_constructible<dynamic_span, big_span>::value,
|
||||
"");
|
||||
static_assert(!std::is_constructible<dynamic_span, big_const_span>::value,
|
||||
"");
|
||||
static_assert(std::is_trivially_copyable<dynamic_span>::value, "");
|
||||
static_assert(std::is_trivially_move_constructible<dynamic_span>::value,
|
||||
"");
|
||||
static_assert(
|
||||
!std::is_constructible<dynamic_span, dynamic_const_span>::value, "");
|
||||
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<dynamic_const_span, zero_span>::value,
|
||||
"");
|
||||
static_assert(std::is_nothrow_constructible<dynamic_const_span,
|
||||
zero_const_span>::value,
|
||||
"");
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<dynamic_const_span, big_span>::value, "");
|
||||
static_assert(std::is_nothrow_constructible<dynamic_const_span,
|
||||
big_const_span>::value,
|
||||
"");
|
||||
static_assert(
|
||||
std::is_nothrow_constructible<dynamic_const_span, dynamic_span>::value,
|
||||
"");
|
||||
static_assert(std::is_trivially_copyable<dynamic_const_span>::value, "");
|
||||
static_assert(
|
||||
std::is_trivially_move_constructible<dynamic_const_span>::value, "");
|
||||
|
||||
constexpr zero_const_span s0{};
|
||||
constexpr dynamic_const_span d{s0};
|
||||
|
||||
static_assert(d.size() == 0, "");
|
||||
static_assert(d.data() == nullptr, "");
|
||||
#ifndef _MSC_VER
|
||||
static_assert(d.begin() == d.end(), "");
|
||||
#else
|
||||
ASSERT_EQ(d.begin(), d.end());
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(Span, MemberSubviewOperations)
|
||||
{
|
||||
//SECTION("first<N>")
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
span<int, 5> s{arr};
|
||||
auto f = s.first<3>();
|
||||
|
||||
static_assert(std::is_same<decltype(f), span<int, 3>>::value, "");
|
||||
ASSERT_EQ(f.size(), 3u);
|
||||
ASSERT_EQ(f.data(), arr);
|
||||
ASSERT_EQ(f.begin(), arr);
|
||||
ASSERT_EQ(f.end(), arr + 3);
|
||||
}
|
||||
|
||||
//SECTION("last<N>")
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
span<int, 5> s{arr};
|
||||
auto l = s.last<3>();
|
||||
|
||||
static_assert(std::is_same<decltype(l), span<int, 3>>::value, "");
|
||||
ASSERT_EQ(l.size(), 3u);
|
||||
ASSERT_EQ(l.data(), arr + 2);
|
||||
ASSERT_EQ(l.begin(), arr + 2);
|
||||
ASSERT_EQ(l.end(), std::end(arr));
|
||||
}
|
||||
|
||||
//SECTION("subspan<N>")
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
span<int, 5> s{arr};
|
||||
auto ss = s.subspan<1, 2>();
|
||||
|
||||
static_assert(std::is_same<decltype(ss), span<int, 2>>::value, "");
|
||||
ASSERT_EQ(ss.size(), 2u);
|
||||
ASSERT_EQ(ss.data(), arr + 1);
|
||||
ASSERT_EQ(ss.begin(), arr + 1);
|
||||
ASSERT_EQ(ss.end(), arr + 1 + 2);
|
||||
}
|
||||
|
||||
//SECTION("first(n)")
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
span<int, 5> s{arr};
|
||||
auto f = s.first(3);
|
||||
|
||||
static_assert(std::is_same<decltype(f), span<int>>::value, "");
|
||||
ASSERT_EQ(f.size(), 3u);
|
||||
ASSERT_EQ(f.data(), arr);
|
||||
ASSERT_EQ(f.begin(), arr);
|
||||
ASSERT_EQ(f.end(), arr + 3);
|
||||
}
|
||||
|
||||
//SECTION("last(n)")
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
span<int, 5> s{arr};
|
||||
auto l = s.last(3);
|
||||
|
||||
static_assert(std::is_same<decltype(l), span<int>>::value, "");
|
||||
ASSERT_EQ(l.size(), 3u);
|
||||
ASSERT_EQ(l.data(), arr + 2);
|
||||
ASSERT_EQ(l.begin(), arr + 2);
|
||||
ASSERT_EQ(l.end(), std::end(arr));
|
||||
}
|
||||
|
||||
//SECTION("subspan(n)")
|
||||
{
|
||||
int arr[] = {1, 2, 3, 4, 5};
|
||||
span<int, 5> s{arr};
|
||||
auto ss = s.subspan(1, 2);
|
||||
|
||||
static_assert(std::is_same<decltype(ss), span<int>>::value, "");
|
||||
ASSERT_EQ(ss.size(), 2u);
|
||||
ASSERT_EQ(ss.data(), arr + 1);
|
||||
ASSERT_EQ(ss.begin(), arr + 1);
|
||||
ASSERT_EQ(ss.end(), arr + 1 + 2);
|
||||
}
|
||||
|
||||
// TODO: Test all the dynamic subspan possibilities
|
||||
}
|
||||
|
||||
TEST(Span, Observers)
|
||||
{
|
||||
// We already use this everywhere, but whatever
|
||||
constexpr span<int, 0> empty{};
|
||||
static_assert(empty.size() == 0, "");
|
||||
static_assert(empty.empty(), "");
|
||||
|
||||
constexpr int arr[] = {1, 2, 3};
|
||||
static_assert(span<const int>{arr}.size() == 3, "");
|
||||
static_assert(!span<const int>{arr}.empty(), "");
|
||||
}
|
||||
|
||||
TEST(Span, ElementAccess)
|
||||
{
|
||||
constexpr int arr[] = {1, 2, 3};
|
||||
span<const int> s{arr};
|
||||
|
||||
ASSERT_EQ(s[0], arr[0]);
|
||||
ASSERT_EQ(s[1], arr[1]);
|
||||
ASSERT_EQ(s[2], arr[2]);
|
||||
}
|
||||
|
||||
TEST(Span, IteratorSupport)
|
||||
{
|
||||
{
|
||||
std::vector<int> vec;
|
||||
span<int> s{vec};
|
||||
std::sort(s.begin(), s.end());
|
||||
ASSERT_TRUE(std::is_sorted(vec.cbegin(), vec.cend()));
|
||||
}
|
||||
|
||||
{
|
||||
const std::vector<int> vec{1, 2, 3};
|
||||
span<const int> s{vec};
|
||||
ASSERT_TRUE(std::equal(s.rbegin(), s.rend(), vec.crbegin()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Span, MakeSpan)
|
||||
{
|
||||
{
|
||||
int arr[3] = {1, 2, 3};
|
||||
auto s = wpi::span(arr);
|
||||
static_assert(std::is_same<decltype(s), span<int, 3>>::value, "");
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
}
|
||||
|
||||
{
|
||||
const int arr[3] = {1, 2, 3};
|
||||
auto s = wpi::span(arr);
|
||||
static_assert(std::is_same<decltype(s), span<const int, 3>>::value, "");
|
||||
ASSERT_EQ(s.data(), arr);
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<int, 3> arr = {1, 2, 3};
|
||||
auto s = wpi::span(arr);
|
||||
static_assert(std::is_same<decltype(s), span<int, 3>>::value, "");
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.size(), arr.size());
|
||||
}
|
||||
|
||||
{
|
||||
const std::array<int, 3> arr = {1, 2, 3};
|
||||
auto s = wpi::span(arr);
|
||||
static_assert(std::is_same<decltype(s), span<const int, 3>>::value, "");
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.size(), 3u);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<int> arr = {1, 2, 3};
|
||||
auto s = wpi::span(arr);
|
||||
static_assert(std::is_same<decltype(s), span<int>>::value, "");
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.size(), arr.size());
|
||||
}
|
||||
|
||||
{
|
||||
const std::vector<int> arr = {1, 2, 3};
|
||||
auto s = wpi::span(arr);
|
||||
static_assert(std::is_same<decltype(s), span<const int>>::value, "");
|
||||
ASSERT_EQ(s.data(), arr.data());
|
||||
ASSERT_EQ(s.size(), arr.size());
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
|
||||
#include "wpi/span.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using static_span_t = wpi::span<int, 3>;
|
||||
using dynamic_span_t = wpi::span<int>;
|
||||
|
||||
static_assert(std::tuple_size_v<static_span_t> == static_span_t::extent);
|
||||
static_assert(!wpi::detail::is_complete<std::tuple_size<dynamic_span_t>>::value);
|
||||
|
||||
TEST(StructuredBindings, Test)
|
||||
{
|
||||
// C++, why you no let me do constexpr structured bindings?
|
||||
|
||||
int arr[] = {1, 2, 3};
|
||||
|
||||
auto& [a1, a2, a3] = arr;
|
||||
auto&& [s1, s2, s3] = wpi::span(arr);
|
||||
|
||||
ASSERT_EQ(a1, s1);
|
||||
ASSERT_EQ(a2, s2);
|
||||
ASSERT_EQ(a3, s3);
|
||||
|
||||
a1 = 99;
|
||||
ASSERT_EQ(s1, 99);
|
||||
|
||||
s2 = 100;
|
||||
ASSERT_EQ(a2, 100);
|
||||
}
|
||||
Reference in New Issue
Block a user