1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================================
6**
7** Source: vfprintf.h
8**
9** Purpose: Contains common testing functions for vfprintf
10**
11**
12**==========================================================================*/
13
14#ifndef __vfprintf_H__
15#define __vfprintf_H__
16
17int DoVfprintf(FILE *fp, const char *format, ...)
18{
19 int retVal;
20 va_list arglist;
21
22 va_start(arglist, format);
23 retVal = vfprintf(fp, format, arglist);
24 va_end(arglist);
25
26 return (retVal);
27}
28
29void DoStrTest(const char *formatstr, char* param, const char *checkstr)
30{
31 FILE *fp;
32 char buf[256] = { 0 };
33
34 if ((fp = fopen("testfile.txt", "w+")) == NULL )
35 {
36 Fail("ERROR: fopen failed to create testfile\n");
37 }
38 if ((DoVfprintf(fp, formatstr, param)) < 0)
39 {
40 Fail("ERROR: vfprintf failed\n");
41 }
42 if ((fseek(fp, 0, SEEK_SET)) != 0)
43 {
44 Fail("ERROR: fseek failed\n");
45 }
46 if ((fgets(buf, 100, fp)) == NULL)
47 {
48 Fail("ERROR: fgets failed\n");
49 }
50
51 if (memcmp(buf, checkstr, strlen(checkstr) + 1) != 0)
52 {
53 Fail("ERROR: failed to insert string \"%s\" into \"%s\"\n"
54 "Expected \"%s\" got \"%s\".\n",
55 param, formatstr, checkstr, buf);
56 }
57 fclose(fp);
58}
59
60void DoWStrTest(const char *formatstr, WCHAR* param, const char *checkstr)
61{
62 FILE *fp;
63 char buf[256] = { 0 };
64
65 if ((fp = fopen("testfile.txt", "w+")) == NULL )
66 {
67 Fail("ERROR: fopen failed to create testfile\n");
68 }
69 if ((DoVfprintf(fp, formatstr, param)) < 0)
70 {
71 Fail("ERROR: vfprintf failed\n");
72 }
73 if ((fseek(fp, 0, SEEK_SET)) != 0)
74 {
75 Fail("ERROR: fseek failed\n");
76 }
77 if ((fgets(buf, 100, fp)) == NULL)
78 {
79 Fail("ERROR: fgets failed\n");
80 }
81
82 if (memcmp(buf, checkstr, strlen(checkstr) + 1) != 0)
83 {
84 Fail("ERROR: failed to insert wide string \"%S\" into \"%s\"\n"
85 "Expected \"%s\" got \"%s\".\n",
86 param, formatstr, checkstr, buf);
87 }
88 fclose(fp);
89}
90
91
92void DoPointerTest(const char *formatstr, void* param, char* paramstr,
93 const char *checkstr1)
94{
95 FILE *fp;
96 char buf[256] = { 0 };
97
98 if ((fp = fopen("testfile.txt", "w+")) == NULL )
99 {
100 Fail("ERROR: fopen failed to create testfile\n");
101 }
102
103 if ((DoVfprintf(fp, formatstr, param)) < 0)
104 {
105 Fail("ERROR: vfprintf failed\n");
106 }
107
108 if ((fseek(fp, 0, SEEK_SET)) != 0)
109 {
110 Fail("ERROR: fseek failed\n");
111 }
112
113 if ((fgets(buf, 100, fp)) == NULL)
114 {
115 Fail("ERROR: fgets failed\n");
116 }
117
118 if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0)
119 {
120 Fail("ERROR: failed to insert %s into \"%s\"\n"
121 "Expected \"%s\" got \"%s\".\n",
122 paramstr, formatstr, checkstr1, buf);
123 }
124
125 if ((fclose( fp )) != 0)
126 {
127 Fail("ERROR: fclose failed to close \"testfile.txt\"\n");
128 }
129}
130
131
132
133void DoCountTest(const char *formatstr, int param, const char *checkstr)
134{
135 FILE *fp;
136 char buf[512] = { 0 };
137 int n = -1;
138
139 if ((fp = fopen("testfile.txt", "w+")) == NULL )
140 {
141 Fail("ERROR: fopen failed to create testfile\n");
142 }
143
144 if ((DoVfprintf(fp, formatstr, &n)) < 0)
145 {
146 Fail("ERROR: vfprintf failed\n");
147 }
148
149 if ((fseek(fp, 0, SEEK_SET)) != 0)
150 {
151 Fail("ERROR: fseek failed\n");
152 }
153
154 if ((fgets(buf, sizeof(buf), fp)) == NULL)
155 {
156 Fail("ERROR: fgets failed\n");
157 }
158
159 if (n != param)
160 {
161 Fail("ERROR: Expected count parameter to resolve to %d, got %X\n",
162 param, n);
163 }
164
165 if (memcmp(buf, checkstr, strlen(checkstr) + 1) != 0)
166 {
167 Fail("ERROR: Expected \"%s\" got \"%s\".\n", checkstr, buf);
168 }
169
170 if ((fclose( fp )) != 0)
171 {
172 Fail("ERROR: fclose failed to close \"testfile.txt\"\n");
173 }
174}
175
176void DoShortCountTest(const char *formatstr, int param, const char *checkstr)
177{
178 FILE *fp;
179 char buf[512] = { 0 };
180 short int n = -1;
181
182 if ((fp = fopen("testfile.txt", "w+")) == NULL )
183 {
184 Fail("ERROR: fopen failed to create testfile\n");
185 }
186
187 if ((DoVfprintf(fp, formatstr, &n)) < 0)
188 {
189 Fail("ERROR: vfprintf failed\n");
190 }
191
192 if ((fseek(fp, 0, SEEK_SET)) != 0)
193 {
194 Fail("ERROR: fseek failed\n");
195 }
196
197 if ((fgets(buf, 100, fp)) == NULL)
198 {
199 Fail("ERROR: fgets failed\n");
200 }
201
202 if (n != param)
203 {
204 Fail("ERROR: Expected count parameter to resolve to %d, got %X\n",
205 param, n);
206 }
207
208 if (memcmp(buf, checkstr, strlen(checkstr) + 1) != 0)
209 {
210 Fail("ERROR: Expected \"%s\" got \"%s\".\n", checkstr, buf);
211 }
212
213 if ((fclose( fp )) != 0)
214 {
215 Fail("ERROR: fclose failed to close \"testfile.txt\"\n");
216 }
217}
218
219
220void DoCharTest(const char *formatstr, char param, const char *checkstr)
221{
222 FILE *fp;
223 char buf[256] = { 0 };
224
225 if ((fp = fopen("testfile.txt", "w+")) == NULL )
226 {
227 Fail("ERROR: fopen failed to create testfile\n");
228 }
229 if ((DoVfprintf(fp, formatstr, param)) < 0)
230 {
231 Fail("ERROR: vfprintf failed\n");
232 }
233 if ((fseek(fp, 0, SEEK_SET)) != 0)
234 {
235 Fail("ERROR: fseek failed\n");
236 }
237 if ((fgets(buf, 100, fp)) == NULL)
238 {
239 Fail("ERROR: fgets failed\n");
240 }
241
242 if (memcmp(buf, checkstr, strlen(checkstr) + 1) != 0)
243 {
244 Fail("ERROR: failed to insert char \'%c\' (%d) into \"%s\"\n"
245 "Expected \"%s\" got \"%s\".\n",
246 param, param, formatstr, checkstr, buf);
247 }
248 fclose(fp);
249}
250
251void DoWCharTest(const char *formatstr, WCHAR param, const char *checkstr)
252{
253 FILE *fp;
254 char buf[256] = { 0 };
255
256 if ((fp = fopen("testfile.txt", "w+")) == NULL )
257 {
258 Fail("ERROR: fopen failed to create testfile\n");
259 }
260 if ((DoVfprintf(fp, formatstr, param)) < 0)
261 {
262 Fail("ERROR: vfprintf failed\n");
263 }
264 if ((fseek(fp, 0, SEEK_SET)) != 0)
265 {
266 Fail("ERROR: fseek failed\n");
267 }
268 if ((fgets(buf, 100, fp)) == NULL)
269 {
270 Fail("ERROR: fgets failed\n");
271 }
272
273 if (memcmp(buf, checkstr, strlen(checkstr) + 1) != 0)
274 {
275 Fail("ERROR: failed to insert wide char \'%c\' (%d) into \"%s\"\n"
276 "Expected \"%s\" got \"%s\".\n",
277 (char)param, param, formatstr, checkstr, buf);
278 }
279 fclose(fp);
280}
281
282void DoNumTest(const char *formatstr, int value, const char *checkstr)
283{
284 FILE *fp;
285 char buf[256] = { 0 };
286
287 if ((fp = fopen("testfile.txt", "w+")) == NULL )
288 {
289 Fail("ERROR: fopen failed to create testfile\n");
290 }
291 if ((DoVfprintf(fp, formatstr, value)) < 0)
292 {
293 Fail("ERROR: vfprintf failed\n");
294 }
295 if ((fseek(fp, 0, SEEK_SET)) != 0)
296 {
297 Fail("ERROR: fseek failed\n");
298 }
299 if ((fgets(buf, 100, fp)) == NULL)
300 {
301 Fail("ERROR: fgets failed\n");
302 }
303
304 if (memcmp(buf, checkstr, strlen(checkstr) + 1) != 0)
305 {
306 Fail("ERROR: failed to insert %#x into \"%s\"\n"
307 "Expected \"%s\" got \"%s\".\n",
308 value, formatstr, checkstr, buf);
309 }
310 fclose(fp);
311}
312
313void DoI64Test(const char *formatstr, INT64 value, char *valuestr, const char *checkstr1)
314{
315 FILE *fp;
316 char buf[256] = { 0 };
317
318 if ((fp = fopen("testfile.txt", "w+")) == NULL )
319 {
320 Fail("ERROR: fopen failed to create testfile\n");
321 }
322 if ((DoVfprintf(fp, formatstr, value)) < 0)
323 {
324 Fail("ERROR: vfprintf failed\n");
325 }
326 if ((fseek(fp, 0, SEEK_SET)) != 0)
327 {
328 Fail("ERROR: fseek failed\n");
329 }
330 if ((fgets(buf, 100, fp)) == NULL)
331 {
332 Fail("ERROR: fgets failed\n");
333 }
334
335 if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0)
336 {
337 Fail("ERROR: failed to insert %s into \"%s\"\n"
338 "Expected \"%s\", got \"%s\".\n",
339 valuestr, formatstr, checkstr1, buf);
340 }
341 fclose(fp);
342}
343
344void DoDoubleTest(const char *formatstr, double value, const char *checkstr1,
345 const char *checkstr2)
346{
347 FILE *fp;
348 char buf[256] = { 0 };
349
350 if ((fp = fopen("testfile.txt", "w+")) == NULL )
351 {
352 Fail("ERROR: fopen failed to create testfile\n");
353 }
354
355 if ((DoVfprintf(fp, formatstr, value)) < 0)
356 {
357 Fail("ERROR: vfprintf failed\n");
358 }
359 if ((fseek(fp, 0, SEEK_SET)) != 0)
360 {
361 Fail("ERROR: fseek failed\n");
362 }
363 if ((fgets(buf, 100, fp)) == NULL)
364 {
365 Fail("ERROR: fgets failed\n");
366 }
367
368 if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0 &&
369 memcmp(buf, checkstr2, strlen(checkstr2) + 1) != 0)
370 {
371 Fail("ERROR: failed to insert %f into \"%s\"\n"
372 "Expected \"%s\" or \"%s\", got \"%s\".\n",
373 value, formatstr, checkstr1, checkstr2, buf);
374 }
375 fclose(fp);
376}
377
378
379void DoArgumentPrecTest(const char *formatstr, int precision, void *param,
380 char *paramstr, const char *checkstr1, const char *checkstr2)
381{
382 FILE *fp;
383 char buf[256];
384
385 if ((fp = fopen("testfile.txt", "w+")) == NULL )
386 {
387 Fail("ERROR: fopen failed to create testfile\n");
388 }
389
390 if ((DoVfprintf(fp, formatstr, precision, param)) < 0)
391 {
392 Fail("ERROR: vfprintf failed\n");
393 }
394
395 if ((fseek(fp, 0, SEEK_SET)) != 0)
396 {
397 Fail("ERROR: fseek failed\n");
398 }
399
400 if ((fgets(buf, 100, fp)) == NULL)
401 {
402 Fail("ERROR: fgets failed\n");
403 }
404
405 if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0 &&
406 memcmp(buf, checkstr2, strlen(checkstr2) + 1) != 0)
407 {
408 Fail("ERROR: failed to insert %s into \"%s\" with precision %d\n"
409 "Expected \"%s\" or \"%s\", got \"%s\".\n", paramstr, formatstr,
410 precision, checkstr1, checkstr2, buf);
411 }
412
413
414 if ((fclose( fp )) != 0)
415 {
416 Fail("ERROR: fclose failed to close \"testfile.txt\"\n");
417 }
418
419}
420
421void DoArgumentPrecDoubleTest(const char *formatstr, int precision, double param,
422 const char *checkstr1, const char *checkstr2)
423{
424 FILE *fp;
425 char buf[256];
426
427 if ((fp = fopen("testfile.txt", "w+")) == NULL )
428 {
429 Fail("ERROR: fopen failed to create testfile\n");
430 }
431
432 if ((DoVfprintf(fp, formatstr, precision, param)) < 0)
433 {
434 Fail("ERROR: vfprintf failed\n");
435 }
436
437 if ((fseek(fp, 0, SEEK_SET)) != 0)
438 {
439 Fail("ERROR: fseek failed\n");
440 }
441
442 if ((fgets(buf, 100, fp)) == NULL)
443 {
444 Fail("ERROR: fgets failed\n");
445 }
446
447 if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0 &&
448 memcmp(buf, checkstr2, strlen(checkstr2) + 1) != 0)
449 {
450 Fail("ERROR: failed to insert %f into \"%s\" with precision %d\n"
451 "Expected \"%s\" or \"%s\", got \"%s\".\n", param, formatstr,
452 precision, checkstr1, checkstr2, buf);
453 }
454
455 if ((fclose( fp )) != 0)
456 {
457 Fail("ERROR: fclose failed to close \"testfile.txt\"\n");
458 }
459
460}
461
462#endif
463
464