summaryrefslogtreecommitdiff
path: root/tests/common
diff options
context:
space:
mode:
Diffstat (limited to 'tests/common')
-rw-r--r--tests/common/RecordsParam.cpp51
-rw-r--r--tests/common/RecordsParam.h58
-rw-r--r--tests/common/RecordsParam_create.cpp62
-rw-r--r--tests/common/printQtTypes.cpp31
-rw-r--r--tests/common/printQtTypes.h23
5 files changed, 225 insertions, 0 deletions
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<string> RecordsParam::fieldNames {"English", "Russian", "Finnish"};
+
+RecordsParam::RecordsParam(
+ vector<int> packFields,
+ vector<vector<string> > records,
+ vector<string> questions,
+ vector<vector<string> > answers):
+ packFields(packFields),
+ questions(questions),
+ answers(answers)
+{
+ for(vector<string> 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<string> RecordsParam::hashToStrVector(const QHash<QString, QString>& hash,
+ const vector<string>& keys)
+{
+ vector<string> res;
+ for(unsigned i = 0; i < keys.size(); i++)
+ res.push_back( hash[keys[i].c_str()].toStdString() );
+ return res;
+}
+
+vector<string> RecordsParam::recordsToStr() const
+{
+ vector<string> res;
+ for(DicRecord* record: records)
+ {
+ vector<string> fieldValues = hashToStrVector(record->getFields(), fieldNames);
+ res.push_back(string("(") + vectorToStr<string>(fieldValues) + ")");
+ }
+ return res;
+}
+
+ostream& operator<<(ostream& os, const RecordsParam& param)
+{
+ os << "Fields(" << param.vectorToStr<int>(param.packFields) << ") ";
+ os << "{" << param.vectorToStr<string>( 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 <iostream>
+#include <vector>
+#include <string>
+#include <sstream>
+#include <QtCore>
+
+class DicRecord;
+
+using std::vector;
+using std::string;
+using std::ostream;
+
+struct RecordsParam
+{
+public:
+ static vector<RecordsParam> createParams();
+
+ template <typename T>
+ static string vectorToStr(const vector<T>& v);
+
+ static vector<string> hashToStrVector(const QHash<QString, QString>& hash,
+ const vector<string>& keys);
+
+public:
+ RecordsParam(vector<int> packFields, vector<vector<string> > records,
+ vector<string> questions, vector<vector<string> > answers);
+
+ vector<string> recordsToStr() const;
+
+public:
+ static const vector<string> fieldNames;
+
+public:
+ QList<DicRecord*> records;
+ vector<int> packFields;
+ vector<string> questions;
+ vector<vector<string> > answers;
+};
+
+ostream& operator<<(ostream& os, const RecordsParam& param);
+
+template <typename T>
+string RecordsParam::vectorToStr(const vector<T>& 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> 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 <iostream>
+#include <QtCore>
+#include <vector>
+
+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