mirror of
https://github.com/microsoft/GSL.git
synced 2025-01-18 09:44:59 -05:00
16a60199df
Some checks failed
CI_Android / Android (push) Has been cancelled
cmake_find_package / Build ${{ matrix.os }} (macos-latest) (push) Has been cancelled
cmake_find_package / Build ${{ matrix.os }} (ubuntu-latest) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 11) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 23) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
- `strict_not_null<std::unique_ptr<int>>{ std::make_unique<int>()}` failed to compile - `strict_not_null` ctor needs to move the passed `unique_ptr`, not copy - Copied `not_null` `TestNotNullConstructors` for `strict_not_null` - The `noexcept` specifiers on the `strict_not_null` and `not_null` constructors were out of sync. - Added unit test for `not_null<unique_ptr<T>>` and for `strict_not_null<unique_ptr<T>>` - Added unit test for `gsl::swap` for two `strict_not_null` - Added unit test for `gsl::swap` for `not_null` and `strict_not_null` Co-authored-by: Werner Henze <w.henze@avm.de>
418 lines
10 KiB
C++
418 lines
10 KiB
C++
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
//
|
|
// This code is licensed under the MIT License (MIT).
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "deathTestCommon.h"
|
|
|
|
using namespace gsl;
|
|
|
|
// stand-in for a user-defined ref-counted class
|
|
template <typename T>
|
|
struct RefCounted
|
|
{
|
|
RefCounted(T* p) : p_(p) {}
|
|
operator T*() { return p_; }
|
|
T* p_;
|
|
};
|
|
|
|
namespace
|
|
{
|
|
// clang-format off
|
|
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
|
// clang-format on
|
|
bool helper(not_null<int*> p) { return *p == 12; }
|
|
|
|
// clang-format off
|
|
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
|
// clang-format on
|
|
bool helper_const(not_null<const int*> p) { return *p == 12; }
|
|
|
|
// clang-format off
|
|
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
|
// clang-format on
|
|
bool strict_helper(strict_not_null<int*> p) { return *p == 12; }
|
|
|
|
// clang-format off
|
|
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
|
|
// clang-format on
|
|
bool strict_helper_const(strict_not_null<const int*> p) { return *p == 12; }
|
|
|
|
int* return_pointer() { return nullptr; }
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
const int* return_pointer_const() { return nullptr; }
|
|
#endif
|
|
} // namespace
|
|
|
|
TEST(strict_notnull_tests, TestStrictNotNullConstructors)
|
|
{
|
|
{
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
strict_not_null<int*> p = nullptr; // yay...does not compile!
|
|
strict_not_null<std::vector<char>*> p1 = 0; // yay...does not compile!
|
|
strict_not_null<int*> p2; // yay...does not compile!
|
|
std::unique_ptr<int> up = std::make_unique<int>(120);
|
|
strict_not_null<int*> p3 = up;
|
|
|
|
// Forbid non-nullptr assignable types
|
|
strict_not_null<std::vector<int>> f(std::vector<int>{1});
|
|
strict_not_null<int> z(10);
|
|
strict_not_null<std::vector<int>> y({1, 2});
|
|
#endif
|
|
}
|
|
|
|
const auto terminateHandler = std::set_terminate([] {
|
|
std::cerr << "Expected Death. TestNotNullConstructors";
|
|
std::abort();
|
|
});
|
|
const auto expected = GetExpectedDeathString(terminateHandler);
|
|
|
|
{
|
|
// from shared pointer
|
|
int i = 12;
|
|
auto rp = RefCounted<int>(&i);
|
|
strict_not_null<int*> p(rp);
|
|
EXPECT_TRUE(p.get() == &i);
|
|
|
|
strict_not_null<std::shared_ptr<int>> x(
|
|
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
|
|
|
|
int* pi = nullptr;
|
|
EXPECT_DEATH((strict_not_null<decltype(pi)>(pi)), expected);
|
|
}
|
|
|
|
{
|
|
// from unique pointer
|
|
strict_not_null<std::unique_ptr<int>> x(
|
|
std::make_unique<int>(10)); // unique_ptr<int> is nullptr assignable
|
|
|
|
EXPECT_DEATH((strict_not_null<std::unique_ptr<int>>(std::unique_ptr<int>{})), expected);
|
|
}
|
|
|
|
{
|
|
// from pointer to local
|
|
int t = 42;
|
|
|
|
strict_not_null<int*> x{&t};
|
|
helper(&t);
|
|
helper_const(&t);
|
|
|
|
EXPECT_TRUE(*x == 42);
|
|
}
|
|
|
|
{
|
|
// from raw pointer
|
|
// from strict_not_null pointer
|
|
|
|
int t = 42;
|
|
int* p = &t;
|
|
|
|
strict_not_null<int*> x{p};
|
|
helper(p);
|
|
helper_const(p);
|
|
helper(x);
|
|
helper_const(x);
|
|
|
|
EXPECT_TRUE(*x == 42);
|
|
}
|
|
|
|
{
|
|
// from raw const pointer
|
|
// from strict_not_null const pointer
|
|
|
|
int t = 42;
|
|
const int* cp = &t;
|
|
|
|
strict_not_null<const int*> x{cp};
|
|
helper_const(cp);
|
|
helper_const(x);
|
|
|
|
EXPECT_TRUE(*x == 42);
|
|
}
|
|
|
|
{
|
|
// from strict_not_null const pointer, using auto
|
|
int t = 42;
|
|
const int* cp = &t;
|
|
|
|
auto x = strict_not_null<const int*>{cp};
|
|
|
|
EXPECT_TRUE(*x == 42);
|
|
}
|
|
|
|
{
|
|
// from returned pointer
|
|
|
|
EXPECT_DEATH(helper(return_pointer()), expected);
|
|
EXPECT_DEATH(helper_const(return_pointer()), expected);
|
|
}
|
|
}
|
|
|
|
TEST(strict_notnull_tests, TestStrictNotNull)
|
|
{
|
|
{
|
|
// raw ptr <-> strict_not_null
|
|
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<int*> snn1{&x};
|
|
|
|
helper(snn1);
|
|
helper_const(snn1);
|
|
|
|
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;
|
|
|
|
strict_not_null<int*> snn1{&x};
|
|
const strict_not_null<int*> snn2{&x};
|
|
|
|
strict_helper(snn1);
|
|
strict_helper_const(snn1);
|
|
strict_helper_const(snn2);
|
|
|
|
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;
|
|
|
|
strict_not_null<int*> snn{&x};
|
|
|
|
const not_null<int*> nn1 = snn;
|
|
const not_null<int*> nn2{snn};
|
|
|
|
helper(snn);
|
|
helper_const(snn);
|
|
|
|
EXPECT_TRUE(snn == nn1);
|
|
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;
|
|
|
|
not_null<int*> nn{&x};
|
|
|
|
const strict_not_null<int*> snn1{nn};
|
|
const strict_not_null<int*> snn2{nn};
|
|
|
|
strict_helper(nn);
|
|
strict_helper_const(nn);
|
|
|
|
EXPECT_TRUE(snn1 == nn);
|
|
EXPECT_TRUE(snn2 == nn);
|
|
|
|
std::hash<strict_not_null<int*>> hash_snn;
|
|
std::hash<not_null<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));
|
|
}
|
|
|
|
{
|
|
// 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};
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
|
|
|
TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
|
|
{
|
|
const auto terminateHandler = std::set_terminate([] {
|
|
std::cerr << "Expected Death. TestStrictNotNullConstructorTypeDeduction";
|
|
std::abort();
|
|
});
|
|
const auto expected = GetExpectedDeathString(terminateHandler);
|
|
|
|
{
|
|
int i = 42;
|
|
|
|
strict_not_null x{&i};
|
|
helper(strict_not_null{&i});
|
|
helper_const(strict_not_null{&i});
|
|
|
|
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;
|
|
|
|
strict_not_null x{p};
|
|
helper(strict_not_null{p});
|
|
helper_const(strict_not_null{p});
|
|
|
|
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;
|
|
const strict_not_null x{p1};
|
|
};
|
|
EXPECT_DEATH(workaround_macro(), expected);
|
|
}
|
|
|
|
{
|
|
auto workaround_macro = []() {
|
|
const int* p1 = nullptr;
|
|
const strict_not_null x{p1};
|
|
};
|
|
EXPECT_DEATH(workaround_macro(), expected);
|
|
}
|
|
|
|
{
|
|
int* p = nullptr;
|
|
|
|
EXPECT_DEATH(helper(strict_not_null{p}), expected);
|
|
EXPECT_DEATH(helper_const(strict_not_null{p}), expected);
|
|
}
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
{
|
|
strict_not_null x{nullptr};
|
|
helper(strict_not_null{nullptr});
|
|
helper_const(strict_not_null{nullptr});
|
|
}
|
|
#endif
|
|
}
|
|
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
|