Make commands silent (except echo's).
[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   printf("  -o <outfile>  File to generate output to (def. testgedcom.out)\n");
48 }
49
50 int test_xref_functions()
51 {
52   struct xref_value* xr;
53   int result;
54
55   xr = gedcom_get_by_xref("@NOTHING_THERE@");
56   if (xr != NULL)
57     return 10;
58
59   xr = gedcom_add_xref(XREF_FAM, "NOT_AN_XREF", (Gedcom_ctxt)1);
60   if (xr != NULL)
61     return 11;
62   
63   xr = gedcom_add_xref(XREF_FAM, "@XREF@ BUT EXTRA", (Gedcom_ctxt)1);
64   if (xr != NULL)
65     return 12;
66   
67   xr = gedcom_add_xref(XREF_FAM, "@NEWFAM@", (Gedcom_ctxt)1);
68   if (xr == NULL)
69     return 13;
70
71   xr = gedcom_add_xref(XREF_FAM, "@NEWFAM@", (Gedcom_ctxt)2);
72   if (xr != NULL)
73     return 14;
74
75   xr = gedcom_get_by_xref("@NEWFAM@");
76   if (xr == NULL)
77     return 15;
78
79   if (xr->type != XREF_FAM) {
80     output(1, "Not the correct cross-reference type\n");
81     return 16;
82   }
83
84   if ((int)xr->object != 1) {
85     output(1, "Not the correct cross-reference object\n");
86     return 17;
87   }
88   
89   xr = gedcom_link_xref(XREF_INDI, "@NEWFAM@");
90   if (xr != NULL)
91     return 18;
92
93   xr = gedcom_link_xref(XREF_FAM, "@NEWFAM@");
94   if (xr == NULL)
95     return 19;
96
97   xr = gedcom_link_xref(XREF_FAM, "@NEWFAM");
98   if (xr != NULL)
99     return 20;
100
101   xr = gedcom_link_xref(XREF_FAM, "@NEWFAM@");
102   if (xr == NULL)
103     return 21;
104
105   xr = gedcom_link_xref(XREF_INDI, "@OLDINDI@");
106   if (xr != NULL)
107     return 22;
108
109   result = gedcom_delete_xref("@NEWFAM@");
110   if (result == 0)
111     return 23;
112
113   xr = gedcom_unlink_xref(XREF_INDI, "@NEWFAM@");
114   if (xr != NULL)
115     return 24;
116
117   xr = gedcom_unlink_xref(XREF_FAM, "@NEWFAM@");
118   if (xr == NULL)
119     return 25;
120
121   result = gedcom_delete_xref("@NEWFAM@");
122   if (result == 0)
123     return 26;
124
125   xr = gedcom_unlink_xref(XREF_FAM, "@NEWFAM@");
126   if (xr == NULL)
127     return 27;
128
129   result = gedcom_delete_xref("@NEWFAM@");
130   if (result != 0)
131     return 28;
132
133   return 0;
134 }
135
136 int main(int argc, char* argv[])
137 {
138   int result;
139   char* outfilename = NULL;
140   
141   if (argc > 1) {
142     int i;
143     for (i=1; i<argc; i++) {
144       if (!strncmp(argv[i], "-h", 3)) {
145         show_help();
146         exit(1);
147       }
148       else if (!strncmp(argv[i], "-q", 3)) {
149         output_set_quiet(1);
150       }
151       else if (!strncmp(argv[i], "-o", 3)) {
152         i++;
153         if (i < argc) {
154           outfilename = argv[i];
155         }
156         else {
157           printf ("Missing output file name\n");
158           show_help();
159           exit(1);
160         }
161       }
162       else {
163         printf ("Unrecognized option: %s\n", argv[i]);
164         show_help();
165         exit(1);
166       }
167     }
168   }
169   
170   gedcom_init();
171   setlocale(LC_ALL, "");
172   gedcom_set_message_handler(gedcom_message_handler);
173
174   output_open(outfilename);
175   
176   result = gedcom_new_model();
177   if (result == 0)
178     result |= test_xref_functions();
179   if (result == 0) {
180     output(1, "Test succeeded\n");
181   }
182   else {
183     output(1, "Test failed: %d\n", result);
184   }
185
186   output_close();
187   return result;
188 }