/[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 997 - (show annotations) (download) (as text)
Sun Dec 24 01:33:59 2006 UTC (14 years, 1 month ago) by johnpye
File MIME type: text/x-csrc
File size: 5026 byte(s)
Refactored tests into a shared library of tests and a executable of
just the CUnit driver. This will allow the CUnit test suite to be executed via
python unittest as part of the growing Python-based test suite.
Renamed setjmp and longjmp to SETJMP and LONGJMP throughout ASCEND, to allow
some debugging output to be added at each call.
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 CU_ErrorCode result;
50 while(suite!=NULL){
51 fprintf(stderr,"Looking at suite %s\n", suite->pName);
52 if(0==strcmp(suite->pName,suitename)){
53 fprintf(stderr,"Found suite %s\n", suitename);
54 if(*n=='.'){
55 ++n;
56 fprintf(stderr,"Looking for test %s\n", n);
57 test = suite->pTest;
58 while(test!=NULL){
59 fprintf(stderr,"Found test %s\n", test->pName);
60 if(0==strcmp(test->pName,n)){
61 fprintf(stderr,"Running test %s (%p, %p)\n", n,suite,test);
62 result = CU_basic_run_test(suite,test);
63 fprintf(stderr,"Result = %d\n",result);
64 fprintf(stderr,"Result: %s\n",CU_get_error_msg());
65 return result;
66 }
67 test = test->pNext;
68 }
69 return CUE_NO_TESTNAME;
70 }else{
71 fprintf(stderr,"Running suite %s (%p)\n",suitename,suite);
72 result = CU_basic_run_suite(suite);
73 fprintf(stderr,"Result = %d\n",result);
74 fprintf(stderr,"Result: %s\n",CU_get_error_msg());
75 return result;
76 }
77 }
78 suite = suite->pNext;
79 }
80 return CUE_NO_SUITENAME;
81 };
82
83 /**
84 Main routine, handles command line options
85 */
86 int main(int argc, char* argv[]){
87 CU_BasicRunMode mode = CU_BRM_VERBOSE;
88 CU_ErrorAction error_action = CUEA_IGNORE;
89 CU_ErrorCode result;
90
91 static struct option long_options[] = {
92 {"on-error", required_argument, 0, 'e'},
93 {"verbose", no_argument, 0, 'v'},
94 {"silent", no_argument, 0, 's'},
95 {"normal", no_argument, 0, 'n'},
96 {"help", no_argument, 0, '?'},
97 {"usage", no_argument, 0, '?'},
98 {0, 0, 0, 0}
99 };
100
101 /* getopt_long stores the option index here. */
102 int option_index = 0;
103
104 const char *usage =
105 "%s -vsne [SuiteName|SuiteName.testname] ...\n"
106 "Test ASCEND base/generic routines\n"
107 "options:\n"
108 " --verbose, -v full output, including memory checking\n"
109 " --silent, -s\n"
110 " --normal, -n\n"
111 " --on-error=[fail|abort|ignore], -e\n"
112 " --help\n";
113
114 char c;
115 while(-1 != (c = getopt_long (argc, argv, "vsne:", long_options, &option_index))){
116 switch(c){
117 case 'v': mode = CU_BRM_VERBOSE; break;
118 case 's': mode = CU_BRM_SILENT; break;
119 case 'n': mode = CU_BRM_NORMAL; break;
120 case 'e':
121 fprintf(stderr,"Got option 'e'\n"); exit(1);
122 if(0==strcmp(optarg,"fail")) error_action = CUEA_FAIL;
123 else if(0==strcmp(optarg,"abort")) error_action = CUEA_ABORT;
124 else if(0==strcmp(optarg,"ignore")) error_action = CUEA_IGNORE;
125 else fprintf(stderr,"Invalid argument for --on-error option!\n"); exit(1);
126 break;
127 case '?':
128 case 'h':
129 fprintf(stderr,usage,argv[0]);
130 exit(1);
131 default:
132 fprintf(stderr,"Unknown option -- '%c'", c);
133 fprintf(stderr,usage,argv[0]);
134 exit(2);
135 }
136 }
137
138 CU_initialize_registry();
139 register_cunit_tests();
140 CU_basic_set_mode(mode);
141 CU_set_error_action(error_action);
142
143 /* any remaining command-line arguments will be specific test suites and/or tests to run */
144 if(optind < argc){
145 while(optind < argc){
146 result = run_suite_or_test(argv[optind]);
147 if(result==CUE_NO_SUITENAME){
148 fprintf(stderr,"Invalid suite name '%s'\n", argv[optind]);
149 exit(1);
150 }else if(result==CUE_NO_TESTNAME){
151 fprintf(stderr,"Invalid test name '%s'\n", argv[optind]);
152 exit(1);
153 }
154 optind++;
155 }
156 }else{
157 result = CU_basic_run_tests();
158 }
159
160 CU_cleanup_registry();
161
162 if(mode == CU_BRM_VERBOSE)ascshutdown("Testing completed.");/* shut down memory manager */
163
164 fprintf(stderr,"RETURN CODE = %d\n", result);
165 return result;
166 }

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