libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2010 Regents of the University of Michigan 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 #include "InputFileTest.h" 00018 #include <assert.h> 00019 #include <iostream> 00020 #include "StringBasics.h" 00021 00022 void testAdditional(const char *extension); 00023 void testWrite(); 00024 00025 00026 int main(int argc, char ** argv) 00027 { 00028 IFILE_Test myFile; 00029 00030 myFile.test(); 00031 00032 testWrite(); 00033 00034 std::cout << "\nAdditional Tests: " << std::endl; 00035 00036 testAdditional("txt"); 00037 #ifdef __ZLIB_AVAILABLE__ 00038 testAdditional("gz"); 00039 #endif 00040 } 00041 00042 00043 const int IFILE_Test::TEST_FILE_SIZE = 37; 00044 const int IFILE_Test::BGZF_TEST_FILE_SIZE = 93; 00045 const std::string IFILE_Test::TEST_FILE_CONTENTS = "ABCDabcd1234\nEFGefg567\nhijklHIJKL8910"; 00046 00047 void IFILE_Test::test() 00048 { 00049 std::cout << "\nUncompressedFileType Tests:" << std::endl; 00050 testAll("txt"); 00051 00052 #ifdef __ZLIB_AVAILABLE__ 00053 std::cout << "\nGzipFileType Tests:" << std::endl; 00054 testAll("gz"); 00055 00056 std::cout << "\nBgzfFileType Tests:" << std::endl; 00057 testAll("bam"); 00058 00059 std::cout << "\n.glf file Tests:" << std::endl; 00060 testAll("glf"); 00061 #endif 00062 } 00063 00064 00065 void IFILE_Test::testAll(const char* extension) 00066 { 00067 test_readFromFile(extension); 00068 test_readTilChar(extension); 00069 test_ifeof_ifrewind(extension); 00070 test_ifread_ifgetc(extension); 00071 test_ifclose(extension); 00072 test_ifseek(extension); 00073 test_noExistRead(extension); 00074 } 00075 00076 00077 void IFILE_Test::test_readFromFile(const char* extension) 00078 { 00079 // First open the test file. 00080 openFile(extension); 00081 00082 // Verify the file successfully opened. 00083 assert(myFileTypePtr != NULL); 00084 assert(isOpen()); 00085 assert(myFileTypePtr->isOpen()); 00086 00087 // Track how many bytes are read by each call. 00088 int numBytesRead = 0; 00089 00090 // Track the total number of the bytes that have been read from the file 00091 // at any given point. 00092 int totalBytesPreviouslyRead = 0; 00093 00094 // Test readFromFile. 00095 numBytesRead = readFromFile(myTestBuffer, 4); 00096 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]); 00097 assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]); 00098 assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]); 00099 assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]); 00100 assert(numBytesRead == 4); 00101 totalBytesPreviouslyRead += numBytesRead; 00102 // This read should not have affected the internal buffer. 00103 assert(myCurrentBufferSize == 0); 00104 assert(myBufferIndex == 0); 00105 // Should not be at eof 00106 assert(myFileTypePtr->eof() == false); 00107 assert(ifeof() == false); 00108 00109 // Read again to verify that the next characters could be read. 00110 numBytesRead = readFromFile(myTestBuffer, 2); 00111 // Read 2 more characters from the test file. 00112 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[4]); 00113 assert(myTestBuffer[1] == TEST_FILE_CONTENTS[5]); 00114 assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]); 00115 assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]); 00116 assert(numBytesRead == 2); 00117 totalBytesPreviouslyRead += numBytesRead; 00118 // This read should not have affected the internal buffer. 00119 assert(myCurrentBufferSize == 0); 00120 assert(myBufferIndex == 0); 00121 // Should not be at eof 00122 assert(myFileTypePtr->eof() == false); 00123 assert(ifeof() == false); 00124 00125 // Read the rest of the file. 00126 // Determine expected results for reading the rest of the file by 00127 // taking the substring starting after what had been previously read. 00128 numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00129 // Read the rest of the file, so the number of bytes read is 00130 // what was left in the file. 00131 assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead)); 00132 assert(numBytesRead != MAX_TEST_BUFFER_SIZE); 00133 for(int i = 0; i < numBytesRead; i++) 00134 { 00135 assert(myTestBuffer[i] == 00136 TEST_FILE_CONTENTS[totalBytesPreviouslyRead+i]); 00137 } 00138 totalBytesPreviouslyRead += numBytesRead; 00139 assert(myFileTypePtr->eof() != 0); 00140 assert(ifeof() != 0); 00141 00142 // Try to read one more time, making sure it doesn't read anything. 00143 numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00144 assert(numBytesRead == 0); 00145 // Should be at eof 00146 assert(myFileTypePtr->eof() != 0); 00147 assert(ifeof() != 0); 00148 00149 ifclose(); 00150 00151 std::cout << " Passed test_readFromFile" << std::endl; 00152 } 00153 00154 00155 00156 00157 void IFILE_Test::test_readTilChar(const char* extension) 00158 { 00159 // First open the test file. 00160 openFile(extension); 00161 00162 // Verify the file successfully opened. 00163 assert(myFileTypePtr != NULL); 00164 assert(isOpen()); 00165 assert(myFileTypePtr->isOpen()); 00166 00167 // Track position of ending char found. 00168 int pos = 0; 00169 00170 // Test readTilChar. 00171 std::string output = ""; 00172 std::string endChars = "a5d"; 00173 pos = readTilChar(endChars, output); 00174 assert(pos == 0); // read til a 00175 assert(output == "ABCD"); 00176 output.clear(); 00177 pos = readTilChar(endChars, output); 00178 assert(pos == 2); // read til d 00179 assert(output == "bc"); 00180 pos = readTilChar(endChars, output); 00181 assert(pos == 1); // read til 5 00182 assert(output == "bc1234\nEFGefg"); 00183 output.clear(); 00184 pos = readTilChar(endChars, output); 00185 assert(pos == -1); // read til 5 00186 assert(output == "67\nhijklHIJKL8910"); 00187 00188 ifrewind(); 00189 // Test readTilChar. 00190 pos = readTilChar(endChars); 00191 assert(pos == 0); // read til a 00192 pos = readTilChar(endChars); 00193 assert(pos == 2); // read til d 00194 pos = readTilChar(endChars); 00195 assert(pos == 1); // read til 5 00196 pos = readTilChar(endChars); 00197 assert(pos == -1); // read til 5 00198 00199 ifclose(); 00200 00201 std::cout << " Passed test_readTilChar" << std::endl; 00202 } 00203 00204 00205 void IFILE_Test::test_ifeof_ifrewind(const char* extension) 00206 { 00207 // First open the test file. 00208 openFile(extension); 00209 00210 // Verify the file successfully opened. 00211 assert(myFileTypePtr != NULL); 00212 assert(isOpen()); 00213 assert(myFileTypePtr->isOpen()); 00214 00215 // Not at eof - verify that it reports not eof. 00216 assert(ifeof() == false); 00217 00218 // Track the total number of the bytes that have been read from the file 00219 // at any given point. 00220 int totalBytesPreviouslyRead = 0; 00221 int numBytesRead = 0; 00222 00223 ////////////////////////////////////////////////////////////// 00224 // Test doing reads from file without IFILE internal buffering. 00225 disableBuffering(); 00226 00227 // Verify position in file. 00228 assert(iftell() == 0); 00229 00230 // Read a character from the file. 00231 numBytesRead = readFromFile(myTestBuffer, 1); 00232 assert(numBytesRead == 1); 00233 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00234 // Now that we have tested based on the previous total bytes read, 00235 // increment the count. 00236 totalBytesPreviouslyRead += numBytesRead; 00237 // Not at eof 00238 assert(ifeof() == false); 00239 00240 // Perform char read. 00241 char readChar = ifgetc(); 00242 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00243 // Now that we have tested based on the previous total bytes read, 00244 // increment the count. 00245 ++totalBytesPreviouslyRead; 00246 // Not at eof 00247 assert(ifeof() == false); 00248 assert(iftell() == totalBytesPreviouslyRead); 00249 00250 // Now read the rest. 00251 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00252 assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead)); 00253 // Hit the end of the file before reading the entire requested size. 00254 assert(numBytesRead != MAX_TEST_BUFFER_SIZE); 00255 // Now that we have tested based on the previous total bytes read, 00256 // increment the count. 00257 totalBytesPreviouslyRead += numBytesRead; 00258 00259 assert(myFileTypePtr->eof() != 0); 00260 assert(ifeof() != 0); 00261 00262 numBytesRead = readFromFile(myTestBuffer, 1); 00263 assert(numBytesRead == 0); 00264 // Now it registers eof 00265 assert(ifeof() != 0); 00266 00267 // bgzf files use a specialized return value for iftell that 00268 // is not just straight file offset. 00269 if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0)) 00270 { 00271 assert(iftell() == (BGZF_TEST_FILE_SIZE << 16)); 00272 } 00273 else 00274 { 00275 assert(iftell() == TEST_FILE_SIZE); 00276 } 00277 00278 /////////////////////////////////// 00279 // Test doing IFILE buffered reads. 00280 // rewind the file and verify that it no longer registers eof. 00281 ifrewind(); 00282 totalBytesPreviouslyRead = 0; 00283 // No longer at eof 00284 assert(ifeof() == false); 00285 // Verify position in file. 00286 assert(iftell() == 0); 00287 00288 // Buffer reads - may have been disabled for iftell to work for bgzf. 00289 bufferReads(); 00290 00291 // Read a character from the file. 00292 numBytesRead = readFromFile(myTestBuffer, 1); 00293 assert(numBytesRead == 1); 00294 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00295 // Now that we have tested based on the previous total bytes read, 00296 // increment the count. 00297 totalBytesPreviouslyRead += numBytesRead; 00298 // Not at eof 00299 assert(ifeof() == false); 00300 00301 // Perform char read. 00302 readChar = ifgetc(); 00303 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00304 // Now that we have tested based on the previous total bytes read, 00305 // increment the count. 00306 ++totalBytesPreviouslyRead; 00307 // Not at eof 00308 assert(ifeof() == false); 00309 00310 // bgzf files use a specialized return value for iftell that 00311 // is not just straight file offset. 00312 if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0)) 00313 { 00314 bool caught = false; 00315 try 00316 { 00317 assert(iftell() == totalBytesPreviouslyRead); 00318 } 00319 catch (std::exception& e) 00320 { 00321 caught = true; 00322 assert(strcmp(e.what(), "IFILE: CANNOT use buffered reads and tell for BGZF files") == 0); 00323 } 00324 assert(caught); 00325 } 00326 else 00327 { 00328 assert(iftell() == totalBytesPreviouslyRead); 00329 } 00330 00331 // Now read the rest. 00332 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00333 assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead)); 00334 // Now that we have tested based on the previous total bytes read, 00335 // increment the count. 00336 totalBytesPreviouslyRead += numBytesRead; 00337 // Registers eof. 00338 assert(ifeof() != 0); 00339 00340 // Read past eof. 00341 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00342 assert(numBytesRead == 0); 00343 // Eof. 00344 assert(ifeof() != 0); 00345 00346 // bgzf files use a specialized return value for iftell that 00347 // is not just straight file offset. 00348 if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0)) 00349 { 00350 bool caught = false; 00351 try 00352 { 00353 assert(iftell() == (BGZF_TEST_FILE_SIZE << 16)); 00354 } 00355 catch (std::exception& e) 00356 { 00357 caught = true; 00358 assert(strcmp(e.what(), "IFILE: CANNOT use buffered reads and tell for BGZF files") == 0); 00359 } 00360 assert(caught); 00361 disableBuffering(); 00362 assert(iftell() == (BGZF_TEST_FILE_SIZE << 16)); 00363 } 00364 else 00365 { 00366 assert(iftell() == TEST_FILE_SIZE); 00367 } 00368 00369 // Verify that after rewind, eof is no longer registered. 00370 ifrewind(); 00371 // reset since we are back to the beginning of the file. 00372 totalBytesPreviouslyRead = 0; 00373 // No longer at eof 00374 assert(ifeof() == false); 00375 // Verify position in file. 00376 assert(iftell() == 0); 00377 00378 // Verify properly works even if already at the beginning. 00379 ifrewind(); 00380 // reset since we are back to the beginning of the file. 00381 totalBytesPreviouslyRead = 0; 00382 // Not eof 00383 assert(ifeof() == false); 00384 // Verify position in file. 00385 assert(iftell() == 0); 00386 00387 // Buffer reads - may have been disabled for iftell to work for bgzf. 00388 bufferReads(); 00389 00390 ////////////////////// 00391 // Close the test file. 00392 ifclose(); 00393 00394 std::cout << " Passed test_ifeof_ifrewind" << std::endl; 00395 } 00396 00397 00398 void IFILE_Test::test_ifread_ifgetc(const char* extension) 00399 { 00400 // First open the test file. 00401 openFile(extension); 00402 00403 // Verify the file successfully opened. 00404 assert(myFileTypePtr != NULL); 00405 assert(isOpen()); 00406 assert(myFileTypePtr->isOpen()); 00407 00408 int numBytesRead = 0; 00409 int totalBytesPreviouslyRead = 0; 00410 00411 //////////////////////////////////// 00412 // Test reading entire file at once. 00413 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00414 assert(numBytesRead == TEST_FILE_SIZE); 00415 00416 for(int i = 0; i < TEST_FILE_SIZE; i++) 00417 { 00418 assert(myTestBuffer[i] == TEST_FILE_CONTENTS[i]); 00419 } 00420 totalBytesPreviouslyRead += numBytesRead; 00421 00422 // Should affect the IFILE buffer 00423 assert(myCurrentBufferSize == TEST_FILE_SIZE); 00424 assert(myBufferIndex == TEST_FILE_SIZE); 00425 00426 assert(myFileTypePtr->eof() != 0); 00427 assert(ifeof() != 0); 00428 00429 // Try reading at end of file twice. 00430 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00431 assert(numBytesRead == 0); 00432 // Should affect the IFILE buffer 00433 assert(myCurrentBufferSize == 0); 00434 assert(myBufferIndex == 0); 00435 assert(ifeof() != 0); 00436 00437 // 2nd read attempt at eof. 00438 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00439 assert(numBytesRead == 0); 00440 // Should affect the IFILE buffer 00441 assert(myCurrentBufferSize == 0); 00442 assert(myBufferIndex == 0); 00443 assert(ifeof() != 0); 00444 00445 00446 // RESET 00447 ifrewind(); 00448 totalBytesPreviouslyRead = 0; 00449 00450 ////////////////////////////////////////// 00451 // Test reading entire file using getc. 00452 // Loop through reading the file. 00453 char readChar; 00454 for(int index = 0; index < TEST_FILE_SIZE; index++) 00455 { 00456 // Read a character. 00457 readChar = ifgetc(); 00458 assert(readChar == TEST_FILE_CONTENTS[index]); 00459 // Should affect the IFILE buffer 00460 assert(myCurrentBufferSize == TEST_FILE_SIZE); 00461 assert(myBufferIndex == index+1); 00462 } 00463 00464 // Now that we have read the file, try reading again at eof. 00465 readChar = ifgetc(); 00466 assert(readChar == EOF); 00467 assert(myCurrentBufferSize == 0); 00468 assert(myBufferIndex == 0); 00469 00470 // Try again at eof. 00471 // Now that we have read the file, try reading again at eof. 00472 readChar = ifgetc(); 00473 assert(readChar == EOF); 00474 assert(myCurrentBufferSize == 0); 00475 assert(myBufferIndex == 0); 00476 00477 // RESET 00478 ifrewind(); 00479 totalBytesPreviouslyRead = 0; 00480 00481 //////////////////////////////////////////////// 00482 // Test reading just the beginning of the file. 00483 numBytesRead = ifread(myTestBuffer, 4); 00484 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]); 00485 assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]); 00486 assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]); 00487 assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]); 00488 assert(numBytesRead == 4); 00489 totalBytesPreviouslyRead += numBytesRead; 00490 // This read should have affected the internal buffer. 00491 assert(myCurrentBufferSize == TEST_FILE_SIZE); 00492 assert(myBufferIndex == 4); 00493 // Should not be at eof 00494 assert(ifeof() == false); 00495 00496 // Test reading rest of file. 00497 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00498 assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead)); 00499 // Verify contents of what read. 00500 for(int i = 0; i < numBytesRead; i++) 00501 { 00502 assert(myTestBuffer[i] == 00503 TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]); 00504 } 00505 totalBytesPreviouslyRead += numBytesRead; 00506 00507 // Try at end of file twice. 00508 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00509 assert(numBytesRead == 0); 00510 // Should affect the IFILE buffer 00511 assert(myCurrentBufferSize == 0); 00512 assert(myBufferIndex == 0); 00513 assert(ifeof() != 0); 00514 00515 // 2nd read attempt at eof. 00516 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00517 assert(numBytesRead == 0); 00518 // Should affect the IFILE buffer 00519 assert(myCurrentBufferSize == 0); 00520 assert(myBufferIndex == 0); 00521 assert(ifeof() != 0); 00522 00523 // RESET 00524 ifrewind(); 00525 totalBytesPreviouslyRead = 0; 00526 00527 ////////////////////////////////////// 00528 // Test reading just the beginning. 00529 numBytesRead = ifread(myTestBuffer, 4); 00530 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]); 00531 assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]); 00532 assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]); 00533 assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]); 00534 assert(numBytesRead == 4); 00535 totalBytesPreviouslyRead += numBytesRead; 00536 // This read should have affected the internal buffer. 00537 assert(myCurrentBufferSize == TEST_FILE_SIZE); 00538 assert(myBufferIndex == 4); 00539 // Should not be at eof 00540 assert(ifeof() == false); 00541 00542 // Test doing 2 getc. 00543 readChar = ifgetc(); 00544 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00545 int bufferSize = TEST_FILE_SIZE; 00546 assert(myCurrentBufferSize == bufferSize); 00547 assert(myBufferIndex == 5); 00548 totalBytesPreviouslyRead++; 00549 00550 readChar = ifgetc(); 00551 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00552 assert(myCurrentBufferSize == bufferSize); 00553 assert(myBufferIndex == 6); 00554 totalBytesPreviouslyRead++; 00555 00556 // Test reading rest of file. 00557 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00558 assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead)); 00559 // Verify contents of what read. 00560 for(int i = 0; i < numBytesRead; i++) 00561 { 00562 assert(myTestBuffer[i] == 00563 TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]); 00564 } 00565 totalBytesPreviouslyRead += numBytesRead; 00566 00567 // Try at end of file twice. 00568 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00569 assert(numBytesRead == 0); 00570 // Should affect the IFILE buffer 00571 assert(myCurrentBufferSize == 0); 00572 assert(myBufferIndex == 0); 00573 assert(ifeof() != 0); 00574 00575 // 2nd read attempt at eof. 00576 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00577 assert(numBytesRead == 0); 00578 // Should affect the IFILE buffer 00579 assert(myCurrentBufferSize == 0); 00580 assert(myBufferIndex == 0); 00581 assert(ifeof() != 0); 00582 00583 // RESET 00584 ifrewind(); 00585 totalBytesPreviouslyRead = 0; 00586 assert(myCurrentBufferSize == 0); 00587 assert(myBufferIndex == 0); 00588 00589 ////////////////////////////////// 00590 // Start with 2 getc. 00591 readChar = ifgetc(); 00592 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00593 bufferSize = TEST_FILE_SIZE; 00594 assert(myCurrentBufferSize == bufferSize); 00595 assert(myBufferIndex == 1); 00596 totalBytesPreviouslyRead++; 00597 00598 readChar = ifgetc(); 00599 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00600 assert(myCurrentBufferSize == bufferSize); 00601 assert(myBufferIndex == 2); 00602 totalBytesPreviouslyRead++; 00603 00604 // Test reading part of the rest of the file. 00605 numBytesRead = ifread(myTestBuffer, 4); 00606 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00607 assert(myTestBuffer[1] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 1]); 00608 assert(myTestBuffer[2] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 2]); 00609 assert(myTestBuffer[3] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 3]); 00610 assert(numBytesRead == 4); 00611 totalBytesPreviouslyRead += numBytesRead; 00612 // This read should have affected the internal buffer. 00613 assert(myCurrentBufferSize == bufferSize); 00614 assert(myBufferIndex == totalBytesPreviouslyRead); 00615 // Should not be at eof 00616 assert(ifeof() == false); 00617 00618 // Test reading 2 char with getc. 00619 readChar = ifgetc(); 00620 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00621 assert(myCurrentBufferSize == bufferSize); 00622 totalBytesPreviouslyRead++; 00623 assert(myBufferIndex == totalBytesPreviouslyRead); 00624 00625 readChar = ifgetc(); 00626 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]); 00627 assert(myCurrentBufferSize == bufferSize); 00628 totalBytesPreviouslyRead++; 00629 assert(myBufferIndex == totalBytesPreviouslyRead); 00630 00631 // Test reading rest of file. 00632 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00633 assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead)); 00634 // Verify contents of what read. 00635 for(int i = 0; i < numBytesRead; i++) 00636 { 00637 assert(myTestBuffer[i] == 00638 TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]); 00639 } 00640 totalBytesPreviouslyRead += numBytesRead; 00641 assert(myBufferIndex == 0); 00642 assert(myCurrentBufferSize == 0); 00643 00644 // Try at end of file twice. 00645 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00646 assert(numBytesRead == 0); 00647 // Should affect the IFILE buffer 00648 assert(myCurrentBufferSize == 0); 00649 assert(myBufferIndex == 0); 00650 assert(ifeof() != 0); 00651 00652 // 2nd read attempt at eof. 00653 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE); 00654 assert(numBytesRead == 0); 00655 // Should affect the IFILE buffer 00656 assert(myCurrentBufferSize == 0); 00657 assert(myBufferIndex == 0); 00658 assert(ifeof() != 0); 00659 00660 // RESET 00661 ifrewind(); 00662 totalBytesPreviouslyRead = 0; 00663 assert(myCurrentBufferSize == 0); 00664 assert(myBufferIndex == 0); 00665 00666 ////////////// 00667 // Close the test file. 00668 ifclose(); 00669 00670 //////////////////////////////////////////////////////////////////////// 00671 // Repeat the test on a test file that is larger than the IFILE 00672 // buffer size. 00673 00674 // First open the test file. 00675 openLargeFile(extension); 00676 00677 // This file contains DEFAULT_BUFFER_SIZE of '0's followed by "12345" 00678 // The size of the file is DEFAULT_BUFFER_SIZE + 5. 00679 int largeTestFileSize = DEFAULT_BUFFER_SIZE + 5; 00680 char largeBuffer[largeTestFileSize + 5]; 00681 00682 // Verify the file successfully opened. 00683 assert(myFileTypePtr != NULL); 00684 assert(isOpen()); 00685 assert(myFileTypePtr->isOpen()); 00686 00687 numBytesRead = 0; 00688 totalBytesPreviouslyRead = 0; 00689 00690 //////////////////////////////////// 00691 // Test reading part of the file, then more then the buffer size, 00692 // then the rest of the file (test buffer handling when read 00693 // available and directly into the file, then read more). 00694 numBytesRead = ifread(largeBuffer, 2); 00695 assert(numBytesRead == 2); 00696 numBytesRead = ifread(largeBuffer + 2, DEFAULT_BUFFER_SIZE * 3); 00697 assert(numBytesRead == DEFAULT_BUFFER_SIZE + 3); 00698 // Should be at the end of the file. 00699 assert(myFileTypePtr->eof() != 0); 00700 assert(ifeof() != 0); 00701 numBytesRead = ifread(largeBuffer + DEFAULT_BUFFER_SIZE + 3, 2); 00702 assert(numBytesRead == 0); 00703 00704 // Validate all the 0s 00705 for(unsigned int i = 0; i < DEFAULT_BUFFER_SIZE; i++) 00706 { 00707 assert(largeBuffer[i] == '0'); 00708 } 00709 // Now validate the "12345" 00710 assert(largeBuffer[DEFAULT_BUFFER_SIZE] == '1'); 00711 assert(largeBuffer[DEFAULT_BUFFER_SIZE+1] == '2'); 00712 assert(largeBuffer[DEFAULT_BUFFER_SIZE+2] == '3'); 00713 assert(largeBuffer[DEFAULT_BUFFER_SIZE+3] == '4'); 00714 assert(largeBuffer[DEFAULT_BUFFER_SIZE+4] == '5'); 00715 00716 totalBytesPreviouslyRead += numBytesRead; 00717 00718 // Should affect the IFILE buffer - 0 because read 00719 // is bigger than the buffer, so just read directly 00720 // into the largeBuffer. 00721 assert(myCurrentBufferSize == 0); 00722 assert(myBufferIndex == 0); 00723 00724 assert(myFileTypePtr->eof() != 0); 00725 assert(ifeof() != 0); 00726 00727 // Try reading at end of file twice. 00728 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00729 assert(numBytesRead == 0); 00730 // Should affect the IFILE buffer 00731 assert(myCurrentBufferSize == 0); 00732 assert(myBufferIndex == 0); 00733 assert(ifeof() != 0); 00734 00735 // 2nd read attempt at eof. 00736 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00737 assert(numBytesRead == 0); 00738 // Should affect the IFILE buffer 00739 assert(myCurrentBufferSize == 0); 00740 assert(myBufferIndex == 0); 00741 assert(ifeof() != 0); 00742 00743 00744 // RESET 00745 ifrewind(); 00746 totalBytesPreviouslyRead = 0; 00747 00748 //////////////////////////////////// 00749 // Test reading entire file at once. 00750 numBytesRead = ifread(largeBuffer, largeTestFileSize + 4); 00751 assert(numBytesRead == largeTestFileSize); 00752 00753 // Validate all the 0s 00754 for(unsigned int i = 0; i < DEFAULT_BUFFER_SIZE; i++) 00755 { 00756 assert(largeBuffer[i] == '0'); 00757 } 00758 // Now validate the "12345" 00759 assert(largeBuffer[DEFAULT_BUFFER_SIZE] == '1'); 00760 assert(largeBuffer[DEFAULT_BUFFER_SIZE+1] == '2'); 00761 assert(largeBuffer[DEFAULT_BUFFER_SIZE+2] == '3'); 00762 assert(largeBuffer[DEFAULT_BUFFER_SIZE+3] == '4'); 00763 assert(largeBuffer[DEFAULT_BUFFER_SIZE+4] == '5'); 00764 00765 totalBytesPreviouslyRead += numBytesRead; 00766 00767 // Should affect the IFILE buffer - 0 because read 00768 // is bigger than the buffer, so just read directly 00769 // into the largeBuffer. 00770 assert(myCurrentBufferSize == 0); 00771 assert(myBufferIndex == 0); 00772 00773 assert(myFileTypePtr->eof() != 0); 00774 assert(ifeof() != 0); 00775 00776 // Try reading at end of file twice. 00777 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00778 assert(numBytesRead == 0); 00779 // Should affect the IFILE buffer 00780 assert(myCurrentBufferSize == 0); 00781 assert(myBufferIndex == 0); 00782 assert(ifeof() != 0); 00783 00784 // 2nd read attempt at eof. 00785 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00786 assert(numBytesRead == 0); 00787 // Should affect the IFILE buffer 00788 assert(myCurrentBufferSize == 0); 00789 assert(myBufferIndex == 0); 00790 assert(ifeof() != 0); 00791 00792 00793 // RESET 00794 ifrewind(); 00795 totalBytesPreviouslyRead = 0; 00796 00797 ////////////////////////////////////////// 00798 // Test reading entire file using getc. 00799 // Loop through reading the file. 00800 // First loop through verifying the 0's 00801 for(int index = 0; index < (int)DEFAULT_BUFFER_SIZE; index++) 00802 { 00803 // Read a character. 00804 readChar = ifgetc(); 00805 assert(readChar == '0'); 00806 // Should affect the IFILE buffer 00807 assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE); 00808 assert(myBufferIndex == index+1); 00809 } 00810 // Now read the 12345. 00811 readChar = ifgetc(); 00812 assert(readChar == '1'); 00813 // Should affect the IFILE buffer 00814 assert(myCurrentBufferSize == 5); 00815 assert(myBufferIndex == 1); 00816 readChar = ifgetc(); 00817 assert(readChar == '2'); 00818 // Should affect the IFILE buffer 00819 assert(myCurrentBufferSize == 5); 00820 assert(myBufferIndex == 2); 00821 readChar = ifgetc(); 00822 assert(readChar == '3'); 00823 // Should affect the IFILE buffer 00824 assert(myCurrentBufferSize == 5); 00825 assert(myBufferIndex == 3); 00826 readChar = ifgetc(); 00827 assert(readChar == '4'); 00828 // Should affect the IFILE buffer 00829 assert(myCurrentBufferSize == 5); 00830 assert(myBufferIndex == 4); 00831 readChar = ifgetc(); 00832 assert(readChar == '5'); 00833 // Should affect the IFILE buffer 00834 assert(myCurrentBufferSize == 5); 00835 assert(myBufferIndex == 5); 00836 00837 // Now that we have read the file, try reading again at eof. 00838 readChar = ifgetc(); 00839 assert(readChar == EOF); 00840 assert(myCurrentBufferSize == 0); 00841 assert(myBufferIndex == 0); 00842 00843 // Try again at eof. 00844 // Now that we have read the file, try reading again at eof. 00845 readChar = ifgetc(); 00846 assert(readChar == EOF); 00847 assert(myCurrentBufferSize == 0); 00848 assert(myBufferIndex == 0); 00849 00850 // RESET 00851 ifrewind(); 00852 totalBytesPreviouslyRead = 0; 00853 00854 //////////////////////////////////////////////// 00855 // Test reading just the beginning of the file. 00856 numBytesRead = ifread(largeBuffer, 4); 00857 assert(largeBuffer[0] == '0'); 00858 assert(largeBuffer[1] == '0'); 00859 assert(largeBuffer[2] == '0'); 00860 assert(largeBuffer[3] == '0'); 00861 assert(numBytesRead == 4); 00862 totalBytesPreviouslyRead += numBytesRead; 00863 // This read should have affected the internal buffer. 00864 assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE); 00865 assert(myBufferIndex == 4); 00866 // Should not be at eof 00867 assert(ifeof() == false); 00868 00869 // Test reading rest of file. 00870 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00871 assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead)); 00872 // Verify contents of what read. First check the 0's 00873 for(int i = 0; i < (numBytesRead-5); i++) 00874 { 00875 assert(largeBuffer[i] == '0'); 00876 } 00877 // Check the 12345 00878 assert(largeBuffer[numBytesRead - 5] == '1'); 00879 assert(largeBuffer[numBytesRead - 5 + 1] == '2'); 00880 assert(largeBuffer[numBytesRead - 5 + 2] == '3'); 00881 assert(largeBuffer[numBytesRead - 5 + 3] == '4'); 00882 assert(largeBuffer[numBytesRead - 5 + 4] == '5'); 00883 totalBytesPreviouslyRead += numBytesRead; 00884 00885 // Try at end of file twice. 00886 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00887 assert(numBytesRead == 0); 00888 // Trying to read at the end cleared the buffer.. 00889 assert(myCurrentBufferSize == 0); 00890 assert(myBufferIndex == 0); 00891 assert(ifeof() != 0); 00892 00893 // 2nd read attempt at eof. 00894 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00895 assert(numBytesRead == 0); 00896 // Should affect the IFILE buffer 00897 assert(myCurrentBufferSize == 0); 00898 assert(myBufferIndex == 0); 00899 assert(ifeof() != 0); 00900 00901 // RESET 00902 ifrewind(); 00903 totalBytesPreviouslyRead = 0; 00904 00905 ////////////////////////////////////// 00906 // Test reading just the beginning. 00907 numBytesRead = ifread(largeBuffer, 2); 00908 assert(largeBuffer[0] == '0'); 00909 assert(largeBuffer[1] == '0'); 00910 assert(numBytesRead == 2); 00911 totalBytesPreviouslyRead += numBytesRead; 00912 // This read should have affected the internal buffer. 00913 assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE); 00914 assert(myBufferIndex == 2); 00915 // Should not be at eof 00916 assert(ifeof() == false); 00917 00918 // Test doing 2 getc. 00919 readChar = ifgetc(); 00920 assert(readChar == '0'); 00921 bufferSize = DEFAULT_BUFFER_SIZE; 00922 assert(myCurrentBufferSize == bufferSize); 00923 assert(myBufferIndex == 3); 00924 totalBytesPreviouslyRead++; 00925 00926 readChar = ifgetc(); 00927 assert(readChar == '0'); 00928 assert(myCurrentBufferSize == bufferSize); 00929 assert(myBufferIndex == 4); 00930 totalBytesPreviouslyRead++; 00931 00932 // Test reading rest of file. 00933 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00934 assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead)); 00935 // Verify contents of what read. 00936 // All except the last 5 should be '0' 00937 for(int i = 0; i < numBytesRead - 5; i++) 00938 { 00939 assert(largeBuffer[i] == '0'); 00940 } 00941 assert(largeBuffer[numBytesRead - 5] == '1'); 00942 assert(largeBuffer[numBytesRead - 4] == '2'); 00943 assert(largeBuffer[numBytesRead - 3] == '3'); 00944 assert(largeBuffer[numBytesRead - 2] == '4'); 00945 assert(largeBuffer[numBytesRead - 1] == '5'); 00946 00947 totalBytesPreviouslyRead += numBytesRead; 00948 00949 // Try at end of file twice. 00950 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00951 assert(numBytesRead == 0); 00952 // Reading at the end clears the buffer 00953 assert(myCurrentBufferSize == 0); 00954 assert(myBufferIndex == 0); 00955 assert(ifeof() != 0); 00956 00957 // 2nd read attempt at eof. 00958 numBytesRead = ifread(largeBuffer, largeTestFileSize); 00959 assert(numBytesRead == 0); 00960 // Reading at the end clears the buffer 00961 assert(myCurrentBufferSize == 0); 00962 assert(myBufferIndex == 0); 00963 assert(ifeof() != 0); 00964 00965 // RESET 00966 ifrewind(); 00967 totalBytesPreviouslyRead = 0; 00968 assert(myCurrentBufferSize == 0); 00969 assert(myBufferIndex == 0); 00970 00971 ////////////////////////////////// 00972 // Start with 2 getc. 00973 readChar = ifgetc(); 00974 assert(readChar == '0'); 00975 bufferSize = DEFAULT_BUFFER_SIZE; 00976 assert(myCurrentBufferSize == bufferSize); 00977 assert(myBufferIndex == 1); 00978 totalBytesPreviouslyRead++; 00979 00980 readChar = ifgetc(); 00981 assert(readChar == '0'); 00982 assert(myCurrentBufferSize == bufferSize); 00983 assert(myBufferIndex == 2); 00984 totalBytesPreviouslyRead++; 00985 00986 // Test reading part of the rest of the file. 00987 numBytesRead = ifread(myTestBuffer, 2); 00988 assert(myTestBuffer[0] == '0'); 00989 assert(myTestBuffer[1] == '0'); 00990 assert(numBytesRead == 2); 00991 totalBytesPreviouslyRead += numBytesRead; 00992 // This read should have affected the internal buffer. 00993 assert(myCurrentBufferSize == bufferSize); 00994 assert(myBufferIndex == totalBytesPreviouslyRead); 00995 // Should not be at eof 00996 assert(ifeof() == false); 00997 00998 // Test reading 2 char with getc. 00999 readChar = ifgetc(); 01000 assert(readChar == '0'); 01001 assert(myCurrentBufferSize == bufferSize); 01002 totalBytesPreviouslyRead++; 01003 assert(myBufferIndex == totalBytesPreviouslyRead); 01004 01005 readChar = ifgetc(); 01006 assert(readChar == '0'); 01007 assert(myCurrentBufferSize == bufferSize); 01008 totalBytesPreviouslyRead++; 01009 assert(myBufferIndex == totalBytesPreviouslyRead); 01010 01011 // Test reading rest of file. 01012 numBytesRead = ifread(largeBuffer, largeTestFileSize); 01013 assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead)); 01014 // Verify contents of what read. 01015 for(int i = 0; i < numBytesRead - 5; i++) 01016 { 01017 assert(largeBuffer[i] == '0'); 01018 } 01019 // Verify the 12345 01020 assert(largeBuffer[numBytesRead - 5] == '1'); 01021 assert(largeBuffer[numBytesRead - 5 + 1] == '2'); 01022 assert(largeBuffer[numBytesRead - 5 + 2] == '3'); 01023 assert(largeBuffer[numBytesRead - 5 + 3] == '4'); 01024 assert(largeBuffer[numBytesRead - 5 + 4] == '5'); 01025 totalBytesPreviouslyRead += numBytesRead; 01026 bufferSize = 5; 01027 assert(myBufferIndex == bufferSize); 01028 assert(myCurrentBufferSize == bufferSize); 01029 01030 // Try at end of file twice. 01031 numBytesRead = ifread(largeBuffer, largeTestFileSize); 01032 assert(numBytesRead == 0); 01033 // Reading at the end clears the buffer 01034 assert(myCurrentBufferSize == 0); 01035 assert(myBufferIndex == 0); 01036 assert(ifeof() != 0); 01037 01038 // 2nd read attempt at eof. 01039 numBytesRead = ifread(largeBuffer, largeTestFileSize); 01040 assert(numBytesRead == 0); 01041 // Reading at the end clears the buffer 01042 assert(myCurrentBufferSize == 0); 01043 assert(myBufferIndex == 0); 01044 assert(ifeof() != 0); 01045 01046 // RESET 01047 ifrewind(); 01048 totalBytesPreviouslyRead = 0; 01049 assert(myCurrentBufferSize == 0); 01050 assert(myBufferIndex == 0); 01051 01052 ifclose(); 01053 01054 std::cout << " Passed test_ifread_ifgetc" << std::endl; 01055 } 01056 01057 01058 // Test closing a file. 01059 void IFILE_Test::test_ifclose(const char* extension) 01060 { 01061 // First open the test file. 01062 openFile(extension); 01063 01064 // Verify the file successfully opened. 01065 assert(myFileTypePtr != NULL); 01066 assert(isOpen()); 01067 assert(myFileTypePtr->isOpen()); 01068 01069 ifclose(); 01070 01071 assert(myFileTypePtr == NULL); 01072 assert(isOpen() == false); 01073 01074 std::cout << " Passed test_ifclose" << std::endl; 01075 } 01076 01077 01078 void IFILE_Test::test_ifseek(const char* extension) 01079 { 01080 disableBuffering(); 01081 // First open the test file. 01082 openFile(extension); 01083 01084 // Read a character from the file. 01085 int numBytesRead = readFromFile(myTestBuffer, 1); 01086 assert(numBytesRead == 1); 01087 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]); 01088 01089 // Get the current position. 01090 long int currentPos = iftell(); 01091 01092 // Read the next character from the file. 01093 numBytesRead = readFromFile(myTestBuffer, 1); 01094 assert(numBytesRead == 1); 01095 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]); 01096 01097 // Seek to just before the character that was just read and read again 01098 // Should be the same character. 01099 assert(ifseek(currentPos, SEEK_SET) == true); 01100 numBytesRead = readFromFile(myTestBuffer, 1); 01101 assert(numBytesRead == 1); 01102 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]); 01103 01104 ifclose(); 01105 01106 assert(myFileTypePtr == NULL); 01107 assert(isOpen() == false); 01108 01109 // Buffer reads - may have been disabled for iftell to work for bgzf. 01110 bufferReads(); 01111 01112 std::cout << " Passed test_ifseek" << std::endl; 01113 } 01114 01115 void IFILE_Test::test_noExistRead(const char* extension) 01116 { 01117 openNoExistFile(extension); 01118 01119 } 01120 01121 01122 // Open a file for testing. 01123 void IFILE_Test::openFile(const char* extension) 01124 { 01125 std::string filename = "data/InputFileTest."; 01126 filename += extension; 01127 assert(InputFile::openFile(filename.c_str(), "rb", InputFile::DEFAULT) == true); 01128 } 01129 01130 // Open a file for testing. 01131 void IFILE_Test::openLargeFile(const char* extension) 01132 { 01133 std::string filename = "data/InputFileTestLarge."; 01134 filename += extension; 01135 assert(InputFile::openFile(filename.data(), "rb", InputFile::DEFAULT) == true); 01136 } 01137 01138 01139 void IFILE_Test::openNoExistFile(const char* extension) 01140 { 01141 std::string filename = "data/noExist."; 01142 filename += extension; 01143 assert(InputFile::openFile(filename.data(), "rb", InputFile::DEFAULT) == false); 01144 } 01145 01146 01147 void testWrite() 01148 { 01149 std::string filenameNoExt = "results/InputFileTest."; 01150 std::string filename = filenameNoExt + "glf"; 01151 01152 IFILE filePtr = ifopen(filename.c_str(), "wt"); 01153 assert(filePtr != NULL); 01154 01155 assert(ifwrite(filePtr, 01156 IFILE_Test::TEST_FILE_CONTENTS.c_str(), 01157 IFILE_Test::TEST_FILE_CONTENTS.length()) 01158 == IFILE_Test::TEST_FILE_CONTENTS.length()); 01159 01160 assert(ifclose(filePtr) == 0); 01161 01162 filename = "results/uncompressedFile.glf"; 01163 01164 filePtr = ifopen(filename.c_str(), "wt", InputFile::UNCOMPRESSED); 01165 assert(filePtr != NULL); 01166 01167 assert(ifwrite(filePtr, 01168 IFILE_Test::TEST_FILE_CONTENTS.c_str(), 01169 IFILE_Test::TEST_FILE_CONTENTS.length()) 01170 == IFILE_Test::TEST_FILE_CONTENTS.length()); 01171 01172 assert(ifclose(filePtr) == 0); 01173 01174 filename = "results/bgzfFile.glf"; 01175 01176 filePtr = ifopen(filename.c_str(), "wt", InputFile::BGZF); 01177 assert(filePtr != NULL); 01178 01179 assert(ifwrite(filePtr, 01180 IFILE_Test::TEST_FILE_CONTENTS.c_str(), 01181 IFILE_Test::TEST_FILE_CONTENTS.length()) 01182 == IFILE_Test::TEST_FILE_CONTENTS.length()); 01183 01184 assert(ifclose(filePtr) == 0); 01185 01186 filename = "results/gzipFile.glf"; 01187 01188 filePtr = ifopen(filename.c_str(), "wt", InputFile::GZIP); 01189 assert(filePtr != NULL); 01190 01191 assert(ifwrite(filePtr, 01192 IFILE_Test::TEST_FILE_CONTENTS.c_str(), 01193 IFILE_Test::TEST_FILE_CONTENTS.length()) 01194 ==IFILE_Test:: TEST_FILE_CONTENTS.length()); 01195 01196 assert(ifclose(filePtr) == 0); 01197 01198 filename = "results/defaultFile.glf"; 01199 01200 filePtr = ifopen(filename.c_str(), "wt"); 01201 assert(filePtr != NULL); 01202 01203 assert(ifwrite(filePtr, 01204 IFILE_Test::TEST_FILE_CONTENTS.c_str(), 01205 IFILE_Test::TEST_FILE_CONTENTS.length()) 01206 == IFILE_Test::TEST_FILE_CONTENTS.length()); 01207 01208 assert(ifclose(filePtr) == 0); 01209 01210 filename = "results/defaultFile.gz"; 01211 01212 filePtr = ifopen(filename.c_str(), "wt"); 01213 assert(filePtr != NULL); 01214 01215 assert(ifwrite(filePtr, 01216 IFILE_Test::TEST_FILE_CONTENTS.c_str(), 01217 IFILE_Test::TEST_FILE_CONTENTS.length()) 01218 == IFILE_Test::TEST_FILE_CONTENTS.length()); 01219 01220 assert(ifclose(filePtr) == 0); 01221 01222 01223 filename = "results/textFile.gz"; 01224 01225 unsigned int myuint = 99; 01226 int myint = -99; 01227 char mychar = 'z'; 01228 01229 filePtr = ifopen(filename.c_str(), "wt"); 01230 (*filePtr) << "Hello\n"; 01231 (*filePtr) << "Hello." << 3 << ' ' << -2 << "How are you"; 01232 (*filePtr) << "?" << "\n"; 01233 std::string mytext = "Bye\n"; 01234 (*filePtr) << mytext; 01235 (*filePtr) << 3.125 << mychar; 01236 (*filePtr) << myuint; 01237 (*filePtr) << mychar; 01238 (*filePtr) << myint; 01239 String myString = "Good Bye!\n"; 01240 (*filePtr) << myString; 01241 assert(ifclose(filePtr) == 0); 01242 01243 filename = "results/textFile1.gz"; 01244 InputFile& fileRef = *(ifopen(filename.c_str(), "wt")); 01245 fileRef << "Hello\n"; 01246 fileRef << "Hello." << 3 << ' ' << -2 << "How are you"; 01247 fileRef << "?" << "\n"; 01248 fileRef << mytext; 01249 fileRef << 3.125 << mychar; 01250 fileRef << myuint; 01251 fileRef << mychar; 01252 fileRef << myint; 01253 fileRef << myString; 01254 InputFile* fileRefPtr = &fileRef; 01255 assert(ifclose(fileRefPtr) == 0); 01256 assert(fileRefPtr == NULL); 01257 01258 // TODO - automatically verify that the files were written in the 01259 // correct format - rather than hand checking. 01260 } 01261 01262 01263 01264 void testAdditional(const char* extension) 01265 { 01266 std::string fileName = "data/InputFileTest2."; 01267 fileName += extension; 01268 IFILE testFile = ifopen(fileName.c_str(), "r"); 01269 assert(testFile != NULL); 01270 01271 std::string buffer = "989"; 01272 std::string stopChars = "C5F2"; 01273 01274 // Test readTilChar that stores the string. 01275 assert(testFile->readTilChar(stopChars, buffer) == 0); 01276 assert(buffer == "989AB"); 01277 buffer.clear(); 01278 assert(testFile->readTilChar(stopChars, buffer) == 2); 01279 assert(buffer == "DE"); 01280 assert(testFile->readTilChar(stopChars, buffer) == 3); 01281 assert(buffer == "DEG\tabcdefg\n1"); 01282 01283 // Test readTilChar that discards the string. 01284 assert(testFile->readTilChar(stopChars) == 1); 01285 buffer.clear(); 01286 buffer = "t"; 01287 assert(testFile->readTilTab(buffer) == 1); 01288 assert(buffer == "t6"); 01289 assert(testFile->readTilTab(buffer) == 0); 01290 assert(buffer == "t6hijklm"); 01291 assert(testFile->readTilTab(buffer) == 0); 01292 assert(buffer == "t6hijklm1"); 01293 assert(testFile->readTilTab(buffer) == 1); 01294 assert(buffer == "t6hijklm1NOP"); 01295 assert(testFile->readLine(buffer) == 0); 01296 assert(buffer == "t6hijklm1NOPQRST\tUVW"); 01297 assert(testFile->readTilTab(buffer) == 0); 01298 assert(buffer == "t6hijklm1NOPQRST\tUVW"); 01299 buffer.clear(); 01300 assert(testFile->discardLine() == 0); 01301 assert(testFile->readLine(buffer) == -1); 01302 assert(buffer == "@#$"); 01303 assert(testFile->discardLine() == -1); 01304 assert(testFile->readTilTab(buffer) == -1); 01305 assert(testFile->readTilChar(stopChars, buffer) == -1); 01306 assert(testFile->readTilChar(stopChars) == -1); 01307 assert(buffer == "@#$"); 01308 01309 ifclose(testFile); 01310 01311 }