Module Name:    src
Committed By:   rillig
Date:           Sat Jun 15 12:20:22 UTC 2024

Modified Files:
        src/tests/lib/libc/stdlib: t_strtod.c

Log Message:
tests/strtold: test a few simple examples


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/tests/lib/libc/stdlib/t_strtod.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/lib/libc/stdlib/t_strtod.c
diff -u src/tests/lib/libc/stdlib/t_strtod.c:1.36 src/tests/lib/libc/stdlib/t_strtod.c:1.37
--- src/tests/lib/libc/stdlib/t_strtod.c:1.36	Mon May  6 18:39:36 2024
+++ src/tests/lib/libc/stdlib/t_strtod.c	Sat Jun 15 12:20:22 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: t_strtod.c,v 1.36 2024/05/06 18:39:36 riastradh Exp $ */
+/*	$NetBSD: t_strtod.c,v 1.37 2024/06/15 12:20:22 rillig Exp $ */
 
 /*-
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -32,9 +32,11 @@
 /* Public domain, Otto Moerbeek <o...@drijf.net>, 2006. */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_strtod.c,v 1.36 2024/05/06 18:39:36 riastradh Exp $");
+__RCSID("$NetBSD: t_strtod.c,v 1.37 2024/06/15 12:20:22 rillig Exp $");
 
 #include <errno.h>
+#include <fenv.h>
+#include <float.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -42,8 +44,6 @@ __RCSID("$NetBSD: t_strtod.c,v 1.36 2024
 
 #include <atf-c.h>
 
-#include <fenv.h>
-
 static const char * const inf_strings[] =
     { "Inf", "INF", "-Inf", "-INF", "Infinity", "+Infinity",
       "INFINITY", "-INFINITY", "InFiNiTy", "+InFiNiTy" };
@@ -73,6 +73,38 @@ ATF_TC_BODY(strtod_basic, tc)
 	}
 }
 
+ATF_TC(strtold_basic);
+ATF_TC_HEAD(strtold_basic, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Some examples of strtold(3)");
+}
+
+ATF_TC_BODY(strtold_basic, tc)
+{
+	static const struct {
+		const char *str;
+		long double val;
+	} testcases[] = {
+		{ "0x0p0", 0.0L },
+		{ "0x1.234p0", 0x1234 / 4096.0L },
+		{ "0x1.234p0", 0x1234 / 4096.0L },
+#if FLT_RADIX == 2 && LDBL_MAX_EXP == 16384 && LDBL_MANT_DIG == 64
+		{ "2.16", 0x8.a3d70a3d70a3d71p-2L },
+#endif
+	};
+
+	for (size_t i = 0, n = __arraycount(testcases); i < n; i++) {
+		char *end;
+		errno = 0;
+		long double val = strtold(testcases[i].str, &end);
+
+		ATF_CHECK_MSG(
+		    errno == 0 && *end == '\0' && val == testcases[i].val,
+		    "'%s' want %La have %La errno %d end '%s'",
+		    testcases[i].str, testcases[i].val, val, errno, end);
+	}
+}
+
 ATF_TC(strtod_hex);
 ATF_TC_HEAD(strtod_hex, tc)
 {
@@ -306,6 +338,7 @@ ATF_TP_ADD_TCS(tp)
 {
 
 	ATF_TP_ADD_TC(tp, strtod_basic);
+	ATF_TP_ADD_TC(tp, strtold_basic);
 	ATF_TP_ADD_TC(tp, strtod_hex);
 	ATF_TP_ADD_TC(tp, strtod_inf);
 	ATF_TP_ADD_TC(tp, strtof_inf);

Reply via email to