a4c5c8c3665b3c9bfc05b66878c7962d12cb624d
[reactos.git] / rostests / winetests / msvcrt / misc.c
1 /*
2 * Unit tests for miscellaneous msvcrt functions
3 *
4 * Copyright 2010 Andrew Nguyen
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "wine/test.h"
22 #include <errno.h>
23 #include <stdio.h>
24 #include "msvcrt.h"
25
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);
33 static void (__cdecl *p__invalid_parameter)(const wchar_t*,
34 const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
35 static void (__cdecl *p_qsort_s)(void*, MSVCRT_size_t, MSVCRT_size_t,
36 int (__cdecl*)(void*, const void*, const void*), void*);
37
38 static void init(void)
39 {
40 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
41
42 prand_s = (void *)GetProcAddress(hmod, "rand_s");
43 pI10_OUTPUT = (void*)GetProcAddress(hmod, "$I10_OUTPUT");
44 pstrerror_s = (void *)GetProcAddress(hmod, "strerror_s");
45 p_get_doserrno = (void *)GetProcAddress(hmod, "_get_doserrno");
46 p_get_errno = (void *)GetProcAddress(hmod, "_get_errno");
47 p_set_doserrno = (void *)GetProcAddress(hmod, "_set_doserrno");
48 p_set_errno = (void *)GetProcAddress(hmod, "_set_errno");
49 p__invalid_parameter = (void *)GetProcAddress(hmod, "_invalid_parameter");
50 p_qsort_s = (void *)GetProcAddress(hmod, "qsort_s");
51 }
52
53 static void test_rand_s(void)
54 {
55 int ret;
56 unsigned int rand;
57
58 if (!prand_s)
59 {
60 win_skip("rand_s is not available\n");
61 return;
62 }
63
64 errno = EBADF;
65 ret = prand_s(NULL);
66 ok(ret == EINVAL, "Expected rand_s to return EINVAL, got %d\n", ret);
67 ok(errno == EINVAL, "Expected errno to return EINVAL, got %d\n", errno);
68
69 ret = prand_s(&rand);
70 ok(ret == 0, "Expected rand_s to return 0, got %d\n", ret);
71 }
72
73 typedef struct _I10_OUTPUT_data {
74 short pos;
75 char sign;
76 BYTE len;
77 char str[100];
78 } I10_OUTPUT_data;
79
80 typedef struct _I10_OUTPUT_test {
81 long double d;
82 int size;
83 int flags;
84
85 I10_OUTPUT_data out;
86 int ret;
87 const char *remain;
88 } I10_OUTPUT_test;
89
90 static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
91 /* arg3 = 0 */
92 { 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
93 { 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
94 { -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
95 { 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
96 { 1e13, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
97 { 1e30, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
98 { 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
99 { 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
100 { 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
101 /* arg3 = 1 */
102 { 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
103 { 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
104 { -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
105 { 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
106 { 1e13, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
107 { 1e30, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
108 { 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
109 { 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
110 { 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
111 { 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
112 /* too small buffer */
113 { 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
114 { 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
115 { 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
116 { 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
117 { 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
118 { 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
119 { 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
120 { 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
121 { 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
122 };
123
124 static void test_I10_OUTPUT(void)
125 {
126 I10_OUTPUT_data out;
127 int i, j = sizeof(long double), ret;
128
129 if(!pI10_OUTPUT) {
130 win_skip("I10_OUTPUT not available\n");
131 return;
132 }
133 if (j != 12)
134 trace("sizeof(long double) = %d on this machine\n", j);
135
136 for(i=0; i<sizeof(I10_OUTPUT_tests)/sizeof(I10_OUTPUT_test); i++) {
137 memset(out.str, '#', sizeof(out.str));
138
139 if (sizeof(long double) == 12)
140 ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
141 else {
142 /* MS' "long double" is an 80 bit FP that takes 12 bytes*/
143 typedef struct { ULONG x80[3]; } uld; /* same calling convention */
144 union { long double ld; uld ld12; } fp80;
145 int (__cdecl *pI10_OUTPUT12)(uld, int, int, void*) = (void*)pI10_OUTPUT;
146 fp80.ld = I10_OUTPUT_tests[i].d;
147 ret = pI10_OUTPUT12(fp80.ld12, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
148 }
149 ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
150 ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
151 ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
152 ok(out.len == I10_OUTPUT_tests[i].out.len, "%d: out.len = %d\n", i, (int)out.len);
153 ok(!strcmp(out.str, I10_OUTPUT_tests[i].out.str), "%d: out.str = %s\n", i, out.str);
154
155 j = strlen(I10_OUTPUT_tests[i].remain);
156 if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
157 todo_wine ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
158 "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
159 else
160 ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
161 "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
162
163
164 for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
165 if(out.str[j] != '#')
166 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i, j, out.str[j]);
167 }
168 }
169
170 static void test_strerror_s(void)
171 {
172 int ret;
173 char buf[256];
174
175 if (!pstrerror_s)
176 {
177 win_skip("strerror_s is not available\n");
178 return;
179 }
180
181 errno = EBADF;
182 ret = pstrerror_s(NULL, 0, 0);
183 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
184 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
185
186 errno = EBADF;
187 ret = pstrerror_s(NULL, sizeof(buf), 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
191 memset(buf, 'X', sizeof(buf));
192 errno = EBADF;
193 ret = pstrerror_s(buf, 0, 0);
194 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
195 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
196 ok(buf[0] == 'X', "Expected output buffer to be untouched\n");
197
198 memset(buf, 'X', sizeof(buf));
199 ret = pstrerror_s(buf, 1, 0);
200 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
201 ok(strlen(buf) == 0, "Expected output buffer to be null terminated\n");
202
203 memset(buf, 'X', sizeof(buf));
204 ret = pstrerror_s(buf, 2, 0);
205 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
206 ok(strlen(buf) == 1, "Expected output buffer to be truncated\n");
207
208 memset(buf, 'X', sizeof(buf));
209 ret = pstrerror_s(buf, sizeof(buf), 0);
210 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
211
212 memset(buf, 'X', sizeof(buf));
213 ret = pstrerror_s(buf, sizeof(buf), -1);
214 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
215 }
216
217 static void test__get_doserrno(void)
218 {
219 int ret, out;
220
221 if (!p_get_doserrno)
222 {
223 win_skip("_get_doserrno is not available\n");
224 return;
225 }
226
227 _doserrno = ERROR_INVALID_CMM;
228 errno = EBADF;
229 ret = p_get_doserrno(NULL);
230 ok(ret == EINVAL, "Expected _get_doserrno to return EINVAL, got %d\n", ret);
231 ok(_doserrno == ERROR_INVALID_CMM, "Expected _doserrno to be ERROR_INVALID_CMM, got %d\n", _doserrno);
232 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
233
234 _doserrno = ERROR_INVALID_CMM;
235 errno = EBADF;
236 out = 0xdeadbeef;
237 ret = p_get_doserrno(&out);
238 ok(ret == 0, "Expected _get_doserrno to return 0, got %d\n", ret);
239 ok(out == ERROR_INVALID_CMM, "Expected output variable to be ERROR_INVAID_CMM, got %d\n", out);
240 }
241
242 static void test__get_errno(void)
243 {
244 int ret, out;
245
246 if (!p_get_errno)
247 {
248 win_skip("_get_errno is not available\n");
249 return;
250 }
251
252 errno = EBADF;
253 ret = p_get_errno(NULL);
254 ok(ret == EINVAL, "Expected _get_errno to return EINVAL, got %d\n", ret);
255 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
256
257 errno = EBADF;
258 out = 0xdeadbeef;
259 ret = p_get_errno(&out);
260 ok(ret == 0, "Expected _get_errno to return 0, got %d\n", ret);
261 ok(out == EBADF, "Expected output variable to be EBADF, got %d\n", out);
262 }
263
264 static void test__set_doserrno(void)
265 {
266 int ret;
267
268 if (!p_set_doserrno)
269 {
270 win_skip("_set_doserrno is not available\n");
271 return;
272 }
273
274 _doserrno = ERROR_INVALID_CMM;
275 ret = p_set_doserrno(ERROR_FILE_NOT_FOUND);
276 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
277 ok(_doserrno == ERROR_FILE_NOT_FOUND,
278 "Expected _doserrno to be ERROR_FILE_NOT_FOUND, got %d\n", _doserrno);
279
280 _doserrno = ERROR_INVALID_CMM;
281 ret = p_set_doserrno(-1);
282 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
283 ok(_doserrno == -1,
284 "Expected _doserrno to be -1, got %d\n", _doserrno);
285
286 _doserrno = ERROR_INVALID_CMM;
287 ret = p_set_doserrno(0xdeadbeef);
288 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
289 ok(_doserrno == 0xdeadbeef,
290 "Expected _doserrno to be 0xdeadbeef, got %d\n", _doserrno);
291 }
292
293 static void test__set_errno(void)
294 {
295 int ret;
296
297 if (!p_set_errno)
298 {
299 win_skip("_set_errno is not available\n");
300 return;
301 }
302
303 errno = EBADF;
304 ret = p_set_errno(EINVAL);
305 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
306 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
307
308 errno = EBADF;
309 ret = p_set_errno(-1);
310 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
311 ok(errno == -1, "Expected errno to be -1, got %d\n", errno);
312
313 errno = EBADF;
314 ret = p_set_errno(0xdeadbeef);
315 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
316 ok(errno == 0xdeadbeef, "Expected errno to be 0xdeadbeef, got %d\n", errno);
317 }
318
319 static void test__popen_child(void)
320 {
321 /* don't execute any tests here */
322 /* ExitProcess is used to set return code of _pclose */
323 printf("child output\n");
324 ExitProcess(0x37);
325 }
326
327 static void test__popen(const char *name)
328 {
329 FILE *pipe;
330 char buf[1024];
331 int ret;
332
333 sprintf(buf, "\"%s\" misc popen", name);
334 pipe = _popen(buf, "r");
335 ok(pipe != NULL, "_popen failed with error: %d\n", errno);
336
337 fgets(buf, sizeof(buf), pipe);
338 ok(!strcmp(buf, "child output\n"), "buf = %s\n", buf);
339
340 ret = _pclose(pipe);
341 ok(ret == 0x37, "_pclose returned %x, expected 0x37\n", ret);
342
343 errno = 0xdeadbeef;
344 ret = _pclose((FILE*)0xdeadbeef);
345 ok(ret == -1, "_pclose returned %x, expected -1\n", ret);
346 if(p_set_errno)
347 ok(errno == EBADF, "errno = %d\n", errno);
348 }
349
350 static void test__invalid_parameter(void)
351 {
352 if(!p__invalid_parameter) {
353 win_skip("_invalid_parameter not available\n");
354 return;
355 }
356
357 p__invalid_parameter(NULL, NULL, NULL, 0, 0);
358 }
359
360 struct qsort_test
361 {
362 int pos;
363 int *base;
364
365 struct {
366 int l;
367 int r;
368 } cmp[64];
369 };
370
371 static int __cdecl qsort_comp(void *ctx, const void *l, const void *r)
372 {
373 struct qsort_test *qt = ctx;
374
375 if(qt) {
376 ok(qt->pos < 64, "qt->pos = %d\n", qt->pos);
377 ok(qt->cmp[qt->pos].l == (int*)l-qt->base,
378 "%d) l on %ld position\n", qt->pos, (long)((int*)l - qt->base));
379 ok(qt->cmp[qt->pos].r == (int*)r-qt->base,
380 "%d) r on %ld position\n", qt->pos, (long)((int*)r - qt->base));
381 qt->pos++;
382 }
383
384 return *(int*)l%1000 - *(int*)r%1000;
385 }
386
387 static void test_qsort_s(void)
388 {
389 static const int nonstable_test[] = {9000, 8001, 7002, 6003, 1003, 5004, 4005, 3006, 2007};
390 int tab[100], i;
391
392 struct qsort_test small_sort = {
393 0, tab, {
394 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 5}, {7, 6},
395 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 5},
396 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4},
397 {1, 0}, {2, 1}, {3, 2}, {4, 3},
398 {1, 0}, {2, 1}, {3, 2},
399 {1, 0}, {2, 1},
400 {1, 0}
401 }
402 }, small_sort2 = {
403 0, tab, {
404 {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0},
405 {1, 0}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1},
406 {1, 0}, {2, 1}, {3, 2}, {4, 2}, {5, 2},
407 {1, 0}, {2, 1}, {3, 2}, {4, 3},
408 {1, 0}, {2, 1}, {3, 2},
409 {1, 0}, {2, 1},
410 {1, 0}
411 }
412 }, quick_sort = {
413 0, tab, {
414 {0, 4}, {0, 8}, {4, 8},
415 {1, 4}, {2, 4}, {3, 4}, {5, 4}, {6, 4}, {7, 4}, {7, 4}, {6, 4},
416 {6, 4},
417 {8, 7},
418 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 4},
419 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 3},
420 {1, 0}, {2, 1}, {3, 2}, {4, 2},
421 {1, 0}, {2, 1}, {3, 2},
422 {1, 0}, {2, 1},
423 {1, 0}
424 }
425 };
426
427 if(!p_qsort_s) {
428 win_skip("qsort_s not available\n");
429 return;
430 }
431
432 for(i=0; i<8; i++) tab[i] = i;
433 p_qsort_s(tab, 8, sizeof(int), qsort_comp, &small_sort);
434 ok(small_sort.pos == 28, "small_sort.pos = %d\n", small_sort.pos);
435 for(i=0; i<8; i++)
436 ok(tab[i] == i, "tab[%d] = %d\n", i, tab[i]);
437
438 for(i=0; i<8; i++) tab[i] = 7-i;
439 p_qsort_s(tab, 8, sizeof(int), qsort_comp, &small_sort2);
440 ok(small_sort2.pos == 28, "small_sort2.pos = %d\n", small_sort2.pos);
441 for(i=0; i<8; i++)
442 ok(tab[i] == i, "tab[%d] = %d\n", i, tab[i]);
443
444 for(i=0; i<9; i++) tab[i] = i;
445 tab[5] = 1;
446 tab[6] = 2;
447 p_qsort_s(tab, 9, sizeof(int), qsort_comp, &quick_sort);
448 ok(quick_sort.pos == 34, "quick_sort.pos = %d\n", quick_sort.pos);
449
450 /* show that qsort is not stable */
451 for(i=0; i<9; i++) tab[i] = 8-i + 1000*(i+1);
452 tab[0] = 1003;
453 p_qsort_s(tab, 9, sizeof(int), qsort_comp, NULL);
454 for(i=0; i<9; i++)
455 ok(tab[i] == nonstable_test[i], "tab[%d] = %d, expected %d\n", i, tab[i], nonstable_test[i]);
456
457 /* check if random data is sorted */
458 srand(0);
459 for(i=0; i<100; i++) tab[i] = rand()%1000;
460 p_qsort_s(tab, 100, sizeof(int), qsort_comp, NULL);
461 for(i=1; i<100; i++)
462 ok(tab[i-1] <= tab[i], "data sorted incorrectly on position %d: %d <= %d\n", i, tab[i-1], tab[i]);
463
464 /* test if random permutation is sorted correctly */
465 for(i=0; i<100; i++) tab[i] = i;
466 for(i=0; i<100; i++) {
467 int b = rand()%100;
468 int e = rand()%100;
469
470 if(b == e) continue;
471 tab[b] ^= tab[e];
472 tab[e] ^= tab[b];
473 tab[b] ^= tab[e];
474 }
475 p_qsort_s(tab, 100, sizeof(int), qsort_comp, NULL);
476 for(i=0; i<100; i++)
477 ok(tab[i] == i, "data sorted incorrectly on position %d: %d\n", i, tab[i]);
478 }
479
480 START_TEST(misc)
481 {
482 int arg_c;
483 char** arg_v;
484
485 init();
486
487 arg_c = winetest_get_mainargs(&arg_v);
488 if(arg_c >= 3) {
489 if(!strcmp(arg_v[2], "popen"))
490 test__popen_child();
491 else
492 ok(0, "invalid argument '%s'\n", arg_v[2]);
493
494 return;
495 }
496
497 test_rand_s();
498 test_I10_OUTPUT();
499 test_strerror_s();
500 test__get_doserrno();
501 test__get_errno();
502 test__set_doserrno();
503 test__set_errno();
504 test__popen(arg_v[0]);
505 test__invalid_parameter();
506 test_qsort_s();
507 }