2001-08-17 17:49:17 -05:00
|
|
|
/*
|
|
|
|
* Created 20010320 by bstanley to hold only those
|
|
|
|
* testing functions which are independent of the rest of
|
|
|
|
* the GNUCash system.
|
|
|
|
*
|
|
|
|
* This allows me to compile simple test programs standalone...
|
|
|
|
*
|
|
|
|
*/
|
2015-09-29 14:08:48 -05:00
|
|
|
/********************************************************************\
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 2 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License*
|
|
|
|
* along with this program; if not, contact: *
|
|
|
|
* *
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942 *
|
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
|
2017-10-26 04:14:21 -05:00
|
|
|
#include <config.h>
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
2011-07-18 07:00:44 -05:00
|
|
|
#include <glib/gprintf.h>
|
2001-08-17 17:49:17 -05:00
|
|
|
#include "test-stuff.h"
|
|
|
|
|
2011-07-14 12:06:13 -05:00
|
|
|
|
2001-08-17 17:49:17 -05:00
|
|
|
void vsuccess_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line,
|
|
|
|
const char *format,
|
|
|
|
va_list ap);
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
void vfailure_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line,
|
|
|
|
const char *format,
|
|
|
|
va_list ap);
|
2001-08-17 17:49:17 -05:00
|
|
|
|
|
|
|
static guint successes;
|
|
|
|
static guint failures;
|
|
|
|
static gboolean success_should_print = FALSE;
|
|
|
|
|
|
|
|
void
|
|
|
|
success_call(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char* file,
|
|
|
|
int line )
|
2001-08-17 17:49:17 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
success_args( test_title, file, line, "" );
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
success_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line,
|
|
|
|
const char *format,
|
|
|
|
... )
|
2001-08-17 17:49:17 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vsuccess_args( test_title, file, line, format, ap );
|
|
|
|
va_end(ap);
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vsuccess_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line,
|
|
|
|
const char *format,
|
|
|
|
va_list ap)
|
2001-08-17 17:49:17 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
if ( success_should_print )
|
|
|
|
{
|
|
|
|
printf("SUCCESS: %s, %s:%d ", test_title, file, line );
|
|
|
|
vprintf(format, ap);
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
++successes;
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
failure_call(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line)
|
2001-08-17 17:49:17 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
failure_args( test_title, file, line, "" );
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
failure_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char *file,
|
|
|
|
int line,
|
|
|
|
const char *format,
|
|
|
|
... )
|
2001-08-17 17:49:17 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vfailure_args( test_title, file, line, format, ap );
|
|
|
|
va_end(ap);
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
void
|
|
|
|
vfailure_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
const char *test_title,
|
|
|
|
const char* file,
|
|
|
|
int line,
|
|
|
|
const char *format,
|
|
|
|
va_list ap)
|
2001-08-17 17:49:17 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
printf("FAILURE %s %s:%d ", test_title, file, line );
|
|
|
|
vprintf(format, ap);
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
2001-08-17 17:49:17 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
++failures;
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_rv(void)
|
|
|
|
{
|
2006-03-30 19:43:16 -06:00
|
|
|
return failures;
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
2001-09-28 04:04:43 -05:00
|
|
|
gboolean
|
2006-08-08 18:16:54 -05:00
|
|
|
do_test_call(gboolean result, const char* test_title, const char* filename,
|
|
|
|
int line )
|
2001-08-17 17:49:17 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
if (result)
|
|
|
|
success_args( test_title, filename, line, "" );
|
|
|
|
else
|
|
|
|
failure_args( test_title, filename, line, "" );
|
2001-09-28 04:04:43 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
return result;
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
2001-10-15 19:50:01 -05:00
|
|
|
gboolean
|
2001-08-17 17:49:17 -05:00
|
|
|
do_test_args(
|
2009-12-29 14:12:48 -06:00
|
|
|
gboolean result,
|
|
|
|
const char* test_title,
|
|
|
|
const char* filename,
|
|
|
|
int line,
|
|
|
|
const char* format,
|
|
|
|
... )
|
2001-08-17 17:49:17 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2001-08-17 17:49:17 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if ( result )
|
|
|
|
{
|
|
|
|
vsuccess_args( test_title, filename, line, format, ap );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vfailure_args( test_title, filename, line, format, ap );
|
|
|
|
}
|
|
|
|
va_end(ap);
|
2001-10-15 19:50:01 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
return result;
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
print_test_results(void)
|
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
guint total = successes + failures;
|
|
|
|
if ( total == 1 )
|
|
|
|
{
|
|
|
|
printf( "Executed 1 test." );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Executed %d tests.", successes + failures );
|
|
|
|
}
|
|
|
|
if ( failures )
|
|
|
|
{
|
|
|
|
if ( failures == 1 )
|
|
|
|
{
|
|
|
|
printf(" There was 1 failure." );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(" There were %d failures.", failures );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf(" All tests passed.");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_success_print( gboolean in_should_print )
|
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
success_should_print = in_should_print;
|
2001-08-17 17:49:17 -05:00
|
|
|
}
|
|
|
|
|
2001-08-17 19:13:45 -05:00
|
|
|
gboolean
|
|
|
|
get_random_boolean(void)
|
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
return get_random_int_in_range (0, 1);
|
2001-08-17 19:13:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
get_random_int_in_range(int start, int end)
|
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
return CLAMP (start + (int)((double)(end - start + 1) * rand() /
|
|
|
|
(RAND_MAX + 1.0)),
|
|
|
|
start,
|
|
|
|
end);
|
2001-08-17 19:13:45 -05:00
|
|
|
}
|
|
|
|
|
2001-10-11 03:22:44 -05:00
|
|
|
static char *random_chars = NULL;
|
|
|
|
|
|
|
|
static char plain_chars[] =
|
2009-12-29 14:12:48 -06:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"1234567890"
|
|
|
|
" ";
|
2001-10-11 03:22:44 -05:00
|
|
|
|
|
|
|
static char funky_chars[] =
|
2009-12-29 14:12:48 -06:00
|
|
|
",.'\"`~!@#$%^*(){}[]/=?+-_\\|"
|
|
|
|
"<>&"
|
|
|
|
"\n\t";
|
2001-10-11 03:22:44 -05:00
|
|
|
|
|
|
|
static int rcend = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
random_character_include_funky_chars (gboolean use_funky_chars)
|
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
g_free (random_chars);
|
2001-10-11 03:22:44 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (use_funky_chars)
|
|
|
|
random_chars = g_strconcat (plain_chars, funky_chars, NULL);
|
|
|
|
else
|
|
|
|
random_chars = g_strdup (plain_chars);
|
2001-10-11 03:22:44 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
rcend = strlen (random_chars) - 1;
|
2001-10-11 03:22:44 -05:00
|
|
|
}
|
2001-08-17 19:13:45 -05:00
|
|
|
|
|
|
|
gchar
|
|
|
|
get_random_character(void)
|
|
|
|
{
|
2001-10-11 03:22:44 -05:00
|
|
|
if (!rcend)
|
2009-12-29 14:12:48 -06:00
|
|
|
random_character_include_funky_chars (TRUE);
|
2001-10-11 03:22:44 -05:00
|
|
|
|
2001-08-17 19:13:45 -05:00
|
|
|
return random_chars[get_random_int_in_range(0, rcend)];
|
|
|
|
}
|
|
|
|
|
2006-03-30 19:43:16 -06:00
|
|
|
gchar *
|
|
|
|
get_random_string_length_in_range(int minlen, int maxlen)
|
|
|
|
{
|
|
|
|
gchar *ret;
|
|
|
|
int i, len = get_random_int_in_range(minlen, maxlen);
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2006-03-30 19:43:16 -06:00
|
|
|
ret = g_new0(gchar, len);
|
|
|
|
|
|
|
|
for (i = 0; i < len - 1; i++)
|
|
|
|
ret[i] = get_random_character ();
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2006-03-30 19:43:16 -06:00
|
|
|
return g_strstrip(ret);
|
|
|
|
}
|
|
|
|
|
2001-10-12 05:50:35 -05:00
|
|
|
gchar *
|
|
|
|
get_random_string_without(const char *exclude_chars)
|
2001-08-17 19:13:45 -05:00
|
|
|
{
|
|
|
|
gchar *ret;
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
switch (get_random_int_in_range(0, 9))
|
2001-08-17 19:13:45 -05:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
/* case 0: */
|
|
|
|
/* return ""; */
|
|
|
|
/* case 1: */
|
|
|
|
/* return NULL; */
|
|
|
|
/* case 2: */
|
|
|
|
/* len = get_random_int_in_range(1000, 5000); */
|
|
|
|
/* break; */
|
2001-08-17 19:13:45 -05:00
|
|
|
case 3:
|
|
|
|
len = get_random_int_in_range(100, 500);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
len = get_random_int_in_range(5, 20);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = g_new0(gchar, len);
|
|
|
|
|
2001-10-12 05:50:35 -05:00
|
|
|
for (i = 0; i < len - 1; i++)
|
2001-08-17 19:13:45 -05:00
|
|
|
{
|
2001-10-12 05:50:35 -05:00
|
|
|
char c;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c = get_random_character ();
|
2009-12-29 14:12:48 -06:00
|
|
|
}
|
|
|
|
while (exclude_chars && strchr (exclude_chars, c));
|
2001-10-12 05:50:35 -05:00
|
|
|
|
|
|
|
ret[i] = c;
|
2001-08-17 19:13:45 -05:00
|
|
|
}
|
2001-10-11 03:22:44 -05:00
|
|
|
|
|
|
|
return g_strstrip (ret);
|
2001-08-17 19:13:45 -05:00
|
|
|
}
|
|
|
|
|
2001-10-12 05:50:35 -05:00
|
|
|
gchar *
|
|
|
|
get_random_string(void)
|
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
return get_random_string_without (NULL);
|
2001-10-12 05:50:35 -05:00
|
|
|
}
|
|
|
|
|
2013-12-05 15:54:18 -06:00
|
|
|
gint32
|
|
|
|
get_random_gint32 (void)
|
|
|
|
{
|
|
|
|
gint32 ret = 0;
|
|
|
|
|
|
|
|
if (RAND_MAX > (1 << 15))
|
|
|
|
return rand ();
|
|
|
|
|
|
|
|
if (RAND_MAX > (1 << 7))
|
|
|
|
{
|
|
|
|
ret = rand ();
|
|
|
|
ret <<= 16;
|
|
|
|
ret += rand ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = rand ();
|
|
|
|
ret <<= 8;
|
|
|
|
ret += rand ();
|
|
|
|
ret <<= 8;
|
|
|
|
ret += rand ();
|
|
|
|
ret <<= 8;
|
|
|
|
ret += rand ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-08-17 19:13:45 -05:00
|
|
|
gint64
|
|
|
|
get_random_gint64(void)
|
|
|
|
{
|
|
|
|
gint64 ret = 0;
|
|
|
|
|
2013-12-05 15:54:18 -06:00
|
|
|
ret = get_random_gint32 ();
|
2001-08-17 19:13:45 -05:00
|
|
|
ret <<= 32;
|
2013-12-05 15:54:18 -06:00
|
|
|
ret += get_random_gint32 ();
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2001-08-17 19:13:45 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
get_random_double(void)
|
|
|
|
{
|
2006-02-12 12:55:36 -06:00
|
|
|
double d;
|
2009-12-29 14:12:48 -06:00
|
|
|
guint i;
|
2001-08-17 19:13:45 -05:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
i = (guint)get_random_int_in_range(8, 13);
|
|
|
|
/* using 0.9 and 7 increases chances of getting lots of decimals */
|
|
|
|
d = ((double)get_random_int_in_range(8, 999999) * i * 0.9 / 7);
|
2006-02-12 12:55:36 -06:00
|
|
|
return d;
|
2001-08-17 19:13:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
get_random_string_in_array(const char* str_list[])
|
|
|
|
{
|
|
|
|
int num;
|
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
/* count number of items in list */
|
|
|
|
for (num = 0; str_list[num] != NULL; num++)
|
2004-08-28 21:10:15 -05:00
|
|
|
;
|
2009-12-29 14:12:48 -06:00
|
|
|
|
|
|
|
num = get_random_int_in_range(0, num - 1);
|
|
|
|
return str_list[num];
|
2001-08-17 19:13:45 -05:00
|
|
|
}
|