mirror of
https://github.com/microsoft/GSL.git
synced 2025-01-18 01:35:00 -05:00
1cdb8d295e
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, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 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
92 lines
2.7 KiB
C++
92 lines
2.7 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <gsl/pointers>
|
|
|
|
#include <memory>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
#if __cplusplus >= 201703l
|
|
using std::void_t;
|
|
#else // __cplusplus >= 201703l
|
|
template <class...>
|
|
using void_t = void;
|
|
#endif // __cplusplus < 201703l
|
|
|
|
namespace
|
|
{
|
|
// Custom pointer type that can be used for gsl::not_null, but for which these cannot be swapped.
|
|
struct NotMoveAssignableCustomPtr
|
|
{
|
|
NotMoveAssignableCustomPtr() = default;
|
|
NotMoveAssignableCustomPtr(const NotMoveAssignableCustomPtr&) = default;
|
|
NotMoveAssignableCustomPtr& operator=(const NotMoveAssignableCustomPtr&) = default;
|
|
NotMoveAssignableCustomPtr(NotMoveAssignableCustomPtr&&) = default;
|
|
NotMoveAssignableCustomPtr& operator=(NotMoveAssignableCustomPtr&&) = delete;
|
|
|
|
bool operator!=(std::nullptr_t) const { return true; }
|
|
|
|
int dummy{}; // Without this clang warns, that NotMoveAssignableCustomPtr() is unneeded
|
|
};
|
|
|
|
template <typename U, typename = void>
|
|
static constexpr bool SwapCompilesFor = false;
|
|
template <typename U>
|
|
static constexpr bool
|
|
SwapCompilesFor<U, void_t<decltype(gsl::swap<U>(std::declval<gsl::not_null<U>&>(),
|
|
std::declval<gsl::not_null<U>&>()))>> = true;
|
|
|
|
TEST(pointers_test, swap)
|
|
{
|
|
// taken from gh-1129:
|
|
{
|
|
gsl::not_null<std::unique_ptr<int>> a(std::make_unique<int>(0));
|
|
gsl::not_null<std::unique_ptr<int>> b(std::make_unique<int>(1));
|
|
|
|
EXPECT_TRUE(*a == 0);
|
|
EXPECT_TRUE(*b == 1);
|
|
|
|
gsl::swap(a, b);
|
|
|
|
EXPECT_TRUE(*a == 1);
|
|
EXPECT_TRUE(*b == 0);
|
|
|
|
// Make sure our custom ptr can be used with not_null. The shared_pr is to prevent "unused"
|
|
// compiler warnings.
|
|
const auto shared_custom_ptr{std::make_shared<NotMoveAssignableCustomPtr>()};
|
|
gsl::not_null<NotMoveAssignableCustomPtr> c{*shared_custom_ptr};
|
|
EXPECT_TRUE(c.get() != nullptr);
|
|
}
|
|
|
|
{
|
|
gsl::strict_not_null<std::unique_ptr<int>> a{std::make_unique<int>(0)};
|
|
gsl::strict_not_null<std::unique_ptr<int>> b{std::make_unique<int>(1)};
|
|
|
|
EXPECT_TRUE(*a == 0);
|
|
EXPECT_TRUE(*b == 1);
|
|
|
|
gsl::swap(a, b);
|
|
|
|
EXPECT_TRUE(*a == 1);
|
|
EXPECT_TRUE(*b == 0);
|
|
}
|
|
|
|
{
|
|
gsl::not_null<std::unique_ptr<int>> a{std::make_unique<int>(0)};
|
|
gsl::strict_not_null<std::unique_ptr<int>> b{std::make_unique<int>(1)};
|
|
|
|
EXPECT_TRUE(*a == 0);
|
|
EXPECT_TRUE(*b == 1);
|
|
|
|
gsl::swap(a, b);
|
|
|
|
EXPECT_TRUE(*a == 1);
|
|
EXPECT_TRUE(*b == 0);
|
|
}
|
|
|
|
static_assert(!SwapCompilesFor<NotMoveAssignableCustomPtr>,
|
|
"!SwapCompilesFor<NotMoveAssignableCustomPtr>");
|
|
}
|
|
|
|
} // namespace
|