- add testsolv tool
[platform/upstream/libsolv.git] / tools / testsolv.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 #include "pool.h"
6 #include "repo.h"
7 #include "solver.h"
8 #include "solverdebug.h"
9 #include "testcase.h"
10
11 static void
12 usage(ex)
13 {
14   fprintf(ex ? stderr : stdout, "Usage: testsolv <testcase>\n");
15   exit(ex);
16 }
17
18 int
19 main(int argc, char **argv)
20 {
21   Pool *pool;
22   Queue job;
23   Solver *solv;
24   char *result = 0;
25   int resultflags = 0;
26   int debuglevel = 0;
27   int c;
28   int ex = 0;
29
30   while ((c = getopt(argc, argv, "vh")) >= 0)
31     {
32       switch (c)
33       {
34         case 'v':
35           debuglevel++;
36           break;
37         case 'h':
38           usage(0);
39           break;
40         default:
41           usage(1);
42           break;
43       }
44     }
45   if (optind == argc)
46     usage(1);
47   for (; optind < argc; optind++)
48     {
49       pool = pool_create();
50       pool_setdebuglevel(pool, debuglevel);
51       queue_init(&job);
52       solv = testcase_read(pool, 0, argv[optind], &job, &result, &resultflags);
53       if (!solv)
54         {
55           pool_free(pool);
56           exit(1);
57         }
58
59       if (result)
60         {
61           char *myresult, *resultdiff;
62           solver_solve(solv, &job);
63           myresult = testcase_solverresult(solv, resultflags);
64           resultdiff = testcase_resultdiff(result, myresult);
65           if (resultdiff)
66             {
67               printf("Results differ:\n%s", resultdiff);
68               ex = 1;
69               solv_free(resultdiff);
70             }
71           solv_free(result);
72           solv_free(myresult);
73         }
74       else
75         {
76           if (solver_solve(solv, &job))
77             {
78               int problem, solution, pcnt, scnt;
79               pcnt = solver_problem_count(solv);
80               printf("Found %d problems:\n", pcnt);
81               for (problem = 1; problem <= pcnt; problem++)
82                 {
83                   printf("Problem %d:\n", problem);
84                   solver_printprobleminfo(solv, problem);
85                   printf("\n");
86                   scnt = solver_solution_count(solv, problem);
87                   for (solution = 1; solution <= scnt; solution++)
88                     {
89                       printf("Solution %d:\n", solution);
90                       solver_printsolution(solv, problem, solution);
91                       printf("\n");
92                     }
93                 }
94             }
95           else
96             {
97               Transaction *trans = solver_create_transaction(solv);
98               printf("Transaction summary:\n\n");
99               transaction_print(trans);
100               transaction_free(trans);
101             }
102         }
103       queue_free(&job);
104       solver_free(solv);
105       pool_free(pool);
106     }
107   exit(ex);
108 }