source: icGREP/icgrep-devel/cudd-2.5.1/mtr/testmtr.c @ 5815

Last change on this file since 5815 was 4597, checked in by nmedfort, 4 years ago

Upload of the CUDD library.

File size: 9.0 KB
RevLine 
[4597]1/**CFile***********************************************************************
2 
3   FileName    [testmtr.c]
4
5   PackageName [mtr]
6
7   Synopsis    [Test program for the mtr package.]
8
9   Description []
10
11   SeeAlso     []
12
13   Author      [Fabio Somenzi]
14   
15  Copyright   [Copyright (c) 1995-2012, Regents of the University of Colorado
16
17  All rights reserved.
18
19  Redistribution and use in source and binary forms, with or without
20  modification, are permitted provided that the following conditions
21  are met:
22
23  Redistributions of source code must retain the above copyright
24  notice, this list of conditions and the following disclaimer.
25
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.
29
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.
33
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.]
46
47******************************************************************************/
48
49#include "util.h"
50#include "mtr.h"
51
52/*---------------------------------------------------------------------------*/
53/* Variable declarations                                                     */
54/*---------------------------------------------------------------------------*/
55
56#ifndef lint
57static char rcsid[] MTR_UNUSED = "$Id: testmtr.c,v 1.7 2015/01/05 20:12:13 fabio Exp $";
58#endif
59
60/*---------------------------------------------------------------------------*/
61/* Constant declarations                                                     */
62/*---------------------------------------------------------------------------*/
63
64#define TESTMTR_VERSION\
65    "TestMtr Version #0.6, Release date 2/6/12"
66
67/**AutomaticStart*************************************************************/
68
69/*---------------------------------------------------------------------------*/
70/* Static function prototypes                                                */
71/*---------------------------------------------------------------------------*/
72
73static void usage (char *prog);
74static FILE * open_file (const char *filename, const char *mode);
75static void printHeader(int argc, char **argv);
76
77/**AutomaticEnd***************************************************************/
78
79/*---------------------------------------------------------------------------*/
80/* Definition of exported functions                                          */
81/*---------------------------------------------------------------------------*/
82
83/**Function********************************************************************
84
85  Synopsis    [Main program for testmtr.]
86
87  Description [Main program for testmtr.  Performs initialization.
88  Reads command line options and network(s).  Builds some simple trees
89  and prints them out.]
90
91  SideEffects [None]
92
93  SeeAlso     []
94
95******************************************************************************/
96int
97main(
98  int  argc,
99  char ** argv)
100{
101    MtrNode *root,
102            *node;
103    int     i,
104            c,
105            pr = 0;
106    FILE    *fp;
107    const char *file = NULL;
108   
109    while ((c = getopt(argc, argv, "Mhp:")) != EOF) {
110        switch(c) {
111        case 'M':
112            break;
113        case 'p':
114            pr = atoi(optarg);
115            break;
116        case 'h':
117        default:
118            printHeader(argc, argv);
119            usage(argv[0]);
120            break;
121        }
122    }
123
124    if (argc - optind == 0) {
125        file = "-";
126    } else if (argc - optind == 1) {
127        file = argv[optind];
128    } else {
129        printHeader(argc, argv);
130        usage(argv[0]);
131    }
132    if (pr > 0)
133        printHeader(argc, argv);
134
135    /* Create and print a simple tree. */
136    root = Mtr_InitTree();
137    root->flags = 0;
138    node = Mtr_CreateFirstChild(root);
139    node->flags = 1;
140    node = Mtr_CreateLastChild(root);
141    node->flags = 2;
142    node = Mtr_CreateFirstChild(root);
143    node->flags = 3;
144    node = Mtr_AllocNode();
145    node->child = NULL;
146    node->flags = 4;
147    Mtr_MakeNextSibling(root->child,node);
148    if (pr > 0) {
149        Mtr_PrintTree(root);
150        (void) printf("#------------------------\n");
151    }
152    Mtr_FreeTree(root);
153
154    /* Create an initial tree in which all variables belong to one group. */
155    root = Mtr_InitGroupTree(0,12);
156    if (pr > 0) {
157        Mtr_PrintTree(root); (void) printf("#  ");
158        Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
159    }
160    node = Mtr_MakeGroup(root,0,6,MTR_DEFAULT);
161    node = Mtr_MakeGroup(root,6,6,MTR_DEFAULT);
162    if (pr > 0) {
163        Mtr_PrintTree(root); (void) printf("#  ");
164        Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
165    }
166    for (i = 0; i < 6; i+=2) {
167        node = Mtr_MakeGroup(root,(unsigned) i,(unsigned) 2,MTR_DEFAULT);
168    }
169    node = Mtr_MakeGroup(root,0,12,MTR_FIXED);
170    if (pr > 0) {
171        Mtr_PrintTree(root); (void) printf("#  ");
172        Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
173        /* Print a partial tree. */
174        (void) printf("#  ");
175        Mtr_PrintGroups(root->child,pr == 0); (void) printf("\n");
176    }
177    node = Mtr_FindGroup(root,0,6);
178    node = Mtr_DissolveGroup(node);
179    if (pr > 0) {
180        Mtr_PrintTree(root); (void) printf("#  ");
181        Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
182    }
183    node = Mtr_FindGroup(root,4,2);
184    if (!Mtr_SwapGroups(node,node->younger)) {
185        (void) printf("error in Mtr_SwapGroups\n");
186        return 3;
187    }
188    if (pr > 0) {
189        Mtr_PrintTree(root); (void) printf("#  ");
190        Mtr_PrintGroups(root,pr == 0);
191        (void) printf("#------------------------\n");
192    }
193    Mtr_FreeTree(root);
194
195    /* Create a group tree with fixed subgroups. */
196    root = Mtr_InitGroupTree(0,4);
197    if (pr > 0) {
198        Mtr_PrintTree(root); (void) printf("#  ");
199        Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
200    }
201    node = Mtr_MakeGroup(root,0,2,MTR_FIXED);
202    node = Mtr_MakeGroup(root,2,2,MTR_FIXED);
203    if (pr > 0) {
204        Mtr_PrintTree(root); (void) printf("#  ");
205        Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
206    }
207    Mtr_FreeTree(root);
208    if (pr > 0) {
209        (void) printf("#------------------------\n");
210    }
211
212    /* Open input file. */
213    fp = open_file(file, "r");
214    root = Mtr_ReadGroups(fp,12);
215    fclose(fp);
216    if (pr > 0) {
217        if (root) {
218            Mtr_PrintTree(root); (void) printf("#  ");
219            Mtr_PrintGroups(root,pr == 0); (void) printf("\n");
220        } else {
221            (void) printf("error in group file\n");
222        }
223    }
224    Mtr_FreeTree(root);
225
226    return 0;
227
228} /* end of main */
229
230
231/**Function********************************************************************
232
233  Synopsis    [Prints usage message and exits.]
234
235  Description []
236
237  SideEffects [none]
238
239  SeeAlso     []
240
241******************************************************************************/
242static void
243usage(
244  char * prog)
245{
246    (void) fprintf(stderr, "usage: %s [options] [file]\n", prog);
247    (void) fprintf(stderr, "   -M\t\tturns off memory allocation recording\n");
248    (void) fprintf(stderr, "   -h\t\tprints this message\n");
249    (void) fprintf(stderr, "   -p n\t\tcontrols verbosity\n");
250    exit(2);
251
252} /* end of usage */
253
254
255/**Function********************************************************************
256
257  Synopsis    [Opens a file.]
258
259  Description [Opens a file, or fails with an error message and exits.
260  Allows '-' as a synonym for standard input.]
261
262  SideEffects [None]
263
264  SeeAlso     []
265
266******************************************************************************/
267static FILE *
268open_file(
269  const char * filename,
270  const char * mode)
271{
272    FILE *fp;
273
274    if (strcmp(filename, "-") == 0) {
275        return mode[0] == 'r' ? stdin : stdout;
276    } else if ((fp = fopen(filename, mode)) == NULL) {
277        perror(filename);
278        exit(1);
279    }
280    return(fp);
281
282} /* end of open_file */
283
284
285/**Function********************************************************************
286
287  Synopsis    [Prints the header of the program output.]
288
289  Description [Prints the header of the program output.]
290
291  SideEffects [None]
292
293  SeeAlso     []
294
295******************************************************************************/
296static void
297printHeader(
298  int argc,
299  char **argv)
300{
301    int i;
302
303    (void) printf("# %s\n", TESTMTR_VERSION);
304    /* Echo command line and arguments. */
305    (void) printf("#");
306    for(i = 0; i < argc; i++) {
307        (void) printf(" %s", argv[i]);
308    }
309    (void) printf("\n");
310    (void) fflush(stdout);
311}
Note: See TracBrowser for help on using the repository browser.