OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/stack_container.h" | 5 #include "base/stack_container.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/memory/aligned_memory.h" |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 class Dummy : public base::RefCounted<Dummy> { | 15 class Dummy : public base::RefCounted<Dummy> { |
15 public: | 16 public: |
16 explicit Dummy(int* alive) : alive_(alive) { | 17 explicit Dummy(int* alive) : alive_(alive) { |
17 ++*alive_; | 18 ++*alive_; |
18 } | 19 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 EXPECT_EQ(alive, 1); | 90 EXPECT_EQ(alive, 1); |
90 | 91 |
91 Container::iterator itr = std::find(vect->begin(), vect->end(), dummy_unref); | 92 Container::iterator itr = std::find(vect->begin(), vect->end(), dummy_unref); |
92 EXPECT_EQ(itr->get(), dummy_unref); | 93 EXPECT_EQ(itr->get(), dummy_unref); |
93 vect->erase(itr); | 94 vect->erase(itr); |
94 EXPECT_EQ(alive, 0); | 95 EXPECT_EQ(alive, 0); |
95 | 96 |
96 // Shouldn't crash at exit. | 97 // Shouldn't crash at exit. |
97 } | 98 } |
98 | 99 |
| 100 namespace { |
| 101 |
| 102 template <size_t alignment> |
| 103 class AlignedData { |
| 104 public: |
| 105 AlignedData() { memset(data_.void_data(), 0, alignment); } |
| 106 ~AlignedData() {} |
| 107 base::AlignedMemory<alignment, alignment> data_; |
| 108 }; |
| 109 |
| 110 } // anonymous namespace |
| 111 |
| 112 #define EXPECT_ALIGNED(ptr, align) \ |
| 113 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 114 |
99 TEST(StackContainer, BufferAlignment) { | 115 TEST(StackContainer, BufferAlignment) { |
100 StackVector<wchar_t, 16> text; | 116 StackVector<wchar_t, 16> text; |
101 text->push_back(L'A'); | 117 text->push_back(L'A'); |
102 text->push_back(L'B'); | 118 EXPECT_ALIGNED(&text[0], ALIGNOF(wchar_t)); |
103 text->push_back(L'C'); | |
104 text->push_back(L'D'); | |
105 text->push_back(L'E'); | |
106 text->push_back(L'F'); | |
107 text->push_back(0); | |
108 | 119 |
109 const wchar_t* buffer = &text[1]; | 120 StackVector<double, 1> doubles; |
110 bool even_aligned = (0 == (((size_t)buffer) & 0x1)); | 121 doubles->push_back(0.0); |
111 EXPECT_EQ(even_aligned, true); | 122 EXPECT_ALIGNED(&doubles[0], ALIGNOF(double)); |
| 123 |
| 124 StackVector<AlignedData<256>, 1> aligned256; |
| 125 aligned256->push_back(AlignedData<256>()); |
| 126 EXPECT_ALIGNED(&aligned256[0], 256); |
112 } | 127 } |
113 | 128 |
114 #ifdef COMPILER_MSVC | 129 #ifdef COMPILER_MSVC |
115 // Make sure all the class compiles correctly. | 130 // Make sure all the class compiles correctly. |
116 // TODO(pinkerton): i'm not sure why this doesn't compile on GCC, but | 131 // TODO(pinkerton): i'm not sure why this doesn't compile on GCC, but |
117 // it doesn't. | 132 // it doesn't. |
118 template StackVector<int, 2>; | 133 template StackVector<int, 2>; |
119 template StackVector<scoped_refptr<Dummy>, 2>; | 134 template StackVector<scoped_refptr<Dummy>, 2>; |
120 #endif | 135 #endif |
OLD | NEW |