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

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

Issue 218883013: Mojo: Move public/bindings/tests/*.cc to public/cpp/bindings/tests/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <limits>
6
7 #include "mojo/public/cpp/bindings/buffer.h"
8 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h"
9 #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"
13
14 namespace mojo {
15 namespace test {
16 namespace {
17
18 bool IsZero(void* p_buf, size_t size) {
19 char* buf = reinterpret_cast<char*>(p_buf);
20 for (size_t i = 0; i < size; ++i) {
21 if (buf[i] != 0)
22 return false;
23 }
24 return true;
25 }
26
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
48 // Tests that Buffer::current() returns the correct value.
49 TEST(ScratchBufferTest, Stacked) {
50 Environment env;
51
52 EXPECT_FALSE(Buffer::current());
53
54 {
55 internal::ScratchBuffer a;
56 EXPECT_EQ(&a, Buffer::current());
57
58 {
59 internal::ScratchBuffer b;
60 EXPECT_EQ(&b, Buffer::current());
61 }
62 }
63
64 EXPECT_FALSE(Buffer::current());
65 }
66
67 // Tests that FixedBuffer allocates memory aligned to 8 byte boundaries.
68 TEST(FixedBufferTest, Alignment) {
69 Environment env;
70
71 internal::FixedBuffer buf(internal::Align(10) * 2);
72 ASSERT_EQ(buf.size(), 16u * 2);
73
74 void* a = buf.Allocate(10);
75 ASSERT_TRUE(a);
76 EXPECT_TRUE(IsZero(a, 10));
77 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(a) % 8);
78
79 void* b = buf.Allocate(10);
80 ASSERT_TRUE(b);
81 EXPECT_TRUE(IsZero(b, 10));
82 EXPECT_EQ(0, reinterpret_cast<ptrdiff_t>(b) % 8);
83
84 // Any more allocations would result in an assert, but we can't test that.
85 }
86
87 // Tests that FixedBuffer::Leak passes ownership to the caller.
88 TEST(FixedBufferTest, Leak) {
89 Environment env;
90
91 void* ptr = NULL;
92 void* buf_ptr = NULL;
93 {
94 internal::FixedBuffer buf(8);
95 ASSERT_EQ(8u, buf.size());
96
97 ptr = buf.Allocate(8);
98 ASSERT_TRUE(ptr);
99 void* buf_ptr = buf.Leak();
100
101 // The buffer should point to the first element allocated.
102 // TODO(mpcomplete): Is this a reasonable expectation?
103 EXPECT_EQ(ptr, buf_ptr);
104
105 // The FixedBuffer should be empty now.
106 EXPECT_EQ(0u, buf.size());
107 EXPECT_FALSE(buf.Leak());
108 }
109
110 // Since we called Leak, ptr is still writable after FixedBuffer went out of
111 // scope.
112 memset(ptr, 1, 8);
113 free(buf_ptr);
114 }
115
116 #ifdef NDEBUG
117 TEST(FixedBufferTest, TooBig) {
118 Environment env;
119
120 internal::FixedBuffer buf(24);
121
122 // A little bit too large.
123 EXPECT_EQ(reinterpret_cast<void*>(0), buf.Allocate(32));
124
125 // Move the cursor forward.
126 EXPECT_NE(reinterpret_cast<void*>(0), buf.Allocate(16));
127
128 // A lot too large, leading to possible integer overflow.
129 EXPECT_EQ(reinterpret_cast<void*>(0),
130 buf.Allocate(std::numeric_limits<size_t>::max() - 8u));
131 }
132 #endif
133
134 } // namespace
135 } // namespace test
136 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/bindings/tests/array_unittest.cc ('k') | mojo/public/bindings/tests/connector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698