summaryrefslogtreecommitdiff
path: root/support/regression/tests/snprintf.c.in
blob: 70dde1f04d4e81526f324b47897cee507622eb97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/** tests for snprintf
  type: INT, LONG, STRING, FLOAT, PTR
*/
#include <testfwk.h>
#include <string.h>
#include <stdio.h>

#define {type} 1

#if 0
# define DEBUG(x) x
#else
# define DEBUG(x)
#endif

#define CHECK_B 0

#if defined(INT)

struct
{
  int arg;
  char *fmt;
  char *result;
} static const cases[] = {
  // arg, fmt,    result
  {0xab, "%04x", "00ab"},
  {0xffff, "0x%02X", "0xFFFF"},
  {0xffffu, "%u", "65535"},
  {1234, "%+6d", " +1234"},
  {12345, "% d", " 12345"},
  {-1234, "%d", "-1234"},
  {32767, "%8d", "   32767"},
  {1, "%%%d", "%1"},
  {1001, "%08i", "00001001"},
  {101, "%-+8d", "+101    "},
  {01234, "%o(oct)", "1234(oct)"},

  // do we want to check these:
#if defined(__SDCC) && !defined(__SDCC_z80) && CHECK_B
  {0x4142, "%bc %bx", "\x41 42"},       /* non-std: print as bytes! */
  {0xfffe, "0x%02bX%02bx", "0xFFfe"},   /* non-std: print as bytes! */
#elif defined(__SDCC) && defined(__SDCC_z80) && CHECK_B
  {0x4142, "%bc %bx", "\x42 41"},       /* non-std: print as bytes! */
  {0xfffe, "0x%02bX%02bx", "0xFEff"},   /* non-std: print as bytes! */
#endif
};

#elif defined(LONG)

struct
{
  long arg;
  char *fmt;
  char *result;
} static const cases[] = {
  // arg, fmt,    result
  {0x12345678, "0x%lx", "0x12345678"},
  {0x7fffFFFF, "%10lx", "  7fffffff"},
  {0x789abcde, "0x%-10lX", "0x789ABCDE  "},
  {0x1000a, "0x%02lX", "0x1000A"},
  {0x80000000, "%lu", "2147483648"},
  {-2147483648, "%li", "-2147483648"},
  {-1234, "%+6ld", " -1234"},
  {012345670123, "%lo(oct)", "12345670123(oct)"},
  {0xffffFFFF, "%lo(oct)", "37777777777(oct)"},

  // do we want to check these:
#if defined(__SDCC) && !defined(__SDCC_z80) && CHECK_B
  {0xfedcba98, "0x%bX%bX%bx%bx", "0xFEDCba98"}, /* non-std: print as bytes! */
#elif defined(__SDCC) && defined(__SDCC_z80) && CHECK_B
  {0xfedcba98, "0x%bX%bX%bx%bx", "0x98BAdcfe"}, /* non-std: print as bytes! */
#endif
};

#elif defined(STRING)

struct
{
  char *arg;
  char *fmt;
  char *result;
} static const cases[] = {
  // arg, fmt,    result
  {"abcd", "%s", "abcd"},
  {"abcd", "%3s", "abcd"},
  {"abcd", "%5s", " abcd"},
  {"abcd", "%-5s", "abcd "},
  {"abcd", "%.2s", "ab"},
  {"XYZ\\", "%s", "XYZ\x5c"},
  {"ab\x1b\x7f", "%s", "ab\x1b\x7f"},
  {"ab\tcd\n", "%s", "ab\tcd\n"},
};

#elif defined(FLOAT)

struct
{
  float arg;
  char *fmt;
  char *result;
} static const cases[] = {
  // arg, fmt,    result
  // ... there should be more ...
#if defined(__SDCC) && !defined(__SDCC_ds390) && !(defined(__SDCC_mcs51) && (defined(__SDCC_USE_XSTACK) || defined(__SDCC_MODEL_HUGE)))
  {1.0, "%f", "<NO FLOAT>"},
#else
  {1.0, "%f", "1.000000"},
  {1.96, "%3.1f", "2.0"},
#endif
};

#elif defined(PTR)

#ifndef PORT_HOST
#define PTR_NON_STD
#endif

#if defined(__SDCC) && (defined(__SDCC_mcs51) || defined(__SDCC_ds390))
__idata unsigned char __at(0xAA) var_idata;
__pdata unsigned char __at(0xAA) var_pdata;
__xdata unsigned char __at(0xAABB) var_xdata;
__code unsigned char __at(0xAABB) var_code;
#endif

struct
{
  void *arg;
  char *fmt;
  char *result;
} static const cases[] = {
  // arg, fmt,    result
#if defined(__SDCC) && defined(__SDCC_ds390)
  // Special cases for ds390 covering different address spaces.
  {var_idata, "%p", "I:0x0000aa"},
  {var_pdata, "%p", "P:0x0000aa"},
  {var_xdata, "%p", "X:0x00aabb"},
  {var_code, "%p", "C:0x00aabb"},
#ifdef PTR_NON_STD
  {var_idata, "%P", "I:0x0000AA"},
  {var_pdata, "%P", "P:0x0000AA"},
  {var_xdata, "%P", "X:0x00AABB"},
  {var_code, "%P", "C:0x00AABB"},
#endif
#elif defined(__SDCC) && defined(__SDCC_mcs51)
  // Special cases for mcs51 covering different address spaces.
  {var_idata, "%p", "I:0xaa"},
  {var_pdata, "%p", "P:0xaa"},
  {var_xdata, "%p", "X:0xaabb"},
  {var_code, "%p", "C:0xaabb"},
#ifdef PTR_NON_STD
  {var_idata, "%P", "I:0xAA"},
  {var_pdata, "%P", "P:0xAA"},
  {var_xdata, "%P", "X:0xAABB"},
  {var_code, "%P", "C:0xAABB"},
#endif
#else
  {(void *)0xAABB, "%p", "0xaabb"},
#ifdef PTR_NON_STD
  {(void *)0xAABB, "%P", "0xAABB"},
#endif
#endif
};

#endif

void
test_snprintf (void)
{
#if !defined(__SDCC_pdk14) && !defined(__SDCC_pdk15) // Lack of memory
#ifndef __SDCC_pic16
  unsigned char buf[32];
  unsigned char i;

  memset (buf, 0xfe, sizeof buf);       /* cookies all over */

  for (i = 0; i < sizeof cases / sizeof cases[0]; i++)
    {
      sprintf (buf, cases[i].fmt, cases[i].arg);
      DEBUG (__prints ("Test"));
      DEBUG (__printd (i));
      DEBUG (__prints (" should be: '"));
      DEBUG (__prints (cases[i].result));
      DEBUG (__prints ("' is: '"));
      DEBUG (__prints (buf));
      DEBUG (__prints ("'\n"));
      ASSERT (!strcmp (buf, cases[i].result));
    }

  ASSERT (buf[sizeof buf - 10] == 0xfe);        /* check for cookie */
#endif
#endif
}