using namespace std;

This commit is contained in:
john30
2014-11-22 20:56:53 +01:00
parent d97bc1bc34
commit 3a34267bee
10 changed files with 294 additions and 277 deletions
+51 -51
View File
@@ -21,27 +21,28 @@
#include <iostream>
#include <iomanip>
void verify(bool expectFailMatch, std::string type, std::string input,
bool match, std::string expectStr, std::string gotStr)
using namespace std;
void verify(bool expectFailMatch, string type, string input,
bool match, string expectStr, string gotStr)
{
if (expectFailMatch == true) {
if (match == true)
std::cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << std::endl;
cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << endl;
else
std::cout << " failed " << type << " match >" << input << "< OK"
<< std::endl;
cout << " failed " << type << " match >" << input << "< OK" << endl;
}
else if (match == true)
std::cout << " " << type << " match >" << input << "< OK" << std::endl;
cout << " " << type << " match >" << input << "< OK" << endl;
else
std::cout << " " << type << " match >" << input << "< error: got >" << gotStr
<< "<, expected >" << expectStr << "<" << std::endl;
cout << " " << type << " match >" << input << "< error: got >"
<< gotStr << "<, expected >" << expectStr << "<" << endl;
}
int main()
{
std::string checks[][5] = {
string checks[][5] = {
//name;[len];type[;[divisor|values][;[unit][;[comment]]]], decoded value, master, slave, flags
{"x;;ign:10", "", "10fe07000a00000000000000000000", "00", ""},
{"x;;str:10", "Hallo, Du!", "10fe07000a48616c6c6f2c20447521", "00", ""},
@@ -180,60 +181,59 @@ int main()
{"x;;trelrel","18.004;19.008","10fe07000401120213", "00", ""}, // reference to template struct
{"x;;temp;;;;y;;d1c","18.004;9.5","10fe070003011213", "00", ""}, // reference to template, normal def
};
std::map<std::string, DataField*> templates;
map<string, DataField*> templates;
DataField* fields = NULL;
for (size_t i = 0; i < sizeof(checks) / sizeof(checks[0]); i++) {
std::string check[5] = checks[i];
std::istringstream isstr(check[0]);
std::string expectStr = check[1];
string check[5] = checks[i];
istringstream isstr(check[0]);
string expectStr = check[1];
SymbolString mstr = SymbolString(check[2], false);
SymbolString sstr = SymbolString(check[3], false);
std::string flags = check[4];
bool isSet = flags.find('s') != std::string::npos;
bool failedCreate = flags.find('c') != std::string::npos;
bool failedRead = flags.find('r') != std::string::npos;
bool failedReadMatch = flags.find('R') != std::string::npos;
bool failedWrite = flags.find('w') != std::string::npos;
bool failedWriteMatch = flags.find('W') != std::string::npos;
bool verbose = flags.find('v') != std::string::npos;
bool isTemplate = flags.find('t') != std::string::npos;
std::string item;
std::vector<std::string> entries;
string flags = check[4];
bool isSet = flags.find('s') != string::npos;
bool failedCreate = flags.find('c') != string::npos;
bool failedRead = flags.find('r') != string::npos;
bool failedReadMatch = flags.find('R') != string::npos;
bool failedWrite = flags.find('w') != string::npos;
bool failedWriteMatch = flags.find('W') != string::npos;
bool verbose = flags.find('v') != string::npos;
bool isTemplate = flags.find('t') != string::npos;
string item;
vector<string> entries;
while (std::getline(isstr, item, ';') != 0)
while (getline(isstr, item, ';') != 0)
entries.push_back(item);
if (fields != NULL) {
delete fields;
fields = NULL;
}
std::vector<std::string>::iterator it = entries.begin();
vector<string>::iterator it = entries.begin();
result_t result = DataField::create(it, entries.end(), templates, fields, isSet, isTemplate ? SYN : mstr[1]);
if (failedCreate == true) {
if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << std::endl;
cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << endl;
else
std::cout << "\"" << check[0] << "\": failed create OK" << std::endl;
cout << "\"" << check[0] << "\": failed create OK" << endl;
continue;
}
if (result != RESULT_OK) {
std::cout << "\"" << check[0] << "\": create error: "
<< getResultCode(result) << std::endl;
cout << "\"" << check[0] << "\": create error: " << getResultCode(result) << endl;
continue;
}
if (fields == NULL) {
std::cout << "\"" << check[0] << "\": create error: NULL" << std::endl;
cout << "\"" << check[0] << "\": create error: NULL" << endl;
continue;
}
if (it != entries.end()) {
std::cout << "\"" << check[0] << "\": create error: trailing input" << std::endl;
cout << "\"" << check[0] << "\": create error: trailing input" << endl;
continue;
}
std::cout << "\"" << check[0] << "\": create OK" << std::endl;
cout << "\"" << check[0] << "\": create OK" << endl;
if (isTemplate) {
// store new template
std::string name = fields->getName();
std::map<std::string, DataField*>::iterator current = templates.find(name);
string name = fields->getName();
map<string, DataField*>::iterator current = templates.find(name);
if (current == templates.end()) {
templates[name] = fields;
} else {
@@ -244,20 +244,20 @@ int main()
continue;
}
std::ostringstream output;
ostringstream output;
SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false);
SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false);
result = fields->read(mstr, 0, sstr, 0, output, verbose);
if (failedRead == true)
if (result == RESULT_OK)
std::cout << " failed read " << fields->getName() << " >"
<< check[2] << "< error: unexpectedly succeeded" << std::endl;
cout << " failed read " << fields->getName() << " >"
<< check[2] << "< error: unexpectedly succeeded" << endl;
else
std::cout << " failed read " << fields->getName() << " >"
<< check[2] << "< OK" << std::endl;
cout << " failed read " << fields->getName() << " >"
<< check[2] << "< OK" << endl;
else if (result != RESULT_OK) {
std::cout << " read " << fields->getName() << " >" << check[2] << "< error: "
<< getResultCode(result) << std::endl;
cout << " read " << fields->getName() << " >" << check[2]
<< "< error: " << getResultCode(result) << endl;
}
else {
bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0;
@@ -265,19 +265,19 @@ int main()
}
if (verbose == false) {
std::istringstream input(expectStr);
istringstream input(expectStr);
result = fields->write(input, writeMstr, 0, writeSstr, 0);
if (failedWrite == true) {
if (result == RESULT_OK)
std::cout << " failed write " << fields->getName() << " >"
<< expectStr << "< error: unexpectedly succeeded" << std::endl;
cout << " failed write " << fields->getName() << " >"
<< expectStr << "< error: unexpectedly succeeded" << endl;
else
std::cout << " failed write " << fields->getName() << " >"
<< expectStr << "< OK" << std::endl;
cout << " failed write " << fields->getName() << " >"
<< expectStr << "< OK" << endl;
}
else if (result != RESULT_OK) {
std::cout << " write " << fields->getName() << " >"
<< expectStr << "< error: " << getResultCode(result) << std::endl;
cout << " write " << fields->getName() << " >" << expectStr
<< "< error: " << getResultCode(result) << endl;
}
else {
bool match = mstr == writeMstr && sstr == writeSstr;
@@ -288,7 +288,7 @@ int main()
fields = NULL;
}
for (std::map<std::string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
for (map<string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
delete it->second;
return 0;
+43 -42
View File
@@ -21,27 +21,28 @@
#include <iostream>
#include <iomanip>
void verify(bool expectFailMatch, std::string type, std::string input,
bool match, std::string expectStr, std::string gotStr)
using namespace std;
void verify(bool expectFailMatch, string type, string input,
bool match, string expectStr, string gotStr)
{
if (expectFailMatch == true) {
if (match == true)
std::cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << std::endl;
cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << endl;
else
std::cout << " failed " << type << " match >" << input << "< OK"
<< std::endl;
cout << " failed " << type << " match >" << input << "< OK" << endl;
}
else if (match == true)
std::cout << " " << type << " match >" << input << "< OK" << std::endl;
cout << " " << type << " match >" << input << "< OK" << endl;
else
std::cout << " " << type << " match >" << input << "< error: got >" << gotStr
<< "<, expected >" << expectStr << "<" << std::endl;
cout << " " << type << " match >" << input << "< error: got >"
<< gotStr << "<, expected >" << expectStr << "<" << endl;
}
void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std::string>::iterator end, std::vector<std::string>::iterator pos)
void printErrorPos(vector<string>::iterator it, const vector<string>::iterator end, vector<string>::iterator pos)
{
std::cout << "Errroneous item is here:" << std::endl;
cout << "Errroneous item is here:" << endl;
bool first = true;
int cnt = 0;
if (pos > it)
@@ -50,7 +51,7 @@ void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std:
if (first == true)
first = false;
else {
std::cout << ';';
cout << ';';
if (it <= pos) {
cnt++;
}
@@ -58,86 +59,86 @@ void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std:
if (it < pos) {
cnt += (*it).length();
}
std::cout << (*it++);
cout << (*it++);
}
std::cout << std::endl;
std::cout << std::setw(cnt) << " " << std::setw(0) << "^" << std::endl;
cout << endl;
cout << setw(cnt) << " " << setw(0) << "^" << endl;
}
int main()
{
// message= [type];class;name;[comment];[QQ];ZZ;PBSB;fields...
// field= name;[pos];type[;[divisor|values][;[unit][;[comment]]]]
std::string checks[][5] = {
string checks[][5] = {
// "message", "flags"
{";;first;;;fe;0700;x;;bda", "26.10.2014", "fffe0700042610001451", "00", ""},
{"w;;first;;;15;b5090400;date;;bda", "26.10.2014", "ff15b5090604002610001445", "00", ""},
};
std::map<std::string, DataField*> templates;
map<string, DataField*> templates;
Message* message = NULL;
for (size_t i = 0; i < sizeof(checks) / sizeof(checks[0]); i++) {
std::string check[5] = checks[i];
std::istringstream isstr(check[0]);
std::string inputStr = check[1];
string check[5] = checks[i];
istringstream isstr(check[0]);
string inputStr = check[1];
SymbolString mstr = SymbolString(check[2], false);
SymbolString sstr = SymbolString(check[3], false);
std::string flags = check[4];
bool failedCreate = flags.find('c') != std::string::npos;
bool failedPrepare = flags.find('p') != std::string::npos;
bool failedPrepareMatch = flags.find('P') != std::string::npos;
std::string item;
std::vector<std::string> entries;
string flags = check[4];
bool failedCreate = flags.find('c') != string::npos;
bool failedPrepare = flags.find('p') != string::npos;
bool failedPrepareMatch = flags.find('P') != string::npos;
string item;
vector<string> entries;
while (std::getline(isstr, item, ';') != 0)
while (getline(isstr, item, ';') != 0)
entries.push_back(item);
if (message != NULL) {
delete message;
message = NULL;
}
std::vector<std::string>::iterator it = entries.begin();
vector<string>::iterator it = entries.begin();
result_t result = Message::create(it, entries.end(), templates, message);
if (failedCreate == true) {
if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << std::endl;
cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << endl;
else
std::cout << "\"" << check[0] << "\": failed create OK" << std::endl;
cout << "\"" << check[0] << "\": failed create OK" << endl;
continue;
}
if (result != RESULT_OK) {
std::cout << "\"" << check[0] << "\": create error: "
<< getResultCode(result) << std::endl;
cout << "\"" << check[0] << "\": create error: "
<< getResultCode(result) << endl;
printErrorPos(entries.begin(), entries.end(), it);
continue;
}
if (message == NULL) {
std::cout << "\"" << check[0] << "\": create error: NULL" << std::endl;
cout << "\"" << check[0] << "\": create error: NULL" << endl;
continue;
}
if (it != entries.end()) {
std::cout << "\"" << check[0] << "\": create error: trailing input" << std::endl;
cout << "\"" << check[0] << "\": create error: trailing input" << endl;
continue;
}
std::cout << "\"" << check[0] << "\": create OK" << std::endl;
cout << "\"" << check[0] << "\": create OK" << endl;
std::istringstream input(inputStr);
istringstream input(inputStr);
SymbolString writeMstr = SymbolString();
result = message->prepare(0xff, writeMstr, input);
if (failedPrepare == true) {
if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed prepare error: unexpectedly succeeded" << std::endl;
cout << "\"" << check[0] << "\": failed prepare error: unexpectedly succeeded" << endl;
else
std::cout << "\"" << check[0] << "\": failed prepare OK" << std::endl;
cout << "\"" << check[0] << "\": failed prepare OK" << endl;
continue;
}
if (result != RESULT_OK) {
std::cout << " prepare >" << inputStr << "< error: "
<< getResultCode(result) << std::endl;
cout << " prepare >" << inputStr << "< error: "
<< getResultCode(result) << endl;
continue;
}
std::cout << " prepare >" << inputStr << "< OK" << std::endl;
cout << " prepare >" << inputStr << "< OK" << endl;
bool match = writeMstr==mstr;
verify(failedPrepareMatch, "prepare", inputStr, match, mstr.getDataStr(), writeMstr.getDataStr());
@@ -146,7 +147,7 @@ int main()
message = NULL;
}
for (std::map<std::string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
for (map<string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
delete it->second;
return 0;
+14 -13
View File
@@ -21,6 +21,8 @@
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
SymbolString sstr = SymbolString("10feb5050427a915aa");
@@ -30,29 +32,28 @@ int main ()
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "ctor escaped successful." << std::endl;
else
std::cout << "ctor escaped invalid: got " << gotStr
<< ", expected " << expectStr << std::endl;
std::cout << "ctor escaped invalid: got " << gotStr << ", expected "
<< expectStr << std::endl;
unsigned char gotCrc = sstr.getCRC(), expectCrc = 0x77;
if (gotCrc == expectCrc)
std::cout << "CRC successful." << std::endl;
else
std::cout << "CRC invalid: got 0x"
<< std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(gotCrc)
<< ", expected 0x"
<< std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(expectCrc)
<< std::endl;
std::cout << "CRC invalid: got 0x" << std::nouppercase << std::setw(2)
<< std::hex << std::setfill('0')
<< static_cast<unsigned>(gotCrc) << ", expected 0x"
<< std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(expectCrc)
<< std::endl;
gotStr = sstr.getDataStr(), expectStr = "10feb5050427a915aa77";
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "unescape successful." << std::endl;
else
std::cout << "unescape invalid: got " << gotStr
<< ", expected " << expectStr << std::endl;
std::cout << "unescape invalid: got " << gotStr << ", expected "
<< expectStr << std::endl;
sstr = SymbolString("10feb5050427a90015a90177", true);
@@ -61,8 +62,8 @@ int main ()
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "ctor unescaped successful." << std::endl;
else
std::cout << "ctor unescaped invalid: got " << gotStr
<< ", expected " << expectStr << std::endl;
std::cout << "ctor unescaped invalid: got " << gotStr << ", expected "
<< expectStr << std::endl;
return 0;