|
|
@@ -89,63 +89,84 @@ struct FileInputStreamTests : public UnitTest |
|
|
|
|
|
|
|
void runTest() override
|
|
|
|
{
|
|
|
|
beginTest ("Open stream non-existent file");
|
|
|
|
{
|
|
|
|
auto tempFile = File::createTempFile (".txt");
|
|
|
|
expect (! tempFile.exists());
|
|
|
|
|
|
|
|
FileInputStream stream (tempFile);
|
|
|
|
expect (stream.failedToOpen());
|
|
|
|
}
|
|
|
|
|
|
|
|
beginTest ("Open stream existing file");
|
|
|
|
{
|
|
|
|
auto tempFile = File::createTempFile (".txt");
|
|
|
|
tempFile.create();
|
|
|
|
expect (tempFile.exists());
|
|
|
|
|
|
|
|
FileInputStream stream (tempFile);
|
|
|
|
expect (stream.openedOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
const MemoryBlock data ("abcdefghijklmnopqrstuvwxyz", 26);
|
|
|
|
File f (File::createTempFile (".txt"));
|
|
|
|
f.appendData (data.getData(), data.getSize());
|
|
|
|
FileInputStream stream (f);
|
|
|
|
|
|
|
|
beginTest ("Read");
|
|
|
|
|
|
|
|
expectEquals (stream.getPosition(), (int64) 0);
|
|
|
|
expectEquals (stream.getTotalLength(), (int64) data.getSize());
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
|
|
|
|
expect (! stream.isExhausted());
|
|
|
|
|
|
|
|
size_t numBytesRead = 0;
|
|
|
|
MemoryBlock readBuffer (data.getSize());
|
|
|
|
|
|
|
|
while (numBytesRead < data.getSize())
|
|
|
|
{
|
|
|
|
numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
|
|
|
|
expectEquals (stream.getPosition(), (int64) 0);
|
|
|
|
expectEquals (stream.getTotalLength(), (int64) data.getSize());
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
|
|
|
|
expect (! stream.isExhausted());
|
|
|
|
|
|
|
|
expectEquals (stream.getPosition(), (int64) numBytesRead);
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
|
|
|
|
expect (stream.isExhausted() == (numBytesRead == data.getSize()));
|
|
|
|
}
|
|
|
|
size_t numBytesRead = 0;
|
|
|
|
MemoryBlock readBuffer (data.getSize());
|
|
|
|
|
|
|
|
expectEquals (stream.getPosition(), (int64) data.getSize());
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), (int64) 0);
|
|
|
|
expect (stream.isExhausted());
|
|
|
|
while (numBytesRead < data.getSize())
|
|
|
|
{
|
|
|
|
numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
|
|
|
|
|
|
|
|
expect (readBuffer == data);
|
|
|
|
expectEquals (stream.getPosition(), (int64) numBytesRead);
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
|
|
|
|
expect (stream.isExhausted() == (numBytesRead == data.getSize()));
|
|
|
|
}
|
|
|
|
|
|
|
|
beginTest ("Skip");
|
|
|
|
expectEquals (stream.getPosition(), (int64) data.getSize());
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), (int64) 0);
|
|
|
|
expect (stream.isExhausted());
|
|
|
|
|
|
|
|
stream.setPosition (0);
|
|
|
|
expectEquals (stream.getPosition(), (int64) 0);
|
|
|
|
expectEquals (stream.getTotalLength(), (int64) data.getSize());
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
|
|
|
|
expect (! stream.isExhausted());
|
|
|
|
|
|
|
|
numBytesRead = 0;
|
|
|
|
const int numBytesToSkip = 5;
|
|
|
|
expect (readBuffer == data);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (numBytesRead < data.getSize())
|
|
|
|
beginTest ("Skip");
|
|
|
|
{
|
|
|
|
stream.skipNextBytes (numBytesToSkip);
|
|
|
|
numBytesRead += numBytesToSkip;
|
|
|
|
numBytesRead = std::min (numBytesRead, data.getSize());
|
|
|
|
|
|
|
|
expectEquals (stream.getPosition(), (int64) numBytesRead);
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
|
|
|
|
expect (stream.isExhausted() == (numBytesRead == data.getSize()));
|
|
|
|
stream.setPosition (0);
|
|
|
|
expectEquals (stream.getPosition(), (int64) 0);
|
|
|
|
expectEquals (stream.getTotalLength(), (int64) data.getSize());
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
|
|
|
|
expect (! stream.isExhausted());
|
|
|
|
|
|
|
|
size_t numBytesRead = 0;
|
|
|
|
const int numBytesToSkip = 5;
|
|
|
|
|
|
|
|
while (numBytesRead < data.getSize())
|
|
|
|
{
|
|
|
|
stream.skipNextBytes (numBytesToSkip);
|
|
|
|
numBytesRead += numBytesToSkip;
|
|
|
|
numBytesRead = std::min (numBytesRead, data.getSize());
|
|
|
|
|
|
|
|
expectEquals (stream.getPosition(), (int64) numBytesRead);
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), (int64) (data.getSize() - numBytesRead));
|
|
|
|
expect (stream.isExhausted() == (numBytesRead == data.getSize()));
|
|
|
|
}
|
|
|
|
|
|
|
|
expectEquals (stream.getPosition(), (int64) data.getSize());
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), (int64) 0);
|
|
|
|
expect (stream.isExhausted());
|
|
|
|
|
|
|
|
f.deleteFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
expectEquals (stream.getPosition(), (int64) data.getSize());
|
|
|
|
expectEquals (stream.getNumBytesRemaining(), (int64) 0);
|
|
|
|
expect (stream.isExhausted());
|
|
|
|
|
|
|
|
f.deleteFile();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|