mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
More work on test infrastructure.
git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@5170 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
@@ -635,6 +635,7 @@ AC_OUTPUT(
|
||||
src/doc/design/Makefile
|
||||
src/engine/Makefile
|
||||
src/engine/test/Makefile
|
||||
src/engine/test-core/Makefile
|
||||
src/experimental/Makefile
|
||||
src/experimental/cbb/Makefile
|
||||
src/experimental/cbb/cbb-engine/Makefile
|
||||
|
||||
@@ -87,66 +87,6 @@ print_dom_tree(gpointer data_for_children, GSList* data_from_children,
|
||||
|
||||
/***********************************************************************/
|
||||
|
||||
gint
|
||||
get_random_int_in_range(int start, int end)
|
||||
{
|
||||
return start + (int)((double)end * rand() / (RAND_MAX + 1.0));
|
||||
}
|
||||
|
||||
static char random_chars[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"1234567890"
|
||||
" "
|
||||
/* ",.'\"`~!@#$%^*(){}[]/=?+-_\\|" */
|
||||
/* "<>&" */
|
||||
/* "\n\t" */
|
||||
"";
|
||||
|
||||
gchar
|
||||
get_random_character(void)
|
||||
{
|
||||
static int rcend = 0;
|
||||
if(!rcend)
|
||||
{
|
||||
rcend = strlen(random_chars) - 1;
|
||||
}
|
||||
|
||||
return random_chars[get_random_int_in_range(0, rcend)];
|
||||
}
|
||||
|
||||
gchar*
|
||||
get_random_string(void)
|
||||
{
|
||||
gchar *ret;
|
||||
int len;
|
||||
int i;
|
||||
|
||||
switch(get_random_int_in_range(0, 9))
|
||||
{
|
||||
/* case 0: */
|
||||
/* return ""; */
|
||||
/* case 1: */
|
||||
/* return NULL; */
|
||||
/* case 2: */
|
||||
/* len = get_random_int_in_range(1000, 5000); */
|
||||
/* break; */
|
||||
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);
|
||||
|
||||
for(i = 0; i < len - 1; i++)
|
||||
{
|
||||
ret[i] = get_random_character();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
Timespec*
|
||||
get_random_timespec(void)
|
||||
{
|
||||
@@ -160,33 +100,6 @@ get_random_timespec(void)
|
||||
return ret;
|
||||
}
|
||||
|
||||
gint64
|
||||
get_random_gint64(void)
|
||||
{
|
||||
gint64 ret = 0;
|
||||
|
||||
ret = rand();
|
||||
ret <<= 32;
|
||||
ret += rand();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
double
|
||||
get_random_double(void)
|
||||
{
|
||||
union
|
||||
{
|
||||
double d;
|
||||
gint64 i;
|
||||
} ret;
|
||||
|
||||
|
||||
ret.i = get_random_gint64();
|
||||
|
||||
return ret.d;
|
||||
}
|
||||
|
||||
gnc_numeric
|
||||
get_random_gnc_numeric(void)
|
||||
{
|
||||
@@ -593,12 +506,6 @@ get_random_commodity(void)
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
get_random_boolean(void)
|
||||
{
|
||||
return get_random_int_in_range (0, 1);
|
||||
}
|
||||
|
||||
static GList *
|
||||
get_random_guids(int max)
|
||||
{
|
||||
@@ -957,30 +864,6 @@ equals_node_val_vs_date(xmlNodePtr node, const Timespec tm)
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************/
|
||||
const char*
|
||||
get_random_string_in_array(const char* str_list[])
|
||||
{
|
||||
int num;
|
||||
const char *to_ret = NULL;
|
||||
|
||||
for(num = 0; str_list[num] != NULL; num++)
|
||||
{
|
||||
switch(get_random_int_in_range(0, num + 1))
|
||||
{
|
||||
case 0:
|
||||
to_ret = str_list[num];
|
||||
default:
|
||||
if(str_list[num + 1] == NULL)
|
||||
{
|
||||
return to_ret;
|
||||
}
|
||||
to_ret = str_list[num + 1];
|
||||
}
|
||||
}
|
||||
return to_ret;
|
||||
}
|
||||
|
||||
/***********************************************************************/
|
||||
|
||||
static gboolean
|
||||
|
||||
@@ -28,8 +28,6 @@
|
||||
#include "Account.h"
|
||||
#include "Query.h"
|
||||
|
||||
gchar get_random_character(void);
|
||||
gchar* get_random_string(void);
|
||||
Timespec* get_random_timespec(void);
|
||||
kvp_value* get_random_kvp_value(int type);
|
||||
|
||||
@@ -43,15 +41,10 @@ typedef struct _bin_data bin_data;
|
||||
bin_data* get_random_binary_data(void);
|
||||
|
||||
kvp_frame* get_random_kvp_frame(void);
|
||||
gint64 get_random_gint64(void);
|
||||
double get_random_double(void);
|
||||
gnc_numeric get_random_gnc_numeric(void);
|
||||
gnc_commodity* get_random_gnc_commodity_ref(void);
|
||||
GUID* get_random_guid(void);
|
||||
GList* get_random_glist(void);
|
||||
gint get_random_int_in_range(int start, int end);
|
||||
const char* get_random_string_in_array(const char* str_list[]);
|
||||
gboolean get_random_boolean(void);
|
||||
|
||||
GNCPrice * get_random_price(void);
|
||||
Account* get_random_account(void);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
SUBDIRS = . test
|
||||
SUBDIRS = . test-core test
|
||||
|
||||
pkglib_LTLIBRARIES = libgncmod-engine.la
|
||||
|
||||
|
||||
2
src/engine/test-core/.cvsignore
Normal file
2
src/engine/test-core/.cvsignore
Normal file
@@ -0,0 +1,2 @@
|
||||
Makefile
|
||||
Makefile.in
|
||||
21
src/engine/test-core/Makefile.am
Normal file
21
src/engine/test-core/Makefile.am
Normal file
@@ -0,0 +1,21 @@
|
||||
noinst_LTLIBRARIES=libgncmod-test-engine.la
|
||||
|
||||
LDADD = \
|
||||
${top_srcdir}/src/gnc-module/libgncmodule.la \
|
||||
../libgncmod-engine.la \
|
||||
${GLIB_LIBS} \
|
||||
-lltdl
|
||||
|
||||
libgncmod_test_engine_la_SOURCES = gncmod-test-engine.c test-engine-stuff.c
|
||||
libgncmod_test_engine_la_LDFLAGS = -module
|
||||
libgncmod_test_engine_la_LIBADD = \
|
||||
-L${top_srcdir}/src/gnc-module -L${top_srcdir}/src/gnc-module/.libs -lgncmodule \
|
||||
${GLIB_LIBS}
|
||||
|
||||
noinst_HEADERS=test-engine-stuff.h
|
||||
|
||||
INCLUDES = \
|
||||
-I${top_srcdir}/src/gnc-module \
|
||||
-I${top_srcdir}/src/test-core \
|
||||
-I${top_srcdir}/src/engine \
|
||||
${GLIB_CFLAGS}
|
||||
40
src/engine/test-core/gncmod-test-engine.c
Normal file
40
src/engine/test-core/gncmod-test-engine.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/*********************************************************************
|
||||
* gncmod-test-engine.c
|
||||
* module definition/initialization for the engine test infrastructure
|
||||
*
|
||||
* Copyright (c) 2001 Linux Developers Group, Inc.
|
||||
*********************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "gnc-module.h"
|
||||
#include "gnc-module-api.h"
|
||||
|
||||
/* version of the gnc module system interface we require */
|
||||
int gnc_module_system_interface = 0;
|
||||
|
||||
/* module versioning uses libtool semantics. */
|
||||
int gnc_module_current = 0;
|
||||
int gnc_module_revision = 0;
|
||||
int gnc_module_age = 0;
|
||||
|
||||
char *
|
||||
gnc_module_path(void) {
|
||||
return g_strdup("gnucash/engine/test");
|
||||
}
|
||||
|
||||
char *
|
||||
gnc_module_description(void) {
|
||||
return g_strdup("GnuCash Engine test infrastructure.");
|
||||
}
|
||||
|
||||
int
|
||||
gnc_module_init(int refcount) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int
|
||||
gnc_module_end(int refcount) {
|
||||
return TRUE;
|
||||
}
|
||||
625
src/engine/test-core/test-engine-stuff.c
Normal file
625
src/engine/test-core/test-engine-stuff.c
Normal file
@@ -0,0 +1,625 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "date.h"
|
||||
#include "gnc-engine-util.h"
|
||||
#include "test-engine-stuff.h"
|
||||
#include "test-stuff.h"
|
||||
|
||||
/***********************************************************************/
|
||||
|
||||
Timespec*
|
||||
get_random_timespec(void)
|
||||
{
|
||||
Timespec *ret;
|
||||
|
||||
ret = g_new(Timespec, 1);
|
||||
|
||||
ret->tv_sec = rand();
|
||||
ret->tv_nsec = rand();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
gnc_numeric
|
||||
get_random_gnc_numeric(void)
|
||||
{
|
||||
return gnc_numeric_create(get_random_gint64(), rand());
|
||||
}
|
||||
|
||||
const char *types[] =
|
||||
{
|
||||
"NASDAQ",
|
||||
"NYSE",
|
||||
"EUREX",
|
||||
"FUND",
|
||||
"AMEX",
|
||||
NULL
|
||||
};
|
||||
|
||||
const char*
|
||||
get_random_commodity_namespace(void)
|
||||
{
|
||||
return get_random_string_in_array(types);
|
||||
}
|
||||
|
||||
gnc_commodity*
|
||||
get_random_gnc_commodity_ref(void)
|
||||
{
|
||||
const gchar *name;
|
||||
gchar *mnemonic;
|
||||
gnc_commodity *ret;
|
||||
|
||||
name = get_random_commodity_namespace();
|
||||
mnemonic = get_random_string();
|
||||
|
||||
if(!name || !mnemonic)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = gnc_commodity_new("", name, mnemonic, "", 0);
|
||||
|
||||
g_free(mnemonic);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
GNCPrice *
|
||||
get_random_price(void)
|
||||
{
|
||||
GNCPrice *p;
|
||||
Timespec *ts;
|
||||
char *string;
|
||||
|
||||
p = gnc_price_create ();
|
||||
|
||||
gnc_price_set_commodity (p, get_random_gnc_commodity_ref ());
|
||||
gnc_price_set_currency (p, get_random_gnc_commodity_ref ());
|
||||
|
||||
ts = get_random_timespec ();
|
||||
gnc_price_set_time (p, *ts);
|
||||
g_free (ts);
|
||||
|
||||
string = get_random_string ();
|
||||
gnc_price_set_source (p, string);
|
||||
g_free (string);
|
||||
|
||||
string = get_random_string ();
|
||||
gnc_price_set_type (p, string);
|
||||
g_free (string);
|
||||
|
||||
gnc_price_set_value (p, get_random_gnc_numeric ());
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
GUID*
|
||||
get_random_guid(void)
|
||||
{
|
||||
GUID *ret;
|
||||
|
||||
ret = g_new(GUID, 1);
|
||||
guid_new(ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
GList*
|
||||
get_random_glist(void)
|
||||
{
|
||||
GList *ret = NULL;
|
||||
int i;
|
||||
int count = get_random_int_in_range(1, 5);
|
||||
|
||||
for(i = 0; i < count; i++)
|
||||
{
|
||||
ret = g_list_prepend(ret, (gpointer)get_random_kvp_value(-2));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bin_data*
|
||||
get_random_binary_data(void)
|
||||
{
|
||||
int len;
|
||||
bin_data *ret;
|
||||
|
||||
len = get_random_int_in_range(20,100);
|
||||
ret = g_new(bin_data, 1);
|
||||
ret->data = g_new(char, len);
|
||||
ret->len = len;
|
||||
|
||||
for(len--; len >= 0; len--)
|
||||
{
|
||||
ret->data[len] = (char)get_random_int_in_range(0,255);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
kvp_frame*
|
||||
get_random_kvp_frame(void)
|
||||
{
|
||||
kvp_frame *ret;
|
||||
int vals_to_add;
|
||||
|
||||
ret = kvp_frame_new();
|
||||
|
||||
vals_to_add = get_random_int_in_range(1,10);
|
||||
|
||||
for(;vals_to_add > 0; vals_to_add--)
|
||||
{
|
||||
gchar *key;
|
||||
kvp_value *val;
|
||||
|
||||
key = get_random_string();
|
||||
val = get_random_kvp_value(-1);
|
||||
|
||||
if(!key)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
kvp_frame_set_slot_nc(ret, key, val);
|
||||
|
||||
g_free(key);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
kvp_value*
|
||||
get_random_kvp_value(int type)
|
||||
{
|
||||
int datype = type;
|
||||
|
||||
if(datype == -1)
|
||||
{
|
||||
datype = get_random_int_in_range(KVP_TYPE_GINT64, KVP_TYPE_FRAME + 1);
|
||||
}
|
||||
|
||||
if(datype == -2)
|
||||
{
|
||||
datype = get_random_int_in_range(KVP_TYPE_GINT64, KVP_TYPE_FRAME);
|
||||
}
|
||||
|
||||
switch(datype)
|
||||
{
|
||||
case KVP_TYPE_GINT64:
|
||||
return kvp_value_new_gint64(get_random_gint64());
|
||||
break;
|
||||
|
||||
case KVP_TYPE_DOUBLE:
|
||||
return kvp_value_new_double(get_random_double());
|
||||
break;
|
||||
|
||||
case KVP_TYPE_NUMERIC:
|
||||
return kvp_value_new_gnc_numeric(get_random_gnc_numeric());
|
||||
break;
|
||||
|
||||
case KVP_TYPE_STRING:
|
||||
{
|
||||
gchar *tmp_str;
|
||||
kvp_value *ret;
|
||||
tmp_str = get_random_string();
|
||||
if(!tmp_str)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = kvp_value_new_string(tmp_str);
|
||||
g_free(tmp_str);
|
||||
return ret;
|
||||
}
|
||||
break;
|
||||
|
||||
case KVP_TYPE_GUID:
|
||||
{
|
||||
GUID *tmp_guid;
|
||||
kvp_value *ret;
|
||||
tmp_guid = get_random_guid();
|
||||
ret = kvp_value_new_guid(tmp_guid);
|
||||
g_free(tmp_guid);
|
||||
return ret;
|
||||
}
|
||||
break;
|
||||
|
||||
case KVP_TYPE_BINARY:
|
||||
{
|
||||
bin_data *tmp_data;
|
||||
kvp_value *ret;
|
||||
tmp_data = get_random_binary_data();
|
||||
ret = kvp_value_new_binary(tmp_data->data, tmp_data->len);
|
||||
g_free(tmp_data->data);
|
||||
g_free(tmp_data);
|
||||
return ret;
|
||||
}
|
||||
break;
|
||||
|
||||
case KVP_TYPE_GLIST:
|
||||
return kvp_value_new_glist_nc(get_random_glist());
|
||||
break;
|
||||
|
||||
case KVP_TYPE_FRAME:
|
||||
{
|
||||
kvp_frame *tmp_frame;
|
||||
kvp_value *ret;
|
||||
tmp_frame = get_random_kvp_frame();
|
||||
ret = kvp_value_new_frame(tmp_frame);
|
||||
kvp_frame_delete(tmp_frame);
|
||||
return ret;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
set_account_random_string(Account* act,
|
||||
void(*func)(Account *act, const gchar*str))
|
||||
{
|
||||
gchar *tmp_str = get_random_string();
|
||||
if(tmp_str)
|
||||
{
|
||||
(func)(act, tmp_str);
|
||||
g_free(tmp_str);
|
||||
}
|
||||
}
|
||||
|
||||
Account*
|
||||
get_random_account(void)
|
||||
{
|
||||
Account *ret;
|
||||
int tmp_int;
|
||||
|
||||
ret = xaccMallocAccount();
|
||||
|
||||
xaccAccountBeginEdit(ret);
|
||||
|
||||
set_account_random_string(ret, xaccAccountSetName);
|
||||
|
||||
tmp_int = get_random_int_in_range(BANK, CREDITLINE);
|
||||
xaccAccountSetType(ret, tmp_int);
|
||||
|
||||
set_account_random_string(ret, xaccAccountSetCode);
|
||||
set_account_random_string(ret, xaccAccountSetDescription);
|
||||
|
||||
xaccAccountSetCommodity(ret, get_random_gnc_commodity_ref());
|
||||
|
||||
xaccAccountSetSlots_nc(ret, get_random_kvp_frame());
|
||||
|
||||
xaccAccountCommitEdit(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
set_split_random_string(Split *spl,
|
||||
void(*func)(Split *act, const gchar*str))
|
||||
{
|
||||
gchar *tmp_str = get_random_string();
|
||||
if(tmp_str)
|
||||
{
|
||||
(func)(spl, tmp_str);
|
||||
g_free(tmp_str);
|
||||
}
|
||||
}
|
||||
|
||||
static char possible_chars[] = { 'c', 'y', 'f', 'n' };
|
||||
|
||||
Split*
|
||||
get_random_split(gnc_numeric num)
|
||||
{
|
||||
Split *ret;
|
||||
gnc_numeric oneVal;
|
||||
|
||||
ret = xaccMallocSplit();
|
||||
|
||||
set_split_random_string(ret, xaccSplitSetMemo);
|
||||
set_split_random_string(ret, xaccSplitSetAction);
|
||||
|
||||
xaccSplitSetReconcile(ret, possible_chars[get_random_int_in_range(0, 4)]);
|
||||
|
||||
xaccSplitSetDateReconciledTS(ret, get_random_timespec());
|
||||
|
||||
xaccSplitSetValue(ret, num);
|
||||
xaccSplitSetAmount(ret, num);
|
||||
|
||||
oneVal = gnc_numeric_create(1,1);
|
||||
xaccSplitSetSharePrice(ret, oneVal);
|
||||
|
||||
xaccSplitSetSlots_nc(ret, get_random_kvp_frame());
|
||||
|
||||
{
|
||||
GUID *ranguid = get_random_guid();
|
||||
xaccSplitSetAccountGUID(ret, *ranguid);
|
||||
g_free(ranguid);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
set_tran_random_string(Transaction* trn,
|
||||
void(*func)(Transaction *act, const gchar*str))
|
||||
{
|
||||
gchar *tmp_str = get_random_string();
|
||||
if(tmp_str)
|
||||
{
|
||||
(func)(trn, tmp_str);
|
||||
g_free(tmp_str);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
add_random_splits(Transaction *trn)
|
||||
{
|
||||
gnc_numeric num = get_random_gnc_numeric();
|
||||
xaccTransAppendSplit(trn, get_random_split(num));
|
||||
xaccTransAppendSplit(trn, get_random_split(gnc_numeric_neg(num)));
|
||||
}
|
||||
|
||||
static void
|
||||
trn_add_ran_timespec(Transaction *trn, void (*func)(Transaction*,
|
||||
const Timespec*))
|
||||
{
|
||||
Timespec *to_set;
|
||||
|
||||
to_set = get_random_timespec();
|
||||
func(trn, to_set);
|
||||
g_free(to_set);
|
||||
}
|
||||
|
||||
|
||||
Transaction*
|
||||
get_random_transaction(void)
|
||||
{
|
||||
Transaction* ret;
|
||||
|
||||
ret = xaccMallocTransaction();
|
||||
|
||||
xaccTransBeginEdit(ret);
|
||||
|
||||
xaccTransSetCurrency(ret, get_random_gnc_commodity_ref());
|
||||
|
||||
set_tran_random_string(ret, xaccTransSetNum);
|
||||
|
||||
trn_add_ran_timespec(ret, xaccTransSetDatePostedTS);
|
||||
trn_add_ran_timespec(ret, xaccTransSetDateEnteredTS);
|
||||
|
||||
set_tran_random_string(ret, xaccTransSetDescription);
|
||||
|
||||
xaccTransSetSlots_nc(ret, get_random_kvp_frame());
|
||||
|
||||
add_random_splits(ret);
|
||||
|
||||
xaccTransCommitEdit(ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
gnc_commodity*
|
||||
get_random_commodity(void)
|
||||
{
|
||||
gnc_commodity *ret;
|
||||
gchar *name;
|
||||
const gchar *space;
|
||||
gchar *mn;
|
||||
gchar *xcode;
|
||||
int ran_int;
|
||||
|
||||
name = get_random_string();
|
||||
space = get_random_commodity_namespace();
|
||||
mn = get_random_string();
|
||||
xcode = get_random_string();
|
||||
ran_int = get_random_int_in_range(1, 100000);
|
||||
|
||||
ret = gnc_commodity_new(name, space, mn, xcode, ran_int);
|
||||
|
||||
g_free(name);
|
||||
g_free(mn);
|
||||
g_free(xcode);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static GList *
|
||||
get_random_guids(int max)
|
||||
{
|
||||
GList *guids = NULL;
|
||||
int num_guids;
|
||||
|
||||
if (max < 1) return NULL;
|
||||
|
||||
num_guids = get_random_int_in_range (1, max);
|
||||
|
||||
while (num_guids-- > 0)
|
||||
g_list_prepend (guids, get_random_guid ());
|
||||
|
||||
return guids;
|
||||
}
|
||||
|
||||
static void
|
||||
free_random_guids(GList *guids)
|
||||
{
|
||||
GList *node;
|
||||
|
||||
for (node = guids; node; node = node->next)
|
||||
g_free (node->data);
|
||||
|
||||
g_list_free (guids);
|
||||
}
|
||||
|
||||
static QueryOp
|
||||
get_random_queryop(void)
|
||||
{
|
||||
return get_random_int_in_range (1, QUERY_XOR);
|
||||
}
|
||||
|
||||
Query *
|
||||
get_random_query(void)
|
||||
{
|
||||
Query *q;
|
||||
Query *temp_q;
|
||||
int num_terms;
|
||||
|
||||
num_terms = get_random_int_in_range (1, 4);
|
||||
|
||||
q = xaccMallocQuery ();
|
||||
|
||||
while (num_terms-- > 0)
|
||||
{
|
||||
pr_type_t pr_type;
|
||||
Timespec *start;
|
||||
Timespec *end;
|
||||
GList *guids;
|
||||
char *string;
|
||||
GUID *guid;
|
||||
|
||||
pr_type = get_random_int_in_range (1, 20);
|
||||
|
||||
switch (pr_type)
|
||||
{
|
||||
case PR_ACCOUNT:
|
||||
guids = get_random_guids (10);
|
||||
xaccQueryAddAccountGUIDMatch
|
||||
(q,
|
||||
guids,
|
||||
get_random_int_in_range (1, ACCT_MATCH_NONE),
|
||||
get_random_queryop ());
|
||||
free_random_guids (guids);
|
||||
break;
|
||||
|
||||
case PR_ACTION:
|
||||
string = get_random_string ();
|
||||
xaccQueryAddActionMatch (q,
|
||||
string,
|
||||
get_random_boolean (),
|
||||
get_random_boolean (),
|
||||
get_random_queryop ());
|
||||
g_free (string);
|
||||
break;
|
||||
|
||||
case PR_AMOUNT:
|
||||
DxaccQueryAddAmountMatch
|
||||
(q,
|
||||
get_random_double (),
|
||||
get_random_int_in_range (1, AMT_SGN_MATCH_DEBIT),
|
||||
get_random_int_in_range (1, AMT_MATCH_EXACTLY),
|
||||
get_random_queryop ());
|
||||
break;
|
||||
|
||||
case PR_BALANCE:
|
||||
xaccQueryAddBalanceMatch
|
||||
(q,
|
||||
get_random_int_in_range (1, BALANCE_UNBALANCED),
|
||||
get_random_queryop ());
|
||||
break;
|
||||
|
||||
case PR_CLEARED:
|
||||
xaccQueryAddClearedMatch
|
||||
(q,
|
||||
get_random_int_in_range (1, CLEARED_FROZEN),
|
||||
get_random_queryop ());
|
||||
break;
|
||||
|
||||
case PR_DATE:
|
||||
start = get_random_timespec ();
|
||||
end = get_random_timespec ();
|
||||
xaccQueryAddDateMatchTS (q,
|
||||
get_random_boolean (),
|
||||
*start,
|
||||
get_random_boolean (),
|
||||
*end,
|
||||
get_random_queryop ());
|
||||
g_free (start);
|
||||
g_free (end);
|
||||
break;
|
||||
|
||||
case PR_DESC:
|
||||
string = get_random_string ();
|
||||
xaccQueryAddDescriptionMatch (q,
|
||||
string,
|
||||
get_random_boolean (),
|
||||
get_random_boolean (),
|
||||
get_random_queryop ());
|
||||
g_free (string);
|
||||
break;
|
||||
|
||||
case PR_GUID:
|
||||
guid = get_random_guid ();
|
||||
xaccQueryAddGUIDMatch (q, guid, get_random_queryop ());
|
||||
g_free (guid);
|
||||
break;
|
||||
|
||||
case PR_MEMO:
|
||||
string = get_random_string ();
|
||||
xaccQueryAddMemoMatch (q,
|
||||
string,
|
||||
get_random_boolean (),
|
||||
get_random_boolean (),
|
||||
get_random_queryop ());
|
||||
g_free (string);
|
||||
break;
|
||||
|
||||
case PR_NUM:
|
||||
string = get_random_string ();
|
||||
xaccQueryAddNumberMatch (q,
|
||||
string,
|
||||
get_random_boolean (),
|
||||
get_random_boolean (),
|
||||
get_random_queryop ());
|
||||
g_free (string);
|
||||
break;
|
||||
|
||||
case PR_PRICE:
|
||||
DxaccQueryAddSharePriceMatch
|
||||
(q,
|
||||
get_random_double (),
|
||||
get_random_int_in_range (1, AMT_MATCH_EXACTLY),
|
||||
get_random_queryop ());
|
||||
break;
|
||||
|
||||
case PR_SHRS:
|
||||
DxaccQueryAddSharesMatch
|
||||
(q,
|
||||
get_random_double (),
|
||||
get_random_int_in_range (1, AMT_MATCH_EXACTLY),
|
||||
get_random_queryop ());
|
||||
break;
|
||||
|
||||
case PR_MISC: /* PR_MISC shouldn't be used anyway :) */
|
||||
default:
|
||||
num_terms++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
xaccQuerySetSortOrder (q,
|
||||
get_random_int_in_range (1, BY_NONE),
|
||||
get_random_int_in_range (1, BY_NONE),
|
||||
get_random_int_in_range (1, BY_NONE));
|
||||
|
||||
xaccQuerySetSortIncreasing (q,
|
||||
get_random_boolean (),
|
||||
get_random_boolean (),
|
||||
get_random_boolean ());
|
||||
|
||||
xaccQuerySetMaxSplits (q, get_random_int_in_range (-50000, 50000));
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
52
src/engine/test-core/test-engine-stuff.h
Normal file
52
src/engine/test-core/test-engine-stuff.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/* Created by bstanley 20010323
|
||||
* moved contents from test-stuff.h
|
||||
*
|
||||
* This file declares testing functions which rely on other parts
|
||||
* of gnucash.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef GNC_TEST_STUFF_H
|
||||
#define GNC_TEST_STUFF_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "gnc-commodity.h"
|
||||
#include "gnc-pricedb.h"
|
||||
#include "guid.h"
|
||||
#include "kvp_frame.h"
|
||||
#include "date.h"
|
||||
#include "Account.h"
|
||||
#include "Query.h"
|
||||
|
||||
Timespec* get_random_timespec(void);
|
||||
kvp_value* get_random_kvp_value(int type);
|
||||
|
||||
struct _bin_data
|
||||
{
|
||||
guchar *data;
|
||||
int len;
|
||||
};
|
||||
typedef struct _bin_data bin_data;
|
||||
|
||||
bin_data* get_random_binary_data(void);
|
||||
|
||||
kvp_frame* get_random_kvp_frame(void);
|
||||
gnc_numeric get_random_gnc_numeric(void);
|
||||
gnc_commodity* get_random_gnc_commodity_ref(void);
|
||||
GUID* get_random_guid(void);
|
||||
GList* get_random_glist(void);
|
||||
|
||||
GNCPrice * get_random_price(void);
|
||||
Account* get_random_account(void);
|
||||
Split* get_random_split(gnc_numeric num);
|
||||
Transaction* get_random_transaction(void);
|
||||
gnc_commodity* get_random_commodity(void);
|
||||
const char *get_random_commodity_namespace(void);
|
||||
|
||||
Query* get_random_query(void);
|
||||
|
||||
#endif /* GNC_TEST_STUFF_H */
|
||||
@@ -1,5 +1,6 @@
|
||||
Makefile
|
||||
test-load-engine
|
||||
Makefile.in
|
||||
.deps
|
||||
.libs
|
||||
Makefile.in
|
||||
test-load-engine
|
||||
test-commodities
|
||||
|
||||
@@ -1,14 +1,35 @@
|
||||
INCLUDES=-I${top_srcdir}/src/gnc-module ${GUILE_INCS}
|
||||
CFLAGS=@CFLAGS@ ${GLIB_CFLAGS}
|
||||
LDADD=${top_srcdir}/src/gnc-module/libgncmodule.la ${GLIB_LIBS} -lltdl
|
||||
INCLUDES = \
|
||||
-I${top_srcdir}/src/gnc-module \
|
||||
-I${top_srcdir}/src/test-core \
|
||||
-I.. \
|
||||
-I../test-core \
|
||||
${GUILE_INCS}
|
||||
|
||||
CFLAGS = @CFLAGS@ ${GLIB_CFLAGS}
|
||||
|
||||
LDADD = \
|
||||
${top_srcdir}/src/gnc-module/libgncmodule.la \
|
||||
${top_srcdir}/src/test-core/libgncmod-test.la \
|
||||
../libgncmod-engine.la \
|
||||
../test-core/libgncmod-test-engine.la \
|
||||
${GLIB_LIBS} \
|
||||
-lltdl
|
||||
|
||||
TESTS = \
|
||||
test-load-engine \
|
||||
test-create-account \
|
||||
test-commodities
|
||||
|
||||
TESTS = test-load-engine test-create-account
|
||||
TESTS_ENVIRONMENT = \
|
||||
GNC_MODULE_PATH="${top_srcdir}/src/engine" \
|
||||
GUILE_LOAD_PATH="${G_WRAP_MODULE_DIR}:..:../../gnc-module" \
|
||||
LTDL_LIBRARY_PATH=../../gnc-module \
|
||||
LD_LIBRARY_PATH=../../gnc-module:../../gnc-module/.libs
|
||||
|
||||
bin_PROGRAMS = test-load-engine
|
||||
test_load_engine_SOURCES=test-load-engine.c
|
||||
bin_PROGRAMS = \
|
||||
test-commodities \
|
||||
test-load-engine
|
||||
|
||||
test_load_engine_SOURCES = test-load-engine.c
|
||||
|
||||
test_commodities_SOURCES = test-commodities.c
|
||||
|
||||
152
src/engine/test/test-commodities.c
Normal file
152
src/engine/test/test-commodities.c
Normal file
@@ -0,0 +1,152 @@
|
||||
#include <glib.h>
|
||||
|
||||
#include "gnc-engine-util.h"
|
||||
|
||||
#include "gnc-commodity.h"
|
||||
#include "test-engine-stuff.h"
|
||||
#include "test-stuff.h"
|
||||
|
||||
|
||||
static void
|
||||
test_commodity(void)
|
||||
{
|
||||
gnc_commodity *com;
|
||||
|
||||
{
|
||||
com = gnc_commodity_new(NULL, NULL, NULL, NULL, 0);
|
||||
|
||||
gnc_commodity_destroy(com);
|
||||
|
||||
success("commodity new and destroy");
|
||||
}
|
||||
|
||||
{
|
||||
char *fullname;
|
||||
const char *namespace;
|
||||
char *mnemonic;
|
||||
char *exchange_code;
|
||||
int fraction;
|
||||
gnc_commodity *com2;
|
||||
|
||||
fullname = get_random_string();
|
||||
namespace = get_random_commodity_namespace();
|
||||
mnemonic = get_random_string();
|
||||
exchange_code = get_random_string();
|
||||
fraction = get_random_int_in_range(0, 10000);
|
||||
|
||||
com = gnc_commodity_new(fullname, namespace, mnemonic,
|
||||
exchange_code, fraction);
|
||||
|
||||
do_test(
|
||||
com != NULL, "commodity with data new and destroy");
|
||||
|
||||
do_test(
|
||||
safe_strcmp(fullname, gnc_commodity_get_fullname(com)) == 0,
|
||||
"fullnames equal test");
|
||||
|
||||
do_test(
|
||||
safe_strcmp(namespace, gnc_commodity_get_namespace(com)) == 0,
|
||||
"namespace equal test");
|
||||
|
||||
do_test(
|
||||
safe_strcmp(mnemonic, gnc_commodity_get_mnemonic(com)) == 0,
|
||||
"mnemonic equal test");
|
||||
|
||||
do_test(
|
||||
safe_strcmp(exchange_code, gnc_commodity_get_exchange_code(com)) == 0,
|
||||
"exchange code equal test");
|
||||
|
||||
do_test(
|
||||
gnc_commodity_get_fraction(com) == fraction,
|
||||
"fraction code equal test");
|
||||
|
||||
fullname = get_random_string();
|
||||
gnc_commodity_set_fullname(com, fullname);
|
||||
do_test(
|
||||
safe_strcmp(fullname, gnc_commodity_get_fullname(com)) == 0,
|
||||
"reset fullnames equal test");
|
||||
|
||||
namespace = get_random_commodity_namespace();
|
||||
gnc_commodity_set_namespace(com, namespace);
|
||||
do_test(
|
||||
safe_strcmp(namespace, gnc_commodity_get_namespace(com)) == 0,
|
||||
"reset namespace equal test");
|
||||
|
||||
mnemonic = get_random_string();
|
||||
gnc_commodity_set_mnemonic(com, mnemonic);
|
||||
do_test(
|
||||
safe_strcmp(mnemonic, gnc_commodity_get_mnemonic(com)) == 0,
|
||||
"reset mnemonic equal test");
|
||||
|
||||
exchange_code = get_random_string();
|
||||
gnc_commodity_set_exchange_code(com, exchange_code);
|
||||
do_test(
|
||||
safe_strcmp(exchange_code, gnc_commodity_get_exchange_code(com)) == 0,
|
||||
"reset exchange code equal test");
|
||||
|
||||
fraction = get_random_int_in_range(0, 10000);
|
||||
gnc_commodity_set_fraction(com, fraction);
|
||||
do_test(
|
||||
gnc_commodity_get_fraction(com) == fraction,
|
||||
"reset fraction code equal test");
|
||||
|
||||
com2 = gnc_commodity_new(fullname, namespace, mnemonic,
|
||||
exchange_code, fraction);
|
||||
do_test(
|
||||
gnc_commodity_equiv(com, com2), "commodity equiv");
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
gnc_commodity_table *tbl;
|
||||
gnc_commodity *coms[20];
|
||||
|
||||
tbl = gnc_commodity_table_new();
|
||||
|
||||
do_test(gnc_commodity_table_get_size(tbl) == 0,
|
||||
"test size for 0 table");
|
||||
|
||||
for(i = 0; i < 20; i++)
|
||||
{
|
||||
coms[i] = get_random_commodity();
|
||||
|
||||
do_test(
|
||||
gnc_commodity_table_insert(tbl, coms[i]) != NULL,
|
||||
"insert test");
|
||||
|
||||
do_test_args(
|
||||
gnc_commodity_table_get_size(tbl) == i + 1,
|
||||
"test next size table", __FILE__, __LINE__, "should be %d and is %d", i + 1, gnc_commodity_table_get_size(tbl));
|
||||
|
||||
for(j = 0; j <= i; j++)
|
||||
{
|
||||
gnc_commodity *testcom;
|
||||
|
||||
do_test(
|
||||
(testcom = gnc_commodity_table_lookup(
|
||||
tbl, gnc_commodity_get_namespace(coms[j]),
|
||||
gnc_commodity_get_mnemonic(coms[j]))) != NULL,
|
||||
"lookup commodity");
|
||||
do_test(
|
||||
gnc_commodity_equiv(testcom, coms[j]),
|
||||
"lookup commodity and test equiv");
|
||||
}
|
||||
|
||||
do_test(
|
||||
gnc_commodity_table_has_namespace(
|
||||
tbl, gnc_commodity_get_namespace(coms[i])),
|
||||
"test have namespace");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
test_commodity();
|
||||
print_test_results();
|
||||
exit(get_rv());
|
||||
}
|
||||
@@ -4,7 +4,7 @@ libgncmod_test_la_SOURCES = gncmod-test.c test-stuff.c
|
||||
libgncmod_test_la_LDFLAGS = -module
|
||||
libgncmod_test_la_LIBADD = \
|
||||
-L${top_srcdir}/src/gnc-module -L${top_srcdir}/src/gnc-module/.libs -lgncmodule \
|
||||
${GLIB_LIBS} ${GUILE_LIBS}
|
||||
${GLIB_LIBS}
|
||||
|
||||
noinst_HEADERS=test-stuff.h
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*********************************************************************
|
||||
* gncmod-test.c
|
||||
* module definition/initialization for the report infrastructure
|
||||
* module definition/initialization for the test infrastructure
|
||||
*
|
||||
* Copyright (c) 2001 Linux Developers Group, Inc.
|
||||
*********************************************************************/
|
||||
|
||||
@@ -188,3 +188,119 @@ set_success_print( gboolean in_should_print )
|
||||
success_should_print = in_should_print;
|
||||
}
|
||||
|
||||
gboolean
|
||||
get_random_boolean(void)
|
||||
{
|
||||
return get_random_int_in_range (0, 1);
|
||||
}
|
||||
|
||||
gint
|
||||
get_random_int_in_range(int start, int end)
|
||||
{
|
||||
return start + (int)((double)end * rand() / (RAND_MAX + 1.0));
|
||||
}
|
||||
|
||||
static char random_chars[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"1234567890"
|
||||
" "
|
||||
/* ",.'\"`~!@#$%^*(){}[]/=?+-_\\|" */
|
||||
/* "<>&" */
|
||||
/* "\n\t" */
|
||||
"";
|
||||
|
||||
gchar
|
||||
get_random_character(void)
|
||||
{
|
||||
static int rcend = 0;
|
||||
if(!rcend)
|
||||
{
|
||||
rcend = strlen(random_chars) - 1;
|
||||
}
|
||||
|
||||
return random_chars[get_random_int_in_range(0, rcend)];
|
||||
}
|
||||
|
||||
gchar*
|
||||
get_random_string(void)
|
||||
{
|
||||
gchar *ret;
|
||||
int len;
|
||||
int i;
|
||||
|
||||
switch(get_random_int_in_range(0, 9))
|
||||
{
|
||||
/* case 0: */
|
||||
/* return ""; */
|
||||
/* case 1: */
|
||||
/* return NULL; */
|
||||
/* case 2: */
|
||||
/* len = get_random_int_in_range(1000, 5000); */
|
||||
/* break; */
|
||||
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);
|
||||
|
||||
for(i = 0; i < len - 1; i++)
|
||||
{
|
||||
ret[i] = get_random_character();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
gint64
|
||||
get_random_gint64(void)
|
||||
{
|
||||
gint64 ret = 0;
|
||||
|
||||
ret = rand();
|
||||
ret <<= 32;
|
||||
ret += rand();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
double
|
||||
get_random_double(void)
|
||||
{
|
||||
union
|
||||
{
|
||||
double d;
|
||||
gint64 i;
|
||||
} ret;
|
||||
|
||||
|
||||
ret.i = get_random_gint64();
|
||||
|
||||
return ret.d;
|
||||
}
|
||||
|
||||
const char*
|
||||
get_random_string_in_array(const char* str_list[])
|
||||
{
|
||||
int num;
|
||||
const char *to_ret = NULL;
|
||||
|
||||
for(num = 0; str_list[num] != NULL; num++)
|
||||
{
|
||||
switch(get_random_int_in_range(0, num + 1))
|
||||
{
|
||||
case 0:
|
||||
to_ret = str_list[num];
|
||||
default:
|
||||
if(str_list[num + 1] == NULL)
|
||||
{
|
||||
return to_ret;
|
||||
}
|
||||
to_ret = str_list[num + 1];
|
||||
}
|
||||
}
|
||||
|
||||
return to_ret;
|
||||
}
|
||||
|
||||
@@ -118,4 +118,12 @@ void failure_args(
|
||||
const char *format,
|
||||
... );
|
||||
|
||||
gboolean get_random_boolean(void);
|
||||
gint get_random_int_in_range(int start, int end);
|
||||
gchar get_random_character(void);
|
||||
gchar* get_random_string(void);
|
||||
gint64 get_random_gint64(void);
|
||||
double get_random_double(void);
|
||||
const char* get_random_string_in_array(const char* str_list[]);
|
||||
|
||||
#endif /* TEST_STUFF_H */
|
||||
|
||||
Reference in New Issue
Block a user