From fed69ff72c0ee0225d369196aa4cd7ced36669a2 Mon Sep 17 00:00:00 2001 From: Dave Peticolas Date: Sun, 25 Nov 2001 09:05:23 +0000 Subject: [PATCH] Fix bugs with new book pointers. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@6053 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/backend/file/gnc-pricedb-xml-v2.c | 4 +- src/backend/file/io-gncbin-r.c | 2 +- src/backend/file/io-gncxml-v1.c | 5 ++- src/backend/file/io-gncxml-v2.c | 3 +- src/backend/postgres/price.c | 2 +- src/engine/Group.c | 5 +-- src/engine/gnc-book.c | 3 +- src/engine/gnc-pricedb-p.h | 4 +- src/engine/gnc-pricedb.c | 10 +++-- src/engine/gnc-pricedb.h | 2 +- src/engine/test-core/test-engine-stuff.c | 50 +++++++++++++----------- src/engine/test/test-group-vs-book.c | 13 +++--- src/engine/test/test-scm-query.c | 2 +- 13 files changed, 59 insertions(+), 46 deletions(-) diff --git a/src/backend/file/gnc-pricedb-xml-v2.c b/src/backend/file/gnc-pricedb-xml-v2.c index dbd4f6a788..bf07963a7f 100644 --- a/src/backend/file/gnc-pricedb-xml-v2.c +++ b/src/backend/file/gnc-pricedb-xml-v2.c @@ -230,7 +230,9 @@ pricedb_start_handler(GSList* sibling_data, const gchar *tag, gchar **attrs) { - GNCPriceDB *db = gnc_pricedb_create(); + gxpf_data *gdata = global_data; + GNCBook *book = gdata->bookdata; + GNCPriceDB *db = gnc_pricedb_create(book); g_return_val_if_fail(db, FALSE); *result = db; return(TRUE); diff --git a/src/backend/file/io-gncbin-r.c b/src/backend/file/io-gncbin-r.c index 95abcfa3be..979ce0224b 100644 --- a/src/backend/file/io-gncbin-r.c +++ b/src/backend/file/io-gncbin-r.c @@ -202,7 +202,7 @@ cvt_potential_prices_to_pricedb_and_cleanup(GNCPriceDB **prices, { GSList *item = potential_quotes; - *prices = gnc_pricedb_create(); + *prices = gnc_pricedb_create(book); if (!*prices) return FALSE; while(item) diff --git a/src/backend/file/io-gncxml-v1.c b/src/backend/file/io-gncxml-v1.c index f684ad66fa..acb045131f 100644 --- a/src/backend/file/io-gncxml-v1.c +++ b/src/backend/file/io-gncxml-v1.c @@ -404,7 +404,7 @@ gnc_session_load_from_xml_file(GNCSession *session) { GNCPriceDB *db = gnc_book_get_pricedb(book); - gnc_book_set_pricedb(book, gnc_pricedb_create()); + gnc_book_set_pricedb(book, gnc_pricedb_create(book)); if(db) gnc_pricedb_destroy(db); } @@ -3711,7 +3711,8 @@ pricedb_start_handler(GSList* sibling_data, const gchar *tag, gchar **attrs) { - GNCPriceDB *db = gnc_pricedb_create(); + GNCParseStatus *pstatus = (GNCParseStatus *) global_data; + GNCPriceDB *db = gnc_pricedb_create(pstatus->book); g_return_val_if_fail(db, FALSE); *result = db; return(TRUE); diff --git a/src/backend/file/io-gncxml-v2.c b/src/backend/file/io-gncxml-v2.c index 37d500e10c..d9a11dafe0 100644 --- a/src/backend/file/io-gncxml-v2.c +++ b/src/backend/file/io-gncxml-v2.c @@ -46,7 +46,6 @@ #include "io-utils.h" - #define GNC_V2_STRING "gnc-v2" static void @@ -533,7 +532,7 @@ gnc_session_load_from_xml_file_v2( * then the file had no pricedb section. However, * this routine is expected to put one in the book. */ if (!gnc_book_get_pricedb (book)) - gnc_book_set_pricedb (book, gnc_pricedb_create ()); + gnc_book_set_pricedb (book, gnc_pricedb_create (book)); /* mark the newly read group as saved, since the act of putting * it together will have caused it to be marked up as not-saved. diff --git a/src/backend/postgres/price.c b/src/backend/postgres/price.c index b9bc304bfa..c8296b2ba0 100644 --- a/src/backend/postgres/price.c +++ b/src/backend/postgres/price.c @@ -300,7 +300,7 @@ pgendGetAllPrices (PGBackend *be, GNCPriceDB *prdb) ENTER ("be=%p, conn=%p", be, be->connection); if (!prdb) { - prdb = gnc_pricedb_create(); + prdb = gnc_pricedb_create(be->book); } /* first, make sure commodities table is up to date */ diff --git a/src/engine/Group.c b/src/engine/Group.c index 0deda79e1e..301365ac69 100644 --- a/src/engine/Group.c +++ b/src/engine/Group.c @@ -662,10 +662,7 @@ xaccAccountInsertSubAccount (Account *adult, Account *child) /* if a container for the children doesn't yet exist, add it */ if (adult->children == NULL) - { - GNCBook *book = xaccGroupGetBook (adult->parent); - adult->children = xaccMallocAccountGroup (book); - } + adult->children = xaccMallocAccountGroup (adult->book); /* set back-pointer to parent */ adult->children->parent = adult; diff --git a/src/engine/gnc-book.c b/src/engine/gnc-book.c index 11135149be..c4b5ac2b87 100644 --- a/src/engine/gnc-book.c +++ b/src/engine/gnc-book.c @@ -78,7 +78,7 @@ gnc_book_init (GNCBook *book) book->kvp_data = kvp_frame_new (); book->topgroup = xaccMallocAccountGroup(book); - book->pricedb = gnc_pricedb_create(); + book->pricedb = gnc_pricedb_create(book); book->sched_xactions = NULL; book->sx_notsaved = FALSE; @@ -238,6 +238,7 @@ gnc_book_set_pricedb(GNCBook *book, GNCPriceDB *db) { if(!book) return; book->pricedb = db; + if (db) db->book = book; } /* ---------------------------------------------------------------------- */ diff --git a/src/engine/gnc-pricedb-p.h b/src/engine/gnc-pricedb-p.h index 74bc04b2b9..0012994fee 100644 --- a/src/engine/gnc-pricedb-p.h +++ b/src/engine/gnc-pricedb-p.h @@ -35,8 +35,6 @@ struct gnc_price_s { /* 'public' data fields */ GUID guid; /* globally unique price id */ - GNCEntityTable *entity_table; /* table in which price is stored */ - GNCBook *book; /* book to which this price belongs to */ GNCPriceDB *db; gnc_commodity *commodity; @@ -49,6 +47,8 @@ struct gnc_price_s guint32 version_check; /* data aging timestamp */ /* 'private' object management fields */ + GNCBook *book; /* book to which this price belongs to */ + GNCEntityTable *entity_table; /* table in which price is stored */ guint32 refcount; /* garbage collection reference count */ gint32 editlevel; /* nesting level of begin/end edit calls */ gboolean not_saved; /* price edit saved flag */ diff --git a/src/engine/gnc-pricedb.c b/src/engine/gnc-pricedb.c index 7c076fc6b6..cce3a73e5a 100644 --- a/src/engine/gnc-pricedb.c +++ b/src/engine/gnc-pricedb.c @@ -612,10 +612,14 @@ commodity_equal (gconstpointer a, gconstpointer b) } GNCPriceDB * -gnc_pricedb_create(void) +gnc_pricedb_create(GNCBook * book) { - GNCPriceDB * result = g_new0(GNCPriceDB, 1); - result->book = NULL; + GNCPriceDB * result; + + g_return_val_if_fail (book, NULL); + + result = g_new0(GNCPriceDB, 1); + result->book = book; result->commodity_hash = g_hash_table_new(commodity_hash, commodity_equal); g_return_val_if_fail (result->commodity_hash, NULL); return result; diff --git a/src/engine/gnc-pricedb.h b/src/engine/gnc-pricedb.h index b5b8c1f0d1..019424f0b5 100644 --- a/src/engine/gnc-pricedb.h +++ b/src/engine/gnc-pricedb.h @@ -219,7 +219,7 @@ typedef struct gnc_price_db_s GNCPriceDB; /* gnc_pricedb_create - create a new pricedb. Normally you won't need this; you will get the pricedb via gnc_book_get_pricedb. */ -GNCPriceDB * gnc_pricedb_create(void); +GNCPriceDB * gnc_pricedb_create(GNCBook *book); /* gnc_pricedb_destroy - destroy the given pricedb and unref all of the prices it contains. This may not deallocate all of those diff --git a/src/engine/test-core/test-engine-stuff.c b/src/engine/test-core/test-engine-stuff.c index 6de1f95c43..9fbbf1cca7 100644 --- a/src/engine/test-core/test-engine-stuff.c +++ b/src/engine/test-core/test-engine-stuff.c @@ -12,7 +12,6 @@ #include "date.h" #include "Group.h" -#include "gnc-book-p.h" #include "gnc-engine.h" #include "gnc-engine-util.h" #include "test-engine-stuff.h" @@ -226,7 +225,7 @@ get_random_pricedb(GNCBook *book) { GNCPriceDB *db; - db = gnc_pricedb_create (); + db = gnc_pricedb_create (book); make_random_pricedb (book, db); return db; @@ -546,16 +545,16 @@ account_add_subaccounts (GNCBook *book, Account *account, int depth) } } -static AccountGroup * -get_random_group_depth(GNCBook *book, int depth) +static void +make_random_group_depth (GNCBook *book, AccountGroup *group, int depth) { - AccountGroup *group; int num_accounts; - if (depth <= 0) - return NULL; + g_return_if_fail (book); + g_return_if_fail (group); - group = xaccMallocAccountGroup (book); + if (depth <= 0) + return; num_accounts = get_random_int_in_range (1, max_group_accounts); @@ -567,18 +566,33 @@ get_random_group_depth(GNCBook *book, int depth) account_add_subaccounts (book, account, depth - 1); } +} - return group; +static void +make_random_group (GNCBook *book, AccountGroup * group) +{ + int depth; + + g_return_if_fail (book); + g_return_if_fail (group); + + depth = get_random_int_in_range (1, max_group_depth); + + make_random_group_depth (book, group, depth); } AccountGroup * get_random_group (GNCBook *book) { - int depth; + AccountGroup * group; - depth = get_random_int_in_range (1, max_group_depth); + g_return_val_if_fail (book, NULL); - return get_random_group_depth (book, depth); + group = xaccMallocAccountGroup (book); + + make_random_group (book, group); + + return group; } typedef struct @@ -1443,11 +1457,7 @@ get_random_book (void) book = gnc_book_new (); - /* XXX fixme -- gnc_book_set_group is a private engine function, - * it should not be invoked in ordinary test cases. Its should - * be more like make_random_pricedb below... */ - gnc_book_set_group (book, get_random_group (book)); - + make_random_group (book, gnc_book_get_group (book)); make_random_pricedb (book, gnc_book_get_pricedb (book)); return book; @@ -1463,11 +1473,7 @@ get_random_session (void) book = gnc_session_get_book (session); - /* XXX fixme -- gnc_book_set_group is a private engine function, - * it should not be invoked in ordinary test cases. Its should - * be more like make_random_pricedb below... */ - gnc_book_set_group (book, get_random_group (book)); - + make_random_group (book, gnc_book_get_group (book)); make_random_pricedb (book, gnc_book_get_pricedb (book)); return session; diff --git a/src/engine/test/test-group-vs-book.c b/src/engine/test/test-group-vs-book.c index b47de8b4c3..4b34b56993 100644 --- a/src/engine/test/test-group-vs-book.c +++ b/src/engine/test/test-group-vs-book.c @@ -62,15 +62,18 @@ run_test (void) exit(get_rv()); } - if (!group_has_book (group1, NULL)) + if (!group_has_book (group1, book)) { - failure("new group has non-null book"); + failure("new group has wrong book"); exit(get_rv()); } - /* ???????? this test seems to be testing routines that - * are private to the engine, not sure why. book_set_group - * should not be called publically. */ + /* this test is testing routines that are private + * to the engine. these tests are intended to test + * the engine as a whole, not just the public + * interface. the maintenance of the correct + * book pointers is important for correct + * engine operation. */ gnc_book_set_group (book, group1); if (!group_has_book (group1, book)) { diff --git a/src/engine/test/test-scm-query.c b/src/engine/test/test-scm-query.c index 264a720515..a48f104031 100644 --- a/src/engine/test/test-scm-query.c +++ b/src/engine/test/test-scm-query.c @@ -48,7 +48,7 @@ run_tests (void) test_query (q); xaccFreeQuery (q); - for (i = 0; i < 10; i++) + for (i = 0; i < 50; i++) { q = get_random_query (); test_query (q);