CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
127 lines
4.0 KiB
C++
127 lines
4.0 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// REQUIRES: std-at-least-c++20
|
|
|
|
// <numeric>
|
|
|
|
// template<class T>
|
|
// constexpr T midpoint(T a, T b) noexcept;
|
|
//
|
|
// Constraints:
|
|
// - T is an arithmetic type other than cv bool.
|
|
|
|
#include <cassert>
|
|
#include <concepts>
|
|
#include <limits>
|
|
#include <numeric>
|
|
|
|
#include "test_macros.h"
|
|
#include "type_algorithms.h"
|
|
|
|
template <typename T>
|
|
concept has_midpoint = requires(T a, T b) { std::midpoint<T>(a, b); };
|
|
|
|
static_assert(!has_midpoint<bool>);
|
|
static_assert(!has_midpoint<const bool>);
|
|
static_assert(!has_midpoint<volatile bool>);
|
|
static_assert(!has_midpoint<const volatile bool>);
|
|
|
|
template <typename T>
|
|
constexpr void test_signed() {
|
|
ASSERT_SAME_TYPE(decltype(std::midpoint(T{}, T{})), T);
|
|
ASSERT_NOEXCEPT(std::midpoint(T{}, T{}));
|
|
using limits = std::numeric_limits<T>;
|
|
|
|
assert(std::midpoint(T{1}, T{3}) == T{2});
|
|
assert(std::midpoint(T{3}, T{1}) == T{2});
|
|
|
|
assert(std::midpoint(T{0}, T{0}) == T{0});
|
|
assert(std::midpoint(T{0}, T{2}) == T{1});
|
|
assert(std::midpoint(T{2}, T{0}) == T{1});
|
|
assert(std::midpoint(T{2}, T{2}) == T{2});
|
|
|
|
assert(std::midpoint(T{1}, T{4}) == T{2});
|
|
assert(std::midpoint(T{4}, T{1}) == T{3});
|
|
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(limits::min(), limits::max()) == T{-1});
|
|
assert(std::midpoint(limits::max(), limits::min()) == T{0});
|
|
|
|
assert(std::midpoint(limits::min(), T{6}) == T{limits::min() / 2 + 3});
|
|
assert(std::midpoint(T{6}, limits::min()) == T{limits::min() / 2 + 3});
|
|
|
|
assert(std::midpoint(limits::max(), T{6}) == T{limits::max() / 2 + 4});
|
|
assert(std::midpoint(T{6}, limits::max()) == T{limits::max() / 2 + 3});
|
|
|
|
assert(std::midpoint(limits::min(), T{-6}) == T{limits::min() / 2 - 3});
|
|
assert(std::midpoint(T{-6}, limits::min()) == T{limits::min() / 2 - 3});
|
|
|
|
assert(std::midpoint(limits::max(), T{-6}) == T{limits::max() / 2 - 2});
|
|
assert(std::midpoint(T{-6}, limits::max()) == T{limits::max() / 2 - 3});
|
|
}
|
|
|
|
template <typename T>
|
|
constexpr void test_unsigned() {
|
|
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(std::midpoint(T{1}, T{3}) == T{2});
|
|
assert(std::midpoint(T{3}, T{1}) == T{2});
|
|
|
|
assert(std::midpoint(T{0}, T{0}) == T{0});
|
|
assert(std::midpoint(T{0}, T{2}) == T{1});
|
|
assert(std::midpoint(T{2}, T{0}) == T{1});
|
|
assert(std::midpoint(T{2}, T{2}) == T{2});
|
|
|
|
assert(std::midpoint(T{1}, T{4}) == T{2});
|
|
assert(std::midpoint(T{4}, T{1}) == T{3});
|
|
assert(std::midpoint(T{3}, T{4}) == T{3});
|
|
assert(std::midpoint(T{4}, T{3}) == T{4});
|
|
|
|
assert(std::midpoint(limits::min(), limits::max()) == half_way);
|
|
assert(std::midpoint(limits::max(), limits::min()) == T{half_way + 1});
|
|
|
|
assert(std::midpoint(limits::min(), T{6}) == T{3});
|
|
assert(std::midpoint(T{6}, limits::min()) == T{3});
|
|
|
|
assert(std::midpoint(limits::max(), T{6}) == T{half_way + 4});
|
|
assert(std::midpoint(T{6}, limits::max()) == T{half_way + 3});
|
|
}
|
|
|
|
constexpr bool test() {
|
|
types::for_each(types::integer_types(), []<class T>() {
|
|
if constexpr (!std::same_as<T, bool>) {
|
|
if constexpr (std::signed_integral<T>) {
|
|
test_signed<T>();
|
|
} else {
|
|
test_unsigned<T>();
|
|
}
|
|
}
|
|
});
|
|
return true;
|
|
}
|
|
|
|
int main(int, char**) {
|
|
test();
|
|
static_assert(test());
|
|
|
|
return 0;
|
|
}
|