1*2851c541SNicolas Bonnefon /*
2*2851c541SNicolas Bonnefon * Copyright (C) 2009, 2010 Nicolas Bonnefon and other contributors
3*2851c541SNicolas Bonnefon *
4*2851c541SNicolas Bonnefon * This file is part of glogg.
5*2851c541SNicolas Bonnefon *
6*2851c541SNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify
7*2851c541SNicolas Bonnefon * it under the terms of the GNU General Public License as published by
8*2851c541SNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or
9*2851c541SNicolas Bonnefon * (at your option) any later version.
10*2851c541SNicolas Bonnefon *
11*2851c541SNicolas Bonnefon * glogg is distributed in the hope that it will be useful,
12*2851c541SNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*2851c541SNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*2851c541SNicolas Bonnefon * GNU General Public License for more details.
15*2851c541SNicolas Bonnefon *
16*2851c541SNicolas Bonnefon * You should have received a copy of the GNU General Public License
17*2851c541SNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>.
18*2851c541SNicolas Bonnefon */
19*2851c541SNicolas Bonnefon
2062819106SNicolas Bonnefon #include <QSignalSpy>
2162819106SNicolas Bonnefon #include <QMutexLocker>
2262819106SNicolas Bonnefon #include <QFile>
2362819106SNicolas Bonnefon
2462819106SNicolas Bonnefon #include "testlogdata.h"
2562819106SNicolas Bonnefon #include "logdata.h"
2662819106SNicolas Bonnefon
2762819106SNicolas Bonnefon #if !defined( TMPDIR )
2862819106SNicolas Bonnefon #define TMPDIR "/tmp"
2962819106SNicolas Bonnefon #endif
3062819106SNicolas Bonnefon
315f0a91faSNicolas Bonnefon static const qint64 VBL_NB_LINES = 4999999LL;
3262819106SNicolas Bonnefon static const int VBL_LINE_PER_PAGE = 70;
3336c1f171SNicolas Bonnefon static const char* vbl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line\t\t%07d\n";
3436c1f171SNicolas Bonnefon static const int VBL_LINE_LENGTH = (76+2+7) ; // Without the final '\n' !
3536c1f171SNicolas Bonnefon static const int VBL_VISIBLE_LINE_LENGTH = (76+8+4+7); // Without the final '\n' !
3662819106SNicolas Bonnefon
375f0a91faSNicolas Bonnefon static const qint64 SL_NB_LINES = 5000LL;
3862819106SNicolas Bonnefon static const int SL_LINE_PER_PAGE = 70;
3962819106SNicolas Bonnefon static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n";
4062819106SNicolas Bonnefon static const int SL_LINE_LENGTH = 83; // Without the final '\n' !
4162819106SNicolas Bonnefon
42b76c5bcfSNicolas Bonnefon static const char* partial_line_begin = "123... beginning of line.";
43b76c5bcfSNicolas Bonnefon static const char* partial_line_end = " end of line 123.\n";
44b76c5bcfSNicolas Bonnefon
initTestCase()4562819106SNicolas Bonnefon void TestLogData::initTestCase()
4662819106SNicolas Bonnefon {
4762819106SNicolas Bonnefon QVERIFY( generateDataFiles() );
4862819106SNicolas Bonnefon }
4962819106SNicolas Bonnefon
simpleLoad()5062819106SNicolas Bonnefon void TestLogData::simpleLoad()
5162819106SNicolas Bonnefon {
5262819106SNicolas Bonnefon LogData logData;
5362819106SNicolas Bonnefon QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) );
5462819106SNicolas Bonnefon
5562819106SNicolas Bonnefon // Register for notification file is loaded
56cef94aa9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished( bool ) ),
5762819106SNicolas Bonnefon this, SLOT( loadingFinished() ) );
5862819106SNicolas Bonnefon
5962819106SNicolas Bonnefon QBENCHMARK {
605f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" );
6162819106SNicolas Bonnefon // Wait for the loading to be done
6262819106SNicolas Bonnefon {
6362819106SNicolas Bonnefon QApplication::exec();
6462819106SNicolas Bonnefon }
6562819106SNicolas Bonnefon }
6662819106SNicolas Bonnefon
6762819106SNicolas Bonnefon // Disconnect all signals
6862819106SNicolas Bonnefon disconnect( &logData, 0 );
6962819106SNicolas Bonnefon }
7062819106SNicolas Bonnefon
multipleLoad()7162819106SNicolas Bonnefon void TestLogData::multipleLoad()
7262819106SNicolas Bonnefon {
7362819106SNicolas Bonnefon LogData logData;
74cef94aa9SNicolas Bonnefon QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished( bool ) ) );
7562819106SNicolas Bonnefon
7662819106SNicolas Bonnefon // Register for notification file is loaded
77cef94aa9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished( bool ) ),
7862819106SNicolas Bonnefon this, SLOT( loadingFinished() ) );
7962819106SNicolas Bonnefon
8062819106SNicolas Bonnefon // Start loading the VBL
815f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" );
8262819106SNicolas Bonnefon
8362819106SNicolas Bonnefon // Immediately interrupt the loading
8462819106SNicolas Bonnefon logData.interruptLoading();
8562819106SNicolas Bonnefon
8662819106SNicolas Bonnefon // and wait for the signal
8762819106SNicolas Bonnefon QApplication::exec();
8862819106SNicolas Bonnefon
8962819106SNicolas Bonnefon // Check we have an empty file
9062819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 1 );
91cef94aa9SNicolas Bonnefon // TODO: check loadingFinished arg == false
925f0a91faSNicolas Bonnefon QCOMPARE( logData.getNbLine(), 0LL );
9362819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), 0 );
9462819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), 0LL );
9562819106SNicolas Bonnefon
9662819106SNicolas Bonnefon // Restart the VBL
975f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" );
9862819106SNicolas Bonnefon
9962819106SNicolas Bonnefon // Ensure the counting has started
100a5fe2cedSNicolas Bonnefon {
10162819106SNicolas Bonnefon QMutex mutex;
10262819106SNicolas Bonnefon QWaitCondition sleep;
103a5fe2cedSNicolas Bonnefon // sleep.wait( &mutex, 10 );
104a5fe2cedSNicolas Bonnefon }
10562819106SNicolas Bonnefon
10662819106SNicolas Bonnefon // Load the SL (should block until VBL is fully indexed)
1075f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/smalllog.txt" );
10862819106SNicolas Bonnefon
10962819106SNicolas Bonnefon // and wait for the 2 signals (one for each file)
11062819106SNicolas Bonnefon QApplication::exec();
11162819106SNicolas Bonnefon QApplication::exec();
11262819106SNicolas Bonnefon
11362819106SNicolas Bonnefon // Check we have the small log loaded
11462819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 3 );
11562819106SNicolas Bonnefon QCOMPARE( logData.getNbLine(), SL_NB_LINES );
11662819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH );
11762819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) );
11862819106SNicolas Bonnefon
11962819106SNicolas Bonnefon // Restart the VBL again
1205f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/verybiglog.txt" );
12162819106SNicolas Bonnefon
12262819106SNicolas Bonnefon // Immediately interrupt the loading
12362819106SNicolas Bonnefon logData.interruptLoading();
12462819106SNicolas Bonnefon
12562819106SNicolas Bonnefon // and wait for the signal
12662819106SNicolas Bonnefon QApplication::exec();
12762819106SNicolas Bonnefon
12862819106SNicolas Bonnefon // Check the small log has been restored
12962819106SNicolas Bonnefon QCOMPARE( finishedSpy.count(), 4 );
13062819106SNicolas Bonnefon QCOMPARE( logData.getNbLine(), SL_NB_LINES );
13162819106SNicolas Bonnefon QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH );
13262819106SNicolas Bonnefon QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) );
13362819106SNicolas Bonnefon
13462819106SNicolas Bonnefon // Disconnect all signals
13562819106SNicolas Bonnefon disconnect( &logData, 0 );
13662819106SNicolas Bonnefon }
13762819106SNicolas Bonnefon
13862819106SNicolas Bonnefon //
13962819106SNicolas Bonnefon // Private functions
14062819106SNicolas Bonnefon //
loadingFinished()14162819106SNicolas Bonnefon void TestLogData::loadingFinished()
14262819106SNicolas Bonnefon {
14362819106SNicolas Bonnefon QApplication::quit();
14462819106SNicolas Bonnefon }
14562819106SNicolas Bonnefon
generateDataFiles()14662819106SNicolas Bonnefon bool TestLogData::generateDataFiles()
14762819106SNicolas Bonnefon {
14862819106SNicolas Bonnefon char newLine[90];
14962819106SNicolas Bonnefon
15062819106SNicolas Bonnefon QFile file( TMPDIR "/verybiglog.txt" );
15162819106SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) {
15262819106SNicolas Bonnefon for (int i = 0; i < VBL_NB_LINES; i++) {
15362819106SNicolas Bonnefon snprintf(newLine, 89, vbl_format, i);
15462819106SNicolas Bonnefon file.write( newLine, qstrlen(newLine) );
15562819106SNicolas Bonnefon }
15662819106SNicolas Bonnefon }
15762819106SNicolas Bonnefon else {
15862819106SNicolas Bonnefon return false;
15962819106SNicolas Bonnefon }
16062819106SNicolas Bonnefon file.close();
16162819106SNicolas Bonnefon
16262819106SNicolas Bonnefon file.setFileName( TMPDIR "/smalllog.txt" );
16362819106SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) {
16462819106SNicolas Bonnefon for (int i = 0; i < SL_NB_LINES; i++) {
16562819106SNicolas Bonnefon snprintf(newLine, 89, sl_format, i);
16662819106SNicolas Bonnefon file.write( newLine, qstrlen(newLine) );
16762819106SNicolas Bonnefon }
16862819106SNicolas Bonnefon }
16962819106SNicolas Bonnefon else {
17062819106SNicolas Bonnefon return false;
17162819106SNicolas Bonnefon }
17262819106SNicolas Bonnefon file.close();
17362819106SNicolas Bonnefon
17462819106SNicolas Bonnefon return true;
17562819106SNicolas Bonnefon }
176