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        bool caught = false;
00209        try
00210        {
00211            assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
00212        }
00213        catch (std::exception& e)
00214        {
00215            caught = true;
00216            assert(strcmp(e.what(), "IFILE: CANNOT use buffered reads and tell for BGZF files") == 0);
00217        }
00218        assert(caught);
00219        disableBuffering();
00220        assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
00221    }
00222    else
00223    {
00224       assert(iftell() == TEST_FILE_SIZE);
00225    }
00226 
00227    // rewind the file and verify that it no longer registers eof.
00228    ifrewind();
00229    totalBytesPreviouslyRead = 0;
00230    // No longer at eof
00231    assert(ifeof() == false);
00232    // Verify position in file.
00233    assert(iftell() == 0);
00234    
00235    // Buffer reads - may have been disabled for iftell to work for bgzf.
00236    bufferReads();
00237 
00238    // Read a character from the file.
00239    numBytesRead = readFromFile(myTestBuffer, 1);
00240    assert(numBytesRead == 1);
00241    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00242    // Now that we have tested based on the previous total bytes read, 
00243    // increment the count.
00244    totalBytesPreviouslyRead += numBytesRead;
00245    // Not at eof
00246    assert(ifeof() == false);
00247 
00248    ///////////////////////////////////
00249    // Test doing IFILE buffered reads.
00250    // Perform char read.
00251    char readChar = ifgetc();
00252    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00253    // Now that we have tested based on the previous total bytes read, 
00254    // increment the count.
00255    totalBytesPreviouslyRead += numBytesRead;
00256    // Not at eof
00257    assert(ifeof() == false);
00258    
00259    // Now read the rest.
00260    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00261    assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead));
00262    // Now that we have tested based on the previous total bytes read, 
00263    // increment the count.
00264    totalBytesPreviouslyRead += numBytesRead;
00265    // Registers eof.
00266    assert(ifeof() == true);
00267 
00268    // Read past eof.
00269    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00270    assert(numBytesRead == 0);
00271    // Eof.
00272    assert(ifeof() == true);
00273 
00274    // bgzf files use a specialized return value for iftell that
00275    // is not just straight file offset.
00276    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00277    {
00278        bool caught = false;
00279        try
00280        {
00281            assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
00282        }
00283        catch (std::exception& e)
00284        {
00285            caught = true;
00286            assert(strcmp(e.what(), "IFILE: CANNOT use buffered reads and tell for BGZF files") == 0);
00287        }
00288        assert(caught);
00289        disableBuffering();
00290        assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
00291    }
00292    else
00293    {
00294       assert(iftell() == TEST_FILE_SIZE);
00295    }
00296 
00297    // Verify that after rewind, eof is no longer registered.
00298    ifrewind();
00299   // reset since we are back to the beginning of the file.
00300    totalBytesPreviouslyRead = 0;
00301    // No longer at eof
00302    assert(ifeof() == false);
00303    // Verify position in file.
00304    assert(iftell() == 0);
00305 
00306    // Verify properly works even if already at the beginning.   
00307    ifrewind();
00308   // reset since we are back to the beginning of the file.
00309    totalBytesPreviouslyRead = 0;
00310    // Not eof
00311    assert(ifeof() == false);
00312    // Verify position in file.
00313    assert(iftell() == 0);
00314 
00315    // Buffer reads - may have been disabled for iftell to work for bgzf.
00316    bufferReads();
00317 
00318    //////////////////////
00319    // Close the test file.
00320    ifclose();
00321    
00322    std::cout << "  Passed test_ifeof_ifrewind" << std::endl;
00323 }
00324 
00325 
00326 void IFILE_Test::test_ifread_ifgetc(const char* extension)
00327 {
00328    // First open the test file.
00329    openFile(extension);
00330 
00331    // Verify the file successfully opened.
00332    assert(myFileTypePtr != NULL);
00333    assert(isOpen());
00334    assert(myFileTypePtr->isOpen());
00335 
00336    int numBytesRead = 0;
00337    unsigned int totalBytesPreviouslyRead = 0;
00338 
00339    ////////////////////////////////////
00340    // Test reading entire file at once.
00341    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00342    assert(numBytesRead == TEST_FILE_SIZE);
00343    
00344    for(int i = 0; i < TEST_FILE_SIZE; i++)
00345    {
00346       assert(myTestBuffer[i] == TEST_FILE_CONTENTS[i]);
00347    }
00348    totalBytesPreviouslyRead += numBytesRead;
00349   
00350    // Should affect the IFILE buffer
00351    assert(myCurrentBufferSize == (unsigned int)TEST_FILE_SIZE);
00352    assert(myBufferIndex == (unsigned int)TEST_FILE_SIZE);
00353    
00354    // Eof - slightly varies based on the type of file on whether or not the
00355    // next read is the one the current read or the next one shows eof.
00356    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00357    {
00358       // does not yet register eof - but will on the next call.
00359       assert(myFileTypePtr->eof() == false);
00360       assert(ifeof() == false);      
00361    }
00362    else
00363    {
00364       assert(myFileTypePtr->eof() == true);
00365       assert(ifeof() == true);
00366    }
00367 
00368    // Try reading at end of file twice.
00369    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00370    assert(numBytesRead == 0);
00371    // Should affect the IFILE buffer
00372    assert(myCurrentBufferSize == 0);
00373    assert(myBufferIndex == 0);
00374    assert(ifeof() == true);
00375 
00376    // 2nd read attempt at eof.   
00377    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00378    assert(numBytesRead == 0);
00379    // Should affect the IFILE buffer
00380    assert(myCurrentBufferSize == 0);
00381    assert(myBufferIndex == 0);
00382    assert(ifeof() == true);
00383    
00384 
00385    // RESET
00386    ifrewind();
00387    totalBytesPreviouslyRead = 0;
00388 
00389    //////////////////////////////////////////
00390    // Test reading entire file using getc.
00391    // Loop through reading the file.
00392    char readChar;
00393    for(unsigned int index = 0; index < (unsigned int)TEST_FILE_SIZE; index++)
00394    {
00395       // Read a character.
00396       readChar = ifgetc();
00397       assert(readChar == TEST_FILE_CONTENTS[index]);
00398       // Should affect the IFILE buffer
00399       assert(myCurrentBufferSize == (unsigned int)TEST_FILE_SIZE);
00400       assert(myBufferIndex == index+1);
00401    }
00402    
00403    // Now that we have read the file, try reading again at eof.
00404    readChar = ifgetc();
00405    assert(readChar == EOF);
00406    assert(myCurrentBufferSize == 0);
00407    assert(myBufferIndex == 0);
00408 
00409    // Try again at eof.
00410    // Now that we have read the file, try reading again at eof.
00411    readChar = ifgetc();
00412    assert(readChar == EOF);
00413    assert(myCurrentBufferSize == 0);
00414    assert(myBufferIndex == 0);
00415 
00416    // RESET
00417    ifrewind();
00418    totalBytesPreviouslyRead = 0;
00419 
00420    ////////////////////////////////////////////////
00421    // Test reading just the beginning of the file.
00422    numBytesRead = ifread(myTestBuffer, 4);
00423    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
00424    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]);
00425    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
00426    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
00427    assert(numBytesRead == 4);
00428    totalBytesPreviouslyRead += numBytesRead;
00429    // This read should have affected the internal buffer.
00430    assert(myCurrentBufferSize == (unsigned int)TEST_FILE_SIZE);
00431    assert(myBufferIndex == 4);
00432    // Should not be at eof
00433    assert(ifeof() == false);
00434 
00435    // Test reading rest of file.
00436    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00437    assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
00438    // Verify contents of what read.   
00439     for(int i = 0; i < numBytesRead; i++)
00440     {
00441        assert(myTestBuffer[i] == 
00442           TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
00443     }
00444     totalBytesPreviouslyRead += numBytesRead;
00445 
00446    // Try at end of file twice.
00447    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00448    assert(numBytesRead == 0);
00449    // Should affect the IFILE buffer
00450    assert(myCurrentBufferSize == 0);
00451    assert(myBufferIndex == 0);
00452    assert(ifeof() == true);
00453 
00454    // 2nd read attempt at eof.   
00455    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00456    assert(numBytesRead == 0);
00457    // Should affect the IFILE buffer
00458    assert(myCurrentBufferSize == 0);
00459    assert(myBufferIndex == 0);
00460    assert(ifeof() == true);
00461    
00462     // RESET
00463    ifrewind();
00464    totalBytesPreviouslyRead = 0;
00465 
00466    //////////////////////////////////////
00467    // Test reading just the beginning.
00468    numBytesRead = ifread(myTestBuffer, 4);
00469    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
00470    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]);
00471    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
00472    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
00473    assert(numBytesRead == 4);
00474    totalBytesPreviouslyRead += numBytesRead;
00475    // This read should have affected the internal buffer.
00476    assert(myCurrentBufferSize == (unsigned int)TEST_FILE_SIZE);
00477    assert(myBufferIndex == 4);
00478    // Should not be at eof
00479    assert(ifeof() == false);
00480 
00481    // Test doing 2 getc.
00482    readChar = ifgetc();
00483    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00484    unsigned int bufferSize = TEST_FILE_SIZE;
00485    assert(myCurrentBufferSize == bufferSize);
00486    assert(myBufferIndex == 5);
00487    totalBytesPreviouslyRead++;
00488 
00489    readChar = ifgetc();
00490    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00491    assert(myCurrentBufferSize == bufferSize);
00492    assert(myBufferIndex == 6);
00493    totalBytesPreviouslyRead++;
00494 
00495    // Test reading rest of file.
00496    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00497    assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
00498    // Verify contents of what read.   
00499     for(int i = 0; i < numBytesRead; i++)
00500     {
00501        assert(myTestBuffer[i] == 
00502           TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
00503     }
00504     totalBytesPreviouslyRead += numBytesRead;
00505 
00506    // Try at end of file twice.
00507    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00508    assert(numBytesRead == 0);
00509    // Should affect the IFILE buffer
00510    assert(myCurrentBufferSize == 0);
00511    assert(myBufferIndex == 0);
00512    assert(ifeof() == true);
00513 
00514    // 2nd read attempt at eof.   
00515    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00516    assert(numBytesRead == 0);
00517    // Should affect the IFILE buffer
00518    assert(myCurrentBufferSize == 0);
00519    assert(myBufferIndex == 0);
00520    assert(ifeof() == true);
00521    
00522     // RESET
00523    ifrewind();
00524    totalBytesPreviouslyRead = 0;
00525    assert(myCurrentBufferSize == 0);
00526    assert(myBufferIndex == 0);
00527 
00528    //////////////////////////////////   
00529    // Start with 2 getc.
00530    readChar = ifgetc();
00531    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00532    bufferSize = TEST_FILE_SIZE;
00533    assert(myCurrentBufferSize == bufferSize);
00534    assert(myBufferIndex == 1);
00535    totalBytesPreviouslyRead++;
00536 
00537    readChar = ifgetc();
00538    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00539    assert(myCurrentBufferSize == bufferSize);
00540    assert(myBufferIndex == 2);
00541    totalBytesPreviouslyRead++;
00542 
00543    // Test reading part of the rest of the file.
00544    numBytesRead = ifread(myTestBuffer, 4);
00545    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00546    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 1]);
00547    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 2]);
00548    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 3]);
00549    assert(numBytesRead == 4);
00550    totalBytesPreviouslyRead += numBytesRead;
00551    // This read should have affected the internal buffer.
00552    assert(myCurrentBufferSize == bufferSize);
00553    assert(myBufferIndex == totalBytesPreviouslyRead);
00554    // Should not be at eof
00555    assert(ifeof() == false);
00556 
00557    // Test reading 2 char with getc.
00558    readChar = ifgetc();
00559    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00560    assert(myCurrentBufferSize == bufferSize);
00561    totalBytesPreviouslyRead++;
00562    assert(myBufferIndex == totalBytesPreviouslyRead);
00563 
00564    readChar = ifgetc();
00565    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00566    assert(myCurrentBufferSize == bufferSize);
00567    totalBytesPreviouslyRead++;
00568    assert(myBufferIndex == totalBytesPreviouslyRead);
00569 
00570    // Test reading rest of file.
00571    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00572    assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
00573    // Verify contents of what read.   
00574     for(int i = 0; i < numBytesRead; i++)
00575     {
00576        assert(myTestBuffer[i] == 
00577           TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
00578     }
00579     totalBytesPreviouslyRead += numBytesRead;
00580    assert(myBufferIndex == 0);
00581    assert(myCurrentBufferSize == 0);
00582 
00583    // Try at end of file twice.
00584    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00585    assert(numBytesRead == 0);
00586    // Should affect the IFILE buffer
00587    assert(myCurrentBufferSize == 0);
00588    assert(myBufferIndex == 0);
00589    assert(ifeof() == true);
00590 
00591    // 2nd read attempt at eof.   
00592    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00593    assert(numBytesRead == 0);
00594    // Should affect the IFILE buffer
00595    assert(myCurrentBufferSize == 0);
00596    assert(myBufferIndex == 0);
00597    assert(ifeof() == true);
00598    
00599     // RESET
00600    ifrewind();
00601    totalBytesPreviouslyRead = 0;
00602    assert(myCurrentBufferSize == 0);
00603    assert(myBufferIndex == 0);
00604 
00605    //////////////
00606    // Close the test file.
00607    ifclose();
00608 
00609    ////////////////////////////////////////////////////////////////////////
00610    // Repeat the test on a test file that is larger than the IFILE
00611    // buffer size.
00612 
00613    // First open the test file.
00614    openLargeFile(extension);
00615 
00616    // This file contains DEFAULT_BUFFER_SIZE of '0's followed by "12345"
00617    // The size of the file is DEFAULT_BUFFER_SIZE + 5.
00618    int largeTestFileSize = DEFAULT_BUFFER_SIZE + 5;
00619    char largeBuffer[largeTestFileSize + 5];
00620 
00621    // Verify the file successfully opened.
00622    assert(myFileTypePtr != NULL);
00623    assert(isOpen());
00624    assert(myFileTypePtr->isOpen());
00625 
00626    numBytesRead = 0;
00627    totalBytesPreviouslyRead = 0;
00628 
00629    ////////////////////////////////////
00630    // Test reading entire file at once.
00631    numBytesRead = ifread(largeBuffer, largeTestFileSize + 4);
00632    assert(numBytesRead == largeTestFileSize);
00633    
00634    // Validate all the 0s
00635    for(unsigned int i = 0; i < DEFAULT_BUFFER_SIZE; i++)
00636    {
00637       assert(largeBuffer[i] == '0');
00638    }
00639    // Now validate the "12345"
00640    assert(largeBuffer[DEFAULT_BUFFER_SIZE] == '1');
00641    assert(largeBuffer[DEFAULT_BUFFER_SIZE+1] == '2');
00642    assert(largeBuffer[DEFAULT_BUFFER_SIZE+2] == '3');
00643    assert(largeBuffer[DEFAULT_BUFFER_SIZE+3] == '4');
00644    assert(largeBuffer[DEFAULT_BUFFER_SIZE+4] == '5');
00645 
00646    totalBytesPreviouslyRead += numBytesRead;
00647   
00648    // Should affect the IFILE buffer - 0 because read
00649    // is bigger than the buffer, so just read directly
00650    // into the largeBuffer.
00651    assert(myCurrentBufferSize == 0);
00652    assert(myBufferIndex == 0);
00653    
00654    // Eof - slightly varies based on the type of file on whether or not the
00655    // next read is the one the current read or the next one shows eof.
00656    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
00657    {
00658       // does not yet register eof - but will on the next call.
00659       assert(myFileTypePtr->eof() == false);
00660       assert(ifeof() == false);      
00661    }
00662    else
00663    {
00664       assert(myFileTypePtr->eof() == true);
00665       assert(ifeof() == true);
00666    }
00667 
00668    // Try reading at end of file twice.
00669    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00670    assert(numBytesRead == 0);
00671    // Should affect the IFILE buffer
00672    assert(myCurrentBufferSize == 0);
00673    assert(myBufferIndex == 0);
00674    assert(ifeof() == true);
00675 
00676    // 2nd read attempt at eof.   
00677    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00678    assert(numBytesRead == 0);
00679    // Should affect the IFILE buffer
00680    assert(myCurrentBufferSize == 0);
00681    assert(myBufferIndex == 0);
00682    assert(ifeof() == true);
00683    
00684 
00685    // RESET
00686    ifrewind();
00687    totalBytesPreviouslyRead = 0;
00688 
00689    //////////////////////////////////////////
00690    // Test reading entire file using getc.
00691    // Loop through reading the file.
00692    // First loop through verifying the 0's
00693    for(unsigned int index = 0; index < DEFAULT_BUFFER_SIZE; index++)
00694    {
00695       // Read a character.
00696       readChar = ifgetc();
00697       assert(readChar == '0');
00698       // Should affect the IFILE buffer
00699       assert(myCurrentBufferSize == DEFAULT_BUFFER_SIZE);
00700       assert(myBufferIndex == index+1);
00701    }
00702    // Now read the 12345.
00703    readChar = ifgetc();
00704    assert(readChar == '1');
00705    // Should affect the IFILE buffer
00706    assert(myCurrentBufferSize == 5);
00707    assert(myBufferIndex == 1);
00708    readChar = ifgetc();
00709    assert(readChar == '2');
00710    // Should affect the IFILE buffer
00711    assert(myCurrentBufferSize == 5);
00712    assert(myBufferIndex == 2);
00713    readChar = ifgetc();
00714    assert(readChar == '3');
00715    // Should affect the IFILE buffer
00716    assert(myCurrentBufferSize == 5);
00717    assert(myBufferIndex == 3);
00718    readChar = ifgetc();
00719    assert(readChar == '4');
00720    // Should affect the IFILE buffer
00721    assert(myCurrentBufferSize == 5);
00722    assert(myBufferIndex == 4);
00723    readChar = ifgetc();
00724    assert(readChar == '5');
00725    // Should affect the IFILE buffer
00726    assert(myCurrentBufferSize == 5);
00727    assert(myBufferIndex == 5);
00728 
00729    // Now that we have read the file, try reading again at eof.
00730    readChar = ifgetc();
00731    assert(readChar == EOF);
00732    assert(myCurrentBufferSize == 0);
00733    assert(myBufferIndex == 0);
00734 
00735    // Try again at eof.
00736    // Now that we have read the file, try reading again at eof.
00737    readChar = ifgetc();
00738    assert(readChar == EOF);
00739    assert(myCurrentBufferSize == 0);
00740    assert(myBufferIndex == 0);
00741 
00742    // RESET
00743    ifrewind();
00744    totalBytesPreviouslyRead = 0;
00745 
00746    ////////////////////////////////////////////////
00747    // Test reading just the beginning of the file.
00748    numBytesRead = ifread(largeBuffer, 4);
00749    assert(largeBuffer[0] == '0');
00750    assert(largeBuffer[1] == '0');
00751    assert(largeBuffer[2] == '0');
00752    assert(largeBuffer[3] == '0');
00753    assert(numBytesRead == 4);
00754    totalBytesPreviouslyRead += numBytesRead;
00755    // This read should have affected the internal buffer.
00756    assert(myCurrentBufferSize == DEFAULT_BUFFER_SIZE);
00757    assert(myBufferIndex == 4);
00758    // Should not be at eof
00759    assert(ifeof() == false);
00760 
00761    // Test reading rest of file.
00762    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00763    assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
00764    // Verify contents of what read.   First check the 0's
00765    for(int i = 0; i < (numBytesRead-5); i++)
00766    {
00767       assert(largeBuffer[i] == '0');
00768    }
00769    // Check the 12345
00770    assert(largeBuffer[numBytesRead - 5] == '1');
00771    assert(largeBuffer[numBytesRead - 5 + 1] == '2');
00772    assert(largeBuffer[numBytesRead - 5 + 2] == '3');
00773    assert(largeBuffer[numBytesRead - 5 + 3] == '4');
00774    assert(largeBuffer[numBytesRead - 5 + 4] == '5');
00775    totalBytesPreviouslyRead += numBytesRead;
00776    
00777    // Try at end of file twice.
00778    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00779    assert(numBytesRead == 0);
00780    // Should affect the IFILE buffer - size if 5, the ammount
00781    // that did not fit in the first read.
00782    assert(myCurrentBufferSize == 5);
00783    assert(myBufferIndex == 5);
00784    assert(ifeof() == true);
00785 
00786    // 2nd read attempt at eof.   
00787    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00788    assert(numBytesRead == 0);
00789    // Should affect the IFILE buffer
00790    assert(myCurrentBufferSize == 5);
00791    assert(myBufferIndex == 5);
00792    assert(ifeof() == true);
00793    
00794     // RESET
00795    ifrewind();
00796    totalBytesPreviouslyRead = 0;
00797 
00798    //////////////////////////////////////
00799    // Test reading just the beginning.
00800    numBytesRead = ifread(largeBuffer, 2);
00801    assert(largeBuffer[0] == '0');
00802    assert(largeBuffer[1] == '0');
00803    assert(numBytesRead == 2);
00804    totalBytesPreviouslyRead += numBytesRead;
00805    // This read should have affected the internal buffer.
00806    assert(myCurrentBufferSize == DEFAULT_BUFFER_SIZE);
00807    assert(myBufferIndex == 2);
00808    // Should not be at eof
00809    assert(ifeof() == false);
00810 
00811    // Test doing 2 getc.
00812    readChar = ifgetc();
00813    assert(readChar == '0');
00814    bufferSize = DEFAULT_BUFFER_SIZE;
00815    assert(myCurrentBufferSize == bufferSize);
00816    assert(myBufferIndex == 3);
00817    totalBytesPreviouslyRead++;
00818 
00819    readChar = ifgetc();
00820    assert(readChar == '0');
00821    assert(myCurrentBufferSize == bufferSize);
00822    assert(myBufferIndex == 4);
00823    totalBytesPreviouslyRead++;
00824 
00825    // Test reading rest of file.
00826    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00827    assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
00828    // Verify contents of what read.   
00829    // All except the last 5 should be '0'
00830     for(int i = 0; i < numBytesRead - 5; i++)
00831     {
00832        assert(largeBuffer[i] == '0');
00833     }
00834     assert(largeBuffer[numBytesRead - 5] == '1');
00835     assert(largeBuffer[numBytesRead - 4] == '2');
00836     assert(largeBuffer[numBytesRead - 3] == '3');
00837     assert(largeBuffer[numBytesRead - 2] == '4');
00838     assert(largeBuffer[numBytesRead - 1] == '5');
00839 
00840     totalBytesPreviouslyRead += numBytesRead;
00841 
00842    // Try at end of file twice.
00843    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00844    assert(numBytesRead == 0);
00845    // Should not affect the IFILE buffer
00846    assert(myCurrentBufferSize == 5);
00847    assert(myBufferIndex == 5);
00848    assert(ifeof() == true);
00849 
00850    // 2nd read attempt at eof.   
00851    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00852    assert(numBytesRead == 0);
00853    // Should not affect the IFILE buffer
00854    assert(myCurrentBufferSize == 5);
00855    assert(myBufferIndex == 5);
00856    assert(ifeof() == true);
00857    
00858     // RESET
00859    ifrewind();
00860    totalBytesPreviouslyRead = 0;
00861    assert(myCurrentBufferSize == 0);
00862    assert(myBufferIndex == 0);
00863 
00864    //////////////////////////////////   
00865    // Start with 2 getc.
00866    readChar = ifgetc();
00867    assert(readChar == '0');
00868    bufferSize = DEFAULT_BUFFER_SIZE;
00869    assert(myCurrentBufferSize == bufferSize);
00870    assert(myBufferIndex == 1);
00871    totalBytesPreviouslyRead++;
00872 
00873    readChar = ifgetc();
00874    assert(readChar == '0');
00875    assert(myCurrentBufferSize == bufferSize);
00876    assert(myBufferIndex == 2);
00877    totalBytesPreviouslyRead++;
00878 
00879    // Test reading part of the rest of the file.
00880    numBytesRead = ifread(myTestBuffer, 2);
00881    assert(myTestBuffer[0] == '0');
00882    assert(myTestBuffer[1] == '0');
00883    assert(numBytesRead == 2);
00884    totalBytesPreviouslyRead += numBytesRead;
00885    // This read should have affected the internal buffer.
00886    assert(myCurrentBufferSize == bufferSize);
00887    assert(myBufferIndex == totalBytesPreviouslyRead);
00888    // Should not be at eof
00889    assert(ifeof() == false);
00890 
00891    // Test reading 2 char with getc.
00892    readChar = ifgetc();
00893    assert(readChar == '0');
00894    assert(myCurrentBufferSize == bufferSize);
00895    totalBytesPreviouslyRead++;
00896    assert(myBufferIndex == totalBytesPreviouslyRead);
00897 
00898    readChar = ifgetc();
00899    assert(readChar == '0');
00900    assert(myCurrentBufferSize == bufferSize);
00901    totalBytesPreviouslyRead++;
00902    assert(myBufferIndex == totalBytesPreviouslyRead);
00903 
00904    // Test reading rest of file.
00905    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00906    assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
00907    // Verify contents of what read.   
00908    for(int i = 0; i < numBytesRead - 5; i++)
00909    {
00910       assert(largeBuffer[i] == '0');
00911    }
00912    // Verify the 12345
00913    assert(largeBuffer[numBytesRead - 5] == '1');
00914    assert(largeBuffer[numBytesRead - 5 + 1] == '2');
00915    assert(largeBuffer[numBytesRead - 5 + 2] == '3');
00916    assert(largeBuffer[numBytesRead - 5 + 3] == '4');
00917    assert(largeBuffer[numBytesRead - 5 + 4] == '5');
00918    totalBytesPreviouslyRead += numBytesRead;
00919    bufferSize = 5;
00920    assert(myBufferIndex == bufferSize);
00921    assert(myCurrentBufferSize == bufferSize);
00922 
00923    // Try at end of file twice.
00924    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00925    assert(numBytesRead == 0);
00926    // Should not affect the IFILE buffer
00927    assert(myCurrentBufferSize == bufferSize);
00928    assert(myBufferIndex == bufferSize);
00929    assert(ifeof() == true);
00930 
00931    // 2nd read attempt at eof.   
00932    numBytesRead = ifread(largeBuffer, largeTestFileSize);
00933    assert(numBytesRead == 0);
00934    // Should not affect the IFILE buffer
00935    assert(myCurrentBufferSize == bufferSize);
00936    assert(myBufferIndex == bufferSize);
00937    assert(ifeof() == true);
00938    
00939     // RESET
00940    ifrewind();
00941    totalBytesPreviouslyRead = 0;
00942    assert(myCurrentBufferSize == 0);
00943    assert(myBufferIndex == 0);
00944 
00945    ifclose();
00946 
00947    std::cout << "  Passed test_ifread_ifgetc" << std::endl;
00948 }
00949 
00950 
00951 // Test closing a file.
00952 void IFILE_Test::test_ifclose(const char* extension)
00953 {
00954    // First open the test file.
00955    openFile(extension);
00956 
00957    // Verify the file successfully opened.
00958    assert(myFileTypePtr != NULL);
00959    assert(isOpen());
00960    assert(myFileTypePtr->isOpen());
00961 
00962    ifclose();
00963 
00964    assert(myFileTypePtr == NULL);
00965    assert(isOpen() == false);
00966 
00967    std::cout << "  Passed test_ifclose" << std::endl;
00968 }
00969 
00970 
00971 void IFILE_Test::test_ifseek(const char* extension)
00972 {
00973     disableBuffering();
00974    // First open the test file.
00975    openFile(extension);
00976 
00977    // Read a character from the file.
00978    int numBytesRead = readFromFile(myTestBuffer, 1);
00979    assert(numBytesRead == 1);
00980    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
00981 
00982    // Get the current position.
00983    long int currentPos = iftell();
00984    
00985    // Read the next character from the file.
00986    numBytesRead = readFromFile(myTestBuffer, 1);
00987    assert(numBytesRead == 1);
00988    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]);
00989 
00990    // Seek to just before the character that was just read and read again
00991    // Should be the same character.
00992    assert(ifseek(currentPos, SEEK_SET) == true);
00993    numBytesRead = readFromFile(myTestBuffer, 1);
00994    assert(numBytesRead == 1);
00995    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]);
00996    
00997    ifclose();
00998 
00999    assert(myFileTypePtr == NULL);
01000    assert(isOpen() == false);
01001 
01002    // Buffer reads - may have been disabled for iftell to work for bgzf.
01003    bufferReads();
01004 
01005    std::cout << "  Passed test_ifseek" << std::endl;
01006 }
01007 
01008 void IFILE_Test::test_noExistRead(const char* extension)
01009 {
01010    openNoExistFile(extension);
01011 
01012 }
01013 
01014 
01015 // Open a file for testing.
01016 void IFILE_Test::openFile(const char* extension)
01017 {
01018    std::string filename = "data/InputFileTest.";
01019    filename += extension;
01020    assert(InputFile::openFile(filename.c_str(), "rt", InputFile::DEFAULT) == true);
01021 }
01022 
01023 // Open a file for testing.
01024 void IFILE_Test::openLargeFile(const char* extension)
01025 {
01026    std::string filename = "data/InputFileTestLarge.";
01027    filename += extension;
01028    assert(InputFile::openFile(filename.data(), "rt", InputFile::DEFAULT) == true);
01029 }
01030 
01031 
01032 void IFILE_Test::openNoExistFile(const char* extension)
01033 {
01034    std::string filename = "data/noExist.";
01035    filename += extension;
01036    assert(InputFile::openFile(filename.data(), "rt", InputFile::DEFAULT) == false);
01037 }
01038 
01039 
01040 void testWrite()
01041 {
01042     std::string filenameNoExt = "results/InputFileTest.";
01043     std::string filename = filenameNoExt + "glf";
01044     
01045     IFILE filePtr = ifopen(filename.c_str(), "wt");
01046     assert(filePtr != NULL);
01047     
01048     assert(ifwrite(filePtr, 
01049                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01050                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01051            == IFILE_Test::TEST_FILE_CONTENTS.length());
01052     
01053     assert(ifclose(filePtr) == 0);
01054 
01055     filename = "results/uncompressedFile.glf";
01056     
01057     filePtr = ifopen(filename.c_str(), "wt", InputFile::UNCOMPRESSED);
01058     assert(filePtr != NULL);
01059     
01060     assert(ifwrite(filePtr,
01061                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01062                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01063            == IFILE_Test::TEST_FILE_CONTENTS.length());
01064     
01065     assert(ifclose(filePtr) == 0);
01066 
01067     filename = "results/bgzfFile.glf";
01068     
01069     filePtr = ifopen(filename.c_str(), "wt", InputFile::BGZF);
01070     assert(filePtr != NULL);
01071     
01072     assert(ifwrite(filePtr,
01073                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01074                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01075            == IFILE_Test::TEST_FILE_CONTENTS.length());
01076     
01077     assert(ifclose(filePtr) == 0);
01078 
01079     filename = "results/gzipFile.glf";
01080     
01081     filePtr = ifopen(filename.c_str(), "wt", InputFile::GZIP);
01082     assert(filePtr != NULL);
01083     
01084     assert(ifwrite(filePtr,
01085                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01086                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01087            ==IFILE_Test:: TEST_FILE_CONTENTS.length());
01088     
01089     assert(ifclose(filePtr) == 0);
01090 
01091     filename = "results/defaultFile.glf";
01092     
01093     filePtr = ifopen(filename.c_str(), "wt");
01094     assert(filePtr != NULL);
01095     
01096     assert(ifwrite(filePtr,
01097                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01098                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01099            == IFILE_Test::TEST_FILE_CONTENTS.length());
01100     
01101     assert(ifclose(filePtr) == 0);
01102 
01103     filename = "results/defaultFile.gz";
01104     
01105     filePtr = ifopen(filename.c_str(), "wt");
01106     assert(filePtr != NULL);
01107     
01108     assert(ifwrite(filePtr,
01109                    IFILE_Test::TEST_FILE_CONTENTS.c_str(), 
01110                    IFILE_Test::TEST_FILE_CONTENTS.length()) 
01111            == IFILE_Test::TEST_FILE_CONTENTS.length());
01112     
01113     assert(ifclose(filePtr) == 0);
01114 
01115     // TODO - automatically verify that the files were written in the
01116     // correct format - rather than hand checking.
01117 }
01118 
01119 
01120 #endif
Generated on Tue Mar 22 22:50:19 2011 for StatGen Software by  doxygen 1.6.3