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/*NOTE:
6The creation of the test file within each function is because the FILE
7structure is not defined within pal.h. Therefore, unable to have
8function with this as a return type.
9*/
10
11#ifndef __FPRINTF_H__
12#define __FPRINTF_H__
13
14void DoStrTest(const char *formatstr, char* param, const char *checkstr)
15{
16 FILE *fp;
17 char buf[256] = { 0 };
18
19 if ((fp = fopen("testfile.txt", "w+")) == NULL )
20 Fail("ERROR: fopen failed to create testfile\n");
21 if ((fprintf(fp, formatstr, param)) < 0)
22 Fail("ERROR: fprintf failed\n");
23 if ((fseek(fp, 0, SEEK_SET)) != 0)
24 Fail("ERROR: fseek failed\n");
25 if ((fgets(buf, 100, fp)) == NULL)
26 Fail("ERROR: fseek failed\n");
27
28 if (memcmp(buf, checkstr, strlen(buf) + 1) != 0)
29 {
30 Fail("ERROR: failed to insert string \"%s\" into \"%s\"\n"
31 "Expected \"%s\" got \"%s\".\n",
32 param, formatstr, checkstr, buf);
33 }
34 fclose(fp);
35}
36
37void DoWStrTest(const char *formatstr, WCHAR* param, const char *checkstr)
38{
39 FILE *fp;
40 char buf[256] = { 0 };
41
42 if ((fp = fopen("testfile.txt", "w+")) == NULL )
43 Fail("ERROR: fopen failed to create testfile\n");
44 if ((fprintf(fp, formatstr, param)) < 0)
45 Fail("ERROR: fprintf failed\n");
46 if ((fseek(fp, 0, SEEK_SET)) != 0)
47 Fail("ERROR: fseek failed\n");
48 if ((fgets(buf, 100, fp)) == NULL)
49 Fail("ERROR: fseek failed\n");
50
51 if (memcmp(buf, checkstr, strlen(buf) + 1) != 0)
52 {
53 Fail("ERROR: failed to insert wide string \"%s\" into \"%s\"\n"
54 "Expected \"%s\" got \"%s\".\n",
55 convertC(param), formatstr, checkstr, buf);
56 }
57 fclose(fp);
58}
59
60
61void DoCharTest(const char *formatstr, char param, const char *checkstr)
62{
63 FILE *fp;
64 char buf[256] = { 0 };
65
66 if ((fp = fopen("testfile.txt", "w+")) == NULL )
67 Fail("ERROR: fopen failed to create testfile\n");
68 if ((fprintf(fp, formatstr, param)) < 0)
69 Fail("ERROR: fprintf failed\n");
70 if ((fseek(fp, 0, SEEK_SET)) != 0)
71 Fail("ERROR: fseek failed\n");
72 if ((fgets(buf, 100, fp)) == NULL)
73 Fail("ERROR: fseek failed\n");
74
75 if (memcmp(buf, checkstr, strlen(buf) + 1) != 0)
76 {
77 Fail("ERROR: failed to insert char \'%c\' (%d) into \"%s\"\n"
78 "Expected \"%s\" got \"%s\".\n",
79 param, param, formatstr, checkstr, buf);
80 }
81 fclose(fp);
82}
83
84void DoWCharTest(const char *formatstr, WCHAR param, const char *checkstr)
85{
86 FILE *fp;
87 char buf[256] = { 0 };
88
89 if ((fp = fopen("testfile.txt", "w+")) == NULL )
90 Fail("ERROR: fopen failed to create testfile\n");
91 if ((fprintf(fp, formatstr, param)) < 0)
92 Fail("ERROR: fprintf failed\n");
93 if ((fseek(fp, 0, SEEK_SET)) != 0)
94 Fail("ERROR: fseek failed\n");
95 if ((fgets(buf, 100, fp)) == NULL)
96 Fail("ERROR: fseek failed\n");
97
98 if (memcmp(buf, checkstr, strlen(buf) + 1) != 0)
99 {
100 Fail("ERROR: failed to insert wide char \'%c\' (%d) into \"%s\"\n"
101 "Expected \"%s\" got \"%s\".\n",
102 (char)param, param, formatstr, checkstr, buf);
103 }
104 fclose(fp);
105}
106
107void DoNumTest(const char *formatstr, int value, const char *checkstr)
108{
109 FILE *fp;
110 char buf[256] = { 0 };
111
112 if ((fp = fopen("testfile.txt", "w+")) == NULL )
113 Fail("ERROR: fopen failed to create testfile\n");
114 if ((fprintf(fp, formatstr, value)) < 0)
115 Fail("ERROR: fprintf failed\n");
116 if ((fseek(fp, 0, SEEK_SET)) != 0)
117 Fail("ERROR: fseek failed\n");
118 if ((fgets(buf, 100, fp)) == NULL)
119 Fail("ERROR: fseek failed\n");
120
121 if (memcmp(buf, checkstr, strlen(buf) + 1) != 0)
122 {
123 Fail("ERROR: failed to insert %#x into \"%s\"\n"
124 "Expected \"%s\" got \"%s\".\n",
125 value, formatstr, checkstr, buf);
126 }
127 fclose(fp);
128}
129
130void DoI64Test(const char *formatstr, INT64 value, char *valuestr, const char *checkstr1, const char *checkstr2)
131{
132 FILE *fp;
133 char buf[256] = { 0 };
134
135 if ((fp = fopen("testfile.txt", "w+")) == NULL )
136 Fail("ERROR: fopen failed to create testfile\n");
137 if ((fprintf(fp, formatstr, value)) < 0)
138 Fail("ERROR: fprintf failed\n");
139 if ((fseek(fp, 0, SEEK_SET)) != 0)
140 Fail("ERROR: fseek failed\n");
141 if ((fgets(buf, 100, fp)) == NULL)
142 Fail("ERROR: fseek failed\n");
143
144 if (memcmp(buf, checkstr1, strlen(buf) + 1) != 0 &&
145 memcmp(buf, checkstr2, strlen(buf) + 1) != 0)
146 {
147 Fail("ERROR: failed to insert %s into \"%s\"\n"
148 "Expected \"%s\" or \"%s\", got \"%s\".\n",
149 valuestr, formatstr, checkstr1, checkstr2, buf);
150 }
151 fclose(fp);
152}
153
154void DoDoubleTest(const char *formatstr, double value, const char *checkstr1, const char *checkstr2)
155{
156 FILE *fp;
157 char buf[256] = { 0 };
158
159 if ((fp = fopen("testfile.txt", "w+")) == NULL )
160 Fail("ERROR: fopen failed to create testfile\n");
161 if ((fprintf(fp, formatstr, value)) < 0)
162 Fail("ERROR: fprintf failed\n");
163 if ((fseek(fp, 0, SEEK_SET)) != 0)
164 Fail("ERROR: fseek failed\n");
165 if ((fgets(buf, 100, fp)) == NULL)
166 Fail("ERROR: fseek failed\n");
167
168 if (memcmp(buf, checkstr1, strlen(buf) + 1) != 0 &&
169 memcmp(buf, checkstr2, strlen(buf) + 1) != 0)
170 {
171 Fail("ERROR: failed to insert %f into \"%s\"\n"
172 "Expected \"%s\" or \"%s\", got \"%s\".\n",
173 value, formatstr, checkstr1, checkstr2, buf);
174 }
175 fclose(fp);
176}
177#endif
178