2017-12-20 21:34:46 +01:00
|
|
|
// Copyright 2017 The Abseil Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
#include "absl/base/internal/exception_safety_testing.h"
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
2018-03-15 21:48:55 +01:00
|
|
|
#include <type_traits>
|
2017-10-30 23:55:37 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "gtest/gtest-spi.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "absl/memory/memory.h"
|
|
|
|
|
2018-04-30 20:44:26 +02:00
|
|
|
namespace testing {
|
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
namespace {
|
2018-04-30 20:44:26 +02:00
|
|
|
|
|
|
|
using ::testing::exceptions_internal::SetCountdown;
|
|
|
|
using ::testing::exceptions_internal::TestException;
|
|
|
|
using ::testing::exceptions_internal::UnsetCountdown;
|
2017-10-30 23:55:37 +01:00
|
|
|
|
|
|
|
// EXPECT_NO_THROW can't inspect the thrown inspection in general.
|
|
|
|
template <typename F>
|
|
|
|
void ExpectNoThrow(const F& f) {
|
|
|
|
try {
|
|
|
|
f();
|
|
|
|
} catch (TestException e) {
|
|
|
|
ADD_FAILURE() << "Unexpected exception thrown from " << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, Throws) {
|
2017-10-30 23:55:37 +01:00
|
|
|
SetCountdown();
|
|
|
|
EXPECT_THROW(ThrowingValue<> bomb, TestException);
|
|
|
|
|
|
|
|
// It's not guaranteed that every operator only throws *once*. The default
|
|
|
|
// ctor only throws once, though, so use it to make sure we only throw when
|
|
|
|
// the countdown hits 0
|
2018-04-20 18:16:52 +02:00
|
|
|
SetCountdown(2);
|
2017-10-30 23:55:37 +01:00
|
|
|
ExpectNoThrow([]() { ThrowingValue<> bomb; });
|
|
|
|
ExpectNoThrow([]() { ThrowingValue<> bomb; });
|
|
|
|
EXPECT_THROW(ThrowingValue<> bomb, TestException);
|
2018-04-26 15:47:58 +02:00
|
|
|
|
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that an operation throws when the countdown is at 0, doesn't throw when
|
|
|
|
// the countdown doesn't hit 0, and doesn't modify the state of the
|
|
|
|
// ThrowingValue if it throws
|
|
|
|
template <typename F>
|
2018-01-05 16:54:33 +01:00
|
|
|
void TestOp(const F& f) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ExpectNoThrow(f);
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
EXPECT_THROW(f(), TestException);
|
|
|
|
UnsetCountdown();
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingCtors) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb;
|
|
|
|
|
|
|
|
TestOp([]() { ThrowingValue<> bomb(1); });
|
|
|
|
TestOp([&]() { ThrowingValue<> bomb1 = bomb; });
|
|
|
|
TestOp([&]() { ThrowingValue<> bomb1 = std::move(bomb); });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingAssignment) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb, bomb1;
|
|
|
|
|
|
|
|
TestOp([&]() { bomb = bomb1; });
|
|
|
|
TestOp([&]() { bomb = std::move(bomb1); });
|
2018-05-04 18:58:56 +02:00
|
|
|
|
|
|
|
// Test that when assignment throws, the assignment should fail (lhs != rhs)
|
|
|
|
// and strong guarantee fails (lhs != lhs_copy).
|
|
|
|
{
|
|
|
|
ThrowingValue<> lhs(39), rhs(42);
|
|
|
|
ThrowingValue<> lhs_copy(lhs);
|
|
|
|
SetCountdown();
|
|
|
|
EXPECT_THROW(lhs = rhs, TestException);
|
|
|
|
UnsetCountdown();
|
|
|
|
EXPECT_NE(lhs, rhs);
|
|
|
|
EXPECT_NE(lhs_copy, lhs);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ThrowingValue<> lhs(39), rhs(42);
|
|
|
|
ThrowingValue<> lhs_copy(lhs), rhs_copy(rhs);
|
|
|
|
SetCountdown();
|
|
|
|
EXPECT_THROW(lhs = std::move(rhs), TestException);
|
|
|
|
UnsetCountdown();
|
|
|
|
EXPECT_NE(lhs, rhs_copy);
|
|
|
|
EXPECT_NE(lhs_copy, lhs);
|
|
|
|
}
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingComparisons) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb1, bomb2;
|
|
|
|
TestOp([&]() { return bomb1 == bomb2; });
|
|
|
|
TestOp([&]() { return bomb1 != bomb2; });
|
|
|
|
TestOp([&]() { return bomb1 < bomb2; });
|
|
|
|
TestOp([&]() { return bomb1 <= bomb2; });
|
|
|
|
TestOp([&]() { return bomb1 > bomb2; });
|
|
|
|
TestOp([&]() { return bomb1 >= bomb2; });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingArithmeticOps) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb1(1), bomb2(2);
|
|
|
|
|
|
|
|
TestOp([&bomb1]() { +bomb1; });
|
|
|
|
TestOp([&bomb1]() { -bomb1; });
|
|
|
|
TestOp([&bomb1]() { ++bomb1; });
|
|
|
|
TestOp([&bomb1]() { bomb1++; });
|
|
|
|
TestOp([&bomb1]() { --bomb1; });
|
|
|
|
TestOp([&bomb1]() { bomb1--; });
|
|
|
|
|
|
|
|
TestOp([&]() { bomb1 + bomb2; });
|
|
|
|
TestOp([&]() { bomb1 - bomb2; });
|
|
|
|
TestOp([&]() { bomb1* bomb2; });
|
|
|
|
TestOp([&]() { bomb1 / bomb2; });
|
|
|
|
TestOp([&]() { bomb1 << 1; });
|
|
|
|
TestOp([&]() { bomb1 >> 1; });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingLogicalOps) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb1, bomb2;
|
|
|
|
|
|
|
|
TestOp([&bomb1]() { !bomb1; });
|
|
|
|
TestOp([&]() { bomb1&& bomb2; });
|
|
|
|
TestOp([&]() { bomb1 || bomb2; });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingBitwiseOps) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb1, bomb2;
|
|
|
|
|
|
|
|
TestOp([&bomb1]() { ~bomb1; });
|
|
|
|
TestOp([&]() { bomb1& bomb2; });
|
|
|
|
TestOp([&]() { bomb1 | bomb2; });
|
|
|
|
TestOp([&]() { bomb1 ^ bomb2; });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingCompoundAssignmentOps) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb1(1), bomb2(2);
|
|
|
|
|
|
|
|
TestOp([&]() { bomb1 += bomb2; });
|
|
|
|
TestOp([&]() { bomb1 -= bomb2; });
|
|
|
|
TestOp([&]() { bomb1 *= bomb2; });
|
|
|
|
TestOp([&]() { bomb1 /= bomb2; });
|
|
|
|
TestOp([&]() { bomb1 %= bomb2; });
|
|
|
|
TestOp([&]() { bomb1 &= bomb2; });
|
|
|
|
TestOp([&]() { bomb1 |= bomb2; });
|
|
|
|
TestOp([&]() { bomb1 ^= bomb2; });
|
|
|
|
TestOp([&]() { bomb1 *= bomb2; });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingStreamOps) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb;
|
|
|
|
|
2018-06-08 17:14:48 +02:00
|
|
|
TestOp([&]() {
|
|
|
|
std::istringstream stream;
|
|
|
|
stream >> bomb;
|
|
|
|
});
|
|
|
|
TestOp([&]() {
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << bomb;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests the operator<< of ThrowingValue by forcing ConstructorTracker to emit
|
|
|
|
// a nonfatal failure that contains the std::string representation of the Thrower
|
|
|
|
TEST(ThrowingValueTest, StreamOpsOutput) {
|
|
|
|
using ::testing::TypeSpec;
|
|
|
|
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
|
|
|
|
|
|
|
// Test default spec list (kEverythingThrows)
|
|
|
|
EXPECT_NONFATAL_FAILURE(
|
|
|
|
{
|
|
|
|
using Thrower = ThrowingValue<TypeSpec{}>;
|
|
|
|
auto thrower = Thrower(123);
|
|
|
|
thrower.~Thrower();
|
|
|
|
},
|
|
|
|
"ThrowingValue<>(123)");
|
|
|
|
|
|
|
|
// Test with one item in spec list (kNoThrowCopy)
|
|
|
|
EXPECT_NONFATAL_FAILURE(
|
|
|
|
{
|
|
|
|
using Thrower = ThrowingValue<TypeSpec::kNoThrowCopy>;
|
|
|
|
auto thrower = Thrower(234);
|
|
|
|
thrower.~Thrower();
|
|
|
|
},
|
|
|
|
"ThrowingValue<kNoThrowCopy>(234)");
|
|
|
|
|
|
|
|
// Test with multiple items in spec list (kNoThrowMove, kNoThrowNew)
|
|
|
|
EXPECT_NONFATAL_FAILURE(
|
|
|
|
{
|
|
|
|
using Thrower =
|
|
|
|
ThrowingValue<TypeSpec::kNoThrowMove | TypeSpec::kNoThrowNew>;
|
|
|
|
auto thrower = Thrower(345);
|
|
|
|
thrower.~Thrower();
|
|
|
|
},
|
|
|
|
"ThrowingValue<kNoThrowMove | kNoThrowNew>(345)");
|
|
|
|
|
|
|
|
// Test with all items in spec list (kNoThrowCopy, kNoThrowMove, kNoThrowNew)
|
|
|
|
EXPECT_NONFATAL_FAILURE(
|
|
|
|
{
|
|
|
|
using Thrower = ThrowingValue<static_cast<TypeSpec>(-1)>;
|
|
|
|
auto thrower = Thrower(456);
|
|
|
|
thrower.~Thrower();
|
|
|
|
},
|
|
|
|
"ThrowingValue<kNoThrowCopy | kNoThrowMove | kNoThrowNew>(456)");
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 16:54:33 +01:00
|
|
|
template <typename F>
|
|
|
|
void TestAllocatingOp(const F& f) {
|
|
|
|
ExpectNoThrow(f);
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
EXPECT_THROW(f(), exceptions_internal::TestBadAllocException);
|
|
|
|
UnsetCountdown();
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingAllocatingOps) {
|
2017-10-30 23:55:37 +01:00
|
|
|
// make_unique calls unqualified operator new, so these exercise the
|
|
|
|
// ThrowingValue overloads.
|
2018-01-05 16:54:33 +01:00
|
|
|
TestAllocatingOp([]() { return absl::make_unique<ThrowingValue<>>(1); });
|
|
|
|
TestAllocatingOp([]() { return absl::make_unique<ThrowingValue<>[]>(2); });
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, NonThrowingMoveCtor) {
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingValue<TypeSpec::kNoThrowMove> nothrow_ctor;
|
2017-10-30 23:55:37 +01:00
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([¬hrow_ctor]() {
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingValue<TypeSpec::kNoThrowMove> nothrow1 = std::move(nothrow_ctor);
|
2017-10-30 23:55:37 +01:00
|
|
|
});
|
2018-04-26 15:47:58 +02:00
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, NonThrowingMoveAssign) {
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingValue<TypeSpec::kNoThrowMove> nothrow_assign1, nothrow_assign2;
|
2017-10-30 23:55:37 +01:00
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([¬hrow_assign1, ¬hrow_assign2]() {
|
|
|
|
nothrow_assign1 = std::move(nothrow_assign2);
|
|
|
|
});
|
2018-04-26 15:47:58 +02:00
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-30 20:44:26 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingCopyCtor) {
|
|
|
|
ThrowingValue<> tv;
|
|
|
|
|
|
|
|
TestOp([&]() { ThrowingValue<> tv_copy(tv); });
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ThrowingValueTest, ThrowingCopyAssign) {
|
|
|
|
ThrowingValue<> tv1, tv2;
|
|
|
|
|
|
|
|
TestOp([&]() { tv1 = tv2; });
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ThrowingValueTest, NonThrowingCopyCtor) {
|
|
|
|
ThrowingValue<TypeSpec::kNoThrowCopy> nothrow_ctor;
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([¬hrow_ctor]() {
|
|
|
|
ThrowingValue<TypeSpec::kNoThrowCopy> nothrow1(nothrow_ctor);
|
|
|
|
});
|
|
|
|
UnsetCountdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ThrowingValueTest, NonThrowingCopyAssign) {
|
|
|
|
ThrowingValue<TypeSpec::kNoThrowCopy> nothrow_assign1, nothrow_assign2;
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([¬hrow_assign1, ¬hrow_assign2]() {
|
|
|
|
nothrow_assign1 = nothrow_assign2;
|
|
|
|
});
|
|
|
|
UnsetCountdown();
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, ThrowingSwap) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<> bomb1, bomb2;
|
|
|
|
TestOp([&]() { std::swap(bomb1, bomb2); });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, NonThrowingSwap) {
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingValue<TypeSpec::kNoThrowMove> bomb1, bomb2;
|
2017-10-30 23:55:37 +01:00
|
|
|
ExpectNoThrow([&]() { std::swap(bomb1, bomb2); });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, NonThrowingAllocation) {
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingValue<TypeSpec::kNoThrowNew>* allocated;
|
|
|
|
ThrowingValue<TypeSpec::kNoThrowNew>* array;
|
2017-10-30 23:55:37 +01:00
|
|
|
|
|
|
|
ExpectNoThrow([&allocated]() {
|
2018-04-30 20:44:26 +02:00
|
|
|
allocated = new ThrowingValue<TypeSpec::kNoThrowNew>(1);
|
2017-10-30 23:55:37 +01:00
|
|
|
delete allocated;
|
|
|
|
});
|
|
|
|
ExpectNoThrow([&array]() {
|
2018-04-30 20:44:26 +02:00
|
|
|
array = new ThrowingValue<TypeSpec::kNoThrowNew>[2];
|
2017-10-30 23:55:37 +01:00
|
|
|
delete[] array;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, NonThrowingDelete) {
|
2017-10-30 23:55:37 +01:00
|
|
|
auto* allocated = new ThrowingValue<>(1);
|
|
|
|
auto* array = new ThrowingValue<>[2];
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([allocated]() { delete allocated; });
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([array]() { delete[] array; });
|
2018-04-26 15:47:58 +02:00
|
|
|
|
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
using Storage =
|
|
|
|
absl::aligned_storage_t<sizeof(ThrowingValue<>), alignof(ThrowingValue<>)>;
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, NonThrowingPlacementDelete) {
|
2017-10-30 23:55:37 +01:00
|
|
|
constexpr int kArrayLen = 2;
|
|
|
|
// We intentionally create extra space to store the tag allocated by placement
|
|
|
|
// new[].
|
|
|
|
constexpr int kStorageLen = 4;
|
|
|
|
|
|
|
|
Storage buf;
|
|
|
|
Storage array_buf[kStorageLen];
|
|
|
|
auto* placed = new (&buf) ThrowingValue<>(1);
|
|
|
|
auto placed_array = new (&array_buf) ThrowingValue<>[kArrayLen];
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([placed, &buf]() {
|
|
|
|
placed->~ThrowingValue<>();
|
|
|
|
ThrowingValue<>::operator delete(placed, &buf);
|
|
|
|
});
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([&, placed_array]() {
|
|
|
|
for (int i = 0; i < kArrayLen; ++i) placed_array[i].~ThrowingValue<>();
|
|
|
|
ThrowingValue<>::operator delete[](placed_array, &array_buf);
|
|
|
|
});
|
2018-04-26 15:47:58 +02:00
|
|
|
|
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingValueTest, NonThrowingDestructor) {
|
2017-10-30 23:55:37 +01:00
|
|
|
auto* allocated = new ThrowingValue<>();
|
2018-04-26 15:47:58 +02:00
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([allocated]() { delete allocated; });
|
2018-04-26 15:47:58 +02:00
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ThrowingBoolTest, ThrowingBool) {
|
|
|
|
ThrowingBool t = true;
|
|
|
|
|
|
|
|
// Test that it's contextually convertible to bool
|
|
|
|
if (t) { // NOLINT(whitespace/empty_if_body)
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(t);
|
|
|
|
|
|
|
|
TestOp([&]() { (void)!t; });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingAllocatorTest, MemoryManagement) {
|
2017-10-30 23:55:37 +01:00
|
|
|
// Just exercise the memory management capabilities under LSan to make sure we
|
|
|
|
// don't leak.
|
|
|
|
ThrowingAllocator<int> int_alloc;
|
|
|
|
int* ip = int_alloc.allocate(1);
|
|
|
|
int_alloc.deallocate(ip, 1);
|
|
|
|
int* i_array = int_alloc.allocate(2);
|
|
|
|
int_alloc.deallocate(i_array, 2);
|
|
|
|
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingAllocator<ThrowingValue<>> tv_alloc;
|
|
|
|
ThrowingValue<>* ptr = tv_alloc.allocate(1);
|
|
|
|
tv_alloc.deallocate(ptr, 1);
|
|
|
|
ThrowingValue<>* tv_array = tv_alloc.allocate(2);
|
|
|
|
tv_alloc.deallocate(tv_array, 2);
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingAllocatorTest, CallsGlobalNew) {
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingAllocator<ThrowingValue<>, AllocSpec::kNoThrowAllocate> nothrow_alloc;
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingValue<>* ptr;
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
// This will only throw if ThrowingValue::new is called.
|
|
|
|
ExpectNoThrow([&]() { ptr = nothrow_alloc.allocate(1); });
|
|
|
|
nothrow_alloc.deallocate(ptr, 1);
|
2018-04-26 15:47:58 +02:00
|
|
|
|
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingAllocatorTest, ThrowingConstructors) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingAllocator<int> int_alloc;
|
|
|
|
int* ip = nullptr;
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
EXPECT_THROW(ip = int_alloc.allocate(1), TestException);
|
|
|
|
ExpectNoThrow([&]() { ip = int_alloc.allocate(1); });
|
|
|
|
|
|
|
|
*ip = 1;
|
|
|
|
SetCountdown();
|
|
|
|
EXPECT_THROW(int_alloc.construct(ip, 2), TestException);
|
|
|
|
EXPECT_EQ(*ip, 1);
|
|
|
|
int_alloc.deallocate(ip, 1);
|
2018-04-26 15:47:58 +02:00
|
|
|
|
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingAllocatorTest, NonThrowingConstruction) {
|
2017-10-30 23:55:37 +01:00
|
|
|
{
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingAllocator<int, AllocSpec::kNoThrowAllocate> int_alloc;
|
2017-10-30 23:55:37 +01:00
|
|
|
int* ip = nullptr;
|
|
|
|
|
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([&]() { ip = int_alloc.allocate(1); });
|
2018-04-26 15:47:58 +02:00
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([&]() { int_alloc.construct(ip, 2); });
|
2018-04-26 15:47:58 +02:00
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
EXPECT_EQ(*ip, 2);
|
|
|
|
int_alloc.deallocate(ip, 1);
|
2018-04-26 15:47:58 +02:00
|
|
|
|
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ThrowingAllocator<int> int_alloc;
|
|
|
|
int* ip = nullptr;
|
|
|
|
ExpectNoThrow([&]() { ip = int_alloc.allocate(1); });
|
|
|
|
ExpectNoThrow([&]() { int_alloc.construct(ip, 2); });
|
|
|
|
EXPECT_EQ(*ip, 2);
|
|
|
|
int_alloc.deallocate(ip, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-04-30 20:44:26 +02:00
|
|
|
ThrowingAllocator<ThrowingValue<>, AllocSpec::kNoThrowAllocate>
|
|
|
|
nothrow_alloc;
|
|
|
|
ThrowingValue<>* ptr;
|
2018-04-26 15:47:58 +02:00
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
SetCountdown();
|
2018-04-30 20:44:26 +02:00
|
|
|
ExpectNoThrow([&]() { ptr = nothrow_alloc.allocate(1); });
|
2018-04-26 15:47:58 +02:00
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
SetCountdown();
|
2018-04-30 20:44:26 +02:00
|
|
|
ExpectNoThrow(
|
2018-05-14 22:41:38 +02:00
|
|
|
[&]() { nothrow_alloc.construct(ptr, 2, testing::nothrow_ctor); });
|
2018-04-26 15:47:58 +02:00
|
|
|
|
2018-04-30 20:44:26 +02:00
|
|
|
EXPECT_EQ(ptr->Get(), 2);
|
|
|
|
nothrow_alloc.destroy(ptr);
|
|
|
|
nothrow_alloc.deallocate(ptr, 1);
|
2018-04-26 15:47:58 +02:00
|
|
|
|
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ThrowingAllocator<int> a;
|
2018-04-26 15:47:58 +02:00
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([&]() { ThrowingAllocator<double> a1 = a; });
|
2018-04-26 15:47:58 +02:00
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
SetCountdown();
|
|
|
|
ExpectNoThrow([&]() { ThrowingAllocator<double> a1 = std::move(a); });
|
2018-04-26 15:47:58 +02:00
|
|
|
|
|
|
|
UnsetCountdown();
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingAllocatorTest, ThrowingAllocatorConstruction) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingAllocator<int> a;
|
|
|
|
TestOp([]() { ThrowingAllocator<int> a; });
|
|
|
|
TestOp([&]() { a.select_on_container_copy_construction(); });
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingAllocatorTest, State) {
|
2017-10-30 23:55:37 +01:00
|
|
|
ThrowingAllocator<int> a1, a2;
|
|
|
|
EXPECT_NE(a1, a2);
|
|
|
|
|
|
|
|
auto a3 = a1;
|
|
|
|
EXPECT_EQ(a3, a1);
|
|
|
|
int* ip = a1.allocate(1);
|
|
|
|
EXPECT_EQ(a3, a1);
|
|
|
|
a3.deallocate(ip, 1);
|
|
|
|
EXPECT_EQ(a3, a1);
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingAllocatorTest, InVector) {
|
2017-10-30 23:55:37 +01:00
|
|
|
std::vector<ThrowingValue<>, ThrowingAllocator<ThrowingValue<>>> v;
|
|
|
|
for (int i = 0; i < 20; ++i) v.push_back({});
|
|
|
|
for (int i = 0; i < 20; ++i) v.pop_back();
|
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ThrowingAllocatorTest, InList) {
|
2017-10-30 23:55:37 +01:00
|
|
|
std::list<ThrowingValue<>, ThrowingAllocator<ThrowingValue<>>> l;
|
|
|
|
for (int i = 0; i < 20; ++i) l.push_back({});
|
|
|
|
for (int i = 0; i < 20; ++i) l.pop_back();
|
|
|
|
for (int i = 0; i < 20; ++i) l.push_front({});
|
|
|
|
for (int i = 0; i < 20; ++i) l.pop_front();
|
|
|
|
}
|
|
|
|
|
2018-04-18 14:56:39 +02:00
|
|
|
template <typename TesterInstance, typename = void>
|
|
|
|
struct NullaryTestValidator : public std::false_type {};
|
|
|
|
|
|
|
|
template <typename TesterInstance>
|
|
|
|
struct NullaryTestValidator<
|
|
|
|
TesterInstance,
|
|
|
|
absl::void_t<decltype(std::declval<TesterInstance>().Test())>>
|
|
|
|
: public std::true_type {};
|
|
|
|
|
|
|
|
template <typename TesterInstance>
|
|
|
|
bool HasNullaryTest(const TesterInstance&) {
|
|
|
|
return NullaryTestValidator<TesterInstance>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DummyOp(void*) {}
|
|
|
|
|
|
|
|
template <typename TesterInstance, typename = void>
|
|
|
|
struct UnaryTestValidator : public std::false_type {};
|
|
|
|
|
|
|
|
template <typename TesterInstance>
|
|
|
|
struct UnaryTestValidator<
|
|
|
|
TesterInstance,
|
|
|
|
absl::void_t<decltype(std::declval<TesterInstance>().Test(DummyOp))>>
|
|
|
|
: public std::true_type {};
|
|
|
|
|
|
|
|
template <typename TesterInstance>
|
|
|
|
bool HasUnaryTest(const TesterInstance&) {
|
|
|
|
return UnaryTestValidator<TesterInstance>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ExceptionSafetyTesterTest, IncompleteTypesAreNotTestable) {
|
|
|
|
using T = exceptions_internal::UninitializedT;
|
|
|
|
auto op = [](T* t) {};
|
|
|
|
auto inv = [](T*) { return testing::AssertionSuccess(); };
|
|
|
|
auto fac = []() { return absl::make_unique<T>(); };
|
|
|
|
|
|
|
|
// Test that providing operation and inveriants still does not allow for the
|
|
|
|
// the invocation of .Test() and .Test(op) because it lacks a factory
|
|
|
|
auto without_fac =
|
2018-04-30 20:44:26 +02:00
|
|
|
testing::MakeExceptionSafetyTester().WithOperation(op).WithInvariants(
|
|
|
|
inv, testing::strong_guarantee);
|
2018-04-18 14:56:39 +02:00
|
|
|
EXPECT_FALSE(HasNullaryTest(without_fac));
|
|
|
|
EXPECT_FALSE(HasUnaryTest(without_fac));
|
|
|
|
|
|
|
|
// Test that providing invariants and factory allows the invocation of
|
|
|
|
// .Test(op) but does not allow for .Test() because it lacks an operation
|
2018-04-30 20:44:26 +02:00
|
|
|
auto without_op = testing::MakeExceptionSafetyTester()
|
|
|
|
.WithInvariants(inv, testing::strong_guarantee)
|
2018-04-18 14:56:39 +02:00
|
|
|
.WithFactory(fac);
|
|
|
|
EXPECT_FALSE(HasNullaryTest(without_op));
|
|
|
|
EXPECT_TRUE(HasUnaryTest(without_op));
|
|
|
|
|
|
|
|
// Test that providing operation and factory still does not allow for the
|
|
|
|
// the invocation of .Test() and .Test(op) because it lacks invariants
|
|
|
|
auto without_inv =
|
2018-04-30 20:44:26 +02:00
|
|
|
testing::MakeExceptionSafetyTester().WithOperation(op).WithFactory(fac);
|
2018-04-18 14:56:39 +02:00
|
|
|
EXPECT_FALSE(HasNullaryTest(without_inv));
|
|
|
|
EXPECT_FALSE(HasUnaryTest(without_inv));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ExampleStruct {};
|
|
|
|
|
|
|
|
std::unique_ptr<ExampleStruct> ExampleFunctionFactory() {
|
|
|
|
return absl::make_unique<ExampleStruct>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExampleFunctionOperation(ExampleStruct*) {}
|
|
|
|
|
|
|
|
testing::AssertionResult ExampleFunctionInvariant(ExampleStruct*) {
|
|
|
|
return testing::AssertionSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
|
|
|
std::unique_ptr<ExampleStruct> operator()() const {
|
|
|
|
return ExampleFunctionFactory();
|
|
|
|
}
|
|
|
|
} example_struct_factory;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
void operator()(ExampleStruct*) const {}
|
|
|
|
} example_struct_operation;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
testing::AssertionResult operator()(ExampleStruct* example_struct) const {
|
|
|
|
return ExampleFunctionInvariant(example_struct);
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
2018-04-18 14:56:39 +02:00
|
|
|
} example_struct_invariant;
|
|
|
|
|
|
|
|
auto example_lambda_factory = []() { return ExampleFunctionFactory(); };
|
|
|
|
|
|
|
|
auto example_lambda_operation = [](ExampleStruct*) {};
|
|
|
|
|
|
|
|
auto example_lambda_invariant = [](ExampleStruct* example_struct) {
|
|
|
|
return ExampleFunctionInvariant(example_struct);
|
2017-10-30 23:55:37 +01:00
|
|
|
};
|
|
|
|
|
2018-04-18 14:56:39 +02:00
|
|
|
// Testing that function references, pointers, structs with operator() and
|
|
|
|
// lambdas can all be used with ExceptionSafetyTester
|
|
|
|
TEST(ExceptionSafetyTesterTest, MixedFunctionTypes) {
|
|
|
|
// function reference
|
2018-04-30 20:44:26 +02:00
|
|
|
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
2018-04-18 14:56:39 +02:00
|
|
|
.WithFactory(ExampleFunctionFactory)
|
|
|
|
.WithOperation(ExampleFunctionOperation)
|
|
|
|
.WithInvariants(ExampleFunctionInvariant)
|
|
|
|
.Test());
|
|
|
|
|
|
|
|
// function pointer
|
2018-04-30 20:44:26 +02:00
|
|
|
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
2018-04-18 14:56:39 +02:00
|
|
|
.WithFactory(&ExampleFunctionFactory)
|
|
|
|
.WithOperation(&ExampleFunctionOperation)
|
|
|
|
.WithInvariants(&ExampleFunctionInvariant)
|
|
|
|
.Test());
|
|
|
|
|
|
|
|
// struct
|
2018-04-30 20:44:26 +02:00
|
|
|
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
2018-04-18 14:56:39 +02:00
|
|
|
.WithFactory(example_struct_factory)
|
|
|
|
.WithOperation(example_struct_operation)
|
|
|
|
.WithInvariants(example_struct_invariant)
|
|
|
|
.Test());
|
|
|
|
|
|
|
|
// lambda
|
2018-04-30 20:44:26 +02:00
|
|
|
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
2018-04-18 14:56:39 +02:00
|
|
|
.WithFactory(example_lambda_factory)
|
|
|
|
.WithOperation(example_lambda_operation)
|
|
|
|
.WithInvariants(example_lambda_invariant)
|
|
|
|
.Test());
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
struct NonNegative {
|
|
|
|
bool operator==(const NonNegative& other) const { return i == other.i; }
|
|
|
|
int i;
|
|
|
|
};
|
|
|
|
|
2018-04-18 14:56:39 +02:00
|
|
|
testing::AssertionResult CheckNonNegativeInvariants(NonNegative* g) {
|
|
|
|
if (g->i >= 0) {
|
|
|
|
return testing::AssertionSuccess();
|
|
|
|
}
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "i should be non-negative but is " << g->i;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
|
|
|
template <typename T>
|
|
|
|
void operator()(T* t) const {
|
|
|
|
(*t)();
|
|
|
|
}
|
|
|
|
} invoker;
|
|
|
|
|
|
|
|
auto tester =
|
2018-04-30 20:44:26 +02:00
|
|
|
testing::MakeExceptionSafetyTester().WithOperation(invoker).WithInvariants(
|
2018-04-18 14:56:39 +02:00
|
|
|
CheckNonNegativeInvariants);
|
2018-04-30 20:44:26 +02:00
|
|
|
auto strong_tester = tester.WithInvariants(testing::strong_guarantee);
|
2017-12-01 21:15:49 +01:00
|
|
|
|
|
|
|
struct FailsBasicGuarantee : public NonNegative {
|
2017-10-30 23:55:37 +01:00
|
|
|
void operator()() {
|
|
|
|
--i;
|
|
|
|
ThrowingValue<> bomb;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ExceptionCheckTest, BasicGuaranteeFailure) {
|
2018-04-18 14:56:39 +02:00
|
|
|
EXPECT_FALSE(tester.WithInitialValue(FailsBasicGuarantee{}).Test());
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
struct FollowsBasicGuarantee : public NonNegative {
|
2017-10-30 23:55:37 +01:00
|
|
|
void operator()() {
|
|
|
|
++i;
|
|
|
|
ThrowingValue<> bomb;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ExceptionCheckTest, BasicGuarantee) {
|
2018-04-18 14:56:39 +02:00
|
|
|
EXPECT_TRUE(tester.WithInitialValue(FollowsBasicGuarantee{}).Test());
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ExceptionCheckTest, StrongGuaranteeFailure) {
|
2018-04-18 14:56:39 +02:00
|
|
|
EXPECT_FALSE(strong_tester.WithInitialValue(FailsBasicGuarantee{}).Test());
|
|
|
|
EXPECT_FALSE(strong_tester.WithInitialValue(FollowsBasicGuarantee{}).Test());
|
2017-11-22 16:42:54 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
struct BasicGuaranteeWithExtraInvariants : public NonNegative {
|
2017-11-22 16:42:54 +01:00
|
|
|
// After operator(), i is incremented. If operator() throws, i is set to 9999
|
|
|
|
void operator()() {
|
|
|
|
int old_i = i;
|
|
|
|
i = kExceptionSentinel;
|
|
|
|
ThrowingValue<> bomb;
|
|
|
|
i = ++old_i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr int kExceptionSentinel = 9999;
|
|
|
|
};
|
|
|
|
constexpr int BasicGuaranteeWithExtraInvariants::kExceptionSentinel;
|
|
|
|
|
2018-06-08 17:14:48 +02:00
|
|
|
TEST(ExceptionCheckTest, BasicGuaranteeWithExtraInvariants) {
|
2018-04-18 14:56:39 +02:00
|
|
|
auto tester_with_val =
|
|
|
|
tester.WithInitialValue(BasicGuaranteeWithExtraInvariants{});
|
|
|
|
EXPECT_TRUE(tester_with_val.Test());
|
|
|
|
EXPECT_TRUE(
|
|
|
|
tester_with_val
|
2018-06-08 17:14:48 +02:00
|
|
|
.WithInvariants([](BasicGuaranteeWithExtraInvariants* o) {
|
|
|
|
if (o->i == BasicGuaranteeWithExtraInvariants::kExceptionSentinel) {
|
2018-04-18 14:56:39 +02:00
|
|
|
return testing::AssertionSuccess();
|
|
|
|
}
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "i should be "
|
|
|
|
<< BasicGuaranteeWithExtraInvariants::kExceptionSentinel
|
2018-06-08 17:14:48 +02:00
|
|
|
<< ", but is " << o->i;
|
2018-04-18 14:56:39 +02:00
|
|
|
})
|
|
|
|
.Test());
|
2017-12-01 21:15:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FollowsStrongGuarantee : public NonNegative {
|
2017-10-30 23:55:37 +01:00
|
|
|
void operator()() { ThrowingValue<> bomb; }
|
2017-12-01 21:15:49 +01:00
|
|
|
};
|
2017-10-30 23:55:37 +01:00
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
TEST(ExceptionCheckTest, StrongGuarantee) {
|
2018-04-18 14:56:39 +02:00
|
|
|
EXPECT_TRUE(tester.WithInitialValue(FollowsStrongGuarantee{}).Test());
|
|
|
|
EXPECT_TRUE(strong_tester.WithInitialValue(FollowsStrongGuarantee{}).Test());
|
2017-12-01 21:15:49 +01:00
|
|
|
}
|
2017-10-30 23:55:37 +01:00
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
struct HasReset : public NonNegative {
|
|
|
|
void operator()() {
|
|
|
|
i = -1;
|
|
|
|
ThrowingValue<> bomb;
|
|
|
|
i = 1;
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
void reset() { i = 0; }
|
2017-10-30 23:55:37 +01:00
|
|
|
};
|
|
|
|
|
2018-04-18 14:56:39 +02:00
|
|
|
testing::AssertionResult CheckHasResetInvariants(HasReset* h) {
|
|
|
|
h->reset();
|
|
|
|
return testing::AssertionResult(h->i == 0);
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
TEST(ExceptionCheckTest, ModifyingChecker) {
|
2018-04-18 14:56:39 +02:00
|
|
|
auto set_to_1000 = [](FollowsBasicGuarantee* g) {
|
|
|
|
g->i = 1000;
|
|
|
|
return testing::AssertionSuccess();
|
|
|
|
};
|
|
|
|
auto is_1000 = [](FollowsBasicGuarantee* g) {
|
|
|
|
return testing::AssertionResult(g->i == 1000);
|
|
|
|
};
|
|
|
|
auto increment = [](FollowsStrongGuarantee* g) {
|
|
|
|
++g->i;
|
|
|
|
return testing::AssertionSuccess();
|
|
|
|
};
|
|
|
|
|
|
|
|
EXPECT_FALSE(tester.WithInitialValue(FollowsBasicGuarantee{})
|
|
|
|
.WithInvariants(set_to_1000, is_1000)
|
|
|
|
.Test());
|
|
|
|
EXPECT_TRUE(strong_tester.WithInitialValue(FollowsStrongGuarantee{})
|
|
|
|
.WithInvariants(increment)
|
|
|
|
.Test());
|
2018-04-30 20:44:26 +02:00
|
|
|
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
2018-04-18 14:56:39 +02:00
|
|
|
.WithInitialValue(HasReset{})
|
|
|
|
.WithInvariants(CheckHasResetInvariants)
|
|
|
|
.Test(invoker));
|
2017-11-22 16:42:54 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
struct NonCopyable : public NonNegative {
|
2017-11-22 16:42:54 +01:00
|
|
|
NonCopyable(const NonCopyable&) = delete;
|
2017-12-01 21:15:49 +01:00
|
|
|
NonCopyable() : NonNegative{0} {}
|
2017-11-22 16:42:54 +01:00
|
|
|
|
|
|
|
void operator()() { ThrowingValue<> bomb; }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ExceptionCheckTest, NonCopyable) {
|
2018-04-18 14:56:39 +02:00
|
|
|
auto factory = []() { return absl::make_unique<NonCopyable>(); };
|
|
|
|
EXPECT_TRUE(tester.WithFactory(factory).Test());
|
|
|
|
EXPECT_TRUE(strong_tester.WithFactory(factory).Test());
|
2017-11-22 16:42:54 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
struct NonEqualityComparable : public NonNegative {
|
2017-11-22 16:42:54 +01:00
|
|
|
void operator()() { ThrowingValue<> bomb; }
|
|
|
|
|
|
|
|
void ModifyOnThrow() {
|
|
|
|
++i;
|
|
|
|
ThrowingValue<> bomb;
|
|
|
|
static_cast<void>(bomb);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ExceptionCheckTest, NonEqualityComparable) {
|
2018-04-18 14:56:39 +02:00
|
|
|
auto nec_is_strong = [](NonEqualityComparable* nec) {
|
|
|
|
return testing::AssertionResult(nec->i == NonEqualityComparable().i);
|
|
|
|
};
|
|
|
|
auto strong_nec_tester = tester.WithInitialValue(NonEqualityComparable{})
|
|
|
|
.WithInvariants(nec_is_strong);
|
|
|
|
|
|
|
|
EXPECT_TRUE(strong_nec_tester.Test());
|
|
|
|
EXPECT_FALSE(strong_nec_tester.Test(
|
|
|
|
[](NonEqualityComparable* n) { n->ModifyOnThrow(); }));
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-12-01 21:15:49 +01:00
|
|
|
struct ExhaustivenessTester {
|
2017-10-30 23:55:37 +01:00
|
|
|
void operator()() {
|
2017-12-01 21:15:49 +01:00
|
|
|
successes |= 1;
|
2017-10-30 23:55:37 +01:00
|
|
|
T b1;
|
|
|
|
static_cast<void>(b1);
|
2017-12-01 21:15:49 +01:00
|
|
|
successes |= (1 << 1);
|
2017-10-30 23:55:37 +01:00
|
|
|
T b2;
|
|
|
|
static_cast<void>(b2);
|
2017-12-01 21:15:49 +01:00
|
|
|
successes |= (1 << 2);
|
2017-10-30 23:55:37 +01:00
|
|
|
T b3;
|
|
|
|
static_cast<void>(b3);
|
2017-12-01 21:15:49 +01:00
|
|
|
successes |= (1 << 3);
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:15:49 +01:00
|
|
|
bool operator==(const ExhaustivenessTester<ThrowingValue<>>&) const {
|
2017-11-22 16:42:54 +01:00
|
|
|
return true;
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 14:56:39 +02:00
|
|
|
static unsigned char successes;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
|
|
|
template <typename T>
|
|
|
|
testing::AssertionResult operator()(ExhaustivenessTester<T>*) const {
|
2017-11-22 16:42:54 +01:00
|
|
|
return testing::AssertionSuccess();
|
|
|
|
}
|
2018-04-18 14:56:39 +02:00
|
|
|
} CheckExhaustivenessTesterInvariants;
|
2017-10-30 23:55:37 +01:00
|
|
|
|
|
|
|
template <typename T>
|
2017-12-01 21:15:49 +01:00
|
|
|
unsigned char ExhaustivenessTester<T>::successes = 0;
|
2017-10-30 23:55:37 +01:00
|
|
|
|
|
|
|
TEST(ExceptionCheckTest, Exhaustiveness) {
|
2018-04-30 20:44:26 +02:00
|
|
|
auto exhaust_tester = testing::MakeExceptionSafetyTester()
|
2018-04-18 14:56:39 +02:00
|
|
|
.WithInvariants(CheckExhaustivenessTesterInvariants)
|
|
|
|
.WithOperation(invoker);
|
2017-10-30 23:55:37 +01:00
|
|
|
|
2018-04-18 14:56:39 +02:00
|
|
|
EXPECT_TRUE(
|
|
|
|
exhaust_tester.WithInitialValue(ExhaustivenessTester<int>{}).Test());
|
|
|
|
EXPECT_EQ(ExhaustivenessTester<int>::successes, 0xF);
|
2017-10-30 23:55:37 +01:00
|
|
|
|
2018-04-18 14:56:39 +02:00
|
|
|
EXPECT_TRUE(
|
|
|
|
exhaust_tester.WithInitialValue(ExhaustivenessTester<ThrowingValue<>>{})
|
2018-04-30 20:44:26 +02:00
|
|
|
.WithInvariants(testing::strong_guarantee)
|
2018-04-18 14:56:39 +02:00
|
|
|
.Test());
|
2017-12-01 21:15:49 +01:00
|
|
|
EXPECT_EQ(ExhaustivenessTester<ThrowingValue<>>::successes, 0xF);
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 16:42:54 +01:00
|
|
|
struct LeaksIfCtorThrows : private exceptions_internal::TrackedObject {
|
|
|
|
LeaksIfCtorThrows() : TrackedObject(ABSL_PRETTY_FUNCTION) {
|
|
|
|
++counter;
|
|
|
|
ThrowingValue<> v;
|
|
|
|
static_cast<void>(v);
|
|
|
|
--counter;
|
|
|
|
}
|
|
|
|
LeaksIfCtorThrows(const LeaksIfCtorThrows&) noexcept
|
|
|
|
: TrackedObject(ABSL_PRETTY_FUNCTION) {}
|
|
|
|
static int counter;
|
|
|
|
};
|
|
|
|
int LeaksIfCtorThrows::counter = 0;
|
|
|
|
|
|
|
|
TEST(ExceptionCheckTest, TestLeakyCtor) {
|
2018-04-30 20:44:26 +02:00
|
|
|
testing::TestThrowingCtor<LeaksIfCtorThrows>();
|
2017-11-22 16:42:54 +01:00
|
|
|
EXPECT_EQ(LeaksIfCtorThrows::counter, 1);
|
|
|
|
LeaksIfCtorThrows::counter = 0;
|
|
|
|
}
|
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
struct Tracked : private exceptions_internal::TrackedObject {
|
|
|
|
Tracked() : TrackedObject(ABSL_PRETTY_FUNCTION) {}
|
|
|
|
};
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ConstructorTrackerTest, CreatedBefore) {
|
|
|
|
Tracked a, b, c;
|
|
|
|
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ConstructorTrackerTest, CreatedAfter) {
|
|
|
|
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
|
|
|
Tracked a, b, c;
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-04-26 15:47:58 +02:00
|
|
|
TEST(ConstructorTrackerTest, NotDestroyedAfter) {
|
2017-10-30 23:55:37 +01:00
|
|
|
absl::aligned_storage_t<sizeof(Tracked), alignof(Tracked)> storage;
|
|
|
|
EXPECT_NONFATAL_FAILURE(
|
|
|
|
{
|
2018-04-26 15:47:58 +02:00
|
|
|
exceptions_internal::ConstructorTracker ct(
|
|
|
|
exceptions_internal::countdown);
|
2017-10-30 23:55:37 +01:00
|
|
|
new (&storage) Tracked;
|
|
|
|
},
|
|
|
|
"not destroyed");
|
|
|
|
}
|
|
|
|
|
2018-03-27 00:15:40 +02:00
|
|
|
TEST(ConstructorTrackerTest, DestroyedTwice) {
|
2018-06-08 17:14:48 +02:00
|
|
|
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
2017-10-30 23:55:37 +01:00
|
|
|
EXPECT_NONFATAL_FAILURE(
|
|
|
|
{
|
|
|
|
Tracked t;
|
|
|
|
t.~Tracked();
|
|
|
|
},
|
2018-06-08 17:14:48 +02:00
|
|
|
"re-destroyed");
|
2017-10-30 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 00:15:40 +02:00
|
|
|
TEST(ConstructorTrackerTest, ConstructedTwice) {
|
2018-06-08 17:14:48 +02:00
|
|
|
exceptions_internal::ConstructorTracker ct(exceptions_internal::countdown);
|
2017-10-30 23:55:37 +01:00
|
|
|
absl::aligned_storage_t<sizeof(Tracked), alignof(Tracked)> storage;
|
|
|
|
EXPECT_NONFATAL_FAILURE(
|
|
|
|
{
|
|
|
|
new (&storage) Tracked;
|
|
|
|
new (&storage) Tracked;
|
2018-06-08 17:14:48 +02:00
|
|
|
reinterpret_cast<Tracked*>(&storage)->~Tracked();
|
2017-10-30 23:55:37 +01:00
|
|
|
},
|
|
|
|
"re-constructed");
|
|
|
|
}
|
2018-03-15 21:48:55 +01:00
|
|
|
|
|
|
|
TEST(ThrowingValueTraitsTest, RelationalOperators) {
|
|
|
|
ThrowingValue<> a, b;
|
|
|
|
EXPECT_TRUE((std::is_convertible<decltype(a == b), bool>::value));
|
|
|
|
EXPECT_TRUE((std::is_convertible<decltype(a != b), bool>::value));
|
|
|
|
EXPECT_TRUE((std::is_convertible<decltype(a < b), bool>::value));
|
|
|
|
EXPECT_TRUE((std::is_convertible<decltype(a <= b), bool>::value));
|
|
|
|
EXPECT_TRUE((std::is_convertible<decltype(a > b), bool>::value));
|
|
|
|
EXPECT_TRUE((std::is_convertible<decltype(a >= b), bool>::value));
|
|
|
|
}
|
|
|
|
|
2018-03-27 00:15:40 +02:00
|
|
|
TEST(ThrowingAllocatorTraitsTest, Assignablility) {
|
|
|
|
EXPECT_TRUE(std::is_move_assignable<ThrowingAllocator<int>>::value);
|
|
|
|
EXPECT_TRUE(std::is_copy_assignable<ThrowingAllocator<int>>::value);
|
|
|
|
EXPECT_TRUE(std::is_nothrow_move_assignable<ThrowingAllocator<int>>::value);
|
|
|
|
EXPECT_TRUE(std::is_nothrow_copy_assignable<ThrowingAllocator<int>>::value);
|
|
|
|
}
|
|
|
|
|
2017-10-30 23:55:37 +01:00
|
|
|
} // namespace
|
2018-04-30 20:44:26 +02:00
|
|
|
|
|
|
|
} // namespace testing
|