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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
// test.cc
//
// Copyright (C) 2005, 2007-2010 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details. You should have
// received a copy of the GNU General Public License along with this
// program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.
//
// Demonstrates the use of the problem-resolver and (eventually) runs
// tests on it. The convoluted adaptor classes are the way they are
// in order to keep the APT end of things reasonably thin and
// efficient.
#include "dummy_universe.h"
#include "problemresolver.h"
#include "sanity_check_universe.h"
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <cwidget/generic/util/eassert.h>
#include <cwidget/generic/util/ssprintf.h>
#include <string.h>
using namespace std;
namespace cw = cwidget;
// This is a gross hack; without defining this here, we'd have to
// somehow link in a higher-level library.
log4cxx::LoggerPtr aptitude::Loggers::getAptitudeResolver()
{
return log4cxx::Logger::getLogger("aptitude.resolver");
}
log4cxx::LoggerPtr aptitude::Loggers::getAptitudeResolverSearch()
{
return log4cxx::Logger::getLogger("aptitude.resolver.search");
}
log4cxx::LoggerPtr aptitude::Loggers::getAptitudeResolverSearchGraph()
{
return log4cxx::Logger::getLogger("aptitude.resolver.search.graph");
}
log4cxx::LoggerPtr aptitude::Loggers::getAptitudeResolverSearchTiers()
{
return log4cxx::Logger::getLogger("aptitude.resolver.search.tiers");
}
// To make things easier, the tests are specified as plaintext files.
// The syntax is quite simple: it consists of whitespace-separated
// words, of the form:
//
// SCRIPT ::= "UNIVERSE" "[" UNIVERSE "]" TEST ...
// UNIVERSE ::= (PACKAGE | DEP) ...
// PACKAGE ::= "PACKAGE" pkgname "<" vername1 ... ">" currentver
// DEP ::= "DEP" pkgname1 vername1 "->" "<" pkgname2 vername2 ... ">"
// | "SOFTDEP" pkgname1 vername1 "->" "<" pkgname2 vername2 ... ">"
// | "DEP" pkgname1 vername1 "!!" "<" pkgname2 vername2 ... ">"
//
// TEST ::= "TEST" step_score broken_score "{" SCORE ... "}" "EXPECT" "(" SOLN ... ")"
// SCORE ::= "SCORE" pkgname "<" vername1 score1 ... ">"
// | "SCORE" { (pkgname vername)* } score
// SOLN ::= step_count "ANY"
// | step_count "<" pkgname1 vername1 ... ">"
//
// The second DEP form is a Conflicts, and is implicitly converted to
// dependency form internally.
namespace
{
dummy_universe_ref::package parse_package(istream &f,
dummy_universe_ref universe)
{
f >> ws;
if(f.eof())
throw ParseError("Expected package name, got EOF");
string package;
f >> package >> ws;
return universe.find_package(package);
}
dummy_universe_ref::version parse_version(istream &f,
dummy_universe_ref universe)
{
dummy_universe_ref::package package = parse_package(f, universe);
f >> ws;
if(f.eof())
throw ParseError("Expected version name, got EOF.");
string version;
f >> version >> ws;
return package.version_from_name(version);
}
void parse_expected(istream &f, const std::string &expected)
{
f >> ws;
if(f.eof())
throw ParseError(cw::util::ssprintf("Expected %s, got EOF.", expected.c_str()));
string s;
f >> s >> ws;
if(s != expected)
throw ParseError(cw::util::ssprintf("Expected %s, got %s.",
expected.c_str(),
s.c_str()));
}
imm::set<dummy_universe_ref::version> parse_version_set_tail(istream &f,
dummy_universe_ref universe)
{
imm::set<dummy_universe_ref::version> rval;
f >> ws;
while(f.peek() != '}')
{
rval.insert(parse_version(f, universe));
f >> ws;
}
parse_expected(f, "}");
return rval;
}
}
/** Reads the list of scores into the resolver. */
void read_scores(istream &f,
dummy_universe_ref universe, dummy_resolver &resolver)
{
if(!universe)
throw ParseError("Internal error: NULL universe in read_scores");
f >> ws;
while(f)
{
f >> ws;
if(f.eof())
throw ParseError("Expected '}' or SCORE, got EOF");
string s;
f >> s >> ws;
if(s == "}")
return;
else if(s == "SCORE")
{
if(f.eof())
throw ParseError("Expected package name following SCORE, got "+s);
string pkgname;
f >> pkgname >> ws;
if(pkgname == "{")
{
imm::set<dummy_universe_ref::version> joint_set = parse_version_set_tail(f, universe);
int score;
f >> ws;
if(f.eof())
throw ParseError("Expected score, got EOF");
f >> score >> ws;
if(!f)
throw ParseError("Error parsing the score of a joint score set.");
resolver.add_joint_score(joint_set, score);
}
else
{
dummy_universe::package pkg=universe.find_package(pkgname);
if(f.eof())
throw ParseError("Expected '<' following package name, got EOF");
f >> s >> ws;
if(s != "<")
throw ParseError("Expected '<' following package name, got "+s);
if(f.eof())
throw ParseError("Expected '>' or version name, got EOF");
while(f)
{
string vername;
int score;
f >> vername >> ws;
if(vername == ">")
break;
dummy_universe::version ver=pkg.version_from_name(vername);
if(f.eof())
throw ParseError("Expected score, got EOF");
f >> score >> ws;
if(f.eof())
throw ParseError("Expected '>' or version name, got EOF");
if(!f)
throw ParseError("Error reading score of " + pkgname + " " + vername);
resolver.set_version_score(ver, score);
}
}
}
else
throw ParseError("Expected 'SCORE' or '}', got "+s);
}
throw ParseError("Unexpected error reading score list.");
}
/** Reads the tail of a non-ANY SOLN form. */
map<dummy_universe::package, dummy_resolver::version> read_solution(istream &f, dummy_universe_ref universe)
{
if(!universe)
throw ParseError("Internal error: NULL universe passed to read_solution");
map<dummy_universe::package, dummy_resolver::version> rval;
f >> ws;
while(f)
{
string s;
if(f.eof())
throw ParseError("Expected '>' or package, got EOF");
f >> s >> ws;
if(s == ">")
return rval;
else
{
if(f.eof())
throw ParseError("Expected version, got EOF");
dummy_universe::package pkg=universe.find_package(s);
f >> s >> ws;
if(f.eof())
throw ParseError("Expected '>' or package name, got EOF");
if(s == ">")
throw ParseError("Expected version name, got '>'");
dummy_universe::version ver=pkg.version_from_name(s);
if(rval.find(pkg) != rval.end())
throw ParseError("Package "+pkg.get_name()+" bound twice in solution");
rval[pkg]=ver;
}
}
throw ParseError("Unexpected error reading solution list");
}
int parse_int(const std::string &s)
{
if(s.empty())
throw ParseError("Can't parse an integer from an empty string.");
char *endptr;
long int rval = strtol(s.c_str(), &endptr, 0);
if(*endptr != '\0')
throw ParseError("Error in integer constant \"" + s + "\"");
return rval;
}
void run_test_file(istream &f, bool show_world)
{
dummy_universe_ref universe=NULL;
f >> ws;
while(f)
{
string s;
if(f.eof())
// This is the only place where EOF is valid.
return;
f >> s >> ws;
if(s == "UNIVERSE")
{
if(f.eof())
throw ParseError("Expected '[' following UNIVERSE, got EOF.");
f >> s >> ws;
if(s != "[")
throw ParseError("Expected '[' following UNIVERSE, got " + s);
universe=parse_universe_tail(f);
sanity_check_universe(universe);
if(show_world)
{
cout << "Input universe:" << endl;
dump_universe(universe, cout);
}
}
else if(s == "TEST")
{
if(!universe)
throw ParseError("Expected UNIVERSE before TEST");
if(f.eof())
throw ParseError("Expected step_score and broken_score following 'TEST', got EOF");
int step_score;
int broken_score;
int unfixed_soft_score;
int infinity;
int goal_score = 50;
int future_horizon = 0;
std::string goal_score_or_brace;
f >> step_score >> broken_score >> unfixed_soft_score >> infinity >> ws >> goal_score_or_brace >> ws;
if(f.eof())
throw ParseError("Expected '{' following broken_score, got EOF");
if(!f)
throw ParseError("Error reading step_score, broken_score, unfixed_soft_score, infinity, max_succ, and goal_score after 'TEST'");
if(goal_score_or_brace != "{")
{
goal_score = parse_int(goal_score_or_brace);
std::string future_horizon_or_brace;
f >> future_horizon_or_brace >> ws;
if(future_horizon_or_brace != "{")
{
future_horizon = parse_int(future_horizon_or_brace);
f >> s >> ws;
}
else
s = future_horizon_or_brace;
}
else
s = goal_score;
if(s != "{")
throw ParseError("Expected '{' following TEST, got "+s);
dummy_resolver resolver(step_score, broken_score,
unfixed_soft_score,
infinity,
goal_score,
// TODO: add the broken soft dep
// tier operation to the test input
// language.
tier_operation(),
future_horizon,
imm::map<dummy_universe::package, dummy_universe::version>(),
universe);
resolver.set_debug(true);
read_scores(f, universe, resolver);
if(f.eof())
throw ParseError("Expected 'EXPECT', got EOF");
f >> s >> ws;
if(s != "EXPECT")
throw ParseError("Expected 'EXPECT', got "+s);
if(f.eof())
throw ParseError("Expected '(' following EXPECT, got EOF");
f >> s >> ws;
if(s != "(")
throw ParseError("Expected '(' following EXPECT, got "+s);
while(f)
{
if(f.eof())
throw ParseError("Expected ')' or package name, got EOF");
f >> s >> ws;
if(s == ")")
break;
int step_count=atoi(s.c_str());
if(step_count<=0)
throw ParseError("step_count must be a positive integer, not "+s);
f >> s >> ws;
if(s == "ANY")
{
try
{
dummy_resolver::solution next_soln = resolver.find_next_solution(step_count, NULL);
cout << "Next solution is ";
next_soln.dump(cout);
cout << " (ignored)" << endl;
}
catch(NoMoreTime)
{
cout << "Ran out of steps (ignored)" << endl;
}
catch(NoMoreSolutions)
{
cout << "Ran out of solutions (ignored)" << endl;
}
}
else if(s == "<")
{
try
{
map<dummy_universe::package, dummy_resolver::version> expected=read_solution(f, universe);
dummy_resolver::solution next_soln=resolver.find_next_solution(step_count, NULL);
cout << "Next solution is ";
next_soln.dump(cout);
bool equal=true;
// Compare the solution and the expected set by
// checking their value at each package.
// Slightly lame but correct and easier than
// writing a proper cross-compare.
for(dummy_universe::package_iterator pi = universe.packages_begin();
equal && !pi.end(); ++pi)
{
std::map<dummy_universe::package, dummy_universe::version>::const_iterator expect_found
= expected.find(*pi);
dummy_resolver::version soln_version;
bool soln_touches_package = next_soln.get_choices().get_version_of(*pi, soln_version);
if(expect_found != expected.end())
{
if(!soln_touches_package || soln_version != expect_found->second)
equal = false;
}
else if(soln_touches_package)
equal = false;
}
if(equal)
cout << " (OK)" << endl;
else
{
cout << " (FAILED)" << endl;
cout << "Expected <";
for(map<dummy_universe::package, dummy_universe::version>::const_iterator i=expected.begin();
i!=expected.end(); ++i)
cout << i->first.get_name()
<< ":="
<< i->second.get_name();
cout << ">" << endl;
}
}
catch(NoMoreSolutions)
{
cout << "Ran out of solutions (FAILED)" << endl;
}
catch(NoMoreTime)
{
cout << "Ran out of time (FAILED)" << endl;
}
}
else
throw ParseError("Expected ANY or '<', got "+s);
}
}
else
throw ParseError("Expected UNIVERSE or TEST, got "+s);
}
}
int main(int argc, char **argv)
{
int rval=0;
bool show_world=false;
for(int i=1; i<argc; ++i)
{
// lame man's command line
if(!strcmp(argv[i], "--dump"))
{
show_world=true;
continue;
}
ifstream f(argv[i]);
if(!f)
{
cerr << "Couldn't read from file " << argv[i] << "." << endl;
rval=-1;
}
try
{
f >> ws;
run_test_file(f, show_world);
}
catch(const cwidget::util::Exception &e)
{
cerr << "Error reading " << argv[i] << ": " << e.errmsg() << endl;
rval=-1;
}
}
return rval;
}
|