6 #include <MpqArchive.h>
14 using WorldStone::StreamPtr;
25 MpqFileWrapper(
const char* filename) :
MpqFileStream(), archive(
"testArchive.mpq")
27 REQUIRE_MESSAGE(archive.good(),
"This archive should be valid, wrong working directory ?");
28 open(archive, filename);
31 ~MpqFileWrapper() { close(); }
34 typedef doctest::Types<WorldStone::FileStream, MpqFileWrapper> stream_types;
37 TYPE_TO_STRING(MpqFileWrapper);
42 GIVEN(
"A file that does not exist")
44 const char* invalidFileName =
"does-not-exist-file";
45 WHEN(
"Trying to open it")
47 StreamType stream{invalidFileName};
49 THEN(
"File is not opened and stream is invalid")
51 REQUIRE_FALSE(stream.is_open());
52 CHECK_FALSE(streamRef.good());
53 CHECK_FALSE(streamRef.bad());
54 CHECK(streamRef.fail());
55 CHECK_FALSE(streamRef.
eof());
63 GIVEN(
"A file containing the word \"test\"")
65 const char* filename =
"test.txt";
66 WHEN(
"Opening the file containing \"test\"")
68 StreamType stream{filename};
70 THEN(
"File is opened and stream is valid")
72 REQUIRE(stream.is_open());
73 CHECK(streamRef.good());
74 CHECK_FALSE(streamRef.bad());
75 CHECK_FALSE(streamRef.fail());
76 CHECK_FALSE(streamRef.
eof());
77 CHECK(streamRef.
tell() == 0);
78 const size_t fileSize = streamRef.
size();
79 const char* fileContent =
"test";
80 CHECK(fileSize == strlen(fileContent));
82 AND_WHEN(
"We read the whole file")
84 char buffer[256] = {};
85 CHECK(fileSize < 256);
86 streamRef.
read(buffer, fileSize);
87 THEN(
"The buffer contains the same things as the file")
89 CHECK(!strcmp(
"test", buffer));
90 CHECK_FALSE(streamRef.
eof());
91 CHECK(streamRef.
tell() == fileSize);
92 AND_WHEN(
"We read more")
94 streamRef.
read(buffer, 1);
95 THEN(
"EOF and fail flags are set")
97 CHECK(streamRef.
eof());
98 CHECK(streamRef.fail());
99 CHECK_FALSE(streamRef.bad());
100 CHECK_FALSE(streamRef.good());
105 AND_WHEN(
"We read the whole file using getc")
107 char buffer[256] = {};
108 CHECK(fileSize < 256);
109 for (
size_t i = 0; i < fileSize; i++)
111 int val = streamRef.
getc();
113 buffer[i] =
static_cast<char>(val);
115 THEN(
"The buffer contains the same things as the file")
117 CHECK(!strcmp(
"test", buffer));
118 CHECK_FALSE(streamRef.
eof());
119 CHECK(streamRef.
tell() == fileSize);
120 AND_WHEN(
"We read more")
122 int val = streamRef.
getc();
124 THEN(
"EOF and fail flags are set")
126 CHECK(streamRef.
eof());
127 CHECK(streamRef.fail());
128 CHECK_FALSE(streamRef.bad());
129 CHECK_FALSE(streamRef.good());
134 AND_WHEN(
"We read more than the whole file")
136 char buffer[256] = {};
137 CHECK(fileSize * 2 < 256);
138 size_t readCount = streamRef.
read(buffer, fileSize * 2);
139 THEN(
"EOF is reached and correct number of bytes read is reported")
142 CHECK(readCount == fileSize);
143 CHECK(strncmp(
"test", buffer, fileSize) == 0);
146 AND_WHEN(
"You seek past the end of file")
148 streamRef.
seek(10, WorldStone::IStream::end);
149 THEN(
"There is no failure")
152 CHECK_FALSE(streamRef.fail());
153 CHECK_FALSE(streamRef.bad());
154 CHECK(streamRef.good());
157 AND_WHEN(
"You seek before the file beginning")
159 streamRef.
seek(-1, WorldStone::IStream::beg);
160 THEN(
"Fail flag is set") { CHECK(streamRef.fail()); }
An interface for a stream of data.
virtual long size()=0
Compute the size of the file.
bool eof() const
True if the end of the stream was reached during the last read operation.
A wrapper to manage MPQ archives.
virtual long tell()=0
Return the current position of the stream pointer.
virtual bool seek(long offset, seekdir origin)=0
Change the pointer of the stream to a given position.
virtual size_t read(void *buffer, size_t size)=0
Read data from the stream.
A file from a MpqArchive.
virtual int getc()
Read one byte from the stream.
Wrapper around POSIX file io, same as ifstream but without iostream formatting.
SCENARIO_TEMPLATE("Read-only filestreams", StreamType, stream_types)