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.5 2005/11/04 17:47:26 bernie
19 *#* Revision 1.4 2005/11/04 17:43:27 bernie
20 *#* Fix for LP64 architectures; Add some more tests.
27 #include <cfg/compiler.h>
28 #include <mware/pgm.h>
31 #include <assert.h> /* assert() */
32 #include <string.h> /* strcmp() */
35 int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv))
38 static const char test_string[] = "Hello, world!\n";
39 static const pgm_char test_string_pgm[] = "Hello, world!\n";
41 snprintf(buf, sizeof buf, "%s", test_string);
42 assert(strcmp(buf, test_string) == 0);
44 snprintf(buf, sizeof buf, "%S", test_string_pgm);
45 assert(strcmp(buf, test_string_pgm) == 0);
47 #define TEST(FMT, VALUE, EXPECT) do { \
48 snprintf(buf, sizeof buf, FMT, VALUE); \
49 assert(strcmp(buf, EXPECT) == 0); \
52 TEST("%d", 12345, "12345");
53 TEST("%ld", 123456789L, "123456789");
54 TEST("%ld", -12345678L, "-12345678");
55 TEST("%lu", 4294967295UL, "4294967295");
56 TEST("%hd", -12345, "-12345");
57 TEST("%hu", 65535U, "65535");
59 TEST("%8d", 123, " 123");
60 TEST("%8d", -123, " -123");
61 TEST("%-8d", -123, "-123 ");
62 TEST("%08d", -123, "-0000123");
64 TEST("%8.2f", -123.456, " -123.46");
65 TEST("%-8.2f", -123.456, "-123.46 ");
66 TEST("%8.0f", -123.456, " -123");
73 snprintf(buf, sizeof buf, "%s", NULL);
74 assert(strcmp(buf, "<NULL>") == 0);
75 snprintf(buf, sizeof buf, "%k");
76 assert(strcmp(buf, "???") == 0);
77 sprintf(NULL, test_string); /* must not crash */