GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-import-parse.c
1 /*
2  * test-import-parse.c -- Test the import-parse routines.
3  *
4  * Created by: Derek Atkins <[email protected]>
5  * Copyright (c) 2003 Derek Atkins <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, contact:
19  *
20  * Free Software Foundation Voice: +1-617-542-5942
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22  * Boston, MA 02110-1301, USA [email protected]
23  */
24 
25 #include "config.h"
26 #include <glib.h>
27 #include <libguile.h>
28 
29 #include "gnc-module.h"
30 #include "import-parse.h"
31 
32 #include "test-stuff.h"
33 
34 typedef struct
35 {
36  int y;
37  int m;
38  int d;
39 } my_ymd_t;
40 
41 /* make sure the numbers are the same in the two sets of lists */
42 const char* period_numbers[] = { " $+2000.00", "-2.00", "1,182,183.1827", NULL };
43 const char* comma_numbers[] = { " $2000,00", "-2,00", "1.182.183,1827", NULL };
44 const char* period_numbers_ambig[] = { " -$1,000 ", "100.277", NULL };
45 const char* comma_numbers_ambig[] = { " -$1.000 ", "100,277", NULL };
46 
47 /* Make sure the strings and numbers match... */
48 const char* dates_ymd[] = { "1999/12/31", "2001-6-17", "20020726", NULL };
49 my_ymd_t dates_ymd_vals[] = { {1999, 12, 31}, {2001, 6, 17}, {2002, 7, 26}, {0, 0, 0} };
50 const char* dates_ydm[] = { "1999/31/12", "2001-17-6", "20012311", NULL };
51 my_ymd_t dates_ydm_vals[] = { {1999, 12, 31}, {2001, 6, 17}, {2001, 11, 23}, {0, 0, 0} };
52 const char* dates_mdy[] = { "1/16/2001", "12-31-1999", "01171983", NULL };
53 my_ymd_t dates_mdy_vals[] = { {2001, 1, 16}, {1999, 12, 31}, {1983, 1, 17}, {0, 0, 0} };
54 const char* dates_dmy[] = { "16/1/2001", "31-12-1999", "17011976", NULL };
55 my_ymd_t dates_dmy_vals[] = { {2001, 1, 16}, {1999, 12, 31}, {1976, 1, 17}, {0, 0, 0} };
56 
57 const char* dates_yxx[] = { "99/1/6", "1999-12'10", "20010306", NULL };
58 const char* dates_xxy[] = { "1/3/99", "12-10'1999", "03062001", NULL };
59 
60 static void
61 run_check(GncImportFormat (*check_fcn)(const char*, GncImportFormat),
62  const char *numbers[], GncImportFormat formats,
63  const char* msg, GncImportFormat expected)
64 {
65  while (*numbers)
66  {
67  do_test(check_fcn(*numbers, formats) == expected, msg);
68  numbers++;
69  }
70 }
71 
72 static void
73 test_check_numeric(void)
74 {
75  GncImportFormat fmts;
76 
77  fmts = GNCIF_NUM_PERIOD | GNCIF_NUM_COMMA | GNCIF_DATE_MDY;
78 
79  run_check(gnc_import_test_numeric, period_numbers, fmts,
80  "Period numbers", GNCIF_NUM_PERIOD);
81  run_check(gnc_import_test_numeric, comma_numbers, fmts,
82  "Comma numbers", GNCIF_NUM_COMMA);
83 
84  run_check(gnc_import_test_numeric, period_numbers_ambig, fmts,
85  "Ambiguous Period numbers", GNCIF_NUM_PERIOD | GNCIF_NUM_COMMA);
86  run_check(gnc_import_test_numeric, comma_numbers_ambig, fmts,
87  "Ambiguous Comma numbers", GNCIF_NUM_PERIOD | GNCIF_NUM_COMMA);
88 }
89 
90 static void
91 test_check_date(void)
92 {
93  GncImportFormat fmts;
94 
95  fmts = GNCIF_DATE_DMY | GNCIF_DATE_MDY | GNCIF_DATE_YMD | GNCIF_DATE_YDM;
96 
97  run_check(gnc_import_test_date, dates_ymd, fmts, "y/m/d dates", GNCIF_DATE_YMD);
98  run_check(gnc_import_test_date, dates_ydm, fmts, "y/d/m dates", GNCIF_DATE_YDM);
99  run_check(gnc_import_test_date, dates_mdy, fmts, "m/d/y dates", GNCIF_DATE_MDY);
100  run_check(gnc_import_test_date, dates_dmy, fmts, "d/m/y dates", GNCIF_DATE_DMY);
101 
102  run_check(gnc_import_test_date, dates_yxx, fmts, "y/x/x dates",
103  GNCIF_DATE_YMD | GNCIF_DATE_YDM);
104  run_check(gnc_import_test_date, dates_xxy, fmts, "x/x/y dates",
105  GNCIF_DATE_DMY | GNCIF_DATE_MDY);
106 }
107 
108 static void
109 test_numbers(const char* pstr, const char* cstr)
110 {
111  gnc_numeric pval, cval;
112 
113  do_test(gnc_import_parse_numeric(pstr, GNCIF_NUM_PERIOD, &pval), "Parsing Period");
114  do_test(gnc_import_parse_numeric(cstr, GNCIF_NUM_COMMA, &cval), "Parsing Comma");
115 
116  do_test(gnc_numeric_equal(pval, cval), "Numbers equal?");
117 }
118 
119 static void
120 test_number_strings(const char** pstrs, const char** cstrs)
121 {
122  while (*pstrs && *cstrs)
123  {
124  test_numbers(*pstrs, *cstrs);
125  pstrs++;
126  cstrs++;
127  }
128 }
129 
130 static void
131 test_parse_numeric(void)
132 {
133  test_number_strings(period_numbers, comma_numbers);
134  test_number_strings(period_numbers_ambig, comma_numbers_ambig);
135 }
136 
137 static void
138 test_date(const char* str, GncImportFormat fmt, my_ymd_t date)
139 {
140  Timespec ts, ts2;;
141 
142  do_test(gnc_import_parse_date(str, fmt, &ts), "Parsing date");
143  ts2 = gnc_dmy2timespec(date.d, date.m, date.y);
144  do_test(timespec_equal(&ts, &ts2), "Date Equal");
145 }
146 
147 static void
148 test_date_list(const char** strs, GncImportFormat fmt, my_ymd_t *dates)
149 {
150  while (*strs && (*dates).y)
151  {
152  test_date(*strs, fmt, *dates);
153  strs++;
154  dates++;
155  }
156 }
157 
158 static void
159 test_parse_date(void)
160 {
161  test_date_list(dates_ymd, GNCIF_DATE_YMD, dates_ymd_vals);
162  test_date_list(dates_ydm, GNCIF_DATE_YDM, dates_ydm_vals);
163  test_date_list(dates_mdy, GNCIF_DATE_MDY, dates_mdy_vals);
164  test_date_list(dates_dmy, GNCIF_DATE_DMY, dates_dmy_vals);
165 }
166 
167 static void
168 test_import_parse(void)
169 {
170  test_check_numeric();
171  test_check_date();
172  test_parse_numeric();
173  test_parse_date();
174 }
175 
176 static void
177 main_helper(void *closure, int argc, char **argv)
178 {
179  gnc_module_system_init ();
180  gnc_module_load("gnucash/import-export", 0);
181  test_import_parse();
182  print_test_results();
183  exit(get_rv());
184 }
185 
186 int
187 main(int argc, char **argv)
188 {
189  g_setenv ("GNC_UNINSTALLED", "1", TRUE);
190  scm_boot_guile(argc, argv, main_helper, NULL);
191  return 0;
192 }
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
gboolean timespec_equal(const Timespec *ta, const Timespec *tb)
Use a 64-bit unsigned int timespec.
Definition: gnc-date.h:299
Timespec gnc_dmy2timespec(gint day, gint month, gint year)