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