• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  This Software is provided under the Zope Public License (ZPL) Version 2.1.
3 
4  Copyright (c) 2009, 2010 by the mingw-w64 project
5 
6  See the AUTHORS file for the list of contributors to the mingw-w64 project.
7 
8  This license has been certified as open source. It has also been designated
9  as GPL compatible by the Free Software Foundation (FSF).
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14    1. Redistributions in source code must retain the accompanying copyright
15       notice, this list of conditions, and the following disclaimer.
16    2. Redistributions in binary form must reproduce the accompanying
17       copyright notice, this list of conditions, and the following disclaimer
18       in the documentation and/or other materials provided with the
19       distribution.
20    3. Names of the copyright holders must not be used to endorse or promote
21       products derived from this software without prior written permission
22       from the copyright holders.
23    4. The right to distribute this software or to use it for any purpose does
24       not give you the right to use Servicemarks (sm) or Trademarks (tm) of
25       the copyright holders.  Use of them is covered by separate agreement
26       with the copyright holders.
27    5. If any files are modified, you must cause the modified files to carry
28       prominent notices stating that you changed the files and the date of
29       any change.
30 
31  Disclaimer
32 
33  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESSED
34  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
36  EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
37  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44 
45 /* Testsuite functions for the complex functions of C99.  */
46 #ifndef _NEWCOMPLEX_TEST_H
47 #define _NEWCOMPLEX_TEST_H
48 
49 /* Support printing of long doubles.  */
50 #define __USE_MINGW_ANSI_STDIO 1
51 #include <stdio.h>
52 #include <math.h>
53 #include <complex.h>
54 
55 /* Default to double types.  */
56 #if !(defined (_NEW_COMPLEX_FLOAT) || defined (_NEW_COMPLEX_DOUBLE) || defined (_NEW_COMPLEX_LDOUBLE))
57 #define _NEW_COMPLEX_DOUBLE 1
58 #endif
59 
60 /* Grab the internals for things like __FLT_PI, etc.  */
61 #include "complex_internal.h"
62 
63 /* Declare comparisons etc.  */
64 #if defined(_NEW_COMPLEX_DOUBLE)
65 # define __FLT_CMP	compare_dbl
66 # define __FLT_CCMP	compare_cdbl
67 # define __FLT_DELTA	(2.0 * __DBL_EPSILON__)
68 # define __FLT_FMT	"%0.32g"
69 #elif defined (_NEW_COMPLEX_FLOAT)
70 # define __FLT_CMP	compare_flt
71 # define __FLT_CCMP	compare_cflt
72 # define __FLT_DELTA	(2.0F * __FLT_EPSILON__)
73 # define __FLT_FMT	 "%0.32g"
74 #elif defined (_NEW_COMPLEX_LDOUBLE)
75 # define __FLT_CMP	compare_ldbl
76 # define __FLT_CCMP	compare_cldbl
77 # define __FLT_DELTA	(2.0L * __LDBL_EPSILON__)
78 # define __FLT_FMT	 "%0.32Lg"
79 #endif
80 
81 struct complex_test
82 {
83   /* The input value to the complex function */
84   __FLT_TYPE input_r;
85   __FLT_TYPE input_i;
86   /* The expected value from the complex function */
87   __FLT_TYPE expected_r;
88   __FLT_TYPE expected_i;
89   /* Some C99 signs are +/-.  When these are true, don't compare signs */
90   int real_sign_dontcare;
91   int imag_sign_dontcare;
92 };
93 
94 /* Definition of the function under test.  */
95 typedef __FLT_TYPE complex (*complex_function)(__FLT_TYPE complex z);
96 typedef __FLT_TYPE         (*real_function)(__FLT_TYPE complex z);
97 
98 /* Some possible behaviors of the function.  */
99 /* func (conj (z)) = conj (func (z))  */
100 #define FUNC_BEHAVIOR_CONJ 0x0001
101 /* func (-z) = -func (z)  */
102 #define FUNC_BEHAVIOR_ODD  0x0002
103 /* func (-z) = func (z)  */
104 #define FUNC_BEHAVIOR_EVEN 0x0004
105 
106 /* Structure to hold info about the complex function.  */
107 struct test_func_desc
108 {
109   const char *fname;
110   complex_function complex_test;
111   real_function    real_test;
112   int              behavior;
113 };
114 
115 /**
116  * Compares expected and test values with a delta tolerance.
117  * 0 for pass, 1 for fail.
118  * TODO:
119  * Tune comparison precision
120  **/
121 
122 static int
__FLT_CMP(__FLT_TYPE expected,__FLT_TYPE in,int sign_dontcare)123 __FLT_CMP (__FLT_TYPE expected, __FLT_TYPE in, int sign_dontcare)
124 {
125   int bad = 0;
126   if (isnan (expected) || isnan (in))
127     {
128       /* NaN comparisons */
129       bad = isnan (expected) != isnan (in);
130     }
131   else if (isfinite (expected) && isfinite (in))
132     {
133       /* Finite value tests */
134       if (expected == 0 && in == 0)
135 	{
136 	  /* Signed zero comparison */
137 	  bad = (sign_dontcare) ? 0 : signbit (expected) != signbit (in);
138 	}
139       else
140 	{
141 	  bad = (expected > in) ?
142 		((expected - in) > __FLT_DELTA) :
143 		((in - expected) > __FLT_DELTA);
144 	}
145     }
146   else
147     {
148       /* Either expected or in is an infinite */
149       bad = !isinf(expected) || !isinf(in);
150       if (!bad && !sign_dontcare)
151 	bad =  signbit (expected) != signbit (in);
152     }
153 
154   return bad;
155 }
156 
157 static int
__FLT_CCMP(complex __FLT_TYPE expected,complex __FLT_TYPE in,int real_sign_dontcare,int imag_sign_dontcare)158 __FLT_CCMP (complex __FLT_TYPE expected,
159 	    complex __FLT_TYPE in,
160 	    int real_sign_dontcare,
161 	    int imag_sign_dontcare)
162 {
163   return (__FLT_CMP (__FLT_ABI(creal)(expected), __FLT_ABI(creal)(in), real_sign_dontcare) ||
164 	  __FLT_CMP (__FLT_ABI(cimag)(expected), __FLT_ABI(cimag)(in), imag_sign_dontcare));
165 }
166 
167 /* Runs comparison and (DEBUG) print error.
168    returns 1 on fail, 0 on success.  */
169 static int
error_check_test(const struct test_func_desc * fdesc,const char * testdescription,int testindex,__FLT_TYPE complex test,__FLT_TYPE complex expected,int real_sign_dontcare,int imag_sign_dontcare)170 error_check_test (const struct test_func_desc *fdesc,
171 		  const char* testdescription,
172 		  int testindex,
173 		  __FLT_TYPE complex test,
174 		  __FLT_TYPE complex expected,
175 		  int real_sign_dontcare,
176 		  int imag_sign_dontcare)
177 {
178   int bad = 0;
179   __FLT_TYPE complex actual;
180   if (fdesc->complex_test)
181     {
182       actual = fdesc->complex_test (test);
183       bad = __FLT_CCMP (expected, actual, real_sign_dontcare, imag_sign_dontcare);
184       if (bad)
185 	{
186 	  printf("%s %s test index %d failed on input " __FLT_FMT " + " __FLT_FMT "i.\n"
187 		"\tExpected: " __FLT_FMT " + " __FLT_FMT "i\n"
188 		"\tActual: " __FLT_FMT " + " __FLT_FMT "i (epsilon=" __FLT_FMT ")\n",
189 		fdesc->fname, testdescription, testindex,
190 		__FLT_ABI(creal) (test),    __FLT_ABI(cimag) (test),
191 		__FLT_ABI(creal) (expected), __FLT_ABI(cimag) (expected),
192 		__FLT_ABI(creal) (actual),   __FLT_ABI(cimag) (actual),
193 		__FLT_DELTA);
194 	}
195     }
196   else
197     {
198       __real__ actual = fdesc->real_test (test);
199       bad = __FLT_CMP (__real__ expected, actual, real_sign_dontcare);
200       if (bad)
201 	{
202 	  printf("%s %s test index %d failed on input " __FLT_FMT " + " __FLT_FMT "i.\n"
203 		"\tExpected: " __FLT_FMT "\n"
204 		"\tActual: " __FLT_FMT " (epsilon=" __FLT_FMT ")\n",
205 		fdesc->fname, testdescription, testindex,
206 		__FLT_ABI(creal) (test),    __FLT_ABI(cimag) (test),
207 		__FLT_ABI(creal) (expected),
208 		__FLT_ABI(creal) (actual),
209 		__FLT_DELTA);
210 	}
211     }
212 
213   return bad;
214 }
215 
216 /* Tests of conjugates, function should satisfy
217    func (conj (z)) = conj (func (z)).  func (conj (z)) should return
218    the conj of the expected, since expected is the expected result of
219    func (z).  */
220 static int
conjugate_tests(const struct test_func_desc * fdesc,int testindex,__FLT_TYPE complex test,__FLT_TYPE complex expected,int real_sign_dontcare,int imag_sign_dontcare)221 conjugate_tests (const struct test_func_desc *fdesc,
222 		 int testindex,
223 		 __FLT_TYPE complex test,
224 		 __FLT_TYPE complex expected,
225 		 int real_sign_dontcare,
226 		 int imag_sign_dontcare)
227 {
228   int ret = 0;
229 
230   /* Conjugate test 1: verify result of func (conj (z)) is conj (expected) */
231   __FLT_TYPE complex ctest =    __FLT_ABI(conj) (test);
232   __FLT_TYPE complex cexpected = __FLT_ABI(conj) (expected);
233 
234   ret |= error_check_test (fdesc, "Congugate of expected", testindex, ctest, cexpected,
235 			  real_sign_dontcare, imag_sign_dontcare);
236   return ret;
237 }
238 
239 /* Tests of odd functions, function should satisfy
240    func (-z) = -func (z).  func (-z) should result in the -expected result,
241    since expected is the expected result of func (z).  */
242 static int
odd_tests(const struct test_func_desc * fdesc,int testindex,__FLT_TYPE complex test,__FLT_TYPE complex expected,int real_sign_dontcare,int imag_sign_dontcare)243 odd_tests (const struct test_func_desc *fdesc,
244 	   int testindex,
245 	   __FLT_TYPE complex test,
246 	   __FLT_TYPE complex expected,
247 	   int real_sign_dontcare,
248 	   int imag_sign_dontcare)
249 {
250   int ret = 0;
251 
252   /* Oddness test 1: verify result of func (-z) is -expected */
253   __FLT_TYPE complex ctest =    -test;
254   __FLT_TYPE complex cexpected = -expected;
255 
256   ret |= error_check_test (fdesc, "Oddness", testindex, ctest, cexpected,
257 			  real_sign_dontcare, imag_sign_dontcare);
258 
259   /* Need to do conjugate of the odd values as well */
260   ret |= conjugate_tests(fdesc, testindex, ctest, cexpected, real_sign_dontcare,
261 			imag_sign_dontcare);
262 
263   return ret;
264 }
265 
266 /* Tests of even functions, function should satisfy
267    func (-z) = func (z).  func (-z) should result in the expected result,
268    since expected is the expected result of func (z).  */
269 static int
even_tests(const struct test_func_desc * fdesc,int testindex,__FLT_TYPE complex test,__FLT_TYPE complex expected,int real_sign_dontcare,int imag_sign_dontcare)270 even_tests (const struct test_func_desc *fdesc,
271 	   int testindex,
272 	   __FLT_TYPE complex test,
273 	   __FLT_TYPE complex expected,
274 	   int real_sign_dontcare,
275 	   int imag_sign_dontcare)
276 {
277   int ret = 0;
278 
279   /* Evenness test 1: verify result of func (-z) is expected */
280   __FLT_TYPE complex ctest =    -test;
281   __FLT_TYPE complex cexpected = expected;
282 
283   ret |= error_check_test (fdesc, "Evenness", testindex, ctest, cexpected,
284 			  real_sign_dontcare, imag_sign_dontcare);
285 
286   /* Need to do conjugate of the even values as well */
287   ret |= conjugate_tests(fdesc, testindex, ctest, cexpected, real_sign_dontcare,
288 			imag_sign_dontcare);
289   return ret;
290 }
291 
292 /* Actually run the tests.  */
293 static int
runtests(const struct test_func_desc * testfunc,const struct complex_test * testarray,int testcount)294 runtests (const struct test_func_desc *testfunc,
295 	  const struct complex_test *testarray,
296 	  int testcount)
297 {
298   int i;
299   int ret = 0;
300 
301   for (i = 0; i < testcount; i++)
302     {
303       __FLT_TYPE complex test;
304       __FLT_TYPE complex expected;
305       __real__ test = testarray[i].input_r;
306       __imag__ test = testarray[i].input_i;
307       __real__ expected = testarray[i].expected_r;
308       __imag__ expected = testarray[i].expected_i;
309 
310       /* Main test --- specified in Annex G, et al. */
311       ret |= error_check_test (testfunc, "Annex G", i, test, expected,
312 		testarray[i].real_sign_dontcare, testarray[i].imag_sign_dontcare);
313 
314       if (testfunc->behavior & FUNC_BEHAVIOR_CONJ)
315 	{
316 	  ret |= conjugate_tests (testfunc, i, test, expected,
317 		 testarray[i].real_sign_dontcare, testarray[i].imag_sign_dontcare);
318 	}
319 
320       if (testfunc->behavior & FUNC_BEHAVIOR_ODD)
321 	{
322 	  ret |= odd_tests (testfunc, i, test, expected,
323 		 testarray[i].real_sign_dontcare, testarray[i].imag_sign_dontcare);
324 	}
325 
326       if (testfunc->behavior & FUNC_BEHAVIOR_EVEN)
327 	{
328 	  ret |= even_tests (testfunc, i, test, expected,
329 		 testarray[i].real_sign_dontcare, testarray[i].imag_sign_dontcare);
330 	}
331     }
332 
333   return ret;
334 }
335 
336 #define __FLT_STRING(x) #x
337 #define __FLT_STRINGIFY(x) __FLT_STRING(x)
338 
339 /**
340  * Defines the test function with a variable name to pass to RUN_TESTS.
341  * varname is the varname to use.
342  * funcame is the function to test.
343  * behavior is the flags of how the function behaves.
344  */
345 #define DEFINE_TEST_FUNCTION(varname, funcname, behavior)	\
346   struct test_func_desc varname =				\
347   {								\
348     __FLT_STRINGIFY(__FLT_ABI(funcname)),			\
349     __FLT_ABI(funcname),					\
350     NULL,							\
351     behavior							\
352   }
353 
354 /**
355  * Defines the test function with a variable name to pass to RUN_TESTS.
356  * This version is for functions that return a real value.
357  * varname is the varname to use.
358  * funcame is the function to test.
359  * behavior is the flags of how the function behaves.
360  */
361 #define DEFINE_TEST_FUNCTION_REAL(varname, funcname, behavior)	\
362   struct test_func_desc varname =					\
363   {									\
364     __FLT_STRINGIFY(__FLT_ABI(funcname)),				\
365     NULL,								\
366     __FLT_ABI(funcname),						\
367     behavior								\
368   }
369 
370 
371 /* Do not use semicolons with these macros.  */
372 
373 /**
374  * Starts a declaration of tests, with a variable name.
375  */
376 #define TESTS_START(v) const struct complex_test v[] = {
377 
378 /**
379  * Starts a declaration of tests, with a default variable name, for use with
380  * RUN_DEFAULT_TESTS.
381  */
382 #define DEFAULT_TESTS_START TESTS_START(_default_tests)
383 
384 /**
385  * Macro to fill a complex_test structure with values while inside a
386  * TESTS block.
387  */
388 #define DEFINE_TEST(input_r, input_i, expected_r, expected_i, real_sign_dontcare, imag_sign_dontcare)	\
389   { input_r, input_i, expected_r, expected_i, real_sign_dontcare, imag_sign_dontcare },
390 
391 /**
392  * Closes a test block opened with the TESTS_START macros.
393  */
394 #define TESTS_END };
395 
396 /**
397  * Runs the tests specified in testarray using the specified test function
398  * definition.
399  * testfunc is the function definition specification defined by one of the
400  * DEFINE_TEST_FUNCTION macros.
401  * testarray is the test array defined by a TESTS_START macro.
402  */
403 #define RUN_TESTS(testfunc, testarray) \
404   (runtests(&testfunc, testarray, (sizeof (testarray) / sizeof (*testarray))))
405 
406 /**
407  * Runs the tests specified in testarray using the test function
408  * definition declared with DEFINE_DEFAULT_TEST_FUNCTION macros.
409  * funcname is the function name passed to the DEFAULT_TEST_FUNCTION* macro.
410  * testarray is the test array defined by the TESTS_START macro.
411  */
412 #define RUN_FUNCTION_TESTS(funcname, testarray) \
413   RUN_TESTS(_test_##funcname, testarray)
414 
415 /**
416  * Runs the tests specified by DEFAULT_TESTS testarray using the test function
417  * definition declared with DEFINE_DEFAULT_TEST_FUNCTION macros.
418  * funcname is the function name passed to the DEFAULT_TEST_FUNCTION* macro.
419  */
420 #define RUN_DEFAULT_TESTS(funcname) \
421   RUN_TESTS(_test_##funcname, _default_tests)
422 
423 /**
424  * Default test functions available.
425  */
426 
427 /**
428  * Defines a test function with a default variable name for use with the
429  * RUN_DEFAULT_TESTS macro.
430  * funcname is the function family to test.
431  * behavior is the function's behavior.
432  */
433 #define _DEFINE_DEFAULT_TEST_FUNCTION(funcname, behavior) \
434 	DEFINE_TEST_FUNCTION(_test_##funcname, funcname, behavior)
435 
436 /**
437  * Defines a test function with a default variable name for use with the
438  * RUN_DEFAULT_TESTS macro.  This version is for functions that return
439  * a real value.
440  * funcname is the function family to test.
441  * behavior is the function's behavior.
442  */
443 #define _DEFINE_DEFAULT_TEST_FUNCTION_REAL(funcname, behavior) \
444 	DEFINE_TEST_FUNCTION_REAL(_test_##funcname, funcname, behavior)
445 
446 #endif  /* _NEWCOMPLEX_TEST_H */
447