ReactOS 0.4.15-dev-8236-g99f0937
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/* NOTE: This test is not only used for all the CRT apitests, but also for
30 * user32's wsprintf. Make sure to test them all */
32{
33 int Length;
34 CHAR Buffer[128];
36
37 /* basic parameter tests */
38 StartSeh()
41
42 StartSeh()
43 Length = sprintf(NULL, "");
44 ok_int(Length, 0);
45#if TEST_CRTDLL || TEST_USER32
47#else
49#endif
50
51 StartSeh()
52 Length = sprintf(NULL, "Hello");
53 ok_int(Length, 5);
54#if TEST_CRTDLL || TEST_USER32
56#else
58#endif
59
60 /* some basic formats */
61 Length = sprintf(Buffer, "abcde");
62 ok_str(Buffer, "abcde");
63 ok_int(Length, 5);
64
65 Length = sprintf(Buffer, "%%");
66 ok_str(Buffer, "%");
67 ok_int(Length, 1);
68
69 Length = sprintf(Buffer, "%");
70 ok_str(Buffer, "");
71 ok_int(Length, 0);
72
73 Length = sprintf(Buffer, "%%%");
74 ok_str(Buffer, "%");
75 ok_int(Length, 1);
76
77 Length = sprintf(Buffer, "%d", 8);
78 ok_str(Buffer, "8");
79 ok_int(Length, 1);
80
81 Length = sprintf(Buffer, "%s", "hello");
82 ok_str(Buffer, "hello");
83 ok_int(Length, 5);
84
85 /* field width for %s */
86 Length = sprintf(Buffer, "%8s", "hello");
87 ok_str(Buffer, " hello");
88 ok_int(Length, 8);
89
90 Length = sprintf(Buffer, "%4s", "hello");
91 ok_str(Buffer, "hello");
92 ok_int(Length, 5);
93
94 Length = sprintf(Buffer, "%-8s", "hello");
95 ok_str(Buffer, "hello ");
96 ok_int(Length, 8);
97
98 Length = sprintf(Buffer, "%-5s", "hello");
99 ok_str(Buffer, "hello");
100 ok_int(Length, 5);
101
102 Length = sprintf(Buffer, "%0s", "hello");
103 ok_str(Buffer, "hello");
104 ok_int(Length, 5);
105
106 Length = sprintf(Buffer, "%-0s", "hello");
107 ok_str(Buffer, "hello");
108 ok_int(Length, 5);
109
110 Length = sprintf(Buffer, "%*s", -8, "hello");
111#ifdef TEST_USER32
112 ok_str(Buffer, "*s");
113 ok_int(Length, 2);
114#else
115 ok_str(Buffer, "hello ");
116 ok_int(Length, 8);
117#endif
118
119 /* precision for %s */
120 Length = sprintf(Buffer, "%.s", "hello");
121 ok_str(Buffer, "");
122 ok_int(Length, 0);
123
124 Length = sprintf(Buffer, "%.0s", "hello");
125 ok_str(Buffer, "");
126 ok_int(Length, 0);
127
128 Length = sprintf(Buffer, "%.10s", "hello");
129 ok_str(Buffer, "hello");
130 ok_int(Length, 5);
131
132 Length = sprintf(Buffer, "%.5s", "hello");
133 ok_str(Buffer, "hello");
134 ok_int(Length, 5);
135
136 Length = sprintf(Buffer, "%.4s", "hello");
137 ok_str(Buffer, "hell");
138 ok_int(Length, 4);
139
140 StartSeh()
141 Length = sprintf(Buffer, "%.*s", -1, "hello");
142#ifdef TEST_USER32
143 ok_str(Buffer, "*s");
144 ok_int(Length, 2);
145#else
146 ok_str(Buffer, "hello");
147 ok_int(Length, 5);
148#endif
150
152 if (!String)
153 {
154 skip("Guarded allocation failure\n");
155 return;
156 }
157
158 strcpy(String, "hello");
159 StartSeh()
160 Length = sprintf(Buffer, "%.8s", String);
161 ok_str(Buffer, "hello");
162 ok_int(Length, 5);
164
165 StartSeh()
166 Length = sprintf(Buffer, "%.6s", String);
167 ok_str(Buffer, "hello");
168 ok_int(Length, 5);
170
171 StartSeh()
172 Length = sprintf(Buffer, "%.5s", String);
173 ok_str(Buffer, "hello");
174 ok_int(Length, 5);
176
177 StartSeh()
178 Length = sprintf(Buffer, "%.4s", String);
179 ok_str(Buffer, "hell");
180 ok_int(Length, 4);
182
183 String[5] = '!';
184 StartSeh()
185 Length = sprintf(Buffer, "%.5s", String);
186 ok_str(Buffer, "hello");
187 ok_int(Length, 5);
188#ifdef TEST_USER32
190#else
192#endif
193
194 StartSeh()
195 Length = sprintf(Buffer, "%.6s", String);
196 ok_str(Buffer, "hello!");
197 ok_int(Length, 6);
198#ifdef TEST_USER32
200#else
202#endif
203
204 StartSeh()
205 Length = sprintf(Buffer, "%.*s", 5, String);
206#ifdef TEST_USER32
207 ok_str(Buffer, "*s");
208 ok_int(Length, 2);
209#else
210 ok_str(Buffer, "hello");
211 ok_int(Length, 5);
212#endif
214
215 StartSeh()
216 Length = sprintf(Buffer, "%.*s", 6, String);
217#ifdef TEST_USER32
218 ok_str(Buffer, "*s");
219 ok_int(Length, 2);
220#else
221 ok_str(Buffer, "hello!");
222 ok_int(Length, 6);
223#endif
225
226 /* both field width and precision */
227 StartSeh()
228 Length = sprintf(Buffer, "%8.5s", String);
229 ok_str(Buffer, " hello");
230 ok_int(Length, 8);
231#ifdef TEST_USER32
233#else
235#endif
236
237 StartSeh()
238 Length = sprintf(Buffer, "%-*.6s", -8, String);
239#ifdef TEST_USER32
240 ok_str(Buffer, "*.6s");
241 ok_int(Length, 4);
242#else
243 ok_str(Buffer, "hello! ");
244 ok_int(Length, 8);
245#endif
247
248 StartSeh()
249 Length = sprintf(Buffer, "%*.*s", -8, 6, String);
250#ifdef TEST_USER32
251 ok_str(Buffer, "*.*s");
252 ok_int(Length, 4);
253#else
254 ok_str(Buffer, "hello! ");
255 ok_int(Length, 8);
256#endif
258
260}
#define StartSeh()
Definition: _sntprintf.h:16
#define EndSeh(ExpectedStatus)
Definition: _sntprintf.h:17
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
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 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 sprintf(buf, format,...)
Definition: sprintf.c:55
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define STATUS_SUCCESS
Definition: shellext.h:65
char * PCHAR
Definition: typedefs.h:51
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
char CHAR
Definition: xmlstorage.h:175