1 /* 2 * Copyright (C) 2011 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 28 #include "wtf/OwnPtr.h" 29 #include "wtf/PassOwnPtr.h" 30 #include "wtf/Vector.h" 31 #include <gtest/gtest.h> 32 33 namespace { 34 35 TEST(WTF_Vector, Basic) 36 { 37 Vector<int> intVector; 38 EXPECT_TRUE(intVector.isEmpty()); 39 EXPECT_EQ(0ul, intVector.size()); 40 EXPECT_EQ(0ul, intVector.capacity()); 41 } 42 43 TEST(WTF_Vector, Reverse) 44 { 45 Vector<int> intVector; 46 intVector.append(10); 47 intVector.append(11); 48 intVector.append(12); 49 intVector.append(13); 50 intVector.reverse(); 51 52 EXPECT_EQ(13, intVector[0]); 53 EXPECT_EQ(12, intVector[1]); 54 EXPECT_EQ(11, intVector[2]); 55 EXPECT_EQ(10, intVector[3]); 56 57 intVector.append(9); 58 intVector.reverse(); 59 60 EXPECT_EQ(9, intVector[0]); 61 EXPECT_EQ(10, intVector[1]); 62 EXPECT_EQ(11, intVector[2]); 63 EXPECT_EQ(12, intVector[3]); 64 EXPECT_EQ(13, intVector[4]); 65 } 66 67 TEST(WTF_Vector, Iterator) 68 { 69 Vector<int> intVector; 70 intVector.append(10); 71 intVector.append(11); 72 intVector.append(12); 73 intVector.append(13); 74 75 Vector<int>::iterator it = intVector.begin(); 76 Vector<int>::iterator end = intVector.end(); 77 EXPECT_TRUE(end != it); 78 79 EXPECT_EQ(10, *it); 80 ++it; 81 EXPECT_EQ(11, *it); 82 ++it; 83 EXPECT_EQ(12, *it); 84 ++it; 85 EXPECT_EQ(13, *it); 86 ++it; 87 88 EXPECT_TRUE(end == it); 89 } 90 91 TEST(WTF_Vector, ReverseIterator) 92 { 93 Vector<int> intVector; 94 intVector.append(10); 95 intVector.append(11); 96 intVector.append(12); 97 intVector.append(13); 98 99 Vector<int>::reverse_iterator it = intVector.rbegin(); 100 Vector<int>::reverse_iterator end = intVector.rend(); 101 EXPECT_TRUE(end != it); 102 103 EXPECT_EQ(13, *it); 104 ++it; 105 EXPECT_EQ(12, *it); 106 ++it; 107 EXPECT_EQ(11, *it); 108 ++it; 109 EXPECT_EQ(10, *it); 110 ++it; 111 112 EXPECT_TRUE(end == it); 113 } 114 115 class DestructCounter { 116 public: 117 explicit DestructCounter(int i, int* destructNumber) 118 : m_i(i) 119 , m_destructNumber(destructNumber) 120 { } 121 122 ~DestructCounter() { ++(*m_destructNumber); } 123 int get() const { return m_i; } 124 125 private: 126 int m_i; 127 int* m_destructNumber; 128 }; 129 130 typedef WTF::Vector<OwnPtr<DestructCounter> > OwnPtrVector; 131 132 TEST(WTF_Vector, OwnPtr) 133 { 134 int destructNumber = 0; 135 OwnPtrVector vector; 136 vector.append(adoptPtr(new DestructCounter(0, &destructNumber))); 137 vector.append(adoptPtr(new DestructCounter(1, &destructNumber))); 138 ASSERT_EQ(2u, vector.size()); 139 140 OwnPtr<DestructCounter>& counter0 = vector.first(); 141 ASSERT_EQ(0, counter0->get()); 142 OwnPtr<DestructCounter>& counter1 = vector.last(); 143 ASSERT_EQ(1, counter1->get()); 144 ASSERT_EQ(0, destructNumber); 145 146 size_t index = 0; 147 for (OwnPtrVector::iterator iter = vector.begin(); iter != vector.end(); ++iter) { 148 OwnPtr<DestructCounter>* refCounter = iter; 149 ASSERT_EQ(index, static_cast<size_t>(refCounter->get()->get())); 150 ASSERT_EQ(index, static_cast<size_t>((*refCounter)->get())); 151 index++; 152 } 153 ASSERT_EQ(0, destructNumber); 154 155 for (index = 0; index < vector.size(); index++) { 156 OwnPtr<DestructCounter>& refCounter = vector[index]; 157 ASSERT_EQ(index, static_cast<size_t>(refCounter->get())); 158 index++; 159 } 160 ASSERT_EQ(0, destructNumber); 161 162 ASSERT_EQ(0, vector[0]->get()); 163 ASSERT_EQ(1, vector[1]->get()); 164 vector.remove(0); 165 ASSERT_EQ(1, vector[0]->get()); 166 ASSERT_EQ(1u, vector.size()); 167 ASSERT_EQ(1, destructNumber); 168 169 OwnPtr<DestructCounter> ownCounter1 = vector[0].release(); 170 vector.remove(0); 171 ASSERT_EQ(counter1.get(), ownCounter1.get()); 172 ASSERT_EQ(0u, vector.size()); 173 ASSERT_EQ(1, destructNumber); 174 175 ownCounter1.clear(); 176 ASSERT_EQ(2, destructNumber); 177 178 size_t count = 1025; 179 destructNumber = 0; 180 for (size_t i = 0; i < count; i++) 181 vector.prepend(adoptPtr(new DestructCounter(i, &destructNumber))); 182 183 // Vector relocation must not destruct OwnPtr element. 184 ASSERT_EQ(0, destructNumber); 185 ASSERT_EQ(count, vector.size()); 186 187 OwnPtrVector copyVector; 188 vector.swap(copyVector); 189 ASSERT_EQ(0, destructNumber); 190 ASSERT_EQ(count, copyVector.size()); 191 ASSERT_EQ(0u, vector.size()); 192 193 copyVector.clear(); 194 ASSERT_EQ(count, static_cast<size_t>(destructNumber)); 195 } 196 197 } // namespace 198