| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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. | |
| 4 | |
| 5 #include "mojo/public/bindings/lib/bindings_serialization.h" | |
| 6 #include "mojo/public/bindings/lib/buffer.h" | |
| 7 #include "mojo/public/tests/simple_bindings_support.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace mojo { | |
| 11 namespace test { | |
| 12 | |
| 13 bool IsZero(void* p_buf, size_t size) { | |
| 14 char* buf = reinterpret_cast<char*>(p_buf); | |
| 15 for (size_t i = 0; i < size; ++i) { | |
| 16 if (buf[i] != 0) | |
| 17 return false; | |
| 18 } | |
| 19 return true; | |
| 20 } | |
| 21 | |
| 22 // Tests small and large allocations in ScratchBuffer. | |
| 23 TEST(ScratchBufferTest, Basic) { | |
| 24 SimpleBindingsSupport bindings_support; | |
| 25 | |
| 26 // Test that a small allocation is placed on the stack. | |
| 27 internal::ScratchBuffer buf; | |
| 28 void* small = buf.Allocate(10); | |
| 29 EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf))); | |
| 30 EXPECT_TRUE(IsZero(small, 10)); | |
| 31 | |
| 32 // Large allocations won't be on the stack. | |
| 33 void* large = buf.Allocate(100*1024); | |
| 34 EXPECT_TRUE(IsZero(large, 100*1024)); | |
| 35 EXPECT_FALSE(large >= &buf && large < (&buf + sizeof(buf))); | |
| 36 | |
| 37 // But another small allocation should be back on the stack. | |
| 38 small = buf.Allocate(10); | |
| 39 EXPECT_TRUE(IsZero(small, 10)); | |
| 40 EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf))); | |
| 41 } | |
| 42 | |
| 43 // Tests that Buffer::current() returns the correct value. | |
| 44 TEST(ScratchBufferTest, Stacked) { | |
| 45 SimpleBindingsSupport bindings_support; | |
| 46 | |
| 47 EXPECT_FALSE(Buffer::current()); | |
| 48 | |
| 49 { | |
| 50 internal::ScratchBuffer a; | |
| 51 EXPECT_EQ(&a, Buffer::current()); | |
| 52 | |
| 53 { | |
| 54 internal::ScratchBuffer b; | |
| 55 EXPECT_EQ(&b, Buffer::current()); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 EXPECT_FALSE(Buffer::current()); | |
| 60 } | |
| 61 | |
| 62 // Tests that FixedBuffer allocates memory aligned to 8 byte boundaries. | |
| 63 TEST(FixedBufferTest, Alignment) { | |
| 64 SimpleBindingsSupport bindings_support; | |
| 65 | |
| 66 internal::FixedBuffer buf(internal::Align(10) * 2); | |
| 67 ASSERT_EQ(buf.size(), 16u * 2); | |
| 68 | |
| 69 void* a = buf.Allocate(10); | |
| 70 ASSERT_TRUE(a); | |
| 71 EXPECT_TRUE(IsZero(a, 10)); | |
| 72 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8); | |
| 73 | |
| 74 void* b = buf.Allocate(10); | |
| 75 ASSERT_TRUE(b); | |
| 76 EXPECT_TRUE(IsZero(b, 10)); | |
| 77 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8); | |
| 78 | |
| 79 // Any more allocations would result in an assert, but we can't test that. | |
| 80 } | |
| 81 | |
| 82 // Tests that FixedBuffer::Leak passes ownership to the caller. | |
| 83 TEST(FixedBufferTest, Leak) { | |
| 84 SimpleBindingsSupport bindings_support; | |
| 85 | |
| 86 void* ptr = NULL; | |
| 87 void* buf_ptr = NULL; | |
| 88 { | |
| 89 internal::FixedBuffer buf(8); | |
| 90 ASSERT_EQ(8u, buf.size()); | |
| 91 | |
| 92 ptr = buf.Allocate(8); | |
| 93 ASSERT_TRUE(ptr); | |
| 94 void* buf_ptr = buf.Leak(); | |
| 95 | |
| 96 // The buffer should point to the first element allocated. | |
| 97 // TODO(mpcomplete): Is this a reasonable expectation? | |
| 98 EXPECT_EQ(ptr, buf_ptr); | |
| 99 | |
| 100 // The FixedBuffer should be empty now. | |
| 101 EXPECT_EQ(0u, buf.size()); | |
| 102 EXPECT_FALSE(buf.Leak()); | |
| 103 } | |
| 104 | |
| 105 // Since we called Leak, ptr is still writable after FixedBuffer went out of | |
| 106 // scope. | |
| 107 memset(ptr, 1, 8); | |
| 108 free(buf_ptr); | |
| 109 } | |
| 110 | |
| 111 } // namespace test | |
| 112 } // namespace mojo | |
| OLD | NEW |