34 #include <sys/types.h> 41 #include <glib/gprintf.h> 42 #include "test-stuff.h" 46 const char *test_title,
53 const char *test_title,
59 static guint successes;
60 static guint failures;
61 static gboolean success_should_print = FALSE;
65 const char *test_title,
69 success_args( test_title, file, line,
"" );
74 const char *test_title,
82 vsuccess_args( test_title, file, line, format, ap );
88 const char *test_title,
94 if ( success_should_print )
96 printf(
"SUCCESS: %s, %s:%d ", test_title, file, line );
106 const char *test_title,
110 failure_args( test_title, file, line,
"" );
116 const char *test_title,
123 va_start(ap, format);
124 vfailure_args( test_title, file, line, format, ap );
129 const char *test_title,
135 printf(
"FAILURE %s %s:%d ", test_title, file, line );
150 do_test_call(gboolean result,
const char* test_title,
const char* filename,
154 success_args( test_title, filename, line,
"" );
156 failure_args( test_title, filename, line,
"" );
164 const char* test_title,
165 const char* filename,
171 va_start(ap, format);
175 vsuccess_args( test_title, filename, line, format, ap );
179 vfailure_args( test_title, filename, line, format, ap );
187 print_test_results(
void)
189 guint total = successes + failures;
192 printf(
"Executed 1 test." );
196 printf(
"Executed %d tests.", successes + failures );
202 printf(
" There was 1 failure." );
206 printf(
" There were %d failures.", failures );
211 printf(
" All tests passed.");
218 set_success_print( gboolean in_should_print )
220 success_should_print = in_should_print;
224 get_random_boolean(
void)
226 return get_random_int_in_range (0, 1);
230 get_random_int_in_range(
int start,
int end)
232 return CLAMP (start + (
int)((
double)(end - start + 1) * rand() /
238 static char *random_chars = NULL;
240 static char plain_chars[] =
241 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 242 "abcdefghijklmnopqrstuvwxyz" 246 static char funky_chars[] =
247 ",.'\"`~!@#$%^*(){}[]/=?+-_\\|" 251 static int rcend = 0;
254 random_character_include_funky_chars (gboolean use_funky_chars)
256 g_free (random_chars);
259 random_chars = g_strconcat (plain_chars, funky_chars, NULL);
261 random_chars = g_strdup (plain_chars);
263 rcend = strlen (random_chars) - 1;
267 get_random_character(
void)
270 random_character_include_funky_chars (TRUE);
272 return random_chars[get_random_int_in_range(0, rcend)];
276 get_random_string_length_in_range(
int minlen,
int maxlen)
279 int i, len = get_random_int_in_range(minlen, maxlen);
281 ret = g_new0(gchar, len);
283 for (i = 0; i < len - 1; i++)
284 ret[i] = get_random_character ();
286 return g_strstrip(ret);
290 get_random_string_without(
const char *exclude_chars)
296 switch (get_random_int_in_range(0, 9))
306 len = get_random_int_in_range(100, 500);
309 len = get_random_int_in_range(5, 20);
312 ret = g_new0(gchar, len);
314 for (i = 0; i < len - 1; i++)
320 c = get_random_character ();
322 while (exclude_chars && strchr (exclude_chars, c));
327 return g_strstrip (ret);
331 get_random_string(
void)
333 return get_random_string_without (NULL);
337 get_random_gint32 (
void)
341 if (RAND_MAX > (1 << 15))
344 if (RAND_MAX > (1 << 7))
363 get_random_gint64(
void)
367 ret = get_random_gint32 ();
369 ret += get_random_gint32 ();
375 get_random_double(
void)
380 i = (guint)get_random_int_in_range(8, 13);
382 d = ((double)get_random_int_in_range(8, 999999) * i * 0.9 / 7);
387 get_random_string_in_array(
const char* str_list[])
392 for (num = 0; str_list[num] != NULL; num++)
395 num = get_random_int_in_range(0, num - 1);
396 return str_list[num];