From 6cdb371a202c15e624a79483b717e1302d84ac9f Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 30 Mar 2011 18:06:15 -0400 Subject: test printing tiny doubles - use ascii arithmetic to check result --- snprintf.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/snprintf.c b/snprintf.c index 8d06add..ea28e18 100644 --- a/snprintf.c +++ b/snprintf.c @@ -91,9 +91,9 @@ static const struct { int test_snprintf(void) { - int i, j; + int i, j, k; int err=0; - char b[500], *s; + char b[2000], *s; TEST(i, snprintf(0, 0, "%d", 123456), 6, "length returned %d != %d"); TEST(i, snprintf(0, 0, "%.4s", "hello"), 4, "length returned %d != %d"); @@ -104,6 +104,20 @@ int test_snprintf(void) TEST_S(b, "123", "incorrect output"); TEST(i, b[5], 'x', "buffer overrun"); + /* Perform ascii arithmetic to test printing tiny doubles */ + TEST(i, snprintf(b, sizeof b, "%.1022f", 0x1p-1021), 1024, "%d != %d"); + b[1] = '0'; + for (i=0; i<1021; i++) { + for (k=0, j=1023; j>0; j--) { + if (b[j]<'5') b[j]+=b[j]-'0'+k, k=0; + else b[j]+=b[j]-'0'-10+k, k=1; + } + } + TEST(i, b[1], '1', "'%c' != '%c'"); + for (j=2; b[j]=='0'; j++); + TEST(i, j, 1024, "%d != %d"); + + #ifndef DISABLE_SLOW_TESTS errno = 0; TEST(i, snprintf(NULL, 0, "%.*u", 2147483647, 0), 2147483647, "cannot print max length %d"); -- cgit v1.2.1