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