- fix fd leak in testsolv
[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 writeresult = 0;
28   int multijob = 0;
29   int c;
30   int ex = 0;
31   FILE *fp;
32
33   while ((c = getopt(argc, argv, "vrh")) >= 0)
34     {
35       switch (c)
36       {
37         case 'v':
38           debuglevel++;
39           break;
40         case 'r':
41           writeresult++;
42           break;
43         case 'h':
44           usage(0);
45           break;
46         default:
47           usage(1);
48           break;
49       }
50     }
51   if (optind == argc)
52     usage(1);
53   for (; optind < argc; optind++)
54     {
55       pool = pool_create();
56       pool_setdebuglevel(pool, debuglevel);
57
58       fp = fopen(argv[optind], "r");
59       if (!fp)
60         {
61           perror(argv[optind]);
62           exit(0);
63         }
64       while(!feof(fp))
65         {
66           queue_init(&job);
67           result = 0;
68           resultflags = 0;
69           solv = testcase_read(pool, fp, argv[optind], &job, &result, &resultflags);
70           if (!solv)
71             {
72               pool_free(pool);
73               exit(1);
74             }
75
76           if (!multijob && !feof(fp))
77             multijob = 1;
78
79           if (multijob)
80             printf("test %d:\n", multijob++);
81           if (result || writeresult)
82             {
83               char *myresult, *resultdiff;
84               solver_solve(solv, &job);
85               if (!resultflags)
86                 resultflags = TESTCASE_RESULT_TRANSACTION | TESTCASE_RESULT_PROBLEMS;
87               myresult = testcase_solverresult(solv, resultflags);
88               if (writeresult)
89                 {
90                   if (*myresult)
91                     {
92                       if (writeresult > 1)
93                         {
94                           char *p = myresult;
95                           while (*p)
96                             {
97                               char *p2 = strchr(p, '\n');
98                               p2 = p2 ? p2 + 1 : p + strlen(p);
99                               printf("#>%.*s", (int)(p2 - p), p);
100                               p = p2;
101                             }
102                         }
103                       else
104                         printf("%s", myresult);
105                     }
106                 }
107               else
108                 {
109                   resultdiff = testcase_resultdiff(result, myresult);
110                   if (resultdiff)
111                     {
112                       printf("Results differ:\n%s", resultdiff);
113                       ex = 1;
114                       solv_free(resultdiff);
115                     }
116                 }
117               solv_free(result);
118               solv_free(myresult);
119             }
120           else
121             {
122               if (solver_solve(solv, &job))
123                 {
124                   int problem, solution, pcnt, scnt;
125                   pcnt = solver_problem_count(solv);
126                   printf("Found %d problems:\n", pcnt);
127                   for (problem = 1; problem <= pcnt; problem++)
128                     {
129                       printf("Problem %d:\n", problem);
130                       solver_printprobleminfo(solv, problem);
131                       printf("\n");
132                       scnt = solver_solution_count(solv, problem);
133                       for (solution = 1; solution <= scnt; solution++)
134                         {
135                           printf("Solution %d:\n", solution);
136                           solver_printsolution(solv, problem, solution);
137                           printf("\n");
138                         }
139                     }
140                 }
141               else
142                 {
143                   Transaction *trans = solver_create_transaction(solv);
144                   printf("Transaction summary:\n\n");
145                   transaction_print(trans);
146                   transaction_free(trans);
147                 }
148             }
149           queue_free(&job);
150           solver_free(solv);
151         }
152       pool_free(pool);
153       fclose(fp);
154     }
155   exit(ex);
156 }