xref: /openbsd/regress/lib/libm/msun/conj_test.c (revision 498c7b5e)
1 /*	$OpenBSD: conj_test.c,v 1.4 2021/12/13 18:04:28 deraadt Exp $	*/
2 /*-
3  * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Tests for conj{,f,l}()
30  */
31 
32 #include <complex.h>
33 #include <fenv.h>
34 #include <math.h>
35 #include <stdio.h>
36 
37 #include "test-utils.h"
38 
39 #pragma	STDC CX_LIMITED_RANGE	OFF
40 
41 static float complex (*libconjf)(float complex) = conjf;
42 static double complex (*libconj)(double complex) = conj;
43 static long double complex (*libconjl)(long double complex) = conjl;
44 static float (*libcrealf)(float complex) = crealf;
45 static double (*libcreal)(double complex) = creal;
46 static long double (*libcreall)(long double complex) = creall;
47 static float (*libcimagf)(float complex) = cimagf;
48 static double (*libcimag)(double complex) = cimag;
49 static long double (*libcimagl)(long double complex) = cimagl;
50 
51 static const double tests[] = {
52 	/* a +  bI */
53 	0.0,	0.0,
54 	0.0,	1.0,
55 	1.0,	0.0,
56 	-1.0,	0.0,
57 	1.0,	-0.0,
58 	0.0,	-1.0,
59 	2.0,	4.0,
60 	0.0,	INFINITY,
61 	0.0,	-INFINITY,
62 	INFINITY, 0.0,
63 	NAN,	1.0,
64 	1.0,	NAN,
65 	NAN,	NAN,
66 	-INFINITY, INFINITY,
67 };
68 
69 ATF_TC_WITHOUT_HEAD(main);
ATF_TC_BODY(main,tc)70 ATF_TC_BODY(main, tc)
71 {
72 	static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2;
73 	complex float in;
74 	complex long double expected;
75 	int i;
76 
77 	for (i = 0; i < ntests; i++) {
78 		__real__ expected = __real__ in = tests[2 * i];
79 		__imag__ in = tests[2 * i + 1];
80 		__imag__ expected = -cimag(in);
81 
82 		ATF_REQUIRE(fpequal_cs(libcrealf(in), __real__ in, true));
83 		ATF_REQUIRE(fpequal_cs(libcreal(in), __real__ in, true));
84 		ATF_REQUIRE(fpequal_cs(libcreall(in), __real__ in, true));
85 		ATF_REQUIRE(fpequal_cs(libcimagf(in), __imag__ in, true));
86 		ATF_REQUIRE(fpequal_cs(libcimag(in), __imag__ in, true));
87 		ATF_REQUIRE(fpequal_cs(libcimagl(in), __imag__ in, true));
88 
89 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
90 		ATF_REQUIRE_MSG(
91 		    cfpequal(libconjf(in), expected),
92 		    "conjf(%#.2g + %#.2gI): wrong value", creal(in), cimag(in)
93 		);
94 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
95 		    "conj(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
96 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
97 
98 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
99 		ATF_REQUIRE_MSG(cfpequal(libconj(in), expected),
100 		    "conj(%#.2g + %#.2gI): wrong value", creal(in), cimag(in));
101 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
102 		    "conj(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
103 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
104 
105 		ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
106 		ATF_REQUIRE_MSG(cfpequal(libconjl(in), expected),
107 		    "conjl(%#.2g + %#.2gI): wrong value", creal(in), cimag(in));
108 		ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
109 		    "conjl(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
110 		    cimag(in), fetestexcept(FE_ALL_EXCEPT));
111  	}
112 }
113 
ATF_TP_ADD_TCS(tp)114 ATF_TP_ADD_TCS(tp)
115 {
116 	ATF_TP_ADD_TC(tp, main);
117 
118 	return (atf_no_error());
119 }
120