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