blob: 06cb4caabad82cd66d67960269f111cb57e3d13c [file] [log] [blame]
[email protected]0419eaa2013-11-20 05:04:571// 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]0d37563e2014-01-14 16:27:517#include "mojo/public/environment/environment.h"
[email protected]0419eaa2013-11-20 05:04:578#include "testing/gtest/include/gtest/gtest.h"
9
10namespace mojo {
11namespace test {
12
13bool 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.
23TEST(ScratchBufferTest, Basic) {
[email protected]0d37563e2014-01-14 16:27:5124 Environment env;
[email protected]2205e7302013-11-28 05:34:5225
[email protected]0419eaa2013-11-20 05:04:5726 // Test that a small allocation is placed on the stack.
[email protected]2205e7302013-11-28 05:34:5227 internal::ScratchBuffer buf;
[email protected]0419eaa2013-11-20 05:04:5728 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]0419eaa2013-11-20 05:04:5738 small = buf.Allocate(10);
39 EXPECT_TRUE(IsZero(small, 10));
[email protected]dce4d4c2013-11-21 03:45:1340 EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
[email protected]0419eaa2013-11-20 05:04:5741}
42
[email protected]2205e7302013-11-28 05:34:5243// Tests that Buffer::current() returns the correct value.
44TEST(ScratchBufferTest, Stacked) {
[email protected]0d37563e2014-01-14 16:27:5145 Environment env;
[email protected]2205e7302013-11-28 05:34:5246
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]0419eaa2013-11-20 05:04:5762// Tests that FixedBuffer allocates memory aligned to 8 byte boundaries.
63TEST(FixedBufferTest, Alignment) {
[email protected]0d37563e2014-01-14 16:27:5164 Environment env;
[email protected]2205e7302013-11-28 05:34:5265
66 internal::FixedBuffer buf(internal::Align(10) * 2);
[email protected]0419eaa2013-11-20 05:04:5767 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.
83TEST(FixedBufferTest, Leak) {
[email protected]0d37563e2014-01-14 16:27:5184 Environment env;
[email protected]2205e7302013-11-28 05:34:5285
[email protected]0419eaa2013-11-20 05:04:5786 void* ptr = NULL;
87 void* buf_ptr = NULL;
88 {
[email protected]2205e7302013-11-28 05:34:5289 internal::FixedBuffer buf(8);
[email protected]0419eaa2013-11-20 05:04:5790 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