• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "base/containers/stack_container.h"
6 
7 #include <algorithm>
8 
9 #include "base/memory/aligned_memory.h"
10 #include "base/memory/ref_counted.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 
15 namespace {
16 
17 class Dummy : public base::RefCounted<Dummy> {
18  public:
Dummy(int * alive)19   explicit Dummy(int* alive) : alive_(alive) {
20     ++*alive_;
21   }
22 
23  private:
24   friend class base::RefCounted<Dummy>;
25 
~Dummy()26   ~Dummy() {
27     --*alive_;
28   }
29 
30   int* const alive_;
31 };
32 
33 }  // namespace
34 
TEST(StackContainer,Vector)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];
39 
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_);
46   }
47 
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_);
53   }
54 
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]);
58 
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_);
66 
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]);
75 }
76 
TEST(StackContainer,VectorDoubleDelete)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;
82 
83   int alive = 0;
84   scoped_refptr<Dummy> dummy(new Dummy(&alive));
85   EXPECT_EQ(alive, 1);
86 
87   vect->push_back(dummy);
88   EXPECT_EQ(alive, 1);
89 
90   Dummy* dummy_unref = dummy.get();
91   dummy = NULL;
92   EXPECT_EQ(alive, 1);
93 
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);
98 
99   // Shouldn't crash at exit.
100 }
101 
102 namespace {
103 
104 template <size_t alignment>
105 class AlignedData {
106  public:
AlignedData()107   AlignedData() { memset(data_.void_data(), 0, alignment); }
~AlignedData()108   ~AlignedData() {}
109   base::AlignedMemory<alignment, alignment> data_;
110 };
111 
112 }  // anonymous namespace
113 
114 #define EXPECT_ALIGNED(ptr, align) \
115     EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
116 
TEST(StackContainer,BufferAlignment)117 TEST(StackContainer, BufferAlignment) {
118   StackVector<wchar_t, 16> text;
119   text->push_back(L'A');
120   EXPECT_ALIGNED(&text[0], ALIGNOF(wchar_t));
121 
122   StackVector<double, 1> doubles;
123   doubles->push_back(0.0);
124   EXPECT_ALIGNED(&doubles[0], ALIGNOF(double));
125 
126   StackVector<AlignedData<16>, 1> aligned16;
127   aligned16->push_back(AlignedData<16>());
128   EXPECT_ALIGNED(&aligned16[0], 16);
129 
130 #if !defined(__GNUC__) || defined(ARCH_CPU_X86_FAMILY)
131   // It seems that non-X86 gcc doesn't respect greater than 16 byte alignment.
132   // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33721 for details.
133   // TODO(sbc):re-enable this if GCC starts respecting higher alignments.
134   StackVector<AlignedData<256>, 1> aligned256;
135   aligned256->push_back(AlignedData<256>());
136   EXPECT_ALIGNED(&aligned256[0], 256);
137 #endif
138 }
139 
140 template class StackVector<int, 2>;
141 template class StackVector<scoped_refptr<Dummy>, 2>;
142 
143 }  // namespace base
144