xref: /openbsd-src/regress/lib/libm/msun/nan_test.c (revision 498c7b5e98c82ca5a0055f676f2a6e4214e2d9a8)
1*498c7b5eSderaadt /*	$OpenBSD: nan_test.c,v 1.3 2021/12/13 18:04:28 deraadt Exp $	*/
2c36e572eSmbuhl /*-
3c36e572eSmbuhl  * Copyright (C) 2007 David Schultz <das@FreeBSD.org>
4c36e572eSmbuhl  * All rights reserved.
5c36e572eSmbuhl  *
6c36e572eSmbuhl  * Redistribution and use in source and binary forms, with or without
7c36e572eSmbuhl  * modification, are permitted provided that the following conditions
8c36e572eSmbuhl  * are met:
9c36e572eSmbuhl  * 1. Redistributions of source code must retain the above copyright
10c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer.
11c36e572eSmbuhl  * 2. Redistributions in binary form must reproduce the above copyright
12c36e572eSmbuhl  *    notice, this list of conditions and the following disclaimer in the
13c36e572eSmbuhl  *    documentation and/or other materials provided with the distribution.
14c36e572eSmbuhl  *
15c36e572eSmbuhl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c36e572eSmbuhl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c36e572eSmbuhl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c36e572eSmbuhl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c36e572eSmbuhl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c36e572eSmbuhl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c36e572eSmbuhl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c36e572eSmbuhl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c36e572eSmbuhl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c36e572eSmbuhl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c36e572eSmbuhl  * SUCH DAMAGE.
26c36e572eSmbuhl  */
27c36e572eSmbuhl 
28c36e572eSmbuhl #include "macros.h"
29c36e572eSmbuhl 
30c36e572eSmbuhl /*
31c36e572eSmbuhl  * Test for nan(), nanf(), and nanl(). We also test that strtod("nan(...)")
32c36e572eSmbuhl  * and sscanf("nan(...)", ...) work identically.
33c36e572eSmbuhl  */
34c36e572eSmbuhl 
3549a6e16fSderaadt #include <sys/types.h>
36c36e572eSmbuhl #include <fenv.h>
37c36e572eSmbuhl #include <float.h>
38c36e572eSmbuhl #include <locale.h>
39c36e572eSmbuhl #include <math.h>
40c36e572eSmbuhl #include <stdio.h>
41c36e572eSmbuhl #include <stdlib.h>
42c36e572eSmbuhl #include <string.h>
43c36e572eSmbuhl 
44c36e572eSmbuhl #include "test-utils.h"
45c36e572eSmbuhl 
46c36e572eSmbuhl static void
testnan(const char * nan_format)47c36e572eSmbuhl testnan(const char *nan_format)
48c36e572eSmbuhl {
49c36e572eSmbuhl 	char nan_str[128];
50c36e572eSmbuhl 	char *end;
51c36e572eSmbuhl 	long double ald[4];
52c36e572eSmbuhl 	double ad[4];
53c36e572eSmbuhl 	float af[4];
54c36e572eSmbuhl 	unsigned i;
55c36e572eSmbuhl 
56c36e572eSmbuhl 	snprintf(nan_str, sizeof(nan_str), "nan(%s)", nan_format);
57c36e572eSmbuhl 	for (i = 0; i < nitems(ad); i++) {
58c36e572eSmbuhl 		/*
59c36e572eSmbuhl 		 * x86 has an 80-bit long double stored in 96 bits,
60c36e572eSmbuhl 		 * so we need to initialize the memory for the memcmp()
61c36e572eSmbuhl 		 * checks below to work.
62c36e572eSmbuhl 		 */
63c36e572eSmbuhl 		bzero(&af[i], sizeof(float));
64c36e572eSmbuhl 		bzero(&ad[i], sizeof(double));
65c36e572eSmbuhl 		bzero(&ald[i], sizeof(long double));
66c36e572eSmbuhl 	}
67c36e572eSmbuhl 
68c36e572eSmbuhl 	af[0] = nanf(nan_format);
69c36e572eSmbuhl 	ATF_REQUIRE(isnan(af[0]));
70c36e572eSmbuhl 	af[1] = strtof(nan_str, &end);
71c36e572eSmbuhl 	ATF_REQUIRE(end == nan_str + strlen(nan_str));
72c36e572eSmbuhl 	ATF_REQUIRE(sscanf(nan_str, "%e", &af[2]) == 1);
73c36e572eSmbuhl 	ATF_REQUIRE(memcmp(&af[0], &af[1], sizeof(float)) == 0);
74c36e572eSmbuhl 	ATF_REQUIRE(memcmp(&af[1], &af[2], sizeof(float)) == 0);
75c36e572eSmbuhl 	if (*nan_format == '\0') {
76c36e572eSmbuhl 		/* nanf("") == strtof("nan") */
77c36e572eSmbuhl 		af[3] = strtof("nan", NULL);
78c36e572eSmbuhl 		ATF_REQUIRE(memcmp(&af[2], &af[3], sizeof(float)) == 0);
79c36e572eSmbuhl 	}
80c36e572eSmbuhl 
81c36e572eSmbuhl 	ad[0] = nan(nan_format);
82c36e572eSmbuhl 	ATF_REQUIRE(isnan(ad[0]));
83c36e572eSmbuhl 	ad[1] = strtod(nan_str, &end);
84c36e572eSmbuhl 	ATF_REQUIRE(end == nan_str + strlen(nan_str));
85c36e572eSmbuhl 	ATF_REQUIRE(sscanf(nan_str, "%le", &ad[2]) == 1);
86c36e572eSmbuhl 	ATF_REQUIRE(memcmp(&ad[0], &ad[1], sizeof(double)) == 0);
87c36e572eSmbuhl 	ATF_REQUIRE(memcmp(&ad[1], &ad[2], sizeof(double)) == 0);
88c36e572eSmbuhl 	if (*nan_format == '\0') {
89c36e572eSmbuhl 		/* nan("") == strtod("nan") */
90c36e572eSmbuhl 		ad[3] = strtod("nan", NULL);
91c36e572eSmbuhl 		ATF_REQUIRE(memcmp(&ad[2], &ad[3], sizeof(double)) == 0);
92c36e572eSmbuhl 	}
93c36e572eSmbuhl 
94c36e572eSmbuhl 	ald[0] = nanl(nan_format);
95c36e572eSmbuhl 	ATF_REQUIRE(isnan(ald[0]));
96c36e572eSmbuhl 	ald[1] = strtold(nan_str, &end);
97c36e572eSmbuhl 	ATF_REQUIRE(end == nan_str + strlen(nan_str));
98c36e572eSmbuhl 	ATF_REQUIRE(sscanf(nan_str, "%Le", &ald[2]) == 1);
99c36e572eSmbuhl 	ATF_REQUIRE(memcmp(&ald[0], &ald[1], sizeof(long double)) == 0);
100c36e572eSmbuhl 	ATF_REQUIRE(memcmp(&ald[1], &ald[2], sizeof(long double)) == 0);
101c36e572eSmbuhl 	if (*nan_format == '\0') {
102c36e572eSmbuhl 		/* nanl("") == strtold("nan") */
103c36e572eSmbuhl 		ald[3] = strtold("nan", NULL);
104c36e572eSmbuhl 		ATF_REQUIRE(memcmp(&ald[2], &ald[3], sizeof(long double)) == 0);
105c36e572eSmbuhl 	}
106c36e572eSmbuhl }
107c36e572eSmbuhl 
108c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(nan);
ATF_TC_BODY(nan,tc)109c36e572eSmbuhl ATF_TC_BODY(nan, tc)
110c36e572eSmbuhl {
111c36e572eSmbuhl 	/* Die if a signalling NaN is returned */
112c36e572eSmbuhl 	feenableexcept(FE_INVALID);
113c36e572eSmbuhl 
114c36e572eSmbuhl 	testnan("0x1234");
115c36e572eSmbuhl 	testnan("");
116c36e572eSmbuhl }
117c36e572eSmbuhl 
ATF_TP_ADD_TCS(tp)118c36e572eSmbuhl ATF_TP_ADD_TCS(tp)
119c36e572eSmbuhl {
120c36e572eSmbuhl 	ATF_TP_ADD_TC(tp, nan);
121c36e572eSmbuhl 
122c36e572eSmbuhl 	return (atf_no_error());
123c36e572eSmbuhl }
124