Skip to content

Commit

Permalink
Add support for custom precision in floating point stringification
Browse files Browse the repository at this point in the history
  • Loading branch information
mlimber authored and horenmar committed May 2, 2019
1 parent 9c741fe commit 53a83e8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
25 changes: 23 additions & 2 deletions include/internal/catch_tostring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,32 @@ std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) {
return "nullptr";
}

int StringMaker<float>::m_precision = 5;

std::string StringMaker<float>::convert(float value) {
return fpToString(value, 5) + 'f';
return fpToString(value, m_precision) + 'f';
}

void StringMaker<float>::setPrecision(int precision) {
m_precision = precision;
}

int StringMaker<float>::getPrecision() {
return m_precision;
}

int StringMaker<double>::m_precision = 10;

std::string StringMaker<double>::convert(double value) {
return fpToString(value, 10);
return fpToString(value, m_precision);
}

void StringMaker<double>::setPrecision(int precision) {
m_precision = precision;
}

int StringMaker<double>::getPrecision() {
return m_precision;
}

std::string ratio_string<std::atto>::symbol() { return "a"; }
Expand Down
9 changes: 9 additions & 0 deletions include/internal/catch_tostring.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,19 @@ namespace Catch {
template<>
struct StringMaker<float> {
static std::string convert(float value);
static void setPrecision(int precision);
static int getPrecision();
private:
static int m_precision;
};

template<>
struct StringMaker<double> {
static std::string convert(double value);
static void setPrecision(int precision);
static int getPrecision();
private:
static int m_precision;
};

template <typename T>
Expand Down
23 changes: 23 additions & 0 deletions projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ TEST_CASE("String views are stringified like other strings", "[toString][approva

#endif

//TEMPLATE_TEST_CASE("Floating-point precision can be set", "[toString][floatingPoint]", float, double)
//{
// const auto oldPrecision = Catch::StringMaker<TestType>::getPrecision();
// const auto precision = GENERATE(-1, 0, 3, std::numeric_limits<TestType>::max_digits10);
// const auto expectedLength = unsigned(precision < 0 ? 3 : precision);
//
// CAPTURE( precision );
//
// if (precision >= 0)
// {
// Catch::StringMaker<TestType>::setPrecision(precision);
// }
//
// // Expected to fail to demonstrate the problem
// const auto str = Catch::StringMaker<TestType>::convert(std::numeric_limits<TestType>::epsilon());
// CHECK(str.length() >= expectedLength);
//
// if (precision >= 0)
// {
// Catch::StringMaker<TestType>::setPrecision(oldPrecision);
// }
//}

namespace {

struct WhatException : std::exception {
Expand Down

0 comments on commit 53a83e8

Please sign in to comment.