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