[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 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" |
[email protected] | 0d37563e | 2014-01-14 16:27:51 | [diff] [blame^] | 7 | #include "mojo/public/environment/environment.h" |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 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) { |
[email protected] | 0d37563e | 2014-01-14 16:27:51 | [diff] [blame^] | 24 | Environment env; |
[email protected] | 2205e730 | 2013-11-28 05:34:52 | [diff] [blame] | 25 | |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 26 | // Test that a small allocation is placed on the stack. |
[email protected] | 2205e730 | 2013-11-28 05:34:52 | [diff] [blame] | 27 | internal::ScratchBuffer buf; |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 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. |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 38 | small = buf.Allocate(10); |
| 39 | EXPECT_TRUE(IsZero(small, 10)); |
[email protected] | dce4d4c | 2013-11-21 03:45:13 | [diff] [blame] | 40 | EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf))); |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 2205e730 | 2013-11-28 05:34:52 | [diff] [blame] | 43 | // Tests that Buffer::current() returns the correct value. |
| 44 | TEST(ScratchBufferTest, Stacked) { |
[email protected] | 0d37563e | 2014-01-14 16:27:51 | [diff] [blame^] | 45 | Environment env; |
[email protected] | 2205e730 | 2013-11-28 05:34:52 | [diff] [blame] | 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 | |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 62 | // Tests that FixedBuffer allocates memory aligned to 8 byte boundaries. |
| 63 | TEST(FixedBufferTest, Alignment) { |
[email protected] | 0d37563e | 2014-01-14 16:27:51 | [diff] [blame^] | 64 | Environment env; |
[email protected] | 2205e730 | 2013-11-28 05:34:52 | [diff] [blame] | 65 | |
| 66 | internal::FixedBuffer buf(internal::Align(10) * 2); |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 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) { |
[email protected] | 0d37563e | 2014-01-14 16:27:51 | [diff] [blame^] | 84 | Environment env; |
[email protected] | 2205e730 | 2013-11-28 05:34:52 | [diff] [blame] | 85 | |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 86 | void* ptr = NULL; |
| 87 | void* buf_ptr = NULL; |
| 88 | { |
[email protected] | 2205e730 | 2013-11-28 05:34:52 | [diff] [blame] | 89 | internal::FixedBuffer buf(8); |
[email protected] | 0419eaa | 2013-11-20 05:04:57 | [diff] [blame] | 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 |