QOF  0.7.5
test-object.c
1 /***************************************************************************
2  * test-object.c
3  *
4  * Copyright 2004 Linas Vepstas <linas@linas.org>
5  * Copyright 2008 Neil Williams <linux@codehelp.co.uk>
6  ****************************************************************************/
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
21  */
22 
23 /*
24  * test the QofObject infrastructure with static and dynamic objects.
25  */
26 #include <glib.h>
27 #include "qof.h"
28 #include "test-stuff.h"
29 
30 #define TEST_MODULE_NAME "object-test"
31 #define TEST_MODULE_DESC "Test Object"
32 #define DYNAMIC_MOD_NAME "dynamic_test"
33 #define DYNAMIC_MOD_DESC "Full test of adding arbitrary objects"
34 
35 static void obj_foreach (QofCollection *, QofEntityForeachCB, gpointer);
36 static const gchar *printable (gpointer obj);
37 static void test_printable (const gchar *name, gpointer obj);
38 static void test_foreach (QofBook *, const gchar *);
39 
40 static QofObject bus_obj = {
41  .interface_version = QOF_OBJECT_VERSION,
42  .e_type = TEST_MODULE_NAME,
43  .type_label = TEST_MODULE_DESC,
44  .create = NULL,
45  .book_begin = NULL,
46  .book_end = NULL,
47  .is_dirty = NULL,
48  .mark_clean = NULL,
49  .foreach = obj_foreach,
50  .printable = printable,
51  .version_cmp = NULL,
52 };
53 
54 static G_GNUC_UNUSED const gchar *
55 test_dyn_printable (gpointer obj)
56 {
57  /* actual dynamic objects can call any function here */
58  return "test";
59 }
60 
61 static QofObject *
62 dyn_create (QofBook * book)
63 {
64  QofInstance * inst;
65  QofCollection *coll;
66  GList *all;
67 
68  g_return_val_if_fail (book, NULL);
69  inst = g_new0 (QofInstance, 1);
70  qof_instance_init (inst, "dynamic_test", book);
71  coll = qof_book_get_collection (book, "dynamic_test");
72  all = qof_collection_get_data (coll);
73  all = g_list_prepend (all, inst);
74  qof_collection_set_data (coll, all);
75  return (QofObject*)inst;
76 }
77 
78 /* pointer to an array of dynamically allocated parameters */
79 static QofParam * p_list;
80 
81 static gchar *
82 dynamic_get_string (QofEntity * ent)
83 {
84  /* actual dynamic objects can call any function here */
85  do_test (!safe_strcmp (DYNAMIC_MOD_NAME, ent->e_type), "e_type check for string");
86  return "test_string";
87 }
88 
89 static gint
90 dynamic_get_int (QofEntity * ent)
91 {
92  /* actual dynamic objects can call any function here */
93  do_test (!safe_strcmp (DYNAMIC_MOD_NAME, ent->e_type), "e_type check for int");
94  return 1;
95 }
96 
97 static gboolean
98 dynamic_get_boolean (QofEntity * ent)
99 {
100  /* actual dynamic objects can call any function here */
101  do_test (!safe_strcmp (DYNAMIC_MOD_NAME, ent->e_type), "e_type check for int");
102  return TRUE;
103 }
104 
105 static QofParam *
106 add_boolean_param (void)
107 {
108  QofParam * p;
109 
110  p = g_new0 (QofParam, 1);
111  p->param_name = "test_boolean";
112  p->param_type = QOF_TYPE_BOOLEAN;
113  p->param_getfcn = (QofAccessFunc)dynamic_get_boolean;
114  return p;
115 }
116 
117 static gboolean
118 test_boolean_param (QofEntity * ent, const QofParam * p)
119 {
120  gboolean b, (*boolean_getter) (QofEntity *, QofParam *);
121  /* actual dynamic objects can call any function here */
122  do_test (!safe_strcmp (DYNAMIC_MOD_NAME, ent->e_type), "e_type check for bool");
123  boolean_getter = (gboolean (*)(QofEntity *, QofParam *)) p->param_getfcn;
124  b = boolean_getter (ent, (QofParam*)p);
125  return b;
126 }
127 
128 static const QofParam *
129 test_class_register (void)
130 {
131  QofParam * p;
132  /* the parameter list needs to be of constant storage size
133  and big enough for all dynamic objects. Registration stops
134  at the first NULL parameter. */
135  static QofParam list[30];
136 
137  p = g_new0 (QofParam, 1);
138  p->param_name = "test_string";
139  p->param_type = QOF_TYPE_STRING;
140  p->param_getfcn = (QofAccessFunc)dynamic_get_string;
141  list[0] = *p;
142  p = g_new0 (QofParam, 1);
143  p->param_name = "test_int";
144  p->param_type = QOF_TYPE_INT32;
145  p->param_getfcn = (QofAccessFunc)dynamic_get_int;
146  list[1] = *p;
147  list[2] = *add_boolean_param ();
148  /* create the terminating NULL */
149  p = g_new0 (QofParam, 1);
150  list[3] = *p;
151  p_list = list;
152  return p_list;
153 }
154 
155 static void
156 test_dynamic_object (void)
157 {
158  QofObject * dynamic;
159  QofInstance * d_ent;
160  const QofObject * check;
161  const gchar * message;
162  gchar * s;
163  gint t, (*int32_getter) (QofEntity *, QofParam *);
164  const QofParam * p;
165  QofBook *book = qof_book_new ();
166 
167  do_test ((NULL != book), "book null");
168  dynamic = g_new0(QofObject,1);
169  dynamic->interface_version = QOF_OBJECT_VERSION,
170  dynamic->e_type = DYNAMIC_MOD_NAME;
171  dynamic->type_label = DYNAMIC_MOD_DESC;
172  dynamic->foreach = obj_foreach;
173  dynamic->create = (gpointer) dyn_create;
174  dynamic->printable = test_dyn_printable;
175  do_test (qof_object_register (dynamic), "dynamic object registration");
176  check = qof_object_lookup (DYNAMIC_MOD_NAME);
177  do_test (check != NULL, "dynamic object lookup");
178  message = qof_object_get_type_label (DYNAMIC_MOD_NAME);
179  do_test (!safe_strcmp(message, "Full test of adding arbitrary objects"),
180  "dynamic object type_label");
181  d_ent = qof_object_new_instance (DYNAMIC_MOD_NAME, book);
182  do_test (check->printable != NULL, "dynamic printable support");
183  message = qof_object_printable (DYNAMIC_MOD_NAME, dynamic);
184  do_test (message != NULL, "dynamic object printable");
185  message = dynamic->printable(d_ent);
186  do_test (message != NULL, "dynamic direct printable");
187  qof_class_register(DYNAMIC_MOD_NAME, NULL, test_class_register());
188  do_test (qof_class_is_registered (DYNAMIC_MOD_NAME), "class register");
189  s = NULL;
190  p = qof_class_get_parameter (DYNAMIC_MOD_NAME, "test_string");
191  s = p->param_getfcn (d_ent, p);
192  do_test (!safe_strcmp(s, "test_string"), "get string from dynamic object");
193  t = 0;
194  p = qof_class_get_parameter (DYNAMIC_MOD_NAME, "test_int");
195  int32_getter = (gint32 (*)(QofEntity *, QofParam *)) p->param_getfcn;
196  t = int32_getter ((QofEntity*)d_ent, (QofParam*)p);
197  do_test (t == 1, "get int from dynamic object");
198  p = qof_class_get_parameter (DYNAMIC_MOD_NAME, "test_boolean");
199  do_test (test_boolean_param((QofEntity*)d_ent, p),
200  "get boolean from dynamic object");
201 }
202 
203 static void
204 test_object (void)
205 {
206  QofBook *book = qof_book_new ();
207 
208  do_test ((NULL != book), "book null");
209 
210  /* Test the global registration and lookup functions */
211  {
212  do_test (!qof_object_register (NULL), "register NULL");
213  do_test (qof_object_register (&bus_obj), "register test object");
214  do_test (!qof_object_register (&bus_obj),
215  "register test object again");
216  do_test (qof_object_lookup (TEST_MODULE_NAME) == &bus_obj,
217  "lookup our installed object");
218  do_test (qof_object_lookup ("snm98sn snml say dyikh9y9ha") == NULL,
219  "lookup non-existant object object");
220 
221  do_test (!safe_strcmp (qof_object_get_type_label (TEST_MODULE_NAME),
222  (TEST_MODULE_DESC)),
223  "test description return");
224  }
225 
226  test_foreach (book, TEST_MODULE_NAME);
227  test_printable (TEST_MODULE_NAME, (gpointer) 1);
228 }
229 
230 static void
231 obj_foreach (QofCollection * col,
232  QofEntityForeachCB cb __attribute__ ((unused)), gpointer u_d)
233 {
234  int *foo = u_d;
235 
236  do_test (col != NULL, "foreach: NULL collection");
237  success ("called foreach callback");
238 
239  *foo = 1;
240 }
241 
242 static void
243 foreachCB (QofEntity * ent __attribute__ ((unused)),
244  gpointer u_d __attribute__ ((unused)))
245 {
246  do_test (FALSE, "FAIL");
247 }
248 
249 static const char *
250 printable (gpointer obj)
251 {
252  do_test (obj != NULL, "printable: object is NULL");
253  success ("called printable callback");
254  return ((const char *) obj);
255 }
256 
257 static void
258 test_foreach (QofBook * book, const char *name)
259 {
260  int res = 0;
261 
262  qof_object_foreach (NULL, NULL, NULL, &res);
263  do_test (res == 0, "object: Foreach: NULL, NULL, NULL");
264  qof_object_foreach (NULL, NULL, foreachCB, &res);
265  do_test (res == 0, "object: Foreach: NULL, NULL, foreachCB");
266 
267  qof_object_foreach (NULL, book, NULL, &res);
268  do_test (res == 0, "object: Foreach: NULL, book, NULL");
269  qof_object_foreach (NULL, book, foreachCB, &res);
270  do_test (res == 0, "object: Foreach: NULL, book, foreachCB");
271 
272  qof_object_foreach (name, NULL, NULL, &res);
273  do_test (res == 0, "object: Foreach: name, NULL, NULL");
274  qof_object_foreach (name, NULL, foreachCB, &res);
275  do_test (res == 0, "object: Foreach: name, NULL, foreachCB");
276 
277  qof_object_foreach (name, book, NULL, &res);
278  do_test (res != 0, "object: Foreach: name, book, NULL");
279 
280  res = 0;
281  qof_object_foreach (name, book, foreachCB, &res);
282  do_test (res != 0, "object: Foreach: name, book, foreachCB");
283 }
284 
285 static void
286 test_printable (const char *name, gpointer obj)
287 {
288  const char *res;
289 
290  do_test (qof_object_printable (NULL, NULL) == NULL,
291  "object: Printable: NULL, NULL");
292  do_test (qof_object_printable (NULL, obj) == NULL,
293  "object: Printable: NULL, object");
294  do_test (qof_object_printable (name, NULL) == NULL,
295  "object: Printable: mod_name, NULL");
296  res = qof_object_printable (name, obj);
297  do_test (res != NULL, "object: Printable: mod_name, object");
298 }
299 
300 int
301 main (void)
302 {
303  qof_init ();
304  test_object ();
305  test_dynamic_object ();
306  print_test_results ();
307  qof_close ();
308  return get_rv ();
309 }