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 <stddef.h>
8 
9 #include <algorithm>
10 
11 #include "base/memory/ref_counted.h"
12 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base {
16 
17 namespace {
18 
19 class Dummy : public base::RefCounted<Dummy> {
20  public:
Dummy(int * alive)21   explicit Dummy(int* alive) : alive_(alive) {
22     ++*alive_;
23   }
24 
25  private:
26   friend class base::RefCounted<Dummy>;
27 
~Dummy()28   ~Dummy() {
29     --*alive_;
30   }
31 
32   int* const alive_;
33 };
34 
35 }  // namespace
36 
TEST(StackContainer,Vector)37 TEST(StackContainer, Vector) {
38   const int stack_size = 3;
39   StackVector<int, stack_size> vect;
40   const int* stack_buffer = &vect.stack_data().stack_buffer()[0];
41 
42   // The initial |stack_size| elements should appear in the stack buffer.
43   EXPECT_EQ(static_cast<size_t>(stack_size), vect.container().capacity());
44   for (int i = 0; i < stack_size; i++) {
45     vect.container().push_back(i);
46     EXPECT_EQ(stack_buffer, &vect.container()[0]);
47     EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
48   }
49 
50   // Adding more elements should push the array onto the heap.
51   for (int i = 0; i < stack_size; i++) {
52     vect.container().push_back(i + stack_size);
53     EXPECT_NE(stack_buffer, &vect.container()[0]);
54     EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
55   }
56 
57   // The array should still be in order.
58   for (int i = 0; i < stack_size * 2; i++)
59     EXPECT_EQ(i, vect.container()[i]);
60 
61   // Resize to smaller. Our STL implementation won't reallocate in this case,
62   // otherwise it might use our stack buffer. We reserve right after the resize
63   // to guarantee it isn't using the stack buffer, even though it doesn't have
64   // much data.
65   vect.container().resize(stack_size);
66   vect.container().reserve(stack_size * 2);
67   EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
68 
69   // Copying the small vector to another should use the same allocator and use
70   // the now-unused stack buffer. GENERALLY CALLERS SHOULD NOT DO THIS since
71   // they have to get the template types just right and it can cause errors.
72   std::vector<int, StackAllocator<int, stack_size> > other(vect.container());
73   EXPECT_EQ(stack_buffer, &other.front());
74   EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
75   for (int i = 0; i < stack_size; i++)
76     EXPECT_EQ(i, other[i]);
77 }
78 
TEST(StackContainer,VectorDoubleDelete)79 TEST(StackContainer, VectorDoubleDelete) {
80   // Regression testing for double-delete.
81   typedef StackVector<scoped_refptr<Dummy>, 2> Vector;
82   typedef Vector::ContainerType Container;
83   Vector vect;
84 
85   int alive = 0;
86   scoped_refptr<Dummy> dummy(new Dummy(&alive));
87   EXPECT_EQ(alive, 1);
88 
89   vect->push_back(dummy);
90   EXPECT_EQ(alive, 1);
91 
92   Dummy* dummy_unref = dummy.get();
93   dummy = nullptr;
94   EXPECT_EQ(alive, 1);
95 
96   Container::iterator itr = std::find(vect->begin(), vect->end(), dummy_unref);
97   EXPECT_EQ(itr->get(), dummy_unref);
98   vect->erase(itr);
99   EXPECT_EQ(alive, 0);
100 
101   // Shouldn't crash at exit.
102 }
103 
104 namespace {
105 
106 template <size_t alignment>
107 class AlignedData {
108  public:
AlignedData()109   AlignedData() { memset(data_, 0, alignment); }
110   ~AlignedData() = default;
111   alignas(alignment) char data_[alignment];
112 };
113 
114 }  // anonymous namespace
115 
116 #define EXPECT_ALIGNED(ptr, align) \
117     EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
118 
TEST(StackContainer,BufferAlignment)119 TEST(StackContainer, BufferAlignment) {
120   StackVector<wchar_t, 16> text;
121   text->push_back(L'A');
122   EXPECT_ALIGNED(&text[0], alignof(wchar_t));
123 
124   StackVector<double, 1> doubles;
125   doubles->push_back(0.0);
126   EXPECT_ALIGNED(&doubles[0], alignof(double));
127 
128   StackVector<AlignedData<16>, 1> aligned16;
129   aligned16->push_back(AlignedData<16>());
130   EXPECT_ALIGNED(&aligned16[0], 16);
131 
132 #if !defined(__GNUC__) || defined(ARCH_CPU_X86_FAMILY)
133   // It seems that non-X86 gcc doesn't respect greater than 16 byte alignment.
134   // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33721 for details.
135   // TODO(sbc):re-enable this if GCC starts respecting higher alignments.
136   StackVector<AlignedData<256>, 1> aligned256;
137   aligned256->push_back(AlignedData<256>());
138   EXPECT_ALIGNED(&aligned256[0], 256);
139 #endif
140 }
141 
142 template class StackVector<int, 2>;
143 template class StackVector<scoped_refptr<Dummy>, 2>;
144 
145 }  // namespace base
146