Merge branch 'master' into qmake
[mdictionary] / tests / XdxfPluginTests / test.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 #include "test.h"
23 #include <QSignalSpy>
24
25 void XdxfTest::getNew() {
26
27     XdxfPlugin xdxfPluginB(this);
28     Settings *settings=new Settings;
29     settings->setValue("path","../tests/dict.xdxf");
30     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
31     delete settings;
32
33     QList<Translation*> te6=xdxfPlugin->searchWordList("*",8);
34     QCOMPARE(te6.size(),8);
35
36     xdxfPlugin->clean();
37     delete xdxfPlugin;
38 }
39
40 void XdxfTest::searchFile() {
41
42     XdxfPlugin xdxfPluginB(this);
43     Settings *settings=new Settings;
44     settings->setValue("path","../tests/dict.xdxf");
45     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
46     delete settings;
47
48     QCOMPARE(xdxfPlugin->search("."), QString("<key>.</key><t>kropka</t>"));
49     QCOMPARE(xdxfPlugin->search("1"), QString("<key>1</key><t>one</t>"));
50     QCOMPARE(xdxfPlugin->search("test"), QString("<key>test</key><t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
51
52     xdxfPlugin->clean();
53     delete xdxfPlugin;
54 }
55
56 void XdxfTest::makeCache()
57 {
58     XdxfPlugin xdxfPluginB(this);
59     Settings *settings=new Settings;
60     settings->setValue("path","../tests/dict.xdxf");
61     settings->setValue("generateCache", "true");
62     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
63     delete settings;
64
65     QCOMPARE(xdxfPlugin->settings()->value("cached"),tr("true"));
66
67     xdxfPlugin->clean();
68     delete xdxfPlugin;
69 }
70
71 void XdxfTest::searchCache() {
72     XdxfPlugin xdxfPluginB(this);
73     Settings *settings=new Settings;
74     settings->setValue("path","../tests/dict.xdxf");
75     settings->setValue("generateCache", "true");
76     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
77     delete settings;
78
79     QCOMPARE(xdxfPlugin->settings()->value("cached"),tr("true"));
80     QCOMPARE(xdxfPlugin->search("."), QString("<key>.</key><t>kropka</t>"));
81     QCOMPARE(xdxfPlugin->search("1"), QString("<key>1</key><t>one</t>"));
82     QCOMPARE(xdxfPlugin->search("test"), QString("<key>test</key><t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
83
84     xdxfPlugin->clean();
85     delete xdxfPlugin;
86 }
87
88 void XdxfTest::searchWordListCache(){
89     XdxfPlugin xdxfPluginB(this);
90     Settings *settings=new Settings;
91     settings->setValue("path","../tests/dict.xdxf");
92     settings->setValue("generateCache", "true");
93     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
94     delete settings;
95
96     QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
97     if(te.size()>0)
98         QCOMPARE(te.size(), 1);
99     QList<Translation*> te2 = xdxfPlugin->searchWordList("1",10);
100     QCOMPARE(te2.size(), 5);
101
102     QList<Translation*> te3 = xdxfPlugin->searchWordList("1", 2);
103         QCOMPARE(te3.size(), 2);
104
105     QList<Translation*> te4 = xdxfPlugin->searchWordList("ho*SE", 10);
106         QCOMPARE(te4.at(0)->key(), QString("house"));
107
108     QList<Translation*> te5 = xdxfPlugin->searchWordList("*");
109         QCOMPARE(te5.size(), 9);
110
111     QList<Translation*> te6 = xdxfPlugin->searchWordList("*", 8);
112         QCOMPARE(te6.size(), 8);
113
114     QList<Translation*> te7 = xdxfPlugin->searchWordList("*ou*");
115         QCOMPARE(te7.at(0)->key(), QString("house"));
116
117     QList<Translation*> te8 = xdxfPlugin->searchWordList("1?");
118         QCOMPARE(te8.at(0)->key(), QString("10"));
119
120     QList<Translation*> te9 = xdxfPlugin->searchWordList("1???");
121         QCOMPARE(te9.at(0)->key(), QString("1000"));
122
123     QList<Translation*> te10 = xdxfPlugin->searchWordList("1????*");
124         QCOMPARE(te10.at(0)->key(), QString("1 000 000"));
125
126     QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
127         QCOMPARE(te11.at(0)->key(), QString("house"));
128
129     xdxfPlugin->clean();
130     delete xdxfPlugin;
131 }
132
133 void XdxfTest::searchWordListFile() {
134
135     XdxfPlugin xdxfPluginB(this);
136     Settings *settings=new Settings;
137     settings->setValue("path","../tests/dict.xdxf");
138     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
139     delete settings;
140
141     QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
142     if(te.size()>0)
143         QCOMPARE(te.size(), 1);
144     QList<Translation*> te2 = xdxfPlugin->searchWordList("1",10);
145     QCOMPARE(te2.size(), 5);
146
147     QList<Translation*> te3 = xdxfPlugin->searchWordList("1", 2);
148         QCOMPARE(te3.size(), 2);
149
150     QList<Translation*> te4 = xdxfPlugin->searchWordList("ho*SE", 10);
151         QCOMPARE(te4.at(0)->key(), QString("house"));
152
153     QList<Translation*> te5 = xdxfPlugin->searchWordList("*");
154         QCOMPARE(te5.size(), 9);
155
156     QList<Translation*> te6 = xdxfPlugin->searchWordList("*", 8);
157         QCOMPARE(te6.size(), 8);
158
159     QList<Translation*> te7 = xdxfPlugin->searchWordList("*ou*");
160         QCOMPARE(te7.at(0)->key(), QString("house"));
161
162     QList<Translation*> te8 = xdxfPlugin->searchWordList("1?");
163         QCOMPARE(te8.at(0)->key(), QString("10"));
164
165     QList<Translation*> te9 = xdxfPlugin->searchWordList("1???");
166         QCOMPARE(te9.at(0)->key(), QString("1000"));
167
168     QList<Translation*> te10 = xdxfPlugin->searchWordList("1????*");
169         QCOMPARE(te10.at(0)->key(), QString("1 000 000"));
170
171     QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
172         QCOMPARE(te11.at(0)->key(), QString("house"));
173
174     xdxfPlugin->clean();
175     delete xdxfPlugin;
176 }
177
178 void XdxfTest::stop() {
179
180     XdxfPlugin xdxfPluginB(this);
181     Settings *settings=new Settings;
182     settings->setValue("path","../tests/dict.xdxf");
183     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
184     delete settings;
185
186     QString string("*");
187     QFuture<QList<Translation*> > future = QtConcurrent::run(xdxfPlugin,
188                         &CommonDictInterface::searchWordList, string, 10);
189     QList<Translation*> te5 = future.result();
190     QCOMPARE(te5.size(), 9);
191
192     xdxfPlugin->clean();
193     delete xdxfPlugin;
194 }
195
196
197 void XdxfTest::langFrom() {
198     XdxfPlugin xdxfPluginB(this);
199     Settings *settings=new Settings;
200     settings->setValue("path","../tests/dict.xdxf");
201     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
202     delete settings;
203
204     QCOMPARE(xdxfPlugin->langFrom(), QString("ENG"));
205
206     xdxfPlugin->clean();
207     delete xdxfPlugin;
208 }
209
210 void XdxfTest::timeCache() {
211     QTime timer;
212     QDate date;
213     QFile File("../tests/time.xml");
214     if(!File.open(QFile::ReadWrite | QFile::Text)) {
215         qDebug()<<"Error: could not open file";
216         return;
217     }
218     QTextStream out(&File);
219     while(!out.atEnd())
220         out.seek(out.pos()+1);
221
222     timer.start();
223     XdxfPlugin xdxfPluginB(this);
224     Settings *settings=new Settings;
225     settings->setValue("path","../../../../../../dict.xdxf");
226     settings->setValue("generateCache", "true");
227     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
228     delete settings;
229
230     out<<"\n<date>" + date.currentDate().toString("dd.MM.yyyy") +" ";
231     out<<timer.currentTime().toString(Qt::TextDate) + "</date>";
232     out<<"\n<type> Cache </type> <time>" << timer.elapsed();
233     out<<"</time>";
234
235     timer.start();
236     xdxfPlugin->search("Bantu");
237     out<<"\n<type> SearchCache-begin </type> <time>" << timer.elapsed();
238     out<< "</time>";
239
240     timer.start();
241     xdxfPlugin->search("level");
242     out<<"\n<type> SearchCache-midle </type> <time>" << timer.elapsed();
243     out<< "</time>";
244
245     timer.start();
246     xdxfPlugin->search("zoril");
247     out<<"\n<type> SearchCache-end </type> <time>" << timer.elapsed();
248     out<< "</time>";
249
250     timer.start();
251     xdxfPlugin->searchWordList("level");
252     out<<"\n<type> SearchWorlListCache </type> <time>" << timer.elapsed();
253     out<< "</time>";
254
255     File.close();
256     xdxfPlugin->clean();
257     delete xdxfPlugin;
258 }
259
260 void XdxfTest::timeFile() {
261     QTime timer;
262     QDate date;
263     QFile File("../tests/time.xml");
264     if(!File.open(QFile::ReadWrite | QFile::Text)) {
265         qDebug()<<"Error: could not open file";
266         return;
267     }
268     QTextStream out(&File);
269     while(!out.atEnd())
270         out.seek(out.pos()+1);
271
272     XdxfPlugin xdxfPluginB(this);
273     Settings *settings=new Settings;
274     settings->setValue("path","../../../../../../dict.xdxf");
275     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
276     delete settings;
277
278     timer.start();
279     xdxfPlugin->search("Bantu");
280     out<<"\n<type> SearchFile-begin </type> <time>" << timer.elapsed();
281     out << "</time>";
282
283     timer.start();
284     xdxfPlugin->search("level");
285     out<<"\n<type> SearchFile-midle </type> <time>" << timer.elapsed();
286     out << "</time>";
287
288     timer.start();
289     xdxfPlugin->search("zoril");
290     out<<"\n<type> SearchFile-end </type> <time>" << timer.elapsed();
291     out << "</time>";
292
293     timer.start();
294     xdxfPlugin->searchWordList("level");
295     out<<"\n<type> SearchWordListFile </type> <time>" << timer.elapsed();
296     out<< "</time>";
297
298     File.close();
299     xdxfPlugin->clean();
300     delete xdxfPlugin;
301 }
302
303 void  XdxfTest::timeCacheNormalize() {
304     QTime timer;
305     QDate date;
306     QFile File("../tests/time.xml");
307     if(!File.open(QFile::ReadWrite | QFile::Text)) {
308         qDebug()<<"Error: could not open file";
309         return;
310     }
311     QTextStream out(&File);
312     while(!out.atEnd())
313         out.seek(out.pos()+1);
314
315     timer.start();
316     XdxfPlugin xdxfPluginB(this);
317     Settings *settings=new Settings;
318     settings->setValue("path","../../../../../../dict.xdxf");
319     settings->setValue("generateCache", "true");
320     settings->setValue("strip_accents", "true");
321     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
322     delete settings;
323
324     out<<"\n<type> Cache with strip accent </type> <time>" << timer.elapsed();
325     out<<"</time>";
326
327     timer.start();
328     xdxfPlugin->search("Bantu");
329     out<<"\n<type> SearchCache-begin with strip accent </type> <time>" << timer.elapsed();
330     out<< "</time>";
331
332     timer.start();
333     xdxfPlugin->search("level");
334     out<<"\n<type> SearchCache-midle with strip accent </type> <time>" << timer.elapsed();
335     out<< "</time>";
336
337     timer.start();
338     xdxfPlugin->search("zoril");
339     out<<"\n<type> SearchCache-end with strip accent </type> <time>" << timer.elapsed();
340     out<< "</time>";
341
342     timer.start();
343     xdxfPlugin->searchWordList("level");
344     out<<"\n<type> SearchWorlListCache with strip accent </type> <time>" << timer.elapsed();
345     out<< "</time>";
346
347     File.close();
348     xdxfPlugin->clean();
349     delete xdxfPlugin;
350 }
351
352 void  XdxfTest::removeAccents() {
353
354     XdxfPluginSub xdxf;
355     xdxf.settings()->setValue(QString("strip_accents"), QString("true"));
356
357     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nóżka")), QString("nozka"));
358     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("motor")), QString("motor"));
359     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nÓżKa")), QString("nozka"));
360     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("ławka")), QString("lawka"));
361     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("éàèùâêîôûëïüÿäöüç")), QString("eaeuaeioueiuyaouc"));
362     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("íőűúó")), QString("iouuo"));
363     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("-ę")), QString("-e"));
364     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\"e\"")), QString("\"e\""));
365     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("'e'")), QString("'e'"));
366     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\e")), QString("e"));
367     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\")), QString(""));
368 }
369
370 void XdxfTest::timeFileNormalize(){
371     QTime timer;
372     QDate date;
373     QFile File("../tests/time.xml");
374     if(!File.open(QFile::ReadWrite | QFile::Text)) {
375         qDebug()<<"Error: could not open file";
376         return;
377     }
378     QTextStream out(&File);
379     while(!out.atEnd())
380         out.seek(out.pos()+1);
381
382     XdxfPlugin xdxfPluginB(this);
383     Settings *settings=new Settings;
384     settings->setValue("path","../../../../../../dict.xdxf");
385     settings->setValue("strip_accents", "true");
386     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
387     delete settings;
388
389     timer.start();
390     xdxfPlugin->search("Bantu");
391     out<<"\n<type> SearchFile-begin with strip accent </type> <time>" << timer.elapsed();
392     out << "</time>";
393
394     timer.start();
395     xdxfPlugin->search("level");
396     out<<"\n<type> SearchFile-midle with strip accent </type> <time>" << timer.elapsed();
397     out << "</time>";
398
399     timer.start();
400     xdxfPlugin->search("zoril");
401     out<<"\n<type> SearchFile-end with strip accent </type> <time>" << timer.elapsed();
402     out << "</time>";
403
404     timer.start();
405     xdxfPlugin->searchWordList("level");
406     out<<"\n<type> SearchWordListFile with strip accent </type> <time>" << timer.elapsed();
407     out<< "</time>\n";
408
409     File.close();
410     xdxfPlugin->clean();
411     delete xdxfPlugin;
412 }
413
414 QTEST_MAIN(XdxfTest)
415 //#include "testqstring.moc"