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