GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-stuff.c
1 /*
2  * Created 20010320 by bstanley to hold only those
3  * testing functions which are independent of the rest of
4  * the GNUCash system.
5  *
6  * This allows me to compile simple test programs standalone...
7  *
8  */
9 
10 
11 #include "config.h"
12 
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <glib/gprintf.h>
22 #include "test-stuff.h"
23 
24 
25 void vsuccess_args(
26  const char *test_title,
27  const char *file,
28  int line,
29  const char *format,
30  va_list ap);
31 
32 void vfailure_args(
33  const char *test_title,
34  const char *file,
35  int line,
36  const char *format,
37  va_list ap);
38 
39 static guint successes;
40 static guint failures;
41 static gboolean success_should_print = FALSE;
42 
43 void
44 success_call(
45  const char *test_title,
46  const char* file,
47  int line )
48 {
49  success_args( test_title, file, line, "" );
50 }
51 
52 void
53 success_args(
54  const char *test_title,
55  const char *file,
56  int line,
57  const char *format,
58  ... )
59 {
60  va_list ap;
61  va_start(ap, format);
62  vsuccess_args( test_title, file, line, format, ap );
63  va_end(ap);
64 }
65 
66 void
67 vsuccess_args(
68  const char *test_title,
69  const char *file,
70  int line,
71  const char *format,
72  va_list ap)
73 {
74  if ( success_should_print )
75  {
76  printf("SUCCESS: %s, %s:%d ", test_title, file, line );
77  vprintf(format, ap);
78  printf("\n");
79  fflush(stdout);
80  }
81  ++successes;
82 }
83 
84 void
85 failure_call(
86  const char *test_title,
87  const char *file,
88  int line)
89 {
90  failure_args( test_title, file, line, "" );
91 }
92 
93 
94 void
95 failure_args(
96  const char *test_title,
97  const char *file,
98  int line,
99  const char *format,
100  ... )
101 {
102  va_list ap;
103  va_start(ap, format);
104  vfailure_args( test_title, file, line, format, ap );
105  va_end(ap);
106 }
107 void
108 vfailure_args(
109  const char *test_title,
110  const char* file,
111  int line,
112  const char *format,
113  va_list ap)
114 {
115  printf("FAILURE %s %s:%d ", test_title, file, line );
116  vprintf(format, ap);
117  printf("\n");
118  fflush(stdout);
119 
120  ++failures;
121 }
122 
123 int
124 get_rv(void)
125 {
126  return failures;
127 }
128 
129 gboolean
130 do_test_call(gboolean result, const char* test_title, const char* filename,
131  int line )
132 {
133  if (result)
134  success_args( test_title, filename, line, "" );
135  else
136  failure_args( test_title, filename, line, "" );
137 
138  return result;
139 }
140 
141 gboolean
142 do_test_args(
143  gboolean result,
144  const char* test_title,
145  const char* filename,
146  int line,
147  const char* format,
148  ... )
149 {
150  va_list ap;
151  va_start(ap, format);
152 
153  if ( result )
154  {
155  vsuccess_args( test_title, filename, line, format, ap );
156  }
157  else
158  {
159  vfailure_args( test_title, filename, line, format, ap );
160  }
161  va_end(ap);
162 
163  return result;
164 }
165 
166 void
167 print_test_results(void)
168 {
169  guint total = successes + failures;
170  if ( total == 1 )
171  {
172  printf( "Executed 1 test." );
173  }
174  else
175  {
176  printf("Executed %d tests.", successes + failures );
177  }
178  if ( failures )
179  {
180  if ( failures == 1 )
181  {
182  printf(" There was 1 failure." );
183  }
184  else
185  {
186  printf(" There were %d failures.", failures );
187  }
188  }
189  else
190  {
191  printf(" All tests passed.");
192  }
193  printf("\n");
194  fflush(stdout);
195 }
196 
197 void
198 set_success_print( gboolean in_should_print )
199 {
200  success_should_print = in_should_print;
201 }
202 
203 gboolean
204 get_random_boolean(void)
205 {
206  return get_random_int_in_range (0, 1);
207 }
208 
209 gint
210 get_random_int_in_range(int start, int end)
211 {
212  return CLAMP (start + (int)((double)(end - start + 1) * rand() /
213  (RAND_MAX + 1.0)),
214  start,
215  end);
216 }
217 
218 static char *random_chars = NULL;
219 
220 static char plain_chars[] =
221  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
222  "abcdefghijklmnopqrstuvwxyz"
223  "1234567890"
224  " ";
225 
226 static char funky_chars[] =
227  ",.'\"`~!@#$%^*(){}[]/=?+-_\\|"
228  "<>&"
229  "\n\t";
230 
231 static int rcend = 0;
232 
233 void
234 random_character_include_funky_chars (gboolean use_funky_chars)
235 {
236  g_free (random_chars);
237 
238  if (use_funky_chars)
239  random_chars = g_strconcat (plain_chars, funky_chars, NULL);
240  else
241  random_chars = g_strdup (plain_chars);
242 
243  rcend = strlen (random_chars) - 1;
244 }
245 
246 gchar
247 get_random_character(void)
248 {
249  if (!rcend)
250  random_character_include_funky_chars (TRUE);
251 
252  return random_chars[get_random_int_in_range(0, rcend)];
253 }
254 
255 gchar *
256 get_random_string_length_in_range(int minlen, int maxlen)
257 {
258  gchar *ret;
259  int i, len = get_random_int_in_range(minlen, maxlen);
260 
261  ret = g_new0(gchar, len);
262 
263  for (i = 0; i < len - 1; i++)
264  ret[i] = get_random_character ();
265 
266  return g_strstrip(ret);
267 }
268 
269 gchar *
270 get_random_string_without(const char *exclude_chars)
271 {
272  gchar *ret;
273  int len;
274  int i;
275 
276  switch (get_random_int_in_range(0, 9))
277  {
278  /* case 0: */
279  /* return ""; */
280  /* case 1: */
281  /* return NULL; */
282  /* case 2: */
283  /* len = get_random_int_in_range(1000, 5000); */
284  /* break; */
285  case 3:
286  len = get_random_int_in_range(100, 500);
287  break;
288  default:
289  len = get_random_int_in_range(5, 20);
290  break;
291  }
292  ret = g_new0(gchar, len);
293 
294  for (i = 0; i < len - 1; i++)
295  {
296  char c;
297 
298  do
299  {
300  c = get_random_character ();
301  }
302  while (exclude_chars && strchr (exclude_chars, c));
303 
304  ret[i] = c;
305  }
306 
307  return g_strstrip (ret);
308 }
309 
310 gchar *
311 get_random_string(void)
312 {
313  return get_random_string_without (NULL);
314 }
315 
316 gint32
317 get_random_gint32 (void)
318 {
319  gint32 ret = 0;
320 
321  if (RAND_MAX > (1 << 15))
322  return rand ();
323 
324  if (RAND_MAX > (1 << 7))
325  {
326  ret = rand ();
327  ret <<= 16;
328  ret += rand ();
329  return ret;
330  }
331 
332  ret = rand ();
333  ret <<= 8;
334  ret += rand ();
335  ret <<= 8;
336  ret += rand ();
337  ret <<= 8;
338  ret += rand ();
339  return ret;
340 }
341 
342 gint64
343 get_random_gint64(void)
344 {
345  gint64 ret = 0;
346 
347  ret = get_random_gint32 ();
348  ret <<= 32;
349  ret += get_random_gint32 ();
350 
351  return ret;
352 }
353 
354 double
355 get_random_double(void)
356 {
357  double d;
358  guint i;
359 
360  i = (guint)get_random_int_in_range(8, 13);
361  /* using 0.9 and 7 increases chances of getting lots of decimals */
362  d = ((double)get_random_int_in_range(8, 999999) * i * 0.9 / 7);
363  return d;
364 }
365 
366 const char*
367 get_random_string_in_array(const char* str_list[])
368 {
369  int num;
370 
371  /* count number of items in list */
372  for (num = 0; str_list[num] != NULL; num++)
373  ;
374 
375  num = get_random_int_in_range(0, num - 1);
376  return str_list[num];
377 }