diff --git a/tests/accessor.cpp b/tests/accessor.cpp new file mode 100644 index 0000000000..fc26a4d1b0 --- /dev/null +++ b/tests/accessor.cpp @@ -0,0 +1,140 @@ +#include "common.hpp" + +TEST_CASE("view.allocView.Default") +{ + auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Particle{}}; + auto view = llama::allocView(mapping, llama::bloballoc::Vector{}, llama::accessor::Default{}); + iotaFillView(view); + iotaCheckView(view); +} + +TEST_CASE("view.allocView.ByValue") +{ + auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Vec3I{}}; + auto view = llama::allocView(mapping, llama::bloballoc::Vector{}, llama::accessor::ByValue{}); + STATIC_REQUIRE(std::is_same_v); +} + +TEST_CASE("view.allocView.Const") +{ + auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Vec3I{}}; + auto view = llama::allocView(mapping, llama::bloballoc::Vector{}, llama::accessor::Const{}); + STATIC_REQUIRE(std::is_same_v); +} + +#ifdef __cpp_lib_atomic_ref +TEST_CASE("view.allocView.Atomic") +{ + auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Vec3I{}}; + auto view = llama::allocView(mapping, llama::bloballoc::Vector{}, llama::accessor::Atomic{}); + STATIC_REQUIRE(std::is_same_v>); + iotaFillView(view); + iotaCheckView(view); +} +#endif + +TEST_CASE("view.withAccessor.Default.Vector") +{ + auto view + = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Vec3I{}}, llama::bloballoc::Vector{}); + auto* addr = &view(1, 2)(tag::X{}); + auto view2 = llama::withAccessor(view); // copies + auto* addr2 = &view2(1, 2)(tag::X{}); + CHECK(addr != addr2); +} + +TEST_CASE("view.withAccessor.Default.Vector.move") +{ + auto view + = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Vec3I{}}, llama::bloballoc::Vector{}); + auto* addr = &view(1, 2)(tag::X{}); + auto view2 = llama::withAccessor(std::move(view)); + auto* addr2 = &view2(1, 2)(tag::X{}); + CHECK(addr == addr2); +} + +TEST_CASE("view.withAccessor.Default.SharedPtr") +{ + auto view + = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Vec3I{}}, llama::bloballoc::SharedPtr{}); + auto* addr = &view(1, 2)(tag::X{}); + auto view2 = llama::withAccessor(view); // copies shared pointers, but not memory chunks + auto* addr2 = &view2(1, 2)(tag::X{}); + CHECK(addr == addr2); +} + +TEMPLATE_TEST_CASE( + "view.withAccessor.shallowCopy.Default", + "", + llama::bloballoc::Vector, + llama::bloballoc::SharedPtr, + llama::bloballoc::UniquePtr) +{ + auto view = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Particle{}}, TestType{}); + auto view2 = llama::withAccessor(llama::shallowCopy(view)); + iotaFillView(view2); + iotaCheckView(view); +} + +#ifdef __cpp_lib_atomic_ref +TEMPLATE_TEST_CASE( + "view.withAccessor.shallowCopy.Atomic", + "", + llama::bloballoc::Vector, + llama::bloballoc::SharedPtr, + llama::bloballoc::UniquePtr) +{ + auto view = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Particle{}}, TestType{}); + auto view2 = llama::withAccessor(llama::shallowCopy(view)); + iotaFillView(view2); + iotaCheckView(view); +} +#endif + +TEMPLATE_TEST_CASE( + "view.withAccessor.shallowCopy.Restrict", + "", + llama::bloballoc::Vector, + llama::bloballoc::SharedPtr, + llama::bloballoc::UniquePtr) +{ + auto view = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Particle{}}, TestType{}); + auto view2 = llama::withAccessor(llama::shallowCopy(view)); + iotaFillView(view2); + iotaCheckView(view); +} + +namespace +{ + struct OffsetFloatAccessor + { + float offset; + + template + auto operator()(T& ref) -> decltype(auto) + { + if constexpr(std::is_same_v) + return ref + offset; + else + return ref; + } + }; +} // namespace + +TEST_CASE("view.withAccessor.OffsetFloatAccessor") +{ + auto view + = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{4}, Particle{}}, llama::bloballoc::SharedPtr{}); + view(0)(tag::Pos{})(tag::X{}) = 2.0; + view(0)(tag::Mass{}) = 2.0f; + + auto view2 = llama::withAccessor(view, OffsetFloatAccessor{3}); + + CHECK(view2(0)(tag::Pos{})(tag::X{}) == 2.0); + CHECK(view2(0)(tag::Mass{}) == 5.0f); + + view2.accessor().offset = 10; + + CHECK(view2(0)(tag::Pos{})(tag::X{}) == 2.0); + CHECK(view2(0)(tag::Mass{}) == 12.0f); +} diff --git a/tests/view.cpp b/tests/view.cpp index 358d0705d9..0ce350fee2 100644 --- a/tests/view.cpp +++ b/tests/view.cpp @@ -361,142 +361,3 @@ TEST_CASE("view.allocViewStack") auto v4 = llama::allocViewStack<4, Vec3I>(); v4(llama::ArrayIndex{0, 0, 0, 0})(tag::X{}) = 42; } - -TEST_CASE("view.allocView.Default") -{ - auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Particle{}}; - auto view = llama::allocView(mapping, llama::bloballoc::Vector{}, llama::accessor::Default{}); - iotaFillView(view); - iotaCheckView(view); -} - -TEST_CASE("view.allocView.ByValue") -{ - auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Vec3I{}}; - auto view = llama::allocView(mapping, llama::bloballoc::Vector{}, llama::accessor::ByValue{}); - STATIC_REQUIRE(std::is_same_v); -} - -TEST_CASE("view.allocView.Const") -{ - auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Vec3I{}}; - auto view = llama::allocView(mapping, llama::bloballoc::Vector{}, llama::accessor::Const{}); - STATIC_REQUIRE(std::is_same_v); -} - -#ifdef __cpp_lib_atomic_ref -TEST_CASE("view.allocView.Atomic") -{ - auto mapping = llama::mapping::AoS{llama::ArrayExtents{3, 4}, Vec3I{}}; - auto view = llama::allocView(mapping, llama::bloballoc::Vector{}, llama::accessor::Atomic{}); - STATIC_REQUIRE(std::is_same_v>); - iotaFillView(view); - iotaCheckView(view); -} -#endif - -TEST_CASE("view.withAccessor.Default.Vector") -{ - auto view - = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Vec3I{}}, llama::bloballoc::Vector{}); - auto* addr = &view(1, 2)(tag::X{}); - auto view2 = llama::withAccessor(view); // copies - auto* addr2 = &view2(1, 2)(tag::X{}); - CHECK(addr != addr2); -} - -TEST_CASE("view.withAccessor.Default.Vector.move") -{ - auto view - = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Vec3I{}}, llama::bloballoc::Vector{}); - auto* addr = &view(1, 2)(tag::X{}); - auto view2 = llama::withAccessor(std::move(view)); - auto* addr2 = &view2(1, 2)(tag::X{}); - CHECK(addr == addr2); -} - -TEST_CASE("view.withAccessor.Default.SharedPtr") -{ - auto view - = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Vec3I{}}, llama::bloballoc::SharedPtr{}); - auto* addr = &view(1, 2)(tag::X{}); - auto view2 = llama::withAccessor(view); // copies shared pointers, but not memory chunks - auto* addr2 = &view2(1, 2)(tag::X{}); - CHECK(addr == addr2); -} - -TEMPLATE_TEST_CASE( - "view.withAccessor.shallowCopy.Default", - "", - llama::bloballoc::Vector, - llama::bloballoc::SharedPtr, - llama::bloballoc::UniquePtr) -{ - auto view = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Particle{}}, TestType{}); - auto view2 = llama::withAccessor(llama::shallowCopy(view)); - iotaFillView(view2); - iotaCheckView(view); -} - -#ifdef __cpp_lib_atomic_ref -TEMPLATE_TEST_CASE( - "view.withAccessor.shallowCopy.Atomic", - "", - llama::bloballoc::Vector, - llama::bloballoc::SharedPtr, - llama::bloballoc::UniquePtr) -{ - auto view = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Particle{}}, TestType{}); - auto view2 = llama::withAccessor(llama::shallowCopy(view)); - iotaFillView(view2); - iotaCheckView(view); -} -#endif - -TEMPLATE_TEST_CASE( - "view.withAccessor.shallowCopy.Restrict", - "", - llama::bloballoc::Vector, - llama::bloballoc::SharedPtr, - llama::bloballoc::UniquePtr) -{ - auto view = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{16, 16}, Particle{}}, TestType{}); - auto view2 = llama::withAccessor(llama::shallowCopy(view)); - iotaFillView(view2); - iotaCheckView(view); -} - -namespace -{ - struct OffsetFloatAccessor - { - float offset; - - template - auto operator()(T& ref) -> decltype(auto) - { - if constexpr(std::is_same_v) - return ref + offset; - else - return ref; - } - }; -} // namespace - -TEST_CASE("view.withAccessor.OffsetFloatAccessor") -{ - auto view - = llama::allocView(llama::mapping::AoS{llama::ArrayExtents{4}, Particle{}}, llama::bloballoc::SharedPtr{}); - view(0)(tag::Pos{})(tag::X{}) = 2.0; - view(0)(tag::Mass{}) = 2.0f; - - auto view2 = llama::withAccessor(view, OffsetFloatAccessor{3}); - - CHECK(view2(0)(tag::Pos{})(tag::X{}) == 2.0); - CHECK(view2(0)(tag::Mass{}) == 5.0f); - - view2.accessor().offset = 10; - - CHECK(view2(0)(tag::Pos{})(tag::X{}) == 2.0); - CHECK(view2(0)(tag::Mass{}) == 12.0f); -}