gdi32: Use NtGdiExcludeClipRect for ExcludeClipRect implementation.
[wine.git] / dlls / msvcrt / tests / misc.c
blob81c23dd23f2448bb712b32b96316d9421e9a275f
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 <stdio.h>
24 #include <math.h>
25 #include <process.h>
27 static inline BOOL almost_equal(double d1, double d2) {
28 if(d1-d2>-1e-30 && d1-d2<1e-30)
29 return TRUE;
30 return FALSE;
33 /* MS' "long double" is an 80 bit FP that takes 12 bytes*/
34 struct uld { ULONG lo, hi, exp; };
36 static int (__cdecl *prand_s)(unsigned int *);
37 static int (__cdecl *pI10_OUTPUT)(struct uld, int, int, void*);
38 static int (__cdecl *pstrerror_s)(char *, size_t, int);
39 static int (__cdecl *p_get_doserrno)(int *);
40 static int (__cdecl *p_get_errno)(int *);
41 static int (__cdecl *p_set_doserrno)(int);
42 static int (__cdecl *p_set_errno)(int);
43 static void (__cdecl *p__invalid_parameter)(const wchar_t*,
44 const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
45 static void (__cdecl *p_qsort_s)(void*, size_t, size_t,
46 int (__cdecl*)(void*, const void*, const void*), void*);
47 static double (__cdecl *p_atan)(double);
48 static double (__cdecl *p_exp)(double);
49 static double (__cdecl *p_tanh)(double);
50 static void *(__cdecl *p_lfind_s)(const void*, const void*, unsigned int*,
51 size_t, int (__cdecl *)(void*, const void*, const void*), void*);
53 static void init(void)
55 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
57 prand_s = (void *)GetProcAddress(hmod, "rand_s");
58 pI10_OUTPUT = (void*)GetProcAddress(hmod, "$I10_OUTPUT");
59 pstrerror_s = (void *)GetProcAddress(hmod, "strerror_s");
60 p_get_doserrno = (void *)GetProcAddress(hmod, "_get_doserrno");
61 p_get_errno = (void *)GetProcAddress(hmod, "_get_errno");
62 p_set_doserrno = (void *)GetProcAddress(hmod, "_set_doserrno");
63 p_set_errno = (void *)GetProcAddress(hmod, "_set_errno");
64 p__invalid_parameter = (void *)GetProcAddress(hmod, "_invalid_parameter");
65 p_qsort_s = (void *)GetProcAddress(hmod, "qsort_s");
66 p_atan = (void *)GetProcAddress(hmod, "atan");
67 p_exp = (void *)GetProcAddress(hmod, "exp");
68 p_tanh = (void *)GetProcAddress(hmod, "tanh");
69 p_lfind_s = (void *)GetProcAddress(hmod, "_lfind_s");
72 static void test_rand_s(void)
74 int ret;
75 unsigned int rand;
77 if (!prand_s)
79 win_skip("rand_s is not available\n");
80 return;
83 errno = EBADF;
84 ret = prand_s(NULL);
85 ok(ret == EINVAL, "Expected rand_s to return EINVAL, got %d\n", ret);
86 ok(errno == EINVAL, "Expected errno to return EINVAL, got %d\n", errno);
88 ret = prand_s(&rand);
89 ok(ret == 0, "Expected rand_s to return 0, got %d\n", ret);
92 typedef struct _I10_OUTPUT_data {
93 short pos;
94 char sign;
95 BYTE len;
96 char str[100];
97 } I10_OUTPUT_data;
99 typedef struct _I10_OUTPUT_test {
100 struct uld d;
101 int size;
102 int flags;
104 I10_OUTPUT_data out;
105 int ret;
106 const char *remain;
107 } I10_OUTPUT_test;
109 static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
110 /* arg3 = 0 */
111 { { 0x00000000, 0x00000000, 0x0000 /* 0.0 */ }, 10, 0, {0, ' ', 1, "0"}, 1, "" },
112 { { 0x00000000, 0x80000000, 0x3fff /* 1.0 */ }, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
113 { { 0x00000000, 0x80000000, 0xbfff /* -1.0 */ }, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
114 { { 0x0a3d7000, 0x9d70a3d7, 0x3fff /* 1.23 */ }, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
115 { { 0x00000000, 0x9184e72a, 0x402a /* 1e13 */ }, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
116 { { 0x04675000, 0xc9f2c9cd, 0x4062 /* 1e30 */ }, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
117 { { 0x4bb41000, 0xe12e1342, 0x3fd3 /* 1e-13 */ }, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
118 { { 0x00000000, 0x80000000, 0x3ffd /* 0.25 */ }, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
119 { { 0xbf94d800, 0x800000d6, 0x3fff /* 1.0000001 */ }, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
120 { { 0x00000000, 0x80000000, 0x7fff /* +inf */ }, 10, 0, {1, ' ', 5, "1#INF"}, 0, "" },
121 { { 0x00000000, 0x80000000, 0xffff /* -inf */ }, 10, 0, {1, '-', 5, "1#INF"}, 0, "" },
122 { { 0x00000001, 0x80000000, 0x7fff /* snan */ }, 10, 0, {1, ' ', 6, "1#SNAN"}, 0, "" },
123 { { 0x00000001, 0x80000000, 0xffff /* snan */ }, 10, 0, {1, '-', 6, "1#SNAN"}, 0, "" },
124 { { 0x00000000, 0xc0000000, 0x7fff /* qnan */ }, 10, 0, {1, ' ', 6, "1#QNAN"}, 0, "" },
125 { { 0x00000000, 0x40000000, 0xffff /* qnan */ }, 10, 0, {1, '-', 6, "1#QNAN"}, 0, "" },
126 /* arg3 = 1 */
127 { { 0x00000000, 0x00000000, 0x0000 /* 0 */ }, 10, 1, {0, ' ', 1, "0"}, 1, "" },
128 { { 0x00000000, 0x80000000, 0x3fff /* 1 */ }, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
129 { { 0x00000000, 0x80000000, 0xbfff /* -1 */ }, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
130 { { 0x0a3d7000, 0x9d70a3d7, 0x3fff /* 1.23 */ }, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
131 { { 0x00000000, 0x9184e72a, 0x402a /* 1e13 */ }, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
132 { { 0x04675000, 0xc9f2c9cd, 0x4062 /* 1e30 */ }, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
133 { { 0x4bb41000, 0xe12e1342, 0x3fd3 /* 1e-13 */ }, 10, 1, {0, ' ', 1, "0"}, 1, "" },
134 { { 0xe57a4000, 0xd6bf94d5, 0x3fe7 /* 1e-7 */ }, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
135 { { 0x00000000, 0x80000000, 0x3ffd /* 0.25 */ }, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
136 { { 0xbf94d800, 0x800000d6, 0x3fff /* 1.0000001 */ }, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
137 { { 0x00000000, 0x80000000, 0x7fff /* +inf */ }, 10, 1, {1, ' ', 5, "1#INF"}, 0, "" },
138 { { 0x00000000, 0x80000000, 0xffff /* -inf */ }, 10, 1, {1, '-', 5, "1#INF"}, 0, "" },
139 { { 0x00000001, 0x80000000, 0x7fff /* snan */ }, 10, 1, {1, ' ', 6, "1#SNAN"}, 0, "" },
140 { { 0x00000000, 0xc0000000, 0x7fff /* qnan */ }, 10, 1, {1, ' ', 6, "1#QNAN"}, 0, "" },
141 { { 0x00000000, 0x40000000, 0x7fff /* qnan */ }, 10, 1, {1, ' ', 6, "1#QNAN"}, 0, "" },
142 /* too small buffer */
143 { { 0x00000000, 0x00000000, 0x0000 /* 0 */ }, 0, 0, {0, ' ', 1, "0"}, 1, "" },
144 { { 0x00000000, 0x00000000, 0x0000 /* 0 */ }, 0, 1, {0, ' ', 1, "0"}, 1, "" },
145 { { 0x00000000, 0xf6000000, 0x4005 /* 123 */ }, 2, 0, {3, ' ', 2, "12"}, 1, "" },
146 { { 0x00000000, 0xf6000000, 0x4005 /* 123 */ }, 0, 0, {0, ' ', 1, "0"}, 1, "" },
147 { { 0x00000000, 0xf6000000, 0x4005 /* 123 */ }, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
148 { { 0x0a3d7000, 0xfd70a3d7, 0x3ffe /* 0.99 */ }, 1, 0, {1, ' ', 1, "1"}, 1, "" },
149 { { 0x00000000, 0x9a5db800, 0x4013 /* 1264567.0 */ }, 2, 0, {7, ' ', 2, "13"}, 1, "" },
150 { { 0x00000000, 0x9a5db800, 0x4013 /* 1264567.0 */ }, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
151 { { 0x00000000, 0x932c05a6, 0x401d /* 1234567891.0 */ }, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
154 static void test_I10_OUTPUT(void)
156 I10_OUTPUT_data out;
157 int i, j, ret;
159 if(!pI10_OUTPUT) {
160 win_skip("I10_OUTPUT not available\n");
161 return;
164 for(i=0; i<ARRAY_SIZE(I10_OUTPUT_tests); i++) {
165 memset(out.str, '#', sizeof(out.str));
166 ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
167 ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
168 ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
169 ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
170 ok(out.len == I10_OUTPUT_tests[i].out.len, "%d: out.len = %d\n", i, (int)out.len);
171 ok(!strcmp(out.str, I10_OUTPUT_tests[i].out.str), "%d: out.str = %s\n", i, out.str);
173 j = strlen(I10_OUTPUT_tests[i].remain);
174 todo_wine_if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
175 ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
176 "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
178 for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
179 if(out.str[j] != '#')
180 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i, j, out.str[j]);
184 static void test_strerror_s(void)
186 int ret;
187 char buf[256];
189 if (!pstrerror_s)
191 win_skip("strerror_s is not available\n");
192 return;
195 errno = EBADF;
196 ret = pstrerror_s(NULL, 0, 0);
197 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
198 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
200 errno = EBADF;
201 ret = pstrerror_s(NULL, sizeof(buf), 0);
202 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
203 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
205 memset(buf, 'X', sizeof(buf));
206 errno = EBADF;
207 ret = pstrerror_s(buf, 0, 0);
208 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
209 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
210 ok(buf[0] == 'X', "Expected output buffer to be untouched\n");
212 memset(buf, 'X', sizeof(buf));
213 ret = pstrerror_s(buf, 1, 0);
214 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
215 ok(buf[0] == 0, "Expected output buffer to be null terminated\n");
217 memset(buf, 'X', sizeof(buf));
218 ret = pstrerror_s(buf, 2, 0);
219 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
220 ok(strlen(buf) == 1, "Expected output buffer to be truncated\n");
222 memset(buf, 'X', sizeof(buf));
223 ret = pstrerror_s(buf, sizeof(buf), 0);
224 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
226 memset(buf, 'X', sizeof(buf));
227 ret = pstrerror_s(buf, sizeof(buf), -1);
228 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
231 static void test__get_doserrno(void)
233 int ret, out;
235 if (!p_get_doserrno)
237 win_skip("_get_doserrno is not available\n");
238 return;
241 _doserrno = ERROR_INVALID_CMM;
242 errno = EBADF;
243 ret = p_get_doserrno(NULL);
244 ok(ret == EINVAL, "Expected _get_doserrno to return EINVAL, got %d\n", ret);
245 ok(_doserrno == ERROR_INVALID_CMM, "Expected _doserrno to be ERROR_INVALID_CMM, got %ld\n", _doserrno);
246 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
248 _doserrno = ERROR_INVALID_CMM;
249 errno = EBADF;
250 out = 0xdeadbeef;
251 ret = p_get_doserrno(&out);
252 ok(ret == 0, "Expected _get_doserrno to return 0, got %d\n", ret);
253 ok(out == ERROR_INVALID_CMM, "Expected output variable to be ERROR_INVALID_CMM, got %d\n", out);
256 static void test__get_errno(void)
258 int ret, out;
260 if (!p_get_errno)
262 win_skip("_get_errno is not available\n");
263 return;
266 errno = EBADF;
267 ret = p_get_errno(NULL);
268 ok(ret == EINVAL, "Expected _get_errno to return EINVAL, got %d\n", ret);
269 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
271 errno = EBADF;
272 out = 0xdeadbeef;
273 ret = p_get_errno(&out);
274 ok(ret == 0, "Expected _get_errno to return 0, got %d\n", ret);
275 ok(out == EBADF, "Expected output variable to be EBADF, got %d\n", out);
278 static void test__set_doserrno(void)
280 int ret;
282 if (!p_set_doserrno)
284 win_skip("_set_doserrno is not available\n");
285 return;
288 _doserrno = ERROR_INVALID_CMM;
289 ret = p_set_doserrno(ERROR_FILE_NOT_FOUND);
290 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
291 ok(_doserrno == ERROR_FILE_NOT_FOUND,
292 "Expected _doserrno to be ERROR_FILE_NOT_FOUND, got %ld\n", _doserrno);
294 _doserrno = ERROR_INVALID_CMM;
295 ret = p_set_doserrno(-1);
296 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
297 ok(_doserrno == -1,
298 "Expected _doserrno to be -1, got %ld\n", _doserrno);
300 _doserrno = ERROR_INVALID_CMM;
301 ret = p_set_doserrno(0xdeadbeef);
302 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
303 ok(_doserrno == 0xdeadbeef,
304 "Expected _doserrno to be 0xdeadbeef, got %ld\n", _doserrno);
307 static void test__set_errno(void)
309 int ret;
311 if (!p_set_errno)
313 win_skip("_set_errno is not available\n");
314 return;
317 errno = EBADF;
318 ret = p_set_errno(EINVAL);
319 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
320 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
322 errno = EBADF;
323 ret = p_set_errno(-1);
324 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
325 ok(errno == -1, "Expected errno to be -1, got %d\n", errno);
327 errno = EBADF;
328 ret = p_set_errno(0xdeadbeef);
329 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
330 ok(errno == 0xdeadbeef, "Expected errno to be 0xdeadbeef, got %d\n", errno);
333 static void test__popen_child(void)
335 /* don't execute any tests here */
336 /* ExitProcess is used to set return code of _pclose */
337 printf("child output\n");
338 ExitProcess(0x37);
341 static void test__popen(const char *name)
343 FILE *pipe;
344 char buf[1024];
345 int ret;
347 sprintf(buf, "\"%s\" misc popen", name);
348 pipe = _popen(buf, "r");
349 ok(pipe != NULL, "_popen failed with error: %d\n", errno);
351 fgets(buf, sizeof(buf), pipe);
352 ok(!strcmp(buf, "child output\n"), "buf = %s\n", buf);
354 ret = _pclose(pipe);
355 ok(ret == 0x37, "_pclose returned %x, expected 0x37\n", ret);
357 errno = 0xdeadbeef;
358 ret = _pclose((FILE*)0xdeadbeef);
359 ok(ret == -1, "_pclose returned %x, expected -1\n", ret);
360 if(p_set_errno)
361 ok(errno == EBADF, "errno = %d\n", errno);
364 static void test__invalid_parameter(void)
366 if(!p__invalid_parameter) {
367 win_skip("_invalid_parameter not available\n");
368 return;
371 p__invalid_parameter(NULL, NULL, NULL, 0, 0);
374 struct qsort_test
376 int pos;
377 int *base;
379 struct {
380 int l;
381 int r;
382 } cmp[64];
385 static int __cdecl qsort_comp(void *ctx, const void *l, const void *r)
387 struct qsort_test *qt = ctx;
389 if(qt) {
390 ok(qt->pos < 64, "qt->pos = %d\n", qt->pos);
391 ok(qt->cmp[qt->pos].l == (int*)l-qt->base,
392 "%d) l on %ld position\n", qt->pos, (long)((int*)l - qt->base));
393 ok(qt->cmp[qt->pos].r == (int*)r-qt->base,
394 "%d) r on %ld position\n", qt->pos, (long)((int*)r - qt->base));
395 qt->pos++;
398 return *(int*)l%1000 - *(int*)r%1000;
401 static void test_qsort_s(void)
403 static const int nonstable_test[] = {9000, 8001, 7002, 6003, 1003, 5004, 4005, 3006, 2007};
404 int tab[100], i;
406 struct qsort_test small_sort = {
407 0, tab, {
408 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 5}, {7, 6},
409 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 5},
410 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4},
411 {1, 0}, {2, 1}, {3, 2}, {4, 3},
412 {1, 0}, {2, 1}, {3, 2},
413 {1, 0}, {2, 1},
414 {1, 0}
416 }, small_sort2 = {
417 0, tab, {
418 {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0},
419 {1, 0}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1},
420 {1, 0}, {2, 1}, {3, 2}, {4, 2}, {5, 2},
421 {1, 0}, {2, 1}, {3, 2}, {4, 3},
422 {1, 0}, {2, 1}, {3, 2},
423 {1, 0}, {2, 1},
424 {1, 0}
426 }, quick_sort = {
427 0, tab, {
428 {0, 4}, {0, 8}, {4, 8},
429 {1, 4}, {2, 4}, {3, 4}, {5, 4}, {6, 4}, {7, 4}, {7, 4}, {6, 4},
430 {6, 4},
431 {8, 7},
432 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 4},
433 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 3},
434 {1, 0}, {2, 1}, {3, 2}, {4, 2},
435 {1, 0}, {2, 1}, {3, 2},
436 {1, 0}, {2, 1},
437 {1, 0}
441 if(!p_qsort_s) {
442 win_skip("qsort_s not available\n");
443 return;
446 for(i=0; i<8; i++) tab[i] = i;
447 p_qsort_s(tab, 8, sizeof(int), qsort_comp, &small_sort);
448 ok(small_sort.pos == 28, "small_sort.pos = %d\n", small_sort.pos);
449 for(i=0; i<8; i++)
450 ok(tab[i] == i, "tab[%d] = %d\n", i, tab[i]);
452 for(i=0; i<8; i++) tab[i] = 7-i;
453 p_qsort_s(tab, 8, sizeof(int), qsort_comp, &small_sort2);
454 ok(small_sort2.pos == 28, "small_sort2.pos = %d\n", small_sort2.pos);
455 for(i=0; i<8; i++)
456 ok(tab[i] == i, "tab[%d] = %d\n", i, tab[i]);
458 for(i=0; i<9; i++) tab[i] = i;
459 tab[5] = 1;
460 tab[6] = 2;
461 p_qsort_s(tab, 9, sizeof(int), qsort_comp, &quick_sort);
462 ok(quick_sort.pos == 34, "quick_sort.pos = %d\n", quick_sort.pos);
464 /* show that qsort is not stable */
465 for(i=0; i<9; i++) tab[i] = 8-i + 1000*(i+1);
466 tab[0] = 1003;
467 p_qsort_s(tab, 9, sizeof(int), qsort_comp, NULL);
468 for(i=0; i<9; i++)
469 ok(tab[i] == nonstable_test[i], "tab[%d] = %d, expected %d\n", i, tab[i], nonstable_test[i]);
471 /* check if random data is sorted */
472 srand(0);
473 for(i=0; i<100; i++) tab[i] = rand()%1000;
474 p_qsort_s(tab, 100, sizeof(int), qsort_comp, NULL);
475 for(i=1; i<100; i++)
476 ok(tab[i-1] <= tab[i], "data sorted incorrectly on position %d: %d <= %d\n", i, tab[i-1], tab[i]);
478 /* test if random permutation is sorted correctly */
479 for(i=0; i<100; i++) tab[i] = i;
480 for(i=0; i<100; i++) {
481 int b = rand()%100;
482 int e = rand()%100;
484 if(b == e) continue;
485 tab[b] ^= tab[e];
486 tab[e] ^= tab[b];
487 tab[b] ^= tab[e];
489 p_qsort_s(tab, 100, sizeof(int), qsort_comp, NULL);
490 for(i=0; i<100; i++)
491 ok(tab[i] == i, "data sorted incorrectly on position %d: %d\n", i, tab[i]);
494 static int eq_nan(UINT64 ai, double b)
496 UINT64 bi = *(UINT64*)&b;
497 UINT64 mask;
499 #if defined(__i386__)
500 mask = 0xFFFFFFFF00000000ULL;
501 #else
502 mask = ~0;
503 #endif
505 ok((ai & mask) == (bi & mask), "comparing %s and %s\n",
506 wine_dbgstr_longlong(ai), wine_dbgstr_longlong(bi));
507 return (ai & mask) == (bi & mask);
510 static int eq_nanf(DWORD ai, float b)
512 DWORD bi = *(DWORD*)&b;
513 ok(ai == bi, "comparing %08x and %08x\n", ai, bi);
514 return ai == bi;
517 static void test_math_functions(void)
519 static const UINT64 test_nan_i = 0xFFF0000123456780ULL;
520 static const DWORD test_nanf_i = 0xFF801234;
521 double test_nan = *(double*)&test_nan_i;
522 float test_nanf = *(float*)&test_nanf_i;
523 double ret;
525 errno = 0xdeadbeef;
526 p_atan(NAN);
527 ok(errno == EDOM, "errno = %d\n", errno);
529 errno = 0xdeadbeef;
530 ret = p_atan(INFINITY);
531 ok(almost_equal(ret, 1.57079632679489661923), "ret = %lf\n", ret);
532 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
534 errno = 0xdeadbeef;
535 ret = p_atan(-INFINITY);
536 ok(almost_equal(ret, -1.57079632679489661923), "ret = %lf\n", ret);
537 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
539 errno = 0xdeadbeef;
540 p_tanh(NAN);
541 ok(errno == EDOM, "errno = %d\n", errno);
543 errno = 0xdeadbeef;
544 ret = p_tanh(INFINITY);
545 ok(almost_equal(ret, 1.0), "ret = %lf\n", ret);
546 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
548 errno = 0xdeadbeef;
549 p_exp(NAN);
550 ok(errno == EDOM, "errno = %d\n", errno);
552 errno = 0xdeadbeef;
553 p_exp(INFINITY);
554 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
556 ok(eq_nan(test_nan_i | (1ULL << 51), cosh(test_nan)), "cosh not preserving nan\n");
557 ok(eq_nan(test_nan_i | (1ULL << 51), sinh(test_nan)), "sinh not preserving nan\n");
558 ok(eq_nan(test_nan_i | (1ULL << 51), tanh(test_nan)), "tanh not preserving nan\n");
559 ok(eq_nanf(test_nanf_i | (1 << 22), coshf(test_nanf)), "coshf not preserving nan\n");
560 ok(eq_nanf(test_nanf_i | (1 << 22), sinhf(test_nanf)), "sinhf not preserving nan\n");
561 ok(eq_nanf(test_nanf_i | (1 << 22), tanhf(test_nanf)), "tanhf not preserving nan\n");
564 static void __cdecl test_thread_func(void *end_thread_type)
566 if (end_thread_type == (void*)1)
567 _endthread();
568 else if (end_thread_type == (void*)2)
569 ExitThread(0);
570 else if (end_thread_type == (void*)3)
571 _endthreadex(0);
574 static unsigned __stdcall test_thread_func_ex(void *arg)
576 _endthread();
577 return 0;
580 static void test_thread_handle_close(void)
582 HANDLE hThread;
583 DWORD ret;
585 /* _beginthread: handle is not closed on ExitThread and _endthreadex */
586 hThread = (HANDLE)_beginthread(test_thread_func, 0, NULL);
587 ok(hThread != INVALID_HANDLE_VALUE, "_beginthread failed (%d)\n", errno);
588 WaitForSingleObject(hThread, INFINITE);
589 ret = CloseHandle(hThread);
590 ok(!ret, "ret = %d\n", ret);
592 hThread = (HANDLE)_beginthread(test_thread_func, 0, (void*)1);
593 ok(hThread != INVALID_HANDLE_VALUE, "_beginthread failed (%d)\n", errno);
594 WaitForSingleObject(hThread, INFINITE);
595 ret = CloseHandle(hThread);
596 ok(!ret, "ret = %d\n", ret);
598 hThread = (HANDLE)_beginthread(test_thread_func, 0, (void*)2);
599 ok(hThread != INVALID_HANDLE_VALUE, "_beginthread failed (%d)\n", errno);
600 Sleep(150);
601 ret = WaitForSingleObject(hThread, INFINITE);
602 ok(ret == WAIT_OBJECT_0, "ret = %d\n", ret);
603 ret = CloseHandle(hThread);
604 ok(ret, "ret = %d\n", ret);
606 hThread = (HANDLE)_beginthread(test_thread_func, 0, (void*)3);
607 ok(hThread != INVALID_HANDLE_VALUE, "_beginthread failed (%d)\n", errno);
608 Sleep(150);
609 ret = WaitForSingleObject(hThread, INFINITE);
610 ok(ret == WAIT_OBJECT_0, "ret = %d\n", ret);
611 ret = CloseHandle(hThread);
612 ok(ret, "ret = %d\n", ret);
614 /* _beginthreadex: handle is not closed on _endthread */
615 hThread = (HANDLE)_beginthreadex(NULL,0, test_thread_func_ex, NULL, 0, NULL);
616 ok(hThread != NULL, "_beginthreadex failed (%d)\n", errno);
617 Sleep(150);
618 ret = WaitForSingleObject(hThread, INFINITE);
619 ok(ret == WAIT_OBJECT_0, "ret = %d\n", ret);
620 ret = CloseHandle(hThread);
621 ok(ret, "ret = %d\n", ret);
624 static void test_thread_suspended(void)
626 HANDLE hThread;
627 DWORD ret;
629 hThread = (HANDLE)_beginthreadex(NULL, 0, test_thread_func_ex, NULL, CREATE_SUSPENDED, NULL);
630 ok(hThread != NULL, "_beginthreadex failed (%d)\n", errno);
631 ret = ResumeThread(hThread);
632 ok(ret == 1, "suspend count = %d\n", ret);
633 ret = WaitForSingleObject(hThread, 200);
634 ok(ret == WAIT_OBJECT_0, "ret = %d\n", ret);
637 static int __cdecl _lfind_s_comp(void *ctx, const void *l, const void *r)
639 *(int *)ctx = 0xdeadc0de;
640 return *(int *)l - *(int *)r;
643 static void test__lfind_s(void)
645 static const int tests[] = {9000, 8001, 7002, 6003, 1003, 5004, 4005, 3006, 2007};
646 unsigned int num;
647 void *found;
648 int ctx;
649 int key;
651 if (!p_lfind_s)
653 win_skip("_lfind_s is not available\n");
654 return;
657 key = 1234;
658 num = ARRAY_SIZE(tests);
660 errno = 0xdeadbeef;
661 found = p_lfind_s(NULL, tests, &num, sizeof(int), _lfind_s_comp, NULL);
662 ok(errno == EINVAL, "errno = %d\n", errno);
663 ok(!found, "Expected NULL, got %p\n", found);
665 errno = 0xdeadbeef;
666 found = p_lfind_s(&key, NULL, &num, sizeof(int), _lfind_s_comp, NULL);
667 ok(errno == EINVAL, "errno = %d\n", errno);
668 ok(!found, "Expected NULL, got %p\n", found);
670 errno = 0xdeadbeef;
671 found = p_lfind_s(&key, tests, &num, 0, _lfind_s_comp, NULL);
672 ok(errno == EINVAL, "errno = %d\n", errno);
673 ok(!found, "Expected NULL, got %p\n", found);
675 errno = 0xdeadbeef;
676 found = p_lfind_s(&key, tests, &num, sizeof(int), NULL, NULL);
677 ok(errno == EINVAL, "errno = %d\n", errno);
678 ok(!found, "Expected NULL, got %p\n", found);
680 ctx = -1;
681 key = 9000;
682 errno = 0xdeadbeef;
683 found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
684 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
685 ok(found == tests, "Expected %p, got %p\n", tests, found);
686 ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
688 ctx = -1;
689 key = 2007;
690 errno = 0xdeadbeef;
691 found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
692 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
693 ok(found == tests+8, "Expected %p, got %p\n", tests+8, found);
694 ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
696 ctx = -1;
697 key = 1234;
698 errno = 0xdeadbeef;
699 found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
700 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
701 ok(!found, "Expected NULL, got %p\n", found);
702 ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
705 START_TEST(misc)
707 int arg_c;
708 char** arg_v;
710 init();
712 arg_c = winetest_get_mainargs(&arg_v);
713 if(arg_c >= 3) {
714 if(!strcmp(arg_v[2], "popen"))
715 test__popen_child();
716 else
717 ok(0, "invalid argument '%s'\n", arg_v[2]);
719 return;
722 test_rand_s();
723 test_I10_OUTPUT();
724 test_strerror_s();
725 test__get_doserrno();
726 test__get_errno();
727 test__set_doserrno();
728 test__set_errno();
729 test__popen(arg_v[0]);
730 test__invalid_parameter();
731 test_qsort_s();
732 test_math_functions();
733 test_thread_handle_close();
734 test_thread_suspended();
735 test__lfind_s();