1/*- 2 * Copyright (c) 2007 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 --- 15 unchanged lines hidden (view full) --- 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * Tests for csqrt{,f}() 29 */ 30 31#include <sys/cdefs.h> |
32__FBSDID("$FreeBSD: head/tools/regression/lib/msun/test-csqrt.c 251241 2013-06-02 04:30:03Z das $"); |
33 34#include <assert.h> 35#include <complex.h> 36#include <float.h> 37#include <math.h> 38#include <stdio.h> 39 |
40#include "test-utils.h" 41 |
42#define N(i) (sizeof(i) / sizeof((i)[0])) 43 44/* 45 * This is a test hook that can point to csqrtl(), _csqrt(), or to _csqrtf(). 46 * The latter two convert to float or double, respectively, and test csqrtf() 47 * and csqrt() with the same arguments. 48 */ 49long double complex (*t_csqrt)(long double complex); --- 10 unchanged lines hidden (view full) --- 60{ 61 62 return (csqrt((double complex)d)); 63} 64 65#pragma STDC CX_LIMITED_RANGE off 66 67/* |
68 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0. 69 * Fail an assertion if they differ. 70 */ 71static void 72assert_equal(long double complex d1, long double complex d2) 73{ 74 |
75 assert(cfpequal(d1, d2)); |
76} 77 78/* 79 * Test csqrt for some finite arguments where the answer is exact. 80 * (We do not test if it produces correctly rounded answers when the 81 * result is inexact, nor do we check whether it throws spurious 82 * exceptions.) 83 */ --- 44 unchanged lines hidden (view full) --- 128 int i, j; 129 130 for (i = 0; i < N(tests); i += 4) { 131 for (j = 0; j < N(mults); j++) { 132 a = tests[i] * mults[j] * mults[j]; 133 b = tests[i + 1] * mults[j] * mults[j]; 134 x = tests[i + 2] * mults[j]; 135 y = tests[i + 3] * mults[j]; |
136 assert(t_csqrt(CMPLXL(a, b)) == CMPLXL(x, y)); |
137 } 138 } 139 140} 141 142/* 143 * Test the handling of +/- 0. 144 */ 145static void 146test_zeros() 147{ 148 |
149 assert_equal(t_csqrt(CMPLXL(0.0, 0.0)), CMPLXL(0.0, 0.0)); 150 assert_equal(t_csqrt(CMPLXL(-0.0, 0.0)), CMPLXL(0.0, 0.0)); 151 assert_equal(t_csqrt(CMPLXL(0.0, -0.0)), CMPLXL(0.0, -0.0)); 152 assert_equal(t_csqrt(CMPLXL(-0.0, -0.0)), CMPLXL(0.0, -0.0)); |
153} 154 155/* 156 * Test the handling of infinities when the other argument is not NaN. 157 */ 158static void 159test_infinities() 160{ --- 5 unchanged lines hidden (view full) --- 166 INFINITY, 167 -INFINITY, 168 }; 169 170 int i; 171 172 for (i = 0; i < N(vals); i++) { 173 if (isfinite(vals[i])) { |
174 assert_equal(t_csqrt(CMPLXL(-INFINITY, vals[i])), 175 CMPLXL(0.0, copysignl(INFINITY, vals[i]))); 176 assert_equal(t_csqrt(CMPLXL(INFINITY, vals[i])), 177 CMPLXL(INFINITY, copysignl(0.0, vals[i]))); |
178 } |
179 assert_equal(t_csqrt(CMPLXL(vals[i], INFINITY)), 180 CMPLXL(INFINITY, INFINITY)); 181 assert_equal(t_csqrt(CMPLXL(vals[i], -INFINITY)), 182 CMPLXL(INFINITY, -INFINITY)); |
183 } 184} 185 186/* 187 * Test the handling of NaNs. 188 */ 189static void 190test_nans() 191{ 192 |
193 assert(creall(t_csqrt(CMPLXL(INFINITY, NAN))) == INFINITY); 194 assert(isnan(cimagl(t_csqrt(CMPLXL(INFINITY, NAN))))); |
195 |
196 assert(isnan(creall(t_csqrt(CMPLXL(-INFINITY, NAN))))); 197 assert(isinf(cimagl(t_csqrt(CMPLXL(-INFINITY, NAN))))); |
198 |
199 assert_equal(t_csqrt(CMPLXL(NAN, INFINITY)), 200 CMPLXL(INFINITY, INFINITY)); 201 assert_equal(t_csqrt(CMPLXL(NAN, -INFINITY)), 202 CMPLXL(INFINITY, -INFINITY)); |
203 |
204 assert_equal(t_csqrt(CMPLXL(0.0, NAN)), CMPLXL(NAN, NAN)); 205 assert_equal(t_csqrt(CMPLXL(-0.0, NAN)), CMPLXL(NAN, NAN)); 206 assert_equal(t_csqrt(CMPLXL(42.0, NAN)), CMPLXL(NAN, NAN)); 207 assert_equal(t_csqrt(CMPLXL(-42.0, NAN)), CMPLXL(NAN, NAN)); 208 assert_equal(t_csqrt(CMPLXL(NAN, 0.0)), CMPLXL(NAN, NAN)); 209 assert_equal(t_csqrt(CMPLXL(NAN, -0.0)), CMPLXL(NAN, NAN)); 210 assert_equal(t_csqrt(CMPLXL(NAN, 42.0)), CMPLXL(NAN, NAN)); 211 assert_equal(t_csqrt(CMPLXL(NAN, -42.0)), CMPLXL(NAN, NAN)); 212 assert_equal(t_csqrt(CMPLXL(NAN, NAN)), CMPLXL(NAN, NAN)); |
213} 214 215/* 216 * Test whether csqrt(a + bi) works for inputs that are large enough to 217 * cause overflow in hypot(a, b) + a. In this case we are using 218 * csqrt(115 + 252*I) == 14 + 9*I 219 * scaled up to near MAX_EXP. 220 */ 221static void 222test_overflow(int maxexp) 223{ 224 long double a, b; 225 long double complex result; 226 227 a = ldexpl(115 * 0x1p-8, maxexp); 228 b = ldexpl(252 * 0x1p-8, maxexp); |
229 result = t_csqrt(CMPLXL(a, b)); |
230 assert(creall(result) == ldexpl(14 * 0x1p-4, maxexp / 2)); 231 assert(cimagl(result) == ldexpl(9 * 0x1p-4, maxexp / 2)); 232} 233 234int 235main(int argc, char *argv[]) 236{ 237 --- 58 unchanged lines hidden --- |