test-conj.c revision 187962
1/*-
2 * Copyright (c) 2008 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
27/*
28 * Tests for conj{,f,l}()
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/tools/regression/lib/msun/test-conj.c 187962 2009-01-31 18:31:57Z das $");
33
34#include <assert.h>
35#include <complex.h>
36#include <fenv.h>
37#include <math.h>
38#include <stdio.h>
39
40#pragma	STDC CX_LIMITED_RANGE	off
41
42/* Make sure gcc doesn't use builtin versions of these or honor __pure2. */
43static float complex (*libconjf)(float complex) = conjf;
44static double complex (*libconj)(double complex) = conj;
45static long double complex (*libconjl)(long double complex) = conjl;
46static float (*libcrealf)(float complex) = crealf;
47static double (*libcreal)(double complex) = creal;
48static long double (*libcreall)(long double complex) = creall;
49static float (*libcimagf)(float complex) = cimagf;
50static double (*libcimag)(double complex) = cimag;
51static long double (*libcimagl)(long double complex) = cimagl;
52
53/*
54 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0.
55 * Fail an assertion if they differ.
56 */
57static int
58fpequal(long double d1, long double d2)
59{
60
61	if (d1 != d2)
62		return (isnan(d1) && isnan(d2));
63	return (copysignl(1.0, d1) == copysignl(1.0, d2));
64}
65
66static int
67cfpequal(long double complex d1, long double complex d2)
68{
69
70	return (fpequal(creall(d1), creall(d2)) &&
71		fpequal(cimagl(d1), cimagl(d2)));
72}
73
74static const double tests[] = {
75	/* a +  bI */
76	0.0,	0.0,
77	0.0,	1.0,
78	1.0,	0.0,
79	-1.0,	0.0,
80	1.0,	-0.0,
81	0.0,	-1.0,
82	2.0,	4.0,
83	0.0,	INFINITY,
84	0.0,	-INFINITY,
85	INFINITY, 0.0,
86	NAN,	1.0,
87	1.0,	NAN,
88	NAN,	NAN,
89	-INFINITY, INFINITY,
90};
91
92int
93main(int argc, char *argv[])
94{
95	static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2;
96	complex float in;
97	complex long double expected;
98	int i;
99
100	printf("1..%d\n", ntests * 3);
101
102	for (i = 0; i < ntests; i++) {
103		__real__ expected = __real__ in = tests[2 * i];
104		__imag__ in = tests[2 * i + 1];
105		__imag__ expected = -cimag(in);
106
107		assert(fpequal(libcrealf(in), __real__ in));
108		assert(fpequal(libcreal(in), __real__ in));
109		assert(fpequal(libcreall(in), __real__ in));
110		assert(fpequal(libcimagf(in), __imag__ in));
111		assert(fpequal(libcimag(in), __imag__ in));
112		assert(fpequal(libcimagl(in), __imag__ in));
113
114		feclearexcept(FE_ALL_EXCEPT);
115		if (!cfpequal(libconjf(in), expected)) {
116			printf("not ok %d\t# conjf(%#.2g + %#.2gI): "
117			       "wrong value\n",
118			       3 * i + 1, creal(in), cimag(in));
119		} else if (fetestexcept(FE_ALL_EXCEPT)) {
120			printf("not ok %d\t# conjf(%#.2g + %#.2gI): "
121			       "threw an exception\n",
122			       3 * i + 1, creal(in), cimag(in));
123		} else {
124			printf("ok %d\t\t# conjf(%#.2g + %#.2gI)\n",
125			       3 * i + 1, creal(in), cimag(in));
126		}
127
128		feclearexcept(FE_ALL_EXCEPT);
129		if (!cfpequal(libconj(in), expected)) {
130			printf("not ok %d\t# conj(%#.2g + %#.2gI): "
131			       "wrong value\n",
132			       3 * i + 2, creal(in), cimag(in));
133		} else if (fetestexcept(FE_ALL_EXCEPT)) {
134			printf("not ok %d\t# conj(%#.2g + %#.2gI): "
135			       "threw an exception\n",
136			       3 * i + 2, creal(in), cimag(in));
137		} else {
138			printf("ok %d\t\t# conj(%#.2g + %#.2gI)\n",
139			       3 * i + 2, creal(in), cimag(in));
140		}
141
142		feclearexcept(FE_ALL_EXCEPT);
143		if (!cfpequal(libconjl(in), expected)) {
144			printf("not ok %d\t# conjl(%#.2g + %#.2gI): "
145			       "wrong value\n",
146			       3 * i + 3, creal(in), cimag(in));
147		} else if (fetestexcept(FE_ALL_EXCEPT)) {
148			printf("not ok %d\t# conjl(%#.2g + %#.2gI): "
149			       "threw an exception\n",
150			       3 * i + 3, creal(in), cimag(in));
151		} else {
152			printf("ok %d\t\t# conjl(%#.2g + %#.2gI)\n",
153			       3 * i + 3, creal(in), cimag(in));
154		}
155	}
156
157	return (0);
158}
159