| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "wtf/LinkedHashSet.h" | 29 #include "wtf/LinkedHashSet.h" |
| 30 #include "wtf/PassRefPtr.h" | 30 #include "wtf/PassRefPtr.h" |
| 31 #include "wtf/RefCounted.h" | 31 #include "wtf/RefCounted.h" |
| 32 #include "wtf/RefPtr.h" | 32 #include "wtf/RefPtr.h" |
| 33 | 33 |
| 34 namespace WTF { | 34 namespace WTF { |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 template <typename Set> | 38 template <typename Set> |
| 39 void removeFirstHelper() | 39 void removeFirstHelper() { |
| 40 { | 40 Set list; |
| 41 Set list; | 41 list.add(-1); |
| 42 list.add(-1); | 42 list.add(0); |
| 43 list.add(0); | 43 list.add(1); |
| 44 list.add(1); | 44 list.add(2); |
| 45 list.add(2); | 45 list.add(3); |
| 46 list.add(3); | 46 |
| 47 | 47 EXPECT_EQ(-1, list.first()); |
| 48 EXPECT_EQ(-1, list.first()); | 48 EXPECT_EQ(3, list.last()); |
| 49 EXPECT_EQ(3, list.last()); | 49 |
| 50 | 50 list.removeFirst(); |
| 51 list.removeFirst(); | 51 EXPECT_EQ(0, list.first()); |
| 52 EXPECT_EQ(0, list.first()); | 52 |
| 53 | 53 list.removeLast(); |
| 54 list.removeLast(); | 54 EXPECT_EQ(2, list.last()); |
| 55 EXPECT_EQ(2, list.last()); | 55 |
| 56 | 56 list.removeFirst(); |
| 57 list.removeFirst(); | 57 EXPECT_EQ(1, list.first()); |
| 58 EXPECT_EQ(1, list.first()); | 58 |
| 59 | 59 list.removeFirst(); |
| 60 list.removeFirst(); | 60 EXPECT_EQ(2, list.first()); |
| 61 EXPECT_EQ(2, list.first()); | 61 |
| 62 | 62 list.removeFirst(); |
| 63 list.removeFirst(); | 63 EXPECT_TRUE(list.isEmpty()); |
| 64 EXPECT_TRUE(list.isEmpty()); | 64 } |
| 65 } | 65 |
| 66 | 66 TEST(ListHashSetTest, RemoveFirst) { |
| 67 TEST(ListHashSetTest, RemoveFirst) | 67 removeFirstHelper<ListHashSet<int>>(); |
| 68 { | 68 removeFirstHelper<ListHashSet<int, 1>>(); |
| 69 removeFirstHelper<ListHashSet<int>>(); | 69 } |
| 70 removeFirstHelper<ListHashSet<int, 1>>(); | 70 |
| 71 } | 71 TEST(LinkedHashSetTest, RemoveFirst) { |
| 72 | 72 removeFirstHelper<LinkedHashSet<int>>(); |
| 73 TEST(LinkedHashSetTest, RemoveFirst) | 73 } |
| 74 { | 74 |
| 75 removeFirstHelper<LinkedHashSet<int>>(); | 75 template <typename Set> |
| 76 } | 76 void appendOrMoveToLastNewItems() { |
| 77 | 77 Set list; |
| 78 template <typename Set> | 78 typename Set::AddResult result = list.appendOrMoveToLast(1); |
| 79 void appendOrMoveToLastNewItems() | 79 EXPECT_TRUE(result.isNewEntry); |
| 80 { | 80 result = list.add(2); |
| 81 Set list; | 81 EXPECT_TRUE(result.isNewEntry); |
| 82 typename Set::AddResult result = list.appendOrMoveToLast(1); | 82 result = list.appendOrMoveToLast(3); |
| 83 EXPECT_TRUE(result.isNewEntry); | 83 EXPECT_TRUE(result.isNewEntry); |
| 84 result = list.add(2); | 84 |
| 85 EXPECT_TRUE(result.isNewEntry); | 85 EXPECT_EQ(list.size(), 3UL); |
| 86 result = list.appendOrMoveToLast(3); | 86 |
| 87 EXPECT_TRUE(result.isNewEntry); | 87 // The list should be in order 1, 2, 3. |
| 88 | 88 typename Set::iterator iterator = list.begin(); |
| 89 EXPECT_EQ(list.size(), 3UL); | 89 EXPECT_EQ(1, *iterator); |
| 90 | 90 ++iterator; |
| 91 // The list should be in order 1, 2, 3. | 91 EXPECT_EQ(2, *iterator); |
| 92 typename Set::iterator iterator = list.begin(); | 92 ++iterator; |
| 93 EXPECT_EQ(1, *iterator); | 93 EXPECT_EQ(3, *iterator); |
| 94 ++iterator; | 94 ++iterator; |
| 95 EXPECT_EQ(2, *iterator); | 95 } |
| 96 ++iterator; | 96 |
| 97 EXPECT_EQ(3, *iterator); | 97 TEST(ListHashSetTest, AppendOrMoveToLastNewItems) { |
| 98 ++iterator; | 98 appendOrMoveToLastNewItems<ListHashSet<int>>(); |
| 99 } | 99 appendOrMoveToLastNewItems<ListHashSet<int, 1>>(); |
| 100 | 100 } |
| 101 TEST(ListHashSetTest, AppendOrMoveToLastNewItems) | 101 |
| 102 { | 102 TEST(LinkedHashSetTest, AppendOrMoveToLastNewItems) { |
| 103 appendOrMoveToLastNewItems<ListHashSet<int>>(); | 103 appendOrMoveToLastNewItems<LinkedHashSet<int>>(); |
| 104 appendOrMoveToLastNewItems<ListHashSet<int, 1>>(); | 104 } |
| 105 } | 105 |
| 106 | 106 template <typename Set> |
| 107 TEST(LinkedHashSetTest, AppendOrMoveToLastNewItems) | 107 void appendOrMoveToLastWithDuplicates() { |
| 108 { | 108 Set list; |
| 109 appendOrMoveToLastNewItems<LinkedHashSet<int>>(); | 109 |
| 110 } | 110 // Add a single element twice. |
| 111 | 111 typename Set::AddResult result = list.add(1); |
| 112 template <typename Set> | 112 EXPECT_TRUE(result.isNewEntry); |
| 113 void appendOrMoveToLastWithDuplicates() | 113 result = list.appendOrMoveToLast(1); |
| 114 { | 114 EXPECT_FALSE(result.isNewEntry); |
| 115 Set list; | 115 EXPECT_EQ(1UL, list.size()); |
| 116 | 116 |
| 117 // Add a single element twice. | 117 list.add(2); |
| 118 typename Set::AddResult result = list.add(1); | 118 list.add(3); |
| 119 EXPECT_TRUE(result.isNewEntry); | 119 EXPECT_EQ(3UL, list.size()); |
| 120 result = list.appendOrMoveToLast(1); | 120 |
| 121 EXPECT_FALSE(result.isNewEntry); | 121 // Appending 2 move it to the end. |
| 122 EXPECT_EQ(1UL, list.size()); | 122 EXPECT_EQ(3, list.last()); |
| 123 | 123 result = list.appendOrMoveToLast(2); |
| 124 list.add(2); | 124 EXPECT_FALSE(result.isNewEntry); |
| 125 list.add(3); | 125 EXPECT_EQ(2, list.last()); |
| 126 EXPECT_EQ(3UL, list.size()); | 126 |
| 127 | 127 // Inverse the list by moving each element to end end. |
| 128 // Appending 2 move it to the end. | 128 result = list.appendOrMoveToLast(3); |
| 129 EXPECT_EQ(3, list.last()); | 129 EXPECT_FALSE(result.isNewEntry); |
| 130 result = list.appendOrMoveToLast(2); | 130 result = list.appendOrMoveToLast(2); |
| 131 EXPECT_FALSE(result.isNewEntry); | 131 EXPECT_FALSE(result.isNewEntry); |
| 132 EXPECT_EQ(2, list.last()); | 132 result = list.appendOrMoveToLast(1); |
| 133 | 133 EXPECT_FALSE(result.isNewEntry); |
| 134 // Inverse the list by moving each element to end end. | 134 EXPECT_EQ(3UL, list.size()); |
| 135 result = list.appendOrMoveToLast(3); | 135 |
| 136 EXPECT_FALSE(result.isNewEntry); | 136 typename Set::iterator iterator = list.begin(); |
| 137 result = list.appendOrMoveToLast(2); | 137 EXPECT_EQ(3, *iterator); |
| 138 EXPECT_FALSE(result.isNewEntry); | 138 ++iterator; |
| 139 result = list.appendOrMoveToLast(1); | 139 EXPECT_EQ(2, *iterator); |
| 140 EXPECT_FALSE(result.isNewEntry); | 140 ++iterator; |
| 141 EXPECT_EQ(3UL, list.size()); | 141 EXPECT_EQ(1, *iterator); |
| 142 | 142 ++iterator; |
| 143 typename Set::iterator iterator = list.begin(); | 143 } |
| 144 EXPECT_EQ(3, *iterator); | 144 |
| 145 ++iterator; | 145 TEST(ListHashSetTest, AppendOrMoveToLastWithDuplicates) { |
| 146 EXPECT_EQ(2, *iterator); | 146 appendOrMoveToLastWithDuplicates<ListHashSet<int>>(); |
| 147 ++iterator; | 147 appendOrMoveToLastWithDuplicates<ListHashSet<int, 1>>(); |
| 148 EXPECT_EQ(1, *iterator); | 148 } |
| 149 ++iterator; | 149 |
| 150 } | 150 TEST(LinkedHashSetTest, AppendOrMoveToLastWithDuplicates) { |
| 151 | 151 appendOrMoveToLastWithDuplicates<LinkedHashSet<int>>(); |
| 152 TEST(ListHashSetTest, AppendOrMoveToLastWithDuplicates) | 152 } |
| 153 { | 153 |
| 154 appendOrMoveToLastWithDuplicates<ListHashSet<int>>(); | 154 template <typename Set> |
| 155 appendOrMoveToLastWithDuplicates<ListHashSet<int, 1>>(); | 155 void prependOrMoveToFirstNewItems() { |
| 156 } | 156 Set list; |
| 157 | 157 typename Set::AddResult result = list.prependOrMoveToFirst(1); |
| 158 TEST(LinkedHashSetTest, AppendOrMoveToLastWithDuplicates) | 158 EXPECT_TRUE(result.isNewEntry); |
| 159 { | 159 result = list.add(2); |
| 160 appendOrMoveToLastWithDuplicates<LinkedHashSet<int>>(); | 160 EXPECT_TRUE(result.isNewEntry); |
| 161 } | 161 result = list.prependOrMoveToFirst(3); |
| 162 | 162 EXPECT_TRUE(result.isNewEntry); |
| 163 template <typename Set> | 163 |
| 164 void prependOrMoveToFirstNewItems() | 164 EXPECT_EQ(list.size(), 3UL); |
| 165 { | 165 |
| 166 Set list; | 166 // The list should be in order 3, 1, 2. |
| 167 typename Set::AddResult result = list.prependOrMoveToFirst(1); | 167 typename Set::iterator iterator = list.begin(); |
| 168 EXPECT_TRUE(result.isNewEntry); | 168 EXPECT_EQ(3, *iterator); |
| 169 result = list.add(2); | 169 ++iterator; |
| 170 EXPECT_TRUE(result.isNewEntry); | 170 EXPECT_EQ(1, *iterator); |
| 171 result = list.prependOrMoveToFirst(3); | 171 ++iterator; |
| 172 EXPECT_TRUE(result.isNewEntry); | 172 EXPECT_EQ(2, *iterator); |
| 173 | 173 ++iterator; |
| 174 EXPECT_EQ(list.size(), 3UL); | 174 } |
| 175 | 175 |
| 176 // The list should be in order 3, 1, 2. | 176 TEST(ListHashSetTest, PrependOrMoveToFirstNewItems) { |
| 177 typename Set::iterator iterator = list.begin(); | 177 prependOrMoveToFirstNewItems<ListHashSet<int>>(); |
| 178 EXPECT_EQ(3, *iterator); | 178 prependOrMoveToFirstNewItems<ListHashSet<int, 1>>(); |
| 179 ++iterator; | 179 } |
| 180 EXPECT_EQ(1, *iterator); | 180 |
| 181 ++iterator; | 181 TEST(LinkedHashSetTest, PrependOrMoveToFirstNewItems) { |
| 182 EXPECT_EQ(2, *iterator); | 182 prependOrMoveToFirstNewItems<LinkedHashSet<int>>(); |
| 183 ++iterator; | 183 } |
| 184 } | 184 |
| 185 | 185 template <typename Set> |
| 186 TEST(ListHashSetTest, PrependOrMoveToFirstNewItems) | 186 void prependOrMoveToLastWithDuplicates() { |
| 187 { | 187 Set list; |
| 188 prependOrMoveToFirstNewItems<ListHashSet<int>>(); | 188 |
| 189 prependOrMoveToFirstNewItems<ListHashSet<int, 1>>(); | 189 // Add a single element twice. |
| 190 } | 190 typename Set::AddResult result = list.add(1); |
| 191 | 191 EXPECT_TRUE(result.isNewEntry); |
| 192 TEST(LinkedHashSetTest, PrependOrMoveToFirstNewItems) | 192 result = list.prependOrMoveToFirst(1); |
| 193 { | 193 EXPECT_FALSE(result.isNewEntry); |
| 194 prependOrMoveToFirstNewItems<LinkedHashSet<int>>(); | 194 EXPECT_EQ(1UL, list.size()); |
| 195 } | 195 |
| 196 | 196 list.add(2); |
| 197 template <typename Set> | 197 list.add(3); |
| 198 void prependOrMoveToLastWithDuplicates() | 198 EXPECT_EQ(3UL, list.size()); |
| 199 { | 199 |
| 200 Set list; | 200 // Prepending 2 move it to the beginning. |
| 201 | 201 EXPECT_EQ(1, list.first()); |
| 202 // Add a single element twice. | 202 result = list.prependOrMoveToFirst(2); |
| 203 typename Set::AddResult result = list.add(1); | 203 EXPECT_FALSE(result.isNewEntry); |
| 204 EXPECT_TRUE(result.isNewEntry); | 204 EXPECT_EQ(2, list.first()); |
| 205 result = list.prependOrMoveToFirst(1); | 205 |
| 206 EXPECT_FALSE(result.isNewEntry); | 206 // Inverse the list by moving each element to the first position. |
| 207 EXPECT_EQ(1UL, list.size()); | 207 result = list.prependOrMoveToFirst(1); |
| 208 | 208 EXPECT_FALSE(result.isNewEntry); |
| 209 list.add(2); | 209 result = list.prependOrMoveToFirst(2); |
| 210 list.add(3); | 210 EXPECT_FALSE(result.isNewEntry); |
| 211 EXPECT_EQ(3UL, list.size()); | 211 result = list.prependOrMoveToFirst(3); |
| 212 | 212 EXPECT_FALSE(result.isNewEntry); |
| 213 // Prepending 2 move it to the beginning. | 213 EXPECT_EQ(3UL, list.size()); |
| 214 EXPECT_EQ(1, list.first()); | 214 |
| 215 result = list.prependOrMoveToFirst(2); | 215 typename Set::iterator iterator = list.begin(); |
| 216 EXPECT_FALSE(result.isNewEntry); | 216 EXPECT_EQ(3, *iterator); |
| 217 EXPECT_EQ(2, list.first()); | 217 ++iterator; |
| 218 | 218 EXPECT_EQ(2, *iterator); |
| 219 // Inverse the list by moving each element to the first position. | 219 ++iterator; |
| 220 result = list.prependOrMoveToFirst(1); | 220 EXPECT_EQ(1, *iterator); |
| 221 EXPECT_FALSE(result.isNewEntry); | 221 ++iterator; |
| 222 result = list.prependOrMoveToFirst(2); | 222 } |
| 223 EXPECT_FALSE(result.isNewEntry); | 223 |
| 224 result = list.prependOrMoveToFirst(3); | 224 TEST(ListHashSetTest, PrependOrMoveToLastWithDuplicates) { |
| 225 EXPECT_FALSE(result.isNewEntry); | 225 prependOrMoveToLastWithDuplicates<ListHashSet<int>>(); |
| 226 EXPECT_EQ(3UL, list.size()); | 226 prependOrMoveToLastWithDuplicates<ListHashSet<int, 1>>(); |
| 227 | 227 } |
| 228 typename Set::iterator iterator = list.begin(); | 228 |
| 229 EXPECT_EQ(3, *iterator); | 229 TEST(LinkedHashSetTest, PrependOrMoveToLastWithDuplicates) { |
| 230 ++iterator; | 230 prependOrMoveToLastWithDuplicates<LinkedHashSet<int>>(); |
| 231 EXPECT_EQ(2, *iterator); | |
| 232 ++iterator; | |
| 233 EXPECT_EQ(1, *iterator); | |
| 234 ++iterator; | |
| 235 } | |
| 236 | |
| 237 TEST(ListHashSetTest, PrependOrMoveToLastWithDuplicates) | |
| 238 { | |
| 239 prependOrMoveToLastWithDuplicates<ListHashSet<int>>(); | |
| 240 prependOrMoveToLastWithDuplicates<ListHashSet<int, 1>>(); | |
| 241 } | |
| 242 | |
| 243 TEST(LinkedHashSetTest, PrependOrMoveToLastWithDuplicates) | |
| 244 { | |
| 245 prependOrMoveToLastWithDuplicates<LinkedHashSet<int>>(); | |
| 246 } | 231 } |
| 247 | 232 |
| 248 class DummyRefCounted : public RefCounted<DummyRefCounted> { | 233 class DummyRefCounted : public RefCounted<DummyRefCounted> { |
| 249 public: | 234 public: |
| 250 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa
lse; } | 235 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { |
| 251 ~DummyRefCounted() { m_isDeleted = true; } | 236 m_isDeleted = false; |
| 252 void ref() | 237 } |
| 253 { | 238 ~DummyRefCounted() { m_isDeleted = true; } |
| 254 WTF::RefCounted<DummyRefCounted>::ref(); | 239 void ref() { |
| 255 ++m_refInvokesCount; | 240 WTF::RefCounted<DummyRefCounted>::ref(); |
| 256 } | 241 ++m_refInvokesCount; |
| 257 | 242 } |
| 258 static int m_refInvokesCount; | 243 |
| 259 | 244 static int m_refInvokesCount; |
| 260 private: | 245 |
| 261 bool& m_isDeleted; | 246 private: |
| 247 bool& m_isDeleted; |
| 262 }; | 248 }; |
| 263 | 249 |
| 264 int DummyRefCounted::m_refInvokesCount = 0; | 250 int DummyRefCounted::m_refInvokesCount = 0; |
| 265 | 251 |
| 266 template <typename Set> | 252 template <typename Set> |
| 267 void withRefPtr() | 253 void withRefPtr() { |
| 268 { | 254 bool isDeleted = false; |
| 269 bool isDeleted = false; | 255 DummyRefCounted::m_refInvokesCount = 0; |
| 270 DummyRefCounted::m_refInvokesCount = 0; | 256 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); |
| 271 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); | 257 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); |
| 272 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); | 258 |
| 273 | 259 Set set; |
| 274 Set set; | 260 set.add(ptr); |
| 275 set.add(ptr); | 261 // Referenced only once (to store a copy in the container). |
| 276 // Referenced only once (to store a copy in the container). | 262 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 277 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 263 EXPECT_EQ(ptr, set.first()); |
| 278 EXPECT_EQ(ptr, set.first()); | 264 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 279 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 265 |
| 280 | 266 DummyRefCounted* rawPtr = ptr.get(); |
| 281 DummyRefCounted* rawPtr = ptr.get(); | 267 |
| 282 | 268 EXPECT_TRUE(set.contains(ptr)); |
| 283 EXPECT_TRUE(set.contains(ptr)); | 269 EXPECT_TRUE(set.contains(rawPtr)); |
| 284 EXPECT_TRUE(set.contains(rawPtr)); | 270 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 285 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 271 |
| 286 | 272 ptr.clear(); |
| 287 ptr.clear(); | 273 EXPECT_FALSE(isDeleted); |
| 288 EXPECT_FALSE(isDeleted); | 274 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 289 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 275 |
| 290 | 276 set.remove(rawPtr); |
| 291 set.remove(rawPtr); | 277 EXPECT_TRUE(isDeleted); |
| 292 EXPECT_TRUE(isDeleted); | 278 |
| 293 | 279 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 294 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 280 } |
| 295 } | 281 |
| 296 | 282 TEST(ListHashSetTest, WithRefPtr) { |
| 297 TEST(ListHashSetTest, WithRefPtr) | 283 withRefPtr<ListHashSet<RefPtr<DummyRefCounted>>>(); |
| 298 { | 284 withRefPtr<ListHashSet<RefPtr<DummyRefCounted>, 1>>(); |
| 299 withRefPtr<ListHashSet<RefPtr<DummyRefCounted>>>(); | 285 } |
| 300 withRefPtr<ListHashSet<RefPtr<DummyRefCounted>, 1>>(); | 286 |
| 301 } | 287 TEST(LinkedHashSetTest, WithRefPtr) { |
| 302 | 288 withRefPtr<LinkedHashSet<RefPtr<DummyRefCounted>>>(); |
| 303 TEST(LinkedHashSetTest, WithRefPtr) | |
| 304 { | |
| 305 withRefPtr<LinkedHashSet<RefPtr<DummyRefCounted>>>(); | |
| 306 } | 289 } |
| 307 | 290 |
| 308 template <typename Set, typename SetRef, typename Iterator> | 291 template <typename Set, typename SetRef, typename Iterator> |
| 309 void findHelper() | 292 void findHelper() { |
| 310 { | 293 Set set; |
| 311 Set set; | 294 set.add(-1); |
| 312 set.add(-1); | 295 set.add(0); |
| 313 set.add(0); | 296 set.add(1); |
| 314 set.add(1); | 297 set.add(2); |
| 315 set.add(2); | 298 set.add(3); |
| 316 set.add(3); | 299 |
| 317 | 300 SetRef ref = set; |
| 318 SetRef ref = set; | 301 Iterator it = ref.find(2); |
| 319 Iterator it = ref.find(2); | 302 EXPECT_EQ(2, *it); |
| 320 EXPECT_EQ(2, *it); | 303 ++it; |
| 321 ++it; | 304 EXPECT_EQ(3, *it); |
| 322 EXPECT_EQ(3, *it); | 305 --it; |
| 323 --it; | 306 --it; |
| 324 --it; | 307 EXPECT_EQ(1, *it); |
| 308 } |
| 309 |
| 310 TEST(ListHashSetTest, Find) { |
| 311 findHelper<ListHashSet<int>, const ListHashSet<int>&, |
| 312 ListHashSet<int>::const_iterator>(); |
| 313 findHelper<ListHashSet<int>, ListHashSet<int>&, ListHashSet<int>::iterator>(); |
| 314 findHelper<ListHashSet<int, 1>, const ListHashSet<int, 1>&, |
| 315 ListHashSet<int, 1>::const_iterator>(); |
| 316 findHelper<ListHashSet<int, 1>, ListHashSet<int, 1>&, |
| 317 ListHashSet<int, 1>::iterator>(); |
| 318 } |
| 319 |
| 320 TEST(LinkedHashSetTest, Find) { |
| 321 findHelper<LinkedHashSet<int>, const LinkedHashSet<int>&, |
| 322 LinkedHashSet<int>::const_iterator>(); |
| 323 findHelper<LinkedHashSet<int>, LinkedHashSet<int>&, |
| 324 LinkedHashSet<int>::iterator>(); |
| 325 } |
| 326 |
| 327 template <typename Set> |
| 328 void insertBeforeHelper(bool canModifyWhileIterating) { |
| 329 Set set; |
| 330 set.add(-1); |
| 331 set.add(0); |
| 332 set.add(2); |
| 333 set.add(3); |
| 334 |
| 335 typename Set::iterator it = set.find(2); |
| 336 EXPECT_EQ(2, *it); |
| 337 set.insertBefore(it, 1); |
| 338 if (!canModifyWhileIterating) |
| 339 it = set.find(2); |
| 340 ++it; |
| 341 EXPECT_EQ(3, *it); |
| 342 EXPECT_EQ(5u, set.size()); |
| 343 --it; |
| 344 --it; |
| 345 EXPECT_EQ(1, *it); |
| 346 if (canModifyWhileIterating) { |
| 347 set.remove(-1); |
| 348 set.remove(0); |
| 349 set.remove(2); |
| 350 set.remove(3); |
| 351 EXPECT_EQ(1u, set.size()); |
| 325 EXPECT_EQ(1, *it); | 352 EXPECT_EQ(1, *it); |
| 326 } | |
| 327 | |
| 328 TEST(ListHashSetTest, Find) | |
| 329 { | |
| 330 findHelper<ListHashSet<int>, const ListHashSet<int>&, ListHashSet<int>::cons
t_iterator>(); | |
| 331 findHelper<ListHashSet<int>, ListHashSet<int>&, ListHashSet<int>::iterator>(
); | |
| 332 findHelper<ListHashSet<int, 1>, const ListHashSet<int, 1>&, ListHashSet<int,
1>::const_iterator>(); | |
| 333 findHelper<ListHashSet<int, 1>, ListHashSet<int, 1>&, ListHashSet<int, 1>::i
terator>(); | |
| 334 } | |
| 335 | |
| 336 TEST(LinkedHashSetTest, Find) | |
| 337 { | |
| 338 findHelper<LinkedHashSet<int>, const LinkedHashSet<int>&, LinkedHashSet<int>
::const_iterator>(); | |
| 339 findHelper<LinkedHashSet<int>, LinkedHashSet<int>&, LinkedHashSet<int>::iter
ator>(); | |
| 340 } | |
| 341 | |
| 342 template <typename Set> | |
| 343 void insertBeforeHelper(bool canModifyWhileIterating) | |
| 344 { | |
| 345 Set set; | |
| 346 set.add(-1); | |
| 347 set.add(0); | |
| 348 set.add(2); | |
| 349 set.add(3); | |
| 350 | |
| 351 typename Set::iterator it = set.find(2); | |
| 352 EXPECT_EQ(2, *it); | |
| 353 set.insertBefore(it, 1); | |
| 354 if (!canModifyWhileIterating) | |
| 355 it = set.find(2); | |
| 356 ++it; | |
| 357 EXPECT_EQ(3, *it); | |
| 358 EXPECT_EQ(5u, set.size()); | |
| 359 --it; | |
| 360 --it; | |
| 361 EXPECT_EQ(1, *it); | |
| 362 if (canModifyWhileIterating) { | |
| 363 set.remove(-1); | |
| 364 set.remove(0); | |
| 365 set.remove(2); | |
| 366 set.remove(3); | |
| 367 EXPECT_EQ(1u, set.size()); | |
| 368 EXPECT_EQ(1, *it); | |
| 369 ++it; | |
| 370 EXPECT_EQ(it, set.end()); | |
| 371 --it; | |
| 372 EXPECT_EQ(1, *it); | |
| 373 set.insertBefore(it, -1); | |
| 374 set.insertBefore(it, 0); | |
| 375 set.add(2); | |
| 376 set.add(3); | |
| 377 } | |
| 378 set.insertBefore(2, 42); | |
| 379 set.insertBefore(-1, 103); | |
| 380 EXPECT_EQ(103, set.first()); | |
| 381 if (!canModifyWhileIterating) | |
| 382 it = set.find(1); | |
| 383 ++it; | |
| 384 EXPECT_EQ(42, *it); | |
| 385 EXPECT_EQ(7u, set.size()); | |
| 386 } | |
| 387 | |
| 388 TEST(ListHashSetTest, InsertBefore) | |
| 389 { | |
| 390 insertBeforeHelper<ListHashSet<int>>(true); | |
| 391 insertBeforeHelper<ListHashSet<int, 1>>(true); | |
| 392 } | |
| 393 | |
| 394 TEST(LinkedHashSetTest, InsertBefore) | |
| 395 { | |
| 396 insertBeforeHelper<LinkedHashSet<int>>(false); | |
| 397 } | |
| 398 | |
| 399 template <typename Set> | |
| 400 void addReturnIterator(bool canModifyWhileIterating) | |
| 401 { | |
| 402 Set set; | |
| 403 set.add(-1); | |
| 404 set.add(0); | |
| 405 set.add(1); | |
| 406 set.add(2); | |
| 407 | |
| 408 typename Set::iterator it = set.addReturnIterator(3); | |
| 409 EXPECT_EQ(3, *it); | |
| 410 --it; | |
| 411 EXPECT_EQ(2, *it); | |
| 412 EXPECT_EQ(5u, set.size()); | |
| 413 --it; | |
| 414 EXPECT_EQ(1, *it); | |
| 415 --it; | |
| 416 EXPECT_EQ(0, *it); | |
| 417 it = set.addReturnIterator(4); | |
| 418 if (canModifyWhileIterating) { | |
| 419 set.remove(3); | |
| 420 set.remove(2); | |
| 421 set.remove(1); | |
| 422 set.remove(0); | |
| 423 set.remove(-1); | |
| 424 EXPECT_EQ(1u, set.size()); | |
| 425 } | |
| 426 EXPECT_EQ(4, *it); | |
| 427 ++it; | 353 ++it; |
| 428 EXPECT_EQ(it, set.end()); | 354 EXPECT_EQ(it, set.end()); |
| 429 --it; | 355 --it; |
| 430 EXPECT_EQ(4, *it); | 356 EXPECT_EQ(1, *it); |
| 431 if (canModifyWhileIterating) { | 357 set.insertBefore(it, -1); |
| 432 set.insertBefore(it, -1); | 358 set.insertBefore(it, 0); |
| 433 set.insertBefore(it, 0); | 359 set.add(2); |
| 434 set.insertBefore(it, 1); | 360 set.add(3); |
| 435 set.insertBefore(it, 2); | 361 } |
| 436 set.insertBefore(it, 3); | 362 set.insertBefore(2, 42); |
| 437 } | 363 set.insertBefore(-1, 103); |
| 438 EXPECT_EQ(6u, set.size()); | 364 EXPECT_EQ(103, set.first()); |
| 439 it = set.addReturnIterator(5); | 365 if (!canModifyWhileIterating) |
| 440 EXPECT_EQ(7u, set.size()); | 366 it = set.find(1); |
| 441 set.remove(it); | 367 ++it; |
| 442 EXPECT_EQ(6u, set.size()); | 368 EXPECT_EQ(42, *it); |
| 443 EXPECT_EQ(4, set.last()); | 369 EXPECT_EQ(7u, set.size()); |
| 444 } | 370 } |
| 445 | 371 |
| 446 TEST(ListHashSetTest, AddReturnIterator) | 372 TEST(ListHashSetTest, InsertBefore) { |
| 447 { | 373 insertBeforeHelper<ListHashSet<int>>(true); |
| 448 addReturnIterator<ListHashSet<int>>(true); | 374 insertBeforeHelper<ListHashSet<int, 1>>(true); |
| 449 addReturnIterator<ListHashSet<int, 1>>(true); | 375 } |
| 450 } | 376 |
| 451 | 377 TEST(LinkedHashSetTest, InsertBefore) { |
| 452 TEST(LinkedHashSetTest, AddReturnIterator) | 378 insertBeforeHelper<LinkedHashSet<int>>(false); |
| 453 { | 379 } |
| 454 addReturnIterator<LinkedHashSet<int>>(false); | 380 |
| 455 } | 381 template <typename Set> |
| 456 | 382 void addReturnIterator(bool canModifyWhileIterating) { |
| 457 template <typename Set> | 383 Set set; |
| 458 void excerciseValuePeekInType() | 384 set.add(-1); |
| 459 { | 385 set.add(0); |
| 460 Set set; | 386 set.add(1); |
| 461 bool isDeleted = false; | 387 set.add(2); |
| 462 bool isDeleted2 = false; | 388 |
| 463 | 389 typename Set::iterator it = set.addReturnIterator(3); |
| 464 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); | 390 EXPECT_EQ(3, *it); |
| 465 RefPtr<DummyRefCounted> ptr2 = adoptRef(new DummyRefCounted(isDeleted2)); | 391 --it; |
| 466 | 392 EXPECT_EQ(2, *it); |
| 467 typename Set::AddResult addResult = set.add(ptr); | 393 EXPECT_EQ(5u, set.size()); |
| 468 EXPECT_TRUE(addResult.isNewEntry); | 394 --it; |
| 469 set.find(ptr); | 395 EXPECT_EQ(1, *it); |
| 470 const Set& constSet(set); | 396 --it; |
| 471 constSet.find(ptr); | 397 EXPECT_EQ(0, *it); |
| 472 EXPECT_TRUE(set.contains(ptr)); | 398 it = set.addReturnIterator(4); |
| 473 typename Set::iterator it = set.addReturnIterator(ptr); | 399 if (canModifyWhileIterating) { |
| 474 set.appendOrMoveToLast(ptr); | 400 set.remove(3); |
| 475 set.prependOrMoveToFirst(ptr); | 401 set.remove(2); |
| 476 set.insertBefore(ptr, ptr); | 402 set.remove(1); |
| 477 set.insertBefore(it, ptr); | 403 set.remove(0); |
| 404 set.remove(-1); |
| 478 EXPECT_EQ(1u, set.size()); | 405 EXPECT_EQ(1u, set.size()); |
| 479 set.add(ptr2); | 406 } |
| 480 ptr2.clear(); | 407 EXPECT_EQ(4, *it); |
| 481 set.remove(ptr); | 408 ++it; |
| 482 | 409 EXPECT_EQ(it, set.end()); |
| 483 EXPECT_FALSE(isDeleted); | 410 --it; |
| 484 ptr.clear(); | 411 EXPECT_EQ(4, *it); |
| 485 EXPECT_TRUE(isDeleted); | 412 if (canModifyWhileIterating) { |
| 486 | 413 set.insertBefore(it, -1); |
| 487 EXPECT_FALSE(isDeleted2); | 414 set.insertBefore(it, 0); |
| 488 set.removeFirst(); | 415 set.insertBefore(it, 1); |
| 489 EXPECT_TRUE(isDeleted2); | 416 set.insertBefore(it, 2); |
| 490 | 417 set.insertBefore(it, 3); |
| 491 EXPECT_EQ(0u, set.size()); | 418 } |
| 492 } | 419 EXPECT_EQ(6u, set.size()); |
| 493 | 420 it = set.addReturnIterator(5); |
| 494 TEST(ListHashSetTest, ExcerciseValuePeekInType) | 421 EXPECT_EQ(7u, set.size()); |
| 495 { | 422 set.remove(it); |
| 496 excerciseValuePeekInType<ListHashSet<RefPtr<DummyRefCounted>>>(); | 423 EXPECT_EQ(6u, set.size()); |
| 497 excerciseValuePeekInType<ListHashSet<RefPtr<DummyRefCounted>, 1>>(); | 424 EXPECT_EQ(4, set.last()); |
| 498 } | 425 } |
| 499 | 426 |
| 500 TEST(LinkedHashSetTest, ExcerciseValuePeekInType) | 427 TEST(ListHashSetTest, AddReturnIterator) { |
| 501 { | 428 addReturnIterator<ListHashSet<int>>(true); |
| 502 excerciseValuePeekInType<LinkedHashSet<RefPtr<DummyRefCounted>>>(); | 429 addReturnIterator<ListHashSet<int, 1>>(true); |
| 430 } |
| 431 |
| 432 TEST(LinkedHashSetTest, AddReturnIterator) { |
| 433 addReturnIterator<LinkedHashSet<int>>(false); |
| 434 } |
| 435 |
| 436 template <typename Set> |
| 437 void excerciseValuePeekInType() { |
| 438 Set set; |
| 439 bool isDeleted = false; |
| 440 bool isDeleted2 = false; |
| 441 |
| 442 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); |
| 443 RefPtr<DummyRefCounted> ptr2 = adoptRef(new DummyRefCounted(isDeleted2)); |
| 444 |
| 445 typename Set::AddResult addResult = set.add(ptr); |
| 446 EXPECT_TRUE(addResult.isNewEntry); |
| 447 set.find(ptr); |
| 448 const Set& constSet(set); |
| 449 constSet.find(ptr); |
| 450 EXPECT_TRUE(set.contains(ptr)); |
| 451 typename Set::iterator it = set.addReturnIterator(ptr); |
| 452 set.appendOrMoveToLast(ptr); |
| 453 set.prependOrMoveToFirst(ptr); |
| 454 set.insertBefore(ptr, ptr); |
| 455 set.insertBefore(it, ptr); |
| 456 EXPECT_EQ(1u, set.size()); |
| 457 set.add(ptr2); |
| 458 ptr2.clear(); |
| 459 set.remove(ptr); |
| 460 |
| 461 EXPECT_FALSE(isDeleted); |
| 462 ptr.clear(); |
| 463 EXPECT_TRUE(isDeleted); |
| 464 |
| 465 EXPECT_FALSE(isDeleted2); |
| 466 set.removeFirst(); |
| 467 EXPECT_TRUE(isDeleted2); |
| 468 |
| 469 EXPECT_EQ(0u, set.size()); |
| 470 } |
| 471 |
| 472 TEST(ListHashSetTest, ExcerciseValuePeekInType) { |
| 473 excerciseValuePeekInType<ListHashSet<RefPtr<DummyRefCounted>>>(); |
| 474 excerciseValuePeekInType<ListHashSet<RefPtr<DummyRefCounted>, 1>>(); |
| 475 } |
| 476 |
| 477 TEST(LinkedHashSetTest, ExcerciseValuePeekInType) { |
| 478 excerciseValuePeekInType<LinkedHashSet<RefPtr<DummyRefCounted>>>(); |
| 503 } | 479 } |
| 504 | 480 |
| 505 struct Simple { | 481 struct Simple { |
| 506 Simple(int value) : m_value(value) { }; | 482 Simple(int value) : m_value(value){}; |
| 507 int m_value; | 483 int m_value; |
| 508 }; | 484 }; |
| 509 | 485 |
| 510 struct Complicated { | 486 struct Complicated { |
| 511 Complicated(int value) : m_simple(value) | 487 Complicated(int value) : m_simple(value) { s_objectsConstructed++; } |
| 512 { | 488 |
| 513 s_objectsConstructed++; | 489 Complicated(const Complicated& other) : m_simple(other.m_simple) { |
| 514 } | 490 s_objectsConstructed++; |
| 515 | 491 } |
| 516 Complicated(const Complicated& other) : m_simple(other.m_simple) | 492 |
| 517 { | 493 Simple m_simple; |
| 518 s_objectsConstructed++; | 494 static int s_objectsConstructed; |
| 519 } | 495 |
| 520 | 496 private: |
| 521 Simple m_simple; | 497 Complicated(); |
| 522 static int s_objectsConstructed; | |
| 523 | |
| 524 private: | |
| 525 Complicated(); | |
| 526 }; | 498 }; |
| 527 | 499 |
| 528 int Complicated::s_objectsConstructed = 0; | 500 int Complicated::s_objectsConstructed = 0; |
| 529 | 501 |
| 530 struct ComplicatedHashFunctions { | 502 struct ComplicatedHashFunctions { |
| 531 static unsigned hash(const Complicated& key) { return key.m_simple.m_value;
} | 503 static unsigned hash(const Complicated& key) { return key.m_simple.m_value; } |
| 532 static bool equal(const Complicated& a, const Complicated& b) { return a.m_s
imple.m_value == b.m_simple.m_value; } | 504 static bool equal(const Complicated& a, const Complicated& b) { |
| 505 return a.m_simple.m_value == b.m_simple.m_value; |
| 506 } |
| 533 }; | 507 }; |
| 534 struct ComplexityTranslator { | 508 struct ComplexityTranslator { |
| 535 static unsigned hash(const Simple& key) { return key.m_value; } | 509 static unsigned hash(const Simple& key) { return key.m_value; } |
| 536 static bool equal(const Complicated& a, const Simple& b) { return a.m_simple
.m_value == b.m_value; } | 510 static bool equal(const Complicated& a, const Simple& b) { |
| 511 return a.m_simple.m_value == b.m_value; |
| 512 } |
| 537 }; | 513 }; |
| 538 | 514 |
| 539 template <typename Set> | 515 template <typename Set> |
| 540 void translatorTest() | 516 void translatorTest() { |
| 541 { | 517 Set set; |
| 542 Set set; | 518 set.add(Complicated(42)); |
| 543 set.add(Complicated(42)); | 519 int baseLine = Complicated::s_objectsConstructed; |
| 544 int baseLine = Complicated::s_objectsConstructed; | 520 |
| 545 | 521 typename Set::iterator it = |
| 546 typename Set::iterator it = set.template find<ComplexityTranslator>(Simple(4
2)); | 522 set.template find<ComplexityTranslator>(Simple(42)); |
| 547 EXPECT_NE(it, set.end()); | 523 EXPECT_NE(it, set.end()); |
| 548 EXPECT_EQ(baseLine, Complicated::s_objectsConstructed); | 524 EXPECT_EQ(baseLine, Complicated::s_objectsConstructed); |
| 549 | 525 |
| 550 it = set.template find<ComplexityTranslator>(Simple(103)); | 526 it = set.template find<ComplexityTranslator>(Simple(103)); |
| 551 EXPECT_EQ(it, set.end()); | 527 EXPECT_EQ(it, set.end()); |
| 552 EXPECT_EQ(baseLine, Complicated::s_objectsConstructed); | 528 EXPECT_EQ(baseLine, Complicated::s_objectsConstructed); |
| 553 | 529 |
| 554 const Set& constSet(set); | 530 const Set& constSet(set); |
| 555 | 531 |
| 556 typename Set::const_iterator constIterator = constSet.template find<Complexi
tyTranslator>(Simple(42)); | 532 typename Set::const_iterator constIterator = |
| 557 EXPECT_NE(constIterator, constSet.end()); | 533 constSet.template find<ComplexityTranslator>(Simple(42)); |
| 558 EXPECT_EQ(baseLine, Complicated::s_objectsConstructed); | 534 EXPECT_NE(constIterator, constSet.end()); |
| 559 | 535 EXPECT_EQ(baseLine, Complicated::s_objectsConstructed); |
| 560 constIterator = constSet.template find<ComplexityTranslator>(Simple(103)); | 536 |
| 561 EXPECT_EQ(constIterator, constSet.end()); | 537 constIterator = constSet.template find<ComplexityTranslator>(Simple(103)); |
| 562 EXPECT_EQ(baseLine, Complicated::s_objectsConstructed); | 538 EXPECT_EQ(constIterator, constSet.end()); |
| 563 } | 539 EXPECT_EQ(baseLine, Complicated::s_objectsConstructed); |
| 564 | 540 } |
| 565 TEST(ListHashSetTest, ComplexityTranslator) | 541 |
| 566 { | 542 TEST(ListHashSetTest, ComplexityTranslator) { |
| 567 translatorTest<ListHashSet<Complicated, 256, ComplicatedHashFunctions>>(); | 543 translatorTest<ListHashSet<Complicated, 256, ComplicatedHashFunctions>>(); |
| 568 translatorTest<ListHashSet<Complicated, 1, ComplicatedHashFunctions>>(); | 544 translatorTest<ListHashSet<Complicated, 1, ComplicatedHashFunctions>>(); |
| 569 } | 545 } |
| 570 | 546 |
| 571 TEST(LinkedHashSetTest, ComplexityTranslator) | 547 TEST(LinkedHashSetTest, ComplexityTranslator) { |
| 572 { | 548 translatorTest<LinkedHashSet<Complicated, ComplicatedHashFunctions>>(); |
| 573 translatorTest<LinkedHashSet<Complicated, ComplicatedHashFunctions>>(); | |
| 574 } | 549 } |
| 575 | 550 |
| 576 struct Dummy { | 551 struct Dummy { |
| 577 Dummy(bool& deleted) : deleted(deleted) { } | 552 Dummy(bool& deleted) : deleted(deleted) {} |
| 578 | 553 |
| 579 ~Dummy() | 554 ~Dummy() { deleted = true; } |
| 580 { | 555 |
| 581 deleted = true; | 556 bool& deleted; |
| 582 } | |
| 583 | |
| 584 bool& deleted; | |
| 585 }; | 557 }; |
| 586 | 558 |
| 587 TEST(ListHashSetTest, WithOwnPtr) | 559 TEST(ListHashSetTest, WithOwnPtr) { |
| 588 { | 560 bool deleted1 = false, deleted2 = false; |
| 589 bool deleted1 = false, deleted2 = false; | 561 |
| 590 | 562 typedef ListHashSet<OwnPtr<Dummy>> OwnPtrSet; |
| 591 typedef ListHashSet<OwnPtr<Dummy>> OwnPtrSet; | 563 OwnPtrSet set; |
| 564 |
| 565 Dummy* ptr1 = new Dummy(deleted1); |
| 566 { |
| 567 // AddResult in a separate scope to avoid assertion hit, |
| 568 // since we modify the container further. |
| 569 OwnPtrSet::AddResult res1 = set.add(adoptPtr(ptr1)); |
| 570 EXPECT_EQ(res1.storedValue->m_value.get(), ptr1); |
| 571 } |
| 572 |
| 573 EXPECT_FALSE(deleted1); |
| 574 EXPECT_EQ(1UL, set.size()); |
| 575 OwnPtrSet::iterator it1 = set.find(ptr1); |
| 576 EXPECT_NE(set.end(), it1); |
| 577 EXPECT_EQ(ptr1, (*it1)); |
| 578 |
| 579 Dummy* ptr2 = new Dummy(deleted2); |
| 580 { |
| 581 OwnPtrSet::AddResult res2 = set.add(adoptPtr(ptr2)); |
| 582 EXPECT_EQ(res2.storedValue->m_value.get(), ptr2); |
| 583 } |
| 584 |
| 585 EXPECT_FALSE(deleted2); |
| 586 EXPECT_EQ(2UL, set.size()); |
| 587 OwnPtrSet::iterator it2 = set.find(ptr2); |
| 588 EXPECT_NE(set.end(), it2); |
| 589 EXPECT_EQ(ptr2, (*it2)); |
| 590 |
| 591 set.remove(ptr1); |
| 592 EXPECT_TRUE(deleted1); |
| 593 |
| 594 set.clear(); |
| 595 EXPECT_TRUE(deleted2); |
| 596 EXPECT_TRUE(set.isEmpty()); |
| 597 |
| 598 deleted1 = false; |
| 599 deleted2 = false; |
| 600 { |
| 592 OwnPtrSet set; | 601 OwnPtrSet set; |
| 593 | 602 set.add(adoptPtr(new Dummy(deleted1))); |
| 594 Dummy* ptr1 = new Dummy(deleted1); | 603 set.add(adoptPtr(new Dummy(deleted2))); |
| 595 { | 604 } |
| 596 // AddResult in a separate scope to avoid assertion hit, | 605 EXPECT_TRUE(deleted1); |
| 597 // since we modify the container further. | 606 EXPECT_TRUE(deleted2); |
| 598 OwnPtrSet::AddResult res1 = set.add(adoptPtr(ptr1)); | 607 |
| 599 EXPECT_EQ(res1.storedValue->m_value.get(), ptr1); | 608 deleted1 = false; |
| 600 } | 609 deleted2 = false; |
| 601 | 610 OwnPtr<Dummy> ownPtr1; |
| 602 EXPECT_FALSE(deleted1); | 611 OwnPtr<Dummy> ownPtr2; |
| 612 ptr1 = new Dummy(deleted1); |
| 613 ptr2 = new Dummy(deleted2); |
| 614 { |
| 615 OwnPtrSet set; |
| 616 set.add(adoptPtr(ptr1)); |
| 617 set.add(adoptPtr(ptr2)); |
| 618 ownPtr1 = set.takeFirst(); |
| 603 EXPECT_EQ(1UL, set.size()); | 619 EXPECT_EQ(1UL, set.size()); |
| 604 OwnPtrSet::iterator it1 = set.find(ptr1); | 620 ownPtr2 = set.take(ptr2); |
| 605 EXPECT_NE(set.end(), it1); | |
| 606 EXPECT_EQ(ptr1, (*it1)); | |
| 607 | |
| 608 Dummy* ptr2 = new Dummy(deleted2); | |
| 609 { | |
| 610 OwnPtrSet::AddResult res2 = set.add(adoptPtr(ptr2)); | |
| 611 EXPECT_EQ(res2.storedValue->m_value.get(), ptr2); | |
| 612 } | |
| 613 | |
| 614 EXPECT_FALSE(deleted2); | |
| 615 EXPECT_EQ(2UL, set.size()); | |
| 616 OwnPtrSet::iterator it2 = set.find(ptr2); | |
| 617 EXPECT_NE(set.end(), it2); | |
| 618 EXPECT_EQ(ptr2, (*it2)); | |
| 619 | |
| 620 set.remove(ptr1); | |
| 621 EXPECT_TRUE(deleted1); | |
| 622 | |
| 623 set.clear(); | |
| 624 EXPECT_TRUE(deleted2); | |
| 625 EXPECT_TRUE(set.isEmpty()); | 621 EXPECT_TRUE(set.isEmpty()); |
| 626 | 622 } |
| 627 deleted1 = false; | 623 EXPECT_FALSE(deleted1); |
| 628 deleted2 = false; | 624 EXPECT_FALSE(deleted2); |
| 629 { | 625 |
| 630 OwnPtrSet set; | 626 EXPECT_EQ(ptr1, ownPtr1); |
| 631 set.add(adoptPtr(new Dummy(deleted1))); | 627 EXPECT_EQ(ptr2, ownPtr2); |
| 632 set.add(adoptPtr(new Dummy(deleted2))); | 628 } |
| 633 } | 629 |
| 634 EXPECT_TRUE(deleted1); | 630 template <typename Set> |
| 635 EXPECT_TRUE(deleted2); | 631 void swapTestHelper() { |
| 636 | 632 int num = 10; |
| 637 | 633 Set set0; |
| 638 deleted1 = false; | 634 Set set1; |
| 639 deleted2 = false; | 635 Set set2; |
| 640 OwnPtr<Dummy> ownPtr1; | 636 for (int i = 0; i < num; ++i) { |
| 641 OwnPtr<Dummy> ownPtr2; | 637 set1.add(i + 1); |
| 642 ptr1 = new Dummy(deleted1); | 638 set2.add(num - i); |
| 643 ptr2 = new Dummy(deleted2); | 639 } |
| 644 { | 640 |
| 645 OwnPtrSet set; | 641 typename Set::iterator it1 = set1.begin(); |
| 646 set.add(adoptPtr(ptr1)); | 642 typename Set::iterator it2 = set2.begin(); |
| 647 set.add(adoptPtr(ptr2)); | 643 for (int i = 0; i < num; ++i, ++it1, ++it2) { |
| 648 ownPtr1 = set.takeFirst(); | 644 EXPECT_EQ(*it1, i + 1); |
| 649 EXPECT_EQ(1UL, set.size()); | 645 EXPECT_EQ(*it2, num - i); |
| 650 ownPtr2 = set.take(ptr2); | 646 } |
| 651 EXPECT_TRUE(set.isEmpty()); | 647 EXPECT_EQ(set0.begin(), set0.end()); |
| 652 } | 648 EXPECT_EQ(it1, set1.end()); |
| 653 EXPECT_FALSE(deleted1); | 649 EXPECT_EQ(it2, set2.end()); |
| 654 EXPECT_FALSE(deleted2); | 650 |
| 655 | 651 // Shift sets: 2->1, 1->0, 0->2 |
| 656 EXPECT_EQ(ptr1, ownPtr1); | 652 set1.swap(set2); // Swap with non-empty sets. |
| 657 EXPECT_EQ(ptr2, ownPtr2); | 653 set0.swap(set2); // Swap with an empty set. |
| 658 } | 654 |
| 659 | 655 it1 = set0.begin(); |
| 660 template <typename Set> | 656 it2 = set1.begin(); |
| 661 void swapTestHelper() | 657 for (int i = 0; i < num; ++i, ++it1, ++it2) { |
| 662 { | 658 EXPECT_EQ(*it1, i + 1); |
| 663 int num = 10; | 659 EXPECT_EQ(*it2, num - i); |
| 664 Set set0; | 660 } |
| 665 Set set1; | 661 EXPECT_EQ(it1, set0.end()); |
| 666 Set set2; | 662 EXPECT_EQ(it2, set1.end()); |
| 667 for (int i = 0; i < num; ++i) { | 663 EXPECT_EQ(set2.begin(), set2.end()); |
| 668 set1.add(i + 1); | 664 |
| 669 set2.add(num - i); | 665 int removedIndex = num >> 1; |
| 670 } | 666 set0.remove(removedIndex + 1); |
| 671 | 667 set1.remove(num - removedIndex); |
| 672 typename Set::iterator it1 = set1.begin(); | 668 |
| 673 typename Set::iterator it2 = set2.begin(); | 669 it1 = set0.begin(); |
| 674 for (int i = 0; i < num; ++i, ++it1, ++it2) { | 670 it2 = set1.begin(); |
| 675 EXPECT_EQ(*it1, i + 1); | 671 for (int i = 0; i < num; ++i, ++it1, ++it2) { |
| 676 EXPECT_EQ(*it2, num - i); | 672 if (i == removedIndex) |
| 677 } | 673 ++i; |
| 678 EXPECT_EQ(set0.begin(), set0.end()); | 674 EXPECT_EQ(*it1, i + 1); |
| 679 EXPECT_EQ(it1, set1.end()); | 675 EXPECT_EQ(*it2, num - i); |
| 680 EXPECT_EQ(it2, set2.end()); | 676 } |
| 681 | 677 EXPECT_EQ(it1, set0.end()); |
| 682 // Shift sets: 2->1, 1->0, 0->2 | 678 EXPECT_EQ(it2, set1.end()); |
| 683 set1.swap(set2); // Swap with non-empty sets. | 679 } |
| 684 set0.swap(set2); // Swap with an empty set. | 680 |
| 685 | 681 TEST(ListHashSetTest, Swap) { |
| 686 it1 = set0.begin(); | 682 swapTestHelper<ListHashSet<int>>(); |
| 687 it2 = set1.begin(); | 683 } |
| 688 for (int i = 0; i < num; ++i, ++it1, ++it2) { | 684 |
| 689 EXPECT_EQ(*it1, i + 1); | 685 TEST(LinkedHashSetTest, Swap) { |
| 690 EXPECT_EQ(*it2, num - i); | 686 swapTestHelper<LinkedHashSet<int>>(); |
| 691 } | 687 } |
| 692 EXPECT_EQ(it1, set0.end()); | 688 |
| 693 EXPECT_EQ(it2, set1.end()); | 689 } // anonymous namespace |
| 694 EXPECT_EQ(set2.begin(), set2.end()); | 690 |
| 695 | 691 } // namespace WTF |
| 696 int removedIndex = num >> 1; | |
| 697 set0.remove(removedIndex + 1); | |
| 698 set1.remove(num - removedIndex); | |
| 699 | |
| 700 it1 = set0.begin(); | |
| 701 it2 = set1.begin(); | |
| 702 for (int i = 0; i < num; ++i, ++it1, ++it2) { | |
| 703 if (i == removedIndex) | |
| 704 ++i; | |
| 705 EXPECT_EQ(*it1, i + 1); | |
| 706 EXPECT_EQ(*it2, num - i); | |
| 707 } | |
| 708 EXPECT_EQ(it1, set0.end()); | |
| 709 EXPECT_EQ(it2, set1.end()); | |
| 710 } | |
| 711 | |
| 712 TEST(ListHashSetTest, Swap) | |
| 713 { | |
| 714 swapTestHelper<ListHashSet<int>>(); | |
| 715 } | |
| 716 | |
| 717 TEST(LinkedHashSetTest, Swap) | |
| 718 { | |
| 719 swapTestHelper<LinkedHashSet<int>>(); | |
| 720 } | |
| 721 | |
| 722 } // anonymous namespace | |
| 723 | |
| 724 } // namespace WTF | |
| OLD | NEW |