sled/src/any_test.cc

49 lines
1.2 KiB
C++
Raw Normal View History

2024-03-13 21:19:34 +08:00
#include <gtest/gtest.h>
#include <sled/any.h>
2024-03-13 22:09:48 +08:00
#include <sled/log/log.h>
2024-03-13 21:19:34 +08:00
2024-03-13 22:09:48 +08:00
TEST(Any, Assign)
{
sled::Any any1;
sled::Any any2(any1);
sled::Any any3(1);
sled::Any any4(any3);
sled::Any any5 = 1;
EXPECT_FALSE(any1.HasValue());
EXPECT_FALSE(any2.HasValue());
EXPECT_TRUE(any3.HasValue());
EXPECT_TRUE(any4.HasValue());
EXPECT_TRUE(any5.HasValue());
EXPECT_EQ(any3.Cast<int>(), 1);
EXPECT_EQ(any4.Cast<int>(), 1);
EXPECT_EQ(any5.Cast<int>(), 1);
EXPECT_EQ(any3.CastOr<std::string>("def"), "def");
EXPECT_EQ(any4.CastOr<std::string>("def"), "def");
EXPECT_EQ(any5.CastOr<std::string>("def"), "def");
EXPECT_EQ(any3.CastOr<int>(11), 1);
}
TEST(Any, std_swap)
{
sled::Any a;
sled::Any b = 2;
EXPECT_FALSE(a.HasValue());
EXPECT_TRUE(b.HasValue());
std::swap(a, b);
EXPECT_TRUE(a.HasValue());
EXPECT_FALSE(b.HasValue());
EXPECT_EQ(a.Cast<int>(), 2);
}
TEST(Any, custom_swap)
{
sled::Any a;
sled::Any b = 2;
EXPECT_FALSE(a.HasValue());
EXPECT_TRUE(b.HasValue());
a.swap(b);
EXPECT_TRUE(a.HasValue());
EXPECT_FALSE(b.HasValue());
EXPECT_EQ(a.Cast<int>(), 2);
}