1f35c72b9SNicolas Bonnefon #include <QSignalSpy> 2f35c72b9SNicolas Bonnefon #include <QMutexLocker> 3f35c72b9SNicolas Bonnefon #include <QFile> 4f35c72b9SNicolas Bonnefon 5f35c72b9SNicolas Bonnefon #include "testlogfiltereddata.h" 6f35c72b9SNicolas Bonnefon #include "logdata.h" 7f35c72b9SNicolas Bonnefon 8f35c72b9SNicolas Bonnefon #if QT_VERSION < 0x040500 9f35c72b9SNicolas Bonnefon #define QBENCHMARK 10f35c72b9SNicolas Bonnefon #endif 11f35c72b9SNicolas Bonnefon 12f35c72b9SNicolas Bonnefon #if !defined( TMPDIR ) 13f35c72b9SNicolas Bonnefon #define TMPDIR "/tmp" 14f35c72b9SNicolas Bonnefon #endif 15f35c72b9SNicolas Bonnefon 16*5f0a91faSNicolas Bonnefon static const qint64 ML_NB_LINES = 15000LL; 17f35c72b9SNicolas Bonnefon static const char* ml_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 18f35c72b9SNicolas Bonnefon 19*5f0a91faSNicolas Bonnefon static const qint64 SL_NB_LINES = 2000LL; 20f35c72b9SNicolas Bonnefon static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 21f35c72b9SNicolas Bonnefon 22f35c72b9SNicolas Bonnefon void TestLogFilteredData::initTestCase() 23f35c72b9SNicolas Bonnefon { 24f35c72b9SNicolas Bonnefon QVERIFY( generateDataFiles() ); 25f35c72b9SNicolas Bonnefon } 26f35c72b9SNicolas Bonnefon 27f35c72b9SNicolas Bonnefon void TestLogFilteredData::simpleSearch() 28f35c72b9SNicolas Bonnefon { 29f35c72b9SNicolas Bonnefon LogData logData; 30f35c72b9SNicolas Bonnefon 31f35c72b9SNicolas Bonnefon // First load the tests file 32f35c72b9SNicolas Bonnefon // Register for notification file is loaded 33f35c72b9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished() ), 34f35c72b9SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 35f35c72b9SNicolas Bonnefon 36*5f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/mediumlog.txt" ); 37f35c72b9SNicolas Bonnefon // Wait for the loading to be done 38f35c72b9SNicolas Bonnefon { 39f35c72b9SNicolas Bonnefon QApplication::exec(); 40f35c72b9SNicolas Bonnefon } 41f35c72b9SNicolas Bonnefon 42f35c72b9SNicolas Bonnefon QCOMPARE( logData.getNbLine(), ML_NB_LINES ); 43f35c72b9SNicolas Bonnefon 44f35c72b9SNicolas Bonnefon // Now perform a simple search 45f35c72b9SNicolas Bonnefon LogFilteredData* filteredData = logData.getNewFilteredData(); 46f35c72b9SNicolas Bonnefon connect( filteredData, SIGNAL( searchProgressed( int, int ) ), 47f35c72b9SNicolas Bonnefon this, SLOT( searchProgressed( int, int ) ) ); 48f35c72b9SNicolas Bonnefon 49f35c72b9SNicolas Bonnefon QSignalSpy progressSpy( filteredData, SIGNAL( searchProgressed( int, int ) ) ); 50f35c72b9SNicolas Bonnefon 51*5f0a91faSNicolas Bonnefon qint64 matches[] = { 0, 15, 20, 135 }; 520ed63e59SNicolas Bonnefon QBENCHMARK { 53f35c72b9SNicolas Bonnefon // Start the search 54f35c72b9SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 55f35c72b9SNicolas Bonnefon 56f35c72b9SNicolas Bonnefon // And check we receive data in 4 chunks (the first being empty) 57f35c72b9SNicolas Bonnefon for ( int i = 0; i < 4; i++ ) { 58f35c72b9SNicolas Bonnefon QApplication::exec(); 59f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[i] ); 60f35c72b9SNicolas Bonnefon } 610ed63e59SNicolas Bonnefon } 620ed63e59SNicolas Bonnefon 630ed63e59SNicolas Bonnefon QCOMPARE( progressSpy.count(), 4 ); 64f35c72b9SNicolas Bonnefon 65f35c72b9SNicolas Bonnefon // Check the search 6646291021SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 123 ), true ); 6746291021SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 124 ), false ); 68f35c72b9SNicolas Bonnefon // Line beyond limit 69f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 60000 ), false ); 70*5f0a91faSNicolas Bonnefon QCOMPARE( filteredData->getMatchingLineNumber( 0 ), 123LL ); 71f35c72b9SNicolas Bonnefon 720ed63e59SNicolas Bonnefon // Now let's try interrupting a search 730ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 740ed63e59SNicolas Bonnefon // ... wait for two chunks. 750ed63e59SNicolas Bonnefon QApplication::exec(); 760ed63e59SNicolas Bonnefon QApplication::exec(); 770ed63e59SNicolas Bonnefon // and interrupt! 780ed63e59SNicolas Bonnefon filteredData->interruptSearch(); 790ed63e59SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[1] ); 800ed63e59SNicolas Bonnefon QApplication::exec(); 810ed63e59SNicolas Bonnefon // After interrupt: should be 100% and the same number of matches 82d0d26f4fSNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[2] ); 830ed63e59SNicolas Bonnefon 840ed63e59SNicolas Bonnefon // (because there is no guarantee when the search is 850ed63e59SNicolas Bonnefon // interrupted, we are not sure how many chunk of result 860ed63e59SNicolas Bonnefon // we will get.) 870ed63e59SNicolas Bonnefon 88f35c72b9SNicolas Bonnefon // Disconnect all signals 89f35c72b9SNicolas Bonnefon disconnect( &logData, 0 ); 90f35c72b9SNicolas Bonnefon 91f35c72b9SNicolas Bonnefon // Destroy the filtered data 92f35c72b9SNicolas Bonnefon delete filteredData; 93f35c72b9SNicolas Bonnefon } 94f35c72b9SNicolas Bonnefon 95f35c72b9SNicolas Bonnefon void TestLogFilteredData::multipleSearch() 96f35c72b9SNicolas Bonnefon { 97f35c72b9SNicolas Bonnefon LogData logData; 98f35c72b9SNicolas Bonnefon 99f35c72b9SNicolas Bonnefon // First load the tests file 100f35c72b9SNicolas Bonnefon // Register for notification file is loaded 101f35c72b9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished() ), 102f35c72b9SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 103f35c72b9SNicolas Bonnefon 104*5f0a91faSNicolas Bonnefon logData.attachFile( TMPDIR "/smalllog.txt" ); 105f35c72b9SNicolas Bonnefon // Wait for the loading to be done 106f35c72b9SNicolas Bonnefon { 107f35c72b9SNicolas Bonnefon QApplication::exec(); 108f35c72b9SNicolas Bonnefon } 109f35c72b9SNicolas Bonnefon 110f35c72b9SNicolas Bonnefon QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 111f35c72b9SNicolas Bonnefon 1120ed63e59SNicolas Bonnefon // Performs two searches in a row 113f35c72b9SNicolas Bonnefon LogFilteredData* filteredData = logData.getNewFilteredData(); 114f35c72b9SNicolas Bonnefon connect( filteredData, SIGNAL( searchProgressed( int, int ) ), 115f35c72b9SNicolas Bonnefon this, SLOT( searchProgressed( int, int ) ) ); 116f35c72b9SNicolas Bonnefon 1170ed63e59SNicolas Bonnefon QSignalSpy progressSpy( filteredData, 1180ed63e59SNicolas Bonnefon SIGNAL( searchProgressed( int, int ) ) ); 119f35c72b9SNicolas Bonnefon 120f35c72b9SNicolas Bonnefon // Start the search, and immediately another one 1210ed63e59SNicolas Bonnefon // (the second call should block until the first search is done) 122f35c72b9SNicolas Bonnefon filteredData->runSearch( QRegExp( "1234" ) ); 1230ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 124f35c72b9SNicolas Bonnefon 1250ed63e59SNicolas Bonnefon for ( int i = 0; i < 3; i++ ) 126f35c72b9SNicolas Bonnefon QApplication::exec(); 127f35c72b9SNicolas Bonnefon 128f35c72b9SNicolas Bonnefon // We should have the result for the 2nd search 129*5f0a91faSNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), 12LL ); 130f35c72b9SNicolas Bonnefon 1310ed63e59SNicolas Bonnefon QCOMPARE( progressSpy.count(), 4 ); 1320ed63e59SNicolas Bonnefon 1330ed63e59SNicolas Bonnefon // Now a tricky one: we run a search and immediately attach a new file 1340ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 1350ed63e59SNicolas Bonnefon QApplication::exec(); 1360ed63e59SNicolas Bonnefon logData.attachFile( TMPDIR "/mediumlog.txt" ); 1370ed63e59SNicolas Bonnefon 1380ed63e59SNicolas Bonnefon // We don't expect meaningful results but it should not crash! 13946291021SNicolas Bonnefon for ( int i = 0; i < 2; i++ ) 1400ed63e59SNicolas Bonnefon QApplication::exec(); 1410ed63e59SNicolas Bonnefon 142f35c72b9SNicolas Bonnefon // Disconnect all signals 143f35c72b9SNicolas Bonnefon disconnect( &logData, 0 ); 144f35c72b9SNicolas Bonnefon 145f35c72b9SNicolas Bonnefon // Destroy the filtered data 146f35c72b9SNicolas Bonnefon delete filteredData; 147f35c72b9SNicolas Bonnefon } 148f35c72b9SNicolas Bonnefon 149f35c72b9SNicolas Bonnefon // 150f35c72b9SNicolas Bonnefon // Private functions 151f35c72b9SNicolas Bonnefon // 152f35c72b9SNicolas Bonnefon void TestLogFilteredData::loadingFinished() 153f35c72b9SNicolas Bonnefon { 154f35c72b9SNicolas Bonnefon QApplication::quit(); 155f35c72b9SNicolas Bonnefon } 156f35c72b9SNicolas Bonnefon 157f35c72b9SNicolas Bonnefon void TestLogFilteredData::searchProgressed( int nbMatches, int completion ) 158f35c72b9SNicolas Bonnefon { 159f35c72b9SNicolas Bonnefon QApplication::quit(); 160f35c72b9SNicolas Bonnefon } 161f35c72b9SNicolas Bonnefon 162f35c72b9SNicolas Bonnefon bool TestLogFilteredData::generateDataFiles() 163f35c72b9SNicolas Bonnefon { 164f35c72b9SNicolas Bonnefon char newLine[90]; 165f35c72b9SNicolas Bonnefon 166f35c72b9SNicolas Bonnefon QFile file( TMPDIR "/mediumlog.txt" ); 167f35c72b9SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 168f35c72b9SNicolas Bonnefon for (int i = 0; i < ML_NB_LINES; i++) { 169f35c72b9SNicolas Bonnefon snprintf(newLine, 89, ml_format, i); 170f35c72b9SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 171f35c72b9SNicolas Bonnefon } 172f35c72b9SNicolas Bonnefon } 173f35c72b9SNicolas Bonnefon else { 174f35c72b9SNicolas Bonnefon return false; 175f35c72b9SNicolas Bonnefon } 176f35c72b9SNicolas Bonnefon file.close(); 177f35c72b9SNicolas Bonnefon 178f35c72b9SNicolas Bonnefon file.setFileName( TMPDIR "/smalllog.txt" ); 179f35c72b9SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 180f35c72b9SNicolas Bonnefon for (int i = 0; i < SL_NB_LINES; i++) { 181f35c72b9SNicolas Bonnefon snprintf(newLine, 89, sl_format, i); 182f35c72b9SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 183f35c72b9SNicolas Bonnefon } 184f35c72b9SNicolas Bonnefon } 185f35c72b9SNicolas Bonnefon else { 186f35c72b9SNicolas Bonnefon return false; 187f35c72b9SNicolas Bonnefon } 188f35c72b9SNicolas Bonnefon file.close(); 189f35c72b9SNicolas Bonnefon 190f35c72b9SNicolas Bonnefon return true; 191f35c72b9SNicolas Bonnefon } 192