Get the latest Blender, older versions, or experimental builds.
Stay up-to-date with the new features in the latest Blender releases.
Access production assets and knowledge from the open movies.
Documentation on the usage and features in Blender.
Latest development updates, by Blender developers.
Guidelines, release notes and development docs.
A platform to collect and share results of the Blender Benchmark.
The yearly event that brings the community together.
Support core development with a monthly contribution.
Perform a single donation with more payment options available.
Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.
/* SPDX-License-Identifier: Apache-2.0 */
#include "BLI_any.hh"
#include "BLI_map.hh"
#include "testing/testing.h"
namespace blender::tests {
TEST(any, DefaultConstructor)
{
Any<> a;
EXPECT_FALSE(a.has_value());
}
TEST(any, AssignInt)
Any<> a = 5;
EXPECT_TRUE(a.has_value());
EXPECT_TRUE(a.is<int>());
EXPECT_FALSE(a.is<float>());
const int &value = a.get<int>();
EXPECT_EQ(value, 5);
a = 10;
EXPECT_EQ(value, 10);
Any<> b = a;
EXPECT_TRUE(b.has_value());
EXPECT_EQ(b.get<int>(), 10);
Any<> c = std::move(a);
EXPECT_TRUE(c);
EXPECT_EQ(c.get<int>(), 10);
EXPECT_EQ(a.get<int>(), 10); /* NOLINT: bugprone-use-after-move */
a.reset();
EXPECT_FALSE(a);
TEST(any, AssignMap)
Any<> a = Map<int, int>();
EXPECT_TRUE((a.is<Map<int, int>>()));
EXPECT_FALSE((a.is<Map<int, float>>()));
Map<int, int> &map = a.get<Map<int, int>>();
map.add(4, 2);
EXPECT_EQ((a.get<Map<int, int>>().lookup(4)), 2);
EXPECT_TRUE(b);
EXPECT_EQ((b.get<Map<int, int>>().lookup(4)), 2);
/* Test valid state after self assignment. Clang emits `-Wself-assign-overloaded` with `c=c;`.
* And `pragma` suppression creates warnings on other compilers. */
c = static_cast<decltype(a) &>(c);
EXPECT_EQ((c.get<Map<int, int>>().lookup(4)), 2);
EXPECT_TRUE((a.get<Map<int, int>>().is_empty())); /* NOLINT: bugprone-use-after-move */
TEST(any, AssignAny)
Any<> b = std::string("hello");
Any<> c;
Any<> z;
EXPECT_FALSE(z.has_value());
z = a;
EXPECT_TRUE(z.has_value());
EXPECT_EQ(z.get<int>(), 5);
z = b;
EXPECT_EQ(z.get<std::string>(), "hello");
z = c;
z = Any(std::in_place_type<Any<>>, a);
EXPECT_FALSE(z.is<int>());
EXPECT_TRUE(z.is<Any<>>());
EXPECT_EQ(z.get<Any<>>().get<int>(), 5);
struct ExtraSizeInfo {
size_t size;
template<typename T> static constexpr ExtraSizeInfo get()
return {sizeof(T)};
};
TEST(any, ExtraInfo)
using MyAny = Any<ExtraSizeInfo>;
MyAny a = 5;
EXPECT_EQ(a.extra_info().size, sizeof(int));
a = std::string("hello");
EXPECT_EQ(a.extra_info().size, sizeof(std::string));
} // namespace blender::tests