diff --git a/src/core/data/tiles/tile_set.hpp b/src/core/data/tiles/tile_set.hpp index d99575cc9..61e9001ba 100644 --- a/src/core/data/tiles/tile_set.hpp +++ b/src/core/data/tiles/tile_set.hpp @@ -6,9 +6,8 @@ #include "core/def.hpp" #include "core/data/ndarray/ndarray_vector.hpp" -#include #include -#include +#include #include namespace PHARE::core @@ -19,7 +18,7 @@ class TileSet public: static auto constexpr dimension = Tile::dimension; - TileSet(Box const& box, std::array const& tile_size) + TileSet(Box const& box, std::array const& tile_size) : box_{box} , tile_size_{tile_size} , shape_{[&]() { @@ -68,6 +67,12 @@ class TileSet NO_DISCARD auto& operator[](std::size_t i) { return tiles_[i]; } NO_DISCARD auto const& operator[](std::size_t i) const { return tiles_[i]; } + template + NO_DISCARD auto& at(Index... indexes) + { + return cells_(indexes...); + } + private: void consistent_tile_size_() const { @@ -132,7 +137,7 @@ class TileSet } - //! bri + //! store the pointer to the tile associated with each cell void tag_cells_() { for (auto& tile : tiles_) @@ -146,7 +151,7 @@ class TileSet Box box_; - std::array tile_size_; + std::array tile_size_; std::array shape_; std::vector tiles_; NdArrayVector cells_; diff --git a/tests/core/data/particles/test_particle_tiles.cpp b/tests/core/data/particles/test_particle_tiles.cpp index d2373f600..99032226b 100644 --- a/tests/core/data/particles/test_particle_tiles.cpp +++ b/tests/core/data/particles/test_particle_tiles.cpp @@ -28,7 +28,7 @@ TYPED_TEST(ParticleTileTest, constructs) { constexpr auto dim = TypeParam::dimension; Box box{ConstArray(0), ConstArray(50)}; - auto const tile_size = PHARE::core::ConstArray(4); + auto const tile_size = PHARE::core::ConstArray(4); TileSet particleTileSet{box, tile_size}; } diff --git a/tests/core/data/tiles/test_tile.cpp b/tests/core/data/tiles/test_tile.cpp index e50ea6f7f..8890c7209 100644 --- a/tests/core/data/tiles/test_tile.cpp +++ b/tests/core/data/tiles/test_tile.cpp @@ -15,12 +15,54 @@ using namespace PHARE::core; template -class TileTest : public ::testing::Test +class TileTestBase +{ +public: + static auto constexpr dimension = TileSet::dimension; + + TileTestBase(Box box_, std::array const& tile_size) + : box{box_} + , tileSet{box, tile_size} + { + } + + Box box; + TileSet tileSet; +}; + +template +class TileTestBoxShapeNotMultipleTileSize : public TileTestBase, public ::testing::Test { public: - TileTest() {} + TileTestBoxShapeNotMultipleTileSize() + : TileTestBase{ + Box{ConstArray(0), + ConstArray(54)}, + ConstArray(4)} + { + } +}; + +template +class TileTestBoxShapeMultipleTileSize : public TileTestBase, public ::testing::Test +{ +public: + TileTestBoxShapeMultipleTileSize() + : TileTestBase{ + Box{ConstArray(0), + ConstArray(47)}, + ConstArray(4)} + { + } +}; + + +template +class TileTest : public ::testing::Test +{ }; + template class TileMock : public Box { @@ -28,70 +70,52 @@ class TileMock : public Box using DimTiles = testing::Types>, TileSet>, TileSet>>; +TYPED_TEST_SUITE(TileTestBoxShapeNotMultipleTileSize, DimTiles); +TYPED_TEST_SUITE(TileTestBoxShapeMultipleTileSize, DimTiles); TYPED_TEST_SUITE(TileTest, DimTiles); -TYPED_TEST(TileTest, expectedNbrOfTilesPerDimToCoverTheBox) -{ - constexpr auto dim = TypeParam::dimension; - Box box{ConstArray(0), ConstArray(50)}; - auto const tile_size = PHARE::core::ConstArray(4); - TypeParam cs{box, tile_size}; - - auto const& shape = cs.shape(); - for (auto i = 0u; i < dim; ++i) - EXPECT_EQ(shape[i], 13); +TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, expectedNbrOfTilesPerDimToCoverTheBox) +{ + auto const& shape = this->tileSet.shape(); + for (auto i = 0u; i < this->dimension; ++i) + EXPECT_EQ(shape[i], 14); } -TYPED_TEST(TileTest, cluserSetSizeIsCorrect) +TYPED_TEST(TileTestBoxShapeMultipleTileSize, cluserSetSizeIsCorrect) { - constexpr auto dim = TypeParam::dimension; - Box box{ConstArray(0), ConstArray(47)}; - auto const tile_size = PHARE::core::ConstArray(4); - TypeParam cs{box, tile_size}; - - EXPECT_EQ(cs.size(), std::pow(12, dim)); + EXPECT_EQ(this->tileSet.size(), std::pow(12, this->dimension)); } -TYPED_TEST(TileTest, totalTileSetSurfaceIsEqualToBoxSurface) +TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, totalTileSetSurfaceIsEqualToBoxSurface) { - constexpr auto dim = TypeParam::dimension; - Box box{ConstArray(0), ConstArray(49)}; - auto const tile_size = PHARE::core::ConstArray(4); - TypeParam cs{box, tile_size}; - auto surface = 0.; - for (auto i = 0u; i < cs.size(); ++i) + for (auto i = 0u; i < this->tileSet.size(); ++i) { auto current_surface = 1.; - for (auto d = 0u; d < dim; ++d) + for (auto d = 0u; d < this->dimension; ++d) { - auto l = (cs[i].upper[d] - cs[i].lower[d] + 1); + auto l = (this->tileSet[i].upper[d] - this->tileSet[i].lower[d] + 1); current_surface *= l; } surface += current_surface; } - EXPECT_EQ(surface, box.size()); + EXPECT_EQ(surface, this->box.size()); } -TYPED_TEST(TileTest, tileHasNoOverlapWithOthers) +TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, tileHasNoOverlapWithOthers) { - constexpr auto dim = TypeParam::dimension; - Box box{ConstArray(0), ConstArray(54)}; - auto const tile_size = PHARE::core::ConstArray(4); - TypeParam cs{box, tile_size}; - - for (auto const& tile : cs) + for (auto const& tile : this->tileSet) { - for (auto const& other : cs) + for (auto const& other : this->tileSet) { if (&tile != &other) { - auto const box1 = Box{tile.lower, tile.upper}; - auto const box2 = Box{other.lower, other.upper}; + auto const box1 = Boxdimension>{tile.lower, tile.upper}; + auto const box2 = Boxdimension>{other.lower, other.upper}; auto overlap = box1 * box2; EXPECT_FALSE(overlap.has_value()); } @@ -100,16 +124,13 @@ TYPED_TEST(TileTest, tileHasNoOverlapWithOthers) } -TYPED_TEST(TileTest, retrieveTilesFromBoxOverlap) +TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, retrieveTilesFromBoxOverlap) { - constexpr auto dim = TypeParam::dimension; - Box box{ConstArray(0), ConstArray(54)}; - auto const tile_size = PHARE::core::ConstArray(4); - TypeParam cs{box, tile_size}; - Box selection_box{ConstArray(11), ConstArray(34)}; + Boxdimension> selection_box{ConstArraydimension>(11), + ConstArraydimension>(34)}; - auto expected_nbr = std::pow(7, dim); - auto overlapeds = cs.export_overlaped_with(selection_box); + auto expected_nbr = std::pow(7, this->dimension); + auto overlapeds = this->tileSet.export_overlaped_with(selection_box); EXPECT_EQ(overlapeds.size(), expected_nbr); auto completes = 0.; @@ -122,8 +143,8 @@ TYPED_TEST(TileTest, retrieveTilesFromBoxOverlap) else ++incompletes; } - EXPECT_EQ(completes, std::pow(5, dim)); - EXPECT_EQ(incompletes, std::pow(7, dim) - std::pow(5, dim)); + EXPECT_EQ(completes, std::pow(5, this->dimension)); + EXPECT_EQ(incompletes, std::pow(7, this->dimension) - std::pow(5, this->dimension)); } @@ -131,11 +152,26 @@ TYPED_TEST(TileTest, cannotCreateTileWithTileSizeBiggerThanBox) { constexpr auto dim = TypeParam::dimension; Box box{ConstArray(0), ConstArray(5)}; - auto const tile_size = PHARE::core::ConstArray(7); // larger than box shape + auto const tile_size = PHARE::core::ConstArray(7); // larger than box shape EXPECT_THROW(std::make_unique(box, tile_size), std::runtime_error); } +TYPED_TEST(TileTestBoxShapeNotMultipleTileSize, canRetrieveTileFromCell) +{ + auto tile = [&]() { + if constexpr (this->dimension == 1) + return this->tileSet.at(13); + else if constexpr (this->dimension == 2) + return this->tileSet.at(13, 13); + else if constexpr (this->dimension == 3) + return this->tileSet.at(13, 13, 13); + }(); + auto const expected_box = Boxdimension>{ConstArraydimension>(12), + ConstArraydimension>(15)}; + EXPECT_TRUE(*tile == expected_box); +} + int main(int argc, char** argv)