Fix for LP64 architectures; Add some more tests.
authorbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Fri, 4 Nov 2005 17:43:27 +0000 (17:43 +0000)
committerbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Fri, 4 Nov 2005 17:43:27 +0000 (17:43 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@435 38d2e660-2303-0410-9eaa-f027e97ec537

mware/formatwr.c
mware/sprintf_test.c

index 04a50dc8666d587d0cc06d3188c14e8e1f137764..8a2e57e1af027e61a51639bd8e1bca650dcbd548 100755 (executable)
@@ -53,6 +53,9 @@
 
 /*#*
  *#* $Log$
+ *#* Revision 1.17  2005/11/04 17:43:27  bernie
+ *#* Fix for LP64 architectures; Add some more tests.
+ *#*
  *#* Revision 1.16  2005/07/19 07:25:46  bernie
  *#* Use appconfig.h instead of cfg/config.h.
  *#*
@@ -592,15 +595,15 @@ NEXT_FLAG:
 #if defined(__AVR__) || defined(__I196__) /* 16bit pointers */
                                        ulong = (unsigned long)(unsigned short)va_arg(ap, char *);
 #else /* 32bit pointers */
-                               ulong = (unsigned long)va_arg(ap, char *);
+                                       ulong = (unsigned long)va_arg(ap, char *);
 #endif /* 32bit pointers */
-                               else if (sizeof(short) == sizeof(int))
-                                       ulong = flags.l_L_modifier ?
-                                               va_arg(ap, unsigned long) : (unsigned long)va_arg(ap, unsigned int);
+                               else if (flags.l_L_modifier)
+                                       ulong = va_arg(ap, unsigned long);
+                               else if (flags.h_modifier)
+                                       ulong = (unsigned long)(unsigned short)va_arg(ap, unsigned int);
                                else
-                                       ulong = flags.h_modifier ?
-                                               (unsigned long)(unsigned short) va_arg(ap, int)
-                                               : (unsigned long)va_arg(ap, int);
+                                       ulong = va_arg(ap, unsigned int);
+
                                flags.div_factor =
 #if CONFIG_PRINTF_OCTAL_FORMATTER
                                        (format_flag == 'o') ? DIV_OCT :
@@ -611,32 +614,18 @@ NEXT_FLAG:
 
                        case 'd':
                        case 'i':
-                               if (sizeof(short) == sizeof(long))
-                               {
-                                       if ( (long)(ulong = va_arg(ap, unsigned long)) < 0)
-                                       {
-                                               flags.plus_space_flag = PSF_MINUS;
-                                               ulong = (unsigned long)(-((signed long)ulong));
-                                       }
-                               }
-                               else if (sizeof(short) == sizeof(int))
-                               {
-                                       if ( (long)(ulong = flags.l_L_modifier ?
-                                                               va_arg(ap,unsigned long) : (unsigned long)va_arg(ap,int)) < 0)
-                                       {
-                                               flags.plus_space_flag = PSF_MINUS;
-                                               ulong = (unsigned long)(-((signed long)ulong));
-                                       }
-                               }
+                               if (flags.l_L_modifier)
+                                       ulong = (unsigned long)(long)va_arg(ap, long);
                                else
+                                       ulong = (unsigned long)(long)va_arg(ap, int);
+
+                               /* Extract sign */
+                               if ((signed long)ulong < 0)
                                {
-                                       if ( (signed long)(ulong = (unsigned long) (flags.h_modifier ?
-                                                                       (short) va_arg(ap, int) : va_arg(ap,int))) < 0)
-                                       {
-                                               flags.plus_space_flag = PSF_MINUS;
-                                               ulong = (unsigned long)(-((signed long)ulong));
-                                       }
+                                       flags.plus_space_flag = PSF_MINUS;
+                                       ulong = (unsigned long)(-((signed long)ulong));
                                }
+
                                flags.div_factor = DIV_DEC;
 
                                /* Now convert to digits */
index 0f4758f9bf593e844eb6dd0ccf9879da3cf90256..a0706525e89936cd1580da74eab41f7c923f7156 100755 (executable)
@@ -1,6 +1,29 @@
+/*!
+ * \file
+ * <!--
+ * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
+ * This file is part of DevLib - See README.devlib for information.
+ * -->
+ *
+ * \brief sprintf() implementation based on _formatted_write()
+ *
+ * \version $Id$
+ * \author Bernardo Innocenti <bernie@develer.com>
+ */
+
+/*#*
+ *#* $Log$
+ *#* Revision 1.4  2005/11/04 17:43:27  bernie
+ *#* Fix for LP64 architectures; Add some more tests.
+ *#*
+ *#*/
+
 #include "sprintf.c"
 #include "formatwr.c"
 #include "hex.c"
+#include <cfg/compiler.h>
+#include <mware/pgm.h>
+#include <stdio.h>
 
 #include <assert.h> /* assert() */
 #include <string.h> /* strcmp() */
@@ -18,14 +41,28 @@ int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv))
        snprintf(buf, sizeof buf, "%S", test_string_pgm);
        assert(strcmp(buf, test_string_pgm) == 0);
 
-       snprintf(buf, sizeof buf, "%d", 12345);
-       assert(strcmp(buf, "12345") == 0);
+#define TEST(FMT, VALUE, EXPECT) do { \
+               snprintf(buf, sizeof buf, FMT, VALUE); \
+               assert(strcmp(buf, EXPECT) == 0); \
+       } while (0)
+
+       TEST("%d",       12345,        "12345");
+       TEST("%ld",  123456789L,   "123456789");
+       TEST("%ld",  -12345678L,   "-12345678");
+       TEST("%lu", 4294967295UL, "4294967295");
+       TEST("%hd",     -12345,       "-12345");
+       TEST("%hu",      65535UL,      "65535");
 
-       snprintf(buf, sizeof buf, "%ld", 123456789L);
-       assert(strcmp(buf, "123456789") == 0);
+       TEST("%8d",      123,       "     123");
+       TEST("%8d",     -123,       "    -123");
+       TEST("%-8d",     -123,      "-123    ");
+       TEST("%08d",     -123,      "-0000123");
 
-       snprintf(buf, sizeof buf, "%lu", 4294967295UL);
-       assert(strcmp(buf, "4294967295") == 0);
+       TEST("%8.2f",  -123.456,    " -123.46");
+       TEST("%-8.2f", -123.456,    "-123.46 ");
+       TEST("%8.0f",  -123.456,    "    -123");
+
+#undef TEST
 
        /*
         * Stress tests.
@@ -38,3 +75,4 @@ int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv))
 
        return 0;
 }
+