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: vsprintf.h |
8 | ** |
9 | ** Purpose: Helper functions for the vsprintf tests. |
10 | ** |
11 | ** |
12 | **===================================================================*/ |
13 | #ifndef __VSPRINTF_H__ |
14 | #define __VSPRINTF_H__ |
15 | |
16 | /* These functions leaks memory like crazy. C'est la vie. */ |
17 | int testvsp(char* buf, size_t buffSize, const char* format, ...) |
18 | { |
19 | int retVal; |
20 | va_list arglist; |
21 | |
22 | va_start(arglist, format); |
23 | retVal = _vsnprintf_s(buf, buffSize, _TRUNCATE, format, arglist); |
24 | va_end(arglist); |
25 | |
26 | return (retVal); |
27 | } |
28 | |
29 | void DoStrTest(const char *formatstr, char* param, const char *checkstr) |
30 | { |
31 | char buf[256] = { 0 }; |
32 | |
33 | testvsp(buf, _countof(buf), formatstr, param); |
34 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
35 | { |
36 | Fail("ERROR: failed to insert string \"%s\" into \"%s\"\n" |
37 | "Expected \"%s\" got \"%s\".\n" , |
38 | param, formatstr, checkstr, buf); |
39 | } |
40 | } |
41 | |
42 | void DoWStrTest(const char *formatstr, WCHAR* param, const char *checkstr) |
43 | { |
44 | char buf[256] = { 0 }; |
45 | |
46 | testvsp(buf, _countof(buf), formatstr, param); |
47 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
48 | { |
49 | Fail("ERROR: failed to insert wide string \"%s\" into \"%s\"\n" |
50 | "Expected \"%s\" got \"%s\".\n" , |
51 | convertC(param), formatstr, checkstr, buf); |
52 | } |
53 | } |
54 | |
55 | |
56 | void DoCharTest(const char *formatstr, char param, const char *checkstr) |
57 | { |
58 | char buf[256] = { 0 }; |
59 | |
60 | testvsp(buf, _countof(buf), formatstr, param); |
61 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
62 | { |
63 | Fail("ERROR: failed to insert char \'%c\' (%d) into \"%s\"\n" |
64 | "Expected \"%s\" got \"%s\".\n" , |
65 | param, param, formatstr, checkstr, buf); |
66 | } |
67 | } |
68 | |
69 | void DoWCharTest(const char *formatstr, WCHAR param, const char *checkstr) |
70 | { |
71 | char buf[256] = { 0 }; |
72 | |
73 | testvsp(buf, _countof(buf), formatstr, param); |
74 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
75 | { |
76 | Fail("ERROR: failed to insert wide char \'%c\' (%d) into \"%s\"\n" |
77 | "Expected \"%s\" got \"%s\".\n" , |
78 | (char)param, param, formatstr, checkstr, buf); |
79 | } |
80 | } |
81 | |
82 | void DoNumTest(const char *formatstr, int value, const char *checkstr) |
83 | { |
84 | char buf[256] = { 0 }; |
85 | |
86 | testvsp(buf, _countof(buf), formatstr, value); |
87 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
88 | { |
89 | Fail("ERROR: failed to insert %#x into \"%s\"\n" |
90 | "Expected \"%s\" got \"%s\".\n" , |
91 | value, formatstr, checkstr, buf); |
92 | } |
93 | } |
94 | |
95 | void DoI64Test(const char *formatstr, INT64 value, char *valuestr, const char *checkstr) |
96 | { |
97 | char buf[256] = { 0 }; |
98 | |
99 | testvsp(buf, _countof(buf), formatstr, value); |
100 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
101 | { |
102 | Fail("ERROR: failed to insert %s into \"%s\"\n" |
103 | "Expected \"%s\" got \"%s\".\n" , |
104 | valuestr, formatstr, checkstr, buf); |
105 | } |
106 | } |
107 | void DoDoubleTest(const char *formatstr, double value, const char *checkstr1, char |
108 | *checkstr2) |
109 | { |
110 | char buf[256] = { 0 }; |
111 | |
112 | testvsp(buf, _countof(buf), formatstr, value); |
113 | if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0 && |
114 | memcmp(buf, checkstr2, strlen(checkstr2) + 1) != 0) |
115 | { |
116 | Fail("ERROR: failed to insert %f into \"%s\"\n" |
117 | "Expected \"%s\" or \"%s\", got \"%s\".\n" , |
118 | value, formatstr, checkstr1, checkstr2, buf); |
119 | } |
120 | } |
121 | /*FROM TEST 9*/ |
122 | void DoArgumentPrecTest(const char *formatstr, int precision, void *param, |
123 | char *paramstr, const char *checkstr1, const char *checkstr2) |
124 | { |
125 | char buf[256]; |
126 | |
127 | testvsp(buf, _countof(buf), formatstr, precision, param); |
128 | if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0 && |
129 | memcmp(buf, checkstr2, strlen(checkstr2) + 1) != 0) |
130 | { |
131 | Fail("ERROR: failed to insert %s into \"%s\" with precision %d\n" |
132 | "Expected \"%s\" or \"%s\", got \"%s\".\n" , paramstr, formatstr, |
133 | precision, checkstr1, checkstr2, buf); |
134 | } |
135 | |
136 | } |
137 | |
138 | void DoArgumentPrecDoubleTest(const char *formatstr, int precision, double param, |
139 | const char *checkstr1, const char *checkstr2) |
140 | { |
141 | char buf[256]; |
142 | |
143 | testvsp(buf, _countof(buf), formatstr, precision, param); |
144 | if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0 && |
145 | memcmp(buf, checkstr2, strlen(checkstr2) + 1) != 0) |
146 | { |
147 | Fail("ERROR: failed to insert %f into \"%s\" with precision %d\n" |
148 | "Expected \"%s\" or \"%s\", got \"%s\".\n" , param, formatstr, |
149 | precision, checkstr1, checkstr2, buf); |
150 | } |
151 | } |
152 | /*FROM TEST4*/ |
153 | void DoPointerTest(const char *formatstr, void* param, char* paramstr, |
154 | const char *checkstr1) |
155 | { |
156 | char buf[256] = { 0 }; |
157 | |
158 | testvsp(buf, _countof(buf), formatstr, param); |
159 | if (memcmp(buf, checkstr1, strlen(checkstr1) + 1)) |
160 | { |
161 | Fail("ERROR: failed to insert %s into \"%s\"\n" |
162 | "Expected \"%s\" got \"%s\".\n" , |
163 | paramstr, formatstr, checkstr1, buf); |
164 | } |
165 | } |
166 | |
167 | void DoI64DoubleTest(const char *formatstr, INT64 value, char *valuestr, |
168 | const char *checkstr1) |
169 | { |
170 | char buf[256] = { 0 }; |
171 | |
172 | testvsp(buf, _countof(buf), formatstr, value); |
173 | if (memcmp(buf, checkstr1, strlen(checkstr1) + 1) != 0) |
174 | { |
175 | Fail("ERROR: failed to insert %s into \"%s\"\n" |
176 | "Expected \"%s\", got \"%s\".\n" , |
177 | valuestr, formatstr, checkstr1, buf); |
178 | } |
179 | } |
180 | |
181 | void DoTest(const char *formatstr, int param, const char *checkstr) |
182 | { |
183 | char buf[256] = { 0 }; |
184 | int n = -1; |
185 | |
186 | testvsp(buf, _countof(buf), formatstr, &n); |
187 | |
188 | if (n != param) |
189 | { |
190 | Fail("ERROR: Expected count parameter to resolve to %d, got %X\n" , |
191 | param, n); |
192 | } |
193 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
194 | { |
195 | Fail("ERROR: Expected \"%s\" got \"%s\".\n" , checkstr, buf); |
196 | } |
197 | } |
198 | |
199 | void DoShortTest(const char *formatstr, int param, const char *checkstr) |
200 | { |
201 | char buf[256] = { 0 }; |
202 | short int n = -1; |
203 | |
204 | testvsp(buf, _countof(buf), formatstr, &n); |
205 | |
206 | if (n != param) |
207 | { |
208 | Fail("ERROR: Expected count parameter to resolve to %d, got %X\n" , |
209 | param, n); |
210 | } |
211 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
212 | { |
213 | Fail("ERROR: Expected \"%s\" got \"%s\".\n" , checkstr, buf); |
214 | } |
215 | } |
216 | |
217 | #endif |
218 | |
219 | |