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