1 /* $OpenBSD: nan_test.c,v 1.1 2021/10/22 18:00:23 mbuhl 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/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <fenv.h> 40 #include <float.h> 41 #include <locale.h> 42 #include <math.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 #include "test-utils.h" 48 49 static void 50 testnan(const char *nan_format) 51 { 52 char nan_str[128]; 53 char *end; 54 long double ald[4]; 55 double ad[4]; 56 float af[4]; 57 unsigned i; 58 59 snprintf(nan_str, sizeof(nan_str), "nan(%s)", nan_format); 60 for (i = 0; i < nitems(ad); i++) { 61 /* 62 * x86 has an 80-bit long double stored in 96 bits, 63 * so we need to initialize the memory for the memcmp() 64 * checks below to work. 65 */ 66 bzero(&af[i], sizeof(float)); 67 bzero(&ad[i], sizeof(double)); 68 bzero(&ald[i], sizeof(long double)); 69 } 70 71 af[0] = nanf(nan_format); 72 ATF_REQUIRE(isnan(af[0])); 73 af[1] = strtof(nan_str, &end); 74 ATF_REQUIRE(end == nan_str + strlen(nan_str)); 75 ATF_REQUIRE(sscanf(nan_str, "%e", &af[2]) == 1); 76 ATF_REQUIRE(memcmp(&af[0], &af[1], sizeof(float)) == 0); 77 ATF_REQUIRE(memcmp(&af[1], &af[2], sizeof(float)) == 0); 78 if (*nan_format == '\0') { 79 /* nanf("") == strtof("nan") */ 80 af[3] = strtof("nan", NULL); 81 ATF_REQUIRE(memcmp(&af[2], &af[3], sizeof(float)) == 0); 82 } 83 84 ad[0] = nan(nan_format); 85 ATF_REQUIRE(isnan(ad[0])); 86 ad[1] = strtod(nan_str, &end); 87 ATF_REQUIRE(end == nan_str + strlen(nan_str)); 88 ATF_REQUIRE(sscanf(nan_str, "%le", &ad[2]) == 1); 89 ATF_REQUIRE(memcmp(&ad[0], &ad[1], sizeof(double)) == 0); 90 ATF_REQUIRE(memcmp(&ad[1], &ad[2], sizeof(double)) == 0); 91 if (*nan_format == '\0') { 92 /* nan("") == strtod("nan") */ 93 ad[3] = strtod("nan", NULL); 94 ATF_REQUIRE(memcmp(&ad[2], &ad[3], sizeof(double)) == 0); 95 } 96 97 ald[0] = nanl(nan_format); 98 ATF_REQUIRE(isnan(ald[0])); 99 ald[1] = strtold(nan_str, &end); 100 ATF_REQUIRE(end == nan_str + strlen(nan_str)); 101 ATF_REQUIRE(sscanf(nan_str, "%Le", &ald[2]) == 1); 102 ATF_REQUIRE(memcmp(&ald[0], &ald[1], sizeof(long double)) == 0); 103 ATF_REQUIRE(memcmp(&ald[1], &ald[2], sizeof(long double)) == 0); 104 if (*nan_format == '\0') { 105 /* nanl("") == strtold("nan") */ 106 ald[3] = strtold("nan", NULL); 107 ATF_REQUIRE(memcmp(&ald[2], &ald[3], sizeof(long double)) == 0); 108 } 109 } 110 111 ATF_TC_WITHOUT_HEAD(nan); 112 ATF_TC_BODY(nan, tc) 113 { 114 /* Die if a signalling NaN is returned */ 115 feenableexcept(FE_INVALID); 116 117 testnan("0x1234"); 118 testnan(""); 119 } 120 121 ATF_TP_ADD_TCS(tp) 122 { 123 ATF_TP_ADD_TC(tp, nan); 124 125 return (atf_no_error()); 126 } 127