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 16f35c72b9SNicolas Bonnefon static const int ML_NB_LINES = 15000; 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 19f35c72b9SNicolas Bonnefon static const int SL_NB_LINES = 2000; 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 36f35c72b9SNicolas Bonnefon QVERIFY( 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*0ed63e59SNicolas Bonnefon int matches[] = { 0, 15, 20, 135 }; 52*0ed63e59SNicolas 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 } 61*0ed63e59SNicolas Bonnefon } 62*0ed63e59SNicolas Bonnefon 63*0ed63e59SNicolas Bonnefon QCOMPARE( progressSpy.count(), 4 ); 64f35c72b9SNicolas Bonnefon 65f35c72b9SNicolas Bonnefon // Check the search 66f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 122 ), true ); 67f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 123 ), false ); 68f35c72b9SNicolas Bonnefon // Line beyond limit 69f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 60000 ), false ); 70f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->getMatchingLineNumber( 0 ), 122 ); 71f35c72b9SNicolas Bonnefon 72*0ed63e59SNicolas Bonnefon // Now let's try interrupting a search 73*0ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 74*0ed63e59SNicolas Bonnefon // ... wait for two chunks. 75*0ed63e59SNicolas Bonnefon QApplication::exec(); 76*0ed63e59SNicolas Bonnefon QApplication::exec(); 77*0ed63e59SNicolas Bonnefon // and interrupt! 78*0ed63e59SNicolas Bonnefon filteredData->interruptSearch(); 79*0ed63e59SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[1] ); 80*0ed63e59SNicolas Bonnefon QApplication::exec(); 81*0ed63e59SNicolas Bonnefon // After interrupt: should be 100% and the same number of matches 82*0ed63e59SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[1] ); 83*0ed63e59SNicolas Bonnefon 84*0ed63e59SNicolas Bonnefon // (because there is no guarantee when the search is 85*0ed63e59SNicolas Bonnefon // interrupted, we are not sure how many chunk of result 86*0ed63e59SNicolas Bonnefon // we will get.) 87*0ed63e59SNicolas 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 104f35c72b9SNicolas Bonnefon QVERIFY( 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 112*0ed63e59SNicolas 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 117*0ed63e59SNicolas Bonnefon QSignalSpy progressSpy( filteredData, 118*0ed63e59SNicolas Bonnefon SIGNAL( searchProgressed( int, int ) ) ); 119f35c72b9SNicolas Bonnefon 120f35c72b9SNicolas Bonnefon // Start the search, and immediately another one 121*0ed63e59SNicolas Bonnefon // (the second call should block until the first search is done) 122f35c72b9SNicolas Bonnefon filteredData->runSearch( QRegExp( "1234" ) ); 123*0ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 124f35c72b9SNicolas Bonnefon 125*0ed63e59SNicolas 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 129f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), 12 ); 130f35c72b9SNicolas Bonnefon 131*0ed63e59SNicolas Bonnefon QCOMPARE( progressSpy.count(), 4 ); 132*0ed63e59SNicolas Bonnefon 133*0ed63e59SNicolas Bonnefon // Now a tricky one: we run a search and immediately attach a new file 134*0ed63e59SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 135*0ed63e59SNicolas Bonnefon QApplication::exec(); 136*0ed63e59SNicolas Bonnefon logData.attachFile( TMPDIR "/mediumlog.txt" ); 137*0ed63e59SNicolas Bonnefon 138*0ed63e59SNicolas Bonnefon // We don't expect meaningful results but it should not crash! 139*0ed63e59SNicolas Bonnefon for ( int i = 0; i < 3; i++ ) 140*0ed63e59SNicolas Bonnefon QApplication::exec(); 141*0ed63e59SNicolas 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