Skip to content

Commit

Permalink
re ulfjack#181: add tests detecting misbehavior with trailing decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Oct 12, 2020
1 parent aa31ca9 commit 00d91c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 13 additions & 2 deletions ryu/tests/s2d_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "ryu/ryu_parse.h"
#include "third_party/gtest/gtest.h"

#define EXPECT_S2D(a, b) { double value; EXPECT_EQ(SUCCESS, s2d(b, &value)); EXPECT_EQ(a, value); } while (0);
#define EXPECT_S2D(a, b) { double value; EXPECT_EQ(SUCCESS, s2d(b, &value)) << "str=" << b; EXPECT_EQ(a, value) << "str=" << b; } while (0);

TEST(S2dTest, BadInput) {
double value;
Expand Down Expand Up @@ -96,4 +96,15 @@ TEST(S2dTest, Issue173) {
EXPECT_S2D(2.2250738585072012e-308, "2.2250738585072012e-308");
EXPECT_S2D(2.2250738585072013e-308, "2.2250738585072013e-308");
EXPECT_S2D(2.2250738585072014e-308, "2.2250738585072014e-308");
}
}

TEST(S2dTest, TrailingDecimalZeros) {
EXPECT_S2D(1. , "1");
EXPECT_S2D(1. , "1.000");
EXPECT_S2D(1. , "1.0000000000000000");
EXPECT_S2D(1. , "1.00000000000000000"); // fail: INPUT_TOO_LONG
EXPECT_S2D(8388605., "8388605");
EXPECT_S2D(8388605., "8388605.000");
EXPECT_S2D(8388605., "8388605.0000000000");
EXPECT_S2D(8388605., "8388605.00000000000"); // fail: INPUT_TOO_LONG
}
11 changes: 10 additions & 1 deletion ryu/tests/s2f_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "ryu/ryu_parse.h"
#include "third_party/gtest/gtest.h"

#define EXPECT_S2F(a, b) do { float value; EXPECT_EQ(SUCCESS, s2f(b, &value)); EXPECT_EQ(a, value); } while (0);
#define EXPECT_S2F(a, b) do { float value; EXPECT_EQ(SUCCESS, s2f(b, &value)) << "str=" << b; EXPECT_EQ(a, value) << "str=" << b; } while (0);

TEST(S2fTest, Basic) {
EXPECT_S2F(0.0f, "0");
Expand Down Expand Up @@ -53,3 +53,12 @@ TEST(S2fTest, TrailingZeros) {
EXPECT_S2F(50000004.0f, "50000002.5");
EXPECT_S2F(99999992.0f, "99999989.5");
}

TEST(S2fTest, TrailingDecimalZeros) {
EXPECT_S2F(1.f, "1");
EXPECT_S2F(1.f, "1.000");
EXPECT_S2F(1.f, "1.000000000"); // fail: INPUT_TOO_LONG
EXPECT_S2F(8388605.f, "8388605");
EXPECT_S2F(8388605.f, "8388605.00");
EXPECT_S2F(8388605.f, "8388605.000"); // fail: INPUT_TOO_LONG
}

0 comments on commit 00d91c7

Please sign in to comment.