14 #include <sys/types.h>
21 #include "test-stuff.h"
23 void vsuccess_args (
const char *test_title,
25 int line,
const char *format, va_list ap);
27 void vfailure_args (
const char *test_title,
29 int line,
const char *format, va_list ap);
31 static guint successes;
32 static guint failures;
33 static gboolean success_should_print = FALSE;
36 success_call (
const char *test_title,
const char *file,
int line)
38 success_args (test_title, file, line,
"");
42 success_args (
const char *test_title,
43 const char *file,
int line,
const char *format, ...)
46 va_start (ap, format);
47 vsuccess_args (test_title, file, line, format, ap);
52 vsuccess_args (
const char *test_title,
53 const char *file,
int line,
const char *format, va_list ap)
55 if (success_should_print)
57 printf (
"SUCCESS: %s, %s:%d ", test_title, file, line);
66 failure_call (
const char *test_title,
const char *file,
int line)
68 failure_args (test_title, file, line,
"");
73 failure_args (
const char *test_title,
74 const char *file,
int line,
const char *format, ...)
77 va_start (ap, format);
78 vfailure_args (test_title, file, line, format, ap);
83 vfailure_args (
const char *test_title,
84 const char *file,
int line,
const char *format, va_list ap)
86 printf (
"FAILURE %s %s:%d ", test_title, file, line);
105 do_test_call (gboolean result,
106 const char *test_title,
const char *filename,
int line)
110 success_args (test_title, filename, line,
"");
114 failure_args (test_title, filename, line,
"");
121 do_test_args (gboolean result,
122 const char *test_title,
123 const char *filename,
int line,
const char *format, ...)
126 va_start (ap, format);
130 vsuccess_args (test_title, filename, line, format, ap);
134 vfailure_args (test_title, filename, line, format, ap);
142 print_test_results (
void)
144 guint total = successes + failures;
147 printf (
"Executed 1 test.");
151 printf (
"Executed %d tests.", successes + failures);
157 printf (
" There was 1 failure.");
161 printf (
" There were %d failures.", failures);
166 printf (
" All tests passed.");
173 set_success_print (gboolean in_should_print)
175 success_should_print = in_should_print;
179 get_random_boolean (
void)
181 return get_random_int_in_range (0, 1);
185 get_random_int_in_range (
int start,
int end)
187 return CLAMP (start + (
int) ((
double) (end - start + 1) * rand () /
188 (RAND_MAX + 1.0)), start, end);
191 static char *random_chars = NULL;
193 static char plain_chars[] =
194 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
195 "abcdefghijklmnopqrstuvwxyz" "1234567890" " ";
197 static char funky_chars[] =
",.'\"`~!@#$%^*(){}[]/=?+-_\\|" "<>&" "\n\t";
199 static int rcend = 0;
202 random_character_include_funky_chars (gboolean use_funky_chars)
204 g_free (random_chars);
207 random_chars = g_strconcat (plain_chars, funky_chars, NULL);
209 random_chars = g_strdup (plain_chars);
211 rcend = strlen (random_chars) - 1;
215 get_random_character (
void)
218 random_character_include_funky_chars (TRUE);
220 return random_chars[get_random_int_in_range (0, rcend)];
224 get_random_string_without (
const char *exclude_chars)
230 switch (get_random_int_in_range (0, 9))
240 len = get_random_int_in_range (100, 500);
243 len = get_random_int_in_range (5, 20);
246 ret = g_new0 (gchar, len);
248 for (i = 0; i < len - 1; i++)
254 c = get_random_character ();
256 while (exclude_chars && strchr (exclude_chars, c));
261 return g_strstrip (ret);
265 get_random_string (
void)
267 return get_random_string_without (NULL);
271 get_random_gint64 (
void)
283 get_random_double (
void)
288 i = (guint) get_random_int_in_range (8, 13);
290 d = ((double) get_random_int_in_range (8, 999999) * i * 0.9 / 7);
295 get_random_string_in_array (
const char *str_list[])
300 for (num = 0; str_list[num] != NULL; num++)
303 num = get_random_int_in_range (0, num - 1);
304 return str_list[num];