GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-scm-query-string.c
1 
2 #include "config.h"
3 #include <glib.h>
4 #include <libguile.h>
5 #include "guile-mappings.h"
6 
7 #include "engine-helpers-guile.h"
8 #include "gnc-module.h"
9 #include "gnc-guile-utils.h"
10 #include "test-engine-stuff.h"
11 #include "test-stuff.h"
12 #include "Query.h"
13 #include "TransLog.h"
14 
15 
16 static void
17 test_query (Query *q, SCM val2str)
18 {
19  SCM scm_q;
20  SCM str_q;
21  SCM res_q;
22  SCM args = SCM_EOL;
23  Query *q2;
24  gchar *str2 = NULL;
25 
26  scm_q = gnc_query2scm (q);
27  args = scm_cons (scm_q, SCM_EOL);
28  str_q = scm_apply (val2str, args, SCM_EOL);
29 
30  args = scm_cons (scm_from_utf8_string ("'"), scm_cons (str_q, SCM_EOL));
31  str_q = scm_string_append (args);
32 
33  str2 = gnc_scm_to_utf8_string (str_q);
34  if (str2)
35  {
36  res_q = scm_c_eval_string (str2);
37  }
38  else
39  {
40  res_q = SCM_BOOL_F;
41  }
42 
43  q2 = gnc_scm2query (res_q);
44 
45  if (!qof_query_equal (q, q2))
46  {
47  failure ("queries don't match");
48  fprintf (stderr, "%s\n\n", str2 ? str2 : "(null)");
49  scm_q = gnc_query2scm (q2);
50  scm_display (scm_q, SCM_UNDEFINED);
51  scm_newline (SCM_UNDEFINED);
52  g_free(str2);
53  exit (1);
54  }
55  else
56  {
57  success ("queries match");
58  }
59  g_free(str2);
60  if (q2) qof_query_destroy (q2);
61 }
62 
63 static void
64 run_tests (void)
65 {
66  Query *q;
67  SCM val2str;
68  int i;
69 
70  val2str = scm_c_eval_string ("gnc:value->string");
71  g_return_if_fail (scm_is_procedure (val2str));
72 
73  for (i = 0; i < 1000; i++)
74  {
75  q = get_random_query ();
76  test_query (q, val2str);
78  fflush(stdout);
79  }
80 
81  {
82  q = get_random_query ();
83  test_query (q, val2str);
85  fflush(stdout);
86  }
87 
88 }
89 
90 static void
91 main_helper (void *closure, int argc, char **argv)
92 {
93  gnc_module_system_init ();
94  gnc_module_load("gnucash/engine", 0);
95  gnc_module_load("gnucash/app-utils", 0);
96 
97  xaccLogDisable ();
98 
99  /* double->string->double is not idempotent */
100  kvp_exclude_type (KVP_TYPE_DOUBLE);
101 
102  /* Initialize to a known RNG position */
103  srand(1);
104 
105  run_tests ();
106 
107  print_test_results ();
108 
109  exit (get_rv ());
110 }
111 
112 int
113 main (int argc, char **argv)
114 {
115  g_setenv ("GNC_UNINSTALLED", "1", TRUE);
116 /* When built with clang, guile-1.8.8's scm_c_eval_string truncates all
117  * integer values to int32, which causes this test to fail.
118  */
119 #ifndef __clang__
120  scm_boot_guile (argc, argv, main_helper, NULL);
121 #endif
122  return 0;
123 }
void xaccLogDisable(void)
Definition: TransLog.c:93
gboolean qof_query_equal(const QofQuery *q1, const QofQuery *q2)
void qof_query_destroy(QofQuery *q)
API for the transaction logger.