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

Contents of /trunk/test/test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2431 - (show annotations) (download) (as text)
Sat Mar 26 04:32:34 2011 UTC (11 years, 3 months ago) by jpye
File MIME type: text/x-csrc
File size: 6999 byte(s)
Fix import issue with test suite on MinGW with gcc 4.x.
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 <ascend/utilities/config.h>
27 #include <ascend/general/platform.h>
28 #include <ascend/utilities/error.h>
29 #include <ascend/compiler/redirectFile.h>
30 #include <ascend/general/ascMalloc.h>
31
32 #include "printutil.h"
33 #include "test_globals.h"
34
35 #include <ascend/general/ospath.h>
36
37 #include <CUnit/Basic.h>
38
39 extern int register_cunit_tests();
40
41 extern struct FilePath* ASC_TEST_DIR;
42
43 /*
44 The following allows the CUnit tests to be run using a standalone executable
45 using the CUnit 'basic' interface.
46 */
47 int run_suite_or_test(char *name){
48 char suitename[1000];
49 char *s,*n;
50 /* locate the '.' separator and copy bits before that into suitename. */
51 for(s=suitename,n=name; *n!='.' && *n!='\0' && s < suitename+999; *s++=*n++);
52 *s='\0';
53 struct CU_TestRegistry *reg = CU_get_registry();
54 struct CU_Suite *suite = reg->pSuite;
55 struct CU_Test *test;
56 if(suite==NULL){
57 fprintf(stderr,"No suites present in registry!\n");
58 return CUE_NO_SUITENAME;
59 }
60
61 CU_ErrorCode result;
62 while(suite!=NULL){
63 if(0==strcmp(suite->pName,suitename)){
64 if(*n=='.'){
65 ++n;
66 test = suite->pTest;
67 while(test!=NULL){
68 if(0==strcmp(test->pName,n)){
69 fprintf(stderr,"Running test %s (%p, %p)\n", n,suite,test);
70 result = CU_basic_run_test(suite,test);
71 fprintf(stderr,"Result code: %d\n",result);
72 fprintf(stderr,"Result: %s\n",CU_get_error_msg());
73 return result;
74 }
75 test = test->pNext;
76 }
77 return CUE_NO_TESTNAME;
78 }else{
79 fprintf(stderr,"Running suite %s (%p)\n",suitename,suite);
80 result = CU_basic_run_suite(suite);
81 fprintf(stderr,"Result: %s\n",CU_get_error_msg());
82 return result;
83 }
84 }
85 suite = suite->pNext;
86 }
87 return CUE_NO_SUITENAME;
88 };
89
90 int list_suites(){
91 struct CU_TestRegistry *reg = CU_get_registry();
92 struct CU_Suite *suite = reg->pSuite;
93 fprintf(stderr,"Test suites found in registry:\n");
94 while(suite!=NULL){
95 fprintf(stderr,"\t%s\n", suite->pName);
96 suite = suite->pNext;
97 }
98 return CUE_NO_SUITENAME;
99 }
100
101 int list_tests(const char *suitename0){
102 char suitename[1000];
103 char *s;
104 const char *n;
105 /* locate the '.' separator and copy bits before that into suitename. */
106 for(s=suitename,n=suitename0; *n!='.' && *n!='\0' && s < suitename+999; *s++=*n++);
107 *s='\0';
108
109 struct CU_TestRegistry *reg = CU_get_registry();
110 struct CU_Suite *suite = reg->pSuite;
111 struct CU_Test *test;
112 while(suite!=NULL){
113 if(0==strcmp(suite->pName,suitename)){
114 fprintf(stderr,"Tests found in suite '%s':\n",suitename);
115 test = suite->pTest;
116 while(test!=NULL){
117 fprintf(stderr,"\t%s\n", test->pName);
118 test = test->pNext;
119 }
120 return CUE_NO_TESTNAME;
121 }
122 suite = suite->pNext;
123 }
124 fprintf(stderr,"Test suite '%s' not found in registry.\n",suitename);
125 return CUE_NO_SUITENAME;
126 }
127
128 /**
129 Main routine, handles command line options
130 */
131 int main(int argc, char* argv[]){
132 CU_BasicRunMode mode = CU_BRM_VERBOSE;
133 CU_ErrorAction error_action = CUEA_IGNORE;
134 CU_ErrorCode result;
135 char suitename[1000];
136 char list = 0;
137
138 struct FilePath* test_executable = ospath_new(argv[0]);
139 ASC_TEST_DIR = ospath_getdir(test_executable); /** Global Variable containing Path information about the test directory */
140
141 static struct option long_options[] = {
142 {"on-error", required_argument, 0, 'e'},
143 {"verbose", no_argument, 0, 'v'},
144 {"silent", no_argument, 0, 's'},
145 {"normal", no_argument, 0, 'n'},
146 {"help", no_argument, 0, '?'},
147 {"usage", no_argument, 0, '?'},
148 {"list-suites",no_argument, 0, 'l'},
149 {"list-tests", required_argument, 0, 't'},
150 {0, 0, 0, 0}
151 };
152
153 /* getopt_long stores the option index here. */
154 int option_index = 0;
155
156 const char *usage =
157 "%s -vsne [SuiteName|SuiteName.testname] ...\n"
158 "Test ASCEND base/generic routines\n"
159 "options:\n"
160 " --verbose, -v full output, including memory checking\n"
161 " --silent, -s\n"
162 " --normal, -n\n"
163 " --on-error=[fail|abort|ignore], -e\n"
164 " --help\n"
165 " --list-suites, -l\n"
166 " --list-tests=SUITENAME, -tSUITENAME\n"
167 ;
168
169 char c;
170 while(-1 != (c = getopt_long (argc, argv, "vsne:t:l", long_options, &option_index))){
171 switch(c){
172 case 'v': mode = CU_BRM_VERBOSE; break;
173 case 's': mode = CU_BRM_SILENT; break;
174 case 'n': mode = CU_BRM_NORMAL; break;
175 case 'e':
176 if(0==strcmp(optarg,"fail")){
177 fprintf(stderr,"on error FAIL\n");
178 error_action = CUEA_FAIL;
179 }else if(0==strcmp(optarg,"abort")){
180 fprintf(stderr,"on error ABORT\n");
181 error_action = CUEA_ABORT;
182 break;
183 }else if(0==strcmp(optarg,"ignore")){
184 error_action = CUEA_IGNORE;
185 }
186 else{
187 fprintf(stderr,"Invalid argument for --on-error option!\n");
188 result = 1;
189 goto cleanup;
190 }
191 break;
192 case 'l':
193 list = 1;
194 suitename[0] = '\0';
195 break;
196 case 't':
197 list = 1;
198 strncpy(suitename, optarg, 999);
199 break;
200 case '?':
201 case 'h':
202 fprintf(stderr,usage,argv[0]);
203 result = 1;
204 goto cleanup;
205 default:
206 fprintf(stderr,"Unknown option -- '%c'", c);
207 fprintf(stderr,usage,argv[0]);
208 result = 2;
209 goto cleanup;
210 }
211 }
212
213 CU_initialize_registry();
214 register_cunit_tests();
215 CU_basic_set_mode(mode);
216 CU_set_error_action(error_action);
217
218 if(list){
219 if(strlen(suitename)){
220 list_tests(suitename);
221 }else{
222 list_suites();
223 }
224 goto cleanup;
225 }
226
227 /* any remaining command-line arguments will be specific test suites and/or tests to run */
228 if(optind < argc){
229 while(optind < argc){
230 result = run_suite_or_test(argv[optind]);
231 if(result==CUE_NO_SUITENAME){
232 fprintf(stderr,"Invalid suite name '%s'\n", argv[optind]);
233 list_suites();
234 result = 1;
235 goto cleanup;
236 }else if(result==CUE_NO_TESTNAME){
237 fprintf(stderr,"Invalid test name '%s'\n", argv[optind]);
238 list_tests(argv[optind]);
239 result = 1;
240 goto cleanup;
241 }
242 optind++;
243 }
244 }else{
245 result = CU_basic_run_tests();
246 }
247
248 cleanup:
249 if(mode == CU_BRM_VERBOSE)ascshutdown("Testing completed.");/* shut down memory manager */
250 CU_cleanup_registry();
251 ospath_free(test_executable);
252 ospath_free(ASC_TEST_DIR);
253 return result;
254 }

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