1 /*
2  *  Copyright (C) 2010  Regents of the University of Michigan
3  *
4  *   This program is free software: you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License as published by
6  *   the Free Software Foundation, either version 3 of the License, or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details.
13  *
14  *   You should have received a copy of the GNU General Public License
15  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "InputFileTest.h"
18 #include <assert.h>
19 #include <iostream>
20 #include "StringBasics.h"
21 
22 void testAdditional(const char *extension);
23 void testWrite();
24 
25 
main(int argc,char ** argv)26 int main(int argc, char ** argv)
27 {
28    IFILE_Test myFile;
29 
30    myFile.test();
31 
32    testWrite();
33 
34    std::cout << "\nAdditional Tests: " << std::endl;
35 
36    testAdditional("txt");
37 #ifdef __ZLIB_AVAILABLE__
38    testAdditional("gz");
39 #endif
40 }
41 
42 
43 const int IFILE_Test::TEST_FILE_SIZE = 37;
44 const int IFILE_Test::BGZF_TEST_FILE_SIZE = 93;
45 const std::string IFILE_Test::TEST_FILE_CONTENTS = "ABCDabcd1234\nEFGefg567\nhijklHIJKL8910";
46 
test()47 void IFILE_Test::test()
48 {
49    std::cout << "\nUncompressedFileType Tests:" << std::endl;
50    testAll("txt");
51 
52 #ifdef __ZLIB_AVAILABLE__
53    std::cout << "\nGzipFileType Tests:" << std::endl;
54    testAll("gz");
55 
56    std::cout << "\nBgzfFileType Tests:" << std::endl;
57    testAll("bam");
58 
59    std::cout << "\n.glf file Tests:" << std::endl;
60    testAll("glf");
61 #endif
62 }
63 
64 
testAll(const char * extension)65 void IFILE_Test::testAll(const char* extension)
66 {
67     test_readFromFile(extension);
68     test_readTilChar(extension);
69     test_ifeof_ifrewind(extension);
70     test_ifread_ifgetc(extension);
71     test_ifclose(extension);
72     test_ifseek(extension);
73     test_noExistRead(extension);
74 }
75 
76 
test_readFromFile(const char * extension)77 void IFILE_Test::test_readFromFile(const char* extension)
78 {
79    // First open the test file.
80    openFile(extension);
81 
82    // Verify the file successfully opened.
83    assert(myFileTypePtr != NULL);
84    assert(isOpen());
85    assert(myFileTypePtr->isOpen());
86 
87    // Track how many bytes are read by each call.
88    int numBytesRead = 0;
89 
90    // Track the total number of the bytes that have been read from the file
91    // at any given point.
92    int totalBytesPreviouslyRead = 0;
93 
94    // Test readFromFile.
95    numBytesRead = readFromFile(myTestBuffer, 4);
96    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
97    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]);
98    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
99    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
100    assert(numBytesRead == 4);
101    totalBytesPreviouslyRead += numBytesRead;
102    // This read should not have affected the internal buffer.
103    assert(myCurrentBufferSize == 0);
104    assert(myBufferIndex == 0);
105    // Should not be at eof
106    assert(myFileTypePtr->eof() == false);
107    assert(ifeof() == false);
108 
109    // Read again to verify that the next characters could be read.
110    numBytesRead = readFromFile(myTestBuffer, 2);
111    // Read 2 more characters from the test file.
112    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[4]);
113    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[5]);
114    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
115    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
116    assert(numBytesRead == 2);
117    totalBytesPreviouslyRead += numBytesRead;
118    // This read should not have affected the internal buffer.
119    assert(myCurrentBufferSize == 0);
120    assert(myBufferIndex == 0);
121    // Should not be at eof
122    assert(myFileTypePtr->eof() == false);
123    assert(ifeof() == false);
124 
125    // Read the rest of the file.
126    // Determine expected results for reading the rest of the file by
127    // taking the substring starting after what had been previously read.
128    numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE);
129    // Read the rest of the file, so the number of bytes read is
130    // what was left in the file.
131    assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead));
132    assert(numBytesRead != MAX_TEST_BUFFER_SIZE);
133    for(int i = 0; i < numBytesRead; i++)
134    {
135       assert(myTestBuffer[i] ==
136 	     TEST_FILE_CONTENTS[totalBytesPreviouslyRead+i]);
137    }
138    totalBytesPreviouslyRead += numBytesRead;
139    assert(myFileTypePtr->eof() != 0);
140    assert(ifeof() != 0);
141 
142    // Try to read one more time, making sure it doesn't read anything.
143     numBytesRead = readFromFile(myTestBuffer, MAX_TEST_BUFFER_SIZE);
144     assert(numBytesRead == 0);
145    // Should be at eof
146    assert(myFileTypePtr->eof() != 0);
147    assert(ifeof() != 0);
148 
149    ifclose();
150 
151    std::cout << "  Passed test_readFromFile" << std::endl;
152 }
153 
154 
155 
156 
test_readTilChar(const char * extension)157 void IFILE_Test::test_readTilChar(const char* extension)
158 {
159    // First open the test file.
160    openFile(extension);
161 
162    // Verify the file successfully opened.
163    assert(myFileTypePtr != NULL);
164    assert(isOpen());
165    assert(myFileTypePtr->isOpen());
166 
167    // Track position of ending char found.
168    int pos = 0;
169 
170    // Test readTilChar.
171    std::string output = "";
172    std::string endChars = "a5d";
173    pos = readTilChar(endChars, output);
174    assert(pos == 0);  // read til a
175    assert(output == "ABCD");
176    output.clear();
177    pos = readTilChar(endChars, output);
178    assert(pos == 2);  // read til d
179    assert(output == "bc");
180    pos = readTilChar(endChars, output);
181    assert(pos == 1);  // read til 5
182    assert(output == "bc1234\nEFGefg");
183    output.clear();
184    pos = readTilChar(endChars, output);
185    assert(pos == -1);  // read til 5
186    assert(output == "67\nhijklHIJKL8910");
187 
188    ifrewind();
189    // Test readTilChar.
190    pos = readTilChar(endChars);
191    assert(pos == 0);  // read til a
192    pos = readTilChar(endChars);
193    assert(pos == 2);  // read til d
194    pos = readTilChar(endChars);
195    assert(pos == 1);  // read til 5
196    pos = readTilChar(endChars);
197    assert(pos == -1);  // read til 5
198 
199    ifclose();
200 
201    std::cout << "  Passed test_readTilChar" << std::endl;
202 }
203 
204 
test_ifeof_ifrewind(const char * extension)205 void IFILE_Test::test_ifeof_ifrewind(const char* extension)
206 {
207    // First open the test file.
208    openFile(extension);
209 
210    // Verify the file successfully opened.
211    assert(myFileTypePtr != NULL);
212    assert(isOpen());
213    assert(myFileTypePtr->isOpen());
214 
215    // Not at eof - verify that it reports not eof.
216    assert(ifeof() == false);
217 
218    // Track the total number of the bytes that have been read from the file
219    // at any given point.
220    int totalBytesPreviouslyRead = 0;
221    int numBytesRead = 0;
222 
223    //////////////////////////////////////////////////////////////
224    // Test doing reads from file without IFILE internal buffering.
225    disableBuffering();
226 
227    // Verify position in file.
228    assert(iftell() == 0);
229 
230    // Read a character from the file.
231    numBytesRead = readFromFile(myTestBuffer, 1);
232    assert(numBytesRead == 1);
233    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
234    // Now that we have tested based on the previous total bytes read,
235    // increment the count.
236    totalBytesPreviouslyRead += numBytesRead;
237    // Not at eof
238    assert(ifeof() == false);
239 
240    // Perform char read.
241    char readChar = ifgetc();
242    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
243    // Now that we have tested based on the previous total bytes read,
244    // increment the count.
245    ++totalBytesPreviouslyRead;
246    // Not at eof
247    assert(ifeof() == false);
248    assert(iftell() == totalBytesPreviouslyRead);
249 
250    // Now read the rest.
251    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
252    assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead));
253    // Hit the end of the file before reading the entire requested size.
254    assert(numBytesRead != MAX_TEST_BUFFER_SIZE);
255    // Now that we have tested based on the previous total bytes read,
256    // increment the count.
257    totalBytesPreviouslyRead += numBytesRead;
258 
259    assert(myFileTypePtr->eof() != 0);
260    assert(ifeof() != 0);
261 
262    numBytesRead = readFromFile(myTestBuffer, 1);
263    assert(numBytesRead == 0);
264    // Now it registers eof
265    assert(ifeof() != 0);
266 
267    // bgzf files use a specialized return value for iftell that
268    // is not just straight file offset.
269    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
270    {
271        assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
272    }
273    else
274    {
275       assert(iftell() == TEST_FILE_SIZE);
276    }
277 
278    ///////////////////////////////////
279    // Test doing IFILE buffered reads.
280    // rewind the file and verify that it no longer registers eof.
281    ifrewind();
282    totalBytesPreviouslyRead = 0;
283    // No longer at eof
284    assert(ifeof() == false);
285    // Verify position in file.
286    assert(iftell() == 0);
287 
288    // Buffer reads - may have been disabled for iftell to work for bgzf.
289    bufferReads();
290 
291    // Read a character from the file.
292    numBytesRead = readFromFile(myTestBuffer, 1);
293    assert(numBytesRead == 1);
294    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
295    // Now that we have tested based on the previous total bytes read,
296    // increment the count.
297    totalBytesPreviouslyRead += numBytesRead;
298    // Not at eof
299    assert(ifeof() == false);
300 
301    // Perform char read.
302    readChar = ifgetc();
303    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
304    // Now that we have tested based on the previous total bytes read,
305    // increment the count.
306    ++totalBytesPreviouslyRead;
307    // Not at eof
308    assert(ifeof() == false);
309 
310    // bgzf files use a specialized return value for iftell that
311    // is not just straight file offset.
312    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
313    {
314        bool caught = false;
315        try
316        {
317            assert(iftell() == totalBytesPreviouslyRead);
318        }
319        catch (std::exception& e)
320        {
321            caught = true;
322            assert(strcmp(e.what(), "IFILE: CANNOT use buffered reads and tell for BGZF files") == 0);
323        }
324        assert(caught);
325    }
326    else
327    {
328       assert(iftell() == totalBytesPreviouslyRead);
329    }
330 
331    // Now read the rest.
332    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
333    assert(numBytesRead == (TEST_FILE_SIZE - totalBytesPreviouslyRead));
334    // Now that we have tested based on the previous total bytes read,
335    // increment the count.
336    totalBytesPreviouslyRead += numBytesRead;
337    // Registers eof.
338    assert(ifeof() != 0);
339 
340    // Read past eof.
341    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
342    assert(numBytesRead == 0);
343    // Eof.
344    assert(ifeof() != 0);
345 
346    // bgzf files use a specialized return value for iftell that
347    // is not just straight file offset.
348    if((strcmp(extension, "bam") == 0) || (strcmp(extension, "glf") == 0))
349    {
350        bool caught = false;
351        try
352        {
353            assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
354        }
355        catch (std::exception& e)
356        {
357            caught = true;
358            assert(strcmp(e.what(), "IFILE: CANNOT use buffered reads and tell for BGZF files") == 0);
359        }
360        assert(caught);
361        disableBuffering();
362        assert(iftell() == (BGZF_TEST_FILE_SIZE << 16));
363    }
364    else
365    {
366       assert(iftell() == TEST_FILE_SIZE);
367    }
368 
369    // Verify that after rewind, eof is no longer registered.
370    ifrewind();
371   // reset since we are back to the beginning of the file.
372    totalBytesPreviouslyRead = 0;
373    // No longer at eof
374    assert(ifeof() == false);
375    // Verify position in file.
376    assert(iftell() == 0);
377 
378    // Verify properly works even if already at the beginning.
379    ifrewind();
380   // reset since we are back to the beginning of the file.
381    totalBytesPreviouslyRead = 0;
382    // Not eof
383    assert(ifeof() == false);
384    // Verify position in file.
385    assert(iftell() == 0);
386 
387    // Buffer reads - may have been disabled for iftell to work for bgzf.
388    bufferReads();
389 
390    //////////////////////
391    // Close the test file.
392    ifclose();
393 
394    std::cout << "  Passed test_ifeof_ifrewind" << std::endl;
395 }
396 
397 
test_ifread_ifgetc(const char * extension)398 void IFILE_Test::test_ifread_ifgetc(const char* extension)
399 {
400    // First open the test file.
401    openFile(extension);
402 
403    // Verify the file successfully opened.
404    assert(myFileTypePtr != NULL);
405    assert(isOpen());
406    assert(myFileTypePtr->isOpen());
407 
408    int numBytesRead = 0;
409    int totalBytesPreviouslyRead = 0;
410 
411    ////////////////////////////////////
412    // Test reading entire file at once.
413    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
414    assert(numBytesRead == TEST_FILE_SIZE);
415 
416    for(int i = 0; i < TEST_FILE_SIZE; i++)
417    {
418       assert(myTestBuffer[i] == TEST_FILE_CONTENTS[i]);
419    }
420    totalBytesPreviouslyRead += numBytesRead;
421 
422    // Should affect the IFILE buffer
423    assert(myCurrentBufferSize == TEST_FILE_SIZE);
424    assert(myBufferIndex == TEST_FILE_SIZE);
425 
426    assert(myFileTypePtr->eof() != 0);
427    assert(ifeof() != 0);
428 
429    // Try reading at end of file twice.
430    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
431    assert(numBytesRead == 0);
432    // Should affect the IFILE buffer
433    assert(myCurrentBufferSize == 0);
434    assert(myBufferIndex == 0);
435    assert(ifeof() != 0);
436 
437    // 2nd read attempt at eof.
438    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
439    assert(numBytesRead == 0);
440    // Should affect the IFILE buffer
441    assert(myCurrentBufferSize == 0);
442    assert(myBufferIndex == 0);
443    assert(ifeof() != 0);
444 
445 
446    // RESET
447    ifrewind();
448    totalBytesPreviouslyRead = 0;
449 
450    //////////////////////////////////////////
451    // Test reading entire file using getc.
452    // Loop through reading the file.
453    char readChar;
454    for(int index = 0; index < TEST_FILE_SIZE; index++)
455    {
456       // Read a character.
457       readChar = ifgetc();
458       assert(readChar == TEST_FILE_CONTENTS[index]);
459       // Should affect the IFILE buffer
460       assert(myCurrentBufferSize == TEST_FILE_SIZE);
461       assert(myBufferIndex == index+1);
462    }
463 
464    // Now that we have read the file, try reading again at eof.
465    readChar = ifgetc();
466    assert(readChar == EOF);
467    assert(myCurrentBufferSize == 0);
468    assert(myBufferIndex == 0);
469 
470    // Try again at eof.
471    // Now that we have read the file, try reading again at eof.
472    readChar = ifgetc();
473    assert(readChar == EOF);
474    assert(myCurrentBufferSize == 0);
475    assert(myBufferIndex == 0);
476 
477    // RESET
478    ifrewind();
479    totalBytesPreviouslyRead = 0;
480 
481    ////////////////////////////////////////////////
482    // Test reading just the beginning of the file.
483    numBytesRead = ifread(myTestBuffer, 4);
484    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
485    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]);
486    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
487    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
488    assert(numBytesRead == 4);
489    totalBytesPreviouslyRead += numBytesRead;
490    // This read should have affected the internal buffer.
491    assert(myCurrentBufferSize == TEST_FILE_SIZE);
492    assert(myBufferIndex == 4);
493    // Should not be at eof
494    assert(ifeof() == false);
495 
496    // Test reading rest of file.
497    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
498    assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
499    // Verify contents of what read.
500     for(int i = 0; i < numBytesRead; i++)
501     {
502        assert(myTestBuffer[i] ==
503 	      TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
504     }
505     totalBytesPreviouslyRead += numBytesRead;
506 
507    // Try at end of file twice.
508    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
509    assert(numBytesRead == 0);
510    // Should affect the IFILE buffer
511    assert(myCurrentBufferSize == 0);
512    assert(myBufferIndex == 0);
513    assert(ifeof() != 0);
514 
515    // 2nd read attempt at eof.
516    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
517    assert(numBytesRead == 0);
518    // Should affect the IFILE buffer
519    assert(myCurrentBufferSize == 0);
520    assert(myBufferIndex == 0);
521    assert(ifeof() != 0);
522 
523     // RESET
524    ifrewind();
525    totalBytesPreviouslyRead = 0;
526 
527    //////////////////////////////////////
528    // Test reading just the beginning.
529    numBytesRead = ifread(myTestBuffer, 4);
530    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
531    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[1]);
532    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[2]);
533    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[3]);
534    assert(numBytesRead == 4);
535    totalBytesPreviouslyRead += numBytesRead;
536    // This read should have affected the internal buffer.
537    assert(myCurrentBufferSize == TEST_FILE_SIZE);
538    assert(myBufferIndex == 4);
539    // Should not be at eof
540    assert(ifeof() == false);
541 
542    // Test doing 2 getc.
543    readChar = ifgetc();
544    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
545    int bufferSize = TEST_FILE_SIZE;
546    assert(myCurrentBufferSize == bufferSize);
547    assert(myBufferIndex == 5);
548    totalBytesPreviouslyRead++;
549 
550    readChar = ifgetc();
551    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
552    assert(myCurrentBufferSize == bufferSize);
553    assert(myBufferIndex == 6);
554    totalBytesPreviouslyRead++;
555 
556    // Test reading rest of file.
557    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
558    assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
559    // Verify contents of what read.
560     for(int i = 0; i < numBytesRead; i++)
561     {
562        assert(myTestBuffer[i] ==
563 	      TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
564     }
565     totalBytesPreviouslyRead += numBytesRead;
566 
567    // Try at end of file twice.
568    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
569    assert(numBytesRead == 0);
570    // Should affect the IFILE buffer
571    assert(myCurrentBufferSize == 0);
572    assert(myBufferIndex == 0);
573    assert(ifeof() != 0);
574 
575    // 2nd read attempt at eof.
576    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
577    assert(numBytesRead == 0);
578    // Should affect the IFILE buffer
579    assert(myCurrentBufferSize == 0);
580    assert(myBufferIndex == 0);
581    assert(ifeof() != 0);
582 
583     // RESET
584    ifrewind();
585    totalBytesPreviouslyRead = 0;
586    assert(myCurrentBufferSize == 0);
587    assert(myBufferIndex == 0);
588 
589    //////////////////////////////////
590    // Start with 2 getc.
591    readChar = ifgetc();
592    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
593    bufferSize = TEST_FILE_SIZE;
594    assert(myCurrentBufferSize == bufferSize);
595    assert(myBufferIndex == 1);
596    totalBytesPreviouslyRead++;
597 
598    readChar = ifgetc();
599    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
600    assert(myCurrentBufferSize == bufferSize);
601    assert(myBufferIndex == 2);
602    totalBytesPreviouslyRead++;
603 
604    // Test reading part of the rest of the file.
605    numBytesRead = ifread(myTestBuffer, 4);
606    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
607    assert(myTestBuffer[1] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 1]);
608    assert(myTestBuffer[2] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 2]);
609    assert(myTestBuffer[3] == TEST_FILE_CONTENTS[totalBytesPreviouslyRead + 3]);
610    assert(numBytesRead == 4);
611    totalBytesPreviouslyRead += numBytesRead;
612    // This read should have affected the internal buffer.
613    assert(myCurrentBufferSize == bufferSize);
614    assert(myBufferIndex == totalBytesPreviouslyRead);
615    // Should not be at eof
616    assert(ifeof() == false);
617 
618    // Test reading 2 char with getc.
619    readChar = ifgetc();
620    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
621    assert(myCurrentBufferSize == bufferSize);
622    totalBytesPreviouslyRead++;
623    assert(myBufferIndex == totalBytesPreviouslyRead);
624 
625    readChar = ifgetc();
626    assert(readChar == TEST_FILE_CONTENTS[totalBytesPreviouslyRead]);
627    assert(myCurrentBufferSize == bufferSize);
628    totalBytesPreviouslyRead++;
629    assert(myBufferIndex == totalBytesPreviouslyRead);
630 
631    // Test reading rest of file.
632    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
633    assert(numBytesRead == (TEST_FILE_SIZE - (int)totalBytesPreviouslyRead));
634    // Verify contents of what read.
635     for(int i = 0; i < numBytesRead; i++)
636     {
637        assert(myTestBuffer[i] ==
638 	      TEST_FILE_CONTENTS[i + totalBytesPreviouslyRead]);
639     }
640     totalBytesPreviouslyRead += numBytesRead;
641    assert(myBufferIndex == 0);
642    assert(myCurrentBufferSize == 0);
643 
644    // Try at end of file twice.
645    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
646    assert(numBytesRead == 0);
647    // Should affect the IFILE buffer
648    assert(myCurrentBufferSize == 0);
649    assert(myBufferIndex == 0);
650    assert(ifeof() != 0);
651 
652    // 2nd read attempt at eof.
653    numBytesRead = ifread(myTestBuffer, MAX_TEST_BUFFER_SIZE);
654    assert(numBytesRead == 0);
655    // Should affect the IFILE buffer
656    assert(myCurrentBufferSize == 0);
657    assert(myBufferIndex == 0);
658    assert(ifeof() != 0);
659 
660     // RESET
661    ifrewind();
662    totalBytesPreviouslyRead = 0;
663    assert(myCurrentBufferSize == 0);
664    assert(myBufferIndex == 0);
665 
666    //////////////
667    // Close the test file.
668    ifclose();
669 
670    ////////////////////////////////////////////////////////////////////////
671    // Repeat the test on a test file that is larger than the IFILE
672    // buffer size.
673 
674    // First open the test file.
675    openLargeFile(extension);
676 
677    // This file contains DEFAULT_BUFFER_SIZE of '0's followed by "12345"
678    // The size of the file is DEFAULT_BUFFER_SIZE + 5.
679    int largeTestFileSize = DEFAULT_BUFFER_SIZE + 5;
680    char largeBuffer[largeTestFileSize + 5];
681 
682    // Verify the file successfully opened.
683    assert(myFileTypePtr != NULL);
684    assert(isOpen());
685    assert(myFileTypePtr->isOpen());
686 
687    numBytesRead = 0;
688    totalBytesPreviouslyRead = 0;
689 
690    ////////////////////////////////////
691    // Test reading part of the file, then more then the buffer size,
692    // then the rest of the file (test buffer handling when read
693    // available and directly into the file, then read more).
694    numBytesRead = ifread(largeBuffer, 2);
695    assert(numBytesRead == 2);
696    numBytesRead = ifread(largeBuffer + 2, DEFAULT_BUFFER_SIZE * 3);
697    assert(numBytesRead == DEFAULT_BUFFER_SIZE + 3);
698    // Should be at the end of the file.
699    assert(myFileTypePtr->eof() != 0);
700    assert(ifeof() != 0);
701    numBytesRead = ifread(largeBuffer + DEFAULT_BUFFER_SIZE + 3, 2);
702    assert(numBytesRead == 0);
703 
704    // Validate all the 0s
705    for(unsigned int i = 0; i < DEFAULT_BUFFER_SIZE; i++)
706    {
707       assert(largeBuffer[i] == '0');
708    }
709    // Now validate the "12345"
710    assert(largeBuffer[DEFAULT_BUFFER_SIZE] == '1');
711    assert(largeBuffer[DEFAULT_BUFFER_SIZE+1] == '2');
712    assert(largeBuffer[DEFAULT_BUFFER_SIZE+2] == '3');
713    assert(largeBuffer[DEFAULT_BUFFER_SIZE+3] == '4');
714    assert(largeBuffer[DEFAULT_BUFFER_SIZE+4] == '5');
715 
716    totalBytesPreviouslyRead += numBytesRead;
717 
718    // Should affect the IFILE buffer - 0 because read
719    // is bigger than the buffer, so just read directly
720    // into the largeBuffer.
721    assert(myCurrentBufferSize == 0);
722    assert(myBufferIndex == 0);
723 
724    assert(myFileTypePtr->eof() != 0);
725    assert(ifeof() != 0);
726 
727    // Try reading at end of file twice.
728    numBytesRead = ifread(largeBuffer, largeTestFileSize);
729    assert(numBytesRead == 0);
730    // Should affect the IFILE buffer
731    assert(myCurrentBufferSize == 0);
732    assert(myBufferIndex == 0);
733    assert(ifeof() != 0);
734 
735    // 2nd read attempt at eof.
736    numBytesRead = ifread(largeBuffer, largeTestFileSize);
737    assert(numBytesRead == 0);
738    // Should affect the IFILE buffer
739    assert(myCurrentBufferSize == 0);
740    assert(myBufferIndex == 0);
741    assert(ifeof() != 0);
742 
743 
744    // RESET
745    ifrewind();
746    totalBytesPreviouslyRead = 0;
747 
748    ////////////////////////////////////
749    // Test reading entire file at once.
750    numBytesRead = ifread(largeBuffer, largeTestFileSize + 4);
751    assert(numBytesRead == largeTestFileSize);
752 
753    // Validate all the 0s
754    for(unsigned int i = 0; i < DEFAULT_BUFFER_SIZE; i++)
755    {
756       assert(largeBuffer[i] == '0');
757    }
758    // Now validate the "12345"
759    assert(largeBuffer[DEFAULT_BUFFER_SIZE] == '1');
760    assert(largeBuffer[DEFAULT_BUFFER_SIZE+1] == '2');
761    assert(largeBuffer[DEFAULT_BUFFER_SIZE+2] == '3');
762    assert(largeBuffer[DEFAULT_BUFFER_SIZE+3] == '4');
763    assert(largeBuffer[DEFAULT_BUFFER_SIZE+4] == '5');
764 
765    totalBytesPreviouslyRead += numBytesRead;
766 
767    // Should affect the IFILE buffer - 0 because read
768    // is bigger than the buffer, so just read directly
769    // into the largeBuffer.
770    assert(myCurrentBufferSize == 0);
771    assert(myBufferIndex == 0);
772 
773    assert(myFileTypePtr->eof() != 0);
774    assert(ifeof() != 0);
775 
776    // Try reading at end of file twice.
777    numBytesRead = ifread(largeBuffer, largeTestFileSize);
778    assert(numBytesRead == 0);
779    // Should affect the IFILE buffer
780    assert(myCurrentBufferSize == 0);
781    assert(myBufferIndex == 0);
782    assert(ifeof() != 0);
783 
784    // 2nd read attempt at eof.
785    numBytesRead = ifread(largeBuffer, largeTestFileSize);
786    assert(numBytesRead == 0);
787    // Should affect the IFILE buffer
788    assert(myCurrentBufferSize == 0);
789    assert(myBufferIndex == 0);
790    assert(ifeof() != 0);
791 
792 
793    // RESET
794    ifrewind();
795    totalBytesPreviouslyRead = 0;
796 
797    //////////////////////////////////////////
798    // Test reading entire file using getc.
799    // Loop through reading the file.
800    // First loop through verifying the 0's
801    for(int index = 0; index < (int)DEFAULT_BUFFER_SIZE; index++)
802    {
803       // Read a character.
804       readChar = ifgetc();
805       assert(readChar == '0');
806       // Should affect the IFILE buffer
807       assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE);
808       assert(myBufferIndex == index+1);
809    }
810    // Now read the 12345.
811    readChar = ifgetc();
812    assert(readChar == '1');
813    // Should affect the IFILE buffer
814    assert(myCurrentBufferSize == 5);
815    assert(myBufferIndex == 1);
816    readChar = ifgetc();
817    assert(readChar == '2');
818    // Should affect the IFILE buffer
819    assert(myCurrentBufferSize == 5);
820    assert(myBufferIndex == 2);
821    readChar = ifgetc();
822    assert(readChar == '3');
823    // Should affect the IFILE buffer
824    assert(myCurrentBufferSize == 5);
825    assert(myBufferIndex == 3);
826    readChar = ifgetc();
827    assert(readChar == '4');
828    // Should affect the IFILE buffer
829    assert(myCurrentBufferSize == 5);
830    assert(myBufferIndex == 4);
831    readChar = ifgetc();
832    assert(readChar == '5');
833    // Should affect the IFILE buffer
834    assert(myCurrentBufferSize == 5);
835    assert(myBufferIndex == 5);
836 
837    // Now that we have read the file, try reading again at eof.
838    readChar = ifgetc();
839    assert(readChar == EOF);
840    assert(myCurrentBufferSize == 0);
841    assert(myBufferIndex == 0);
842 
843    // Try again at eof.
844    // Now that we have read the file, try reading again at eof.
845    readChar = ifgetc();
846    assert(readChar == EOF);
847    assert(myCurrentBufferSize == 0);
848    assert(myBufferIndex == 0);
849 
850    // RESET
851    ifrewind();
852    totalBytesPreviouslyRead = 0;
853 
854    ////////////////////////////////////////////////
855    // Test reading just the beginning of the file.
856    numBytesRead = ifread(largeBuffer, 4);
857    assert(largeBuffer[0] == '0');
858    assert(largeBuffer[1] == '0');
859    assert(largeBuffer[2] == '0');
860    assert(largeBuffer[3] == '0');
861    assert(numBytesRead == 4);
862    totalBytesPreviouslyRead += numBytesRead;
863    // This read should have affected the internal buffer.
864    assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE);
865    assert(myBufferIndex == 4);
866    // Should not be at eof
867    assert(ifeof() == false);
868 
869    // Test reading rest of file.
870    numBytesRead = ifread(largeBuffer, largeTestFileSize);
871    assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
872    // Verify contents of what read.   First check the 0's
873    for(int i = 0; i < (numBytesRead-5); i++)
874    {
875       assert(largeBuffer[i] == '0');
876    }
877    // Check the 12345
878    assert(largeBuffer[numBytesRead - 5] == '1');
879    assert(largeBuffer[numBytesRead - 5 + 1] == '2');
880    assert(largeBuffer[numBytesRead - 5 + 2] == '3');
881    assert(largeBuffer[numBytesRead - 5 + 3] == '4');
882    assert(largeBuffer[numBytesRead - 5 + 4] == '5');
883    totalBytesPreviouslyRead += numBytesRead;
884 
885    // Try at end of file twice.
886    numBytesRead = ifread(largeBuffer, largeTestFileSize);
887    assert(numBytesRead == 0);
888    // Trying to read at the end cleared the buffer..
889    assert(myCurrentBufferSize == 0);
890    assert(myBufferIndex == 0);
891    assert(ifeof() != 0);
892 
893    // 2nd read attempt at eof.
894    numBytesRead = ifread(largeBuffer, largeTestFileSize);
895    assert(numBytesRead == 0);
896    // Should affect the IFILE buffer
897    assert(myCurrentBufferSize == 0);
898    assert(myBufferIndex == 0);
899    assert(ifeof() != 0);
900 
901     // RESET
902    ifrewind();
903    totalBytesPreviouslyRead = 0;
904 
905    //////////////////////////////////////
906    // Test reading just the beginning.
907    numBytesRead = ifread(largeBuffer, 2);
908    assert(largeBuffer[0] == '0');
909    assert(largeBuffer[1] == '0');
910    assert(numBytesRead == 2);
911    totalBytesPreviouslyRead += numBytesRead;
912    // This read should have affected the internal buffer.
913    assert(myCurrentBufferSize == (int)DEFAULT_BUFFER_SIZE);
914    assert(myBufferIndex == 2);
915    // Should not be at eof
916    assert(ifeof() == false);
917 
918    // Test doing 2 getc.
919    readChar = ifgetc();
920    assert(readChar == '0');
921    bufferSize = DEFAULT_BUFFER_SIZE;
922    assert(myCurrentBufferSize == bufferSize);
923    assert(myBufferIndex == 3);
924    totalBytesPreviouslyRead++;
925 
926    readChar = ifgetc();
927    assert(readChar == '0');
928    assert(myCurrentBufferSize == bufferSize);
929    assert(myBufferIndex == 4);
930    totalBytesPreviouslyRead++;
931 
932    // Test reading rest of file.
933    numBytesRead = ifread(largeBuffer, largeTestFileSize);
934    assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
935    // Verify contents of what read.
936    // All except the last 5 should be '0'
937     for(int i = 0; i < numBytesRead - 5; i++)
938     {
939        assert(largeBuffer[i] == '0');
940     }
941     assert(largeBuffer[numBytesRead - 5] == '1');
942     assert(largeBuffer[numBytesRead - 4] == '2');
943     assert(largeBuffer[numBytesRead - 3] == '3');
944     assert(largeBuffer[numBytesRead - 2] == '4');
945     assert(largeBuffer[numBytesRead - 1] == '5');
946 
947     totalBytesPreviouslyRead += numBytesRead;
948 
949    // Try at end of file twice.
950    numBytesRead = ifread(largeBuffer, largeTestFileSize);
951    assert(numBytesRead == 0);
952    // Reading at the end clears the buffer
953    assert(myCurrentBufferSize == 0);
954    assert(myBufferIndex == 0);
955    assert(ifeof() != 0);
956 
957    // 2nd read attempt at eof.
958    numBytesRead = ifread(largeBuffer, largeTestFileSize);
959    assert(numBytesRead == 0);
960    // Reading at the end clears the buffer
961    assert(myCurrentBufferSize == 0);
962    assert(myBufferIndex == 0);
963    assert(ifeof() != 0);
964 
965     // RESET
966    ifrewind();
967    totalBytesPreviouslyRead = 0;
968    assert(myCurrentBufferSize == 0);
969    assert(myBufferIndex == 0);
970 
971    //////////////////////////////////
972    // Start with 2 getc.
973    readChar = ifgetc();
974    assert(readChar == '0');
975    bufferSize = DEFAULT_BUFFER_SIZE;
976    assert(myCurrentBufferSize == bufferSize);
977    assert(myBufferIndex == 1);
978    totalBytesPreviouslyRead++;
979 
980    readChar = ifgetc();
981    assert(readChar == '0');
982    assert(myCurrentBufferSize == bufferSize);
983    assert(myBufferIndex == 2);
984    totalBytesPreviouslyRead++;
985 
986    // Test reading part of the rest of the file.
987    numBytesRead = ifread(myTestBuffer, 2);
988    assert(myTestBuffer[0] == '0');
989    assert(myTestBuffer[1] == '0');
990    assert(numBytesRead == 2);
991    totalBytesPreviouslyRead += numBytesRead;
992    // This read should have affected the internal buffer.
993    assert(myCurrentBufferSize == bufferSize);
994    assert(myBufferIndex == totalBytesPreviouslyRead);
995    // Should not be at eof
996    assert(ifeof() == false);
997 
998    // Test reading 2 char with getc.
999    readChar = ifgetc();
1000    assert(readChar == '0');
1001    assert(myCurrentBufferSize == bufferSize);
1002    totalBytesPreviouslyRead++;
1003    assert(myBufferIndex == totalBytesPreviouslyRead);
1004 
1005    readChar = ifgetc();
1006    assert(readChar == '0');
1007    assert(myCurrentBufferSize == bufferSize);
1008    totalBytesPreviouslyRead++;
1009    assert(myBufferIndex == totalBytesPreviouslyRead);
1010 
1011    // Test reading rest of file.
1012    numBytesRead = ifread(largeBuffer, largeTestFileSize);
1013    assert(numBytesRead == (largeTestFileSize - (int)totalBytesPreviouslyRead));
1014    // Verify contents of what read.
1015    for(int i = 0; i < numBytesRead - 5; i++)
1016    {
1017       assert(largeBuffer[i] == '0');
1018    }
1019    // Verify the 12345
1020    assert(largeBuffer[numBytesRead - 5] == '1');
1021    assert(largeBuffer[numBytesRead - 5 + 1] == '2');
1022    assert(largeBuffer[numBytesRead - 5 + 2] == '3');
1023    assert(largeBuffer[numBytesRead - 5 + 3] == '4');
1024    assert(largeBuffer[numBytesRead - 5 + 4] == '5');
1025    totalBytesPreviouslyRead += numBytesRead;
1026    bufferSize = 5;
1027    assert(myBufferIndex == bufferSize);
1028    assert(myCurrentBufferSize == bufferSize);
1029 
1030    // Try at end of file twice.
1031    numBytesRead = ifread(largeBuffer, largeTestFileSize);
1032    assert(numBytesRead == 0);
1033    // Reading at the end clears the buffer
1034    assert(myCurrentBufferSize == 0);
1035    assert(myBufferIndex == 0);
1036    assert(ifeof() != 0);
1037 
1038    // 2nd read attempt at eof.
1039    numBytesRead = ifread(largeBuffer, largeTestFileSize);
1040    assert(numBytesRead == 0);
1041    // Reading at the end clears the buffer
1042    assert(myCurrentBufferSize == 0);
1043    assert(myBufferIndex == 0);
1044    assert(ifeof() != 0);
1045 
1046     // RESET
1047    ifrewind();
1048    totalBytesPreviouslyRead = 0;
1049    assert(myCurrentBufferSize == 0);
1050    assert(myBufferIndex == 0);
1051 
1052    ifclose();
1053 
1054    std::cout << "  Passed test_ifread_ifgetc" << std::endl;
1055 }
1056 
1057 
1058 // Test closing a file.
test_ifclose(const char * extension)1059 void IFILE_Test::test_ifclose(const char* extension)
1060 {
1061    // First open the test file.
1062    openFile(extension);
1063 
1064    // Verify the file successfully opened.
1065    assert(myFileTypePtr != NULL);
1066    assert(isOpen());
1067    assert(myFileTypePtr->isOpen());
1068 
1069    ifclose();
1070 
1071    assert(myFileTypePtr == NULL);
1072    assert(isOpen() == false);
1073 
1074    std::cout << "  Passed test_ifclose" << std::endl;
1075 }
1076 
1077 
test_ifseek(const char * extension)1078 void IFILE_Test::test_ifseek(const char* extension)
1079 {
1080     disableBuffering();
1081    // First open the test file.
1082    openFile(extension);
1083 
1084    // Read a character from the file.
1085    int numBytesRead = readFromFile(myTestBuffer, 1);
1086    assert(numBytesRead == 1);
1087    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[0]);
1088 
1089    // Get the current position.
1090    long int currentPos = iftell();
1091 
1092    // Read the next character from the file.
1093    numBytesRead = readFromFile(myTestBuffer, 1);
1094    assert(numBytesRead == 1);
1095    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]);
1096 
1097    // Seek to just before the character that was just read and read again
1098    // Should be the same character.
1099    assert(ifseek(currentPos, SEEK_SET) == true);
1100    numBytesRead = readFromFile(myTestBuffer, 1);
1101    assert(numBytesRead == 1);
1102    assert(myTestBuffer[0] == TEST_FILE_CONTENTS[1]);
1103 
1104    ifclose();
1105 
1106    assert(myFileTypePtr == NULL);
1107    assert(isOpen() == false);
1108 
1109    // Buffer reads - may have been disabled for iftell to work for bgzf.
1110    bufferReads();
1111 
1112    std::cout << "  Passed test_ifseek" << std::endl;
1113 }
1114 
test_noExistRead(const char * extension)1115 void IFILE_Test::test_noExistRead(const char* extension)
1116 {
1117    openNoExistFile(extension);
1118 
1119 }
1120 
1121 
1122 // Open a file for testing.
openFile(const char * extension)1123 void IFILE_Test::openFile(const char* extension)
1124 {
1125    std::string filename = "data/InputFileTest.";
1126    filename += extension;
1127    assert(InputFile::openFile(filename.c_str(), "rb", InputFile::DEFAULT) == true);
1128 }
1129 
1130 // Open a file for testing.
openLargeFile(const char * extension)1131 void IFILE_Test::openLargeFile(const char* extension)
1132 {
1133    std::string filename = "data/InputFileTestLarge.";
1134    filename += extension;
1135    assert(InputFile::openFile(filename.data(), "rb", InputFile::DEFAULT) == true);
1136 }
1137 
1138 
openNoExistFile(const char * extension)1139 void IFILE_Test::openNoExistFile(const char* extension)
1140 {
1141    std::string filename = "data/noExist.";
1142    filename += extension;
1143    assert(InputFile::openFile(filename.data(), "rb", InputFile::DEFAULT) == false);
1144 }
1145 
1146 
testWrite()1147 void testWrite()
1148 {
1149     std::string filenameNoExt = "results/InputFileTest.";
1150     std::string filename = filenameNoExt + "glf";
1151 
1152     IFILE filePtr = ifopen(filename.c_str(), "wt");
1153     assert(filePtr != NULL);
1154 
1155     assert(ifwrite(filePtr,
1156                    IFILE_Test::TEST_FILE_CONTENTS.c_str(),
1157                    IFILE_Test::TEST_FILE_CONTENTS.length())
1158            == IFILE_Test::TEST_FILE_CONTENTS.length());
1159 
1160     assert(ifclose(filePtr) == 0);
1161 
1162     filename = "results/uncompressedFile.glf";
1163 
1164     filePtr = ifopen(filename.c_str(), "wt", InputFile::UNCOMPRESSED);
1165     assert(filePtr != NULL);
1166 
1167     assert(ifwrite(filePtr,
1168                    IFILE_Test::TEST_FILE_CONTENTS.c_str(),
1169                    IFILE_Test::TEST_FILE_CONTENTS.length())
1170            == IFILE_Test::TEST_FILE_CONTENTS.length());
1171 
1172     assert(ifclose(filePtr) == 0);
1173 
1174     filename = "results/bgzfFile.glf";
1175 
1176     filePtr = ifopen(filename.c_str(), "wt", InputFile::BGZF);
1177     assert(filePtr != NULL);
1178 
1179     assert(ifwrite(filePtr,
1180                    IFILE_Test::TEST_FILE_CONTENTS.c_str(),
1181                    IFILE_Test::TEST_FILE_CONTENTS.length())
1182            == IFILE_Test::TEST_FILE_CONTENTS.length());
1183 
1184     assert(ifclose(filePtr) == 0);
1185 
1186     filename = "results/gzipFile.glf";
1187 
1188     filePtr = ifopen(filename.c_str(), "wt", InputFile::GZIP);
1189     assert(filePtr != NULL);
1190 
1191     assert(ifwrite(filePtr,
1192                    IFILE_Test::TEST_FILE_CONTENTS.c_str(),
1193                    IFILE_Test::TEST_FILE_CONTENTS.length())
1194            ==IFILE_Test:: TEST_FILE_CONTENTS.length());
1195 
1196     assert(ifclose(filePtr) == 0);
1197 
1198     filename = "results/defaultFile.glf";
1199 
1200     filePtr = ifopen(filename.c_str(), "wt");
1201     assert(filePtr != NULL);
1202 
1203     assert(ifwrite(filePtr,
1204                    IFILE_Test::TEST_FILE_CONTENTS.c_str(),
1205                    IFILE_Test::TEST_FILE_CONTENTS.length())
1206            == IFILE_Test::TEST_FILE_CONTENTS.length());
1207 
1208     assert(ifclose(filePtr) == 0);
1209 
1210     filename = "results/defaultFile.gz";
1211 
1212     filePtr = ifopen(filename.c_str(), "wt");
1213     assert(filePtr != NULL);
1214 
1215     assert(ifwrite(filePtr,
1216                    IFILE_Test::TEST_FILE_CONTENTS.c_str(),
1217                    IFILE_Test::TEST_FILE_CONTENTS.length())
1218            == IFILE_Test::TEST_FILE_CONTENTS.length());
1219 
1220     assert(ifclose(filePtr) == 0);
1221 
1222 
1223     filename = "results/textFile.gz";
1224 
1225     unsigned int myuint = 99;
1226     int myint = -99;
1227     char mychar = 'z';
1228 
1229     filePtr = ifopen(filename.c_str(), "wt");
1230     (*filePtr) << "Hello\n";
1231     (*filePtr) << "Hello." << 3 << ' ' << -2 << "How are you";
1232     (*filePtr) << "?" << "\n";
1233     std::string mytext = "Bye\n";
1234     (*filePtr) << mytext;
1235     (*filePtr) << 3.125 << mychar;
1236     (*filePtr) << myuint;
1237     (*filePtr) << mychar;
1238     (*filePtr) << myint;
1239     String myString = "Good Bye!\n";
1240     (*filePtr) << myString;
1241     assert(ifclose(filePtr) == 0);
1242 
1243     filename = "results/textFile1.gz";
1244     InputFile& fileRef = *(ifopen(filename.c_str(), "wt"));
1245     fileRef << "Hello\n";
1246     fileRef << "Hello." << 3 << ' ' << -2 << "How are you";
1247     fileRef << "?" << "\n";
1248     fileRef << mytext;
1249     fileRef << 3.125 << mychar;
1250     fileRef << myuint;
1251     fileRef << mychar;
1252     fileRef << myint;
1253     fileRef << myString;
1254     InputFile* fileRefPtr = &fileRef;
1255     assert(ifclose(fileRefPtr) == 0);
1256     assert(fileRefPtr == NULL);
1257 
1258     // TODO - automatically verify that the files were written in the
1259     // correct format - rather than hand checking.
1260 }
1261 
1262 
1263 
testAdditional(const char * extension)1264 void testAdditional(const char* extension)
1265 {
1266     std::string fileName = "data/InputFileTest2.";
1267     fileName += extension;
1268     IFILE testFile = ifopen(fileName.c_str(), "r");
1269     assert(testFile != NULL);
1270 
1271     std::string buffer = "989";
1272     std::string stopChars = "C5F2";
1273 
1274     // Test readTilChar that stores the string.
1275     assert(testFile->readTilChar(stopChars, buffer) == 0);
1276     assert(buffer == "989AB");
1277     buffer.clear();
1278     assert(testFile->readTilChar(stopChars, buffer) == 2);
1279     assert(buffer == "DE");
1280     assert(testFile->readTilChar(stopChars, buffer) == 3);
1281     assert(buffer == "DEG\tabcdefg\n1");
1282 
1283     // Test readTilChar that discards the string.
1284     assert(testFile->readTilChar(stopChars) == 1);
1285     buffer.clear();
1286     buffer = "t";
1287     assert(testFile->readTilTab(buffer) == 1);
1288     assert(buffer == "t6");
1289     assert(testFile->readTilTab(buffer) == 0);
1290     assert(buffer == "t6hijklm");
1291     assert(testFile->readTilTab(buffer) == 0);
1292     assert(buffer == "t6hijklm1");
1293     assert(testFile->readTilTab(buffer) == 1);
1294     assert(buffer == "t6hijklm1NOP");
1295     assert(testFile->readLine(buffer) == 0);
1296     assert(buffer == "t6hijklm1NOPQRST\tUVW");
1297     assert(testFile->readTilTab(buffer) == 0);
1298     assert(buffer == "t6hijklm1NOPQRST\tUVW");
1299     buffer.clear();
1300     assert(testFile->discardLine() == 0);
1301     assert(testFile->readLine(buffer) == -1);
1302     assert(buffer == "@#$");
1303     assert(testFile->discardLine() == -1);
1304     assert(testFile->readTilTab(buffer) == -1);
1305     assert(testFile->readTilChar(stopChars, buffer) == -1);
1306     assert(testFile->readTilChar(stopChars) == -1);
1307     assert(buffer == "@#$");
1308 
1309     ifclose(testFile);
1310 
1311 }
1312