summaryrefslogtreecommitdiff
path: root/tests/glean/main.cpp
blob: 77fae4ff8cc1aa2e9a24540923b5c915b727ebf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// BEGIN_COPYRIGHT -*- glean -*-
// 
// Copyright (C) 1999  Allen Akin   All Rights Reserved.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the
// Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL ALLEN AKIN BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// 
// END_COPYRIGHT




// main.cpp:  main program for Glean

using namespace std;

#include <cassert>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "options.h"
#include "environ.h"
#include "test.h"
#include "version.h"
#include "lex.h"
#include "dsfilt.h"

using namespace GLEAN;

char* mandatoryArg(int argc, char* argv[], int i);
void selectTests(Options& o, vector<string>& allTestNames, int argc,
        char* argv[], int i);
void usage(char* command);
void listTests(const Test *tests, bool verbose);

int
main(int argc, char* argv[]) {

	// Until someone gets around to writing a fancy GUI front-end,
	// we'll set options the old-fashioned way.
	Options o;

        vector<string> allTestNames;
        for (Test* t = Test::testList; t; t = t->nextTest)
                allTestNames.push_back(t->name);
        sort(allTestNames.begin(), allTestNames.end());
        o.selectedTests = allTestNames;

	for (int i = 1; i < argc; ++i) {
		if (!strcmp(argv[i], "--help")) {
			usage(argv[0]);
		} else if (!strcmp(argv[i], "-v")
		    || !strcmp(argv[i], "--verbose")) {
			++o.verbosity;
		} else if (!strcmp(argv[i], "-r")
		    || !strcmp(argv[i], "--run")) {
			o.mode = Options::run;
			++i;
			o.db1Name = mandatoryArg(argc, argv, i);
		} else if (!strcmp(argv[i], "-o")
		    || !strcmp(argv[i], "--overwrite")) {
			o.overwrite = true;
		} else if (!strcmp(argv[i], "-c")
		    || !strcmp(argv[i], "--compare")) {
			o.mode = Options::compare;
			++i;
			o.db1Name = mandatoryArg(argc, argv, i);
			++i;
			o.db2Name = mandatoryArg(argc, argv, i);
		} else if (!strcmp(argv[i], "--visuals")) {
			++i;
			o.visFilter = mandatoryArg(argc, argv, i);
		} else if (!strcmp(argv[i], "-t")
			   || !strcmp(argv[i], "--tests")) {
			++i;
			selectTests(o, allTestNames, argc, argv, i);
		} else if (!strcmp(argv[i], "--listtests")) {
			o.mode = Options::listtests;
#	    if defined(__X11__)
		} else if (!strcmp(argv[i], "-display")
		    || !strcmp(argv[i], "--display")) {
			++i;
			o.dpyName = mandatoryArg(argc, argv, i);
#	    endif
		} else {
			usage(argv[0]);
		}
	}

	if (o.mode == Options::notSet)
		usage(argv[0]);

	if (o.mode == Options::listtests) {
		listTests(Test::testList, o.verbosity);
		exit(0);
	}

	// Create the test environment, then invoke each test to generate
	// results or compare two previous runs.
	try {
		Environment e(o);
		switch (o.mode) {
		case Options::run:
		{
			for (Test* t = Test::testList; t; t = t->nextTest)
                                if (binary_search(o.selectedTests.begin(),
                                    o.selectedTests.end(), t->name))
                                        t->run(e);
			break;
		}
		case Options::compare:
		{
			for (Test* t = Test::testList; t; t = t->nextTest)
				if (binary_search(o.selectedTests.begin(),
				    o.selectedTests.end(), t->name))
					try {
						t->compare(e);
					}
					catch (Test::CantOpenResultsFile e) {
						// For comparisons, we want to
						// continue running even if a
						// test result file can't be
						// opened.  We report the
						// problem here, but don't exit
						// as we would in the catch{}
						// below.
						cerr << "Can't open results file for test "
							<< e.testName
							<< " in database "
							<< e.dbName
							<< '\n';
					}
			break;
		}
		default:
			cerr << "Bad run mode in main()\n";
			break;
		}
	}
#if defined(__X11__)
	catch (WindowSystem::CantOpenDisplay) {
		cerr << "can't open display " << o.dpyName << "\n";
		exit(1);
	}
#endif
	catch (WindowSystem::NoOpenGL) {
		cerr << "display doesn't support OpenGL\n";
		exit(1);
	}
	catch (DrawingSurfaceFilter::Syntax e) {
		cerr << "Syntax error in visual selection criteria:\n"
			"'" << o.visFilter << "'\n";
		for (int i = 0; i < e.position; ++i)
			cerr << ' ';
		cerr << "^ " << e.err << '\n';
		exit(1);
	}
	catch (Environment::DBExists) {
		cerr << "Won't overwrite existing database " << o.db1Name
			<< "\n";
		exit(1);
	}
	catch (Environment::DBCantOpen e) {
		cerr << "Can't open database directory " << *e.db << "\n";
		exit(1);
	}
	catch (Test::CantOpenResultsFile e) {
		cerr << "Can't open results file for test " << e.testName
			<< " in database " << e.dbName << '\n';
		exit(1);
	}
	catch (...) {
		cerr << "caught an unexpected error in main()\n";
		exit(1);
	}

	return 0;
} // main


char*
mandatoryArg(int argc, char* argv[], int i) {
	if (i < argc && argv[i][0] != '-')
		return argv[i];
	usage(argv[0]);
	/*NOTREACHED*/
	return 0;
} // mandatoryArg


void
selectTests(Options& o, vector<string>& allTestNames, int argc, char* argv[],
    int i) {
        if (i >= argc)
                usage(argv[0]);

        // At present, we deal with the following syntax:
        //      [+] testname {(+|-) testname}
        //              Assume we're running none of the tests, then include
        //              those preceded by "+" and exclude those preceded by
        //              "-".
        //      - testname {(+|-) testname}
        //              Assume we're running all of the tests, then exclude
        //              those preceded by "-" and include those preceded by
        //              "+".
        // XXX It would be nice to support the syntax "@filename" to mean
        // "the list of tests given in the named file."  This could be
        // preceded by "+" or "-" just like an ordinary test name, or maybe
        // the +/- should be required in the file itself.

        struct SyntaxError {
                int position;
                SyntaxError(int pos): position(pos) { }
                };

        Lex lex(argv[i]);
        try {
                lex.next();
                if (lex.token == Lex::MINUS)
                        o.selectedTests = allTestNames;
                else
                        o.selectedTests.resize(0);

                while (lex.token != Lex::END) {
                        bool inserting = true;
                        if (lex.token == Lex::MINUS) {
                                inserting = false;
                                lex.next();
                        } else if (lex.token == Lex::PLUS)
                                lex.next();

                        if (lex.token != Lex::ID)
                                throw SyntaxError(lex.position());

                        if (!binary_search(allTestNames.begin(),
                            allTestNames.end(), lex.id))
                                cerr << "Warning: " << lex.id << " ignored;"
                                        " not a valid test name.\n";
                        else {
                                vector<string>::iterator p =
                                    lower_bound(o.selectedTests.begin(),
                                        o.selectedTests.end(), lex.id);
                                if (inserting) {
                                        if (p == o.selectedTests.end()
                                          || *p != lex.id)
                                                o.selectedTests.insert(p,
                                                    lex.id);
                                } else {
                                        // removing
                                        if (p != o.selectedTests.end()
                                          && *p == lex.id)
                                                o.selectedTests.erase(p);
                                }
                        }
                        lex.next();
                }
        }
        catch (Lex::Lexical e) {
                cerr << "Lexical error in test inclusion/exclusion list:\n"
                        "'" << argv[i] << "'\n";
                for (int i = 0; i < e.position; ++i)
                        cerr << ' ';
                cerr << "^ " << e.err << "\n\n";
                usage(argv[0]);
        }
        catch (SyntaxError e) {
                cerr << "'" << argv[i] << "'\n";
                for (int i = 0; i < e.position; ++i)
                        cerr << ' ';
                cerr << "^ Syntax error in test inclusion/exclusion list\n\n";
                usage(argv[0]);
        }
} // selectTests


void
listTests(const Test *tests, bool verbose) {
	for (const Test *t = tests; t; t = t->nextTest) {
		cout << t->name << (verbose? ":" : "") << "\n";
		if (verbose) {
			cout << t->description;
			cout << '\n';
		}
	}
}


void
usage(char* command) {
	cerr << GLEAN::versionString << '\n';
	cerr << "Usage:  " << command << " mode [options]\n"
"\n"
"mode:\n"
"       (-r|--run) results-directory\n"
"   or  (-c|--compare) old-results-dir new-results-dir\n"
"\n"
"options:\n"
"       (-v|--verbose)             # each occurrence increases\n"
"                                  # verbosity of output\n"
"       (-o|--overwrite)           # overwrite existing results database\n"
"       --visuals 'filter-string'  # select subset of visuals (FBConfigs,\n"
"                                  # pixel formats) to test\n"
"       (-t|--tests) {(+|-)test}   # choose tests to include (+) or exclude (-)\n"
"       --listtests                # list test names and exit\n"
"       --help                     # display usage information\n"
#if defined(__X11__)
"       -display X11-display-name  # select X11 display to use\n"
"           (or --display)\n"
#elif defined(__WIN__)
#endif
		;
	exit(1);
} // usage