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