test-stuff.c

00001 /*
00002  * Created 20010320 by bstanley to hold only those
00003  * testing functions which are independent of the rest of
00004  * the GNUCash system.
00005  *
00006  * This allows me to compile simple test programs standalone...
00007  *
00008  */
00009 
00010 
00011 #include "config.h"
00012 
00013 #include <unistd.h>
00014 #include <sys/types.h>
00015 #include <sys/stat.h>
00016 #include <fcntl.h>
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <string.h>
00020 #include <glib.h>
00021 #include "test-stuff.h"
00022 
00023 void vsuccess_args(
00024                 const char *test_title,
00025                 const char *file,
00026                 int line,
00027                 const char *format,
00028                 va_list ap);
00029 
00030 void vfailure_args(
00031                 const char *test_title,
00032                 const char *file,
00033                 int line,
00034                 const char *format,
00035                 va_list ap);
00036 
00037 static guint successes;
00038 static guint failures;
00039 static gboolean success_should_print = FALSE;
00040 
00041 void
00042 success_call(
00043                 const char *test_title,
00044                 const char* file,
00045                 int line )
00046 {
00047         success_args( test_title, file, line, "" );
00048 }
00049 
00050 void
00051 success_args(
00052                 const char *test_title,
00053                 const char *file,
00054                 int line,
00055                 const char *format,
00056                 ... )
00057 {
00058         va_list ap;
00059         va_start(ap,format);
00060         vsuccess_args( test_title, file, line, format, ap );
00061         va_end(ap);
00062 }
00063 
00064 void
00065 vsuccess_args(
00066                 const char *test_title,
00067                 const char *file,
00068                 int line,
00069                 const char *format,
00070                 va_list ap)
00071 {
00072         if( success_should_print ) {
00073                 printf("SUCCESS: %s, %s:%d ", test_title, file, line );
00074                 vprintf(format, ap);
00075                 printf("\n");
00076                 fflush(stdout);
00077         }
00078         ++successes;
00079 }
00080 
00081 void
00082 failure_call(
00083                 const char *test_title,
00084                 const char *file,
00085                 int line)
00086 {
00087         failure_args( test_title, file, line, "" );
00088 }
00089 
00090 
00091 void
00092 failure_args(
00093                 const char *test_title,
00094                 const char *file,
00095                 int line,
00096                 const char *format,
00097                 ... )
00098 {
00099         va_list ap;
00100         va_start(ap,format);
00101         vfailure_args( test_title, file, line, format, ap );
00102         va_end(ap);
00103 }
00104 void
00105 vfailure_args(
00106                 const char *test_title,
00107                 const char* file,
00108                 int line,
00109                 const char *format,
00110                 va_list ap)
00111 {
00112         printf("FAILURE %s %s:%d ", test_title, file, line );
00113         vprintf(format, ap);
00114         printf("\n");
00115         fflush(stdout);
00116 
00117         ++failures;
00118 }
00119 
00120 int
00121 get_rv(void)
00122 {
00123         if( failures ) {
00124                 return 1;
00125         }
00126         return 0;
00127 }
00128 
00129 gboolean
00130 do_test_call(
00131     gboolean result,
00132     const char* test_title,
00133     const char* filename,
00134     int line )
00135 {
00136         if( result ) {
00137                 success_args( test_title, filename, line, "" );
00138         } else {
00139                 failure_args( test_title, filename, line, "" );
00140         }
00141 
00142         return result;
00143 }
00144 
00145 gboolean
00146 do_test_args(
00147         gboolean result,
00148         const char* test_title,
00149         const char* filename,
00150         int line,
00151         const char* format,
00152         ... )
00153 {
00154         va_list ap;
00155         va_start(ap, format);
00156 
00157         if( result ) {
00158                 vsuccess_args( test_title, filename, line, format, ap );
00159         } else {
00160                 vfailure_args( test_title, filename, line, format, ap );
00161         }
00162         va_end(ap);
00163 
00164         return result;
00165 }
00166 
00167 void
00168 print_test_results(void)
00169 {
00170         guint total = successes+failures;
00171         if( total == 1 ) {
00172                 printf( "Executed 1 test." );
00173         } else {
00174                 printf("Executed %d tests.", successes+failures );
00175         }
00176         if( failures ) {
00177                 if( failures == 1 ) {
00178                         printf(" There was 1 failure." );
00179                 } else {
00180                         printf(" There were %d failures.", failures );
00181                 }
00182         } else {
00183                 printf(" All tests passed.");
00184         }
00185         printf("\n");
00186         fflush(stdout);
00187 }
00188 
00189 void
00190 set_success_print( gboolean in_should_print )
00191 {
00192         success_should_print = in_should_print;
00193 }
00194 
00195 gboolean
00196 get_random_boolean(void)
00197 {
00198   return get_random_int_in_range (0, 1);
00199 }
00200 
00201 gint
00202 get_random_int_in_range(int start, int end)
00203 {
00204   return CLAMP (start + (int)((double)(end - start + 1) * rand() /
00205                               (RAND_MAX + 1.0)),
00206                 start,
00207                 end);
00208 }
00209 
00210 static char *random_chars = NULL;
00211 
00212 static char plain_chars[] =
00213 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00214 "abcdefghijklmnopqrstuvwxyz"
00215 "1234567890"
00216 " ";
00217 
00218 static char funky_chars[] =
00219 ",.'\"`~!@#$%^*(){}[]/=?+-_\\|"
00220 "<>&"
00221 "\n\t";
00222 
00223 static int rcend = 0;
00224 
00225 void
00226 random_character_include_funky_chars (gboolean use_funky_chars)
00227 {
00228   g_free (random_chars);
00229 
00230   if (use_funky_chars)
00231     random_chars = g_strconcat (plain_chars, funky_chars, NULL);
00232   else
00233     random_chars = g_strdup (plain_chars);
00234 
00235   rcend = strlen (random_chars) - 1;
00236 }
00237 
00238 gchar
00239 get_random_character(void)
00240 {
00241     if (!rcend)
00242       random_character_include_funky_chars (TRUE);
00243 
00244     return random_chars[get_random_int_in_range(0, rcend)];
00245 }
00246 
00247 gchar *
00248 get_random_string_without(const char *exclude_chars)
00249 {
00250     gchar *ret;
00251     int len;
00252     int i;
00253 
00254     switch(get_random_int_in_range(0, 9))
00255     {
00256 /*     case 0: */
00257 /*         return ""; */
00258 /*     case 1: */
00259 /*         return NULL; */
00260 /*     case 2: */
00261 /*         len = get_random_int_in_range(1000, 5000); */
00262 /*         break; */
00263     case 3:
00264         len = get_random_int_in_range(100, 500);
00265         break;
00266     default:
00267         len = get_random_int_in_range(5, 20);
00268         break;
00269     }
00270     ret = g_new0(gchar, len);
00271 
00272     for (i = 0; i < len - 1; i++)
00273     {
00274         char c;
00275 
00276         do
00277         {
00278             c = get_random_character ();
00279         } while (exclude_chars && strchr (exclude_chars, c));
00280 
00281         ret[i] = c;
00282     }
00283 
00284     return g_strstrip (ret);
00285 }
00286 
00287 gchar *
00288 get_random_string(void)
00289 {
00290   return get_random_string_without (NULL);
00291 }
00292 
00293 gint64
00294 get_random_gint64(void)
00295 {
00296     gint64 ret = 0;
00297 
00298     ret = rand();
00299     ret <<= 32;
00300     ret += rand();
00301     
00302     return ret;
00303 }
00304 
00305 double
00306 get_random_double(void)
00307 {
00308     double d;
00309         guint  i;
00310 
00311         i = (guint)get_random_int_in_range(8,13);
00312         /* using 0.9 and 7 increases chances of getting lots of decimals */
00313     d = ((double)get_random_int_in_range(8,999999) * i * 0.9 / 7);
00314     return d;
00315 }
00316 
00317 const char*
00318 get_random_string_in_array(const char* str_list[])
00319 {
00320     int num;
00321 
00322          /* count number of items in list */
00323     for(num = 0; str_list[num] != NULL; num++)
00324         ;
00325          
00326          num = get_random_int_in_range(0, num-1);
00327          return str_list[num];
00328 }

Generated on Fri May 12 18:00:33 2006 for QOF by  doxygen 1.4.4