xref: /dragonfly/tools/regression/lib/libm/test-nan.c (revision c37c9ab3)
1 /*-
2  * Copyright (C) 2007 David Schultz <das@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/tools/regression/lib/msun/test-nan.c,v 1.1 2007/12/16 21:19:51 das Exp $
27  */
28 
29 /*
30  * Test for nan(), nanf(), and nanl(). We also test that strtod("nan(...)")
31  * and sscanf("nan(...)", ...) work identically.
32  */
33 
34 #include <assert.h>
35 #include <fenv.h>
36 #include <float.h>
37 #include <locale.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 void
44 testnan(const char *nan_format)
45 {
46 	char nan_str[128];
47 	char *end;
48 	long double ald[4];
49 	double ad[4];
50 	float af[4];
51 	int i;
52 
53 	snprintf(nan_str, sizeof(nan_str), "nan(%s)", nan_format);
54 	for (i = 0; i < 4; i++) {
55 		/*
56 		 * x86 has an 80-bit long double stored in 96 bits,
57 		 * so we need to initialize the memory for the memcmp()
58 		 * checks below to work.
59 		 */
60 		bzero(&af[i], sizeof(float));
61 		bzero(&ad[i], sizeof(double));
62 		bzero(&ald[i], sizeof(long double));
63 
64 	}
65 
66 	af[0] = nanf(nan_format);
67 	assert(isnan(af[0]));
68 	af[1] = strtof(nan_str, &end);
69 	assert(end == nan_str + strlen(nan_str));
70 	assert(sscanf(nan_str, "%e", &af[2]) == 1);
71 	assert(memcmp(&af[0], &af[1], sizeof(float)) == 0);
72 	assert(memcmp(&af[1], &af[2], sizeof(float)) == 0);
73 	if (*nan_format == '\0') {
74 		/* nanf("") == strtof("nan") */
75 		af[3] = strtof("nan", NULL);
76 		assert(memcmp(&af[2], &af[3], sizeof(float)) == 0);
77 	}
78 
79 	ad[0] = nan(nan_format);
80 	assert(isnan(ad[0]));
81 	ad[1] = strtod(nan_str, &end);
82 	assert(end == nan_str + strlen(nan_str));
83 	assert(sscanf(nan_str, "%le", &ad[2]) == 1);
84 	assert(memcmp(&ad[0], &ad[1], sizeof(double)) == 0);
85 	assert(memcmp(&ad[1], &ad[2], sizeof(double)) == 0);
86 	if (*nan_format == '\0') {
87 		/* nan("") == strtod("nan") */
88 		ad[3] = strtod("nan", NULL);
89 		assert(memcmp(&ad[2], &ad[3], sizeof(double)) == 0);
90 	}
91 
92 	ald[0] = nanl(nan_format);
93 	assert(isnan(ald[0]));
94 	ald[1] = strtold(nan_str, &end);
95 	assert(end == nan_str + strlen(nan_str));
96 	assert(sscanf(nan_str, "%Le", &ald[2]) == 1);
97 	assert(memcmp(&ald[0], &ald[1], sizeof(long double)) == 0);
98 	assert(memcmp(&ald[1], &ald[2], sizeof(long double)) == 0);
99 	if (*nan_format == '\0') {
100 		/* nanl("") == strtold("nan") */
101 		ald[3] = strtold("nan", NULL);
102 		assert(memcmp(&ald[2], &ald[3], sizeof(long double)) == 0);
103 	}
104 }
105 
106 int
107 main(int argc, char *argv[])
108 {
109 
110 	printf("1..1\n");
111 
112 	/* Die if a signalling NaN is returned */
113 	feenableexcept(FE_INVALID);
114 
115 	testnan("0x1234");
116 	testnan("");
117 
118 	printf("ok 1 - nan\n");
119 
120 	return (0);
121 }
122