From 79bf86d9739269cd124b550c3177bb9f7a9d8566 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Mon, 7 Apr 2014 23:15:31 +0200 Subject: [PATCH] printf %g regression tests --- src/regression/printf-1e9-oob.c | 2 ++ src/regression/printf-fmt-g-round.c | 21 +++++++++++++++++++++ src/regression/printf-fmt-g-zeros.c | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/regression/printf-fmt-g-round.c create mode 100644 src/regression/printf-fmt-g-zeros.c diff --git a/src/regression/printf-1e9-oob.c b/src/regression/printf-1e9-oob.c index 3d7dccf..d1d0b2e 100644 --- a/src/regression/printf-1e9-oob.c +++ b/src/regression/printf-1e9-oob.c @@ -1,3 +1,5 @@ +// commit 109048e031f39fbb370211fde44ababf6c04c8fb 2014-04-07 +// float printf out-of-bounds access #include #include #include "test.h" diff --git a/src/regression/printf-fmt-g-round.c b/src/regression/printf-fmt-g-round.c new file mode 100644 index 0000000..c5f9a08 --- /dev/null +++ b/src/regression/printf-fmt-g-round.c @@ -0,0 +1,21 @@ +// commit e94d0692864ecf9522fd6a97610a47a2f718d3de 2014-04-07 +// %g midpoint cases should be rounded to even +#include +#include +#include "test.h" + +static void t(const char *fmt, double d, const char *want) +{ + char buf[256]; + int n = strlen(want); + int r = snprintf(buf, sizeof buf, fmt, d); + if (r != n || memcmp(buf, want, n+1) != 0) + t_error("snprintf(\"%s\", %f) want %s got %s\n", fmt, d, want, buf); +} + +int main() +{ + t("%.12g", 1000000000005.0, "1e+12"); + t("%.12g", 100000000002500.0, "1.00000000002e+14"); + return t_status; +} diff --git a/src/regression/printf-fmt-g-zeros.c b/src/regression/printf-fmt-g-zeros.c new file mode 100644 index 0000000..2b08691 --- /dev/null +++ b/src/regression/printf-fmt-g-zeros.c @@ -0,0 +1,21 @@ +// commit 89740868c9f1c84b8ee528468d12df1fa72cd392 2014-04-07 +// %g should not print trailing zeros +#include +#include +#include "test.h" + +static void t(const char *fmt, double d, const char *want) +{ + char buf[256]; + int n = strlen(want); + int r = snprintf(buf, sizeof buf, fmt, d); + if (r != n || memcmp(buf, want, n+1) != 0) + t_error("snprintf(\"%s\",%f) want %s got %s\n", fmt, d, want, buf); +} + +int main() +{ + t("%.50g", 100000000000000.5, "100000000000000.5"); + t("%.50g", 987654321098765.0, "987654321098765"); + return t_status; +} -- 2.20.1