29 #include <sys/types.h> 36 #include "test-stuff.h" 38 void vsuccess_args (
const char *test_title,
40 int line,
const char *format, va_list ap);
42 void vfailure_args (
const char *test_title,
44 int line,
const char *format, va_list ap);
46 static guint successes;
47 static guint failures;
48 static gboolean success_should_print = FALSE;
51 success_call (
const char *test_title,
const char *file,
int line)
53 success_args (test_title, file, line,
"");
57 success_args (
const char *test_title,
58 const char *file,
int line,
const char *format, ...)
61 va_start (ap, format);
62 vsuccess_args (test_title, file, line, format, ap);
67 vsuccess_args (
const char *test_title,
68 const char *file,
int line,
const char *format, va_list ap)
70 if (success_should_print)
72 printf (
"SUCCESS: %s, %s:%d ", test_title, file, line);
81 failure_call (
const char *test_title,
const char *file,
int line)
83 failure_args (test_title, file, line,
"");
88 failure_args (
const char *test_title,
89 const char *file,
int line,
const char *format, ...)
92 va_start (ap, format);
93 vfailure_args (test_title, file, line, format, ap);
98 vfailure_args (
const char *test_title,
99 const char *file,
int line,
const char *format, va_list ap)
101 printf (
"FAILURE %s %s:%d ", test_title, file, line);
102 vprintf (format, ap);
120 do_test_call (gboolean result,
121 const char *test_title,
const char *filename,
int line)
125 success_args (test_title, filename, line,
"");
129 failure_args (test_title, filename, line,
"");
136 do_test_args (gboolean result,
137 const char *test_title,
138 const char *filename,
int line,
const char *format, ...)
141 va_start (ap, format);
145 vsuccess_args (test_title, filename, line, format, ap);
149 vfailure_args (test_title, filename, line, format, ap);
157 print_test_results (
void)
159 guint total = successes + failures;
162 printf (
"Executed 1 test.");
166 printf (
"Executed %d tests.", successes + failures);
172 printf (
" There was 1 failure.");
176 printf (
" There were %d failures.", failures);
181 printf (
" All tests passed.");
188 set_success_print (gboolean in_should_print)
190 success_should_print = in_should_print;
194 get_random_boolean (
void)
196 return get_random_int_in_range (0, 1);
200 get_random_int_in_range (
int start,
int end)
202 return CLAMP (start + (
int) ((
double) (end - start + 1) * rand () /
203 (RAND_MAX + 1.0)), start, end);
206 static char *random_chars = NULL;
208 static char plain_chars[] =
209 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 210 "abcdefghijklmnopqrstuvwxyz" "1234567890" " ";
212 static char funky_chars[] =
",.'\"`~!@#$%^*(){}[]/=?+-_\\|" "<>&" "\n\t";
214 static int rcend = 0;
217 random_character_include_funky_chars (gboolean use_funky_chars)
219 g_free (random_chars);
222 random_chars = g_strconcat (plain_chars, funky_chars, NULL);
224 random_chars = g_strdup (plain_chars);
226 rcend = strlen (random_chars) - 1;
230 get_random_character (
void)
233 random_character_include_funky_chars (TRUE);
235 return random_chars[get_random_int_in_range (0, rcend)];
239 get_random_string_without (
const char *exclude_chars)
245 switch (get_random_int_in_range (0, 9))
255 len = get_random_int_in_range (100, 500);
258 len = get_random_int_in_range (5, 20);
261 ret = g_new0 (gchar, len);
263 for (i = 0; i < len - 1; i++)
269 c = get_random_character ();
271 while (exclude_chars && strchr (exclude_chars, c));
276 return g_strstrip (ret);
280 get_random_string (
void)
282 return get_random_string_without (NULL);
286 get_random_gint64 (
void)
298 get_random_double (
void)
303 i = (guint) get_random_int_in_range (8, 13);
305 d = ((double) get_random_int_in_range (8, 999999) * i * 0.9 / 7);
310 get_random_string_in_array (
const char *str_list[])
315 for (num = 0; str_list[num] != NULL; num++)
318 num = get_random_int_in_range (0, num - 1);
319 return str_list[num];