Skip to content

Commit 7f4fc93

Browse files
authoredFeb 12, 2025
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
1 parent 355982d commit 7f4fc93

File tree

1 file changed

+150
-2
lines changed

1 file changed

+150
-2
lines changed
 

‎tests/span_tests.cpp

+150-2
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ TEST(span_test, from_std_array_constructor)
412412
static_assert(!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>,
413413
"!CtorCompilesFor<span<int, 5>, std::array<int, 4>&>");
414414

415-
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
415+
#if !defined(_MSC_VER) || (_MSC_VER > 1943) || (__cplusplus >= 201703L)
416416
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
417417
static_assert(!ConversionCompilesFor<span<int>, std::array<int, 4>>,
418418
"!ConversionCompilesFor<span<int>, std::array<int, 4>>");
@@ -529,7 +529,7 @@ TEST(span_test, from_container_constructor)
529529
EXPECT_TRUE(cs.data() == cstr.data());
530530
}
531531

532-
#if !defined(_MSC_VER) || (_MSC_VER > 1942) || (__cplusplus >= 201703L)
532+
#if !defined(_MSC_VER) || (_MSC_VER > 1943) || (__cplusplus >= 201703L)
533533
// Fails on "Visual Studio 16 2019/Visual Studio 17 2022, windows-2019/2022, Debug/Release, 14".
534534
static_assert(!ConversionCompilesFor<span<int>, std::vector<int>>,
535535
"!ConversionCompilesFor<span<int>, std::vector<int>>");
@@ -1276,3 +1276,151 @@ TEST(span_test, msvc_compile_error_PR1100)
12761276
for (const auto& e : sp) { (void) e; }
12771277
}
12781278
#endif // defined(__cpp_lib_span) && defined(__cpp_lib_ranges)
1279+
1280+
TEST(span_test, empty_span)
1281+
{
1282+
span<int> s{};
1283+
EXPECT_TRUE(s.empty());
1284+
EXPECT_TRUE(s.size() == 0);
1285+
EXPECT_TRUE(s.data() == nullptr);
1286+
1287+
span<const int> cs{};
1288+
EXPECT_TRUE(cs.empty());
1289+
EXPECT_TRUE(cs.size() == 0);
1290+
EXPECT_TRUE(cs.data() == nullptr);
1291+
}
1292+
1293+
TEST(span_test, conversions)
1294+
{
1295+
int arr[5] = {1, 2, 3, 4, 5};
1296+
1297+
#if defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L)
1298+
span s = arr;
1299+
span cs = s;
1300+
#else
1301+
span<int, 5> s = arr;
1302+
span<int, 5> cs = s;
1303+
#endif
1304+
1305+
EXPECT_TRUE(cs.size() == s.size());
1306+
EXPECT_TRUE(cs.data() == s.data());
1307+
1308+
span<int, 5> fs = s;
1309+
EXPECT_TRUE(fs.size() == s.size());
1310+
EXPECT_TRUE(fs.data() == s.data());
1311+
1312+
span<const int, 5> cfs = s;
1313+
EXPECT_TRUE(cfs.size() == s.size());
1314+
EXPECT_TRUE(cfs.data() == s.data());
1315+
}
1316+
1317+
TEST(span_test, comparison_operators)
1318+
{
1319+
int arr1[3] = {1, 2, 3};
1320+
int arr2[3] = {1, 2, 3};
1321+
int arr3[3] = {4, 5, 6};
1322+
1323+
span<int> s1 = arr1;
1324+
span<int> s2 = arr2;
1325+
span<int> s3 = arr3;
1326+
1327+
EXPECT_TRUE(s1 == s2);
1328+
EXPECT_FALSE(s1 != s2);
1329+
EXPECT_FALSE(s1 == s3);
1330+
EXPECT_TRUE(s1 != s3);
1331+
EXPECT_TRUE(s1 < s3);
1332+
EXPECT_FALSE(s3 < s1);
1333+
EXPECT_TRUE(s1 <= s2);
1334+
EXPECT_TRUE(s1 <= s3);
1335+
EXPECT_FALSE(s3 <= s1);
1336+
EXPECT_TRUE(s3 > s1);
1337+
EXPECT_FALSE(s1 > s3);
1338+
EXPECT_TRUE(s3 >= s1);
1339+
EXPECT_TRUE(s1 >= s2);
1340+
EXPECT_FALSE(s1 >= s3);
1341+
}
1342+
1343+
// ...existing code...
1344+
1345+
#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002L
1346+
1347+
#include <span> // for std::span
1348+
1349+
TEST(span_test, compare_empty_span)
1350+
{
1351+
gsl::span<int> gsl_s{};
1352+
std::span<int> std_s{};
1353+
1354+
EXPECT_TRUE(gsl_s.empty());
1355+
EXPECT_TRUE(std_s.empty());
1356+
EXPECT_EQ(gsl_s.size(), std_s.size());
1357+
EXPECT_EQ(gsl_s.data(), std_s.data());
1358+
}
1359+
1360+
TEST(span_test, compare_subspan)
1361+
{
1362+
int arr[5] = {1, 2, 3, 4, 5};
1363+
gsl::span gsl_s = arr;
1364+
std::span std_s = arr;
1365+
1366+
auto gsl_sub1 = gsl_s.subspan(1);
1367+
auto std_sub1 = std_s.subspan(1);
1368+
EXPECT_EQ(gsl_sub1.size(), std_sub1.size());
1369+
EXPECT_EQ(gsl_sub1.data(), std_sub1.data());
1370+
1371+
auto gsl_sub2 = gsl_s.subspan(1, 2);
1372+
auto std_sub2 = std_s.subspan(1, 2);
1373+
EXPECT_EQ(gsl_sub2.size(), std_sub2.size());
1374+
EXPECT_EQ(gsl_sub2.data(), std_sub2.data());
1375+
}
1376+
1377+
TEST(span_test, compare_conversions)
1378+
{
1379+
int arr[5] = {1, 2, 3, 4, 5};
1380+
gsl::span gsl_s = arr;
1381+
std::span std_s = arr;
1382+
1383+
gsl::span gsl_cs = gsl_s;
1384+
std::span std_cs = std_s;
1385+
EXPECT_EQ(gsl_cs.size(), std_cs.size());
1386+
EXPECT_EQ(gsl_cs.data(), std_cs.data());
1387+
1388+
gsl::span<int, 5> gsl_fs = gsl_s;
1389+
std::span<int, 5> std_fs = std_s;
1390+
EXPECT_EQ(gsl_fs.size(), std_fs.size());
1391+
EXPECT_EQ(gsl_fs.data(), std_fs.data());
1392+
1393+
gsl::span<const int, 5> gsl_cfs = gsl_s;
1394+
std::span<const int, 5> std_cfs = std_s;
1395+
EXPECT_EQ(gsl_cfs.size(), std_cfs.size());
1396+
EXPECT_EQ(gsl_cfs.data(), std_cfs.data());
1397+
}
1398+
1399+
TEST(span_test, deduction_guides)
1400+
{
1401+
int arr[5] = {1, 2, 3, 4, 5};
1402+
std::array<int, 5> std_arr = {1, 2, 3, 4, 5};
1403+
std::vector<int> vec = {1, 2, 3, 4, 5};
1404+
1405+
// Test deduction guides for gsl::span
1406+
gsl::span gsl_s1 = arr;
1407+
gsl::span gsl_s2 = std_arr;
1408+
gsl::span gsl_s3 = vec;
1409+
1410+
// Test deduction guides for std::span (for sanity checks)
1411+
std::span std_s1 = arr;
1412+
std::span std_s2 = std_arr;
1413+
std::span std_s3 = vec;
1414+
1415+
// Compare sizes
1416+
EXPECT_EQ(gsl_s1.size(), std_s1.size());
1417+
EXPECT_EQ(gsl_s2.size(), std_s2.size());
1418+
EXPECT_EQ(gsl_s3.size(), std_s3.size());
1419+
1420+
// Compare data pointers
1421+
EXPECT_EQ(gsl_s1.data(), std_s1.data());
1422+
EXPECT_EQ(gsl_s2.data(), std_s2.data());
1423+
EXPECT_EQ(gsl_s3.data(), std_s3.data());
1424+
}
1425+
1426+
#endif // defined(__cpp_lib_span) && __cpp_lib_span >= 202002L

0 commit comments

Comments
 (0)