From 00e65de9d83f846af732e3ace3f48d43d67b13d1 Mon Sep 17 00:00:00 2001 From: John McCall Date: Thu, 24 Dec 2009 08:56:26 +0000 Subject: Add accessors for the largest-magnitude, smallest-magnitude, and smallest-normalized-magnitude values in a given FP semantics. Provide an APFloat-to-string conversion which I am quite ready to admit could be much more efficient. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92126 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/APFloatTest.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'unittests/ADT') diff --git a/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp index 92f020b382..d2a977a05c 100644 --- a/unittests/ADT/APFloatTest.cpp +++ b/unittests/ADT/APFloatTest.cpp @@ -8,10 +8,12 @@ //===----------------------------------------------------------------------===// #include +#include #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/SmallVector.h" using namespace llvm; @@ -21,6 +23,13 @@ static double convertToDoubleFromString(const char *Str) { return F.convertToDouble(); } +static std::string convertToString(double d, unsigned Prec, unsigned Pad) { + llvm::SmallVector Buffer; + llvm::APFloat F(d); + F.toString(Buffer, Prec, Pad); + return std::string(Buffer.data(), Buffer.size()); +} + namespace { TEST(APFloatTest, Zero) { @@ -313,6 +322,17 @@ TEST(APFloatTest, fromHexadecimalString) { EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828")); } +TEST(APFloatTest, toString) { + ASSERT_EQ("10", convertToString(10.0, 6, 3)); + ASSERT_EQ("1.0E+1", convertToString(10.0, 6, 0)); + ASSERT_EQ("10100", convertToString(1.01E+4, 5, 2)); + ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 4, 2)); + ASSERT_EQ("1.01E+4", convertToString(1.01E+4, 5, 1)); + ASSERT_EQ("0.0101", convertToString(1.01E-2, 5, 2)); + ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 4, 2)); + ASSERT_EQ("1.01E-2", convertToString(1.01E-2, 5, 1)); +} + #ifdef GTEST_HAS_DEATH_TEST TEST(APFloatTest, SemanticsDeath) { EXPECT_DEATH(APFloat(APFloat::IEEEsingle, 0.0f).convertToDouble(), "Float semantics are not IEEEdouble"); -- cgit v1.2.3