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

Annotation of /trunk/test/test.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2528 - (hide 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 johnpye 997 /* ASCEND modelling environment
2 jpye 1638 Copyright (C) 2005 Jerry St.Clair
3     Copyright (C) 2006 Carnegie Mellon University
4 johnpye 997
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 jpye 1638 */
22     #include <stdlib.h>
23     #include <string.h>
24 johnpye 997 #include <getopt.h>
25    
26 jpye 2018 #include <ascend/utilities/config.h>
27 jpye 2323 #include <ascend/general/platform.h>
28 jpye 2018 #include <ascend/utilities/error.h>
29     #include <ascend/compiler/redirectFile.h>
30 jpye 2322 #include <ascend/general/ascMalloc.h>
31 mahesh 2130
32 jpye 2017 #include "printutil.h"
33 mahesh 2130 #include "test_globals.h"
34 johnpye 997
35 mahesh 2130 #include <ascend/general/ospath.h>
36    
37 jpye 1638 #include <CUnit/Basic.h>
38    
39 jpye 2431 extern int register_cunit_tests();
40 johnpye 997
41 jpye 2528 extern char ASC_TEST_PATH[PATH_MAX];
42 mahesh 2130
43 johnpye 997 /*
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 jpye 2346 /* locate the '.' separator and copy bits before that into suitename. */
51 johnpye 997 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 jpye 1638 if(suite==NULL){
57     fprintf(stderr,"No suites present in registry!\n");
58     return CUE_NO_SUITENAME;
59     }
60    
61 johnpye 997 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 johnpye 1029 fprintf(stderr,"Running test %s (%p, %p)\n", n,suite,test);
70 johnpye 997 result = CU_basic_run_test(suite,test);
71 jpye 2346 fprintf(stderr,"Result code: %d\n",result);
72 johnpye 997 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 johnpye 1029 }
84 johnpye 997 }
85     suite = suite->pNext;
86     }
87     return CUE_NO_SUITENAME;
88     };
89    
90 jpye 2346 int list_suites(){
91     struct CU_TestRegistry *reg = CU_get_registry();
92     struct CU_Suite *suite = reg->pSuite;
93 jpye 2431 fprintf(stderr,"Test suites found in registry:\n");
94 jpye 2346 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 johnpye 997 /**
129     Main routine, handles command line options
130 jpye 1638 */
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 jpye 2346 char suitename[1000];
136     char list = 0;
137 johnpye 997
138 jpye 2528 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 mahesh 2130
144 johnpye 997 static struct option long_options[] = {
145 jpye 2346 {"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 johnpye 997 {0, 0, 0, 0}
154     };
155    
156     /* getopt_long stores the option index here. */
157     int option_index = 0;
158    
159 johnpye 1029 const char *usage =
160 johnpye 997 "%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 jpye 2346 " --help\n"
168     " --list-suites, -l\n"
169     " --list-tests=SUITENAME, -tSUITENAME\n"
170     ;
171 johnpye 997
172     char c;
173 jpye 2346 while(-1 != (c = getopt_long (argc, argv, "vsne:t:l", long_options, &option_index))){
174 johnpye 997 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 johnpye 1002 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 johnpye 1029 fprintf(stderr,"Invalid argument for --on-error option!\n");
191 jpye 2334 result = 1;
192 jpye 2431 goto cleanup;
193 johnpye 1002 }
194 johnpye 997 break;
195 jpye 2346 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 johnpye 997 case '?':
204     case 'h':
205     fprintf(stderr,usage,argv[0]);
206 jpye 2331 result = 1;
207     goto cleanup;
208 johnpye 997 default:
209     fprintf(stderr,"Unknown option -- '%c'", c);
210     fprintf(stderr,usage,argv[0]);
211 jpye 2331 result = 2;
212     goto cleanup;
213 johnpye 997 }
214     }
215 jpye 1638
216 johnpye 997 CU_initialize_registry();
217 jpye 1638 register_cunit_tests();
218     CU_basic_set_mode(mode);
219     CU_set_error_action(error_action);
220 johnpye 997
221 jpye 2346 if(list){
222     if(strlen(suitename)){
223     list_tests(suitename);
224     }else{
225     list_suites();
226     }
227     goto cleanup;
228     }
229    
230 johnpye 997 /* 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 jpye 2346 list_suites();
237 jpye 2331 result = 1;
238     goto cleanup;
239 johnpye 997 }else if(result==CUE_NO_TESTNAME){
240     fprintf(stderr,"Invalid test name '%s'\n", argv[optind]);
241 jpye 2346 list_tests(argv[optind]);
242 jpye 2331 result = 1;
243     goto cleanup;
244 johnpye 997 }
245 johnpye 1029 optind++;
246 johnpye 997 }
247     }else{
248     result = CU_basic_run_tests();
249     }
250 jpye 1638
251 jpye 2332 cleanup:
252 jpye 1638 if(mode == CU_BRM_VERBOSE)ascshutdown("Testing completed.");/* shut down memory manager */
253 jpye 2331 CU_cleanup_registry();
254 jpye 1638 return result;
255     }

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