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