Abseil Team | ff70456 | 2017-12-20 12:34:46 -0800 | [diff] [blame] | 1 | // Copyright 2017 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
nik7273 | 38b7043 | 2019-03-08 10:27:53 -0500 | [diff] [blame] | 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
Abseil Team | ff70456 | 2017-12-20 12:34:46 -0800 | [diff] [blame] | 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 15 | #include "absl/base/internal/exception_safety_testing.h" |
| 16 | |
| 17 | #include <cstddef> |
| 18 | #include <exception> |
| 19 | #include <iostream> |
| 20 | #include <list> |
Abseil Team | eed36bf | 2018-03-15 13:48:55 -0700 | [diff] [blame] | 21 | #include <type_traits> |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include "gtest/gtest-spi.h" |
| 25 | #include "gtest/gtest.h" |
| 26 | #include "absl/memory/memory.h" |
| 27 | |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 28 | namespace testing { |
| 29 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 30 | namespace { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 31 | |
| 32 | using ::testing::exceptions_internal::SetCountdown; |
| 33 | using ::testing::exceptions_internal::TestException; |
| 34 | using ::testing::exceptions_internal::UnsetCountdown; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 35 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 36 | // EXPECT_NO_THROW can't inspect the thrown inspection in general. |
| 37 | template <typename F> |
| 38 | void ExpectNoThrow(const F& f) { |
| 39 | try { |
| 40 | f(); |
Abseil Team | 0245191 | 2018-09-11 11:22:56 -0700 | [diff] [blame] | 41 | } catch (const TestException& e) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 42 | ADD_FAILURE() << "Unexpected exception thrown from " << e.what(); |
| 43 | } |
| 44 | } |
| 45 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 46 | TEST(ThrowingValueTest, Throws) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 47 | SetCountdown(); |
| 48 | EXPECT_THROW(ThrowingValue<> bomb, TestException); |
| 49 | |
| 50 | // It's not guaranteed that every operator only throws *once*. The default |
| 51 | // ctor only throws once, though, so use it to make sure we only throw when |
| 52 | // the countdown hits 0 |
Abseil Team | faf0a1b | 2018-04-20 09:16:52 -0700 | [diff] [blame] | 53 | SetCountdown(2); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 54 | ExpectNoThrow([]() { ThrowingValue<> bomb; }); |
| 55 | ExpectNoThrow([]() { ThrowingValue<> bomb; }); |
| 56 | EXPECT_THROW(ThrowingValue<> bomb, TestException); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 57 | |
| 58 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Tests that an operation throws when the countdown is at 0, doesn't throw when |
| 62 | // the countdown doesn't hit 0, and doesn't modify the state of the |
| 63 | // ThrowingValue if it throws |
| 64 | template <typename F> |
Abseil Team | 4132ce2 | 2018-01-05 07:54:33 -0800 | [diff] [blame] | 65 | void TestOp(const F& f) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 66 | ExpectNoThrow(f); |
| 67 | |
| 68 | SetCountdown(); |
| 69 | EXPECT_THROW(f(), TestException); |
| 70 | UnsetCountdown(); |
| 71 | } |
| 72 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 73 | TEST(ThrowingValueTest, ThrowingCtors) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 74 | ThrowingValue<> bomb; |
| 75 | |
| 76 | TestOp([]() { ThrowingValue<> bomb(1); }); |
| 77 | TestOp([&]() { ThrowingValue<> bomb1 = bomb; }); |
| 78 | TestOp([&]() { ThrowingValue<> bomb1 = std::move(bomb); }); |
| 79 | } |
| 80 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 81 | TEST(ThrowingValueTest, ThrowingAssignment) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 82 | ThrowingValue<> bomb, bomb1; |
| 83 | |
| 84 | TestOp([&]() { bomb = bomb1; }); |
| 85 | TestOp([&]() { bomb = std::move(bomb1); }); |
Abseil Team | 26b789f | 2018-05-04 09:58:56 -0700 | [diff] [blame] | 86 | |
| 87 | // Test that when assignment throws, the assignment should fail (lhs != rhs) |
| 88 | // and strong guarantee fails (lhs != lhs_copy). |
| 89 | { |
| 90 | ThrowingValue<> lhs(39), rhs(42); |
| 91 | ThrowingValue<> lhs_copy(lhs); |
| 92 | SetCountdown(); |
| 93 | EXPECT_THROW(lhs = rhs, TestException); |
| 94 | UnsetCountdown(); |
| 95 | EXPECT_NE(lhs, rhs); |
| 96 | EXPECT_NE(lhs_copy, lhs); |
| 97 | } |
| 98 | { |
| 99 | ThrowingValue<> lhs(39), rhs(42); |
| 100 | ThrowingValue<> lhs_copy(lhs), rhs_copy(rhs); |
| 101 | SetCountdown(); |
| 102 | EXPECT_THROW(lhs = std::move(rhs), TestException); |
| 103 | UnsetCountdown(); |
| 104 | EXPECT_NE(lhs, rhs_copy); |
| 105 | EXPECT_NE(lhs_copy, lhs); |
| 106 | } |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 109 | TEST(ThrowingValueTest, ThrowingComparisons) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 110 | ThrowingValue<> bomb1, bomb2; |
| 111 | TestOp([&]() { return bomb1 == bomb2; }); |
| 112 | TestOp([&]() { return bomb1 != bomb2; }); |
| 113 | TestOp([&]() { return bomb1 < bomb2; }); |
| 114 | TestOp([&]() { return bomb1 <= bomb2; }); |
| 115 | TestOp([&]() { return bomb1 > bomb2; }); |
| 116 | TestOp([&]() { return bomb1 >= bomb2; }); |
| 117 | } |
| 118 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 119 | TEST(ThrowingValueTest, ThrowingArithmeticOps) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 120 | ThrowingValue<> bomb1(1), bomb2(2); |
| 121 | |
| 122 | TestOp([&bomb1]() { +bomb1; }); |
| 123 | TestOp([&bomb1]() { -bomb1; }); |
| 124 | TestOp([&bomb1]() { ++bomb1; }); |
| 125 | TestOp([&bomb1]() { bomb1++; }); |
| 126 | TestOp([&bomb1]() { --bomb1; }); |
| 127 | TestOp([&bomb1]() { bomb1--; }); |
| 128 | |
| 129 | TestOp([&]() { bomb1 + bomb2; }); |
| 130 | TestOp([&]() { bomb1 - bomb2; }); |
| 131 | TestOp([&]() { bomb1* bomb2; }); |
| 132 | TestOp([&]() { bomb1 / bomb2; }); |
| 133 | TestOp([&]() { bomb1 << 1; }); |
| 134 | TestOp([&]() { bomb1 >> 1; }); |
| 135 | } |
| 136 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 137 | TEST(ThrowingValueTest, ThrowingLogicalOps) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 138 | ThrowingValue<> bomb1, bomb2; |
| 139 | |
| 140 | TestOp([&bomb1]() { !bomb1; }); |
| 141 | TestOp([&]() { bomb1&& bomb2; }); |
| 142 | TestOp([&]() { bomb1 || bomb2; }); |
| 143 | } |
| 144 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 145 | TEST(ThrowingValueTest, ThrowingBitwiseOps) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 146 | ThrowingValue<> bomb1, bomb2; |
| 147 | |
| 148 | TestOp([&bomb1]() { ~bomb1; }); |
| 149 | TestOp([&]() { bomb1& bomb2; }); |
| 150 | TestOp([&]() { bomb1 | bomb2; }); |
| 151 | TestOp([&]() { bomb1 ^ bomb2; }); |
| 152 | } |
| 153 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 154 | TEST(ThrowingValueTest, ThrowingCompoundAssignmentOps) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 155 | ThrowingValue<> bomb1(1), bomb2(2); |
| 156 | |
| 157 | TestOp([&]() { bomb1 += bomb2; }); |
| 158 | TestOp([&]() { bomb1 -= bomb2; }); |
| 159 | TestOp([&]() { bomb1 *= bomb2; }); |
| 160 | TestOp([&]() { bomb1 /= bomb2; }); |
| 161 | TestOp([&]() { bomb1 %= bomb2; }); |
| 162 | TestOp([&]() { bomb1 &= bomb2; }); |
| 163 | TestOp([&]() { bomb1 |= bomb2; }); |
| 164 | TestOp([&]() { bomb1 ^= bomb2; }); |
| 165 | TestOp([&]() { bomb1 *= bomb2; }); |
| 166 | } |
| 167 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 168 | TEST(ThrowingValueTest, ThrowingStreamOps) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 169 | ThrowingValue<> bomb; |
| 170 | |
Abseil Team | 92020a0 | 2018-06-08 08:14:48 -0700 | [diff] [blame] | 171 | TestOp([&]() { |
| 172 | std::istringstream stream; |
| 173 | stream >> bomb; |
| 174 | }); |
| 175 | TestOp([&]() { |
| 176 | std::stringstream stream; |
| 177 | stream << bomb; |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | // Tests the operator<< of ThrowingValue by forcing ConstructorTracker to emit |
Abseil Team | bed5bd6 | 2018-08-21 11:31:02 -0700 | [diff] [blame] | 182 | // a nonfatal failure that contains the string representation of the Thrower |
Abseil Team | 92020a0 | 2018-06-08 08:14:48 -0700 | [diff] [blame] | 183 | TEST(ThrowingValueTest, StreamOpsOutput) { |
| 184 | using ::testing::TypeSpec; |
| 185 | exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown); |
| 186 | |
| 187 | // Test default spec list (kEverythingThrows) |
| 188 | EXPECT_NONFATAL_FAILURE( |
| 189 | { |
| 190 | using Thrower = ThrowingValue<TypeSpec{}>; |
| 191 | auto thrower = Thrower(123); |
| 192 | thrower.~Thrower(); |
| 193 | }, |
| 194 | "ThrowingValue<>(123)"); |
| 195 | |
| 196 | // Test with one item in spec list (kNoThrowCopy) |
| 197 | EXPECT_NONFATAL_FAILURE( |
| 198 | { |
| 199 | using Thrower = ThrowingValue<TypeSpec::kNoThrowCopy>; |
| 200 | auto thrower = Thrower(234); |
| 201 | thrower.~Thrower(); |
| 202 | }, |
| 203 | "ThrowingValue<kNoThrowCopy>(234)"); |
| 204 | |
| 205 | // Test with multiple items in spec list (kNoThrowMove, kNoThrowNew) |
| 206 | EXPECT_NONFATAL_FAILURE( |
| 207 | { |
| 208 | using Thrower = |
| 209 | ThrowingValue<TypeSpec::kNoThrowMove | TypeSpec::kNoThrowNew>; |
| 210 | auto thrower = Thrower(345); |
| 211 | thrower.~Thrower(); |
| 212 | }, |
| 213 | "ThrowingValue<kNoThrowMove | kNoThrowNew>(345)"); |
| 214 | |
| 215 | // Test with all items in spec list (kNoThrowCopy, kNoThrowMove, kNoThrowNew) |
| 216 | EXPECT_NONFATAL_FAILURE( |
| 217 | { |
| 218 | using Thrower = ThrowingValue<static_cast<TypeSpec>(-1)>; |
| 219 | auto thrower = Thrower(456); |
| 220 | thrower.~Thrower(); |
| 221 | }, |
| 222 | "ThrowingValue<kNoThrowCopy | kNoThrowMove | kNoThrowNew>(456)"); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Abseil Team | 4132ce2 | 2018-01-05 07:54:33 -0800 | [diff] [blame] | 225 | template <typename F> |
| 226 | void TestAllocatingOp(const F& f) { |
Abseil Team | 4132ce2 | 2018-01-05 07:54:33 -0800 | [diff] [blame] | 227 | ExpectNoThrow(f); |
| 228 | |
| 229 | SetCountdown(); |
| 230 | EXPECT_THROW(f(), exceptions_internal::TestBadAllocException); |
| 231 | UnsetCountdown(); |
| 232 | } |
| 233 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 234 | TEST(ThrowingValueTest, ThrowingAllocatingOps) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 235 | // make_unique calls unqualified operator new, so these exercise the |
| 236 | // ThrowingValue overloads. |
Abseil Team | 4132ce2 | 2018-01-05 07:54:33 -0800 | [diff] [blame] | 237 | TestAllocatingOp([]() { return absl::make_unique<ThrowingValue<>>(1); }); |
| 238 | TestAllocatingOp([]() { return absl::make_unique<ThrowingValue<>[]>(2); }); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 241 | TEST(ThrowingValueTest, NonThrowingMoveCtor) { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 242 | ThrowingValue<TypeSpec::kNoThrowMove> nothrow_ctor; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 243 | |
| 244 | SetCountdown(); |
| 245 | ExpectNoThrow([¬hrow_ctor]() { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 246 | ThrowingValue<TypeSpec::kNoThrowMove> nothrow1 = std::move(nothrow_ctor); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 247 | }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 248 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 251 | TEST(ThrowingValueTest, NonThrowingMoveAssign) { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 252 | ThrowingValue<TypeSpec::kNoThrowMove> nothrow_assign1, nothrow_assign2; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 253 | |
| 254 | SetCountdown(); |
| 255 | ExpectNoThrow([¬hrow_assign1, ¬hrow_assign2]() { |
| 256 | nothrow_assign1 = std::move(nothrow_assign2); |
| 257 | }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 258 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 261 | TEST(ThrowingValueTest, ThrowingCopyCtor) { |
| 262 | ThrowingValue<> tv; |
| 263 | |
| 264 | TestOp([&]() { ThrowingValue<> tv_copy(tv); }); |
| 265 | } |
| 266 | |
| 267 | TEST(ThrowingValueTest, ThrowingCopyAssign) { |
| 268 | ThrowingValue<> tv1, tv2; |
| 269 | |
| 270 | TestOp([&]() { tv1 = tv2; }); |
| 271 | } |
| 272 | |
| 273 | TEST(ThrowingValueTest, NonThrowingCopyCtor) { |
| 274 | ThrowingValue<TypeSpec::kNoThrowCopy> nothrow_ctor; |
| 275 | |
| 276 | SetCountdown(); |
| 277 | ExpectNoThrow([¬hrow_ctor]() { |
| 278 | ThrowingValue<TypeSpec::kNoThrowCopy> nothrow1(nothrow_ctor); |
| 279 | }); |
| 280 | UnsetCountdown(); |
| 281 | } |
| 282 | |
| 283 | TEST(ThrowingValueTest, NonThrowingCopyAssign) { |
| 284 | ThrowingValue<TypeSpec::kNoThrowCopy> nothrow_assign1, nothrow_assign2; |
| 285 | |
| 286 | SetCountdown(); |
| 287 | ExpectNoThrow([¬hrow_assign1, ¬hrow_assign2]() { |
| 288 | nothrow_assign1 = nothrow_assign2; |
| 289 | }); |
| 290 | UnsetCountdown(); |
| 291 | } |
| 292 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 293 | TEST(ThrowingValueTest, ThrowingSwap) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 294 | ThrowingValue<> bomb1, bomb2; |
| 295 | TestOp([&]() { std::swap(bomb1, bomb2); }); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 298 | TEST(ThrowingValueTest, NonThrowingSwap) { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 299 | ThrowingValue<TypeSpec::kNoThrowMove> bomb1, bomb2; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 300 | ExpectNoThrow([&]() { std::swap(bomb1, bomb2); }); |
| 301 | } |
| 302 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 303 | TEST(ThrowingValueTest, NonThrowingAllocation) { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 304 | ThrowingValue<TypeSpec::kNoThrowNew>* allocated; |
| 305 | ThrowingValue<TypeSpec::kNoThrowNew>* array; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 306 | |
| 307 | ExpectNoThrow([&allocated]() { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 308 | allocated = new ThrowingValue<TypeSpec::kNoThrowNew>(1); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 309 | delete allocated; |
| 310 | }); |
| 311 | ExpectNoThrow([&array]() { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 312 | array = new ThrowingValue<TypeSpec::kNoThrowNew>[2]; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 313 | delete[] array; |
| 314 | }); |
| 315 | } |
| 316 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 317 | TEST(ThrowingValueTest, NonThrowingDelete) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 318 | auto* allocated = new ThrowingValue<>(1); |
| 319 | auto* array = new ThrowingValue<>[2]; |
| 320 | |
| 321 | SetCountdown(); |
| 322 | ExpectNoThrow([allocated]() { delete allocated; }); |
| 323 | SetCountdown(); |
| 324 | ExpectNoThrow([array]() { delete[] array; }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 325 | |
| 326 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | using Storage = |
| 330 | absl::aligned_storage_t<sizeof(ThrowingValue<>), alignof(ThrowingValue<>)>; |
| 331 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 332 | TEST(ThrowingValueTest, NonThrowingPlacementDelete) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 333 | constexpr int kArrayLen = 2; |
| 334 | // We intentionally create extra space to store the tag allocated by placement |
| 335 | // new[]. |
| 336 | constexpr int kStorageLen = 4; |
| 337 | |
| 338 | Storage buf; |
| 339 | Storage array_buf[kStorageLen]; |
| 340 | auto* placed = new (&buf) ThrowingValue<>(1); |
| 341 | auto placed_array = new (&array_buf) ThrowingValue<>[kArrayLen]; |
| 342 | |
| 343 | SetCountdown(); |
| 344 | ExpectNoThrow([placed, &buf]() { |
| 345 | placed->~ThrowingValue<>(); |
| 346 | ThrowingValue<>::operator delete(placed, &buf); |
| 347 | }); |
| 348 | |
| 349 | SetCountdown(); |
| 350 | ExpectNoThrow([&, placed_array]() { |
| 351 | for (int i = 0; i < kArrayLen; ++i) placed_array[i].~ThrowingValue<>(); |
| 352 | ThrowingValue<>::operator delete[](placed_array, &array_buf); |
| 353 | }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 354 | |
| 355 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 358 | TEST(ThrowingValueTest, NonThrowingDestructor) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 359 | auto* allocated = new ThrowingValue<>(); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 360 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 361 | SetCountdown(); |
| 362 | ExpectNoThrow([allocated]() { delete allocated; }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 363 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | TEST(ThrowingBoolTest, ThrowingBool) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 367 | ThrowingBool t = true; |
| 368 | |
| 369 | // Test that it's contextually convertible to bool |
| 370 | if (t) { // NOLINT(whitespace/empty_if_body) |
| 371 | } |
| 372 | EXPECT_TRUE(t); |
| 373 | |
| 374 | TestOp([&]() { (void)!t; }); |
| 375 | } |
| 376 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 377 | TEST(ThrowingAllocatorTest, MemoryManagement) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 378 | // Just exercise the memory management capabilities under LSan to make sure we |
| 379 | // don't leak. |
| 380 | ThrowingAllocator<int> int_alloc; |
| 381 | int* ip = int_alloc.allocate(1); |
| 382 | int_alloc.deallocate(ip, 1); |
| 383 | int* i_array = int_alloc.allocate(2); |
| 384 | int_alloc.deallocate(i_array, 2); |
| 385 | |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 386 | ThrowingAllocator<ThrowingValue<>> tv_alloc; |
| 387 | ThrowingValue<>* ptr = tv_alloc.allocate(1); |
| 388 | tv_alloc.deallocate(ptr, 1); |
| 389 | ThrowingValue<>* tv_array = tv_alloc.allocate(2); |
| 390 | tv_alloc.deallocate(tv_array, 2); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 393 | TEST(ThrowingAllocatorTest, CallsGlobalNew) { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 394 | ThrowingAllocator<ThrowingValue<>, AllocSpec::kNoThrowAllocate> nothrow_alloc; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 395 | ThrowingValue<>* ptr; |
| 396 | |
| 397 | SetCountdown(); |
| 398 | // This will only throw if ThrowingValue::new is called. |
| 399 | ExpectNoThrow([&]() { ptr = nothrow_alloc.allocate(1); }); |
| 400 | nothrow_alloc.deallocate(ptr, 1); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 401 | |
| 402 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 405 | TEST(ThrowingAllocatorTest, ThrowingConstructors) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 406 | ThrowingAllocator<int> int_alloc; |
| 407 | int* ip = nullptr; |
| 408 | |
| 409 | SetCountdown(); |
| 410 | EXPECT_THROW(ip = int_alloc.allocate(1), TestException); |
| 411 | ExpectNoThrow([&]() { ip = int_alloc.allocate(1); }); |
| 412 | |
| 413 | *ip = 1; |
| 414 | SetCountdown(); |
| 415 | EXPECT_THROW(int_alloc.construct(ip, 2), TestException); |
| 416 | EXPECT_EQ(*ip, 1); |
| 417 | int_alloc.deallocate(ip, 1); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 418 | |
| 419 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 422 | TEST(ThrowingAllocatorTest, NonThrowingConstruction) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 423 | { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 424 | ThrowingAllocator<int, AllocSpec::kNoThrowAllocate> int_alloc; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 425 | int* ip = nullptr; |
| 426 | |
| 427 | SetCountdown(); |
| 428 | ExpectNoThrow([&]() { ip = int_alloc.allocate(1); }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 429 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 430 | SetCountdown(); |
| 431 | ExpectNoThrow([&]() { int_alloc.construct(ip, 2); }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 432 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 433 | EXPECT_EQ(*ip, 2); |
| 434 | int_alloc.deallocate(ip, 1); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 435 | |
| 436 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 439 | { |
| 440 | ThrowingAllocator<int> int_alloc; |
| 441 | int* ip = nullptr; |
| 442 | ExpectNoThrow([&]() { ip = int_alloc.allocate(1); }); |
| 443 | ExpectNoThrow([&]() { int_alloc.construct(ip, 2); }); |
| 444 | EXPECT_EQ(*ip, 2); |
| 445 | int_alloc.deallocate(ip, 1); |
| 446 | } |
| 447 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 448 | { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 449 | ThrowingAllocator<ThrowingValue<>, AllocSpec::kNoThrowAllocate> |
| 450 | nothrow_alloc; |
| 451 | ThrowingValue<>* ptr; |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 452 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 453 | SetCountdown(); |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 454 | ExpectNoThrow([&]() { ptr = nothrow_alloc.allocate(1); }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 455 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 456 | SetCountdown(); |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 457 | ExpectNoThrow( |
Abseil Team | 04dd99d | 2018-05-14 13:41:38 -0700 | [diff] [blame] | 458 | [&]() { nothrow_alloc.construct(ptr, 2, testing::nothrow_ctor); }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 459 | |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 460 | EXPECT_EQ(ptr->Get(), 2); |
| 461 | nothrow_alloc.destroy(ptr); |
| 462 | nothrow_alloc.deallocate(ptr, 1); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 463 | |
| 464 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 467 | { |
| 468 | ThrowingAllocator<int> a; |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 469 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 470 | SetCountdown(); |
| 471 | ExpectNoThrow([&]() { ThrowingAllocator<double> a1 = a; }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 472 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 473 | SetCountdown(); |
| 474 | ExpectNoThrow([&]() { ThrowingAllocator<double> a1 = std::move(a); }); |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 475 | |
| 476 | UnsetCountdown(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 480 | TEST(ThrowingAllocatorTest, ThrowingAllocatorConstruction) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 481 | ThrowingAllocator<int> a; |
| 482 | TestOp([]() { ThrowingAllocator<int> a; }); |
| 483 | TestOp([&]() { a.select_on_container_copy_construction(); }); |
| 484 | } |
| 485 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 486 | TEST(ThrowingAllocatorTest, State) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 487 | ThrowingAllocator<int> a1, a2; |
| 488 | EXPECT_NE(a1, a2); |
| 489 | |
| 490 | auto a3 = a1; |
| 491 | EXPECT_EQ(a3, a1); |
| 492 | int* ip = a1.allocate(1); |
| 493 | EXPECT_EQ(a3, a1); |
| 494 | a3.deallocate(ip, 1); |
| 495 | EXPECT_EQ(a3, a1); |
| 496 | } |
| 497 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 498 | TEST(ThrowingAllocatorTest, InVector) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 499 | std::vector<ThrowingValue<>, ThrowingAllocator<ThrowingValue<>>> v; |
| 500 | for (int i = 0; i < 20; ++i) v.push_back({}); |
| 501 | for (int i = 0; i < 20; ++i) v.pop_back(); |
| 502 | } |
| 503 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 504 | TEST(ThrowingAllocatorTest, InList) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 505 | std::list<ThrowingValue<>, ThrowingAllocator<ThrowingValue<>>> l; |
| 506 | for (int i = 0; i < 20; ++i) l.push_back({}); |
| 507 | for (int i = 0; i < 20; ++i) l.pop_back(); |
| 508 | for (int i = 0; i < 20; ++i) l.push_front({}); |
| 509 | for (int i = 0; i < 20; ++i) l.pop_front(); |
| 510 | } |
| 511 | |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 512 | template <typename TesterInstance, typename = void> |
| 513 | struct NullaryTestValidator : public std::false_type {}; |
| 514 | |
| 515 | template <typename TesterInstance> |
| 516 | struct NullaryTestValidator< |
| 517 | TesterInstance, |
| 518 | absl::void_t<decltype(std::declval<TesterInstance>().Test())>> |
| 519 | : public std::true_type {}; |
| 520 | |
| 521 | template <typename TesterInstance> |
| 522 | bool HasNullaryTest(const TesterInstance&) { |
| 523 | return NullaryTestValidator<TesterInstance>::value; |
| 524 | } |
| 525 | |
| 526 | void DummyOp(void*) {} |
| 527 | |
| 528 | template <typename TesterInstance, typename = void> |
| 529 | struct UnaryTestValidator : public std::false_type {}; |
| 530 | |
| 531 | template <typename TesterInstance> |
| 532 | struct UnaryTestValidator< |
| 533 | TesterInstance, |
| 534 | absl::void_t<decltype(std::declval<TesterInstance>().Test(DummyOp))>> |
| 535 | : public std::true_type {}; |
| 536 | |
| 537 | template <typename TesterInstance> |
| 538 | bool HasUnaryTest(const TesterInstance&) { |
| 539 | return UnaryTestValidator<TesterInstance>::value; |
| 540 | } |
| 541 | |
| 542 | TEST(ExceptionSafetyTesterTest, IncompleteTypesAreNotTestable) { |
| 543 | using T = exceptions_internal::UninitializedT; |
| 544 | auto op = [](T* t) {}; |
| 545 | auto inv = [](T*) { return testing::AssertionSuccess(); }; |
| 546 | auto fac = []() { return absl::make_unique<T>(); }; |
| 547 | |
| 548 | // Test that providing operation and inveriants still does not allow for the |
| 549 | // the invocation of .Test() and .Test(op) because it lacks a factory |
| 550 | auto without_fac = |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 551 | testing::MakeExceptionSafetyTester().WithOperation(op).WithContracts( |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 552 | inv, testing::strong_guarantee); |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 553 | EXPECT_FALSE(HasNullaryTest(without_fac)); |
| 554 | EXPECT_FALSE(HasUnaryTest(without_fac)); |
| 555 | |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 556 | // Test that providing contracts and factory allows the invocation of |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 557 | // .Test(op) but does not allow for .Test() because it lacks an operation |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 558 | auto without_op = testing::MakeExceptionSafetyTester() |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 559 | .WithContracts(inv, testing::strong_guarantee) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 560 | .WithFactory(fac); |
| 561 | EXPECT_FALSE(HasNullaryTest(without_op)); |
| 562 | EXPECT_TRUE(HasUnaryTest(without_op)); |
| 563 | |
| 564 | // Test that providing operation and factory still does not allow for the |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 565 | // the invocation of .Test() and .Test(op) because it lacks contracts |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 566 | auto without_inv = |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 567 | testing::MakeExceptionSafetyTester().WithOperation(op).WithFactory(fac); |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 568 | EXPECT_FALSE(HasNullaryTest(without_inv)); |
| 569 | EXPECT_FALSE(HasUnaryTest(without_inv)); |
| 570 | } |
| 571 | |
| 572 | struct ExampleStruct {}; |
| 573 | |
| 574 | std::unique_ptr<ExampleStruct> ExampleFunctionFactory() { |
| 575 | return absl::make_unique<ExampleStruct>(); |
| 576 | } |
| 577 | |
| 578 | void ExampleFunctionOperation(ExampleStruct*) {} |
| 579 | |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 580 | testing::AssertionResult ExampleFunctionContract(ExampleStruct*) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 581 | return testing::AssertionSuccess(); |
| 582 | } |
| 583 | |
| 584 | struct { |
| 585 | std::unique_ptr<ExampleStruct> operator()() const { |
| 586 | return ExampleFunctionFactory(); |
| 587 | } |
| 588 | } example_struct_factory; |
| 589 | |
| 590 | struct { |
| 591 | void operator()(ExampleStruct*) const {} |
| 592 | } example_struct_operation; |
| 593 | |
| 594 | struct { |
| 595 | testing::AssertionResult operator()(ExampleStruct* example_struct) const { |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 596 | return ExampleFunctionContract(example_struct); |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 597 | } |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 598 | } example_struct_contract; |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 599 | |
| 600 | auto example_lambda_factory = []() { return ExampleFunctionFactory(); }; |
| 601 | |
| 602 | auto example_lambda_operation = [](ExampleStruct*) {}; |
| 603 | |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 604 | auto example_lambda_contract = [](ExampleStruct* example_struct) { |
| 605 | return ExampleFunctionContract(example_struct); |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | // Testing that function references, pointers, structs with operator() and |
| 609 | // lambdas can all be used with ExceptionSafetyTester |
| 610 | TEST(ExceptionSafetyTesterTest, MixedFunctionTypes) { |
| 611 | // function reference |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 612 | EXPECT_TRUE(testing::MakeExceptionSafetyTester() |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 613 | .WithFactory(ExampleFunctionFactory) |
| 614 | .WithOperation(ExampleFunctionOperation) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 615 | .WithContracts(ExampleFunctionContract) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 616 | .Test()); |
| 617 | |
| 618 | // function pointer |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 619 | EXPECT_TRUE(testing::MakeExceptionSafetyTester() |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 620 | .WithFactory(&ExampleFunctionFactory) |
| 621 | .WithOperation(&ExampleFunctionOperation) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 622 | .WithContracts(&ExampleFunctionContract) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 623 | .Test()); |
| 624 | |
| 625 | // struct |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 626 | EXPECT_TRUE(testing::MakeExceptionSafetyTester() |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 627 | .WithFactory(example_struct_factory) |
| 628 | .WithOperation(example_struct_operation) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 629 | .WithContracts(example_struct_contract) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 630 | .Test()); |
| 631 | |
| 632 | // lambda |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 633 | EXPECT_TRUE(testing::MakeExceptionSafetyTester() |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 634 | .WithFactory(example_lambda_factory) |
| 635 | .WithOperation(example_lambda_operation) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 636 | .WithContracts(example_lambda_contract) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 637 | .Test()); |
| 638 | } |
| 639 | |
| 640 | struct NonNegative { |
| 641 | bool operator==(const NonNegative& other) const { return i == other.i; } |
| 642 | int i; |
| 643 | }; |
| 644 | |
| 645 | testing::AssertionResult CheckNonNegativeInvariants(NonNegative* g) { |
| 646 | if (g->i >= 0) { |
| 647 | return testing::AssertionSuccess(); |
| 648 | } |
| 649 | return testing::AssertionFailure() |
| 650 | << "i should be non-negative but is " << g->i; |
| 651 | } |
| 652 | |
| 653 | struct { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 654 | template <typename T> |
| 655 | void operator()(T* t) const { |
| 656 | (*t)(); |
| 657 | } |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 658 | } invoker; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 659 | |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 660 | auto tester = |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 661 | testing::MakeExceptionSafetyTester().WithOperation(invoker).WithContracts( |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 662 | CheckNonNegativeInvariants); |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 663 | auto strong_tester = tester.WithContracts(testing::strong_guarantee); |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 664 | |
| 665 | struct FailsBasicGuarantee : public NonNegative { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 666 | void operator()() { |
| 667 | --i; |
| 668 | ThrowingValue<> bomb; |
| 669 | ++i; |
| 670 | } |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | TEST(ExceptionCheckTest, BasicGuaranteeFailure) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 674 | EXPECT_FALSE(tester.WithInitialValue(FailsBasicGuarantee{}).Test()); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 677 | struct FollowsBasicGuarantee : public NonNegative { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 678 | void operator()() { |
| 679 | ++i; |
| 680 | ThrowingValue<> bomb; |
| 681 | } |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 682 | }; |
| 683 | |
| 684 | TEST(ExceptionCheckTest, BasicGuarantee) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 685 | EXPECT_TRUE(tester.WithInitialValue(FollowsBasicGuarantee{}).Test()); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | TEST(ExceptionCheckTest, StrongGuaranteeFailure) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 689 | EXPECT_FALSE(strong_tester.WithInitialValue(FailsBasicGuarantee{}).Test()); |
| 690 | EXPECT_FALSE(strong_tester.WithInitialValue(FollowsBasicGuarantee{}).Test()); |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 693 | struct BasicGuaranteeWithExtraContracts : public NonNegative { |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 694 | // After operator(), i is incremented. If operator() throws, i is set to 9999 |
| 695 | void operator()() { |
| 696 | int old_i = i; |
| 697 | i = kExceptionSentinel; |
| 698 | ThrowingValue<> bomb; |
| 699 | i = ++old_i; |
| 700 | } |
| 701 | |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 702 | static constexpr int kExceptionSentinel = 9999; |
| 703 | }; |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 704 | constexpr int BasicGuaranteeWithExtraContracts::kExceptionSentinel; |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 705 | |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 706 | TEST(ExceptionCheckTest, BasicGuaranteeWithExtraContracts) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 707 | auto tester_with_val = |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 708 | tester.WithInitialValue(BasicGuaranteeWithExtraContracts{}); |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 709 | EXPECT_TRUE(tester_with_val.Test()); |
| 710 | EXPECT_TRUE( |
| 711 | tester_with_val |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 712 | .WithContracts([](BasicGuaranteeWithExtraContracts* o) { |
| 713 | if (o->i == BasicGuaranteeWithExtraContracts::kExceptionSentinel) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 714 | return testing::AssertionSuccess(); |
| 715 | } |
| 716 | return testing::AssertionFailure() |
| 717 | << "i should be " |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 718 | << BasicGuaranteeWithExtraContracts::kExceptionSentinel |
Abseil Team | 92020a0 | 2018-06-08 08:14:48 -0700 | [diff] [blame] | 719 | << ", but is " << o->i; |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 720 | }) |
| 721 | .Test()); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 724 | struct FollowsStrongGuarantee : public NonNegative { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 725 | void operator()() { ThrowingValue<> bomb; } |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 726 | }; |
| 727 | |
| 728 | TEST(ExceptionCheckTest, StrongGuarantee) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 729 | EXPECT_TRUE(tester.WithInitialValue(FollowsStrongGuarantee{}).Test()); |
| 730 | EXPECT_TRUE(strong_tester.WithInitialValue(FollowsStrongGuarantee{}).Test()); |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 731 | } |
| 732 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 733 | struct HasReset : public NonNegative { |
| 734 | void operator()() { |
| 735 | i = -1; |
| 736 | ThrowingValue<> bomb; |
| 737 | i = 1; |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 740 | void reset() { i = 0; } |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 741 | }; |
| 742 | |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 743 | testing::AssertionResult CheckHasResetContracts(HasReset* h) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 744 | h->reset(); |
| 745 | return testing::AssertionResult(h->i == 0); |
| 746 | } |
| 747 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 748 | TEST(ExceptionCheckTest, ModifyingChecker) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 749 | auto set_to_1000 = [](FollowsBasicGuarantee* g) { |
| 750 | g->i = 1000; |
| 751 | return testing::AssertionSuccess(); |
| 752 | }; |
| 753 | auto is_1000 = [](FollowsBasicGuarantee* g) { |
| 754 | return testing::AssertionResult(g->i == 1000); |
| 755 | }; |
| 756 | auto increment = [](FollowsStrongGuarantee* g) { |
| 757 | ++g->i; |
| 758 | return testing::AssertionSuccess(); |
| 759 | }; |
| 760 | |
| 761 | EXPECT_FALSE(tester.WithInitialValue(FollowsBasicGuarantee{}) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 762 | .WithContracts(set_to_1000, is_1000) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 763 | .Test()); |
| 764 | EXPECT_TRUE(strong_tester.WithInitialValue(FollowsStrongGuarantee{}) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 765 | .WithContracts(increment) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 766 | .Test()); |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 767 | EXPECT_TRUE(testing::MakeExceptionSafetyTester() |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 768 | .WithInitialValue(HasReset{}) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 769 | .WithContracts(CheckHasResetContracts) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 770 | .Test(invoker)); |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Abseil Team | 94c298e | 2018-10-23 18:09:41 -0700 | [diff] [blame] | 773 | TEST(ExceptionSafetyTesterTest, ResetsCountdown) { |
| 774 | auto test = |
| 775 | testing::MakeExceptionSafetyTester() |
| 776 | .WithInitialValue(ThrowingValue<>()) |
| 777 | .WithContracts([](ThrowingValue<>*) { return AssertionSuccess(); }) |
| 778 | .WithOperation([](ThrowingValue<>*) {}); |
| 779 | ASSERT_TRUE(test.Test()); |
| 780 | // If the countdown isn't reset because there were no exceptions thrown, then |
| 781 | // this will fail with a termination from an unhandled exception |
| 782 | EXPECT_TRUE(test.Test()); |
| 783 | } |
| 784 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 785 | struct NonCopyable : public NonNegative { |
| 786 | NonCopyable(const NonCopyable&) = delete; |
| 787 | NonCopyable() : NonNegative{0} {} |
| 788 | |
| 789 | void operator()() { ThrowingValue<> bomb; } |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 790 | }; |
| 791 | |
| 792 | TEST(ExceptionCheckTest, NonCopyable) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 793 | auto factory = []() { return absl::make_unique<NonCopyable>(); }; |
| 794 | EXPECT_TRUE(tester.WithFactory(factory).Test()); |
| 795 | EXPECT_TRUE(strong_tester.WithFactory(factory).Test()); |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 798 | struct NonEqualityComparable : public NonNegative { |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 799 | void operator()() { ThrowingValue<> bomb; } |
| 800 | |
| 801 | void ModifyOnThrow() { |
| 802 | ++i; |
| 803 | ThrowingValue<> bomb; |
| 804 | static_cast<void>(bomb); |
| 805 | --i; |
| 806 | } |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 807 | }; |
| 808 | |
| 809 | TEST(ExceptionCheckTest, NonEqualityComparable) { |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 810 | auto nec_is_strong = [](NonEqualityComparable* nec) { |
| 811 | return testing::AssertionResult(nec->i == NonEqualityComparable().i); |
| 812 | }; |
| 813 | auto strong_nec_tester = tester.WithInitialValue(NonEqualityComparable{}) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 814 | .WithContracts(nec_is_strong); |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 815 | |
| 816 | EXPECT_TRUE(strong_nec_tester.Test()); |
| 817 | EXPECT_FALSE(strong_nec_tester.Test( |
| 818 | [](NonEqualityComparable* n) { n->ModifyOnThrow(); })); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | template <typename T> |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 822 | struct ExhaustivenessTester { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 823 | void operator()() { |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 824 | successes |= 1; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 825 | T b1; |
| 826 | static_cast<void>(b1); |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 827 | successes |= (1 << 1); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 828 | T b2; |
| 829 | static_cast<void>(b2); |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 830 | successes |= (1 << 2); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 831 | T b3; |
| 832 | static_cast<void>(b3); |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 833 | successes |= (1 << 3); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 836 | bool operator==(const ExhaustivenessTester<ThrowingValue<>>&) const { |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 837 | return true; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 840 | static unsigned char successes; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 841 | }; |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 842 | |
| 843 | struct { |
| 844 | template <typename T> |
| 845 | testing::AssertionResult operator()(ExhaustivenessTester<T>*) const { |
| 846 | return testing::AssertionSuccess(); |
| 847 | } |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 848 | } CheckExhaustivenessTesterContracts; |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 849 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 850 | template <typename T> |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 851 | unsigned char ExhaustivenessTester<T>::successes = 0; |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 852 | |
| 853 | TEST(ExceptionCheckTest, Exhaustiveness) { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 854 | auto exhaust_tester = testing::MakeExceptionSafetyTester() |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 855 | .WithContracts(CheckExhaustivenessTesterContracts) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 856 | .WithOperation(invoker); |
| 857 | |
| 858 | EXPECT_TRUE( |
| 859 | exhaust_tester.WithInitialValue(ExhaustivenessTester<int>{}).Test()); |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 860 | EXPECT_EQ(ExhaustivenessTester<int>::successes, 0xF); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 861 | |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 862 | EXPECT_TRUE( |
| 863 | exhaust_tester.WithInitialValue(ExhaustivenessTester<ThrowingValue<>>{}) |
Abseil Team | e01d955 | 2018-09-24 11:57:52 -0700 | [diff] [blame] | 864 | .WithContracts(testing::strong_guarantee) |
Abseil Team | 5b53540 | 2018-04-18 05:56:39 -0700 | [diff] [blame] | 865 | .Test()); |
Abseil Team | 8b727aa | 2017-12-01 12:15:49 -0800 | [diff] [blame] | 866 | EXPECT_EQ(ExhaustivenessTester<ThrowingValue<>>::successes, 0xF); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 869 | struct LeaksIfCtorThrows : private exceptions_internal::TrackedObject { |
| 870 | LeaksIfCtorThrows() : TrackedObject(ABSL_PRETTY_FUNCTION) { |
| 871 | ++counter; |
| 872 | ThrowingValue<> v; |
| 873 | static_cast<void>(v); |
| 874 | --counter; |
| 875 | } |
| 876 | LeaksIfCtorThrows(const LeaksIfCtorThrows&) noexcept |
| 877 | : TrackedObject(ABSL_PRETTY_FUNCTION) {} |
| 878 | static int counter; |
| 879 | }; |
| 880 | int LeaksIfCtorThrows::counter = 0; |
| 881 | |
| 882 | TEST(ExceptionCheckTest, TestLeakyCtor) { |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 883 | testing::TestThrowingCtor<LeaksIfCtorThrows>(); |
Abseil Team | ae0cef3 | 2017-11-22 07:42:54 -0800 | [diff] [blame] | 884 | EXPECT_EQ(LeaksIfCtorThrows::counter, 1); |
| 885 | LeaksIfCtorThrows::counter = 0; |
| 886 | } |
| 887 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 888 | struct Tracked : private exceptions_internal::TrackedObject { |
| 889 | Tracked() : TrackedObject(ABSL_PRETTY_FUNCTION) {} |
| 890 | }; |
| 891 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 892 | TEST(ConstructorTrackerTest, CreatedBefore) { |
| 893 | Tracked a, b, c; |
| 894 | exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 897 | TEST(ConstructorTrackerTest, CreatedAfter) { |
| 898 | exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown); |
| 899 | Tracked a, b, c; |
| 900 | } |
| 901 | |
| 902 | TEST(ConstructorTrackerTest, NotDestroyedAfter) { |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 903 | absl::aligned_storage_t<sizeof(Tracked), alignof(Tracked)> storage; |
| 904 | EXPECT_NONFATAL_FAILURE( |
| 905 | { |
Abseil Team | 28f5b89 | 2018-04-26 06:47:58 -0700 | [diff] [blame] | 906 | exceptions_internal::ConstructorTracker ct( |
| 907 | exceptions_internal::countdown); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 908 | new (&storage) Tracked; |
| 909 | }, |
| 910 | "not destroyed"); |
| 911 | } |
| 912 | |
Abseil Team | 70b5fa9 | 2018-03-26 15:15:40 -0700 | [diff] [blame] | 913 | TEST(ConstructorTrackerTest, DestroyedTwice) { |
Abseil Team | 92020a0 | 2018-06-08 08:14:48 -0700 | [diff] [blame] | 914 | exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 915 | EXPECT_NONFATAL_FAILURE( |
| 916 | { |
| 917 | Tracked t; |
| 918 | t.~Tracked(); |
| 919 | }, |
Abseil Team | 92020a0 | 2018-06-08 08:14:48 -0700 | [diff] [blame] | 920 | "re-destroyed"); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Abseil Team | 70b5fa9 | 2018-03-26 15:15:40 -0700 | [diff] [blame] | 923 | TEST(ConstructorTrackerTest, ConstructedTwice) { |
Abseil Team | 92020a0 | 2018-06-08 08:14:48 -0700 | [diff] [blame] | 924 | exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 925 | absl::aligned_storage_t<sizeof(Tracked), alignof(Tracked)> storage; |
| 926 | EXPECT_NONFATAL_FAILURE( |
| 927 | { |
| 928 | new (&storage) Tracked; |
| 929 | new (&storage) Tracked; |
Abseil Team | 92020a0 | 2018-06-08 08:14:48 -0700 | [diff] [blame] | 930 | reinterpret_cast<Tracked*>(&storage)->~Tracked(); |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 931 | }, |
| 932 | "re-constructed"); |
| 933 | } |
Abseil Team | eed36bf | 2018-03-15 13:48:55 -0700 | [diff] [blame] | 934 | |
| 935 | TEST(ThrowingValueTraitsTest, RelationalOperators) { |
| 936 | ThrowingValue<> a, b; |
| 937 | EXPECT_TRUE((std::is_convertible<decltype(a == b), bool>::value)); |
| 938 | EXPECT_TRUE((std::is_convertible<decltype(a != b), bool>::value)); |
| 939 | EXPECT_TRUE((std::is_convertible<decltype(a < b), bool>::value)); |
| 940 | EXPECT_TRUE((std::is_convertible<decltype(a <= b), bool>::value)); |
| 941 | EXPECT_TRUE((std::is_convertible<decltype(a > b), bool>::value)); |
| 942 | EXPECT_TRUE((std::is_convertible<decltype(a >= b), bool>::value)); |
| 943 | } |
| 944 | |
Abseil Team | 70b5fa9 | 2018-03-26 15:15:40 -0700 | [diff] [blame] | 945 | TEST(ThrowingAllocatorTraitsTest, Assignablility) { |
Abseil Team | 083d04d | 2018-08-07 11:43:34 -0700 | [diff] [blame] | 946 | EXPECT_TRUE(absl::is_move_assignable<ThrowingAllocator<int>>::value); |
| 947 | EXPECT_TRUE(absl::is_copy_assignable<ThrowingAllocator<int>>::value); |
Abseil Team | 70b5fa9 | 2018-03-26 15:15:40 -0700 | [diff] [blame] | 948 | EXPECT_TRUE(std::is_nothrow_move_assignable<ThrowingAllocator<int>>::value); |
| 949 | EXPECT_TRUE(std::is_nothrow_copy_assignable<ThrowingAllocator<int>>::value); |
| 950 | } |
| 951 | |
Abseil Team | 8db6cfd | 2017-10-30 15:55:37 -0700 | [diff] [blame] | 952 | } // namespace |
Abseil Team | 9613678 | 2018-04-30 11:44:26 -0700 | [diff] [blame] | 953 | |
| 954 | } // namespace testing |