xref: /freebsd-src/lib/msun/tests/conj_test.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
14dc607e7SEnji Cooper /*-
24dc607e7SEnji Cooper  * Copyright (c) 2008 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  * Tests for conj{,f,l}()
294dc607e7SEnji Cooper  */
304dc607e7SEnji Cooper 
314dc607e7SEnji Cooper #include <sys/cdefs.h>
324dc607e7SEnji Cooper #include <complex.h>
334dc607e7SEnji Cooper #include <fenv.h>
344dc607e7SEnji Cooper #include <math.h>
354dc607e7SEnji Cooper #include <stdio.h>
364dc607e7SEnji Cooper 
374dc607e7SEnji Cooper #include "test-utils.h"
384dc607e7SEnji Cooper 
394dc607e7SEnji Cooper #pragma	STDC CX_LIMITED_RANGE	OFF
404dc607e7SEnji Cooper 
414dc607e7SEnji Cooper /* Make sure gcc doesn't use builtin versions of these or honor __pure2. */
424dc607e7SEnji Cooper static float complex (*libconjf)(float complex) = conjf;
434dc607e7SEnji Cooper static double complex (*libconj)(double complex) = conj;
444dc607e7SEnji Cooper static long double complex (*libconjl)(long double complex) = conjl;
454dc607e7SEnji Cooper static float (*libcrealf)(float complex) = crealf;
464dc607e7SEnji Cooper static double (*libcreal)(double complex) = creal;
474dc607e7SEnji Cooper static long double (*libcreall)(long double complex) = creall;
484dc607e7SEnji Cooper static float (*libcimagf)(float complex) = cimagf;
494dc607e7SEnji Cooper static double (*libcimag)(double complex) = cimag;
504dc607e7SEnji Cooper static long double (*libcimagl)(long double complex) = cimagl;
514dc607e7SEnji Cooper 
524dc607e7SEnji Cooper static const double tests[] = {
534dc607e7SEnji Cooper 	/* a +  bI */
544dc607e7SEnji Cooper 	0.0,	0.0,
554dc607e7SEnji Cooper 	0.0,	1.0,
564dc607e7SEnji Cooper 	1.0,	0.0,
574dc607e7SEnji Cooper 	-1.0,	0.0,
584dc607e7SEnji Cooper 	1.0,	-0.0,
594dc607e7SEnji Cooper 	0.0,	-1.0,
604dc607e7SEnji Cooper 	2.0,	4.0,
614dc607e7SEnji Cooper 	0.0,	INFINITY,
624dc607e7SEnji Cooper 	0.0,	-INFINITY,
634dc607e7SEnji Cooper 	INFINITY, 0.0,
644dc607e7SEnji Cooper 	NAN,	1.0,
654dc607e7SEnji Cooper 	1.0,	NAN,
664dc607e7SEnji Cooper 	NAN,	NAN,
674dc607e7SEnji Cooper 	-INFINITY, INFINITY,
684dc607e7SEnji Cooper };
694dc607e7SEnji Cooper 
70133bc645SAlex Richardson ATF_TC_WITHOUT_HEAD(main);
ATF_TC_BODY(main,tc)71133bc645SAlex Richardson ATF_TC_BODY(main, tc)
724dc607e7SEnji Cooper {
734dc607e7SEnji Cooper 	static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2;
744dc607e7SEnji Cooper 	complex float in;
754dc607e7SEnji Cooper 	complex long double expected;
764dc607e7SEnji Cooper 	int i;
774dc607e7SEnji Cooper 
784dc607e7SEnji Cooper 	for (i = 0; i < ntests; i++) {
794dc607e7SEnji Cooper 		__real__ expected = __real__ in = tests[2 * i];
804dc607e7SEnji Cooper 		__imag__ in = tests[2 * i + 1];
814dc607e7SEnji Cooper 		__imag__ expected = -cimag(in);
824dc607e7SEnji Cooper 
83*b424e003SAlex Richardson 		ATF_REQUIRE(fpequal_cs(libcrealf(in), __real__ in, true));
84*b424e003SAlex Richardson 		ATF_REQUIRE(fpequal_cs(libcreal(in), __real__ in, true));
85*b424e003SAlex Richardson 		ATF_REQUIRE(fpequal_cs(libcreall(in), __real__ in, true));
86*b424e003SAlex Richardson 		ATF_REQUIRE(fpequal_cs(libcimagf(in), __imag__ in, true));
87*b424e003SAlex Richardson 		ATF_REQUIRE(fpequal_cs(libcimag(in), __imag__ in, true));
88*b424e003SAlex Richardson 		ATF_REQUIRE(fpequal_cs(libcimagl(in), __imag__ in, true));
894dc607e7SEnji Cooper 
90133bc645SAlex Richardson 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
91133bc645SAlex Richardson 		ATF_REQUIRE_MSG(
92133bc645SAlex Richardson 		    cfpequal(libconjf(in), expected),
93133bc645SAlex Richardson 		    "conjf(%#.2g + %#.2gI): wrong value", creal(in), cimag(in)
94133bc645SAlex Richardson 		);
95133bc645SAlex Richardson 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
96133bc645SAlex Richardson 		    "conj(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
97133bc645SAlex Richardson 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
984dc607e7SEnji Cooper 
99133bc645SAlex Richardson 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
100133bc645SAlex Richardson 		ATF_REQUIRE_MSG(cfpequal(libconj(in), expected),
101133bc645SAlex Richardson 		    "conj(%#.2g + %#.2gI): wrong value", creal(in), cimag(in));
102133bc645SAlex Richardson 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
103133bc645SAlex Richardson 		    "conj(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
104133bc645SAlex Richardson 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
1054dc607e7SEnji Cooper 
106133bc645SAlex Richardson 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
107133bc645SAlex Richardson 		ATF_REQUIRE_MSG(cfpequal(libconjl(in), expected),
108133bc645SAlex Richardson 		    "conjl(%#.2g + %#.2gI): wrong value", creal(in), cimag(in));
109133bc645SAlex Richardson 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
110133bc645SAlex Richardson 		    "conjl(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
111133bc645SAlex Richardson 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
1124dc607e7SEnji Cooper 	}
1134dc607e7SEnji Cooper }
1144dc607e7SEnji Cooper 
ATF_TP_ADD_TCS(tp)115133bc645SAlex Richardson ATF_TP_ADD_TCS(tp)
116133bc645SAlex Richardson {
117133bc645SAlex Richardson 	ATF_TP_ADD_TC(tp, main);
118133bc645SAlex Richardson 
119133bc645SAlex Richardson 	return (atf_no_error());
1204dc607e7SEnji Cooper }
121