OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google 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 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 | 296 |
297 TEST(HashMapTest, ValueTypeDestructed) | 297 TEST(HashMapTest, ValueTypeDestructed) |
298 { | 298 { |
299 InstanceCounter::counter = 0; | 299 InstanceCounter::counter = 0; |
300 HashMap<int, InstanceCounter> map; | 300 HashMap<int, InstanceCounter> map; |
301 map.set(1, InstanceCounter()); | 301 map.set(1, InstanceCounter()); |
302 map.clear(); | 302 map.clear(); |
303 EXPECT_EQ(0, InstanceCounter::counter); | 303 EXPECT_EQ(0, InstanceCounter::counter); |
304 } | 304 } |
305 | 305 |
306 class CopyCounter { | |
307 public: | |
308 CopyCounter() = default; | |
309 explicit CopyCounter(int data) : m_data(data) { } | |
310 | |
311 CopyCounter(const CopyCounter& copy) : m_data(copy.m_data) { ++s_counter; } | |
312 | |
313 int data() const { return m_data; } | |
314 | |
315 bool isHashTableDeletedValue() const { return m_data == HashTableDeletedValu e; } | |
316 | |
317 static int s_counter; | |
318 private: | |
319 int m_data; | |
320 }; | |
321 | |
322 int CopyCounter::s_counter = 0; | |
323 | |
324 struct CopyCounterHash { | |
325 static unsigned hash(const CopyCounter& key) | |
326 { | |
327 return IntHash<unsigned>::hash(key.data()); | |
328 } | |
329 | |
330 static bool equal(const CopyCounter& a, const CopyCounter& b) | |
331 { | |
332 return a.data() == b.data(); | |
333 } | |
334 | |
335 static const bool safeToCompareToEmptyOrDeleted = true; | |
336 }; | |
337 | |
338 struct CopyCounterHashTraits: SimpleClassHashTraits<CopyCounter> { | |
339 #if COMPILER(MSVC) | |
340 static CopyCounter emptyValue() | |
tkent
2015/07/30 23:55:54
Please add a comment why this is necessary.
| |
341 { | |
342 CopyCounter empty; | |
343 return empty; | |
344 } | |
345 #endif | |
346 static const bool hasIsEmptyValueFunction = true; | |
347 static bool isEmptyValue(const CopyCounter& counter) { return !counter.data( ); } | |
348 }; | |
349 | |
350 TEST(HashMapTest, LookupNoKeyCopies) | |
351 { | |
352 HashMap<CopyCounter, int, CopyCounterHash, CopyCounterHashTraits> map; | |
353 | |
354 map.contains(CopyCounter(1)); | |
355 auto it = map.find(CopyCounter(1)); | |
356 ALLOW_UNUSED_LOCAL(it); | |
357 EXPECT_EQ(0, CopyCounter::s_counter); | |
358 } | |
359 | |
306 } // anonymous namespace | 360 } // anonymous namespace |
307 | 361 |
308 } // namespace WTF | 362 } // namespace WTF |
OLD | NEW |