From 2eeb7552d656857054161d476dc9e5434b136c4c Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 17 Dec 2020 13:04:25 +0100 Subject: [PATCH] test: * more on any const and non-const reference support * poly support for const and non-const references * poly as_ref overloads --- src/entt/poly/poly.hpp | 2 +- test/entt/core/any.cpp | 21 ++++++++++- test/entt/poly/poly_deduced.cpp | 63 ++++++++++++++++++++++++++++++-- test/entt/poly/poly_defined.cpp | 65 ++++++++++++++++++++++++++++++--- 4 files changed, 140 insertions(+), 11 deletions(-) diff --git a/src/entt/poly/poly.hpp b/src/entt/poly/poly.hpp index 82b03fbe3..8a332eafd 100644 --- a/src/entt/poly/poly.hpp +++ b/src/entt/poly/poly.hpp @@ -263,7 +263,7 @@ public: /*! @copydoc data */ [[nodiscard]] void * data() ENTT_NOEXCEPT { - return const_cast(std::as_const(*this).data()); + return storage.data(); } /** diff --git a/test/entt/core/any.cpp b/test/entt/core/any.cpp index bc1a94dfb..9c6803c8c 100644 --- a/test/entt/core/any.cpp +++ b/test/entt/core/any.cpp @@ -661,17 +661,24 @@ TEST(Any, AsRef) { ASSERT_EQ(entt::any_cast(any), 42); ASSERT_EQ(entt::any_cast(ref), 42); + ASSERT_EQ(entt::any_cast(cref), 42); + + ASSERT_EQ(entt::any_cast(any), 42); + ASSERT_EQ(entt::any_cast(ref), 42); ASSERT_EQ(entt::any_cast(cref), 42); ASSERT_EQ(entt::any_cast(any), 42); + ASSERT_EQ(entt::any_cast(any), 42); ASSERT_EQ(entt::any_cast(ref), 42); + ASSERT_EQ(entt::any_cast(ref), 42); + ASSERT_DEATH(entt::any_cast(cref), ".*"); ASSERT_EQ(entt::any_cast(cref), 42); entt::any_cast(any) = 3; ASSERT_EQ(entt::any_cast(any), 3); ASSERT_EQ(entt::any_cast(ref), 3); - ASSERT_EQ(entt::any_cast(cref), 3); + ASSERT_EQ(entt::any_cast(cref), 3); std::swap(ref, cref); @@ -683,12 +690,24 @@ TEST(Any, AsRef) { ASSERT_EQ(entt::any_cast(&ref), nullptr); ASSERT_EQ(entt::any_cast(&cref), nullptr); + ASSERT_EQ(entt::any_cast(&ref), any.data()); + ASSERT_EQ(entt::any_cast(&cref), any.data()); + + ASSERT_DEATH(entt::any_cast(ref), ".*"); + ASSERT_DEATH(entt::any_cast(cref), ".*"); + + ASSERT_EQ(entt::any_cast(ref), 3); + ASSERT_EQ(entt::any_cast(cref), 3); ref = 42; cref = 42; ASSERT_NE(entt::any_cast(&ref), nullptr); ASSERT_NE(entt::any_cast(&cref), nullptr); + ASSERT_EQ(entt::any_cast(ref), 42); + ASSERT_EQ(entt::any_cast(cref), 42); + ASSERT_EQ(entt::any_cast(ref), 42); + ASSERT_EQ(entt::any_cast(cref), 42); ASSERT_NE(entt::any_cast(&ref), any.data()); ASSERT_NE(entt::any_cast(&cref), any.data()); } diff --git a/test/entt/poly/poly_deduced.cpp b/test/entt/poly/poly_deduced.cpp index a2552306b..20fa94ac4 100644 --- a/test/entt/poly/poly_deduced.cpp +++ b/test/entt/poly/poly_deduced.cpp @@ -11,13 +11,13 @@ struct Deduced: entt::type_list<> { void set(int v) { entt::poly_call<1>(*this, v); } int get() const { return entt::poly_call<2>(*this); } void decr() { entt::poly_call<3>(*this); } - int mul(int v) { return entt::poly_call<4>(*this, v); } + int mul(int v) const { return entt::poly_call<4>(*this, v); } }; template struct members { static void decr(Type &self) { self.set(self.get()-1); } - static double mul(Type &self, double v) { return v * self.get(); } + static double mul(const Type &self, double v) { return v * self.get(); } }; template @@ -35,7 +35,7 @@ struct impl { void set(int v) { value = v; } int get() const { return value; } void decrement() { --value; } - double multiply(double v) { return v * value; } + double multiply(double v) const { return v * value; } int value{}; }; @@ -123,7 +123,7 @@ TEST(PolyDeduced, Owned) { ASSERT_EQ(poly->mul(3), 3); } -TEST(PolyDeduced, Alias) { +TEST(PolyDeduced, Reference) { impl instance{}; entt::poly poly{std::ref(instance)}; @@ -146,3 +146,58 @@ TEST(PolyDeduced, Alias) { ASSERT_EQ(poly->get(), 1); ASSERT_EQ(poly->mul(3), 3); } + +TEST(PolyDeduced, ConstReference) { + impl instance{}; + entt::poly poly{std::cref(instance)}; + + ASSERT_TRUE(poly); + ASSERT_EQ(poly.data(), nullptr); + ASSERT_NE(std::as_const(poly).data(), nullptr); + ASSERT_EQ(instance.value, 0); + ASSERT_EQ(poly->get(), 0); + + ASSERT_DEATH(poly->set(1), ".*"); + ASSERT_DEATH(poly->incr(), ".*"); + + ASSERT_EQ(instance.value, 0); + ASSERT_EQ(poly->get(), 0); + ASSERT_EQ(poly->mul(3), 0); + + ASSERT_DEATH(poly->decr(), ".*"); + + ASSERT_EQ(instance.value, 0); + ASSERT_EQ(poly->get(), 0); + ASSERT_EQ(poly->mul(3), 0); +} + +TEST(PolyDeduced, AsRef) { + entt::poly poly{impl{}}; + auto ref = as_ref(poly); + auto cref = as_ref(std::as_const(poly)); + + ASSERT_NE(poly.data(), nullptr); + ASSERT_NE(ref.data(), nullptr); + ASSERT_EQ(cref.data(), nullptr); + ASSERT_NE(std::as_const(cref).data(), nullptr); + + std::swap(ref, cref); + + ASSERT_EQ(ref.data(), nullptr); + ASSERT_NE(std::as_const(ref).data(), nullptr); + ASSERT_NE(cref.data(), nullptr); + + ref = as_ref(ref); + cref = as_ref(std::as_const(cref)); + + ASSERT_EQ(ref.data(), nullptr); + ASSERT_NE(std::as_const(ref).data(), nullptr); + ASSERT_EQ(cref.data(), nullptr); + ASSERT_NE(std::as_const(cref).data(), nullptr); + + ref = impl{}; + cref = impl{}; + + ASSERT_NE(ref.data(), nullptr); + ASSERT_NE(cref.data(), nullptr); +} diff --git a/test/entt/poly/poly_defined.cpp b/test/entt/poly/poly_defined.cpp index 0c74c4aa6..b00431598 100644 --- a/test/entt/poly/poly_defined.cpp +++ b/test/entt/poly/poly_defined.cpp @@ -9,7 +9,7 @@ struct Defined: entt::type_list< void(int), int() const, void(), - int(int) + int(int) const > { template struct type: Base { @@ -17,13 +17,13 @@ struct Defined: entt::type_list< void set(int v) { entt::poly_call<1>(*this, v); } int get() const { return entt::poly_call<2>(*this); } void decr() { entt::poly_call<3>(*this); } - int mul(int v) { return entt::poly_call<4>(*this, v); } + int mul(int v) const { return entt::poly_call<4>(*this, v); } }; template struct members { static void decr(Type &self) { self.decrement(); } - static double mul(Type &self, double v) { return self.multiply(v); } + static double mul(const Type &self, double v) { return self.multiply(v); } }; template @@ -41,7 +41,7 @@ struct impl { void set(int v) { value = v; } int get() const { return value; } void decrement() { --value; } - double multiply(double v) { return v * value; } + double multiply(double v) const { return v * value; } int value{}; }; @@ -129,7 +129,7 @@ TEST(PolyDefined, Owned) { ASSERT_EQ(poly->mul(3), 3); } -TEST(PolyDefined, Alias) { +TEST(PolyDefined, Reference) { impl instance{}; entt::poly poly{std::ref(instance)}; @@ -152,3 +152,58 @@ TEST(PolyDefined, Alias) { ASSERT_EQ(poly->get(), 1); ASSERT_EQ(poly->mul(3), 3); } + +TEST(PolyDefined, ConstReference) { + impl instance{}; + entt::poly poly{std::cref(instance)}; + + ASSERT_TRUE(poly); + ASSERT_EQ(poly.data(), nullptr); + ASSERT_NE(std::as_const(poly).data(), nullptr); + ASSERT_EQ(instance.value, 0); + ASSERT_EQ(poly->get(), 0); + + ASSERT_DEATH(poly->set(1), ".*"); + ASSERT_DEATH(poly->incr(), ".*"); + + ASSERT_EQ(instance.value, 0); + ASSERT_EQ(poly->get(), 0); + ASSERT_EQ(poly->mul(3), 0); + + ASSERT_DEATH(poly->decr(), ".*"); + + ASSERT_EQ(instance.value, 0); + ASSERT_EQ(poly->get(), 0); + ASSERT_EQ(poly->mul(3), 0); +} + +TEST(PolyDefined, AsRef) { + entt::poly poly{impl{}}; + auto ref = as_ref(poly); + auto cref = as_ref(std::as_const(poly)); + + ASSERT_NE(poly.data(), nullptr); + ASSERT_NE(ref.data(), nullptr); + ASSERT_EQ(cref.data(), nullptr); + ASSERT_NE(std::as_const(cref).data(), nullptr); + + std::swap(ref, cref); + + ASSERT_EQ(ref.data(), nullptr); + ASSERT_NE(std::as_const(ref).data(), nullptr); + ASSERT_NE(cref.data(), nullptr); + + ref = as_ref(ref); + cref = as_ref(std::as_const(cref)); + + ASSERT_EQ(ref.data(), nullptr); + ASSERT_NE(std::as_const(ref).data(), nullptr); + ASSERT_EQ(cref.data(), nullptr); + ASSERT_NE(std::as_const(cref).data(), nullptr); + + ref = impl{}; + cref = impl{}; + + ASSERT_NE(ref.data(), nullptr); + ASSERT_NE(cref.data(), nullptr); +}