Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:37

0001 /*
0002  *  @file
0003  *  @brief Test suite for fenv.h methods
0004  */
0005 
0006 /*
0007  * SPDX-License-Identifier: BSD-2-Clause
0008  *
0009  * Copyright (C) 2020 Eshan Dhawan
0010  * Copyright (C) 2019 Vaibhav Gupta
0011  *
0012  * Redistribution and use in source and binary forms, with or without
0013  * modification, are permitted provided that the following conditions
0014  * are met:
0015  * 1. Redistributions of source code must retain the above copyright
0016  *    notice, this list of conditions and the following disclaimer.
0017  * 2. Redistributions in binary form must reproduce the above copyright
0018  *    notice, this list of conditions and the following disclaimer in the
0019  *    documentation and/or other materials provided with the distribution.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0031  * POSSIBILITY OF SUCH DAMAGE.
0032  */
0033 
0034 #ifdef HAVE_CONFIG_H
0035 #include "config.h"
0036 #endif
0037 
0038 /* header files are listed in lexical/lexicographical/alphabetical order */
0039 
0040 #include <errno.h>
0041 #include <limits.h>
0042 #include <math.h>
0043 #include <fenv.h>       /* contains declarations of fenv methods */
0044 #include <stddef.h>
0045 #include <stdint.h>
0046 #include <stdio.h>
0047 #include <string.h>
0048 #include <rtems/test-info.h>
0049 #include <tmacros.h>
0050 #include <float.h>
0051 
0052 const char rtems_test_name[] = "PSXFENV 01";
0053 
0054 /* forward declarations to avoid warnings */
0055 rtems_task Init( rtems_task_argument ignored );
0056 
0057 /* Test Function Begins */
0058 rtems_task Init(rtems_task_argument ignored)
0059 {
0060   double a, b, c;
0061   int r;
0062   fexcept_t excepts;
0063   TEST_BEGIN();
0064 
0065   /*
0066    * 'FE_ALL_EXCEPT' will be defined only when 'feclearexcept()',
0067    * fegetexceptflag() , feraiseexcept(), fesetexceptflag() and
0068    * fetestexcept() functions are supported by the architecture.
0069    * Hence their testcases can be wrapped under #ifdef and #endif.
0070    */
0071   #ifdef FE_ALL_EXCEPT /* floating-point exceptions */
0072     r = fesetenv( FE_DFL_ENV );
0073     if ( r ) {
0074       printf( "fesetenv ==> %d\n", r);
0075     }
0076     rtems_test_assert( r == 0 );
0077 
0078     /* Test feclearexcept() and fetestexcept() in one go. */
0079     r = feclearexcept( FE_ALL_EXCEPT );
0080     if ( r ) {
0081       printf( "feclearexcept ==> 0x%x\n", r );
0082     }
0083     rtems_test_assert( r == 0 );
0084 
0085     r = fetestexcept( FE_ALL_EXCEPT );
0086     if ( r ) {
0087       printf( "fetestexcept ==> 0x%x\n", r );
0088     }
0089     rtems_test_assert( r == 0 );
0090 
0091     /* Test 'FE_DIVBYZERO'
0092      * Divide by zero and confirm fetestexcept() */
0093     a = 0.0;
0094     b = 1.0;
0095     c = b/a;
0096     (void) c;
0097     /* Test fegetexceptflag() and fesetexceptflag() */
0098     r = fegetexceptflag( &excepts, FE_ALL_EXCEPT );
0099     if ( r ) {
0100       printf( "fegetexceptflag ==> 0x%x\n", r );
0101     }
0102     rtems_test_assert( r == 0 );
0103 
0104     r = fesetexceptflag( &excepts, FE_ALL_EXCEPT );
0105     if ( r ) {
0106       printf( "fesetexceptflag ==> 0x%x\n", r );
0107     }
0108     rtems_test_assert( r == 0 );
0109 
0110     /* Test for fegetround() and fesetround()
0111      * They have four main macros to be tested separated by ifdef
0112      * Since not all architectures support them
0113      * The test case gets and sets the rounding directions */
0114   #ifdef FE_TONEAREST /* Rounding direction TONEAREST */
0115     rtems_test_assert( fegetround() == FE_TONEAREST );
0116   #endif /*rounding direction TONEAREST */
0117   #ifdef FE_TOWARDZERO /* rounding direction TOWARDZERO */
0118     r = fesetround( FE_TOWARDZERO );
0119     if ( r ) {
0120       printf( "fesetround ==> 0x%x\n", r );
0121     }
0122     rtems_test_assert( r == 0 );
0123     rtems_test_assert( fegetround() == FE_TOWARDZERO );
0124   #endif/*rounding direction TOWARDZERO */
0125   #ifdef FE_DOWNWARD /* rounding direction DOWNWARD */
0126     r = fesetround( FE_DOWNWARD );
0127     if ( r ) {
0128       printf( "fesetround ==> 0x%x\n", r );
0129     }
0130     rtems_test_assert( r == 0 );
0131     rtems_test_assert( fegetround() == FE_DOWNWARD );
0132   #endif /* rounding direction DOWNWARD */
0133   #ifdef FE_UPWARD /* rounding direction UPWARD */
0134     r = fesetround( FE_UPWARD );
0135     if ( r ) {
0136       printf( "fesetround ==> 0x%x\n", r );
0137     }
0138     rtems_test_assert( r == 0 );
0139     rtems_test_assert( fegetround() == FE_UPWARD );
0140   #endif /* rounding direction upward */
0141   #ifdef FE_TONEAREST /* rounding direction TONEAREST */
0142     r = fesetround( FE_TONEAREST );
0143     if ( r ) {
0144       printf( "fesetround ==> 0x%x\n", r );
0145     }
0146     rtems_test_assert( r == 0 );
0147   #endif /* rounding direction TONEAREST */
0148 
0149   #ifdef FE_DIVBYZERO /* divide by zero exeption */
0150     r = feraiseexcept( FE_DIVBYZERO ) ;
0151     rtems_test_assert( fetestexcept( FE_DIVBYZERO ) );
0152   #endif /* divide by zero exeption */
0153 
0154     /* Test 'FE_INEXACT' */
0155     a = 10.0;
0156     c = b/a;
0157 
0158   #endif /* floating-point exceptions */
0159 
0160   TEST_END();
0161   rtems_test_exit(0);
0162 }
0163 
0164 /* NOTICE: the clock driver is explicitly disabled */
0165 
0166 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0167 #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
0168 
0169 #define CONFIGURE_MAXIMUM_TASKS                  1
0170 
0171 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 6
0172 
0173 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0174 
0175 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0176 
0177 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
0178 
0179 #define CONFIGURE_INIT
0180 #include <rtems/confdefs.h>
0181 /* end of file */