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 <limits> | 5 #include <limits> |
6 | 6 |
7 #include "mojo/public/cpp/bindings/buffer.h" | 7 #include "mojo/public/cpp/bindings/buffer.h" |
8 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | 8 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
9 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 9 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
10 #include "mojo/public/cpp/bindings/lib/scratch_buffer.h" | 10 #include "mojo/public/cpp/bindings/lib/scratch_buffer.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 | 46 |
47 // And a request so large it will fail. | 47 // And a request so large it will fail. |
48 void* fail = buf.Allocate(std::numeric_limits<size_t>::max() - 1024u); | 48 void* fail = buf.Allocate(std::numeric_limits<size_t>::max() - 1024u); |
49 EXPECT_TRUE(!fail); | 49 EXPECT_TRUE(!fail); |
50 | 50 |
51 // And a request so large it will overflow and fail. | 51 // And a request so large it will overflow and fail. |
52 void* overflow = buf.Allocate(std::numeric_limits<size_t>::max() - 12u); | 52 void* overflow = buf.Allocate(std::numeric_limits<size_t>::max() - 12u); |
53 EXPECT_TRUE(!overflow); | 53 EXPECT_TRUE(!overflow); |
54 } | 54 } |
55 | 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. | 56 // Tests that Buffer::current() returns the correct value. |
74 TEST(ScratchBufferTest, Stacked) { | 57 TEST(ScratchBufferTest, Stacked) { |
75 Environment env; | 58 Environment env; |
76 | 59 |
77 EXPECT_FALSE(Buffer::current()); | 60 EXPECT_FALSE(Buffer::current()); |
78 | 61 |
79 { | 62 { |
80 internal::ScratchBuffer a; | 63 internal::ScratchBuffer a; |
81 EXPECT_EQ(&a, Buffer::current()); | 64 EXPECT_EQ(&a, Buffer::current()); |
82 | 65 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 139 |
157 // A lot too large, leading to possible integer overflow. | 140 // A lot too large, leading to possible integer overflow. |
158 EXPECT_EQ(reinterpret_cast<void*>(0), | 141 EXPECT_EQ(reinterpret_cast<void*>(0), |
159 buf.Allocate(std::numeric_limits<size_t>::max() - 8u)); | 142 buf.Allocate(std::numeric_limits<size_t>::max() - 8u)); |
160 } | 143 } |
161 #endif | 144 #endif |
162 | 145 |
163 } // namespace | 146 } // namespace |
164 } // namespace test | 147 } // namespace test |
165 } // namespace mojo | 148 } // namespace mojo |
OLD | NEW |