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