1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "mojo/public/bindings/buffer.h"
8 #include "mojo/public/bindings/lib/bindings_serialization.h"
9 #include "mojo/public/bindings/lib/fixed_buffer.h"
10 #include "mojo/public/bindings/lib/scratch_buffer.h"
11 #include "mojo/public/environment/environment.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 bool IsZero(void* p_buf, size_t size) {
19 char* buf = reinterpret_cast<char*>(p_buf);
20 for (size_t i = 0; i < size; ++i) {
27 // Tests small and large allocations in ScratchBuffer.
28 TEST(ScratchBufferTest, Basic) {
31 // Test that a small allocation is placed on the stack.
32 internal::ScratchBuffer buf;
33 void* small = buf.Allocate(10);
34 EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
35 EXPECT_TRUE(IsZero(small, 10));
37 // Large allocations won't be on the stack.
38 void* large = buf.Allocate(100*1024);
39 EXPECT_TRUE(IsZero(large, 100*1024));
40 EXPECT_FALSE(large >= &buf && large < (&buf + sizeof(buf)));
42 // But another small allocation should be back on the stack.
43 small = buf.Allocate(10);
44 EXPECT_TRUE(IsZero(small, 10));
45 EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
48 // Tests that Buffer::current() returns the correct value.
49 TEST(ScratchBufferTest, Stacked) {
52 EXPECT_FALSE(Buffer::current());
55 internal::ScratchBuffer a;
56 EXPECT_EQ(&a, Buffer::current());
59 internal::ScratchBuffer b;
60 EXPECT_EQ(&b, Buffer::current());
64 EXPECT_FALSE(Buffer::current());
67 // Tests that FixedBuffer allocates memory aligned to 8 byte boundaries.
68 TEST(FixedBufferTest, Alignment) {
71 internal::FixedBuffer buf(internal::Align(10) * 2);
72 ASSERT_EQ(buf.size(), 16u * 2);
74 void* a = buf.Allocate(10);
76 EXPECT_TRUE(IsZero(a, 10));
77 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8);
79 void* b = buf.Allocate(10);
81 EXPECT_TRUE(IsZero(b, 10));
82 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8);
84 // Any more allocations would result in an assert, but we can't test that.
87 // Tests that FixedBuffer::Leak passes ownership to the caller.
88 TEST(FixedBufferTest, Leak) {
94 internal::FixedBuffer buf(8);
95 ASSERT_EQ(8u, buf.size());
97 ptr = buf.Allocate(8);
99 void* buf_ptr = buf.Leak();
101 // The buffer should point to the first element allocated.
102 // TODO(mpcomplete): Is this a reasonable expectation?
103 EXPECT_EQ(ptr, buf_ptr);
105 // The FixedBuffer should be empty now.
106 EXPECT_EQ(0u, buf.size());
107 EXPECT_FALSE(buf.Leak());
110 // Since we called Leak, ptr is still writable after FixedBuffer went out of
117 TEST(FixedBufferTest, TooBig) {
120 internal::FixedBuffer buf(24);
122 // A little bit too large.
123 EXPECT_EQ(reinterpret_cast<void*>(0), buf.Allocate(32));
125 // Move the cursor forward.
126 EXPECT_NE(reinterpret_cast<void*>(0), buf.Allocate(16));
128 // A lot too large, leading to possible integer overflow.
129 EXPECT_EQ(reinterpret_cast<void*>(0),
130 buf.Allocate(std::numeric_limits<size_t>::max() - 8u));