Tests for date functions.
[gedcom-parse.git] / t / src / update_gom.c
1 /* Test program for the Gedcom library.
2    Copyright (C) 2001, 2002 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
5
6    The Gedcom parser library is free software; you can redistribute it
7    and/or modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The Gedcom parser library is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the Gedcom parser library; if not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 #include "gedcom.h"
25 #include "gom.h"
26 #include "output.h"
27 #include "dump_gom.h"
28 #include "portability.h"
29 #include <locale.h>
30 #include <stdio.h>
31
32 void gedcom_message_handler(Gedcom_msg_type type, char *msg)
33 {
34   if (type == MESSAGE)
35     output(1, "MESSAGE: ");
36   else if (type == WARNING)
37     output(1, "WARNING: ");
38   else if (type == ERROR)
39     output(1, "ERROR: ");
40   output(1, "%s\n", msg);
41 }
42
43 void show_help ()
44 {
45   printf("gedcom-parse test program for libgedcom\n\n");
46   printf("Usage:  updategomtest [options]\n");
47   printf("Options:\n");
48   printf("  -h    Show this help text\n");
49   printf("  -q    No output to standard output\n");
50   printf("  -o <outfile>  File to generate output to (def. testgedcom.out)\n");
51 }
52
53 int test_string_functions()
54 {
55   struct header* head;
56   struct submitter* subm;
57   struct xref_value* xref;
58   char* value;
59   int conv_fails = 0;
60   const char* orig_source_id = "GEDCOM_PARSE";
61   const char* new_source_id = "TEST_UPDATE";
62   const char* new_submitter_name_utf8 = "Belgi\xC3\xAB";
63   const char* new_submitter_name_ansi = "Belgi\xEB";
64
65   head = gom_get_header();
66   if (head == NULL)
67     return 10;
68   
69   value = gom_get_string(head->source.id);
70   if (value == NULL)
71     return 11;
72   if (strcmp(value, orig_source_id))
73     return 12;
74
75   value = gom_set_string(&head->source.id, new_source_id);
76   if (value == NULL)
77     return 13;
78   if (strcmp(value, new_source_id))
79     return 14;
80
81   value = gom_get_string(head->source.id);
82   if (value == NULL)
83     return 15;
84   if (strcmp(value, new_source_id))
85     return 16;
86
87   xref = head->submitter;
88   if (xref == NULL)
89     return 17;
90   
91   subm = gom_get_submitter_by_xref(xref->string);
92   if (subm == NULL)
93     return 18;
94
95   value = gom_set_string(&subm->name, new_submitter_name_utf8);
96   if (value == NULL)
97     return 19;
98   if (strcmp(value, new_submitter_name_utf8))
99     return 20;
100
101   value = gom_get_string_for_locale(subm->name, &conv_fails);
102   if (value == NULL)
103     return 21;
104   if (!strcmp(value, new_submitter_name_utf8))
105     return 22;
106   if (conv_fails != 1)
107     return 23;
108
109   value = gom_set_string(&subm->name, new_submitter_name_ansi);
110   if (value != NULL)
111     return 24;
112   
113   value = gom_set_string_for_locale(&subm->name, new_submitter_name_ansi);
114   if (value != NULL)
115     return 25;
116
117   return 0;
118 }
119
120 char* print_date(const char* message, struct date_value* dv)
121 {
122   char* date_str;
123   output(0, "\n%s:", message);
124   show_date(dv);
125   date_str = gedcom_date_to_string(dv);
126   output(0, "String: '%s'\n", str_val(date_str));
127   return date_str;
128 }
129
130 int test_date_functions()
131 {
132   struct header* head;
133   struct date_value* dv;
134   char* date_str;
135   int normalized;
136   
137   head = gom_get_header();
138   if (head == NULL)
139     return 100;
140
141   dv = head->date;
142   if (dv != NULL)
143     return 101;
144
145   dv = gedcom_new_date_value(NULL);
146   if (dv == NULL)
147     return 102;
148
149   head->date = dv;
150   date_str = print_date("Initial date value", dv);
151   if (date_str[0])
152     return 103;
153
154   dv->date1.cal = CAL_GREGORIAN;
155   strcpy(dv->date1.year_str, "1990");
156   normalized = gedcom_normalize_date(DI_FROM_STRINGS, dv);
157   if (normalized != 0)
158     return 104;
159   date_str = print_date("Setting only year string", dv);
160   if (! date_str[0])
161     return 105;
162
163   dv->date1.year = 1989;
164   normalized = gedcom_normalize_date(DI_FROM_NUMBERS, dv);
165   if (normalized != 0)
166     return 106;
167   date_str = print_date("Setting only year number", dv);
168   if (! date_str[0])
169     return 107;
170
171   dv->date1.type = DATE_EXACT;
172   dv->date1.sdn1 = 2500000;
173   dv->date1.sdn2 = -1;
174   normalized = gedcom_normalize_date(DI_FROM_SDN, dv);
175   if (normalized != 0)
176     return 108;
177   date_str = print_date("Setting only SDN 1", dv);
178   if (! date_str[0])
179     return 109;
180
181   dv->date1.cal = CAL_HEBREW;
182   normalized = gedcom_normalize_date(DI_FROM_SDN, dv);  
183   if (normalized != 0)
184     return 110;
185   date_str = print_date("Same date in Hebrew calendar", dv);
186   if (! date_str[0])
187     return 111;
188
189   dv->date1.cal = CAL_FRENCH_REV;
190   normalized = gedcom_normalize_date(DI_FROM_SDN, dv);  
191   if (normalized == 0)
192     return 112;
193   date_str = print_date("Same date in French revolution calendar", dv);
194   if (date_str[0])
195     return 113;
196
197   dv->date1.cal = CAL_GREGORIAN;
198   dv->date1.day = 4;
199   dv->date1.month = 2;
200   dv->date1.year = 1799;
201   normalized = gedcom_normalize_date(DI_FROM_NUMBERS, dv);
202   if (normalized != 0)
203     return 114;
204   dv->date1.cal = CAL_FRENCH_REV;
205   normalized = gedcom_normalize_date(DI_FROM_SDN, dv);
206   if (normalized != 0)
207     return 115;
208   date_str = print_date("Valid French revolution date", dv);
209   if (! date_str[0])
210     return 116;
211
212   return 0;
213 }
214
215 int main(int argc, char* argv[])
216 {
217   int result;
218   char* outfilename = NULL;
219   
220   if (argc > 1) {
221     int i;
222     for (i=1; i<argc; i++) {
223       if (!strncmp(argv[i], "-h", 3)) {
224         show_help();
225         exit(1);
226       }
227       else if (!strncmp(argv[i], "-q", 3)) {
228         output_set_quiet(1);
229       }
230       else if (!strncmp(argv[i], "-o", 3)) {
231         i++;
232         if (i < argc) {
233           outfilename = argv[i];
234         }
235         else {
236           printf ("Missing output file name\n");
237           show_help();
238           exit(1);
239         }
240       }
241       else {
242         printf ("Unrecognized option: %s\n", argv[i]);
243         show_help();
244         exit(1);
245       }
246     }
247   }
248   
249   gedcom_init();
250   setlocale(LC_ALL, "");
251   gedcom_set_message_handler(gedcom_message_handler);
252
253   output_open(outfilename);
254   
255   result = gom_new_model();
256   if (result == 0)
257     result |= test_string_functions();
258   if (result == 0)
259     result |= test_date_functions();
260   if (result == 0) {
261     output(1, "Test succeeded\n");
262   }
263   else {
264     output(1, "Test failed: %d\n", result);
265   }
266
267   show_data();
268   output_close();
269   return result;
270 }