llvm.org GIT mirror llvm / 4857402
Only include move-related Optional<T> tests when rvalue references are available. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175730 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 6 years ago
1 changed file(s) with 131 addition(s) and 129 deletion(s). Raw diff Collapse all Expand all
3939 unsigned NonDefaultConstructible::Destructions = 0;
4040 unsigned NonDefaultConstructible::CopyAssignments = 0;
4141
42 // Test fixture
43 class OptionalTest : public testing::Test {
44 };
45
46 TEST_F(OptionalTest, NonDefaultConstructibleTest) {
47 Optional O;
48 EXPECT_FALSE(O);
49 }
50
51 TEST_F(OptionalTest, ResetTest) {
52 NonDefaultConstructible::ResetCounts();
53 Optional O(NonDefaultConstructible(3));
54 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
55 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
56 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
57 NonDefaultConstructible::ResetCounts();
58 O.reset();
59 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
60 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
61 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
62 }
63
64 TEST_F(OptionalTest, InitializationLeakTest) {
65 NonDefaultConstructible::ResetCounts();
66 Optional(NonDefaultConstructible(3));
67 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
68 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
69 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
70 }
71
72 TEST_F(OptionalTest, CopyConstructionTest) {
73 NonDefaultConstructible::ResetCounts();
74 {
75 Optional A(NonDefaultConstructible(3));
76 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
77 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
78 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
79 NonDefaultConstructible::ResetCounts();
80 Optional B(A);
81 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
82 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
83 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
84 NonDefaultConstructible::ResetCounts();
85 }
86 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
87 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
88 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
89 }
90
91 TEST_F(OptionalTest, ConstructingCopyAssignmentTest) {
92 NonDefaultConstructible::ResetCounts();
93 {
94 Optional A(NonDefaultConstructible(3));
95 Optional B;
96 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
97 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
98 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
99 NonDefaultConstructible::ResetCounts();
100 B = A;
101 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
102 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
103 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
104 NonDefaultConstructible::ResetCounts();
105 }
106 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
107 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
108 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
109 }
110
111 TEST_F(OptionalTest, CopyingCopyAssignmentTest) {
112 NonDefaultConstructible::ResetCounts();
113 {
114 Optional A(NonDefaultConstructible(3));
115 Optional B(NonDefaultConstructible(4));
116 EXPECT_EQ(2u, NonDefaultConstructible::CopyConstructions);
117 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
118 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
119 NonDefaultConstructible::ResetCounts();
120 B = A;
121 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
122 EXPECT_EQ(1u, NonDefaultConstructible::CopyAssignments);
123 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
124 NonDefaultConstructible::ResetCounts();
125 }
126 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
127 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
128 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
129 }
130
131 TEST_F(OptionalTest, DeletingCopyAssignmentTest) {
132 NonDefaultConstructible::ResetCounts();
133 {
134 Optional A;
135 Optional B(NonDefaultConstructible(3));
136 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
137 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
138 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
139 NonDefaultConstructible::ResetCounts();
140 B = A;
141 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
142 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
143 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
144 NonDefaultConstructible::ResetCounts();
145 }
146 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
147 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
148 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
149 }
150
151 TEST_F(OptionalTest, NullCopyConstructionTest) {
152 NonDefaultConstructible::ResetCounts();
153 {
154 Optional A;
155 Optional B;
156 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
157 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
158 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
159 NonDefaultConstructible::ResetCounts();
160 B = A;
161 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
162 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
163 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
164 NonDefaultConstructible::ResetCounts();
165 }
166 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
167 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
168 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
169 }
170
171 #if LLVM_HAS_RVALUE_REFERENCES
42172 struct MoveOnly {
43173 static unsigned MoveConstructions;
44174 static unsigned Destructions;
69199 unsigned MoveOnly::Destructions = 0;
70200 unsigned MoveOnly::MoveAssignments = 0;
71201
72 // Test fixture
73 class OptionalTest : public testing::Test {
74 };
75
76 TEST_F(OptionalTest, NonDefaultConstructibleTest) {
77 Optional O;
78 EXPECT_FALSE(O);
79 }
80
81 TEST_F(OptionalTest, ResetTest) {
82 NonDefaultConstructible::ResetCounts();
83 Optional O(NonDefaultConstructible(3));
84 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
85 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
86 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
87 NonDefaultConstructible::ResetCounts();
88 O.reset();
89 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
90 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
91 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
92 }
93
94 TEST_F(OptionalTest, InitializationLeakTest) {
95 NonDefaultConstructible::ResetCounts();
96 Optional(NonDefaultConstructible(3));
97 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
98 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
99 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
100 }
101
102 TEST_F(OptionalTest, CopyConstructionTest) {
103 NonDefaultConstructible::ResetCounts();
104 {
105 Optional A(NonDefaultConstructible(3));
106 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
107 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
108 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
109 NonDefaultConstructible::ResetCounts();
110 Optional B(A);
111 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
112 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
113 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
114 NonDefaultConstructible::ResetCounts();
115 }
116 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
117 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
118 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
119 }
120
121 TEST_F(OptionalTest, ConstructingCopyAssignmentTest) {
122 NonDefaultConstructible::ResetCounts();
123 {
124 Optional A(NonDefaultConstructible(3));
125 Optional B;
126 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
127 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
128 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
129 NonDefaultConstructible::ResetCounts();
130 B = A;
131 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
132 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
133 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
134 NonDefaultConstructible::ResetCounts();
135 }
136 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
137 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
138 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
139 }
140
141 TEST_F(OptionalTest, CopyingCopyAssignmentTest) {
142 NonDefaultConstructible::ResetCounts();
143 {
144 Optional A(NonDefaultConstructible(3));
145 Optional B(NonDefaultConstructible(4));
146 EXPECT_EQ(2u, NonDefaultConstructible::CopyConstructions);
147 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
148 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
149 NonDefaultConstructible::ResetCounts();
150 B = A;
151 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
152 EXPECT_EQ(1u, NonDefaultConstructible::CopyAssignments);
153 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
154 NonDefaultConstructible::ResetCounts();
155 }
156 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
157 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
158 EXPECT_EQ(2u, NonDefaultConstructible::Destructions);
159 }
160
161 TEST_F(OptionalTest, DeletingCopyAssignmentTest) {
162 NonDefaultConstructible::ResetCounts();
163 {
164 Optional A;
165 Optional B(NonDefaultConstructible(3));
166 EXPECT_EQ(1u, NonDefaultConstructible::CopyConstructions);
167 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
168 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
169 NonDefaultConstructible::ResetCounts();
170 B = A;
171 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
172 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
173 EXPECT_EQ(1u, NonDefaultConstructible::Destructions);
174 NonDefaultConstructible::ResetCounts();
175 }
176 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
177 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
178 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
179 }
180
181 TEST_F(OptionalTest, NullCopyConstructionTest) {
182 NonDefaultConstructible::ResetCounts();
183 {
184 Optional A;
185 Optional B;
186 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
187 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
188 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
189 NonDefaultConstructible::ResetCounts();
190 B = A;
191 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
192 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
193 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
194 NonDefaultConstructible::ResetCounts();
195 }
196 EXPECT_EQ(0u, NonDefaultConstructible::CopyConstructions);
197 EXPECT_EQ(0u, NonDefaultConstructible::CopyAssignments);
198 EXPECT_EQ(0u, NonDefaultConstructible::Destructions);
199 }
200
201202 TEST_F(OptionalTest, MoveOnlyNull) {
202203 MoveOnly::ResetCounts();
203204 Optional O;
276277 EXPECT_EQ(1u, MoveOnly::MoveAssignments);
277278 EXPECT_EQ(1u, MoveOnly::Destructions);
278279 }
280 #endif
279281
280282 } // end anonymous namespace
281283