1 /* Test for exception handling functions of libm */
18 Since not all architectures might define all exceptions, we define
19 a private set and map accordingly.
22 #define INEXACT_EXC 0x1
23 #define DIVBYZERO_EXC 0x2
24 #define UNDERFLOW_EXC 0x04
25 #define OVERFLOW_EXC 0x08
26 #define INVALID_EXC 0x10
28 (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC | \
31 static int count_errors;
33 /* Test whether a given exception was raised. */
35 test_single_exception (short int exception,
38 const char *flag_name)
40 if (exception & exc_flag)
42 if (fetestexcept (fe_flag))
43 printf (" Pass: Exception \"%s\" is set\n", flag_name);
46 printf (" Fail: Exception \"%s\" is not set\n", flag_name);
52 if (fetestexcept (fe_flag))
54 printf (" Fail: Exception \"%s\" is set\n", flag_name);
59 printf (" Pass: Exception \"%s\" is not set\n", flag_name);
65 test_exceptions (const char *test_name, short int exception)
67 printf ("Test: %s\n", test_name);
69 test_single_exception (exception, DIVBYZERO_EXC, FE_DIVBYZERO,
73 test_single_exception (exception, INVALID_EXC, FE_INVALID,
77 test_single_exception (exception, INEXACT_EXC, FE_INEXACT,
81 test_single_exception (exception, UNDERFLOW_EXC, FE_UNDERFLOW,
85 test_single_exception (exception, OVERFLOW_EXC, FE_OVERFLOW,
92 set_single_exc (const char *test_name, int fe_exc, fexcept_t exception)
96 strcpy (str, test_name);
97 strcat (str, ": set flag, with rest not set");
98 feclearexcept (FE_ALL_EXCEPT);
99 feraiseexcept (exception);
100 test_exceptions (str, fe_exc);
102 strcpy (str, test_name);
103 strcat (str, ": clear flag, rest also unset");
104 feclearexcept (exception);
105 test_exceptions (str, NO_EXC);
107 strcpy (str, test_name);
108 strcat (str, ": set flag, with rest set");
109 feraiseexcept (FE_ALL_EXCEPT ^ exception);
110 feraiseexcept (exception);
111 test_exceptions (str, ALL_EXC);
113 strcpy (str, test_name);
114 strcat (str, ": clear flag, leave rest set");
115 feclearexcept (exception);
116 test_exceptions (str, ALL_EXC ^ fe_exc);
122 /* clear all exceptions and test if all are cleared */
123 feclearexcept (FE_ALL_EXCEPT);
124 test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
127 /* raise all exceptions and test if all are raised */
128 feraiseexcept (FE_ALL_EXCEPT);
129 test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
131 feclearexcept (FE_ALL_EXCEPT);
135 set_single_exc ("Set/Clear FE_DIVBYZERO", DIVBYZERO_EXC, FE_DIVBYZERO);
138 set_single_exc ("Set/Clear FE_INVALID", INVALID_EXC, FE_INVALID);
141 set_single_exc ("Set/Clear FE_INEXACT", INEXACT_EXC, FE_INEXACT);
144 set_single_exc ("Set/Clear FE_UNDERFLOW", UNDERFLOW_EXC, FE_UNDERFLOW);
147 set_single_exc ("Set/Clear FE_OVERFLOW", OVERFLOW_EXC, FE_OVERFLOW);
155 /* _LIB_VERSION = _SVID;*/
159 printf ("\n%d errors occured.\n", count_errors);
162 printf ("\n All tests passed successfully.\n");