test-object.c

00001 /***************************************************************************
00002  *            test-object.c
00003  *
00004  *  Copyright  2004  Linas Vepstas <linas@linas.org>
00005  ****************************************************************************/
00006 /*
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020  */
00021  
00022 /*
00023  * Lightly test the QofObject infrastructure.
00024  */
00025 #include <glib.h>
00026 
00027 #include "qof.h"
00028 
00029 #include "test-stuff.h"
00030 
00031 #define TEST_MODULE_NAME "object-test"
00032 #define TEST_MODULE_DESC "Test Object"
00033 
00034 static void obj_foreach (QofCollection *, QofEntityForeachCB, gpointer);
00035 static const char * printable (gpointer obj);
00036 static void test_printable (const char *name, gpointer obj);
00037 static void test_foreach (QofBook *, const char *);
00038 
00039 static QofObject bus_obj = {
00040   interface_version:  QOF_OBJECT_VERSION,
00041   e_type:             TEST_MODULE_NAME,
00042   type_label:         TEST_MODULE_DESC,
00043   create:             NULL,
00044   book_begin:         NULL,
00045   book_end:           NULL,
00046   is_dirty:           NULL,
00047   mark_clean:         NULL,
00048   foreach:            obj_foreach,
00049   printable:          printable,
00050   version_cmp:        NULL,
00051 };
00052 
00053 static void 
00054 test_object (void)
00055 {
00056   QofBook *book = qof_book_new();
00057 
00058   do_test ((NULL != book), "book null");
00059 
00060   /* Test the global registration and lookup functions */
00061   {
00062     do_test (!qof_object_register (NULL), "register NULL");
00063     do_test (qof_object_register (&bus_obj), "register test object");
00064     do_test (!qof_object_register (&bus_obj), "register test object again");
00065     do_test (qof_object_lookup (TEST_MODULE_NAME) == &bus_obj,
00066              "lookup our installed object");
00067     do_test (qof_object_lookup ("snm98sn snml say  dyikh9y9ha") == NULL,
00068              "lookup non-existant object object");
00069 
00070     do_test (!safe_strcmp (qof_object_get_type_label (TEST_MODULE_NAME),
00071                       (TEST_MODULE_DESC)),
00072              "test description return");
00073   }
00074 
00075   test_foreach (book, TEST_MODULE_NAME);
00076   test_printable (TEST_MODULE_NAME, (gpointer)1);
00077 }
00078 
00079 static void
00080 obj_foreach (QofCollection *col, QofEntityForeachCB cb, gpointer u_d)
00081 {
00082   int *foo = u_d;
00083 
00084   do_test (col != NULL, "foreach: NULL collection");
00085   success ("called foreach callback");
00086 
00087   *foo = 1;
00088 }
00089 
00090 static void foreachCB (QofEntity *ent, gpointer u_d)
00091 {
00092   do_test (FALSE, "FAIL");
00093 }
00094 
00095 static const char *
00096 printable (gpointer obj)
00097 {
00098   do_test (obj != NULL, "printable: object is NULL");
00099   success ("called printable callback");
00100   return ((const char *)obj);
00101 }
00102 
00103 static void
00104 test_foreach (QofBook *book, const char *name)
00105 {
00106   int res = 0;
00107 
00108   qof_object_foreach (NULL, NULL, NULL, &res);
00109   do_test (res == 0, "object: Foreach: NULL, NULL, NULL");
00110   qof_object_foreach (NULL, NULL, foreachCB, &res);
00111   do_test (res == 0, "object: Foreach: NULL, NULL, foreachCB");
00112 
00113   qof_object_foreach (NULL, book, NULL, &res);
00114   do_test (res == 0, "object: Foreach: NULL, book, NULL");
00115   qof_object_foreach (NULL, book, foreachCB, &res);
00116   do_test (res == 0, "object: Foreach: NULL, book, foreachCB");
00117 
00118   qof_object_foreach (name, NULL, NULL, &res);
00119   do_test (res == 0, "object: Foreach: name, NULL, NULL");
00120   qof_object_foreach (name, NULL, foreachCB, &res);
00121   do_test (res == 0, "object: Foreach: name, NULL, foreachCB");
00122 
00123   qof_object_foreach (name, book, NULL, &res);
00124   do_test (res != 0, "object: Foreach: name, book, NULL");
00125 
00126   res = 0;
00127   qof_object_foreach (name, book, foreachCB, &res);
00128   do_test (res != 0, "object: Foreach: name, book, foreachCB");
00129 }
00130 
00131 static void
00132 test_printable (const char *name, gpointer obj)
00133 {
00134   const char *res;
00135 
00136   do_test (qof_object_printable (NULL, NULL) == NULL,
00137            "object: Printable: NULL, NULL");
00138   do_test (qof_object_printable (NULL, obj) == NULL,
00139            "object: Printable: NULL, object");
00140   do_test (qof_object_printable (name, NULL) == NULL,
00141            "object: Printable: mod_name, NULL");
00142   res = qof_object_printable (name, obj);
00143   do_test (res != NULL, "object: Printable: mod_name, object");
00144 }
00145 
00146 int
00147 main (int argc, char **argv)
00148 {
00149         gnc_engine_get_string_cache ();
00150         guid_init ();
00151         qof_object_initialize ();
00152         qof_book_register ();
00153         test_object();
00154         print_test_results();
00155         exit(get_rv());
00156         qof_object_shutdown ();
00157         guid_shutdown();
00158         gnc_engine_string_cache_destroy();
00159         return 0;
00160 }

Generated on Fri May 12 17:57:21 2006 for QOF by  doxygen 1.4.4