1*f35c72b9SNicolas Bonnefon #include <QSignalSpy> 2*f35c72b9SNicolas Bonnefon #include <QMutexLocker> 3*f35c72b9SNicolas Bonnefon #include <QFile> 4*f35c72b9SNicolas Bonnefon 5*f35c72b9SNicolas Bonnefon #include "testlogfiltereddata.h" 6*f35c72b9SNicolas Bonnefon #include "logdata.h" 7*f35c72b9SNicolas Bonnefon 8*f35c72b9SNicolas Bonnefon #if QT_VERSION < 0x040500 9*f35c72b9SNicolas Bonnefon #define QBENCHMARK 10*f35c72b9SNicolas Bonnefon #endif 11*f35c72b9SNicolas Bonnefon 12*f35c72b9SNicolas Bonnefon #if !defined( TMPDIR ) 13*f35c72b9SNicolas Bonnefon #define TMPDIR "/tmp" 14*f35c72b9SNicolas Bonnefon #endif 15*f35c72b9SNicolas Bonnefon 16*f35c72b9SNicolas Bonnefon static const int ML_NB_LINES = 15000; 17*f35c72b9SNicolas Bonnefon static const char* ml_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 18*f35c72b9SNicolas Bonnefon 19*f35c72b9SNicolas Bonnefon static const int SL_NB_LINES = 2000; 20*f35c72b9SNicolas Bonnefon static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 21*f35c72b9SNicolas Bonnefon 22*f35c72b9SNicolas Bonnefon void TestLogFilteredData::initTestCase() 23*f35c72b9SNicolas Bonnefon { 24*f35c72b9SNicolas Bonnefon QVERIFY( generateDataFiles() ); 25*f35c72b9SNicolas Bonnefon } 26*f35c72b9SNicolas Bonnefon 27*f35c72b9SNicolas Bonnefon void TestLogFilteredData::simpleSearch() 28*f35c72b9SNicolas Bonnefon { 29*f35c72b9SNicolas Bonnefon LogData logData; 30*f35c72b9SNicolas Bonnefon 31*f35c72b9SNicolas Bonnefon // First load the tests file 32*f35c72b9SNicolas Bonnefon // Register for notification file is loaded 33*f35c72b9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished() ), 34*f35c72b9SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 35*f35c72b9SNicolas Bonnefon 36*f35c72b9SNicolas Bonnefon QVERIFY( logData.attachFile( TMPDIR "/mediumlog.txt" ) ); 37*f35c72b9SNicolas Bonnefon // Wait for the loading to be done 38*f35c72b9SNicolas Bonnefon { 39*f35c72b9SNicolas Bonnefon QApplication::exec(); 40*f35c72b9SNicolas Bonnefon } 41*f35c72b9SNicolas Bonnefon 42*f35c72b9SNicolas Bonnefon QCOMPARE( logData.getNbLine(), ML_NB_LINES ); 43*f35c72b9SNicolas Bonnefon 44*f35c72b9SNicolas Bonnefon // Now perform a simple search 45*f35c72b9SNicolas Bonnefon LogFilteredData* filteredData = logData.getNewFilteredData(); 46*f35c72b9SNicolas Bonnefon connect( filteredData, SIGNAL( searchProgressed( int, int ) ), 47*f35c72b9SNicolas Bonnefon this, SLOT( searchProgressed( int, int ) ) ); 48*f35c72b9SNicolas Bonnefon 49*f35c72b9SNicolas Bonnefon QSignalSpy progressSpy( filteredData, SIGNAL( searchProgressed( int, int ) ) ); 50*f35c72b9SNicolas Bonnefon 51*f35c72b9SNicolas Bonnefon // Start the search 52*f35c72b9SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 53*f35c72b9SNicolas Bonnefon 54*f35c72b9SNicolas Bonnefon // And check we receive data in 4 chunks (the first being empty) 55*f35c72b9SNicolas Bonnefon int matches[] = { 0, 15, 20, 135 }; 56*f35c72b9SNicolas Bonnefon for ( int i = 0; i < 4; i++ ) { 57*f35c72b9SNicolas Bonnefon QApplication::exec(); 58*f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), matches[i] ); 59*f35c72b9SNicolas Bonnefon } 60*f35c72b9SNicolas Bonnefon 61*f35c72b9SNicolas Bonnefon // Check the search 62*f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 122 ), true ); 63*f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 123 ), false ); 64*f35c72b9SNicolas Bonnefon // Line beyond limit 65*f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->isLineInMatchingList( 60000 ), false ); 66*f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->getMatchingLineNumber( 0 ), 122 ); 67*f35c72b9SNicolas Bonnefon 68*f35c72b9SNicolas Bonnefon // Disconnect all signals 69*f35c72b9SNicolas Bonnefon disconnect( &logData, 0 ); 70*f35c72b9SNicolas Bonnefon 71*f35c72b9SNicolas Bonnefon // Destroy the filtered data 72*f35c72b9SNicolas Bonnefon delete filteredData; 73*f35c72b9SNicolas Bonnefon } 74*f35c72b9SNicolas Bonnefon 75*f35c72b9SNicolas Bonnefon void TestLogFilteredData::multipleSearch() 76*f35c72b9SNicolas Bonnefon { 77*f35c72b9SNicolas Bonnefon LogData logData; 78*f35c72b9SNicolas Bonnefon 79*f35c72b9SNicolas Bonnefon // First load the tests file 80*f35c72b9SNicolas Bonnefon // Register for notification file is loaded 81*f35c72b9SNicolas Bonnefon connect( &logData, SIGNAL( loadingFinished() ), 82*f35c72b9SNicolas Bonnefon this, SLOT( loadingFinished() ) ); 83*f35c72b9SNicolas Bonnefon 84*f35c72b9SNicolas Bonnefon QVERIFY( logData.attachFile( TMPDIR "/smalllog.txt" ) ); 85*f35c72b9SNicolas Bonnefon // Wait for the loading to be done 86*f35c72b9SNicolas Bonnefon { 87*f35c72b9SNicolas Bonnefon QApplication::exec(); 88*f35c72b9SNicolas Bonnefon } 89*f35c72b9SNicolas Bonnefon 90*f35c72b9SNicolas Bonnefon QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 91*f35c72b9SNicolas Bonnefon 92*f35c72b9SNicolas Bonnefon // Now perform a simple search 93*f35c72b9SNicolas Bonnefon LogFilteredData* filteredData = logData.getNewFilteredData(); 94*f35c72b9SNicolas Bonnefon connect( filteredData, SIGNAL( searchProgressed( int, int ) ), 95*f35c72b9SNicolas Bonnefon this, SLOT( searchProgressed( int, int ) ) ); 96*f35c72b9SNicolas Bonnefon 97*f35c72b9SNicolas Bonnefon QSignalSpy progressSpy( filteredData, SIGNAL( searchProgressed( int, int ) ) ); 98*f35c72b9SNicolas Bonnefon 99*f35c72b9SNicolas Bonnefon // Start the search, and immediately another one 100*f35c72b9SNicolas Bonnefon filteredData->runSearch( QRegExp( "123" ) ); 101*f35c72b9SNicolas Bonnefon filteredData->runSearch( QRegExp( "1234" ) ); 102*f35c72b9SNicolas Bonnefon 103*f35c72b9SNicolas Bonnefon for ( int i = 0; i < 4; i++ ) 104*f35c72b9SNicolas Bonnefon QApplication::exec(); 105*f35c72b9SNicolas Bonnefon 106*f35c72b9SNicolas Bonnefon // We should have the result for the 2nd search 107*f35c72b9SNicolas Bonnefon QCOMPARE( filteredData->getNbLine(), 12 ); 108*f35c72b9SNicolas Bonnefon 109*f35c72b9SNicolas Bonnefon // Disconnect all signals 110*f35c72b9SNicolas Bonnefon disconnect( &logData, 0 ); 111*f35c72b9SNicolas Bonnefon 112*f35c72b9SNicolas Bonnefon // Destroy the filtered data 113*f35c72b9SNicolas Bonnefon delete filteredData; 114*f35c72b9SNicolas Bonnefon } 115*f35c72b9SNicolas Bonnefon 116*f35c72b9SNicolas Bonnefon // 117*f35c72b9SNicolas Bonnefon // Private functions 118*f35c72b9SNicolas Bonnefon // 119*f35c72b9SNicolas Bonnefon void TestLogFilteredData::loadingFinished() 120*f35c72b9SNicolas Bonnefon { 121*f35c72b9SNicolas Bonnefon QApplication::quit(); 122*f35c72b9SNicolas Bonnefon } 123*f35c72b9SNicolas Bonnefon 124*f35c72b9SNicolas Bonnefon void TestLogFilteredData::searchProgressed( int nbMatches, int completion ) 125*f35c72b9SNicolas Bonnefon { 126*f35c72b9SNicolas Bonnefon QApplication::quit(); 127*f35c72b9SNicolas Bonnefon } 128*f35c72b9SNicolas Bonnefon 129*f35c72b9SNicolas Bonnefon bool TestLogFilteredData::generateDataFiles() 130*f35c72b9SNicolas Bonnefon { 131*f35c72b9SNicolas Bonnefon char newLine[90]; 132*f35c72b9SNicolas Bonnefon 133*f35c72b9SNicolas Bonnefon QFile file( TMPDIR "/mediumlog.txt" ); 134*f35c72b9SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 135*f35c72b9SNicolas Bonnefon for (int i = 0; i < ML_NB_LINES; i++) { 136*f35c72b9SNicolas Bonnefon snprintf(newLine, 89, ml_format, i); 137*f35c72b9SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 138*f35c72b9SNicolas Bonnefon } 139*f35c72b9SNicolas Bonnefon } 140*f35c72b9SNicolas Bonnefon else { 141*f35c72b9SNicolas Bonnefon return false; 142*f35c72b9SNicolas Bonnefon } 143*f35c72b9SNicolas Bonnefon file.close(); 144*f35c72b9SNicolas Bonnefon 145*f35c72b9SNicolas Bonnefon file.setFileName( TMPDIR "/smalllog.txt" ); 146*f35c72b9SNicolas Bonnefon if ( file.open( QIODevice::WriteOnly ) ) { 147*f35c72b9SNicolas Bonnefon for (int i = 0; i < SL_NB_LINES; i++) { 148*f35c72b9SNicolas Bonnefon snprintf(newLine, 89, sl_format, i); 149*f35c72b9SNicolas Bonnefon file.write( newLine, qstrlen(newLine) ); 150*f35c72b9SNicolas Bonnefon } 151*f35c72b9SNicolas Bonnefon } 152*f35c72b9SNicolas Bonnefon else { 153*f35c72b9SNicolas Bonnefon return false; 154*f35c72b9SNicolas Bonnefon } 155*f35c72b9SNicolas Bonnefon file.close(); 156*f35c72b9SNicolas Bonnefon 157*f35c72b9SNicolas Bonnefon return true; 158*f35c72b9SNicolas Bonnefon } 159