More tests of printf %f.
[gnulib.git] / tests / test-snprintf-posix.h
index 8803edf..4036689 100644 (file)
@@ -29,6 +29,17 @@ NaN ()
 # define NaN() (0.0 / 0.0)
 #endif
 
+static int
+strmatch (const char *pattern, const char *string)
+{
+  if (strlen (pattern) != strlen (string))
+    return 0;
+  for (; *pattern != '\0'; pattern++, string++)
+    if (*pattern != '*' && *string != *pattern)
+      return 0;
+  return 1;
+}
+
 static void
 test_function (int (*my_snprintf) (char *, size_t, const char *, ...))
 {
@@ -633,6 +644,95 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...))
     ASSERT (retval == strlen (result));
   }
 
+  { /* Small and large positive numbers.  */
+    static struct { double value; const char *string; } data[] =
+      {
+       { 1.234321234321234e-37, "0.000000" },
+       { 1.234321234321234e-36, "0.000000" },
+       { 1.234321234321234e-35, "0.000000" },
+       { 1.234321234321234e-34, "0.000000" },
+       { 1.234321234321234e-33, "0.000000" },
+       { 1.234321234321234e-32, "0.000000" },
+       { 1.234321234321234e-31, "0.000000" },
+       { 1.234321234321234e-30, "0.000000" },
+       { 1.234321234321234e-29, "0.000000" },
+       { 1.234321234321234e-28, "0.000000" },
+       { 1.234321234321234e-27, "0.000000" },
+       { 1.234321234321234e-26, "0.000000" },
+       { 1.234321234321234e-25, "0.000000" },
+       { 1.234321234321234e-24, "0.000000" },
+       { 1.234321234321234e-23, "0.000000" },
+       { 1.234321234321234e-22, "0.000000" },
+       { 1.234321234321234e-21, "0.000000" },
+       { 1.234321234321234e-20, "0.000000" },
+       { 1.234321234321234e-19, "0.000000" },
+       { 1.234321234321234e-18, "0.000000" },
+       { 1.234321234321234e-17, "0.000000" },
+       { 1.234321234321234e-16, "0.000000" },
+       { 1.234321234321234e-15, "0.000000" },
+       { 1.234321234321234e-14, "0.000000" },
+       { 1.234321234321234e-13, "0.000000" },
+       { 1.234321234321234e-12, "0.000000" },
+       { 1.234321234321234e-11, "0.000000" },
+       { 1.234321234321234e-10, "0.000000" },
+       { 1.234321234321234e-9, "0.000000" },
+       { 1.234321234321234e-8, "0.000000" },
+       { 1.234321234321234e-7, "0.000000" },
+       { 1.234321234321234e-6, "0.000001" },
+       { 1.234321234321234e-5, "0.000012" },
+       { 1.234321234321234e-4, "0.000123" },
+       { 1.234321234321234e-3, "0.001234" },
+       { 1.234321234321234e-2, "0.012343" },
+       { 1.234321234321234e-1, "0.123432" },
+       { 1.234321234321234, "1.234321" },
+       { 1.234321234321234e1, "12.343212" },
+       { 1.234321234321234e2, "123.432123" },
+       { 1.234321234321234e3, "1234.321234" },
+       { 1.234321234321234e4, "12343.212343" },
+       { 1.234321234321234e5, "123432.123432" },
+       { 1.234321234321234e6, "1234321.234321" },
+       { 1.234321234321234e7, "12343212.343212" },
+       { 1.234321234321234e8, "123432123.432123" },
+       { 1.234321234321234e9, "1234321234.321234" },
+       { 1.234321234321234e10, "12343212343.2123**" },
+       { 1.234321234321234e11, "123432123432.123***" },
+       { 1.234321234321234e12, "1234321234321.23****" },
+       { 1.234321234321234e13, "12343212343212.3*****" },
+       { 1.234321234321234e14, "123432123432123.******" },
+       { 1.234321234321234e15, "1234321234321234.000000" },
+       { 1.234321234321234e16, "123432123432123**.000000" },
+       { 1.234321234321234e17, "123432123432123***.000000" },
+       { 1.234321234321234e18, "123432123432123****.000000" },
+       { 1.234321234321234e19, "123432123432123*****.000000" },
+       { 1.234321234321234e20, "123432123432123******.000000" },
+       { 1.234321234321234e21, "123432123432123*******.000000" },
+       { 1.234321234321234e22, "123432123432123********.000000" },
+       { 1.234321234321234e23, "123432123432123*********.000000" },
+       { 1.234321234321234e24, "123432123432123**********.000000" },
+       { 1.234321234321234e25, "123432123432123***********.000000" },
+       { 1.234321234321234e26, "123432123432123************.000000" },
+       { 1.234321234321234e27, "123432123432123*************.000000" },
+       { 1.234321234321234e28, "123432123432123**************.000000" },
+       { 1.234321234321234e29, "123432123432123***************.000000" },
+       { 1.234321234321234e30, "123432123432123****************.000000" },
+       { 1.234321234321234e31, "123432123432123*****************.000000" },
+       { 1.234321234321234e32, "123432123432123******************.000000" },
+       { 1.234321234321234e33, "123432123432123*******************.000000" },
+       { 1.234321234321234e34, "123432123432123********************.000000" },
+       { 1.234321234321234e35, "123432123432123*********************.000000" },
+       { 1.234321234321234e36, "123432123432123**********************.000000" }
+      };
+    size_t k;
+    for (k = 0; k < SIZEOF (data); k++)
+      {
+       char result[100];
+       int retval =
+         my_snprintf (result, sizeof (result), "%f", data[k].value);
+       ASSERT (strmatch (data[k].string, result));
+       ASSERT (retval == strlen (result));
+      }
+  }
+
   { /* A negative number.  */
     char result[100];
     int retval =
@@ -786,6 +886,95 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...))
     ASSERT (retval == strlen (result));
   }
 
+  { /* Small and large positive numbers.  */
+    static struct { long double value; const char *string; } data[] =
+      {
+       { 1.234321234321234e-37L, "0.000000" },
+       { 1.234321234321234e-36L, "0.000000" },
+       { 1.234321234321234e-35L, "0.000000" },
+       { 1.234321234321234e-34L, "0.000000" },
+       { 1.234321234321234e-33L, "0.000000" },
+       { 1.234321234321234e-32L, "0.000000" },
+       { 1.234321234321234e-31L, "0.000000" },
+       { 1.234321234321234e-30L, "0.000000" },
+       { 1.234321234321234e-29L, "0.000000" },
+       { 1.234321234321234e-28L, "0.000000" },
+       { 1.234321234321234e-27L, "0.000000" },
+       { 1.234321234321234e-26L, "0.000000" },
+       { 1.234321234321234e-25L, "0.000000" },
+       { 1.234321234321234e-24L, "0.000000" },
+       { 1.234321234321234e-23L, "0.000000" },
+       { 1.234321234321234e-22L, "0.000000" },
+       { 1.234321234321234e-21L, "0.000000" },
+       { 1.234321234321234e-20L, "0.000000" },
+       { 1.234321234321234e-19L, "0.000000" },
+       { 1.234321234321234e-18L, "0.000000" },
+       { 1.234321234321234e-17L, "0.000000" },
+       { 1.234321234321234e-16L, "0.000000" },
+       { 1.234321234321234e-15L, "0.000000" },
+       { 1.234321234321234e-14L, "0.000000" },
+       { 1.234321234321234e-13L, "0.000000" },
+       { 1.234321234321234e-12L, "0.000000" },
+       { 1.234321234321234e-11L, "0.000000" },
+       { 1.234321234321234e-10L, "0.000000" },
+       { 1.234321234321234e-9L, "0.000000" },
+       { 1.234321234321234e-8L, "0.000000" },
+       { 1.234321234321234e-7L, "0.000000" },
+       { 1.234321234321234e-6L, "0.000001" },
+       { 1.234321234321234e-5L, "0.000012" },
+       { 1.234321234321234e-4L, "0.000123" },
+       { 1.234321234321234e-3L, "0.001234" },
+       { 1.234321234321234e-2L, "0.012343" },
+       { 1.234321234321234e-1L, "0.123432" },
+       { 1.234321234321234L, "1.234321" },
+       { 1.234321234321234e1L, "12.343212" },
+       { 1.234321234321234e2L, "123.432123" },
+       { 1.234321234321234e3L, "1234.321234" },
+       { 1.234321234321234e4L, "12343.212343" },
+       { 1.234321234321234e5L, "123432.123432" },
+       { 1.234321234321234e6L, "1234321.234321" },
+       { 1.234321234321234e7L, "12343212.343212" },
+       { 1.234321234321234e8L, "123432123.432123" },
+       { 1.234321234321234e9L, "1234321234.321234" },
+       { 1.234321234321234e10L, "12343212343.2123**" },
+       { 1.234321234321234e11L, "123432123432.123***" },
+       { 1.234321234321234e12L, "1234321234321.23****" },
+       { 1.234321234321234e13L, "12343212343212.3*****" },
+       { 1.234321234321234e14L, "123432123432123.******" },
+       { 1.234321234321234e15L, "1234321234321234.000000" },
+       { 1.234321234321234e16L, "123432123432123**.000000" },
+       { 1.234321234321234e17L, "123432123432123***.000000" },
+       { 1.234321234321234e18L, "123432123432123****.000000" },
+       { 1.234321234321234e19L, "123432123432123*****.000000" },
+       { 1.234321234321234e20L, "123432123432123******.000000" },
+       { 1.234321234321234e21L, "123432123432123*******.000000" },
+       { 1.234321234321234e22L, "123432123432123********.000000" },
+       { 1.234321234321234e23L, "123432123432123*********.000000" },
+       { 1.234321234321234e24L, "123432123432123**********.000000" },
+       { 1.234321234321234e25L, "123432123432123***********.000000" },
+       { 1.234321234321234e26L, "123432123432123************.000000" },
+       { 1.234321234321234e27L, "123432123432123*************.000000" },
+       { 1.234321234321234e28L, "123432123432123**************.000000" },
+       { 1.234321234321234e29L, "123432123432123***************.000000" },
+       { 1.234321234321234e30L, "123432123432123****************.000000" },
+       { 1.234321234321234e31L, "123432123432123*****************.000000" },
+       { 1.234321234321234e32L, "123432123432123******************.000000" },
+       { 1.234321234321234e33L, "123432123432123*******************.000000" },
+       { 1.234321234321234e34L, "123432123432123********************.000000" },
+       { 1.234321234321234e35L, "123432123432123*********************.000000" },
+       { 1.234321234321234e36L, "123432123432123**********************.000000" }
+      };
+    size_t k;
+    for (k = 0; k < SIZEOF (data); k++)
+      {
+       char result[100];
+       int retval =
+         my_snprintf (result, sizeof (result), "%Lf", data[k].value);
+       ASSERT (strmatch (data[k].string, result));
+       ASSERT (retval == strlen (result));
+      }
+  }
+
   { /* A negative number.  */
     char result[100];
     int retval =