Compare commits

...

3 Commits

Author SHA1 Message Date
Carson Radtke
4742bc192a
use relative path in # include (#1194)
Some checks failed
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
Reverts commit that changes #include "assert" -> #include "gsl/assert".
This change is necessary in order to comply with CppCoreGuideline's
SF.12. Now we do #include "./assert".
2025-02-12 09:16:45 -06:00
Carson Radtke
ec729d63a7
introduce gsl::not_null<T>::element_type (#1196)
* introduce gsl::not_null<T>::element_type

* use std::is_same instead of is_same_v

* fix: cannot put a non-pointer in a gsl::not_null
2025-02-12 09:16:05 -06:00
Carson Radtke
7f4fc9388b
Add more gsl::span tests [copilot] (#1189)
* Add more gsl::span tests [copilot]

This PR adds comprehensive unit tests for `gsl::span` to ensure its correctness and consistency. The following tests have been added:

1. **Empty Span Tests**:
   - Tests to verify the behavior of an empty `gsl::span` and `gsl::span<const int>`.

2. **Conversion Tests**:
   - Tests to check the conversion between different types of `gsl::span`.

3. **Comparison Operator Tests**:
   - Tests to verify the comparison operators for `gsl::span`.

4. **Deduction Guide Tests**:
   - Tests to compare the behavior of `gsl::span` and `std::span` deduction guides for various types of arrays and containers.

These tests help ensure that `gsl::span` behaves correctly in various scenarios and is consistent with `std::span`.

This PR was created with the help of GitHub Copilot.

**Changes**:
- Added tests for empty span.
- Added tests for conversions.
- Added tests for comparison operators.
- Added tests for deduction guides.

**Testing**:
- All new tests have been added to the existing test suite.
- Run the test suite using `ctest` to ensure all tests pass.This PR adds comprehensive unit tests for `gsl::span` to ensure its correctness and consistency. The following tests have been added:

1. **Empty Span Tests**:
   - Tests to verify the behavior of an empty `gsl::span` and `gsl::span<const int>`.

2. **Conversion Tests**:
   - Tests to check the conversion between different types of `gsl::span`.

3. **Comparison Operator Tests**:
   - Tests to verify the comparison operators for `gsl::span`.

4. **Deduction Guide Tests**:
   - Tests to compare the behavior of `gsl::span` and `std::span` deduction guides for various types of arrays and containers.

These tests help ensure that `gsl::span` behaves correctly in various scenarios and is consistent with `std::span`.

This PR was created with the help of GitHub Copilot.

**Changes**:
- Added tests for empty span.
- Added tests for conversions.
- Added tests for comparison operators.
- Added tests for deduction guides.

**Testing**:
- All new tests have been added to the existing test suite.
- Run the test suite using `ctest` to ensure all tests pass.

* fix tests for pre-C++17
2025-02-12 09:15:43 -06:00
12 changed files with 194 additions and 23 deletions

View File

@ -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) 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 #### Member functions
##### Construct/Copy ##### Construct/Copy

View File

@ -17,8 +17,8 @@
#ifndef GSL_ALGORITHM_H #ifndef GSL_ALGORITHM_H
#define GSL_ALGORITHM_H #define GSL_ALGORITHM_H
#include "gsl/assert" // for Expects #include "./assert" // for Expects
#include "gsl/span" // for dynamic_extent, span #include "./span" // for dynamic_extent, span
#include <algorithm> // for copy_n #include <algorithm> // for copy_n
#include <cstddef> // for ptrdiff_t #include <cstddef> // for ptrdiff_t

View File

@ -18,16 +18,16 @@
#define GSL_GSL_H #define GSL_GSL_H
// IWYU pragma: begin_exports // IWYU pragma: begin_exports
#include "gsl/algorithm" // copy #include "./algorithm" // copy
#include "gsl/assert" // Ensures/Expects #include "./assert" // Ensures/Expects
#include "gsl/byte" // byte #include "./byte" // byte
#include "gsl/pointers" // owner, not_null #include "./pointers" // owner, not_null
#include "gsl/span" // span #include "./span" // span
#include "gsl/zstring" // zstring #include "./zstring" // zstring
#include "gsl/util" // finally()/narrow_cast()... #include "./util" // finally()/narrow_cast()...
#ifdef __cpp_exceptions #ifdef __cpp_exceptions
#include "gsl/narrow" // narrow() #include "./narrow" // narrow()
#endif #endif
// IWYU pragma: end_exports // IWYU pragma: end_exports

View File

@ -16,8 +16,8 @@
#ifndef GSL_NARROW_H #ifndef GSL_NARROW_H
#define GSL_NARROW_H #define GSL_NARROW_H
#include "gsl/assert" // for GSL_SUPPRESS #include "./assert" // for GSL_SUPPRESS
#include "gsl/util" // for narrow_cast #include "./util" // for narrow_cast
#include <exception> // for std::exception #include <exception> // for std::exception
namespace gsl namespace gsl
{ {

View File

@ -17,7 +17,7 @@
#ifndef GSL_POINTERS_H #ifndef GSL_POINTERS_H
#define 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 <cstddef> // for ptrdiff_t, nullptr_t, size_t
#include <functional> // for less, greater #include <functional> // for less, greater
@ -98,6 +98,8 @@ class not_null
public: public:
static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr."); 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>> 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)) constexpr not_null(U&& u) noexcept(std::is_nothrow_move_constructible<T>::value) : ptr_(std::forward<U>(u))
{ {

View File

@ -17,10 +17,10 @@
#ifndef GSL_SPAN_H #ifndef GSL_SPAN_H
#define GSL_SPAN_H #define GSL_SPAN_H
#include "gsl/assert" // for Expects #include "./assert" // for Expects
#include "gsl/byte" // for byte #include "./byte" // for byte
#include "gsl/span_ext" // for span specialization of gsl::at and other span-related extensions #include "./span_ext" // for span specialization of gsl::at and other span-related extensions
#include "gsl/util" // for narrow_cast #include "./util" // for narrow_cast
#include <array> // for array #include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t #include <cstddef> // for ptrdiff_t, size_t, nullptr_t

View File

@ -27,8 +27,8 @@
// //
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#include "gsl/assert" // GSL_KERNEL_MODE #include "./assert" // GSL_KERNEL_MODE
#include "gsl/util" // for narrow_cast, narrow #include "./util" // for narrow_cast, narrow
#include <cstddef> // for ptrdiff_t, size_t #include <cstddef> // for ptrdiff_t, size_t
#include <utility> #include <utility>

View File

@ -17,7 +17,7 @@
#ifndef GSL_UTIL_H #ifndef GSL_UTIL_H
#define GSL_UTIL_H #define GSL_UTIL_H
#include "gsl/assert" // for Expects #include "./assert" // for Expects
#include <array> #include <array>
#include <cstddef> // for ptrdiff_t, size_t #include <cstddef> // for ptrdiff_t, size_t

View File

@ -17,7 +17,7 @@
#ifndef GSL_ZSTRING_H #ifndef GSL_ZSTRING_H
#define 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 #include <cstddef> // for size_t, nullptr_t

View File

@ -88,4 +88,10 @@ TEST(pointers_test, swap)
"!SwapCompilesFor<NotMoveAssignableCustomPtr>"); "!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 } // namespace

View File

@ -412,7 +412,7 @@ TEST(span_test, from_std_array_constructor)
static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>, static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>,
"!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". // 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>>, static_assert(!ConversionCompilesFor<span<int>, std::array<int, 4>>,
"!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()); 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". // Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
static_assert(!ConversionCompilesFor<span<int>, std::vector<int>>, static_assert(!ConversionCompilesFor<span<int>, std::vector<int>>,
"!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; } for (const auto& e : sp) { (void) e; }
} }
#endif // defined(__cpp_lib_span) && defined(__cpp_lib_ranges) #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

View File

@ -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) #if defined(__cplusplus) && (__cplusplus >= 201703L)
TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction) TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)