#include "llvm/ADT/Any.h"
#include "gtest/gtest.h"
#include <cstdlib>
using namespace llvm;
namespace {
TEST(AnyTest, ConstructionAndAssignment) {
llvm::Any A;
llvm::Any B{7};
llvm::Any C{8};
llvm::Any D{"hello"};
llvm::Any E{3.7};
EXPECT_FALSE(A.hasValue());
EXPECT_FALSE(any_isa<int>(A));
EXPECT_TRUE(B.hasValue());
EXPECT_TRUE(any_isa<int>(B));
EXPECT_FALSE(any_isa<float>(B));
EXPECT_TRUE(C.hasValue());
EXPECT_TRUE(any_isa<int>(C));
EXPECT_TRUE(D.hasValue());
EXPECT_TRUE(any_isa<const char *>(D));
EXPECT_FALSE(any_isa<int>(D));
EXPECT_TRUE(E.hasValue());
EXPECT_TRUE(any_isa<double>(E));
EXPECT_FALSE(any_isa<float>(E));
llvm::Any F(B);
EXPECT_TRUE(B.hasValue());
EXPECT_TRUE(F.hasValue());
EXPECT_TRUE(any_isa<int>(F));
EXPECT_TRUE(any_isa<int>(B));
llvm::Any G(std::move(C));
EXPECT_FALSE(C.hasValue());
EXPECT_TRUE(G.hasValue());
EXPECT_TRUE(any_isa<int>(G));
EXPECT_FALSE(any_isa<int>(C));
A = F;
EXPECT_TRUE(A.hasValue());
EXPECT_TRUE(F.hasValue());
EXPECT_TRUE(any_isa<int>(A));
EXPECT_TRUE(any_isa<int>(F));
B = std::move(G);
EXPECT_TRUE(B.hasValue());
EXPECT_FALSE(G.hasValue());
EXPECT_TRUE(any_isa<int>(B));
EXPECT_FALSE(any_isa<int>(G));
}
TEST(AnyTest, GoodAnyCast) {
llvm::Any A;
llvm::Any B{7};
llvm::Any C{8};
llvm::Any D{"hello"};
llvm::Any E{'x'};
EXPECT_EQ(7, llvm::any_cast<int>(B));
EXPECT_EQ(7, llvm::any_cast<int>(B));
EXPECT_STREQ("hello", llvm::any_cast<const char *>(D));
EXPECT_STREQ("hello", llvm::any_cast<const char *>(D));
EXPECT_EQ('x', llvm::any_cast<char>(E));
EXPECT_EQ('x', llvm::any_cast<char>(E));
llvm::Any F(B);
EXPECT_EQ(7, llvm::any_cast<int>(F));
EXPECT_EQ(7, llvm::any_cast<int>(F));
llvm::Any G(std::move(C));
EXPECT_EQ(8, llvm::any_cast<int>(G));
EXPECT_EQ(8, llvm::any_cast<int>(G));
A = F;
EXPECT_EQ(7, llvm::any_cast<int>(A));
EXPECT_EQ(7, llvm::any_cast<int>(A));
E = std::move(G);
EXPECT_EQ(8, llvm::any_cast<int>(E));
EXPECT_EQ(8, llvm::any_cast<int>(E));
EXPECT_EQ(8, llvm::any_cast<int>(std::move(E)));
EXPECT_TRUE(E.hasValue());
EXPECT_EQ(7, *llvm::any_cast<int>(&A));
int *N = llvm::any_cast<int>(&A);
*N = 42;
EXPECT_EQ(42, llvm::any_cast<int>(A));
llvm::any_cast<int &>(A) = 43;
EXPECT_EQ(43, llvm::any_cast<int>(A));
}
TEST(AnyTest, CopiesAndMoves) {
struct TestType {
TestType() = default;
TestType(const TestType &Other)
: Copies(Other.Copies + 1), Moves(Other.Moves) {}
TestType(TestType &&Other) : Copies(Other.Copies), Moves(Other.Moves + 1) {}
int Copies = 0;
int Moves = 0;
};
TestType T1 = llvm::any_cast<TestType>(Any{TestType()});
EXPECT_EQ(0, T1.Copies);
EXPECT_EQ(2, T1.Moves);
Any A{TestType()};
TestType T2 = llvm::any_cast<TestType>(A);
EXPECT_EQ(1, T2.Copies);
EXPECT_EQ(1, T2.Moves);
TestType T3 = llvm::any_cast<TestType>(std::move(A));
EXPECT_EQ(0, T3.Copies);
EXPECT_EQ(2, T3.Moves);
}
TEST(AnyTest, BadAnyCast) {
llvm::Any A;
llvm::Any B{7};
llvm::Any C{"hello"};
llvm::Any D{'x'};
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
EXPECT_DEATH(llvm::any_cast<int>(A), "");
EXPECT_DEATH(llvm::any_cast<float>(B), "");
EXPECT_DEATH(llvm::any_cast<int *>(B), "");
EXPECT_DEATH(llvm::any_cast<std::string>(C), "");
EXPECT_DEATH(llvm::any_cast<unsigned char>(D), "");
#endif
}
}