fix bugs in fileconflicts code and prepare for aliased dirs
[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 struct resultflags2str {
12   Id flag;
13   const char *str;
14 } resultflags2str[] = {
15   { TESTCASE_RESULT_TRANSACTION,        "transaction" },
16   { TESTCASE_RESULT_PROBLEMS,           "problems" },
17   { TESTCASE_RESULT_ORPHANED,           "orphaned" },
18   { TESTCASE_RESULT_RECOMMENDED,        "recommended" },
19   { TESTCASE_RESULT_UNNEEDED,           "unneeded" },
20   { 0, 0 }
21 };
22
23 static void
24 usage(ex)
25 {
26   fprintf(ex ? stderr : stdout, "Usage: testsolv <testcase>\n");
27   exit(ex);
28 }
29
30 int
31 main(int argc, char **argv)
32 {
33   Pool *pool;
34   Queue job;
35   Solver *solv;
36   char *result = 0;
37   int resultflags = 0;
38   int debuglevel = 0;
39   int writeresult = 0;
40   int multijob = 0;
41   int c;
42   int ex = 0;
43   FILE *fp;
44
45   while ((c = getopt(argc, argv, "vrh")) >= 0)
46     {
47       switch (c)
48       {
49         case 'v':
50           debuglevel++;
51           break;
52         case 'r':
53           writeresult++;
54           break;
55         case 'h':
56           usage(0);
57           break;
58         default:
59           usage(1);
60           break;
61       }
62     }
63   if (optind == argc)
64     usage(1);
65   for (; optind < argc; optind++)
66     {
67       pool = pool_create();
68       pool_setdebuglevel(pool, debuglevel);
69
70       fp = fopen(argv[optind], "r");
71       if (!fp)
72         {
73           perror(argv[optind]);
74           exit(0);
75         }
76       while(!feof(fp))
77         {
78           queue_init(&job);
79           result = 0;
80           resultflags = 0;
81           solv = testcase_read(pool, fp, argv[optind], &job, &result, &resultflags);
82           if (!solv)
83             {
84               pool_free(pool);
85               exit(1);
86             }
87
88           if (!multijob && !feof(fp))
89             multijob = 1;
90
91           if (multijob)
92             printf("test %d:\n", multijob++);
93           if (result || writeresult)
94             {
95               char *myresult, *resultdiff;
96               solver_solve(solv, &job);
97               if (!resultflags)
98                 resultflags = TESTCASE_RESULT_TRANSACTION | TESTCASE_RESULT_PROBLEMS;
99               myresult = testcase_solverresult(solv, resultflags);
100               if (writeresult)
101                 {
102                   if (*myresult)
103                     {
104                       if (writeresult > 1)
105                         {
106                           const char *p;
107                           int i;
108                           
109                           printf("result ");
110                           p = "%s";
111                           for (i = 0; resultflags2str[i].str; i++)
112                             if ((resultflags & resultflags2str[i].flag) != 0)
113                               {
114                                 printf(p, resultflags2str[i].str);
115                                 p = ",%s";
116                               }
117                           printf(" <inline>\n");
118                           p = myresult;
119                           while (*p)
120                             {
121                               const char *p2 = strchr(p, '\n');
122                               p2 = p2 ? p2 + 1 : p + strlen(p);
123                               printf("#>%.*s", (int)(p2 - p), p);
124                               p = p2;
125                             }
126                         }
127                       else
128                         printf("%s", myresult);
129                     }
130                 }
131               else
132                 {
133                   resultdiff = testcase_resultdiff(result, myresult);
134                   if (resultdiff)
135                     {
136                       printf("Results differ:\n%s", resultdiff);
137                       ex = 1;
138                       solv_free(resultdiff);
139                     }
140                 }
141               solv_free(result);
142               solv_free(myresult);
143             }
144           else
145             {
146               if (solver_solve(solv, &job))
147                 {
148                   int problem, solution, pcnt, scnt;
149                   pcnt = solver_problem_count(solv);
150                   printf("Found %d problems:\n", pcnt);
151                   for (problem = 1; problem <= pcnt; problem++)
152                     {
153                       printf("Problem %d:\n", problem);
154                       solver_printprobleminfo(solv, problem);
155                       printf("\n");
156                       scnt = solver_solution_count(solv, problem);
157                       for (solution = 1; solution <= scnt; solution++)
158                         {
159                           printf("Solution %d:\n", solution);
160                           solver_printsolution(solv, problem, solution);
161                           printf("\n");
162                         }
163                     }
164                 }
165               else
166                 {
167                   Transaction *trans = solver_create_transaction(solv);
168                   printf("Transaction summary:\n\n");
169                   transaction_print(trans);
170                   transaction_free(trans);
171                 }
172             }
173           queue_free(&job);
174           solver_free(solv);
175         }
176       pool_free(pool);
177       fclose(fp);
178     }
179   exit(ex);
180 }