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