2 * Copyright 2015 Martin Storsjo
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
34 #define DEFINE_EXPECT(func) \
35 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
37 #define SET_EXPECT(func) \
38 expect_ ## func = TRUE
40 #define CHECK_EXPECT2(func) \
42 ok(expect_ ##func, "unexpected call " #func "\n"); \
43 called_ ## func = TRUE; \
46 #define CHECK_EXPECT(func) \
48 CHECK_EXPECT2(func); \
49 expect_ ## func = FALSE; \
52 #define CHECK_CALLED(func) \
54 ok(called_ ## func, "expected " #func "\n"); \
55 expect_ ## func = called_ ## func = FALSE; \
58 DEFINE_EXPECT(invalid_parameter_handler
);
60 static void __cdecl
test_invalid_parameter_handler(const wchar_t *expression
,
61 const wchar_t *function
, const wchar_t *file
,
62 unsigned line
, uintptr_t arg
)
64 CHECK_EXPECT(invalid_parameter_handler
);
65 ok(expression
== NULL
, "expression is not NULL\n");
66 ok(function
== NULL
, "function is not NULL\n");
67 ok(file
== NULL
, "file is not NULL\n");
68 ok(line
== 0, "line = %u\n", line
);
69 ok(arg
== 0, "arg = %lx\n", (UINT_PTR
)arg
);
72 _ACRTIMP
int __cdecl
_o_tolower(int);
73 _ACRTIMP
int __cdecl
_o_toupper(int);
75 static BOOL
local_isnan(double d
)
80 #define test_strtod_str_errno(string, value, length, err) _test_strtod_str(__LINE__, string, value, length, err)
81 #define test_strtod_str(string, value, length) _test_strtod_str(__LINE__, string, value, length, 0)
82 static void _test_strtod_str(int line
, const char* string
, double value
, int length
, int err
)
87 d
= strtod(string
, &end
);
89 ok_(__FILE__
, line
)(errno
== 0xdeadbeef, "errno = %d\n", errno
);
91 ok_(__FILE__
, line
)(errno
== err
, "errno = %d\n", errno
);
92 if (local_isnan(value
))
93 ok_(__FILE__
, line
)(local_isnan(d
), "d = %.16le (\"%s\")\n", d
, string
);
95 ok_(__FILE__
, line
)(d
== value
, "d = %.16le (\"%s\")\n", d
, string
);
96 ok_(__FILE__
, line
)(end
== string
+ length
, "incorrect end (%d, \"%s\")\n", (int)(end
- string
), string
);
99 static void test_strtod(void)
101 test_strtod_str("infinity", INFINITY
, 8);
102 test_strtod_str("INFINITY", INFINITY
, 8);
103 test_strtod_str("InFiNiTy", INFINITY
, 8);
104 test_strtod_str("INF", INFINITY
, 3);
105 test_strtod_str("-inf", -INFINITY
, 4);
106 test_strtod_str("inf42", INFINITY
, 3);
107 test_strtod_str("inffoo", INFINITY
, 3);
108 test_strtod_str("infini", INFINITY
, 3);
109 test_strtod_str("input", 0, 0);
110 test_strtod_str("-input", 0, 0);
111 test_strtod_str_errno("1.7976931348623159e+308", INFINITY
, 23, ERANGE
);
112 test_strtod_str_errno("-1.7976931348623159e+308", -INFINITY
, 24, ERANGE
);
114 test_strtod_str("NAN", NAN
, 3);
115 test_strtod_str("nan", NAN
, 3);
116 test_strtod_str("NaN", NAN
, 3);
118 test_strtod_str("0x42", 66, 4);
119 test_strtod_str("0X42", 66, 4);
120 test_strtod_str("-0x42", -66, 5);
121 test_strtod_str("0x1p1", 2, 5);
122 test_strtod_str("0x1P1", 2, 5);
123 test_strtod_str("0x1p+1", 2, 6);
124 test_strtod_str("0x2p-1", 1, 6);
125 test_strtod_str("0xA", 10, 3);
126 test_strtod_str("0xa", 10, 3);
127 test_strtod_str("0xABCDEF", 11259375, 8);
128 test_strtod_str("0Xabcdef", 11259375, 8);
130 test_strtod_str("0x1.1", 1.0625, 5);
131 test_strtod_str("0x1.1p1", 2.125, 7);
132 test_strtod_str("0x1.A", 1.625, 5);
133 test_strtod_str("0x1p1a", 2, 5);
134 test_strtod_str("0xp3", 0, 1);
135 test_strtod_str("0x.", 0, 1);
136 test_strtod_str("0x.8", 0.5, 4);
137 test_strtod_str("0x.8p", 0.5, 4);
138 test_strtod_str("0x0p10000000000000000000000000", 0, 30);
139 test_strtod_str("0x1p-1026", 1.3906711615670009e-309, 9);
141 test_strtod_str("0x1ffffffffffffe.80000000000000000000", 9007199254740990.0, 37);
142 test_strtod_str("0x1ffffffffffffe.80000000000000000001", 9007199254740991.0, 37);
143 test_strtod_str("0x1fffffffffffff.80000000000000000000", 9007199254740992.0, 37);
144 test_strtod_str("0x1fffffffffffff.80000000000000000001", 9007199254740992.0, 37);
146 test_strtod_str("4.0621786324484881721115322e-53", 4.0621786324484881721115322e-53, 31);
147 test_strtod_str("1.8905590910042396899370942", 1.8905590910042396899370942, 27);
148 test_strtod_str("1.7976931348623158e+308", 1.7976931348623158e+308, 23);
149 test_strtod_str("2.2250738585072014e-308", 2.2250738585072014e-308, 23);
150 test_strtod_str("4.9406564584124654e-324", 4.9406564584124654e-324, 23);
151 test_strtod_str("2.48e-324", 4.9406564584124654e-324, 9);
152 test_strtod_str_errno("2.47e-324", 0, 9, ERANGE
);
155 static void test__memicmp(void)
157 static const char *s1
= "abc";
158 static const char *s2
= "aBd";
161 ret
= _memicmp(NULL
, NULL
, 0);
162 ok(!ret
, "got %d\n", ret
);
164 SET_EXPECT(invalid_parameter_handler
);
166 ret
= _memicmp(NULL
, NULL
, 1);
167 ok(ret
== _NLSCMPERROR
, "got %d\n", ret
);
168 ok(errno
== EINVAL
, "Unexpected errno = %d\n", errno
);
169 CHECK_CALLED(invalid_parameter_handler
);
171 SET_EXPECT(invalid_parameter_handler
);
173 ret
= _memicmp(s1
, NULL
, 1);
174 ok(ret
== _NLSCMPERROR
, "got %d\n", ret
);
175 ok(errno
== EINVAL
, "Unexpected errno = %d\n", errno
);
176 CHECK_CALLED(invalid_parameter_handler
);
178 SET_EXPECT(invalid_parameter_handler
);
180 ret
= _memicmp(NULL
, s2
, 1);
181 ok(ret
== _NLSCMPERROR
, "got %d\n", ret
);
182 ok(errno
== EINVAL
, "Unexpected errno = %d\n", errno
);
183 CHECK_CALLED(invalid_parameter_handler
);
185 ret
= _memicmp(s1
, s2
, 2);
186 ok(!ret
, "got %d\n", ret
);
188 ret
= _memicmp(s1
, s2
, 3);
189 ok(ret
== -1, "got %d\n", ret
);
192 static void test__memicmp_l(void)
194 static const char *s1
= "abc";
195 static const char *s2
= "aBd";
198 ret
= _memicmp_l(NULL
, NULL
, 0, NULL
);
199 ok(!ret
, "got %d\n", ret
);
201 SET_EXPECT(invalid_parameter_handler
);
203 ret
= _memicmp_l(NULL
, NULL
, 1, NULL
);
204 ok(ret
== _NLSCMPERROR
, "got %d\n", ret
);
205 ok(errno
== EINVAL
, "Unexpected errno = %d\n", errno
);
206 CHECK_CALLED(invalid_parameter_handler
);
208 SET_EXPECT(invalid_parameter_handler
);
210 ret
= _memicmp_l(s1
, NULL
, 1, NULL
);
211 ok(ret
== _NLSCMPERROR
, "got %d\n", ret
);
212 ok(errno
== EINVAL
, "Unexpected errno = %d\n", errno
);
213 CHECK_CALLED(invalid_parameter_handler
);
215 SET_EXPECT(invalid_parameter_handler
);
217 ret
= _memicmp_l(NULL
, s2
, 1, NULL
);
218 ok(ret
== _NLSCMPERROR
, "got %d\n", ret
);
219 ok(errno
== EINVAL
, "Unexpected errno = %d\n", errno
);
220 CHECK_CALLED(invalid_parameter_handler
);
222 ret
= _memicmp_l(s1
, s2
, 2, NULL
);
223 ok(!ret
, "got %d\n", ret
);
225 ret
= _memicmp_l(s1
, s2
, 3, NULL
);
226 ok(ret
== -1, "got %d\n", ret
);
230 static void test___strncnt(void)
248 for (i
= 0; i
< ARRAY_SIZE(strncnt_tests
); ++i
)
250 ret
= __strncnt(strncnt_tests
[i
].str
, strncnt_tests
[i
].size
);
251 ok(ret
== strncnt_tests
[i
].ret
, "%u: unexpected return value %u.\n", i
, (int)ret
);
256 ret
= __strncnt(NULL
, 0);
257 ret
= __strncnt(NULL
, 1);
261 static void test_C_locale(void)
266 static const char *locales
[] = { NULL
, "C" };
268 /* C locale only converts case for [a-zA-Z] */
269 setlocale(LC_ALL
, "C");
270 for (i
= 0; i
<= 0xffff; i
++)
273 if (i
>= 'A' && i
<= 'Z')
276 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
279 ok(ret
== i
, "expected self %x, got %x for C locale\n", i
, ret
);
283 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
286 if (i
>= 'A' && i
<= 'Z')
289 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
292 ok(ret
== i
, "expected self %x, got %x for C locale\n", i
, ret
);
295 if (i
>= 'A' && i
<= 'Z')
298 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
301 ok(ret
== i
, "expected self %x, got %x for C locale\n", i
, ret
);
304 if (i
>= 'a' && i
<= 'z')
307 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
310 ok(ret
== i
, "expected self %x, got %x for C locale\n", i
, ret
);
314 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
317 if (i
>= 'a' && i
<= 'z')
320 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
323 ok(ret
== i
, "expected self %x, got %x for C locale\n", i
, ret
);
326 if (i
>= 'a' && i
<= 'z')
329 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
332 ok(ret
== i
, "expected self %x, got %x for C locale\n", i
, ret
);
335 for (i
= 0; i
< ARRAY_SIZE(locales
); i
++) {
336 locale
= locales
[i
] ? _create_locale(LC_ALL
, locales
[i
]) : NULL
;
338 for (j
= 0; j
<= 0xffff; j
++) {
339 ret
= _towlower_l(j
, locale
);
340 if (j
>= 'A' && j
<= 'Z')
343 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
346 ok(ret
== j
, "expected self %x, got %x for C locale\n", j
, ret
);
348 ret
= _towupper_l(j
, locale
);
349 if (j
>= 'a' && j
<= 'z')
352 ok(ret
== exp
, "expected %x, got %x for C locale\n", exp
, ret
);
355 ok(ret
== j
, "expected self %x, got %x for C locale\n", j
, ret
);
358 _free_locale(locale
);
362 static void test_mbsspn( void)
364 unsigned char str1
[] = "cabernet";
365 unsigned char str2
[] = "shiraz";
366 unsigned char set
[] = "abc";
367 unsigned char empty
[] = "";
368 unsigned char mbstr
[] = " 2019\x94\x4e" "6\x8c\x8e" "29\x93\xfa";
369 unsigned char mbset1
[] = "0123456789 \x94\x4e";
370 unsigned char mbset2
[] = " \x94\x4e\x8c\x8e";
371 unsigned char mbset3
[] = "\x8e";
372 int ret
, cp
= _getmbcp();
374 ret
= _mbsspn(str1
, set
);
375 ok(ret
== 3, "_mbsspn returns %d should be 3\n", ret
);
376 ret
= _mbsspn(str2
, set
);
377 ok(ret
== 0, "_mbsspn returns %d should be 0\n", ret
);
378 ret
= _mbsspn(str1
, empty
);
379 ok(ret
== 0, "_mbsspn returns %d should be 0\n", ret
);
382 ret
= _mbsspn(mbstr
, mbset1
);
383 ok(ret
== 8, "_mbsspn returns %d should be 8\n", ret
);
384 ret
= _mbsspn(mbstr
, mbset2
);
385 ok(ret
== 1, "_mbsspn returns %d should be 1\n", ret
);
386 ret
= _mbsspn(mbstr
+8, mbset1
);
387 ok(ret
== 0, "_mbsspn returns %d should be 0\n", ret
);
388 ret
= _mbsspn(mbstr
+8, mbset2
);
389 ok(ret
== 2, "_mbsspn returns %d should be 2\n", ret
);
390 ret
= _mbsspn(mbstr
, mbset3
);
391 ok(ret
== 14, "_mbsspn returns %d should be 14\n", ret
);
396 static void test_wcstok(void)
398 static const wchar_t *input
= L
"two words";
404 wcscpy(buffer
, input
);
405 token
= wcstok(buffer
, L
" ", &next
);
406 ok(!wcscmp(L
"two", token
), "expected \"two\", got \"%ls\"\n", token
);
407 ok(next
== token
+ 4, "expected %p, got %p\n", token
+ 4, next
);
408 token
= wcstok(NULL
, L
" ", &next
);
409 ok(!wcscmp(L
"words", token
), "expected \"words\", got \"%ls\"\n", token
);
410 ok(next
== token
+ 5, "expected %p, got %p\n", token
+ 5, next
);
411 token
= wcstok(NULL
, L
" ", &next
);
412 ok(!token
, "expected NULL, got %p\n", token
);
414 wcscpy(buffer
, input
);
415 token
= wcstok(buffer
, L
" ", NULL
);
416 ok(!wcscmp(L
"two", token
), "expected \"two\", got \"%ls\"\n", token
);
417 token
= wcstok(NULL
, L
" ", NULL
);
418 ok(!wcscmp(L
"words", token
), "expected \"words\", got \"%ls\"\n", token
);
419 token
= wcstok(NULL
, L
" ", NULL
);
420 ok(!token
, "expected NULL, got %p\n", token
);
423 static void test__strnicmp(void)
425 static const char str1
[] = "TEST";
426 static const char str2
[] = "test";
429 SET_EXPECT(invalid_parameter_handler
);
431 ret
= _strnicmp(str1
, str2
, -1);
432 todo_wine
CHECK_CALLED(invalid_parameter_handler
);
433 todo_wine
ok(ret
== _NLSCMPERROR
, "got %d.\n", ret
);
434 todo_wine
ok(errno
== EINVAL
, "Unexpected errno %d.\n", errno
);
436 ret
= _strnicmp(str1
, str2
, 0x7fffffff);
437 ok(!ret
, "got %d.\n", ret
);
440 static void test_SpecialCasing(void)
451 struct test ucases
[] = {
452 {"English", 'I', 'i'}, /* LATIN CAPITAL LETTER I */
453 {"English", 0x0130}, /* LATIN CAPITAL LETTER I WITH DOT ABOVE */
455 {"Turkish", 'I', 'i'}, /* LATIN CAPITAL LETTER I */
456 {"Turkish", 0x0130}, /* LATIN CAPITAL LETTER I WITH DOT ABOVE */
458 struct test lcases
[] = {
459 {"English", 'i', 'I'}, /* LATIN SMALL LETTER I */
460 {"English", 0x0131}, /* LATIN SMALL LETTER DOTLESS I */
462 {"Turkish", 'i', 'I'}, /* LATIN SMALL LETTER I */
463 {"Turkish", 0x0131}, /* LATIN SMALL LETTER DOTLESS I */
466 for (i
= 0; i
< ARRAY_SIZE(ucases
); i
++) {
467 if (!setlocale(LC_ALL
, ucases
[i
].lang
)) {
468 win_skip("skipping special case tests for %s\n", ucases
[i
].lang
);
472 ret
= towlower(ucases
[i
].ch
);
473 exp
= ucases
[i
].exp
? ucases
[i
].exp
: ucases
[i
].ch
;
474 ok(ret
== exp
, "expected lowercase %x, got %x for locale %s\n", exp
, ret
, ucases
[i
].lang
);
477 for (i
= 0; i
< ARRAY_SIZE(lcases
); i
++) {
478 if (!setlocale(LC_ALL
, lcases
[i
].lang
)) {
479 win_skip("skipping special case tests for %s\n", lcases
[i
].lang
);
483 ret
= towupper(lcases
[i
].ch
);
484 exp
= lcases
[i
].exp
? lcases
[i
].exp
: lcases
[i
].ch
;
485 ok(ret
== exp
, "expected uppercase %x, got %x for locale %s\n", exp
, ret
, lcases
[i
].lang
);
488 setlocale(LC_ALL
, "C");
490 /* test _towlower_l creating locale */
491 for (i
= 0; i
< ARRAY_SIZE(ucases
); i
++) {
492 if (!(locale
= _create_locale(LC_ALL
, ucases
[i
].lang
))) {
493 win_skip("locale %s not available. skipping\n", ucases
[i
].lang
);
497 ret
= _towlower_l(ucases
[i
].ch
, locale
);
498 exp
= ucases
[i
].exp
? ucases
[i
].exp
: ucases
[i
].ch
;
499 ok(ret
== exp
, "expected lowercase %x, got %x for locale %s\n", exp
, ret
, ucases
[i
].lang
);
501 _free_locale(locale
);
504 /* test _towupper_l creating locale */
505 for (i
= 0; i
< ARRAY_SIZE(lcases
); i
++) {
506 if (!(locale
= _create_locale(LC_ALL
, lcases
[i
].lang
))) {
507 win_skip("locale %s not available. skipping\n", lcases
[i
].lang
);
511 ret
= _towupper_l(lcases
[i
].ch
, locale
);
512 exp
= lcases
[i
].exp
? lcases
[i
].exp
: lcases
[i
].ch
;
513 ok(ret
== exp
, "expected uppercase %x, got %x for locale %s\n", exp
, ret
, lcases
[i
].lang
);
515 _free_locale(locale
);
519 static void test__mbbtype_l(void)
524 _setmbcp(_MB_CP_LOCALE
);
525 for (c
= 0; c
< 256; ++c
)
527 expected
= _mbbtype(c
, 0);
528 ret
= _mbbtype_l(c
, 0, NULL
);
529 ok(ret
== expected
, "c %#x, got ret %#x, expected %#x.\n", c
, ret
, expected
);
531 expected
= _mbbtype(c
, 1);
532 ret
= _mbbtype_l(c
, 1, NULL
);
533 ok(ret
== expected
, "c %#x, got ret %#x, expected %#x.\n", c
, ret
, expected
);
539 ok(_set_invalid_parameter_handler(test_invalid_parameter_handler
) == NULL
,
540 "Invalid parameter handler was already set\n");
550 test_SpecialCasing();