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