refaktoring
[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     qDebug()<<"\n";
27     XdxfPlugin xdxfPluginB(this);
28     Settings *settings=new Settings;
29     settings->setValue("path","../XdxfPluginTests/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
41 void XdxfTest::searchFile() {
42     qDebug()<<"\n";
43     XdxfPlugin xdxfPluginB(this);
44     Settings *settings=new Settings;
45     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
46     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
47     delete settings;
48
49     QCOMPARE(xdxfPlugin->search("."), QString("<key>.</key><t>kropka</t>"));
50     QCOMPARE(xdxfPlugin->search("1"), QString("<key>1</key><t>one</t>"));
51     QCOMPARE(xdxfPlugin->search("test"), QString("<key>test</key><t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
52
53     xdxfPlugin->clean();
54     delete xdxfPlugin;
55
56 }
57
58 void XdxfTest::makeCache()
59 {
60     qDebug()<<"\n";
61     XdxfPlugin xdxfPluginB(this);
62     Settings *settings=new Settings;
63     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
64     settings->setValue("generateCache", "true");
65     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
66
67     QCOMPARE(xdxfPlugin->settings()->value("cached"),tr("true"));
68
69     xdxfPlugin->clean();
70     delete settings;
71     delete xdxfPlugin;
72
73 }
74
75 void XdxfTest::searchCache() {
76     qDebug()<<"\n";
77     XdxfPlugin xdxfPluginB(this);
78     Settings *settings=new Settings;
79     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
80     settings->setValue("generateCache", "true");
81     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
82     delete settings;
83
84     QCOMPARE(xdxfPlugin->settings()->value("cached"),tr("true"));
85     QCOMPARE(xdxfPlugin->search("."), QString("<key>.</key><t>kropka</t>"));
86     QCOMPARE(xdxfPlugin->search("1"), QString("<key>1</key><t>one</t>"));
87     QCOMPARE(xdxfPlugin->search("test"), QString("<key>test</key><t><c c=\"FF00FF\">kro</c>test01<pos>krowa</pos></t>"));
88
89     xdxfPlugin->clean();
90     delete xdxfPlugin;
91
92 }
93
94 void XdxfTest::searchWordListCache(){
95     qDebug()<<"\n";
96     XdxfPlugin xdxfPluginB(this);
97     Settings *settings=new Settings;
98     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
99     settings->setValue("generateCache", "true");
100     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
101     delete settings;
102
103     QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
104     if(te.size()>0)
105         QCOMPARE(te.size(), 1);
106     QList<Translation*> te2 = xdxfPlugin->searchWordList("1",10);
107     QCOMPARE(te2.size(), 5);
108
109     QList<Translation*> te3 = xdxfPlugin->searchWordList("1", 2);
110         QCOMPARE(te3.size(), 2);
111
112     QList<Translation*> te4 = xdxfPlugin->searchWordList("ho*SE", 10);
113         QCOMPARE(te4.at(0)->key(), QString("house"));
114
115     QList<Translation*> te5 = xdxfPlugin->searchWordList("*");
116         QCOMPARE(te5.size(), 9);
117
118     QList<Translation*> te6 = xdxfPlugin->searchWordList("*", 8);
119         QCOMPARE(te6.size(), 8);
120
121     QList<Translation*> te7 = xdxfPlugin->searchWordList("*ou*");
122         QCOMPARE(te7.at(0)->key(), QString("house"));
123
124     QList<Translation*> te8 = xdxfPlugin->searchWordList("1?");
125         QCOMPARE(te8.at(0)->key(), QString("10"));
126
127     QList<Translation*> te9 = xdxfPlugin->searchWordList("1???");
128         QCOMPARE(te9.at(0)->key(), QString("1000"));
129
130     QList<Translation*> te10 = xdxfPlugin->searchWordList("1????*");
131         QCOMPARE(te10.at(0)->key(), QString("1 000 000"));
132
133     QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
134         QCOMPARE(te11.at(0)->key(), QString("house"));
135
136     xdxfPlugin->clean();
137     QList<Translation*> te12 = xdxfPlugin->searchWordList("1",10);
138         QCOMPARE(te2.size(), 5);
139
140     delete xdxfPlugin;
141
142 }
143
144 void XdxfTest::searchWordListFile() {
145     qDebug()<<"\n";
146     XdxfPlugin xdxfPluginB(this);
147     Settings *settings=new Settings;
148     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
149     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
150     delete settings;
151
152     QList<Translation*> te = xdxfPlugin->searchWordList(".", 10);
153     if(te.size()>0)
154         QCOMPARE(te.size(), 1);
155     QList<Translation*> te2 = xdxfPlugin->searchWordList("1",10);
156     QCOMPARE(te2.size(), 5);
157
158     QList<Translation*> te3 = xdxfPlugin->searchWordList("1", 2);
159         QCOMPARE(te3.size(), 2);
160
161     QList<Translation*> te4 = xdxfPlugin->searchWordList("ho*SE", 10);
162         QCOMPARE(te4.at(0)->key(), QString("house"));
163
164     QList<Translation*> te5 = xdxfPlugin->searchWordList("*");
165         QCOMPARE(te5.size(), 9);
166
167     QList<Translation*> te6 = xdxfPlugin->searchWordList("*", 8);
168         QCOMPARE(te6.size(), 8);
169
170     QList<Translation*> te7 = xdxfPlugin->searchWordList("*ou*");
171         QCOMPARE(te7.at(0)->key(), QString("house"));
172
173     QList<Translation*> te8 = xdxfPlugin->searchWordList("1?");
174         QCOMPARE(te8.at(0)->key(), QString("10"));
175
176     QList<Translation*> te9 = xdxfPlugin->searchWordList("1???");
177         QCOMPARE(te9.at(0)->key(), QString("1000"));
178
179     QList<Translation*> te10 = xdxfPlugin->searchWordList("1????*");
180         QCOMPARE(te10.at(0)->key(), QString("1 000 000"));
181
182     QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
183         QCOMPARE(te11.at(0)->key(), QString("house"));
184
185     xdxfPlugin->clean();
186     delete xdxfPlugin;
187 }
188
189 void XdxfTest::stop() {
190     qDebug()<<"\n";
191     XdxfPlugin xdxfPluginB(this);
192     Settings *settings=new Settings;
193     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
194     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
195     delete settings;
196
197     QString string("*");
198     QFuture<QList<Translation*> > future = QtConcurrent::run(xdxfPlugin,
199                         &CommonDictInterface::searchWordList, string, 10);
200     QList<Translation*> te5 = future.result();
201     QCOMPARE(te5.size(), 9);
202
203     xdxfPlugin->clean();
204     delete xdxfPlugin;
205 }
206
207
208 void XdxfTest::langFrom() {
209     qDebug()<<"\n";
210     XdxfPlugin xdxfPluginB(this);
211     Settings *settings=new Settings;
212     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
213     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
214     delete settings;
215
216     QCOMPARE(xdxfPlugin->langFrom(), QString("ENG"));
217     QCOMPARE(xdxfPlugin->langTo(), QString("POL"));
218     QCOMPARE(xdxfPlugin->name(), QString("English-Polish dictionary"));
219     QCOMPARE(xdxfPlugin->infoNote(), QString("Copyright: http://www.freelang.net/dictionary/; Version: 1.0"));
220     QCOMPARE(xdxfPlugin->type(), QString("xdxf"));
221
222     xdxfPlugin->clean();
223     delete xdxfPlugin;
224 }
225
226 void XdxfTest::isAvailable() {
227     XdxfPlugin xdxfPluginB(this);
228     Settings *settings=new Settings;
229     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
230     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
231     delete settings;
232
233     QCOMPARE(xdxfPlugin->isAvailable(), true);
234
235     xdxfPlugin->clean();
236     delete xdxfPlugin;
237 }
238
239 void  XdxfTest::removeAccents() {
240     qDebug()<<"\n";
241     XdxfPluginSub xdxf;
242     xdxf.settings()->setValue(QString("strip_accents"), QString("true"));
243
244     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nóżka")), QString("nozka"));
245     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("motor")), QString("motor"));
246     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nÓżKa")), QString("nozka"));
247     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("ławka")), QString("lawka"));
248     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("éàèùâêîôûëïüÿäöüç")), QString("eaeuaeioueiuyaouc"));
249     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("íőűúó")), QString("iouuo"));
250     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("-ę")), QString("-e"));
251     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\"e\"")), QString("\"e\""));
252     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("'e'")), QString("'e'"));
253     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\e")), QString("e"));
254     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\")), QString(""));
255 }
256
257 void XdxfTest::timeCache() {
258     qDebug()<<"\n";
259     QTime timer;
260     QDate date;
261     QFile File("../XdxfPluginTests/time.xml");
262     if(!File.open(QFile::ReadWrite | QFile::Text)) {
263         qDebug()<<"Error: could not open file";
264         return;
265     }
266     if(!QFile::exists("../../../dict.xdxf")) {
267         qDebug()<<"Error: could not open dict file";
268         return;
269     }
270     QTextStream out(&File);
271     while(!out.atEnd())
272         out.seek(out.pos()+1);
273
274     timer.start();
275     XdxfPlugin xdxfPluginB(this);
276     Settings *settings=new Settings;
277     settings->setValue("path","../../../dict.xdxf");
278     settings->setValue("generateCache", "true");
279     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
280     delete settings;
281
282     out<<"\n<date>" + date.currentDate().toString("dd.MM.yyyy") +" ";
283     out<<timer.currentTime().toString(Qt::TextDate) + "</date>";
284     out<<"\n<type> Cache </type> <time>" << timer.elapsed();
285     out<<"</time>";
286
287     timer.start();
288     xdxfPlugin->search("Bantu");
289     out<<"\n<type> SearchCache-begin </type> <time>" << timer.elapsed();
290     out<< "</time>";
291
292     timer.start();
293     xdxfPlugin->search("level");
294     out<<"\n<type> SearchCache-midle </type> <time>" << timer.elapsed();
295     out<< "</time>";
296
297     timer.start();
298     xdxfPlugin->search("zoril");
299     out<<"\n<type> SearchCache-end </type> <time>" << timer.elapsed();
300     out<< "</time>";
301
302     timer.start();
303     xdxfPlugin->searchWordList("level");
304     out<<"\n<type> SearchWorlListCache </type> <time>" << timer.elapsed();
305     out<< "</time>";
306
307     File.close();
308     xdxfPlugin->clean();
309     delete xdxfPlugin;
310 }
311
312 void XdxfTest::timeFile() {
313     qDebug()<<"\n";
314     QTime timer;
315     QFile File("../XdxfPluginTests/time.xml");
316     if(!File.open(QFile::ReadWrite | QFile::Text)) {
317         qDebug()<<"Error: could not open file";
318         return;
319     }
320     if(!QFile::exists("../../../dict.xdxf")) {
321         qDebug()<<"Error: could not open dict file";
322         return;
323     }
324     QTextStream out(&File);
325     while(!out.atEnd())
326         out.seek(out.pos()+1);
327
328     XdxfPlugin xdxfPluginB(this);
329     Settings *settings=new Settings;
330     if(!QFile::exists("../../../dict.xdxf"))
331         return;
332     settings->setValue("path","../../../dict.xdxf");
333     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
334     delete settings;
335
336     timer.start();
337     xdxfPlugin->search("Bantu");
338     out<<"\n<type> SearchFile-begin </type> <time>" << timer.elapsed();
339     out << "</time>";
340
341     timer.start();
342     xdxfPlugin->search("level");
343     out<<"\n<type> SearchFile-midle </type> <time>" << timer.elapsed();
344     out << "</time>";
345
346     timer.start();
347     xdxfPlugin->search("zoril");
348     out<<"\n<type> SearchFile-end </type> <time>" << timer.elapsed();
349     out << "</time>";
350
351     timer.start();
352     xdxfPlugin->searchWordList("level");
353     out<<"\n<type> SearchWordListFile </type> <time>" << timer.elapsed();
354     out<< "</time>";
355
356     File.close();
357     xdxfPlugin->clean();
358     delete xdxfPlugin;
359 }
360
361 void  XdxfTest::timeCacheNormalize() {
362     qDebug()<<"\n";
363     QTime timer;
364     QFile File("../XdxfPluginTests/time.xml");
365     if(!File.open(QFile::ReadWrite | QFile::Text)) {
366         qDebug()<<"Error: could not open file";
367         return;
368     }
369     if(!QFile::exists("../../../dict.xdxf")) {
370         qDebug()<<"Error: could not open dict file";
371         return;
372     }
373     QTextStream out(&File);
374     while(!out.atEnd())
375         out.seek(out.pos()+1);
376
377     timer.start();
378     XdxfPlugin xdxfPluginB(this);
379     Settings *settings=new Settings;
380
381     settings->setValue("path","../../../dict.xdxf");
382     settings->setValue("generateCache", "true");
383     settings->setValue("strip_accents", "true");
384     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
385     delete settings;
386
387     out<<"\n<type> Cache with strip accent </type> <time>" << timer.elapsed();
388     out<<"</time>";
389
390     timer.start();
391     xdxfPlugin->search("Bantu");
392     out<<"\n<type> SearchCache-begin with strip accent </type> <time>" << timer.elapsed();
393     out<< "</time>";
394
395     timer.start();
396     xdxfPlugin->search("level");
397     out<<"\n<type> SearchCache-midle with strip accent </type> <time>" << timer.elapsed();
398     out<< "</time>";
399
400     timer.start();
401     xdxfPlugin->search("zoril");
402     out<<"\n<type> SearchCache-end with strip accent </type> <time>" << timer.elapsed();
403     out<< "</time>";
404
405     timer.start();
406     xdxfPlugin->searchWordList("level");
407     out<<"\n<type> SearchWorlListCache with strip accent </type> <time>" << timer.elapsed();
408     out<< "</time>";
409
410     File.close();
411     xdxfPlugin->clean();
412     delete xdxfPlugin;
413 }
414
415 void XdxfTest::timeFileNormalize(){
416     qDebug()<<"\n";
417     QTime timer;
418     QFile File("../XdxfPluginTests/time.xml");
419     if(!File.open(QFile::ReadWrite | QFile::Text)) {
420         qDebug()<<"Error: could not open file";
421         return;
422     }
423     if(!QFile::exists("../../../dict.xdxf")) {
424         qDebug()<<"Error: could not open dict file";
425         return;
426     }
427     QTextStream out(&File);
428     while(!out.atEnd())
429         out.seek(out.pos()+1);
430
431     XdxfPlugin xdxfPluginB(this);
432     Settings *settings=new Settings;
433     settings->setValue("path","../../../dict.xdxf");
434     settings->setValue("strip_accents", "true");
435     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
436     delete settings;
437
438     timer.start();
439     xdxfPlugin->search("Bantu");
440     out<<"\n<type> SearchFile-begin with strip accent </type> <time>" << timer.elapsed();
441     out << "</time>";
442
443     timer.start();
444     xdxfPlugin->search("level");
445     out<<"\n<type> SearchFile-midle with strip accent </type> <time>" << timer.elapsed();
446     out << "</time>";
447
448     timer.start();
449     xdxfPlugin->search("zoril");
450     out<<"\n<type> SearchFile-end with strip accent </type> <time>" << timer.elapsed();
451     out << "</time>";
452
453     timer.start();
454     xdxfPlugin->searchWordList("level");
455     out<<"\n<type> SearchWordListFile with strip accent </type> <time>" << timer.elapsed();
456     out<< "</time>\n";
457
458     File.close();
459     xdxfPlugin->clean();
460     delete xdxfPlugin;
461 }
462
463 QTEST_MAIN(XdxfTest)
464 //#include "testqstring.moc"