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

Contents of /trunk/test/test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2528 - (show annotations) (download) (as text)
Fri Jan 20 03:57:39 2012 UTC (10 years, 6 months ago) by jpye
File MIME type: text/x-csrc
File size: 7057 byte(s)
Resolve memory leak in test_mem.c by converting test.c FilePath to a char[].
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 char ASC_TEST_PATH[PATH_MAX];
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 struct FilePath *test_dir = ospath_getdir(test_executable); /** Global Variable containing Path information about the test directory */
140 ospath_strncpy(test_dir,ASC_TEST_PATH,PATH_MAX);
141 ospath_free(test_dir);
142 ospath_free(test_executable);
143
144 static struct option long_options[] = {
145 {"on-error", required_argument, 0, 'e'},
146 {"verbose", no_argument, 0, 'v'},
147 {"silent", no_argument, 0, 's'},
148 {"normal", no_argument, 0, 'n'},
149 {"help", no_argument, 0, '?'},
150 {"usage", no_argument, 0, '?'},
151 {"list-suites",no_argument, 0, 'l'},
152 {"list-tests", required_argument, 0, 't'},
153 {0, 0, 0, 0}
154 };
155
156 /* getopt_long stores the option index here. */
157 int option_index = 0;
158
159 const char *usage =
160 "%s -vsne [SuiteName|SuiteName.testname] ...\n"
161 "Test ASCEND base/generic routines\n"
162 "options:\n"
163 " --verbose, -v full output, including memory checking\n"
164 " --silent, -s\n"
165 " --normal, -n\n"
166 " --on-error=[fail|abort|ignore], -e\n"
167 " --help\n"
168 " --list-suites, -l\n"
169 " --list-tests=SUITENAME, -tSUITENAME\n"
170 ;
171
172 char c;
173 while(-1 != (c = getopt_long (argc, argv, "vsne:t:l", long_options, &option_index))){
174 switch(c){
175 case 'v': mode = CU_BRM_VERBOSE; break;
176 case 's': mode = CU_BRM_SILENT; break;
177 case 'n': mode = CU_BRM_NORMAL; break;
178 case 'e':
179 if(0==strcmp(optarg,"fail")){
180 fprintf(stderr,"on error FAIL\n");
181 error_action = CUEA_FAIL;
182 }else if(0==strcmp(optarg,"abort")){
183 fprintf(stderr,"on error ABORT\n");
184 error_action = CUEA_ABORT;
185 break;
186 }else if(0==strcmp(optarg,"ignore")){
187 error_action = CUEA_IGNORE;
188 }
189 else{
190 fprintf(stderr,"Invalid argument for --on-error option!\n");
191 result = 1;
192 goto cleanup;
193 }
194 break;
195 case 'l':
196 list = 1;
197 suitename[0] = '\0';
198 break;
199 case 't':
200 list = 1;
201 strncpy(suitename, optarg, 999);
202 break;
203 case '?':
204 case 'h':
205 fprintf(stderr,usage,argv[0]);
206 result = 1;
207 goto cleanup;
208 default:
209 fprintf(stderr,"Unknown option -- '%c'", c);
210 fprintf(stderr,usage,argv[0]);
211 result = 2;
212 goto cleanup;
213 }
214 }
215
216 CU_initialize_registry();
217 register_cunit_tests();
218 CU_basic_set_mode(mode);
219 CU_set_error_action(error_action);
220
221 if(list){
222 if(strlen(suitename)){
223 list_tests(suitename);
224 }else{
225 list_suites();
226 }
227 goto cleanup;
228 }
229
230 /* any remaining command-line arguments will be specific test suites and/or tests to run */
231 if(optind < argc){
232 while(optind < argc){
233 result = run_suite_or_test(argv[optind]);
234 if(result==CUE_NO_SUITENAME){
235 fprintf(stderr,"Invalid suite name '%s'\n", argv[optind]);
236 list_suites();
237 result = 1;
238 goto cleanup;
239 }else if(result==CUE_NO_TESTNAME){
240 fprintf(stderr,"Invalid test name '%s'\n", argv[optind]);
241 list_tests(argv[optind]);
242 result = 1;
243 goto cleanup;
244 }
245 optind++;
246 }
247 }else{
248 result = CU_basic_run_tests();
249 }
250
251 cleanup:
252 if(mode == CU_BRM_VERBOSE)ascshutdown("Testing completed.");/* shut down memory manager */
253 CU_cleanup_registry();
254 return result;
255 }

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