xref: /openbsd-src/regress/lib/libm/msun/conj_test.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: conj_test.c,v 1.2 2019/02/21 17:36:41 bluhm 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 <sys/cdefs.h>
33 /* $FreeBSD: head/lib/msun/tests/conj_test.c 314650 2017-03-04 10:07:46Z ngie $ */
34 
35 #include <assert.h>
36 #include <complex.h>
37 #include <fenv.h>
38 #include <math.h>
39 #include <stdio.h>
40 
41 #include "test-utils.h"
42 
43 #pragma	STDC CX_LIMITED_RANGE	OFF
44 
45 /*
46  * Test that a function returns the correct value and sets the
47  * exception flags correctly. The exceptmask specifies which
48  * exceptions we should check. We need to be lenient for several
49  * reasons, but mainly because on some architectures it's impossible
50  * to raise FE_OVERFLOW without raising FE_INEXACT. In some cases,
51  * whether cexp() raises an invalid exception is unspecified.
52  *
53  * These are macros instead of functions so that assert provides more
54  * meaningful error messages.
55  */
56 #define	test(func, z, result, exceptmask, excepts)		do {	\
57 	assert(feclearexcept(FE_ALL_EXCEPT) == 0);			\
58 	assert(cfpequal((func)(z), (result)));				\
59 	assert(((void)(func), fetestexcept(exceptmask) == (excepts)));	\
60 } while (0)
61 
62 /* Make sure gcc doesn't use builtin versions of these or honor __pure2. */
63 static float complex (*libconjf)(float complex) = conjf;
64 static double complex (*libconj)(double complex) = conj;
65 static long double complex (*libconjl)(long double complex) = conjl;
66 static float (*libcrealf)(float complex) = crealf;
67 static double (*libcreal)(double complex) = creal;
68 static long double (*libcreall)(long double complex) = creall;
69 static float (*libcimagf)(float complex) = cimagf;
70 static double (*libcimag)(double complex) = cimag;
71 static long double (*libcimagl)(long double complex) = cimagl;
72 
73 static const double tests[] = {
74 	/* a +  bI */
75 	0.0,	0.0,
76 	0.0,	1.0,
77 	1.0,	0.0,
78 	-1.0,	0.0,
79 	1.0,	-0.0,
80 	0.0,	-1.0,
81 	2.0,	4.0,
82 	0.0,	INFINITY,
83 	0.0,	-INFINITY,
84 	INFINITY, 0.0,
85 	NAN,	1.0,
86 	1.0,	NAN,
87 	NAN,	NAN,
88 	-INFINITY, INFINITY,
89 };
90 
91 int
92 main(void)
93 {
94 	static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2;
95 	complex float in;
96 	complex long double expected;
97 	int i;
98 
99 	printf("1..%d\n", ntests * 3);
100 
101 	for (i = 0; i < ntests; i++) {
102 		__real__ expected = __real__ in = tests[2 * i];
103 		__imag__ in = tests[2 * i + 1];
104 		__imag__ expected = -cimag(in);
105 
106 		assert(fpequal(libcrealf(in), __real__ in));
107 		assert(fpequal(libcreal(in), __real__ in));
108 		assert(fpequal(libcreall(in), __real__ in));
109 		assert(fpequal(libcimagf(in), __imag__ in));
110 		assert(fpequal(libcimag(in), __imag__ in));
111 		assert(fpequal(libcimagl(in), __imag__ in));
112 
113 		test(libconjf, in, expected, FE_ALL_EXCEPT, 0);
114 		printf("ok %d\t\t# conjf(%#.2g + %#.2gI)\n",
115 		    3 * i + 1, creal(in), cimag(in));
116 
117 		test(libconj, in, expected, FE_ALL_EXCEPT, 0);
118 		printf("ok %d\t\t# conj(%#.2g + %#.2gI)\n",
119 		    3 * i + 2, creal(in), cimag(in));
120 
121 		test(libconjl, in, expected, FE_ALL_EXCEPT, 0);
122 		printf("ok %d\t\t# conjl(%#.2g + %#.2gI)\n",
123 		    3 * i + 3, creal(in), cimag(in));
124 	}
125 
126 	return (0);
127 }
128