Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(931)

Side by Side Diff: mojo/public/cpp/bindings/tests/buffer_unittest.cc

Issue 294833002: Mojo: more idiomatic C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <limits> 5 #include <limits>
6 6
7 #include "mojo/public/cpp/bindings/buffer.h"
8 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" 7 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
9 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
10 #include "mojo/public/cpp/bindings/lib/scratch_buffer.h"
11 #include "mojo/public/cpp/environment/environment.h"
12 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
13 10
14 namespace mojo { 11 namespace mojo {
15 namespace test { 12 namespace test {
16 namespace { 13 namespace {
17 14
18 bool IsZero(void* p_buf, size_t size) { 15 bool IsZero(void* p_buf, size_t size) {
19 char* buf = reinterpret_cast<char*>(p_buf); 16 char* buf = reinterpret_cast<char*>(p_buf);
20 for (size_t i = 0; i < size; ++i) { 17 for (size_t i = 0; i < size; ++i) {
21 if (buf[i] != 0) 18 if (buf[i] != 0)
22 return false; 19 return false;
23 } 20 }
24 return true; 21 return true;
25 } 22 }
26 23
27 // Tests small and large allocations in ScratchBuffer.
28 TEST(ScratchBufferTest, Basic) {
29 Environment env;
30
31 // Test that a small allocation is placed on the stack.
32 internal::ScratchBuffer buf;
33 void* small = buf.Allocate(10);
34 EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
35 EXPECT_TRUE(IsZero(small, 10));
36
37 // Large allocations won't be on the stack.
38 void* large = buf.Allocate(100*1024);
39 EXPECT_TRUE(IsZero(large, 100*1024));
40 EXPECT_FALSE(large >= &buf && large < (&buf + sizeof(buf)));
41
42 // But another small allocation should be back on the stack.
43 small = buf.Allocate(10);
44 EXPECT_TRUE(IsZero(small, 10));
45 EXPECT_TRUE(small >= &buf && small < (&buf + sizeof(buf)));
46
47 // And a request so large it will fail.
48 void* fail = buf.Allocate(std::numeric_limits<size_t>::max() - 1024u);
49 EXPECT_TRUE(!fail);
50
51 // And a request so large it will overflow and fail.
52 void* overflow = buf.Allocate(std::numeric_limits<size_t>::max() - 12u);
53 EXPECT_TRUE(!overflow);
54 }
55
56 TEST(ScratchBufferTest, Alignment) {
57 Environment env;
58
59 internal::ScratchBuffer buf;
60 // Test that small allocations on the stack are aligned properly.
61 void* small = buf.Allocate(1);
62 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(small) % 8);
63 small = buf.Allocate(2);
64 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(small) % 8);
65
66 // Test that large allocations on the heap are aligned properly.
67 void* large = buf.Allocate(10*1024);
68 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(large) % 8);
69 large = buf.Allocate(100*1024);
70 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(large) % 8);
71 }
72
73 // Tests that Buffer::current() returns the correct value.
74 TEST(ScratchBufferTest, Stacked) {
75 Environment env;
76
77 EXPECT_FALSE(Buffer::current());
78
79 {
80 internal::ScratchBuffer a;
81 EXPECT_EQ(&a, Buffer::current());
82
83 {
84 internal::ScratchBuffer b;
85 EXPECT_EQ(&b, Buffer::current());
86 }
87 }
88
89 EXPECT_FALSE(Buffer::current());
90 }
91
92 // Tests that FixedBuffer allocates memory aligned to 8 byte boundaries. 24 // Tests that FixedBuffer allocates memory aligned to 8 byte boundaries.
93 TEST(FixedBufferTest, Alignment) { 25 TEST(FixedBufferTest, Alignment) {
94 Environment env;
95
96 internal::FixedBuffer buf(internal::Align(10) * 2); 26 internal::FixedBuffer buf(internal::Align(10) * 2);
97 ASSERT_EQ(buf.size(), 16u * 2); 27 ASSERT_EQ(buf.size(), 16u * 2);
98 28
99 void* a = buf.Allocate(10); 29 void* a = buf.Allocate(10);
100 ASSERT_TRUE(a); 30 ASSERT_TRUE(a);
101 EXPECT_TRUE(IsZero(a, 10)); 31 EXPECT_TRUE(IsZero(a, 10));
102 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8); 32 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8);
103 33
104 void* b = buf.Allocate(10); 34 void* b = buf.Allocate(10);
105 ASSERT_TRUE(b); 35 ASSERT_TRUE(b);
106 EXPECT_TRUE(IsZero(b, 10)); 36 EXPECT_TRUE(IsZero(b, 10));
107 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8); 37 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8);
108 38
109 // Any more allocations would result in an assert, but we can't test that. 39 // Any more allocations would result in an assert, but we can't test that.
110 } 40 }
111 41
112 // Tests that FixedBuffer::Leak passes ownership to the caller. 42 // Tests that FixedBuffer::Leak passes ownership to the caller.
113 TEST(FixedBufferTest, Leak) { 43 TEST(FixedBufferTest, Leak) {
114 Environment env;
115
116 void* ptr = NULL; 44 void* ptr = NULL;
117 void* buf_ptr = NULL; 45 void* buf_ptr = NULL;
118 { 46 {
119 internal::FixedBuffer buf(8); 47 internal::FixedBuffer buf(8);
120 ASSERT_EQ(8u, buf.size()); 48 ASSERT_EQ(8u, buf.size());
121 49
122 ptr = buf.Allocate(8); 50 ptr = buf.Allocate(8);
123 ASSERT_TRUE(ptr); 51 ASSERT_TRUE(ptr);
124 buf_ptr = buf.Leak(); 52 buf_ptr = buf.Leak();
125 53
126 // The buffer should point to the first element allocated. 54 // The buffer should point to the first element allocated.
127 // TODO(mpcomplete): Is this a reasonable expectation? 55 // TODO(mpcomplete): Is this a reasonable expectation?
128 EXPECT_EQ(ptr, buf_ptr); 56 EXPECT_EQ(ptr, buf_ptr);
129 57
130 // The FixedBuffer should be empty now. 58 // The FixedBuffer should be empty now.
131 EXPECT_EQ(0u, buf.size()); 59 EXPECT_EQ(0u, buf.size());
132 EXPECT_FALSE(buf.Leak()); 60 EXPECT_FALSE(buf.Leak());
133 } 61 }
134 62
135 // Since we called Leak, ptr is still writable after FixedBuffer went out of 63 // Since we called Leak, ptr is still writable after FixedBuffer went out of
136 // scope. 64 // scope.
137 memset(ptr, 1, 8); 65 memset(ptr, 1, 8);
138 free(buf_ptr); 66 free(buf_ptr);
139 } 67 }
140 68
141 #ifdef NDEBUG 69 #ifdef NDEBUG
142 TEST(FixedBufferTest, TooBig) { 70 TEST(FixedBufferTest, TooBig) {
143 Environment env;
144
145 internal::FixedBuffer buf(24); 71 internal::FixedBuffer buf(24);
146 72
147 // A little bit too large. 73 // A little bit too large.
148 EXPECT_EQ(reinterpret_cast<void*>(0), buf.Allocate(32)); 74 EXPECT_EQ(reinterpret_cast<void*>(0), buf.Allocate(32));
149 75
150 // Move the cursor forward. 76 // Move the cursor forward.
151 EXPECT_NE(reinterpret_cast<void*>(0), buf.Allocate(16)); 77 EXPECT_NE(reinterpret_cast<void*>(0), buf.Allocate(16));
152 78
153 // A lot too large. 79 // A lot too large.
154 EXPECT_EQ(reinterpret_cast<void*>(0), 80 EXPECT_EQ(reinterpret_cast<void*>(0),
155 buf.Allocate(std::numeric_limits<size_t>::max() - 1024u)); 81 buf.Allocate(std::numeric_limits<size_t>::max() - 1024u));
156 82
157 // A lot too large, leading to possible integer overflow. 83 // A lot too large, leading to possible integer overflow.
158 EXPECT_EQ(reinterpret_cast<void*>(0), 84 EXPECT_EQ(reinterpret_cast<void*>(0),
159 buf.Allocate(std::numeric_limits<size_t>::max() - 8u)); 85 buf.Allocate(std::numeric_limits<size_t>::max() - 8u));
160 } 86 }
161 #endif 87 #endif
162 88
163 } // namespace 89 } // namespace
164 } // namespace test 90 } // namespace test
165 } // namespace mojo 91 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/array_unittest.cc ('k') | mojo/public/cpp/bindings/tests/handle_passing_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698