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>
10 #define WIN32_NO_STATUS
13 #include <pseh/pseh2.h>
14 #include <ndk/mmfuncs.h>
15 #include <ndk/rtlfuncs.h>
18 #pragma GCC diagnostic ignored "-Wformat"
19 #pragma GCC diagnostic ignored "-Wformat-zero-length"
20 #pragma GCC diagnostic ignored "-Wnonnull"
29 SIZE_T Size
= PAGE_ROUND_UP(SizeRequested
+ PAGE_SIZE
);
30 PVOID VirtualMemory
= NULL
;
33 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &VirtualMemory
, 0, &Size
, MEM_RESERVE
, PAGE_NOACCESS
);
35 if (!NT_SUCCESS(Status
))
41 Status
= NtAllocateVirtualMemory(NtCurrentProcess(), &VirtualMemory
, 0, &Size
, MEM_COMMIT
, PAGE_READWRITE
);
42 if (!NT_SUCCESS(Status
))
45 Status
= NtFreeVirtualMemory(NtCurrentProcess(), &VirtualMemory
, &Size
, MEM_RELEASE
);
46 ok(Status
== STATUS_SUCCESS
, "Status = %lx\n", Status
);
51 StartOfBuffer
= VirtualMemory
;
52 StartOfBuffer
+= Size
- SizeRequested
;
63 PVOID VirtualMemory
= (PVOID
)PAGE_ROUND_DOWN((SIZE_T
)Pointer
);
66 Status
= NtFreeVirtualMemory(NtCurrentProcess(), &VirtualMemory
, &Size
, MEM_RELEASE
);
67 ok(Status
== STATUS_SUCCESS
, "Status = %lx\n", Status
);
70 /* NOTE: This test is not only used for all the CRT apitests, but also for
71 * user32's wsprintf. Make sure to test them all */
78 /* basic parameter tests */
80 Length
= sprintf(NULL
, NULL
);
81 EndSeh(STATUS_ACCESS_VIOLATION
);
84 Length
= sprintf(NULL
, "");
86 #if defined(TEST_CRTDLL) || defined(TEST_USER32)
87 EndSeh(STATUS_ACCESS_VIOLATION
);
89 EndSeh(STATUS_SUCCESS
);
93 Length
= sprintf(NULL
, "Hello");
95 #if defined(TEST_CRTDLL) || defined(TEST_USER32)
96 EndSeh(STATUS_ACCESS_VIOLATION
);
98 EndSeh(STATUS_SUCCESS
);
101 /* some basic formats */
102 Length
= sprintf(Buffer
, "abcde");
103 ok_str(Buffer
, "abcde");
106 Length
= sprintf(Buffer
, "%%");
110 Length
= sprintf(Buffer
, "%");
114 Length
= sprintf(Buffer
, "%%%");
118 Length
= sprintf(Buffer
, "%d", 8);
122 Length
= sprintf(Buffer
, "%s", "hello");
123 ok_str(Buffer
, "hello");
126 /* field width for %s */
127 Length
= sprintf(Buffer
, "%8s", "hello");
128 ok_str(Buffer
, " hello");
131 Length
= sprintf(Buffer
, "%4s", "hello");
132 ok_str(Buffer
, "hello");
135 Length
= sprintf(Buffer
, "%-8s", "hello");
136 ok_str(Buffer
, "hello ");
139 Length
= sprintf(Buffer
, "%-5s", "hello");
140 ok_str(Buffer
, "hello");
143 Length
= sprintf(Buffer
, "%0s", "hello");
144 ok_str(Buffer
, "hello");
147 Length
= sprintf(Buffer
, "%-0s", "hello");
148 ok_str(Buffer
, "hello");
151 Length
= sprintf(Buffer
, "%*s", -8, "hello");
153 ok_str(Buffer
, "*s");
156 ok_str(Buffer
, "hello ");
160 /* precision for %s */
161 Length
= sprintf(Buffer
, "%.s", "hello");
165 Length
= sprintf(Buffer
, "%.0s", "hello");
169 Length
= sprintf(Buffer
, "%.10s", "hello");
170 ok_str(Buffer
, "hello");
173 Length
= sprintf(Buffer
, "%.5s", "hello");
174 ok_str(Buffer
, "hello");
177 Length
= sprintf(Buffer
, "%.4s", "hello");
178 ok_str(Buffer
, "hell");
182 Length
= sprintf(Buffer
, "%.*s", -1, "hello");
184 ok_str(Buffer
, "*s");
187 ok_str(Buffer
, "hello");
190 EndSeh(STATUS_SUCCESS
);
192 String
= AllocateGuarded(6);
195 skip("Guarded allocation failure\n");
199 strcpy(String
, "hello");
201 Length
= sprintf(Buffer
, "%.8s", String
);
202 ok_str(Buffer
, "hello");
204 EndSeh(STATUS_SUCCESS
);
207 Length
= sprintf(Buffer
, "%.6s", String
);
208 ok_str(Buffer
, "hello");
210 EndSeh(STATUS_SUCCESS
);
213 Length
= sprintf(Buffer
, "%.5s", String
);
214 ok_str(Buffer
, "hello");
216 EndSeh(STATUS_SUCCESS
);
219 Length
= sprintf(Buffer
, "%.4s", String
);
220 ok_str(Buffer
, "hell");
222 EndSeh(STATUS_SUCCESS
);
226 Length
= sprintf(Buffer
, "%.5s", String
);
227 ok_str(Buffer
, "hello");
230 EndSeh(STATUS_ACCESS_VIOLATION
);
232 EndSeh(STATUS_SUCCESS
);
236 Length
= sprintf(Buffer
, "%.6s", String
);
237 ok_str(Buffer
, "hello!");
240 EndSeh(STATUS_ACCESS_VIOLATION
);
242 EndSeh(STATUS_SUCCESS
);
246 Length
= sprintf(Buffer
, "%.*s", 5, String
);
248 ok_str(Buffer
, "*s");
251 ok_str(Buffer
, "hello");
254 EndSeh(STATUS_SUCCESS
);
257 Length
= sprintf(Buffer
, "%.*s", 6, String
);
259 ok_str(Buffer
, "*s");
262 ok_str(Buffer
, "hello!");
265 EndSeh(STATUS_SUCCESS
);
267 /* both field width and precision */
269 Length
= sprintf(Buffer
, "%8.5s", String
);
270 ok_str(Buffer
, " hello");
273 EndSeh(STATUS_ACCESS_VIOLATION
);
275 EndSeh(STATUS_SUCCESS
);
279 Length
= sprintf(Buffer
, "%-*.6s", -8, String
);
281 ok_str(Buffer
, "*.6s");
284 ok_str(Buffer
, "hello! ");
287 EndSeh(STATUS_SUCCESS
);
290 Length
= sprintf(Buffer
, "%*.*s", -8, 6, String
);
292 ok_str(Buffer
, "*.*s");
295 ok_str(Buffer
, "hello! ");
298 EndSeh(STATUS_SUCCESS
);