summaryrefslogtreecommitdiffstats
path: root/base/stack_container_unittest.cc
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-10 00:46:16 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-10 00:46:16 +0000
commit8392ccddddf7f355d57871a5e9af37750f7139f8 (patch)
tree90fad5b03f607fcabea23fb3756005c50da970cd /base/stack_container_unittest.cc
parent5e992fa6227a36da5cb705bfca39ebb785027b61 (diff)
downloadchromium_src-8392ccddddf7f355d57871a5e9af37750f7139f8.zip
chromium_src-8392ccddddf7f355d57871a5e9af37750f7139f8.tar.gz
chromium_src-8392ccddddf7f355d57871a5e9af37750f7139f8.tar.bz2
Move stack_container and linked_list to the new containers subdirectory.
This also replaces the StackWString with StackString16 and adds a char traits definition to the string16 header file to make this possible without ifdefs. BUG= Review URL: https://codereview.chromium.org/11360174 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167022 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/stack_container_unittest.cc')
-rw-r--r--base/stack_container_unittest.cc138
1 files changed, 0 insertions, 138 deletions
diff --git a/base/stack_container_unittest.cc b/base/stack_container_unittest.cc
deleted file mode 100644
index 7efab36..0000000
--- a/base/stack_container_unittest.cc
+++ /dev/null
@@ -1,138 +0,0 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "base/stack_container.h"
-
-#include <algorithm>
-
-#include "base/memory/aligned_memory.h"
-#include "base/memory/ref_counted.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace {
-
-class Dummy : public base::RefCounted<Dummy> {
- public:
- explicit Dummy(int* alive) : alive_(alive) {
- ++*alive_;
- }
-
- private:
- friend class base::RefCounted<Dummy>;
-
- ~Dummy() {
- --*alive_;
- }
-
- int* const alive_;
-};
-
-} // namespace
-
-TEST(StackContainer, Vector) {
- const int stack_size = 3;
- StackVector<int, stack_size> vect;
- const int* stack_buffer = &vect.stack_data().stack_buffer()[0];
-
- // The initial |stack_size| elements should appear in the stack buffer.
- EXPECT_EQ(static_cast<size_t>(stack_size), vect.container().capacity());
- for (int i = 0; i < stack_size; i++) {
- vect.container().push_back(i);
- EXPECT_EQ(stack_buffer, &vect.container()[0]);
- EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
- }
-
- // Adding more elements should push the array onto the heap.
- for (int i = 0; i < stack_size; i++) {
- vect.container().push_back(i + stack_size);
- EXPECT_NE(stack_buffer, &vect.container()[0]);
- EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
- }
-
- // The array should still be in order.
- for (int i = 0; i < stack_size * 2; i++)
- EXPECT_EQ(i, vect.container()[i]);
-
- // Resize to smaller. Our STL implementation won't reallocate in this case,
- // otherwise it might use our stack buffer. We reserve right after the resize
- // to guarantee it isn't using the stack buffer, even though it doesn't have
- // much data.
- vect.container().resize(stack_size);
- vect.container().reserve(stack_size * 2);
- EXPECT_FALSE(vect.stack_data().used_stack_buffer_);
-
- // Copying the small vector to another should use the same allocator and use
- // the now-unused stack buffer. GENERALLY CALLERS SHOULD NOT DO THIS since
- // they have to get the template types just right and it can cause errors.
- std::vector<int, StackAllocator<int, stack_size> > other(vect.container());
- EXPECT_EQ(stack_buffer, &other.front());
- EXPECT_TRUE(vect.stack_data().used_stack_buffer_);
- for (int i = 0; i < stack_size; i++)
- EXPECT_EQ(i, other[i]);
-}
-
-TEST(StackContainer, VectorDoubleDelete) {
- // Regression testing for double-delete.
- typedef StackVector<scoped_refptr<Dummy>, 2> Vector;
- typedef Vector::ContainerType Container;
- Vector vect;
-
- int alive = 0;
- scoped_refptr<Dummy> dummy(new Dummy(&alive));
- EXPECT_EQ(alive, 1);
-
- vect->push_back(dummy);
- EXPECT_EQ(alive, 1);
-
- Dummy* dummy_unref = dummy.get();
- dummy = NULL;
- EXPECT_EQ(alive, 1);
-
- Container::iterator itr = std::find(vect->begin(), vect->end(), dummy_unref);
- EXPECT_EQ(itr->get(), dummy_unref);
- vect->erase(itr);
- EXPECT_EQ(alive, 0);
-
- // Shouldn't crash at exit.
-}
-
-namespace {
-
-template <size_t alignment>
-class AlignedData {
- public:
- AlignedData() { memset(data_.void_data(), 0, alignment); }
- ~AlignedData() {}
- base::AlignedMemory<alignment, alignment> data_;
-};
-
-} // anonymous namespace
-
-#define EXPECT_ALIGNED(ptr, align) \
- EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1))
-
-TEST(StackContainer, BufferAlignment) {
- StackVector<wchar_t, 16> text;
- text->push_back(L'A');
- EXPECT_ALIGNED(&text[0], ALIGNOF(wchar_t));
-
- StackVector<double, 1> doubles;
- doubles->push_back(0.0);
- EXPECT_ALIGNED(&doubles[0], ALIGNOF(double));
-
- StackVector<AlignedData<16>, 1> aligned16;
- aligned16->push_back(AlignedData<16>());
- EXPECT_ALIGNED(&aligned16[0], 16);
-
-#if !defined(OS_ANDROID)
- // It seems that android doesn't respect greater than 16 byte alignment for
- // non-POD data on the stack, even though ALIGNOF(aligned256) == 256.
- StackVector<AlignedData<256>, 1> aligned256;
- aligned256->push_back(AlignedData<256>());
- EXPECT_ALIGNED(&aligned256[0], 256);
-#endif
-}
-
-template class StackVector<int, 2>;
-template class StackVector<scoped_refptr<Dummy>, 2>;