diff options
| author | Martin Hořeňovský | 2019-05-02 21:32:54 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2019-05-03 15:38:06 +0200 |
| commit | 1161011dd0c8b08ef26aabf99f1e28ab9604108a (patch) | |
| tree | cca4730fe3e76f0e9594047d60595237c65fcc0d /projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp | |
| parent | 53a83e855ec5c2a5659dd4ceacf15c00435eeaa6 (diff) | |
| download | catch3-1161011dd0c8b08ef26aabf99f1e28ab9604108a.tar.gz | |
Refactor custom precision in floating point stringification
Also fixup tests.
Diffstat (limited to 'projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp')
| -rw-r--r-- | projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp index bbcc9a66..69d6320d 100644 --- a/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp +++ b/projects/SelfTest/UsageTests/ToStringGeneral.tests.cpp @@ -128,28 +128,39 @@ 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); -// } -//} +TEST_CASE("Precision of floating point stringification can be set", "[toString][floatingPoint]") { + SECTION("Floats") { + using sm = Catch::StringMaker<float>; + const auto oldPrecision = sm::precision; + + const float testFloat = 1.12345678901234567899f; + auto str1 = sm::convert(testFloat); + sm::precision = 5; + // "1." prefix = 2 chars, f suffix is another char + CHECK(str1.size() == 3 + 5); + + sm::precision = 10; + auto str2 = sm::convert(testFloat); + REQUIRE(str2.size() == 3 + 10); + sm::precision = oldPrecision; + } + SECTION("Double") { + using sm = Catch::StringMaker<double>; + const auto oldPrecision = sm::precision; + + const double testDouble = 1.123456789012345678901234567899; + sm::precision = 5; + auto str1 = sm::convert(testDouble); + // "1." prefix = 2 chars + CHECK(str1.size() == 2 + 5); + + sm::precision = 15; + auto str2 = sm::convert(testDouble); + REQUIRE(str2.size() == 2 + 15); + + sm::precision = oldPrecision; + } +} namespace { |