25 #include <sys/types.h>
37 #include "test-stuff.h"
39 static gboolean glist_strings_only = FALSE;
41 static GHashTable *exclude_kvp_types = NULL;
42 static gint kvp_max_depth = 5;
43 static gint kvp_frame_max_elements = 10;
45 gboolean gnc_engine_debug_random = FALSE;
51 set_max_kvp_depth (gint max_kvp_depth)
53 kvp_max_depth = MAX (max_kvp_depth, 1);
57 set_max_kvp_frame_elements (gint max_kvp_frame_elements)
59 kvp_frame_max_elements = MAX (max_kvp_frame_elements, 1);
67 if (!exclude_kvp_types)
68 exclude_kvp_types = g_hash_table_new (g_int_hash, g_int_equal);
70 key = g_new (gint, 1);
73 g_hash_table_insert (exclude_kvp_types, key, exclude_kvp_types);
81 if (!exclude_kvp_types)
84 if (g_hash_table_lookup (exclude_kvp_types, &key))
91 random_glist_strings_only (gboolean strings_only)
93 glist_strings_only = strings_only;
97 #ifndef QOF_DISABLE_DEPRECATED
98 static gboolean zero_nsec = FALSE;
101 random_timespec_zero_nsec (gboolean zero_nsec_in)
103 zero_nsec = zero_nsec_in;
106 static gboolean usec_resolution = FALSE;
109 random_timespec_usec_resolution (gboolean usec_resolution_in)
111 usec_resolution = usec_resolution_in;
116 static gint borked = 80;
118 static inline gboolean
121 if (1 == get_random_int_in_range (0, borked))
141 static KvpValue *get_random_kvp_value_depth (
int type, gint depth);
144 get_random_glist_depth (gint depth)
147 int count = get_random_int_in_range (1, 5);
150 if (depth >= kvp_max_depth)
153 for (i = 0; i < count; i++)
162 value = get_random_kvp_value_depth (kvpt, depth + 1);
166 ret = g_list_prepend (ret, value);
173 get_random_glist (
void)
175 return get_random_glist_depth (0);
180 #ifndef QOF_DISABLE_DEPRECATED
182 get_random_timespec (
void)
188 while (ret->tv_sec <= 0)
189 ret->tv_sec = rand ();
195 ret->tv_nsec = rand ();
199 ret->tv_nsec = MIN (ret->tv_nsec, 999999999);
200 ret->tv_nsec /= 1000;
201 ret->tv_nsec *= 1000;
210 get_random_guid (
void)
214 ret = g_new (
GUID, 1);
221 get_random_binary_data (
void)
226 len = get_random_int_in_range (20, 100);
228 ret->data = g_new (guchar, len);
231 for (len--; len >= 0; len--)
233 ret->data[len] = (guchar) get_random_int_in_range (0, 255);
242 static KvpFrame *get_random_kvp_frame_depth (gint depth);
245 get_random_kvp_value_depth (
int type, gint depth)
267 if (kvp_type_excluded (datype))
273 ret = kvp_value_new_gint64 (get_random_gint64 ());
281 ret = kvp_value_new_numeric (get_random_qof_numeric ());
287 tmp_str = get_random_string ();
291 ret = kvp_value_new_string (tmp_str);
299 tmp_guid = get_random_guid ();
300 ret = kvp_value_new_guid (tmp_guid);
304 #ifndef QOF_DISABLE_DEPRECATED
307 Timespec *ts = get_random_timespec ();
308 ret = kvp_value_new_timespec (*ts);
316 tmp_data = get_random_binary_data ();
317 ret = kvp_value_new_binary (tmp_data->data, tmp_data->len);
318 g_free (tmp_data->data);
330 tmp_frame = get_random_kvp_frame_depth (depth + 1);
331 ret = kvp_value_new_frame (tmp_frame);
344 get_random_kvp_frame_depth (gint depth)
350 if (depth >= kvp_max_depth)
355 vals_to_add = get_random_int_in_range (1, kvp_frame_max_elements);
358 for (; vals_to_add > 0; vals_to_add--)
366 key = get_random_string_without (
"/");
374 val = get_random_kvp_value_depth (-1, depth + 1);
394 get_random_kvp_frame (
void)
396 return get_random_kvp_frame_depth (0);
400 get_random_kvp_value (
int type)
402 return get_random_kvp_value_depth (type, 0);
408 #define RAND_IN_RANGE(X) (((X)*((gint64) (rand()+1)))/RAND_MAX)
411 get_random_qof_numeric (
void)
416 if (RAND_MAX / 8 > rand ())
419 deno = RAND_IN_RANGE (6000ULL);
423 gint64 norm = RAND_IN_RANGE (10ULL);
437 numer = get_random_gint64 () / 100000;
526 get_random_query_type (
void)
528 switch (get_random_int_in_range (0, 4))