4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
33 * \brief sprintf() implementation based on _formatted_write()
36 * \author Bernie Innocenti <bernie@codewiz.org>
40 #include <cfg/compiler.h>
42 #include <cfg/debug.h>
48 #include <string.h> /* strcmp() */
50 #warning FIXME:Review and refactor this test..
60 static const char test_string[] = "Hello, world!\n";
61 static const pgm_char test_string_pgm[] = "Hello, world!\n";
63 snprintf(buf, sizeof buf, "%s", test_string);
64 assert(strcmp(buf, test_string) == 0);
66 snprintf(buf, sizeof buf, "%S", test_string_pgm);
67 assert(strcmp(buf, test_string_pgm) == 0);
69 #define TEST(FMT, VALUE, EXPECT) do { \
70 snprintf(buf, sizeof buf, FMT, VALUE); \
71 assert(strcmp(buf, EXPECT) == 0); \
74 TEST("%d", 12345, "12345");
75 TEST("%ld", 123456789L, "123456789");
76 TEST("%ld", -12345678L, "-12345678");
77 TEST("%lu", 4294967295UL, "4294967295");
78 TEST("%hd", -12345, "-12345");
79 TEST("%hu", 65535U, "65535");
81 TEST("%8d", 123, " 123");
82 TEST("%8d", -123, " -123");
83 TEST("%-8d", -123, "-123 ");
84 TEST("%08d", -123, "-0000123");
86 TEST("%8.2f", -123.456, " -123.46");
87 TEST("%-8.2f", -123.456, "-123.46 ");
88 TEST("%8.0f", -123.456, " -123");
94 snprintf(buf, sizeof buf, "%s", NULL);
95 assert(strcmp(buf, "<NULL>") == 0);
96 snprintf(buf, sizeof buf, "%k");
97 assert(strcmp(buf, "???") == 0);
98 sprintf(NULL, test_string); /* must not crash */