InputFileTest.cpp

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 
00021 #ifdef __ZLIB_AVAILABLE__
00022 void testWrite();
00023 
00024 
00025 int main(int argc, char ** argv)
00026 {
00027 
00028    IFILE_Test myFile;
00029 
00030    myFile.test();
00031 
00032    testWrite();
00033 }
00034 
00035 
00036 const int IFILE_Test::TEST_FILE_SIZE = 37;
00037 const int IFILE_Test::BGZF_TEST_FILE_SIZE = 93;
00038 const std::string IFILE_Test::TEST_FILE_CONTENTS = "ABCDabcd1234\nEFGefg567\nhijklHIJKL8910";
00039 
00040 void IFILE_Test::test()
00041 {
00042    std::cout << "\nUncompressedFileType Tests:" << std::endl;
00043    test_readFromFile("txt");
00044    test_ifeof_ifrewind("txt");
00045    test_ifread_ifgetc("txt");
00046    test_ifclose("txt");
00047    test_ifseek("txt");
00048    test_noExistRead("txt");
00049 
00050    std::cout << "\nGzipFileType Tests:" << std::endl;
00051    test_readFromFile("gz");
00052    test_ifeof_ifrewind("gz");
00053    test_ifread_ifgetc("gz");
00054    test_ifclose("gz");
00055    test_ifseek("gz");
00056    test_noExistRead("gz");
00057 
00058    std::cout << "\nBgzfFileType Tests:" << std::endl;
00059    test_readFromFile("bam");
00060    test_ifeof_ifrewind("bam");
00061    test_ifread_ifgetc("bam");
00062    test_ifclose("bam");
00063    test_ifseek("bam");
00064    test_noExistRead("bam");
00065 
00066    std::cout << "\n .glf file Tests:" << std::endl;
00067    test_readFromFile("glf");
00068    test_ifeof_ifrewind("glf");
00069    test_ifread_ifgetc("glf");
00070    test_ifclose("glf");
00071    test_ifseek("glf");
00072    test_noExistRead("glf");
00073 }
00074 
00075 
00076 void IFILE_Test::test_readFromFile(const char* extension)
00077 {
00078    // First open the test file.
00079    openFile(extension);
00080 
00081    // Verify the file successfully opened.
00082    assert(myFileTypePtr != NULL);
00083    assert(isOpen());
00084    assert(myFileTypePtr->isOpen());
00085 
00086    // Track how many bytes are read by each call.
00087    int numBytesRead = 0;
00088 
00089    // Track the total number of the bytes that have been read from the file
00090    // at any given point.
00091    int totalBytesPreviouslyRead = 0;
00092 
00093    // Test readFromFile.
00094    numBytesRead = readFromFile(myTestBuffer, 4);
00095    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
00096    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]);
00097    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
00098    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
00099    assert(numBytesRead == 4);
00100    totalBytesPreviouslyRead += numBytesRead;
00101    // This read should not have affected the internal buffer.
00102    assert(myCurrentBufferSize == 0);
00103    assert(myBufferIndex == 0);
00104    // Should not be at eof
00105    assert(myFileTypePtr->eof() == false);
00106    assert(ifeof() == false);
00107 
00108    // Read again to verify that the next characters could be read.
00109    numBytesRead = readFromFile(myTestBuffer, 2);
00110    // Read 2 more characters from the test file.
00111    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[4]);
00112    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[5]);
00113    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
00114    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
00115    assert(numBytesRead == 2);
00116    totalBytesPreviouslyRead += numBytesRead;
00117    // This read should not have affected the internal buffer.
00118    assert(myCurrentBufferSize == 0);
00119    assert(myBufferIndex == 0);
00120    // Should not be at eof
00121    assert(myFileTypePtr->eof() == false);
00122    assert(ifeof() == false);
00123   
00124    // Read the rest of the file.
00125    // Determine expected results for reading the rest of the file by
00126    // taking the substring starting after what had been previously read.
00127    numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00128    // Read the rest of the file, so the number of bytes read is
00129    // what was left in the file.
00130    assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead));
00131    for(int i = 0; i < numBytesRead; i++)
00132    {
00133       assert(myTestBuffer[i] ==
00134          TEST_FILE_CONTENTS[totalBytesPreviouslyRead+i]);
00135    }
00136    totalBytesPreviouslyRead += numBytesRead;
00137    // Eof - slightly varies based on the type of file on whether or not the
00138    // next read is the one the current read or the next one shows eof.
00139    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00140    {
00141       assert(myFileTypePtr->eof() == false);
00142       assert(ifeof() == false);      
00143    }
00144    else
00145    {
00146       assert(myFileTypePtr->eof() == true);
00147       assert(ifeof() == true);
00148    }
00149 
00150    // Try to read one more time, making sure it doesn't read anything.
00151     numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00152     assert(numBytesRead == 0);
00153    // Should be at eof
00154    assert(myFileTypePtr->eof() == true);
00155    assert(ifeof() == true);
00156 
00157    ifclose();
00158 
00159    std::cout << "  Passed test_readFromFile" << std::endl;
00160 }
00161 
00162 
00163 void IFILE_Test::test_ifeof_ifrewind(const char* extension)
00164 {
00165    // First open the test file.
00166    openFile(extension);
00167    
00168    // Verify the file successfully opened.
00169    assert(myFileTypePtr != NULL);
00170    assert(isOpen());
00171    assert(myFileTypePtr->isOpen());
00172    
00173    // Not at eof - verify that it reports not eof.
00174    assert(ifeof() == false);
00175    
00176    // Track the total number of the bytes that have been read from the file
00177    // at any given point.
00178    int totalBytesPreviouslyRead = 0;
00179 
00180    //////////////////////////////////////////////////////////////
00181    // Test doing reads from file without IFILE internal buffering.
00182    // Read the entire file, then check for eof.
00183    int numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00184    assert(numBytesRead < MAX_TEST_BUFFER_SIZE);
00185 
00186    // Eof - slightly varies based on the type of file on whether or not the
00187    // next read is the one the current read or the next one shows eof.
00188    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00189    {
00190       assert(myFileTypePtr->eof() == false);
00191       assert(ifeof() == false);      
00192    }
00193    else
00194    {
00195       assert(myFileTypePtr->eof() == true);
00196       assert(ifeof() == true);
00197    }
00198    
00199    numBytesRead = readFromFile(myTestBuffer, 1);
00200    assert(numBytesRead == 0);
00201    // Now it registers eof
00202    assert(ifeof() == true);
00203 
00204    // bgzf files use a specialized return value for iftell that
00205    // is not just straight file offset.
00206    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00207    {
00208       assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
00209    }
00210    else
00211    {
00212       assert(iftell() == TEST_FILE_SIZE);
00213    }
00214 
00215    // rewind the file and verify that it no longer registers eof.
00216    ifrewind();
00217    totalBytesPreviouslyRead = 0;
00218    // No longer at eof
00219    assert(ifeof() == false);
00220    // Verify position in file.
00221    assert(iftell() == 0);
00222 
00223    // Read a character from the file.
00224    numBytesRead = readFromFile(myTestBuffer, 1);
00225    assert(numBytesRead == 1);
00226    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00227    // Now that we have tested based on the previous total bytes read, 
00228    // increment the count.
00229    totalBytesPreviouslyRead += numBytesRead;
00230    // Not at eof
00231    assert(ifeof() == false);
00232 
00233    ///////////////////////////////////
00234    // Test doing IFILE buffered reads.
00235    // Perform char read.
00236    char readChar = ifgetc();
00237    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00238    // Now that we have tested based on the previous total bytes read, 
00239    // increment the count.
00240    totalBytesPreviouslyRead += numBytesRead;
00241    // Not at eof
00242    assert(ifeof() == false);
00243    
00244    // Now read the rest.
00245    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00246    assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead));
00247    // Now that we have tested based on the previous total bytes read, 
00248    // increment the count.
00249    totalBytesPreviouslyRead += numBytesRead;
00250    // Registers eof.
00251    assert(ifeof() == true);
00252 
00253    // Read past eof.
00254    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00255    assert(numBytesRead == 0);
00256    // Eof.
00257    assert(ifeof() == true);
00258 
00259    // bgzf files use a specialized return value for iftell that
00260    // is not just straight file offset.
00261    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00262    {
00263       assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
00264    }
00265    else
00266    {
00267       assert(iftell() == TEST_FILE_SIZE);
00268    }
00269 
00270    // Verify that after rewind, eof is no longer registered.
00271    ifrewind();
00272   // reset since we are back to the beginning of the file.
00273    totalBytesPreviouslyRead = 0;
00274    // No longer at eof
00275    assert(ifeof() == false);
00276    // Verify position in file.
00277    assert(iftell() == 0);
00278 
00279    // Verify properly works even if already at the beginning.   
00280    ifrewind();
00281   // reset since we are back to the beginning of the file.
00282    totalBytesPreviouslyRead = 0;
00283    // Not eof
00284    assert(ifeof() == false);
00285    // Verify position in file.
00286    assert(iftell() == 0);
00287 
00288    //////////////////////
00289    // Close the test file.
00290    ifclose();
00291    
00292    std::cout << "  Passed test_ifeof_ifrewind" << std::endl;
00293 }
00294 
00295 
00296 void IFILE_Test::test_ifread_ifgetc(const char* extension)
00297 {
00298    // First open the test file.
00299    openFile(extension);
00300 
00301    // Verify the file successfully opened.
00302    assert(myFileTypePtr != NULL);
00303    assert(isOpen());
00304    assert(myFileTypePtr->isOpen());
00305 
00306    int numBytesRead = 0;
00307    int totalBytesPreviouslyRead = 0;
00308 
00309    ////////////////////////////////////
00310    // Test reading entire file at once.
00311    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00312    assert(numBytesRead == TEST_FILE_SIZE);
00313    
00314    for(int i = 0; i < TEST_FILE_SIZE; i++)
00315    {
00316       assert(myTestBuffer[i] == TEST_FILE_CONTENTS[i]);
00317    }
00318    totalBytesPreviouslyRead += numBytesRead;
00319   
00320    // Should not affect the IFILE buffer
00321    assert(myCurrentBufferSize == 0);
00322    assert(myBufferIndex == 0);
00323    
00324    // Eof - slightly varies based on the type of file on whether or not the
00325    // next read is the one the current read or the next one shows eof.
00326    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00327    {
00328       // does not yet register eof - but will on the next call.
00329       assert(myFileTypePtr->eof() == false);
00330       assert(ifeof() == false);      
00331    }
00332    else
00333    {
00334       assert(myFileTypePtr->eof() == true);
00335       assert(ifeof() == true);
00336    }
00337 
00338    // Try reading at end of file twice.
00339    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00340    assert(numBytesRead == 0);
00341    // Should not affect the IFILE buffer
00342    assert(myCurrentBufferSize == 0);
00343    assert(myBufferIndex == 0);
00344    assert(ifeof() == true);
00345 
00346    // 2nd read attempt at eof.   
00347    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00348    assert(numBytesRead == 0);
00349    // Should not affect the IFILE buffer
00350    assert(myCurrentBufferSize == 0);
00351    assert(myBufferIndex == 0);
00352    assert(ifeof() == true);
00353    
00354 
00355    // RESET
00356    ifrewind();
00357    totalBytesPreviouslyRead = 0;
00358 
00359    //////////////////////////////////////////
00360    // Test reading entire file using getc.
00361    // Loop through reading the file.
00362    char readChar;
00363    for(int index = 0; index < TEST_FILE_SIZE; index++)
00364    {
00365       // Read a character.
00366       readChar = ifgetc();
00367       assert(readChar == TEST_FILE_CONTENTS[index]);
00368       // Should affect the IFILE buffer
00369       assert(myCurrentBufferSize == TEST_FILE_SIZE);
00370       assert(myBufferIndex == index+1);
00371    }
00372    
00373    // Now that we have read the file, try reading again at eof.
00374    readChar = ifgetc();
00375    assert(readChar == EOF);
00376    assert(myCurrentBufferSize == 0);
00377    assert(myBufferIndex == 0);
00378 
00379    // Try again at eof.
00380    // Now that we have read the file, try reading again at eof.
00381    readChar = ifgetc();
00382    assert(readChar == EOF);
00383    assert(myCurrentBufferSize == 0);
00384    assert(myBufferIndex == 0);
00385 
00386    // RESET
00387    ifrewind();
00388    totalBytesPreviouslyRead = 0;
00389 
00390    ////////////////////////////////////////////////
00391    // Test reading just the beginning of the file.
00392    numBytesRead = ifread(myTestBuffer, 4);
00393    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
00394    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]);
00395    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
00396    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
00397    assert(numBytesRead == 4);
00398    totalBytesPreviouslyRead += numBytesRead;
00399    // This read should not have affected the internal buffer.
00400    assert(myCurrentBufferSize == 0);
00401    assert(myBufferIndex == 0);
00402    // Should not be at eof
00403    assert(ifeof() == false);
00404 
00405    // Test reading rest of file.
00406    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00407    assert(numBytesRead == TEST_FILE_SIZE - totalBytesPreviouslyRead);
00408    // Verify contents of what read.   
00409     for(int i = 0; i < numBytesRead; i++)
00410     {
00411        assert(myTestBuffer[i] == 
00412           TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
00413     }
00414     totalBytesPreviouslyRead += numBytesRead;
00415 
00416    // Try at end of file twice.
00417    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00418    assert(numBytesRead == 0);
00419    // Should not affect the IFILE buffer
00420    assert(myCurrentBufferSize == 0);
00421    assert(myBufferIndex == 0);
00422    assert(ifeof() == true);
00423 
00424    // 2nd read attempt at eof.   
00425    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00426    assert(numBytesRead == 0);
00427    // Should not affect the IFILE buffer
00428    assert(myCurrentBufferSize == 0);
00429    assert(myBufferIndex == 0);
00430    assert(ifeof() == true);
00431    
00432     // RESET
00433    ifrewind();
00434    totalBytesPreviouslyRead = 0;
00435 
00436    //////////////////////////////////////
00437    // Test reading just the beginning.
00438    numBytesRead = ifread(myTestBuffer, 4);
00439    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
00440    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]);
00441    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
00442    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
00443    assert(numBytesRead == 4);
00444    totalBytesPreviouslyRead += numBytesRead;
00445    // This read should not have affected the internal buffer.
00446    assert(myCurrentBufferSize == 0);
00447    assert(myBufferIndex == 0);
00448    // Should not be at eof
00449    assert(ifeof() == false);
00450 
00451    // Test doing 2 getc.
00452    readChar = ifgetc();
00453    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00454    int bufferSize = TEST_FILE_SIZE - totalBytesPreviouslyRead;
00455    assert(myCurrentBufferSize == bufferSize);
00456    assert(myBufferIndex == 1);
00457    totalBytesPreviouslyRead++;
00458 
00459    readChar = ifgetc();
00460    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00461    assert(myCurrentBufferSize == bufferSize);
00462    assert(myBufferIndex == 2);
00463    totalBytesPreviouslyRead++;
00464 
00465    // Test reading rest of file.
00466    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00467    assert(numBytesRead == TEST_FILE_SIZE - totalBytesPreviouslyRead);
00468    // Verify contents of what read.   
00469     for(int i = 0; i < numBytesRead; i++)
00470     {
00471        assert(myTestBuffer[i] == 
00472           TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
00473     }
00474     totalBytesPreviouslyRead += numBytesRead;
00475 
00476    // Try at end of file twice.
00477    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00478    assert(numBytesRead == 0);
00479    // Should not affect the IFILE buffer
00480    assert(myCurrentBufferSize == bufferSize);
00481    assert(myBufferIndex == bufferSize);
00482    assert(ifeof() == true);
00483 
00484    // 2nd read attempt at eof.   
00485    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00486    assert(numBytesRead == 0);
00487    // Should not affect the IFILE buffer
00488    assert(myCurrentBufferSize == bufferSize);
00489    assert(myBufferIndex == bufferSize);
00490    assert(ifeof() == true);
00491    
00492     // RESET
00493    ifrewind();
00494    totalBytesPreviouslyRead = 0;
00495    assert(myCurrentBufferSize == 0);
00496    assert(myBufferIndex == 0);
00497 
00498    //////////////////////////////////   
00499    // Start with 2 getc.
00500    readChar = ifgetc();
00501    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00502    bufferSize = TEST_FILE_SIZE;
00503    assert(myCurrentBufferSize == bufferSize);
00504    assert(myBufferIndex == 1);
00505    totalBytesPreviouslyRead++;
00506 
00507    readChar = ifgetc();
00508    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00509    assert(myCurrentBufferSize == bufferSize);
00510    assert(myBufferIndex == 2);
00511    totalBytesPreviouslyRead++;
00512 
00513    // Test reading part of the rest of the file.
00514    numBytesRead = ifread(myTestBuffer, 4);
00515    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00516    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 1]);
00517    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 2]);
00518    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 3]);
00519    assert(numBytesRead == 4);
00520    totalBytesPreviouslyRead += numBytesRead;
00521    // This read should not have affected the internal buffer.
00522    assert(myCurrentBufferSize == bufferSize);
00523    assert(myBufferIndex == totalBytesPreviouslyRead);
00524    // Should not be at eof
00525    assert(ifeof() == false);
00526 
00527    // Test reading 2 char with getc.
00528    readChar = ifgetc();
00529    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00530    assert(myCurrentBufferSize == bufferSize);
00531    totalBytesPreviouslyRead++;
00532    assert(myBufferIndex == totalBytesPreviouslyRead);
00533 
00534    readChar = ifgetc();
00535    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00536    assert(myCurrentBufferSize == bufferSize);
00537    totalBytesPreviouslyRead++;
00538    assert(myBufferIndex == totalBytesPreviouslyRead);
00539 
00540    // Test reading rest of file.
00541    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00542    assert(numBytesRead == TEST_FILE_SIZE - totalBytesPreviouslyRead);
00543    // Verify contents of what read.   
00544     for(int i = 0; i < numBytesRead; i++)
00545     {
00546        assert(myTestBuffer[i] == 
00547           TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
00548     }
00549     totalBytesPreviouslyRead += numBytesRead;
00550    assert(myBufferIndex == totalBytesPreviouslyRead);
00551    assert(myCurrentBufferSize == bufferSize);
00552 
00553    // Try at end of file twice.
00554    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00555    assert(numBytesRead == 0);
00556    // Should not affect the IFILE buffer
00557    assert(myCurrentBufferSize == bufferSize);
00558    assert(myBufferIndex == bufferSize);
00559    assert(ifeof() == true);
00560 
00561    // 2nd read attempt at eof.   
00562    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00563    assert(numBytesRead == 0);
00564    // Should not affect the IFILE buffer
00565    assert(myCurrentBufferSize == bufferSize);
00566    assert(myBufferIndex == bufferSize);
00567    assert(ifeof() == true);
00568    
00569     // RESET
00570    ifrewind();
00571    totalBytesPreviouslyRead = 0;
00572    assert(myCurrentBufferSize == 0);
00573    assert(myBufferIndex == 0);
00574 
00575    //////////////
00576    // Close the test file.
00577    ifclose();
00578 
00579    ////////////////////////////////////////////////////////////////////////
00580    // Repeat the test on a test file that is larger than the IFILE
00581    // buffer size.
00582 
00583    // First open the test file.
00584    openLargeFile(extension);
00585 
00586    // This file contains MAX_BUFFER_SIZE of '0's followed by "12345"
00587    // The size of the file is MAX_BUFFER_SIZE + 5.
00588    int largeTestFileSize = MAX_BUFFER_SIZE + 5;
00589    char largeBuffer[largeTestFileSize + 5];
00590 
00591    // Verify the file successfully opened.
00592    assert(myFileTypePtr != NULL);
00593    assert(isOpen());
00594    assert(myFileTypePtr->isOpen());
00595 
00596    numBytesRead = 0;
00597    totalBytesPreviouslyRead = 0;
00598 
00599    ////////////////////////////////////
00600    // Test reading entire file at once.
00601    numBytesRead = ifread(largeBuffer, largeTestFileSize + 4);
00602    assert(numBytesRead == largeTestFileSize);
00603    
00604    // Validate all the 0s
00605    for(int i = 0; i < MAX_BUFFER_SIZE; i++)
00606    {
00607       assert(largeBuffer[i] == '0');
00608    }
00609    // Now validate the "12345"
00610    assert(largeBuffer[MAX_BUFFER_SIZE] == '1');
00611    assert(largeBuffer[MAX_BUFFER_SIZE+1] == '2');
00612    assert(largeBuffer[MAX_BUFFER_SIZE+2] == '3');
00613    assert(largeBuffer[MAX_BUFFER_SIZE+3] == '4');
00614    assert(largeBuffer[MAX_BUFFER_SIZE+4] == '5');
00615 
00616    totalBytesPreviouslyRead += numBytesRead;
00617   
00618    // Should not affect the IFILE buffer
00619    assert(myCurrentBufferSize == 0);
00620    assert(myBufferIndex == 0);
00621    
00622    // Eof - slightly varies based on the type of file on whether or not the
00623    // next read is the one the current read or the next one shows eof.
00624    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00625    {
00626       // does not yet register eof - but will on the next call.
00627       assert(myFileTypePtr->eof() == false);
00628       assert(ifeof() == false);      
00629    }
00630    else
00631    {
00632       assert(myFileTypePtr->eof() == true);
00633       assert(ifeof() == true);
00634    }
00635 
00636    // Try reading at end of file twice.
00637    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00638    assert(numBytesRead == 0);
00639    // Should not affect the IFILE buffer
00640    assert(myCurrentBufferSize == 0);
00641    assert(myBufferIndex == 0);
00642    assert(ifeof() == true);
00643 
00644    // 2nd read attempt at eof.   
00645    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00646    assert(numBytesRead == 0);
00647    // Should not affect the IFILE buffer
00648    assert(myCurrentBufferSize == 0);
00649    assert(myBufferIndex == 0);
00650    assert(ifeof() == true);
00651    
00652 
00653    // RESET
00654    ifrewind();
00655    totalBytesPreviouslyRead = 0;
00656 
00657    //////////////////////////////////////////
00658    // Test reading entire file using getc.
00659    // Loop through reading the file.
00660    // First loop through verifying the 0's
00661    for(int index = 0; index < MAX_BUFFER_SIZE; index++)
00662    {
00663       // Read a character.
00664       readChar = ifgetc();
00665       assert(readChar == '0');
00666       // Should affect the IFILE buffer
00667       assert(myCurrentBufferSize == MAX_BUFFER_SIZE);
00668       assert(myBufferIndex == index+1);
00669    }
00670    // Now read the 12345.
00671    readChar = ifgetc();
00672    assert(readChar == '1');
00673    // Should affect the IFILE buffer
00674    assert(myCurrentBufferSize == 5);
00675    assert(myBufferIndex == 1);
00676    readChar = ifgetc();
00677    assert(readChar == '2');
00678    // Should affect the IFILE buffer
00679    assert(myCurrentBufferSize == 5);
00680    assert(myBufferIndex == 2);
00681    readChar = ifgetc();
00682    assert(readChar == '3');
00683    // Should affect the IFILE buffer
00684    assert(myCurrentBufferSize == 5);
00685    assert(myBufferIndex == 3);
00686    readChar = ifgetc();
00687    assert(readChar == '4');
00688    // Should affect the IFILE buffer
00689    assert(myCurrentBufferSize == 5);
00690    assert(myBufferIndex == 4);
00691    readChar = ifgetc();
00692    assert(readChar == '5');
00693    // Should affect the IFILE buffer
00694    assert(myCurrentBufferSize == 5);
00695    assert(myBufferIndex == 5);
00696 
00697    // Now that we have read the file, try reading again at eof.
00698    readChar = ifgetc();
00699    assert(readChar == EOF);
00700    assert(myCurrentBufferSize == 0);
00701    assert(myBufferIndex == 0);
00702 
00703    // Try again at eof.
00704    // Now that we have read the file, try reading again at eof.
00705    readChar = ifgetc();
00706    assert(readChar == EOF);
00707    assert(myCurrentBufferSize == 0);
00708    assert(myBufferIndex == 0);
00709 
00710    // RESET
00711    ifrewind();
00712    totalBytesPreviouslyRead = 0;
00713 
00714    ////////////////////////////////////////////////
00715    // Test reading just the beginning of the file.
00716    numBytesRead = ifread(largeBuffer, 4);
00717    assert(largeBuffer[0] == '0');
00718    assert(largeBuffer[1] == '0');
00719    assert(largeBuffer[2] == '0');
00720    assert(largeBuffer[3] == '0');
00721    assert(numBytesRead == 4);
00722    totalBytesPreviouslyRead += numBytesRead;
00723    // This read should not have affected the internal buffer.
00724    assert(myCurrentBufferSize == 0);
00725    assert(myBufferIndex == 0);
00726    // Should not be at eof
00727    assert(ifeof() == false);
00728 
00729    // Test reading rest of file.
00730    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00731    assert(numBytesRead == largeTestFileSize - totalBytesPreviouslyRead);
00732    // Verify contents of what read.   First check the 0's
00733    for(int i = 0; i < (numBytesRead-5); i++)
00734    {
00735       assert(largeBuffer[i] == '0');
00736    }
00737    // Check the 12345
00738    assert(largeBuffer[numBytesRead - 5] == '1');
00739    assert(largeBuffer[numBytesRead - 5 + 1] == '2');
00740    assert(largeBuffer[numBytesRead - 5 + 2] == '3');
00741    assert(largeBuffer[numBytesRead - 5 + 3] == '4');
00742    assert(largeBuffer[numBytesRead - 5 + 4] == '5');
00743    totalBytesPreviouslyRead += numBytesRead;
00744    
00745    // Try at end of file twice.
00746    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00747    assert(numBytesRead == 0);
00748    // Should not affect the IFILE buffer
00749    assert(myCurrentBufferSize == 0);
00750    assert(myBufferIndex == 0);
00751    assert(ifeof() == true);
00752 
00753    // 2nd read attempt at eof.   
00754    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00755    assert(numBytesRead == 0);
00756    // Should not affect the IFILE buffer
00757    assert(myCurrentBufferSize == 0);
00758    assert(myBufferIndex == 0);
00759    assert(ifeof() == true);
00760    
00761     // RESET
00762    ifrewind();
00763    totalBytesPreviouslyRead = 0;
00764 
00765    //////////////////////////////////////
00766    // Test reading just the beginning.
00767    numBytesRead = ifread(largeBuffer, 2);
00768    assert(largeBuffer[0] == '0');
00769    assert(largeBuffer[1] == '0');
00770    assert(numBytesRead == 2);
00771    totalBytesPreviouslyRead += numBytesRead;
00772    // This read should not have affected the internal buffer.
00773    assert(myCurrentBufferSize == 0);
00774    assert(myBufferIndex == 0);
00775    // Should not be at eof
00776    assert(ifeof() == false);
00777 
00778    // Test doing 2 getc.
00779    readChar = ifgetc();
00780    assert(readChar == '0');
00781    bufferSize = MAX_BUFFER_SIZE;
00782    assert(myCurrentBufferSize == bufferSize);
00783    assert(myBufferIndex == 1);
00784    totalBytesPreviouslyRead++;
00785 
00786    readChar = ifgetc();
00787    assert(readChar == '0');
00788    assert(myCurrentBufferSize == bufferSize);
00789    assert(myBufferIndex == 2);
00790    totalBytesPreviouslyRead++;
00791 
00792    // Test reading rest of file.
00793    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00794    assert(numBytesRead == largeTestFileSize - totalBytesPreviouslyRead);
00795    // Verify contents of what read.   
00796    // All except the last 5 should be '0'
00797     for(int i = 0; i < numBytesRead - 5; i++)
00798     {
00799        assert(largeBuffer[i] == '0');
00800     }
00801     assert(largeBuffer[numBytesRead - 5] == '1');
00802     assert(largeBuffer[numBytesRead - 4] == '2');
00803     assert(largeBuffer[numBytesRead - 3] == '3');
00804     assert(largeBuffer[numBytesRead - 2] == '4');
00805     assert(largeBuffer[numBytesRead - 1] == '5');
00806 
00807     totalBytesPreviouslyRead += numBytesRead;
00808 
00809    // Try at end of file twice.
00810    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00811    assert(numBytesRead == 0);
00812    // Should not affect the IFILE buffer
00813    assert(myCurrentBufferSize == bufferSize);
00814    assert(myBufferIndex == bufferSize);
00815    assert(ifeof() == true);
00816 
00817    // 2nd read attempt at eof.   
00818    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00819    assert(numBytesRead == 0);
00820    // Should not affect the IFILE buffer
00821    assert(myCurrentBufferSize == bufferSize);
00822    assert(myBufferIndex == bufferSize);
00823    assert(ifeof() == true);
00824    
00825     // RESET
00826    ifrewind();
00827    totalBytesPreviouslyRead = 0;
00828    assert(myCurrentBufferSize == 0);
00829    assert(myBufferIndex == 0);
00830 
00831    //////////////////////////////////   
00832    // Start with 2 getc.
00833    readChar = ifgetc();
00834    assert(readChar == '0');
00835    bufferSize = MAX_BUFFER_SIZE;
00836    assert(myCurrentBufferSize == bufferSize);
00837    assert(myBufferIndex == 1);
00838    totalBytesPreviouslyRead++;
00839 
00840    readChar = ifgetc();
00841    assert(readChar == '0');
00842    assert(myCurrentBufferSize == bufferSize);
00843    assert(myBufferIndex == 2);
00844    totalBytesPreviouslyRead++;
00845 
00846    // Test reading part of the rest of the file.
00847    numBytesRead = ifread(myTestBuffer, 2);
00848    assert(myTestBuffer[0] == '0');
00849    assert(myTestBuffer[1] == '0');
00850    assert(numBytesRead == 2);
00851    totalBytesPreviouslyRead += numBytesRead;
00852    // This read should not have affected the internal buffer.
00853    assert(myCurrentBufferSize == bufferSize);
00854    assert(myBufferIndex == totalBytesPreviouslyRead);
00855    // Should not be at eof
00856    assert(ifeof() == false);
00857 
00858    // Test reading 2 char with getc.
00859    readChar = ifgetc();
00860    assert(readChar == '0');
00861    assert(myCurrentBufferSize == bufferSize);
00862    totalBytesPreviouslyRead++;
00863    assert(myBufferIndex == totalBytesPreviouslyRead);
00864 
00865    readChar = ifgetc();
00866    assert(readChar == '0');
00867    assert(myCurrentBufferSize == bufferSize);
00868    totalBytesPreviouslyRead++;
00869    assert(myBufferIndex == totalBytesPreviouslyRead);
00870 
00871    // Test reading rest of file.
00872    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00873    assert(numBytesRead == largeTestFileSize - totalBytesPreviouslyRead);
00874    // Verify contents of what read.   
00875    for(int i = 0; i < numBytesRead - 5; i++)
00876    {
00877       assert(largeBuffer[i] == '0');
00878    }
00879    // Verify the 12345
00880    assert(largeBuffer[numBytesRead - 5] == '1');
00881    assert(largeBuffer[numBytesRead - 5 + 1] == '2');
00882    assert(largeBuffer[numBytesRead - 5 + 2] == '3');
00883    assert(largeBuffer[numBytesRead - 5 + 3] == '4');
00884    assert(largeBuffer[numBytesRead - 5 + 4] == '5');
00885    totalBytesPreviouslyRead += numBytesRead;
00886    assert(myBufferIndex == MAX_BUFFER_SIZE);
00887    assert(myCurrentBufferSize == bufferSize);
00888 
00889    // Try at end of file twice.
00890    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00891    assert(numBytesRead == 0);
00892    // Should not affect the IFILE buffer
00893    assert(myCurrentBufferSize == bufferSize);
00894    assert(myBufferIndex == bufferSize);
00895    assert(ifeof() == true);
00896 
00897    // 2nd read attempt at eof.   
00898    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00899    assert(numBytesRead == 0);
00900    // Should not affect the IFILE buffer
00901    assert(myCurrentBufferSize == bufferSize);
00902    assert(myBufferIndex == bufferSize);
00903    assert(ifeof() == true);
00904    
00905     // RESET
00906    ifrewind();
00907    totalBytesPreviouslyRead = 0;
00908    assert(myCurrentBufferSize == 0);
00909    assert(myBufferIndex == 0);
00910 
00911    ifclose();
00912 
00913    std::cout << "  Passed test_ifread_ifgetc" << std::endl;
00914 }
00915 
00916 
00917 // Test closing a file.
00918 void IFILE_Test::test_ifclose(const char* extension)
00919 {
00920    // First open the test file.
00921    openFile(extension);
00922 
00923    // Verify the file successfully opened.
00924    assert(myFileTypePtr != NULL);
00925    assert(isOpen());
00926    assert(myFileTypePtr->isOpen());
00927 
00928    ifclose();
00929 
00930    assert(myFileTypePtr == NULL);
00931    assert(isOpen() == false);
00932 
00933    std::cout << "  Passed test_ifclose" << std::endl;
00934 }
00935 
00936 
00937 void IFILE_Test::test_ifseek(const char* extension)
00938 {
00939    // First open the test file.
00940    openFile(extension);
00941 
00942    // Read a character from the file.
00943    int numBytesRead = readFromFile(myTestBuffer, 1);
00944    assert(numBytesRead == 1);
00945    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
00946 
00947    // Get the current position.
00948    long int currentPos = iftell();
00949    
00950    // Read the next character from the file.
00951    numBytesRead = readFromFile(myTestBuffer, 1);
00952    assert(numBytesRead == 1);
00953    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]);
00954 
00955    // Seek to just before the character that was just read and read again
00956    // Should be the same character.
00957    assert(ifseek(currentPos, SEEK_SET) == true);
00958    numBytesRead = readFromFile(myTestBuffer, 1);
00959    assert(numBytesRead == 1);
00960    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]);
00961    
00962    ifclose();
00963 
00964    assert(myFileTypePtr == NULL);
00965    assert(isOpen() == false);
00966 
00967    std::cout << "  Passed test_ifseek" << std::endl;
00968 }
00969 
00970 void IFILE_Test::test_noExistRead(const char* extension)
00971 {
00972    openNoExistFile(extension);
00973 
00974 }
00975 
00976 
00977 // Open a file for testing.
00978 void IFILE_Test::openFile(const char* extension)
00979 {
00980    std::string filename = "data/InputFileTest.";
00981    filename += extension;
00982    assert(InputFile::openFile(filename.c_str(), "rt", InputFile::DEFAULT) == true);
00983 }
00984 
00985 // Open a file for testing.
00986 void IFILE_Test::openLargeFile(const char* extension)
00987 {
00988    std::string filename = "data/InputFileTestLarge.";
00989    filename += extension;
00990    assert(InputFile::openFile(filename.data(), "rt", InputFile::DEFAULT) == true);
00991 }
00992 
00993 
00994 void IFILE_Test::openNoExistFile(const char* extension)
00995 {
00996    std::string filename = "data/noExist.";
00997    filename += extension;
00998    assert(InputFile::openFile(filename.data(), "rt", InputFile::DEFAULT) == false);
00999 }
01000 
01001 
01002 void testWrite()
01003 {
01004     std::string filenameNoExt = "results/InputFileTest.";
01005     std::string filename = filenameNoExt + "glf";
01006     
01007     IFILE filePtr = ifopen(filename.c_str(), "wt");
01008     assert(filePtr != NULL);
01009     
01010     assert(ifwrite(filePtr, 
01011                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01012                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01013            == IFILE_Test::TEST_FILE_CONTENTS.length());
01014     
01015     assert(ifclose(filePtr) == 0);
01016 
01017     filename = "results/uncompressedFile.glf";
01018     
01019     filePtr = ifopen(filename.c_str(), "wt", InputFile::UNCOMPRESSED);
01020     assert(filePtr != NULL);
01021     
01022     assert(ifwrite(filePtr,
01023                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01024                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01025            == IFILE_Test::TEST_FILE_CONTENTS.length());
01026     
01027     assert(ifclose(filePtr) == 0);
01028 
01029     filename = "results/bgzfFile.glf";
01030     
01031     filePtr = ifopen(filename.c_str(), "wt", InputFile::BGZF);
01032     assert(filePtr != NULL);
01033     
01034     assert(ifwrite(filePtr,
01035                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01036                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01037            == IFILE_Test::TEST_FILE_CONTENTS.length());
01038     
01039     assert(ifclose(filePtr) == 0);
01040 
01041     filename = "results/gzipFile.glf";
01042     
01043     filePtr = ifopen(filename.c_str(), "wt", InputFile::GZIP);
01044     assert(filePtr != NULL);
01045     
01046     assert(ifwrite(filePtr,
01047                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01048                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01049            ==IFILE_Test:: TEST_FILE_CONTENTS.length());
01050     
01051     assert(ifclose(filePtr) == 0);
01052 
01053     filename = "results/defaultFile.glf";
01054     
01055     filePtr = ifopen(filename.c_str(), "wt");
01056     assert(filePtr != NULL);
01057     
01058     assert(ifwrite(filePtr,
01059                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01060                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01061            == IFILE_Test::TEST_FILE_CONTENTS.length());
01062     
01063     assert(ifclose(filePtr) == 0);
01064 
01065     filename = "results/defaultFile.gz";
01066     
01067     filePtr = ifopen(filename.c_str(), "wt");
01068     assert(filePtr != NULL);
01069     
01070     assert(ifwrite(filePtr,
01071                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01072                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01073            == IFILE_Test::TEST_FILE_CONTENTS.length());
01074     
01075     assert(ifclose(filePtr) == 0);
01076 
01077     // TODO - automatically verify that the files were written in the
01078     // correct format - rather than hand checking.
01079 }
01080 
01081 
01082 #endif
Generated on Wed Nov 17 15:38:29 2010 for StatGen Software by  doxygen 1.6.3