changeset 317:c532088b4f05

Fix float tests (type was int) :D
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 22 Feb 2016 23:43:50 +0200
parents d4a069014899
children f8c385739571
files tests.c th_string.c
diffstat 2 files changed, 31 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/tests.c	Mon Feb 22 23:42:29 2016 +0200
+++ b/tests.c	Mon Feb 22 23:43:50 2016 +0200
@@ -323,7 +323,7 @@
 
     if (test_set_start("printf() float"))
     {
-        int f_vals[] = { 2.02, 612342.234, -2.07, -612342.12, 0x1fff, 0x8000000, };
+        double f_vals[] = { 2.02, 612342.234, -2.07, -612342.12, 437692.9876543219, 0x1fff, 0x8000000, };
         char *f_fmts[] = { "%f", "%1.1f", "%5.5f", "%5f", "%-5f", "", "%-2.2f", "%05.5f" };
 
         for (i1 = 0; i1 < sizeof(f_vals) / sizeof(f_vals[0]); i1++)
--- a/th_string.c	Mon Feb 22 23:42:29 2016 +0200
+++ b/th_string.c	Mon Feb 22 23:43:50 2016 +0200
@@ -164,10 +164,26 @@
     // Calculate necessary padding, if any
     int ret, nwidth = f_width - pos;
 
+    if (f_prec > 0 && f_prec > pos)
+    {
+        nwidth = nwidth - f_prec + 1;
+        f_flags &= ~TH_PF_ZERO;
+    }
+
     // Prefix padding?
     if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags)) == EOF)
         return ret;
 
+    if (f_prec > 0 && f_prec > pos)
+    {
+        while (--f_prec)
+        {
+            int ret;
+            if ((ret = vputch(ctx, '0')) == EOF)
+                return ret;
+        }
+    }
+
     // Output the value
     while (pos--)
     {
@@ -202,19 +218,25 @@
 static int th_printf_vput_str(th_printf_ctx *ctx, th_printf_vputch vputch,
     const char *str, int f_flags, const int f_width, const int f_prec)
 {
-    int nwidth, ret = 0;
+    int nwidth, slen, ret = 0;
+
+    f_flags &= ~TH_PF_ZERO;
 
     // Check for null strings
     if (str == NULL)
         str = "(null)";
 
-    nwidth = f_width - strlen(str);
+    slen = strlen(str);
+    if (f_prec >= 0 && slen > f_prec)
+        slen = f_prec;
+    
+    nwidth = f_width - slen;
 
     // Prefix padding?
     if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags)) == EOF)
         goto out;
 
-    while (*str)
+    while (*str && slen--)
     {
         if ((ret = vputch(ctx, *str++)) == EOF)
             goto out;
@@ -363,7 +385,12 @@
                 case 'f':
                 case 'F':
                     return -112;
+/*
+                    if ((ret = th_vput_float(ctx, vputch, va_arg(ap, double),
+                        f_flags, f_width, f_prec)) == EOF)
+                        goto out;
                     break;
+*/
 
                 case 's':
                     if ((ret = th_printf_vput_str(ctx, vputch, va_arg(ap, char *),