QOF: test-stuff.c Source File

QOF 0.7.5

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 "test-stuff.h"
22
23 void vsuccess_args (const char *test_title,
24 const char *file,
25 int line, const char *format, va_list ap);
26
27 void vfailure_args (const char *test_title,
28 const char *file,
29 int line, const char *format, va_list ap);
30
31 static guint successes;
32 static guint failures;
33 static gboolean success_should_print = FALSE;
34
35 void
36 success_call (const char *test_title, const char *file, int line)
37 {
38 success_args (test_title, file, line, "");
39 }
40
41 void
42 success_args (const char *test_title,
43 const char *file, int line, const char *format, ...)
44 {
45 va_list ap;
46 va_start (ap, format);
47 vsuccess_args (test_title, file, line, format, ap);
48 va_end (ap);
49 }
50
51 void
52 vsuccess_args (const char *test_title,
53 const char *file, int line, const char *format, va_list ap)
54 {
55 if (success_should_print)
56 {
57 printf ("SUCCESS: %s, %s:%d ", test_title, file, line);
58 vprintf (format, ap);
59 printf ("\n");
60 fflush (stdout);
61 }
62 ++successes;
63 }
64
65 void
66 failure_call (const char *test_title, const char *file, int line)
67 {
68 failure_args (test_title, file, line, "");
69 }
70
71
72 void
73 failure_args (const char *test_title,
74 const char *file, int line, const char *format, ...)
75 {
76 va_list ap;
77 va_start (ap, format);
78 vfailure_args (test_title, file, line, format, ap);
79 va_end (ap);
80 }
81
82 void
83 vfailure_args (const char *test_title,
84 const char *file, int line, const char *format, va_list ap)
85 {
86 printf ("FAILURE %s %s:%d ", test_title, file, line);
87 vprintf (format, ap);
88 printf ("\n");
89 fflush (stdout);
90
91 ++failures;
92 }
93
94 int
95 get_rv (void)
96 {
97 if (failures)
98 {
99 return 1;
100 }
101 return 0;
102 }
103
104 gboolean
105 do_test_call (gboolean result,
106 const char *test_title, const char *filename, int line)
107 {
108 if (result)
109 {
110 success_args (test_title, filename, line, "");
111 }
112 else
113 {
114 failure_args (test_title, filename, line, "");
115 }
116
117 return result;
118 }
119
120 gboolean
121 do_test_args (gboolean result,
122 const char *test_title,
123 const char *filename, int line, const char *format, ...)
124 {
125 va_list ap;
126 va_start (ap, format);
127
128 if (result)
129 {
130 vsuccess_args (test_title, filename, line, format, ap);
131 }
132 else
133 {
134 vfailure_args (test_title, filename, line, format, ap);
135 }
136 va_end (ap);
137
138 return result;
139 }
140
141 void
142 print_test_results (void)
143 {
144 guint total = successes + failures;
145 if (total == 1)
146 {
147 printf ("Executed 1 test.");
148 }
149 else
150 {
151 printf ("Executed %d tests.", successes + failures);
152 }
153 if (failures)
154 {
155 if (failures == 1)
156 {
157 printf (" There was 1 failure.");
158 }
159 else
160 {
161 printf (" There were %d failures.", failures);
162 }
163 }
164 else
165 {
166 printf (" All tests passed.");
167 }
168 printf ("\n");
169 fflush (stdout);
170 }
171
172 void
173 set_success_print (gboolean in_should_print)
174 {
175 success_should_print = in_should_print;
176 }
177
178 gboolean
179 get_random_boolean (void)
180 {
181 return get_random_int_in_range (0, 1);
182 }
183
184 gint
185 get_random_int_in_range (int start, int end)
186 {
187 return CLAMP (start + (int) ((double) (end - start + 1) * rand () /
188 (RAND_MAX + 1.0)), start, end);
189 }
190
191 static char *random_chars = NULL;
192
193 static char plain_chars[] =
194 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
195 "abcdefghijklmnopqrstuvwxyz" "1234567890" " ";
196
197 static char funky_chars[] = ",.'\"`~!@#$%^*(){}[]/=?+-_\\|" "<>&" "\n\t";
198
199 static int rcend = 0;
200
201 void
202 random_character_include_funky_chars (gboolean use_funky_chars)
203 {
204 g_free (random_chars);
205
206 if (use_funky_chars)
207 random_chars = g_strconcat (plain_chars, funky_chars, NULL);
208 else
209 random_chars = g_strdup (plain_chars);
210
211 rcend = strlen (random_chars) - 1;
212 }
213
214 gchar
215 get_random_character (void)
216 {
217 if (!rcend)
218 random_character_include_funky_chars (TRUE);
219
220 return random_chars[get_random_int_in_range (0, rcend)];
221 }
222
223 gchar *
224 get_random_string_without (const char *exclude_chars)
225 {
226 gchar *ret;
227 int len;
228 int i;
229
230 switch (get_random_int_in_range (0, 9))
231 {
232 /* case 0: */
233 /* return ""; */
234 /* case 1: */
235 /* return NULL; */
236 /* case 2: */
237 /* len = get_random_int_in_range(1000, 5000); */
238 /* break; */
239 case 3:
240 len = get_random_int_in_range (100, 500);
241 break;
242 default:
243 len = get_random_int_in_range (5, 20);
244 break;
245 }
246 ret = g_new0 (gchar, len);
247
248 for (i = 0; i < len - 1; i++)
249 {
250 char c;
251
252 do
253 {
254 c = get_random_character ();
255 }
256 while (exclude_chars && strchr (exclude_chars, c));
257
258 ret[i] = c;
259 }
260
261 return g_strstrip (ret);
262 }
263
264 gchar *
265 get_random_string (void)
266 {
267 return get_random_string_without (NULL);
268 }
269
270 gint64
271 get_random_gint64 (void)
272 {
273 gint64 ret = 0;
274
275 ret = rand ();
276 ret <<= 32;
277 ret += rand ();
278
279 return ret;
280 }
281
282 double
283 get_random_double (void)
284 {
285 double d;
286 guint i;
287
288 i = (guint) get_random_int_in_range (8, 13);
289 /* using 0.9 and 7 increases chances of getting lots of decimals */
290 d = ((double) get_random_int_in_range (8, 999999) * i * 0.9 / 7);
291 return d;
292 }
293
294 const char *
295 get_random_string_in_array (const char *str_list[])
296 {
297 int num;
298
299 /* count number of items in list */
300 for (num = 0; str_list[num] != NULL; num++)
301 ;
302
303 num = get_random_int_in_range (0, num - 1);
304 return str_list[num];
305 }