Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/microsoft/GSL.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'tests/notnull_tests.cpp')
-rw-r--r--tests/notnull_tests.cpp69
1 files changed, 43 insertions, 26 deletions
diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp
index d4258e8..db22c72 100644
--- a/tests/notnull_tests.cpp
+++ b/tests/notnull_tests.cpp
@@ -25,13 +25,9 @@
#include <string> // for basic_string, operator==, string, operator<<
#include <typeinfo> // for type_info
+#include "deathTestCommon.h"
using namespace gsl;
-namespace
-{
-constexpr char deathstring[] = "Expected Death";
-} // namespace
-
struct MyBase
{
};
@@ -64,7 +60,9 @@ struct CustomPtr
template <typename T, typename U>
std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
+ // clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+ // clang-format on
return reinterpret_cast<const void*>(lhs.p_) == reinterpret_cast<const void*>(rhs.p_) ? "true"
: "false";
}
@@ -72,7 +70,9 @@ std::string operator==(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
template <typename T, typename U>
std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
+ // clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+ // clang-format on
return reinterpret_cast<const void*>(lhs.p_) != reinterpret_cast<const void*>(rhs.p_) ? "true"
: "false";
}
@@ -80,7 +80,9 @@ std::string operator!=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
template <typename T, typename U>
std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
+ // clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+ // clang-format on
return reinterpret_cast<const void*>(lhs.p_) < reinterpret_cast<const void*>(rhs.p_) ? "true"
: "false";
}
@@ -88,7 +90,9 @@ std::string operator<(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
template <typename T, typename U>
std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
+ // clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+ // clang-format on
return reinterpret_cast<const void*>(lhs.p_) > reinterpret_cast<const void*>(rhs.p_) ? "true"
: "false";
}
@@ -96,7 +100,9 @@ std::string operator>(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
template <typename T, typename U>
std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
+ // clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+ // clang-format on
return reinterpret_cast<const void*>(lhs.p_) <= reinterpret_cast<const void*>(rhs.p_) ? "true"
: "false";
}
@@ -104,7 +110,9 @@ std::string operator<=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
template <typename T, typename U>
std::string operator>=(CustomPtr<T> const& lhs, CustomPtr<U> const& rhs)
{
+ // clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+ // clang-format on
return reinterpret_cast<const void*>(lhs.p_) >= reinterpret_cast<const void*>(rhs.p_) ? "true"
: "false";
}
@@ -120,9 +128,13 @@ struct NonCopyableNonMovable
namespace
{
-GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
+// clang-format off
+GSL_SUPPRESS(f .4) // NO-FORMAT: attribute
+// clang-format on
bool helper(not_null<int*> p) { return *p == 12; }
-GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
+// clang-format off
+GSL_SUPPRESS(f .4) // NO-FORMAT: attribute
+// clang-format on
bool helper_const(not_null<const int*> p) { return *p == 12; }
int* return_pointer() { return nullptr; }
@@ -145,10 +157,12 @@ TEST(notnull_tests, TestNotNullConstructors)
#endif
}
- std::set_terminate([] {
+ const auto terminateHandler = std::set_terminate([] {
std::cerr << "Expected Death. TestNotNullConstructors";
std::abort();
});
+ const auto expected = GetExpectedDeathString(terminateHandler);
+
{
// from shared pointer
int i = 12;
@@ -160,7 +174,7 @@ TEST(notnull_tests, TestNotNullConstructors)
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
int* pi = nullptr;
- EXPECT_DEATH((not_null<decltype(pi)>(pi)), deathstring);
+ EXPECT_DEATH((not_null<decltype(pi)>(pi)), expected);
}
{
@@ -217,8 +231,8 @@ TEST(notnull_tests, TestNotNullConstructors)
{
// from returned pointer
- EXPECT_DEATH(helper(return_pointer()), deathstring);
- EXPECT_DEATH(helper_const(return_pointer()), deathstring);
+ EXPECT_DEATH(helper(return_pointer()), expected);
+ EXPECT_DEATH(helper_const(return_pointer()), expected);
}
}
@@ -278,17 +292,18 @@ TEST(notnull_tests, TestNotNullCasting)
TEST(notnull_tests, TestNotNullAssignment)
{
- std::set_terminate([] {
+ const auto terminateHandler = std::set_terminate([] {
std::cerr << "Expected Death. TestNotNullAssignmentd";
std::abort();
});
+ const auto expected = GetExpectedDeathString(terminateHandler);
int i = 12;
not_null<int*> p(&i);
EXPECT_TRUE(helper(p));
int* q = nullptr;
- EXPECT_DEATH(p = not_null<int*>(q), deathstring);
+ EXPECT_DEATH(p = not_null<int*>(q), expected);
}
TEST(notnull_tests, TestNotNullRawPointerComparison)
@@ -437,17 +452,18 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
EXPECT_TRUE(*x == 42);
}
- std::set_terminate([] {
+ const auto terminateHandler = std::set_terminate([] {
std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction";
std::abort();
});
+ const auto expected = GetExpectedDeathString(terminateHandler);
{
auto workaround_macro = []() {
int* p1 = nullptr;
const not_null x{p1};
};
- EXPECT_DEATH(workaround_macro(), deathstring);
+ EXPECT_DEATH(workaround_macro(), expected);
}
{
@@ -455,14 +471,14 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
const int* p1 = nullptr;
const not_null x{p1};
};
- EXPECT_DEATH(workaround_macro(), deathstring);
+ EXPECT_DEATH(workaround_macro(), expected);
}
{
int* p = nullptr;
- EXPECT_DEATH(helper(not_null{p}), deathstring);
- EXPECT_DEATH(helper_const(not_null{p}), deathstring);
+ EXPECT_DEATH(helper(not_null{p}), expected);
+ EXPECT_DEATH(helper_const(not_null{p}), expected);
}
#ifdef CONFIRM_COMPILATION_ERRORS
@@ -498,10 +514,11 @@ TEST(notnull_tests, TestMakeNotNull)
EXPECT_TRUE(*x == 42);
}
- std::set_terminate([] {
+ const auto terminateHandler = std::set_terminate([] {
std::cerr << "Expected Death. TestMakeNotNull";
std::abort();
});
+ const auto expected = GetExpectedDeathString(terminateHandler);
{
const auto workaround_macro = []() {
@@ -509,7 +526,7 @@ TEST(notnull_tests, TestMakeNotNull)
const auto x = make_not_null(p1);
EXPECT_TRUE(*x == 42);
};
- EXPECT_DEATH(workaround_macro(), deathstring);
+ EXPECT_DEATH(workaround_macro(), expected);
}
{
@@ -518,21 +535,21 @@ TEST(notnull_tests, TestMakeNotNull)
const auto x = make_not_null(p1);
EXPECT_TRUE(*x == 42);
};
- EXPECT_DEATH(workaround_macro(), deathstring);
+ EXPECT_DEATH(workaround_macro(), expected);
}
{
int* p = nullptr;
- EXPECT_DEATH(helper(make_not_null(p)), deathstring);
- EXPECT_DEATH(helper_const(make_not_null(p)), deathstring);
+ EXPECT_DEATH(helper(make_not_null(p)), expected);
+ EXPECT_DEATH(helper_const(make_not_null(p)), expected);
}
#ifdef CONFIRM_COMPILATION_ERRORS
{
- EXPECT_DEATH(make_not_null(nullptr), deathstring);
- EXPECT_DEATH(helper(make_not_null(nullptr)), deathstring);
- EXPECT_DEATH(helper_const(make_not_null(nullptr)), deathstring);
+ EXPECT_DEATH(make_not_null(nullptr), expected);
+ EXPECT_DEATH(helper(make_not_null(nullptr)), expected);
+ EXPECT_DEATH(helper_const(make_not_null(nullptr)), expected);
}
#endif
}