diff --git a/LCS/Stream/include/Stream/FixedBufferStream.h b/LCS/Stream/include/Stream/FixedBufferStream.h index e86589708758b35cc7179262446b09b92deb7ef3..b920406d8c4a9e4d412412b6befe666ca0f9a264 100644 --- a/LCS/Stream/include/Stream/FixedBufferStream.h +++ b/LCS/Stream/include/Stream/FixedBufferStream.h @@ -46,7 +46,6 @@ class FixedBufferStream : public Stream char * const itsEnd; char *itsHead; - char *itsTail; }; } // namespace LOFAR diff --git a/LCS/Stream/src/FixedBufferStream.cc b/LCS/Stream/src/FixedBufferStream.cc index 1d4c270cd881ab40ad4ddf6125d658c9a76150d3..73538230c04fe23de50d3b9fe4bf5579d6859c04 100644 --- a/LCS/Stream/src/FixedBufferStream.cc +++ b/LCS/Stream/src/FixedBufferStream.cc @@ -35,8 +35,7 @@ FixedBufferStream::FixedBufferStream(char *buffer, size_t size) : itsStart(buffer), itsEnd(buffer + size), - itsHead(buffer), - itsTail(buffer) + itsHead(buffer) { } @@ -50,13 +49,13 @@ size_t FixedBufferStream::tryRead(void *ptr, size_t size) { Cancellation::point(); // keep behaviour consistent with real I/O streams - size_t numBytes = std::min<size_t>(size, itsHead - itsTail); + size_t numBytes = std::min<size_t>(size, itsEnd - itsHead); if (numBytes == 0) THROW(EndOfStreamException, "No space left in buffer"); - memcpy(ptr, itsTail, numBytes); - itsTail += numBytes; + memcpy(ptr, itsHead, numBytes); + itsHead += numBytes; return numBytes; } diff --git a/LCS/Stream/test/tFixedBufferStream.cc b/LCS/Stream/test/tFixedBufferStream.cc index 6136317fabfe4af8b818ab5d4355eed912f7aa3b..7bff4812c9d1ada3378b4f7adaff73885f934f3a 100644 --- a/LCS/Stream/test/tFixedBufferStream.cc +++ b/LCS/Stream/test/tFixedBufferStream.cc @@ -35,18 +35,16 @@ using namespace std; vector<char> buffer; -FixedBufferStream *createStream(size_t numbytes) +void createStream(size_t numbytes) { buffer.resize(numbytes); for (size_t i = 0; i < numbytes; ++i) buffer[i] = 0; - - return new FixedBufferStream(&buffer[0], numbytes); } -void testWrite(FixedBufferStream *s, size_t numbytes) +void testWrite(FixedBufferStream &s, size_t numbytes) { vector<char> buf_in(numbytes, 0); @@ -55,17 +53,17 @@ void testWrite(FixedBufferStream *s, size_t numbytes) for (size_t i = 0; i < numbytes; i++) buf_in[i] = (writeCounter++) % 128; - s->write(&buf_in[0], numbytes); + s.write(&buf_in[0], numbytes); } -void testRead(FixedBufferStream *s, size_t numbytes) +void testRead(FixedBufferStream &s, size_t numbytes) { vector<char> buf_out(numbytes, 0); static size_t readCounter = 0; - s->read(&buf_out[0], numbytes); + s.read(&buf_out[0], numbytes); for (size_t i = 0; i < numbytes; i++) { char val = (readCounter++) % 128; @@ -80,34 +78,37 @@ int main(int /*argc*/, const char* argv[]) try { alarm(30); - FixedBufferStream *s = createStream(1000); + createStream(1000); + + FixedBufferStream rs(&buffer[0], buffer.size()); + FixedBufferStream ws(&buffer[0], buffer.size()); // 1 write, 1 read - testWrite(s, 100); - testRead(s, 100); + testWrite(ws, 100); + testRead(rs, 100); // 1 write, 2 reads - testWrite(s, 200); - testRead(s, 100); - testRead(s, 100); + testWrite(ws, 200); + testRead(rs, 100); + testRead(rs, 100); // 2 writes, 1 read - testWrite(s, 100); - testWrite(s, 100); - testRead(s, 200); + testWrite(ws, 100); + testWrite(ws, 100); + testRead(rs, 200); // 3 writes, 2 reads - testWrite(s, 100); - testWrite(s, 100); - testWrite(s, 100); - testRead(s, 250); - testRead(s, 50); + testWrite(ws, 100); + testWrite(ws, 100); + testWrite(ws, 100); + testRead(rs, 250); + testRead(rs, 50); // write beyond EOB bool EOB = false; try { - testWrite(s, 500); + testWrite(ws, 500); } catch(Stream::EndOfStreamException &e) { EOB = true; } @@ -118,14 +119,12 @@ int main(int /*argc*/, const char* argv[]) EOB = false; try { - testRead(s, 500); + testRead(rs, 500); } catch(Stream::EndOfStreamException &e) { EOB = true; } ASSERTSTR(EOB, "Expected to read beyond EOB"); - - delete s; } catch (Exception& e) { LOG_ERROR_STR(e); return 1;