aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNicolas Catania <niko@google.com>2009-05-23 14:47:26 -0700
committerNicolas Catania <niko@google.com>2009-06-10 09:45:21 -0700
commitfd56a38d5dcb569b146634bb22c5d9cb1e138e3f (patch)
tree9adaa254dcdca29f63127f93b8ab568c79b48938 /tests
parent60fd0f758a63e74980d712a13c91bc24d98cedc6 (diff)
downloadastl-fd56a38d5dcb569b146634bb22c5d9cb1e138e3f.tar.gz
Basic implementation of vector.
Uses malloc/realloc for pod type. For classes uses placement new to insert new copies of the data. In the test package, renamed macros.h to common.h New memory file with uninitialized_copy and uninitialized_fill.
Diffstat (limited to 'tests')
-rw-r--r--tests/Android.mk38
-rw-r--r--tests/common.h (renamed from tests/macros.h)54
-rw-r--r--tests/test_algorithm.cpp2
-rw-r--r--tests/test_string.cpp21
-rw-r--r--tests/test_type_traits.cpp2
-rw-r--r--tests/test_uninitialized.cpp177
-rw-r--r--tests/test_vector.cpp336
7 files changed, 590 insertions, 40 deletions
diff --git a/tests/Android.mk b/tests/Android.mk
index 8451b23..432dece 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -44,30 +44,32 @@ endef
# same as 'device-test' but builds a host executable instead
# you can use EXTRA_LDLIBS to indicate additional linker flags
#
-# define host-test
-# $(foreach file,$(1), \
-# $(eval include $(CLEAR_VARS)) \
-# $(eval LOCAL_SRC_FILES := $(file)) \
-# $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \
-# $(eval $(info LOCAL_MODULE=$(LOCAL_MODULE) file=$(file))) \
-# $(eval LOCAL_CFLAGS += $(EXTRA_CFLAGS)) \
-# $(eval LOCAL_LDLIBS += $(EXTRA_LDLIBS)) \
-# $(eval LOCAL_MODULE_TAGS := eng tests) \
-# $(eval LOCAL_STATIC_LIBRARIES := libastl) \
-# $(eval include $(BUILD_HOST_EXECUTABLE)) \
-# ) \
-# $(eval EXTRA_CFLAGS :=) \
-# $(eval EXTRA_LDLIBS :=)
-# endef
+define host-test
+ $(foreach file,$(1), \
+ $(eval include $(CLEAR_VARS)) \
+ $(eval LOCAL_SRC_FILES := $(file)) \
+ $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \
+ $(eval $(info LOCAL_MODULE=$(LOCAL_MODULE) file=$(file))) \
+ $(eval LOCAL_CFLAGS += $(EXTRA_CFLAGS)) \
+ $(eval LOCAL_LDLIBS += $(EXTRA_LDLIBS)) \
+ $(eval LOCAL_MODULE_TAGS := eng tests) \
+ $(eval LOCAL_STATIC_LIBRARIES := libastl) \
+ $(eval include $(BUILD_HOST_EXECUTABLE)) \
+ ) \
+ $(eval EXTRA_CFLAGS :=) \
+ $(eval EXTRA_LDLIBS :=)
+endef
sources := \
test_algorithm.cpp \
test_string.cpp \
- test_type_traits.cpp
+ test_type_traits.cpp \
+ test_uninitialized.cpp \
+ test_vector.cpp
# Disable all optimization for the host target to help test tools (valgrind...)
-# EXTRA_CFLAGS := -I bionic/libstdc++/include -I external/astl/include -g -O0
-# $(call host-test, $(sources))
+EXTRA_CFLAGS := -I bionic/libstdc++/include -I external/astl/include -g -O0
+$(call host-test, $(sources))
EXTRA_CFLAGS := -I bionic/libstdc++/include -I external/astl/include
$(call device-test, $(sources))
diff --git a/tests/macros.h b/tests/common.h
index 1ef577f..1942018 100644
--- a/tests/macros.h
+++ b/tests/common.h
@@ -27,27 +27,65 @@
* SUCH DAMAGE.
*/
-#ifndef ANDROID_ASTL_TESTS_MACROS__
-#define ANDROID_ASTL_TESTS_MACROS__
-
-// Common macros for tests.
+#ifndef ANDROID_ASTL_TESTS_COMMON__
+#define ANDROID_ASTL_TESTS_COMMON__
#include <cstdio>
+// Classes and macros used in tests.
namespace {
+const size_t kMaxSizeT = ~((size_t)0);
const int kPassed = 0;
const int kFailed = 1;
#define FAIL_UNLESS(v) if (!android::v()) return kFailed;
-#define EXPECT_TRUE(expr) \
- if (!(expr)) { \
+#define EXPECT_TRUE(expr) \
+ if (!(expr)) { \
std::fprintf(stderr, "%d: %s\n", __LINE__, #expr); \
- return false; \
+ return false; \
}
#ifndef ARRAYSIZE
#define ARRAYSIZE(array) (sizeof(array)/sizeof(array[0]))
#endif
+// Cannot be copied.
+struct NoCopy {
+ private:
+ NoCopy(const NoCopy& nc) {}
+};
+
+// Count the number of assignement.
+struct CopyCounter {
+ static size_t mCount;
+
+ CopyCounter() { }
+ CopyCounter& operator=(const CopyCounter& cc) {return *this; }
+ CopyCounter(const CopyCounter& nc) {++mCount;}
+ private:
+};
+
+class CtorDtorCounter {
+ public:
+ static size_t mCtorCount;
+ static size_t mCopyCtorCount;
+ static size_t mAssignCount;
+ static size_t mDtorCount;
+
+ CtorDtorCounter() {++mCtorCount;}
+ CtorDtorCounter(const CtorDtorCounter& nc) {++mCopyCtorCount;}
+ CtorDtorCounter& operator=(const CtorDtorCounter& nc) {++mAssignCount; return *this;}
+ ~CtorDtorCounter() {++mDtorCount;}
+ static void reset() {mCtorCount = 0; mCopyCtorCount = 0; mAssignCount = 0; mDtorCount = 0;}
+ private:
+};
+
+size_t CopyCounter::mCount;
+size_t CtorDtorCounter::mCtorCount;
+size_t CtorDtorCounter::mCopyCtorCount;
+size_t CtorDtorCounter::mAssignCount;
+size_t CtorDtorCounter::mDtorCount;
+
} // anonymous namespace
-#endif // ANDROID_ASTL_TEST_MACROS__
+
+#endif // ANDROID_ASTL_TEST_COMMON__
diff --git a/tests/test_algorithm.cpp b/tests/test_algorithm.cpp
index 831e1fd..e00bb67 100644
--- a/tests/test_algorithm.cpp
+++ b/tests/test_algorithm.cpp
@@ -31,7 +31,7 @@
#ifndef ANDROID_ASTL_ALGORITHM__
#error "Wrong header included!!"
#endif
-#include "macros.h"
+#include "common.h"
namespace android {
diff --git a/tests/test_string.cpp b/tests/test_string.cpp
index 130f98b..bd69a46 100644
--- a/tests/test_string.cpp
+++ b/tests/test_string.cpp
@@ -33,11 +33,8 @@
#endif
#include <climits>
#include <cstring>
-#include "macros.h"
+#include "common.h"
-#ifndef MAX_SIZE_T
-#define MAX_SIZE_T (~(size_t)0)
-#endif
namespace android {
using std::string;
@@ -321,7 +318,7 @@ bool testReserve()
str07.reserve(10);
EXPECT_TRUE(str07.capacity() == 10);
- str07.reserve(MAX_SIZE_T);
+ str07.reserve(kMaxSizeT);
EXPECT_TRUE(str07.capacity() == 10);
@@ -383,9 +380,9 @@ bool testAppend()
string str12("original");
char dummy[] = "unused";
// We lie about the size but that is ok. Since the lenght of the new string
- // is going to be MAX_SIZE_T, the call will have not effect (there is no
+ // is going to be kMaxSizeT, the call will have not effect (there is no
// space for the trailing '\0').
- str12.append(dummy, MAX_SIZE_T);
+ str12.append(dummy, kMaxSizeT);
EXPECT_TRUE(str12 == "original");
return true;
@@ -799,11 +796,11 @@ bool testErase()
EXPECT_TRUE(empty_string.capacity() == 0);
EXPECT_TRUE(empty_string.size() == 0);
- empty_string.erase(MAX_SIZE_T);
+ empty_string.erase(kMaxSizeT);
EXPECT_TRUE(empty_string.capacity() == 0);
EXPECT_TRUE(empty_string.size() == 0);
- empty_string.erase(MAX_SIZE_T, MAX_SIZE_T);
+ empty_string.erase(kMaxSizeT, kMaxSizeT);
EXPECT_TRUE(empty_string.capacity() == 0);
EXPECT_TRUE(empty_string.size() == 0);
}
@@ -819,7 +816,7 @@ bool testErase()
{
string str02("a");
- str02.erase(MAX_SIZE_T);
+ str02.erase(kMaxSizeT);
EXPECT_TRUE(str02.capacity() == 1);
EXPECT_TRUE(str02.size() == 1);
}
@@ -827,7 +824,7 @@ bool testErase()
{
string str03("a");
- str03.erase(0, MAX_SIZE_T);
+ str03.erase(0, kMaxSizeT);
EXPECT_TRUE(str03.capacity() == 1);
EXPECT_TRUE(str03.size() == 0);
}
@@ -835,7 +832,7 @@ bool testErase()
{
string str04("a");
- str04.erase(1, MAX_SIZE_T);
+ str04.erase(1, kMaxSizeT);
EXPECT_TRUE(str04.capacity() == 1);
EXPECT_TRUE(str04.size() == 1);
}
diff --git a/tests/test_type_traits.cpp b/tests/test_type_traits.cpp
index a5bba4c..ef3690b 100644
--- a/tests/test_type_traits.cpp
+++ b/tests/test_type_traits.cpp
@@ -31,7 +31,7 @@
#error "Wrong header included!!"
#endif
-#include "macros.h"
+#include "common.h"
namespace android {
diff --git a/tests/test_uninitialized.cpp b/tests/test_uninitialized.cpp
new file mode 100644
index 0000000..808e836
--- /dev/null
+++ b/tests/test_uninitialized.cpp
@@ -0,0 +1,177 @@
+/* -*- c++ -*- */
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+
+#include "../include/memory"
+#ifndef ANDROID_ASTL_MEMORY__
+#error "Wrong header included!!"
+#endif
+#include "common.h"
+#include <algorithm>
+#include <cstdlib>
+
+namespace android {
+using std::uninitialized_copy;
+using std::uninitialized_fill;
+
+bool testCopyPod()
+{
+ int src[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+ const int size = ARRAYSIZE(src);
+ int dest[size] = {0, };
+
+ EXPECT_TRUE(uninitialized_copy(src, src + size, dest) == dest);
+
+ EXPECT_TRUE(std::equal(src, src + size, dest));
+ return true;
+}
+
+
+bool testCopyPodOverflow()
+{
+ int src, dest;
+
+ // Should not crash
+ EXPECT_TRUE(uninitialized_copy(&src, &src + kMaxSizeT / sizeof(src) + 1, &dest) == &dest);
+ return true;
+}
+
+bool testCopyClass()
+{
+ const size_t kSize = 100;
+ CtorDtorCounter::reset();
+
+ CtorDtorCounter src[kSize];
+ CtorDtorCounter *dest = static_cast<CtorDtorCounter*>(malloc(kSize * sizeof(CtorDtorCounter)));
+
+ EXPECT_TRUE(CtorDtorCounter::mCtorCount == kSize);
+ EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 0);
+ EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
+
+ CtorDtorCounter::reset();
+
+ EXPECT_TRUE(uninitialized_copy(src, src + kSize, dest) == dest);
+
+ EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0);
+ EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == kSize);
+ EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
+ free(dest);
+ return true;
+}
+
+struct A {};
+bool testCopyArray()
+{
+ {
+ const A src[] = {A()};
+ A one;
+ A *dest = &one;
+
+ EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest);
+ }
+ {
+ A src[] = {A()};
+ A one;
+ A *dest = &one;
+
+ EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest);
+ }
+ {
+ const A src[] = {A()};
+ A dest[1];
+
+ EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest);
+ }
+ return true;
+}
+
+bool testFillChar()
+{
+ const char src[] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
+ const int size = ARRAYSIZE(src);
+ char dest[size];
+
+ uninitialized_fill(dest, dest + size, 'a');
+
+ EXPECT_TRUE(std::equal(dest, dest + size, src));
+ return true;
+}
+
+bool testFillPod()
+{
+ const int src[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
+ const int size = ARRAYSIZE(src);
+ int dest[size];
+
+ uninitialized_fill(dest, dest + size, 10);
+
+ EXPECT_TRUE(std::equal(dest, dest + size, src));
+ return true;
+}
+
+bool testFillClass()
+{
+ const size_t kSize = 100;
+ CtorDtorCounter::reset();
+
+ CtorDtorCounter src;
+ CtorDtorCounter *dest = static_cast<CtorDtorCounter*>(malloc(kSize * sizeof(CtorDtorCounter)));
+
+ EXPECT_TRUE(CtorDtorCounter::mCtorCount == 1);
+ EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 0);
+ EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
+
+ CtorDtorCounter::reset();
+
+ uninitialized_fill(dest, dest + kSize, src);
+
+ EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0);
+ EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == kSize);
+ EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
+ free(dest);
+ return true;
+}
+
+
+} // namespace android
+
+int main(int argc, char **argv)
+{
+ // copy
+ FAIL_UNLESS(testCopyPod);
+ FAIL_UNLESS(testCopyPodOverflow);
+ FAIL_UNLESS(testCopyClass);
+ FAIL_UNLESS(testCopyArray);
+ // fill
+ FAIL_UNLESS(testFillChar);
+ FAIL_UNLESS(testFillPod);
+ FAIL_UNLESS(testFillClass);
+
+ return kPassed;
+}
diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp
new file mode 100644
index 0000000..01b3b10
--- /dev/null
+++ b/tests/test_vector.cpp
@@ -0,0 +1,336 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "../include/vector"
+#ifndef ANDROID_ASTL_VECTOR__
+#error "Wrong header included!!"
+#endif
+#include <climits>
+#include <cstring>
+#include "common.h"
+
+namespace android {
+using std::vector;
+static const size_t kDefaultCapacity = 4;
+static const size_t kExponentialFactor = 2;
+bool testConstructorInt()
+{
+ {
+ vector<int> vec1;
+ EXPECT_TRUE(vec1.empty());
+ EXPECT_TRUE(vec1.size() == 0);
+ EXPECT_TRUE(vec1.capacity() == 0);
+ }
+ {
+ vector<int> vec2(100);
+ EXPECT_TRUE(!vec2.empty());
+ EXPECT_TRUE(vec2.size() == 100);
+ EXPECT_TRUE(vec2.capacity() == 100);
+ for (size_t i = 0; i < 100; ++i)
+ {
+ EXPECT_TRUE(vec2[i] == 0);
+ }
+ }
+ {
+ vector<int> vec3(200, 0xaa);
+ EXPECT_TRUE(!vec3.empty());
+ EXPECT_TRUE(vec3.size() == 200);
+ EXPECT_TRUE(vec3.capacity() == 200);
+ for (size_t i = 0; i < 200; ++i)
+ {
+ EXPECT_TRUE(vec3[i] == 0xaa);
+ }
+ }
+ return true;
+}
+
+typedef enum { ONE = 10, TWO} TestEnum;
+
+template<typename T> struct A { };
+struct B { };
+
+bool testConstructorRepeat()
+{
+ {
+ const vector<int> vec1(100, 10);
+
+ EXPECT_TRUE(vec1.end() - vec1.begin() == 100);
+ for (int i = 0; i < 100; ++i)
+ {
+ EXPECT_TRUE(vec1[i] == 10);
+ }
+ }
+ {
+ const vector<float> vec2(100, 10.0f);
+
+ for (int i = 0; i < 100; ++i)
+ {
+ EXPECT_TRUE(vec2[i] == 10.0f);
+ }
+ }
+ {
+ const vector<TestEnum> vec3(100, ONE);
+
+ for (int i = 0; i < 100; ++i)
+ {
+ EXPECT_TRUE(vec3[i] == ONE);
+ }
+ }
+ {
+ const vector< A<B> > vec4;
+ const vector< A<B> > vec5(10);
+
+ EXPECT_TRUE(vec4.size() == 0);
+ EXPECT_TRUE(vec5.size() == 10);
+ }
+ return true;
+}
+
+
+bool testReserve()
+{
+ { // basic reserve + shrink.
+ vector<int> vec1(100, 10);
+
+ EXPECT_TRUE(vec1.capacity() == 100);
+ EXPECT_TRUE(vec1.reserve(200));
+ EXPECT_TRUE(vec1.capacity() == 200);
+ EXPECT_TRUE(vec1.size() == 100);
+
+ EXPECT_TRUE(vec1.reserve());
+ EXPECT_TRUE(vec1.capacity() == 100);
+ EXPECT_TRUE(vec1.size() == 100);
+ }
+ {
+ vector<int> vec2;
+
+ EXPECT_TRUE(vec2.capacity() == 0);
+ vec2.reserve(200);
+ EXPECT_TRUE(vec2.capacity() == 200);
+ vec2.reserve();
+ EXPECT_TRUE(vec2.capacity() == kDefaultCapacity);
+ vec2.push_back(3);
+ EXPECT_TRUE(vec2.capacity() == kDefaultCapacity);
+ }
+ {
+ vector<int> vec3;
+
+ vec3.push_back(5);
+ vec3.reserve();
+ EXPECT_TRUE(vec3.capacity() == 1);
+ vec3.push_back(3);
+ EXPECT_TRUE(vec3.capacity() == kDefaultCapacity);
+ for (int i = 0; i < 2; ++i) vec3.push_back(3);
+ EXPECT_TRUE(vec3.size() == kDefaultCapacity);
+ EXPECT_TRUE(vec3.capacity() == kDefaultCapacity);
+
+ // exp increment.
+ vec3.push_back(10);
+ EXPECT_TRUE(vec3.capacity() == kExponentialFactor * kDefaultCapacity);
+ }
+ {
+ CopyCounter c;
+
+ c.mCount = 0;
+ vector<CopyCounter> vec4(100, c);
+ EXPECT_TRUE(c.mCount == 100);
+ // Resizing does not do any copy via the copy assignement op.
+ vec4.reserve(1000);
+ EXPECT_TRUE(c.mCount == 200);
+ vec4.reserve(50); // reserving less than length is a nop.
+ EXPECT_TRUE(c.mCount == 200);
+ }
+ {
+ vector<unsigned short> vec5;
+
+ EXPECT_TRUE(!vec5.reserve(vec5.max_size() + 1));
+ EXPECT_TRUE(vec5.capacity() == 0);
+ }
+ return true;
+}
+
+
+bool testPushBack()
+{
+ {
+ vector<CtorDtorCounter> vec1;
+ CtorDtorCounter c;
+
+ c.reset();
+ for (int i = 0; i < 1000; ++i)
+ {
+ vec1.push_back(c);
+ }
+ EXPECT_TRUE(vec1.capacity() == 1024);
+ EXPECT_TRUE(vec1.size() == 1000);
+ EXPECT_TRUE(c.mAssignCount == 1000);
+ // Due to the multiple augmentation of the capacity, the copy
+ // constructor has been invoked.
+ EXPECT_TRUE(c.mCopyCtorCount > 0);
+ EXPECT_TRUE(c.mCtorCount == 0);
+ }
+ {
+ vector<int> vec2;
+
+ vec2.push_back(10);
+ EXPECT_TRUE(vec2.front() == 10);
+ EXPECT_TRUE(vec2.back() == 10);
+ EXPECT_TRUE(vec2.size() == 1);
+ vec2.push_back(20);
+ EXPECT_TRUE(vec2.front() == 10);
+ EXPECT_TRUE(vec2.back() == 20);
+ EXPECT_TRUE(vec2.size() == 2);
+ }
+ return true;
+}
+
+
+bool testPopBack()
+{
+ vector<int> vec1(10, 0xdeadbeef);;
+
+ EXPECT_TRUE(vec1.capacity() == 10);
+ EXPECT_TRUE(vec1.size() == 10);
+
+ for(size_t i = 10; i > 0; --i)
+ {
+ EXPECT_TRUE(vec1.capacity() == 10);
+ EXPECT_TRUE(vec1.size() == i);
+ vec1.pop_back();
+ }
+ EXPECT_TRUE(vec1.empty());
+ EXPECT_TRUE(vec1.begin() == vec1.end());
+ vec1.pop_back(); // pop_back on empty vector
+ EXPECT_TRUE(vec1.size() == 0);
+ EXPECT_TRUE(vec1.capacity() == 10);
+
+ vec1.clear();
+ vec1.pop_back(); // pop_back on empty vector
+ EXPECT_TRUE(vec1.size() == 0);
+ EXPECT_TRUE(vec1.capacity() == 0);
+ EXPECT_TRUE(vec1.begin() == vec1.end());
+ EXPECT_TRUE(vec1.begin() == NULL);
+ return true;
+}
+
+
+bool testSwap()
+{
+ vector<int> vec1(100, 10);
+ vector<int> vec2;
+
+ vec1.swap(vec2);
+
+ EXPECT_TRUE(vec1.capacity() == 0);
+ EXPECT_TRUE(vec2.capacity() == 100);
+
+ EXPECT_TRUE(vec1.size() == 0);
+ EXPECT_TRUE(vec2.size() == 100);
+
+ EXPECT_TRUE(vec1.begin() == vec1.end());
+ EXPECT_TRUE(vec2.begin() != vec2.end());
+ return true;
+}
+
+
+bool testIterators()
+{
+ vector<int> vec1(10);
+
+ for (size_t i = 0; i < 10; ++i)
+ {
+ vec1[i] = i;
+ }
+
+ vector<int>::iterator i = vec1.begin();
+ for (int c = 0; i != vec1.end(); ++i, ++c)
+ {
+ EXPECT_TRUE(c == *i);
+ }
+
+ vector<int>::const_iterator j = vec1.begin();
+ for (int c = 0; j != vec1.end(); ++j, ++c)
+ {
+ EXPECT_TRUE(c == *j);
+ }
+ return true;
+}
+
+bool testCtorDtorForNonPod()
+{
+ { // empty vector, no construction should happen.
+ CtorDtorCounter::reset();
+ vector<CtorDtorCounter> vec1;
+
+ EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0);
+ EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 0);
+ }
+ EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
+
+ {
+ CtorDtorCounter instance;
+ EXPECT_TRUE(CtorDtorCounter::mCtorCount == 1);
+ CtorDtorCounter::reset();
+
+ vector<CtorDtorCounter> vec2(200, instance);
+
+ // 200 copies by assignement of the sample instance
+ EXPECT_TRUE(CtorDtorCounter::mAssignCount == 0);
+ EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0);
+ EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 200);
+ EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
+
+ CtorDtorCounter::reset();
+ vec2.reserve(400);
+
+ // 200 moves: 200 copies by copy constructor and 200 destructions.
+ EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 200);
+ EXPECT_TRUE(CtorDtorCounter::mDtorCount == 200);
+ EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0);
+ EXPECT_TRUE(CtorDtorCounter::mAssignCount == 0);
+
+ CtorDtorCounter::reset();
+ }
+ // 200 + 1 for the instance
+ EXPECT_TRUE(CtorDtorCounter::mDtorCount == 201);
+ return true;
+}
+} // namespace android
+
+int main(int argc, char **argv)
+{
+ FAIL_UNLESS(testConstructorInt);
+ FAIL_UNLESS(testConstructorRepeat);
+ FAIL_UNLESS(testReserve);
+ FAIL_UNLESS(testPushBack);
+ FAIL_UNLESS(testPopBack);
+ FAIL_UNLESS(testSwap);
+ FAIL_UNLESS(testIterators);
+ FAIL_UNLESS(testCtorDtorForNonPod);
+ return kPassed;
+}