summaryrefslogtreecommitdiff
path: root/chromium/net/base/test_data_stream.cc
blob: 0d723827f01a3fa9d1a0b2f95a2303d137f5351b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/base/test_data_stream.h"

#include <algorithm>
#include <cstring>

namespace net {

TestDataStream::TestDataStream() {
  Reset();
}

// Fill |buffer| with |length| bytes of data from the stream.
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_;
}

// If there is no data spilled over from the previous index, advance the
// index and fill the buffer.
void TestDataStream::AdvanceIndex() {
  if (bytes_remaining_ == 0) {
    // Convert it to ascii, but don't bother to reverse it.
    // (e.g. 12345 becomes "54321")
    int val = index_++;
    do {
      buffer_[bytes_remaining_++] = (val % 10) + '0';
    } while ((val /= 10) > 0);
    buffer_[bytes_remaining_++] = '.';
  }
}

// Consume data from the spill buffer.
void TestDataStream::Consume(int bytes) {
  bytes_remaining_ -= bytes;
  if (bytes_remaining_)
    buffer_ptr_ += bytes;
  else
    buffer_ptr_ = buffer_;
}

}  // namespace net