Commit 91c2cf35 authored by Tomek Mrugalski's avatar Tomek Mrugalski 🛰
Browse files

[1228] InputBuffer::readVector() implemented (+tests)

parent c6e8dd84
......@@ -207,6 +207,28 @@ public:
}
//@}
/// @brief Read specified number of bytes as a vector.
///
/// If specified buffer is too short, it will be expanded
/// using vector::resize() method.
///
/// @param Reference to a buffer (data will be stored there).
/// @param Size specified number of bytes to read in a vector.
///
void readVector(std::vector<uint8_t>& data, size_t len)
{
if (position_ + len > len_) {
isc_throw(InvalidBufferPosition, "read beyond end of buffer");
}
if (data.size() < len)
data.resize(len);
const uint8_t* ptr = &data_[position_];
data = std::vector<uint8_t>(ptr, ptr + len);
position_ += len;
}
private:
size_t position_;
......@@ -519,6 +541,6 @@ typedef boost::shared_ptr<OutputBuffer> OutputBufferPtr;
} // namespace isc
#endif // __BUFFER_H
// Local Variables:
// Local Variables:
// mode: c++
// End:
// End:
......@@ -239,4 +239,36 @@ TEST_F(BufferTest, outputBufferZeroSize) {
});
}
TEST_F(BufferTest, readVectorAll) {
std::vector<uint8_t> vec;
// check that vector can read the whole buffer
ibuffer.readVector(vec, 5);
ASSERT_EQ(5, vec.size());
EXPECT_EQ(0, memcmp(&vec[0], testdata, 5));
// ibuffer is 5 bytes long. Can't read past it.
EXPECT_THROW(
ibuffer.readVector(vec, 1),
isc::util::InvalidBufferPosition
);
}
TEST_F(BufferTest, readVectorChunks) {
std::vector<uint8_t> vec;
// check that vector can read the whole buffer
ibuffer.readVector(vec, 3);
EXPECT_EQ(3, vec.size());
EXPECT_EQ(0, memcmp(&vec[0], testdata, 3));
EXPECT_NO_THROW(
ibuffer.readVector(vec, 2)
);
EXPECT_EQ(0, memcmp(&vec[0], testdata+3, 2));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment