1f35c72b9SNicolas Bonnefon #include <QSignalSpy> 2f35c72b9SNicolas Bonnefon #include <QMutexLocker> 3f35c72b9SNicolas Bonnefon #include <QFile> 4f35c72b9SNicolas Bonnefon 5f35c72b9SNicolas Bonnefon #include "testlogfiltereddata.h" 6f35c72b9SNicolas Bonnefon #include "logdata.h" 7*20ec540aSNicolas Bonnefon #include "logfiltereddata.h" 8f35c72b9SNicolas Bonnefon 9f35c72b9SNicolas Bonnefon #if QT_VERSION < 0x040500 10f35c72b9SNicolas Bonnefon #define QBENCHMARK 11f35c72b9SNicolas Bonnefon #endif 12f35c72b9SNicolas Bonnefon 13f35c72b9SNicolas Bonnefon #if !defined( TMPDIR ) 14f35c72b9SNicolas Bonnefon #define TMPDIR "/tmp" 15f35c72b9SNicolas Bonnefon #endif 16f35c72b9SNicolas Bonnefon 175f0a91faSNicolas Bonnefon static const qint64 ML_NB_LINES = 15000LL; 185fc7617dSNicolas Bonnefon static const char* ml_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line\t\t%06d\n"; 195fc7617dSNicolas Bonnefon static const int ML_VISIBLE_LINE_LENGTH = (76+8+4+6); // Without the final '\n' ! 20f35c72b9SNicolas Bonnefon 215f0a91faSNicolas Bonnefon static const qint64 SL_NB_LINES = 2000LL; 22f35c72b9SNicolas Bonnefon static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 23f35c72b9SNicolas Bonnefon 24f35c72b9SNicolas Bonnefon void TestLogFilteredData::initTestCase() 25f35c72b9SNicolas Bonnefon { 26f35c72b9SNicolas Bonnefon QVERIFY( generateDataFiles() ); 27f35c72b9SNicolas Bonnefon } 28f35c72b9SNicolas Bonnefon 29f35c72b9SNicolas Bonnefon void TestLogFilteredData::simpleSearch() 30f35c72b9SNicolas Bonnefon { 31f35c72b9SNicolas Bonnefon LogData logData; 32f35c72b9SNicolas Bonnefon 33f35c72b9SNicolas Bonnefon // First load the tests file 34f35c72b9SNicolas Bonnefon // Register for notification file is loaded 353ab7c5b5SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished( bool ) ), 36f35c72b9SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 37f35c72b9SNicolas Bonnefon 385f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/mediumlog.txt" ); 39f35c72b9SNicolas Bonnefon // Wait for the loading to be done 40f35c72b9SNicolas Bonnefon { 41f35c72b9SNicolas Bonnefon QApplication::exec(); 42f35c72b9SNicolas Bonnefon } 43f35c72b9SNicolas Bonnefon 44f35c72b9SNicolas Bonnefon QCOMPARE( logData.getNbLine(), ML_NB_LINES ); 45f35c72b9SNicolas Bonnefon 46f35c72b9SNicolas Bonnefon // Now perform a simple search 47f35c72b9SNicolas Bonnefon LogFilteredData* filteredData = logData.getNewFilteredData(); 48f35c72b9SNicolas Bonnefon connect( filteredData, SIGNAL( searchProgressed( int, int ) ), 49f35c72b9SNicolas Bonnefon this, SLOT( searchProgressed( int, int ) ) ); 50f35c72b9SNicolas Bonnefon 51f35c72b9SNicolas Bonnefon QSignalSpy progressSpy( filteredData, SIGNAL( searchProgressed( int, int ) ) ); 52f35c72b9SNicolas Bonnefon 535f0a91faSNicolas Bonnefon qint64 matches[] = { 0, 15, 20, 135 }; 540ed63e59SNicolas Bonnefon QBENCHMARK { 55f35c72b9SNicolas Bonnefon // Start the search 56f35c72b9SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 57f35c72b9SNicolas Bonnefon 58f35c72b9SNicolas Bonnefon // And check we receive data in 4 chunks (the first being empty) 59f35c72b9SNicolas Bonnefon for ( int i = 0; i < 4; i++ ) { 60f35c72b9SNicolas Bonnefon QApplication::exec(); 61f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[i] ); 62f35c72b9SNicolas Bonnefon } 630ed63e59SNicolas Bonnefon } 640ed63e59SNicolas Bonnefon 650ed63e59SNicolas Bonnefon QCOMPARE( progressSpy.count(), 4 ); 66f35c72b9SNicolas Bonnefon 67f35c72b9SNicolas Bonnefon // Check the search 6846291021SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 123 ), true ); 6946291021SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 124 ), false ); 705fc7617dSNicolas Bonnefon QCOMPARE( filteredData->getMaxLength(), ML_VISIBLE_LINE_LENGTH ); 715fc7617dSNicolas Bonnefon QCOMPARE( filteredData->getLineLength( 12 ), ML_VISIBLE_LINE_LENGTH ); 725fc7617dSNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), 135LL ); 73f35c72b9SNicolas Bonnefon // Line beyond limit 74f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 60000 ), false ); 755f0a91faSNicolas Bonnefon QCOMPARE( filteredData->getMatchingLineNumber( 0 ), 123LL ); 76f35c72b9SNicolas Bonnefon 770ed63e59SNicolas Bonnefon // Now let's try interrupting a search 780ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 790ed63e59SNicolas Bonnefon // ... wait for two chunks. 800ed63e59SNicolas Bonnefon QApplication::exec(); 810ed63e59SNicolas Bonnefon QApplication::exec(); 820ed63e59SNicolas Bonnefon // and interrupt! 830ed63e59SNicolas Bonnefon filteredData->interruptSearch(); 840ed63e59SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[1] ); 850ed63e59SNicolas Bonnefon QApplication::exec(); 860ed63e59SNicolas Bonnefon // After interrupt: should be 100% and the same number of matches 87d0d26f4fSNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[2] ); 880ed63e59SNicolas Bonnefon 890ed63e59SNicolas Bonnefon // (because there is no guarantee when the search is 900ed63e59SNicolas Bonnefon // interrupted, we are not sure how many chunk of result 910ed63e59SNicolas Bonnefon // we will get.) 920ed63e59SNicolas Bonnefon 93f35c72b9SNicolas Bonnefon // Disconnect all signals 94f35c72b9SNicolas Bonnefon disconnect( &logData, 0 ); 95f35c72b9SNicolas Bonnefon 96f35c72b9SNicolas Bonnefon // Destroy the filtered data 97f35c72b9SNicolas Bonnefon delete filteredData; 98f35c72b9SNicolas Bonnefon } 99f35c72b9SNicolas Bonnefon 100f35c72b9SNicolas Bonnefon void TestLogFilteredData::multipleSearch() 101f35c72b9SNicolas Bonnefon { 102f35c72b9SNicolas Bonnefon LogData logData; 103f35c72b9SNicolas Bonnefon 104f35c72b9SNicolas Bonnefon // First load the tests file 105f35c72b9SNicolas Bonnefon // Register for notification file is loaded 1063ab7c5b5SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished( bool ) ), 107f35c72b9SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 108f35c72b9SNicolas Bonnefon 1095f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/smalllog.txt" ); 110f35c72b9SNicolas Bonnefon // Wait for the loading to be done 111f35c72b9SNicolas Bonnefon { 112f35c72b9SNicolas Bonnefon QApplication::exec(); 113f35c72b9SNicolas Bonnefon } 114f35c72b9SNicolas Bonnefon 115f35c72b9SNicolas Bonnefon QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 116f35c72b9SNicolas Bonnefon 1170ed63e59SNicolas Bonnefon // Performs two searches in a row 118f35c72b9SNicolas Bonnefon LogFilteredData* filteredData = logData.getNewFilteredData(); 119f35c72b9SNicolas Bonnefon connect( filteredData, SIGNAL( searchProgressed( int, int ) ), 120f35c72b9SNicolas Bonnefon this, SLOT( searchProgressed( int, int ) ) ); 121f35c72b9SNicolas Bonnefon 1220ed63e59SNicolas Bonnefon QSignalSpy progressSpy( filteredData, 1230ed63e59SNicolas Bonnefon SIGNAL( searchProgressed( int, int ) ) ); 124f35c72b9SNicolas Bonnefon 125f35c72b9SNicolas Bonnefon // Start the search, and immediately another one 1260ed63e59SNicolas Bonnefon // (the second call should block until the first search is done) 127f35c72b9SNicolas Bonnefon filteredData->runSearch( QRegExp( "1234" ) ); 1280ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 129f35c72b9SNicolas Bonnefon 1300ed63e59SNicolas Bonnefon for ( int i = 0; i < 3; i++ ) 131f35c72b9SNicolas Bonnefon QApplication::exec(); 132f35c72b9SNicolas Bonnefon 133f35c72b9SNicolas Bonnefon // We should have the result for the 2nd search 1345f0a91faSNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), 12LL ); 135f35c72b9SNicolas Bonnefon 1360ed63e59SNicolas Bonnefon QCOMPARE( progressSpy.count(), 4 ); 1370ed63e59SNicolas Bonnefon 1380ed63e59SNicolas Bonnefon // Now a tricky one: we run a search and immediately attach a new file 1390ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 1400ed63e59SNicolas Bonnefon QApplication::exec(); 1410ed63e59SNicolas Bonnefon logData.attachFile( TMPDIR "/mediumlog.txt" ); 1420ed63e59SNicolas Bonnefon 1430ed63e59SNicolas Bonnefon // We don't expect meaningful results but it should not crash! 14446291021SNicolas Bonnefon for ( int i = 0; i < 2; i++ ) 1450ed63e59SNicolas Bonnefon QApplication::exec(); 1460ed63e59SNicolas Bonnefon 147f35c72b9SNicolas Bonnefon // Disconnect all signals 148f35c72b9SNicolas Bonnefon disconnect( &logData, 0 ); 149f35c72b9SNicolas Bonnefon 150f35c72b9SNicolas Bonnefon // Destroy the filtered data 151f35c72b9SNicolas Bonnefon delete filteredData; 152f35c72b9SNicolas Bonnefon } 153f35c72b9SNicolas Bonnefon 154f35c72b9SNicolas Bonnefon // 155f35c72b9SNicolas Bonnefon // Private functions 156f35c72b9SNicolas Bonnefon // 157f35c72b9SNicolas Bonnefon void TestLogFilteredData::loadingFinished() 158f35c72b9SNicolas Bonnefon { 159f35c72b9SNicolas Bonnefon QApplication::quit(); 160f35c72b9SNicolas Bonnefon } 161f35c72b9SNicolas Bonnefon 162f35c72b9SNicolas Bonnefon void TestLogFilteredData::searchProgressed( int nbMatches, int completion ) 163f35c72b9SNicolas Bonnefon { 164f35c72b9SNicolas Bonnefon QApplication::quit(); 165f35c72b9SNicolas Bonnefon } 166f35c72b9SNicolas Bonnefon 167f35c72b9SNicolas Bonnefon bool TestLogFilteredData::generateDataFiles() 168f35c72b9SNicolas Bonnefon { 169f35c72b9SNicolas Bonnefon char newLine[90]; 170f35c72b9SNicolas Bonnefon 171f35c72b9SNicolas Bonnefon QFile file( TMPDIR "/mediumlog.txt" ); 172f35c72b9SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 173f35c72b9SNicolas Bonnefon for (int i = 0; i < ML_NB_LINES; i++) { 174f35c72b9SNicolas Bonnefon snprintf(newLine, 89, ml_format, i); 175f35c72b9SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 176f35c72b9SNicolas Bonnefon } 177f35c72b9SNicolas Bonnefon } 178f35c72b9SNicolas Bonnefon else { 179f35c72b9SNicolas Bonnefon return false; 180f35c72b9SNicolas Bonnefon } 181f35c72b9SNicolas Bonnefon file.close(); 182f35c72b9SNicolas Bonnefon 183f35c72b9SNicolas Bonnefon file.setFileName( TMPDIR "/smalllog.txt" ); 184f35c72b9SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 185f35c72b9SNicolas Bonnefon for (int i = 0; i < SL_NB_LINES; i++) { 186f35c72b9SNicolas Bonnefon snprintf(newLine, 89, sl_format, i); 187f35c72b9SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 188f35c72b9SNicolas Bonnefon } 189f35c72b9SNicolas Bonnefon } 190f35c72b9SNicolas Bonnefon else { 191f35c72b9SNicolas Bonnefon return false; 192f35c72b9SNicolas Bonnefon } 193f35c72b9SNicolas Bonnefon file.close(); 194f35c72b9SNicolas Bonnefon 195f35c72b9SNicolas Bonnefon return true; 196f35c72b9SNicolas Bonnefon } 197