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