GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-dom-converters1.c
1 /***************************************************************************
2  * test-dom-converters1.c
3  *
4  * Fri Oct 7 20:51:06 2005
5  * Copyright 2005 Neil Williams
7  ****************************************************************************/
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24 
25 #include "config.h"
26 
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <glib.h>
31 
32 #include "test-stuff.h"
33 #include "test-engine-stuff.h"
34 #include "test-file-stuff.h"
35 #include "cashobjects.h"
36 #include "gnc-xml-helper.h"
37 #include "gnc-engine.h"
38 #include "sixtp.h"
39 #include "sixtp-parsers.h"
40 #include "sixtp-utils.h"
41 #include "sixtp-dom-parsers.h"
42 #include "sixtp-dom-generators.h"
43 
44 #include "gnc-commodity.h"
45 
46 #define GNC_V2_STRING "gnc-v2"
47 const gchar *gnc_v2_xml_version_string = GNC_V2_STRING;
48 
49 static void
50 test_dom_tree_to_commodity_ref(void)
51 {
52  int i;
53  for (i = 0; i < 20; i++)
54  {
55  gnc_commodity *test_com1;
56  gchar *test_str1;
57  gchar *test_str2;
58  gnc_commodity *test_com2;
59  xmlNodePtr test_node;
60  QofBook *book;
61 
62  book = qof_book_new ();
63 
64  test_str1 = get_random_string();
65  test_str2 = get_random_string();
66 
67  test_com1 = gnc_commodity_new(book, NULL, test_str1, test_str2, NULL, 0);
68  test_node = commodity_ref_to_dom_tree("test-com", test_com1);
69 
70  test_com2 = dom_tree_to_commodity_ref_no_engine(test_node, book);
71 
72  do_test(gnc_commodity_equiv(test_com1, test_com2),
73  "dom_tree_to_commodity_ref_no_engine");
74 
75  xmlFreeNode(test_node);
76  gnc_commodity_destroy(test_com1);
77  gnc_commodity_destroy(test_com2);
78  g_free(test_str1);
79  g_free(test_str2);
80 
81  qof_book_destroy (book);
82  }
83 }
84 
85 static void
86 test_dom_tree_to_text(void)
87 {
88  int i;
89 
90  for (i = 0; i < 20; i++)
91  {
92  gchar *test_string1;
93  gchar *test_string2;
94  xmlNodePtr test_node;
95 
96  test_node = xmlNewNode(NULL, BAD_CAST "test-node");
97  test_string1 = get_random_string();
98 
99  xmlNodeAddContent(test_node, BAD_CAST test_string1);
100 
101  test_string2 = dom_tree_to_text(test_node);
102 
103  if (!test_string2)
104  {
105  failure_args("dom_tree_to_text", __FILE__, __LINE__,
106  "null return from dom_tree_to_text");
107  xmlElemDump(stdout, NULL, test_node);
108  }
109  else if (g_strcmp0(test_string1, test_string2) == 0)
110  {
111  success_args("dom_tree_to_text", __FILE__, __LINE__, "with string %s",
112  test_string1);
113  }
114  else
115  {
116  failure_args("dom_tree_to_text", __FILE__, __LINE__,
117  "with string %s", test_string1);
118  }
119 
120  xmlFreeNode(test_node);
121  g_free(test_string1);
122  if (test_string2) g_free(test_string2);
123  }
124 }
125 
126 
127 static void
128 test_dom_tree_to_timespec(void)
129 {
130  int i;
131  for (i = 0; i < 20; i++)
132  {
133  Timespec *test_spec1;
134  Timespec test_spec2;
135  xmlNodePtr test_node;
136 
137  test_spec1 = get_random_timespec();
138 
139  test_node = timespec_to_dom_tree("test-spec", test_spec1);
140 
141  test_spec2 = dom_tree_to_timespec(test_node);
142 
143  if (!dom_tree_valid_timespec(&test_spec2, (const xmlChar*)"test-spec"))
144  {
145  failure_args("dom_tree_to_timespec",
146  __FILE__, __LINE__, "NULL return");
147  printf("Node looks like:\n");
148  xmlElemDump(stdout, NULL, test_node);
149  printf("\n");
150  }
151 
152  else if (timespec_cmp(test_spec1, &test_spec2) == 0)
153  {
154  success("dom_tree_to_timespec");
155  }
156  else
157  {
158  failure("dom_tree_to_timespec");
159  printf("Node looks like:\n");
160  xmlElemDump(stdout, NULL, test_node);
161  printf("\n");
162  printf("Secs are %" G_GUINT64_FORMAT " vs %" G_GUINT64_FORMAT " :: ",
163  test_spec1->tv_sec,
164  test_spec2.tv_sec);
165  printf("NSecs are %ld vs %ld\n",
166  test_spec1->tv_nsec,
167  test_spec2.tv_nsec);
168  }
169 
170  g_free(test_spec1);
171  xmlFreeNode(test_node);
172  }
173 }
174 
175 static gchar *
176 test_gnc_nums_internal(gnc_numeric to_test)
177 {
178  gchar *ret = NULL;
179  gnc_numeric *to_compare = NULL;
180  xmlNodePtr to_gen = NULL;
181 
182  to_gen = gnc_numeric_to_dom_tree("test-num", &to_test);
183  if (!to_gen)
184  {
185  ret = "no dom tree created";
186  }
187  else
188  {
189  to_compare = dom_tree_to_gnc_numeric(to_gen);
190  if (!to_compare)
191  {
192  ret = "no gnc_numeric parsed";
193  }
194  else
195  {
196  if (!gnc_numeric_equal(to_test, *to_compare))
197  {
198  ret = "numerics compared different";
199  }
200  }
201  }
202 
203  if (to_compare)
204  {
205  g_free(to_compare);
206  }
207  if (to_gen)
208  {
209  xmlFreeNode(to_gen);
210  }
211 
212  return ret;
213 }
214 
215 static void
216 test_dom_tree_to_gnc_numeric(void)
217 {
218  int i;
219 
220  for (i = 0; i < 20; i++)
221  {
222  gchar *message = NULL;
223 
224  message = test_gnc_nums_internal(get_random_gnc_numeric(GNC_DENOM_AUTO));
225 
226  do_test_args(message == NULL, "dom_tree_to_gnc_numeric",
227  __FILE__, __LINE__, message);
228  }
229 
230  {
231  gchar *message = NULL;
232 
233  message = test_gnc_nums_internal
234  (gnc_numeric_create(18768786810LL, 100000));
235 
236  do_test_args(message == NULL, "gnc_num 18768786810/100000",
237  __FILE__, __LINE__, message);
238  }
239 }
240 
241 
242 static void
243 test_dom_tree_to_guid(void)
244 {
245  int i;
246  for (i = 0; i < 20; i++)
247  {
248  GncGUID *test_guid1;
249  GncGUID *test_guid2;
250  xmlNodePtr test_node;
251 
252  test_guid1 = get_random_guid();
253 
254  if (!(test_node = guid_to_dom_tree("test-guid", test_guid1)))
255  {
256  failure_args("guid_to_dom_tree", __FILE__, __LINE__,
257  "conversion to dom tree failed");
258  }
259 
260  test_guid2 = dom_tree_to_guid(test_node);
261 
262  do_test(guid_equal(test_guid1, test_guid2),
263  "dom_tree_to_guid" );
264 
265  xmlFreeNode(test_node);
266  g_free(test_guid1);
267  g_free(test_guid2);
268  }
269 }
270 
271 int
272 main(int argc, char **argv)
273 {
274  qof_init();
275  cashobjects_register();
276  test_dom_tree_to_guid();
277  fflush(stdout);
278  test_dom_tree_to_commodity_ref();
279  fflush(stdout);
280  test_dom_tree_to_text();
281  fflush(stdout);
282  test_dom_tree_to_timespec();
283  fflush(stdout);
284  test_dom_tree_to_gnc_numeric();
285  fflush(stdout);
286  print_test_results();
287  qof_close();
288  exit(get_rv());
289 }
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
QofBook * qof_book_new(void)
Use a 64-bit unsigned int timespec.
Definition: gnc-date.h:299
Definition: guid.h:65
gint timespec_cmp(const Timespec *ta, const Timespec *tb)
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
gnc_commodity * gnc_commodity_new(QofBook *book, const char *fullname, const char *name_space, const char *mnemonic, const char *cusip, int fraction)
All type declarations for the whole Gnucash engine.
void qof_close(void)
Safely close down the Query Object Framework.
void qof_init(void)
Initialise the Query Object Framework.
#define GNC_DENOM_AUTO
Definition: gnc-numeric.h:246
Commodity handling public routines.
void gnc_commodity_destroy(gnc_commodity *cm)
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
void qof_book_destroy(QofBook *book)