1 #include <QSignalSpy> 2 #include <QMutexLocker> 3 #include <QFile> 4 5 #include "testlogdata.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 VBL_NB_LINES = 4999999; 17 static const int VBL_LINE_PER_PAGE = 70; 18 static const char* vbl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 19 static const int VBL_LINE_LENGTH = 83; // Without the final '\n' ! 20 21 static const int SL_NB_LINES = 5000; 22 static const int SL_LINE_PER_PAGE = 70; 23 static const char* sl_format="LOGDATA is a part of glogg, we are going to test it thoroughly, this is line %06d\n"; 24 static const int SL_LINE_LENGTH = 83; // Without the final '\n' ! 25 26 void TestLogData::initTestCase() 27 { 28 QVERIFY( generateDataFiles() ); 29 } 30 31 void TestLogData::simpleLoad() 32 { 33 LogData logData; 34 QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) ); 35 36 // First try to log a non existent file 37 QVERIFY( logData.attachFile( "/non/existent/file" ) == false ); 38 39 // Register for notification file is loaded 40 connect( &logData, SIGNAL( loadingFinished() ), 41 this, SLOT( loadingFinished() ) ); 42 43 QBENCHMARK { 44 QVERIFY( logData.attachFile( TMPDIR "/verybiglog.txt" ) ); 45 // Wait for the loading to be done 46 { 47 QApplication::exec(); 48 } 49 } 50 QCOMPARE( (qint64) progressSpy.count(), logData.getFileSize() / (5LL*1024*1024) + 1 ); 51 QCOMPARE( logData.getNbLine(), VBL_NB_LINES ); 52 QCOMPARE( logData.getMaxLength(), VBL_LINE_LENGTH ); 53 QCOMPARE( logData.getFileSize(), VBL_NB_LINES * (VBL_LINE_LENGTH+1LL) ); 54 55 // Disconnect all signals 56 disconnect( &logData, 0 ); 57 } 58 59 void TestLogData::multipleLoad() 60 { 61 LogData logData; 62 QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished() ) ); 63 64 // Register for notification file is loaded 65 connect( &logData, SIGNAL( loadingFinished() ), 66 this, SLOT( loadingFinished() ) ); 67 68 // Start loading the VBL 69 QVERIFY( logData.attachFile( TMPDIR "/verybiglog.txt" ) ); 70 71 // Immediately interrupt the loading 72 logData.interruptLoading(); 73 74 // and wait for the signal 75 QApplication::exec(); 76 77 // Check we have an empty file 78 QCOMPARE( finishedSpy.count(), 1 ); 79 QCOMPARE( logData.getNbLine(), 0 ); 80 QCOMPARE( logData.getMaxLength(), 0 ); 81 QCOMPARE( logData.getFileSize(), 0LL ); 82 83 // Restart the VBL 84 QVERIFY( logData.attachFile( TMPDIR "/verybiglog.txt" ) ); 85 86 // Ensure the counting has started 87 QMutex mutex; 88 QWaitCondition sleep; 89 sleep.wait( &mutex, 10 ); 90 91 // Load the SL (should block until VBL is fully indexed) 92 QVERIFY( logData.attachFile( TMPDIR "/smalllog.txt" ) ); 93 94 // and wait for the 2 signals (one for each file) 95 QApplication::exec(); 96 QApplication::exec(); 97 98 // Check we have the small log loaded 99 QCOMPARE( finishedSpy.count(), 3 ); 100 QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 101 QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 102 QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) ); 103 104 // Restart the VBL again 105 QVERIFY( logData.attachFile( TMPDIR "/verybiglog.txt" ) ); 106 107 // Immediately interrupt the loading 108 logData.interruptLoading(); 109 110 // and wait for the signal 111 QApplication::exec(); 112 113 // Check the small log has been restored 114 QCOMPARE( finishedSpy.count(), 4 ); 115 QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 116 QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 117 QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) ); 118 119 // Disconnect all signals 120 disconnect( &logData, 0 ); 121 } 122 123 void TestLogData::changingFile() 124 { 125 char newLine[90]; 126 LogData logData; 127 128 QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished() ) ); 129 QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) ); 130 131 // Register for notification file is loaded 132 connect( &logData, SIGNAL( loadingFinished() ), 133 this, SLOT( loadingFinished() ) ); 134 135 // Generate a small file 136 QFile file( TMPDIR "/changingfile.txt" ); 137 if ( file.open( QIODevice::WriteOnly ) ) { 138 for (int i = 0; i < 200; i++) { 139 snprintf(newLine, 89, sl_format, i); 140 file.write( newLine, qstrlen(newLine) ); 141 } 142 } 143 file.close(); 144 145 // Start loading it 146 QVERIFY( logData.attachFile( TMPDIR "/changingfile.txt" ) ); 147 148 // and wait for the signal 149 QApplication::exec(); 150 151 // Check we have the small file 152 QCOMPARE( finishedSpy.count(), 1 ); 153 QCOMPARE( logData.getNbLine(), 200 ); 154 QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 155 QCOMPARE( logData.getFileSize(), 200 * (SL_LINE_LENGTH+1LL) ); 156 157 // Add some data to it 158 if ( file.open( QIODevice::Append ) ) { 159 for (int i = 0; i < 200; i++) { 160 snprintf(newLine, 89, sl_format, i); 161 file.write( newLine, qstrlen(newLine) ); 162 } 163 } 164 file.close(); 165 166 // and wait for the signal 167 QApplication::exec(); 168 169 // Check we have a bigger file 170 QCOMPARE( finishedSpy.count(), 2 ); 171 QCOMPARE( logData.getNbLine(), 400 ); 172 QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 173 QCOMPARE( logData.getFileSize(), 400 * (SL_LINE_LENGTH+1LL) ); 174 175 // Truncate the file 176 QVERIFY( file.open( QIODevice::WriteOnly ) ); 177 file.close(); 178 179 // and wait for the signal 180 QApplication::exec(); 181 182 // Check we have an empty file 183 QCOMPARE( finishedSpy.count(), 3 ); 184 QCOMPARE( logData.getNbLine(), 0 ); 185 QCOMPARE( logData.getMaxLength(), 0 ); 186 QCOMPARE( logData.getFileSize(), 0LL ); 187 } 188 189 #if 0 190 void TestLogData::sequentialRead() 191 { 192 LogData* logData; 193 194 { 195 QByteArray array = generateData(); 196 logData = new LogData(array); 197 } 198 199 // Read all lines sequentially 200 QString s; 201 QBENCHMARK { 202 for (int i = 0; i < NB_LINES; i++) 203 s = logData->getLineString(i); 204 } 205 QVERIFY(s.length() > 0); 206 207 delete logData; 208 } 209 210 void TestLogData::randomPageRead() 211 { 212 LogData* logData; 213 214 { 215 QByteArray array = generateData(); 216 logData = new LogData(array); 217 } 218 219 // Read page by page from the beginning and the end 220 QString s; 221 QBENCHMARK { 222 for (int page = 0; page < (NB_LINES/LINE_PER_PAGE)-1; page++) 223 { 224 for (int i = page*LINE_PER_PAGE; i < ((page+1)*LINE_PER_PAGE); i++) 225 s = logData->getLineString(i); 226 int page_from_end = (NB_LINES/LINE_PER_PAGE) - page - 1; 227 for (int i = page_from_end*LINE_PER_PAGE; i < ((page_from_end+1)*LINE_PER_PAGE); i++) 228 s = logData->getLineString(i); 229 } 230 } 231 QVERIFY(s.length() > 0); 232 233 delete logData; 234 } 235 #endif 236 237 // 238 // Private functions 239 // 240 void TestLogData::loadingFinished() 241 { 242 QApplication::quit(); 243 } 244 245 bool TestLogData::generateDataFiles() 246 { 247 char newLine[90]; 248 249 QFile file( TMPDIR "/verybiglog.txt" ); 250 if ( file.open( QIODevice::WriteOnly ) ) { 251 for (int i = 0; i < VBL_NB_LINES; i++) { 252 snprintf(newLine, 89, vbl_format, i); 253 file.write( newLine, qstrlen(newLine) ); 254 } 255 } 256 else { 257 return false; 258 } 259 file.close(); 260 261 file.setFileName( TMPDIR "/smalllog.txt" ); 262 if ( file.open( QIODevice::WriteOnly ) ) { 263 for (int i = 0; i < SL_NB_LINES; i++) { 264 snprintf(newLine, 89, sl_format, i); 265 file.write( newLine, qstrlen(newLine) ); 266 } 267 } 268 else { 269 return false; 270 } 271 file.close(); 272 273 return true; 274 } 275