From 646a88d7412eec847c282b9b2050b3a37a9af7a5 Mon Sep 17 00:00:00 2001 From: bernie Date: Fri, 4 Nov 2005 17:43:27 +0000 Subject: [PATCH] Fix for LP64 architectures; Add some more tests. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@435 38d2e660-2303-0410-9eaa-f027e97ec537 --- mware/formatwr.c | 49 +++++++++++++++++-------------------------- mware/sprintf_test.c | 50 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 36 deletions(-) diff --git a/mware/formatwr.c b/mware/formatwr.c index 04a50dc8..8a2e57e1 100755 --- a/mware/formatwr.c +++ b/mware/formatwr.c @@ -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 */ diff --git a/mware/sprintf_test.c b/mware/sprintf_test.c index 0f4758f9..a0706525 100755 --- a/mware/sprintf_test.c +++ b/mware/sprintf_test.c @@ -1,6 +1,29 @@ +/*! + * \file + * + * + * \brief sprintf() implementation based on _formatted_write() + * + * \version $Id$ + * \author Bernardo Innocenti + */ + +/*#* + *#* $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 +#include +#include #include /* assert() */ #include /* 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; } + -- 2.25.1