Skip to content

Commit 60d7912

Browse files
authored
Merge pull request #2255 from pbarone-latai/iox-2253-fix-span-assert
iox-2253 Fix span assert
2 parents 0cf06f2 + 91bdd46 commit 60d7912

File tree

4 files changed

+205
-9
lines changed

4 files changed

+205
-9
lines changed

doc/website/release-notes/iceoryx-unreleased.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
- Fix `const` value assignment in `iox::optional` [\#2224](https://github.com/eclipse-iceoryx/iceoryx/issues/2224)
129129
- Generated files cause recompilation even without any changes [#2210](https://github.com/eclipse-iceoryx/iceoryx/issues/2210)
130130
- Fix span_iterator constructor to prevent assertion when iterating over spans [#2216](https://github.com/eclipse-iceoryx/iceoryx/issues/2216)
131+
- Fix span span constructor assert when using begin/end constructor [#2253](https://github.com/eclipse-iceoryx/iceoryx/issues/2253)
131132
- Listener examples need to take all samples in the callback [#2251](https://github.com/eclipse-iceoryx/iceoryx/issues/2251)
132133

133134
**Refactoring:**

iceoryx_hoofs/test/moduletests/test_vocabulary_span.cpp

Lines changed: 198 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ TEST(span_test, NewEmptySpanCreatedFromIteratorContainsSameData)
3737
EXPECT_EQ(nullptr, empty_sut.data());
3838
}
3939

40-
TEST(span_test, NewDynSpanCreatedFromIteratorContainsSameData)
40+
TEST(span_test, NewDynSpanCreatedFromIteratorAndSizeContainsSameData)
4141
{
4242
::testing::Test::RecordProperty("TEST_ID", "05db30c2-e13d-4116-ba05-668b30ba4a23");
4343
const std::vector<int32_t> expected_data = {1, 1, 2, 3, 5, 8};
@@ -53,7 +53,7 @@ TEST(span_test, NewDynSpanCreatedFromIteratorContainsSameData)
5353
}
5454
}
5555

56-
TEST(span_test, NewStaticSpanCreatedFromIteratorContainsSameData)
56+
TEST(span_test, NewStaticSpanCreatedFromIteratorAndSizeContainsSameData)
5757
{
5858
::testing::Test::RecordProperty("TEST_ID", "fdc6a3fe-3971-4326-b6b2-1967afbc9726");
5959
std::vector<int32_t> vector = {8, 2, 2, 4, 5, 8};
@@ -69,6 +69,38 @@ TEST(span_test, NewStaticSpanCreatedFromIteratorContainsSameData)
6969
}
7070
}
7171

72+
TEST(span_test, NewDynSpanCreatedFromIteratorsContainsSameData)
73+
{
74+
::testing::Test::RecordProperty("TEST_ID", "6f7224c9-b500-43f1-acb7-b64c5d407fce");
75+
const std::vector<int32_t> expected_data = {1, 1, 2, 3, 5, 8};
76+
std::vector<int32_t> vector = expected_data;
77+
78+
span<int32_t> dyn_sut(vector.begin(), vector.end());
79+
80+
EXPECT_EQ(vector.data(), dyn_sut.data());
81+
EXPECT_EQ(vector.size(), dyn_sut.size());
82+
for (size_t i = 0; i < dyn_sut.size(); ++i)
83+
{
84+
EXPECT_EQ(vector[i], dyn_sut[i]);
85+
}
86+
}
87+
88+
TEST(span_test, NewStaticSpanCreatedFromIteratorsContainsSameData)
89+
{
90+
::testing::Test::RecordProperty("TEST_ID", "ff3f731e-9fa2-4584-a6a4-090ba5aad5f4");
91+
std::vector<int32_t> vector = {8, 2, 2, 4, 5, 8};
92+
93+
span<int32_t, 6> static_sut(vector.begin(), vector.end());
94+
95+
EXPECT_EQ(vector.data(), static_sut.data());
96+
EXPECT_EQ(vector.size(), static_sut.size());
97+
98+
for (size_t i = 0; i < static_sut.size(); ++i)
99+
{
100+
EXPECT_EQ(vector[i], static_sut[i]);
101+
}
102+
}
103+
72104
TEST(span_test, NewConstSpanCreatedFromContainerContainsSameData)
73105
{
74106
::testing::Test::RecordProperty("TEST_ID", "593aa3b6-9937-469d-991d-9e682110727e");
@@ -324,12 +356,64 @@ TEST(span_test, NewStaticSpanFromConstIoxUninitializedArrayContainsSameData)
324356
}
325357
}
326358

359+
TEST(span_test, NewStaticSpanCopyConstructed)
360+
{
361+
::testing::Test::RecordProperty("TEST_ID", "88da307d-ed51-42a0-a587-784f29be7905");
362+
static constexpr std::array<int32_t, 5> arr = {1, 6, 2, 8, 0};
363+
const span<const int32_t, 5> original_span(arr.begin(), arr.size());
364+
span<const int32_t, 5> new_static_span(original_span);
365+
EXPECT_EQ(arr.data(), new_static_span.data());
366+
EXPECT_EQ(arr.size(), new_static_span.size());
367+
}
368+
369+
TEST(span_test, NewDynamicSpanCopyConstructed)
370+
{
371+
::testing::Test::RecordProperty("TEST_ID", "9c9e897b-2755-44f9-9075-dc224d0e72ac");
372+
static constexpr std::array<int32_t, 5> arr = {1, 6, 2, 8, 0};
373+
const span<const int32_t> original_span(arr);
374+
span<const int32_t> new_static_span(original_span);
375+
EXPECT_EQ(arr.data(), new_static_span.data());
376+
EXPECT_EQ(arr.size(), new_static_span.size());
377+
}
378+
379+
TEST(span_test, NewStaticSpanMoveConstructed)
380+
{
381+
::testing::Test::RecordProperty("TEST_ID", "9e8c6cea-e005-41c2-9bc3-ebfb968b3674");
382+
static constexpr std::array<int32_t, 5> arr = {1, 6, 2, 8, 0};
383+
span<const int32_t, 5> original_span(arr.begin(), arr.size());
384+
span<const int32_t, 5> new_static_span(std::move(original_span));
385+
EXPECT_EQ(arr.data(), new_static_span.data());
386+
EXPECT_EQ(arr.size(), new_static_span.size());
387+
}
388+
389+
TEST(span_test, NewDynamicSpanMoveConstructed)
390+
{
391+
::testing::Test::RecordProperty("TEST_ID", "e58c41f5-4ea8-40e9-8131-0f8e7a93644c");
392+
static constexpr std::array<int32_t, 5> arr = {1, 6, 2, 8, 0};
393+
span<const int32_t> original_span(arr);
394+
span<const int32_t> new_static_span(std::move(original_span));
395+
EXPECT_EQ(arr.data(), new_static_span.data());
396+
EXPECT_EQ(arr.size(), new_static_span.size());
397+
}
398+
327399
TEST(span_test, CheckFrontOfSpanIfItReturnsTheElementAtIndex0)
328400
{
329401
::testing::Test::RecordProperty("TEST_ID", "57b2f67f-79c1-4c1e-a305-f4665283c474");
330402
static constexpr std::array<int32_t, 5> arr = {1, 6, 2, 8, 0};
331403
constexpr span<const int32_t> span(arr);
332404
static_assert(arr.data() == &span.front(), "span.front() does not refer to the same element as arr[0]");
405+
// Also check at runtime to show in coverage reports
406+
EXPECT_EQ(arr[0], span.front());
407+
}
408+
409+
TEST(span_test, CheckBackOfSpanIfItReturnsTheElementAtLastIndex)
410+
{
411+
::testing::Test::RecordProperty("TEST_ID", "2b9fa3d2-e57b-4c17-b8ef-541de8b3f9f9");
412+
static constexpr std::array<int32_t, 5> arr = {1, 6, 2, 8, 0};
413+
constexpr span<const int32_t> span(arr);
414+
static_assert(arr.data() + 4 == &span.back(), "span.back() does not refer to the same element as arr[N-1]");
415+
// Also check at runtime to show in coverage reports
416+
EXPECT_EQ(arr[4], span.back());
333417
}
334418

335419
TEST(span_test, CheckIterOfSpan)
@@ -397,4 +481,116 @@ TEST(span_test, IterateOverSpan)
397481
EXPECT_EQ(sum, 31);
398482
}
399483

484+
TEST(span_test, IterateOverSpanInReverse)
485+
{
486+
::testing::Test::RecordProperty("TEST_ID", "2994f138-41ec-4a51-8266-c4c461454411");
487+
std::vector<int32_t> vector = {1, 1, 13, 3, 5, 8};
488+
span<int32_t, 6> static_sut(vector.data(), vector.size());
489+
490+
// Sum the values in the span as a simple test
491+
int32_t sum{0U};
492+
for (auto it = static_sut.rbegin(); it != static_sut.rend(); ++it)
493+
{
494+
sum += *it;
495+
}
496+
EXPECT_EQ(sum, 31);
497+
}
498+
499+
TEST(span_test, CreateStaticSubspan)
500+
{
501+
::testing::Test::RecordProperty("TEST_ID", "bd1983a4-3e73-4f1f-8bca-7613fa2a0b43");
502+
constexpr std::array<int32_t, 6> array = {1, 1, 13, 3, 5, 8};
503+
const span<const int32_t, 6> static_sut(array.begin(), array.end());
504+
505+
// Create subspan
506+
const span<const int32_t, 3> subspan = static_sut.subspan<2U, 3U>();
507+
508+
EXPECT_EQ(subspan.size(), 3);
509+
EXPECT_EQ(subspan.data(), &array[2]);
510+
EXPECT_EQ(subspan[0], 13);
511+
EXPECT_EQ(subspan[1], 3);
512+
EXPECT_EQ(subspan[2], 5);
513+
}
514+
515+
TEST(span_test, CreateDynSubspan)
516+
{
517+
::testing::Test::RecordProperty("TEST_ID", "45595686-ed6e-47e1-9523-7312052187ec");
518+
constexpr std::array<int32_t, 6> array = {1, 1, 13, 3, 5, 8};
519+
const span<const int32_t, 6> static_sut(array.begin(), array.end());
520+
521+
// Create subspan
522+
const span<const int32_t> subspan = static_sut.subspan(1U, 2U);
523+
524+
EXPECT_EQ(subspan.size(), 2);
525+
EXPECT_EQ(subspan.data(), &array[1]);
526+
EXPECT_EQ(subspan[0], 1);
527+
EXPECT_EQ(subspan[1], 13);
528+
}
529+
530+
TEST(span_test, CreateStaticSubspanFirstN)
531+
{
532+
::testing::Test::RecordProperty("TEST_ID", "4a0d421c-f308-4ef8-a3b1-dd795e8920b0");
533+
constexpr std::array<int32_t, 6> array = {1, 1, 13, 3, 5, 8};
534+
const span<const int32_t, 6> static_sut(array.begin(), array.end());
535+
536+
// Create subspan
537+
const span<const int32_t, 3> subspan = static_sut.first<3U>();
538+
539+
EXPECT_EQ(subspan.size(), 3);
540+
EXPECT_EQ(subspan.data(), array.data());
541+
EXPECT_EQ(subspan[0], 1);
542+
EXPECT_EQ(subspan[1], 1);
543+
EXPECT_EQ(subspan[2], 13);
544+
}
545+
546+
TEST(span_test, CreateDynSubspanFirstN)
547+
{
548+
::testing::Test::RecordProperty("TEST_ID", "bc1df89d-e727-42a2-9d1f-20055883e605");
549+
constexpr std::array<int32_t, 6> array = {1, 1, 13, 3, 5, 8};
550+
const span<const int32_t, 6> static_sut(array.begin(), array.end());
551+
552+
// Create subspan
553+
const span<const int32_t> subspan = static_sut.first(3U);
554+
555+
EXPECT_EQ(subspan.size(), 3);
556+
EXPECT_EQ(subspan.data(), array.data());
557+
EXPECT_EQ(subspan[0], 1);
558+
EXPECT_EQ(subspan[1], 1);
559+
EXPECT_EQ(subspan[2], 13);
560+
}
561+
562+
TEST(span_test, CreateStaticSubspanLastN)
563+
{
564+
::testing::Test::RecordProperty("TEST_ID", "e1ae58ef-e4c5-4ea9-88b0-af0701f5cebe");
565+
constexpr std::array<int32_t, 6> array = {1, 1, 13, 3, 5, 8};
566+
const span<const int32_t, 6> static_sut(array.begin(), array.end());
567+
568+
// Create subspan
569+
const span<const int32_t, 4> subspan = static_sut.last<4U>();
570+
571+
EXPECT_EQ(subspan.size(), 4);
572+
EXPECT_EQ(subspan.data(), &array[2]);
573+
EXPECT_EQ(subspan[0], 13);
574+
EXPECT_EQ(subspan[1], 3);
575+
EXPECT_EQ(subspan[2], 5);
576+
EXPECT_EQ(subspan[3], 8);
577+
}
578+
579+
TEST(span_test, CreateDynSubspanLastN)
580+
{
581+
::testing::Test::RecordProperty("TEST_ID", "4948e802-3134-45f7-89fa-3d51bfe0e3eb");
582+
constexpr std::array<int32_t, 6> array = {1, 1, 13, 3, 5, 8};
583+
const span<const int32_t, 6> static_sut(array.begin(), array.end());
584+
585+
// Create subspan
586+
const span<const int32_t> subspan = static_sut.last(4U);
587+
588+
EXPECT_EQ(subspan.size(), 4);
589+
EXPECT_EQ(subspan.data(), &array[2]);
590+
EXPECT_EQ(subspan[0], 13);
591+
EXPECT_EQ(subspan[1], 3);
592+
EXPECT_EQ(subspan[2], 5);
593+
EXPECT_EQ(subspan[3], 8);
594+
}
595+
400596
} // namespace

iceoryx_hoofs/vocabulary/include/iox/detail/span.inl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ inline constexpr span<T, Extent>::span(It first, uint64_t count) noexcept
5656
}
5757

5858
template <typename T, uint64_t Extent>
59-
template <typename It, typename End, typename>
60-
inline constexpr span<T, Extent>::span(It begin, End end) noexcept
59+
template <typename It, typename>
60+
inline constexpr span<T, Extent>::span(It begin, It end) noexcept
6161
: span(begin, static_cast<uint64_t>(end - begin))
6262
{
63-
assert(begin == nullptr || end == nullptr || begin <= end);
63+
assert(begin <= end);
6464
}
6565

6666
template <typename T, uint64_t Extent>

iceoryx_hoofs/vocabulary/include/iox/span.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,11 @@ class span final : public detail::span_storage<Extent>
243243
constexpr span(It first, uint64_t count) noexcept;
244244

245245
/// @brief Constructs a span that is a view over the range [first, last);
246-
/// @tparam It Type of the start iterator
247-
/// @tparam End Type of the end iterator
246+
/// @tparam It Type of the start/end iterators
248247
/// @param begin iterator of where to start creating the span
249248
/// @param end iterator of where to stop
250-
template <typename It, typename End, typename = std::enable_if_t<!std::is_convertible<End, uint64_t>::value>>
251-
constexpr span(It begin, End end) noexcept;
249+
template <typename It, typename = std::enable_if_t<!std::is_convertible<It, uint64_t>::value>>
250+
constexpr span(It begin, It end) noexcept;
252251

253252
/// @brief Constructs a span that is a view over the array arr; the resulting span has size() == N and data() ==
254253
/// std::data(arr)

0 commit comments

Comments
 (0)