libcpp_midpoint.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
  10. // <numeric>
  11. // template <class _Tp>
  12. // _Tp midpoint(_Tp __a, _Tp __b) noexcept
  13. //
  14. #include <stdint.h>
  15. #include <limits>
  16. #include <numeric>
  17. #include <cassert>
  18. #include "simple/support/algorithm.hpp"
  19. using namespace simple::support;
  20. template <typename T>
  21. void signed_test()
  22. {
  23. constexpr T zero{0};
  24. constexpr T one{1};
  25. constexpr T two{2};
  26. constexpr T three{3};
  27. constexpr T four{4};
  28. static_assert(std::is_same_v<decltype(midpoint(T(), T())), T>);
  29. // ASSERT_NOEXCEPT( midpoint(T(), T()));
  30. using limits = std::numeric_limits<T>;
  31. static_assert(midpoint(one, three) == two, "");
  32. static_assert(midpoint(three, one) == two, "");
  33. assert(midpoint(zero, zero) == zero);
  34. assert(midpoint(zero, two) == one);
  35. assert(midpoint(two, zero) == one);
  36. assert(midpoint(two, two) == two);
  37. assert(midpoint(one, four) == two);
  38. assert(midpoint(four, one) == three);
  39. assert(midpoint(three, four) == three);
  40. assert(midpoint(four, three) == four);
  41. assert(midpoint(T( 3), T( 4)) == T(3));
  42. assert(midpoint(T( 4), T( 3)) == T(4));
  43. assert(midpoint(T(-3), T( 4)) == T(0));
  44. assert(midpoint(T(-4), T( 3)) == T(-1));
  45. assert(midpoint(T( 3), T(-4)) == T(0));
  46. assert(midpoint(T( 4), T(-3)) == T(1));
  47. assert(midpoint(T(-3), T(-4)) == T(-3));
  48. assert(midpoint(T(-4), T(-3)) == T(-4));
  49. static_assert(midpoint(limits::min(), limits::max()) == T(-1), "");
  50. static_assert(midpoint(limits::max(), limits::min()) == T( 0), "");
  51. static_assert(midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
  52. assert( midpoint(T(6), limits::min()) == limits::min()/2 + 3);
  53. assert( midpoint(limits::max(), T(6)) == limits::max()/2 + 4);
  54. static_assert(midpoint(T(6), limits::max()) == limits::max()/2 + 3, "");
  55. assert( midpoint(limits::min(), T(-6)) == limits::min()/2 - 3);
  56. static_assert(midpoint(T(-6), limits::min()) == limits::min()/2 - 3, "");
  57. static_assert(midpoint(limits::max(), T(-6)) == limits::max()/2 - 2, "");
  58. assert( midpoint(T(-6), limits::max()) == limits::max()/2 - 3);
  59. }
  60. template <typename T>
  61. void unsigned_test()
  62. {
  63. constexpr T zero{0};
  64. constexpr T one{1};
  65. constexpr T two{2};
  66. constexpr T three{3};
  67. constexpr T four{4};
  68. static_assert(std::is_same_v<decltype(midpoint(T(), T())), T>);
  69. // ASSERT_NOEXCEPT( midpoint(T(), T()));
  70. using limits = std::numeric_limits<T>;
  71. const T half_way = (limits::max() - limits::min())/2;
  72. static_assert(midpoint(one, three) == two, "");
  73. static_assert(midpoint(three, one) == two, "");
  74. assert(midpoint(zero, zero) == zero);
  75. assert(midpoint(zero, two) == one);
  76. assert(midpoint(two, zero) == one);
  77. assert(midpoint(two, two) == two);
  78. assert(midpoint(one, four) == two);
  79. assert(midpoint(four, one) == three);
  80. assert(midpoint(three, four) == three);
  81. assert(midpoint(four, three) == four);
  82. assert(midpoint(limits::min(), limits::max()) == T(half_way));
  83. assert(midpoint(limits::max(), limits::min()) == T(half_way + 1));
  84. static_assert(midpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
  85. assert( midpoint(T(6), limits::min()) == limits::min()/2 + 3);
  86. assert( midpoint(limits::max(), T(6)) == half_way + 4);
  87. static_assert(midpoint(T(6), limits::max()) == half_way + 3, "");
  88. static_assert(std::is_same_v<decltype(umidpoint(T(), T())), T>);
  89. // ASSERT_NOEXCEPT( midpoint(T(), T()));
  90. static_assert(umidpoint(one, three) == two, "");
  91. static_assert(umidpoint(three, one) == two, "");
  92. assert(umidpoint(zero, zero) == zero);
  93. assert(umidpoint(zero, two) == one);
  94. assert(umidpoint(two, zero) == one);
  95. assert(umidpoint(two, two) == two);
  96. assert(umidpoint(one, four) == two);
  97. assert(umidpoint(four, one) == three);
  98. assert(umidpoint(three, four) == three);
  99. assert(umidpoint(four, three) == four);
  100. assert(umidpoint(limits::min(), limits::max()) == T(half_way));
  101. assert(umidpoint(limits::max(), limits::min()) == T(half_way + 1));
  102. static_assert(umidpoint(limits::min(), T(6)) == limits::min()/2 + 3, "");
  103. assert( umidpoint(T(6), limits::min()) == limits::min()/2 + 3);
  104. assert( umidpoint(limits::max(), T(6)) == half_way + 4);
  105. static_assert(umidpoint(T(6), limits::max()) == half_way + 3, "");
  106. }
  107. int main(int, char**)
  108. {
  109. signed_test<signed char>();
  110. signed_test<short>();
  111. signed_test<int>();
  112. signed_test<long>();
  113. signed_test<long long>();
  114. signed_test<int8_t>();
  115. signed_test<int16_t>();
  116. signed_test<int32_t>();
  117. signed_test<int64_t>();
  118. unsigned_test<unsigned char>();
  119. unsigned_test<unsigned short>();
  120. unsigned_test<unsigned int>();
  121. unsigned_test<unsigned long>();
  122. unsigned_test<unsigned long long>();
  123. unsigned_test<uint8_t>();
  124. unsigned_test<uint16_t>();
  125. unsigned_test<uint32_t>();
  126. unsigned_test<uint64_t>();
  127. #ifndef _LIBCPP_HAS_NO_INT128
  128. // unsigned_test<__uint128_t>();
  129. // signed_test<__int128_t>();
  130. #endif
  131. // int_test<char>();
  132. signed_test<ptrdiff_t>();
  133. unsigned_test<size_t>();
  134. return 0;
  135. }