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 %07d\n"; 19 static const int VBL_LINE_LENGTH = 84; // 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 { 88 QMutex mutex; 89 QWaitCondition sleep; 90 // sleep.wait( &mutex, 10 ); 91 } 92 93 // Load the SL (should block until VBL is fully indexed) 94 QVERIFY( logData.attachFile( TMPDIR "/smalllog.txt" ) ); 95 96 // and wait for the 2 signals (one for each file) 97 QApplication::exec(); 98 QApplication::exec(); 99 100 // Check we have the small log loaded 101 QCOMPARE( finishedSpy.count(), 3 ); 102 QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 103 QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 104 QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) ); 105 106 // Restart the VBL again 107 QVERIFY( logData.attachFile( TMPDIR "/verybiglog.txt" ) ); 108 109 // Immediately interrupt the loading 110 logData.interruptLoading(); 111 112 // and wait for the signal 113 QApplication::exec(); 114 115 // Check the small log has been restored 116 QCOMPARE( finishedSpy.count(), 4 ); 117 QCOMPARE( logData.getNbLine(), SL_NB_LINES ); 118 QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 119 QCOMPARE( logData.getFileSize(), SL_NB_LINES * (SL_LINE_LENGTH+1LL) ); 120 121 // Disconnect all signals 122 disconnect( &logData, 0 ); 123 } 124 125 void TestLogData::changingFile() 126 { 127 char newLine[90]; 128 LogData logData; 129 130 QSignalSpy finishedSpy( &logData, SIGNAL( loadingFinished() ) ); 131 QSignalSpy progressSpy( &logData, SIGNAL( loadingProgressed( int ) ) ); 132 133 // Register for notification file is loaded 134 connect( &logData, SIGNAL( loadingFinished() ), 135 this, SLOT( loadingFinished() ) ); 136 137 // Generate a small file 138 QFile file( TMPDIR "/changingfile.txt" ); 139 if ( file.open( QIODevice::WriteOnly ) ) { 140 for (int i = 0; i < 200; i++) { 141 snprintf(newLine, 89, sl_format, i); 142 file.write( newLine, qstrlen(newLine) ); 143 } 144 } 145 file.close(); 146 147 // Start loading it 148 QVERIFY( logData.attachFile( TMPDIR "/changingfile.txt" ) ); 149 150 // and wait for the signal 151 QApplication::exec(); 152 153 // Check we have the small file 154 QCOMPARE( finishedSpy.count(), 1 ); 155 QCOMPARE( logData.getNbLine(), 200 ); 156 QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 157 QCOMPARE( logData.getFileSize(), 200 * (SL_LINE_LENGTH+1LL) ); 158 159 // Add some data to it 160 if ( file.open( QIODevice::Append ) ) { 161 for (int i = 0; i < 200; i++) { 162 snprintf(newLine, 89, sl_format, i); 163 file.write( newLine, qstrlen(newLine) ); 164 } 165 } 166 file.close(); 167 168 // and wait for the signal 169 QApplication::exec(); 170 171 // Check we have a bigger file 172 QCOMPARE( finishedSpy.count(), 2 ); 173 QCOMPARE( logData.getNbLine(), 400 ); 174 QCOMPARE( logData.getMaxLength(), SL_LINE_LENGTH ); 175 QCOMPARE( logData.getFileSize(), 400 * (SL_LINE_LENGTH+1LL) ); 176 177 // Truncate the file 178 QVERIFY( file.open( QIODevice::WriteOnly ) ); 179 file.close(); 180 181 // and wait for the signal 182 QApplication::exec(); 183 184 // Check we have an empty file 185 QCOMPARE( finishedSpy.count(), 3 ); 186 QCOMPARE( logData.getNbLine(), 0 ); 187 QCOMPARE( logData.getMaxLength(), 0 ); 188 QCOMPARE( logData.getFileSize(), 0LL ); 189 } 190 191 #if 0 192 void TestLogData::sequentialRead() 193 { 194 LogData* logData; 195 196 { 197 QByteArray array = generateData(); 198 logData = new LogData(array); 199 } 200 201 // Read all lines sequentially 202 QString s; 203 QBENCHMARK { 204 for (int i = 0; i < NB_LINES; i++) 205 s = logData->getLineString(i); 206 } 207 QVERIFY(s.length() > 0); 208 209 delete logData; 210 } 211 212 void TestLogData::randomPageRead() 213 { 214 LogData* logData; 215 216 { 217 QByteArray array = generateData(); 218 logData = new LogData(array); 219 } 220 221 // Read page by page from the beginning and the end 222 QString s; 223 QBENCHMARK { 224 for (int page = 0; page < (NB_LINES/LINE_PER_PAGE)-1; page++) 225 { 226 for (int i = page*LINE_PER_PAGE; i < ((page+1)*LINE_PER_PAGE); i++) 227 s = logData->getLineString(i); 228 int page_from_end = (NB_LINES/LINE_PER_PAGE) - page - 1; 229 for (int i = page_from_end*LINE_PER_PAGE; i < ((page_from_end+1)*LINE_PER_PAGE); i++) 230 s = logData->getLineString(i); 231 } 232 } 233 QVERIFY(s.length() > 0); 234 235 delete logData; 236 } 237 #endif 238 239 // 240 // Private functions 241 // 242 void TestLogData::loadingFinished() 243 { 244 QApplication::quit(); 245 } 246 247 bool TestLogData::generateDataFiles() 248 { 249 char newLine[90]; 250 251 QFile file( TMPDIR "/verybiglog.txt" ); 252 if ( file.open( QIODevice::WriteOnly ) ) { 253 for (int i = 0; i < VBL_NB_LINES; i++) { 254 snprintf(newLine, 89, vbl_format, i); 255 file.write( newLine, qstrlen(newLine) ); 256 } 257 } 258 else { 259 return false; 260 } 261 file.close(); 262 263 file.setFileName( TMPDIR "/smalllog.txt" ); 264 if ( file.open( QIODevice::WriteOnly ) ) { 265 for (int i = 0; i < SL_NB_LINES; i++) { 266 snprintf(newLine, 89, sl_format, i); 267 file.write( newLine, qstrlen(newLine) ); 268 } 269 } 270 else { 271 return false; 272 } 273 file.close(); 274 275 return true; 276 } 277