2 * Copyright (C) 2007 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Test for nan(), nanf(), and nanl(). We also test that strtod("nan(...)")
29 * and sscanf("nan(...)", ...) work identically.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
45 testnan(const char *nan_format)
54 snprintf(nan_str, sizeof(nan_str), "nan(%s)", nan_format);
55 for (i = 0; i < 4; i++) {
57 * x86 has an 80-bit long double stored in 96 bits,
58 * so we need to initialize the memory for the memcmp()
59 * checks below to work.
61 bzero(&af[i], sizeof(float));
62 bzero(&ad[i], sizeof(double));
63 bzero(&ald[i], sizeof(long double));
67 af[0] = nanf(nan_format);
69 af[1] = strtof(nan_str, &end);
70 assert(end == nan_str + strlen(nan_str));
71 assert(sscanf(nan_str, "%e", &af[2]) == 1);
72 assert(memcmp(&af[0], &af[1], sizeof(float)) == 0);
73 assert(memcmp(&af[1], &af[2], sizeof(float)) == 0);
74 if (*nan_format == '\0') {
75 /* nanf("") == strtof("nan") */
76 af[3] = strtof("nan", NULL);
77 assert(memcmp(&af[2], &af[3], sizeof(float)) == 0);
80 ad[0] = nan(nan_format);
82 ad[1] = strtod(nan_str, &end);
83 assert(end == nan_str + strlen(nan_str));
84 assert(sscanf(nan_str, "%le", &ad[2]) == 1);
85 assert(memcmp(&ad[0], &ad[1], sizeof(double)) == 0);
86 assert(memcmp(&ad[1], &ad[2], sizeof(double)) == 0);
87 if (*nan_format == '\0') {
88 /* nan("") == strtod("nan") */
89 ad[3] = strtod("nan", NULL);
90 assert(memcmp(&ad[2], &ad[3], sizeof(double)) == 0);
93 ald[0] = nanl(nan_format);
94 assert(isnan(ald[0]));
95 ald[1] = strtold(nan_str, &end);
96 assert(end == nan_str + strlen(nan_str));
97 assert(sscanf(nan_str, "%Le", &ald[2]) == 1);
98 assert(memcmp(&ald[0], &ald[1], sizeof(long double)) == 0);
99 assert(memcmp(&ald[1], &ald[2], sizeof(long double)) == 0);
100 if (*nan_format == '\0') {
101 /* nanl("") == strtold("nan") */
102 ald[3] = strtold("nan", NULL);
103 assert(memcmp(&ald[2], &ald[3], sizeof(long double)) == 0);
108 main(int argc, char *argv[])
113 /* Die if a signalling NaN is returned */
114 feenableexcept(FE_INVALID);
119 printf("ok 1 - nan\n");