ReactOS 0.4.16-dev-1946-g52006dd
sprintf.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for sprintf
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8#include <apitest.h>
9#include <apitest_guard.h>
10
11#define WIN32_NO_STATUS
12#include <stdio.h>
13#include <tchar.h>
14#include <pseh/pseh2.h>
15#include <ndk/mmfuncs.h>
16#include <ndk/rtlfuncs.h>
17
18#ifdef _MSC_VER
19#pragma warning(disable:4778) // unterminated format string '%'
20#elif defined(__GNUC__)
21#pragma GCC diagnostic ignored "-Wformat"
22#pragma GCC diagnostic ignored "-Wformat-zero-length"
23#pragma GCC diagnostic ignored "-Wnonnull"
24#if __GNUC__ >= 7
25#pragma GCC diagnostic ignored "-Wformat-overflow"
26#endif
27#endif
28
29#ifndef TEST_STATIC_CRT
30
31typedef int (__cdecl *PFN_sprintf)(char *_Dest, const char *_Format, ...);
33
34static BOOL Init(void)
35{
37#ifdef TEST_USER32
39#else
41#endif
42 ok(p_sprintf != NULL, "Failed to load sprintf from %s\n", TEST_DLL_NAME);
43 return (p_sprintf != NULL);
44}
45#define sprintf p_sprintf
46
47#endif // !TEST_STATIC_CRT
48
49/* NOTE: This test is not only used for all the CRT apitests, but also for
50 * user32's wsprintf. Make sure to test them all */
52{
53 int Length;
54 CHAR Buffer[128];
56
57#ifndef TEST_STATIC_CRT
58 if (!Init())
59 {
60 skip("Skipping tests, because sprintf is not available\n");
61 return;
62 }
63#endif
64
65 /* basic parameter tests */
66 StartSeh()
68#if defined(TEST_CRTDLL) || defined(TEST_USER32)
70#else
72#endif
73
74 StartSeh()
75 Length = sprintf(NULL, "");
77#if defined(TEST_CRTDLL) || defined(TEST_USER32)
79#else
81#endif
82
83 StartSeh()
84 Length = sprintf(NULL, "Hello");
86#if defined(TEST_CRTDLL) || defined(TEST_USER32)
88#else
90#endif
91
92 /* some basic formats */
93 Length = sprintf(Buffer, "abcde");
94 ok_str(Buffer, "abcde");
95 ok_int(Length, 5);
96
97 Length = sprintf(Buffer, "%%");
98 ok_str(Buffer, "%");
99 ok_int(Length, 1);
100
101 Length = sprintf(Buffer, "%");
102 ok_str(Buffer, "");
103 ok_int(Length, 0);
104
105 Length = sprintf(Buffer, "%%%");
106 ok_str(Buffer, "%");
107 ok_int(Length, 1);
108
109 Length = sprintf(Buffer, "%d", 8);
110 ok_str(Buffer, "8");
111 ok_int(Length, 1);
112
113 Length = sprintf(Buffer, "%s", "hello");
114 ok_str(Buffer, "hello");
115 ok_int(Length, 5);
116
117 /* field width for %s */
118 Length = sprintf(Buffer, "%8s", "hello");
119 ok_str(Buffer, " hello");
120 ok_int(Length, 8);
121
122 Length = sprintf(Buffer, "%4s", "hello");
123 ok_str(Buffer, "hello");
124 ok_int(Length, 5);
125
126 Length = sprintf(Buffer, "%-8s", "hello");
127 ok_str(Buffer, "hello ");
128 ok_int(Length, 8);
129
130 Length = sprintf(Buffer, "%-5s", "hello");
131 ok_str(Buffer, "hello");
132 ok_int(Length, 5);
133
134 Length = sprintf(Buffer, "%0s", "hello");
135 ok_str(Buffer, "hello");
136 ok_int(Length, 5);
137
138 Length = sprintf(Buffer, "%-0s", "hello");
139 ok_str(Buffer, "hello");
140 ok_int(Length, 5);
141
142 Length = sprintf(Buffer, "%*s", -8, "hello");
143#ifdef TEST_USER32
144 ok_str(Buffer, "*s");
145 ok_int(Length, 2);
146#else
147 ok_str(Buffer, "hello ");
148 ok_int(Length, 8);
149#endif
150
151 /* precision for %s */
152 Length = sprintf(Buffer, "%.s", "hello");
153 ok_str(Buffer, "");
154 ok_int(Length, 0);
155
156 Length = sprintf(Buffer, "%.0s", "hello");
157 ok_str(Buffer, "");
158 ok_int(Length, 0);
159
160 Length = sprintf(Buffer, "%.10s", "hello");
161 ok_str(Buffer, "hello");
162 ok_int(Length, 5);
163
164 Length = sprintf(Buffer, "%.5s", "hello");
165 ok_str(Buffer, "hello");
166 ok_int(Length, 5);
167
168 Length = sprintf(Buffer, "%.4s", "hello");
169 ok_str(Buffer, "hell");
170 ok_int(Length, 4);
171
172 StartSeh()
173 Length = sprintf(Buffer, "%.*s", -1, "hello");
174#ifdef TEST_USER32
175 ok_str(Buffer, "*s");
176 ok_int(Length, 2);
177#else
178 ok_str(Buffer, "hello");
179 ok_int(Length, 5);
180#endif
182
184 if (!String)
185 {
186 skip("Guarded allocation failure\n");
187 return;
188 }
189
190 strcpy(String, "hello");
191 StartSeh()
192 Length = sprintf(Buffer, "%.8s", String);
193 ok_str(Buffer, "hello");
194 ok_int(Length, 5);
196
197 StartSeh()
198 Length = sprintf(Buffer, "%.6s", String);
199 ok_str(Buffer, "hello");
200 ok_int(Length, 5);
202
203 StartSeh()
204 Length = sprintf(Buffer, "%.5s", String);
205 ok_str(Buffer, "hello");
206 ok_int(Length, 5);
208
209 StartSeh()
210 Length = sprintf(Buffer, "%.4s", String);
211 ok_str(Buffer, "hell");
212 ok_int(Length, 4);
214
215 String[5] = '!';
216 StartSeh()
217 Length = sprintf(Buffer, "%.5s", String);
218 ok_str(Buffer, "hello");
219 ok_int(Length, 5);
220#ifdef TEST_USER32
222#else
224#endif
225
226 StartSeh()
227 Length = sprintf(Buffer, "%.6s", String);
228 ok_str(Buffer, "hello!");
229 ok_int(Length, 6);
230#ifdef TEST_USER32
232#else
234#endif
235
236 StartSeh()
237 Length = sprintf(Buffer, "%.*s", 5, String);
238#ifdef TEST_USER32
239 ok_str(Buffer, "*s");
240 ok_int(Length, 2);
241#else
242 ok_str(Buffer, "hello");
243 ok_int(Length, 5);
244#endif
246
247 StartSeh()
248 Length = sprintf(Buffer, "%.*s", 6, String);
249#ifdef TEST_USER32
250 ok_str(Buffer, "*s");
251 ok_int(Length, 2);
252#else
253 ok_str(Buffer, "hello!");
254 ok_int(Length, 6);
255#endif
257
258 /* both field width and precision */
259 StartSeh()
260 Length = sprintf(Buffer, "%8.5s", String);
261 ok_str(Buffer, " hello");
262 ok_int(Length, 8);
263#ifdef TEST_USER32
265#else
267#endif
268
269 StartSeh()
270 Length = sprintf(Buffer, "%-*.6s", -8, String);
271#ifdef TEST_USER32
272 ok_str(Buffer, "*.6s");
273 ok_int(Length, 4);
274#else
275 ok_str(Buffer, "hello! ");
276 ok_int(Length, 8);
277#endif
279
280 StartSeh()
281 Length = sprintf(Buffer, "%*.*s", -8, 6, String);
282#ifdef TEST_USER32
283 ok_str(Buffer, "*.*s");
284 ok_int(Length, 4);
285#else
286 ok_str(Buffer, "hello! ");
287 ok_int(Length, 8);
288#endif
290
292}
#define __cdecl
Definition: accygwin.h:79
#define GetNTVersion()
Definition: apitest.h:17
#define StartSeh()
Definition: apitest.h:93
#define EndSeh(ExpectedStatus)
Definition: apitest.h:99
static VOID FreeGuarded(_In_ PVOID Pointer)
Definition: apitest_guard.h:45
static PVOID AllocateGuarded(_In_ SIZE_T SizeRequested)
Definition: apitest_guard.h:10
#define ok_str(x, y)
Definition: atltest.h:127
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
#define ok_int(expression, result)
Definition: atltest.h:134
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define GetProcAddress(x, y)
Definition: compat.h:753
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
_In_z_ _Printf_format_string_ char const *const _Format
Definition: printf.c:19
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
unsigned int BOOL
Definition: ntddk_ex.h:94
#define STATUS_ACCESS_VIOLATION
int(__cdecl * PFN_sprintf)(char *_Dest, const char *_Format,...)
Definition: sprintf.c:31
static BOOL Init(void)
Definition: sprintf.c:34
#define sprintf
Definition: sprintf.c:45
static PFN_sprintf p_sprintf
Definition: sprintf.c:32
#define TEST_DLL_NAME
Definition: wsprintf.c:3
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
_Dest
Definition: stdlib.h:926
strcpy
Definition: string.h:131
#define _WIN32_WINNT_VISTA
Definition: sdkddkver.h:25
#define STATUS_SUCCESS
Definition: shellext.h:65
static PVOID hdll
Definition: shimdbg.c:126
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2439
char CHAR
Definition: xmlstorage.h:175