From d24f813f3f2a05c112e803e4256b53535895fc98 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 14 Jul 2021 11:49:10 +1200 Subject: Initial mirror commit --- tests/common/RecordsParam.cpp | 51 +++++++++++++++++++++++++++++ tests/common/RecordsParam.h | 58 +++++++++++++++++++++++++++++++++ tests/common/RecordsParam_create.cpp | 62 ++++++++++++++++++++++++++++++++++++ tests/common/printQtTypes.cpp | 31 ++++++++++++++++++ tests/common/printQtTypes.h | 23 +++++++++++++ 5 files changed, 225 insertions(+) create mode 100644 tests/common/RecordsParam.cpp create mode 100644 tests/common/RecordsParam.h create mode 100644 tests/common/RecordsParam_create.cpp create mode 100644 tests/common/printQtTypes.cpp create mode 100644 tests/common/printQtTypes.h (limited to 'tests/common') diff --git a/tests/common/RecordsParam.cpp b/tests/common/RecordsParam.cpp new file mode 100644 index 0000000..74ad802 --- /dev/null +++ b/tests/common/RecordsParam.cpp @@ -0,0 +1,51 @@ +#include "RecordsParam.h" + +#include "../../src/dictionary/Field.h" +#include "../../src/dictionary/DicRecord.h" + +const vector RecordsParam::fieldNames {"English", "Russian", "Finnish"}; + +RecordsParam::RecordsParam( + vector packFields, + vector > records, + vector questions, + vector > answers): + packFields(packFields), + questions(questions), + answers(answers) +{ + for(vector fieldValues: records) + { + DicRecord* record = new DicRecord; + for(unsigned i = 0; i < fieldValues.size(); i++) + record->setField(fieldNames[i].c_str(), fieldValues[i].c_str()); + this->records << record; + } +} + +vector RecordsParam::hashToStrVector(const QHash& hash, + const vector& keys) +{ + vector res; + for(unsigned i = 0; i < keys.size(); i++) + res.push_back( hash[keys[i].c_str()].toStdString() ); + return res; +} + +vector RecordsParam::recordsToStr() const +{ + vector res; + for(DicRecord* record: records) + { + vector fieldValues = hashToStrVector(record->getFields(), fieldNames); + res.push_back(string("(") + vectorToStr(fieldValues) + ")"); + } + return res; +} + +ostream& operator<<(ostream& os, const RecordsParam& param) +{ + os << "Fields(" << param.vectorToStr(param.packFields) << ") "; + os << "{" << param.vectorToStr( param.recordsToStr() ) << "}"; + return os; +} diff --git a/tests/common/RecordsParam.h b/tests/common/RecordsParam.h new file mode 100644 index 0000000..19d4532 --- /dev/null +++ b/tests/common/RecordsParam.h @@ -0,0 +1,58 @@ +#ifndef RECORDS_PARAM_H +#define RECORDS_PARAM_H + +#include +#include +#include +#include +#include + +class DicRecord; + +using std::vector; +using std::string; +using std::ostream; + +struct RecordsParam +{ +public: + static vector createParams(); + + template + static string vectorToStr(const vector& v); + + static vector hashToStrVector(const QHash& hash, + const vector& keys); + +public: + RecordsParam(vector packFields, vector > records, + vector questions, vector > answers); + + vector recordsToStr() const; + +public: + static const vector fieldNames; + +public: + QList records; + vector packFields; + vector questions; + vector > answers; +}; + +ostream& operator<<(ostream& os, const RecordsParam& param); + +template +string RecordsParam::vectorToStr(const vector& v) +{ + std::stringstream ss; + for(unsigned i = 0; i < v.size(); i++) + { + if(i != 0) + ss << ", "; + ss << v[i]; + } + return ss.str(); +} + +#endif diff --git a/tests/common/RecordsParam_create.cpp b/tests/common/RecordsParam_create.cpp new file mode 100644 index 0000000..b247f35 --- /dev/null +++ b/tests/common/RecordsParam_create.cpp @@ -0,0 +1,62 @@ +#include "RecordsParam.h" + +typedef RecordsParam RP; + +vector RecordsParam::createParams() +{ +return + { + RP({0, 1}, + {{"table", "стол"}, {"window", "окно"}}, + {"table", "window"}, + {{"table", "стол"}, {"window", "окно"}}), + RP({0, 1}, + {{"table", "стол"}, {"window", "окно"}, {"world", "мир"}}, + {"table", "window", "world"}, + {{"table", "стол"}, {"window", "окно"}, {"world", "мир"}}), + RP({1, 0}, + {{"table", "стол"}, {"window", "окно"}}, + {"стол", "окно"}, + {{"стол", "table"}, {"окно", "window"}}), + RP({0, 1}, + {{"table", "стол"}, {"table", "стол"}}, + {"table"}, + {{"table", "стол"}}), + RP({0, 1}, + {{"table", "стол"}, {"table", "таблица"}}, + {"table"}, + {{"table", "стол; таблица"}}), + RP({1, 0}, + {{"world", "мир"}, {"peace", "мир"}}, + {"мир"}, + {{"мир", "world; peace"}}), + RP({1, 0}, + {{"man", "человек; мужчина"}, {"man", "мужик"}}, + {"человек", "мужчина", "мужик"}, + {{"человек", "man"}, {"мужчина", "man"}, {"мужик", "man"}}), + RP({0, 1}, + {{"better; best", "лучший"}}, + {"better", "best"}, + {{"better", "лучший"}, {"best", "лучший"}}), + RP({0, 1}, + {{"table", ""},}, + {}, + {}), + RP({0, 1}, + {{"", "стол"},}, + {}, + {}), + RP({0, 1, 2}, + {{"table", "стол", "pöytä"}, {"window", "окно", "ikkuna"}}, + {"table", "window"}, + {{"table", "стол", "pöytä"}, {"window", "окно", "ikkuna"}}), + RP({0, 2, 1}, + {{"table", "стол", "pöytä"}, {"window", "окно", "ikkuna"}}, + {"table", "window"}, + {{"table", "pöytä", "стол"}, {"window", "ikkuna", "окно"}}), + RP({0, 1, 2}, + {{"table", "", "pöytä"}, {"window", "окно", ""}}, + {"table", "window"}, + {{"table", "", "pöytä"}, {"window", "окно", ""}}), + }; +} diff --git a/tests/common/printQtTypes.cpp b/tests/common/printQtTypes.cpp new file mode 100644 index 0000000..72975b7 --- /dev/null +++ b/tests/common/printQtTypes.cpp @@ -0,0 +1,31 @@ +#include "printQtTypes.h" + +void PrintTo(const QString& str, ::std::ostream* os) +{ + *os << "\"" << str.toStdString() << "\""; +} + +void PrintTo(const QStringList& list, ::std::ostream* os) +{ + *os << "(" << list.join(", ").toStdString() << ")"; +} + +void PrintTo(const QDateTime& time, ::std::ostream* os) +{ + *os << time.toString("yyyy-MM-dd HH:mm:ss").toStdString(); +} + +void PrintTo(const QByteArray& array, ::std::ostream* os) +{ + *os << "\""; + const char* hex = array.toHex().constData(); + for(int i = 0; i < array.size(); i++) + { + unsigned char ch = array.constData()[i]; + if(ch >= 32 && ch <= 126) + *os << ch; + else + *os << "\\x" << hex[i * 2] << hex[i * 2 + 1] << " "; + } + *os << "\""; +} diff --git a/tests/common/printQtTypes.h b/tests/common/printQtTypes.h new file mode 100644 index 0000000..20c23d6 --- /dev/null +++ b/tests/common/printQtTypes.h @@ -0,0 +1,23 @@ +#ifndef PRINT_QT_TYPES_H +#define PRINT_QT_TYPES_H + +#include +#include +#include + +using std::ostream; +using std::vector; + +void PrintTo(const QString& str, ::std::ostream* os); +void PrintTo(const QStringList& list, ::std::ostream* os); + +#define ASSERT_EQ_QSTR(x, y) ASSERT_EQ(x, y) << "\"" << x.toStdString() << "\"" << \ + " != " << "\"" << y.toStdString() << "\""; + +#define ASSERT_EQ_QSTRLIST(x, y) ASSERT_EQ(x, y) << "(" << x.join(", ").toStdString() << ")" << \ + " != " << "(" << y.join(", ").toStdString() << ")"; + +void PrintTo(const QDateTime& time, ::std::ostream* os); +void PrintTo(const QByteArray& array, ::std::ostream* os); + +#endif -- cgit