[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / unittests / ADT / AnyTest.cpp
blob2ca81bd61d571580dab8949344b00586ca1b0345
1 //===- llvm/unittest/Support/AnyTest.cpp - Any tests ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/Any.h"
10 #include "gtest/gtest.h"
11 #include <cstdlib>
13 using namespace llvm;
15 namespace {
17 // Make sure we can construct, copy-construct, move-construct, and assign Any's.
18 TEST(AnyTest, ConstructionAndAssignment) {
19 llvm::Any A;
20 llvm::Any B{7};
21 llvm::Any C{8};
22 llvm::Any D{"hello"};
23 llvm::Any E{3.7};
25 // An empty Any is not anything.
26 EXPECT_FALSE(A.hasValue());
27 EXPECT_FALSE(any_isa<int>(A));
29 // An int is an int but not something else.
30 EXPECT_TRUE(B.hasValue());
31 EXPECT_TRUE(any_isa<int>(B));
32 EXPECT_FALSE(any_isa<float>(B));
34 EXPECT_TRUE(C.hasValue());
35 EXPECT_TRUE(any_isa<int>(C));
37 // A const char * is a const char * but not an int.
38 EXPECT_TRUE(D.hasValue());
39 EXPECT_TRUE(any_isa<const char *>(D));
40 EXPECT_FALSE(any_isa<int>(D));
42 // A double is a double but not a float.
43 EXPECT_TRUE(E.hasValue());
44 EXPECT_TRUE(any_isa<double>(E));
45 EXPECT_FALSE(any_isa<float>(E));
47 // After copy constructing from an int, the new item and old item are both
48 // ints.
49 llvm::Any F(B);
50 EXPECT_TRUE(B.hasValue());
51 EXPECT_TRUE(F.hasValue());
52 EXPECT_TRUE(any_isa<int>(F));
53 EXPECT_TRUE(any_isa<int>(B));
55 // After move constructing from an int, the new item is an int and the old one
56 // isn't.
57 llvm::Any G(std::move(C));
58 EXPECT_FALSE(C.hasValue());
59 EXPECT_TRUE(G.hasValue());
60 EXPECT_TRUE(any_isa<int>(G));
61 EXPECT_FALSE(any_isa<int>(C));
63 // After copy-assigning from an int, the new item and old item are both ints.
64 A = F;
65 EXPECT_TRUE(A.hasValue());
66 EXPECT_TRUE(F.hasValue());
67 EXPECT_TRUE(any_isa<int>(A));
68 EXPECT_TRUE(any_isa<int>(F));
70 // After move-assigning from an int, the new item and old item are both ints.
71 B = std::move(G);
72 EXPECT_TRUE(B.hasValue());
73 EXPECT_FALSE(G.hasValue());
74 EXPECT_TRUE(any_isa<int>(B));
75 EXPECT_FALSE(any_isa<int>(G));
78 TEST(AnyTest, GoodAnyCast) {
79 llvm::Any A;
80 llvm::Any B{7};
81 llvm::Any C{8};
82 llvm::Any D{"hello"};
83 llvm::Any E{'x'};
85 // Check each value twice to make sure it isn't damaged by the cast.
86 EXPECT_EQ(7, llvm::any_cast<int>(B));
87 EXPECT_EQ(7, llvm::any_cast<int>(B));
89 EXPECT_STREQ("hello", llvm::any_cast<const char *>(D));
90 EXPECT_STREQ("hello", llvm::any_cast<const char *>(D));
92 EXPECT_EQ('x', llvm::any_cast<char>(E));
93 EXPECT_EQ('x', llvm::any_cast<char>(E));
95 llvm::Any F(B);
96 EXPECT_EQ(7, llvm::any_cast<int>(F));
97 EXPECT_EQ(7, llvm::any_cast<int>(F));
99 llvm::Any G(std::move(C));
100 EXPECT_EQ(8, llvm::any_cast<int>(G));
101 EXPECT_EQ(8, llvm::any_cast<int>(G));
103 A = F;
104 EXPECT_EQ(7, llvm::any_cast<int>(A));
105 EXPECT_EQ(7, llvm::any_cast<int>(A));
107 E = std::move(G);
108 EXPECT_EQ(8, llvm::any_cast<int>(E));
109 EXPECT_EQ(8, llvm::any_cast<int>(E));
111 // Make sure we can any_cast from an rvalue and that it's properly destroyed
112 // in the process.
113 EXPECT_EQ(8, llvm::any_cast<int>(std::move(E)));
114 EXPECT_TRUE(E.hasValue());
116 // Make sure moving from pointers gives back pointers, and that we can modify
117 // the underlying value through those pointers.
118 EXPECT_EQ(7, *llvm::any_cast<int>(&A));
119 int *N = llvm::any_cast<int>(&A);
120 *N = 42;
121 EXPECT_EQ(42, llvm::any_cast<int>(A));
123 // Make sure that we can any_cast to a reference and this is considered a good
124 // cast, resulting in an lvalue which can be modified.
125 llvm::any_cast<int &>(A) = 43;
126 EXPECT_EQ(43, llvm::any_cast<int>(A));
129 TEST(AnyTest, CopiesAndMoves) {
130 struct TestType {
131 TestType() = default;
132 TestType(const TestType &Other)
133 : Copies(Other.Copies + 1), Moves(Other.Moves) {}
134 TestType(TestType &&Other) : Copies(Other.Copies), Moves(Other.Moves + 1) {}
135 int Copies = 0;
136 int Moves = 0;
139 // One move to get TestType into the Any, and one move on the cast.
140 TestType T1 = llvm::any_cast<TestType>(Any{TestType()});
141 EXPECT_EQ(0, T1.Copies);
142 EXPECT_EQ(2, T1.Moves);
144 // One move to get TestType into the Any, and one copy on the cast.
145 Any A{TestType()};
146 TestType T2 = llvm::any_cast<TestType>(A);
147 EXPECT_EQ(1, T2.Copies);
148 EXPECT_EQ(1, T2.Moves);
150 // One move to get TestType into the Any, and one move on the cast.
151 TestType T3 = llvm::any_cast<TestType>(std::move(A));
152 EXPECT_EQ(0, T3.Copies);
153 EXPECT_EQ(2, T3.Moves);
156 TEST(AnyTest, BadAnyCast) {
157 llvm::Any A;
158 llvm::Any B{7};
159 llvm::Any C{"hello"};
160 llvm::Any D{'x'};
162 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
163 EXPECT_DEATH(llvm::any_cast<int>(A), "");
165 EXPECT_DEATH(llvm::any_cast<float>(B), "");
166 EXPECT_DEATH(llvm::any_cast<int *>(B), "");
168 EXPECT_DEATH(llvm::any_cast<std::string>(C), "");
170 EXPECT_DEATH(llvm::any_cast<unsigned char>(D), "");
171 #endif
174 } // anonymous namespace