ucrtbase: Make the strtod implementation C99 compatible.

Add a test for ucrtbase that verifies these aspects of the C99
behaviour of strtod.

Signed-off-by: Martin Storsjo <martin@martin.st>
Signed-off-by: Piotr Caban <piotr@codeweavers.com>
This commit is contained in:
Martin Storsjo 2015-09-29 14:40:12 +03:00 committed by Alexandre Julliard
parent 2f9987fd04
commit 8578702e06
5 changed files with 186 additions and 10 deletions

1
configure vendored
View File

@ -17786,6 +17786,7 @@ wine_fn_config_dll twain_32 enable_twain_32
wine_fn_config_test dlls/twain_32/tests twain_32_test wine_fn_config_test dlls/twain_32/tests twain_32_test
wine_fn_config_dll typelib.dll16 enable_win16 wine_fn_config_dll typelib.dll16 enable_win16
wine_fn_config_dll ucrtbase enable_ucrtbase wine_fn_config_dll ucrtbase enable_ucrtbase
wine_fn_config_test dlls/ucrtbase/tests ucrtbase_test
wine_fn_config_dll unicows enable_unicows implib wine_fn_config_dll unicows enable_unicows implib
wine_fn_config_dll updspapi enable_updspapi wine_fn_config_dll updspapi enable_updspapi
wine_fn_config_dll url enable_url implib wine_fn_config_dll url enable_url implib

View File

@ -3336,6 +3336,7 @@ WINE_CONFIG_DLL(twain_32)
WINE_CONFIG_TEST(dlls/twain_32/tests) WINE_CONFIG_TEST(dlls/twain_32/tests)
WINE_CONFIG_DLL(typelib.dll16,enable_win16) WINE_CONFIG_DLL(typelib.dll16,enable_win16)
WINE_CONFIG_DLL(ucrtbase) WINE_CONFIG_DLL(ucrtbase)
WINE_CONFIG_TEST(dlls/ucrtbase/tests)
WINE_CONFIG_DLL(unicows,,[implib]) WINE_CONFIG_DLL(unicows,,[implib])
WINE_CONFIG_DLL(updspapi) WINE_CONFIG_DLL(updspapi)
WINE_CONFIG_DLL(url,,[implib]) WINE_CONFIG_DLL(url,,[implib])

View File

@ -314,6 +314,7 @@ static double strtod_helper(const char *str, char **end, MSVCRT__locale_t locale
double ret; double ret;
long double lret=1, expcnt = 10; long double lret=1, expcnt = 10;
BOOL found_digit = FALSE, negexp; BOOL found_digit = FALSE, negexp;
int base = 10;
if(err) if(err)
*err = 0; *err = 0;
@ -339,16 +340,50 @@ static double strtod_helper(const char *str, char **end, MSVCRT__locale_t locale
} else if(*p == '+') } else if(*p == '+')
p++; p++;
while(isdigit(*p)) { #if _MSVCR_VER >= 140
if(tolower(p[0]) == 'i' && tolower(p[1]) == 'n' && tolower(p[2]) == 'f') {
if(end)
*end = (char*) &p[3];
if(tolower(p[3]) == 'i' && tolower(p[4]) == 'n' && tolower(p[5]) == 'i' &&
tolower(p[6]) == 't' && tolower(p[7]) == 'y' && end)
*end = (char*) &p[8];
return sign*INFINITY;
}
if(tolower(p[0]) == 'n' &&
tolower(p[1]) == 'a' &&
tolower(p[2]) == 'n') {
if(end)
*end = (char*) &p[3];
return NAN;
}
if(p[0] == '0' && tolower(p[1]) == 'x') {
base = 16;
expcnt = 2;
p += 2;
}
#endif
while(isdigit(*p) ||
(base == 16 && ((*p >= 'a' && *p <= 'f') || (*p >= 'A' && *p <= 'F')))) {
char c = *p++;
int val;
found_digit = TRUE; found_digit = TRUE;
hlp = d*10+*(p++)-'0'; if (isdigit(c))
if(d>MSVCRT_UI64_MAX/10 || hlp<d) { val = c - '0';
else if (c >= 'a' && c <= 'f')
val = 10 + c - 'a';
else
val = 10 + c - 'A';
hlp = d*base+val;
if(d>MSVCRT_UI64_MAX/base || hlp<d) {
exp++; exp++;
break; break;
} else } else
d = hlp; d = hlp;
} }
while(isdigit(*p)) { while(isdigit(*p) ||
(base == 16 && ((*p >= 'a' && *p <= 'f') || (*p >= 'A' && *p <= 'F')))) {
exp++; exp++;
p++; p++;
} }
@ -356,16 +391,25 @@ static double strtod_helper(const char *str, char **end, MSVCRT__locale_t locale
if(*p == *locinfo->lconv->decimal_point) if(*p == *locinfo->lconv->decimal_point)
p++; p++;
while(isdigit(*p)) { while(isdigit(*p) ||
(base == 16 && ((*p >= 'a' && *p <= 'f') || (*p >= 'A' && *p <= 'F')))) {
char c = *p++;
int val;
found_digit = TRUE; found_digit = TRUE;
hlp = d*10+*(p++)-'0'; if (isdigit(c))
if(d>MSVCRT_UI64_MAX/10 || hlp<d) val = c - '0';
else if (c >= 'a' && c <= 'f')
val = 10 + c - 'a';
else
val = 10 + c - 'A';
hlp = d*base+val;
if(d>MSVCRT_UI64_MAX/base || hlp<d)
break; break;
d = hlp; d = hlp;
exp--; exp--;
} }
while(isdigit(*p)) while(isdigit(*p) ||
(base == 16 && ((*p >= 'a' && *p <= 'f') || (*p >= 'A' && *p <= 'F'))))
p++; p++;
if(!found_digit) { if(!found_digit) {
@ -374,7 +418,11 @@ static double strtod_helper(const char *str, char **end, MSVCRT__locale_t locale
return 0.0; return 0.0;
} }
if(*p=='e' || *p=='E' || *p=='d' || *p=='D') { if(base == 16)
exp *= 4;
if((base == 10 && (*p=='e' || *p=='E' || *p=='d' || *p=='D')) ||
(base == 16 && (*p=='p' || *p=='P'))) {
int e=0, s=1; int e=0, s=1;
p++; p++;

View File

@ -0,0 +1,5 @@
TESTDLL = ucrtbase.dll
APPMODE = -mno-cygwin
C_SRCS = \
string.c

View File

@ -0,0 +1,121 @@
/*
* Copyright 2015 Martin Storsjo
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <wchar.h>
#include <stdio.h>
#include <windef.h>
#include <winbase.h>
#include "wine/test.h"
#include <math.h>
#ifndef INFINITY
static inline float __port_infinity(void)
{
static const unsigned __inf_bytes = 0x7f800000;
return *(const float *)&__inf_bytes;
}
#define INFINITY __port_infinity()
#endif
#ifndef NAN
static inline float __port_nan(void)
{
static const unsigned __nan_bytes = 0x7fc00000;
return *(const float *)&__nan_bytes;
}
#define NAN __port_nan()
#endif
static double (CDECL *p_strtod)(const char*, char** end);
static BOOL init(void)
{
HMODULE module;
module = LoadLibraryA("ucrtbase.dll");
if (!module)
{
win_skip("ucrtbase.dll not installed\n");
return FALSE;
}
p_strtod = (void*)GetProcAddress(module, "strtod");
return TRUE;
}
static BOOL local_isnan(double d)
{
return d != d;
}
#define test_strtod_str(string, value, length) _test_strtod_str(__LINE__, string, value, length)
static void _test_strtod_str(int line, const char* string, double value, int length)
{
char *end;
double d;
d = p_strtod(string, &end);
if (local_isnan(value))
ok_(__FILE__, line)(local_isnan(d), "d = %lf (\"%s\")\n", d, string);
else
ok_(__FILE__, line)(d == value, "d = %lf (\"%s\")\n", d, string);
ok_(__FILE__, line)(end == string + length, "incorrect end (%d, \"%s\")\n", (int)(end - string), string);
}
static void test_strtod(void)
{
test_strtod_str("infinity", INFINITY, 8);
test_strtod_str("INFINITY", INFINITY, 8);
test_strtod_str("InFiNiTy", INFINITY, 8);
test_strtod_str("INF", INFINITY, 3);
test_strtod_str("-inf", -INFINITY, 4);
test_strtod_str("inf42", INFINITY, 3);
test_strtod_str("inffoo", INFINITY, 3);
test_strtod_str("infini", INFINITY, 3);
test_strtod_str("NAN", NAN, 3);
test_strtod_str("nan", NAN, 3);
test_strtod_str("NaN", NAN, 3);
test_strtod_str("0x42", 66, 4);
test_strtod_str("0X42", 66, 4);
test_strtod_str("-0x42", -66, 5);
test_strtod_str("0x1p1", 2, 5);
test_strtod_str("0x1P1", 2, 5);
test_strtod_str("0x1p+1", 2, 6);
test_strtod_str("0x2p-1", 1, 6);
test_strtod_str("0xA", 10, 3);
test_strtod_str("0xa", 10, 3);
test_strtod_str("0xABCDEF", 11259375, 8);
test_strtod_str("0Xabcdef", 11259375, 8);
test_strtod_str("0x1.1", 1.0625, 5);
test_strtod_str("0x1.1p1", 2.125, 7);
test_strtod_str("0x1.A", 1.625, 5);
test_strtod_str("0x1p1a", 2, 5);
}
START_TEST(string)
{
if (!init()) return;
test_strtod();
}