This source file includes following definitions.
- GetBytes
- VerifyBytes
- Reset
- AdvanceIndex
- Consume
#include "net/base/test_data_stream.h"
namespace net {
TestDataStream::TestDataStream() {
Reset();
}
void TestDataStream::GetBytes(char* buffer, int length) {
while (length) {
AdvanceIndex();
int bytes_to_copy = std::min(length, bytes_remaining_);
memcpy(buffer, buffer_ptr_, bytes_to_copy);
buffer += bytes_to_copy;
Consume(bytes_to_copy);
length -= bytes_to_copy;
}
}
bool TestDataStream::VerifyBytes(const char *buffer, int length) {
while (length) {
AdvanceIndex();
int bytes_to_compare = std::min(length, bytes_remaining_);
if (memcmp(buffer, buffer_ptr_, bytes_to_compare))
return false;
Consume(bytes_to_compare);
length -= bytes_to_compare;
buffer += bytes_to_compare;
}
return true;
}
void TestDataStream::Reset() {
index_ = 0;
bytes_remaining_ = 0;
buffer_ptr_ = buffer_;
}
void TestDataStream::AdvanceIndex() {
if (bytes_remaining_ == 0) {
int val = index_++;
do {
buffer_[bytes_remaining_++] = (val % 10) + '0';
} while ((val /= 10) > 0);
buffer_[bytes_remaining_++] = '.';
}
}
void TestDataStream::Consume(int bytes) {
bytes_remaining_ -= bytes;
if (bytes_remaining_)
buffer_ptr_ += bytes;
else
buffer_ptr_ = buffer_;
}
}