You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
8.2 KiB

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