Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

test.h

00001 /*
00002 *  Name:      test.h
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Definitons for testing C and C++ programs
00005 *  Date:      $Date: 2003/04/14 00:18:31 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1994-2001  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #ifndef _TEST__
00027 #define _TEST__
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <time.h>
00033 
00034 
00035 /*
00036 *  E X P O R T E D   V A R I A B L E S
00037 */
00038 
00043 extern FILE*   hptTestTargetFile;
00044 
00045 
00046 #define TEST_AVAILABLE_MEMORY  (0)
00047 #ifdef __WATCOMC__
00048 #include <malloc.h>
00049 #define TEST_AVAILABLE_MEMORY  _memavl()
00050 #endif
00051 #ifdef __BORLAND__
00052 #include <malloc.h>
00053 #define TEST_AVAILABLE_MEMORY  coreleft()
00054 #endif
00055 
00056 #define TEST_NORMAL_FORMAT "%s \"%s\" [file <%s> line <%i>]\n"
00057 #define TEST_WATCH_FORMAT  "%s [cpu-time <%gs> total-time <%gs> file <%s> line <%i>] \"%s\"\n"
00058 #define TEST_MEMORY_FORMAT "%s [memory-difference <%ld> file <%s> line <%i>]\n"
00059 
00060 
00069 #define TEST_INIT(pccTEXT)                                             \
00070         long unsigned int   hluiInitialFreeMemory;                     \
00071         clock_t             htCpuTime;                                 \
00072         time_t              htInitialTime;                             \
00073         time_t              htEndTime;                                 \
00074         int                 hiExitCode = 0;                            \
00075                                                                        \
00076         {                                                              \
00077           hptTestTargetFile = stderr;                                  \
00078           if ( getenv ("TESTMODE") )                                   \
00079           {                                                            \
00080             if ( !std::strcmp (getenv ("TESTMODE"), "silent_mode") )   \
00081             {                                                          \
00082               hptTestTargetFile = NULL;                                \
00083             }                                                          \
00084           }                                                            \
00085           if ( hptTestTargetFile )                                     \
00086           {                                                            \
00087             fprintf (hptTestTargetFile, "#TEST INIT (%s)\n", pccTEXT); \
00088           }                                                            \
00089           hluiInitialFreeMemory = TEST_AVAILABLE_MEMORY;               \
00090           htCpuTime             = clock();                             \
00091           htInitialTime         = time (NULL);                         \
00092           htEndTime             = time (NULL);                         \
00093         }
00094 
00095 #define TEST_PASS
00096 #define TEST_FAIL                       { hiExitCode = 1; }
00097 #define TEST_FAIL_WITH_CODE(EXIT_CODE)  { hiExitCode = EXIT_CODE; }
00098 #define TEST_FUNCTION(FUNCTION)         { hiExitCode |= FUNCTION; }
00099 #define TEST_IN_SILENT_MODE             ( hptTestTargetFile == NULL )
00100 #define TEST_START_WATCH               \
00101         {                              \
00102           htCpuTime     = clock();     \
00103           htInitialTime = time (NULL); \
00104         }
00105 #define TEST_STOP_WATCH                    \
00106         {                                  \
00107           htCpuTime = clock() - htCpuTime; \
00108           htEndTime = time (NULL);         \
00109         }
00110 #define TEST_DISPLAY_WATCH(pccINFO)                        \
00111         if ( hptTestTargetFile )                           \
00112         {                                                  \
00113           fprintf ( hptTestTargetFile                   ,  \
00114                     TEST_WATCH_FORMAT                   ,  \
00115                     ">TIME STATS"                       ,  \
00116                     (double) htCpuTime / CLOCKS_PER_SEC ,  \
00117                     difftime (htEndTime, htInitialTime) ,  \
00118                     __FILE__                            ,  \
00119                     __LINE__                            ,  \
00120                     pccINFO                             ); \
00121         }
00122 
00123 #define TEST_NUMBERS(gREAL,g)                        \
00124         if ( (g) != (gREAL) )                        \
00125         {                                            \
00126           TEST_WRITE_MESSAGE ( ">ERROR IN"        ,  \
00127                                #g                 ,  \
00128                                __FILE__           ,  \
00129                                __LINE__           ,  \
00130                                TEST_NORMAL_FORMAT ); \
00131           TEST_FAIL;                                 \
00132         }
00133 
00134 #define TEST_STRINGS(cREAL,c)                        \
00135         if ( std::strcmp (c, cREAL) != 0 )           \
00136         {                                            \
00137           TEST_WRITE_MESSAGE ( ">ERROR IN"        ,  \
00138                                c                  ,  \
00139                                __FILE__           ,  \
00140                                __LINE__           ,  \
00141                                TEST_NORMAL_FORMAT ); \
00142           TEST_FAIL;                                 \
00143         }
00144 
00145 #define TEST_MEMORY_STATUS                                                      \
00146         if ( TEST_AVAILABLE_MEMORY != hluiInitialFreeMemory )                   \
00147         {                                                                       \
00148           TEST_WRITE_MESSAGE ( ">MEMORY ERROR"                               ,  \
00149                                hluiInitialFreeMemory - TEST_AVAILABLE_MEMORY ,  \
00150                                __FILE__                                      ,  \
00151                                __LINE__                                      ,  \
00152                                TEST_MEMORY_FORMAT                            ); \
00153           TEST_FAIL;                                                            \
00154         }
00155 
00156 #define TEST_WRITE_MESSAGE(pccDESC, xOP, pccFILE, iLINE, pccFORMAT)               \
00157         {                                                                         \
00158           if ( hptTestTargetFile )                                                \
00159           {                                                                       \
00160             fprintf (hptTestTargetFile, pccFORMAT, pccDESC, xOP, pccFILE, iLINE); \
00161           }                                                                       \
00162         }
00163 
00170 #define TEST_WRITE_INFO(pccFORMAT, pccTEXT)                            \
00171         {                                                              \
00172           if ( !hptTestTargetFile  )                                   \
00173           {                                                            \
00174             hptTestTargetFile = stderr;                                \
00175             if ( getenv ("TESTMODE") )                                 \
00176             {                                                          \
00177               if ( !std::strcmp (getenv ("TESTMODE"), "silent_mode") ) \
00178               {                                                        \
00179                 hptTestTargetFile = NULL;                              \
00180               }                                                        \
00181             }                                                          \
00182           }                                                            \
00183           if ( hptTestTargetFile )                                     \
00184           {                                                            \
00185             fprintf (hptTestTargetFile, "#INFO: ");                    \
00186             fprintf (hptTestTargetFile, pccFORMAT, pccTEXT);           \
00187             fprintf (hptTestTargetFile, "\n");                         \
00188           }                                                            \
00189         }
00190 
00196 #define TEST_END                                                                       \
00197         {                                                                              \
00198           if ( hptTestTargetFile )                                                     \
00199           {                                                                            \
00200             fprintf (hptTestTargetFile, "#TEST END\n");                                \
00201             if ( hptTestTargetFile != stderr )                                         \
00202             {                                                                          \
00203               fclose (hptTestTargetFile);                                              \
00204               hptTestTargetFile = NULL;                                                \
00205             }                                                                          \
00206             if ( hiExitCode )                                                          \
00207             {                                                                          \
00208               fprintf (hptTestTargetFile, "#TEST BAD RETURN CODE (%d)\n", hiExitCode); \
00209             }                                                                          \
00210           }                                                                            \
00211         }
00212 
00219 #define TEST_RETURN_CODE  { TEST_END; return hiExitCode; }
00220 
00221 
00222 #endif  /* not _TEST__ */

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18