xref: /freebsd/lib/msun/tests/conj_test.c (revision 10ff414c)
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$");
33 
34 #include <complex.h>
35 #include <fenv.h>
36 #include <math.h>
37 #include <stdio.h>
38 
39 #include "test-utils.h"
40 
41 #pragma	STDC CX_LIMITED_RANGE	OFF
42 
43 /* Make sure gcc doesn't use builtin versions of these or honor __pure2. */
44 static float complex (*libconjf)(float complex) = conjf;
45 static double complex (*libconj)(double complex) = conj;
46 static long double complex (*libconjl)(long double complex) = conjl;
47 static float (*libcrealf)(float complex) = crealf;
48 static double (*libcreal)(double complex) = creal;
49 static long double (*libcreall)(long double complex) = creall;
50 static float (*libcimagf)(float complex) = cimagf;
51 static double (*libcimag)(double complex) = cimag;
52 static long double (*libcimagl)(long double complex) = cimagl;
53 
54 static const double tests[] = {
55 	/* a +  bI */
56 	0.0,	0.0,
57 	0.0,	1.0,
58 	1.0,	0.0,
59 	-1.0,	0.0,
60 	1.0,	-0.0,
61 	0.0,	-1.0,
62 	2.0,	4.0,
63 	0.0,	INFINITY,
64 	0.0,	-INFINITY,
65 	INFINITY, 0.0,
66 	NAN,	1.0,
67 	1.0,	NAN,
68 	NAN,	NAN,
69 	-INFINITY, INFINITY,
70 };
71 
72 ATF_TC_WITHOUT_HEAD(main);
73 ATF_TC_BODY(main, tc)
74 {
75 	static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2;
76 	complex float in;
77 	complex long double expected;
78 	int i;
79 
80 	for (i = 0; i < ntests; i++) {
81 		__real__ expected = __real__ in = tests[2 * i];
82 		__imag__ in = tests[2 * i + 1];
83 		__imag__ expected = -cimag(in);
84 
85 		ATF_REQUIRE(fpequal_cs(libcrealf(in), __real__ in, true));
86 		ATF_REQUIRE(fpequal_cs(libcreal(in), __real__ in, true));
87 		ATF_REQUIRE(fpequal_cs(libcreall(in), __real__ in, true));
88 		ATF_REQUIRE(fpequal_cs(libcimagf(in), __imag__ in, true));
89 		ATF_REQUIRE(fpequal_cs(libcimag(in), __imag__ in, true));
90 		ATF_REQUIRE(fpequal_cs(libcimagl(in), __imag__ in, true));
91 
92 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
93 		ATF_REQUIRE_MSG(
94 		    cfpequal(libconjf(in), expected),
95 		    "conjf(%#.2g + %#.2gI): wrong value", creal(in), cimag(in)
96 		);
97 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
98 		    "conj(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
99 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
100 
101 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
102 		ATF_REQUIRE_MSG(cfpequal(libconj(in), expected),
103 		    "conj(%#.2g + %#.2gI): wrong value", creal(in), cimag(in));
104 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
105 		    "conj(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
106 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
107 
108 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
109 		ATF_REQUIRE_MSG(cfpequal(libconjl(in), expected),
110 		    "conjl(%#.2g + %#.2gI): wrong value", creal(in), cimag(in));
111 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
112 		    "conjl(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
113 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
114 	}
115 }
116 
117 ATF_TP_ADD_TCS(tp)
118 {
119 	ATF_TP_ADD_TC(tp, main);
120 
121 	return (atf_no_error());
122 }
123