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