mirror of
https://github.com/microsoft/GSL.git
synced 2025-01-30 23:34: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>
91 lines
2.7 KiB
C++
91 lines
2.7 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <gsl/pointers>
|
|
|
|
#include <memory>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
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
|
|
};
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
#if __cplusplus >= 201703l
|
|
using std::void_t;
|
|
#else // __cplusplus >= 201703l
|
|
template <class...>
|
|
using void_t = void;
|
|
#endif // __cplusplus < 201703l
|
|
|
|
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;
|
|
static_assert(!SwapCompilesFor<NotMoveAssignableCustomPtr>,
|
|
"!SwapCompilesFor<NotMoveAssignableCustomPtr>");
|
|
|
|
} // namespace
|