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
This commit is contained in:
Carson Radtke 2025-02-12 09:15:43 -06:00 committed by GitHub
parent 355982daf6
commit 7f4fc9388b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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