1*498c7b5eSderaadt /* $OpenBSD: conj_test.c,v 1.4 2021/12/13 18:04:28 deraadt Exp $ */
24c142d94Sbluhm /*-
34c142d94Sbluhm * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
44c142d94Sbluhm * All rights reserved.
54c142d94Sbluhm *
64c142d94Sbluhm * Redistribution and use in source and binary forms, with or without
74c142d94Sbluhm * modification, are permitted provided that the following conditions
84c142d94Sbluhm * are met:
94c142d94Sbluhm * 1. Redistributions of source code must retain the above copyright
104c142d94Sbluhm * notice, this list of conditions and the following disclaimer.
114c142d94Sbluhm * 2. Redistributions in binary form must reproduce the above copyright
124c142d94Sbluhm * notice, this list of conditions and the following disclaimer in the
134c142d94Sbluhm * documentation and/or other materials provided with the distribution.
144c142d94Sbluhm *
154c142d94Sbluhm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
164c142d94Sbluhm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174c142d94Sbluhm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184c142d94Sbluhm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194c142d94Sbluhm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204c142d94Sbluhm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
214c142d94Sbluhm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224c142d94Sbluhm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234c142d94Sbluhm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244c142d94Sbluhm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254c142d94Sbluhm * SUCH DAMAGE.
264c142d94Sbluhm */
274c142d94Sbluhm
284c142d94Sbluhm /*
294c142d94Sbluhm * Tests for conj{,f,l}()
304c142d94Sbluhm */
314c142d94Sbluhm
324c142d94Sbluhm #include <complex.h>
334c142d94Sbluhm #include <fenv.h>
344c142d94Sbluhm #include <math.h>
354c142d94Sbluhm #include <stdio.h>
364c142d94Sbluhm
374c142d94Sbluhm #include "test-utils.h"
384c142d94Sbluhm
394c142d94Sbluhm #pragma STDC CX_LIMITED_RANGE OFF
404c142d94Sbluhm
414c142d94Sbluhm static float complex (*libconjf)(float complex) = conjf;
424c142d94Sbluhm static double complex (*libconj)(double complex) = conj;
434c142d94Sbluhm static long double complex (*libconjl)(long double complex) = conjl;
444c142d94Sbluhm static float (*libcrealf)(float complex) = crealf;
454c142d94Sbluhm static double (*libcreal)(double complex) = creal;
464c142d94Sbluhm static long double (*libcreall)(long double complex) = creall;
474c142d94Sbluhm static float (*libcimagf)(float complex) = cimagf;
484c142d94Sbluhm static double (*libcimag)(double complex) = cimag;
494c142d94Sbluhm static long double (*libcimagl)(long double complex) = cimagl;
504c142d94Sbluhm
514c142d94Sbluhm static const double tests[] = {
524c142d94Sbluhm /* a + bI */
534c142d94Sbluhm 0.0, 0.0,
544c142d94Sbluhm 0.0, 1.0,
554c142d94Sbluhm 1.0, 0.0,
564c142d94Sbluhm -1.0, 0.0,
574c142d94Sbluhm 1.0, -0.0,
584c142d94Sbluhm 0.0, -1.0,
594c142d94Sbluhm 2.0, 4.0,
604c142d94Sbluhm 0.0, INFINITY,
614c142d94Sbluhm 0.0, -INFINITY,
624c142d94Sbluhm INFINITY, 0.0,
634c142d94Sbluhm NAN, 1.0,
644c142d94Sbluhm 1.0, NAN,
654c142d94Sbluhm NAN, NAN,
664c142d94Sbluhm -INFINITY, INFINITY,
674c142d94Sbluhm };
684c142d94Sbluhm
69c36e572eSmbuhl ATF_TC_WITHOUT_HEAD(main);
ATF_TC_BODY(main,tc)70c36e572eSmbuhl ATF_TC_BODY(main, tc)
714c142d94Sbluhm {
724c142d94Sbluhm static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2;
734c142d94Sbluhm complex float in;
744c142d94Sbluhm complex long double expected;
754c142d94Sbluhm int i;
764c142d94Sbluhm
774c142d94Sbluhm for (i = 0; i < ntests; i++) {
784c142d94Sbluhm __real__ expected = __real__ in = tests[2 * i];
794c142d94Sbluhm __imag__ in = tests[2 * i + 1];
804c142d94Sbluhm __imag__ expected = -cimag(in);
814c142d94Sbluhm
82c36e572eSmbuhl ATF_REQUIRE(fpequal_cs(libcrealf(in), __real__ in, true));
83c36e572eSmbuhl ATF_REQUIRE(fpequal_cs(libcreal(in), __real__ in, true));
84c36e572eSmbuhl ATF_REQUIRE(fpequal_cs(libcreall(in), __real__ in, true));
85c36e572eSmbuhl ATF_REQUIRE(fpequal_cs(libcimagf(in), __imag__ in, true));
86c36e572eSmbuhl ATF_REQUIRE(fpequal_cs(libcimag(in), __imag__ in, true));
87c36e572eSmbuhl ATF_REQUIRE(fpequal_cs(libcimagl(in), __imag__ in, true));
884c142d94Sbluhm
89c36e572eSmbuhl ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
90c36e572eSmbuhl ATF_REQUIRE_MSG(
91c36e572eSmbuhl cfpequal(libconjf(in), expected),
92c36e572eSmbuhl "conjf(%#.2g + %#.2gI): wrong value", creal(in), cimag(in)
93c36e572eSmbuhl );
94c36e572eSmbuhl ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
95c36e572eSmbuhl "conj(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
96c36e572eSmbuhl cimag(in), fetestexcept(FE_ALL_EXCEPT));
974c142d94Sbluhm
98c36e572eSmbuhl ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
99c36e572eSmbuhl ATF_REQUIRE_MSG(cfpequal(libconj(in), expected),
100c36e572eSmbuhl "conj(%#.2g + %#.2gI): wrong value", creal(in), cimag(in));
101c36e572eSmbuhl ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
102c36e572eSmbuhl "conj(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
103c36e572eSmbuhl cimag(in), fetestexcept(FE_ALL_EXCEPT));
1044c142d94Sbluhm
105c36e572eSmbuhl ATF_REQUIRE_EQ(0, feclearexcept(FE_ALL_EXCEPT));
106c36e572eSmbuhl ATF_REQUIRE_MSG(cfpequal(libconjl(in), expected),
107c36e572eSmbuhl "conjl(%#.2g + %#.2gI): wrong value", creal(in), cimag(in));
108c36e572eSmbuhl ATF_REQUIRE_EQ_MSG(0, fetestexcept(FE_ALL_EXCEPT),
109c36e572eSmbuhl "conjl(%#.2g + %#.2gI): threw an exception: %#x", creal(in),
110c36e572eSmbuhl cimag(in), fetestexcept(FE_ALL_EXCEPT));
111c36e572eSmbuhl }
1124c142d94Sbluhm }
1134c142d94Sbluhm
ATF_TP_ADD_TCS(tp)114c36e572eSmbuhl ATF_TP_ADD_TCS(tp)
115c36e572eSmbuhl {
116c36e572eSmbuhl ATF_TP_ADD_TC(tp, main);
117c36e572eSmbuhl
118c36e572eSmbuhl return (atf_no_error());
1194c142d94Sbluhm }
120