[Gnucash-changes] r13307 - gnucash/trunk/src/engine/test - updated
test routine for new recursive code in libqof
Neil Williams
codehelp at cvs.gnucash.org
Sun Feb 19 17:55:11 EST 2006
Author: codehelp
Date: 2006-02-19 17:55:11 -0500 (Sun, 19 Feb 2006)
New Revision: 13307
Trac: http://svn.gnucash.org/trac/changeset/13307
Modified:
gnucash/trunk/src/engine/test/test-recursive.c
Log:
updated test routine for new recursive code in libqof
Modified: gnucash/trunk/src/engine/test/test-recursive.c
===================================================================
--- gnucash/trunk/src/engine/test/test-recursive.c 2006-02-19 22:54:39 UTC (rev 13306)
+++ gnucash/trunk/src/engine/test/test-recursive.c 2006-02-19 22:55:11 UTC (rev 13307)
@@ -18,7 +18,8 @@
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor
+ * Boston, MA 02110-1301, USA
*/
#include <glib.h>
@@ -38,17 +39,17 @@
#define OBJ_NAME "somename"
#define OBJ_AMOUNT "anamount"
#define OBJ_DATE "nottoday"
-#define OBJ_GUID "unique"
#define OBJ_DISCOUNT "hefty"
#define OBJ_VERSION "early"
#define OBJ_MINOR "tiny"
#define OBJ_ACTIVE "ofcourse"
#define OBJ_FLAG "tiny_flag"
#define OBJ_RELATIVE "family"
+#define OBJ_LIST "descendents"
/* set to TRUE to get QSF XML output
* requires QSF available (i.e. make install) */
-static gboolean debug = FALSE;
+static gboolean debug = FALSE;
/* simple object structure */
typedef struct child_s
@@ -57,7 +58,6 @@
gchar *Name;
gchar flag;
gnc_numeric Amount;
- const GUID *child_guid;
Timespec date;
double discount; /* cheap pun, I know. */
gboolean active;
@@ -73,7 +73,6 @@
gchar *Name;
gchar flag;
gnc_numeric Amount;
- const GUID *parent_guid;
Timespec date;
double discount; /* cheap pun, I know. */
gboolean active;
@@ -86,10 +85,10 @@
{
QofInstance inst;
myparent *child;
+ GList *descend;
gchar *Name;
gchar flag;
gnc_numeric Amount;
- const GUID *grand_guid;
Timespec date;
double discount; /* cheap pun, I know. */
gboolean active;
@@ -107,7 +106,6 @@
/* obvious setter functions */
void grand_setName(mygrand*, gchar*);
-void grand_setGUID(mygrand*, const GUID*);
void grand_setAmount(mygrand*, gnc_numeric);
void grand_setDate(mygrand*, Timespec h);
void grand_setDiscount(mygrand*, double);
@@ -118,7 +116,6 @@
/* obvious getter functions */
gchar* grand_getName(mygrand*);
-const GUID* grand_getGUID(mygrand*);
gnc_numeric grand_getAmount(mygrand*);
Timespec grand_getDate(mygrand*);
double grand_getDiscount(mygrand*);
@@ -129,7 +126,6 @@
/* obvious setter functions */
void parent_setName(myparent*, gchar*);
-void parent_setGUID(myparent*, const GUID*);
void parent_setAmount(myparent*, gnc_numeric);
void parent_setDate(myparent*, Timespec h);
void parent_setDiscount(myparent*, double);
@@ -140,7 +136,6 @@
/* obvious getter functions */
gchar* parent_getName(myparent*);
-const GUID* parent_getGUID(myparent*);
gnc_numeric parent_getAmount(myparent*);
Timespec parent_getDate(myparent*);
double parent_getDiscount(myparent*);
@@ -151,7 +146,6 @@
/* obvious setter functions */
void child_setName(mychild*, gchar*);
-void child_setGUID(mychild*, const GUID*);
void child_setAmount(mychild*, gnc_numeric);
void child_setDate(mychild*, Timespec h);
void child_setDiscount(mychild*, double);
@@ -162,7 +156,6 @@
/* obvious getter functions */
gchar* child_getName(mychild*);
-const GUID* child_getGUID(mychild*);
gnc_numeric child_getAmount(mychild*);
Timespec child_getDate(mychild*);
double child_getDiscount(mychild*);
@@ -179,16 +172,16 @@
g_return_val_if_fail(book, NULL);
g = g_new0(mygrand, 1);
qof_instance_init (&g->inst, GRAND_MODULE_NAME, book);
- g->grand_guid = get_random_guid();
g->date = *get_random_timespec();
- g->discount = get_random_double();
+ g->discount = get_random_double();;
g->active = get_random_boolean();
g->version = get_random_int_in_range(1,10000);
- g->minor = get_random_int_in_range(100000,99999999);
+ g->minor = get_random_int_in_range(100001,99999999);
g->flag = get_random_character();
g->Name = get_random_string();
g->Amount = get_random_gnc_numeric();
g->child = NULL;
+ g->descend = NULL;
gnc_engine_gen_event(&g->inst.entity, GNC_EVENT_CREATE);
return g;
}
@@ -201,12 +194,11 @@
g_return_val_if_fail(book, NULL);
g = g_new0(myparent, 1);
qof_instance_init (&g->inst, PARENT_MODULE_NAME, book);
- g->parent_guid = get_random_guid();
g->date = *get_random_timespec();
g->discount = get_random_double();
g->active = get_random_boolean();
g->version = get_random_int_in_range(1,10000);
- g->minor = get_random_int_in_range(100000,99999999);
+ g->minor = get_random_int_in_range(100001,99999999);
g->flag = get_random_character();
g->Name = get_random_string();
g->Amount = get_random_gnc_numeric();
@@ -223,12 +215,11 @@
g_return_val_if_fail(book, NULL);
g = g_new0(mychild, 1);
qof_instance_init (&g->inst, CHILD_MODULE_NAME, book);
- g->child_guid = get_random_guid();
g->date = *get_random_timespec();
g->discount = get_random_double();
g->active = get_random_boolean();
g->version = get_random_int_in_range(1,10000);
- g->minor = get_random_int_in_range(100000,99999999);
+ g->minor = get_random_int_in_range(100001,99999999);
g->flag = get_random_character();
g->Name = get_random_string();
g->Amount = get_random_gnc_numeric();
@@ -237,6 +228,45 @@
}
static void
+descend_cb (QofEntity *ent, gpointer user_data)
+{
+ mygrand *g = (mygrand*)user_data;
+
+ g_return_if_fail(g || ent);
+ g->descend = g_list_prepend(g->descend, (mychild*)ent);
+}
+
+static void
+grand_setDescend(mygrand *g, QofCollection *coll)
+{
+ g_return_if_fail(g || coll);
+ if(0 != safe_strcmp(qof_collection_get_type(coll), CHILD_MODULE_NAME))
+ {
+ return;
+ }
+ qof_collection_foreach(coll, descend_cb, g);
+}
+
+static QofCollection*
+grand_getDescend(mygrand *g)
+{
+ QofCollection *col;
+ QofEntity *ent;
+ GList *list;
+
+ g_return_val_if_fail(g, NULL);
+ col = qof_collection_new(CHILD_MODULE_NAME);
+ for(list = g_list_copy(g->descend);list;list=list->next)
+ {
+ ent = (QofEntity*)list->data;
+ if(!ent) { break; }
+ do_test(0 == safe_strcmp(ent->e_type, CHILD_MODULE_NAME), "wrong entity");
+ qof_collection_add_entity(col, ent);
+ }
+ return col;
+}
+
+static void
grand_setChild(mygrand *g, myparent *p)
{
g_return_if_fail(g || p);
@@ -339,20 +369,6 @@
}
void
-grand_setGUID(mygrand* g, const GUID* h)
-{
- if(!g) return;
- g->grand_guid = h;
-}
-
-const GUID*
-grand_getGUID(mygrand *g)
-{
- if(!g) return NULL;
- return g->grand_guid;
-}
-
-void
grand_setName(mygrand* g, gchar* h)
{
if(!g || !h) return;
@@ -483,20 +499,6 @@
}
void
-parent_setGUID(myparent* p, const GUID* h)
-{
- if(!p) return;
- p->parent_guid = h;
-}
-
-const GUID*
-parent_getGUID(myparent *p)
-{
- if(!p) return NULL;
- return p->parent_guid;
-}
-
-void
parent_setName(myparent* p, gchar* h)
{
if(!p || !h) return;
@@ -613,20 +615,6 @@
}
void
-child_setGUID(mychild* c, const GUID* h)
-{
- if(!c) return;
- c->child_guid = h;
-}
-
-const GUID*
-child_getGUID(mychild *c)
-{
- if(!c) return NULL;
- return c->child_guid;
-}
-
-void
child_setName(mychild* c, gchar* h)
{
if(!c || !h) return;
@@ -661,8 +649,8 @@
create: (gpointer)grand_create,
book_begin: NULL,
book_end: NULL,
- is_dirty: NULL,
- mark_clean: NULL,
+ is_dirty: qof_collection_is_dirty,
+ mark_clean: qof_collection_mark_clean,
foreach: qof_collection_foreach,
printable: NULL,
version_cmp: (int (*)(gpointer,gpointer)) qof_instance_version_cmp,
@@ -675,8 +663,6 @@
(QofSetterFunc)grand_setName },
{ OBJ_AMOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc)grand_getAmount,
(QofSetterFunc)grand_setAmount },
-/* { OBJ_GUID, QOF_TYPE_GUID, (QofAccessFunc)grand_getGUID,
- (QofSetterFunc)grand_setGUID },*/
{ OBJ_DATE, QOF_TYPE_DATE, (QofAccessFunc)grand_getDate,
(QofSetterFunc)grand_setDate },
{ OBJ_DISCOUNT, QOF_TYPE_DOUBLE, (QofAccessFunc)grand_getDiscount,
@@ -691,12 +677,15 @@
(QofSetterFunc)grand_setFlag },
{ OBJ_RELATIVE, PARENT_MODULE_NAME, (QofAccessFunc)grand_getChild,
(QofSetterFunc)grand_setChild },
+ { OBJ_LIST, QOF_TYPE_COLLECT, (QofAccessFunc)grand_getDescend,
+ (QofSetterFunc)grand_setDescend },
{ QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL },
{ QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL },
{ NULL },
};
qof_class_register (GRAND_MODULE_NAME, NULL, params);
+/* if(!qof_choice_create(GRAND_MODULE_NAME)) { return FALSE; }*/
return qof_object_register (&grand_object_def);
}
@@ -708,8 +697,8 @@
create: (gpointer)parent_create,
book_begin: NULL,
book_end: NULL,
- is_dirty: NULL,
- mark_clean: NULL,
+ is_dirty: qof_collection_is_dirty,
+ mark_clean: qof_collection_mark_clean,
foreach: qof_collection_foreach,
printable: NULL,
version_cmp: (int (*)(gpointer,gpointer)) qof_instance_version_cmp,
@@ -722,8 +711,6 @@
(QofSetterFunc)parent_setName },
{ OBJ_AMOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc)parent_getAmount,
(QofSetterFunc)parent_setAmount },
-/* { OBJ_GUID, QOF_TYPE_GUID, (QofAccessFunc)parent_getGUID,
- (QofSetterFunc)parent_setGUID },*/
{ OBJ_DATE, QOF_TYPE_DATE, (QofAccessFunc)parent_getDate,
(QofSetterFunc)parent_setDate },
{ OBJ_DISCOUNT, QOF_TYPE_DOUBLE, (QofAccessFunc)parent_getDiscount,
@@ -755,8 +742,8 @@
create: (gpointer)child_create,
book_begin: NULL,
book_end: NULL,
- is_dirty: NULL,
- mark_clean: NULL,
+ is_dirty: qof_collection_is_dirty,
+ mark_clean: qof_collection_mark_clean,
foreach: qof_collection_foreach,
printable: NULL,
version_cmp: (int (*)(gpointer,gpointer)) qof_instance_version_cmp,
@@ -769,8 +756,6 @@
(QofSetterFunc)child_setName },
{ OBJ_AMOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc)child_getAmount,
(QofSetterFunc)child_setAmount },
-/* { OBJ_GUID, QOF_TYPE_GUID, (QofAccessFunc)child_getGUID,
- (QofSetterFunc)child_setGUID },*/
{ OBJ_DATE, QOF_TYPE_DATE, (QofAccessFunc)child_getDate,
(QofSetterFunc)child_setDate },
{ OBJ_DISCOUNT, QOF_TYPE_DOUBLE, (QofAccessFunc)child_getDiscount,
@@ -796,6 +781,7 @@
static void
create_data (QofSession *original, guint counter)
{
+ QofCollection *coll;
QofBook *start;
mygrand *grand1;
myparent *parent1;
@@ -803,11 +789,20 @@
start = qof_session_get_book(original);
grand1 = (mygrand*)qof_object_new_instance(GRAND_MODULE_NAME, start);
- do_test ((NULL != &grand1->inst), "#2 instance init");
+ do_test ((NULL != &grand1->inst), "instance init");
switch (counter)
{
case 0 : { /* NULL tree */
do_test((grand1 != NULL), "empty tree check");
+ coll = qof_book_get_collection(start, GRAND_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Too many grandparents found - should be 1");
+ coll = qof_book_get_collection(start, CHILD_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 0),
+ "child found, should be empty");
+ coll = qof_book_get_collection(start, PARENT_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 0),
+ "tree not empty: parent found");
break;
}
case 1 : { /* one parent, no child */
@@ -815,6 +810,15 @@
grand_setChild(grand1, parent1);
do_test((parent1 != NULL), "single parent check");
do_test((grand_getChild(grand1) == parent1), "set child in grandparent");
+ coll = qof_book_get_collection(start, GRAND_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Wrong number of grandparents, should be 1");
+ coll = qof_book_get_collection(start, CHILD_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 0),
+ "Should be no child entities this iteration.");
+ coll = qof_book_get_collection(start, PARENT_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Wrong number of parents found, should be 1");
break;
}
case 2 : { /* one parent, one child */
@@ -825,6 +829,15 @@
parent_setChild(parent1, child1);
do_test((child1 != NULL), "one parent with one related child");
do_test((child1 == parent_getChild(parent1)), "child of single parent");
+ coll = qof_book_get_collection(start, GRAND_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Wrong number of grandparents. Should be 1");
+ coll = qof_book_get_collection(start, CHILD_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Wrong number of child entities, should be 1");
+ coll = qof_book_get_collection(start, PARENT_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Wrong number of parents. Should be 1");
break;
}
case 3 : { /* same grand, new parent, same child */
@@ -834,6 +847,15 @@
parent_setChild(parent1, child1);
do_test((parent1 == grand_getChild(grand1)), "same grandparent, new parent");
do_test((child1 == parent_getChild(parent1)), "new parent, same child");
+ coll = qof_book_get_collection(start, GRAND_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Wrong number of grandparents. Should be 1, Iteration 3.");
+ coll = qof_book_get_collection(start, CHILD_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Wrong number of child entities, should be 1. Iteration 3.");
+ coll = qof_book_get_collection(start, PARENT_MODULE_NAME);
+ do_test((qof_collection_count(coll) == 1),
+ "Wrong number of parents. Should be 1. Iteration 3.");
break;
}
case 4 : { /* new grand, unrelated parent, child unrelated to grand */
@@ -843,79 +865,28 @@
parent_setChild(parent1, child1);
do_test((NULL == grand_getChild(grand1)), "new grand, unrelated parent");
do_test((child1 == parent_getChild(parent1)), "child unrelated to grand");
+ coll = grand_getDescend(grand1);
+ do_test((coll != NULL), "grandparent not valid");
+ if(coll)
+ {
+ QofEntity *ent;
+
+ ent = (QofEntity*)child1;
+ qof_collection_add_entity(coll, ent);
+ grand_setDescend(grand1, coll);
+ qof_collection_destroy(coll);
+ do_test((g_list_length(grand1->descend) > 0), "entity not added");
+ do_test((qof_collection_count(grand_getDescend(grand1)) > 0),
+ "empty collection returned");
+ }
break;
}
}
}
-/** \brief Read QofEntityReference data for this entity and set values.
-
- at param partial_book The partial book containing the referenceList
- at param ent The parent entity to hold the converted reference.
-
-The referenceList is a GList of QofEntityReference structures that contain
-the GUID of each end of a reference. e.g. where one entity refers to another.
-
-The referenceList is used in partial books to store relationships between
-entities when the entities themselves might not exist in the partial book.
-
-If the book is not marked as a partial book, an assertion error is generated.
-
-This routine tries to lookup the given entity in the referenceList for the
-book and then tries to lookup the reference - to find the child entity that
-was originally linked to this parent. The child entity is then set in the
-parent so that it can be located as normal.
-
-If the child entity does not exist in this partial book, the parent entity
-is not updated. The referenceList is unchanged (in case the child is added
-later).
-
-*/
-void
-qof_entity_set_reference_data(QofBook *partial_book, QofEntity *ent);
-
-void
-qof_entity_set_reference_data(QofBook *partial_book, QofEntity *ent)
-{
- void (*reference_setter) (QofEntity*, QofEntity*);
- QofEntityReference *ref;
- GList *book_ref_list;
- QofCollection *coll;
- QofEntity *reference;
- gboolean partial;
-
- g_return_if_fail(partial_book || ent);
- partial =
- (gboolean)GPOINTER_TO_INT(qof_book_get_data(partial_book, PARTIAL_QOFBOOK));
- g_return_if_fail(partial);
- book_ref_list = qof_book_get_data(partial_book, ENTITYREFERENCE);
- while(book_ref_list)
- {
- ref = (QofEntityReference*)book_ref_list->data;
- if(0 != guid_compare(ref->ref_guid, qof_entity_get_guid(ent)))
- {
- book_ref_list = g_list_next(book_ref_list);
- continue;
- }
- coll = qof_book_get_collection(partial_book, ref->type);
- reference = qof_collection_lookup_entity(coll, ref->ref_guid);
- do_test((reference == NULL), "reference is null");
- reference_setter = (void(*)(QofEntity*, QofEntity*))ref->param->param_setfcn;
- if(reference_setter != NULL)
- {
- qof_begin_edit((QofInstance*)ent);
- qof_begin_edit((QofInstance*)reference);
- reference_setter(ent, reference);
- qof_commit_edit((QofInstance*)ent);
- qof_commit_edit((QofInstance*)reference);
- }
- book_ref_list = g_list_next(book_ref_list);
- }
-}
-
struct tally
{
- guint nulls, total;
+ guint nulls, total, collect;
QofBook *book;
};
@@ -923,6 +894,7 @@
check_cb (QofEntity *ent, gpointer data)
{
QofEntity *parent, *child;
+ QofCollection *coll;
struct tally *c;
const QofParam *param;
mygrand *testg;
@@ -930,20 +902,27 @@
mychild *testc;
c = (struct tally*)data;
- /* find the child entity in the copied book */
+ /* check the same number and type of entities
+ exist in the copied book */
testg = (mygrand*)ent;
+ /* we always have a grandparent */
do_test((testg != NULL), "grandparent not found");
c->total++;
- if(c->book) { qof_entity_set_reference_data(c->book, ent); }
- testp = grand_getChild(testg);
+ param = qof_class_get_parameter(GRAND_MODULE_NAME, OBJ_LIST);
+ coll = (QofCollection*)param->param_getfcn(ent, param);
+ c->collect = qof_collection_count(coll);
+ if(c->book) { qof_book_set_references(c->book); }
param = qof_class_get_parameter(GRAND_MODULE_NAME, OBJ_RELATIVE);
- parent = param->param_getfcn(ent, param);
- if(!parent || !testp) { c->nulls++; return; }
+ parent = (QofEntity*)param->param_getfcn(ent, param);
+ testp = grand_getChild((mygrand*)ent);
+ /* not all grandparents have family so just keep count. */
+ if(!parent) { c->nulls++; return; }
do_test((0 == safe_strcmp(parent_getName(testp),
parent_getName((myparent*)parent))), "parent copy test");
param = qof_class_get_parameter(PARENT_MODULE_NAME, OBJ_RELATIVE);
- testc = parent_getChild(testp);
child = param->param_getfcn(parent, param);
+ testc = parent_getChild((myparent*)parent);
+ if(!child) { c->nulls++; return; }
do_test((0 == safe_strcmp(child_getName(testc),
child_getName((mychild*)child))), "child copy test");
}
@@ -955,15 +934,17 @@
QofCollection *grand_coll;
struct tally c;
QofBook *book;
- guint d, e;
+ guint d, e, f;
c.nulls = 0;
c.total = 0;
+ c.collect = 0;
c.book = NULL;
book = qof_session_get_book(original);
grand_coll = qof_book_get_collection(book, GRAND_MODULE_NAME);
copy = qof_session_new();
if(debug) { qof_session_begin(copy, QOF_STDOUT, TRUE, FALSE); }
+ /* TODO: implement QOF_TYPE_CHOICE testing. */
qof_entity_copy_coll_r(copy, grand_coll);
/* test the original */
qof_object_foreach(GRAND_MODULE_NAME, book, check_cb, &c);
@@ -971,13 +952,19 @@
/* test the copy */
d = c.nulls;
e = c.total;
+ f = c.collect;
c.nulls = 0;
c.total = 0;
+ c.collect = 0;
c.book = book;
qof_object_foreach(GRAND_MODULE_NAME, book, check_cb, &c);
-// do_test((d == c.nulls), "Null parents do not match");
+ do_test((d == c.nulls), "Null parents do not match");
do_test((e == c.total), "Total parents do not match");
- if(debug) { qof_session_save(copy, NULL); }
+ do_test((f == c.collect), "Number of children in descendents does not match");
+ if(counter == 4 && debug == TRUE) {
+ qof_session_save(copy, NULL);
+ qof_session_save(original, NULL);
+ }
qof_session_end(copy);
copy = NULL;
}
@@ -992,12 +979,13 @@
mygrandRegister();
myparentRegister();
mychildRegister();
- original = qof_session_new();
- if(debug) { qof_session_begin(original, QOF_STDOUT, TRUE, FALSE); }
- for(counter = 0; counter < 25; counter++)
+ for(counter = 0; counter < 35; counter++)
{
+ original = qof_session_new();
+ if(debug) { qof_session_begin(original, QOF_STDOUT, TRUE, FALSE); }
create_data(original, (counter % 5));
test_recursion(original, (counter % 5));
+ qof_session_end(original);
}
print_test_results();
qof_close();
More information about the gnucash-changes
mailing list