From 13469142fb7901d7baa749f4718129a80ca9ebaa Mon Sep 17 00:00:00 2001 From: john30 Date: Fri, 27 May 2016 09:55:29 +0200 Subject: [PATCH] include overall state in exit code --- src/lib/ebus/test/test_data.cpp | 53 +++++++++++------ src/lib/ebus/test/test_filereader.cpp | 23 ++++--- src/lib/ebus/test/test_symbol.cpp | 86 +++++++++++++++------------ 3 files changed, 98 insertions(+), 64 deletions(-) diff --git a/src/lib/ebus/test/test_data.cpp b/src/lib/ebus/test/test_data.cpp index feb9b286..50868296 100644 --- a/src/lib/ebus/test/test_data.cpp +++ b/src/lib/ebus/test/test_data.cpp @@ -22,22 +22,27 @@ using namespace std; +static bool error = false; + void verify(bool expectFailMatch, string type, string input, bool match, string expectStr, string gotStr) { match = match && expectStr == gotStr; if (expectFailMatch) { - if (match) + if (match) { cout << " failed " << type << " match >" << input << "< error: unexpectedly succeeded" << endl; - else + error = true; + } else { cout << " failed " << type << " match >" << input << "< OK" << endl; - } - else if (match) + } + } else if (match) { cout << " " << type << " match >" << input << "< OK" << endl; - else + } else { cout << " " << type << " match >" << input << "< error: got >" << gotStr << "<, expected >" << expectStr << "<" << endl; + error = true; + } } int main() @@ -386,12 +391,14 @@ int main() result_t result = mstr.parseHex(check[2]); if (result != RESULT_OK) { cout << "\"" << check[0] << "\": parse \"" << check[2] << "\" error: " << getResultCode(result) << endl; + error = true; continue; } SymbolString sstr(false); result = sstr.parseHex(check[3]); if (result != RESULT_OK) { cout << "\"" << check[0] << "\": parse \"" << check[3] << "\" error: " << getResultCode(result) << endl; + error = true; continue; } string flags = check[4]; @@ -418,22 +425,27 @@ int main() vector::iterator it = entries.begin(); result = DataField::create(it, entries.end(), templates, fields, isSet, isTemplate, !isTemplate && (mstr[1]==BROADCAST || isMaster(mstr[1]))); if (failedCreate) { - if (result == RESULT_OK) + if (result == RESULT_OK) { cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << endl; - else + error = true; + } else { cout << "\"" << check[0] << "\": failed create OK" << endl; + } continue; } if (result != RESULT_OK) { cout << "\"" << check[0] << "\": create error: " << getResultCode(result) << endl; + error = true; continue; } if (fields == NULL) { cout << "\"" << check[0] << "\": create error: NULL" << endl; + error = true; continue; } if (it != entries.end()) { cout << "\"" << check[0] << "\": create error: trailing input" << endl; + error = true; continue; } cout << "\"" << check[0] << "\": create OK" << endl; @@ -443,9 +455,10 @@ int main() if (result == RESULT_OK) { fields = NULL; cout << " store template OK" << endl; - } - else + } else { cout << " store template error: " << getResultCode(result) << endl; + error = true; + } continue; } @@ -454,26 +467,31 @@ int main() result = writeMstr.parseHex(mstr.getDataStr(true, false).substr(0, 10)); if (result != RESULT_OK) { cout << " parse \"" << mstr.getDataStr(true, false).substr(0, 10) << "\" error: " << getResultCode(result) << endl; + error = true; } SymbolString writeSstr(false); result = writeSstr.parseHex(sstr.getDataStr(true, false).substr(0, 2)); if (result != RESULT_OK) { cout << " parse \"" << sstr.getDataStr(true, false).substr(0, 2) << "\" error: " << getResultCode(result) << endl; + error = true; } result = fields->read(pt_masterData, mstr, 0, output, (verbose?OF_VERBOSE:0)|(numeric?OF_NUMERIC:0)|(json?OF_JSON:0), -1, false); if (result >= RESULT_OK) { result = fields->read(pt_slaveData, sstr, 0, output, (verbose?OF_VERBOSE:0)|(numeric?OF_NUMERIC:0)|(json?OF_JSON:0), -1, !output.str().empty()); } if (failedRead) - if (result >= RESULT_OK) + if (result >= RESULT_OK) { cout << " failed read " << fields->getName() << " >" << check[2] << " " << check[3] << "< error: unexpectedly succeeded" << endl; - else + error = true; + } else { cout << " failed read " << fields->getName() << " >" << check[2] << " " << check[3] << "< OK" << endl; + } else if (result < RESULT_OK) { cout << " read " << fields->getName() << " >" << check[2] << " " << check[3] << "< error: " << getResultCode(result) << endl; + error = true; } else { bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0; @@ -486,18 +504,20 @@ int main() if (result >= RESULT_OK) result = fields->write(input, pt_slaveData, writeSstr, 0); if (failedWrite) { - if (result >= RESULT_OK) + if (result >= RESULT_OK) { cout << " failed write " << fields->getName() << " >" << expectStr << "< error: unexpectedly succeeded" << endl; - else + error = true; + } else { cout << " failed write " << fields->getName() << " >" << expectStr << "< OK" << endl; + } } else if (result < RESULT_OK) { cout << " write " << fields->getName() << " >" << expectStr << "< error: " << getResultCode(result) << endl; - } - else { + error = true; + } else { bool match = mstr == writeMstr && sstr == writeSstr; verify(failedWriteMatch, "write", expectStr, match, mstr.getDataStr(true, false) + " " + sstr.getDataStr(true, false), writeMstr.getDataStr(true, false) + " " + writeSstr.getDataStr(true, false)); } @@ -508,6 +528,5 @@ int main() delete templates; - return 0; - + return error ? 1 : 0; } diff --git a/src/lib/ebus/test/test_filereader.cpp b/src/lib/ebus/test/test_filereader.cpp index 837e8c85..c94c18d7 100644 --- a/src/lib/ebus/test/test_filereader.cpp +++ b/src/lib/ebus/test/test_filereader.cpp @@ -22,22 +22,27 @@ using namespace std; +static bool error = false; + void verify(bool expectFailMatch, string type, string input, bool match, string expectStr, string gotStr) { match = match && expectStr == gotStr; if (expectFailMatch) { - if (match) + if (match) { cout << " failed " << type << " match >" << input << "< error: unexpectedly succeeded" << endl; - else + error = true; + } else { cout << " failed " << type << " match >" << input << "< OK" << endl; - } - else if (match) + } + } else if (match) { cout << " " << type << " match >" << input << "< OK" << endl; - else + } else { cout << " " << type << " match >" << input << "< error: got >" << gotStr << "<, expected >" << expectStr << "<" << endl; + error = true; + } } int main() @@ -70,10 +75,12 @@ int main() string resultline[3] = resultlines[lineNo-1]; if (row.empty()) { cout << " result empty"; - if (resultline[0] == "") + if (resultline[0] == "") { cout << ": OK" << endl; - else + } else { cout << ": error" << endl; + error = true; + } continue; } for (vector::iterator it = row.begin(); it != row.end(); it++) { @@ -85,5 +92,5 @@ int main() } } - return 0; + return error ? 1 : 0; } diff --git a/src/lib/ebus/test/test_symbol.cpp b/src/lib/ebus/test/test_symbol.cpp index 2818193e..248f2576 100644 --- a/src/lib/ebus/test/test_symbol.cpp +++ b/src/lib/ebus/test/test_symbol.cpp @@ -22,6 +22,29 @@ using namespace std; +static bool error = false; + +void verify(bool expectFailMatch, string type, string input, + bool match, string expectStr, string gotStr) +{ + match = match && expectStr == gotStr; + if (expectFailMatch) { + if (match) { + cout << " failed " << type << " match >" << input + << "< error: unexpectedly succeeded" << endl; + error = true; + } else { + cout << " failed " << type << " match >" << input << "< OK" << endl; + } + } else if (match) { + cout << " " << type << " match >" << input << "< OK" << endl; + } else { + cout << " " << type << " match >" << input << "< error: got >" + << gotStr << "<, expected >" << expectStr << "<" << endl; + error = true; + } +} + int main(int argc, char** argv) { SymbolString sstr(true); @@ -38,51 +61,36 @@ int main(int argc, char** argv) } return 0; } + string gotStr, expectStr; result_t result = sstr.parseHex("10feb5050427a915aa", false); - if (result != RESULT_OK) + if (result != RESULT_OK) { cout << "parse escaped error: " << getResultCode(result) << endl; + error = true; + } else { + gotStr = sstr.getDataStr(false, false), expectStr = "10feb5050427a90015a90177"; + verify(false, "parse escaped", "10feb5050427a915aa", true, expectStr, gotStr); + unsigned char gotCrc = sstr.getCRC(), expectCrc = 0x77; + ostringstream ostr; + ostr << nouppercase << setw(2) << hex << setfill('0') << static_cast(expectCrc); + expectStr = ostr.str(); + ostr.str(""); + ostr << nouppercase << setw(2) << hex << setfill('0') << static_cast(gotCrc); + gotStr = ostr.str(); + verify(false, "CRC", "10feb5050427a915aa", gotCrc==expectCrc, expectStr, gotStr); - string gotStr = sstr.getDataStr(false, false), expectStr = "10feb5050427a90015a90177"; - - if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) - cout << "parse escaped OK" << endl; - else - cout << "parse escaped error: got " << gotStr << ", expected " - << expectStr << endl; - - unsigned char gotCrc = sstr.getCRC(), expectCrc = 0x77; - - if (gotCrc == expectCrc) - cout << "CRC OK" << endl; - else - cout << "CRC error: got 0x" << nouppercase << setw(2) - << hex << setfill('0') - << static_cast(gotCrc) << ", expected 0x" - << nouppercase << setw(2) << hex - << setfill('0') << static_cast(expectCrc) - << endl; - - gotStr = sstr.getDataStr(true, false), expectStr = "10feb5050427a915aa77"; - - if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) - cout << "unescape OK" << endl; - else - cout << "unescape error: got " << gotStr << ", expected " - << expectStr << endl; + gotStr = sstr.getDataStr(true, false), expectStr = "10feb5050427a915aa77"; + verify(false, "unescape", "10feb5050427a915aa", true, expectStr, gotStr); + } sstr = SymbolString(false); result = sstr.parseHex("10feb5050427a90015a90177", true); - if (result != RESULT_OK) + if (result != RESULT_OK) { cout << "parse unescaped error: " << getResultCode(result) << endl; + error = true; + } else { + gotStr = sstr.getDataStr(true, false), expectStr = "10feb5050427a915aa77"; + verify(false, "parse unescaped", "10feb5050427a90015a90177", true, expectStr, gotStr); + } - gotStr = sstr.getDataStr(true, false); - - if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) - cout << "parse unescaped OK" << endl; - else - cout << "parse unescaped error: got " << gotStr << ", expected " - << expectStr << endl; - - return 0; - + return error ? 1 : 0; }