2 * Unit tests for miscellaneous msvcrt functions
4 * Copyright 2010 Andrew Nguyen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
26 static int (__cdecl
*prand_s
)(unsigned int *);
27 static int (__cdecl
*pI10_OUTPUT
)(long double, int, int, void*);
28 static int (__cdecl
*pstrerror_s
)(char *, MSVCRT_size_t
, int);
29 static int (__cdecl
*p_get_doserrno
)(int *);
30 static int (__cdecl
*p_get_errno
)(int *);
31 static int (__cdecl
*p_set_doserrno
)(int);
32 static int (__cdecl
*p_set_errno
)(int);
34 static void init(void)
36 HMODULE hmod
= GetModuleHandleA("msvcrt.dll");
38 prand_s
= (void *)GetProcAddress(hmod
, "rand_s");
39 pI10_OUTPUT
= (void*)GetProcAddress(hmod
, "$I10_OUTPUT");
40 pstrerror_s
= (void *)GetProcAddress(hmod
, "strerror_s");
41 p_get_doserrno
= (void *)GetProcAddress(hmod
, "_get_doserrno");
42 p_get_errno
= (void *)GetProcAddress(hmod
, "_get_errno");
43 p_set_doserrno
= (void *)GetProcAddress(hmod
, "_set_doserrno");
44 p_set_errno
= (void *)GetProcAddress(hmod
, "_set_errno");
47 static void test_rand_s(void)
54 win_skip("rand_s is not available\n");
60 ok(ret
== EINVAL
, "Expected rand_s to return EINVAL, got %d\n", ret
);
61 ok(errno
== EINVAL
, "Expected errno to return EINVAL, got %d\n", errno
);
64 ok(ret
== 0, "Expected rand_s to return 0, got %d\n", ret
);
67 typedef struct _I10_OUTPUT_data
{
74 typedef struct _I10_OUTPUT_test
{
84 static const I10_OUTPUT_test I10_OUTPUT_tests
[] = {
86 { 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
87 { 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
88 { -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
89 { 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
90 { 1e13
, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
91 { 1e30
, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
92 { 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
93 { 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
94 { 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
96 { 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
97 { 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
98 { -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
99 { 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
100 { 1e13
, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
101 { 1e30
, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
102 { 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
103 { 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
104 { 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
105 { 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
106 /* too small buffer */
107 { 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
108 { 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
109 { 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
110 { 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
111 { 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
112 { 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
113 { 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
114 { 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
115 { 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
118 static void test_I10_OUTPUT(void)
121 int i
, j
= sizeof(long double), ret
;
124 win_skip("I10_OUTPUT not available\n");
128 trace("sizeof(long double) = %d on this machine\n", j
);
130 for(i
=0; i
<sizeof(I10_OUTPUT_tests
)/sizeof(I10_OUTPUT_test
); i
++) {
131 memset(out
.str
, '#', sizeof(out
.str
));
133 if (sizeof(long double) == 12)
134 ret
= pI10_OUTPUT(I10_OUTPUT_tests
[i
].d
, I10_OUTPUT_tests
[i
].size
, I10_OUTPUT_tests
[i
].flags
, &out
);
136 /* MS' "long double" is an 80 bit FP that takes 12 bytes*/
137 typedef struct { ULONG x80
[3]; } uld
; /* same calling convention */
138 union { long double ld
; uld ld12
; } fp80
;
139 int (__cdecl
*pI10_OUTPUT12
)(uld
, int, int, void*) = (void*)pI10_OUTPUT
;
140 fp80
.ld
= I10_OUTPUT_tests
[i
].d
;
141 ret
= pI10_OUTPUT12(fp80
.ld12
, I10_OUTPUT_tests
[i
].size
, I10_OUTPUT_tests
[i
].flags
, &out
);
143 ok(ret
== I10_OUTPUT_tests
[i
].ret
, "%d: ret = %d\n", i
, ret
);
144 ok(out
.pos
== I10_OUTPUT_tests
[i
].out
.pos
, "%d: out.pos = %hd\n", i
, out
.pos
);
145 ok(out
.sign
== I10_OUTPUT_tests
[i
].out
.sign
, "%d: out.size = %c\n", i
, out
.sign
);
146 ok(out
.len
== I10_OUTPUT_tests
[i
].out
.len
, "%d: out.len = %d\n", i
, (int)out
.len
);
147 ok(!strcmp(out
.str
, I10_OUTPUT_tests
[i
].out
.str
), "%d: out.str = %s\n", i
, out
.str
);
149 j
= strlen(I10_OUTPUT_tests
[i
].remain
);
150 if(j
&& I10_OUTPUT_tests
[i
].remain
[j
-1]=='9')
151 todo_wine
ok(!strncmp(out
.str
+out
.len
+1, I10_OUTPUT_tests
[i
].remain
, j
),
152 "%d: &out.str[%d] = %.25s...\n", i
, out
.len
+1, out
.str
+out
.len
+1);
154 ok(!strncmp(out
.str
+out
.len
+1, I10_OUTPUT_tests
[i
].remain
, j
),
155 "%d: &out.str[%d] = %.25s...\n", i
, out
.len
+1, out
.str
+out
.len
+1);
158 for(j
=out
.len
+strlen(I10_OUTPUT_tests
[i
].remain
)+1; j
<sizeof(out
.str
); j
++)
159 if(out
.str
[j
] != '#')
160 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i
, j
, out
.str
[j
]);
164 static void test_strerror_s(void)
171 win_skip("strerror_s is not available\n");
176 ret
= pstrerror_s(NULL
, 0, 0);
177 ok(ret
== EINVAL
, "Expected strerror_s to return EINVAL, got %d\n", ret
);
178 ok(errno
== EINVAL
, "Expected errno to be EINVAL, got %d\n", errno
);
181 ret
= pstrerror_s(NULL
, sizeof(buf
), 0);
182 ok(ret
== EINVAL
, "Expected strerror_s to return EINVAL, got %d\n", ret
);
183 ok(errno
== EINVAL
, "Expected errno to be EINVAL, got %d\n", errno
);
185 memset(buf
, 'X', sizeof(buf
));
187 ret
= pstrerror_s(buf
, 0, 0);
188 ok(ret
== EINVAL
, "Expected strerror_s to return EINVAL, got %d\n", ret
);
189 ok(errno
== EINVAL
, "Expected errno to be EINVAL, got %d\n", errno
);
190 ok(buf
[0] == 'X', "Expected output buffer to be untouched\n");
192 memset(buf
, 'X', sizeof(buf
));
193 ret
= pstrerror_s(buf
, 1, 0);
194 ok(ret
== 0, "Expected strerror_s to return 0, got %d\n", ret
);
195 ok(strlen(buf
) == 0, "Expected output buffer to be null terminated\n");
197 memset(buf
, 'X', sizeof(buf
));
198 ret
= pstrerror_s(buf
, 2, 0);
199 ok(ret
== 0, "Expected strerror_s to return 0, got %d\n", ret
);
200 ok(strlen(buf
) == 1, "Expected output buffer to be truncated\n");
202 memset(buf
, 'X', sizeof(buf
));
203 ret
= pstrerror_s(buf
, sizeof(buf
), 0);
204 ok(ret
== 0, "Expected strerror_s to return 0, got %d\n", ret
);
206 memset(buf
, 'X', sizeof(buf
));
207 ret
= pstrerror_s(buf
, sizeof(buf
), -1);
208 ok(ret
== 0, "Expected strerror_s to return 0, got %d\n", ret
);
211 static void test__get_doserrno(void)
217 win_skip("_get_doserrno is not available\n");
221 _doserrno
= ERROR_INVALID_CMM
;
223 ret
= p_get_doserrno(NULL
);
224 ok(ret
== EINVAL
, "Expected _get_doserrno to return EINVAL, got %d\n", ret
);
225 ok(_doserrno
== ERROR_INVALID_CMM
, "Expected _doserrno to be ERROR_INVALID_CMM, got %d\n", _doserrno
);
226 ok(errno
== EBADF
, "Expected errno to be EBADF, got %d\n", errno
);
228 _doserrno
= ERROR_INVALID_CMM
;
231 ret
= p_get_doserrno(&out
);
232 ok(ret
== 0, "Expected _get_doserrno to return 0, got %d\n", ret
);
233 ok(out
== ERROR_INVALID_CMM
, "Expected output variable to be ERROR_INVAID_CMM, got %d\n", out
);
236 static void test__get_errno(void)
242 win_skip("_get_errno is not available\n");
247 ret
= p_get_errno(NULL
);
248 ok(ret
== EINVAL
, "Expected _get_errno to return EINVAL, got %d\n", ret
);
249 ok(errno
== EBADF
, "Expected errno to be EBADF, got %d\n", errno
);
253 ret
= p_get_errno(&out
);
254 ok(ret
== 0, "Expected _get_errno to return 0, got %d\n", ret
);
255 ok(out
== EBADF
, "Expected output variable to be EBADF, got %d\n", out
);
258 static void test__set_doserrno(void)
264 win_skip("_set_doserrno is not available\n");
268 _doserrno
= ERROR_INVALID_CMM
;
269 ret
= p_set_doserrno(ERROR_FILE_NOT_FOUND
);
270 ok(ret
== 0, "Expected _set_doserrno to return 0, got %d\n", ret
);
271 ok(_doserrno
== ERROR_FILE_NOT_FOUND
,
272 "Expected _doserrno to be ERROR_FILE_NOT_FOUND, got %d\n", _doserrno
);
274 _doserrno
= ERROR_INVALID_CMM
;
275 ret
= p_set_doserrno(-1);
276 ok(ret
== 0, "Expected _set_doserrno to return 0, got %d\n", ret
);
278 "Expected _doserrno to be -1, got %d\n", _doserrno
);
280 _doserrno
= ERROR_INVALID_CMM
;
281 ret
= p_set_doserrno(0xdeadbeef);
282 ok(ret
== 0, "Expected _set_doserrno to return 0, got %d\n", ret
);
283 ok(_doserrno
== 0xdeadbeef,
284 "Expected _doserrno to be 0xdeadbeef, got %d\n", _doserrno
);
287 static void test__set_errno(void)
293 win_skip("_set_errno is not available\n");
298 ret
= p_set_errno(EINVAL
);
299 ok(ret
== 0, "Expected _set_errno to return 0, got %d\n", ret
);
300 ok(errno
== EINVAL
, "Expected errno to be EINVAL, got %d\n", errno
);
303 ret
= p_set_errno(-1);
304 ok(ret
== 0, "Expected _set_errno to return 0, got %d\n", ret
);
305 ok(errno
== -1, "Expected errno to be -1, got %d\n", errno
);
308 ret
= p_set_errno(0xdeadbeef);
309 ok(ret
== 0, "Expected _set_errno to return 0, got %d\n", ret
);
310 ok(errno
== 0xdeadbeef, "Expected errno to be 0xdeadbeef, got %d\n", errno
);
313 static void test__popen_child(void)
315 /* don't execute any tests here */
316 /* ExitProcess is used to set return code of _pclose */
317 printf("child output\n");
321 static void test__popen(const char *name
)
327 sprintf(buf
, "%s misc popen", name
);
328 pipe
= _popen(buf
, "r");
329 ok(pipe
!= NULL
, "_popen failed with error: %d\n", errno
);
331 fgets(buf
, sizeof(buf
), pipe
);
332 ok(!strcmp(buf
, "child output\n"), "buf = %s\n", buf
);
335 ok(ret
== 0x37, "_pclose returned %x, expected 0x37\n", ret
);
338 ret
= _pclose((FILE*)0xdeadbeef);
339 ok(ret
== -1, "_pclose returned %x, expected -1\n", ret
);
341 ok(errno
== EBADF
, "errno = %d\n", errno
);
351 arg_c
= winetest_get_mainargs(&arg_v
);
353 if(!strcmp(arg_v
[2], "popen"))
356 ok(0, "invalid argument '%s'\n", arg_v
[2]);
364 test__get_doserrno();
366 test__set_doserrno();
368 test__popen(arg_v
[0]);