GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-kvp-frames.c
1 #include "config.h"
2 
3 #include <stdlib.h>
4 
5 #include "test-stuff.h"
6 #include "test-engine-stuff.h"
7 #include "test-file-stuff.h"
8 
9 #include "qof.h"
10 
11 #include "sixtp-dom-generators.h"
12 #include "sixtp-dom-parsers.h"
13 
14 #define GNC_V2_STRING "gnc-v2"
15 const gchar *gnc_v2_xml_version_string = GNC_V2_STRING;
16 
17 static void
18 test_kvp_get_slot(int run,
19  KvpFrame *test_frame1, const KvpValue *test_val1,
20  const gchar *test_key)
21 {
22  const KvpValue *test_val2;
23  test_val2 = kvp_frame_get_slot(test_frame1, test_key);
24  if (kvp_value_compare(test_val1, test_val2) == 0)
25  {
26  success_args("kvp_frame_get_slot", __FILE__, __LINE__, "run=%d", run);
27  }
28  else
29  {
30  gchar *tmp;
31  failure_args("kvp_frame_get_slot", __FILE__, __LINE__, "run=%d", run);
32  tmp = kvp_value_to_string(test_val2);
33  printf(" Value is %s\n", tmp);
34  g_free(tmp);
35  }
36 }
37 
38 static void
39 test_kvp_copy_compare(int run,
40  KvpFrame *test_frame1, const KvpValue *test_val1,
41  const gchar *test_key)
42 {
43  KvpFrame *test_frame2;
44 
45  test_frame2 = kvp_frame_copy(test_frame1);
46 
47  do_test_args(test_frame2 != NULL, "kvp_frame_copy",
48  __FILE__, __LINE__, "run=%d", run);
49 
50  if (kvp_frame_compare(test_frame1, test_frame2) == 0)
51  {
52  success_args("kvp_frame_copy->kvp_frame_compare",
53  __FILE__, __LINE__, "run=%d", run);
54  }
55  else
56  {
57  gchar *tmp;
58  failure_args("kvp_frame_copy->kvp_frame_compare",
59  __FILE__, __LINE__, "run=%d", run);
60  tmp = kvp_frame_to_string(test_frame1);
61  printf("Frame1 is %s\n", tmp);
62  g_free(tmp);
63  tmp = kvp_frame_to_string(test_frame2);
64  printf("Frame2 is %s\n", tmp);
65  g_free(tmp);
66  }
67 
68  kvp_frame_delete(test_frame2);
69 }
70 
71 static void
72 test_kvp_copy_get_slot(int run,
73  KvpFrame *test_frame1, const KvpValue *test_val1,
74  const gchar *test_key)
75 {
76  KvpFrame *test_frame2;
77  const KvpValue *test_val2;
78 
79  test_frame2 = kvp_frame_copy(test_frame1);
80  test_val2 = kvp_frame_get_slot(test_frame2, test_key);
81  if (kvp_value_compare(test_val1, test_val2) == 0)
82  {
83  success_args("kvp_frame_copy->kvp_frame_get_slot",
84  __FILE__, __LINE__, "run=%d", run);
85  }
86  else
87  {
88  gchar *tmp;
89  failure_args("kvp_frame_copy->kvp_frame_get_slot",
90  __FILE__, __LINE__, "run=%d", run);
91  tmp = kvp_frame_to_string(test_frame1);
92  printf("Frame1 is %s\n", tmp);
93  g_free(tmp);
94  tmp = kvp_frame_to_string(test_frame2);
95  printf("Frame2 is %s\n", tmp);
96  g_free(tmp);
97  }
98  kvp_frame_delete(test_frame2);
99 }
100 
101 static void
102 test_kvp_create_delete(void)
103 {
104  KvpFrame *test_frame;
105 
106  test_frame = kvp_frame_new();
107 
108  if (test_frame != NULL)
109  {
110  kvp_frame_delete(test_frame);
111  test_frame = NULL;
112  success("kvp_frame_new");
113  }
114  else
115  {
116  failure("kvp_frame_new");
117  }
118 }
119 
120 static void
121 test_kvp_frames1(void)
122 {
123  int i;
124 
125  for (i = 0; i < 20; i++)
126  {
127  KvpFrame *test_frame1;
128  gchar *test_key;
129  KvpValue *test_val1;
130 
131  test_val1 = get_random_kvp_value(i % KVP_TYPE_FRAME);
132 
133  test_frame1 = kvp_frame_new();
134  test_key = get_random_string();
135 
136  kvp_frame_set_slot(test_frame1, test_key, test_val1);
137 
138  test_kvp_get_slot(i, test_frame1, test_val1, test_key);
139  test_kvp_copy_compare(i, test_frame1, test_val1, test_key);
140  test_kvp_copy_get_slot(i, test_frame1, test_val1, test_key);
141 
142  kvp_value_delete(test_val1);
143  g_free(test_key);
144  kvp_frame_delete(test_frame1);
145  }
146 }
147 
148 static void
149 test_kvp_printing(void)
150 {
151  int i;
152  for (i = 0; i < 20; i++)
153  {
154  KvpFrame *ran_frame;
155  gchar *char_rep;
156 
157  ran_frame = get_random_kvp_frame();
158  char_rep = kvp_frame_to_string(ran_frame);
159 
160  /* if we don't crash it's good :) */
161  /* puts(char_rep); */
162 
163  g_free(char_rep);
164  kvp_frame_delete(ran_frame);
165  }
166 }
167 
168 static void
169 test_kvp_xml_stuff(void)
170 {
171  int i;
172  for (i = 0; i < 20; i++)
173  {
174  KvpFrame *test_frame1;
175  KvpFrame *test_frame2;
176  xmlNodePtr test_node;
177 
178  test_frame1 = get_random_kvp_frame();
179 
180  test_node = kvp_frame_to_dom_tree("test-kvp", test_frame1);
181 
182  if (!test_node)
183  {
184  failure_args("xml stuff", __FILE__, __LINE__,
185  "kvp_frame_to_dom_tree produced NULL");
186  }
187  else
188  {
189  test_frame2 = dom_tree_to_kvp_frame(test_node);
190 
191  if (kvp_frame_compare(test_frame1, test_frame2) == 0)
192  {
193  success("xml stuff");
194  }
195  else
196  {
197  gchar *tmp;
198  failure("xml stuff");
199  tmp = kvp_frame_to_string(test_frame1);
200  printf(" with kvp_frame 1:\n%s\n", tmp);
201  g_free(tmp);
202  printf(" and xml:\n");
203  xmlElemDump(stdout, NULL, test_node);
204  printf("\n");
205  tmp = kvp_frame_to_string(test_frame2);
206  printf(" and kvp_frame 2:\n%s\n", tmp);
207  g_free(tmp);
208  }
209  kvp_frame_delete(test_frame2);
210  xmlFreeNode(test_node);
211  }
212 
213  kvp_frame_delete(test_frame1);
214  }
215 }
216 
217 int
218 main(int argc, char** argv)
219 {
220  qof_init();
221  test_kvp_create_delete();
222  test_kvp_printing();
223  test_kvp_frames1();
224  test_kvp_xml_stuff();
225  print_test_results();
226  exit(get_rv());
227 }
void kvp_frame_set_slot(KvpFrame *frame, const gchar *key, KvpValue *value)
gint kvp_frame_compare(const KvpFrame *fa, const KvpFrame *fb)
KvpFrame * kvp_frame_copy(const KvpFrame *frame)
void kvp_frame_delete(KvpFrame *frame)
gchar * kvp_frame_to_string(const KvpFrame *frame)
struct KvpFrameImpl KvpFrame
Definition: kvp_frame.h:76
KvpFrame * kvp_frame_new(void)
void kvp_value_delete(KvpValue *value)
gint kvp_value_compare(const KvpValue *va, const KvpValue *vb)
void qof_init(void)
Initialise the Query Object Framework.
gchar * kvp_value_to_string(const KvpValue *val)
Debug version of kvp_value_to_string.
struct KvpValueImpl KvpValue
Definition: kvp_frame.h:80