14dc607e7SEnji Cooper /*-
24dc607e7SEnji Cooper * Copyright (C) 2007 David Schultz <das@FreeBSD.org>
34dc607e7SEnji Cooper * All rights reserved.
44dc607e7SEnji Cooper *
54dc607e7SEnji Cooper * Redistribution and use in source and binary forms, with or without
64dc607e7SEnji Cooper * modification, are permitted provided that the following conditions
74dc607e7SEnji Cooper * are met:
84dc607e7SEnji Cooper * 1. Redistributions of source code must retain the above copyright
94dc607e7SEnji Cooper * notice, this list of conditions and the following disclaimer.
104dc607e7SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
114dc607e7SEnji Cooper * notice, this list of conditions and the following disclaimer in the
124dc607e7SEnji Cooper * documentation and/or other materials provided with the distribution.
134dc607e7SEnji Cooper *
144dc607e7SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154dc607e7SEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164dc607e7SEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174dc607e7SEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184dc607e7SEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194dc607e7SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204dc607e7SEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214dc607e7SEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224dc607e7SEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234dc607e7SEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244dc607e7SEnji Cooper * SUCH DAMAGE.
254dc607e7SEnji Cooper */
264dc607e7SEnji Cooper
274dc607e7SEnji Cooper /*
284dc607e7SEnji Cooper * Test for nan(), nanf(), and nanl(). We also test that strtod("nan(...)")
294dc607e7SEnji Cooper * and sscanf("nan(...)", ...) work identically.
304dc607e7SEnji Cooper */
314dc607e7SEnji Cooper
32abe427afSEnji Cooper #include <sys/param.h>
334dc607e7SEnji Cooper #include <fenv.h>
344dc607e7SEnji Cooper #include <float.h>
354dc607e7SEnji Cooper #include <locale.h>
364dc607e7SEnji Cooper #include <math.h>
374dc607e7SEnji Cooper #include <stdio.h>
384dc607e7SEnji Cooper #include <stdlib.h>
394dc607e7SEnji Cooper #include <string.h>
404dc607e7SEnji Cooper
41*133bc645SAlex Richardson #include "test-utils.h"
42*133bc645SAlex Richardson
43abe427afSEnji Cooper static void
testnan(const char * nan_format)444dc607e7SEnji Cooper testnan(const char *nan_format)
454dc607e7SEnji Cooper {
464dc607e7SEnji Cooper char nan_str[128];
474dc607e7SEnji Cooper char *end;
484dc607e7SEnji Cooper long double ald[4];
494dc607e7SEnji Cooper double ad[4];
504dc607e7SEnji Cooper float af[4];
51abe427afSEnji Cooper unsigned i;
524dc607e7SEnji Cooper
534dc607e7SEnji Cooper snprintf(nan_str, sizeof(nan_str), "nan(%s)", nan_format);
54abe427afSEnji Cooper for (i = 0; i < nitems(ad); i++) {
554dc607e7SEnji Cooper /*
564dc607e7SEnji Cooper * x86 has an 80-bit long double stored in 96 bits,
574dc607e7SEnji Cooper * so we need to initialize the memory for the memcmp()
584dc607e7SEnji Cooper * checks below to work.
594dc607e7SEnji Cooper */
604dc607e7SEnji Cooper bzero(&af[i], sizeof(float));
614dc607e7SEnji Cooper bzero(&ad[i], sizeof(double));
624dc607e7SEnji Cooper bzero(&ald[i], sizeof(long double));
634dc607e7SEnji Cooper }
644dc607e7SEnji Cooper
654dc607e7SEnji Cooper af[0] = nanf(nan_format);
66*133bc645SAlex Richardson ATF_REQUIRE(isnan(af[0]));
674dc607e7SEnji Cooper af[1] = strtof(nan_str, &end);
68*133bc645SAlex Richardson ATF_REQUIRE(end == nan_str + strlen(nan_str));
69*133bc645SAlex Richardson ATF_REQUIRE(sscanf(nan_str, "%e", &af[2]) == 1);
70*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&af[0], &af[1], sizeof(float)) == 0);
71*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&af[1], &af[2], sizeof(float)) == 0);
724dc607e7SEnji Cooper if (*nan_format == '\0') {
734dc607e7SEnji Cooper /* nanf("") == strtof("nan") */
744dc607e7SEnji Cooper af[3] = strtof("nan", NULL);
75*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&af[2], &af[3], sizeof(float)) == 0);
764dc607e7SEnji Cooper }
774dc607e7SEnji Cooper
784dc607e7SEnji Cooper ad[0] = nan(nan_format);
79*133bc645SAlex Richardson ATF_REQUIRE(isnan(ad[0]));
804dc607e7SEnji Cooper ad[1] = strtod(nan_str, &end);
81*133bc645SAlex Richardson ATF_REQUIRE(end == nan_str + strlen(nan_str));
82*133bc645SAlex Richardson ATF_REQUIRE(sscanf(nan_str, "%le", &ad[2]) == 1);
83*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&ad[0], &ad[1], sizeof(double)) == 0);
84*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&ad[1], &ad[2], sizeof(double)) == 0);
854dc607e7SEnji Cooper if (*nan_format == '\0') {
864dc607e7SEnji Cooper /* nan("") == strtod("nan") */
874dc607e7SEnji Cooper ad[3] = strtod("nan", NULL);
88*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&ad[2], &ad[3], sizeof(double)) == 0);
894dc607e7SEnji Cooper }
904dc607e7SEnji Cooper
914dc607e7SEnji Cooper ald[0] = nanl(nan_format);
92*133bc645SAlex Richardson ATF_REQUIRE(isnan(ald[0]));
934dc607e7SEnji Cooper ald[1] = strtold(nan_str, &end);
94*133bc645SAlex Richardson ATF_REQUIRE(end == nan_str + strlen(nan_str));
95*133bc645SAlex Richardson ATF_REQUIRE(sscanf(nan_str, "%Le", &ald[2]) == 1);
96*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&ald[0], &ald[1], sizeof(long double)) == 0);
97*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&ald[1], &ald[2], sizeof(long double)) == 0);
984dc607e7SEnji Cooper if (*nan_format == '\0') {
994dc607e7SEnji Cooper /* nanl("") == strtold("nan") */
1004dc607e7SEnji Cooper ald[3] = strtold("nan", NULL);
101*133bc645SAlex Richardson ATF_REQUIRE(memcmp(&ald[2], &ald[3], sizeof(long double)) == 0);
1024dc607e7SEnji Cooper }
1034dc607e7SEnji Cooper }
1044dc607e7SEnji Cooper
105*133bc645SAlex Richardson ATF_TC_WITHOUT_HEAD(nan);
ATF_TC_BODY(nan,tc)106*133bc645SAlex Richardson ATF_TC_BODY(nan, tc)
1074dc607e7SEnji Cooper {
1084dc607e7SEnji Cooper /* Die if a signalling NaN is returned */
1094dc607e7SEnji Cooper feenableexcept(FE_INVALID);
1104dc607e7SEnji Cooper
1114dc607e7SEnji Cooper testnan("0x1234");
1124dc607e7SEnji Cooper testnan("");
113*133bc645SAlex Richardson }
1144dc607e7SEnji Cooper
ATF_TP_ADD_TCS(tp)115*133bc645SAlex Richardson ATF_TP_ADD_TCS(tp)
116*133bc645SAlex Richardson {
117*133bc645SAlex Richardson ATF_TP_ADD_TC(tp, nan);
1184dc607e7SEnji Cooper
119*133bc645SAlex Richardson return (atf_no_error());
1204dc607e7SEnji Cooper }
121