Louis Dionne 029c9c3176
[libc++] Refactor tests for std::pointer_traits (#66645)
After landing the implementation of LWG3545, I realized that the tests
for std::pointer_traits had become a bit disorganized. This patch is a
NFC that refactors the tests:
- Move compile-only tests to `.compile.pass.cpp` tests
- Re-create the clear distinction between tests for the std::pointer_traits base tempate and for the T* specialization.
- De-duplicate test coverage -- we had a bunch of things that were tested in duplication.
2023-09-25 11:15:33 -04:00

66 lines
2.1 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <memory>
// template <class T>
// struct pointer_traits<T*>
// {
// static pointer pointer_to(<details>) noexcept; // constexpr in C++20
// ...
// };
#include <memory>
#include <cassert>
#include <type_traits>
#include "test_macros.h"
TEST_CONSTEXPR_CXX20 bool test()
{
// pointer_traits<T*>
{
int i = 0;
static_assert(std::is_same<decltype(std::pointer_traits<int*>::pointer_to(i)), int*>::value, "");
ASSERT_NOEXCEPT(std::pointer_traits<int*>::pointer_to(i));
assert(std::pointer_traits<int*>::pointer_to(i) == &i);
}
// pointer_traits<const T*>
{
int const i = 0;
static_assert(std::is_same<decltype(std::pointer_traits<const int*>::pointer_to(i)), const int*>::value, "");
ASSERT_NOEXCEPT(std::pointer_traits<const int*>::pointer_to(i));
assert(std::pointer_traits<const int*>::pointer_to(i) == &i);
}
// pointer_traits<const T*> with a non-const argument
{
int i = 0;
static_assert(std::is_same<decltype(std::pointer_traits<const int*>::pointer_to(i)), const int*>::value, "");
ASSERT_NOEXCEPT(std::pointer_traits<const int*>::pointer_to(i));
assert(std::pointer_traits<const int*>::pointer_to(i) == &i);
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
{
// Check that pointer_traits<void*> is still well-formed, even though it has no pointer_to.
static_assert(std::is_same<std::pointer_traits<void*>::element_type, void>::value, "");
static_assert(std::is_same<std::pointer_traits<const void*>::element_type, const void>::value, "");
static_assert(std::is_same<std::pointer_traits<volatile void*>::element_type, volatile void>::value, "");
}
return 0;
}