diff --git a/libc/test/utils/CPP/CMakeLists.txt b/libc/test/utils/CPP/CMakeLists.txt --- a/libc/test/utils/CPP/CMakeLists.txt +++ b/libc/test/utils/CPP/CMakeLists.txt @@ -20,6 +20,16 @@ libc.utils.CPP.standalone_cpp ) +add_libc_unittest( + limits_test + SUITE + libc_cpp_utils_unittests + SRCS + limits_test.cpp + DEPENDS + libc.utils.CPP.standalone_cpp +) + add_libc_unittest( arrayref_test SUITE diff --git a/libc/test/utils/CPP/limits_test.cpp b/libc/test/utils/CPP/limits_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/utils/CPP/limits_test.cpp @@ -0,0 +1,54 @@ +//===-- Unittests for Limits ----------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "utils/CPP/Limits.h" +#include "utils/UnitTest/Test.h" + +// This just checks against the C spec, almost all implementations will surpass +// this. +TEST(LlvmLibcLimitsTest, LimitsFollowSpec) { + ASSERT_GE(__llvm_libc::cpp::NumericLimits::max(), 32767); + ASSERT_LE(__llvm_libc::cpp::NumericLimits::min(), -32767); + + ASSERT_GE(__llvm_libc::cpp::NumericLimits::max(), 65535u); + + ASSERT_GE(__llvm_libc::cpp::NumericLimits::max(), 2147483647l); + ASSERT_LE(__llvm_libc::cpp::NumericLimits::min(), -2147483647l); + + ASSERT_GE(__llvm_libc::cpp::NumericLimits::max(), + 4294967295ul); + + ASSERT_GE(__llvm_libc::cpp::NumericLimits::max(), + 9223372036854775807ll); + ASSERT_LE(__llvm_libc::cpp::NumericLimits::min(), + -9223372036854775807ll); + + ASSERT_GE(__llvm_libc::cpp::NumericLimits::max(), + 18446744073709551615ull); +} + +// This checks that the current environment supports 128 bit integers. +TEST(LlvmLibcLimitsTest, Int128Works) { + // __int128_t max128 = -(~__int128_t(0) + 1); + // __int128_t min128 = ~__int128_t(0); + // EXPECT_GT( + // __llvm_libc::cpp::NumericLimits<__int128_t>::max(), + // __int128_t(__llvm_libc::cpp::NumericLimits::max())); + // ASSERT_EQ(__llvm_libc::cpp::NumericLimits<__int128_t>::max(), max128); + + // EXPECT_LT( + // __llvm_libc::cpp::NumericLimits<__int128_t>::min(), + // __int128_t(__llvm_libc::cpp::NumericLimits::min())); + // ASSERT_EQ(__llvm_libc::cpp::NumericLimits<__int128_t>::min(), min128); + + __uint128_t umax128 = ~__uint128_t(0); + EXPECT_GT( + __llvm_libc::cpp::NumericLimits<__uint128_t>::max(), + __uint128_t(__llvm_libc::cpp::NumericLimits::max())); + ASSERT_EQ(__llvm_libc::cpp::NumericLimits<__uint128_t>::max(), umax128); +} diff --git a/libc/utils/CPP/Limits.h b/libc/utils/CPP/Limits.h --- a/libc/utils/CPP/Limits.h +++ b/libc/utils/CPP/Limits.h @@ -52,6 +52,18 @@ static constexpr unsigned long long max() { return ULLONG_MAX; } static constexpr unsigned long long min() { return 0; } }; +template <> class NumericLimits<__uint128_t> { +public: + static constexpr __uint128_t max() { return ~__uint128_t(0); } + static constexpr __uint128_t min() { return 0; } +}; +// template <> class NumericLimits<__int128_t> { +// public: +// static constexpr __int128_t max() { +// return -(~__int128_t(0) + 1); +// } +// static constexpr __int128_t min() { return ~__int128_t(0); } +// }; } // namespace cpp } // namespace __llvm_libc