summaryrefslogtreecommitdiff
path: root/unittests/ADT
diff options
context:
space:
mode:
authorJohn McCall <rjmccall@apple.com>2009-12-24 08:56:26 +0000
committerJohn McCall <rjmccall@apple.com>2009-12-24 08:56:26 +0000
commit00e65de9d83f846af732e3ace3f48d43d67b13d1 (patch)
tree42c408ec6150bfa417d15fa1bf930cafa6d01f65 /unittests/ADT
parentd73bf5987a0855e677fcefba74334c6a85b0b326 (diff)
downloadllvm-00e65de9d83f846af732e3ace3f48d43d67b13d1.tar.gz
llvm-00e65de9d83f846af732e3ace3f48d43d67b13d1.tar.bz2
llvm-00e65de9d83f846af732e3ace3f48d43d67b13d1.tar.xz
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
Diffstat (limited to 'unittests/ADT')
-rw-r--r--unittests/ADT/APFloatTest.cpp20
1 files changed, 20 insertions, 0 deletions
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 <ostream>
+#include <string>
#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<char, 100> 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");