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

Side by Side Diff: mojo/system/raw_shared_buffer_unittest.cc

Issue 471773002: Mojo: Add a platform interface for shared memory (embedder::PlatformSharedBuffer). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win fix Created 6 years, 4 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
« no previous file with comments | « mojo/system/raw_shared_buffer_posix.cc ('k') | mojo/system/raw_shared_buffer_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "mojo/system/raw_shared_buffer.h" 5 #include "mojo/system/raw_shared_buffer.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 10 matching lines...) Expand all
21 // A fudge so that we're not just writing zero bytes 75% of the time. 21 // A fudge so that we're not just writing zero bytes 75% of the time.
22 const int kFudge = 1234567890; 22 const int kFudge = 1234567890;
23 23
24 // Make some memory. 24 // Make some memory.
25 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kNumBytes)); 25 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kNumBytes));
26 ASSERT_TRUE(buffer); 26 ASSERT_TRUE(buffer);
27 27
28 // Map it all, scribble some stuff, and then unmap it. 28 // Map it all, scribble some stuff, and then unmap it.
29 { 29 {
30 EXPECT_TRUE(buffer->IsValidMap(0, kNumBytes)); 30 EXPECT_TRUE(buffer->IsValidMap(0, kNumBytes));
31 scoped_ptr<RawSharedBufferMapping> mapping(buffer->Map(0, kNumBytes)); 31 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping(
32 buffer->Map(0, kNumBytes));
32 ASSERT_TRUE(mapping); 33 ASSERT_TRUE(mapping);
33 ASSERT_TRUE(mapping->base()); 34 ASSERT_TRUE(mapping->GetBase());
34 int* stuff = static_cast<int*>(mapping->base()); 35 int* stuff = static_cast<int*>(mapping->GetBase());
35 for (size_t i = 0; i < kNumInts; i++) 36 for (size_t i = 0; i < kNumInts; i++)
36 stuff[i] = static_cast<int>(i) + kFudge; 37 stuff[i] = static_cast<int>(i) + kFudge;
37 } 38 }
38 39
39 // Map it all again, check that our scribbling is still there, then do a 40 // Map it all again, check that our scribbling is still there, then do a
40 // partial mapping and scribble on that, check that everything is coherent, 41 // partial mapping and scribble on that, check that everything is coherent,
41 // unmap the first mapping, scribble on some of the second mapping, and then 42 // unmap the first mapping, scribble on some of the second mapping, and then
42 // unmap it. 43 // unmap it.
43 { 44 {
44 ASSERT_TRUE(buffer->IsValidMap(0, kNumBytes)); 45 ASSERT_TRUE(buffer->IsValidMap(0, kNumBytes));
45 // Use |MapNoCheck()| this time. 46 // Use |MapNoCheck()| this time.
46 scoped_ptr<RawSharedBufferMapping> mapping1( 47 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping1(
47 buffer->MapNoCheck(0, kNumBytes)); 48 buffer->MapNoCheck(0, kNumBytes));
48 ASSERT_TRUE(mapping1); 49 ASSERT_TRUE(mapping1);
49 ASSERT_TRUE(mapping1->base()); 50 ASSERT_TRUE(mapping1->GetBase());
50 int* stuff1 = static_cast<int*>(mapping1->base()); 51 int* stuff1 = static_cast<int*>(mapping1->GetBase());
51 for (size_t i = 0; i < kNumInts; i++) 52 for (size_t i = 0; i < kNumInts; i++)
52 EXPECT_EQ(static_cast<int>(i) + kFudge, stuff1[i]) << i; 53 EXPECT_EQ(static_cast<int>(i) + kFudge, stuff1[i]) << i;
53 54
54 scoped_ptr<RawSharedBufferMapping> mapping2( 55 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping2(
55 buffer->Map((kNumInts / 2) * sizeof(int), 2 * sizeof(int))); 56 buffer->Map((kNumInts / 2) * sizeof(int), 2 * sizeof(int)));
56 ASSERT_TRUE(mapping2); 57 ASSERT_TRUE(mapping2);
57 ASSERT_TRUE(mapping2->base()); 58 ASSERT_TRUE(mapping2->GetBase());
58 int* stuff2 = static_cast<int*>(mapping2->base()); 59 int* stuff2 = static_cast<int*>(mapping2->GetBase());
59 EXPECT_EQ(static_cast<int>(kNumInts / 2) + kFudge, stuff2[0]); 60 EXPECT_EQ(static_cast<int>(kNumInts / 2) + kFudge, stuff2[0]);
60 EXPECT_EQ(static_cast<int>(kNumInts / 2) + 1 + kFudge, stuff2[1]); 61 EXPECT_EQ(static_cast<int>(kNumInts / 2) + 1 + kFudge, stuff2[1]);
61 62
62 stuff2[0] = 123; 63 stuff2[0] = 123;
63 stuff2[1] = 456; 64 stuff2[1] = 456;
64 EXPECT_EQ(123, stuff1[kNumInts / 2]); 65 EXPECT_EQ(123, stuff1[kNumInts / 2]);
65 EXPECT_EQ(456, stuff1[kNumInts / 2 + 1]); 66 EXPECT_EQ(456, stuff1[kNumInts / 2 + 1]);
66 67
67 mapping1.reset(); 68 mapping1.reset();
68 69
69 EXPECT_EQ(123, stuff2[0]); 70 EXPECT_EQ(123, stuff2[0]);
70 EXPECT_EQ(456, stuff2[1]); 71 EXPECT_EQ(456, stuff2[1]);
71 stuff2[1] = 789; 72 stuff2[1] = 789;
72 } 73 }
73 74
74 // Do another partial mapping and check that everything is the way we expect 75 // Do another partial mapping and check that everything is the way we expect
75 // it to be. 76 // it to be.
76 { 77 {
77 EXPECT_TRUE(buffer->IsValidMap(sizeof(int), kNumBytes - sizeof(int))); 78 EXPECT_TRUE(buffer->IsValidMap(sizeof(int), kNumBytes - sizeof(int)));
78 scoped_ptr<RawSharedBufferMapping> mapping( 79 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping(
79 buffer->Map(sizeof(int), kNumBytes - sizeof(int))); 80 buffer->Map(sizeof(int), kNumBytes - sizeof(int)));
80 ASSERT_TRUE(mapping); 81 ASSERT_TRUE(mapping);
81 ASSERT_TRUE(mapping->base()); 82 ASSERT_TRUE(mapping->GetBase());
82 int* stuff = static_cast<int*>(mapping->base()); 83 int* stuff = static_cast<int*>(mapping->GetBase());
83 84
84 for (size_t j = 0; j < kNumInts - 1; j++) { 85 for (size_t j = 0; j < kNumInts - 1; j++) {
85 int i = static_cast<int>(j) + 1; 86 int i = static_cast<int>(j) + 1;
86 if (i == kNumInts / 2) { 87 if (i == kNumInts / 2) {
87 EXPECT_EQ(123, stuff[j]); 88 EXPECT_EQ(123, stuff[j]);
88 } else if (i == kNumInts / 2 + 1) { 89 } else if (i == kNumInts / 2 + 1) {
89 EXPECT_EQ(789, stuff[j]); 90 EXPECT_EQ(789, stuff[j]);
90 } else { 91 } else {
91 EXPECT_EQ(i + kFudge, stuff[j]) << i; 92 EXPECT_EQ(i + kFudge, stuff[j]) << i;
92 } 93 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 if (buffer) 134 if (buffer)
134 EXPECT_FALSE(buffer->Map(0, kMaxSizeT)); 135 EXPECT_FALSE(buffer->Map(0, kMaxSizeT));
135 } 136 }
136 137
137 // Tests that separate mappings get distinct addresses. 138 // Tests that separate mappings get distinct addresses.
138 // Note: It's not inconceivable that the OS could ref-count identical mappings 139 // Note: It's not inconceivable that the OS could ref-count identical mappings
139 // and reuse the same address, in which case we'd have to be more careful about 140 // and reuse the same address, in which case we'd have to be more careful about
140 // using the address as the key for unmapping. 141 // using the address as the key for unmapping.
141 TEST(RawSharedBufferTest, MappingsDistinct) { 142 TEST(RawSharedBufferTest, MappingsDistinct) {
142 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100)); 143 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100));
143 scoped_ptr<RawSharedBufferMapping> mapping1(buffer->Map(0, 100)); 144 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping1(
144 scoped_ptr<RawSharedBufferMapping> mapping2(buffer->Map(0, 100)); 145 buffer->Map(0, 100));
145 EXPECT_NE(mapping1->base(), mapping2->base()); 146 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping2(
147 buffer->Map(0, 100));
148 EXPECT_NE(mapping1->GetBase(), mapping2->GetBase());
146 } 149 }
147 150
148 TEST(RawSharedBufferTest, BufferZeroInitialized) { 151 TEST(RawSharedBufferTest, BufferZeroInitialized) {
149 static const size_t kSizes[] = {10, 100, 1000, 10000, 100000}; 152 static const size_t kSizes[] = {10, 100, 1000, 10000, 100000};
150 for (size_t i = 0; i < arraysize(kSizes); i++) { 153 for (size_t i = 0; i < arraysize(kSizes); i++) {
151 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kSizes[i])); 154 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kSizes[i]));
152 scoped_ptr<RawSharedBufferMapping> mapping(buffer->Map(0, kSizes[i])); 155 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping(
156 buffer->Map(0, kSizes[i]));
153 for (size_t j = 0; j < kSizes[i]; j++) { 157 for (size_t j = 0; j < kSizes[i]; j++) {
154 // "Assert" instead of "expect" so we don't spam the output with thousands 158 // "Assert" instead of "expect" so we don't spam the output with thousands
155 // of failures if we fail. 159 // of failures if we fail.
156 ASSERT_EQ('\0', static_cast<char*>(mapping->base())[j]) 160 ASSERT_EQ('\0', static_cast<char*>(mapping->GetBase())[j])
157 << "size " << kSizes[i] << ", offset " << j; 161 << "size " << kSizes[i] << ", offset " << j;
158 } 162 }
159 } 163 }
160 } 164 }
161 165
162 TEST(RawSharedBufferTest, MappingsOutliveBuffer) { 166 TEST(RawSharedBufferTest, MappingsOutliveBuffer) {
163 scoped_ptr<RawSharedBufferMapping> mapping1; 167 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping1;
164 scoped_ptr<RawSharedBufferMapping> mapping2; 168 scoped_ptr<embedder::PlatformSharedBufferMapping> mapping2;
165 169
166 { 170 {
167 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100)); 171 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100));
168 mapping1 = buffer->Map(0, 100).Pass(); 172 mapping1 = buffer->Map(0, 100).Pass();
169 mapping2 = buffer->Map(50, 50).Pass(); 173 mapping2 = buffer->Map(50, 50).Pass();
170 static_cast<char*>(mapping1->base())[50] = 'x'; 174 static_cast<char*>(mapping1->GetBase())[50] = 'x';
171 } 175 }
172 176
173 EXPECT_EQ('x', static_cast<char*>(mapping2->base())[0]); 177 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]);
174 178
175 static_cast<char*>(mapping2->base())[1] = 'y'; 179 static_cast<char*>(mapping2->GetBase())[1] = 'y';
176 EXPECT_EQ('y', static_cast<char*>(mapping1->base())[51]); 180 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]);
177 } 181 }
178 182
179 } // namespace 183 } // namespace
180 } // namespace system 184 } // namespace system
181 } // namespace mojo 185 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/raw_shared_buffer_posix.cc ('k') | mojo/system/raw_shared_buffer_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698