1187962Sdas/*-
2187962Sdas * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
3187962Sdas * All rights reserved.
4187962Sdas *
5187962Sdas * Redistribution and use in source and binary forms, with or without
6187962Sdas * modification, are permitted provided that the following conditions
7187962Sdas * are met:
8187962Sdas * 1. Redistributions of source code must retain the above copyright
9187962Sdas *    notice, this list of conditions and the following disclaimer.
10187962Sdas * 2. Redistributions in binary form must reproduce the above copyright
11187962Sdas *    notice, this list of conditions and the following disclaimer in the
12187962Sdas *    documentation and/or other materials provided with the distribution.
13187962Sdas *
14187962Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15187962Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16187962Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17187962Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18187962Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19187962Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20187962Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21187962Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22187962Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23187962Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24187962Sdas * SUCH DAMAGE.
25187962Sdas */
26187962Sdas
27187962Sdas/*
28187962Sdas * Tests for conj{,f,l}()
29187962Sdas */
30187962Sdas
31187962Sdas#include <sys/cdefs.h>
32187962Sdas__FBSDID("$FreeBSD$");
33187962Sdas
34187962Sdas#include <assert.h>
35187962Sdas#include <complex.h>
36187962Sdas#include <fenv.h>
37187962Sdas#include <math.h>
38187962Sdas#include <stdio.h>
39187962Sdas
40251241Sdas#include "test-utils.h"
41251241Sdas
42187962Sdas#pragma	STDC CX_LIMITED_RANGE	off
43187962Sdas
44187962Sdas/* Make sure gcc doesn't use builtin versions of these or honor __pure2. */
45187962Sdasstatic float complex (*libconjf)(float complex) = conjf;
46187962Sdasstatic double complex (*libconj)(double complex) = conj;
47187962Sdasstatic long double complex (*libconjl)(long double complex) = conjl;
48187962Sdasstatic float (*libcrealf)(float complex) = crealf;
49187962Sdasstatic double (*libcreal)(double complex) = creal;
50187962Sdasstatic long double (*libcreall)(long double complex) = creall;
51187962Sdasstatic float (*libcimagf)(float complex) = cimagf;
52187962Sdasstatic double (*libcimag)(double complex) = cimag;
53187962Sdasstatic long double (*libcimagl)(long double complex) = cimagl;
54187962Sdas
55187962Sdasstatic const double tests[] = {
56187962Sdas	/* a +  bI */
57187962Sdas	0.0,	0.0,
58187962Sdas	0.0,	1.0,
59187962Sdas	1.0,	0.0,
60187962Sdas	-1.0,	0.0,
61187962Sdas	1.0,	-0.0,
62187962Sdas	0.0,	-1.0,
63187962Sdas	2.0,	4.0,
64187962Sdas	0.0,	INFINITY,
65187962Sdas	0.0,	-INFINITY,
66187962Sdas	INFINITY, 0.0,
67187962Sdas	NAN,	1.0,
68187962Sdas	1.0,	NAN,
69187962Sdas	NAN,	NAN,
70187962Sdas	-INFINITY, INFINITY,
71187962Sdas};
72187962Sdas
73187962Sdasint
74187962Sdasmain(int argc, char *argv[])
75187962Sdas{
76187962Sdas	static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2;
77187962Sdas	complex float in;
78187962Sdas	complex long double expected;
79187962Sdas	int i;
80187962Sdas
81187962Sdas	printf("1..%d\n", ntests * 3);
82187962Sdas
83187962Sdas	for (i = 0; i < ntests; i++) {
84187962Sdas		__real__ expected = __real__ in = tests[2 * i];
85187962Sdas		__imag__ in = tests[2 * i + 1];
86187962Sdas		__imag__ expected = -cimag(in);
87187962Sdas
88187962Sdas		assert(fpequal(libcrealf(in), __real__ in));
89187962Sdas		assert(fpequal(libcreal(in), __real__ in));
90187962Sdas		assert(fpequal(libcreall(in), __real__ in));
91187962Sdas		assert(fpequal(libcimagf(in), __imag__ in));
92187962Sdas		assert(fpequal(libcimag(in), __imag__ in));
93187962Sdas		assert(fpequal(libcimagl(in), __imag__ in));
94187962Sdas
95187962Sdas		feclearexcept(FE_ALL_EXCEPT);
96187962Sdas		if (!cfpequal(libconjf(in), expected)) {
97187962Sdas			printf("not ok %d\t# conjf(%#.2g + %#.2gI): "
98187962Sdas			       "wrong value\n",
99187962Sdas			       3 * i + 1, creal(in), cimag(in));
100187962Sdas		} else if (fetestexcept(FE_ALL_EXCEPT)) {
101187962Sdas			printf("not ok %d\t# conjf(%#.2g + %#.2gI): "
102187962Sdas			       "threw an exception\n",
103187962Sdas			       3 * i + 1, creal(in), cimag(in));
104187962Sdas		} else {
105187962Sdas			printf("ok %d\t\t# conjf(%#.2g + %#.2gI)\n",
106187962Sdas			       3 * i + 1, creal(in), cimag(in));
107187962Sdas		}
108187962Sdas
109187962Sdas		feclearexcept(FE_ALL_EXCEPT);
110187962Sdas		if (!cfpequal(libconj(in), expected)) {
111187962Sdas			printf("not ok %d\t# conj(%#.2g + %#.2gI): "
112187962Sdas			       "wrong value\n",
113187962Sdas			       3 * i + 2, creal(in), cimag(in));
114187962Sdas		} else if (fetestexcept(FE_ALL_EXCEPT)) {
115187962Sdas			printf("not ok %d\t# conj(%#.2g + %#.2gI): "
116187962Sdas			       "threw an exception\n",
117187962Sdas			       3 * i + 2, creal(in), cimag(in));
118187962Sdas		} else {
119187962Sdas			printf("ok %d\t\t# conj(%#.2g + %#.2gI)\n",
120187962Sdas			       3 * i + 2, creal(in), cimag(in));
121187962Sdas		}
122187962Sdas
123187962Sdas		feclearexcept(FE_ALL_EXCEPT);
124187962Sdas		if (!cfpequal(libconjl(in), expected)) {
125187962Sdas			printf("not ok %d\t# conjl(%#.2g + %#.2gI): "
126187962Sdas			       "wrong value\n",
127187962Sdas			       3 * i + 3, creal(in), cimag(in));
128187962Sdas		} else if (fetestexcept(FE_ALL_EXCEPT)) {
129187962Sdas			printf("not ok %d\t# conjl(%#.2g + %#.2gI): "
130187962Sdas			       "threw an exception\n",
131187962Sdas			       3 * i + 3, creal(in), cimag(in));
132187962Sdas		} else {
133187962Sdas			printf("ok %d\t\t# conjl(%#.2g + %#.2gI)\n",
134187962Sdas			       3 * i + 3, creal(in), cimag(in));
135187962Sdas		}
136187962Sdas	}
137187962Sdas
138187962Sdas	return (0);
139187962Sdas}
140