This source file includes following definitions.
- IsZero
- TEST
- TEST
- TEST
- TEST
- TEST
#include <limits>
#include "mojo/public/cpp/bindings/buffer.h"
#include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
#include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
#include "mojo/public/cpp/bindings/lib/scratch_buffer.h"
#include "mojo/public/cpp/environment/environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
namespace test {
namespace {
bool IsZero(void* p_buf, size_t size) {
char* buf = reinterpret_cast<char*>(p_buf);
for (size_t i = 0; i < size; ++i) {
if (buf[i] != 0)
return false;
}
return true;
}
TEST(ScratchBufferTest, Basic) {
Environment env;
internal::ScratchBuffer buf;
void* small = buf.Allocate(10);
EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
EXPECT_TRUE(IsZero(small, 10));
void* large = buf.Allocate(100*1024);
EXPECT_TRUE(IsZero(large, 100*1024));
EXPECT_FALSE(large >= &buf && large < (&buf + sizeof(buf)));
small = buf.Allocate(10);
EXPECT_TRUE(IsZero(small, 10));
EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
}
TEST(ScratchBufferTest, Stacked) {
Environment env;
EXPECT_FALSE(Buffer::current());
{
internal::ScratchBuffer a;
EXPECT_EQ(&a, Buffer::current());
{
internal::ScratchBuffer b;
EXPECT_EQ(&b, Buffer::current());
}
}
EXPECT_FALSE(Buffer::current());
}
TEST(FixedBufferTest, Alignment) {
Environment env;
internal::FixedBuffer buf(internal::Align(10) * 2);
ASSERT_EQ(buf.size(), 16u * 2);
void* a = buf.Allocate(10);
ASSERT_TRUE(a);
EXPECT_TRUE(IsZero(a, 10));
EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8);
void* b = buf.Allocate(10);
ASSERT_TRUE(b);
EXPECT_TRUE(IsZero(b, 10));
EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8);
}
TEST(FixedBufferTest, Leak) {
Environment env;
void* ptr = NULL;
void* buf_ptr = NULL;
{
internal::FixedBuffer buf(8);
ASSERT_EQ(8u, buf.size());
ptr = buf.Allocate(8);
ASSERT_TRUE(ptr);
void* buf_ptr = buf.Leak();
EXPECT_EQ(ptr, buf_ptr);
EXPECT_EQ(0u, buf.size());
EXPECT_FALSE(buf.Leak());
}
memset(ptr, 1, 8);
free(buf_ptr);
}
#ifdef NDEBUG
TEST(FixedBufferTest, TooBig) {
Environment env;
internal::FixedBuffer buf(24);
EXPECT_EQ(reinterpret_cast<void*>(0), buf.Allocate(32));
EXPECT_NE(reinterpret_cast<void*>(0), buf.Allocate(16));
EXPECT_EQ(reinterpret_cast<void*>(0),
buf.Allocate(std::numeric_limits<size_t>::max() - 8u));
}
#endif
}
}
}