Add more tests of strtod end pointer

Message ID 79aa8aa0-fca7-9024-ef7-5e2f744cdd9f@redhat.com
State Committed
Commit b5d3737b305525315e0c7c93ca49eadc868eabd5
Headers
Series Add more tests of strtod end pointer |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch fail Patch failed to apply to master at the time it was sent
redhat-pt-bot/TryBot-32bit fail Patch series failed to apply

Commit Message

Joseph Myers Aug. 14, 2024, 7:58 p.m. UTC
  Although there are some tests in tst-strtod2 and tst-strtod3 for the
end pointer provided by strtod when it doesn't parse the whole string,
they aren't very thorough.  Add tests of more such cases to
tst-strtod2.

Tested for x86_64.

---

This patch is relative to a tree with
<https://sourceware.org/pipermail/libc-alpha/2024-August/159183.html>
(pending review) applied.
  

Comments

Joseph Myers Aug. 20, 2024, 7:32 p.m. UTC | #1
Ping.  This patch 
<https://sourceware.org/pipermail/libc-alpha/2024-August/159209.html> is 
pending review.
  
Joseph Myers Aug. 30, 2024, 1:05 p.m. UTC | #2
Ping^2.  This patch 
<https://sourceware.org/pipermail/libc-alpha/2024-August/159209.html> is 
still pending review.
  
Joseph Myers Sept. 6, 2024, 7:32 p.m. UTC | #3
Ping^3.  This patch 
<https://sourceware.org/pipermail/libc-alpha/2024-August/159209.html> is 
still pending review.
  
Carlos O'Donell Sept. 6, 2024, 8:07 p.m. UTC | #4
On 8/14/24 3:58 PM, Joseph Myers wrote:
> Although there are some tests in tst-strtod2 and tst-strtod3 for the
> end pointer provided by strtod when it doesn't parse the whole string,
> they aren't very thorough.  Add tests of more such cases to
> tst-strtod2.

Thanks for the extra cases.

LGTM.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> Tested for x86_64.
> 
> ---
> 
> This patch is relative to a tree with
> <https://sourceware.org/pipermail/libc-alpha/2024-August/159183.html>
> (pending review) applied.
> 
> diff --git a/stdlib/tst-strtod2.c b/stdlib/tst-strtod2.c
> index 2cb0953fa9..c84bd792c1 100644
> --- a/stdlib/tst-strtod2.c
> +++ b/stdlib/tst-strtod2.c
> @@ -1,3 +1,4 @@
> +#include <math.h>

OK. Add the required include.

>  #include <stdio.h>
>  #include <stdlib.h>
>  
> @@ -17,10 +18,46 @@ struct test_strto ## FSUF						\
>    { "0x.0y", 0.0 ## LSUF, 4 },						\
>    { ".y", 0.0 ## LSUF, 0 },						\
>    { "0.y", 0.0 ## LSUF, 2 },						\
> -  { ".0y", 0.0 ## LSUF, 2 }						\
> +  { ".0y", 0.0 ## LSUF, 2 },						\

> +  { "1.0e", 1.0 ## LSUF, 3 },						\
> +  { "1.0e+", 1.0 ## LSUF, 3 },						\
> +  { "1.0e-", 1.0 ## LSUF, 3 },						\
> +  { "1.0ex", 1.0 ## LSUF, 3 },						\
> +  { "1.0e+x", 1.0 ## LSUF, 3 },						\
> +  { "1.0e-x", 1.0 ## LSUF, 3 },						\
> +  { "0x1p", 1.0 ## LSUF, 3 },						\
> +  { "0x1p+", 1.0 ## LSUF, 3 },						\
> +  { "0x1p-", 1.0 ## LSUF, 3 },						\
> +  { "0x1px", 1.0 ## LSUF, 3 },						\
> +  { "0x1p+x", 1.0 ## LSUF, 3 },						\
> +  { "0x1p-x", 1.0 ## LSUF, 3 },						\

OK. Equivalent block for 1.0.

> +  { "INFx", INFINITY, 3 },						\
> +  { "infx", INFINITY, 3 },						\
> +  { "INFINITx", INFINITY, 3 },						\
> +  { "infinitx", INFINITY, 3 },						\

OK. Equivalent block for INFINITY.

> +  { "INFINITYY", INFINITY, 8 },						\
> +  { "infinityy", INFINITY, 8 },						\

OK. Likewise but 8 returned because all of "infinity" is parsed.

> +  { "NANx", NAN, 3 },							\
> +  { "nanx", NAN, 3 },							\
> +  { "NAN(", NAN, 3 },							\
> +  { "nan(", NAN, 3 },							\
> +  { "NAN(x", NAN, 3 },							\
> +  { "nan(x", NAN, 3 },							\

OK. Equivalent block for NAN.

> +  { "NAN(x)y", NAN, 6 },						\
> +  { "nan(x)y", NAN, 6 },						\

OK. Likewise but the whole of (x) is parsed.

> +  { "NAN(*)y", NAN, 3 },						\
> +  { "nan(*)y", NAN, 3 }							\

OK. Equivalent block for NAN.

>  };									\
>  									\
>  static int								\
> +compare_strto ## FSUF (FTYPE x, FTYPE y)				\
> +{									\
> +  if (isnan (x) && isnan (y))						\
> +    return 1;								\
> +  return x == y;							\

OK.

> +}									\
> +									\
> +static int								\
>  test_strto ## FSUF (void)						\
>  {									\
>    int status = 0;							\
> @@ -30,7 +67,7 @@ test_strto ## FSUF (void)						\
>      {									\
>        char *ep;								\
>        FTYPE r = strto ## FSUF (tests_strto ## FSUF[i].str, &ep);	\
> -      if (r != tests_strto ## FSUF[i].result)				\
> +      if (!compare_strto ## FSUF (r, tests_strto ## FSUF[i].result))	\

OK. Introduce comparison with nan checking.

>  	{								\
>  	  char buf1[FSTRLENMAX], buf2[FSTRLENMAX];			\
>  	  FTOSTR (buf1, sizeof (buf1), "%g", r);			\
>
  

Patch

diff --git a/stdlib/tst-strtod2.c b/stdlib/tst-strtod2.c
index 2cb0953fa9..c84bd792c1 100644
--- a/stdlib/tst-strtod2.c
+++ b/stdlib/tst-strtod2.c
@@ -1,3 +1,4 @@ 
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -17,10 +18,46 @@  struct test_strto ## FSUF						\
   { "0x.0y", 0.0 ## LSUF, 4 },						\
   { ".y", 0.0 ## LSUF, 0 },						\
   { "0.y", 0.0 ## LSUF, 2 },						\
-  { ".0y", 0.0 ## LSUF, 2 }						\
+  { ".0y", 0.0 ## LSUF, 2 },						\
+  { "1.0e", 1.0 ## LSUF, 3 },						\
+  { "1.0e+", 1.0 ## LSUF, 3 },						\
+  { "1.0e-", 1.0 ## LSUF, 3 },						\
+  { "1.0ex", 1.0 ## LSUF, 3 },						\
+  { "1.0e+x", 1.0 ## LSUF, 3 },						\
+  { "1.0e-x", 1.0 ## LSUF, 3 },						\
+  { "0x1p", 1.0 ## LSUF, 3 },						\
+  { "0x1p+", 1.0 ## LSUF, 3 },						\
+  { "0x1p-", 1.0 ## LSUF, 3 },						\
+  { "0x1px", 1.0 ## LSUF, 3 },						\
+  { "0x1p+x", 1.0 ## LSUF, 3 },						\
+  { "0x1p-x", 1.0 ## LSUF, 3 },						\
+  { "INFx", INFINITY, 3 },						\
+  { "infx", INFINITY, 3 },						\
+  { "INFINITx", INFINITY, 3 },						\
+  { "infinitx", INFINITY, 3 },						\
+  { "INFINITYY", INFINITY, 8 },						\
+  { "infinityy", INFINITY, 8 },						\
+  { "NANx", NAN, 3 },							\
+  { "nanx", NAN, 3 },							\
+  { "NAN(", NAN, 3 },							\
+  { "nan(", NAN, 3 },							\
+  { "NAN(x", NAN, 3 },							\
+  { "nan(x", NAN, 3 },							\
+  { "NAN(x)y", NAN, 6 },						\
+  { "nan(x)y", NAN, 6 },						\
+  { "NAN(*)y", NAN, 3 },						\
+  { "nan(*)y", NAN, 3 }							\
 };									\
 									\
 static int								\
+compare_strto ## FSUF (FTYPE x, FTYPE y)				\
+{									\
+  if (isnan (x) && isnan (y))						\
+    return 1;								\
+  return x == y;							\
+}									\
+									\
+static int								\
 test_strto ## FSUF (void)						\
 {									\
   int status = 0;							\
@@ -30,7 +67,7 @@  test_strto ## FSUF (void)						\
     {									\
       char *ep;								\
       FTYPE r = strto ## FSUF (tests_strto ## FSUF[i].str, &ep);	\
-      if (r != tests_strto ## FSUF[i].result)				\
+      if (!compare_strto ## FSUF (r, tests_strto ## FSUF[i].result))	\
 	{								\
 	  char buf1[FSTRLENMAX], buf2[FSTRLENMAX];			\
 	  FTOSTR (buf1, sizeof (buf1), "%g", r);			\