зеркало из https://github.com/microsoft/GSL.git
add not_null and strict_not_null tests for const (#1085)
This commit is contained in:
Родитель
f7da2e41f0
Коммит
49c88f27bd
|
@ -441,6 +441,18 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
|
|||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
const int i = 42;
|
||||
|
||||
not_null x{&i};
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
helper(not_null{&i});
|
||||
#endif
|
||||
helper_const(not_null{&i});
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
int i = 42;
|
||||
int* p = &i;
|
||||
|
@ -452,6 +464,19 @@ TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
|
|||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
const int i = 42;
|
||||
const int* p = &i;
|
||||
|
||||
not_null x{p};
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
helper(not_null{p});
|
||||
#endif
|
||||
helper_const(not_null{p});
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
const auto terminateHandler = std::set_terminate([] {
|
||||
std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction";
|
||||
std::abort();
|
||||
|
@ -503,6 +528,18 @@ TEST(notnull_tests, TestMakeNotNull)
|
|||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
const int i = 42;
|
||||
|
||||
const auto x = make_not_null(&i);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
helper(make_not_null(&i));
|
||||
#endif
|
||||
helper_const(make_not_null(&i));
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
int i = 42;
|
||||
int* p = &i;
|
||||
|
@ -514,6 +551,19 @@ TEST(notnull_tests, TestMakeNotNull)
|
|||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
const int i = 42;
|
||||
const int* p = &i;
|
||||
|
||||
const auto x = make_not_null(p);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
helper(make_not_null(p));
|
||||
#endif
|
||||
helper_const(make_not_null(p));
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
const auto terminateHandler = std::set_terminate([] {
|
||||
std::cerr << "Expected Death. TestMakeNotNull";
|
||||
std::abort();
|
||||
|
@ -556,15 +606,31 @@ TEST(notnull_tests, TestMakeNotNull)
|
|||
|
||||
TEST(notnull_tests, TestStdHash)
|
||||
{
|
||||
int x = 42;
|
||||
int y = 99;
|
||||
not_null<int*> nn{&x};
|
||||
const not_null<int*> cnn{&x};
|
||||
{
|
||||
int x = 42;
|
||||
int y = 99;
|
||||
not_null<int*> nn{&x};
|
||||
const not_null<int*> cnn{&x};
|
||||
|
||||
std::hash<not_null<int*>> hash_nn;
|
||||
std::hash<int*> hash_intptr;
|
||||
std::hash<not_null<int*>> hash_nn;
|
||||
std::hash<int*> hash_intptr;
|
||||
|
||||
EXPECT_TRUE(hash_nn(nn) == hash_intptr(&x));
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y));
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr));
|
||||
EXPECT_TRUE(hash_nn(nn) == hash_intptr(&x));
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y));
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr));
|
||||
}
|
||||
|
||||
{
|
||||
const int x = 42;
|
||||
const int y = 99;
|
||||
not_null<const int*> nn{&x};
|
||||
const not_null<const int*> cnn{&x};
|
||||
|
||||
std::hash<not_null<const int*>> hash_nn;
|
||||
std::hash<const int*> hash_intptr;
|
||||
|
||||
EXPECT_TRUE(hash_nn(nn) == hash_intptr(&x));
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y));
|
||||
EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,6 +71,28 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
|||
EXPECT_TRUE(*snn1 == 42);
|
||||
}
|
||||
|
||||
{
|
||||
// raw ptr <-> strict_not_null
|
||||
const int x = 42;
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
strict_not_null<int*> snn = &x;
|
||||
strict_helper(&x);
|
||||
strict_helper_const(&x);
|
||||
strict_helper(return_pointer());
|
||||
strict_helper_const(return_pointer_const());
|
||||
#endif
|
||||
|
||||
const strict_not_null<const int*> snn1{&x};
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
helper(snn1);
|
||||
#endif
|
||||
helper_const(snn1);
|
||||
|
||||
EXPECT_TRUE(*snn1 == 42);
|
||||
}
|
||||
|
||||
{
|
||||
// strict_not_null -> strict_not_null
|
||||
int x = 42;
|
||||
|
@ -85,6 +107,22 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
|||
EXPECT_TRUE(snn1 == snn2);
|
||||
}
|
||||
|
||||
{
|
||||
// strict_not_null -> strict_not_null
|
||||
const int x = 42;
|
||||
|
||||
strict_not_null<const int*> snn1{&x};
|
||||
const strict_not_null<const int*> snn2{&x};
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
strict_helper(snn1);
|
||||
#endif
|
||||
strict_helper_const(snn1);
|
||||
strict_helper_const(snn2);
|
||||
|
||||
EXPECT_TRUE(snn1 == snn2);
|
||||
}
|
||||
|
||||
{
|
||||
// strict_not_null -> not_null
|
||||
int x = 42;
|
||||
|
@ -101,6 +139,24 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
|||
EXPECT_TRUE(snn == nn2);
|
||||
}
|
||||
|
||||
{
|
||||
// strict_not_null -> not_null
|
||||
const int x = 42;
|
||||
|
||||
strict_not_null<const int*> snn{&x};
|
||||
|
||||
const not_null<const int*> nn1 = snn;
|
||||
const not_null<const int*> nn2{snn};
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
helper(snn);
|
||||
#endif
|
||||
helper_const(snn);
|
||||
|
||||
EXPECT_TRUE(snn == nn1);
|
||||
EXPECT_TRUE(snn == nn2);
|
||||
}
|
||||
|
||||
{
|
||||
// not_null -> strict_not_null
|
||||
int x = 42;
|
||||
|
@ -125,6 +181,32 @@ TEST(strict_notnull_tests, TestStrictNotNull)
|
|||
EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn));
|
||||
}
|
||||
|
||||
{
|
||||
// not_null -> strict_not_null
|
||||
const int x = 42;
|
||||
|
||||
not_null<const int*> nn{&x};
|
||||
|
||||
const strict_not_null<const int*> snn1{nn};
|
||||
const strict_not_null<const int*> snn2{nn};
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
strict_helper(nn);
|
||||
#endif
|
||||
strict_helper_const(nn);
|
||||
|
||||
EXPECT_TRUE(snn1 == nn);
|
||||
EXPECT_TRUE(snn2 == nn);
|
||||
|
||||
std::hash<strict_not_null<const int*>> hash_snn;
|
||||
std::hash<not_null<const int*>> hash_nn;
|
||||
|
||||
EXPECT_TRUE(hash_nn(snn1) == hash_nn(nn));
|
||||
EXPECT_TRUE(hash_snn(snn1) == hash_nn(nn));
|
||||
EXPECT_TRUE(hash_nn(snn1) == hash_nn(snn2));
|
||||
EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn));
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
{
|
||||
strict_not_null<int*> p{nullptr};
|
||||
|
@ -152,6 +234,18 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
|
|||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
const int i = 42;
|
||||
|
||||
strict_not_null x{&i};
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
helper(strict_not_null{&i});
|
||||
#endif
|
||||
helper_const(strict_not_null{&i});
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
int i = 42;
|
||||
int* p = &i;
|
||||
|
@ -163,6 +257,19 @@ TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
|
|||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
const int i = 42;
|
||||
const int* p = &i;
|
||||
|
||||
strict_not_null x{p};
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
helper(strict_not_null{p});
|
||||
#endif
|
||||
helper_const(strict_not_null{p});
|
||||
|
||||
EXPECT_TRUE(*x == 42);
|
||||
}
|
||||
|
||||
{
|
||||
auto workaround_macro = []() {
|
||||
int* p1 = nullptr;
|
||||
|
|
Загрузка…
Ссылка в новой задаче