4 * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief sprintf() implementation based on _formatted_write()
11 * \author Bernardo Innocenti <bernie@develer.com>
16 *#* Revision 1.6 2006/07/19 12:56:28 bernie
17 *#* Convert to new Doxygen style.
19 *#* Revision 1.5 2005/11/04 17:47:26 bernie
22 *#* Revision 1.4 2005/11/04 17:43:27 bernie
23 *#* Fix for LP64 architectures; Add some more tests.
30 #include <cfg/compiler.h>
31 #include <mware/pgm.h>
34 #include <assert.h> /* assert() */
35 #include <string.h> /* strcmp() */
38 int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv))
41 static const char test_string[] = "Hello, world!\n";
42 static const pgm_char test_string_pgm[] = "Hello, world!\n";
44 snprintf(buf, sizeof buf, "%s", test_string);
45 assert(strcmp(buf, test_string) == 0);
47 snprintf(buf, sizeof buf, "%S", test_string_pgm);
48 assert(strcmp(buf, test_string_pgm) == 0);
50 #define TEST(FMT, VALUE, EXPECT) do { \
51 snprintf(buf, sizeof buf, FMT, VALUE); \
52 assert(strcmp(buf, EXPECT) == 0); \
55 TEST("%d", 12345, "12345");
56 TEST("%ld", 123456789L, "123456789");
57 TEST("%ld", -12345678L, "-12345678");
58 TEST("%lu", 4294967295UL, "4294967295");
59 TEST("%hd", -12345, "-12345");
60 TEST("%hu", 65535U, "65535");
62 TEST("%8d", 123, " 123");
63 TEST("%8d", -123, " -123");
64 TEST("%-8d", -123, "-123 ");
65 TEST("%08d", -123, "-0000123");
67 TEST("%8.2f", -123.456, " -123.46");
68 TEST("%-8.2f", -123.456, "-123.46 ");
69 TEST("%8.0f", -123.456, " -123");
76 snprintf(buf, sizeof buf, "%s", NULL);
77 assert(strcmp(buf, "<NULL>") == 0);
78 snprintf(buf, sizeof buf, "%k");
79 assert(strcmp(buf, "???") == 0);
80 sprintf(NULL, test_string); /* must not crash */