GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-kvp_frame.c
1 /********************************************************************
2  * test-kvp_frame.c: GLib g_test test suite for kvp_frame.c. *
3  * Copyright 2011 John Ralls <[email protected]> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA [email protected] *
21 ********************************************************************/
22 #ifdef __cplusplus
23 extern "C"
24 {
25 #endif
26 
27 #include "config.h"
28 #include <string.h>
29 #include <glib.h>
30 #include "unittest-support.h"
31 
32 #ifdef __cplusplus
33 }
34 #endif
35 
36 #include "qof.h"
37 
38 static const gchar *suitename = "/qof/kvp_frame";
39 void test_suite_kvp_frame ( void );
40 
41 typedef struct
42 {
43  KvpFrame *frame;
44  GSList *hdlrs;
45 } Fixture;
46 
47 static void
48 setup( Fixture *fixture, gconstpointer pData )
49 {
50  fixture->frame = kvp_frame_new();
51  fixture->hdlrs = NULL;
52 }
53 
54 static void
55 teardown( Fixture *fixture, gconstpointer pData )
56 {
57  kvp_frame_delete( fixture->frame );
58  g_slist_free_full (fixture->hdlrs, test_free_log_handler);
59  test_clear_error_list ();
60 }
61 
62 static gchar* glist_to_string( const GList *list )
63 {
64  KvpValue * val = kvp_value_new_glist( list );
65  gchar * ret = kvp_value_to_string( val );
66  kvp_value_delete( val );
67  return ret;
68 }
69 
70 extern KvpFrame* ( *p_get_trailer_make )( KvpFrame *frame, const char *key_path, char **end_key );
71 extern KvpFrame* ( *p_get_or_make )( KvpFrame *fr, const char * key );
72 extern const KvpFrame* ( *p_kvp_frame_get_frame_or_null_slash_trash )( const KvpFrame *frame, char *key_path );
73 extern const KvpFrame* ( *p_get_trailer_or_null )( const KvpFrame * frame, const char * key_path, char **end_key );
74 
75 extern void init_static_test_pointers( void );
76 
77 static void
78 setup_static( Fixture *fixture, gconstpointer pData )
79 {
80  fixture->frame = kvp_frame_new();
81  fixture->hdlrs = NULL;
82  init_static_test_pointers();
83  g_assert( p_get_trailer_make && p_get_or_make && p_get_trailer_or_null &&
84  p_kvp_frame_get_frame_or_null_slash_trash );
85 }
86 
87 static void
88 teardown_static( Fixture *fixture, gconstpointer pData )
89 {
90  kvp_frame_delete( fixture->frame );
91  g_slist_free_full (fixture->hdlrs, test_free_log_handler);
92  test_clear_error_list ();
93  p_get_trailer_make = NULL;
94  p_get_or_make = NULL;
95  p_kvp_frame_get_frame_or_null_slash_trash = NULL;
96  p_get_trailer_or_null = NULL;
97 }
98 
99 static GncGUID*
100 populate_frame (KvpFrame *frame)
101 {
102  GList *list = NULL;
103  Timespec ts;
104  GncGUID *guid;
105  GDate gdate;
106 
107  ts.tv_sec = 1;
108  ts.tv_nsec = 1;
109  guid = guid_new ();
110  g_date_set_dmy (&gdate, 26, 1, 1957);
111 
112  kvp_frame_set_gint64( frame, "gint64-type", 100 );
113  kvp_frame_set_double( frame, "double-type", 3.14159 );
114  kvp_frame_set_numeric( frame, "numeric-type", gnc_numeric_zero() );
115  kvp_frame_set_timespec( frame, "timespec-type", ts );
116  kvp_frame_set_string( frame, "string-type", "abcdefghijklmnop" );
117  kvp_frame_set_guid( frame, "guid-type", guid );
118  kvp_frame_set_value_nc (frame, "gdate-type", kvp_value_new_gdate (gdate));
119  kvp_frame_set_frame( frame, "frame-type", kvp_frame_new() );
120 
121  list = g_list_prepend (list, kvp_value_new_guid (guid));
122  list = g_list_prepend (list, kvp_value_new_string ("qrstuvwxyz"));
123  list = g_list_prepend (list, kvp_value_new_timespec (ts));
124  list = g_list_prepend (list, kvp_value_new_numeric (gnc_numeric_create (256, 120)));
125  list = g_list_prepend (list, kvp_value_new_double (0.4342944819));
126  list = g_list_prepend (list, kvp_value_new_gint64 (0x1f2e3d4c5b6a79LL));
127  kvp_frame_set_value (frame, "list-type", kvp_value_new_glist_nc (list));
128 
129  return guid;
130 }
131 
132 static void
133 test_kvp_frame_new_delete( void )
134 {
135  KvpFrame *frame;
136  GncGUID *guid;
137 
138  frame = kvp_frame_new();
139  g_assert( frame );
140  g_assert( kvp_frame_is_empty( frame ) );
141 
142  guid = populate_frame (frame);
143 
144  g_assert( !kvp_frame_is_empty( frame ) );
145 
146  kvp_frame_delete( frame );
147  g_assert( frame );
148  guid_free (guid);
149 }
150 
151 static void
152 test_kvp_frame_copy( Fixture *fixture, gconstpointer pData )
153 {
154  KvpFrame *to_copy = NULL;
155  gint64 test_gint64, copy_gint64;
156  double test_double, copy_double;
157  gnc_numeric test_gnc_numeric, copy_gnc_numeric;
158  Timespec test_ts, copy_ts;
159  const char* test_str, *copy_str;
160  GncGUID *test_guid, *copy_guid;
161  KvpFrame *test_frame, *copy_frame;
162 
163  /* init data in source frame */
164  test_gint64 = 111;
165  test_double = 1.1;
166  test_gnc_numeric = gnc_numeric_zero();
167  test_ts.tv_sec = 1;
168  test_ts.tv_nsec = 1;
169  test_str = "abcdefghijklmnop";
170  test_guid = guid_new();
171  test_frame = kvp_frame_new();
172 
173  g_assert( fixture->frame );
174  kvp_frame_set_gint64( fixture->frame, "gint64-type", test_gint64 );
175  kvp_frame_set_double( fixture->frame, "double-type", test_double );
176  kvp_frame_set_numeric( fixture->frame, "numeric-type", test_gnc_numeric );
177  kvp_frame_set_timespec( fixture->frame, "timespec-type", test_ts );
178  kvp_frame_set_string( fixture->frame, "string-type", test_str );
179  kvp_frame_set_guid( fixture->frame, "guid-type", test_guid );
180  kvp_frame_set_frame( fixture->frame, "frame-type", test_frame );
181  g_assert( !kvp_frame_is_empty( fixture->frame ) );
182 
183  g_test_message( "Test frame copy" );
184  to_copy = kvp_frame_copy( fixture->frame );
185  g_assert( to_copy );
186  g_assert( !kvp_frame_is_empty( to_copy ) );
187  g_assert( to_copy != fixture->frame );
188 
189  g_assert_cmpint( kvp_frame_compare( fixture->frame, to_copy ), == , 0 );
190  copy_gint64 = kvp_frame_get_gint64( to_copy, "gint64-type" );
191  g_assert( &copy_gint64 != &test_gint64 );
192  g_assert_cmpint( copy_gint64, == , test_gint64 );
193  copy_double = kvp_frame_get_double( to_copy, "double-type" );
194  g_assert( &copy_double != &test_double );
195  g_assert_cmpfloat( copy_double, == , test_double );
196  copy_gnc_numeric = kvp_frame_get_numeric( to_copy, "numeric-type" );
197  g_assert( &copy_gnc_numeric != &test_gnc_numeric );
198  g_assert_cmpfloat( copy_gnc_numeric.num, == , test_gnc_numeric.num );
199  g_assert_cmpfloat( copy_gnc_numeric.denom, == , test_gnc_numeric.denom );
200  copy_ts = kvp_frame_get_timespec( to_copy, "timespec-type" );
201  g_assert( &copy_ts != &test_ts );
202  g_assert_cmpfloat( copy_ts.tv_sec, == , test_ts.tv_sec );
203  g_assert_cmpfloat( copy_ts.tv_nsec, == , test_ts.tv_nsec );
204  copy_str = kvp_frame_get_string( to_copy, "string-type" );
205  g_assert( copy_str != test_str );
206  g_assert_cmpstr( copy_str, == , test_str );
207  copy_guid = kvp_frame_get_guid( to_copy, "guid-type" );
208  g_assert( copy_guid != test_guid );
209  g_assert( guid_equal( copy_guid, test_guid ) );
210  copy_frame = kvp_frame_get_frame( to_copy, "frame-type");
211  g_assert( copy_frame );
212  g_assert( kvp_frame_is_empty( copy_frame ) );
213  g_assert( copy_frame != test_frame );
214  g_assert_cmpint( kvp_frame_compare( copy_frame, test_frame ), == , 0 );
215 
216  kvp_frame_delete( to_copy );
217  guid_free( test_guid );
218 }
219 
220 static void
221 test_kvp_frame_set_foo( Fixture *fixture, gconstpointer pData )
222 {
223  gnc_numeric test_gnc_numeric, copy_gnc_numeric;
224  Timespec test_ts, copy_ts;
225  GncGUID *test_guid, *copy_guid;
226 
227  test_gnc_numeric = gnc_numeric_zero();
228  test_ts.tv_sec = 1;
229  test_ts.tv_nsec = 1;
230  test_guid = guid_new();
231 
232  g_assert( fixture->frame );
233  g_assert( kvp_frame_is_empty( fixture->frame ) );
234 
235  g_test_message( "Test gint64 setup and replace, test frame is created" );
236  g_assert( kvp_frame_get_frame( fixture->frame, "/test" ) == NULL );
237  kvp_frame_set_gint64( fixture->frame, "/test/gint64", 1 );
238  g_assert( kvp_frame_get_frame( fixture->frame, "/test" ) != NULL );
239  g_assert_cmpint( kvp_frame_get_gint64( fixture->frame, "/test/gint64" ), == , 1 );
240  kvp_frame_set_gint64( fixture->frame, "/test/gint64", 5 );
241  g_assert_cmpint( kvp_frame_get_gint64( fixture->frame, "/test/gint64" ), == , 5 );
242 
243  g_test_message( "Test double setup and replace, test2 frame is created" );
244  g_assert( kvp_frame_get_frame( fixture->frame, "/test2" ) == NULL );
245  kvp_frame_set_double( fixture->frame, "/test2/double", 1.1 );
246  g_assert( kvp_frame_get_frame( fixture->frame, "/test2" ) != NULL );
247  g_assert_cmpfloat( kvp_frame_get_double( fixture->frame, "/test2/double" ), == , 1.1 );
248  kvp_frame_set_double( fixture->frame, "/test2/double", 5.5 );
249  g_assert_cmpfloat( kvp_frame_get_double( fixture->frame, "/test2/double" ), == , 5.5 );
250 
251  g_test_message( "Test double setup and replace, test3 frame is created" );
252  g_assert( kvp_frame_get_frame( fixture->frame, "/test3" ) == NULL );
253  kvp_frame_set_numeric( fixture->frame, "/test3/numeric", test_gnc_numeric );
254  g_assert( kvp_frame_get_frame( fixture->frame, "/test3" ) != NULL );
255  copy_gnc_numeric = kvp_frame_get_numeric( fixture->frame, "/test3/numeric" );
256  g_assert_cmpint( copy_gnc_numeric.num, == , 0 );
257  g_assert_cmpint( copy_gnc_numeric.denom, == , 1 );
258  test_gnc_numeric.num = 2;
259  test_gnc_numeric.denom = 3;
260  kvp_frame_set_numeric( fixture->frame, "/test3/numeric", test_gnc_numeric );
261  copy_gnc_numeric = kvp_frame_get_numeric( fixture->frame, "/test3/numeric" );
262  g_assert_cmpint( copy_gnc_numeric.num, == , 2 );
263  g_assert_cmpint( copy_gnc_numeric.denom, == , 3 );
264 
265  g_test_message( "Test timespec setup and replace, test4 frame is created" );
266  g_assert( kvp_frame_get_frame( fixture->frame, "/test4" ) == NULL );
267  kvp_frame_set_timespec( fixture->frame, "/test4/timespec", test_ts );
268  g_assert( kvp_frame_get_frame( fixture->frame, "/test4" ) != NULL );
269  copy_ts = kvp_frame_get_timespec( fixture->frame, "/test4/timespec" );
270  g_assert_cmpint( copy_ts.tv_sec, == , 1 );
271  g_assert_cmpint( copy_ts.tv_nsec, == , 1 );
272  test_ts.tv_sec = 7;
273  test_ts.tv_nsec = 13;
274  kvp_frame_set_timespec( fixture->frame, "/test4/timespec", test_ts );
275  copy_ts = kvp_frame_get_timespec( fixture->frame, "/test4/timespec" );
276  g_assert_cmpint( copy_ts.tv_sec, == , 7 );
277  g_assert_cmpint( copy_ts.tv_nsec, == , 13 );
278 
279  g_test_message( "Test string setup and replace, test5 frame is created" );
280  g_assert( kvp_frame_get_frame( fixture->frame, "/test5" ) == NULL );
281  kvp_frame_set_string( fixture->frame, "/test5/string", "one string" );
282  g_assert( kvp_frame_get_frame( fixture->frame, "/test5" ) != NULL );
283  g_assert_cmpstr( kvp_frame_get_string( fixture->frame, "/test5/string" ), == , "one string" );
284  kvp_frame_set_string( fixture->frame, "/test5/string", "another string" );
285  g_assert_cmpstr( kvp_frame_get_string( fixture->frame, "/test5/string" ), == , "another string" );
286 
287  g_test_message( "Test guid setup and replace, test6 frame is created" );
288  g_assert( kvp_frame_get_frame( fixture->frame, "/test6" ) == NULL );
289  kvp_frame_set_guid( fixture->frame, "/test6/guid", test_guid );
290  g_assert( kvp_frame_get_frame( fixture->frame, "/test6" ) != NULL );
291  copy_guid = kvp_frame_get_guid( fixture->frame, "/test6/guid" );
292  g_assert( guid_equal( copy_guid, test_guid ) );
293  kvp_frame_set_guid( fixture->frame, "/test6/guid", guid_null() );
294  copy_guid = kvp_frame_get_guid( fixture->frame, "/test6/guid" );
295  g_assert( guid_equal( copy_guid, guid_null() ) );
296 
297  g_test_message( "Test frame setup and replace, test7 frame is created" );
298  g_assert( kvp_frame_get_frame( fixture->frame, "/test7" ) == NULL );
299  kvp_frame_set_frame( fixture->frame, "/test7", kvp_frame_new() );
300  g_assert( kvp_frame_get_frame( fixture->frame, "/test7" ) != NULL );
301  kvp_frame_set_frame( fixture->frame, "/test7", NULL );
302  g_assert( kvp_frame_get_frame( fixture->frame, "/test7" ) == NULL );
303 }
304 
305 static void
306 test_kvp_frame_get_frame_slash( Fixture *fixture, gconstpointer pData )
307 {
308  KvpFrame *result_frame = NULL;
309  /* Mostly testing static routine kvp_frmae_get_frame_slash_trash */
310  g_assert( fixture->frame );
311 
312  g_test_message( "Test path with one slash same frame should be returned" );
313  result_frame = kvp_frame_get_frame_slash( fixture->frame, "/" );
314  g_assert( result_frame );
315  g_assert( result_frame == fixture->frame );
316 
317  g_test_message( "Test path with trailing slash same frame should be returned" );
318  result_frame = kvp_frame_get_frame_slash( fixture->frame, "/////" );
319  g_assert( result_frame );
320  g_assert( result_frame == fixture->frame );
321 
322  g_test_message( "Test new frame is created" );
323  result_frame = kvp_frame_get_frame_slash( fixture->frame, "/test" );
324  g_assert( result_frame );
325  g_assert( result_frame != fixture->frame );
326  g_assert( result_frame == kvp_frame_get_frame( fixture->frame, "/test" ) );
327 
328  g_test_message( "Test trailing slashes are ignored and frame created" );
329  result_frame = kvp_frame_get_frame_slash( fixture->frame, "////test2/////" );
330  g_assert( result_frame );
331  g_assert( result_frame != fixture->frame );
332  g_assert( result_frame == kvp_frame_get_frame( fixture->frame, "/test2" ) );
333  g_assert( result_frame != kvp_frame_get_frame( fixture->frame, "/test" ) );
334 
335  g_test_message( "Test frames are created along the path if not exist and last frame is returned" );
336  result_frame = kvp_frame_get_frame_slash( fixture->frame, "////test3/////test4//////" );
337  g_assert( result_frame );
338  g_assert( result_frame != fixture->frame );
339  g_assert( result_frame != kvp_frame_get_frame( fixture->frame, "/test2" ) );
340  g_assert( result_frame != kvp_frame_get_frame( fixture->frame, "/test" ) );
341  g_assert( kvp_frame_get_frame( fixture->frame, "/test3" ) != NULL );
342  g_assert( result_frame != kvp_frame_get_frame( fixture->frame, "/test3" ) );
343  g_assert( result_frame == kvp_frame_get_frame( fixture->frame, "/test3/test4" ) );
344 
345  g_test_message( "Test existing frame is returned" );
346  g_assert( result_frame == kvp_frame_get_frame_slash( fixture->frame, "////test3/////test4//////" ) );
347 }
348 
349 static void
350 test_kvp_frame_get_slot_path( Fixture *fixture, gconstpointer pData )
351 {
352  KvpValue *result_value = NULL ;
353 
354  g_assert( fixture->frame );
355  g_assert( kvp_frame_is_empty( fixture->frame ) );
356 
357  g_test_message( "Test with non existing path should return NULL" );
358  result_value = kvp_frame_get_slot_path( fixture->frame, "test", "test2", NULL );
359  g_assert( !result_value );
360 
361  g_test_message( "Test with existing value set to current frame" );
362  kvp_frame_set_gint64( fixture->frame, "/test", 1 );
363  result_value = kvp_frame_get_slot_path( fixture->frame, "test", NULL );
364  g_assert( result_value );
365  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_GINT64 );
366  g_assert_cmpint( kvp_value_get_gint64( result_value ), == , 1 );
367 
368  g_test_message( "Test should return null as test is not a frame" );
369  kvp_frame_set_gint64( fixture->frame, "/test/test2", 2 );
370  result_value = kvp_frame_get_slot_path( fixture->frame, "test", "test2", NULL );
371  g_assert( !result_value );
372 
373  g_test_message( "Test should return last value in the path" );
374  kvp_frame_set_gint64( fixture->frame, "/test2/test3", 2 );
375  result_value = kvp_frame_get_slot_path( fixture->frame, "test2", "test3", NULL );
376  g_assert( result_value );
377  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_GINT64 );
378  g_assert_cmpint( kvp_value_get_gint64( result_value ), == , 2 );
379 
380  g_test_message( "Test should return null as last value in the path does not exist" );
381  result_value = kvp_frame_get_slot_path( fixture->frame, "test2", "test3", "test4", NULL );
382  g_assert( !result_value );
383 }
384 
385 static void
386 test_kvp_frame_get_slot_path_gslist( Fixture *fixture, gconstpointer pData )
387 {
388  /* similar to previous test except path is passed as GSList*/
389  GSList *path_list = NULL;
390  KvpValue *result_value = NULL ;
391 
392  g_assert( fixture->frame );
393  g_assert( kvp_frame_is_empty( fixture->frame ) );
394 
395  g_test_message( "Test with non existing path should return NULL" );
396  path_list = g_slist_append (path_list, "test");
397  path_list = g_slist_append (path_list, "test2");
398  result_value = kvp_frame_get_slot_path_gslist( fixture->frame, path_list );
399  g_assert( !result_value );
400 
401  g_test_message( "Test with existing value set to current frame" );
402  path_list = g_slist_remove( path_list, "test2" );
403  kvp_frame_set_gint64( fixture->frame, "/test", 1 );
404  result_value = kvp_frame_get_slot_path_gslist( fixture->frame, path_list );
405  g_assert( result_value );
406  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_GINT64 );
407  g_assert_cmpint( kvp_value_get_gint64( result_value ), == , 1 );
408 
409  g_test_message( "Test should return null as test is not a frame" );
410  path_list = g_slist_append (path_list, "test2");
411  kvp_frame_set_gint64( fixture->frame, "/test/test2", 2 );
412  result_value = kvp_frame_get_slot_path_gslist( fixture->frame, path_list );
413  g_assert( !result_value );
414 
415  g_test_message( "Test should return last value in the path" );
416  path_list = g_slist_remove( path_list, "test" );
417  path_list = g_slist_append (path_list, "test3");
418  kvp_frame_set_gint64( fixture->frame, "/test2/test3", 2 );
419  result_value = kvp_frame_get_slot_path_gslist( fixture->frame, path_list );
420  g_assert( result_value );
421  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_GINT64 );
422  g_assert_cmpint( kvp_value_get_gint64( result_value ), == , 2 );
423 
424  g_test_message( "Test should return null as last value in the path does not exist" );
425  path_list = g_slist_append (path_list, "test4");
426  result_value = kvp_frame_get_slot_path_gslist( fixture->frame, path_list );
427  g_assert( !result_value );
428  g_slist_free( path_list );
429 }
430 
431 static void
432 test_kvp_frame_add_frame_nc( Fixture *fixture, gconstpointer pData )
433 {
434  /* basically we test static function kvp_frame_add_value_nc
435  * if path not exist it's created
436  * if gslist exist on the path new value added to the list
437  * if any other value exist it's converted to gslist and newvalue added
438  */
439  KvpFrame *test_frame = NULL,
440  *test_frame2 = NULL,
441  *test_frame3 = NULL,
442  *result_frame = NULL;
443  KvpValue *result_value = NULL;
444  GList *result_list = NULL;
445 
446  g_assert( fixture->frame );
447  g_assert( kvp_frame_is_empty( fixture->frame ) );
448 
449  g_test_message( "Test when path does not exist it is created" );
450  result_frame = kvp_frame_get_frame( fixture->frame, "/test/test2/test3" );
451  g_assert( !result_frame );
452  test_frame = kvp_frame_new();
453  kvp_frame_add_frame_nc( fixture->frame, "/test/test2/test3", test_frame );
454  result_frame = kvp_frame_get_frame( fixture->frame, "/test/test2/test3" );
455  g_assert( result_frame );
456  g_assert( result_frame == test_frame ); /* no copying done */
457  result_frame = kvp_frame_get_frame( fixture->frame, "/test/test2" );
458  g_assert( result_frame != test_frame );
459  result_frame = kvp_frame_get_frame( fixture->frame, "/test" );
460  g_assert( result_frame != test_frame );
461 
462  g_test_message( "Test when value exist on the path it's converted to bag and new value added" );
463  test_frame2 = kvp_frame_new();
464  kvp_frame_add_frame_nc( fixture->frame, "/test/test2/test3", test_frame2 );
465  result_value = kvp_frame_get_value( fixture->frame, "/test/test2/test3" );
466  result_list = kvp_value_get_glist( result_value );
467  g_assert( result_list );
468  g_assert_cmpint( g_list_length( result_list ), == , 2 );
469  result_value = g_list_first( result_list )->data;
470  g_assert( result_value );
471  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_FRAME );
472  g_assert( kvp_value_get_frame( result_value ) == test_frame );
473  result_value = g_list_next( result_list )->data;
474  g_assert( result_value );
475  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_FRAME );
476  g_assert( kvp_value_get_frame( result_value ) == test_frame2 );
477 
478  g_test_message( "Test when bag exists on the path new values are added to it" );
479  test_frame3 = kvp_frame_new();
480  kvp_frame_add_frame_nc( fixture->frame, "/test/test2/test3", test_frame3 );
481  result_value = kvp_frame_get_value( fixture->frame, "/test/test2/test3" );
482  g_assert( result_list == kvp_value_get_glist( result_value ) ); /* same list used */
483  g_assert_cmpint( g_list_length( result_list ), == , 3 );
484  result_value = g_list_first( result_list )->data;
485  g_assert( result_value );
486  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_FRAME );
487  g_assert( kvp_value_get_frame( result_value ) == test_frame );
488  result_value = g_list_next( result_list )->data;
489  g_assert( result_value );
490  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_FRAME );
491  g_assert( kvp_value_get_frame( result_value ) == test_frame2 );
492  result_value = g_list_last( result_list )->data;
493  g_assert( result_value );
494  g_assert( kvp_value_get_type( result_value ) == KVP_TYPE_FRAME );
495  g_assert( kvp_value_get_frame( result_value ) == test_frame3 );
496 }
497 
498 static void
499 test_kvp_value_copy( void )
500 {
501  KvpValue *gint64_orig_value, *gint64_copy_value;
502  KvpValue *double_orig_value, *double_copy_value;
503  KvpValue *numeric_orig_value, *numeric_copy_value;
504  KvpValue *string_orig_value, *string_copy_value;
505  KvpValue *guid_orig_value, *guid_copy_value;
506  KvpValue *timespec_orig_value, *timespec_copy_value;
507  KvpValue *glist_orig_value, *glist_copy_value;
508  KvpValue *frame_orig_value, *frame_copy_value;
509 
510  /* data init */
511  gnc_numeric gnc_numeric_orig, gnc_numeric_copy;
512  GncGUID *guid_orig, *guid_copy;
513  Timespec ts_orig, ts_copy;
514  GList *list_orig, *list_copy;
515  KvpFrame *frame_orig, *frame_copy;
516 
517  gnc_numeric_orig = gnc_numeric_zero();
518  guid_orig = guid_new();
519  ts_orig.tv_sec = 1;
520  ts_orig.tv_nsec = 1;
521  list_orig = NULL;
522  list_orig = g_list_append( list_orig, kvp_value_new_string( "abcdefghijklmnop" ) );
523  frame_orig = kvp_frame_new();
524 
525  g_test_message( "Test creates original values and checks copies of them" );
526  gint64_orig_value = kvp_value_new_gint64( 2 );
527  double_orig_value = kvp_value_new_double( 3.3 );
528  numeric_orig_value = kvp_value_new_gnc_numeric( gnc_numeric_orig );
529  string_orig_value = kvp_value_new_string( "abcdefghijklmnop" );
530  guid_orig_value = kvp_value_new_guid( guid_orig );
531  timespec_orig_value = kvp_value_new_timespec( ts_orig );
532  glist_orig_value = kvp_value_new_glist( list_orig );
533  frame_orig_value = kvp_value_new_frame( frame_orig );
534  g_assert( gint64_orig_value && double_orig_value && numeric_orig_value && string_orig_value
535  && guid_orig_value && timespec_orig_value && glist_orig_value && frame_orig_value );
536 
537  /* copy values */
538  gint64_copy_value = kvp_value_copy( gint64_orig_value );
539  g_assert( gint64_copy_value );
540  g_assert( gint64_copy_value != gint64_orig_value );
541  g_assert( kvp_value_get_type( gint64_copy_value ) == KVP_TYPE_GINT64 );
542  g_assert_cmpint( kvp_value_get_gint64( gint64_copy_value ), == , 2 );
543 
544  double_copy_value = kvp_value_copy( double_orig_value );
545  g_assert( double_copy_value );
546  g_assert( double_copy_value != double_orig_value );
547  g_assert( kvp_value_get_type( double_copy_value ) == KVP_TYPE_DOUBLE );
548  g_assert_cmpfloat( kvp_value_get_double( double_copy_value ), == , 3.3 );
549 
550  numeric_copy_value = kvp_value_copy( numeric_orig_value );
551  g_assert( numeric_copy_value );
552  g_assert( numeric_copy_value != numeric_orig_value );
553  g_assert( kvp_value_get_type( numeric_copy_value ) == KVP_TYPE_NUMERIC );
554  gnc_numeric_copy = kvp_value_get_numeric( numeric_copy_value );
555  g_assert_cmpfloat( gnc_numeric_copy.num, == , gnc_numeric_orig.num );
556  g_assert_cmpfloat( gnc_numeric_copy.denom, == , gnc_numeric_orig.denom );
557 
558  string_copy_value = kvp_value_copy( string_orig_value );
559  g_assert( string_copy_value );
560  g_assert( string_copy_value != string_orig_value );
561  g_assert( kvp_value_get_type( string_copy_value ) == KVP_TYPE_STRING );
562  g_assert_cmpstr( kvp_value_get_string( string_copy_value ), == , "abcdefghijklmnop" );
563 
564  guid_copy_value = kvp_value_copy( guid_orig_value );
565  g_assert( guid_copy_value );
566  g_assert( guid_copy_value != guid_orig_value );
567  g_assert( kvp_value_get_type( guid_copy_value ) == KVP_TYPE_GUID );
568  guid_copy = kvp_value_get_guid( guid_copy_value );
569  g_assert( guid_orig != guid_copy );
570  g_assert( guid_equal( guid_orig, guid_copy ) );
571 
572  timespec_copy_value = kvp_value_copy( timespec_orig_value );
573  g_assert( timespec_copy_value );
574  g_assert( timespec_copy_value != timespec_orig_value );
575  g_assert( kvp_value_get_type( timespec_copy_value ) == KVP_TYPE_TIMESPEC );
576  ts_copy = kvp_value_get_timespec( timespec_copy_value );
577  g_assert_cmpfloat( ts_copy.tv_sec, == , ts_orig.tv_sec );
578  g_assert_cmpfloat( ts_copy.tv_nsec, == , ts_orig.tv_nsec );
579 
580  glist_copy_value = kvp_value_copy( glist_orig_value );
581  g_assert( glist_copy_value );
582  g_assert( glist_copy_value != glist_orig_value );
583  g_assert( kvp_value_get_type( glist_copy_value ) == KVP_TYPE_GLIST );
584  list_copy = kvp_value_get_glist( glist_copy_value );
585  g_assert( list_copy != list_orig );
586  g_assert_cmpint( g_list_length( list_copy ), == , g_list_length( list_orig ) );
587  g_assert_cmpint( kvp_glist_compare( list_orig, list_copy ), == , 0 );
588 
589  frame_copy_value = kvp_value_copy( frame_orig_value );
590  g_assert( frame_copy_value );
591  g_assert( frame_copy_value != frame_orig_value );
592  g_assert( kvp_value_get_type( frame_copy_value ) == KVP_TYPE_FRAME );
593  frame_copy = kvp_value_get_frame( frame_copy_value );
594  g_assert_cmpint( kvp_frame_compare( frame_orig, frame_copy ), == , 0 );
595 
596  /* destroy objects */
597  kvp_value_delete( gint64_orig_value );
598  kvp_value_delete( double_orig_value );
599  kvp_value_delete( numeric_orig_value );
600  kvp_value_delete( string_orig_value );
601  kvp_value_delete( guid_orig_value );
602  kvp_value_delete( timespec_orig_value );
603  kvp_value_delete( glist_orig_value );
604  kvp_value_delete( frame_orig_value );
605 
606  kvp_value_delete( gint64_copy_value );
607  kvp_value_delete( double_copy_value );
608  kvp_value_delete( numeric_copy_value );
609  kvp_value_delete( string_copy_value );
610  kvp_value_delete( guid_copy_value );
611  kvp_value_delete( timespec_copy_value );
612  kvp_value_delete( glist_copy_value );
613  kvp_value_delete( frame_copy_value );
614 }
615 
616 static void
617 test_kvp_glist_copy( void )
618 {
619  GList *value_list = NULL, *copy_list = NULL, *lp1 = NULL, *lp2 = NULL;
620  KvpValue *gint64_value;
621  KvpValue *double_value;
622  KvpValue *numeric_value;
623  KvpValue *string_value;
624  KvpValue *guid_value;
625  KvpValue *timespec_value;
626  KvpValue *glist_value;
627  KvpValue *frame_value;
628 
629  gnc_numeric gnc_numeric_orig;
630  GncGUID *guid_orig;
631  Timespec ts_orig;
632  GList *list_orig;
633  KvpFrame *frame_orig;
634 
635  gnc_numeric_orig = gnc_numeric_zero();
636  guid_orig = guid_new();
637  ts_orig.tv_sec = 1;
638  ts_orig.tv_nsec = 1;
639  list_orig = NULL;
640  list_orig = g_list_append( list_orig, kvp_value_new_string( "abcdefghijklmnop" ) );
641  frame_orig = kvp_frame_new();
642 
643  gint64_value = kvp_value_new_gint64( 2 );
644  double_value = kvp_value_new_double( 3.3 );
645  numeric_value = kvp_value_new_gnc_numeric( gnc_numeric_orig );
646  string_value = kvp_value_new_string( "abcdefghijklmnop" );
647  guid_value = kvp_value_new_guid( guid_orig );
648  timespec_value = kvp_value_new_timespec( ts_orig );
649  glist_value = kvp_value_new_glist( list_orig );
650  frame_value = kvp_value_new_frame( frame_orig );
651 
652  value_list = g_list_append( value_list, gint64_value );
653  value_list = g_list_append( value_list, double_value );
654  value_list = g_list_append( value_list, numeric_value );
655  value_list = g_list_append( value_list, string_value );
656  value_list = g_list_append( value_list, guid_value );
657  value_list = g_list_append( value_list, timespec_value );
658  value_list = g_list_append( value_list, glist_value );
659  value_list = g_list_append( value_list, frame_value );
660  g_assert( value_list );
661  g_assert_cmpint( g_list_length( value_list ), == , 8 );
662 
663  g_test_message( "Test list and all values are copied to new list" );
664  copy_list = kvp_glist_copy( value_list );
665  g_assert( copy_list );
666  g_assert( copy_list != value_list );
667  g_assert_cmpint( g_list_length( copy_list ), == , 8 );
668  lp1 = value_list;
669  lp2 = copy_list;
670  while (lp1 && lp2)
671  {
672  KvpValue *v1 = (KvpValue *) lp1->data;
673  KvpValue *v2 = (KvpValue *) lp2->data;
674  g_assert( v1 != v2 );
675  g_assert_cmpint( kvp_value_compare(v1, v2), == , 0 );
676  lp1 = lp1->next;
677  lp2 = lp2->next;
678  }
679  g_assert_cmpint( kvp_glist_compare( value_list, copy_list ), == , 0 );
680 
681  /* destroy */
682  kvp_glist_delete( value_list );
683  kvp_glist_delete( copy_list );
684 }
685 
686 static void
687 test_kvp_glist_compare( void )
688 {
689  GList *list1 = NULL, *list2 = NULL;
690 
691  KvpValue *gint64_value;
692  KvpValue *double_value;
693  KvpValue *numeric_value;
694  KvpValue *string_value;
695  KvpValue *guid_value;
696  KvpValue *timespec_value;
697  KvpValue *glist_value;
698  KvpValue *frame_value;
699 
700  gnc_numeric gnc_numeric_orig;
701  GncGUID *guid_orig;
702  Timespec ts_orig;
703  GList *list_orig;
704  KvpFrame *frame_orig;
705 
706  gnc_numeric_orig = gnc_numeric_zero();
707  guid_orig = guid_new();
708  ts_orig.tv_sec = 1;
709  ts_orig.tv_nsec = 1;
710  list_orig = NULL;
711  list_orig = g_list_append( list_orig, kvp_value_new_string( "abcdefghijklmnop" ) );
712  frame_orig = kvp_frame_new();
713 
714  gint64_value = kvp_value_new_gint64( 2 );
715  double_value = kvp_value_new_double( 3.3 );
716  numeric_value = kvp_value_new_gnc_numeric( gnc_numeric_orig );
717  string_value = kvp_value_new_string( "abcdefghijklmnop" );
718  guid_value = kvp_value_new_guid( guid_orig );
719  timespec_value = kvp_value_new_timespec( ts_orig );
720  glist_value = kvp_value_new_glist( list_orig );
721  frame_value = kvp_value_new_frame( frame_orig );
722 
723  /* init list 1 */
724  list1 = g_list_append( list1, gint64_value );
725  list1 = g_list_append( list1, double_value );
726  list1 = g_list_append( list1, numeric_value );
727  list1 = g_list_append( list1, string_value );
728  list1 = g_list_append( list1, guid_value );
729  list1 = g_list_append( list1, timespec_value );
730  list1 = g_list_append( list1, glist_value );
731  list1 = g_list_append( list1, frame_value );
732  g_assert( list1 );
733  g_assert_cmpint( g_list_length( list1 ), == , 8 );
734 
735  g_test_message( "Test when list is the same" );
736  list2 = list1;
737  g_assert_cmpint( kvp_glist_compare( list1, list2 ), == , 0 );
738 
739  g_test_message( "Test when list1 is null" );
740  g_assert_cmpint( kvp_glist_compare( NULL, list2 ), == , -1 );
741 
742  g_test_message( "Test when list2 is null" );
743  g_assert_cmpint( kvp_glist_compare( list1, NULL ), == , 1 );
744 
745  g_test_message( "Copy list and test they are equal" );
746  list2 = kvp_glist_copy( list1 );
747  g_assert( list1 != list2 );
748  g_assert_cmpint( g_list_length( list1 ), == , g_list_length( list2 ) );
749  g_assert_cmpint( kvp_glist_compare( list1, list2 ), == , 0 );
750 
751  g_test_message( "Test when list 1 is shorter lists are not equal" );
752  list1 = g_list_remove( list1, frame_value );
753  g_assert_cmpint( g_list_length( list1 ), == , 7 );
754  g_assert_cmpint( g_list_length( list2 ), == , 8 );
755  g_assert_cmpint( kvp_glist_compare( list1, list2 ), == , -1 );
756 
757  g_test_message( "Test when list 2 is shorter lists are not equal" );
758  list1 = g_list_append( list1, frame_value );
759  list1 = g_list_append( list1, frame_value );
760  g_assert_cmpint( g_list_length( list1 ), == , 9 );
761  g_assert_cmpint( g_list_length( list2 ), == , 8 );
762  g_assert_cmpint( kvp_glist_compare( list1, list2 ), == , 1 );
763 
764  g_test_message( "Test when data is not equal lists are not equal" );
765  list1 = g_list_remove( list1, frame_value );
766  g_assert_cmpint( g_list_length( list1 ), == , g_list_length( list2 ) );
767  g_assert_cmpint( kvp_glist_compare( list1, list2 ), == , 0 );
768  list1 = g_list_remove( list1, gint64_value );
769  kvp_value_delete( gint64_value );
770  list1 = g_list_prepend( list1, kvp_value_new_gint64( 5 ) );
771  g_assert_cmpint( g_list_length( list1 ), == , g_list_length( list2 ) );
772  g_assert_cmpint( kvp_glist_compare( list1, list2 ), != , 0 );
773 
774  /* delete lists */
775  kvp_glist_delete( list1 );
776  kvp_glist_delete( list2 );
777 }
778 
779 static void
780 test_kvp_value_compare( void )
781 {
782  KvpValue *gint64_orig_value, *gint64_copy_value;
783  KvpValue *double_orig_value, *double_copy_value;
784  KvpValue *numeric_orig_value, *numeric_copy_value;
785  KvpValue *string_orig_value, *string_copy_value;
786  KvpValue *guid_orig_value, *guid_copy_value;
787  KvpValue *timespec_orig_value, *timespec_copy_value;
788  KvpValue *glist_orig_value, *glist_copy_value;
789  KvpValue *frame_orig_value, *frame_copy_value;
790 
791  /* data init */
792  gnc_numeric gnc_numeric_orig, gnc_numeric_copy;
793  GncGUID *guid_orig, *guid_copy;
794  Timespec ts_orig, ts_copy;
795  GList *list_orig, *list_copy;
796  KvpFrame *frame_orig, *frame_copy;
797 
798  gnc_numeric_orig = gnc_numeric_zero();
799  gnc_numeric_copy = gnc_numeric_zero();
800  guid_orig = guid_new();
801  guid_copy = guid_new();
802  ts_orig.tv_sec = 1;
803  ts_orig.tv_nsec = 1;
804  ts_copy.tv_sec = 2;
805  ts_copy.tv_nsec = 2;
806  list_orig = NULL;
807  list_orig = g_list_append( list_orig, kvp_value_new_string( "abcdefghijklmnop" ) );
808  list_copy = NULL;
809  list_copy = g_list_append( list_copy, kvp_value_new_string( "abcdefg" ) );
810  frame_orig = kvp_frame_new();
811  frame_copy = kvp_frame_new();
812 
813  gint64_orig_value = kvp_value_new_gint64( 2 );
814  gint64_copy_value = kvp_value_new_gint64( 5 );
815  double_orig_value = kvp_value_new_double( 3.3 );
816  double_copy_value = kvp_value_new_double( 3.5 );
817  numeric_orig_value = kvp_value_new_gnc_numeric( gnc_numeric_orig );
818  numeric_copy_value = kvp_value_new_gnc_numeric( gnc_numeric_copy );
819  string_orig_value = kvp_value_new_string( "abcdefghijklmnop" );
820  string_copy_value = kvp_value_new_string( "abcdefghijklmnop" );
821  guid_orig_value = kvp_value_new_guid( guid_orig );
822  guid_copy_value = kvp_value_new_guid( guid_copy );
823  timespec_orig_value = kvp_value_new_timespec( ts_orig );
824  timespec_copy_value = kvp_value_new_timespec( ts_copy );
825  glist_orig_value = kvp_value_new_glist( list_orig );
826  glist_copy_value = kvp_value_new_glist( list_copy );
827  frame_orig_value = kvp_value_new_frame( frame_orig );
828  frame_copy_value = kvp_value_new_frame( frame_copy );
829 
830  g_test_message( "Test the same kvpvalue is equal" );
831  g_assert_cmpint( kvp_value_compare( gint64_orig_value, gint64_orig_value ), == , 0 );
832 
833  g_test_message( "Test first value is null" );
834  g_assert_cmpint( kvp_value_compare( NULL, gint64_orig_value ), == , -1 );
835 
836  g_test_message( "Test second value is null" );
837  g_assert_cmpint( kvp_value_compare( gint64_orig_value, NULL ), == , 1 );
838 
839  g_test_message( "Test diffrent data types first is lesser" );
840  g_assert_cmpint( kvp_value_compare( gint64_orig_value, double_orig_value ), == , -1 );
841 
842  g_test_message( "Test diffrent data types second is lesser" );
843  g_assert_cmpint( kvp_value_compare( double_orig_value, gint64_orig_value ), == , 1 );
844 
845  /* testing all different cases of data equality is not the aim
846  * of this test. Rather we check that all data types are being compared.
847  */
848  g_test_message( "Test different kvpvalues of all the types" );
849  g_assert_cmpint( kvp_value_compare( gint64_orig_value, gint64_copy_value ), == , -1 );
850  g_assert_cmpint( kvp_value_compare( gint64_copy_value, gint64_orig_value ), == , 1 );
851  g_assert_cmpint( kvp_value_compare( double_orig_value, double_copy_value ), == , -1 );
852  g_assert_cmpint( kvp_value_compare( numeric_orig_value, numeric_copy_value ), == , gnc_numeric_compare( gnc_numeric_orig, gnc_numeric_copy ) );
853  g_assert_cmpint( kvp_value_compare( string_orig_value, string_copy_value ), == , strcmp( "abcdefghijklmnop", "abcdefghijklmnop" ) );
854  g_assert_cmpint( kvp_value_compare( guid_orig_value, guid_copy_value ), == , guid_compare( guid_orig, guid_copy ) );
855  g_assert_cmpint( kvp_value_compare( timespec_orig_value, timespec_copy_value ), == , timespec_cmp( &ts_orig, &ts_copy ) );
856  g_assert_cmpint( kvp_value_compare( glist_orig_value, glist_copy_value ), == , kvp_glist_compare( list_orig, list_copy ) );
857  g_assert_cmpint( kvp_value_compare( frame_orig_value, frame_copy_value ), == , kvp_frame_compare( frame_orig, frame_copy ) );
858 
859  /* destroy objects */
860  kvp_value_delete( gint64_orig_value );
861  kvp_value_delete( double_orig_value );
862  kvp_value_delete( numeric_orig_value );
863  kvp_value_delete( string_orig_value );
864  kvp_value_delete( guid_orig_value );
865  kvp_value_delete( timespec_orig_value );
866  kvp_value_delete( glist_orig_value );
867  kvp_value_delete( frame_orig_value );
868 
869  kvp_value_delete( gint64_copy_value );
870  kvp_value_delete( double_copy_value );
871  kvp_value_delete( numeric_copy_value );
872  kvp_value_delete( string_copy_value );
873  kvp_value_delete( guid_copy_value );
874  kvp_value_delete( timespec_copy_value );
875  kvp_value_delete( glist_copy_value );
876  kvp_value_delete( frame_copy_value );
877 }
878 
879 static void
880 test_kvp_value_new_foo_nc( void )
881 {
882  KvpValue *glist_value_nc, *frame_value_nc;
883  void *val;
884  guint64 size;
885  GList *list = NULL;
886  KvpFrame *frame = NULL;
887 
888  g_test_message( "Test new glist is not copied" );
889  list = g_list_append( list, kvp_value_new_gint64( 2 ) );
890  g_assert_cmpint( g_list_length( list ), == , 1 );
891  glist_value_nc = kvp_value_new_glist_nc( list );
892  g_assert( glist_value_nc );
893  g_assert( kvp_value_get_type( glist_value_nc ) == KVP_TYPE_GLIST );
894  g_assert( kvp_value_get_glist( glist_value_nc ) == list );
895 
896  g_test_message( "Test new frame is not copied" );
897  frame = kvp_frame_new();
898  frame_value_nc = kvp_value_new_frame_nc( frame );
899  g_assert( frame_value_nc );
900  g_assert( kvp_value_get_type( frame_value_nc ) == KVP_TYPE_FRAME );
901  g_assert( kvp_value_get_frame( frame_value_nc ) == frame );
902 
903  kvp_value_delete( glist_value_nc );
904  kvp_value_delete( frame_value_nc );
905 }
906 
907 static void
908 test_kvp_frame_compare( Fixture *fixture, gconstpointer pData )
909 {
910  KvpFrame *cmp_frame = NULL;
911 
912  cmp_frame = kvp_frame_new();
913  g_assert( cmp_frame );
914 
915  g_test_message( "Test the same frame is equal with itself" );
916  g_assert_cmpint( kvp_frame_compare( fixture->frame, fixture->frame ), == , 0 );
917 
918  g_test_message( "Test first frame null second not null" );
919  g_assert_cmpint( kvp_frame_compare( NULL, fixture->frame ), == , -1 );
920 
921  g_test_message( "Test first frame not null second null" );
922  g_assert_cmpint( kvp_frame_compare( fixture->frame, NULL ), == , 1 );
923 
924  g_test_message( "Test first frame is empty second not empty" );
925  kvp_frame_set_gint64( fixture->frame, "/test/test2", 64 );
926  g_assert( !kvp_frame_is_empty( fixture->frame ) );
927  g_assert( kvp_frame_is_empty( cmp_frame ) );
928  g_assert_cmpint( kvp_frame_compare( cmp_frame, fixture->frame ), == , -1 );
929 
930  g_test_message( "Test first frame is not empty second is empty" );
931  g_assert_cmpint( kvp_frame_compare( fixture->frame, cmp_frame ), == , 1 );
932 
933  g_test_message( "Test when frames are equal" );
934  kvp_frame_set_gint64( cmp_frame, "/test/test2", 64 );
935  g_assert( !kvp_frame_is_empty( cmp_frame ) );
936  g_assert_cmpint( kvp_frame_compare( fixture->frame, cmp_frame ), == , 0 );
937 
938  g_test_message( "Test when frames have equal data but second frame has additional slot set" );
939  kvp_frame_set_string( fixture->frame, "/test/test3", "abcdefghijklmnop" );
940  g_assert_cmpint( kvp_frame_compare( cmp_frame, fixture->frame ), == , -1 );
941 
942  g_test_message( "Test when frames have equal data but first frame has additional slot set" );
943  g_assert_cmpint( kvp_frame_compare( fixture->frame, cmp_frame ), == , 1 );
944 
945  g_test_message( "Test when frames have equal number of slots second frame has different data in one slot" );
946  kvp_frame_set_string( cmp_frame, "/test/test3", "abcdefg" );
947  g_assert_cmpint( kvp_frame_compare( cmp_frame, fixture->frame ), < , 0 );
948 
949  g_test_message( "Test when frames have equal number of slots second frame has different data in one slot" );
950  g_assert_cmpint( kvp_frame_compare( fixture->frame, cmp_frame ), > , 0 );
951 
952  kvp_frame_delete( cmp_frame );
953 }
954 
955 static void
956 test_kvp_value_to_string( void )
957 {
958  gchar guidstr[GUID_ENCODING_LENGTH+1];
959  gchar *str_tmp2, *str_tmp3;
960  gchar *result;
961  KvpValue *gint64_value;
962  KvpValue *double_value;
963  KvpValue *numeric_value;
964  KvpValue *string_value;
965  KvpValue *guid_value;
966  KvpValue *timespec_value;
967  KvpValue *glist_value;
968  KvpValue *frame_value;
969 
970  gnc_numeric gnc_numeric_orig;
971  GncGUID *guid_orig;
972  Timespec ts_orig;
973  GList *list_orig;
974  KvpFrame *frame_orig;
975 
976  gnc_numeric_orig = gnc_numeric_zero();
977  guid_orig = guid_new();
978  ts_orig.tv_sec = 1;
979  ts_orig.tv_nsec = 1;
980  list_orig = NULL;
981  list_orig = g_list_append( list_orig, kvp_value_new_string( "abcdefghijklmnop" ) );
982  frame_orig = kvp_frame_new();
983 
984  gint64_value = kvp_value_new_gint64( 2 );
985  double_value = kvp_value_new_double( 3.3 );
986  numeric_value = kvp_value_new_gnc_numeric( gnc_numeric_orig );
987  string_value = kvp_value_new_string( "abcdefghijklmnop" );
988  guid_value = kvp_value_new_guid( guid_orig );
989  timespec_value = kvp_value_new_timespec( ts_orig );
990  glist_value = kvp_value_new_glist( list_orig );
991  frame_value = kvp_value_new_frame( frame_orig );
992 
993  g_test_message( "Test value string representation with different data types" );
994  result = kvp_value_to_string( gint64_value );
995  g_assert( result );
996  g_assert_cmpstr( result, == , "KVP_VALUE_GINT64(2)" );
997  g_free( result );
998 
999  result = kvp_value_to_string( double_value );
1000  g_assert( result );
1001  g_assert_cmpstr( result, == , "KVP_VALUE_DOUBLE(3.3)" );
1002  g_free( result );
1003 
1004  result = kvp_value_to_string( numeric_value );
1005  g_assert( result );
1006  g_assert_cmpstr( result, == , "KVP_VALUE_NUMERIC(0/1)" );
1007  g_free( result );
1008 
1009  result = kvp_value_to_string( string_value );
1010  g_assert( result );
1011  g_assert_cmpstr( result, == , "KVP_VALUE_STRING(abcdefghijklmnop)" );
1012  g_free( result );
1013 
1014  result = kvp_value_to_string( guid_value );
1015  g_assert( result );
1016  guid_to_string_buff( kvp_value_get_guid( guid_value ), guidstr);
1017  str_tmp2 = g_strdup_printf("KVP_VALUE_GUID(%s)", guidstr);
1018  g_assert_cmpstr( result, == , str_tmp2 );
1019  g_free( result );
1020  g_free( str_tmp2 );
1021 
1022  result = kvp_value_to_string( timespec_value );
1023  g_assert( result );
1024  str_tmp2 = g_new0 (char, 40);
1025  gnc_timespec_to_iso8601_buff( kvp_value_get_timespec( timespec_value ), str_tmp2 );
1026  str_tmp3 = g_strdup_printf("KVP_VALUE_TIMESPEC(%s)", str_tmp2);
1027  g_assert_cmpstr( result, == , str_tmp3 );
1028  g_free( result );
1029  g_free( str_tmp2 );
1030  g_free( str_tmp3 );
1031 
1032  result = kvp_value_to_string( glist_value );
1033  g_assert( result );
1034  g_assert_cmpstr( result, == , "KVP_VALUE_GLIST([ KVP_VALUE_STRING(abcdefghijklmnop), ])" );
1035  g_free( result );
1036 
1037  result = kvp_value_to_string( frame_value );
1038  g_assert( result );
1039  g_assert_cmpstr( result, == , "KVP_VALUE_FRAME({\n}\n)" );
1040  g_free( result );
1041 
1042  kvp_value_delete( gint64_value );
1043  kvp_value_delete( double_value );
1044  kvp_value_delete( numeric_value );
1045  kvp_value_delete( string_value );
1046  kvp_value_delete( guid_value );
1047  kvp_value_delete( timespec_value );
1048  kvp_value_delete( glist_value );
1049  kvp_value_delete( frame_value );
1050 }
1051 
1052 static void
1053 test_kvp_frame_to_string( Fixture *fixture, gconstpointer pData )
1054 {
1055  gchar *result;
1056  gnc_numeric test_gnc_numeric;
1057  GncGUID *test_guid;
1058  Timespec test_ts;
1059  KvpFrame *test_frame;
1060 
1061  test_gnc_numeric = gnc_numeric_zero();
1062  test_guid = guid_new();
1063  test_ts.tv_sec = 1;
1064  test_ts.tv_nsec = 1;
1065  test_frame = kvp_frame_new();
1066 
1067  g_assert( fixture->frame );
1068  g_assert( kvp_frame_is_empty( fixture->frame ) );
1069 
1070  g_test_message( "Test empty frame" );
1071  result = kvp_frame_to_string( fixture->frame );
1072  g_assert_cmpstr( result, == , "{\n}\n" );
1073  g_free( result );
1074 
1075  /* slots can be randomly distributed in hash table
1076  * instead of checking the whole return string we rather check if certain entries exist in it
1077  */
1078  g_test_message( "Test with all data types and nested frames" );
1079  kvp_frame_set_gint64( fixture->frame, "/gint64-type", 2 );
1080  result = kvp_frame_to_string( fixture->frame );
1081  g_assert( g_strrstr( result, " gint64-type => KVP_VALUE_GINT64(2),\n" ) != NULL );
1082  g_free( result );
1083 
1084  kvp_frame_set_double( fixture->frame, "/double-type", 3.3 );
1085  result = kvp_frame_to_string( fixture->frame );
1086  g_assert( g_strrstr( result, " double-type => KVP_VALUE_DOUBLE(3.3),\n" ) != NULL );
1087  g_free( result );
1088 
1089  kvp_frame_set_numeric( fixture->frame, "/numeric-type", test_gnc_numeric );
1090  result = kvp_frame_to_string( fixture->frame );
1091  g_assert( g_strrstr( result, " numeric-type => KVP_VALUE_NUMERIC(0/1),\n" ) != NULL );
1092  g_free( result );
1093 
1094  kvp_frame_set_timespec( fixture->frame, "/timespec-type", test_ts );
1095  result = kvp_frame_to_string( fixture->frame );
1096  g_assert( g_strrstr( result, " timespec-type => KVP_VALUE_TIMESPEC" ) != NULL );
1097  g_free( result );
1098 
1099  kvp_frame_set_string( fixture->frame, "/string-type", "abcdefghijklmnop" );
1100  result = kvp_frame_to_string( fixture->frame );
1101  g_assert( g_strrstr( result, " string-type => KVP_VALUE_STRING(abcdefghijklmnop),\n" ) != NULL );
1102  g_free( result );
1103 
1104  kvp_frame_set_guid( fixture->frame, "/guid-type", test_guid );
1105  result = kvp_frame_to_string( fixture->frame );
1106  g_assert( g_strrstr( result, " guid-type => KVP_VALUE_GUID" ) != NULL );
1107  g_free( result );
1108 
1109  kvp_frame_set_frame( fixture->frame, "/nested/frame-type", test_frame );
1110  result = kvp_frame_to_string( fixture->frame );
1111  g_assert( g_strrstr( result, " nested => KVP_VALUE_FRAME({\n frame-type => KVP_VALUE_FRAME({\n}\n),\n}\n),\n" ) != NULL );
1112  g_free( result );
1113 }
1114 
1115 static void
1116 test_kvp_frame_set_slot_path( Fixture *fixture, gconstpointer pData )
1117 {
1118  KvpValue *input_value, *output_value;
1119 
1120  g_assert( fixture->frame );
1121  g_assert( kvp_frame_is_empty( fixture->frame ) );
1122 
1123  g_test_message( "Test with a simple value added to the empty frame" );
1124  input_value = kvp_value_new_gint64( 2 );
1125  kvp_frame_set_slot_path( fixture->frame, input_value, "test", NULL );
1126  output_value = kvp_frame_get_slot_path( fixture->frame, "test", NULL );
1127  g_assert( output_value );
1128  g_assert( input_value != output_value ); /* copied */
1129  g_assert_cmpint( kvp_value_compare( output_value, input_value ), == , 0 );
1130  kvp_value_delete( input_value );
1131 
1132  g_test_message( "Test when value is being replaced" );
1133  input_value = kvp_value_new_double( 3.3 );
1134  kvp_frame_set_slot_path( fixture->frame, input_value, "test", NULL );
1135  output_value = kvp_frame_get_slot_path( fixture->frame, "test", NULL );
1136  g_assert( output_value );
1137  g_assert( input_value != output_value ); /* copied */
1138  g_assert_cmpint( kvp_value_compare( output_value, input_value ), == , 0 ); /* old value removed */
1139  kvp_value_delete( input_value );
1140 
1141  g_test_message( "Test when existing path elements are not frames" );
1142  input_value = kvp_value_new_string( "abcdefghijklmnop" );
1143  kvp_frame_set_slot_path( fixture->frame, input_value, "test", "test2", NULL );
1144  g_assert( kvp_frame_get_slot_path( fixture->frame, "test2", NULL ) == NULL );/* was not added */
1145  g_assert_cmpint( kvp_value_compare( output_value, kvp_frame_get_slot_path( fixture->frame, "test", NULL ) ), == , 0 ); /* nothing changed */
1146  kvp_value_delete( input_value );
1147 
1148  g_test_message( "Test frames are created along the path when needed" );
1149  input_value = kvp_value_new_string( "abcdefghijklmnop" );
1150  kvp_frame_set_slot_path( fixture->frame, input_value, "test2", "test3", NULL );
1151  output_value = kvp_frame_get_slot_path( fixture->frame, "test2", NULL );
1152  g_assert( output_value );
1153  g_assert( kvp_value_get_type( output_value ) == KVP_TYPE_FRAME );
1154  output_value = kvp_frame_get_slot_path( fixture->frame, "test2", "test3", NULL );
1155  g_assert( output_value );
1156  g_assert( input_value != output_value ); /* copied */
1157  g_assert_cmpint( kvp_value_compare( output_value, input_value ), == , 0 );
1158  kvp_value_delete( input_value );
1159 }
1160 
1161 static void
1162 test_kvp_frame_set_slot_path_gslist( Fixture *fixture, gconstpointer pData )
1163 {
1164  /* similar to previous test except path is passed as GSList*/
1165  GSList *path_list = NULL;
1166  KvpValue *input_value, *output_value;
1167 
1168  g_assert( fixture->frame );
1169  g_assert( kvp_frame_is_empty( fixture->frame ) );
1170 
1171  g_test_message( "Test with a simple value added to the empty frame" );
1172  path_list = g_slist_append( path_list, "test" );
1173  input_value = kvp_value_new_gint64( 2 );
1174  kvp_frame_set_slot_path_gslist( fixture->frame, input_value, path_list );
1175  output_value = kvp_frame_get_slot_path( fixture->frame, "test", NULL );
1176  g_assert( output_value );
1177  g_assert( input_value != output_value ); /* copied */
1178  g_assert_cmpint( kvp_value_compare( output_value, input_value ), == , 0 );
1179  kvp_value_delete( input_value );
1180 
1181  g_test_message( "Test when value is being replaced" );
1182  input_value = kvp_value_new_double( 3.3 );
1183  kvp_frame_set_slot_path_gslist( fixture->frame, input_value, path_list );
1184  output_value = kvp_frame_get_slot_path( fixture->frame, "test", NULL );
1185  g_assert( output_value );
1186  g_assert( input_value != output_value ); /* copied */
1187  g_assert_cmpint( kvp_value_compare( output_value, input_value ), == , 0 ); /* old value removed */
1188  kvp_value_delete( input_value );
1189 
1190  g_test_message( "Test when existing path elements are not frames" );
1191  path_list = g_slist_append( path_list, "test2");
1192  input_value = kvp_value_new_string( "abcdefghijklmnop" );
1193  kvp_frame_set_slot_path_gslist( fixture->frame, input_value, path_list );
1194  g_assert( kvp_frame_get_slot_path( fixture->frame, "test2", NULL ) == NULL );/* was not added */
1195  g_assert_cmpint( kvp_value_compare( output_value, kvp_frame_get_slot_path( fixture->frame, "test", NULL ) ), == , 0 ); /* nothing changed */
1196  kvp_value_delete( input_value );
1197 
1198  g_test_message( "Test frames are created along the path when needed" );
1199  path_list = g_slist_remove( path_list, "test" );
1200  path_list = g_slist_append( path_list, "test3");
1201  input_value = kvp_value_new_string( "abcdefghijklmnop" );
1202  kvp_frame_set_slot_path_gslist( fixture->frame, input_value, path_list );
1203  output_value = kvp_frame_get_slot_path( fixture->frame, "test2", NULL );
1204  g_assert( output_value );
1205  g_assert( kvp_value_get_type( output_value ) == KVP_TYPE_FRAME );
1206  output_value = kvp_frame_get_slot_path( fixture->frame, "test2", "test3", NULL );
1207  g_assert( output_value );
1208  g_assert( input_value != output_value ); /* copied */
1209  g_assert_cmpint( kvp_value_compare( output_value, input_value ), == , 0 );
1210  kvp_value_delete( input_value );
1211 
1212  g_slist_free( path_list );
1213 }
1214 
1215 static void
1216 test_kvp_frame_replace_slot_nc( Fixture *fixture, gconstpointer pData )
1217 {
1218  KvpValue *orig_value, *orig_value2;
1219  const char ** keys;
1220  /* test indirectly static function kvp_frame_replace_slot_nc */
1221  g_assert( fixture->frame );
1222  g_assert( kvp_frame_is_empty( fixture->frame ) );
1223 
1224  g_test_message( "Test when new value is created frame hash init and value stored in hash" );
1225  orig_value = kvp_value_new_gint64( 2 );
1226  kvp_frame_set_slot( fixture->frame, "test", orig_value );
1227  g_assert( !kvp_frame_is_empty( fixture->frame ) );
1228  keys = kvp_frame_get_keys( fixture->frame );
1229  g_assert( keys );
1230  g_assert( !keys[1] );
1231  g_assert( orig_value != kvp_frame_get_value( fixture->frame, keys[0] ) );
1232  g_assert_cmpint( kvp_value_compare( kvp_frame_get_value( fixture->frame, keys[0] ), orig_value ), ==, 0 );
1233  g_free( keys );
1234 
1235  g_test_message( "Test when value is replaced" );
1236  orig_value2 = kvp_value_new_gint64( 5 );
1237  kvp_frame_set_slot( fixture->frame, "test", orig_value2 );
1238  keys = kvp_frame_get_keys( fixture->frame );
1239  g_assert( keys );
1240  g_assert( !keys[1] );
1241  g_assert( orig_value != kvp_frame_get_value( fixture->frame, keys[0] ) );
1242  g_assert_cmpint( kvp_value_compare( kvp_frame_get_value( fixture->frame, keys[0] ), orig_value2 ), ==, 0 );
1243  g_assert_cmpint( kvp_value_compare( kvp_frame_get_value( fixture->frame, keys[0] ), orig_value ), !=, 0 );
1244  g_free( keys );
1245 
1246  kvp_value_delete( orig_value );
1247  kvp_value_delete( orig_value2 );
1248 }
1249 
1250 static void
1251 test_get_trailer_make( Fixture *fixture, gconstpointer pData )
1252 {
1253  char *last_key = NULL;
1254  KvpValue *frame_value = NULL;
1255  KvpFrame *frame = NULL, *frame2 = NULL;
1256 
1257  g_test_message( "Test null frame and empty string checks" );
1258  g_assert( p_get_trailer_make( NULL, "test", &last_key ) == NULL );
1259  g_assert( !last_key );
1260  g_assert( p_get_trailer_make( fixture->frame, NULL, &last_key ) == NULL );
1261  g_assert( !last_key );
1262  g_assert( p_get_trailer_make( fixture->frame, "", &last_key ) == NULL );
1263  g_assert( !last_key );
1264 
1265  g_test_message( "Test single frame on the path with no slash" );
1266  g_assert( p_get_trailer_make( fixture->frame, "test", &last_key ) == fixture->frame );
1267  g_assert_cmpstr( last_key, == , "test" );
1268 
1269  g_test_message( "Test single frame on the path with slash" );
1270  last_key = NULL;
1271  g_assert( p_get_trailer_make( fixture->frame, "/test", &last_key ) == fixture->frame );
1272  g_assert_cmpstr( last_key, == , "test" );
1273 
1274  g_test_message( "Test path of trailing slash" );
1275  last_key = NULL;
1276  g_assert( p_get_trailer_make( fixture->frame, "test/", &last_key ) == NULL );
1277  g_assert( !last_key );
1278 
1279  g_test_message( "Test path of two entries: frame for test should be created" );
1280  /* test is considered to be last frame on the path
1281  * and it is returned. Currently it doesn't exist and will be created
1282  * test2 is stripped away and returned as last entry of the path
1283  */
1284  last_key = NULL;
1285  frame = p_get_trailer_make( fixture->frame, "/test/test2", &last_key );
1286  g_assert( frame );
1287  g_assert( frame != fixture->frame );
1288  frame_value = kvp_frame_get_slot( fixture->frame, "test" );
1289  g_assert( frame_value );
1290  g_assert( kvp_value_get_frame( frame_value ) == frame );
1291  frame_value = kvp_frame_get_slot( frame, "test2" );
1292  g_assert( !frame_value );
1293  g_assert_cmpstr( last_key, == , "test2" );
1294 
1295  g_test_message( "Test path of two entries: test frame already exist" );
1296  /* here test frame already exist and should be returned
1297  */
1298  last_key = NULL;
1299  g_assert( frame == p_get_trailer_make( fixture->frame, "/test/test2", &last_key ) );
1300  g_assert_cmpstr( last_key, == , "test2" );
1301 
1302  g_test_message( "Test path of three entries: neither frame exist" );
1303  /* test3 and test4 frames will be created. test4 will be created inside test3 frame
1304  * while test3 inside fixture->frame. test4 will be returned
1305  * test5 stripped away and returned in last_key
1306  */
1307  last_key = NULL;
1308  frame = p_get_trailer_make( fixture->frame, "/test3/test4/test5", &last_key );
1309  g_assert( frame );
1310  g_assert( frame != fixture->frame );
1311  frame_value = kvp_frame_get_slot( fixture->frame, "test3" );
1312  g_assert( frame_value );
1313  frame2 = kvp_value_get_frame( frame_value );
1314  g_assert( frame2 != frame );
1315  g_assert( frame2 != fixture->frame );
1316  frame_value = kvp_frame_get_slot( frame2, "test4" );
1317  g_assert( frame_value );
1318  g_assert( kvp_value_get_frame( frame_value ) == frame );
1319  frame_value = kvp_frame_get_slot( frame, "test5" );
1320  g_assert( !frame_value );
1321  g_assert_cmpstr( last_key, == , "test5" );
1322 }
1323 
1324 static void
1325 test_kvp_value_glist_to_string( Fixture *fixture, gconstpointer pData )
1326 {
1327  /*
1328  * kvp_value_glist_to_string and kvp_value_to_string call each other
1329  */
1330  GList *value_list = NULL;
1331  gchar *result;
1332 
1333  gnc_numeric gnc_numeric_orig;
1334  GList *list_orig;
1335  KvpFrame *frame_orig;
1336 
1337  gnc_numeric_orig = gnc_numeric_zero();
1338  list_orig = NULL;
1339  list_orig = g_list_append( list_orig, kvp_value_new_string( "abcdefghijklmnop" ) );
1340  frame_orig = kvp_frame_new();
1341 
1342  g_test_message( "Test empty list" );
1343  result = glist_to_string( value_list );
1344  g_assert_cmpstr( result, == , "" );
1345  g_free( result );
1346 
1347  g_test_message( "Test list with simple and complex values" );
1348  value_list = g_list_append( value_list, kvp_value_new_gint64( 2 ) );
1349  value_list = g_list_append( value_list, kvp_value_new_double( 3.3 ) );
1350  value_list = g_list_append( value_list, kvp_value_new_gnc_numeric( gnc_numeric_orig ) );
1351  value_list = g_list_append( value_list, kvp_value_new_string( "abcdefghijklmnop" ) );
1352  value_list = g_list_append( value_list, kvp_value_new_glist( list_orig ) );
1353  value_list = g_list_append( value_list, kvp_value_new_frame( frame_orig ) );
1354  g_assert( value_list );
1355  g_assert_cmpint( g_list_length( value_list ), == , 6 );
1356  result = glist_to_string( value_list );
1357 
1358  g_assert_cmpstr( result, == , "KVP_VALUE_GLIST([ KVP_VALUE_GINT64(2), KVP_VALUE_DOUBLE(3.3), KVP_VALUE_NUMERIC(0/1), KVP_VALUE_STRING(abcdefghijklmnop), KVP_VALUE_GLIST([ KVP_VALUE_STRING(abcdefghijklmnop), ]), KVP_VALUE_FRAME({\n}\n), ])" );
1359  g_free( result );
1360 
1361  kvp_glist_delete( value_list );
1362 }
1363 
1364 static void
1365 test_get_or_make( Fixture *fixture, gconstpointer pData )
1366 {
1367  KvpFrame *test_frame = NULL;
1368 
1369  g_assert( fixture->frame );
1370  g_assert( kvp_frame_is_empty( fixture->frame ) );
1371 
1372  g_test_message( "Test new frame is created" );
1373  test_frame = p_get_or_make( fixture->frame, "test" );
1374  g_assert( test_frame );
1375  g_assert( test_frame != fixture->frame );
1376  g_assert( kvp_frame_get_frame( fixture->frame, "test" ) == test_frame );
1377 
1378  g_test_message( "Test existing frame is returned" );
1379  g_assert( test_frame == p_get_or_make( fixture->frame, "test" ) );
1380  g_assert( kvp_frame_get_frame( fixture->frame, "test" ) == test_frame );
1381 }
1382 
1383 static void
1384 test_kvp_frame_get_frame_or_null_slash_trash( Fixture *fixture, gconstpointer pData )
1385 {
1386  g_test_message( "Test null checks" );
1387  g_assert( p_kvp_frame_get_frame_or_null_slash_trash( NULL, "test" ) == NULL );
1388  g_assert( p_kvp_frame_get_frame_or_null_slash_trash( fixture->frame, NULL ) == NULL );
1389 
1390  g_test_message( "Test single slash and trailing slash path" );
1391  g_assert( p_kvp_frame_get_frame_or_null_slash_trash( fixture->frame, "/" ) == fixture->frame );
1392  g_assert( p_kvp_frame_get_frame_or_null_slash_trash( fixture->frame, "////" ) == fixture->frame );
1393 
1394  g_test_message( "Test non existing path" );
1395  g_assert( p_kvp_frame_get_frame_or_null_slash_trash( fixture->frame, "/test" ) == NULL );
1396 
1397  g_test_message( "Test existing path when value is not frame" );
1398  kvp_frame_set_gint64( fixture->frame, "/test", 2 );
1399  g_assert( p_kvp_frame_get_frame_or_null_slash_trash( fixture->frame, "/test" ) == NULL );
1400 
1401  g_test_message( "Test existing path when value is frame" );
1402  kvp_frame_set_frame( fixture->frame, "/test2", kvp_frame_new() );
1403  g_assert( p_kvp_frame_get_frame_or_null_slash_trash( fixture->frame, "/test2" ) != NULL );
1404 }
1405 
1406 static void
1407 test_get_trailer_or_null( Fixture *fixture, gconstpointer pData )
1408 {
1409  char *last_key = NULL;
1410  KvpFrame *frame = NULL;
1411  const KvpFrame* frame2 = NULL;
1412 
1413  g_test_message( "Test null frame and empty string checks" );
1414  g_assert( p_get_trailer_or_null( NULL, "test", &last_key ) == NULL );
1415  g_assert( !last_key );
1416  g_assert( p_get_trailer_or_null( fixture->frame, NULL, &last_key ) == NULL );
1417  g_assert( !last_key );
1418  g_assert( p_get_trailer_or_null( fixture->frame, "", &last_key ) == NULL );
1419  g_assert( !last_key );
1420 
1421  g_test_message( "Test single frame on the path with no slash" );
1422  g_assert( p_get_trailer_or_null( fixture->frame, "test", &last_key ) == fixture->frame );
1423  g_assert_cmpstr( last_key, == , "test" );
1424 
1425  g_test_message( "Test single frame on the path with slash" );
1426  last_key = NULL;
1427  g_assert( p_get_trailer_or_null( fixture->frame, "/test", &last_key ) == fixture->frame );
1428  g_assert_cmpstr( last_key, == , "test" );
1429 
1430  g_test_message( "Test path of trailing slash" );
1431  last_key = NULL;
1432  g_assert( p_get_trailer_or_null( fixture->frame, "test/", &last_key ) == NULL );
1433  g_assert( !last_key );
1434 
1435  g_test_message( "Test with non existing path" );
1436  last_key = NULL;
1437  g_assert( p_get_trailer_or_null( fixture->frame, "/test/test2", &last_key ) == NULL );
1438  g_assert_cmpstr( last_key, == , "test2" );
1439 
1440  g_test_message( "Test with existing path" );
1441  last_key = NULL;
1442  frame = kvp_frame_new();
1443  kvp_frame_set_frame( fixture->frame, "/test/test2", frame );
1444  frame2 = p_get_trailer_or_null( fixture->frame, "/test/test2", &last_key );
1445  g_assert( kvp_frame_get_frame( fixture->frame, "/test") == frame2 );
1446  g_assert_cmpstr( last_key, == , "test2" );
1447 }
1448 
1449 static void
1450 test_kvp_frame_get_gvalue (Fixture *fixture, gconstpointer pData)
1451 {
1452  KvpFrame *frame = fixture->frame;
1453  GValue *value;
1454  Timespec ts = {1, 1};
1455  GncGUID *guid = populate_frame (frame);
1456  GDate date;
1457  gchar *log_domain = "qof.kvp";
1458  gint log_level = G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL;
1459  gchar *msg1 = "[gvalue_from_kvp_value()] Error! Attempt to transfer KvpFrame!";
1460  gchar *msg2 = "[gvalue_from_kvp_value()] Error! Invalid KVP Transfer Request!";
1461 #undef _func
1462  TestErrorStruct *check1 = test_error_struct_new (log_domain, log_level,
1463  msg1);
1464  TestErrorStruct *check2 = test_error_struct_new (log_domain, log_level,
1465  msg2);
1466  fixture->hdlrs = test_log_set_fatal_handler (fixture->hdlrs, check1,
1467  (GLogFunc)test_list_handler);
1468  test_add_error (check1);
1469  test_add_error (check2);
1470 
1471  g_date_clear (&date, 1);
1472  g_date_set_dmy (&date, 26, 1, 1957);
1473 
1474  value = kvp_frame_get_gvalue (frame, "gint64-type");
1475  g_assert (value != NULL);
1476  g_assert (G_VALUE_HOLDS_INT64 (value));
1477  g_assert_cmpint (g_value_get_int64 (value), ==, 100);
1478  gnc_gvalue_free (value);
1479 
1480  value = kvp_frame_get_gvalue (frame, "double-type");
1481  g_assert (value != NULL);
1482  g_assert (G_VALUE_HOLDS_DOUBLE (value));
1483  g_assert_cmpfloat (g_value_get_double (value), ==, 3.14159);
1484  gnc_gvalue_free (value);
1485 
1486  value = kvp_frame_get_gvalue (frame, "numeric-type");
1487  g_assert (value != NULL);
1488  g_assert_cmpint (G_VALUE_TYPE (value), ==, GNC_TYPE_NUMERIC);
1489  g_assert (gnc_numeric_zero_p (*(gnc_numeric*)g_value_get_boxed (value)));
1490  gnc_gvalue_free (value);
1491 
1492  value = kvp_frame_get_gvalue (frame, "timespec-type");
1493  g_assert (value != NULL);
1494  g_assert_cmpint (G_VALUE_TYPE (value), ==, GNC_TYPE_TIMESPEC);
1495  g_assert (timespec_equal (&ts, (Timespec*)g_value_get_boxed (value)));
1496  gnc_gvalue_free (value);
1497 
1498  value = kvp_frame_get_gvalue (frame, "string-type");
1499  g_assert (value != NULL);
1500  g_assert (G_VALUE_HOLDS_STRING (value));
1501  g_assert_cmpstr (g_value_get_string (value), ==, "abcdefghijklmnop");
1502  gnc_gvalue_free (value);
1503 
1504  value = kvp_frame_get_gvalue (frame, "guid-type");
1505  g_assert (value != NULL);
1506  g_assert_cmpint (G_VALUE_TYPE (value), ==, GNC_TYPE_GUID);
1507  g_assert (guid_equal (guid, (GncGUID*)g_value_get_boxed (value)));
1508  gnc_gvalue_free (value);
1509 
1510  value = kvp_frame_get_gvalue (frame, "gdate-type");
1511  g_assert (value != NULL);
1512  g_assert_cmpint (G_VALUE_TYPE (value), ==, G_TYPE_DATE);
1513  g_assert_cmpint (g_date_compare (&date, (GDate*)g_value_get_boxed (value)), ==, 0);
1514  gnc_gvalue_free (value);
1515 
1516  value = kvp_frame_get_gvalue (frame, "frame-type");
1517  g_assert (value == NULL);
1518  g_assert_cmpint (check1->hits, ==, 1);
1519  g_assert_cmpint (check2->hits, ==, 1);
1520 
1521  value = kvp_frame_get_gvalue (frame, "list-type");
1522  g_assert (value != NULL);
1523  g_assert_cmpint (G_VALUE_TYPE (value), ==, GNC_TYPE_VALUE_LIST);
1524  {
1525  GList *list = (GList*)g_value_get_boxed (value);
1526  GValue *value = NULL;
1527 
1528  value = (GValue*)(list->data);
1529  g_assert (G_VALUE_HOLDS_INT64 (value));
1530  g_assert (g_value_get_int64 (value) == 0x1f2e3d4c5b6a79LL);
1531  list = g_list_next (list);
1532 
1533  value = (GValue*)(list->data);
1534  g_assert (G_VALUE_HOLDS_DOUBLE (value));
1535  g_assert_cmpfloat (g_value_get_double (value), ==, 0.4342944819);
1536  list = g_list_next (list);
1537 
1538  value = (GValue*)(list->data);
1539  g_assert_cmpint (G_VALUE_TYPE (value), ==, GNC_TYPE_NUMERIC);
1540  g_assert (gnc_numeric_eq (*(gnc_numeric*)g_value_get_boxed (value),
1541  gnc_numeric_create (256, 120)));
1542  list = g_list_next (list);
1543 
1544  value = (GValue*)(list->data);
1545  g_assert_cmpint (G_VALUE_TYPE (value), ==, GNC_TYPE_TIMESPEC);
1546  g_assert (timespec_equal (&ts, (Timespec*)g_value_get_boxed (value)));
1547  list = g_list_next (list);
1548 
1549  value = (GValue*)(list->data);
1550  g_assert (G_VALUE_HOLDS_STRING (value));
1551  g_assert_cmpstr (g_value_get_string (value), ==, "qrstuvwxyz");
1552  list = g_list_next (list);
1553 
1554  value = (GValue*)(list->data);
1555  g_assert_cmpint (G_VALUE_TYPE (value), ==, GNC_TYPE_GUID);
1556  g_assert (guid_equal (guid, (GncGUID*)g_value_get_boxed (value)));
1557  list = g_list_next (list);
1558 
1559  g_assert (list == NULL);
1560 
1561  }
1562  gnc_gvalue_free (value);
1563 }
1564 
1565 static void
1566 test_kvp_frame_set_gvalue (Fixture *fixture, gconstpointer pData)
1567 {
1568 /* Bit of a shortcut: We'll use kvp_frame_get_item to make our KvpItem
1569  * and feed it into a new frame; something of a round-trip test.
1570  */
1571  KvpFrame *o_frame = fixture->frame;
1572  KvpFrame *n_frame = kvp_frame_new ();
1573  GValue *value;
1574  GList *o_list, *n_list;
1575 
1576  populate_frame (o_frame);
1577 
1578  value = kvp_frame_get_gvalue (o_frame, "gint64-type");
1579  g_assert (value != NULL);
1580  kvp_frame_set_gvalue (n_frame, "gint64-type", value);
1581  g_assert_cmpint (kvp_frame_get_gint64 (o_frame, "gint64-type"), ==,
1582  kvp_frame_get_gint64 (n_frame, "gint64-type"));
1583 
1584  value = kvp_frame_get_gvalue (o_frame, "double-type");
1585  g_assert (value != NULL);
1586  kvp_frame_set_gvalue (n_frame, "double-type", value);
1587  g_assert_cmpint (kvp_frame_get_double (o_frame, "double-type"), ==,
1588  kvp_frame_get_double (n_frame, "double-type"));
1589 
1590  value = kvp_frame_get_gvalue (o_frame, "numeric-type");
1591  g_assert (value != NULL);
1592  kvp_frame_set_gvalue (n_frame, "numeric-type", value);
1593  g_assert (gnc_numeric_equal (kvp_frame_get_numeric (o_frame, "numeric-type"),
1594  kvp_frame_get_numeric (n_frame, "numeric-type")));
1595 
1596  value = kvp_frame_get_gvalue (o_frame, "timespec-type");
1597  g_assert (value != NULL);
1598  kvp_frame_set_gvalue (n_frame, "timespec-type", value);
1599  {
1600  Timespec o_ts = kvp_frame_get_timespec (o_frame, "timespec-type");
1601  Timespec n_ts = kvp_frame_get_timespec (n_frame, "timespec-type");
1602  g_assert (timespec_equal (&o_ts, &n_ts));
1603  }
1604 
1605  value = kvp_frame_get_gvalue (o_frame, "string-type");
1606  g_assert (value != NULL);
1607  kvp_frame_set_gvalue (n_frame, "string-type", value);
1608  g_assert_cmpstr (kvp_frame_get_string (o_frame, "string-type"), ==,
1609  kvp_frame_get_string (n_frame, "string-type"));
1610 
1611  value = kvp_frame_get_gvalue (o_frame, "gdate-type");
1612  g_assert (value != NULL);
1613  kvp_frame_set_gvalue (n_frame, "gdate-type", value);
1614  {
1615  GDate o_date = kvp_value_get_gdate (kvp_frame_get_slot (o_frame,
1616  "gdate-type"));
1617  GDate n_date = kvp_value_get_gdate (kvp_frame_get_slot (n_frame,
1618  "gdate-type"));
1619  g_assert_cmpint (g_date_compare (&o_date, &n_date), ==, 0);
1620  }
1621 
1622  value = kvp_frame_get_gvalue (o_frame, "guid-type");
1623  g_assert (value != NULL);
1624  kvp_frame_set_gvalue (n_frame, "guid-type", value);
1625  g_assert (guid_equal (kvp_frame_get_guid (o_frame, "guid-type"),
1626  kvp_frame_get_guid (n_frame, "guid-type")));
1627 
1628  value = kvp_frame_get_gvalue (o_frame, "list-type");
1629  g_assert (value != NULL);
1630  kvp_frame_set_gvalue (n_frame, "list-type", value);
1631  o_list = kvp_value_get_glist (kvp_frame_get_slot (o_frame, "list_type"));
1632  n_list = kvp_value_get_glist (kvp_frame_get_slot (n_frame, "list_type"));
1633 
1634  g_assert_cmpint (g_list_length (o_list), ==, g_list_length (n_list));
1635  while (o_list && n_list)
1636  {
1637  g_assert_cmpint (kvp_value_compare ((KvpValue*)o_list->data,
1638  (KvpValue*)n_list->data), ==, 0);
1639  o_list = g_list_next (o_list);
1640  n_list = g_list_next (n_list);
1641  }
1642  kvp_frame_delete (n_frame);
1643 }
1644 
1645 static void
1646 test_kvp_frame_get_keys( void )
1647 {
1648  KvpFrame * frame = kvp_frame_new();
1649  const char * key1 = "number one";
1650  const char * key2 = "number one/number two";
1651  const char * key3 = "number three";
1652  const char * val1 = "Value1";
1653  const char * val2 = "Value2";
1654  const char * val3 = "Value3";
1655  unsigned int spot = 0;
1656  const char ** keys;
1657  kvp_frame_set_string(frame, key1, val1);
1658  kvp_frame_set_string(frame, key2, val2);
1659  kvp_frame_set_string(frame, key3, val3);
1660  keys = kvp_frame_get_keys(frame);
1661 
1662  g_assert(keys);
1663  g_assert(keys[spot]);
1664  g_assert(strcmp(keys[spot++], val1));
1665  g_assert(keys[spot]);
1666  g_assert(strcmp(keys[spot++], val3));
1667  g_assert(!keys[spot]);
1668 
1669  g_free(keys);
1670  kvp_frame_delete(frame);
1671 }
1672 
1673 void
1674 test_suite_kvp_frame( void )
1675 {
1676  GNC_TEST_ADD_FUNC( suitename, "kvp frame new and delete", test_kvp_frame_new_delete );
1677  GNC_TEST_ADD( suitename, "kvp frame copy", Fixture, NULL, setup, test_kvp_frame_copy, teardown );
1678  GNC_TEST_ADD( suitename, "kvp frame set foo", Fixture, NULL, setup, test_kvp_frame_set_foo, teardown );
1679  GNC_TEST_ADD( suitename, "kvp frame get frame slash", Fixture, NULL, setup, test_kvp_frame_get_frame_slash, teardown );
1680  GNC_TEST_ADD( suitename, "kvp frame get slot path", Fixture, NULL, setup, test_kvp_frame_get_slot_path, teardown );
1681  GNC_TEST_ADD( suitename, "kvp frame get slot path gslist", Fixture, NULL, setup, test_kvp_frame_get_slot_path_gslist, teardown );
1682  GNC_TEST_ADD( suitename, "kvp frame add frame nc", Fixture, NULL, setup, test_kvp_frame_add_frame_nc, teardown );
1683  GNC_TEST_ADD_FUNC( suitename, "kvp value copy", test_kvp_value_copy );
1684  GNC_TEST_ADD_FUNC( suitename, "kvp glist copy", test_kvp_glist_copy );
1685  GNC_TEST_ADD_FUNC( suitename, "kvp glist compare", test_kvp_glist_compare );
1686  GNC_TEST_ADD_FUNC( suitename, "kvp value compare", test_kvp_value_compare );
1687  GNC_TEST_ADD_FUNC( suitename, "kvp value new foo no copy", test_kvp_value_new_foo_nc );
1688  GNC_TEST_ADD( suitename, "kvp frame compare", Fixture, NULL, setup, test_kvp_frame_compare, teardown );
1689  GNC_TEST_ADD_FUNC( suitename, "kvp value to string", test_kvp_value_to_string );
1690  GNC_TEST_ADD( suitename, "kvp frame to string", Fixture, NULL, setup, test_kvp_frame_to_string, teardown );
1691  GNC_TEST_ADD( suitename, "kvp frame set slot path", Fixture, NULL, setup, test_kvp_frame_set_slot_path, teardown );
1692  GNC_TEST_ADD( suitename, "kvp frame set slot path gslist", Fixture, NULL, setup, test_kvp_frame_set_slot_path_gslist, teardown );
1693  GNC_TEST_ADD( suitename, "kvp frame replace slot nc", Fixture, NULL, setup, test_kvp_frame_replace_slot_nc, teardown );
1694  GNC_TEST_ADD_FUNC( suitename, "kvp frame get keys", test_kvp_frame_get_keys );
1695  GNC_TEST_ADD( suitename, "get trailer make", Fixture, NULL, setup_static, test_get_trailer_make, teardown_static );
1696  GNC_TEST_ADD( suitename, "kvp value glist to string", Fixture, NULL, setup_static, test_kvp_value_glist_to_string, teardown_static );
1697  GNC_TEST_ADD( suitename, "get or make", Fixture, NULL, setup_static, test_get_or_make, teardown_static );
1698  GNC_TEST_ADD( suitename, "kvp frame get frame or null slash trash", Fixture, NULL, setup_static, test_kvp_frame_get_frame_or_null_slash_trash, teardown_static );
1699  GNC_TEST_ADD( suitename, "get trailer or null", Fixture, NULL, setup_static, test_get_trailer_or_null, teardown_static );
1700  GNC_TEST_ADD ( suitename, "kvp frame get gvalue", Fixture, NULL, setup, test_kvp_frame_get_gvalue, teardown);
1701  GNC_TEST_ADD ( suitename, "kvp frame set gvalue", Fixture, NULL, setup, test_kvp_frame_set_gvalue, teardown);
1702 }
void kvp_frame_set_gvalue(KvpFrame *frame, const gchar *key, const GValue *value)
void kvp_frame_set_slot_path_gslist(KvpFrame *frame, KvpValue *value, GSList *key_path)
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
gchar * gnc_timespec_to_iso8601_buff(Timespec ts, gchar *buff)
void kvp_frame_set_slot(KvpFrame *frame, const gchar *key, KvpValue *value)
void kvp_frame_set_timespec(KvpFrame *frame, const gchar *path, Timespec ts)
GList * kvp_glist_copy(const GList *list)
gboolean kvp_frame_is_empty(const KvpFrame *frame)
GncGUID * guid_copy(const GncGUID *guid)
void kvp_frame_add_frame_nc(KvpFrame *frame, const gchar *path, KvpFrame *chld)
Store the given kvp_frame to the glist bag at the indicated path (non-copying)
GncGUID * guid_new(void)
gint kvp_frame_compare(const KvpFrame *fa, const KvpFrame *fb)
KvpFrame * kvp_frame_copy(const KvpFrame *frame)
gboolean timespec_equal(const Timespec *ta, const Timespec *tb)
Use a 64-bit unsigned int timespec.
Definition: gnc-date.h:299
void kvp_frame_set_gint64(KvpFrame *frame, const gchar *path, gint64 ival)
gboolean gnc_numeric_zero_p(gnc_numeric a)
gchar * guid_to_string_buff(const GncGUID *guid, gchar *buff)
gint gnc_numeric_compare(gnc_numeric a, gnc_numeric b)
KvpValue * kvp_value_new_frame_nc(KvpFrame *value)
KvpFrame * kvp_frame_set_value(KvpFrame *frame, const gchar *path, const KvpValue *value)
void kvp_frame_delete(KvpFrame *frame)
void kvp_frame_set_slot_path(KvpFrame *frame, KvpValue *value, const gchar *first_key,...)
Definition: guid.h:65
gint64 kvp_value_get_gint64(const KvpValue *value)
void gnc_gvalue_free(GValue *value)
Convenience function to release the value in a GValue acquired by kvp_frame_get_gvalue and to free th...
gint timespec_cmp(const Timespec *ta, const Timespec *tb)
char * kvp_value_get_string(const KvpValue *value)
KvpValue * kvp_value_copy(const KvpValue *value)
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
#define GUID_ENCODING_LENGTH
Definition: guid.h:74
GValue * kvp_frame_get_gvalue(KvpFrame *frame, const gchar *key)
const char ** kvp_frame_get_keys(const KvpFrame *frame)
#define kvp_value_new_gnc_numeric
Definition: kvp_frame.h:466
GList * kvp_value_get_glist(const KvpValue *value)
void kvp_frame_set_numeric(KvpFrame *frame, const gchar *path, gnc_numeric nval)
void kvp_glist_delete(GList *list)
GncGUID * kvp_value_get_guid(const KvpValue *value)
KvpValue * kvp_frame_get_slot_path(KvpFrame *frame, const gchar *first_key,...)
gchar * kvp_frame_to_string(const KvpFrame *frame)
gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b)
KvpFrame * kvp_frame_set_value_nc(KvpFrame *frame, const gchar *path, KvpValue *value)
KvpValue * kvp_value_new_glist(const GList *value)
void kvp_frame_set_double(KvpFrame *frame, const gchar *path, double dval)
KvpValue * kvp_value_new_glist_nc(GList *lst)
const GncGUID * guid_null(void)
struct KvpFrameImpl KvpFrame
Definition: kvp_frame.h:76
GDate kvp_value_get_gdate(const KvpValue *value)
KvpFrame * kvp_frame_new(void)
KvpFrame * kvp_value_get_frame(const KvpValue *value)
KvpFrame * kvp_frame_get_frame(const KvpFrame *frame, const gchar *path)
void kvp_value_delete(KvpValue *value)
gint kvp_value_compare(const KvpValue *va, const KvpValue *vb)
gchar * kvp_value_to_string(const KvpValue *val)
Debug version of kvp_value_to_string.
void kvp_frame_set_string(KvpFrame *frame, const gchar *path, const gchar *str)
Store a copy of the string at the indicated path.
struct KvpValueImpl KvpValue
Definition: kvp_frame.h:80
KvpValue * kvp_frame_get_slot_path_gslist(KvpFrame *frame, const GSList *key_path)