aboutsummaryrefslogtreecommitdiff
path: root/tests/libtest/lib557.c
blob: 8e49dbff3a1a3d57b2d786c7565925fe264cb052 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*****************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * $Id$
 */

/*
 * The purpose of this test is to minimally exercise libcurl's internal
 * curl_m*printf formatting capabilities and handling of some data types.
 */

#include "test.h"

int curl_msprintf(char *buffer, const char *format, ...);


#if (CURL_SIZEOF_CURL_OFF_T > 4)
#  if (CURL_SIZEOF_LONG > 4)
#    define MPRNT_SUFFIX_CURL_OFF_T  L
#  else
#    define MPRNT_SUFFIX_CURL_OFF_T  LL
#  endif
#else
#  if (CURL_SIZEOF_LONG > 2)
#    define MPRNT_SUFFIX_CURL_OFF_T  L
#  else
#    define MPRNT_SUFFIX_CURL_OFF_T  LL
#  endif
#endif

#ifdef CURL_ISOCPP
#  define MPRNT_OFF_T_C_HELPER2(Val,Suffix) Val ## Suffix
#else
#  define MPRNT_OFF_T_C_HELPER2(Val,Suffix) Val/**/Suffix
#endif
#define MPRNT_OFF_T_C_HELPER1(Val,Suffix) MPRNT_OFF_T_C_HELPER2(Val,Suffix)
#define MPRNT_OFF_T_C(Val)  MPRNT_OFF_T_C_HELPER1(Val,MPRNT_SUFFIX_CURL_OFF_T)


#define BUFSZ    256
#define NUM_ULONG_TESTS  4
#define NUM_SLONG_TESTS  7
#define NUM_COFFT_TESTS  7


struct unslong_st {
  unsigned long num;    /* unsigned long   */
  const char *expected; /* expected string */
  char result[BUFSZ];   /* result string   */
};


struct siglong_st {
  long num;             /* signed long     */
  const char *expected; /* expected string */
  char result[BUFSZ];   /* result string   */
};


struct curloff_st {
  curl_off_t num;       /* curl_off_t      */
  const char *expected; /* expected string */
  char result[BUFSZ];   /* result string   */
};


static struct unslong_st ul_test[NUM_ULONG_TESTS];
static struct siglong_st sl_test[NUM_SLONG_TESTS];
static struct curloff_st co_test[NUM_COFFT_TESTS];


static int test_unsigned_long_formatting(void)
{
  int i, j;
  int failed = 0;

  ul_test[0].num = 0x0L;
  ul_test[0].expected = "0";
  ul_test[1].num = 0x1L;
  ul_test[1].expected = "1";
#if (CURL_SIZEOF_LONG == 2)
  ul_test[2].num = 0xFFL;
  ul_test[2].expected = "255";
  ul_test[3].num = 0xFFFFL;
  ul_test[3].expected = "65535";
#elif (CURL_SIZEOF_LONG == 4)
  ul_test[2].num = 0xFFFFL;
  ul_test[2].expected = "65535";
  ul_test[3].num = 0xFFFFFFFFL;
  ul_test[3].expected = "4294967295";
#elif (CURL_SIZEOF_LONG == 8)
  ul_test[2].num = 0xFFFFFFFFL;
  ul_test[2].expected = "4294967295";
  ul_test[3].num = 0xFFFFFFFFFFFFFFFFL;
  ul_test[3].expected = "18446744073709551615";
#endif

  for(i=0; i<NUM_ULONG_TESTS; i++) {

    for(j=0; j<BUFSZ; j++)
      ul_test[i].result[j] = 'X';
    ul_test[i].result[BUFSZ-1] = '\0';

    (void)curl_msprintf(ul_test[i].result, "%lu", ul_test[i].num);

    if(!memcmp(ul_test[i].result,
               ul_test[i].expected,
               strlen(ul_test[i].expected)))
      printf("unsigned long test #%d: OK\n", i+1);
    else {
      printf("unsigned long test #%d: Failed (Expected: %s Got: %s)\n",
             i+1, ul_test[i].expected, ul_test[i].result);
      failed++;
    }

  }

  return failed;
}


static int test_signed_long_formatting(void)
{
  int i, j;
  int failed = 0;

  sl_test[0].num = 0x0L;
  sl_test[0].expected = "0";
  sl_test[1].num = 0x1L;
  sl_test[1].expected = "1";
  sl_test[2].num = -0x1L;
  sl_test[2].expected = "-1";
#if (CURL_SIZEOF_LONG == 2)
  sl_test[3].num = 0x7FL;
  sl_test[3].expected = "127";
  sl_test[4].num = 0x7FFFL;
  sl_test[4].expected = "32767";
  sl_test[5].num = -0x7FL -1L;
  sl_test[5].expected = "-128";
  sl_test[6].num = -0x7FFFL -1L;
  sl_test[6].expected = "-32768";
#elif (CURL_SIZEOF_LONG == 4)
  sl_test[3].num = 0x7FFFL;
  sl_test[3].expected = "32767";
  sl_test[4].num = 0x7FFFFFFFL;
  sl_test[4].expected = "2147483647";
  sl_test[5].num = -0x7FFFL -1L;
  sl_test[5].expected = "-32768";
  sl_test[6].num = -0x7FFFFFFFL -1L;
  sl_test[6].expected = "-2147483648";
#elif (CURL_SIZEOF_LONG == 8)
  sl_test[3].num = 0x7FFFFFFFL;
  sl_test[3].expected = "2147483647";
  sl_test[4].num = 0x7FFFFFFFFFFFFFFFL;
  sl_test[4].expected = "9223372036854775807";
  sl_test[5].num = -0x7FFFFFFFL -1L;
  sl_test[5].expected = "-2147483648";
  sl_test[6].num = -0x7FFFFFFFFFFFFFFFL -1L;
  sl_test[6].expected = "-9223372036854775808";
#endif

  for(i=0; i<NUM_SLONG_TESTS; i++) {

    for(j=0; j<BUFSZ; j++)
      sl_test[i].result[j] = 'X';
    sl_test[i].result[BUFSZ-1] = '\0';

    (void)curl_msprintf(sl_test[i].result, "%ld", sl_test[i].num);

    if(!memcmp(sl_test[i].result,
               sl_test[i].expected,
               strlen(sl_test[i].expected)))
      printf("signed long test #%d: OK\n", i+1);
    else {
      printf("signed long test #%d: Failed (Expected: %s Got: %s)\n",
             i+1, sl_test[i].expected, sl_test[i].result);
      failed++;
    }

  }

  return failed;
}


static int test_curl_off_t_formatting(void)
{
  int i, j;
  int failed = 0;

  co_test[0].num = MPRNT_OFF_T_C(0x0);
  co_test[0].expected = "0";
  co_test[1].num = MPRNT_OFF_T_C(0x1);
  co_test[1].expected = "1";
  co_test[2].num = -MPRNT_OFF_T_C(0x1);
  co_test[2].expected = "-1";
#if (CURL_SIZEOF_CURL_OFF_T == 2)
  co_test[3].num = MPRNT_OFF_T_C(0x7F);
  co_test[3].expected = "127";
  co_test[4].num = MPRNT_OFF_T_C(0x7FFF);
  co_test[4].expected = "32767";
  co_test[5].num = -MPRNT_OFF_T_C(0x7F) -MPRNT_OFF_T_C(1);
  co_test[5].expected = "-128";
  co_test[6].num = -MPRNT_OFF_T_C(0x7FFF) -MPRNT_OFF_T_C(1);
  co_test[6].expected = "-32768";
#elif (CURL_SIZEOF_CURL_OFF_T == 4)
  co_test[3].num = MPRNT_OFF_T_C(0x7FFF);
  co_test[3].expected = "32767";
  co_test[4].num = MPRNT_OFF_T_C(0x7FFFFFFF);
  co_test[4].expected = "2147483647";
  co_test[5].num = -MPRNT_OFF_T_C(0x7FFF) -MPRNT_OFF_T_C(1);
  co_test[5].expected = "-32768";
  co_test[6].num = -MPRNT_OFF_T_C(0x7FFFFFFF) -MPRNT_OFF_T_C(1);
  co_test[6].expected = "-2147483648";
#elif (CURL_SIZEOF_CURL_OFF_T == 8)
  co_test[3].num = MPRNT_OFF_T_C(0x7FFFFFFF);
  co_test[3].expected = "2147483647";
  co_test[4].num = MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFF);
  co_test[4].expected = "9223372036854775807";
  co_test[5].num = -MPRNT_OFF_T_C(0x7FFFFFFF) -MPRNT_OFF_T_C(1);
  co_test[5].expected = "-2147483648";
  co_test[6].num = -MPRNT_OFF_T_C(0x7FFFFFFFFFFFFFFF) -MPRNT_OFF_T_C(1);
  co_test[6].expected = "-9223372036854775808";
#endif

  for(i=0; i<NUM_COFFT_TESTS; i++) {

    for(j=0; j<BUFSZ; j++)
      co_test[i].result[j] = 'X';
    co_test[i].result[BUFSZ-1] = '\0';

    (void)curl_msprintf(co_test[i].result, "%" FORMAT_OFF_T, co_test[i].num);

    if(!memcmp(co_test[i].result,
               co_test[i].expected,
               strlen(co_test[i].expected)))
      printf("curl_off_t test #%d: OK\n", i+1);
    else {
      printf("curl_off_t test #%d: Failed (Expected: %s Got: %s)\n",
             i+1, co_test[i].expected, co_test[i].result);
      failed++;
    }

  }

  return failed;
}


int test(char *URL)
{
  int errors = 0;
  (void)URL; /* not used */

  errors += test_unsigned_long_formatting();

  errors += test_signed_long_formatting();

  errors += test_curl_off_t_formatting();

  if(errors)
    return TEST_ERR_MAJOR_BAD;
  else
    return 0;
}