blob: 7ac527dc9e4f438192a5e29a528242d5e49c95f0 [file] [log] [blame]
Adam Lesinskiffa16862014-01-23 18:17:42 -08001#include <iostream>
2#include "options.h"
3
4const bool VERBOSE = false;
5
Christopher Wiley9f4c7ae2015-08-24 14:07:32 -07006using std::string;
7using std::vector;
Adam Lesinskiffa16862014-01-23 18:17:42 -08008
9struct Answer {
10 const char* argv[8];
11 int result;
12 const char* systemSearchPath[8];
13 const char* localSearchPath[8];
14 const char* inputFileName;
15 language_t nativeLanguage;
16 const char* outputH;
17 const char* outputCPP;
18 const char* outputJava;
19};
20
21bool
22match_arrays(const char* const*expected, const vector<string> &got)
23{
24 int count = 0;
25 while (expected[count] != NULL) {
26 count++;
27 }
28 if (got.size() != count) {
29 return false;
30 }
31 for (int i=0; i<count; i++) {
32 if (got[i] != expected[i]) {
33 return false;
34 }
35 }
36 return true;
37}
38
39void
40print_array(const char* prefix, const char* const*expected)
41{
42 while (*expected) {
43 cout << prefix << *expected << endl;
44 expected++;
45 }
46}
47
48void
49print_array(const char* prefix, const vector<string> &got)
50{
51 size_t count = got.size();
52 for (size_t i=0; i<count; i++) {
53 cout << prefix << got[i] << endl;
54 }
55}
56
57static int
58test(const Answer& answer)
59{
60 int argc = 0;
61 while (answer.argv[argc]) {
62 argc++;
63 }
64
65 int err = 0;
66
67 Options options;
68 int result = parse_options(argc, answer.argv, &options);
69
70 // result
71 if (((bool)result) != ((bool)answer.result)) {
72 cout << "mismatch: result: got " << result << " expected " <<
73 answer.result << endl;
74 err = 1;
75 }
76
77 if (result != 0) {
78 // if it failed, everything is invalid
79 return err;
80 }
81
82 // systemSearchPath
83 if (!match_arrays(answer.systemSearchPath, options.systemSearchPath)) {
84 cout << "mismatch: systemSearchPath: got" << endl;
85 print_array(" ", options.systemSearchPath);
86 cout << " expected" << endl;
87 print_array(" ", answer.systemSearchPath);
88 err = 1;
89 }
90
91 // localSearchPath
92 if (!match_arrays(answer.localSearchPath, options.localSearchPath)) {
93 cout << "mismatch: localSearchPath: got" << endl;
94 print_array(" ", options.localSearchPath);
95 cout << " expected" << endl;
96 print_array(" ", answer.localSearchPath);
97 err = 1;
98 }
99
100 // inputFileName
101 if (answer.inputFileName != options.inputFileName) {
102 cout << "mismatch: inputFileName: got " << options.inputFileName
103 << " expected " << answer.inputFileName << endl;
104 err = 1;
105 }
106
107 // nativeLanguage
108 if (answer.nativeLanguage != options.nativeLanguage) {
109 cout << "mismatch: nativeLanguage: got " << options.nativeLanguage
110 << " expected " << answer.nativeLanguage << endl;
111 err = 1;
112 }
113
114 // outputH
115 if (answer.outputH != options.outputH) {
116 cout << "mismatch: outputH: got " << options.outputH
117 << " expected " << answer.outputH << endl;
118 err = 1;
119 }
120
121 // outputCPP
122 if (answer.outputCPP != options.outputCPP) {
123 cout << "mismatch: outputCPP: got " << options.outputCPP
124 << " expected " << answer.outputCPP << endl;
125 err = 1;
126 }
127
128 // outputJava
129 if (answer.outputJava != options.outputJava) {
130 cout << "mismatch: outputJava: got " << options.outputJava
131 << " expected " << answer.outputJava << endl;
132 err = 1;
133 }
134
135 return err;
136}
137
138const Answer g_tests[] = {
139
140 {
141 /* argv */ { "test", "-i/moof", "-I/blah", "-Ibleh", "-imoo", "inputFileName.aidl_cpp", NULL, NULL },
142 /* result */ 0,
143 /* systemSearchPath */ { "/blah", "bleh", NULL, NULL, NULL, NULL, NULL, NULL },
144 /* localSearchPath */ { "/moof", "moo", NULL, NULL, NULL, NULL, NULL, NULL },
145 /* inputFileName */ "inputFileName.aidl_cpp",
146 /* nativeLanguage */ CPP,
147 /* outputH */ "",
148 /* outputCPP */ "",
149 /* outputJava */ ""
150 },
151
152 {
153 /* argv */ { "test", "inputFileName.aidl_cpp", "-oh", "outputH", NULL, NULL, NULL, NULL },
154 /* result */ 0,
155 /* systemSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
156 /* localSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
157 /* inputFileName */ "inputFileName.aidl_cpp",
158 /* nativeLanguage */ CPP,
159 /* outputH */ "outputH",
160 /* outputCPP */ "",
161 /* outputJava */ ""
162 },
163
164 {
165 /* argv */ { "test", "inputFileName.aidl_cpp", "-ocpp", "outputCPP", NULL, NULL, NULL, NULL },
166 /* result */ 0,
167 /* systemSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
168 /* localSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
169 /* inputFileName */ "inputFileName.aidl_cpp",
170 /* nativeLanguage */ CPP,
171 /* outputH */ "",
172 /* outputCPP */ "outputCPP",
173 /* outputJava */ ""
174 },
175
176 {
177 /* argv */ { "test", "inputFileName.aidl_cpp", "-ojava", "outputJava", NULL, NULL, NULL, NULL },
178 /* result */ 0,
179 /* systemSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
180 /* localSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
181 /* inputFileName */ "inputFileName.aidl_cpp",
182 /* nativeLanguage */ CPP,
183 /* outputH */ "",
184 /* outputCPP */ "",
185 /* outputJava */ "outputJava"
186 },
187
188 {
189 /* argv */ { "test", "inputFileName.aidl_cpp", "-oh", "outputH", "-ocpp", "outputCPP", "-ojava", "outputJava" },
190 /* result */ 0,
191 /* systemSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
192 /* localSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
193 /* inputFileName */ "inputFileName.aidl_cpp",
194 /* nativeLanguage */ CPP,
195 /* outputH */ "outputH",
196 /* outputCPP */ "outputCPP",
197 /* outputJava */ "outputJava"
198 },
199
200 {
201 /* argv */ { "test", "inputFileName.aidl_cpp", "-oh", "outputH", "-oh", "outputH1", NULL, NULL },
202 /* result */ 1,
203 /* systemSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
204 /* localSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
205 /* inputFileName */ "",
206 /* nativeLanguage */ CPP,
207 /* outputH */ "",
208 /* outputCPP */ "",
209 /* outputJava */ ""
210 },
211
212 {
213 /* argv */ { "test", "inputFileName.aidl_cpp", "-ocpp", "outputCPP", "-ocpp", "outputCPP1", NULL, NULL },
214 /* result */ 1,
215 /* systemSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
216 /* localSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
217 /* inputFileName */ "",
218 /* nativeLanguage */ CPP,
219 /* outputH */ "",
220 /* outputCPP */ "",
221 /* outputJava */ ""
222 },
223
224 {
225 /* argv */ { "test", "inputFileName.aidl_cpp", "-ojava", "outputJava", "-ojava", "outputJava1", NULL, NULL },
226 /* result */ 1,
227 /* systemSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
228 /* localSearchPath */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
229 /* inputFileName */ "",
230 /* nativeLanguage */ CPP,
231 /* outputH */ "",
232 /* outputCPP */ "",
233 /* outputJava */ ""
234 },
235
236};
237
238int
239main(int argc, const char** argv)
240{
241 const int count = sizeof(g_tests)/sizeof(g_tests[0]);
242 int matches[count];
243
244 int result = 0;
245 for (int i=0; i<count; i++) {
246 if (VERBOSE) {
247 cout << endl;
248 cout << "---------------------------------------------" << endl;
249 const char* const* p = g_tests[i].argv;
250 while (*p) {
251 cout << " " << *p;
252 p++;
253 }
254 cout << endl;
255 cout << "---------------------------------------------" << endl;
256 }
257 matches[i] = test(g_tests[i]);
258 if (VERBOSE) {
259 if (0 == matches[i]) {
260 cout << "passed" << endl;
261 } else {
262 cout << "failed" << endl;
263 }
264 result |= matches[i];
265 }
266 }
267
268 cout << endl;
269 cout << "=============================================" << endl;
270 cout << "options_test summary" << endl;
271 cout << "=============================================" << endl;
272
273 if (!result) {
274 cout << "passed" << endl;
275 } else {
276 cout << "failed the following tests:" << endl;
277 for (int i=0; i<count; i++) {
278 if (matches[i]) {
279 cout << " ";
280 const char* const* p = g_tests[i].argv;
281 while (*p) {
282 cout << " " << *p;
283 p++;
284 }
285 cout << endl;
286 }
287 }
288 }
289
290 return result;
291}
292