testmtr.c 7.9 KB

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