1 //===- STLForwardCompatTest.cpp - Unit tests for STLForwardCompat ---------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/STLForwardCompat.h"
10 #include "CountCopyAndMove.h"
11 #include "gtest/gtest.h"
16 class STLForwardCompatRemoveCVRefTest
: public ::testing::Test
{};
18 using STLForwardCompatRemoveCVRefTestTypes
= ::testing::Types
<
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 *>
32 TYPED_TEST_SUITE(STLForwardCompatRemoveCVRefTest
,
33 STLForwardCompatRemoveCVRefTestTypes
, );
35 TYPED_TEST(STLForwardCompatRemoveCVRefTest
, RemoveCVRef
) {
36 using From
= typename
TypeParam::first_type
;
37 using To
= typename
TypeParam::second_type
;
39 (std::is_same
<typename
llvm::remove_cvref
<From
>::type
, To
>::value
));
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
));
48 TEST(TransformTest
, TransformStd
) {
51 std::optional
<int> B
= llvm::transformOptional(A
, [&](int N
) { return N
+ 1; });
52 EXPECT_FALSE(B
.has_value());
55 std::optional
<int> C
= llvm::transformOptional(A
, [&](int N
) { return N
+ 1; });
56 EXPECT_TRUE(C
.has_value());
60 TEST(TransformTest
, MoveTransformStd
) {
61 using llvm::CountCopyAndMove
;
63 std::optional
<CountCopyAndMove
> A
;
65 CountCopyAndMove::ResetCounts();
66 std::optional
<int> B
= llvm::transformOptional(
67 std::move(A
), [&](const CountCopyAndMove
&M
) { return M
.val
+ 2; });
68 EXPECT_FALSE(B
.has_value());
69 EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
70 EXPECT_EQ(0, CountCopyAndMove::MoveConstructions
);
71 EXPECT_EQ(0, CountCopyAndMove::MoveAssignments
);
72 EXPECT_EQ(0, CountCopyAndMove::Destructions
);
74 A
= CountCopyAndMove(5);
75 CountCopyAndMove::ResetCounts();
76 std::optional
<int> C
= llvm::transformOptional(
77 std::move(A
), [&](const CountCopyAndMove
&M
) { return M
.val
+ 2; });
78 EXPECT_TRUE(C
.has_value());
80 EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
81 EXPECT_EQ(0, CountCopyAndMove::MoveConstructions
);
82 EXPECT_EQ(0, CountCopyAndMove::MoveAssignments
);
83 EXPECT_EQ(0, CountCopyAndMove::Destructions
);
86 TEST(TransformTest
, TransformLlvm
) {
89 std::optional
<int> B
=
90 llvm::transformOptional(A
, [&](int N
) { return N
+ 1; });
91 EXPECT_FALSE(B
.has_value());
94 std::optional
<int> C
=
95 llvm::transformOptional(A
, [&](int N
) { return N
+ 1; });
96 EXPECT_TRUE(C
.has_value());
100 TEST(TransformTest
, MoveTransformLlvm
) {
101 using llvm::CountCopyAndMove
;
103 std::optional
<CountCopyAndMove
> A
;
105 CountCopyAndMove::ResetCounts();
106 std::optional
<int> B
= llvm::transformOptional(
107 std::move(A
), [&](const CountCopyAndMove
&M
) { return M
.val
+ 2; });
108 EXPECT_FALSE(B
.has_value());
109 EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
110 EXPECT_EQ(0, CountCopyAndMove::MoveConstructions
);
111 EXPECT_EQ(0, CountCopyAndMove::MoveAssignments
);
112 EXPECT_EQ(0, CountCopyAndMove::Destructions
);
114 A
= CountCopyAndMove(5);
115 CountCopyAndMove::ResetCounts();
116 std::optional
<int> C
= llvm::transformOptional(
117 std::move(A
), [&](const CountCopyAndMove
&M
) { return M
.val
+ 2; });
118 EXPECT_TRUE(C
.has_value());
120 EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
121 EXPECT_EQ(0, CountCopyAndMove::MoveConstructions
);
122 EXPECT_EQ(0, CountCopyAndMove::MoveAssignments
);
123 EXPECT_EQ(0, CountCopyAndMove::Destructions
);
126 TEST(TransformTest
, ToUnderlying
) {
127 enum E
{ A1
= 0, B1
= -1 };
128 static_assert(llvm::to_underlying(A1
) == 0);
129 static_assert(llvm::to_underlying(B1
) == -1);
131 enum E2
: unsigned char { A2
= 0, B2
};
133 std::is_same_v
<unsigned char, decltype(llvm::to_underlying(A2
))>);
134 static_assert(llvm::to_underlying(A2
) == 0);
135 static_assert(llvm::to_underlying(B2
) == 1);
137 enum class E3
{ A3
= -1, B3
};
138 static_assert(std::is_same_v
<int, decltype(llvm::to_underlying(E3::A3
))>);
139 static_assert(llvm::to_underlying(E3::A3
) == -1);
140 static_assert(llvm::to_underlying(E3::B3
) == 0);