From 492ed743ae8c5d21db88bab5e1c91d7bc4eb78d4 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Sat, 3 Jul 2004 15:29:39 +0000 Subject: [PATCH] re-order the way that random data is created, making it slightly more random. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@10144 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/engine/test-core/test-engine-stuff.c | 79 ++++++++++++++---------- src/engine/test-core/test-engine-stuff.h | 8 ++- src/engine/test/test-split-vs-account.c | 6 +- 3 files changed, 53 insertions(+), 40 deletions(-) diff --git a/src/engine/test-core/test-engine-stuff.c b/src/engine/test-core/test-engine-stuff.c index d6fb3b4a17..311f1aa3b9 100644 --- a/src/engine/test-core/test-engine-stuff.c +++ b/src/engine/test-core/test-engine-stuff.c @@ -1,3 +1,12 @@ +/** + * @file test-engine-stuff.c + * @brief tools to set up random, but finanically consistent books. + * Create transactions with random values, random accounts, random + * account heirarchies, etc. + * + * Created by Linux Developers Group, 2001 + * Updates Linas Vepstas July 2004 + */ #include "config.h" #include @@ -35,7 +44,7 @@ static gint total_num_accounts = 0; static kvp_value* get_random_kvp_value_depth (int type, gint depth); static gpointer get_random_list_element (GList *list); -static void add_random_splits(QofBook *book, Transaction *trn); +static void add_random_splits(QofBook *book, Transaction *trn, GList *account_list); gboolean gnc_engine_debug_random = FALSE; @@ -171,7 +180,8 @@ get_random_gnc_numeric(void) /* Arbitrary random numbers can cause pointless overflow * during calculations. Limit dynamic range in hopes * of avoiding overflow. */ - numer = get_random_gint64()/100; + numer = get_random_gint64()/100000; + if (0 == numer) numer = 1; return gnc_numeric_create(numer, deno); } @@ -669,7 +679,7 @@ make_random_changes_to_transaction_and_splits (QofBook *book, xaccSplitDestroy (split); } while (split); - add_random_splits (book, trans); + add_random_splits (book, trans, accounts); /* fall through */ @@ -929,10 +939,10 @@ set_split_random_string(Split *spl, static char possible_chars[] = { NREC, CREC, YREC, FREC }; Split* -get_random_split(QofBook *book, gnc_numeric num) +get_random_split(QofBook *book, Account *acct) { Split *ret; - gnc_numeric oneVal; + gnc_numeric num; ret = xaccMallocSplit(book); @@ -943,12 +953,11 @@ get_random_split(QofBook *book, gnc_numeric num) xaccSplitSetDateReconciledTS(ret, get_random_timespec()); - xaccSplitSetValue(ret, num); + /* Split must be in an account before we can set an amount */ + xaccAccountInsertSplit (acct, ret); + num = get_random_gnc_numeric (); xaccSplitSetAmount(ret, num); - oneVal = gnc_numeric_create(1,1); - xaccSplitSetSharePrice(ret, oneVal); - xaccSplitSetSlots_nc(ret, get_random_kvp_frame()); return ret; @@ -994,12 +1003,26 @@ set_tran_random_string(Transaction* trn, } static void -add_random_splits(QofBook *book, Transaction *trn) +add_random_splits(QofBook *book, Transaction *trn, GList *account_list) { - gnc_numeric num = get_random_gnc_numeric(); + Account *account; + Split *s; - xaccTransAppendSplit(trn, get_random_split(book, num)); - xaccTransAppendSplit(trn, get_random_split(book, gnc_numeric_neg(num))); + /* Set up two splits whose values really are opposites. */ + gnc_commodity *com = xaccTransGetCurrency (trn); + int scu = gnc_commodity_get_fraction(com); + gnc_numeric num = get_random_gnc_numeric(); + num = gnc_numeric_convert (num, scu, GNC_HOW_RND_ROUND); + + account = get_random_list_element (account_list); + s = get_random_split(book, account); + xaccTransAppendSplit(trn, s); + xaccSplitSetValue(s, num); + + account = get_random_list_element (account_list); + s = get_random_split(book, account); + xaccTransAppendSplit(trn, s); + xaccSplitSetValue(s, gnc_numeric_neg(num)); } static void @@ -1016,10 +1039,16 @@ trn_add_ran_timespec(Transaction *trn, void (*func)(Transaction*, Transaction * get_random_transaction_with_currency(QofBook *book, - gnc_commodity *currency) + gnc_commodity *currency, + GList *account_list) { Transaction* ret; + if (!account_list) + { + account_list = xaccGroupGetSubAccounts (gnc_book_get_group (book)); + } + ret = xaccMallocTransaction(book); xaccTransBeginEdit(ret); @@ -1034,7 +1063,7 @@ get_random_transaction_with_currency(QofBook *book, xaccTransSetSlots_nc(ret, get_random_kvp_frame()); - add_random_splits(book, ret); + add_random_splits(book, ret, account_list); if (get_random_int_in_range (1, 10) == 1) { @@ -1050,7 +1079,7 @@ get_random_transaction_with_currency(QofBook *book, Transaction* get_random_transaction (QofBook *book) { - return get_random_transaction_with_currency (book, NULL); + return get_random_transaction_with_currency (book, NULL, NULL); } void @@ -1163,7 +1192,7 @@ get_random_commodity (QofBook *book) xcode = get_random_string(); /* SCU == smallest currency unit -- the value of the denominator */ -#define MAX_SCU 106000 +#define MAX_SCU 6000 ran_int = get_random_int_in_range(1, MAX_SCU); ret = gnc_commodity_new (name, space, mn, xcode, ran_int); @@ -1615,23 +1644,9 @@ add_random_transactions_to_book (QofBook *book, gint num_transactions) { gnc_commodity *com; Transaction *trans; - Account *account; - Split *split; com = get_random_commodity_from_table (table); - trans = get_random_transaction_with_currency (book, com); - - xaccTransBeginEdit (trans); - - split = xaccTransGetSplit (trans, 0); - account = get_random_list_element (accounts); - xaccAccountInsertSplit (account, split); - - split = xaccTransGetSplit (trans, 1); - account = get_random_list_element (accounts); - xaccAccountInsertSplit (account, split); - - xaccTransCommitEdit (trans); + trans = get_random_transaction_with_currency (book, com, accounts); } g_list_free (accounts); } diff --git a/src/engine/test-core/test-engine-stuff.h b/src/engine/test-core/test-engine-stuff.h index c46406c18d..7bc3b0ba9a 100644 --- a/src/engine/test-core/test-engine-stuff.h +++ b/src/engine/test-core/test-engine-stuff.h @@ -1,4 +1,5 @@ -/* This file declares testing functions for the engine. +/** @file test-engine-stuff.h + * $brief This file declares testing functions for the engine. */ #ifndef TEST_ENGINE_STUFF_H @@ -46,10 +47,11 @@ void make_random_pricedb (QofBook *book, GNCPriceDB *pdb); GNCPriceDB * get_random_pricedb(QofBook *book); AccountGroup * get_random_group(QofBook * book); Account* get_random_account(QofBook * book); -Split* get_random_split(QofBook *book, gnc_numeric num); +Split* get_random_split(QofBook *book, Account *account); Transaction* get_random_transaction(QofBook *book); Transaction* get_random_transaction_with_currency(QofBook *book, - gnc_commodity *currency); + gnc_commodity *currency, + GList *account_list); gnc_commodity* get_random_commodity(QofBook *book); const char *get_random_commodity_namespace(void); diff --git a/src/engine/test/test-split-vs-account.c b/src/engine/test/test-split-vs-account.c index 4230afcba7..15a93ccb54 100644 --- a/src/engine/test/test-split-vs-account.c +++ b/src/engine/test/test-split-vs-account.c @@ -19,7 +19,6 @@ run_test (void) Account *act1; Account *act2; Split *spl; - gnc_numeric num; QofSession *session; QofBook *book; @@ -40,16 +39,13 @@ run_test (void) return; } - num = get_random_gnc_numeric(); - spl = get_random_split(book, num); + spl = get_random_split(book, act1); if(!spl) { failure("spl not created"); return; } - xaccAccountInsertSplit(act1, spl); - if(act1 != xaccSplitGetAccount(spl)) { failure("xaccAccountInsertSplit is broken");