Skip to content
Permalink
Browse files

tests: sprintf: Add inf/nan testing for %{e,E,g,G}

Added testing to make sure we get the proper results from %e, %E, %g and
%G conversion specifiers.

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
  • Loading branch information...
galak committed Jul 12, 2018
1 parent e66da3f commit 8317e366a8a6182079ae2781a0e9163023f733fe
Showing with 64 additions and 0 deletions.
  1. +64 −0 tests/lib/sprintf/src/main.c
@@ -73,6 +73,14 @@ void test_sprintf_double(void)

var.u1 = 0x00000000;
var.u2 = 0x7ff00000; /* Bit pattern for +INF (double) */
sprintf(buffer, "%e", var.d);
zassert_true((strcmp(buffer, "inf") == 0),
"sprintf(inf) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%E", var.d);
zassert_true((strcmp(buffer, "INF") == 0),
"sprintf(INF) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%f", var.d);
zassert_true((strcmp(buffer, "inf") == 0),
"sprintf(inf) - incorrect output '%s'\n", buffer);
@@ -81,8 +89,24 @@ void test_sprintf_double(void)
zassert_true((strcmp(buffer, "INF") == 0),
"sprintf(INF) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%g", var.d);
zassert_true((strcmp(buffer, "inf") == 0),
"sprintf(inf) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%G", var.d);
zassert_true((strcmp(buffer, "INF") == 0),
"sprintf(INF) - incorrect output '%s'\n", buffer);

var.u1 = 0x00000000;
var.u2 = 0xfff00000; /* Bit pattern for -INF (double) */
sprintf(buffer, "%e", var.d);
zassert_true((strcmp(buffer, "-inf") == 0),
"sprintf(-INF) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%E", var.d);
zassert_true((strcmp(buffer, "-INF") == 0),
"sprintf(-INF) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%f", var.d);
zassert_true((strcmp(buffer, "-inf") == 0),
"sprintf(-INF) - incorrect output '%s'\n", buffer);
@@ -91,8 +115,24 @@ void test_sprintf_double(void)
zassert_true((strcmp(buffer, "-INF") == 0),
"sprintf(-INF) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%g", var.d);
zassert_true((strcmp(buffer, "-inf") == 0),
"sprintf(-INF) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%G", var.d);
zassert_true((strcmp(buffer, "-INF") == 0),
"sprintf(-INF) - incorrect output '%s'\n", buffer);

var.u1 = 0x00000000;
var.u2 = 0x7ff80000; /* Bit pattern for NaN (double) */
sprintf(buffer, "%e", var.d);
zassert_true((strcmp(buffer, "nan") == 0),
"sprintf(nan) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%E", var.d);
zassert_true((strcmp(buffer, "NAN") == 0),
"sprintf(NAN) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%f", var.d);
zassert_true((strcmp(buffer, "nan") == 0),
"sprintf(nan) - incorrect output '%s'\n", buffer);
@@ -101,8 +141,24 @@ void test_sprintf_double(void)
zassert_true((strcmp(buffer, "NAN") == 0),
"sprintf(NAN) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%g", var.d);
zassert_true((strcmp(buffer, "nan") == 0),
"sprintf(nan) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%G", var.d);
zassert_true((strcmp(buffer, "NAN") == 0),
"sprintf(NAN) - incorrect output '%s'\n", buffer);

var.u1 = 0x00000000;
var.u2 = 0xfff80000; /* Bit pattern for -NaN (double) */
sprintf(buffer, "%e", var.d);
zassert_true((strcmp(buffer, "-nan") == 0),
"sprintf(-nan) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%E", var.d);
zassert_true((strcmp(buffer, "-NAN") == 0),
"sprintf(-NAN) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%f", var.d);
zassert_true((strcmp(buffer, "-nan") == 0),
"sprintf(-nan) - incorrect output '%s'\n", buffer);
@@ -111,6 +167,14 @@ void test_sprintf_double(void)
zassert_true((strcmp(buffer, "-NAN") == 0),
"sprintf(-NAN) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%g", var.d);
zassert_true((strcmp(buffer, "-nan") == 0),
"sprintf(-nan) - incorrect output '%s'\n", buffer);

sprintf(buffer, "%G", var.d);
zassert_true((strcmp(buffer, "-NAN") == 0),
"sprintf(-NAN) - incorrect output '%s'\n", buffer);

var.d = 1.0;
sprintf(buffer, "%f", var.d);
zassert_true((strcmp(buffer, "1.000000") == 0),

0 comments on commit 8317e36

Please sign in to comment.
You can’t perform that action at this time.