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 "precomp.h"
21 
22 static const struct
23 {
24     const char *fmt;
25     ULONGLONG value;
26     const char *res;
27 } i64_formats[] =
28 {
29     { "%I64X", ((ULONGLONG)0x12345 << 32) | 0x67890a, "123450067890A" },
30     { "%I32X", ((ULONGLONG)0x12345 << 32) | 0x67890a, "67890A" },
31     { "%I64d", (ULONGLONG)543210 * 1000000, "543210000000" },
32     { "%I64X", (LONGLONG)-0x12345, "FFFFFFFFFFFEDCBB" },
33     { "%I32x", (LONGLONG)-0x12345, "fffedcbb" },
34     { "%I64u", (LONGLONG)-123, "18446744073709551493" },
35     { "%Id",   (LONGLONG)-12345, "-12345" },
36 #ifdef _WIN64
37     { "%Ix",   ((ULONGLONG)0x12345 << 32) | 0x67890a, "123450067890a" },
38     { "%Ix",   (LONGLONG)-0x12345, "fffffffffffedcbb" },
39     { "%p",    (LONGLONG)-0x12345, "FFFFFFFFFFFEDCBB" },
40 #else
41     { "%Ix",   ((ULONGLONG)0x12345 << 32) | 0x67890a, "67890a" },
42     { "%Ix",   (LONGLONG)-0x12345, "fffedcbb" },
43     { "%p",    (LONGLONG)-0x12345, "FFFEDCBB" },
44 #endif
45 };
46 
47 static void wsprintfATest(void)
48 {
49     char buf[25];
50     unsigned int i;
51     int rc;
52 
53     rc=wsprintfA(buf, "%010ld", -1);
54     ok(rc == 10, "wsprintfA length failure: rc=%d error=%d\n",rc,GetLastError());
55     ok((lstrcmpA(buf, "-000000001") == 0),
56        "wsprintfA zero padded negative value failure: buf=[%s]\n",buf);
57     rc = wsprintfA(buf, "%I64X", (ULONGLONG)0);
58     if (rc == 4 && !lstrcmpA(buf, "I64X"))
59     {
60         win_skip( "I64 formats not supported\n" );
61         return;
62     }
63     for (i = 0; i < sizeof(i64_formats)/sizeof(i64_formats[0]); i++)
64     {
65         rc = wsprintfA(buf, i64_formats[i].fmt, i64_formats[i].value);
66         ok(rc == strlen(i64_formats[i].res), "%u: wsprintfA length failure: rc=%d\n", i, rc);
67         ok(!strcmp(buf, i64_formats[i].res), "%u: wrong result [%s]\n", i, buf);
68     }
69 }
70 
71 static void wsprintfWTest(void)
72 {
73     static const WCHAR fmt_010ld[] = {'%','0','1','0','l','d','\0'};
74     static const WCHAR res_010ld[] = {'-','0','0','0','0','0','0','0','0','1', '\0'};
75     static const WCHAR fmt_I64x[] = {'%','I','6','4','x',0};
76     WCHAR buf[25], fmt[25], res[25];
77     unsigned int i;
78     int rc;
79 
80     rc=wsprintfW(buf, fmt_010ld, -1);
81     if (rc==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
82     {
83         win_skip("wsprintfW is not implemented\n");
84         return;
85     }
86     ok(rc == 10, "wsPrintfW length failure: rc=%d error=%d\n",rc,GetLastError());
87     ok((lstrcmpW(buf, res_010ld) == 0),
88        "wsprintfW zero padded negative value failure\n");
89     rc = wsprintfW(buf, fmt_I64x, (ULONGLONG)0 );
90     if (rc == 4 && !lstrcmpW(buf, fmt_I64x + 1))
91     {
92         win_skip( "I64 formats not supported\n" );
93         return;
94     }
95     for (i = 0; i < sizeof(i64_formats)/sizeof(i64_formats[0]); i++)
96     {
97         MultiByteToWideChar( CP_ACP, 0, i64_formats[i].fmt, -1, fmt, sizeof(fmt)/sizeof(WCHAR) );
98         MultiByteToWideChar( CP_ACP, 0, i64_formats[i].res, -1, res, sizeof(res)/sizeof(WCHAR) );
99         rc = wsprintfW(buf, fmt, i64_formats[i].value);
100         ok(rc == lstrlenW(res), "%u: wsprintfW length failure: rc=%d\n", i, rc);
101         ok(!lstrcmpW(buf, res), "%u: wrong result [%s]\n", i, wine_dbgstr_w(buf));
102     }
103 }
104 
105 /* Test if the CharUpper / CharLower functions return true 16 bit results,
106    if the input is a 16 bit input value. */
107 
108 static void CharUpperTest(void)
109 {
110     INT_PTR i, out;
111     BOOL failed = FALSE;
112 
113     for (i=0;i<256;i++)
114     	{
115 	out = (INT_PTR)CharUpperA((LPSTR)i);
116 	if ((out >> 16) != 0)
117 	   {
118            failed = TRUE;
119 	   break;
120 	   }
121 	}
122     ok(!failed,"CharUpper failed - 16bit input (0x%0lx) returned 32bit result (0x%0lx)\n",i,out);
123 }
124 
125 static void CharLowerTest(void)
126 {
127     INT_PTR i, out;
128     BOOL failed = FALSE;
129 
130     for (i=0;i<256;i++)
131     	{
132 	out = (INT_PTR)CharLowerA((LPSTR)i);
133 	if ((out >> 16) != 0)
134 	   {
135            failed = TRUE;
136 	   break;
137 	   }
138 	}
139     ok(!failed,"CharLower failed - 16bit input (0x%0lx) returned 32bit result (0x%0lx)\n",i,out);
140 }
141 
142 
143 START_TEST(wsprintf)
144 {
145     wsprintfATest();
146     wsprintfWTest();
147     CharUpperTest();
148     CharLowerTest();
149 }
150