Fix one warning.
[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.5  2005/11/04 17:47:26  bernie
17  *#* Fix one warning.
18  *#*
19  *#* Revision 1.4  2005/11/04 17:43:27  bernie
20  *#* Fix for LP64 architectures; Add some more tests.
21  *#*
22  *#*/
23
24 #include "sprintf.c"
25 #include "formatwr.c"
26 #include "hex.c"
27 #include <cfg/compiler.h>
28 #include <mware/pgm.h>
29 #include <stdio.h>
30
31 #include <assert.h> /* assert() */
32 #include <string.h> /* strcmp() */
33
34
35 int main(UNUSED_ARG(int, argc), UNUSED_ARG(char **,argv))
36 {
37         char buf[256];
38         static const char test_string[] = "Hello, world!\n";
39         static const pgm_char test_string_pgm[] = "Hello, world!\n";
40
41         snprintf(buf, sizeof buf, "%s", test_string);
42         assert(strcmp(buf, test_string) == 0);
43
44         snprintf(buf, sizeof buf, "%S", test_string_pgm);
45         assert(strcmp(buf, test_string_pgm) == 0);
46
47 #define TEST(FMT, VALUE, EXPECT) do { \
48                 snprintf(buf, sizeof buf, FMT, VALUE); \
49                 assert(strcmp(buf, EXPECT) == 0); \
50         } while (0)
51
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");
58
59         TEST("%8d",      123,       "     123");
60         TEST("%8d",     -123,       "    -123");
61         TEST("%-8d",     -123,      "-123    ");
62         TEST("%08d",     -123,      "-0000123");
63
64         TEST("%8.2f",  -123.456,    " -123.46");
65         TEST("%-8.2f", -123.456,    "-123.46 ");
66         TEST("%8.0f",  -123.456,    "    -123");
67
68 #undef TEST
69
70         /*
71          * Stress tests.
72          */
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 */
78
79         return 0;
80 }
81