ReactOS 0.4.16-dev-2491-g3dc6630
wsprintf.c
Go to the documentation of this file.
1 /* Unit test suite for the wsprintf functions
2 *
3 * Copyright 2002 Bill Medland
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20#include <stdarg.h>
21
22#include "wine/test.h"
23#include "windef.h"
24#include "winbase.h"
25#include "winuser.h"
26#include "winnls.h"
27
28static const struct
29{
30 const char *fmt;
32 const char *res;
33} i64_formats[] =
34{
35 { "%I64X", ((ULONGLONG)0x12345 << 32) | 0x67890a, "123450067890A" },
36 { "%I32X", ((ULONGLONG)0x12345 << 32) | 0x67890a, "67890A" },
37 { "%I64d", (ULONGLONG)543210 * 1000000, "543210000000" },
38 { "%I64X", (LONGLONG)-0x12345, "FFFFFFFFFFFEDCBB" },
39 { "%I32x", (LONGLONG)-0x12345, "fffedcbb" },
40 { "%I64u", (LONGLONG)-123, "18446744073709551493" },
41 { "%Id", (LONGLONG)-12345, "-12345" },
42#ifdef _WIN64
43 { "%Ix", ((ULONGLONG)0x12345 << 32) | 0x67890a, "123450067890a" },
44 { "%Ix", (LONGLONG)-0x12345, "fffffffffffedcbb" },
45 { "%p", (LONGLONG)-0x12345, "FFFFFFFFFFFEDCBB" },
46#else
47 { "%Ix", ((ULONGLONG)0x12345 << 32) | 0x67890a, "67890a" },
48 { "%Ix", (LONGLONG)-0x12345, "fffedcbb" },
49 { "%p", (LONGLONG)-0x12345, "FFFEDCBB" },
50#endif
51};
52
53static void wsprintfATest(void)
54{
55 char buf[25], star[4], partial[4];
56 static const WCHAR starW[] = L"\x2606";
57 static const WCHAR fffeW[] = L"\xfffe";
58 static const WCHAR wineW[] = L"\xd83c\xdf77"; /* U+1F377: wine glass */
59 const struct {
60 const void *input;
61 const char *fmt;
62 const char *str;
63 int rc;
64 }
65 testcase[] = {
66 { starW, "%.1S", partial, 1 },
67 { starW, "%.2S", star, 2 },
68 { starW, "%.3S", star, 2 },
69 { fffeW, "%.1S", "?", 1 },
70 { fffeW, "%.2S", "?", 1 },
71 { wineW, "%.2S", "??", 2 },
72 { star, "%.1s", partial, 1 },
73 { (void *)0x2606, "%2C", star, 2 },
74 { (void *)0xfffd, "%C", "?", 1 },
75 { (void *)0xe199, "%2c", " \x99", 2 },
76 };
77 CPINFO cpinfo;
78 unsigned int i;
79 int rc;
80
81 rc=wsprintfA(buf, "%010ld", -1);
82 ok(rc == 10, "wsprintfA length failure: rc=%d error=%ld\n",rc,GetLastError());
83 ok((lstrcmpA(buf, "-000000001") == 0),
84 "wsprintfA zero padded negative value failure: buf=[%s]\n",buf);
85 rc = wsprintfA(buf, "%I64X", (ULONGLONG)0);
86 if (rc == 4 && !lstrcmpA(buf, "I64X"))
87 {
88 win_skip( "I64 formats not supported\n" );
89 return;
90 }
91 for (i = 0; i < ARRAY_SIZE(i64_formats); i++)
92 {
94 ok(rc == strlen(i64_formats[i].res), "%u: wsprintfA length failure: rc=%d\n", i, rc);
95 ok(!strcmp(buf, i64_formats[i].res), "%u: wrong result [%s]\n", i, buf);
96 }
97
98 if (!GetCPInfo(CP_ACP, &cpinfo) || cpinfo.MaxCharSize != 2)
99 {
100 skip("Multi-byte wsprintfA test isn't available for the current codepage\n");
101 return;
102 }
103
104 rc = WideCharToMultiByte(CP_ACP, 0, starW, -1, star, sizeof(star), NULL, NULL);
105 ok(rc == 3, "unexpected rc, got %d\n", rc);
106 partial[0] = star[0];
107 partial[1] = '\0';
108
109 for (i = 0; i < ARRAY_SIZE(testcase); i++)
110 {
111 memset(buf, 0x11, sizeof(buf));
112 rc = wsprintfA(buf, testcase[i].fmt, testcase[i].input);
113
114 ok(rc == testcase[i].rc,
115 "%u: expected %d, got %d\n",
116 i, testcase[i].rc, rc);
117
118 ok(!strcmp(buf, testcase[i].str),
119 "%u: expected %s, got %s\n",
120 i, wine_dbgstr_a(testcase[i].str), wine_dbgstr_an(buf, rc));
121 }
122}
123
125{
126 WCHAR wc;
127 if (!IsDBCSLeadByte(c) &&
128 MultiByteToWideChar(CP_ACP, 0, (const char *)&c, 1, &wc, 1) > 0)
129 return wc;
130 return 0;
131}
132
133static void wsprintfWTest(void)
134{
135 static const WCHAR stars[] = L"*\x2606\x2605";
136 WCHAR def_spc[] = L"*?\x2605 ";
137 WCHAR buf[25], fmt[25], res[25], wcA1, wc99;
138 char stars_mb[8], partial00[8], partialFF[8];
139 const struct {
140 const char *input;
141 const WCHAR *fmt;
142 const WCHAR *str;
143 int rc;
144 }
145 testcase[] = {
146 { stars_mb, L"%.3S", stars, 3 },
147 { partial00, L"%-4S", L"*\0 ", 4 },
148 { partialFF, L"%-4S", def_spc, 4 },
149 };
150 CPINFOEXW cpinfoex;
151 unsigned int i;
152 int rc;
153
154 rc=wsprintfW(buf, L"%010ld", -1);
156 {
157 win_skip("wsprintfW is not implemented\n");
158 return;
159 }
160 ok(rc == 10, "wsPrintfW length failure: rc=%d error=%ld\n",rc,GetLastError());
161 ok((lstrcmpW(buf, L"-000000001") == 0),
162 "wsprintfW zero padded negative value failure\n");
163 rc = wsprintfW(buf, L"%I64x", (ULONGLONG)0 );
164 if (rc == 4 && !lstrcmpW(buf, L"I64x"))
165 {
166 win_skip( "I64 formats not supported\n" );
167 return;
168 }
169 for (i = 0; i < ARRAY_SIZE(i64_formats); i++)
170 {
174 ok(rc == lstrlenW(res), "%u: wsprintfW length failure: rc=%d\n", i, rc);
175 ok(!lstrcmpW(buf, res), "%u: wrong result [%s]\n", i, wine_dbgstr_w(buf));
176 }
177
178 rc = wsprintfW(buf, L"%2c", L'*');
179 ok(rc == 2, "expected 2, got %d\n", rc);
180 ok(buf[0] == L' ', "expected \\x0020, got \\x%04x\n", buf[0]);
181 ok(buf[1] == L'*', "expected \\x%04x, got \\x%04x\n", L'*', buf[1]);
182
183 rc = wsprintfW(buf, L"%c", L'\x2605');
184 ok(rc == 1, "expected 1, got %d\n", rc);
185 ok(buf[0] == L'\x2605', "expected \\x%04x, got \\x%04x\n", L'\x2605', buf[0]);
186
187 wcA1 = my_btowc(0xA1);
188 rc = wsprintfW(buf, L"%C", 0xA1);
189 ok(rc == 1, "expected 1, got %d\n", rc);
190 ok(buf[0] == wcA1, "expected \\x%04x, got \\x%04x\n", wcA1, buf[0]);
191
192 rc = wsprintfW(buf, L"%C", 0x81A1);
193 ok(rc == 1, "expected 1, got %d\n", rc);
194 ok(buf[0] == wcA1, "expected \\x%04x, got \\x%04x\n", wcA1, buf[0]);
195
196 wc99 = my_btowc(0x99);
197 rc = wsprintfW(buf, L"%2C", 0xe199);
198 ok(rc == 2, "expected 1, got %d\n", rc);
199 ok(buf[0] == L' ', "expected \\x0020, got \\x%04x\n", buf[0]);
200 ok(buf[1] == wc99, "expected \\x%04x, got \\x%04x\n", wc99, buf[1]);
201
202 if (!GetCPInfoExW(CP_ACP, 0, &cpinfoex) || cpinfoex.MaxCharSize != 2)
203 {
204 skip("Multi-byte wsprintfW test isn't available for the current codepage\n");
205 return;
206 }
207 def_spc[1] = cpinfoex.UnicodeDefaultChar;
208
209 rc = WideCharToMultiByte(CP_ACP, 0, stars, -1, stars_mb, sizeof(stars_mb), NULL, NULL);
210 ok(rc == 6, "expected 6, got %d\n", rc);
211 strcpy(partial00, stars_mb);
212 partial00[2] = '\0';
213 strcpy(partialFF, stars_mb);
214 partialFF[2] = 0xff;
215
216 for (i = 0; i < ARRAY_SIZE(testcase); i++)
217 {
218 memset(buf, 0x11, sizeof(buf));
219 rc = wsprintfW(buf, testcase[i].fmt, testcase[i].input);
220
221 ok(rc == testcase[i].rc,
222 "%u: expected %d, got %d\n",
223 i, testcase[i].rc, rc);
224
225 ok(!memcmp(buf, testcase[i].str, (testcase[i].rc + 1) * sizeof(WCHAR)),
226 "%u: expected %s, got %s\n", i,
227 wine_dbgstr_wn(testcase[i].str, testcase[i].rc + 1),
228 wine_dbgstr_wn(buf, rc + 1));
229 }
230}
231
232/* Test if the CharUpper / CharLower functions return true 16 bit results,
233 if the input is a 16 bit input value. */
234
235static void CharUpperTest(void)
236{
237 INT_PTR i, out;
238 BOOL failed = FALSE;
239
240 for (i=0;i<256;i++)
241 {
243 if ((out >> 16) != 0)
244 {
245 failed = TRUE;
246 break;
247 }
248 }
249 ok(!failed,"CharUpper failed - 16bit input (0x%0Ix) returned 32bit result (0x%0Ix)\n",i,out);
250}
251
252static void CharLowerTest(void)
253{
254 INT_PTR i, out;
255 BOOL failed = FALSE;
256
257 for (i=0;i<256;i++)
258 {
260 if ((out >> 16) != 0)
261 {
262 failed = TRUE;
263 break;
264 }
265 }
266 ok(!failed,"CharLower failed - 16bit input (0x%0Ix) returned 32bit result (0x%0Ix)\n",i,out);
267}
268
269
271{
276}
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
#define ARRAY_SIZE(A)
Definition: main.h:20
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
const char * wine_dbgstr_an(const char *str, int n)
Definition: compat.c:313
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
#define CP_ACP
Definition: compat.h:109
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
#define lstrlenW
Definition: compat.h:750
BOOL WINAPI IsDBCSLeadByte(BYTE testchar)
Definition: locale.c:2126
int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
Definition: locale.c:4152
BOOL WINAPI GetCPInfoExW(UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo)
Definition: locale.c:2222
BOOL WINAPI GetCPInfo(UINT codepage, LPCPINFO cpinfo)
Definition: locale.c:2146
int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
Definition: locale.c:4104
LPSTR WINAPI CharLowerA(char *str)
Definition: string.c:1043
LPSTR WINAPI CharUpperA(LPSTR str)
Definition: string.c:1156
_ACRTIMP int __cdecl memcmp(const void *, const void *, size_t)
Definition: string.c:2802
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1592
_ACRTIMP int __cdecl strcmp(const char *, const char *)
Definition: string.c:3319
#define L(x)
Definition: resources.c:13
unsigned int BOOL
Definition: ntddk_ex.h:94
GLuint res
Definition: glext.h:9613
const GLubyte * c
Definition: glext.h:8905
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLenum GLenum input
Definition: glext.h:9031
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define wine_dbgstr_w
Definition: kernel32.h:34
static WCHAR wineW[]
Definition: localmon.c:128
#define win_skip
Definition: minitest.h:67
STAR * stars
Definition: screensaver.c:45
static void wsprintfATest(void)
Definition: wsprintf.c:53
static WCHAR my_btowc(BYTE c)
Definition: wsprintf.c:124
static void CharLowerTest(void)
Definition: wsprintf.c:252
const char * fmt
Definition: wsprintf.c:30
ULONGLONG value
Definition: wsprintf.c:31
static const struct @1869 i64_formats[]
static void CharUpperTest(void)
Definition: wsprintf.c:235
static void wsprintfWTest(void)
Definition: wsprintf.c:133
#define wine_dbgstr_wn
Definition: testlist.c:2
const WCHAR * str
strcpy
Definition: string.h:131
const char int int int static __inline const char * wine_dbgstr_a(const char *s)
Definition: debug.h:187
#define memset(x, y, z)
Definition: compat.h:39
UINT MaxCharSize
Definition: winnls.h:650
WCHAR UnicodeDefaultChar
Definition: winnls.h:666
UINT MaxCharSize
Definition: winnls.h:663
Definition: dsound.c:943
static const WCHAR starW[]
Definition: symbol.c:43
int32_t INT_PTR
Definition: typedefs.h:64
int64_t LONGLONG
Definition: typedefs.h:68
uint64_t ULONGLONG
Definition: typedefs.h:67
Definition: pdh_main.c:96
wchar_t tm const _CrtWcstime_Writes_and_advances_ptr_ count wchar_t ** out
Definition: wcsftime.cpp:383
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
int WINAPIV wsprintfW(_Out_ LPWSTR, _In_ _Printf_format_string_ LPCWSTR,...)
int WINAPIV wsprintfA(_Out_ LPSTR, _In_ _Printf_format_string_ LPCSTR,...)
#define wsprintf
Definition: winuser.h:6031
__wchar_t WCHAR
Definition: xmlstorage.h:180
char * LPSTR
Definition: xmlstorage.h:182
unsigned char BYTE
Definition: xxhash.c:193