blob: 9c6a8e0b9e7293b02946aef9ee68c6af1599005e [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include "base/stack_container.h"
6
7#include <algorithm>
8
9#include "testing/gtest/include/gtest/gtest.h"
10#include "base/ref_counted.h"
11
12namespace {
13
14class Dummy : public base::RefCounted<Dummy> {
15 public:
16 Dummy(int* alive) : alive_(alive) {
17 ++*alive_;
18 }
19 ~Dummy() {
20 --*alive_;
21 }
22 private:
23 int* const alive_;
24};
25
26} // namespace
27
28TEST(StackContainer, Vector) {
29 const int stack_size = 3;
30 StackVector<int, stack_size> vect;
31 const int* stack_buffer = &vect.stack_data().stack_buffer()[0];
32
33 // The initial |stack_size| elements should appear in the stack buffer.
darin@google.comcf7eff42008-08-15 10:05:11 +090034 EXPECT_EQ(static_cast<size_t>(stack_size), vect.container().capacity());
initial.commit3f4a7322008-07-27 06:49:38 +090035 for (int i = 0; i < stack_size; i++) {
36 vect.container().push_back(i);
37 EXPECT_EQ(stack_buffer, &vect.container()[0]);
38 EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
39 }
40
41 // Adding more elements should push the array onto the heap.
42 for (int i = 0; i < stack_size; i++) {
43 vect.container().push_back(i + stack_size);
44 EXPECT_NE(stack_buffer, &vect.container()[0]);
45 EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
46 }
47
48 // The array should still be in order.
49 for (int i = 0; i < stack_size * 2; i++)
50 EXPECT_EQ(i, vect.container()[i]);
51
52 // Resize to smaller. Our STL implementation won't reallocate in this case,
53 // otherwise it might use our stack buffer. We reserve right after the resize
54 // to guarantee it isn't using the stack buffer, even though it doesn't have
55 // much data.
56 vect.container().resize(stack_size);
57 vect.container().reserve(stack_size * 2);
58 EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
59
60 // Copying the small vector to another should use the same allocator and use
61 // the now-unused stack buffer. GENERALLY CALLERS SHOULD NOT DO THIS since
62 // they have to get the template types just right and it can cause errors.
63 std::vector<int, StackAllocator<int, stack_size> > other(vect.container());
64 EXPECT_EQ(stack_buffer, &other.front());
65 EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
66 for (int i = 0; i < stack_size; i++)
67 EXPECT_EQ(i, other[i]);
68}
69
70TEST(StackContainer, VectorDoubleDelete) {
71 // Regression testing for double-delete.
72 typedef StackVector<scoped_refptr<Dummy>, 2> Vector;
73 typedef Vector::ContainerType Container;
74 Vector vect;
75
76 int alive = 0;
77 scoped_refptr<Dummy> dummy(new Dummy(&alive));
78 EXPECT_EQ(alive, 1);
79
80 vect->push_back(dummy);
81 EXPECT_EQ(alive, 1);
82
83 Dummy* dummy_unref = dummy.get();
84 dummy = NULL;
85 EXPECT_EQ(alive, 1);
86
87 Container::iterator itr = std::find(vect->begin(), vect->end(), dummy_unref);
88 EXPECT_EQ(itr->get(), dummy_unref);
89 vect->erase(itr);
90 EXPECT_EQ(alive, 0);
91
92 // Shouldn't crash at exit.
93}
94
95TEST(StackContainer, BufferAlignment) {
96 StackVector<wchar_t, 16> text;
97 text->push_back(L'A');
98 text->push_back(L'B');
99 text->push_back(L'C');
100 text->push_back(L'D');
101 text->push_back(L'E');
102 text->push_back(L'F');
103 text->push_back(0);
104
105 const wchar_t* buffer = &text[1];
106 bool even_aligned = (0 == (((size_t)buffer) & 0x1));
107 EXPECT_EQ(even_aligned, true);
108}
109
pinkerton@google.comffbd9592008-08-06 22:26:05 +0900110#ifdef COMPILER_MSVC
initial.commit3f4a7322008-07-27 06:49:38 +0900111// Make sure all the class compiles correctly.
pinkerton@google.comffbd9592008-08-06 22:26:05 +0900112// TODO(pinkerton): i'm not sure why this doesn't compile on GCC, but
113// it doesn't.
initial.commit3f4a7322008-07-27 06:49:38 +0900114template StackVector<int, 2>;
115template StackVector<scoped_refptr<Dummy>, 2>;
pinkerton@google.comffbd9592008-08-06 22:26:05 +0900116#endif
license.botf003cfe2008-08-24 09:55:55 +0900117