Debian Codegraph @

llvm/unittests/ADT/STLForwardCompatTest.cpp

raw
1 //===- STLForwardCompatTest.cpp - Unit tests for STLForwardCompat ---------===//
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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/STLForwardCompat.h"
10 #include "CountCopyAndMove.h"
11 #include "gtest/gtest.h"
12
13 namespace {
14
15 template <typename T>
16 class STLForwardCompatRemoveCVRefTest : public ::testing::Test {};
17
18 using STLForwardCompatRemoveCVRefTestTypes = ::testing::Types<
19 // clang-format off
20 std::pair<int, int>,
21 std::pair<int &, int>,
22 std::pair<const int, int>,
23 std::pair<volatile int, int>,
24 std::pair<const volatile int &, int>,
25 std::pair<int *, int *>,
26 std::pair<int *const, int *>,
27 std::pair<const int *, const int *>,
28 std::pair<int *&, int *>
29 // clang-format on
30 >;
31
32 TYPED_TEST_SUITE(STLForwardCompatRemoveCVRefTest,
33 STLForwardCompatRemoveCVRefTestTypes, );
34
35 TYPED_TEST(STLForwardCompatRemoveCVRefTest, RemoveCVRef) {
36 using From = typename TypeParam::first_type;
37 using To = typename TypeParam::second_type;
38 EXPECT_TRUE(
39 (std::is_same<typename llvm::remove_cvref<From>::type, To>::value));
40 }
41
42 TYPED_TEST(STLForwardCompatRemoveCVRefTest, RemoveCVRefT) {
43 using From = typename TypeParam::first_type;
44 EXPECT_TRUE((std::is_same<typename llvm::remove_cvref<From>::type,
45 llvm::remove_cvref_t<From>>::value));
46 }
47
48 template <typename T> class TypeIdentityTest : public ::testing::Test {
49 public:
50 using TypeIdentity = llvm::type_identity<T>;
51 };
52
53 struct A {
54 struct B {};
55 };
56 using TypeIdentityTestTypes =
57 ::testing::Types<int, volatile int, A, const A::B>;
58
59 TYPED_TEST_SUITE(TypeIdentityTest, TypeIdentityTestTypes, /*NameGenerator*/);
60
61 TYPED_TEST(TypeIdentityTest, Identity) {
62 // TestFixture is the instantiated TypeIdentityTest.
63 EXPECT_TRUE(
64 (std::is_same_v<TypeParam, typename TestFixture::TypeIdentity::type>));
65 }
66
67 TEST(TransformTest, TransformStd) {
68 std::optional<int> A;
69
70 std::optional<int> B = llvm::transformOptional(A, [&](int N) { return N + 1; });
71 EXPECT_FALSE(B.has_value());
72
73 A = 3;
74 std::optional<int> C = llvm::transformOptional(A, [&](int N) { return N + 1; });
75 EXPECT_TRUE(C.has_value());
76 EXPECT_EQ(4, *C);
77 }
78
79 TEST(TransformTest, MoveTransformStd) {
80 using llvm::CountCopyAndMove;
81
82 std::optional<CountCopyAndMove> A;
83
84 CountCopyAndMove::ResetCounts();
85 std::optional<int> B = llvm::transformOptional(
86 std::move(A), [&](const CountCopyAndMove &M) { return M.val + 2; });
87 EXPECT_FALSE(B.has_value());
88 EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
89 EXPECT_EQ(0, CountCopyAndMove::MoveConstructions);
90 EXPECT_EQ(0, CountCopyAndMove::MoveAssignments);
91 EXPECT_EQ(0, CountCopyAndMove::Destructions);
92
93 A = CountCopyAndMove(5);
94 CountCopyAndMove::ResetCounts();
95 std::optional<int> C = llvm::transformOptional(
96 std::move(A), [&](const CountCopyAndMove &M) { return M.val + 2; });
97 EXPECT_TRUE(C.has_value());
98 EXPECT_EQ(7, *C);
99 EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
100 EXPECT_EQ(0, CountCopyAndMove::MoveConstructions);
101 EXPECT_EQ(0, CountCopyAndMove::MoveAssignments);
102 EXPECT_EQ(0, CountCopyAndMove::Destructions);
103 }
104
105 TEST(TransformTest, TransformLlvm) {
106 std::optional<int> A;
107
108 std::optional<int> B =
109 llvm::transformOptional(A, [&](int N) { return N + 1; });
110 EXPECT_FALSE(B.has_value());
111
112 A = 3;
113 std::optional<int> C =
114 llvm::transformOptional(A, [&](int N) { return N + 1; });
115 EXPECT_TRUE(C.has_value());
116 EXPECT_EQ(4, *C);
117 }
118
119 TEST(TransformTest, MoveTransformLlvm) {
120 using llvm::CountCopyAndMove;
121
122 std::optional<CountCopyAndMove> A;
123
124 CountCopyAndMove::ResetCounts();
125 std::optional<int> B = llvm::transformOptional(
126 std::move(A), [&](const CountCopyAndMove &M) { return M.val + 2; });
127 EXPECT_FALSE(B.has_value());
128 EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
129 EXPECT_EQ(0, CountCopyAndMove::MoveConstructions);
130 EXPECT_EQ(0, CountCopyAndMove::MoveAssignments);
131 EXPECT_EQ(0, CountCopyAndMove::Destructions);
132
133 A = CountCopyAndMove(5);
134 CountCopyAndMove::ResetCounts();
135 std::optional<int> C = llvm::transformOptional(
136 std::move(A), [&](const CountCopyAndMove &M) { return M.val + 2; });
137 EXPECT_TRUE(C.has_value());
138 EXPECT_EQ(7, *C);
139 EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
140 EXPECT_EQ(0, CountCopyAndMove::MoveConstructions);
141 EXPECT_EQ(0, CountCopyAndMove::MoveAssignments);
142 EXPECT_EQ(0, CountCopyAndMove::Destructions);
143 }
144
145 TEST(TransformTest, ToUnderlying) {
146 enum E { A1 = 0, B1 = -1 };
147 static_assert(llvm::to_underlying(A1) == 0);
148 static_assert(llvm::to_underlying(B1) == -1);
149
150 enum E2 : unsigned char { A2 = 0, B2 };
151 static_assert(
152 std::is_same_v<unsigned char, decltype(llvm::to_underlying(A2))>);
153 static_assert(llvm::to_underlying(A2) == 0);
154 static_assert(llvm::to_underlying(B2) == 1);
155
156 enum class E3 { A3 = -1, B3 };
157 static_assert(std::is_same_v<int, decltype(llvm::to_underlying(E3::A3))>);
158 static_assert(llvm::to_underlying(E3::A3) == -1);
159 static_assert(llvm::to_underlying(E3::B3) == 0);
160 }
161
162 } // namespace