emergency commit
[cl-cudd.git] / distr / mtr / testmtr.c
blob632d76c882f775d381c555ebf6221a080b6bc43d
1 /**CFile***********************************************************************
3 FileName [testmtr.c]
5 PackageName [mtr]
7 Synopsis [Test program for the mtr package.]
9 Description []
11 SeeAlso []
13 Author [Fabio Somenzi]
15 Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
17 All rights reserved.
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions
21 are met:
23 Redistributions of source code must retain the above copyright
24 notice, this list of conditions and the following disclaimer.
26 Redistributions in binary form must reproduce the above copyright
27 notice, this list of conditions and the following disclaimer in the
28 documentation and/or other materials provided with the distribution.
30 Neither the name of the University of Colorado nor the names of its
31 contributors may be used to endorse or promote products derived from
32 this software without specific prior written permission.
34 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
38 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
39 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
40 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
42 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
44 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 POSSIBILITY OF SUCH DAMAGE.]
47 ******************************************************************************/
49 #include "util.h"
50 #include "mtr.h"
52 /*---------------------------------------------------------------------------*/
53 /* Variable declarations */
54 /*---------------------------------------------------------------------------*/
56 #ifndef lint
57 static char rcsid[] MTR_UNUSED = "$Id: testmtr.c,v 1.3 2004/08/13 18:15:12 fabio Exp $";
58 #endif
60 /*---------------------------------------------------------------------------*/
61 /* Constant declarations */
62 /*---------------------------------------------------------------------------*/
64 #define TESTMTR_VERSION\
65 "TestMtr Version #0.5, Release date 8/26/99"
67 /**AutomaticStart*************************************************************/
69 /*---------------------------------------------------------------------------*/
70 /* Static function prototypes */
71 /*---------------------------------------------------------------------------*/
73 static void usage (char *prog);
74 static FILE * open_file (char *filename, char *mode);
76 /**AutomaticEnd***************************************************************/
78 /*---------------------------------------------------------------------------*/
79 /* Definition of exported functions */
80 /*---------------------------------------------------------------------------*/
82 /**Function********************************************************************
84 Synopsis [Main program for testmtr.]
86 Description [Main program for testmtr. Performs initialization.
87 Reads command line options and network(s). Builds some simple trees
88 and prints them out.]
90 SideEffects [None]
92 SeeAlso []
94 ******************************************************************************/
95 int
96 main(
97 int argc,
98 char ** argv)
100 MtrNode *root,
101 *node;
102 int i,
104 pr = 0;
105 FILE *fp;
106 char *file = NULL;
108 (void) printf("# %s\n", TESTMTR_VERSION);
109 /* Echo command line and arguments. */
110 (void) printf("#");
111 for(i = 0; i < argc; i++) {
112 (void) printf(" %s", argv[i]);
114 (void) printf("\n");
115 (void) fflush(stdout);
117 while ((c = getopt(argc, argv, "Mhp:")) != EOF) {
118 switch(c) {
119 case 'M':
120 #ifdef MNEMOSYNE
121 (void) mnem_setrecording(0);
122 #endif
123 break;
124 case 'p':
125 pr = atoi(optarg);
126 break;
127 case 'h':
128 default:
129 usage(argv[0]);
130 break;
134 if (argc - optind == 0) {
135 file = "-";
136 } else if (argc - optind == 1) {
137 file = argv[optind];
138 } else {
139 usage(argv[0]);
142 /* Create and print a simple tree. */
143 root = Mtr_InitTree();
144 root->flags = 0;
145 node = Mtr_CreateFirstChild(root);
146 node->flags = 1;
147 node = Mtr_CreateLastChild(root);
148 node->flags = 2;
149 node = Mtr_CreateFirstChild(root);
150 node->flags = 3;
151 node = Mtr_AllocNode();
152 node->flags = 4;
153 Mtr_MakeNextSibling(root->child,node);
154 Mtr_PrintTree(root);
155 Mtr_FreeTree(root);
156 (void) printf("#------------------------\n");
158 /* Create an initial tree in which all variables belong to one group. */
159 root = Mtr_InitGroupTree(0,12);
160 Mtr_PrintTree(root); (void) printf("# ");
161 Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
162 node = Mtr_MakeGroup(root,0,6,MTR_DEFAULT);
163 node = Mtr_MakeGroup(root,6,6,MTR_DEFAULT);
164 Mtr_PrintTree(root); (void) printf("# ");
165 Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
166 for (i = 0; i < 6; i+=2) {
167 node = Mtr_MakeGroup(root,(unsigned) i,(unsigned) 2,MTR_DEFAULT);
169 node = Mtr_MakeGroup(root,0,12,MTR_FIXED);
170 Mtr_PrintTree(root); (void) printf("# ");
171 Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
172 /* Print a partial tree. */
173 (void) printf("# ");
174 Mtr_PrintGroups(root->child,pr == 0); (void) printf("\n");
175 node = Mtr_FindGroup(root,0,6);
176 node = Mtr_DissolveGroup(node);
177 Mtr_PrintTree(root); (void) printf("# ");
178 Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
179 node = Mtr_FindGroup(root,4,2);
180 if (!Mtr_SwapGroups(node,node->younger)) {
181 (void) printf("error in Mtr_SwapGroups\n");
182 exit(3);
184 Mtr_PrintTree(root); (void) printf("# ");
185 Mtr_PrintGroups(root,pr == 0);
186 Mtr_FreeTree(root);
187 (void) printf("#------------------------\n");
189 /* Create a group tree with fixed subgroups. */
190 root = Mtr_InitGroupTree(0,4);
191 Mtr_PrintTree(root); (void) printf("# ");
192 Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
193 node = Mtr_MakeGroup(root,0,2,MTR_FIXED);
194 node = Mtr_MakeGroup(root,2,2,MTR_FIXED);
195 Mtr_PrintTree(root); (void) printf("# ");
196 Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
197 Mtr_FreeTree(root);
198 (void) printf("#------------------------\n");
200 /* Open input file. */
201 fp = open_file(file, "r");
202 root = Mtr_ReadGroups(fp,12);
203 Mtr_PrintTree(root); (void) printf("# ");
204 Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
206 Mtr_FreeTree(root);
208 #ifdef MNEMOSYNE
209 mnem_writestats();
210 #endif
212 exit(0);
213 /* NOTREACHED */
215 } /* end of main */
218 /**Function********************************************************************
220 Synopsis [Prints usage message and exits.]
222 Description []
224 SideEffects [none]
226 SeeAlso []
228 ******************************************************************************/
229 static void
230 usage(
231 char * prog)
233 (void) fprintf(stderr, "usage: %s [options] [file]\n", prog);
234 (void) fprintf(stderr, " -M\t\tturns off memory allocation recording\n");
235 (void) fprintf(stderr, " -h\t\tprints this message\n");
236 (void) fprintf(stderr, " -p n\t\tcontrols verbosity\n");
237 exit(2);
239 } /* end of usage */
242 /**Function********************************************************************
244 Synopsis [Opens a file.]
246 Description [Opens a file, or fails with an error message and exits.
247 Allows '-' as a synonym for standard input.]
249 SideEffects [None]
251 SeeAlso []
253 ******************************************************************************/
254 static FILE *
255 open_file(
256 char * filename,
257 char * mode)
259 FILE *fp;
261 if (strcmp(filename, "-") == 0) {
262 return mode[0] == 'r' ? stdin : stdout;
263 } else if ((fp = fopen(filename, mode)) == NULL) {
264 perror(filename);
265 exit(1);
267 return(fp);
269 } /* end of open_file */