Skip to content

Commit

Permalink
Extend the SoA unit test to check for out-of-range access
Browse files Browse the repository at this point in the history
  • Loading branch information
fwyzard committed May 20, 2024
1 parent d6d45f7 commit e8ae2ea
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion DataFormats/SoATemplate/test/SoAUnitTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <tuple>

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <catch.hpp>

#include "DataFormats/SoATemplate/interface/SoALayout.h"

// clang-format off
Expand All @@ -16,11 +17,16 @@ GENERATE_SOA_LAYOUT(SimpleLayoutTemplate,
using SimpleLayout = SimpleLayoutTemplate<>;

TEST_CASE("SoATemplate") {
// number of elements
const std::size_t slSize = 10;
// size in bytes
const std::size_t slBufferSize = SimpleLayout::computeDataSize(slSize);
// memory buffer aligned according to the layout requirements
std::unique_ptr<std::byte, decltype(std::free) *> slBuffer{
reinterpret_cast<std::byte *>(aligned_alloc(SimpleLayout::alignment, slBufferSize)), std::free};
// SoA layout
SimpleLayout sl{slBuffer.get(), slSize};

SECTION("Row wide copies, row") {
SimpleLayout::View slv{sl};
SimpleLayout::ConstView slcv{sl};
Expand Down Expand Up @@ -100,4 +106,33 @@ TEST_CASE("SoATemplate") {
t += tx;
}
}

SECTION("Range checking View") {
// Enable range checking
using View = SimpleLayout::ViewTemplate<cms::soa::RestrictQualify::Default, cms::soa::RangeChecking::enabled>;
View slv{sl};
int underflow = -1;
int overflow = slv.metadata().size();
// Check for under-and overflow in the row accessor
REQUIRE_THROWS_AS(slv[underflow], std::out_of_range);
REQUIRE_THROWS_AS(slv[overflow], std::out_of_range);
// Check for under-and overflow in the element accessors
REQUIRE_THROWS_AS(slv.x(underflow), std::out_of_range);
REQUIRE_THROWS_AS(slv.x(overflow), std::out_of_range);
}

SECTION("Range checking ConstView") {
// Enable range checking
using ConstView =
SimpleLayout::ConstViewTemplate<cms::soa::RestrictQualify::Default, cms::soa::RangeChecking::enabled>;
ConstView slcv{sl};
int underflow = -1;
int overflow = slcv.metadata().size();
// Check for under-and overflow in the row accessor
REQUIRE_THROWS_AS(slcv[underflow], std::out_of_range);
REQUIRE_THROWS_AS(slcv[overflow], std::out_of_range);
// Check for under-and overflow in the element accessors
REQUIRE_THROWS_AS(slcv.x(underflow), std::out_of_range);
REQUIRE_THROWS_AS(slcv.x(overflow), std::out_of_range);
}
}

0 comments on commit e8ae2ea

Please sign in to comment.