[libc++][NFC] Format std::midpoint tests (#175531)
### Summary - Format code - Fix comments Extracted NFC changes from PR #175388.
This commit is contained in:
parent
20bdcbd41d
commit
baef055d30
@ -5,18 +5,17 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// UNSUPPORTED: c++03, c++11, c++14, c++17
|
||||
|
||||
// REQUIRES: std-at-least-c++20
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template <class _Float>
|
||||
// _Tp midpoint(_Float __a, _Float __b) noexcept
|
||||
//
|
||||
// template <class _Fp>
|
||||
// _Fp midpoint(_Fp __a, _Fp __b) noexcept
|
||||
|
||||
#include <cassert>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
#include "fp_compare.h"
|
||||
@ -26,110 +25,114 @@ template <typename T>
|
||||
constexpr T fp_error_pct();
|
||||
|
||||
template <>
|
||||
constexpr float fp_error_pct<float>() { return 1.0e-4f; }
|
||||
constexpr float fp_error_pct<float>() {
|
||||
return 1.0e-4f;
|
||||
}
|
||||
|
||||
template <>
|
||||
constexpr double fp_error_pct<double>() { return 1.0e-12; }
|
||||
constexpr double fp_error_pct<double>() {
|
||||
return 1.0e-12;
|
||||
}
|
||||
|
||||
template <>
|
||||
constexpr long double fp_error_pct<long double>() { return 1.0e-13l; }
|
||||
|
||||
constexpr long double fp_error_pct<long double>() {
|
||||
return 1.0e-13l;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void fp_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(T, decltype(std::midpoint(T(), T())));
|
||||
ASSERT_NOEXCEPT( std::midpoint(T(), T()));
|
||||
void fp_test() {
|
||||
ASSERT_SAME_TYPE(T, decltype(std::midpoint(T(), T())));
|
||||
ASSERT_NOEXCEPT(std::midpoint(T(), T()));
|
||||
|
||||
constexpr T maxV = std::numeric_limits<T>::max();
|
||||
constexpr T minV = std::numeric_limits<T>::min();
|
||||
constexpr T maxV = std::numeric_limits<T>::max();
|
||||
constexpr T minV = std::numeric_limits<T>::min();
|
||||
|
||||
// Things that can be compared exactly
|
||||
static_assert((std::midpoint(T(0), T(0)) == T(0)), "");
|
||||
static_assert((std::midpoint(T(2), T(4)) == T(3)), "");
|
||||
static_assert((std::midpoint(T(4), T(2)) == T(3)), "");
|
||||
static_assert((std::midpoint(T(3), T(4)) == T(3.5)), "");
|
||||
static_assert((std::midpoint(T(0), T(0.4)) == T(0.2)), "");
|
||||
// Things that can be compared exactly
|
||||
static_assert((std::midpoint(T(0), T(0)) == T(0)), "");
|
||||
static_assert((std::midpoint(T(2), T(4)) == T(3)), "");
|
||||
static_assert((std::midpoint(T(4), T(2)) == T(3)), "");
|
||||
static_assert((std::midpoint(T(3), T(4)) == T(3.5)), "");
|
||||
static_assert((std::midpoint(T(0), T(0.4)) == T(0.2)), "");
|
||||
|
||||
// Things that can't be compared exactly
|
||||
constexpr T pct = fp_error_pct<T>();
|
||||
assert((fptest_close_pct(std::midpoint(T( 1.3), T(11.4)), T( 6.35), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(11.33), T(31.45)), T(21.39), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(-1.3), T(11.4)), T( 5.05), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(11.4), T(-1.3)), T( 5.05), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(0.1), T(0.4)), T(0.25), pct)));
|
||||
// Things that can't be compared exactly
|
||||
constexpr T pct = fp_error_pct<T>();
|
||||
assert((fptest_close_pct(std::midpoint(T(1.3), T(11.4)), T(6.35), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(11.33), T(31.45)), T(21.39), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(-1.3), T(11.4)), T(5.05), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(11.4), T(-1.3)), T(5.05), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(0.1), T(0.4)), T(0.25), pct)));
|
||||
|
||||
assert((fptest_close_pct(std::midpoint(T(11.2345), T(14.5432)), T(12.88885), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(11.2345), T(14.5432)), T(12.88885), pct)));
|
||||
|
||||
// From e to pi
|
||||
assert((fptest_close_pct(std::midpoint(T(2.71828182845904523536028747135266249775724709369995),
|
||||
T(3.14159265358979323846264338327950288419716939937510)),
|
||||
T(2.92993724102441923691146542731608269097720824653752), pct)));
|
||||
// From e to pi
|
||||
assert((fptest_close_pct(
|
||||
std::midpoint(T(2.71828182845904523536028747135266249775724709369995),
|
||||
T(3.14159265358979323846264338327950288419716939937510)),
|
||||
T(2.92993724102441923691146542731608269097720824653752),
|
||||
pct)));
|
||||
|
||||
assert((fptest_close_pct(std::midpoint(maxV, T(0)), maxV/2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(0), maxV), maxV/2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(minV, T(0)), minV/2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(0), minV), minV/2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV, maxV), maxV, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(minV, minV), minV, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV, minV), maxV/2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(minV, maxV), maxV/2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV, T(0)), maxV / 2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(0), maxV), maxV / 2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(minV, T(0)), minV / 2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(T(0), minV), minV / 2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV, maxV), maxV, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(minV, minV), minV, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV, minV), maxV / 2, pct)));
|
||||
assert((fptest_close_pct(std::midpoint(minV, maxV), maxV / 2, pct)));
|
||||
|
||||
// Near the min and the max
|
||||
assert((fptest_close_pct(std::midpoint(maxV*T(0.75), maxV*T(0.50)), maxV*T(0.625), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV*T(0.50), maxV*T(0.75)), maxV*T(0.625), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(minV*T(2), minV*T(8)), minV*T(5), pct)));
|
||||
// Near the min and the max
|
||||
assert((fptest_close_pct(std::midpoint(maxV * T(0.75), maxV * T(0.50)), maxV * T(0.625), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV * T(0.50), maxV * T(0.75)), maxV * T(0.625), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(minV * T(2), minV * T(8)), minV * T(5), pct)));
|
||||
|
||||
// Big numbers of different signs
|
||||
assert((fptest_close_pct(std::midpoint(maxV*T( 0.75), maxV*T(-0.5)), maxV*T( 0.125), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV*T(-0.75), maxV*T( 0.5)), maxV*T(-0.125), pct)));
|
||||
// Big numbers of different signs
|
||||
assert((fptest_close_pct(std::midpoint(maxV * T(0.75), maxV * T(-0.5)), maxV * T(0.125), pct)));
|
||||
assert((fptest_close_pct(std::midpoint(maxV * T(-0.75), maxV * T(0.5)), maxV * T(-0.125), pct)));
|
||||
|
||||
// Denormalized values
|
||||
// TODO
|
||||
// Denormalized values
|
||||
// TODO
|
||||
|
||||
// Check two values "close to each other"
|
||||
T d1 = T(3.14);
|
||||
T d0 = std::nextafter(d1, T(2));
|
||||
T d2 = std::nextafter(d1, T(5));
|
||||
assert(d0 < d1); // sanity checking
|
||||
assert(d1 < d2); // sanity checking
|
||||
// Check two values "close to each other"
|
||||
T d1 = T(3.14);
|
||||
T d0 = std::nextafter(d1, T(2));
|
||||
T d2 = std::nextafter(d1, T(5));
|
||||
assert(d0 < d1); // sanity checking
|
||||
assert(d1 < d2); // sanity checking
|
||||
|
||||
#if defined(__PPC__) && (defined(__LONG_DOUBLE_128__) && __LONG_DOUBLE_128__) && \
|
||||
!(defined(__LONG_DOUBLE_IEEE128__) && __LONG_DOUBLE_IEEE128__)
|
||||
// For 128 bit long double implemented as 2 doubles on PowerPC,
|
||||
// nextafterl() of libm gives imprecise results which fails the
|
||||
// midpoint() tests below. So skip the test for this case.
|
||||
if constexpr (sizeof(T) != 16)
|
||||
// For 128 bit long double implemented as 2 doubles on PowerPC,
|
||||
// nextafterl() of libm gives imprecise results which fails the
|
||||
// midpoint() tests below. So skip the test for this case.
|
||||
if constexpr (sizeof(T) != 16)
|
||||
#endif
|
||||
{
|
||||
{
|
||||
// Since there's nothing in between, the midpoint has to be one or the other
|
||||
T res;
|
||||
res = std::midpoint(d0, d1);
|
||||
assert(res == d0 || res == d1);
|
||||
assert(d0 <= res);
|
||||
assert(res <= d1);
|
||||
res = std::midpoint(d1, d0);
|
||||
assert(res == d0 || res == d1);
|
||||
assert(d0 <= res);
|
||||
assert(res <= d1);
|
||||
T res;
|
||||
res = std::midpoint(d0, d1);
|
||||
assert(res == d0 || res == d1);
|
||||
assert(d0 <= res);
|
||||
assert(res <= d1);
|
||||
res = std::midpoint(d1, d0);
|
||||
assert(res == d0 || res == d1);
|
||||
assert(d0 <= res);
|
||||
assert(res <= d1);
|
||||
|
||||
res = std::midpoint(d1, d2);
|
||||
assert(res == d1 || res == d2);
|
||||
assert(d1 <= res);
|
||||
assert(res <= d2);
|
||||
res = std::midpoint(d2, d1);
|
||||
assert(res == d1 || res == d2);
|
||||
assert(d1 <= res);
|
||||
assert(res <= d2);
|
||||
}
|
||||
res = std::midpoint(d1, d2);
|
||||
assert(res == d1 || res == d2);
|
||||
assert(d1 <= res);
|
||||
assert(res <= d2);
|
||||
res = std::midpoint(d2, d1);
|
||||
assert(res == d1 || res == d2);
|
||||
assert(d1 <= res);
|
||||
assert(res <= d2);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
fp_test<float>();
|
||||
fp_test<double>();
|
||||
fp_test<long double>();
|
||||
|
||||
int main (int, char**)
|
||||
{
|
||||
fp_test<float>();
|
||||
fp_test<double>();
|
||||
fp_test<long double>();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -5,140 +5,139 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// UNSUPPORTED: c++03, c++11, c++14, c++17
|
||||
|
||||
// REQUIRES: std-at-least-c++20
|
||||
|
||||
// <numeric>
|
||||
|
||||
// template <class _Tp>
|
||||
// _Tp midpoint(_Tp __a, _Tp __b) noexcept
|
||||
//
|
||||
// Constraints:
|
||||
// - T is an arithmetic type other than bool.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <numeric>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
template <typename T>
|
||||
void signed_test()
|
||||
{
|
||||
constexpr T zero{0};
|
||||
constexpr T one{1};
|
||||
constexpr T two{2};
|
||||
constexpr T three{3};
|
||||
constexpr T four{4};
|
||||
void signed_test() {
|
||||
constexpr T zero{0};
|
||||
constexpr T one{1};
|
||||
constexpr T two{2};
|
||||
constexpr T three{3};
|
||||
constexpr T four{4};
|
||||
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
|
||||
ASSERT_NOEXCEPT( std::midpoint(T(), T()));
|
||||
using limits = std::numeric_limits<T>;
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
|
||||
ASSERT_NOEXCEPT(std::midpoint(T(), T()));
|
||||
using limits = std::numeric_limits<T>;
|
||||
|
||||
static_assert(std::midpoint(one, three) == two, "");
|
||||
static_assert(std::midpoint(three, one) == two, "");
|
||||
static_assert(std::midpoint(one, three) == two, "");
|
||||
static_assert(std::midpoint(three, one) == two, "");
|
||||
|
||||
assert(std::midpoint(zero, zero) == zero);
|
||||
assert(std::midpoint(zero, two) == one);
|
||||
assert(std::midpoint(two, zero) == one);
|
||||
assert(std::midpoint(two, two) == two);
|
||||
assert(std::midpoint(zero, zero) == zero);
|
||||
assert(std::midpoint(zero, two) == one);
|
||||
assert(std::midpoint(two, zero) == one);
|
||||
assert(std::midpoint(two, two) == two);
|
||||
|
||||
assert(std::midpoint(one, four) == two);
|
||||
assert(std::midpoint(four, one) == three);
|
||||
assert(std::midpoint(three, four) == three);
|
||||
assert(std::midpoint(four, three) == four);
|
||||
assert(std::midpoint(one, four) == two);
|
||||
assert(std::midpoint(four, one) == three);
|
||||
assert(std::midpoint(three, four) == three);
|
||||
assert(std::midpoint(four, three) == four);
|
||||
|
||||
assert(std::midpoint(T( 3), T( 4)) == T(3));
|
||||
assert(std::midpoint(T( 4), T( 3)) == T(4));
|
||||
assert(std::midpoint(T(-3), T( 4)) == T(0));
|
||||
assert(std::midpoint(T(-4), T( 3)) == T(-1));
|
||||
assert(std::midpoint(T( 3), T(-4)) == T(0));
|
||||
assert(std::midpoint(T( 4), T(-3)) == T(1));
|
||||
assert(std::midpoint(T(-3), T(-4)) == T(-3));
|
||||
assert(std::midpoint(T(-4), T(-3)) == T(-4));
|
||||
assert(std::midpoint(T(3), T(4)) == T(3));
|
||||
assert(std::midpoint(T(4), T(3)) == T(4));
|
||||
assert(std::midpoint(T(-3), T(4)) == T(0));
|
||||
assert(std::midpoint(T(-4), T(3)) == T(-1));
|
||||
assert(std::midpoint(T(3), T(-4)) == T(0));
|
||||
assert(std::midpoint(T(4), T(-3)) == T(1));
|
||||
assert(std::midpoint(T(-3), T(-4)) == T(-3));
|
||||
assert(std::midpoint(T(-4), T(-3)) == T(-4));
|
||||
|
||||
static_assert(std::midpoint(limits::min(), limits::max()) == T(-1), "");
|
||||
static_assert(std::midpoint(limits::max(), limits::min()) == T( 0), "");
|
||||
static_assert(std::midpoint(limits::min(), limits::max()) == T(-1), "");
|
||||
static_assert(std::midpoint(limits::max(), limits::min()) == T(0), "");
|
||||
|
||||
static_assert(std::midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
|
||||
assert( std::midpoint(T(6), limits::min()) == limits::min()/2 + 3);
|
||||
assert( std::midpoint(limits::max(), T(6)) == limits::max()/2 + 4);
|
||||
static_assert(std::midpoint(T(6), limits::max()) == limits::max()/2 + 3, "");
|
||||
static_assert(std::midpoint(limits::min(), T(6)) == limits::min() / 2 + 3, "");
|
||||
assert(std::midpoint(T(6), limits::min()) == limits::min() / 2 + 3);
|
||||
assert(std::midpoint(limits::max(), T(6)) == limits::max() / 2 + 4);
|
||||
static_assert(std::midpoint(T(6), limits::max()) == limits::max() / 2 + 3, "");
|
||||
|
||||
assert( std::midpoint(limits::min(), T(-6)) == limits::min()/2 - 3);
|
||||
static_assert(std::midpoint(T(-6), limits::min()) == limits::min()/2 - 3, "");
|
||||
static_assert(std::midpoint(limits::max(), T(-6)) == limits::max()/2 - 2, "");
|
||||
assert( std::midpoint(T(-6), limits::max()) == limits::max()/2 - 3);
|
||||
assert(std::midpoint(limits::min(), T(-6)) == limits::min() / 2 - 3);
|
||||
static_assert(std::midpoint(T(-6), limits::min()) == limits::min() / 2 - 3, "");
|
||||
static_assert(std::midpoint(limits::max(), T(-6)) == limits::max() / 2 - 2, "");
|
||||
assert(std::midpoint(T(-6), limits::max()) == limits::max() / 2 - 3);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void unsigned_test()
|
||||
{
|
||||
constexpr T zero{0};
|
||||
constexpr T one{1};
|
||||
constexpr T two{2};
|
||||
constexpr T three{3};
|
||||
constexpr T four{4};
|
||||
void unsigned_test() {
|
||||
constexpr T zero{0};
|
||||
constexpr T one{1};
|
||||
constexpr T two{2};
|
||||
constexpr T three{3};
|
||||
constexpr T four{4};
|
||||
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
|
||||
ASSERT_NOEXCEPT( std::midpoint(T(), T()));
|
||||
using limits = std::numeric_limits<T>;
|
||||
const T half_way = (limits::max() - limits::min())/2;
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint(T(), T())), T);
|
||||
ASSERT_NOEXCEPT(std::midpoint(T(), T()));
|
||||
using limits = std::numeric_limits<T>;
|
||||
const T half_way = (limits::max() - limits::min()) / 2;
|
||||
|
||||
static_assert(std::midpoint(one, three) == two, "");
|
||||
static_assert(std::midpoint(three, one) == two, "");
|
||||
static_assert(std::midpoint(one, three) == two, "");
|
||||
static_assert(std::midpoint(three, one) == two, "");
|
||||
|
||||
assert(std::midpoint(zero, zero) == zero);
|
||||
assert(std::midpoint(zero, two) == one);
|
||||
assert(std::midpoint(two, zero) == one);
|
||||
assert(std::midpoint(two, two) == two);
|
||||
assert(std::midpoint(zero, zero) == zero);
|
||||
assert(std::midpoint(zero, two) == one);
|
||||
assert(std::midpoint(two, zero) == one);
|
||||
assert(std::midpoint(two, two) == two);
|
||||
|
||||
assert(std::midpoint(one, four) == two);
|
||||
assert(std::midpoint(four, one) == three);
|
||||
assert(std::midpoint(three, four) == three);
|
||||
assert(std::midpoint(four, three) == four);
|
||||
assert(std::midpoint(one, four) == two);
|
||||
assert(std::midpoint(four, one) == three);
|
||||
assert(std::midpoint(three, four) == three);
|
||||
assert(std::midpoint(four, three) == four);
|
||||
|
||||
assert(std::midpoint(limits::min(), limits::max()) == T(half_way));
|
||||
assert(std::midpoint(limits::max(), limits::min()) == T(half_way + 1));
|
||||
assert(std::midpoint(limits::min(), limits::max()) == T(half_way));
|
||||
assert(std::midpoint(limits::max(), limits::min()) == T(half_way + 1));
|
||||
|
||||
static_assert(std::midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
|
||||
assert( std::midpoint(T(6), limits::min()) == limits::min()/2 + 3);
|
||||
assert( std::midpoint(limits::max(), T(6)) == half_way + 4);
|
||||
static_assert(std::midpoint(T(6), limits::max()) == half_way + 3, "");
|
||||
static_assert(std::midpoint(limits::min(), T(6)) == limits::min() / 2 + 3, "");
|
||||
assert(std::midpoint(T(6), limits::min()) == limits::min() / 2 + 3);
|
||||
assert(std::midpoint(limits::max(), T(6)) == half_way + 4);
|
||||
static_assert(std::midpoint(T(6), limits::max()) == half_way + 3, "");
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
signed_test<signed char>();
|
||||
signed_test<short>();
|
||||
signed_test<int>();
|
||||
signed_test<long>();
|
||||
signed_test<long long>();
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
signed_test<signed char>();
|
||||
signed_test<short>();
|
||||
signed_test<int>();
|
||||
signed_test<long>();
|
||||
signed_test<long long>();
|
||||
signed_test<std::int8_t>();
|
||||
signed_test<std::int16_t>();
|
||||
signed_test<std::int32_t>();
|
||||
signed_test<std::int64_t>();
|
||||
|
||||
signed_test<std::int8_t>();
|
||||
signed_test<std::int16_t>();
|
||||
signed_test<std::int32_t>();
|
||||
signed_test<std::int64_t>();
|
||||
unsigned_test<unsigned char>();
|
||||
unsigned_test<unsigned short>();
|
||||
unsigned_test<unsigned int>();
|
||||
unsigned_test<unsigned long>();
|
||||
unsigned_test<unsigned long long>();
|
||||
|
||||
unsigned_test<unsigned char>();
|
||||
unsigned_test<unsigned short>();
|
||||
unsigned_test<unsigned int>();
|
||||
unsigned_test<unsigned long>();
|
||||
unsigned_test<unsigned long long>();
|
||||
|
||||
unsigned_test<std::uint8_t>();
|
||||
unsigned_test<std::uint16_t>();
|
||||
unsigned_test<std::uint32_t>();
|
||||
unsigned_test<std::uint64_t>();
|
||||
unsigned_test<std::uint8_t>();
|
||||
unsigned_test<std::uint16_t>();
|
||||
unsigned_test<std::uint32_t>();
|
||||
unsigned_test<std::uint64_t>();
|
||||
|
||||
#ifndef TEST_HAS_NO_INT128
|
||||
unsigned_test<__uint128_t>();
|
||||
signed_test<__int128_t>();
|
||||
unsigned_test<__uint128_t>();
|
||||
signed_test<__int128_t>();
|
||||
#endif
|
||||
|
||||
// int_test<char>();
|
||||
signed_test<std::ptrdiff_t>();
|
||||
unsigned_test<std::size_t>();
|
||||
// int_test<char>();
|
||||
signed_test<std::ptrdiff_t>();
|
||||
unsigned_test<std::size_t>();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -5,8 +5,8 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// UNSUPPORTED: c++03, c++11, c++14, c++17
|
||||
|
||||
// REQUIRES: std-at-least-c++20
|
||||
|
||||
// MSVC warning C5215: a function parameter with a volatile qualified type is deprecated in C++20
|
||||
// MSVC warning C5216: a volatile qualified return type is deprecated in C++20
|
||||
@ -16,83 +16,77 @@
|
||||
|
||||
// template <class _Tp>
|
||||
// _Tp* midpoint(_Tp* __a, _Tp* __b) noexcept
|
||||
//
|
||||
// Constraints:
|
||||
// - T is a complete object type.
|
||||
|
||||
#include <numeric>
|
||||
#include <cassert>
|
||||
#include <numeric>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
constexpr void constexpr_test()
|
||||
{
|
||||
constexpr T array[1000] = {};
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), const T*);
|
||||
ASSERT_NOEXCEPT( std::midpoint(array, array));
|
||||
constexpr void constexpr_test() {
|
||||
constexpr T array[1000] = {};
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), const T*);
|
||||
ASSERT_NOEXCEPT(std::midpoint(array, array));
|
||||
|
||||
static_assert(std::midpoint(array, array) == array, "");
|
||||
static_assert(std::midpoint(array, array + 1000) == array + 500, "");
|
||||
static_assert(std::midpoint(array, array) == array, "");
|
||||
static_assert(std::midpoint(array, array + 1000) == array + 500, "");
|
||||
|
||||
static_assert(std::midpoint(array, array + 9) == array + 4, "");
|
||||
static_assert(std::midpoint(array, array + 10) == array + 5, "");
|
||||
static_assert(std::midpoint(array, array + 11) == array + 5, "");
|
||||
static_assert(std::midpoint(array + 9, array) == array + 5, "");
|
||||
static_assert(std::midpoint(array + 10, array) == array + 5, "");
|
||||
static_assert(std::midpoint(array + 11, array) == array + 6, "");
|
||||
static_assert(std::midpoint(array, array + 9) == array + 4, "");
|
||||
static_assert(std::midpoint(array, array + 10) == array + 5, "");
|
||||
static_assert(std::midpoint(array, array + 11) == array + 5, "");
|
||||
static_assert(std::midpoint(array + 9, array) == array + 5, "");
|
||||
static_assert(std::midpoint(array + 10, array) == array + 5, "");
|
||||
static_assert(std::midpoint(array + 11, array) == array + 6, "");
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
T array[1000] = {}; // we need an array to make valid pointers
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), T*);
|
||||
ASSERT_NOEXCEPT( std::midpoint(array, array));
|
||||
void runtime_test() {
|
||||
T array[1000] = {}; // we need an array to make valid pointers
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint(array, array)), T*);
|
||||
ASSERT_NOEXCEPT(std::midpoint(array, array));
|
||||
|
||||
assert(std::midpoint(array, array) == array);
|
||||
assert(std::midpoint(array, array + 1000) == array + 500);
|
||||
assert(std::midpoint(array, array) == array);
|
||||
assert(std::midpoint(array, array + 1000) == array + 500);
|
||||
|
||||
assert(std::midpoint(array, array + 9) == array + 4);
|
||||
assert(std::midpoint(array, array + 10) == array + 5);
|
||||
assert(std::midpoint(array, array + 11) == array + 5);
|
||||
assert(std::midpoint(array + 9, array) == array + 5);
|
||||
assert(std::midpoint(array + 10, array) == array + 5);
|
||||
assert(std::midpoint(array + 11, array) == array + 6);
|
||||
assert(std::midpoint(array, array + 9) == array + 4);
|
||||
assert(std::midpoint(array, array + 10) == array + 5);
|
||||
assert(std::midpoint(array, array + 11) == array + 5);
|
||||
assert(std::midpoint(array + 9, array) == array + 5);
|
||||
assert(std::midpoint(array + 10, array) == array + 5);
|
||||
assert(std::midpoint(array + 11, array) == array + 6);
|
||||
|
||||
// explicit instantiation
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint<T>(array, array)), T*);
|
||||
ASSERT_NOEXCEPT(std::midpoint<T>(array, array));
|
||||
assert(std::midpoint<T>(array, array) == array);
|
||||
assert(std::midpoint<T>(array, array + 1000) == array + 500);
|
||||
// explicit instantiation
|
||||
ASSERT_SAME_TYPE(decltype(std::midpoint<T>(array, array)), T*);
|
||||
ASSERT_NOEXCEPT(std::midpoint<T>(array, array));
|
||||
assert(std::midpoint<T>(array, array) == array);
|
||||
assert(std::midpoint<T>(array, array + 1000) == array + 500);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void pointer_test()
|
||||
{
|
||||
runtime_test< T>();
|
||||
runtime_test<const T>();
|
||||
runtime_test< volatile T>();
|
||||
runtime_test<const volatile T>();
|
||||
void pointer_test() {
|
||||
runtime_test< T>();
|
||||
runtime_test<const T>();
|
||||
runtime_test< volatile T>();
|
||||
runtime_test<const volatile T>();
|
||||
|
||||
// The constexpr tests are always const, but we can test them anyway.
|
||||
constexpr_test< T>();
|
||||
constexpr_test<const T>();
|
||||
// The constexpr tests are always const, but we can test them anyway.
|
||||
constexpr_test< T>();
|
||||
constexpr_test<const T>();
|
||||
|
||||
// GCC 9.0.1 (unreleased as of 2019-03) barfs on this, but we have a bot for it.
|
||||
// Uncomment when gcc 9.1 is released
|
||||
#ifndef TEST_COMPILER_GCC
|
||||
constexpr_test< volatile T>();
|
||||
constexpr_test<const volatile T>();
|
||||
constexpr_test< volatile T>();
|
||||
constexpr_test<const volatile T>();
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int, char**) {
|
||||
pointer_test<char>();
|
||||
pointer_test<int>();
|
||||
pointer_test<double>();
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
pointer_test<char>();
|
||||
pointer_test<int>();
|
||||
pointer_test<double>();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user