mirror of
https://github.com/microsoft/GSL.git
synced 2025-04-23 17:45:31 -04:00
Compare commits
3 Commits
355982daf6
...
4742bc192a
Author | SHA1 | Date | |
---|---|---|---|
|
4742bc192a | ||
|
ec729d63a7 | ||
|
7f4fc9388b |
@ -224,6 +224,14 @@ When a nullptr check fails, `std::terminate` is called.
|
||||
|
||||
See [F.23: Use a `not_null<T>` to indicate that “null” is not a valid value](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-nullptr)
|
||||
|
||||
#### Member Types
|
||||
|
||||
```cpp
|
||||
using element_type = T;
|
||||
```
|
||||
|
||||
The type of the pointed-to object.
|
||||
|
||||
#### Member functions
|
||||
|
||||
##### Construct/Copy
|
||||
|
@ -17,8 +17,8 @@
|
||||
#ifndef GSL_ALGORITHM_H
|
||||
#define GSL_ALGORITHM_H
|
||||
|
||||
#include "gsl/assert" // for Expects
|
||||
#include "gsl/span" // for dynamic_extent, span
|
||||
#include "./assert" // for Expects
|
||||
#include "./span" // for dynamic_extent, span
|
||||
|
||||
#include <algorithm> // for copy_n
|
||||
#include <cstddef> // for ptrdiff_t
|
||||
|
@ -18,16 +18,16 @@
|
||||
#define GSL_GSL_H
|
||||
|
||||
// IWYU pragma: begin_exports
|
||||
#include "gsl/algorithm" // copy
|
||||
#include "gsl/assert" // Ensures/Expects
|
||||
#include "gsl/byte" // byte
|
||||
#include "gsl/pointers" // owner, not_null
|
||||
#include "gsl/span" // span
|
||||
#include "gsl/zstring" // zstring
|
||||
#include "gsl/util" // finally()/narrow_cast()...
|
||||
#include "./algorithm" // copy
|
||||
#include "./assert" // Ensures/Expects
|
||||
#include "./byte" // byte
|
||||
#include "./pointers" // owner, not_null
|
||||
#include "./span" // span
|
||||
#include "./zstring" // zstring
|
||||
#include "./util" // finally()/narrow_cast()...
|
||||
|
||||
#ifdef __cpp_exceptions
|
||||
#include "gsl/narrow" // narrow()
|
||||
#include "./narrow" // narrow()
|
||||
#endif
|
||||
// IWYU pragma: end_exports
|
||||
|
||||
|
@ -16,8 +16,8 @@
|
||||
|
||||
#ifndef GSL_NARROW_H
|
||||
#define GSL_NARROW_H
|
||||
#include "gsl/assert" // for GSL_SUPPRESS
|
||||
#include "gsl/util" // for narrow_cast
|
||||
#include "./assert" // for GSL_SUPPRESS
|
||||
#include "./util" // for narrow_cast
|
||||
#include <exception> // for std::exception
|
||||
namespace gsl
|
||||
{
|
||||
|
@ -17,7 +17,7 @@
|
||||
#ifndef GSL_POINTERS_H
|
||||
#define GSL_POINTERS_H
|
||||
|
||||
#include "gsl/assert" // for Ensures, Expects
|
||||
#include "./assert" // for Ensures, Expects
|
||||
|
||||
#include <cstddef> // for ptrdiff_t, nullptr_t, size_t
|
||||
#include <functional> // for less, greater
|
||||
@ -98,6 +98,8 @@ class not_null
|
||||
public:
|
||||
static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");
|
||||
|
||||
using element_type = T;
|
||||
|
||||
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : ptr_(std::forward<U>(u))
|
||||
{
|
||||
|
@ -17,10 +17,10 @@
|
||||
#ifndef GSL_SPAN_H
|
||||
#define GSL_SPAN_H
|
||||
|
||||
#include "gsl/assert" // for Expects
|
||||
#include "gsl/byte" // for byte
|
||||
#include "gsl/span_ext" // for span specialization of gsl::at and other span-related extensions
|
||||
#include "gsl/util" // for narrow_cast
|
||||
#include "./assert" // for Expects
|
||||
#include "./byte" // for byte
|
||||
#include "./span_ext" // for span specialization of gsl::at and other span-related extensions
|
||||
#include "./util" // for narrow_cast
|
||||
|
||||
#include <array> // for array
|
||||
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
|
||||
|
@ -27,8 +27,8 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "gsl/assert" // GSL_KERNEL_MODE
|
||||
#include "gsl/util" // for narrow_cast, narrow
|
||||
#include "./assert" // GSL_KERNEL_MODE
|
||||
#include "./util" // for narrow_cast, narrow
|
||||
|
||||
#include <cstddef> // for ptrdiff_t, size_t
|
||||
#include <utility>
|
||||
|
@ -17,7 +17,7 @@
|
||||
#ifndef GSL_UTIL_H
|
||||
#define GSL_UTIL_H
|
||||
|
||||
#include "gsl/assert" // for Expects
|
||||
#include "./assert" // for Expects
|
||||
|
||||
#include <array>
|
||||
#include <cstddef> // for ptrdiff_t, size_t
|
||||
|
@ -17,7 +17,7 @@
|
||||
#ifndef GSL_ZSTRING_H
|
||||
#define GSL_ZSTRING_H
|
||||
|
||||
#include "gsl/span_ext" // for dynamic_extent
|
||||
#include "./span_ext" // for dynamic_extent
|
||||
|
||||
#include <cstddef> // for size_t, nullptr_t
|
||||
|
||||
|
@ -88,4 +88,10 @@ TEST(pointers_test, swap)
|
||||
"!SwapCompilesFor<NotMoveAssignableCustomPtr>");
|
||||
}
|
||||
|
||||
TEST(pointers_test, member_types)
|
||||
{
|
||||
static_assert(std::is_same<gsl::not_null<int*>::element_type, int*>::value,
|
||||
"check member type: element_type");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
@ -412,7 +412,7 @@ TEST(span_test, from_std_array_constructor)
|
||||
static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>,
|
||||
"!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>");
|
||||
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1943) || (__cplusplus >= 201703L)
|
||||
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
|
||||
static_assert(!ConversionCompilesFor<span<int>, std::array<int, 4>>,
|
||||
"!ConversionCompilesFor<span<int>, std::array<int, 4>>");
|
||||
@ -529,7 +529,7 @@ TEST(span_test, from_container_constructor)
|
||||
EXPECT_TRUE(cs.data() == cstr.data());
|
||||
}
|
||||
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
|
||||
#if !defined(_MSC_VER) || (_MSC_VER > 1943) || (__cplusplus >= 201703L)
|
||||
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
|
||||
static_assert(!ConversionCompilesFor<span<int>, std::vector<int>>,
|
||||
"!ConversionCompilesFor<span<int>, std::vector<int>>");
|
||||
@ -1276,3 +1276,151 @@ TEST(span_test, msvc_compile_error_PR1100)
|
||||
for (const auto& e : sp) { (void) e; }
|
||||
}
|
||||
#endif // defined(__cpp_lib_span) && defined(__cpp_lib_ranges)
|
||||
|
||||
TEST(span_test, empty_span)
|
||||
{
|
||||
span<int> s{};
|
||||
EXPECT_TRUE(s.empty());
|
||||
EXPECT_TRUE(s.size() == 0);
|
||||
EXPECT_TRUE(s.data() == nullptr);
|
||||
|
||||
span<const int> cs{};
|
||||
EXPECT_TRUE(cs.empty());
|
||||
EXPECT_TRUE(cs.size() == 0);
|
||||
EXPECT_TRUE(cs.data() == nullptr);
|
||||
}
|
||||
|
||||
TEST(span_test, conversions)
|
||||
{
|
||||
int arr[5] = {1, 2, 3, 4, 5};
|
||||
|
||||
#if defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L)
|
||||
span s = arr;
|
||||
span cs = s;
|
||||
#else
|
||||
span<int, 5> s = arr;
|
||||
span<int, 5> cs = s;
|
||||
#endif
|
||||
|
||||
EXPECT_TRUE(cs.size() == s.size());
|
||||
EXPECT_TRUE(cs.data() == s.data());
|
||||
|
||||
span<int, 5> fs = s;
|
||||
EXPECT_TRUE(fs.size() == s.size());
|
||||
EXPECT_TRUE(fs.data() == s.data());
|
||||
|
||||
span<const int, 5> cfs = s;
|
||||
EXPECT_TRUE(cfs.size() == s.size());
|
||||
EXPECT_TRUE(cfs.data() == s.data());
|
||||
}
|
||||
|
||||
TEST(span_test, comparison_operators)
|
||||
{
|
||||
int arr1[3] = {1, 2, 3};
|
||||
int arr2[3] = {1, 2, 3};
|
||||
int arr3[3] = {4, 5, 6};
|
||||
|
||||
span<int> s1 = arr1;
|
||||
span<int> s2 = arr2;
|
||||
span<int> s3 = arr3;
|
||||
|
||||
EXPECT_TRUE(s1 == s2);
|
||||
EXPECT_FALSE(s1 != s2);
|
||||
EXPECT_FALSE(s1 == s3);
|
||||
EXPECT_TRUE(s1 != s3);
|
||||
EXPECT_TRUE(s1 < s3);
|
||||
EXPECT_FALSE(s3 < s1);
|
||||
EXPECT_TRUE(s1 <= s2);
|
||||
EXPECT_TRUE(s1 <= s3);
|
||||
EXPECT_FALSE(s3 <= s1);
|
||||
EXPECT_TRUE(s3 > s1);
|
||||
EXPECT_FALSE(s1 > s3);
|
||||
EXPECT_TRUE(s3 >= s1);
|
||||
EXPECT_TRUE(s1 >= s2);
|
||||
EXPECT_FALSE(s1 >= s3);
|
||||
}
|
||||
|
||||
// ...existing code...
|
||||
|
||||
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
|
||||
|
||||
#include <span> // for std::span
|
||||
|
||||
TEST(span_test, compare_empty_span)
|
||||
{
|
||||
gsl::span<int> gsl_s{};
|
||||
std::span<int> std_s{};
|
||||
|
||||
EXPECT_TRUE(gsl_s.empty());
|
||||
EXPECT_TRUE(std_s.empty());
|
||||
EXPECT_EQ(gsl_s.size(), std_s.size());
|
||||
EXPECT_EQ(gsl_s.data(), std_s.data());
|
||||
}
|
||||
|
||||
TEST(span_test, compare_subspan)
|
||||
{
|
||||
int arr[5] = {1, 2, 3, 4, 5};
|
||||
gsl::span gsl_s = arr;
|
||||
std::span std_s = arr;
|
||||
|
||||
auto gsl_sub1 = gsl_s.subspan(1);
|
||||
auto std_sub1 = std_s.subspan(1);
|
||||
EXPECT_EQ(gsl_sub1.size(), std_sub1.size());
|
||||
EXPECT_EQ(gsl_sub1.data(), std_sub1.data());
|
||||
|
||||
auto gsl_sub2 = gsl_s.subspan(1, 2);
|
||||
auto std_sub2 = std_s.subspan(1, 2);
|
||||
EXPECT_EQ(gsl_sub2.size(), std_sub2.size());
|
||||
EXPECT_EQ(gsl_sub2.data(), std_sub2.data());
|
||||
}
|
||||
|
||||
TEST(span_test, compare_conversions)
|
||||
{
|
||||
int arr[5] = {1, 2, 3, 4, 5};
|
||||
gsl::span gsl_s = arr;
|
||||
std::span std_s = arr;
|
||||
|
||||
gsl::span gsl_cs = gsl_s;
|
||||
std::span std_cs = std_s;
|
||||
EXPECT_EQ(gsl_cs.size(), std_cs.size());
|
||||
EXPECT_EQ(gsl_cs.data(), std_cs.data());
|
||||
|
||||
gsl::span<int, 5> gsl_fs = gsl_s;
|
||||
std::span<int, 5> std_fs = std_s;
|
||||
EXPECT_EQ(gsl_fs.size(), std_fs.size());
|
||||
EXPECT_EQ(gsl_fs.data(), std_fs.data());
|
||||
|
||||
gsl::span<const int, 5> gsl_cfs = gsl_s;
|
||||
std::span<const int, 5> std_cfs = std_s;
|
||||
EXPECT_EQ(gsl_cfs.size(), std_cfs.size());
|
||||
EXPECT_EQ(gsl_cfs.data(), std_cfs.data());
|
||||
}
|
||||
|
||||
TEST(span_test, deduction_guides)
|
||||
{
|
||||
int arr[5] = {1, 2, 3, 4, 5};
|
||||
std::array<int, 5> std_arr = {1, 2, 3, 4, 5};
|
||||
std::vector<int> vec = {1, 2, 3, 4, 5};
|
||||
|
||||
// Test deduction guides for gsl::span
|
||||
gsl::span gsl_s1 = arr;
|
||||
gsl::span gsl_s2 = std_arr;
|
||||
gsl::span gsl_s3 = vec;
|
||||
|
||||
// Test deduction guides for std::span (for sanity checks)
|
||||
std::span std_s1 = arr;
|
||||
std::span std_s2 = std_arr;
|
||||
std::span std_s3 = vec;
|
||||
|
||||
// Compare sizes
|
||||
EXPECT_EQ(gsl_s1.size(), std_s1.size());
|
||||
EXPECT_EQ(gsl_s2.size(), std_s2.size());
|
||||
EXPECT_EQ(gsl_s3.size(), std_s3.size());
|
||||
|
||||
// Compare data pointers
|
||||
EXPECT_EQ(gsl_s1.data(), std_s1.data());
|
||||
EXPECT_EQ(gsl_s2.data(), std_s2.data());
|
||||
EXPECT_EQ(gsl_s3.data(), std_s3.data());
|
||||
}
|
||||
|
||||
#endif // defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
|
||||
|
@ -366,6 +366,13 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(pointers_test, member_types)
|
||||
{
|
||||
// make sure `element_type` is inherited from `gsl::not_null`
|
||||
static_assert(std::is_same<gsl::strict_not_null<int*>::element_type, int*>::value,
|
||||
"check member type: element_type");
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
||||
|
||||
TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
|
||||
|
Loading…
x
Reference in New Issue
Block a user