This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "mojo/system/raw_shared_buffer.h"
#include <limits>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace system {
namespace {
TEST(RawSharedBufferTest, Basic) {
const size_t kNumInts = 100;
const size_t kNumBytes = kNumInts * sizeof(int);
const int kFudge = 1234567890;
scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kNumBytes));
ASSERT_TRUE(buffer);
{
EXPECT_TRUE(buffer->IsValidMap(0, kNumBytes));
scoped_ptr<RawSharedBufferMapping> mapping(buffer->Map(0, kNumBytes));
ASSERT_TRUE(mapping);
ASSERT_TRUE(mapping->base());
int* stuff = static_cast<int*>(mapping->base());
for (size_t i = 0; i < kNumInts; i++)
stuff[i] = static_cast<int>(i) + kFudge;
}
{
ASSERT_TRUE(buffer->IsValidMap(0, kNumBytes));
scoped_ptr<RawSharedBufferMapping> mapping1(
buffer->MapNoCheck(0, kNumBytes));
ASSERT_TRUE(mapping1);
ASSERT_TRUE(mapping1->base());
int* stuff1 = static_cast<int*>(mapping1->base());
for (size_t i = 0; i < kNumInts; i++)
EXPECT_EQ(static_cast<int>(i) + kFudge, stuff1[i]) << i;
scoped_ptr<RawSharedBufferMapping> mapping2(
buffer->Map((kNumInts / 2) * sizeof(int), 2 * sizeof(int)));
ASSERT_TRUE(mapping2);
ASSERT_TRUE(mapping2->base());
int* stuff2 = static_cast<int*>(mapping2->base());
EXPECT_EQ(static_cast<int>(kNumInts / 2) + kFudge, stuff2[0]);
EXPECT_EQ(static_cast<int>(kNumInts / 2) + 1 + kFudge, stuff2[1]);
stuff2[0] = 123;
stuff2[1] = 456;
EXPECT_EQ(123, stuff1[kNumInts / 2]);
EXPECT_EQ(456, stuff1[kNumInts / 2 + 1]);
mapping1.reset();
EXPECT_EQ(123, stuff2[0]);
EXPECT_EQ(456, stuff2[1]);
stuff2[1] = 789;
}
{
EXPECT_TRUE(buffer->IsValidMap(sizeof(int), kNumBytes - sizeof(int)));
scoped_ptr<RawSharedBufferMapping> mapping(
buffer->Map(sizeof(int), kNumBytes - sizeof(int)));
ASSERT_TRUE(mapping);
ASSERT_TRUE(mapping->base());
int* stuff = static_cast<int*>(mapping->base());
for (size_t j = 0; j < kNumInts - 1; j++) {
int i = static_cast<int>(j) + 1;
if (i == kNumInts / 2) {
EXPECT_EQ(123, stuff[j]);
} else if (i == kNumInts / 2 + 1) {
EXPECT_EQ(789, stuff[j]);
} else {
EXPECT_EQ(i + kFudge, stuff[j]) << i;
}
}
}
}
TEST(RawSharedBufferTest, InvalidMappings) {
scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100));
ASSERT_TRUE(buffer);
EXPECT_FALSE(buffer->Map(0, 0));
EXPECT_FALSE(buffer->IsValidMap(0, 0));
EXPECT_TRUE(buffer->Map(0, 100));
EXPECT_TRUE(buffer->IsValidMap(0, 100));
EXPECT_FALSE(buffer->Map(0, 101));
EXPECT_FALSE(buffer->IsValidMap(0, 101));
EXPECT_FALSE(buffer->Map(1, 100));
EXPECT_FALSE(buffer->IsValidMap(1, 100));
EXPECT_TRUE(buffer->Map(50, 50));
EXPECT_TRUE(buffer->IsValidMap(50, 50));
EXPECT_FALSE(buffer->Map(50, 51));
EXPECT_FALSE(buffer->IsValidMap(50, 51));
EXPECT_FALSE(buffer->Map(51, 50));
EXPECT_FALSE(buffer->IsValidMap(51, 50));
}
TEST(RawSharedBufferTest, TooBig) {
const size_t kMaxSizeT = std::numeric_limits<size_t>::max();
scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kMaxSizeT));
if (buffer)
EXPECT_FALSE(buffer->Map(0, kMaxSizeT));
}
TEST(RawSharedBufferTest, MappingsDistinct) {
scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100));
scoped_ptr<RawSharedBufferMapping> mapping1(buffer->Map(0, 100));
scoped_ptr<RawSharedBufferMapping> mapping2(buffer->Map(0, 100));
EXPECT_NE(mapping1->base(), mapping2->base());
}
TEST(RawSharedBufferTest, BufferZeroInitialized) {
static const size_t kSizes[] = { 10, 100, 1000, 10000, 100000 };
for (size_t i = 0; i < arraysize(kSizes); i++) {
scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kSizes[i]));
scoped_ptr<RawSharedBufferMapping> mapping(buffer->Map(0, kSizes[i]));
for (size_t j = 0; j < kSizes[i]; j++) {
ASSERT_EQ('\0', static_cast<char*>(mapping->base())[j])
<< "size " << kSizes[i] << ", offset " << j;
}
}
}
TEST(RawSharedBufferTest, MappingsOutliveBuffer) {
scoped_ptr<RawSharedBufferMapping> mapping1;
scoped_ptr<RawSharedBufferMapping> mapping2;
{
scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100));
mapping1 = buffer->Map(0, 100).Pass();
mapping2 = buffer->Map(50, 50).Pass();
static_cast<char*>(mapping1->base())[50] = 'x';
}
EXPECT_EQ('x', static_cast<char*>(mapping2->base())[0]);
static_cast<char*>(mapping2->base())[1] = 'y';
EXPECT_EQ('y', static_cast<char*>(mapping1->base())[51]);
}
}
}
}