QOF  0.8.7
test-stuff.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
15  */
16 /*
17  * Created 20010320 by bstanley to hold only those
18  * testing functions which are independent of the rest of
19  * the GNUCash system.
20  *
21  * This allows me to compile simple test programs standalone...
22  *
23  */
24 
25 
26 #include "config.h"
27 
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <glib.h>
36 #include "test-stuff.h"
37 
38 void vsuccess_args (const char *test_title,
39  const char *file,
40  int line, const char *format, va_list ap);
41 
42 void vfailure_args (const char *test_title,
43  const char *file,
44  int line, const char *format, va_list ap);
45 
46 static guint successes;
47 static guint failures;
48 static gboolean success_should_print = FALSE;
49 
50 void
51 success_call (const char *test_title, const char *file, int line)
52 {
53  success_args (test_title, file, line, "");
54 }
55 
56 void
57 success_args (const char *test_title,
58  const char *file, int line, const char *format, ...)
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 (const char *test_title,
68  const char *file, int line, const char *format, va_list ap)
69 {
70  if (success_should_print)
71  {
72  printf ("SUCCESS: %s, %s:%d ", test_title, file, line);
73  vprintf (format, ap);
74  printf ("\n");
75  fflush (stdout);
76  }
77  ++successes;
78 }
79 
80 void
81 failure_call (const char *test_title, const char *file, int line)
82 {
83  failure_args (test_title, file, line, "");
84 }
85 
86 
87 void
88 failure_args (const char *test_title,
89  const char *file, int line, const char *format, ...)
90 {
91  va_list ap;
92  va_start (ap, format);
93  vfailure_args (test_title, file, line, format, ap);
94  va_end (ap);
95 }
96 
97 void
98 vfailure_args (const char *test_title,
99  const char *file, int line, const char *format, va_list ap)
100 {
101  printf ("FAILURE %s %s:%d ", test_title, file, line);
102  vprintf (format, ap);
103  printf ("\n");
104  fflush (stdout);
105 
106  ++failures;
107 }
108 
109 int
110 get_rv (void)
111 {
112  if (failures)
113  {
114  return 1;
115  }
116  return 0;
117 }
118 
119 gboolean
120 do_test_call (gboolean result,
121  const char *test_title, const char *filename, int line)
122 {
123  if (result)
124  {
125  success_args (test_title, filename, line, "");
126  }
127  else
128  {
129  failure_args (test_title, filename, line, "");
130  }
131 
132  return result;
133 }
134 
135 gboolean
136 do_test_args (gboolean result,
137  const char *test_title,
138  const char *filename, int line, const char *format, ...)
139 {
140  va_list ap;
141  va_start (ap, format);
142 
143  if (result)
144  {
145  vsuccess_args (test_title, filename, line, format, ap);
146  }
147  else
148  {
149  vfailure_args (test_title, filename, line, format, ap);
150  }
151  va_end (ap);
152 
153  return result;
154 }
155 
156 void
157 print_test_results (void)
158 {
159  guint total = successes + failures;
160  if (total == 1)
161  {
162  printf ("Executed 1 test.");
163  }
164  else
165  {
166  printf ("Executed %d tests.", successes + failures);
167  }
168  if (failures)
169  {
170  if (failures == 1)
171  {
172  printf (" There was 1 failure.");
173  }
174  else
175  {
176  printf (" There were %d failures.", failures);
177  }
178  }
179  else
180  {
181  printf (" All tests passed.");
182  }
183  printf ("\n");
184  fflush (stdout);
185 }
186 
187 void
188 set_success_print (gboolean in_should_print)
189 {
190  success_should_print = in_should_print;
191 }
192 
193 gboolean
194 get_random_boolean (void)
195 {
196  return get_random_int_in_range (0, 1);
197 }
198 
199 gint
200 get_random_int_in_range (int start, int end)
201 {
202  return CLAMP (start + (int) ((double) (end - start + 1) * rand () /
203  (RAND_MAX + 1.0)), start, end);
204 }
205 
206 static char *random_chars = NULL;
207 
208 static char plain_chars[] =
209  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
210  "abcdefghijklmnopqrstuvwxyz" "1234567890" " ";
211 
212 static char funky_chars[] = ",.'\"`~!@#$%^*(){}[]/=?+-_\\|" "<>&" "\n\t";
213 
214 static int rcend = 0;
215 
216 void
217 random_character_include_funky_chars (gboolean use_funky_chars)
218 {
219  g_free (random_chars);
220 
221  if (use_funky_chars)
222  random_chars = g_strconcat (plain_chars, funky_chars, NULL);
223  else
224  random_chars = g_strdup (plain_chars);
225 
226  rcend = strlen (random_chars) - 1;
227 }
228 
229 gchar
230 get_random_character (void)
231 {
232  if (!rcend)
233  random_character_include_funky_chars (TRUE);
234 
235  return random_chars[get_random_int_in_range (0, rcend)];
236 }
237 
238 gchar *
239 get_random_string_without (const char *exclude_chars)
240 {
241  gchar *ret;
242  int len;
243  int i;
244 
245  switch (get_random_int_in_range (0, 9))
246  {
247 /* case 0: */
248 /* return ""; */
249 /* case 1: */
250 /* return NULL; */
251 /* case 2: */
252 /* len = get_random_int_in_range(1000, 5000); */
253 /* break; */
254  case 3:
255  len = get_random_int_in_range (100, 500);
256  break;
257  default:
258  len = get_random_int_in_range (5, 20);
259  break;
260  }
261  ret = g_new0 (gchar, len);
262 
263  for (i = 0; i < len - 1; i++)
264  {
265  gchar c;
266 
267  do
268  {
269  c = get_random_character ();
270  }
271  while (exclude_chars && strchr (exclude_chars, c));
272 
273  ret[i] = c;
274  }
275 
276  return g_strstrip (ret);
277 }
278 
279 gchar *
280 get_random_string (void)
281 {
282  return get_random_string_without (NULL);
283 }
284 
285 gint64
286 get_random_gint64 (void)
287 {
288  gint64 ret = 0;
289 
290  ret = rand ();
291  ret <<= 32;
292  ret += rand ();
293 
294  return ret;
295 }
296 
297 double
298 get_random_double (void)
299 {
300  double d;
301  guint i;
302 
303  i = (guint) get_random_int_in_range (8, 13);
304  /* using 0.9 and 7 increases chances of getting lots of decimals */
305  d = ((double) get_random_int_in_range (8, 999999) * i * 0.9 / 7);
306  return d;
307 }
308 
309 const char *
310 get_random_string_in_array (const char *str_list[])
311 {
312  int num;
313 
314  /* count number of items in list */
315  for (num = 0; str_list[num] != NULL; num++)
316  ;
317 
318  num = get_random_int_in_range (0, num - 1);
319  return str_list[num];
320 }