OLD | NEW |
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/edk/embedder/simple_platform_shared_buffer.h" | 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "mojo/public/cpp/system/macros.h" | 10 #include "mojo/public/cpp/system/macros.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
13 | 12 |
14 namespace mojo { | 13 namespace mojo { |
15 namespace embedder { | 14 namespace embedder { |
16 namespace { | 15 namespace { |
17 | 16 |
18 TEST(SimplePlatformSharedBufferTest, Basic) { | 17 TEST(SimplePlatformSharedBufferTest, Basic) { |
19 const size_t kNumInts = 100; | 18 const size_t kNumInts = 100; |
20 const size_t kNumBytes = kNumInts * sizeof(int); | 19 const size_t kNumBytes = kNumInts * sizeof(int); |
21 // A fudge so that we're not just writing zero bytes 75% of the time. | 20 // A fudge so that we're not just writing zero bytes 75% of the time. |
22 const int kFudge = 1234567890; | 21 const int kFudge = 1234567890; |
23 | 22 |
24 // Make some memory. | 23 // Make some memory. |
25 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 24 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
26 SimplePlatformSharedBuffer::Create(kNumBytes)); | 25 SimplePlatformSharedBuffer::Create(kNumBytes)); |
27 ASSERT_TRUE(buffer); | 26 ASSERT_TRUE(buffer); |
28 | 27 |
29 // Map it all, scribble some stuff, and then unmap it. | 28 // Map it all, scribble some stuff, and then unmap it. |
30 { | 29 { |
31 EXPECT_TRUE(buffer->IsValidMap(0, kNumBytes)); | 30 EXPECT_TRUE(buffer->IsValidMap(0, kNumBytes)); |
32 scoped_ptr<PlatformSharedBufferMapping> mapping(buffer->Map(0, kNumBytes)); | 31 std::unique_ptr<PlatformSharedBufferMapping> mapping( |
| 32 buffer->Map(0, kNumBytes)); |
33 ASSERT_TRUE(mapping); | 33 ASSERT_TRUE(mapping); |
34 ASSERT_TRUE(mapping->GetBase()); | 34 ASSERT_TRUE(mapping->GetBase()); |
35 int* stuff = static_cast<int*>(mapping->GetBase()); | 35 int* stuff = static_cast<int*>(mapping->GetBase()); |
36 for (size_t i = 0; i < kNumInts; i++) | 36 for (size_t i = 0; i < kNumInts; i++) |
37 stuff[i] = static_cast<int>(i) + kFudge; | 37 stuff[i] = static_cast<int>(i) + kFudge; |
38 } | 38 } |
39 | 39 |
40 // 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 |
41 // partial mapping and scribble on that, check that everything is coherent, | 41 // partial mapping and scribble on that, check that everything is coherent, |
42 // 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 |
43 // unmap it. | 43 // unmap it. |
44 { | 44 { |
45 ASSERT_TRUE(buffer->IsValidMap(0, kNumBytes)); | 45 ASSERT_TRUE(buffer->IsValidMap(0, kNumBytes)); |
46 // Use |MapNoCheck()| this time. | 46 // Use |MapNoCheck()| this time. |
47 scoped_ptr<PlatformSharedBufferMapping> mapping1( | 47 std::unique_ptr<PlatformSharedBufferMapping> mapping1( |
48 buffer->MapNoCheck(0, kNumBytes)); | 48 buffer->MapNoCheck(0, kNumBytes)); |
49 ASSERT_TRUE(mapping1); | 49 ASSERT_TRUE(mapping1); |
50 ASSERT_TRUE(mapping1->GetBase()); | 50 ASSERT_TRUE(mapping1->GetBase()); |
51 int* stuff1 = static_cast<int*>(mapping1->GetBase()); | 51 int* stuff1 = static_cast<int*>(mapping1->GetBase()); |
52 for (size_t i = 0; i < kNumInts; i++) | 52 for (size_t i = 0; i < kNumInts; i++) |
53 EXPECT_EQ(static_cast<int>(i) + kFudge, stuff1[i]) << i; | 53 EXPECT_EQ(static_cast<int>(i) + kFudge, stuff1[i]) << i; |
54 | 54 |
55 scoped_ptr<PlatformSharedBufferMapping> mapping2( | 55 std::unique_ptr<PlatformSharedBufferMapping> mapping2( |
56 buffer->Map((kNumInts / 2) * sizeof(int), 2 * sizeof(int))); | 56 buffer->Map((kNumInts / 2) * sizeof(int), 2 * sizeof(int))); |
57 ASSERT_TRUE(mapping2); | 57 ASSERT_TRUE(mapping2); |
58 ASSERT_TRUE(mapping2->GetBase()); | 58 ASSERT_TRUE(mapping2->GetBase()); |
59 int* stuff2 = static_cast<int*>(mapping2->GetBase()); | 59 int* stuff2 = static_cast<int*>(mapping2->GetBase()); |
60 EXPECT_EQ(static_cast<int>(kNumInts / 2) + kFudge, stuff2[0]); | 60 EXPECT_EQ(static_cast<int>(kNumInts / 2) + kFudge, stuff2[0]); |
61 EXPECT_EQ(static_cast<int>(kNumInts / 2) + 1 + kFudge, stuff2[1]); | 61 EXPECT_EQ(static_cast<int>(kNumInts / 2) + 1 + kFudge, stuff2[1]); |
62 | 62 |
63 stuff2[0] = 123; | 63 stuff2[0] = 123; |
64 stuff2[1] = 456; | 64 stuff2[1] = 456; |
65 EXPECT_EQ(123, stuff1[kNumInts / 2]); | 65 EXPECT_EQ(123, stuff1[kNumInts / 2]); |
66 EXPECT_EQ(456, stuff1[kNumInts / 2 + 1]); | 66 EXPECT_EQ(456, stuff1[kNumInts / 2 + 1]); |
67 | 67 |
68 mapping1.reset(); | 68 mapping1.reset(); |
69 | 69 |
70 EXPECT_EQ(123, stuff2[0]); | 70 EXPECT_EQ(123, stuff2[0]); |
71 EXPECT_EQ(456, stuff2[1]); | 71 EXPECT_EQ(456, stuff2[1]); |
72 stuff2[1] = 789; | 72 stuff2[1] = 789; |
73 } | 73 } |
74 | 74 |
75 // 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 |
76 // it to be. | 76 // it to be. |
77 { | 77 { |
78 EXPECT_TRUE(buffer->IsValidMap(sizeof(int), kNumBytes - sizeof(int))); | 78 EXPECT_TRUE(buffer->IsValidMap(sizeof(int), kNumBytes - sizeof(int))); |
79 scoped_ptr<PlatformSharedBufferMapping> mapping( | 79 std::unique_ptr<PlatformSharedBufferMapping> mapping( |
80 buffer->Map(sizeof(int), kNumBytes - sizeof(int))); | 80 buffer->Map(sizeof(int), kNumBytes - sizeof(int))); |
81 ASSERT_TRUE(mapping); | 81 ASSERT_TRUE(mapping); |
82 ASSERT_TRUE(mapping->GetBase()); | 82 ASSERT_TRUE(mapping->GetBase()); |
83 int* stuff = static_cast<int*>(mapping->GetBase()); | 83 int* stuff = static_cast<int*>(mapping->GetBase()); |
84 | 84 |
85 for (size_t j = 0; j < kNumInts - 1; j++) { | 85 for (size_t j = 0; j < kNumInts - 1; j++) { |
86 int i = static_cast<int>(j) + 1; | 86 int i = static_cast<int>(j) + 1; |
87 if (i == kNumInts / 2) { | 87 if (i == kNumInts / 2) { |
88 EXPECT_EQ(123, stuff[j]); | 88 EXPECT_EQ(123, stuff[j]); |
89 } else if (i == kNumInts / 2 + 1) { | 89 } else if (i == kNumInts / 2 + 1) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 EXPECT_FALSE(buffer->Map(0, kMaxSizeT)); | 137 EXPECT_FALSE(buffer->Map(0, kMaxSizeT)); |
138 } | 138 } |
139 | 139 |
140 // Tests that separate mappings get distinct addresses. | 140 // Tests that separate mappings get distinct addresses. |
141 // Note: It's not inconceivable that the OS could ref-count identical mappings | 141 // Note: It's not inconceivable that the OS could ref-count identical mappings |
142 // and reuse the same address, in which case we'd have to be more careful about | 142 // and reuse the same address, in which case we'd have to be more careful about |
143 // using the address as the key for unmapping. | 143 // using the address as the key for unmapping. |
144 TEST(SimplePlatformSharedBufferTest, MappingsDistinct) { | 144 TEST(SimplePlatformSharedBufferTest, MappingsDistinct) { |
145 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 145 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
146 SimplePlatformSharedBuffer::Create(100)); | 146 SimplePlatformSharedBuffer::Create(100)); |
147 scoped_ptr<PlatformSharedBufferMapping> mapping1(buffer->Map(0, 100)); | 147 std::unique_ptr<PlatformSharedBufferMapping> mapping1(buffer->Map(0, 100)); |
148 scoped_ptr<PlatformSharedBufferMapping> mapping2(buffer->Map(0, 100)); | 148 std::unique_ptr<PlatformSharedBufferMapping> mapping2(buffer->Map(0, 100)); |
149 EXPECT_NE(mapping1->GetBase(), mapping2->GetBase()); | 149 EXPECT_NE(mapping1->GetBase(), mapping2->GetBase()); |
150 } | 150 } |
151 | 151 |
152 TEST(SimplePlatformSharedBufferTest, BufferZeroInitialized) { | 152 TEST(SimplePlatformSharedBufferTest, BufferZeroInitialized) { |
153 static const size_t kSizes[] = {10, 100, 1000, 10000, 100000}; | 153 static const size_t kSizes[] = {10, 100, 1000, 10000, 100000}; |
154 for (size_t i = 0; i < MOJO_ARRAYSIZE(kSizes); i++) { | 154 for (size_t i = 0; i < MOJO_ARRAYSIZE(kSizes); i++) { |
155 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 155 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
156 SimplePlatformSharedBuffer::Create(kSizes[i])); | 156 SimplePlatformSharedBuffer::Create(kSizes[i])); |
157 scoped_ptr<PlatformSharedBufferMapping> mapping(buffer->Map(0, kSizes[i])); | 157 std::unique_ptr<PlatformSharedBufferMapping> mapping( |
| 158 buffer->Map(0, kSizes[i])); |
158 for (size_t j = 0; j < kSizes[i]; j++) { | 159 for (size_t j = 0; j < kSizes[i]; j++) { |
159 // "Assert" instead of "expect" so we don't spam the output with thousands | 160 // "Assert" instead of "expect" so we don't spam the output with thousands |
160 // of failures if we fail. | 161 // of failures if we fail. |
161 ASSERT_EQ('\0', static_cast<char*>(mapping->GetBase())[j]) | 162 ASSERT_EQ('\0', static_cast<char*>(mapping->GetBase())[j]) |
162 << "size " << kSizes[i] << ", offset " << j; | 163 << "size " << kSizes[i] << ", offset " << j; |
163 } | 164 } |
164 } | 165 } |
165 } | 166 } |
166 | 167 |
167 TEST(SimplePlatformSharedBufferTest, MappingsOutliveBuffer) { | 168 TEST(SimplePlatformSharedBufferTest, MappingsOutliveBuffer) { |
168 scoped_ptr<PlatformSharedBufferMapping> mapping1; | 169 std::unique_ptr<PlatformSharedBufferMapping> mapping1; |
169 scoped_ptr<PlatformSharedBufferMapping> mapping2; | 170 std::unique_ptr<PlatformSharedBufferMapping> mapping2; |
170 | 171 |
171 { | 172 { |
172 scoped_refptr<SimplePlatformSharedBuffer> buffer( | 173 scoped_refptr<SimplePlatformSharedBuffer> buffer( |
173 SimplePlatformSharedBuffer::Create(100)); | 174 SimplePlatformSharedBuffer::Create(100)); |
174 mapping1 = buffer->Map(0, 100).Pass(); | 175 mapping1 = buffer->Map(0, 100); |
175 mapping2 = buffer->Map(50, 50).Pass(); | 176 mapping2 = buffer->Map(50, 50); |
176 static_cast<char*>(mapping1->GetBase())[50] = 'x'; | 177 static_cast<char*>(mapping1->GetBase())[50] = 'x'; |
177 } | 178 } |
178 | 179 |
179 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); | 180 EXPECT_EQ('x', static_cast<char*>(mapping2->GetBase())[0]); |
180 | 181 |
181 static_cast<char*>(mapping2->GetBase())[1] = 'y'; | 182 static_cast<char*>(mapping2->GetBase())[1] = 'y'; |
182 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); | 183 EXPECT_EQ('y', static_cast<char*>(mapping1->GetBase())[51]); |
183 } | 184 } |
184 | 185 |
185 } // namespace | 186 } // namespace |
186 } // namespace embedder | 187 } // namespace embedder |
187 } // namespace mojo | 188 } // namespace mojo |
OLD | NEW |