webView
[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     qDebug()<<"\n\n\n";
165     QList<Translation*> te5 = xdxfPlugin->searchWordList("*");
166     qDebug()<<"\n\n\n";
167     QCOMPARE(te5.size(), 9);
168
169
170     QList<Translation*> te6 = xdxfPlugin->searchWordList("*", 8);
171         QCOMPARE(te6.size(), 8);
172
173     QList<Translation*> te7 = xdxfPlugin->searchWordList("*ou*");
174         QCOMPARE(te7.at(0)->key(), QString("house"));
175
176     QList<Translation*> te8 = xdxfPlugin->searchWordList("1?");
177         QCOMPARE(te8.at(0)->key(), QString("10"));
178
179     QList<Translation*> te9 = xdxfPlugin->searchWordList("1???");
180         QCOMPARE(te9.at(0)->key(), QString("1000"));
181
182     QList<Translation*> te10 = xdxfPlugin->searchWordList("1????*");
183         QCOMPARE(te10.at(0)->key(), QString("1 000 000"));
184
185     QList<Translation*> te11 = xdxfPlugin->searchWordList("h**?*?**e");
186         QCOMPARE(te11.at(0)->key(), QString("house"));
187
188     xdxfPlugin->clean();
189     delete xdxfPlugin;
190 }
191
192 void XdxfTest::stop() {
193     qDebug()<<"\n";
194     XdxfPlugin xdxfPluginB(this);
195     Settings *settings=new Settings;
196     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
197     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
198     delete settings;
199
200     QString string("*");
201     QFuture<QList<Translation*> > future = QtConcurrent::run(xdxfPlugin,
202                         &CommonDictInterface::searchWordList, string, 10);
203     QList<Translation*> te5 = future.result();
204     QCOMPARE(te5.size(), 9);
205
206     xdxfPlugin->clean();
207     delete xdxfPlugin;
208 }
209
210
211 void XdxfTest::langFrom() {
212     qDebug()<<"\n";
213     XdxfPlugin xdxfPluginB(this);
214     Settings *settings=new Settings;
215     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
216     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
217     delete settings;
218
219     QCOMPARE(xdxfPlugin->langFrom(), QString("ENG"));
220     QCOMPARE(xdxfPlugin->langTo(), QString("POL"));
221     QCOMPARE(xdxfPlugin->name(), QString("English-Polish dictionary"));
222     QCOMPARE(xdxfPlugin->infoNote(), QString("Copyright: http://www.freelang.net/dictionary/; Version: 1.0"));
223     QCOMPARE(xdxfPlugin->type(), QString("xdxf"));
224
225     xdxfPlugin->clean();
226     delete xdxfPlugin;
227 }
228
229 void XdxfTest::isAvailable() {
230     XdxfPlugin xdxfPluginB(this);
231     Settings *settings=new Settings;
232     settings->setValue("path","../XdxfPluginTests/dict.xdxf");
233     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
234     delete settings;
235
236     QCOMPARE(xdxfPlugin->isAvailable(), true);
237
238     xdxfPlugin->clean();
239     delete xdxfPlugin;
240 }
241
242 void  XdxfTest::removeAccents() {
243     qDebug()<<"\n";
244     XdxfPluginSub xdxf;
245     xdxf.settings()->setValue(QString("strip_accents"), QString("true"));
246
247     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nóżka")), QString("nozka"));
248     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("motor")), QString("motor"));
249     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("nÓżKa")), QString("nozka"));
250     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("ławka")), QString("lawka"));
251     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("éàèùâêîôûëïüÿäöüç")), QString("eaeuaeioueiuyaouc"));
252     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("íőűúó")), QString("iouuo"));
253     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("-ę")), QString("-e"));
254     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\"e\"")), QString("\"e\""));
255     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("'e'")), QString("'e'"));
256     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\e")), QString("e"));
257     QCOMPARE(xdxf.getRemoveAccents(QString::fromUtf8("\\")), QString(""));
258 }
259
260 void XdxfTest::timeCache() {
261     qDebug()<<"\n";
262     QTime timer;
263     QDate date;
264     QFile File("../XdxfPluginTests/time.xml");
265     if(!File.open(QFile::ReadWrite | QFile::Text)) {
266         qDebug()<<"Error: could not open file";
267         return;
268     }
269     if(!QFile::exists("../../../dict.xdxf")) {
270         qDebug()<<"Error: could not open dict file";
271         return;
272     }
273     QTextStream out(&File);
274     while(!out.atEnd())
275         out.seek(out.pos()+1);
276
277     timer.start();
278     XdxfPlugin xdxfPluginB(this);
279     Settings *settings=new Settings;
280     settings->setValue("path","../../../dict.xdxf");
281     settings->setValue("generateCache", "true");
282     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
283     delete settings;
284
285     out<<"\n<date>" + date.currentDate().toString("dd.MM.yyyy") +" ";
286     out<<timer.currentTime().toString(Qt::TextDate) + "</date>";
287     out<<"\n<type> Cache </type> <time>" << timer.elapsed();
288     out<<"</time>";
289
290     timer.start();
291     xdxfPlugin->search("Bantu");
292     out<<"\n<type> SearchCache-begin </type> <time>" << timer.elapsed();
293     out<< "</time>";
294
295     timer.start();
296     xdxfPlugin->search("level");
297     out<<"\n<type> SearchCache-midle </type> <time>" << timer.elapsed();
298     out<< "</time>";
299
300     timer.start();
301     xdxfPlugin->search("zoril");
302     out<<"\n<type> SearchCache-end </type> <time>" << timer.elapsed();
303     out<< "</time>";
304
305     timer.start();
306     xdxfPlugin->searchWordList("level");
307     out<<"\n<type> SearchWorlListCache </type> <time>" << timer.elapsed();
308     out<< "</time>";
309
310     File.close();
311     xdxfPlugin->clean();
312     delete xdxfPlugin;
313 }
314
315 void XdxfTest::timeFile() {
316     qDebug()<<"\n";
317     QTime timer;
318     QFile File("../XdxfPluginTests/time.xml");
319     if(!File.open(QFile::ReadWrite | QFile::Text)) {
320         qDebug()<<"Error: could not open file";
321         return;
322     }
323     if(!QFile::exists("../../../dict.xdxf")) {
324         qDebug()<<"Error: could not open dict file";
325         return;
326     }
327     QTextStream out(&File);
328     while(!out.atEnd())
329         out.seek(out.pos()+1);
330
331     XdxfPlugin xdxfPluginB(this);
332     Settings *settings=new Settings;
333     if(!QFile::exists("../../../dict.xdxf"))
334         return;
335     settings->setValue("path","../../../dict.xdxf");
336     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
337     delete settings;
338
339     timer.start();
340     xdxfPlugin->search("Bantu");
341     out<<"\n<type> SearchFile-begin </type> <time>" << timer.elapsed();
342     out << "</time>";
343
344     timer.start();
345     xdxfPlugin->search("level");
346     out<<"\n<type> SearchFile-midle </type> <time>" << timer.elapsed();
347     out << "</time>";
348
349     timer.start();
350     xdxfPlugin->search("zoril");
351     out<<"\n<type> SearchFile-end </type> <time>" << timer.elapsed();
352     out << "</time>";
353
354     timer.start();
355     xdxfPlugin->searchWordList("level");
356     out<<"\n<type> SearchWordListFile </type> <time>" << timer.elapsed();
357     out<< "</time>";
358
359     File.close();
360     xdxfPlugin->clean();
361     delete xdxfPlugin;
362 }
363
364 void  XdxfTest::timeCacheNormalize() {
365     qDebug()<<"\n";
366     QTime timer;
367     QFile File("../XdxfPluginTests/time.xml");
368     if(!File.open(QFile::ReadWrite | QFile::Text)) {
369         qDebug()<<"Error: could not open file";
370         return;
371     }
372     if(!QFile::exists("../../../dict.xdxf")) {
373         qDebug()<<"Error: could not open dict file";
374         return;
375     }
376     QTextStream out(&File);
377     while(!out.atEnd())
378         out.seek(out.pos()+1);
379
380     timer.start();
381     XdxfPlugin xdxfPluginB(this);
382     Settings *settings=new Settings;
383
384     settings->setValue("path","../../../dict.xdxf");
385     settings->setValue("generateCache", "true");
386     settings->setValue("strip_accents", "true");
387     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
388     delete settings;
389
390     out<<"\n<type> Cache with strip accent </type> <time>" << timer.elapsed();
391     out<<"</time>";
392
393     timer.start();
394     xdxfPlugin->search("Bantu");
395     out<<"\n<type> SearchCache-begin with strip accent </type> <time>" << timer.elapsed();
396     out<< "</time>";
397
398     timer.start();
399     xdxfPlugin->search("level");
400     out<<"\n<type> SearchCache-midle with strip accent </type> <time>" << timer.elapsed();
401     out<< "</time>";
402
403     timer.start();
404     xdxfPlugin->search("zoril");
405     out<<"\n<type> SearchCache-end with strip accent </type> <time>" << timer.elapsed();
406     out<< "</time>";
407
408     timer.start();
409     xdxfPlugin->searchWordList("level");
410     out<<"\n<type> SearchWorlListCache with strip accent </type> <time>" << timer.elapsed();
411     out<< "</time>";
412
413     File.close();
414     xdxfPlugin->clean();
415     delete xdxfPlugin;
416 }
417
418 void XdxfTest::timeFileNormalize(){
419     qDebug()<<"\n";
420     QTime timer;
421     QFile File("../XdxfPluginTests/time.xml");
422     if(!File.open(QFile::ReadWrite | QFile::Text)) {
423         qDebug()<<"Error: could not open file";
424         return;
425     }
426     if(!QFile::exists("../../../dict.xdxf")) {
427         qDebug()<<"Error: could not open dict file";
428         return;
429     }
430     QTextStream out(&File);
431     while(!out.atEnd())
432         out.seek(out.pos()+1);
433
434     XdxfPlugin xdxfPluginB(this);
435     Settings *settings=new Settings;
436     settings->setValue("path","../../../dict.xdxf");
437     settings->setValue("strip_accents", "true");
438     CommonDictInterface *xdxfPlugin = xdxfPluginB.getNew(settings);
439     delete settings;
440
441     timer.start();
442     xdxfPlugin->search("Bantu");
443     out<<"\n<type> SearchFile-begin with strip accent </type> <time>" << timer.elapsed();
444     out << "</time>";
445
446     timer.start();
447     xdxfPlugin->search("level");
448     out<<"\n<type> SearchFile-midle with strip accent </type> <time>" << timer.elapsed();
449     out << "</time>";
450
451     timer.start();
452     xdxfPlugin->search("zoril");
453     out<<"\n<type> SearchFile-end with strip accent </type> <time>" << timer.elapsed();
454     out << "</time>";
455
456     timer.start();
457     xdxfPlugin->searchWordList("level");
458     out<<"\n<type> SearchWordListFile with strip accent </type> <time>" << timer.elapsed();
459     out<< "</time>\n";
460
461     File.close();
462     xdxfPlugin->clean();
463     delete xdxfPlugin;
464 }
465
466 QTEST_MAIN(XdxfTest)
467 //#include "testqstring.moc"