00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00079 openFile(extension);
00080
00081
00082 assert(myFileTypePtr != NULL);
00083 assert(isOpen());
00084 assert(myFileTypePtr->isOpen());
00085
00086
00087 int numBytesRead = 0;
00088
00089
00090
00091 int totalBytesPreviouslyRead = 0;
00092
00093
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
00102 assert(myCurrentBufferSize == 0);
00103 assert(myBufferIndex == 0);
00104
00105 assert(myFileTypePtr->eof() == false);
00106 assert(ifeof() == false);
00107
00108
00109 numBytesRead = readFromFile(myTestBuffer, 2);
00110
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
00118 assert(myCurrentBufferSize == 0);
00119 assert(myBufferIndex == 0);
00120
00121 assert(myFileTypePtr->eof() == false);
00122 assert(ifeof() == false);
00123
00124
00125
00126
00127 numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00128
00129
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
00142 numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00143 assert(numBytesRead == 0);
00144
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
00157 openFile(extension);
00158
00159
00160 assert(myFileTypePtr != NULL);
00161 assert(isOpen());
00162 assert(myFileTypePtr->isOpen());
00163
00164
00165 assert(ifeof() == false);
00166
00167
00168
00169 int totalBytesPreviouslyRead = 0;
00170 int numBytesRead = 0;
00171
00172
00173
00174 disableBuffering();
00175
00176
00177 assert(iftell() == 0);
00178
00179
00180 numBytesRead = readFromFile(myTestBuffer, 1);
00181 assert(numBytesRead == 1);
00182 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00183
00184
00185 totalBytesPreviouslyRead += numBytesRead;
00186
00187 assert(ifeof() == false);
00188
00189
00190 char readChar = ifgetc();
00191 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00192
00193
00194 ++totalBytesPreviouslyRead;
00195
00196 assert(ifeof() == false);
00197 assert(iftell() == totalBytesPreviouslyRead);
00198
00199
00200 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00201 assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead));
00202
00203 assert(numBytesRead != MAX_TEST_BUFFER_SIZE);
00204
00205
00206 totalBytesPreviouslyRead += numBytesRead;
00207
00208 assert(myFileTypePtr->eof() == true);
00209 assert(ifeof() == true);
00210
00211 numBytesRead = readFromFile(myTestBuffer, 1);
00212 assert(numBytesRead == 0);
00213
00214 assert(ifeof() == true);
00215
00216
00217
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
00229
00230 ifrewind();
00231 totalBytesPreviouslyRead = 0;
00232
00233 assert(ifeof() == false);
00234
00235 assert(iftell() == 0);
00236
00237
00238 bufferReads();
00239
00240
00241 numBytesRead = readFromFile(myTestBuffer, 1);
00242 assert(numBytesRead == 1);
00243 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00244
00245
00246 totalBytesPreviouslyRead += numBytesRead;
00247
00248 assert(ifeof() == false);
00249
00250
00251 readChar = ifgetc();
00252 assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
00253
00254
00255 ++totalBytesPreviouslyRead;
00256
00257 assert(ifeof() == false);
00258
00259
00260
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
00281 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00282 assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead));
00283
00284
00285 totalBytesPreviouslyRead += numBytesRead;
00286
00287 assert(ifeof() == true);
00288
00289
00290 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00291 std::cerr << numBytesRead << std::endl;
00292 assert(numBytesRead == 0);
00293
00294 assert(ifeof() == true);
00295
00296
00297
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
00320 ifrewind();
00321
00322 totalBytesPreviouslyRead = 0;
00323
00324 assert(ifeof() == false);
00325
00326 assert(iftell() == 0);
00327
00328
00329 ifrewind();
00330
00331 totalBytesPreviouslyRead = 0;
00332
00333 assert(ifeof() == false);
00334
00335 assert(iftell() == 0);
00336
00337
00338 bufferReads();
00339
00340
00341
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
00351 openFile(extension);
00352
00353
00354 assert(myFileTypePtr != NULL);
00355 assert(isOpen());
00356 assert(myFileTypePtr->isOpen());
00357
00358 int numBytesRead = 0;
00359 int totalBytesPreviouslyRead = 0;
00360
00361
00362
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
00373 assert(myCurrentBufferSize == TEST_FILE_SIZE);
00374 assert(myBufferIndex == TEST_FILE_SIZE);
00375
00376 assert(myFileTypePtr->eof() == true);
00377 assert(ifeof() == true);
00378
00379
00380 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00381 assert(numBytesRead == 0);
00382
00383 assert(myCurrentBufferSize == 0);
00384 assert(myBufferIndex == 0);
00385 assert(ifeof() == true);
00386
00387
00388 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00389 assert(numBytesRead == 0);
00390
00391 assert(myCurrentBufferSize == 0);
00392 assert(myBufferIndex == 0);
00393 assert(ifeof() == true);
00394
00395
00396
00397 ifrewind();
00398 totalBytesPreviouslyRead = 0;
00399
00400
00401
00402
00403 char readChar;
00404 for(int index = 0; index < TEST_FILE_SIZE; index++)
00405 {
00406
00407 readChar = ifgetc();
00408 assert(readChar == TEST_FILE_CONTENTS[index]);
00409
00410 assert(myCurrentBufferSize == TEST_FILE_SIZE);
00411 assert(myBufferIndex == index+1);
00412 }
00413
00414
00415 readChar = ifgetc();
00416 assert(readChar == EOF);
00417 assert(myCurrentBufferSize == 0);
00418 assert(myBufferIndex == 0);
00419
00420
00421
00422 readChar = ifgetc();
00423 assert(readChar == EOF);
00424 assert(myCurrentBufferSize == 0);
00425 assert(myBufferIndex == 0);
00426
00427
00428 ifrewind();
00429 totalBytesPreviouslyRead = 0;
00430
00431
00432
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
00441 assert(myCurrentBufferSize == TEST_FILE_SIZE);
00442 assert(myBufferIndex == 4);
00443
00444 assert(ifeof() == false);
00445
00446
00447 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00448 assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
00449
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
00458 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00459 assert(numBytesRead == 0);
00460
00461 assert(myCurrentBufferSize == 0);
00462 assert(myBufferIndex == 0);
00463 assert(ifeof() == true);
00464
00465
00466 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00467 assert(numBytesRead == 0);
00468
00469 assert(myCurrentBufferSize == 0);
00470 assert(myBufferIndex == 0);
00471 assert(ifeof() == true);
00472
00473
00474 ifrewind();
00475 totalBytesPreviouslyRead = 0;
00476
00477
00478
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
00487 assert(myCurrentBufferSize == TEST_FILE_SIZE);
00488 assert(myBufferIndex == 4);
00489
00490 assert(ifeof() == false);
00491
00492
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
00507 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00508 assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
00509
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
00518 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00519 assert(numBytesRead == 0);
00520
00521 assert(myCurrentBufferSize == 0);
00522 assert(myBufferIndex == 0);
00523 assert(ifeof() == true);
00524
00525
00526 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00527 assert(numBytesRead == 0);
00528
00529 assert(myCurrentBufferSize == 0);
00530 assert(myBufferIndex == 0);
00531 assert(ifeof() == true);
00532
00533
00534 ifrewind();
00535 totalBytesPreviouslyRead = 0;
00536 assert(myCurrentBufferSize == 0);
00537 assert(myBufferIndex == 0);
00538
00539
00540
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
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
00563 assert(myCurrentBufferSize == bufferSize);
00564 assert(myBufferIndex == totalBytesPreviouslyRead);
00565
00566 assert(ifeof() == false);
00567
00568
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
00582 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00583 assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
00584
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
00595 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00596 assert(numBytesRead == 0);
00597
00598 assert(myCurrentBufferSize == 0);
00599 assert(myBufferIndex == 0);
00600 assert(ifeof() == true);
00601
00602
00603 numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
00604 assert(numBytesRead == 0);
00605
00606 assert(myCurrentBufferSize == 0);
00607 assert(myBufferIndex == 0);
00608 assert(ifeof() == true);
00609
00610
00611 ifrewind();
00612 totalBytesPreviouslyRead = 0;
00613 assert(myCurrentBufferSize == 0);
00614 assert(myBufferIndex == 0);
00615
00616
00617
00618 ifclose();
00619
00620
00621
00622
00623
00624
00625 openLargeFile(extension);
00626
00627
00628
00629 int largeTestFileSize = DEFAULT_BUFFER_SIZE + 5;
00630 char largeBuffer[largeTestFileSize + 5];
00631
00632
00633 assert(myFileTypePtr != NULL);
00634 assert(isOpen());
00635 assert(myFileTypePtr->isOpen());
00636
00637 numBytesRead = 0;
00638 totalBytesPreviouslyRead = 0;
00639
00640
00641
00642
00643
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
00649 assert(myFileTypePtr->eof() == true);
00650 assert(ifeof() == true);
00651 numBytesRead = ifread(largeBuffer + DEFAULT_BUFFER_SIZE + 3, 2);
00652 assert(numBytesRead == 0);
00653
00654
00655 for(unsigned int i = 0; i < DEFAULT_BUFFER_SIZE; i++)
00656 {
00657 assert(largeBuffer[i] == '0');
00658 }
00659
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
00669
00670
00671 assert(myCurrentBufferSize == 0);
00672 assert(myBufferIndex == 0);
00673
00674 assert(myFileTypePtr->eof() == true);
00675 assert(ifeof() == true);
00676
00677
00678 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00679 assert(numBytesRead == 0);
00680
00681 assert(myCurrentBufferSize == 0);
00682 assert(myBufferIndex == 0);
00683 assert(ifeof() == true);
00684
00685
00686 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00687 assert(numBytesRead == 0);
00688
00689 assert(myCurrentBufferSize == 0);
00690 assert(myBufferIndex == 0);
00691 assert(ifeof() == true);
00692
00693
00694
00695 ifrewind();
00696 totalBytesPreviouslyRead = 0;
00697
00698
00699
00700 numBytesRead = ifread(largeBuffer, largeTestFileSize + 4);
00701 assert(numBytesRead == largeTestFileSize);
00702
00703
00704 for(unsigned int i = 0; i < DEFAULT_BUFFER_SIZE; i++)
00705 {
00706 assert(largeBuffer[i] == '0');
00707 }
00708
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
00718
00719
00720 assert(myCurrentBufferSize == 0);
00721 assert(myBufferIndex == 0);
00722
00723 assert(myFileTypePtr->eof() == true);
00724 assert(ifeof() == true);
00725
00726
00727 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00728 assert(numBytesRead == 0);
00729
00730 assert(myCurrentBufferSize == 0);
00731 assert(myBufferIndex == 0);
00732 assert(ifeof() == true);
00733
00734
00735 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00736 assert(numBytesRead == 0);
00737
00738 assert(myCurrentBufferSize == 0);
00739 assert(myBufferIndex == 0);
00740 assert(ifeof() == true);
00741
00742
00743
00744 ifrewind();
00745 totalBytesPreviouslyRead = 0;
00746
00747
00748
00749
00750
00751 for(int index = 0; index < (int)DEFAULT_BUFFER_SIZE; index++)
00752 {
00753
00754 readChar = ifgetc();
00755 assert(readChar == '0');
00756
00757 assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE);
00758 assert(myBufferIndex == index+1);
00759 }
00760
00761 readChar = ifgetc();
00762 assert(readChar == '1');
00763
00764 assert(myCurrentBufferSize == 5);
00765 assert(myBufferIndex == 1);
00766 readChar = ifgetc();
00767 assert(readChar == '2');
00768
00769 assert(myCurrentBufferSize == 5);
00770 assert(myBufferIndex == 2);
00771 readChar = ifgetc();
00772 assert(readChar == '3');
00773
00774 assert(myCurrentBufferSize == 5);
00775 assert(myBufferIndex == 3);
00776 readChar = ifgetc();
00777 assert(readChar == '4');
00778
00779 assert(myCurrentBufferSize == 5);
00780 assert(myBufferIndex == 4);
00781 readChar = ifgetc();
00782 assert(readChar == '5');
00783
00784 assert(myCurrentBufferSize == 5);
00785 assert(myBufferIndex == 5);
00786
00787
00788 readChar = ifgetc();
00789 assert(readChar == EOF);
00790 assert(myCurrentBufferSize == 0);
00791 assert(myBufferIndex == 0);
00792
00793
00794
00795 readChar = ifgetc();
00796 assert(readChar == EOF);
00797 assert(myCurrentBufferSize == 0);
00798 assert(myBufferIndex == 0);
00799
00800
00801 ifrewind();
00802 totalBytesPreviouslyRead = 0;
00803
00804
00805
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
00814 assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE);
00815 assert(myBufferIndex == 4);
00816
00817 assert(ifeof() == false);
00818
00819
00820 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00821 assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
00822
00823 for(int i = 0; i < (numBytesRead-5); i++)
00824 {
00825 assert(largeBuffer[i] == '0');
00826 }
00827
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
00836 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00837 assert(numBytesRead == 0);
00838
00839 assert(myCurrentBufferSize == 0);
00840 assert(myBufferIndex == 0);
00841 assert(ifeof() == true);
00842
00843
00844 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00845 assert(numBytesRead == 0);
00846
00847 assert(myCurrentBufferSize == 0);
00848 assert(myBufferIndex == 0);
00849 assert(ifeof() == true);
00850
00851
00852 ifrewind();
00853 totalBytesPreviouslyRead = 0;
00854
00855
00856
00857 numBytesRead = ifread(largeBuffer, 2);
00858 assert(largeBuffer[0] == '0');
00859 assert(largeBuffer[1] == '0');
00860 assert(numBytesRead == 2);
00861 totalBytesPreviouslyRead += numBytesRead;
00862
00863 assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE);
00864 assert(myBufferIndex == 2);
00865
00866 assert(ifeof() == false);
00867
00868
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
00883 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00884 assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
00885
00886
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
00900 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00901 assert(numBytesRead == 0);
00902
00903 assert(myCurrentBufferSize == 0);
00904 assert(myBufferIndex == 0);
00905 assert(ifeof() == true);
00906
00907
00908 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00909 assert(numBytesRead == 0);
00910
00911 assert(myCurrentBufferSize == 0);
00912 assert(myBufferIndex == 0);
00913 assert(ifeof() == true);
00914
00915
00916 ifrewind();
00917 totalBytesPreviouslyRead = 0;
00918 assert(myCurrentBufferSize == 0);
00919 assert(myBufferIndex == 0);
00920
00921
00922
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
00937 numBytesRead = ifread(myTestBuffer, 2);
00938 assert(myTestBuffer[0] == '0');
00939 assert(myTestBuffer[1] == '0');
00940 assert(numBytesRead == 2);
00941 totalBytesPreviouslyRead += numBytesRead;
00942
00943 assert(myCurrentBufferSize == bufferSize);
00944 assert(myBufferIndex == totalBytesPreviouslyRead);
00945
00946 assert(ifeof() == false);
00947
00948
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
00962 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00963 assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
00964
00965 for(int i = 0; i < numBytesRead - 5; i++)
00966 {
00967 assert(largeBuffer[i] == '0');
00968 }
00969
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
00981 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00982 assert(numBytesRead == 0);
00983
00984 assert(myCurrentBufferSize == 0);
00985 assert(myBufferIndex == 0);
00986 assert(ifeof() == true);
00987
00988
00989 numBytesRead = ifread(largeBuffer, largeTestFileSize);
00990 assert(numBytesRead == 0);
00991
00992 assert(myCurrentBufferSize == 0);
00993 assert(myBufferIndex == 0);
00994 assert(ifeof() == true);
00995
00996
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
01009 void IFILE_Test::test_ifclose(const char* extension)
01010 {
01011
01012 openFile(extension);
01013
01014
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
01032 openFile(extension);
01033
01034
01035 int numBytesRead = readFromFile(myTestBuffer, 1);
01036 assert(numBytesRead == 1);
01037 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
01038
01039
01040 long int currentPos = iftell();
01041
01042
01043 numBytesRead = readFromFile(myTestBuffer, 1);
01044 assert(numBytesRead == 1);
01045 assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]);
01046
01047
01048
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
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
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
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
01173
01174 }
01175
01176
01177 #endif