GnuCash 2.4.99
test-import-parse.c
00001 /*
00002  * test-import-parse.c -- Test the import-parse routines.
00003  *
00004  * Created by:  Derek Atkins <derek@ihtfp.com>
00005  * Copyright (c) 2003 Derek Atkins <warlord@MIT.EDU>
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, contact:
00019  *
00020  * Free Software Foundation           Voice:  +1-617-542-5942
00021  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00022  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00023  */
00024 
00025 #include "config.h"
00026 #include <glib.h>
00027 #include <libguile.h>
00028 
00029 #include "gnc-module.h"
00030 #include "import-parse.h"
00031 
00032 #include "test-stuff.h"
00033 
00034 typedef struct
00035 {
00036     int y;
00037     int m;
00038     int d;
00039 } my_ymd_t;
00040 
00041 /* make sure the numbers are the same in the two sets of lists */
00042 const char* period_numbers[] = { " $+2000.00", "-2.00", "1,182,183.1827", NULL };
00043 const char* comma_numbers[] = { " $2000,00", "-2,00", "1.182.183,1827", NULL };
00044 const char* period_numbers_ambig[] = { "  -$1,000 ", "100.277", NULL };
00045 const char* comma_numbers_ambig[] = { "  -$1.000 ", "100,277", NULL };
00046 
00047 /* Make sure the strings and numbers match... */
00048 const char* dates_ymd[] = { "1999/12/31", "2001-6-17", "20020726",  NULL };
00049 my_ymd_t dates_ymd_vals[] = { {1999, 12, 31}, {2001, 6, 17}, {2002, 7, 26}, {0, 0, 0} };
00050 const char* dates_ydm[] = { "1999/31/12", "2001-17-6", "20012311", NULL };
00051 my_ymd_t dates_ydm_vals[] = { {1999, 12, 31}, {2001, 6, 17}, {2001, 11, 23}, {0, 0, 0} };
00052 const char* dates_mdy[] = { "1/16/2001", "12-31-1999", "01171983", NULL };
00053 my_ymd_t dates_mdy_vals[] = { {2001, 1, 16}, {1999, 12, 31}, {1983, 1, 17}, {0, 0, 0} };
00054 const char* dates_dmy[] = { "16/1/2001", "31-12-1999", "17011976", NULL };
00055 my_ymd_t dates_dmy_vals[] = { {2001, 1, 16}, {1999, 12, 31}, {1976, 1, 17}, {0, 0, 0} };
00056 
00057 const char* dates_yxx[] = { "99/1/6", "1999-12'10", "20010306", NULL };
00058 const char* dates_xxy[] = { "1/3/99", "12-10'1999", "03062001", NULL };
00059 
00060 static void
00061 run_check(GncImportFormat (*check_fcn)(const char*, GncImportFormat),
00062 const char *numbers[], GncImportFormat formats,
00063 const char* msg, GncImportFormat expected)
00064 {
00065     while (*numbers)
00066     {
00067         do_test(check_fcn(*numbers, formats) == expected, msg);
00068         numbers++;
00069     }
00070 }
00071 
00072 static void
00073 test_check_numeric(void)
00074 {
00075     GncImportFormat fmts;
00076 
00077     fmts = GNCIF_NUM_PERIOD | GNCIF_NUM_COMMA | GNCIF_DATE_MDY;
00078 
00079     run_check(gnc_import_test_numeric, period_numbers, fmts,
00080     "Period numbers", GNCIF_NUM_PERIOD);
00081     run_check(gnc_import_test_numeric, comma_numbers, fmts,
00082     "Comma numbers", GNCIF_NUM_COMMA);
00083 
00084     run_check(gnc_import_test_numeric, period_numbers_ambig, fmts,
00085     "Ambiguous Period numbers", GNCIF_NUM_PERIOD | GNCIF_NUM_COMMA);
00086     run_check(gnc_import_test_numeric, comma_numbers_ambig, fmts,
00087     "Ambiguous Comma numbers", GNCIF_NUM_PERIOD | GNCIF_NUM_COMMA);
00088 }
00089 
00090 static void
00091 test_check_date(void)
00092 {
00093     GncImportFormat fmts;
00094 
00095     fmts = GNCIF_DATE_DMY | GNCIF_DATE_MDY | GNCIF_DATE_YMD | GNCIF_DATE_YDM;
00096 
00097     run_check(gnc_import_test_date, dates_ymd, fmts, "y/m/d dates", GNCIF_DATE_YMD);
00098     run_check(gnc_import_test_date, dates_ydm, fmts, "y/d/m dates", GNCIF_DATE_YDM);
00099     run_check(gnc_import_test_date, dates_mdy, fmts, "m/d/y dates", GNCIF_DATE_MDY);
00100     run_check(gnc_import_test_date, dates_dmy, fmts, "d/m/y dates", GNCIF_DATE_DMY);
00101 
00102     run_check(gnc_import_test_date, dates_yxx, fmts, "y/x/x dates",
00103     GNCIF_DATE_YMD | GNCIF_DATE_YDM);
00104     run_check(gnc_import_test_date, dates_xxy, fmts, "x/x/y dates",
00105     GNCIF_DATE_DMY | GNCIF_DATE_MDY);
00106 }
00107 
00108 static void
00109 test_numbers(const char* pstr, const char* cstr)
00110 {
00111     gnc_numeric pval, cval;
00112 
00113     do_test(gnc_import_parse_numeric(pstr, GNCIF_NUM_PERIOD, &pval), "Parsing Period");
00114     do_test(gnc_import_parse_numeric(cstr, GNCIF_NUM_COMMA, &cval), "Parsing Comma");
00115 
00116     do_test(gnc_numeric_equal(pval, cval), "Numbers equal?");
00117 }
00118 
00119 static void
00120 test_number_strings(const char** pstrs, const char** cstrs)
00121 {
00122     while (*pstrs && *cstrs)
00123     {
00124         test_numbers(*pstrs, *cstrs);
00125         pstrs++;
00126         cstrs++;
00127     }
00128 }
00129 
00130 static void
00131 test_parse_numeric(void)
00132 {
00133     test_number_strings(period_numbers, comma_numbers);
00134     test_number_strings(period_numbers_ambig, comma_numbers_ambig);
00135 }
00136 
00137 static void
00138 test_date(const char* str, GncImportFormat fmt, my_ymd_t date)
00139 {
00140     Timespec ts, ts2;;
00141 
00142     do_test(gnc_import_parse_date(str, fmt, &ts), "Parsing date");
00143     ts2 = gnc_dmy2timespec(date.d, date.m, date.y);
00144     do_test(timespec_equal(&ts, &ts2), "Date Equal");
00145 }
00146 
00147 static void
00148 test_date_list(const char** strs, GncImportFormat fmt, my_ymd_t *dates)
00149 {
00150     while (*strs && (*dates).y)
00151     {
00152         test_date(*strs, fmt, *dates);
00153         strs++;
00154         dates++;
00155     }
00156 }
00157 
00158 static void
00159 test_parse_date(void)
00160 {
00161     test_date_list(dates_ymd, GNCIF_DATE_YMD, dates_ymd_vals);
00162     test_date_list(dates_ydm, GNCIF_DATE_YDM, dates_ydm_vals);
00163     test_date_list(dates_mdy, GNCIF_DATE_MDY, dates_mdy_vals);
00164     test_date_list(dates_dmy, GNCIF_DATE_DMY, dates_dmy_vals);
00165 }
00166 
00167 static void
00168 test_import_parse(void)
00169 {
00170     test_check_numeric();
00171     test_check_date();
00172     test_parse_numeric();
00173     test_parse_date();
00174 }
00175 
00176 static void
00177 main_helper(void *closure, int argc, char **argv)
00178 {
00179     gnc_module_system_init ();
00180     gnc_module_load("gnucash/import-export", 0);
00181     test_import_parse();
00182     print_test_results();
00183     exit(get_rv());
00184 }
00185 
00186 int
00187 main(int argc, char **argv)
00188 {
00189     g_setenv ("GNC_UNINSTALLED", "1", TRUE);
00190     scm_boot_guile(argc, argv, main_helper, NULL);
00191     return 0;
00192 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines