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