Removed 'This file is part of DevLib ...'
[bertos.git] / mware / sprintf_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief sprintf() implementation based on _formatted_write()
34  *
35  * \version $Id$
36  * \author Bernardo Innocenti <bernie@develer.com>
37  */
38
39 /*#*
40  *#* $Log$
41  *#* Revision 1.6  2006/07/19 12:56:28  bernie
42  *#* Convert to new Doxygen style.
43  *#*
44  *#* Revision 1.5  2005/11/04 17:47:26  bernie
45  *#* Fix one warning.
46  *#*
47  *#* Revision 1.4  2005/11/04 17:43:27  bernie
48  *#* Fix for LP64 architectures; Add some more tests.
49  *#*
50  *#*/
51
52 #include "sprintf.c"
53 #include "formatwr.c"
54 #include "hex.c"
55 #include <cfg/compiler.h>
56 #include <mware/pgm.h>
57 #include <stdio.h>
58
59 #include <assert.h> /* assert() */
60 #include <string.h> /* strcmp() */
61
62
63 int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv))
64 {
65         char buf[256];
66         static const char test_string[] = "Hello, world!\n";
67         static const pgm_char test_string_pgm[] = "Hello, world!\n";
68
69         snprintf(buf, sizeof buf, "%s", test_string);
70         assert(strcmp(buf, test_string) == 0);
71
72         snprintf(buf, sizeof buf, "%S", test_string_pgm);
73         assert(strcmp(buf, test_string_pgm) == 0);
74
75 #define TEST(FMT, VALUE, EXPECT) do { \
76                 snprintf(buf, sizeof buf, FMT, VALUE); \
77                 assert(strcmp(buf, EXPECT) == 0); \
78         } while (0)
79
80         TEST("%d",       12345,        "12345");
81         TEST("%ld",  123456789L,   "123456789");
82         TEST("%ld",  -12345678L,   "-12345678");
83         TEST("%lu", 4294967295UL, "4294967295");
84         TEST("%hd",     -12345,       "-12345");
85         TEST("%hu",      65535U,       "65535");
86
87         TEST("%8d",      123,       "     123");
88         TEST("%8d",     -123,       "    -123");
89         TEST("%-8d",     -123,      "-123    ");
90         TEST("%08d",     -123,      "-0000123");
91
92         TEST("%8.2f",  -123.456,    " -123.46");
93         TEST("%-8.2f", -123.456,    "-123.46 ");
94         TEST("%8.0f",  -123.456,    "    -123");
95
96 #undef TEST
97
98         /*
99          * Stress tests.
100          */
101         snprintf(buf, sizeof buf, "%s", NULL);
102         assert(strcmp(buf, "<NULL>") == 0);
103         snprintf(buf, sizeof buf, "%k");
104         assert(strcmp(buf, "???") == 0);
105         sprintf(NULL, test_string); /* must not crash */
106
107         return 0;
108 }
109