This source file includes following definitions.
- write_counter_
- Push
- Pop
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include <sstream>
#include "chrome/renderer/net/predictor_queue.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class DnsQueueTest : public testing::Test {
};
class DnsQueueSequentialTester {
public:
DnsQueueSequentialTester(DnsQueue& buffer, int32 read_counter = 0,
int32 write_counter = 0);
bool Push(void);
bool Pop(void);
private:
DnsQueue* buffer_;
int32 read_counter_;
int32 write_counter_;
DISALLOW_COPY_AND_ASSIGN(DnsQueueSequentialTester);
};
DnsQueueSequentialTester::DnsQueueSequentialTester(
DnsQueue& buffer, int32 read_counter, int32 write_counter)
: buffer_(&buffer),
read_counter_(read_counter),
write_counter_(write_counter) {
}
bool DnsQueueSequentialTester::Push(void) {
std::ostringstream value;
value << write_counter_;
DnsQueue::PushResult result = (write_counter_ % 2) ?
buffer_->Push(value.str().c_str(), value.str().size()) :
buffer_->Push(value.str());
if (DnsQueue::SUCCESSFUL_PUSH == result)
write_counter_++;
return DnsQueue::OVERFLOW_PUSH != result;
}
bool DnsQueueSequentialTester::Pop(void) {
std::string string;
if (buffer_->Pop(&string)) {
std::ostringstream expected_value;
expected_value << read_counter_++;
EXPECT_STREQ(expected_value.str().c_str(), string.c_str())
<< "Pop did not match write for value " << read_counter_;
return true;
}
return false;
}
TEST(DnsQueueTest, BufferUseCheck) {
const int buffer_size = 3;
std::string string;
DnsQueue buffer(buffer_size);
DnsQueueSequentialTester tester(buffer);
EXPECT_FALSE(tester.Pop()) << "Pop from empty buffer succeeded";
int i;
for (i = 0; i < 102; i++) {
if (!tester.Push())
break;
EXPECT_TRUE(tester.Pop()) << "Unable to read back data " << i;
EXPECT_FALSE(buffer.Pop(&string))
<< "read from empty buffer not flagged";
}
EXPECT_GE(i, 100) << "Can't write 2 digit strings in 4 character buffer";
EXPECT_LT(i, 101) << "We wrote 3 digit strings into a 4 character buffer";
}
TEST(DnsQueueTest, SubstringUseCheck) {
const int buffer_size = 100;
const char big_string[] = "123456789";
std::string string;
DnsQueue buffer(buffer_size);
EXPECT_FALSE(buffer.Pop(&string)) << "Initial buffer not empty";
EXPECT_EQ(DnsQueue::SUCCESSFUL_PUSH, buffer.Push(big_string, 3))
<< "Can't write string";
EXPECT_EQ(DnsQueue::SUCCESSFUL_PUSH, buffer.Push(big_string, 0))
<< "Can't write null string";
EXPECT_EQ(DnsQueue::SUCCESSFUL_PUSH, buffer.Push(big_string, 5))
<< "Can't write string";
EXPECT_TRUE(buffer.Pop(&string)) << "Filled buffer marked as empty";
EXPECT_STREQ(string.c_str(), "123") << "Can't read actual data";
EXPECT_TRUE(buffer.Pop(&string)) << "Filled buffer marked as empty";
EXPECT_STREQ(string.c_str(), "") << "Can't read null string";
EXPECT_TRUE(buffer.Pop(&string)) << "Filled buffer marked as empty";
EXPECT_STREQ(string.c_str(), "12345") << "Can't read actual data";
EXPECT_FALSE(buffer.Pop(&string))
<< "read from empty buffer not flagged";
}
TEST(DnsQueueTest, SizeCheck) {
const int buffer_size = 100;
std::string input_string = "Hello";
std::string string;
DnsQueue buffer(buffer_size);
EXPECT_EQ(0U, buffer.Size());
EXPECT_FALSE(buffer.Pop(&string));
EXPECT_EQ(DnsQueue::SUCCESSFUL_PUSH, buffer.Push(input_string));
EXPECT_EQ(1U, buffer.Size());
EXPECT_EQ(DnsQueue::SUCCESSFUL_PUSH, buffer.Push("Hi There"));
EXPECT_EQ(2U, buffer.Size());
EXPECT_TRUE(buffer.Pop(&string));
EXPECT_EQ(1U, buffer.Size());
EXPECT_TRUE(buffer.Pop(&string));
EXPECT_EQ(0U, buffer.Size());
EXPECT_EQ(DnsQueue::SUCCESSFUL_PUSH, buffer.Push(input_string));
EXPECT_EQ(1U, buffer.Size());
EXPECT_EQ(DnsQueue::REDUNDANT_PUSH, buffer.Push(input_string));
EXPECT_EQ(1U, buffer.Size());
}
TEST(DnsQueueTest, FillThenEmptyCheck) {
const size_t buffer_size = 1000;
size_t byte_usage_counter = 1;
DnsQueue buffer(buffer_size);
DnsQueueSequentialTester tester(buffer);
size_t write_success;
for (write_success = 0; write_success < buffer_size; write_success++) {
if (!tester.Push())
break;
EXPECT_EQ(buffer.Size(), write_success + 1);
if (write_success > 99)
byte_usage_counter += 4;
else if (write_success > 9)
byte_usage_counter += 3;
else
byte_usage_counter += 2;
}
EXPECT_LE(byte_usage_counter, buffer_size)
<< "Written data exceeded buffer size";
EXPECT_GE(byte_usage_counter, buffer_size - 4)
<< "Buffer does not appear to have filled";
EXPECT_GE(write_success, 10U) << "Couldn't even write 10 one digit strings "
"in " << buffer_size << " byte buffer";
while (1) {
if (!tester.Pop())
break;
write_success--;
}
EXPECT_EQ(write_success, 0U) << "Push and Pop count were different";
EXPECT_FALSE(tester.Pop()) << "Read from empty buffer succeeded";
}
TEST(DnsQueueTest, ClearCheck) {
const size_t buffer_size = 1000;
DnsQueue buffer(buffer_size);
std::string string("ABC");
DnsQueueSequentialTester tester(buffer);
size_t write_success;
for (write_success = 0; write_success < buffer_size; write_success++) {
if (!tester.Push())
break;
EXPECT_EQ(buffer.Size(), write_success + 1);
}
buffer.Clear();
EXPECT_EQ(buffer.Size(), 0U);
size_t write_success2;
for (write_success2 = 0; write_success2 < buffer_size; write_success2++) {
if (!tester.Push())
break;
EXPECT_EQ(buffer.Size(), write_success2 + 1);
}
for (; write_success2 > 0; write_success2--) {
EXPECT_EQ(buffer.Size(), write_success2);
EXPECT_TRUE(buffer.Pop(&string));
}
EXPECT_EQ(buffer.Size(), 0U);
buffer.Clear();
EXPECT_EQ(buffer.Size(), 0U);
}
TEST(DnsQueueTest, WrapOnVariousSubstrings) {
const int prime_number = 23;
const int buffer_size = prime_number - 1;
DnsQueue buffer(buffer_size);
DnsQueueSequentialTester tester(buffer);
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 30; i++) {
EXPECT_TRUE(tester.Push()) << "write failed with only " << j
<< " blocks in buffer";
EXPECT_TRUE(tester.Pop()) << "Unable to read back data ";
}
EXPECT_TRUE(tester.Push());
}
EXPECT_TRUE(tester.Pop());
EXPECT_TRUE(tester.Pop());
EXPECT_TRUE(tester.Pop());
EXPECT_FALSE(tester.Pop());
}
};