From cf18e2de01c53229a284f4c97fb01ae01ff492aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Sat, 5 Jun 2021 00:35:16 +0300 Subject: [PATCH] test: Add tests for strtod/strtof/strtold/wcstod/wcstof/wcstold --- test/crt-test.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/crt-test.c b/test/crt-test.c index 6db3718..09d38aa 100644 --- a/test/crt-test.c +++ b/test/crt-test.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef _WIN32 #include #endif @@ -256,6 +257,73 @@ int main(int argc, char* argv[]) { TEST_INT(strtol("foo", NULL, 100), 0); TEST_INT(errno, EINVAL); +#define TEST_STRTOD(strtod, type, prefix) do { \ + const type *curstr = prefix ## "1.e5z"; \ + type *end; \ + errno = 0; \ + TEST_FLT(strtod(curstr, &end), 1.e5); \ + TEST_INT(errno, 0); \ + TEST_INT((int)(end - curstr), 4); \ + curstr = prefix ## "0x125p-1z"; \ + errno = 0; \ + TEST_FLT(strtod(curstr, &end), 146.5); \ + TEST_INT(errno, 0); \ + TEST_INT((int)(end - curstr), 8); \ + } while (0) + + TEST_STRTOD(strtod, char, ); + TEST_STRTOD(strtof, char, ); + TEST_STRTOD(strtold, char, ); + TEST_STRTOD(wcstod, wchar_t, L); + TEST_STRTOD(wcstof, wchar_t, L); + TEST_STRTOD(wcstold, wchar_t, L); + +#define TEST_STRTOD_VALUE(strtod, prefix, val, expect) \ + errno = 0; \ + TEST_FLT_SIGN(strtod(prefix ## #val, NULL), expect); \ + TEST_INT(errno, 0) + +#define TEST_STRTOD_RANGE_EXPECT(strtod, prefix, val, expect) \ + errno = 0; \ + TEST_FLT_SIGN(strtod(prefix ## #val, NULL), expect); \ + TEST_INT(errno, ERANGE) + +#define TEST_STRTOD_STRTOF_RANGE_EXPECT(strtod, strtof, prefix, val, expect) \ + TEST_STRTOD_VALUE(strtod, prefix, val, val); \ + TEST_STRTOD_RANGE_EXPECT(strtof, prefix, val, expect) + +#define TEST_STRTOF_32B_RANGE(strtod, strtof, prefix) \ + TEST_STRTOD_STRTOF_RANGE_EXPECT(strtod, strtof, prefix, 1.e40, HUGE_VALF); \ + TEST_STRTOD_STRTOF_RANGE_EXPECT(strtod, strtof, prefix, -1.e40, -HUGE_VALF); \ + TEST_STRTOD_STRTOF_RANGE_EXPECT(strtod, strtof, prefix, 1.e-60, 0.0); \ + TEST_STRTOD_STRTOF_RANGE_EXPECT(strtod, strtof, prefix, -1.e-60, -0.0) + + TEST_STRTOF_32B_RANGE(strtod, strtof, ); + TEST_STRTOF_32B_RANGE(wcstod, wcstof, L); + +#define TEST_STRTOD_64B_RANGE(strtod, prefix) \ + TEST_STRTOD_RANGE_EXPECT(strtod, prefix, 1.e310, HUGE_VAL); \ + TEST_STRTOD_RANGE_EXPECT(strtod, prefix, -1.e310, -HUGE_VAL); \ + TEST_STRTOD_RANGE_EXPECT(strtod, prefix, 1.e-400, 0.0); \ + TEST_STRTOD_RANGE_EXPECT(strtod, prefix, -1.e-400, -0.0) + + TEST_STRTOD_64B_RANGE(strtod, ); + TEST_STRTOD_64B_RANGE(wcstod, L); + +#if !defined(_WIN32) || (defined(__MINGW32__) && (defined(__i386__) || defined(__x86_64__))) +#define TEST_STRTOLD_80B_RANGE(strtold, prefix) \ + TEST_STRTOD_VALUE(strtold, prefix, 1.e310, 1e310L); \ + TEST_STRTOD_VALUE(strtold, prefix, -1.e310, -1e310L); \ + TEST_STRTOD_VALUE(strtold, prefix, 1.e-310, 1e-310L); \ + TEST_STRTOD_VALUE(strtold, prefix, -1.e-310, -1e-310L) + + TEST_STRTOLD_80B_RANGE(strtold, ); + TEST_STRTOLD_80B_RANGE(wcstold, L); +#else + TEST_STRTOD_64B_RANGE(strtold, ); + TEST_STRTOD_64B_RANGE(wcstold, L); +#endif + int env_ok = 0; putenv("CRT_TEST_VAR=1"); for (char **ptr = environ; *ptr; ptr++)