From e138ad41e0e6609bf77df4859de1e3ac3d19c466 Mon Sep 17 00:00:00 2001 From: Tue Ly Date: Thu, 16 Jul 2020 02:29:08 -0400 Subject: Update Test (EXPECT_EQ and friends) to accept __uint128_t and floating point types (float, double, long double). Summary: Update Test (EXPECT_EQ and friends) to accept __uint128_t and floating point types (float, double, long double). Reviewers: sivachandra Subscribers: mgorny, libc-commits Tags: #libc-project Differential Revision: https://reviews.llvm.org/D83931 --- libc/utils/CPP/TypeTraits.h | 8 ++- libc/utils/UnitTest/Test.cpp | 154 +++++++++++++++++++++++++++++++++---------- libc/utils/UnitTest/Test.h | 2 +- 3 files changed, 129 insertions(+), 35 deletions(-) (limited to 'libc/utils') diff --git a/libc/utils/CPP/TypeTraits.h b/libc/utils/CPP/TypeTraits.h index 9b121c0..1f2c85a 100644 --- a/libc/utils/CPP/TypeTraits.h +++ b/libc/utils/CPP/TypeTraits.h @@ -49,7 +49,8 @@ template struct IsIntegral { IsSameV || IsSameV || IsSameV || IsSameV || IsSameV || IsSameV || - IsSameV || IsSameV; + IsSameV || IsSameV || + IsSameV<__uint128_t, TypeNoCV>; }; template struct IsPointerTypeNoCV : public FalseValue {}; @@ -65,6 +66,11 @@ template struct IsFloatingPointType { IsSame::Value; }; +template struct IsArithmetic { + static constexpr bool Value = + IsIntegral::Value || IsFloatingPointType::Value; +}; + } // namespace cpp } // namespace __llvm_libc diff --git a/libc/utils/UnitTest/Test.cpp b/libc/utils/UnitTest/Test.cpp index b2b70c8..84730ad 100644 --- 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 "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/raw_ostream.h" @@ -32,77 +34,143 @@ private: namespace internal { +// Display the first N hexadecimal digits of an integer in upper case. +template +cpp::EnableIfType::Value, std::string> +uintToHex(T X, size_t Length = sizeof(T) * 2) { + std::string s(Length, '0'); + + for (auto it = s.rbegin(), end = s.rend(); it != end; ++it, X >>= 4) { + unsigned char Mod = static_cast(X) & 15; + *it = llvm::hexdigit(Mod, true); + } + + return s; +} + +// When the value is not floating-point type, just display it as normal. +template +cpp::EnableIfType::Value, std::string> +describeValue(ValType Value) { + return std::to_string(Value); +} + +template <> std::string describeValue(llvm::StringRef Value) { + return std::string(Value); +} + +// When the value is __uint128_t, also show its hexadecimal digits. +// Using template to force exact match, prevent ambiguous promotion. +template <> std::string describeValue<__uint128_t>(__uint128_t Value) { + return "0x" + uintToHex(Value); +} + +// When the value is a floating point type, also show its sign | exponent | +// mantissa. +template +cpp::EnableIfType::Value, std::string> +describeValue(ValType Value) { + fputil::FPBits Bits(Value); + + if (Bits.isNaN()) { + return "(NaN)"; + } else if (Bits.isInf()) { + return Bits.sign ? "(-Infinity)" : "(+Infinity)"; + } else { + constexpr int ExponentWidthInHex = + (fputil::ExponentWidth::value - 1) / 4 + 1; + constexpr int MantissaWidthInHex = + (fputil::MantissaWidth::value - 1) / 4 + 1; + + return std::string("Sign: ") + (Bits.sign ? '1' : '0') + ", Exponent: 0x" + + uintToHex(Bits.exponent, ExponentWidthInHex) + + ", Mantissa: 0x" + + uintToHex::UIntType>( + Bits.mantissa, MantissaWidthInHex); + } +} + +template +void explainDifference(ValType LHS, ValType RHS, const char *LHSStr, + const char *RHSStr, const char *File, unsigned long Line, + llvm::StringRef OpString) { + size_t OffsetLength = OpString.size() > 2 ? OpString.size() - 2 : 0; + std::string Offset(OffsetLength, ' '); + + llvm::outs() << File << ":" << Line << ": FAILURE\n" + << Offset << "Expected: " << LHSStr << '\n' + << Offset << "Which is: " << describeValue(LHS) << '\n' + << "To be " << OpString << ": " << RHSStr << '\n' + << Offset << "Which is: " << describeValue(RHS) << '\n'; +} + +template +cpp::EnableIfType::Value, bool> +testEQ(ValType LHS, ValType RHS) { + return LHS == RHS; +} + +// For floating points, we consider all NaNs are equal, and +0.0 is not equal to +// -0.0. +template +cpp::EnableIfType::Value, bool> +testEQ(ValType LHS, ValType RHS) { + fputil::FPBits LHSBits(LHS), RHSBits(RHS); + + return (LHSBits.isNaN() && RHSBits.isNaN()) || + (LHSBits.bitsAsUInt() == RHSBits.bitsAsUInt()); +} + template bool test(RunContext &Ctx, TestCondition Cond, ValType LHS, ValType RHS, const char *LHSStr, const char *RHSStr, const char *File, unsigned long Line) { + auto ExplainDifference = [=](llvm::StringRef OpString) { + explainDifference(LHS, RHS, LHSStr, RHSStr, File, Line, OpString); + }; + switch (Cond) { case Cond_EQ: - if (LHS == RHS) + if (testEQ(LHS, RHS)) 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'; - + ExplainDifference("equal to"); return false; case Cond_NE: - if (LHS != RHS) + if (!testEQ(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'; + ExplainDifference("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'; + ExplainDifference("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'; + ExplainDifference("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'; + ExplainDifference("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'; + ExplainDifference("greater than or equal to"); return false; default: Ctx.markFail(); @@ -218,6 +286,26 @@ template bool Test::test( 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/UnitTest/Test.h b/libc/utils/UnitTest/Test.h index fbed1d6..ec92bd2 100644 --- a/libc/utils/UnitTest/Test.h +++ b/libc/utils/UnitTest/Test.h @@ -78,7 +78,7 @@ protected: // |Cond| on mismatched |LHS| and |RHS| types can potentially succeed because // of type promotion. template ::Value, ValType> = 0> + cpp::EnableIfType::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) { -- cgit v1.1