/*#*
*#* $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.
*#*
#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 :
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 */
+/*!
+ * \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() */
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.
return 0;
}
+