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