ReactOS 0.4.17-dev-218-g5635d24
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 */
51#ifdef TEST_USER32
52START_TEST(wsprintfApi)
53#else
55#endif
56{
57 int Length;
60
61#ifndef TEST_STATIC_CRT
62 if (!Init())
63 {
64 skip("Skipping tests, because sprintf is not available\n");
65 return;
66 }
67#endif
68
69 /* basic parameter tests */
72#if defined(TEST_CRTDLL) || defined(TEST_USER32)
74#else
76#endif
77
78 StartSeh()
79 Length = sprintf(NULL, "");
81#if defined(TEST_CRTDLL) || defined(TEST_USER32)
83#else
85#endif
86
87 StartSeh()
88 Length = sprintf(NULL, "Hello");
90#if defined(TEST_CRTDLL) || defined(TEST_USER32)
92#else
94#endif
95
96 /* some basic formats */
97 Length = sprintf(Buffer, "abcde");
98 ok_str(Buffer, "abcde");
100
101 Length = sprintf(Buffer, "%%");
104
105 Length = sprintf(Buffer, "%");
108
109 Length = sprintf(Buffer, "%%%");
110 ok_str(Buffer, "%");
111 ok_int(Length, 1);
112
113 Length = sprintf(Buffer, "%d", 8);
115 ok_int(Length, 1);
116
117 Length = sprintf(Buffer, "%s", "hello");
118 ok_str(Buffer, "hello");
119 ok_int(Length, 5);
120
121 /* field width for %s */
122 Length = sprintf(Buffer, "%8s", "hello");
123 ok_str(Buffer, " hello");
125
126 Length = sprintf(Buffer, "%4s", "hello");
127 ok_str(Buffer, "hello");
128 ok_int(Length, 5);
129
130 Length = sprintf(Buffer, "%-8s", "hello");
131 ok_str(Buffer, "hello ");
132 ok_int(Length, 8);
133
134 Length = sprintf(Buffer, "%-5s", "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, "%-0s", "hello");
143 ok_str(Buffer, "hello");
144 ok_int(Length, 5);
145
146 Length = sprintf(Buffer, "%*s", -8, "hello");
147#ifdef TEST_USER32
148 ok_str(Buffer, "*s");
149 ok_int(Length, 2);
150#else
151 ok_str(Buffer, "hello ");
152 ok_int(Length, 8);
153#endif
154
155 /* precision for %s */
156 Length = sprintf(Buffer, "%.s", "hello");
157 ok_str(Buffer, "");
158 ok_int(Length, 0);
159
160 Length = sprintf(Buffer, "%.0s", "hello");
161 ok_str(Buffer, "");
162 ok_int(Length, 0);
163
164 Length = sprintf(Buffer, "%.10s", "hello");
165 ok_str(Buffer, "hello");
166 ok_int(Length, 5);
167
168 Length = sprintf(Buffer, "%.5s", "hello");
169 ok_str(Buffer, "hello");
170 ok_int(Length, 5);
171
172 Length = sprintf(Buffer, "%.4s", "hello");
173 ok_str(Buffer, "hell");
175
176 StartSeh()
177 Length = sprintf(Buffer, "%.*s", -1, "hello");
178#ifdef TEST_USER32
179 ok_str(Buffer, "*s");
180 ok_int(Length, 2);
181#else
182 ok_str(Buffer, "hello");
183 ok_int(Length, 5);
184#endif
186
188 if (!String)
189 {
190 skip("Guarded allocation failure\n");
191 return;
192 }
193
194 strcpy(String, "hello");
195 StartSeh()
196 Length = sprintf(Buffer, "%.8s", String);
197 ok_str(Buffer, "hello");
198 ok_int(Length, 5);
200
201 StartSeh()
202 Length = sprintf(Buffer, "%.6s", String);
203 ok_str(Buffer, "hello");
204 ok_int(Length, 5);
206
207 StartSeh()
208 Length = sprintf(Buffer, "%.5s", String);
209 ok_str(Buffer, "hello");
210 ok_int(Length, 5);
212
213 StartSeh()
214 Length = sprintf(Buffer, "%.4s", String);
215 ok_str(Buffer, "hell");
216 ok_int(Length, 4);
218
219 String[5] = '!';
220 StartSeh()
221 Length = sprintf(Buffer, "%.5s", String);
222 ok_str(Buffer, "hello");
223 ok_int(Length, 5);
224#ifdef TEST_USER32
226#else
228#endif
229
230 StartSeh()
231 Length = sprintf(Buffer, "%.6s", String);
234#ifdef TEST_USER32
236#else
238#endif
239
240 StartSeh()
241 Length = sprintf(Buffer, "%.*s", 5, String);
242#ifdef TEST_USER32
243 ok_str(Buffer, "*s");
244 ok_int(Length, 2);
245#else
246 ok_str(Buffer, "hello");
247 ok_int(Length, 5);
248#endif
250
251 StartSeh()
252 Length = sprintf(Buffer, "%.*s", 6, String);
253#ifdef TEST_USER32
254 ok_str(Buffer, "*s");
255 ok_int(Length, 2);
256#else
257 ok_str(Buffer, "hello!");
258 ok_int(Length, 6);
259#endif
261
262 /* both field width and precision */
263 StartSeh()
264 Length = sprintf(Buffer, "%8.5s", String);
265 ok_str(Buffer, " hello");
266 ok_int(Length, 8);
267#ifdef TEST_USER32
269#else
271#endif
272
273 StartSeh()
274 Length = sprintf(Buffer, "%-*.6s", -8, String);
275#ifdef TEST_USER32
276 ok_str(Buffer, "*.6s");
277 ok_int(Length, 4);
278#else
279 ok_str(Buffer, "hello! ");
280 ok_int(Length, 8);
281#endif
283
284 StartSeh()
285 Length = sprintf(Buffer, "%*.*s", -8, 6, String);
286#ifdef TEST_USER32
287 ok_str(Buffer, "*.*s");
288 ok_int(Length, 4);
289#else
290 ok_str(Buffer, "hello! ");
291 ok_int(Length, 8);
292#endif
294
296}
#define GetNTVersion()
Definition: apitest.h:17
#define EndSeh(ExpectedStatus)
Definition: apitest.h:99
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
#define __cdecl
Definition: corecrt.h:121
_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
GLdouble s
Definition: gl.h:2039
Definition: ctx.idl:7
PCHAR String
Definition: sprintf.c:59
int(__cdecl * PFN_sprintf)(char *_Dest, const char *_Format,...)
Definition: sprintf.c:31
FreeGuarded(String)
static BOOL Init(void)
Definition: sprintf.c:34
#define sprintf
Definition: sprintf.c:45
StartSeh() Length
Length
Definition: sprintf.c:97
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
char CHAR
Definition: pedump.c:57
_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