mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-25 01:01:58 -05:00
Fix C++20 bugs and tests
This commit is contained in:
parent
e0880931ae
commit
4430ba9663
@ -143,10 +143,10 @@ GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
|
||||
|
||||
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
|
||||
template <class T, size_t extent = std::dynamic_extent>
|
||||
constexpr auto at(std::span<T, extent> sp, const index i)
|
||||
constexpr auto at(std::span<T, extent> sp, const index i) -> decltype(sp[sp.size()])
|
||||
{
|
||||
Expects(i >= 0 && i < narrow_cast<i>(sp.size()));
|
||||
return sp[i];
|
||||
Expects(i >= 0 && i < narrow_cast<index>(sp.size()));
|
||||
return sp[gsl::narrow_cast<size_t>(i)];
|
||||
}
|
||||
#endif // __cpp_lib_span >= 202002L
|
||||
} // namespace gsl
|
||||
|
@ -11,16 +11,24 @@ jobs:
|
||||
matrix:
|
||||
14_debug:
|
||||
GSL_CXX_STANDARD: '14'
|
||||
CMAKE_CXX_STANDARD: '14'
|
||||
BUILD_TYPE: 'Debug'
|
||||
14_release:
|
||||
GSL_CXX_STANDARD: '14'
|
||||
CMAKE_CXX_STANDARD: '14'
|
||||
BUILD_TYPE: 'Release'
|
||||
17_debug:
|
||||
GSL_CXX_STANDARD: '17'
|
||||
CMAKE_CXX_STANDARD: '17'
|
||||
BUILD_TYPE: 'Debug'
|
||||
17_release:
|
||||
GSL_CXX_STANDARD: '17'
|
||||
CMAKE_CXX_STANDARD: '17'
|
||||
BUILD_TYPE: 'Release'
|
||||
20_debug:
|
||||
GSL_CXX_STANDARD: '17'
|
||||
CMAKE_CXX_STANDARD: '20'
|
||||
BUILD_TYPE: 'Debug'
|
||||
continueOnError: false
|
||||
steps:
|
||||
- template: ./steps.yml
|
||||
|
@ -3,7 +3,7 @@ steps:
|
||||
name: Configure
|
||||
inputs:
|
||||
workingDirectory: build
|
||||
cmakeArgs: '-DCMAKE_CXX_STANDARD=$(GSL_CXX_STANDARD) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -Werror=dev .. '
|
||||
cmakeArgs: '-DCMAKE_CXX_STANDARD=$(CMAKE_CXX_STANDARD) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -Werror=dev .. '
|
||||
|
||||
- task: CMake@1
|
||||
name: Build
|
||||
|
@ -130,6 +130,12 @@ else()
|
||||
-Wno-unused-member-function
|
||||
-Wno-unused-variable
|
||||
>
|
||||
$<$<BOOL:${CMAKE_CXX_STANDARD}>:
|
||||
$<$<EQUAL:${CMAKE_CXX_STANDARD},20>:
|
||||
-Wno-zero-as-null-pointer-constant # Very noisy, minor, and just so
|
||||
-Wno-sign-conversion # happens to trigger on googletest
|
||||
>
|
||||
>
|
||||
>
|
||||
$<$<CXX_COMPILER_ID:Clang>:
|
||||
$<$<AND:$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.99>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,6>>:
|
||||
@ -232,6 +238,11 @@ else()
|
||||
-Wpedantic
|
||||
-Wshadow
|
||||
-Wsign-conversion
|
||||
$<$<BOOL:${CMAKE_CXX_STANDARD}>: # happens to trigger on googletest
|
||||
$<$<EQUAL:${CMAKE_CXX_STANDARD},20>:
|
||||
-Wno-sign-conversion # happens to trigger on googletest
|
||||
>
|
||||
>
|
||||
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
|
||||
-Weverything
|
||||
-Wno-c++98-compat
|
||||
|
@ -27,7 +27,6 @@ namespace gsl
|
||||
struct fail_fast;
|
||||
} // namespace gsl
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
TEST(algorithm_tests, same_type)
|
||||
@ -73,8 +72,8 @@ TEST(algorithm_tests, same_type)
|
||||
std::array<int, 5> src{1, 2, 3, 4, 5};
|
||||
std::array<int, 10> dst{};
|
||||
|
||||
const span<int> src_span(src);
|
||||
const span<int, 10> dst_span(dst);
|
||||
const gsl::span<int> src_span(src);
|
||||
const gsl::span<int, 10> dst_span(dst);
|
||||
|
||||
copy(src_span, dst_span);
|
||||
copy(src_span, dst_span.subspan(src_span.size()));
|
||||
|
@ -128,16 +128,22 @@ TEST(at_tests, std_span)
|
||||
std::vector<int> cvec{1, 2, 3, 4, 5};
|
||||
std::span csp{cvec};
|
||||
|
||||
for (size_t i = 0, i < vec.size(); ++i)
|
||||
for (gsl::index i = 0; i < gsl::narrow_cast<gsl::index>(vec.size()); ++i)
|
||||
{
|
||||
EXPECT_TRUE(&gsl::at(sp, i) == &vec[i]);
|
||||
EXPECT_TRUE(&gsl::at(csp, i) == &cvec[i]);
|
||||
EXPECT_TRUE(&gsl::at(sp, i) == &vec[gsl::narrow_cast<size_t>(i)]);
|
||||
EXPECT_TRUE(&gsl::at(csp, i) == &cvec[gsl::narrow_cast<size_t>(i)]);
|
||||
}
|
||||
|
||||
const auto terminateHandler = std::set_terminate([] {
|
||||
std::cerr << "Expected Death. std_span";
|
||||
std::abort();
|
||||
});
|
||||
const auto expected = GetExpectedDeathString(terminateHandler);
|
||||
|
||||
EXPECT_DEATH(gsl::at(sp, -1), expected);
|
||||
EXPECT_DEATH(gsl::at(sp, sp.size()), expected);
|
||||
EXPECT_DEATH(gsl::at(sp, gsl::narrow_cast<gsl::index>(sp.size())), expected);
|
||||
EXPECT_DEATH(gsl::at(csp, -1), expected);
|
||||
EXPECT_DEATH(gsl::at(csp, sp.size()), expected);
|
||||
EXPECT_DEATH(gsl::at(csp, gsl::narrow_cast<gsl::index>(sp.size())), expected);
|
||||
}
|
||||
#endif // __cplusplus >= 202002L
|
||||
|
||||
|
@ -55,14 +55,14 @@ void ArrayConvertibilityCheck()
|
||||
EXPECT_TRUE(sp_const_nullptr_1.data() == stl_nullptr.data());
|
||||
EXPECT_TRUE(sp_const_nullptr_1.size() == 3);
|
||||
|
||||
span<const T* const> sp_const_nullptr_2{std::as_const(stl_nullptr)};
|
||||
gsl::span<const T* const> sp_const_nullptr_2{std::as_const(stl_nullptr)};
|
||||
EXPECT_TRUE(sp_const_nullptr_2.data() == stl_nullptr.data());
|
||||
EXPECT_TRUE(sp_const_nullptr_2.size() == 3);
|
||||
|
||||
static_assert(std::is_same<decltype(span{stl_nullptr}), span<T*, 3>>::value,
|
||||
static_assert(std::is_same<decltype(gsl::span{stl_nullptr}), gsl::span<T*, 3>>::value,
|
||||
"std::is_same< decltype(span{stl_nullptr}), span<T*, 3>>::value");
|
||||
static_assert(
|
||||
std::is_same<decltype(span{std::as_const(stl_nullptr)}), span<T* const, 3>>::value,
|
||||
std::is_same<decltype(gsl::span{std::as_const(stl_nullptr)}), gsl::span<T* const, 3>>::value,
|
||||
"std::is_same< decltype(span{std::as_const(stl_nullptr)}), span<T* const, "
|
||||
"3>>::value");
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ TEST(span_ext_test, make_span_from_pointer_length_constructor)
|
||||
|
||||
{
|
||||
int* p = nullptr;
|
||||
auto s = make_span(p, narrow_cast<span<int>::size_type>(0));
|
||||
auto s = make_span(p, narrow_cast<gsl::span<int>::size_type>(0));
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
}
|
||||
@ -136,9 +136,9 @@ TEST(span_ext_test, make_span_from_std_array_constructor)
|
||||
|
||||
// This test checks for the bug found in gcc 6.1, 6.2, 6.3, 6.4, 6.5 7.1, 7.2, 7.3 - issue #590
|
||||
{
|
||||
span<int> s1 = make_span(arr);
|
||||
gsl::span<int> s1 = make_span(arr);
|
||||
|
||||
static span<int> s2;
|
||||
static gsl::span<int> s2;
|
||||
s2 = s1;
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ == 6 && (__GNUC_MINOR__ == 4 || __GNUC_MINOR__ == 5) && \
|
||||
@ -194,7 +194,7 @@ TEST(span_ext_test, make_span_from_container_constructor)
|
||||
TEST(span_test, interop_with_gsl_at)
|
||||
{
|
||||
int arr[5] = {1, 2, 3, 4, 5};
|
||||
span<int> s{arr};
|
||||
gsl::span<int> s{arr};
|
||||
EXPECT_TRUE(at(s, 0) == 1);
|
||||
EXPECT_TRUE(at(s, 1) == 2);
|
||||
}
|
||||
@ -202,7 +202,7 @@ TEST(span_test, interop_with_gsl_at)
|
||||
TEST(span_ext_test, iterator_free_functions)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
span<int> s{a};
|
||||
gsl::span<int> s{a};
|
||||
|
||||
EXPECT_TRUE((std::is_same<decltype(s.begin()), decltype(begin(s))>::value));
|
||||
EXPECT_TRUE((std::is_same<decltype(s.end()), decltype(end(s))>::value));
|
||||
@ -232,7 +232,7 @@ TEST(span_ext_test, iterator_free_functions)
|
||||
TEST(span_ext_test, ssize_free_function)
|
||||
{
|
||||
int a[] = {1, 2, 3, 4};
|
||||
span<int> s{a};
|
||||
gsl::span<int> s{a};
|
||||
|
||||
EXPECT_FALSE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
|
||||
EXPECT_TRUE(s.size() == static_cast<std::size_t>(ssize(s)));
|
||||
@ -242,8 +242,8 @@ TEST(span_ext_test, ssize_free_function)
|
||||
TEST(span_ext_test, comparison_operators)
|
||||
{
|
||||
{
|
||||
span<int> s1;
|
||||
span<int> s2;
|
||||
gsl::span<int> s1;
|
||||
gsl::span<int> s2;
|
||||
EXPECT_TRUE(s1 == s2);
|
||||
EXPECT_FALSE(s1 != s2);
|
||||
EXPECT_FALSE(s1 < s2);
|
||||
@ -260,8 +260,8 @@ TEST(span_ext_test, comparison_operators)
|
||||
|
||||
{
|
||||
int arr[] = {2, 1};
|
||||
span<int> s1 = arr;
|
||||
span<int> s2 = arr;
|
||||
gsl::span<int> s1 = arr;
|
||||
gsl::span<int> s2 = arr;
|
||||
|
||||
EXPECT_TRUE(s1 == s2);
|
||||
EXPECT_FALSE(s1 != s2);
|
||||
@ -280,8 +280,8 @@ TEST(span_ext_test, comparison_operators)
|
||||
{
|
||||
int arr[] = {2, 1}; // bigger
|
||||
|
||||
span<int> s1;
|
||||
span<int> s2 = arr;
|
||||
gsl::span<int> s1;
|
||||
gsl::span<int> s2 = arr;
|
||||
|
||||
EXPECT_TRUE(s1 != s2);
|
||||
EXPECT_TRUE(s2 != s1);
|
||||
@ -300,8 +300,8 @@ TEST(span_ext_test, comparison_operators)
|
||||
{
|
||||
int arr1[] = {1, 2};
|
||||
int arr2[] = {1, 2};
|
||||
span<int> s1 = arr1;
|
||||
span<int> s2 = arr2;
|
||||
gsl::span<int> s1 = arr1;
|
||||
gsl::span<int> s2 = arr2;
|
||||
|
||||
EXPECT_TRUE(s1 == s2);
|
||||
EXPECT_FALSE(s1 != s2);
|
||||
@ -320,8 +320,8 @@ TEST(span_ext_test, comparison_operators)
|
||||
{
|
||||
int arr[] = {1, 2, 3};
|
||||
|
||||
span<int> s1 = {&arr[0], 2}; // shorter
|
||||
span<int> s2 = arr; // longer
|
||||
gsl::span<int> s1 = {&arr[0], 2}; // shorter
|
||||
gsl::span<int> s2 = arr; // longer
|
||||
|
||||
EXPECT_TRUE(s1 != s2);
|
||||
EXPECT_TRUE(s2 != s1);
|
||||
@ -341,8 +341,8 @@ TEST(span_ext_test, comparison_operators)
|
||||
int arr1[] = {1, 2}; // smaller
|
||||
int arr2[] = {2, 1}; // bigger
|
||||
|
||||
span<int> s1 = arr1;
|
||||
span<int> s2 = arr2;
|
||||
gsl::span<int> s1 = arr1;
|
||||
gsl::span<int> s2 = arr2;
|
||||
|
||||
EXPECT_TRUE(s1 != s2);
|
||||
EXPECT_TRUE(s2 != s1);
|
||||
|
@ -43,7 +43,6 @@
|
||||
|
||||
#include "deathTestCommon.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
namespace
|
||||
@ -1085,7 +1084,7 @@ TEST(span_test, as_bytes)
|
||||
|
||||
int b[5] = {1, 2, 3, 4, 5};
|
||||
{
|
||||
span<int> sp(begin(b), static_cast<size_t>(-2));
|
||||
span<int> sp(std::begin(b), static_cast<size_t>(-2));
|
||||
EXPECT_DEATH((void) sp.size_bytes(), expected);
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,6 @@
|
||||
|
||||
#include "deathTestCommon.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
|
||||
// Generic string functions
|
||||
|
Loading…
Reference in New Issue
Block a user