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

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

Issue 219023010: Mojo: RawSharedBuffer::Mapping -> RawSharedBufferMapping. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win 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
« 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<RawSharedBuffer::Mapping> mapping(buffer->Map(0, kNumBytes)); 31 scoped_ptr<RawSharedBufferMapping> mapping(buffer->Map(0, kNumBytes));
32 ASSERT_TRUE(mapping); 32 ASSERT_TRUE(mapping);
33 ASSERT_TRUE(mapping->base()); 33 ASSERT_TRUE(mapping->base());
34 int* stuff = static_cast<int*>(mapping->base()); 34 int* stuff = static_cast<int*>(mapping->base());
35 for (size_t i = 0; i < kNumInts; i++) 35 for (size_t i = 0; i < kNumInts; i++)
36 stuff[i] = static_cast<int>(i) + kFudge; 36 stuff[i] = static_cast<int>(i) + kFudge;
37 } 37 }
38 38
39 // Map it all again, check that our scribbling is still there, then do a 39 // 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, 40 // 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 41 // unmap the first mapping, scribble on some of the second mapping, and then
42 // unmap it. 42 // unmap it.
43 { 43 {
44 ASSERT_TRUE(buffer->IsValidMap(0, kNumBytes)); 44 ASSERT_TRUE(buffer->IsValidMap(0, kNumBytes));
45 // Use |MapNoCheck()| this time. 45 // Use |MapNoCheck()| this time.
46 scoped_ptr<RawSharedBuffer::Mapping> mapping1( 46 scoped_ptr<RawSharedBufferMapping> mapping1(
47 buffer->MapNoCheck(0, kNumBytes)); 47 buffer->MapNoCheck(0, kNumBytes));
48 ASSERT_TRUE(mapping1); 48 ASSERT_TRUE(mapping1);
49 ASSERT_TRUE(mapping1->base()); 49 ASSERT_TRUE(mapping1->base());
50 int* stuff1 = static_cast<int*>(mapping1->base()); 50 int* stuff1 = static_cast<int*>(mapping1->base());
51 for (size_t i = 0; i < kNumInts; i++) 51 for (size_t i = 0; i < kNumInts; i++)
52 EXPECT_EQ(static_cast<int>(i) + kFudge, stuff1[i]) << i; 52 EXPECT_EQ(static_cast<int>(i) + kFudge, stuff1[i]) << i;
53 53
54 scoped_ptr<RawSharedBuffer::Mapping> mapping2( 54 scoped_ptr<RawSharedBufferMapping> mapping2(
55 buffer->Map((kNumInts / 2) * sizeof(int), 2 * sizeof(int))); 55 buffer->Map((kNumInts / 2) * sizeof(int), 2 * sizeof(int)));
56 ASSERT_TRUE(mapping2); 56 ASSERT_TRUE(mapping2);
57 ASSERT_TRUE(mapping2->base()); 57 ASSERT_TRUE(mapping2->base());
58 int* stuff2 = static_cast<int*>(mapping2->base()); 58 int* stuff2 = static_cast<int*>(mapping2->base());
59 EXPECT_EQ(static_cast<int>(kNumInts / 2) + kFudge, stuff2[0]); 59 EXPECT_EQ(static_cast<int>(kNumInts / 2) + kFudge, stuff2[0]);
60 EXPECT_EQ(static_cast<int>(kNumInts / 2) + 1 + kFudge, stuff2[1]); 60 EXPECT_EQ(static_cast<int>(kNumInts / 2) + 1 + kFudge, stuff2[1]);
61 61
62 stuff2[0] = 123; 62 stuff2[0] = 123;
63 stuff2[1] = 456; 63 stuff2[1] = 456;
64 EXPECT_EQ(123, stuff1[kNumInts / 2]); 64 EXPECT_EQ(123, stuff1[kNumInts / 2]);
65 EXPECT_EQ(456, stuff1[kNumInts / 2 + 1]); 65 EXPECT_EQ(456, stuff1[kNumInts / 2 + 1]);
66 66
67 mapping1.reset(); 67 mapping1.reset();
68 68
69 EXPECT_EQ(123, stuff2[0]); 69 EXPECT_EQ(123, stuff2[0]);
70 EXPECT_EQ(456, stuff2[1]); 70 EXPECT_EQ(456, stuff2[1]);
71 stuff2[1] = 789; 71 stuff2[1] = 789;
72 } 72 }
73 73
74 // Do another partial mapping and check that everything is the way we expect 74 // Do another partial mapping and check that everything is the way we expect
75 // it to be. 75 // it to be.
76 { 76 {
77 EXPECT_TRUE(buffer->IsValidMap(sizeof(int), kNumBytes - sizeof(int))); 77 EXPECT_TRUE(buffer->IsValidMap(sizeof(int), kNumBytes - sizeof(int)));
78 scoped_ptr<RawSharedBuffer::Mapping> mapping( 78 scoped_ptr<RawSharedBufferMapping> mapping(
79 buffer->Map(sizeof(int), kNumBytes - sizeof(int))); 79 buffer->Map(sizeof(int), kNumBytes - sizeof(int)));
80 ASSERT_TRUE(mapping); 80 ASSERT_TRUE(mapping);
81 ASSERT_TRUE(mapping->base()); 81 ASSERT_TRUE(mapping->base());
82 int* stuff = static_cast<int*>(mapping->base()); 82 int* stuff = static_cast<int*>(mapping->base());
83 83
84 for (size_t j = 0; j < kNumInts - 1; j++) { 84 for (size_t j = 0; j < kNumInts - 1; j++) {
85 int i = static_cast<int>(j) + 1; 85 int i = static_cast<int>(j) + 1;
86 if (i == kNumInts / 2) { 86 if (i == kNumInts / 2) {
87 EXPECT_EQ(123, stuff[j]); 87 EXPECT_EQ(123, stuff[j]);
88 } else if (i == kNumInts / 2 + 1) { 88 } else if (i == kNumInts / 2 + 1) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 if (buffer) 133 if (buffer)
134 EXPECT_FALSE(buffer->Map(0, kMaxSizeT)); 134 EXPECT_FALSE(buffer->Map(0, kMaxSizeT));
135 } 135 }
136 136
137 // Tests that separate mappings get distinct addresses. 137 // Tests that separate mappings get distinct addresses.
138 // Note: It's not inconceivable that the OS could ref-count identical mappings 138 // 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 139 // 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. 140 // using the address as the key for unmapping.
141 TEST(RawSharedBufferTest, MappingsDistinct) { 141 TEST(RawSharedBufferTest, MappingsDistinct) {
142 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100)); 142 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100));
143 scoped_ptr<RawSharedBuffer::Mapping> mapping1(buffer->Map(0, 100)); 143 scoped_ptr<RawSharedBufferMapping> mapping1(buffer->Map(0, 100));
144 scoped_ptr<RawSharedBuffer::Mapping> mapping2(buffer->Map(0, 100)); 144 scoped_ptr<RawSharedBufferMapping> mapping2(buffer->Map(0, 100));
145 EXPECT_NE(mapping1->base(), mapping2->base()); 145 EXPECT_NE(mapping1->base(), mapping2->base());
146 } 146 }
147 147
148 TEST(RawSharedBufferTest, BufferZeroInitialized) { 148 TEST(RawSharedBufferTest, BufferZeroInitialized) {
149 static const size_t kSizes[] = { 10, 100, 1000, 10000, 100000 }; 149 static const size_t kSizes[] = { 10, 100, 1000, 10000, 100000 };
150 for (size_t i = 0; i < arraysize(kSizes); i++) { 150 for (size_t i = 0; i < arraysize(kSizes); i++) {
151 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kSizes[i])); 151 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(kSizes[i]));
152 scoped_ptr<RawSharedBuffer::Mapping> mapping(buffer->Map(0, kSizes[i])); 152 scoped_ptr<RawSharedBufferMapping> mapping(buffer->Map(0, kSizes[i]));
153 for (size_t j = 0; j < kSizes[i]; j++) { 153 for (size_t j = 0; j < kSizes[i]; j++) {
154 // "Assert" instead of "expect" so we don't spam the output with thousands 154 // "Assert" instead of "expect" so we don't spam the output with thousands
155 // of failures if we fail. 155 // of failures if we fail.
156 ASSERT_EQ('\0', static_cast<char*>(mapping->base())[j]) 156 ASSERT_EQ('\0', static_cast<char*>(mapping->base())[j])
157 << "size " << kSizes[i] << ", offset " << j; 157 << "size " << kSizes[i] << ", offset " << j;
158 } 158 }
159 } 159 }
160 } 160 }
161 161
162 TEST(RawSharedBufferTest, MappingsOutliveBuffer) { 162 TEST(RawSharedBufferTest, MappingsOutliveBuffer) {
163 scoped_ptr<RawSharedBuffer::Mapping> mapping1; 163 scoped_ptr<RawSharedBufferMapping> mapping1;
164 scoped_ptr<RawSharedBuffer::Mapping> mapping2; 164 scoped_ptr<RawSharedBufferMapping> mapping2;
165 165
166 { 166 {
167 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100)); 167 scoped_refptr<RawSharedBuffer> buffer(RawSharedBuffer::Create(100));
168 mapping1 = buffer->Map(0, 100).Pass(); 168 mapping1 = buffer->Map(0, 100).Pass();
169 mapping2 = buffer->Map(50, 50).Pass(); 169 mapping2 = buffer->Map(50, 50).Pass();
170 static_cast<char*>(mapping1->base())[50] = 'x'; 170 static_cast<char*>(mapping1->base())[50] = 'x';
171 } 171 }
172 172
173 EXPECT_EQ('x', static_cast<char*>(mapping2->base())[0]); 173 EXPECT_EQ('x', static_cast<char*>(mapping2->base())[0]);
174 174
175 static_cast<char*>(mapping2->base())[1] = 'y'; 175 static_cast<char*>(mapping2->base())[1] = 'y';
176 EXPECT_EQ('y', static_cast<char*>(mapping1->base())[51]); 176 EXPECT_EQ('y', static_cast<char*>(mapping1->base())[51]);
177 } 177 }
178 178
179 } // namespace 179 } // namespace
180 } // namespace system 180 } // namespace system
181 } // namespace mojo 181 } // 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