Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / msvcrt / tests / misc.c
blob7135248fb479d4e832a8e6d3189fe6c811f36691
1 /*
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"
22 #include <errno.h>
23 #include "msvcrt.h"
25 static int (__cdecl *prand_s)(unsigned int *);
26 static int (__cdecl *pmemcpy_s)(void *, MSVCRT_size_t, void*, MSVCRT_size_t);
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 pmemcpy_s = (void*)GetProcAddress(hmod, "memcpy_s");
40 pI10_OUTPUT = (void*)GetProcAddress(hmod, "$I10_OUTPUT");
41 pstrerror_s = (void *)GetProcAddress(hmod, "strerror_s");
42 p_get_doserrno = (void *)GetProcAddress(hmod, "_get_doserrno");
43 p_get_errno = (void *)GetProcAddress(hmod, "_get_errno");
44 p_set_doserrno = (void *)GetProcAddress(hmod, "_set_doserrno");
45 p_set_errno = (void *)GetProcAddress(hmod, "_set_errno");
48 static void test_rand_s(void)
50 int ret;
51 unsigned int rand;
53 if (!prand_s)
55 win_skip("rand_s is not available\n");
56 return;
59 errno = EBADF;
60 ret = prand_s(NULL);
61 ok(ret == EINVAL, "Expected rand_s to return EINVAL, got %d\n", ret);
62 ok(errno == EINVAL, "Expected errno to return EINVAL, got %d\n", errno);
64 ret = prand_s(&rand);
65 ok(ret == 0, "Expected rand_s to return 0, got %d\n", ret);
68 static void test_memcpy_s(void)
70 static char data[] = "data\0to\0be\0copied";
71 static char dest[32];
72 int ret;
74 if(!pmemcpy_s)
76 win_skip("memcpy_s is not available\n");
77 return;
80 errno = 0xdeadbeef;
81 ret = pmemcpy_s(NULL, 0, NULL, 0);
82 ok(ret == 0, "ret = %x\n", ret);
83 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
85 errno = 0xdeadbeef;
86 dest[0] = 'x';
87 ret = pmemcpy_s(dest, 10, NULL, 0);
88 ok(ret == 0, "ret = %x\n", ret);
89 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
90 ok(dest[0] == 'x', "dest[0] != \'x\'\n");
92 errno = 0xdeadbeef;
93 ret = pmemcpy_s(NULL, 10, data, 10);
94 ok(ret == EINVAL, "ret = %x\n", ret);
95 ok(errno == EINVAL, "errno = %x\n", errno);
97 errno = 0xdeadbeef;
98 dest[7] = 'x';
99 ret = pmemcpy_s(dest, 10, data, 5);
100 ok(ret == 0, "ret = %x\n", ret);
101 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
102 ok(memcmp(dest, data, 10), "All data copied\n");
103 ok(!memcmp(dest, data, 5), "First five bytes are different\n");
105 errno = 0xdeadbeef;
106 ret = pmemcpy_s(data, 10, data, 10);
107 ok(ret == 0, "ret = %x\n", ret);
108 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
109 ok(!memcmp(dest, data, 5), "data was destroyed during overwriting\n");
111 errno = 0xdeadbeef;
112 dest[0] = 'x';
113 ret = pmemcpy_s(dest, 5, data, 10);
114 ok(ret == ERANGE, "ret = %x\n", ret);
115 ok(errno == ERANGE, "errno = %x\n", errno);
116 ok(dest[0] == '\0', "dest[0] != \'\\0\'\n");
119 typedef struct _I10_OUTPUT_data {
120 short pos;
121 char sign;
122 BYTE len;
123 char str[100];
124 } I10_OUTPUT_data;
126 typedef struct _I10_OUTPUT_test {
127 long double d;
128 int size;
129 int flags;
131 I10_OUTPUT_data out;
132 int ret;
133 const char *remain;
134 } I10_OUTPUT_test;
136 static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
137 /* arg3 = 0 */
138 { 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
139 { 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
140 { -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
141 { 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
142 { 1e13, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
143 { 1e30, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
144 { 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
145 { 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
146 { 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
147 /* arg3 = 1 */
148 { 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
149 { 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
150 { -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
151 { 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
152 { 1e13, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
153 { 1e30, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
154 { 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
155 { 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
156 { 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
157 { 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
158 /* too small buffer */
159 { 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
160 { 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
161 { 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
162 { 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
163 { 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
164 { 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
165 { 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
166 { 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
167 { 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
170 static void test_I10_OUTPUT(void)
172 I10_OUTPUT_data out;
173 int i, j = sizeof(long double), ret;
175 if(!pI10_OUTPUT) {
176 win_skip("I10_OUTPUT not available\n");
177 return;
179 if (j != 12)
180 trace("sizeof(long double) = %d on this machine\n", j);
182 for(i=0; i<sizeof(I10_OUTPUT_tests)/sizeof(I10_OUTPUT_test); i++) {
183 memset(out.str, '#', sizeof(out.str));
185 if (sizeof(long double) == 12)
186 ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
187 else {
188 /* MS' "long double" is an 80 bit FP that takes 12 bytes*/
189 typedef struct { ULONG x80[3]; } uld; /* same calling convention */
190 union { long double ld; uld ld12; } fp80;
191 int (__cdecl *pI10_OUTPUT12)(uld, int, int, void*) = (void*)pI10_OUTPUT;
192 fp80.ld = I10_OUTPUT_tests[i].d;
193 ret = pI10_OUTPUT12(fp80.ld12, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
195 ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
196 ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
197 ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
198 ok(out.len == I10_OUTPUT_tests[i].out.len, "%d: out.len = %d\n", i, (int)out.len);
199 ok(!strcmp(out.str, I10_OUTPUT_tests[i].out.str), "%d: out.str = %s\n", i, out.str);
201 j = strlen(I10_OUTPUT_tests[i].remain);
202 if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
203 todo_wine ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
204 "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
205 else
206 ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
207 "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
210 for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
211 if(out.str[j] != '#')
212 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i, j, out.str[j]);
216 static void test_strerror_s(void)
218 int ret;
219 char buf[256];
221 if (!pstrerror_s)
223 win_skip("strerror_s is not available\n");
224 return;
227 errno = EBADF;
228 ret = pstrerror_s(NULL, 0, 0);
229 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
230 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
232 errno = EBADF;
233 ret = pstrerror_s(NULL, sizeof(buf), 0);
234 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
235 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
237 memset(buf, 'X', sizeof(buf));
238 errno = EBADF;
239 ret = pstrerror_s(buf, 0, 0);
240 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
241 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
242 ok(buf[0] == 'X', "Expected output buffer to be untouched\n");
244 memset(buf, 'X', sizeof(buf));
245 ret = pstrerror_s(buf, 1, 0);
246 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
247 ok(strlen(buf) == 0, "Expected output buffer to be null terminated\n");
249 memset(buf, 'X', sizeof(buf));
250 ret = pstrerror_s(buf, 2, 0);
251 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
252 ok(strlen(buf) == 1, "Expected output buffer to be truncated\n");
254 memset(buf, 'X', sizeof(buf));
255 ret = pstrerror_s(buf, sizeof(buf), 0);
256 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
258 memset(buf, 'X', sizeof(buf));
259 ret = pstrerror_s(buf, sizeof(buf), -1);
260 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
263 static void test__get_doserrno(void)
265 int ret, out;
267 if (!p_get_doserrno)
269 win_skip("_get_doserrno is not available\n");
270 return;
273 _doserrno = ERROR_INVALID_CMM;
274 errno = EBADF;
275 ret = p_get_doserrno(NULL);
276 ok(ret == EINVAL, "Expected _get_doserrno to return EINVAL, got %d\n", ret);
277 ok(_doserrno == ERROR_INVALID_CMM, "Expected _doserrno to be ERROR_INVALID_CMM, got %d\n", _doserrno);
278 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
280 _doserrno = ERROR_INVALID_CMM;
281 errno = EBADF;
282 out = 0xdeadbeef;
283 ret = p_get_doserrno(&out);
284 ok(ret == 0, "Expected _get_doserrno to return 0, got %d\n", ret);
285 ok(out == ERROR_INVALID_CMM, "Expected output variable to be ERROR_INVAID_CMM, got %d\n", out);
288 static void test__get_errno(void)
290 int ret, out;
292 if (!p_get_errno)
294 win_skip("_get_errno is not available\n");
295 return;
298 errno = EBADF;
299 ret = p_get_errno(NULL);
300 ok(ret == EINVAL, "Expected _get_errno to return EINVAL, got %d\n", ret);
301 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
303 errno = EBADF;
304 out = 0xdeadbeef;
305 ret = p_get_errno(&out);
306 ok(ret == 0, "Expected _get_errno to return 0, got %d\n", ret);
307 ok(out == EBADF, "Expected output variable to be EBADF, got %d\n", out);
310 static void test__set_doserrno(void)
312 int ret;
314 if (!p_set_doserrno)
316 win_skip("_set_doserrno is not available\n");
317 return;
320 _doserrno = ERROR_INVALID_CMM;
321 ret = p_set_doserrno(ERROR_FILE_NOT_FOUND);
322 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
323 ok(_doserrno == ERROR_FILE_NOT_FOUND,
324 "Expected _doserrno to be ERROR_FILE_NOT_FOUND, got %d\n", _doserrno);
326 _doserrno = ERROR_INVALID_CMM;
327 ret = p_set_doserrno(-1);
328 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
329 ok(_doserrno == -1,
330 "Expected _doserrno to be -1, got %d\n", _doserrno);
332 _doserrno = ERROR_INVALID_CMM;
333 ret = p_set_doserrno(0xdeadbeef);
334 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
335 ok(_doserrno == 0xdeadbeef,
336 "Expected _doserrno to be 0xdeadbeef, got %d\n", _doserrno);
339 static void test__set_errno(void)
341 int ret;
343 if (!p_set_errno)
345 win_skip("_set_errno is not available\n");
346 return;
349 errno = EBADF;
350 ret = p_set_errno(EINVAL);
351 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
352 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
354 errno = EBADF;
355 ret = p_set_errno(-1);
356 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
357 ok(errno == -1, "Expected errno to be -1, got %d\n", errno);
359 errno = EBADF;
360 ret = p_set_errno(0xdeadbeef);
361 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
362 ok(errno == 0xdeadbeef, "Expected errno to be 0xdeadbeef, got %d\n", errno);
365 START_TEST(misc)
367 init();
369 test_rand_s();
370 test_memcpy_s();
371 test_I10_OUTPUT();
372 test_strerror_s();
373 test__get_doserrno();
374 test__get_errno();
375 test__set_doserrno();
376 test__set_errno();