[MSVCRT_WINETEST] Sync with Wine Staging 2.9. CORE-13362
[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 #include <process.h>
26
27 static inline float __port_infinity(void)
28 {
29 static const unsigned __inf_bytes = 0x7f800000;
30 return *(const float *)&__inf_bytes;
31 }
32 #define INFINITY __port_infinity()
33
34 static inline float __port_nan(void)
35 {
36 static const unsigned __nan_bytes = 0x7fc00000;
37 return *(const float *)&__nan_bytes;
38 }
39 #define NAN __port_nan()
40
41 static inline BOOL almost_equal(double d1, double d2) {
42 if(d1-d2>-1e-30 && d1-d2<1e-30)
43 return TRUE;
44 return FALSE;
45 }
46
47 static int (__cdecl *prand_s)(unsigned int *);
48 static int (__cdecl *pI10_OUTPUT)(long double, int, int, void*);
49 static int (__cdecl *pstrerror_s)(char *, MSVCRT_size_t, int);
50 static int (__cdecl *p_get_doserrno)(int *);
51 static int (__cdecl *p_get_errno)(int *);
52 static int (__cdecl *p_set_doserrno)(int);
53 static int (__cdecl *p_set_errno)(int);
54 static void (__cdecl *p__invalid_parameter)(const wchar_t*,
55 const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
56 static void (__cdecl *p_qsort_s)(void*, MSVCRT_size_t, MSVCRT_size_t,
57 int (__cdecl*)(void*, const void*, const void*), void*);
58 static double (__cdecl *p_atan)(double);
59 static double (__cdecl *p_exp)(double);
60 static double (__cdecl *p_tanh)(double);
61 static void *(__cdecl *p_lfind_s)(const void*, const void*, unsigned int*,
62 size_t, int (__cdecl *)(void*, const void*, const void*), void*);
63
64 static void init(void)
65 {
66 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
67
68 prand_s = (void *)GetProcAddress(hmod, "rand_s");
69 pI10_OUTPUT = (void*)GetProcAddress(hmod, "$I10_OUTPUT");
70 pstrerror_s = (void *)GetProcAddress(hmod, "strerror_s");
71 p_get_doserrno = (void *)GetProcAddress(hmod, "_get_doserrno");
72 p_get_errno = (void *)GetProcAddress(hmod, "_get_errno");
73 p_set_doserrno = (void *)GetProcAddress(hmod, "_set_doserrno");
74 p_set_errno = (void *)GetProcAddress(hmod, "_set_errno");
75 p__invalid_parameter = (void *)GetProcAddress(hmod, "_invalid_parameter");
76 p_qsort_s = (void *)GetProcAddress(hmod, "qsort_s");
77 p_atan = (void *)GetProcAddress(hmod, "atan");
78 p_exp = (void *)GetProcAddress(hmod, "exp");
79 p_tanh = (void *)GetProcAddress(hmod, "tanh");
80 p_lfind_s = (void *)GetProcAddress(hmod, "_lfind_s");
81 }
82
83 static void test_rand_s(void)
84 {
85 int ret;
86 unsigned int rand;
87
88 if (!prand_s)
89 {
90 win_skip("rand_s is not available\n");
91 return;
92 }
93
94 errno = EBADF;
95 ret = prand_s(NULL);
96 ok(ret == EINVAL, "Expected rand_s to return EINVAL, got %d\n", ret);
97 ok(errno == EINVAL, "Expected errno to return EINVAL, got %d\n", errno);
98
99 ret = prand_s(&rand);
100 ok(ret == 0, "Expected rand_s to return 0, got %d\n", ret);
101 }
102
103 typedef struct _I10_OUTPUT_data {
104 short pos;
105 char sign;
106 BYTE len;
107 char str[100];
108 } I10_OUTPUT_data;
109
110 typedef struct _I10_OUTPUT_test {
111 long double d;
112 int size;
113 int flags;
114
115 I10_OUTPUT_data out;
116 int ret;
117 const char *remain;
118 } I10_OUTPUT_test;
119
120 static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
121 /* arg3 = 0 */
122 { 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
123 { 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
124 { -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
125 { 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
126 { 1e13, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
127 { 1e30, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
128 { 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
129 { 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
130 { 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
131 /* arg3 = 1 */
132 { 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
133 { 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
134 { -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
135 { 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
136 { 1e13, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
137 { 1e30, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
138 { 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
139 { 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
140 { 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
141 { 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
142 /* too small buffer */
143 { 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
144 { 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
145 { 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
146 { 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
147 { 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
148 { 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
149 { 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
150 { 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
151 { 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
152 };
153
154 static void test_I10_OUTPUT(void)
155 {
156 I10_OUTPUT_data out;
157 int i, j = sizeof(long double), ret;
158
159 if(!pI10_OUTPUT) {
160 win_skip("I10_OUTPUT not available\n");
161 return;
162 }
163 if (j != 12)
164 trace("sizeof(long double) = %d on this machine\n", j);
165
166 for(i=0; i<sizeof(I10_OUTPUT_tests)/sizeof(I10_OUTPUT_test); i++) {
167 memset(out.str, '#', sizeof(out.str));
168
169 if (sizeof(long double) == 12)
170 ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
171 else {
172 /* MS' "long double" is an 80 bit FP that takes 12 bytes*/
173 typedef struct { ULONG x80[3]; } uld; /* same calling convention */
174 union { long double ld; uld ld12; } fp80;
175 int (__cdecl *pI10_OUTPUT12)(uld, int, int, void*) = (void*)pI10_OUTPUT;
176 fp80.ld = I10_OUTPUT_tests[i].d;
177 ret = pI10_OUTPUT12(fp80.ld12, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
178 }
179 ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
180 ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
181 ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
182 ok(out.len == I10_OUTPUT_tests[i].out.len, "%d: out.len = %d\n", i, (int)out.len);
183 ok(!strcmp(out.str, I10_OUTPUT_tests[i].out.str), "%d: out.str = %s\n", i, out.str);
184
185 j = strlen(I10_OUTPUT_tests[i].remain);
186 todo_wine_if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
187 ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
188 "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
189
190 for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
191 if(out.str[j] != '#')
192 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i, j, out.str[j]);
193 }
194 }
195
196 static void test_strerror_s(void)
197 {
198 int ret;
199 char buf[256];
200
201 if (!pstrerror_s)
202 {
203 win_skip("strerror_s is not available\n");
204 return;
205 }
206
207 errno = EBADF;
208 ret = pstrerror_s(NULL, 0, 0);
209 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
210 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
211
212 errno = EBADF;
213 ret = pstrerror_s(NULL, sizeof(buf), 0);
214 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
215 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
216
217 memset(buf, 'X', sizeof(buf));
218 errno = EBADF;
219 ret = pstrerror_s(buf, 0, 0);
220 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
221 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
222 ok(buf[0] == 'X', "Expected output buffer to be untouched\n");
223
224 memset(buf, 'X', sizeof(buf));
225 ret = pstrerror_s(buf, 1, 0);
226 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
227 ok(strlen(buf) == 0, "Expected output buffer to be null terminated\n");
228
229 memset(buf, 'X', sizeof(buf));
230 ret = pstrerror_s(buf, 2, 0);
231 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
232 ok(strlen(buf) == 1, "Expected output buffer to be truncated\n");
233
234 memset(buf, 'X', sizeof(buf));
235 ret = pstrerror_s(buf, sizeof(buf), 0);
236 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
237
238 memset(buf, 'X', sizeof(buf));
239 ret = pstrerror_s(buf, sizeof(buf), -1);
240 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
241 }
242
243 static void test__get_doserrno(void)
244 {
245 int ret, out;
246
247 if (!p_get_doserrno)
248 {
249 win_skip("_get_doserrno is not available\n");
250 return;
251 }
252
253 _doserrno = ERROR_INVALID_CMM;
254 errno = EBADF;
255 ret = p_get_doserrno(NULL);
256 ok(ret == EINVAL, "Expected _get_doserrno to return EINVAL, got %d\n", ret);
257 ok(_doserrno == ERROR_INVALID_CMM, "Expected _doserrno to be ERROR_INVALID_CMM, got %d\n", _doserrno);
258 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
259
260 _doserrno = ERROR_INVALID_CMM;
261 errno = EBADF;
262 out = 0xdeadbeef;
263 ret = p_get_doserrno(&out);
264 ok(ret == 0, "Expected _get_doserrno to return 0, got %d\n", ret);
265 ok(out == ERROR_INVALID_CMM, "Expected output variable to be ERROR_INVALID_CMM, got %d\n", out);
266 }
267
268 static void test__get_errno(void)
269 {
270 int ret, out;
271
272 if (!p_get_errno)
273 {
274 win_skip("_get_errno is not available\n");
275 return;
276 }
277
278 errno = EBADF;
279 ret = p_get_errno(NULL);
280 ok(ret == EINVAL, "Expected _get_errno to return EINVAL, got %d\n", ret);
281 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
282
283 errno = EBADF;
284 out = 0xdeadbeef;
285 ret = p_get_errno(&out);
286 ok(ret == 0, "Expected _get_errno to return 0, got %d\n", ret);
287 ok(out == EBADF, "Expected output variable to be EBADF, got %d\n", out);
288 }
289
290 static void test__set_doserrno(void)
291 {
292 int ret;
293
294 if (!p_set_doserrno)
295 {
296 win_skip("_set_doserrno is not available\n");
297 return;
298 }
299
300 _doserrno = ERROR_INVALID_CMM;
301 ret = p_set_doserrno(ERROR_FILE_NOT_FOUND);
302 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
303 ok(_doserrno == ERROR_FILE_NOT_FOUND,
304 "Expected _doserrno to be ERROR_FILE_NOT_FOUND, got %d\n", _doserrno);
305
306 _doserrno = ERROR_INVALID_CMM;
307 ret = p_set_doserrno(-1);
308 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
309 ok(_doserrno == -1,
310 "Expected _doserrno to be -1, got %d\n", _doserrno);
311
312 _doserrno = ERROR_INVALID_CMM;
313 ret = p_set_doserrno(0xdeadbeef);
314 ok(ret == 0, "Expected _set_doserrno to return 0, got %d\n", ret);
315 ok(_doserrno == 0xdeadbeef,
316 "Expected _doserrno to be 0xdeadbeef, got %d\n", _doserrno);
317 }
318
319 static void test__set_errno(void)
320 {
321 int ret;
322
323 if (!p_set_errno)
324 {
325 win_skip("_set_errno is not available\n");
326 return;
327 }
328
329 errno = EBADF;
330 ret = p_set_errno(EINVAL);
331 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
332 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
333
334 errno = EBADF;
335 ret = p_set_errno(-1);
336 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
337 ok(errno == -1, "Expected errno to be -1, got %d\n", errno);
338
339 errno = EBADF;
340 ret = p_set_errno(0xdeadbeef);
341 ok(ret == 0, "Expected _set_errno to return 0, got %d\n", ret);
342 ok(errno == 0xdeadbeef, "Expected errno to be 0xdeadbeef, got %d\n", errno);
343 }
344
345 static void test__popen_child(void)
346 {
347 /* don't execute any tests here */
348 /* ExitProcess is used to set return code of _pclose */
349 printf("child output\n");
350 ExitProcess(0x37);
351 }
352
353 static void test__popen(const char *name)
354 {
355 FILE *pipe;
356 char buf[1024];
357 int ret;
358
359 sprintf(buf, "\"%s\" misc popen", name);
360 pipe = _popen(buf, "r");
361 ok(pipe != NULL, "_popen failed with error: %d\n", errno);
362
363 fgets(buf, sizeof(buf), pipe);
364 ok(!strcmp(buf, "child output\n"), "buf = %s\n", buf);
365
366 ret = _pclose(pipe);
367 ok(ret == 0x37, "_pclose returned %x, expected 0x37\n", ret);
368
369 errno = 0xdeadbeef;
370 ret = _pclose((FILE*)0xdeadbeef);
371 ok(ret == -1, "_pclose returned %x, expected -1\n", ret);
372 if(p_set_errno)
373 ok(errno == EBADF, "errno = %d\n", errno);
374 }
375
376 static void test__invalid_parameter(void)
377 {
378 if(!p__invalid_parameter) {
379 win_skip("_invalid_parameter not available\n");
380 return;
381 }
382
383 p__invalid_parameter(NULL, NULL, NULL, 0, 0);
384 }
385
386 struct qsort_test
387 {
388 int pos;
389 int *base;
390
391 struct {
392 int l;
393 int r;
394 } cmp[64];
395 };
396
397 static int __cdecl qsort_comp(void *ctx, const void *l, const void *r)
398 {
399 struct qsort_test *qt = ctx;
400
401 if(qt) {
402 ok(qt->pos < 64, "qt->pos = %d\n", qt->pos);
403 ok(qt->cmp[qt->pos].l == (int*)l-qt->base,
404 "%d) l on %ld position\n", qt->pos, (long)((int*)l - qt->base));
405 ok(qt->cmp[qt->pos].r == (int*)r-qt->base,
406 "%d) r on %ld position\n", qt->pos, (long)((int*)r - qt->base));
407 qt->pos++;
408 }
409
410 return *(int*)l%1000 - *(int*)r%1000;
411 }
412
413 static void test_qsort_s(void)
414 {
415 static const int nonstable_test[] = {9000, 8001, 7002, 6003, 1003, 5004, 4005, 3006, 2007};
416 int tab[100], i;
417
418 struct qsort_test small_sort = {
419 0, tab, {
420 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 5}, {7, 6},
421 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 5},
422 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4},
423 {1, 0}, {2, 1}, {3, 2}, {4, 3},
424 {1, 0}, {2, 1}, {3, 2},
425 {1, 0}, {2, 1},
426 {1, 0}
427 }
428 }, small_sort2 = {
429 0, tab, {
430 {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0},
431 {1, 0}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1},
432 {1, 0}, {2, 1}, {3, 2}, {4, 2}, {5, 2},
433 {1, 0}, {2, 1}, {3, 2}, {4, 3},
434 {1, 0}, {2, 1}, {3, 2},
435 {1, 0}, {2, 1},
436 {1, 0}
437 }
438 }, quick_sort = {
439 0, tab, {
440 {0, 4}, {0, 8}, {4, 8},
441 {1, 4}, {2, 4}, {3, 4}, {5, 4}, {6, 4}, {7, 4}, {7, 4}, {6, 4},
442 {6, 4},
443 {8, 7},
444 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 4}, {6, 4},
445 {1, 0}, {2, 1}, {3, 2}, {4, 3}, {5, 3},
446 {1, 0}, {2, 1}, {3, 2}, {4, 2},
447 {1, 0}, {2, 1}, {3, 2},
448 {1, 0}, {2, 1},
449 {1, 0}
450 }
451 };
452
453 if(!p_qsort_s) {
454 win_skip("qsort_s not available\n");
455 return;
456 }
457
458 for(i=0; i<8; i++) tab[i] = i;
459 p_qsort_s(tab, 8, sizeof(int), qsort_comp, &small_sort);
460 ok(small_sort.pos == 28, "small_sort.pos = %d\n", small_sort.pos);
461 for(i=0; i<8; i++)
462 ok(tab[i] == i, "tab[%d] = %d\n", i, tab[i]);
463
464 for(i=0; i<8; i++) tab[i] = 7-i;
465 p_qsort_s(tab, 8, sizeof(int), qsort_comp, &small_sort2);
466 ok(small_sort2.pos == 28, "small_sort2.pos = %d\n", small_sort2.pos);
467 for(i=0; i<8; i++)
468 ok(tab[i] == i, "tab[%d] = %d\n", i, tab[i]);
469
470 for(i=0; i<9; i++) tab[i] = i;
471 tab[5] = 1;
472 tab[6] = 2;
473 p_qsort_s(tab, 9, sizeof(int), qsort_comp, &quick_sort);
474 ok(quick_sort.pos == 34, "quick_sort.pos = %d\n", quick_sort.pos);
475
476 /* show that qsort is not stable */
477 for(i=0; i<9; i++) tab[i] = 8-i + 1000*(i+1);
478 tab[0] = 1003;
479 p_qsort_s(tab, 9, sizeof(int), qsort_comp, NULL);
480 for(i=0; i<9; i++)
481 ok(tab[i] == nonstable_test[i], "tab[%d] = %d, expected %d\n", i, tab[i], nonstable_test[i]);
482
483 /* check if random data is sorted */
484 srand(0);
485 for(i=0; i<100; i++) tab[i] = rand()%1000;
486 p_qsort_s(tab, 100, sizeof(int), qsort_comp, NULL);
487 for(i=1; i<100; i++)
488 ok(tab[i-1] <= tab[i], "data sorted incorrectly on position %d: %d <= %d\n", i, tab[i-1], tab[i]);
489
490 /* test if random permutation is sorted correctly */
491 for(i=0; i<100; i++) tab[i] = i;
492 for(i=0; i<100; i++) {
493 int b = rand()%100;
494 int e = rand()%100;
495
496 if(b == e) continue;
497 tab[b] ^= tab[e];
498 tab[e] ^= tab[b];
499 tab[b] ^= tab[e];
500 }
501 p_qsort_s(tab, 100, sizeof(int), qsort_comp, NULL);
502 for(i=0; i<100; i++)
503 ok(tab[i] == i, "data sorted incorrectly on position %d: %d\n", i, tab[i]);
504 }
505
506 static void test_math_functions(void)
507 {
508 double ret;
509
510 errno = 0xdeadbeef;
511 p_atan(NAN);
512 ok(errno == EDOM, "errno = %d\n", errno);
513
514 errno = 0xdeadbeef;
515 ret = p_atan(INFINITY);
516 ok(almost_equal(ret, 1.57079632679489661923), "ret = %lf\n", ret);
517 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
518
519 errno = 0xdeadbeef;
520 ret = p_atan(-INFINITY);
521 ok(almost_equal(ret, -1.57079632679489661923), "ret = %lf\n", ret);
522 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
523
524 errno = 0xdeadbeef;
525 p_tanh(NAN);
526 ok(errno == EDOM, "errno = %d\n", errno);
527
528 errno = 0xdeadbeef;
529 ret = p_tanh(INFINITY);
530 ok(almost_equal(ret, 1.0), "ret = %lf\n", ret);
531 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
532
533 errno = 0xdeadbeef;
534 p_exp(NAN);
535 ok(errno == EDOM, "errno = %d\n", errno);
536
537 errno = 0xdeadbeef;
538 p_exp(INFINITY);
539 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
540 }
541
542 static void __cdecl test_thread_func(void *end_thread_type)
543 {
544 if (end_thread_type == (void*)1)
545 _endthread();
546 else if (end_thread_type == (void*)2)
547 ExitThread(0);
548 else if (end_thread_type == (void*)3)
549 _endthreadex(0);
550 }
551
552 static unsigned __stdcall test_thread_func_ex(void *arg)
553 {
554 _endthread();
555 return 0;
556 }
557
558 static void test_thread_handle_close(void)
559 {
560 HANDLE hThread;
561 DWORD ret;
562
563 /* _beginthread: handle is not closed on ExitThread and _endthreadex */
564 hThread = (HANDLE)_beginthread(test_thread_func, 0, NULL);
565 ok(hThread != INVALID_HANDLE_VALUE, "_beginthread failed (%d)\n", errno);
566 WaitForSingleObject(hThread, INFINITE);
567 ret = CloseHandle(hThread);
568 ok(!ret, "ret = %d\n", ret);
569
570 hThread = (HANDLE)_beginthread(test_thread_func, 0, (void*)1);
571 ok(hThread != INVALID_HANDLE_VALUE, "_beginthread failed (%d)\n", errno);
572 WaitForSingleObject(hThread, INFINITE);
573 ret = CloseHandle(hThread);
574 ok(!ret, "ret = %d\n", ret);
575
576 hThread = (HANDLE)_beginthread(test_thread_func, 0, (void*)2);
577 ok(hThread != INVALID_HANDLE_VALUE, "_beginthread failed (%d)\n", errno);
578 Sleep(150);
579 ret = WaitForSingleObject(hThread, INFINITE);
580 ok(ret == WAIT_OBJECT_0, "ret = %d\n", ret);
581 ret = CloseHandle(hThread);
582 ok(ret, "ret = %d\n", ret);
583
584 hThread = (HANDLE)_beginthread(test_thread_func, 0, (void*)3);
585 ok(hThread != INVALID_HANDLE_VALUE, "_beginthread failed (%d)\n", errno);
586 Sleep(150);
587 ret = WaitForSingleObject(hThread, INFINITE);
588 ok(ret == WAIT_OBJECT_0, "ret = %d\n", ret);
589 ret = CloseHandle(hThread);
590 ok(ret, "ret = %d\n", ret);
591
592 /* _beginthreadex: handle is not closed on _endthread */
593 hThread = (HANDLE)_beginthreadex(NULL,0, test_thread_func_ex, NULL, 0, NULL);
594 ok(hThread != NULL, "_beginthreadex failed (%d)\n", errno);
595 Sleep(150);
596 ret = WaitForSingleObject(hThread, INFINITE);
597 ok(ret == WAIT_OBJECT_0, "ret = %d\n", ret);
598 ret = CloseHandle(hThread);
599 ok(ret, "ret = %d\n", ret);
600 }
601
602 static int __cdecl _lfind_s_comp(void *ctx, const void *l, const void *r)
603 {
604 *(int *)ctx = 0xdeadc0de;
605 return *(int *)l - *(int *)r;
606 }
607
608 static void test__lfind_s(void)
609 {
610 static const int tests[] = {9000, 8001, 7002, 6003, 1003, 5004, 4005, 3006, 2007};
611 unsigned int num;
612 void *found;
613 int ctx;
614 int key;
615
616 if (!p_lfind_s)
617 {
618 win_skip("_lfind_s is not available\n");
619 return;
620 }
621
622 key = 1234;
623 num = sizeof(tests)/sizeof(tests[0]);
624
625 errno = 0xdeadbeef;
626 found = p_lfind_s(NULL, tests, &num, sizeof(int), _lfind_s_comp, NULL);
627 ok(errno == EINVAL, "errno = %d\n", errno);
628 ok(!found, "Expected NULL, got %p\n", found);
629
630 errno = 0xdeadbeef;
631 found = p_lfind_s(&key, NULL, &num, sizeof(int), _lfind_s_comp, NULL);
632 ok(errno == EINVAL, "errno = %d\n", errno);
633 ok(!found, "Expected NULL, got %p\n", found);
634
635 errno = 0xdeadbeef;
636 found = p_lfind_s(&key, tests, &num, 0, _lfind_s_comp, NULL);
637 ok(errno == EINVAL, "errno = %d\n", errno);
638 ok(!found, "Expected NULL, got %p\n", found);
639
640 errno = 0xdeadbeef;
641 found = p_lfind_s(&key, tests, &num, sizeof(int), NULL, NULL);
642 ok(errno == EINVAL, "errno = %d\n", errno);
643 ok(!found, "Expected NULL, got %p\n", found);
644
645 ctx = -1;
646 key = 9000;
647 errno = 0xdeadbeef;
648 found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
649 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
650 ok(found == tests, "Expected %p, got %p\n", tests, found);
651 ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
652
653 ctx = -1;
654 key = 2007;
655 errno = 0xdeadbeef;
656 found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
657 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
658 ok(found == tests+8, "Expected %p, got %p\n", tests+8, found);
659 ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
660
661 ctx = -1;
662 key = 1234;
663 errno = 0xdeadbeef;
664 found = p_lfind_s(&key, tests, &num, sizeof(int), _lfind_s_comp, &ctx);
665 ok(errno == 0xdeadbeef, "errno = %d\n", errno);
666 ok(!found, "Expected NULL, got %p\n", found);
667 ok(ctx == 0xdeadc0de, "Expected 0xdeadc0de, got %x\n", ctx);
668 }
669
670 START_TEST(misc)
671 {
672 int arg_c;
673 char** arg_v;
674
675 init();
676
677 arg_c = winetest_get_mainargs(&arg_v);
678 if(arg_c >= 3) {
679 if(!strcmp(arg_v[2], "popen"))
680 test__popen_child();
681 else
682 ok(0, "invalid argument '%s'\n", arg_v[2]);
683
684 return;
685 }
686
687 test_rand_s();
688 test_I10_OUTPUT();
689 test_strerror_s();
690 test__get_doserrno();
691 test__get_errno();
692 test__set_doserrno();
693 test__set_errno();
694 test__popen(arg_v[0]);
695 test__invalid_parameter();
696 test_qsort_s();
697 test_math_functions();
698 test_thread_handle_close();
699 test__lfind_s();
700 }