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 // Need this in order to enable RVO on VS2013 (the default traits | |
341 // implementation would produce an unneeded copy). | |
342 static CopyCounter emptyValue() | |
343 { | |
344 CopyCounter empty; | |
345 return empty; | |
346 } | |
347 #endif | |
348 static const bool hasIsEmptyValueFunction = true; | |
349 static bool isEmptyValue(const CopyCounter& counter) { return !counter.data(
); } | |
350 }; | |
351 | |
352 TEST(HashMapTest, LookupNoKeyCopies) | |
353 { | |
354 HashMap<CopyCounter, int, CopyCounterHash, CopyCounterHashTraits> map; | |
355 | |
356 map.contains(CopyCounter(1)); | |
357 auto it = map.find(CopyCounter(1)); | |
358 ALLOW_UNUSED_LOCAL(it); | |
359 EXPECT_EQ(0, CopyCounter::s_counter); | |
360 } | |
361 | |
362 } // anonymous namespace | 306 } // anonymous namespace |
363 | 307 |
364 } // namespace WTF | 308 } // namespace WTF |
OLD | NEW |