diff --git a/libc/utils/CPP/TypeTraits.h b/libc/utils/CPP/TypeTraits.h --- a/libc/utils/CPP/TypeTraits.h +++ b/libc/utils/CPP/TypeTraits.h @@ -39,6 +39,7 @@ template <> struct IsIntegral : public TrueValue {}; template <> struct IsIntegral : public TrueValue {}; template <> struct IsIntegral : public TrueValue {}; +template <> struct IsIntegral<__uint128_t> : public TrueValue {}; template struct IsPointerType : public FalseValue {}; template struct IsPointerType : public TrueValue {}; diff --git a/libc/utils/UnitTest/Test.h b/libc/utils/UnitTest/Test.h --- a/libc/utils/UnitTest/Test.h +++ b/libc/utils/UnitTest/Test.h @@ -78,7 +78,9 @@ // |Cond| on mismatched |LHS| and |RHS| types can potentially succeed because // of type promotion. template ::Value, ValType> = 0> + cpp::EnableIfType::Value || + cpp::IsFloatingPointType::Value, + int> = 0> static bool test(RunContext &Ctx, TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { diff --git a/libc/utils/UnitTest/Test.cpp b/libc/utils/UnitTest/Test.cpp --- a/libc/utils/UnitTest/Test.cpp +++ b/libc/utils/UnitTest/Test.cpp @@ -8,7 +8,9 @@ #include "Test.h" +#include "utils/FPUtil/FPBits.h" #include "utils/testutils/ExecuteFunction.h" +#include "utils/testutils/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/raw_ostream.h" @@ -32,6 +34,47 @@ namespace internal { +// When the value is not floating-point type, just display it as normal +template +cpp::EnableIfType::Value, void> +describeValue(ValType Value) { + testutils::outs() << Value << '\n'; +} + +// When the value is __uint128_t, also show its hexadecimal digits. +template <> void describeValue<__uint128_t>(__uint128_t Value) { + testutils::outs() << Value << '\n' + << "In hexadecimal: " << llvm::utohexstr(Value) << '\n'; +} + +// When the value is a floating point type, also show its sign | exponent | +// mantissa +template +typename cpp::EnableIfType::Value, void> +describeValue(ValType Value) { + fputil::FPBits Bits(Value); + testutils::outs() << Value << '\n' + << "Sign: " << (Bits.sign ? 1 : 0) << ", " + << "Exponent: " << llvm::utohexstr(Bits.exponent) << ", " + << "Mantissa: " << llvm::utohexstr(Bits.exponent) << '\n'; +} + +template +void showDifference(ValType LHS, ValType RHS, const char *LHSStr, + const char *RHSStr, const char *File, unsigned long Line, + const std::string &OpString) { + const size_t OffsetLength = OpString.size() > 2 ? OpString.size() - 2 : 0; + std::string Offset(' ', OffsetLength); + + testutils::outs() << File << ":" << Line << ": FAILURE\n" + << Offset << "Expected: " << LHSStr << '\n' + << Offset << "Which is: "; + describeValue(LHS); + testutils::outs() << "To be " << OpString << ": " << RHSStr << '\n' + << Offset << "Which is: "; + describeValue(RHS); +} + template bool test(RunContext &Ctx, TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, @@ -42,67 +85,44 @@ return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be equal to: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; - + showDifference(LHS, RHS, LHSStr, RHSStr, File, Line, "equal to"); return false; case Cond_NE: if (LHS != RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be not equal to: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + showDifference(LHS, RHS, LHSStr, RHSStr, File, Line, "not equal to"); return false; case Cond_LT: if (LHS < RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be less than: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + showDifference(LHS, RHS, LHSStr, RHSStr, File, Line, "less than"); return false; case Cond_LE: if (LHS <= RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be less than or equal to: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + showDifference(LHS, RHS, LHSStr, RHSStr, File, Line, + "less than or equal to"); return false; case Cond_GT: if (LHS > RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be greater than: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + showDifference(LHS, RHS, LHSStr, RHSStr, File, Line, "greater than"); return false; case Cond_GE: if (LHS >= RHS) return true; Ctx.markFail(); - llvm::outs() << File << ":" << Line << ": FAILURE\n" - << " Expected: " << LHSStr << '\n' - << " Which is: " << LHS << '\n' - << "To be greater than or equal to: " << RHSStr << '\n' - << " Which is: " << RHS << '\n'; + showDifference(LHS, RHS, LHSStr, RHSStr, File, Line, + "greater than or equal to"); return false; default: Ctx.markFail(); @@ -218,6 +238,26 @@ unsigned long long RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line); +template bool Test::test<__uint128_t, 0>(RunContext &Ctx, TestCondition Cond, + __uint128_t LHS, __uint128_t RHS, + const char *LHSStr, const char *RHSStr, + const char *File, unsigned long Line); + +template bool Test::test(RunContext &Ctx, TestCondition Cond, + float LHS, float RHS, const char *LHSStr, + const char *RHSStr, const char *File, + unsigned long Line); + +template bool Test::test(RunContext &Ctx, TestCondition Cond, + double LHS, double RHS, const char *LHSStr, + const char *RHSStr, const char *File, + unsigned long Line); + +template bool Test::test(RunContext &Ctx, TestCondition Cond, + long double LHS, long double RHS, + const char *LHSStr, const char *RHSStr, + const char *File, unsigned long Line); + bool Test::testStrEq(RunContext &Ctx, const char *LHS, const char *RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { diff --git a/libc/utils/testutils/CMakeLists.txt b/libc/utils/testutils/CMakeLists.txt --- a/libc/utils/testutils/CMakeLists.txt +++ b/libc/utils/testutils/CMakeLists.txt @@ -7,6 +7,7 @@ libc_test_utils StreamWrapper.cpp StreamWrapper.h + StringExtras.h ${EFFile} ExecuteFunction.h ${FDReaderFile} @@ -14,3 +15,4 @@ LINK_COMPONENTS Support ) +target_include_directories(libc_test_utils PUBLIC ${LIBC_SOURCE_DIR}) diff --git a/libc/utils/testutils/StreamWrapper.cpp b/libc/utils/testutils/StreamWrapper.cpp --- a/libc/utils/testutils/StreamWrapper.cpp +++ b/libc/utils/testutils/StreamWrapper.cpp @@ -7,11 +7,23 @@ //===----------------------------------------------------------------------===// #include "StreamWrapper.h" +#include "StringExtras.h" +#include "utils/FPUtil/FPBits.h" + +#include "llvm/ADT/StringRef.h" #include "llvm/Support/raw_ostream.h" #include #include #include +std::string paddingZero(const std::string &s, size_t N) { + if (s.size() >= N) { + return s; + } + + return std::string(N - s.size(), '0') + s; +} + namespace __llvm_libc { namespace testutils { @@ -24,6 +36,58 @@ return *this; } +// To display the decimals of a __uint128_t, we divide the input into 3 chunks +// mod 10^13, since log10(2^128) ~ 38.5 < 39. Furthermore, log2(10^13) ~ 43.2 +// < 64, so each chunk can be properly contained with uint64_t and displayed +// natively. +// TODO: support hex and oct format +template <> +StreamWrapper &StreamWrapper::operator<<<__uint128_t>(__uint128_t t) { + assert(OS); + llvm::raw_ostream &Stream = *reinterpret_cast(OS); + + const __uint128_t chunk = 10'000'000'000'000ULL; + uint64_t bottom = static_cast(t % chunk); + t /= chunk; + uint64_t middle = static_cast(t % chunk); + uint64_t top = static_cast(t / chunk); + + if (top) { + Stream << top << paddingZero(std::to_string(middle), 13) + << paddingZero(std::to_string(bottom), 13); + } else if (middle) { + Stream << middle << paddingZero(std::to_string(bottom), 13); + } else { + Stream << bottom; + } + + return *this; +} + +// Adapted from write_double in llvm/lib/Support/NativeFormatting.cpp +template <> +StreamWrapper &StreamWrapper::operator<<(long double t) { + assert(OS); + llvm::raw_ostream &Stream = *reinterpret_cast(OS); + + fputil::FPBits bits(t); + + if (bits.isNaN()) { + Stream << "nan"; + } else if (bits.isInf()) { + if (bits.sign) { + Stream << "-"; + } + Stream << "INF"; + } else { + char Buf[32]; + snprintf(Buf, sizeof(Buf), "%.30Le", t); + Stream << Buf; + } + + return *this; +} + template StreamWrapper &StreamWrapper::operator<<(void *t); template StreamWrapper &StreamWrapper::operator<<(const char *t); template StreamWrapper &StreamWrapper::operator<<(char *t); @@ -42,7 +106,13 @@ template StreamWrapper & StreamWrapper::operator<<(unsigned long long t); template StreamWrapper &StreamWrapper::operator<<(bool t); + +template StreamWrapper &StreamWrapper::operator<<(float t); +template StreamWrapper &StreamWrapper::operator<<(double t); + template StreamWrapper &StreamWrapper::operator<<(std::string t); +template StreamWrapper & + StreamWrapper::operator<<(llvm::StringRef t); } // namespace testutils } // namespace __llvm_libc diff --git a/libc/utils/testutils/StringExtras.h b/libc/utils/testutils/StringExtras.h new file mode 100644 --- /dev/null +++ b/libc/utils/testutils/StringExtras.h @@ -0,0 +1,40 @@ +//===-- StringExtras.h -----------------------------------------*- 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_UTILS_TESTUTILS_STRINGEXTRAS_H +#define LLVM_LIBC_UTILS_TESTUTILS_STRINGEXTRAS_H + +#include "utils/CPP/TypeTraits.h" + +#include "llvm/ADT/StringExtras.h" + +namespace llvm { + +// Adapted from llvm/ADT/StringExtras.h +template ::Value, int> = 0> +std::string utohexstr(T X, bool LowerCase = false) { + char Buffer[33]; + char *BufPtr = std::end(Buffer); + + if (X == 0) + *--BufPtr = '0'; + + while (X) { + unsigned char Mod = static_cast(X) & 15; + *--BufPtr = llvm::hexdigit(Mod, LowerCase); + X >>= 4; + } + + return std::string(BufPtr, std::end(Buffer)); +} + +} // namespace llvm + +#endif // LLVM_LIBC_UTILS_TESTUTILS_STRINGEXTRAS_H