Portability fix: printf of null string sometimes core dumps.
[gedcom-parse.git] / t / src / update.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 "output.h"
26 #include <locale.h>
27 #include <stdio.h>
28
29 void gedcom_message_handler(Gedcom_msg_type type, char *msg)
30 {
31   if (type == MESSAGE)
32     output(1, "MESSAGE: ");
33   else if (type == WARNING)
34     output(1, "WARNING: ");
35   else if (type == ERROR)
36     output(1, "ERROR: ");
37   output(1, "%s\n", msg);
38 }
39
40 void show_help ()
41 {
42   printf("gedcom-parse test program for libgedcom\n\n");
43   printf("Usage:  updatetest [options] file\n");
44   printf("Options:\n");
45   printf("  -h    Show this help text\n");
46   printf("  -q    No output to standard output\n");
47 }
48
49 int test_xref_functions()
50 {
51   struct xref_value* xr;
52   int result;
53
54   xr = gedcom_get_by_xref("@NOTHING_THERE@");
55   if (xr != NULL)
56     return 10;
57
58   xr = gedcom_add_xref(XREF_FAM, "NOT_AN_XREF", (Gedcom_ctxt)1);
59   if (xr != NULL)
60     return 11;
61   
62   xr = gedcom_add_xref(XREF_FAM, "@XREF@ BUT EXTRA", (Gedcom_ctxt)1);
63   if (xr != NULL)
64     return 12;
65   
66   xr = gedcom_add_xref(XREF_FAM, "@NEWFAM@", (Gedcom_ctxt)1);
67   if (xr == NULL)
68     return 13;
69
70   xr = gedcom_add_xref(XREF_FAM, "@NEWFAM@", (Gedcom_ctxt)2);
71   if (xr != NULL)
72     return 14;
73
74   xr = gedcom_get_by_xref("@NEWFAM@");
75   if (xr == NULL)
76     return 15;
77
78   if (xr->type != XREF_FAM) {
79     output(1, "Not the correct cross-reference type\n");
80     return 16;
81   }
82
83   if ((int)xr->object != 1) {
84     output(1, "Not the correct cross-reference object\n");
85     return 17;
86   }
87   
88   xr = gedcom_link_xref(XREF_INDI, "@NEWFAM@");
89   if (xr != NULL)
90     return 18;
91
92   xr = gedcom_link_xref(XREF_FAM, "@NEWFAM@");
93   if (xr == NULL)
94     return 19;
95
96   xr = gedcom_link_xref(XREF_FAM, "@NEWFAM");
97   if (xr != NULL)
98     return 20;
99
100   xr = gedcom_link_xref(XREF_FAM, "@NEWFAM@");
101   if (xr == NULL)
102     return 21;
103
104   xr = gedcom_link_xref(XREF_INDI, "@OLDINDI@");
105   if (xr != NULL)
106     return 22;
107
108   result = gedcom_delete_xref("@NEWFAM@");
109   if (result == 0)
110     return 23;
111
112   xr = gedcom_unlink_xref(XREF_INDI, "@NEWFAM@");
113   if (xr != NULL)
114     return 24;
115
116   xr = gedcom_unlink_xref(XREF_FAM, "@NEWFAM@");
117   if (xr == NULL)
118     return 25;
119
120   result = gedcom_delete_xref("@NEWFAM@");
121   if (result == 0)
122     return 26;
123
124   xr = gedcom_unlink_xref(XREF_FAM, "@NEWFAM@");
125   if (xr == NULL)
126     return 27;
127
128   result = gedcom_delete_xref("@NEWFAM@");
129   if (result != 0)
130     return 28;
131
132   return 0;
133 }
134
135 int main(int argc, char* argv[])
136 {
137   int result;
138   
139   if (argc > 1) {
140     int i;
141     for (i=1; i<argc; i++) {
142       if (!strncmp(argv[i], "-h", 3)) {
143         show_help();
144         exit(1);
145       }
146       else if (!strncmp(argv[i], "-q", 3)) {
147         output_set_quiet(1);
148       }
149       else {
150         printf ("Unrecognized option: %s\n", argv[i]);
151         show_help();
152         exit(1);
153       }
154     }
155   }
156   
157   gedcom_init();
158   setlocale(LC_ALL, "");
159   gedcom_set_message_handler(gedcom_message_handler);
160
161   output_open();
162   
163   result = gedcom_new_model();
164   if (result == 0)
165     result |= test_xref_functions();
166   if (result == 0) {
167     output(1, "Test succeeded\n");
168   }
169   else {
170     output(1, "Test failed: %d\n", result);
171   }
172
173   output_close();
174   return result;
175 }