1187962Sdas/*- 2187962Sdas * Copyright (c) 2008 David Schultz <das@FreeBSD.org> 3187962Sdas * All rights reserved. 4187962Sdas * 5187962Sdas * Redistribution and use in source and binary forms, with or without 6187962Sdas * modification, are permitted provided that the following conditions 7187962Sdas * are met: 8187962Sdas * 1. Redistributions of source code must retain the above copyright 9187962Sdas * notice, this list of conditions and the following disclaimer. 10187962Sdas * 2. Redistributions in binary form must reproduce the above copyright 11187962Sdas * notice, this list of conditions and the following disclaimer in the 12187962Sdas * documentation and/or other materials provided with the distribution. 13187962Sdas * 14187962Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15187962Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16187962Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17187962Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18187962Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19187962Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20187962Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21187962Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22187962Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23187962Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24187962Sdas * SUCH DAMAGE. 25187962Sdas */ 26187962Sdas 27187962Sdas/* 28187962Sdas * Tests for conj{,f,l}() 29187962Sdas */ 30187962Sdas 31187962Sdas#include <sys/cdefs.h> 32187962Sdas__FBSDID("$FreeBSD$"); 33187962Sdas 34187962Sdas#include <assert.h> 35187962Sdas#include <complex.h> 36187962Sdas#include <fenv.h> 37187962Sdas#include <math.h> 38187962Sdas#include <stdio.h> 39187962Sdas 40187962Sdas#pragma STDC CX_LIMITED_RANGE off 41187962Sdas 42187962Sdas/* Make sure gcc doesn't use builtin versions of these or honor __pure2. */ 43187962Sdasstatic float complex (*libconjf)(float complex) = conjf; 44187962Sdasstatic double complex (*libconj)(double complex) = conj; 45187962Sdasstatic long double complex (*libconjl)(long double complex) = conjl; 46187962Sdasstatic float (*libcrealf)(float complex) = crealf; 47187962Sdasstatic double (*libcreal)(double complex) = creal; 48187962Sdasstatic long double (*libcreall)(long double complex) = creall; 49187962Sdasstatic float (*libcimagf)(float complex) = cimagf; 50187962Sdasstatic double (*libcimag)(double complex) = cimag; 51187962Sdasstatic long double (*libcimagl)(long double complex) = cimagl; 52187962Sdas 53187962Sdas/* 54187962Sdas * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0. 55187962Sdas * Fail an assertion if they differ. 56187962Sdas */ 57187962Sdasstatic int 58187962Sdasfpequal(long double d1, long double d2) 59187962Sdas{ 60187962Sdas 61187962Sdas if (d1 != d2) 62187962Sdas return (isnan(d1) && isnan(d2)); 63187962Sdas return (copysignl(1.0, d1) == copysignl(1.0, d2)); 64187962Sdas} 65187962Sdas 66187962Sdasstatic int 67187962Sdascfpequal(long double complex d1, long double complex d2) 68187962Sdas{ 69187962Sdas 70187962Sdas return (fpequal(creall(d1), creall(d2)) && 71187962Sdas fpequal(cimagl(d1), cimagl(d2))); 72187962Sdas} 73187962Sdas 74187962Sdasstatic const double tests[] = { 75187962Sdas /* a + bI */ 76187962Sdas 0.0, 0.0, 77187962Sdas 0.0, 1.0, 78187962Sdas 1.0, 0.0, 79187962Sdas -1.0, 0.0, 80187962Sdas 1.0, -0.0, 81187962Sdas 0.0, -1.0, 82187962Sdas 2.0, 4.0, 83187962Sdas 0.0, INFINITY, 84187962Sdas 0.0, -INFINITY, 85187962Sdas INFINITY, 0.0, 86187962Sdas NAN, 1.0, 87187962Sdas 1.0, NAN, 88187962Sdas NAN, NAN, 89187962Sdas -INFINITY, INFINITY, 90187962Sdas}; 91187962Sdas 92187962Sdasint 93187962Sdasmain(int argc, char *argv[]) 94187962Sdas{ 95187962Sdas static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2; 96187962Sdas complex float in; 97187962Sdas complex long double expected; 98187962Sdas int i; 99187962Sdas 100187962Sdas printf("1..%d\n", ntests * 3); 101187962Sdas 102187962Sdas for (i = 0; i < ntests; i++) { 103187962Sdas __real__ expected = __real__ in = tests[2 * i]; 104187962Sdas __imag__ in = tests[2 * i + 1]; 105187962Sdas __imag__ expected = -cimag(in); 106187962Sdas 107187962Sdas assert(fpequal(libcrealf(in), __real__ in)); 108187962Sdas assert(fpequal(libcreal(in), __real__ in)); 109187962Sdas assert(fpequal(libcreall(in), __real__ in)); 110187962Sdas assert(fpequal(libcimagf(in), __imag__ in)); 111187962Sdas assert(fpequal(libcimag(in), __imag__ in)); 112187962Sdas assert(fpequal(libcimagl(in), __imag__ in)); 113187962Sdas 114187962Sdas feclearexcept(FE_ALL_EXCEPT); 115187962Sdas if (!cfpequal(libconjf(in), expected)) { 116187962Sdas printf("not ok %d\t# conjf(%#.2g + %#.2gI): " 117187962Sdas "wrong value\n", 118187962Sdas 3 * i + 1, creal(in), cimag(in)); 119187962Sdas } else if (fetestexcept(FE_ALL_EXCEPT)) { 120187962Sdas printf("not ok %d\t# conjf(%#.2g + %#.2gI): " 121187962Sdas "threw an exception\n", 122187962Sdas 3 * i + 1, creal(in), cimag(in)); 123187962Sdas } else { 124187962Sdas printf("ok %d\t\t# conjf(%#.2g + %#.2gI)\n", 125187962Sdas 3 * i + 1, creal(in), cimag(in)); 126187962Sdas } 127187962Sdas 128187962Sdas feclearexcept(FE_ALL_EXCEPT); 129187962Sdas if (!cfpequal(libconj(in), expected)) { 130187962Sdas printf("not ok %d\t# conj(%#.2g + %#.2gI): " 131187962Sdas "wrong value\n", 132187962Sdas 3 * i + 2, creal(in), cimag(in)); 133187962Sdas } else if (fetestexcept(FE_ALL_EXCEPT)) { 134187962Sdas printf("not ok %d\t# conj(%#.2g + %#.2gI): " 135187962Sdas "threw an exception\n", 136187962Sdas 3 * i + 2, creal(in), cimag(in)); 137187962Sdas } else { 138187962Sdas printf("ok %d\t\t# conj(%#.2g + %#.2gI)\n", 139187962Sdas 3 * i + 2, creal(in), cimag(in)); 140187962Sdas } 141187962Sdas 142187962Sdas feclearexcept(FE_ALL_EXCEPT); 143187962Sdas if (!cfpequal(libconjl(in), expected)) { 144187962Sdas printf("not ok %d\t# conjl(%#.2g + %#.2gI): " 145187962Sdas "wrong value\n", 146187962Sdas 3 * i + 3, creal(in), cimag(in)); 147187962Sdas } else if (fetestexcept(FE_ALL_EXCEPT)) { 148187962Sdas printf("not ok %d\t# conjl(%#.2g + %#.2gI): " 149187962Sdas "threw an exception\n", 150187962Sdas 3 * i + 3, creal(in), cimag(in)); 151187962Sdas } else { 152187962Sdas printf("ok %d\t\t# conjl(%#.2g + %#.2gI)\n", 153187962Sdas 3 * i + 3, creal(in), cimag(in)); 154187962Sdas } 155187962Sdas } 156187962Sdas 157187962Sdas return (0); 158187962Sdas} 159