4517214ffec9ee6a370ce1c476a05eb15116adf8
[bertos.git] / mware / sprintf_test.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief sprintf() implementation based on _formatted_write()
9  *
10  * \version $Id$
11  * \author Bernardo Innocenti <bernie@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.6  2006/07/19 12:56:28  bernie
17  *#* Convert to new Doxygen style.
18  *#*
19  *#* Revision 1.5  2005/11/04 17:47:26  bernie
20  *#* Fix one warning.
21  *#*
22  *#* Revision 1.4  2005/11/04 17:43:27  bernie
23  *#* Fix for LP64 architectures; Add some more tests.
24  *#*
25  *#*/
26
27 #include "sprintf.c"
28 #include "formatwr.c"
29 #include "hex.c"
30 #include <cfg/compiler.h>
31 #include <mware/pgm.h>
32 #include <stdio.h>
33
34 #include <assert.h> /* assert() */
35 #include <string.h> /* strcmp() */
36
37
38 int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv))
39 {
40         char buf[256];
41         static const char test_string[] = "Hello, world!\n";
42         static const pgm_char test_string_pgm[] = "Hello, world!\n";
43
44         snprintf(buf, sizeof buf, "%s", test_string);
45         assert(strcmp(buf, test_string) == 0);
46
47         snprintf(buf, sizeof buf, "%S", test_string_pgm);
48         assert(strcmp(buf, test_string_pgm) == 0);
49
50 #define TEST(FMT, VALUE, EXPECT) do { \
51                 snprintf(buf, sizeof buf, FMT, VALUE); \
52                 assert(strcmp(buf, EXPECT) == 0); \
53         } while (0)
54
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");
61
62         TEST("%8d",      123,       "     123");
63         TEST("%8d",     -123,       "    -123");
64         TEST("%-8d",     -123,      "-123    ");
65         TEST("%08d",     -123,      "-0000123");
66
67         TEST("%8.2f",  -123.456,    " -123.46");
68         TEST("%-8.2f", -123.456,    "-123.46 ");
69         TEST("%8.0f",  -123.456,    "    -123");
70
71 #undef TEST
72
73         /*
74          * Stress tests.
75          */
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 */
81
82         return 0;
83 }
84