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