Update .DEPS.git
[chromium-blink-merge.git] / base / containers / stack_container_unittest.cc
blob3a1b3eddd4234e9ca3d8a9942c8fa9469d7881d9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/containers/stack_container.h"
7 #include <algorithm>
9 #include "base/memory/aligned_memory.h"
10 #include "base/memory/ref_counted.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace base {
15 namespace {
17 class Dummy : public base::RefCounted<Dummy> {
18 public:
19 explicit Dummy(int* alive) : alive_(alive) {
20 ++*alive_;
23 private:
24 friend class base::RefCounted<Dummy>;
26 ~Dummy() {
27 --*alive_;
30 int* const alive_;
33 } // namespace
35 TEST(StackContainer, Vector) {
36 const int stack_size = 3;
37 StackVector<int, stack_size> vect;
38 const int* stack_buffer = &vect.stack_data().stack_buffer()[0];
40 // The initial |stack_size| elements should appear in the stack buffer.
41 EXPECT_EQ(static_cast<size_t>(stack_size), vect.container().capacity());
42 for (int i = 0; i < stack_size; i++) {
43 vect.container().push_back(i);
44 EXPECT_EQ(stack_buffer, &vect.container()[0]);
45 EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
48 // Adding more elements should push the array onto the heap.
49 for (int i = 0; i < stack_size; i++) {
50 vect.container().push_back(i + stack_size);
51 EXPECT_NE(stack_buffer, &vect.container()[0]);
52 EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
55 // The array should still be in order.
56 for (int i = 0; i < stack_size * 2; i++)
57 EXPECT_EQ(i, vect.container()[i]);
59 // Resize to smaller. Our STL implementation won't reallocate in this case,
60 // otherwise it might use our stack buffer. We reserve right after the resize
61 // to guarantee it isn't using the stack buffer, even though it doesn't have
62 // much data.
63 vect.container().resize(stack_size);
64 vect.container().reserve(stack_size * 2);
65 EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
67 // Copying the small vector to another should use the same allocator and use
68 // the now-unused stack buffer. GENERALLY CALLERS SHOULD NOT DO THIS since
69 // they have to get the template types just right and it can cause errors.
70 std::vector<int, StackAllocator<int, stack_size> > other(vect.container());
71 EXPECT_EQ(stack_buffer, &other.front());
72 EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
73 for (int i = 0; i < stack_size; i++)
74 EXPECT_EQ(i, other[i]);
77 TEST(StackContainer, VectorDoubleDelete) {
78 // Regression testing for double-delete.
79 typedef StackVector<scoped_refptr<Dummy>, 2> Vector;
80 typedef Vector::ContainerType Container;
81 Vector vect;
83 int alive = 0;
84 scoped_refptr<Dummy> dummy(new Dummy(&alive));
85 EXPECT_EQ(alive, 1);
87 vect->push_back(dummy);
88 EXPECT_EQ(alive, 1);
90 Dummy* dummy_unref = dummy.get();
91 dummy = NULL;
92 EXPECT_EQ(alive, 1);
94 Container::iterator itr = std::find(vect->begin(), vect->end(), dummy_unref);
95 EXPECT_EQ(itr->get(), dummy_unref);
96 vect->erase(itr);
97 EXPECT_EQ(alive, 0);
99 // Shouldn't crash at exit.
102 namespace {
104 template <size_t alignment>
105 class AlignedData {
106 public:
107 AlignedData() { memset(data_.void_data(), 0, alignment); }
108 ~AlignedData() {}
109 base::AlignedMemory<alignment, alignment> data_;
112 } // anonymous namespace
114 #define EXPECT_ALIGNED(ptr, align) \
115 EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
117 TEST(StackContainer, BufferAlignment) {
118 StackVector<wchar_t, 16> text;
119 text->push_back(L'A');
120 EXPECT_ALIGNED(&text[0], ALIGNOF(wchar_t));
122 StackVector<double, 1> doubles;
123 doubles->push_back(0.0);
124 EXPECT_ALIGNED(&doubles[0], ALIGNOF(double));
126 StackVector<AlignedData<16>, 1> aligned16;
127 aligned16->push_back(AlignedData<16>());
128 EXPECT_ALIGNED(&aligned16[0], 16);
130 #if !defined(OS_ANDROID)
131 // It seems that android doesn't respect greater than 16 byte alignment for
132 // non-POD data on the stack, even though ALIGNOF(aligned256) == 256.
133 StackVector<AlignedData<256>, 1> aligned256;
134 aligned256->push_back(AlignedData<256>());
135 EXPECT_ALIGNED(&aligned256[0], 256);
136 #endif
139 template class StackVector<int, 2>;
140 template class StackVector<scoped_refptr<Dummy>, 2>;
142 } // namespace base