14 #include <sys/types.h>
21 #include <glib/gprintf.h>
22 #include "test-stuff.h"
26 const char *test_title,
33 const char *test_title,
39 static guint successes;
40 static guint failures;
41 static gboolean success_should_print = FALSE;
45 const char *test_title,
49 success_args( test_title, file, line,
"" );
54 const char *test_title,
62 vsuccess_args( test_title, file, line, format, ap );
68 const char *test_title,
74 if ( success_should_print )
76 printf(
"SUCCESS: %s, %s:%d ", test_title, file, line );
86 const char *test_title,
90 failure_args( test_title, file, line,
"" );
96 const char *test_title,
103 va_start(ap, format);
104 vfailure_args( test_title, file, line, format, ap );
109 const char *test_title,
115 printf(
"FAILURE %s %s:%d ", test_title, file, line );
130 do_test_call(gboolean result,
const char* test_title,
const char* filename,
134 success_args( test_title, filename, line,
"" );
136 failure_args( test_title, filename, line,
"" );
144 const char* test_title,
145 const char* filename,
151 va_start(ap, format);
155 vsuccess_args( test_title, filename, line, format, ap );
159 vfailure_args( test_title, filename, line, format, ap );
167 print_test_results(
void)
169 guint total = successes + failures;
172 printf(
"Executed 1 test." );
176 printf(
"Executed %d tests.", successes + failures );
182 printf(
" There was 1 failure." );
186 printf(
" There were %d failures.", failures );
191 printf(
" All tests passed.");
198 set_success_print( gboolean in_should_print )
200 success_should_print = in_should_print;
204 get_random_boolean(
void)
206 return get_random_int_in_range (0, 1);
210 get_random_int_in_range(
int start,
int end)
212 return CLAMP (start + (
int)((
double)(end - start + 1) * rand() /
218 static char *random_chars = NULL;
220 static char plain_chars[] =
221 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
222 "abcdefghijklmnopqrstuvwxyz"
226 static char funky_chars[] =
227 ",.'\"`~!@#$%^*(){}[]/=?+-_\\|"
231 static int rcend = 0;
234 random_character_include_funky_chars (gboolean use_funky_chars)
236 g_free (random_chars);
239 random_chars = g_strconcat (plain_chars, funky_chars, NULL);
241 random_chars = g_strdup (plain_chars);
243 rcend = strlen (random_chars) - 1;
247 get_random_character(
void)
250 random_character_include_funky_chars (TRUE);
252 return random_chars[get_random_int_in_range(0, rcend)];
256 get_random_string_length_in_range(
int minlen,
int maxlen)
259 int i, len = get_random_int_in_range(minlen, maxlen);
261 ret = g_new0(gchar, len);
263 for (i = 0; i < len - 1; i++)
264 ret[i] = get_random_character ();
266 return g_strstrip(ret);
270 get_random_string_without(
const char *exclude_chars)
276 switch (get_random_int_in_range(0, 9))
286 len = get_random_int_in_range(100, 500);
289 len = get_random_int_in_range(5, 20);
292 ret = g_new0(gchar, len);
294 for (i = 0; i < len - 1; i++)
300 c = get_random_character ();
302 while (exclude_chars && strchr (exclude_chars, c));
307 return g_strstrip (ret);
311 get_random_string(
void)
313 return get_random_string_without (NULL);
317 get_random_gint32 (
void)
321 if (RAND_MAX > (1 << 15))
324 if (RAND_MAX > (1 << 7))
343 get_random_gint64(
void)
347 ret = get_random_gint32 ();
349 ret += get_random_gint32 ();
355 get_random_double(
void)
360 i = (guint)get_random_int_in_range(8, 13);
362 d = ((double)get_random_int_in_range(8, 999999) * i * 0.9 / 7);
367 get_random_string_in_array(
const char* str_list[])
372 for (num = 0; str_list[num] != NULL; num++)
375 num = get_random_int_in_range(0, num - 1);
376 return str_list[num];