* device/lib/Makefile.in, device/lib/gbz80/Makefile.in,
[fw/sdcc] / support / regression / tests / snprintf.c
1 /** tests for snprintf
2   type: INT, LONG, STRING, FLOAT
3 */
4 #include <testfwk.h>
5 #include <string.h>
6 #include <stdio.h>
7
8 #define {type} 1
9
10 #if 0
11 # define DEBUG(x) x
12 #else
13 # define DEBUG(x)
14 #endif
15
16 #define CHECK_B 0
17
18 #if defined(INT)
19
20 struct
21 {
22   int arg;
23   char *fmt;
24   char *result;
25 } static const cases[] = {
26   // arg, fmt,    result
27   {0xab, "%04x", "00ab"},
28   {0xffff, "0x%02X", "0xFFFF"},
29   {0xffffu, "%u", "65535"},
30   {1234, "%+6d", " +1234"},
31   {12345, "% d", " 12345"},
32   {-1234, "%d", "-1234"},
33   {32767, "%8d", "   32767"},
34   {1, "%%%d", "%1"},
35   {1001, "%08i", "00001001"},
36   {101, "%-+8d", "+101    "},
37   {01234, "%o(oct)", "1234(oct)"},
38
39   // do we want to check these:
40 #if defined(SDCC) && !defined(SDCC_z80) && CHECK_B
41   {0x4142, "%bc %bx", "\x41 42"},       /* non-std: print as bytes! */
42   {0xfffe, "0x%02bX%02bx", "0xFFfe"},   /* non-std: print as bytes! */
43 #elif defined(SDCC) && defined(SDCC_z80) && CHECK_B
44   {0x4142, "%bc %bx", "\x42 41"},       /* non-std: print as bytes! */
45   {0xfffe, "0x%02bX%02bx", "0xFEff"},   /* non-std: print as bytes! */
46 #endif
47 };
48
49 #elif defined(LONG)
50
51 struct
52 {
53   long arg;
54   char *fmt;
55   char *result;
56 } static const cases[] = {
57   // arg, fmt,    result
58   {0x12345678, "0x%lx", "0x12345678"},
59   {0x7fffFFFF, "%10lx", "  7fffffff"},
60   {0x789abcde, "0x%-10lX", "0x789ABCDE  "},
61   {0x1000a, "0x%02lX", "0x1000A"},
62   {0x80000000, "%lu", "2147483648"},
63   {-2147483648, "%li", "-2147483648"},
64   {-1234, "%+6ld", " -1234"},
65   {012345670123, "%lo(oct)", "12345670123(oct)"},
66   {0xffffFFFF, "%lo(oct)", "37777777777(oct)"},
67
68   // do we want to check these:
69 #if defined(SDCC) && !defined(SDCC_z80) && CHECK_B
70   {0xfedcba98, "0x%bX%bX%bx%bx", "0xFEDCba98"}, /* non-std: print as bytes! */
71 #elif defined(SDCC) && defined(SDCC_z80) && CHECK_B
72   {0xfedcba98, "0x%bX%bX%bx%bx", "0x98BAdcfe"}, /* non-std: print as bytes! */
73 #endif
74 };
75
76 #elif defined(STRING)
77
78 struct
79 {
80   char *arg;
81   char *fmt;
82   char *result;
83 } static const cases[] = {
84   // arg, fmt,    result
85   {"abcd", "%s", "abcd"},
86   {"abcd", "%3s", "abcd"},
87   {"abcd", "%5s", " abcd"},
88   {"abcd", "%-5s", "abcd "},
89   {"abcd", "%.2s", "ab"},
90   {"XYZ\\", "%s", "XYZ\x5c"},
91   {"ab\x1b\x7f", "%s", "ab\x1b\x7f"},
92   {"ab\tcd\n", "%s", "ab\tcd\n"},
93 };
94
95 #elif defined(FLOAT)
96
97 struct
98 {
99   float arg;
100   char *fmt;
101   char *result;
102 } static const cases[] = {
103   // arg, fmt,    result
104   // ... there should be more ...
105 #if defined(SDCC) && !defined(SDCC_ds390) && !(defined(SDCC_mcs51) && defined(SDCC_USE_XSTACK))
106   {1.0, "%f", "<NO FLOAT>"},
107 #else
108   {1.0, "%f", "1.000000"},
109   {1.96, "%3.1f", "2.0"},
110 #endif
111 };
112
113 #endif
114
115 void
116 test_snprintf (void)
117 {
118   unsigned char buf[32];
119   unsigned char i;
120
121   memset (buf, 0xfe, sizeof buf);       /* cookies all over */
122
123   for (i = 0; i < sizeof cases / sizeof cases[0]; i++)
124     {
125       sprintf (buf, cases[i].fmt, cases[i].arg);
126       DEBUG (printf ("Test%d should be: \"%s\" is: \"%s\"\n", i, cases[i].result, buf));
127       ASSERT (!strcmp (buf, cases[i].result));
128     }
129
130   ASSERT (buf[sizeof buf - 10] == 0xfe);        /* check for cookie */
131 }