/[ascend]/trunk/base/generic/test/test.c
ViewVC logotype

Contents of /trunk/base/generic/test/test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1029 - (show annotations) (download) (as text)
Wed Jan 3 14:00:27 2007 UTC (17 years, 11 months ago) by johnpye
File MIME type: text/x-csrc
File size: 5065 byte(s)
Fixed for reporting of METHOD errors in PyGTK GUI.
Removed libasctest target (was causing messed up build on Windows)
Removed attempt to link libasctestsuite to -ldl on Windows (not necessary)
Adding reporting in test.c when no tests have been registered (to flag a problem with static/dynamic linking of CUnit on Windows)
1 /* ASCEND modelling environment
2 Copyright (C) 2005 Jerry St.Clair
3 Copyright (C) 2006 Carnegie Mellon University
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 *//**
20 Test runner for the 'base/generic' routines in ASCEND
21 */
22 #include <stdlib.h>
23 #include <string.h>
24 #include <getopt.h>
25
26 #include <utilities/config.h>
27 #include <utilities/ascConfig.h>
28 #include <utilities/error.h>
29 #include <compiler/redirectFile.h>
30 #include <utilities/ascMalloc.h>
31 #include <printutil.h>
32
33 #include <CUnit/Basic.h>
34
35 ASC_IMPORT(int) register_cunit_tests();
36
37 /*
38 The following allows the CUnit tests to be run using a standalone executable
39 using the CUnit 'basic' interface.
40 */
41 int run_suite_or_test(char *name){
42 char suitename[1000];
43 char *s,*n;
44 for(s=suitename,n=name; *n!='.' && *n!='\0' && s < suitename+999; *s++=*n++);
45 *s='\0';
46 struct CU_TestRegistry *reg = CU_get_registry();
47 struct CU_Suite *suite = reg->pSuite;
48 struct CU_Test *test;
49 if(suite==NULL){
50 fprintf(stderr,"No suites present in registry!\n");
51 return CUE_NO_SUITENAME;
52 }
53
54 CU_ErrorCode result;
55 while(suite!=NULL){
56 fprintf(stderr,"Looking at suite %s\n", suite->pName);
57 if(0==strcmp(suite->pName,suitename)){
58 fprintf(stderr,"Found suite %s\n", suitename);
59 if(*n=='.'){
60 ++n;
61 fprintf(stderr,"Looking for test %s\n", n);
62 test = suite->pTest;
63 while(test!=NULL){
64 fprintf(stderr,"Found test %s\n", test->pName);
65 if(0==strcmp(test->pName,n)){
66 fprintf(stderr,"Running test %s (%p, %p)\n", n,suite,test);
67 result = CU_basic_run_test(suite,test);
68 fprintf(stderr,"Result: %s\n",CU_get_error_msg());
69 return result;
70 }
71 test = test->pNext;
72 }
73 return CUE_NO_TESTNAME;
74 }else{
75 fprintf(stderr,"Running suite %s (%p)\n",suitename,suite);
76 result = CU_basic_run_suite(suite);
77 fprintf(stderr,"Result: %s\n",CU_get_error_msg());
78 return result;
79 }
80 }
81 suite = suite->pNext;
82 }
83 return CUE_NO_SUITENAME;
84 };
85
86 /**
87 Main routine, handles command line options
88 */
89 int main(int argc, char* argv[]){
90 CU_BasicRunMode mode = CU_BRM_VERBOSE;
91 CU_ErrorAction error_action = CUEA_IGNORE;
92 CU_ErrorCode result;
93
94 static struct option long_options[] = {
95 {"on-error", required_argument, 0, 'e'},
96 {"verbose", no_argument, 0, 'v'},
97 {"silent", no_argument, 0, 's'},
98 {"normal", no_argument, 0, 'n'},
99 {"help", no_argument, 0, '?'},
100 {"usage", no_argument, 0, '?'},
101 {0, 0, 0, 0}
102 };
103
104 /* getopt_long stores the option index here. */
105 int option_index = 0;
106
107 const char *usage =
108 "%s -vsne [SuiteName|SuiteName.testname] ...\n"
109 "Test ASCEND base/generic routines\n"
110 "options:\n"
111 " --verbose, -v full output, including memory checking\n"
112 " --silent, -s\n"
113 " --normal, -n\n"
114 " --on-error=[fail|abort|ignore], -e\n"
115 " --help\n";
116
117 char c;
118 while(-1 != (c = getopt_long (argc, argv, "vsne:", long_options, &option_index))){
119 switch(c){
120 case 'v': mode = CU_BRM_VERBOSE; break;
121 case 's': mode = CU_BRM_SILENT; break;
122 case 'n': mode = CU_BRM_NORMAL; break;
123 case 'e':
124 if(0==strcmp(optarg,"fail")){
125 fprintf(stderr,"on error FAIL\n");
126 error_action = CUEA_FAIL;
127 }else if(0==strcmp(optarg,"abort")){
128 fprintf(stderr,"on error ABORT\n");
129 error_action = CUEA_ABORT;
130 break;
131 }else if(0==strcmp(optarg,"ignore")){
132 error_action = CUEA_IGNORE;
133 }
134 else{
135 fprintf(stderr,"Invalid argument for --on-error option!\n");
136 exit(1);
137 }
138 break;
139 case '?':
140 case 'h':
141 fprintf(stderr,usage,argv[0]);
142 exit(1);
143 default:
144 fprintf(stderr,"Unknown option -- '%c'", c);
145 fprintf(stderr,usage,argv[0]);
146 exit(2);
147 }
148 }
149
150 CU_initialize_registry();
151 register_cunit_tests();
152 CU_basic_set_mode(mode);
153 CU_set_error_action(error_action);
154
155 /* any remaining command-line arguments will be specific test suites and/or tests to run */
156 if(optind < argc){
157 while(optind < argc){
158 result = run_suite_or_test(argv[optind]);
159 if(result==CUE_NO_SUITENAME){
160 fprintf(stderr,"Invalid suite name '%s'\n", argv[optind]);
161 exit(1);
162 }else if(result==CUE_NO_TESTNAME){
163 fprintf(stderr,"Invalid test name '%s'\n", argv[optind]);
164 exit(1);
165 }
166 optind++;
167 }
168 }else{
169 result = CU_basic_run_tests();
170 }
171
172 CU_cleanup_registry();
173
174 if(mode == CU_BRM_VERBOSE)ascshutdown("Testing completed.");/* shut down memory manager */
175
176 return result;
177 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22