[Gnucash-changes] re-order the way that random data is created, making it slightly more

Linas Vepstas linas at cvs.gnucash.org
Sat Jul 3 11:29:41 EDT 2004


Log Message:
-----------
re-order the way that random data is created,
making it slightly more random.

Modified Files:
--------------
    gnucash/src/engine/test:
        test-split-vs-account.c
    gnucash/src/engine/test-core:
        test-engine-stuff.c
        test-engine-stuff.h

Revision Data
-------------
Index: test-split-vs-account.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/test/test-split-vs-account.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -Lsrc/engine/test/test-split-vs-account.c -Lsrc/engine/test/test-split-vs-account.c -u -r1.11 -r1.12
--- src/engine/test/test-split-vs-account.c
+++ src/engine/test/test-split-vs-account.c
@@ -19,7 +19,6 @@
     Account *act1;
     Account *act2;
     Split *spl;
-    gnc_numeric num;
     QofSession *session;
     QofBook *book;
 
@@ -40,16 +39,13 @@
         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");
Index: test-engine-stuff.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/test-core/test-engine-stuff.c,v
retrieving revision 1.69
retrieving revision 1.70
diff -Lsrc/engine/test-core/test-engine-stuff.c -Lsrc/engine/test-core/test-engine-stuff.c -u -r1.69 -r1.70
--- src/engine/test-core/test-engine-stuff.c
+++ 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 <sys/types.h>
@@ -35,7 +44,7 @@
 
 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 @@
     /* 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 @@
         xaccSplitDestroy (split);
       } while (split);
 
-      add_random_splits (book, trans);
+      add_random_splits (book, trans, accounts);
 
       /* fall through */
 
@@ -929,10 +939,10 @@
 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 @@
 
     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 @@
 }
 
 static void
-add_random_splits(QofBook *book, Transaction *trn)
+add_random_splits(QofBook *book, Transaction *trn, GList *account_list)
 {
+    Account *account;
+    Split *s;
+
+    /* 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);
 
-    xaccTransAppendSplit(trn, get_random_split(book, num));
-    xaccTransAppendSplit(trn, get_random_split(book, gnc_numeric_neg(num)));
+    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 @@
     
 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 @@
 
     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 @@
 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 @@
     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 @@
   {
     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);
 }
Index: test-engine-stuff.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/test-core/test-engine-stuff.h,v
retrieving revision 1.26
retrieving revision 1.27
diff -Lsrc/engine/test-core/test-engine-stuff.h -Lsrc/engine/test-core/test-engine-stuff.h -u -r1.26 -r1.27
--- src/engine/test-core/test-engine-stuff.h
+++ 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 @@
 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);
 


More information about the gnucash-changes mailing list