You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2982 lines
111 KiB

  1. // ----------------------------------------------------------------------
  2. // Copyright (c) 2016, Gregory Popovitch - greg7mdp@gmail.com
  3. // All rights reserved.
  4. //
  5. // This work is derived from Google's sparsehash library
  6. //
  7. // Copyright (c) 2010, Google Inc.
  8. // All rights reserved.
  9. //
  10. // Redistribution and use in source and binary forms, with or without
  11. // modification, are permitted provided that the following conditions are
  12. // met:
  13. //
  14. // * Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. // * Redistributions in binary form must reproduce the above
  17. // copyright notice, this list of conditions and the following disclaimer
  18. // in the documentation and/or other materials provided with the
  19. // distribution.
  20. // * Neither the name of Google Inc. nor the names of its
  21. // contributors may be used to endorse or promote products derived from
  22. // this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. // ----------------------------------------------------------------------
  36. #ifdef _MSC_VER
  37. #pragma warning( disable : 4820 ) // '6' bytes padding added after data member...
  38. #pragma warning( disable : 4710 ) // function not inlined
  39. #pragma warning( disable : 4514 ) // unreferenced inline function has been removed
  40. #pragma warning( disable : 4996 ) // 'fopen': This function or variable may be unsafe
  41. #endif
  42. #include "sparsepp.h"
  43. #ifdef _MSC_VER
  44. #pragma warning( disable : 4127 ) // conditional expression is constant
  45. #pragma warning(push, 0)
  46. #endif
  47. #include <math.h>
  48. #include <stddef.h> // for size_t
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <iostream>
  53. #include <set>
  54. #include <sstream>
  55. #include <typeinfo> // for class typeinfo (returned by typeid)
  56. #include <vector>
  57. #include <stdexcept> // for length_error
  58. namespace sparsehash_internal = SPP_NAMESPACE::sparsehash_internal;
  59. using SPP_NAMESPACE::sparsetable;
  60. using SPP_NAMESPACE::sparse_hashtable;
  61. using SPP_NAMESPACE::sparse_hash_map;
  62. using SPP_NAMESPACE::sparse_hash_set;
  63. // ---------------------------------------------------------------------
  64. // ---------------------------------------------------------------------
  65. #ifndef _MSC_VER // windows defines its own version
  66. #define _strdup strdup
  67. #ifdef __MINGW32__ // mingw has trouble writing to /tmp
  68. static std::string TmpFile(const char* basename)
  69. {
  70. return std::string("./#") + basename;
  71. }
  72. #endif
  73. #else
  74. #pragma warning(disable : 4996)
  75. #define snprintf sprintf_s
  76. #define WIN32_LEAN_AND_MEAN /* We always want minimal includes */
  77. #include <windows.h>
  78. std::string TmpFile(const char* basename)
  79. {
  80. char tmppath_buffer[1024];
  81. int tmppath_len = GetTempPathA(sizeof(tmppath_buffer), tmppath_buffer);
  82. if (tmppath_len <= 0 || tmppath_len >= sizeof(tmppath_buffer))
  83. return basename; // an error, so just bail on tmppath
  84. sprintf_s(tmppath_buffer + tmppath_len, 1024 - tmppath_len, "\\%s", basename);
  85. return tmppath_buffer;
  86. }
  87. #endif
  88. #ifdef _MSC_VER
  89. #pragma warning(pop)
  90. #endif
  91. // ---------------------------------------------------------------------
  92. // This is the "default" interface, which just passes everything
  93. // through to the underlying hashtable. You'll need to subclass it to
  94. // specialize behavior for an individual hashtable.
  95. // ---------------------------------------------------------------------
  96. template <class HT>
  97. class BaseHashtableInterface
  98. {
  99. public:
  100. virtual ~BaseHashtableInterface() {}
  101. typedef typename HT::key_type key_type;
  102. typedef typename HT::value_type value_type;
  103. typedef typename HT::hasher hasher;
  104. typedef typename HT::key_equal key_equal;
  105. typedef typename HT::allocator_type allocator_type;
  106. typedef typename HT::size_type size_type;
  107. typedef typename HT::difference_type difference_type;
  108. typedef typename HT::pointer pointer;
  109. typedef typename HT::const_pointer const_pointer;
  110. typedef typename HT::reference reference;
  111. typedef typename HT::const_reference const_reference;
  112. class const_iterator;
  113. class iterator : public HT::iterator
  114. {
  115. public:
  116. iterator() : parent_(NULL) { } // this allows code like "iterator it;"
  117. iterator(typename HT::iterator it, const BaseHashtableInterface* parent)
  118. : HT::iterator(it), parent_(parent) { }
  119. key_type key() { return parent_->it_to_key(*this); }
  120. private:
  121. friend class BaseHashtableInterface::const_iterator; // for its ctor
  122. const BaseHashtableInterface* parent_;
  123. };
  124. class const_iterator : public HT::const_iterator
  125. {
  126. public:
  127. const_iterator() : parent_(NULL) { }
  128. const_iterator(typename HT::const_iterator it,
  129. const BaseHashtableInterface* parent)
  130. : HT::const_iterator(it), parent_(parent) { }
  131. const_iterator(typename HT::iterator it,
  132. BaseHashtableInterface* parent)
  133. : HT::const_iterator(it), parent_(parent) { }
  134. // The parameter type here *should* just be "iterator", but MSVC
  135. // gets confused by that, so I'm overly specific.
  136. const_iterator(typename BaseHashtableInterface<HT>::iterator it)
  137. : HT::const_iterator(it), parent_(it.parent_) { }
  138. key_type key() { return parent_->it_to_key(*this); }
  139. private:
  140. const BaseHashtableInterface* parent_;
  141. };
  142. class const_local_iterator;
  143. class local_iterator : public HT::local_iterator
  144. {
  145. public:
  146. local_iterator() : parent_(NULL) { }
  147. local_iterator(typename HT::local_iterator it,
  148. const BaseHashtableInterface* parent)
  149. : HT::local_iterator(it), parent_(parent) { }
  150. key_type key() { return parent_->it_to_key(*this); }
  151. private:
  152. friend class BaseHashtableInterface::const_local_iterator; // for its ctor
  153. const BaseHashtableInterface* parent_;
  154. };
  155. class const_local_iterator : public HT::const_local_iterator
  156. {
  157. public:
  158. const_local_iterator() : parent_(NULL) { }
  159. const_local_iterator(typename HT::const_local_iterator it,
  160. const BaseHashtableInterface* parent)
  161. : HT::const_local_iterator(it), parent_(parent) { }
  162. const_local_iterator(typename HT::local_iterator it,
  163. BaseHashtableInterface* parent)
  164. : HT::const_local_iterator(it), parent_(parent) { }
  165. const_local_iterator(local_iterator it)
  166. : HT::const_local_iterator(it), parent_(it.parent_) { }
  167. key_type key() { return parent_->it_to_key(*this); }
  168. private:
  169. const BaseHashtableInterface* parent_;
  170. };
  171. iterator begin() { return iterator(ht_.begin(), this); }
  172. iterator end() { return iterator(ht_.end(), this); }
  173. const_iterator begin() const { return const_iterator(ht_.begin(), this); }
  174. const_iterator end() const { return const_iterator(ht_.end(), this); }
  175. local_iterator begin(size_type i) { return local_iterator(ht_.begin(i), this); }
  176. local_iterator end(size_type i) { return local_iterator(ht_.end(i), this); }
  177. const_local_iterator begin(size_type i) const { return const_local_iterator(ht_.begin(i), this); }
  178. const_local_iterator end(size_type i) const { return const_local_iterator(ht_.end(i), this); }
  179. hasher hash_funct() const { return ht_.hash_funct(); }
  180. hasher hash_function() const { return ht_.hash_function(); }
  181. key_equal key_eq() const { return ht_.key_eq(); }
  182. allocator_type get_allocator() const { return ht_.get_allocator(); }
  183. BaseHashtableInterface(size_type expected_max_items_in_table,
  184. const hasher& hf,
  185. const key_equal& eql,
  186. const allocator_type& alloc)
  187. : ht_(expected_max_items_in_table, hf, eql, alloc) { }
  188. // Not all ht_'s support this constructor: you should only call it
  189. // from a subclass if you know your ht supports it. Otherwise call
  190. // the previous constructor, followed by 'insert(f, l);'.
  191. template <class InputIterator>
  192. BaseHashtableInterface(InputIterator f, InputIterator l,
  193. size_type expected_max_items_in_table,
  194. const hasher& hf,
  195. const key_equal& eql,
  196. const allocator_type& alloc)
  197. : ht_(f, l, expected_max_items_in_table, hf, eql, alloc) {
  198. }
  199. // This is the version of the constructor used by dense_*, which
  200. // requires an empty key in the constructor.
  201. template <class InputIterator>
  202. BaseHashtableInterface(InputIterator f, InputIterator l, key_type empty_k,
  203. size_type expected_max_items_in_table,
  204. const hasher& hf,
  205. const key_equal& eql,
  206. const allocator_type& alloc)
  207. : ht_(f, l, empty_k, expected_max_items_in_table, hf, eql, alloc) {
  208. }
  209. // This is the constructor appropriate for {dense,sparse}hashtable.
  210. template <class ExtractKey, class SetKey>
  211. BaseHashtableInterface(size_type expected_max_items_in_table,
  212. const hasher& hf,
  213. const key_equal& eql,
  214. const ExtractKey& ek,
  215. const SetKey& sk,
  216. const allocator_type& alloc)
  217. : ht_(expected_max_items_in_table, hf, eql, ek, sk, alloc) { }
  218. void clear() { ht_.clear(); }
  219. void swap(BaseHashtableInterface& other) { ht_.swap(other.ht_); }
  220. // Only part of the API for some hashtable implementations.
  221. void clear_no_resize() { clear(); }
  222. size_type size() const { return ht_.size(); }
  223. size_type max_size() const { return ht_.max_size(); }
  224. bool empty() const { return ht_.empty(); }
  225. size_type bucket_count() const { return ht_.bucket_count(); }
  226. size_type max_bucket_count() const { return ht_.max_bucket_count(); }
  227. size_type bucket_size(size_type i) const {
  228. return ht_.bucket_size(i);
  229. }
  230. size_type bucket(const key_type& key) const {
  231. return ht_.bucket(key);
  232. }
  233. float load_factor() const { return ht_.load_factor(); }
  234. float max_load_factor() const { return ht_.max_load_factor(); }
  235. void max_load_factor(float grow) { ht_.max_load_factor(grow); }
  236. float min_load_factor() const { return ht_.min_load_factor(); }
  237. void min_load_factor(float shrink) { ht_.min_load_factor(shrink); }
  238. void set_resizing_parameters(float shrink, float grow) {
  239. ht_.set_resizing_parameters(shrink, grow);
  240. }
  241. void resize(size_type hint) { ht_.resize(hint); }
  242. void rehash(size_type hint) { ht_.rehash(hint); }
  243. iterator find(const key_type& key) {
  244. return iterator(ht_.find(key), this);
  245. }
  246. const_iterator find(const key_type& key) const {
  247. return const_iterator(ht_.find(key), this);
  248. }
  249. // Rather than try to implement operator[], which doesn't make much
  250. // sense for set types, we implement two methods: bracket_equal and
  251. // bracket_assign. By default, bracket_equal(a, b) returns true if
  252. // ht[a] == b, and false otherwise. (Note that this follows
  253. // operator[] semantics exactly, including inserting a if it's not
  254. // already in the hashtable, before doing the equality test.) For
  255. // sets, which have no operator[], b is ignored, and bracket_equal
  256. // returns true if key is in the set and false otherwise.
  257. // bracket_assign(a, b) is equivalent to ht[a] = b. For sets, b is
  258. // ignored, and bracket_assign is equivalent to ht.insert(a).
  259. template<typename AssignValue>
  260. bool bracket_equal(const key_type& key, const AssignValue& expected) {
  261. return ht_[key] == expected;
  262. }
  263. template<typename AssignValue>
  264. void bracket_assign(const key_type& key, const AssignValue& value) {
  265. ht_[key] = value;
  266. }
  267. size_type count(const key_type& key) const { return ht_.count(key); }
  268. std::pair<iterator, iterator> equal_range(const key_type& key)
  269. {
  270. std::pair<typename HT::iterator, typename HT::iterator> r
  271. = ht_.equal_range(key);
  272. return std::pair<iterator, iterator>(iterator(r.first, this),
  273. iterator(r.second, this));
  274. }
  275. std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const
  276. {
  277. std::pair<typename HT::const_iterator, typename HT::const_iterator> r
  278. = ht_.equal_range(key);
  279. return std::pair<const_iterator, const_iterator>(
  280. const_iterator(r.first, this), const_iterator(r.second, this));
  281. }
  282. const_iterator random_element(class ACMRandom* r) const {
  283. return const_iterator(ht_.random_element(r), this);
  284. }
  285. iterator random_element(class ACMRandom* r) {
  286. return iterator(ht_.random_element(r), this);
  287. }
  288. std::pair<iterator, bool> insert(const value_type& obj) {
  289. std::pair<typename HT::iterator, bool> r = ht_.insert(obj);
  290. return std::pair<iterator, bool>(iterator(r.first, this), r.second);
  291. }
  292. template <class InputIterator>
  293. void insert(InputIterator f, InputIterator l) {
  294. ht_.insert(f, l);
  295. }
  296. void insert(typename HT::const_iterator f, typename HT::const_iterator l) {
  297. ht_.insert(f, l);
  298. }
  299. iterator insert(typename HT::iterator, const value_type& obj) {
  300. return iterator(insert(obj).first, this);
  301. }
  302. // These will commonly need to be overridden by the child.
  303. void set_empty_key(const key_type& k) { ht_.set_empty_key(k); }
  304. void clear_empty_key() { ht_.clear_empty_key(); }
  305. key_type empty_key() const { return ht_.empty_key(); }
  306. void set_deleted_key(const key_type& k) { ht_.set_deleted_key(k); }
  307. void clear_deleted_key() { ht_.clear_deleted_key(); }
  308. key_type deleted_key() const { return ht_.deleted_key(); }
  309. size_type erase(const key_type& key) { return ht_.erase(key); }
  310. void erase(typename HT::iterator it) { ht_.erase(it); }
  311. void erase(typename HT::iterator f, typename HT::iterator l) {
  312. ht_.erase(f, l);
  313. }
  314. bool operator==(const BaseHashtableInterface& other) const {
  315. return ht_ == other.ht_;
  316. }
  317. bool operator!=(const BaseHashtableInterface& other) const {
  318. return ht_ != other.ht_;
  319. }
  320. template <typename ValueSerializer, typename OUTPUT>
  321. bool serialize(ValueSerializer serializer, OUTPUT *fp) {
  322. return ht_.serialize(serializer, fp);
  323. }
  324. template <typename ValueSerializer, typename INPUT>
  325. bool unserialize(ValueSerializer serializer, INPUT *fp) {
  326. return ht_.unserialize(serializer, fp);
  327. }
  328. template <typename OUTPUT>
  329. bool write_metadata(OUTPUT *fp) {
  330. return ht_.write_metadata(fp);
  331. }
  332. template <typename INPUT>
  333. bool read_metadata(INPUT *fp) {
  334. return ht_.read_metadata(fp);
  335. }
  336. template <typename OUTPUT>
  337. bool write_nopointer_data(OUTPUT *fp) {
  338. return ht_.write_nopointer_data(fp);
  339. }
  340. template <typename INPUT>
  341. bool read_nopointer_data(INPUT *fp) {
  342. return ht_.read_nopointer_data(fp);
  343. }
  344. // low-level stats
  345. int num_table_copies() const { return (int)ht_.num_table_copies(); }
  346. // Not part of the hashtable API, but is provided to make testing easier.
  347. virtual key_type get_key(const value_type& value) const = 0;
  348. // All subclasses should define get_data(value_type) as well. I don't
  349. // provide an abstract-virtual definition here, because the return type
  350. // differs between subclasses (not all subclasses define data_type).
  351. //virtual data_type get_data(const value_type& value) const = 0;
  352. //virtual data_type default_data() const = 0;
  353. // These allow introspection into the interface. "Supports" means
  354. // that the implementation of this functionality isn't a noop.
  355. virtual bool supports_clear_no_resize() const = 0;
  356. virtual bool supports_empty_key() const = 0;
  357. virtual bool supports_deleted_key() const = 0;
  358. virtual bool supports_brackets() const = 0; // has a 'real' operator[]
  359. virtual bool supports_readwrite() const = 0;
  360. virtual bool supports_num_table_copies() const = 0;
  361. virtual bool supports_serialization() const = 0;
  362. protected:
  363. HT ht_;
  364. // These are what subclasses have to define to get class-specific behavior
  365. virtual key_type it_to_key(const iterator& it) const = 0;
  366. virtual key_type it_to_key(const const_iterator& it) const = 0;
  367. virtual key_type it_to_key(const local_iterator& it) const = 0;
  368. virtual key_type it_to_key(const const_local_iterator& it) const = 0;
  369. };
  370. // ---------------------------------------------------------------------
  371. // ---------------------------------------------------------------------
  372. template <class Key, class T,
  373. class HashFcn = SPP_HASH_CLASS<Key>,
  374. class EqualKey = std::equal_to<Key>,
  375. class Alloc = spp::libc_allocator_with_realloc<std::pair<const Key, T> > >
  376. class HashtableInterface_SparseHashMap
  377. : public BaseHashtableInterface< sparse_hash_map<Key, T, HashFcn,
  378. EqualKey, Alloc> >
  379. {
  380. private:
  381. typedef sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc> ht;
  382. typedef BaseHashtableInterface<ht> p; // parent
  383. public:
  384. explicit HashtableInterface_SparseHashMap(
  385. typename p::size_type expected_max_items = 0,
  386. const typename p::hasher& hf = typename p::hasher(),
  387. const typename p::key_equal& eql = typename p::key_equal(),
  388. const typename p::allocator_type& alloc = typename p::allocator_type())
  389. : BaseHashtableInterface<ht>(expected_max_items, hf, eql, alloc) { }
  390. template <class InputIterator>
  391. HashtableInterface_SparseHashMap(
  392. InputIterator f, InputIterator l,
  393. typename p::size_type expected_max_items = 0,
  394. const typename p::hasher& hf = typename p::hasher(),
  395. const typename p::key_equal& eql = typename p::key_equal(),
  396. const typename p::allocator_type& alloc = typename p::allocator_type())
  397. : BaseHashtableInterface<ht>(f, l, expected_max_items, hf, eql, alloc) { }
  398. typename p::key_type get_key(const typename p::value_type& value) const {
  399. return value.first;
  400. }
  401. typename ht::data_type get_data(const typename p::value_type& value) const {
  402. return value.second;
  403. }
  404. typename ht::data_type default_data() const {
  405. return typename ht::data_type();
  406. }
  407. bool supports_clear_no_resize() const { return false; }
  408. bool supports_empty_key() const { return false; }
  409. bool supports_deleted_key() const { return false; }
  410. bool supports_brackets() const { return true; }
  411. bool supports_readwrite() const { return true; }
  412. bool supports_num_table_copies() const { return false; }
  413. bool supports_serialization() const { return true; }
  414. void set_empty_key(const typename p::key_type&) { }
  415. void clear_empty_key() { }
  416. typename p::key_type empty_key() const { return typename p::key_type(); }
  417. int num_table_copies() const { return 0; }
  418. typedef typename ht::NopointerSerializer NopointerSerializer;
  419. protected:
  420. template <class K2, class T2, class H2, class E2, class A2>
  421. friend void swap(HashtableInterface_SparseHashMap<K2,T2,H2,E2,A2>& a,
  422. HashtableInterface_SparseHashMap<K2,T2,H2,E2,A2>& b);
  423. typename p::key_type it_to_key(const typename p::iterator& it) const {
  424. return it->first;
  425. }
  426. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  427. return it->first;
  428. }
  429. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  430. return it->first;
  431. }
  432. typename p::key_type it_to_key(const typename p::const_local_iterator& it) const {
  433. return it->first;
  434. }
  435. };
  436. // ---------------------------------------------------------------------
  437. // ---------------------------------------------------------------------
  438. template <class K, class T, class H, class E, class A>
  439. void swap(HashtableInterface_SparseHashMap<K,T,H,E,A>& a,
  440. HashtableInterface_SparseHashMap<K,T,H,E,A>& b)
  441. {
  442. swap(a.ht_, b.ht_);
  443. }
  444. // ---------------------------------------------------------------------
  445. // ---------------------------------------------------------------------
  446. template <class Value,
  447. class HashFcn = SPP_HASH_CLASS<Value>,
  448. class EqualKey = std::equal_to<Value>,
  449. class Alloc = spp::libc_allocator_with_realloc<Value> >
  450. class HashtableInterface_SparseHashSet
  451. : public BaseHashtableInterface< sparse_hash_set<Value, HashFcn,
  452. EqualKey, Alloc> >
  453. {
  454. private:
  455. typedef sparse_hash_set<Value, HashFcn, EqualKey, Alloc> ht;
  456. typedef BaseHashtableInterface<ht> p; // parent
  457. public:
  458. explicit HashtableInterface_SparseHashSet(
  459. typename p::size_type expected_max_items = 0,
  460. const typename p::hasher& hf = typename p::hasher(),
  461. const typename p::key_equal& eql = typename p::key_equal(),
  462. const typename p::allocator_type& alloc = typename p::allocator_type())
  463. : BaseHashtableInterface<ht>(expected_max_items, hf, eql, alloc) { }
  464. template <class InputIterator>
  465. HashtableInterface_SparseHashSet(
  466. InputIterator f, InputIterator l,
  467. typename p::size_type expected_max_items = 0,
  468. const typename p::hasher& hf = typename p::hasher(),
  469. const typename p::key_equal& eql = typename p::key_equal(),
  470. const typename p::allocator_type& alloc = typename p::allocator_type())
  471. : BaseHashtableInterface<ht>(f, l, expected_max_items, hf, eql, alloc) { }
  472. template<typename AssignValue>
  473. bool bracket_equal(const typename p::key_type& key, const AssignValue&) {
  474. return this->ht_.find(key) != this->ht_.end();
  475. }
  476. template<typename AssignValue>
  477. void bracket_assign(const typename p::key_type& key, const AssignValue&) {
  478. this->ht_.insert(key);
  479. }
  480. typename p::key_type get_key(const typename p::value_type& value) const {
  481. return value;
  482. }
  483. // For sets, the only 'data' is that an item is actually inserted.
  484. bool get_data(const typename p::value_type&) const {
  485. return true;
  486. }
  487. bool default_data() const {
  488. return true;
  489. }
  490. bool supports_clear_no_resize() const { return false; }
  491. bool supports_empty_key() const { return false; }
  492. bool supports_deleted_key() const { return false; }
  493. bool supports_brackets() const { return false; }
  494. bool supports_readwrite() const { return true; }
  495. bool supports_num_table_copies() const { return false; }
  496. bool supports_serialization() const { return true; }
  497. void set_empty_key(const typename p::key_type&) { }
  498. void clear_empty_key() { }
  499. typename p::key_type empty_key() const { return typename p::key_type(); }
  500. int num_table_copies() const { return 0; }
  501. typedef typename ht::NopointerSerializer NopointerSerializer;
  502. protected:
  503. template <class K2, class H2, class E2, class A2>
  504. friend void swap(HashtableInterface_SparseHashSet<K2,H2,E2,A2>& a,
  505. HashtableInterface_SparseHashSet<K2,H2,E2,A2>& b);
  506. typename p::key_type it_to_key(const typename p::iterator& it) const {
  507. return *it;
  508. }
  509. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  510. return *it;
  511. }
  512. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  513. return *it;
  514. }
  515. typename p::key_type it_to_key(const typename p::const_local_iterator& it)
  516. const {
  517. return *it;
  518. }
  519. };
  520. // ---------------------------------------------------------------------
  521. // ---------------------------------------------------------------------
  522. template <class K, class H, class E, class A>
  523. void swap(HashtableInterface_SparseHashSet<K,H,E,A>& a,
  524. HashtableInterface_SparseHashSet<K,H,E,A>& b)
  525. {
  526. swap(a.ht_, b.ht_);
  527. }
  528. // ---------------------------------------------------------------------
  529. // ---------------------------------------------------------------------
  530. template <class Value, class Key, class HashFcn, class ExtractKey,
  531. class SetKey, class EqualKey, class Alloc>
  532. class HashtableInterface_SparseHashtable
  533. : public BaseHashtableInterface< sparse_hashtable<Value, Key, HashFcn,
  534. ExtractKey, SetKey,
  535. EqualKey, Alloc> >
  536. {
  537. private:
  538. typedef sparse_hashtable<Value, Key, HashFcn, ExtractKey, SetKey,
  539. EqualKey, Alloc> ht;
  540. typedef BaseHashtableInterface<ht> p; // parent
  541. public:
  542. explicit HashtableInterface_SparseHashtable(
  543. typename p::size_type expected_max_items = 0,
  544. const typename p::hasher& hf = typename p::hasher(),
  545. const typename p::key_equal& eql = typename p::key_equal(),
  546. const typename p::allocator_type& alloc = typename p::allocator_type())
  547. : BaseHashtableInterface<ht>(expected_max_items, hf, eql,
  548. ExtractKey(), SetKey(), alloc) { }
  549. template <class InputIterator>
  550. HashtableInterface_SparseHashtable(
  551. InputIterator f, InputIterator l,
  552. typename p::size_type expected_max_items = 0,
  553. const typename p::hasher& hf = typename p::hasher(),
  554. const typename p::key_equal& eql = typename p::key_equal(),
  555. const typename p::allocator_type& alloc = typename p::allocator_type())
  556. : BaseHashtableInterface<ht>(expected_max_items, hf, eql,
  557. ExtractKey(), SetKey(), alloc) {
  558. this->insert(f, l);
  559. }
  560. float max_load_factor() const {
  561. float shrink, grow;
  562. this->ht_.get_resizing_parameters(&shrink, &grow);
  563. return grow;
  564. }
  565. void max_load_factor(float new_grow) {
  566. float shrink, grow;
  567. this->ht_.get_resizing_parameters(&shrink, &grow);
  568. this->ht_.set_resizing_parameters(shrink, new_grow);
  569. }
  570. float min_load_factor() const {
  571. float shrink, grow;
  572. this->ht_.get_resizing_parameters(&shrink, &grow);
  573. return shrink;
  574. }
  575. void min_load_factor(float new_shrink) {
  576. float shrink, grow;
  577. this->ht_.get_resizing_parameters(&shrink, &grow);
  578. this->ht_.set_resizing_parameters(new_shrink, grow);
  579. }
  580. template<typename AssignValue>
  581. bool bracket_equal(const typename p::key_type&, const AssignValue&) {
  582. return false;
  583. }
  584. template<typename AssignValue>
  585. void bracket_assign(const typename p::key_type&, const AssignValue&) {
  586. }
  587. typename p::key_type get_key(const typename p::value_type& value) const {
  588. return extract_key(value);
  589. }
  590. typename p::value_type get_data(const typename p::value_type& value) const {
  591. return value;
  592. }
  593. typename p::value_type default_data() const {
  594. return typename p::value_type();
  595. }
  596. bool supports_clear_no_resize() const { return false; }
  597. bool supports_empty_key() const { return false; }
  598. bool supports_deleted_key() const { return false; }
  599. bool supports_brackets() const { return false; }
  600. bool supports_readwrite() const { return true; }
  601. bool supports_num_table_copies() const { return true; }
  602. bool supports_serialization() const { return true; }
  603. void set_empty_key(const typename p::key_type&) { }
  604. void clear_empty_key() { }
  605. typename p::key_type empty_key() const { return typename p::key_type(); }
  606. // These tr1 names aren't defined for sparse_hashtable.
  607. typename p::hasher hash_function() { return this->hash_funct(); }
  608. void rehash(typename p::size_type hint) { this->resize(hint); }
  609. // TODO(csilvers): also support/test destructive_begin()/destructive_end()?
  610. typedef typename ht::NopointerSerializer NopointerSerializer;
  611. protected:
  612. template <class V2, class K2, class HF2, class EK2, class SK2, class Eq2,
  613. class A2>
  614. friend void swap(
  615. HashtableInterface_SparseHashtable<V2,K2,HF2,EK2,SK2,Eq2,A2>& a,
  616. HashtableInterface_SparseHashtable<V2,K2,HF2,EK2,SK2,Eq2,A2>& b);
  617. typename p::key_type it_to_key(const typename p::iterator& it) const {
  618. return extract_key(*it);
  619. }
  620. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  621. return extract_key(*it);
  622. }
  623. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  624. return extract_key(*it);
  625. }
  626. typename p::key_type it_to_key(const typename p::const_local_iterator& it)
  627. const {
  628. return extract_key(*it);
  629. }
  630. private:
  631. ExtractKey extract_key;
  632. };
  633. // ---------------------------------------------------------------------
  634. // ---------------------------------------------------------------------
  635. template <class V, class K, class HF, class EK, class SK, class Eq, class A>
  636. void swap(HashtableInterface_SparseHashtable<V,K,HF,EK,SK,Eq,A>& a,
  637. HashtableInterface_SparseHashtable<V,K,HF,EK,SK,Eq,A>& b) {
  638. swap(a.ht_, b.ht_);
  639. }
  640. void EXPECT_TRUE(bool cond)
  641. {
  642. if (!cond)
  643. {
  644. ::fputs("Test failed:\n", stderr);
  645. ::exit(1);
  646. }
  647. }
  648. SPP_START_NAMESPACE
  649. namespace testing
  650. {
  651. #define EXPECT_FALSE(a) EXPECT_TRUE(!(a))
  652. #define EXPECT_EQ(a, b) EXPECT_TRUE((a) == (b))
  653. #define EXPECT_NE(a, b) EXPECT_TRUE((a) != (b))
  654. #define EXPECT_LT(a, b) EXPECT_TRUE((a) < (b))
  655. #define EXPECT_GT(a, b) EXPECT_TRUE((a) > (b))
  656. #define EXPECT_LE(a, b) EXPECT_TRUE((a) <= (b))
  657. #define EXPECT_GE(a, b) EXPECT_TRUE((a) >= (b))
  658. #define EXPECT_DEATH(cmd, expected_error_string) \
  659. try { \
  660. cmd; \
  661. EXPECT_FALSE("did not see expected error: " #expected_error_string); \
  662. } catch (const std::length_error&) { \
  663. /* Good, the cmd failed. */ \
  664. }
  665. #define TEST(suitename, testname) \
  666. class TEST_##suitename##_##testname { \
  667. public: \
  668. TEST_##suitename##_##testname() { \
  669. ::fputs("Running " #suitename "." #testname "\n", stderr); \
  670. Run(); \
  671. } \
  672. void Run(); \
  673. }; \
  674. static TEST_##suitename##_##testname \
  675. test_instance_##suitename##_##testname; \
  676. void TEST_##suitename##_##testname::Run()
  677. template<typename C1, typename C2, typename C3>
  678. struct TypeList3
  679. {
  680. typedef C1 type1;
  681. typedef C2 type2;
  682. typedef C3 type3;
  683. };
  684. // I need to list 9 types here, for code below to compile, though
  685. // only the first 3 are ever used.
  686. #define TYPED_TEST_CASE_3(classname, typelist) \
  687. typedef typelist::type1 classname##_type1; \
  688. typedef typelist::type2 classname##_type2; \
  689. typedef typelist::type3 classname##_type3; \
  690. SPP_ATTRIBUTE_UNUSED static const int classname##_numtypes = 3; \
  691. typedef typelist::type1 classname##_type4; \
  692. typedef typelist::type1 classname##_type5; \
  693. typedef typelist::type1 classname##_type6; \
  694. typedef typelist::type1 classname##_type7; \
  695. typedef typelist::type1 classname##_type8; \
  696. typedef typelist::type1 classname##_type9
  697. template<typename C1, typename C2, typename C3, typename C4, typename C5,
  698. typename C6, typename C7, typename C8, typename C9>
  699. struct TypeList9
  700. {
  701. typedef C1 type1;
  702. typedef C2 type2;
  703. typedef C3 type3;
  704. typedef C4 type4;
  705. typedef C5 type5;
  706. typedef C6 type6;
  707. typedef C7 type7;
  708. typedef C8 type8;
  709. typedef C9 type9;
  710. };
  711. #define TYPED_TEST_CASE_9(classname, typelist) \
  712. typedef typelist::type1 classname##_type1; \
  713. typedef typelist::type2 classname##_type2; \
  714. typedef typelist::type3 classname##_type3; \
  715. typedef typelist::type4 classname##_type4; \
  716. typedef typelist::type5 classname##_type5; \
  717. typedef typelist::type6 classname##_type6; \
  718. typedef typelist::type7 classname##_type7; \
  719. typedef typelist::type8 classname##_type8; \
  720. typedef typelist::type9 classname##_type9; \
  721. static const int classname##_numtypes = 9
  722. #define TYPED_TEST(superclass, testname) \
  723. template<typename TypeParam> \
  724. class TEST_onetype_##superclass##_##testname : \
  725. public superclass<TypeParam> { \
  726. public: \
  727. TEST_onetype_##superclass##_##testname() { \
  728. Run(); \
  729. } \
  730. private: \
  731. void Run(); \
  732. }; \
  733. class TEST_typed_##superclass##_##testname { \
  734. public: \
  735. explicit TEST_typed_##superclass##_##testname() { \
  736. if (superclass##_numtypes >= 1) { \
  737. ::fputs("Running " #superclass "." #testname ".1\n", stderr); \
  738. TEST_onetype_##superclass##_##testname<superclass##_type1> t; \
  739. } \
  740. if (superclass##_numtypes >= 2) { \
  741. ::fputs("Running " #superclass "." #testname ".2\n", stderr); \
  742. TEST_onetype_##superclass##_##testname<superclass##_type2> t; \
  743. } \
  744. if (superclass##_numtypes >= 3) { \
  745. ::fputs("Running " #superclass "." #testname ".3\n", stderr); \
  746. TEST_onetype_##superclass##_##testname<superclass##_type3> t; \
  747. } \
  748. if (superclass##_numtypes >= 4) { \
  749. ::fputs("Running " #superclass "." #testname ".4\n", stderr); \
  750. TEST_onetype_##superclass##_##testname<superclass##_type4> t; \
  751. } \
  752. if (superclass##_numtypes >= 5) { \
  753. ::fputs("Running " #superclass "." #testname ".5\n", stderr); \
  754. TEST_onetype_##superclass##_##testname<superclass##_type5> t; \
  755. } \
  756. if (superclass##_numtypes >= 6) { \
  757. ::fputs("Running " #superclass "." #testname ".6\n", stderr); \
  758. TEST_onetype_##superclass##_##testname<superclass##_type6> t; \
  759. } \
  760. if (superclass##_numtypes >= 7) { \
  761. ::fputs("Running " #superclass "." #testname ".7\n", stderr); \
  762. TEST_onetype_##superclass##_##testname<superclass##_type7> t; \
  763. } \
  764. if (superclass##_numtypes >= 8) { \
  765. ::fputs("Running " #superclass "." #testname ".8\n", stderr); \
  766. TEST_onetype_##superclass##_##testname<superclass##_type8> t; \
  767. } \
  768. if (superclass##_numtypes >= 9) { \
  769. ::fputs("Running " #superclass "." #testname ".9\n", stderr); \
  770. TEST_onetype_##superclass##_##testname<superclass##_type9> t; \
  771. } \
  772. } \
  773. }; \
  774. static TEST_typed_##superclass##_##testname \
  775. test_instance_typed_##superclass##_##testname; \
  776. template<class TypeParam> \
  777. void TEST_onetype_##superclass##_##testname<TypeParam>::Run()
  778. // This is a dummy class just to make converting from internal-google
  779. // to opensourcing easier.
  780. class Test { };
  781. } // namespace testing
  782. SPP_END_NAMESPACE
  783. namespace testing = SPP_NAMESPACE::testing;
  784. using std::cout;
  785. using std::pair;
  786. using std::set;
  787. using std::string;
  788. using std::vector;
  789. typedef unsigned char uint8;
  790. #ifdef _MSC_VER
  791. // Below, we purposefully test having a very small allocator size.
  792. // This causes some "type conversion too small" errors when using this
  793. // allocator with sparsetable buckets. We're testing to make sure we
  794. // handle that situation ok, so we don't need the compiler warnings.
  795. #pragma warning(disable:4244)
  796. #define ATTRIBUTE_UNUSED
  797. #else
  798. #define ATTRIBUTE_UNUSED __attribute__((unused))
  799. #endif
  800. namespace {
  801. #ifndef _MSC_VER // windows defines its own version
  802. # ifdef __MINGW32__ // mingw has trouble writing to /tmp
  803. static string TmpFile(const char* basename) {
  804. return string("./#") + basename;
  805. }
  806. # else
  807. static string TmpFile(const char* basename) {
  808. string kTmpdir = "/tmp";
  809. return kTmpdir + "/" + basename;
  810. }
  811. # endif
  812. #endif
  813. // Used as a value in some of the hashtable tests. It's just some
  814. // arbitrary user-defined type with non-trivial memory management.
  815. // ---------------------------------------------------------------
  816. struct ValueType
  817. {
  818. public:
  819. ValueType() : s_(kDefault) { }
  820. ValueType(const char* init_s) : s_(kDefault) { set_s(init_s); }
  821. ~ValueType() { set_s(NULL); }
  822. ValueType(const ValueType& that) : s_(kDefault) { operator=(that); }
  823. void operator=(const ValueType& that) { set_s(that.s_); }
  824. bool operator==(const ValueType& that) const {
  825. return strcmp(this->s(), that.s()) == 0;
  826. }
  827. void set_s(const char* new_s) {
  828. if (s_ != kDefault)
  829. free(const_cast<char*>(s_));
  830. s_ = (new_s == NULL ? kDefault : reinterpret_cast<char*>(_strdup(new_s)));
  831. }
  832. const char* s() const { return s_; }
  833. private:
  834. const char* s_;
  835. static const char* const kDefault;
  836. };
  837. const char* const ValueType::kDefault = "hi";
  838. // This is used by the low-level sparse/dense_hashtable classes,
  839. // which support the most general relationship between keys and
  840. // values: the key is derived from the value through some arbitrary
  841. // function. (For classes like sparse_hash_map, the 'value' is a
  842. // key/data pair, and the function to derive the key is
  843. // FirstElementOfPair.) KeyToValue is the inverse of this function,
  844. // so GetKey(KeyToValue(key)) == key. To keep the tests a bit
  845. // simpler, we've chosen to make the key and value actually be the
  846. // same type, which is why we need only one template argument for the
  847. // types, rather than two (one for the key and one for the value).
  848. template<class KeyAndValueT, class KeyToValue>
  849. struct SetKey
  850. {
  851. void operator()(KeyAndValueT* value, const KeyAndValueT& new_key) const
  852. {
  853. *value = KeyToValue()(new_key);
  854. }
  855. };
  856. // A hash function that keeps track of how often it's called. We use
  857. // a simple djb-hash so we don't depend on how STL hashes. We use
  858. // this same method to do the key-comparison, so we can keep track
  859. // of comparison-counts too.
  860. struct Hasher
  861. {
  862. explicit Hasher(int i=0) : id_(i), num_hashes_(0), num_compares_(0) { }
  863. int id() const { return id_; }
  864. int num_hashes() const { return num_hashes_; }
  865. int num_compares() const { return num_compares_; }
  866. size_t operator()(int a) const {
  867. num_hashes_++;
  868. return static_cast<size_t>(a);
  869. }
  870. size_t operator()(const char* a) const {
  871. num_hashes_++;
  872. size_t hash = 0;
  873. for (size_t i = 0; a[i]; i++ )
  874. hash = 33 * hash + a[i];
  875. return hash;
  876. }
  877. size_t operator()(const string& a) const {
  878. num_hashes_++;
  879. size_t hash = 0;
  880. for (size_t i = 0; i < a.length(); i++ )
  881. hash = 33 * hash + a[i];
  882. return hash;
  883. }
  884. size_t operator()(const int* a) const {
  885. num_hashes_++;
  886. return static_cast<size_t>(reinterpret_cast<uintptr_t>(a));
  887. }
  888. bool operator()(int a, int b) const {
  889. num_compares_++;
  890. return a == b;
  891. }
  892. bool operator()(const string& a, const string& b) const {
  893. num_compares_++;
  894. return a == b;
  895. }
  896. bool operator()(const char* a, const char* b) const {
  897. num_compares_++;
  898. // The 'a == b' test is necessary, in case a and b are both NULL.
  899. return (a == b || (a && b && strcmp(a, b) == 0));
  900. }
  901. private:
  902. mutable int id_;
  903. mutable int num_hashes_;
  904. mutable int num_compares_;
  905. };
  906. // Allocator that allows controlling its size in various ways, to test
  907. // allocator overflow. Because we use this allocator in a vector, we
  908. // need to define != and swap for gcc.
  909. // ------------------------------------------------------------------
  910. template<typename T,
  911. typename SizeT = size_t,
  912. SizeT MAX_SIZE = static_cast<SizeT>(~0)>
  913. struct Alloc
  914. {
  915. typedef T value_type;
  916. typedef SizeT size_type;
  917. typedef ptrdiff_t difference_type;
  918. typedef T* pointer;
  919. typedef const T* const_pointer;
  920. typedef T& reference;
  921. typedef const T& const_reference;
  922. explicit Alloc(int i=0, int* count=NULL) : id_(i), count_(count) {}
  923. ~Alloc() {}
  924. pointer address(reference r) const { return &r; }
  925. const_pointer address(const_reference r) const { return &r; }
  926. pointer allocate(size_type n, const_pointer = 0) {
  927. if (count_) ++(*count_);
  928. return static_cast<pointer>(malloc(n * sizeof(value_type)));
  929. }
  930. void deallocate(pointer p, size_type) {
  931. free(p);
  932. }
  933. pointer reallocate(pointer p, size_type n) {
  934. if (count_) ++(*count_);
  935. return static_cast<pointer>(realloc(p, n * sizeof(value_type)));
  936. }
  937. size_type max_size() const {
  938. return static_cast<size_type>(MAX_SIZE);
  939. }
  940. void construct(pointer p, const value_type& val) {
  941. new(p) value_type(val);
  942. }
  943. void destroy(pointer p) { p->~value_type(); }
  944. bool is_custom_alloc() const { return true; }
  945. template <class U>
  946. Alloc(const Alloc<U, SizeT, MAX_SIZE>& that)
  947. : id_(that.id_), count_(that.count_) {
  948. }
  949. template <class U>
  950. struct rebind {
  951. typedef Alloc<U, SizeT, MAX_SIZE> other;
  952. };
  953. bool operator==(const Alloc& that) const {
  954. return this->id_ == that.id_ && this->count_ == that.count_;
  955. }
  956. bool operator!=(const Alloc& that) const {
  957. return !this->operator==(that);
  958. }
  959. int id() const { return id_; }
  960. // I have to make these public so the constructor used for rebinding
  961. // can see them. Normally, I'd just make them private and say:
  962. // template<typename U, typename U_SizeT, U_SizeT U_MAX_SIZE> friend struct Alloc;
  963. // but MSVC 7.1 barfs on that. So public it is. But no peeking!
  964. public:
  965. int id_;
  966. int* count_;
  967. };
  968. // Below are a few fun routines that convert a value into a key, used
  969. // for dense_hashtable and sparse_hashtable. It's our responsibility
  970. // to make sure, when we insert values into these objects, that the
  971. // values match the keys we insert them under. To allow us to use
  972. // these routines for SetKey as well, we require all these functions
  973. // be their own inverse: f(f(x)) == x.
  974. template<class Value>
  975. struct Negation {
  976. typedef Value result_type;
  977. Value operator()(Value& v) { return -v; }
  978. const Value operator()(const Value& v) const { return -v; }
  979. };
  980. struct Capital
  981. {
  982. typedef string result_type;
  983. string operator()(string& s) {
  984. return string(1, s[0] ^ 32) + s.substr(1);
  985. }
  986. const string operator()(const string& s) const {
  987. return string(1, s[0] ^ 32) + s.substr(1);
  988. }
  989. };
  990. struct Identity
  991. { // lame, I know, but an important case to test.
  992. typedef const char* result_type;
  993. const char* operator()(const char* s) const {
  994. return s;
  995. }
  996. };
  997. // This is just to avoid memory leaks -- it's a global pointer to
  998. // all the memory allocated by UniqueObjectHelper. We'll use it
  999. // to semi-test sparsetable as well. :-)
  1000. std::vector<char*> g_unique_charstar_objects(16, (char *)0);
  1001. // This is an object-generator: pass in an index, and it will return a
  1002. // unique object of type ItemType. We provide specializations for the
  1003. // types we actually support.
  1004. template <typename ItemType> ItemType UniqueObjectHelper(int index);
  1005. template<> int UniqueObjectHelper(int index)
  1006. {
  1007. return index;
  1008. }
  1009. template<> string UniqueObjectHelper(int index)
  1010. {
  1011. char buffer[64];
  1012. snprintf(buffer, sizeof(buffer), "%d", index);
  1013. return buffer;
  1014. }
  1015. template<> char* UniqueObjectHelper(int index)
  1016. {
  1017. // First grow the table if need be.
  1018. size_t table_size = g_unique_charstar_objects.size();
  1019. while (index >= static_cast<int>(table_size)) {
  1020. assert(table_size * 2 > table_size); // avoid overflow problems
  1021. table_size *= 2;
  1022. }
  1023. if (table_size > g_unique_charstar_objects.size())
  1024. g_unique_charstar_objects.resize(table_size, (char *)0);
  1025. if (!g_unique_charstar_objects[static_cast<size_t>(index)]) {
  1026. char buffer[64];
  1027. snprintf(buffer, sizeof(buffer), "%d", index);
  1028. g_unique_charstar_objects[static_cast<size_t>(index)] = _strdup(buffer);
  1029. }
  1030. return g_unique_charstar_objects[static_cast<size_t>(index)];
  1031. }
  1032. template<> const char* UniqueObjectHelper(int index) {
  1033. return UniqueObjectHelper<char*>(index);
  1034. }
  1035. template<> ValueType UniqueObjectHelper(int index) {
  1036. return ValueType(UniqueObjectHelper<string>(index).c_str());
  1037. }
  1038. template<> pair<const int, int> UniqueObjectHelper(int index) {
  1039. return pair<const int,int>(index, index + 1);
  1040. }
  1041. template<> pair<const string, string> UniqueObjectHelper(int index)
  1042. {
  1043. return pair<const string,string>(
  1044. UniqueObjectHelper<string>(index), UniqueObjectHelper<string>(index + 1));
  1045. }
  1046. template<> pair<const char* const,ValueType> UniqueObjectHelper(int index)
  1047. {
  1048. return pair<const char* const,ValueType>(
  1049. UniqueObjectHelper<char*>(index), UniqueObjectHelper<ValueType>(index+1));
  1050. }
  1051. class ValueSerializer
  1052. {
  1053. public:
  1054. bool operator()(FILE* fp, const int& value) {
  1055. return fwrite(&value, sizeof(value), 1, fp) == 1;
  1056. }
  1057. bool operator()(FILE* fp, int* value) {
  1058. return fread(value, sizeof(*value), 1, fp) == 1;
  1059. }
  1060. bool operator()(FILE* fp, const string& value) {
  1061. const size_t size = value.size();
  1062. return (*this)(fp, (int)size) && fwrite(value.c_str(), size, 1, fp) == 1;
  1063. }
  1064. bool operator()(FILE* fp, string* value) {
  1065. int size;
  1066. if (!(*this)(fp, &size)) return false;
  1067. char* buf = new char[(size_t)size];
  1068. if (fread(buf, (size_t)size, 1, fp) != 1) {
  1069. delete[] buf;
  1070. return false;
  1071. }
  1072. new (value) string(buf, (size_t)size);
  1073. delete[] buf;
  1074. return true;
  1075. }
  1076. template <typename OUTPUT>
  1077. bool operator()(OUTPUT* fp, const ValueType& v) {
  1078. return (*this)(fp, string(v.s()));
  1079. }
  1080. template <typename INPUT>
  1081. bool operator()(INPUT* fp, ValueType* v) {
  1082. string data;
  1083. if (!(*this)(fp, &data)) return false;
  1084. new(v) ValueType(data.c_str());
  1085. return true;
  1086. }
  1087. template <typename OUTPUT>
  1088. bool operator()(OUTPUT* fp, const char* const& value) {
  1089. // Just store the index.
  1090. return (*this)(fp, atoi(value));
  1091. }
  1092. template <typename INPUT>
  1093. bool operator()(INPUT* fp, const char** value) {
  1094. // Look up via index.
  1095. int index;
  1096. if (!(*this)(fp, &index)) return false;
  1097. *value = UniqueObjectHelper<char*>(index);
  1098. return true;
  1099. }
  1100. template <typename OUTPUT, typename First, typename Second>
  1101. bool operator()(OUTPUT* fp, std::pair<const First, Second>* value) {
  1102. return (*this)(fp, const_cast<First*>(&value->first))
  1103. && (*this)(fp, &value->second);
  1104. }
  1105. template <typename INPUT, typename First, typename Second>
  1106. bool operator()(INPUT* fp, const std::pair<const First, Second>& value) {
  1107. return (*this)(fp, value.first) && (*this)(fp, value.second);
  1108. }
  1109. };
  1110. template <typename HashtableType>
  1111. class HashtableTest : public ::testing::Test
  1112. {
  1113. public:
  1114. HashtableTest() : ht_() { }
  1115. // Give syntactically-prettier access to UniqueObjectHelper.
  1116. typename HashtableType::value_type UniqueObject(int index) {
  1117. return UniqueObjectHelper<typename HashtableType::value_type>(index);
  1118. }
  1119. typename HashtableType::key_type UniqueKey(int index) {
  1120. return this->ht_.get_key(this->UniqueObject(index));
  1121. }
  1122. protected:
  1123. HashtableType ht_;
  1124. };
  1125. }
  1126. // These are used to specify the empty key and deleted key in some
  1127. // contexts. They can't be in the unnamed namespace, or static,
  1128. // because the template code requires external linkage.
  1129. extern const string kEmptyString("--empty string--");
  1130. extern const string kDeletedString("--deleted string--");
  1131. extern const int kEmptyInt = 0;
  1132. extern const int kDeletedInt = -1234676543; // an unlikely-to-pick int
  1133. extern const char* const kEmptyCharStar = "--empty char*--";
  1134. extern const char* const kDeletedCharStar = "--deleted char*--";
  1135. namespace {
  1136. #define INT_HASHTABLES \
  1137. HashtableInterface_SparseHashMap<int, int, Hasher, Hasher, \
  1138. Alloc<int> >, \
  1139. HashtableInterface_SparseHashSet<int, Hasher, Hasher, \
  1140. Alloc<int> >, \
  1141. /* This is a table where the key associated with a value is -value */ \
  1142. HashtableInterface_SparseHashtable<int, int, Hasher, Negation<int>, \
  1143. SetKey<int, Negation<int> >, \
  1144. Hasher, Alloc<int> >
  1145. #define STRING_HASHTABLES \
  1146. HashtableInterface_SparseHashMap<string, string, Hasher, Hasher, \
  1147. Alloc<string> >, \
  1148. HashtableInterface_SparseHashSet<string, Hasher, Hasher, \
  1149. Alloc<string> >, \
  1150. /* This is a table where the key associated with a value is Cap(value) */ \
  1151. HashtableInterface_SparseHashtable<string, string, Hasher, Capital, \
  1152. SetKey<string, Capital>, \
  1153. Hasher, Alloc<string> >
  1154. // ---------------------------------------------------------------------
  1155. // I'd like to use ValueType keys for SparseHashtable<> and
  1156. // DenseHashtable<> but I can't due to memory-management woes (nobody
  1157. // really owns the char* involved). So instead I do something simpler.
  1158. // ---------------------------------------------------------------------
  1159. #define CHARSTAR_HASHTABLES \
  1160. HashtableInterface_SparseHashMap<const char*, ValueType, \
  1161. Hasher, Hasher, Alloc<const char*> >, \
  1162. HashtableInterface_SparseHashSet<const char*, Hasher, Hasher, \
  1163. Alloc<const char*> >, \
  1164. HashtableInterface_SparseHashtable<const char*, const char*, \
  1165. Hasher, Identity, \
  1166. SetKey<const char*, Identity>, \
  1167. Hasher, Alloc<const char*> >
  1168. // ---------------------------------------------------------------------
  1169. // This is the list of types we run each test against.
  1170. // We need to define the same class 4 times due to limitations in the
  1171. // testing framework. Basically, we associate each class below with
  1172. // the set of types we want to run tests on it with.
  1173. // ---------------------------------------------------------------------
  1174. template <typename HashtableType> class HashtableIntTest
  1175. : public HashtableTest<HashtableType> { };
  1176. template <typename HashtableType> class HashtableStringTest
  1177. : public HashtableTest<HashtableType> { };
  1178. template <typename HashtableType> class HashtableCharStarTest
  1179. : public HashtableTest<HashtableType> { };
  1180. template <typename HashtableType> class HashtableAllTest
  1181. : public HashtableTest<HashtableType> { };
  1182. typedef testing::TypeList3<INT_HASHTABLES> IntHashtables;
  1183. typedef testing::TypeList3<STRING_HASHTABLES> StringHashtables;
  1184. typedef testing::TypeList3<CHARSTAR_HASHTABLES> CharStarHashtables;
  1185. typedef testing::TypeList9<INT_HASHTABLES, STRING_HASHTABLES,
  1186. CHARSTAR_HASHTABLES> AllHashtables;
  1187. TYPED_TEST_CASE_3(HashtableIntTest, IntHashtables);
  1188. TYPED_TEST_CASE_3(HashtableStringTest, StringHashtables);
  1189. TYPED_TEST_CASE_3(HashtableCharStarTest, CharStarHashtables);
  1190. TYPED_TEST_CASE_9(HashtableAllTest, AllHashtables);
  1191. // ------------------------------------------------------------------------
  1192. // First, some testing of the underlying infrastructure.
  1193. #if 0
  1194. TEST(HashtableCommonTest, HashMunging)
  1195. {
  1196. const Hasher hasher;
  1197. // We don't munge the hash value on non-pointer template types.
  1198. {
  1199. const sparsehash_internal::sh_hashtable_settings<int, Hasher, size_t, 1>
  1200. settings(hasher, 0.0, 0.0);
  1201. const int v = 1000;
  1202. EXPECT_EQ(hasher(v), settings.hash(v));
  1203. }
  1204. {
  1205. // We do munge the hash value on pointer template types.
  1206. const sparsehash_internal::sh_hashtable_settings<int*, Hasher, size_t, 1>
  1207. settings(hasher, 0.0, 0.0);
  1208. int* v = NULL;
  1209. v += 0x10000; // get a non-trivial pointer value
  1210. EXPECT_NE(hasher(v), settings.hash(v));
  1211. }
  1212. {
  1213. const sparsehash_internal::sh_hashtable_settings<const int*, Hasher,
  1214. size_t, 1>
  1215. settings(hasher, 0.0, 0.0);
  1216. const int* v = NULL;
  1217. v += 0x10000; // get a non-trivial pointer value
  1218. EXPECT_NE(hasher(v), settings.hash(v));
  1219. }
  1220. }
  1221. #endif
  1222. // ------------------------------------------------------------------------
  1223. // If the first arg to TYPED_TEST is HashtableIntTest, it will run
  1224. // this test on all the hashtable types, with key=int and value=int.
  1225. // Likewise, HashtableStringTest will have string key/values, and
  1226. // HashtableCharStarTest will have char* keys and -- just to mix it up
  1227. // a little -- ValueType values. HashtableAllTest will run all three
  1228. // key/value types on all 6 hashtables types, for 9 test-runs total
  1229. // per test.
  1230. //
  1231. // In addition, TYPED_TEST makes available the magic keyword
  1232. // TypeParam, which is the type being used for the current test.
  1233. // This first set of tests just tests the public API, going through
  1234. // the public typedefs and methods in turn. It goes approximately
  1235. // in the definition-order in sparse_hash_map.h.
  1236. // ------------------------------------------------------------------------
  1237. TYPED_TEST(HashtableIntTest, Typedefs)
  1238. {
  1239. // Make sure all the standard STL-y typedefs are defined. The exact
  1240. // key/value types don't matter here, so we only bother testing on
  1241. // the int tables. This is just a compile-time "test"; nothing here
  1242. // can fail at runtime.
  1243. this->ht_.set_deleted_key(-2); // just so deleted_key succeeds
  1244. typename TypeParam::key_type kt;
  1245. typename TypeParam::value_type vt;
  1246. typename TypeParam::hasher h;
  1247. typename TypeParam::key_equal ke;
  1248. typename TypeParam::allocator_type at;
  1249. typename TypeParam::size_type st;
  1250. typename TypeParam::difference_type dt;
  1251. typename TypeParam::pointer p;
  1252. typename TypeParam::const_pointer cp;
  1253. // I can't declare variables of reference-type, since I have nothing
  1254. // to point them to, so I just make sure that these types exist.
  1255. ATTRIBUTE_UNUSED typedef typename TypeParam::reference r;
  1256. ATTRIBUTE_UNUSED typedef typename TypeParam::const_reference cf;
  1257. typename TypeParam::iterator i;
  1258. typename TypeParam::const_iterator ci;
  1259. typename TypeParam::local_iterator li;
  1260. typename TypeParam::const_local_iterator cli;
  1261. // Now make sure the variables are used, so the compiler doesn't
  1262. // complain. Where possible, I "use" the variable by calling the
  1263. // method that's supposed to return the unique instance of the
  1264. // relevant type (eg. get_allocator()). Otherwise, I try to call a
  1265. // different, arbitrary function that returns the type. Sometimes
  1266. // the type isn't used at all, and there's no good way to use the
  1267. // variable.
  1268. kt = this->ht_.deleted_key();
  1269. (void)vt; // value_type may not be copyable. Easiest not to try.
  1270. h = this->ht_.hash_funct();
  1271. ke = this->ht_.key_eq();
  1272. at = this->ht_.get_allocator();
  1273. st = this->ht_.size();
  1274. (void)dt;
  1275. (void)p;
  1276. (void)cp;
  1277. (void)kt;
  1278. (void)st;
  1279. i = this->ht_.begin();
  1280. ci = this->ht_.begin();
  1281. li = this->ht_.begin(0);
  1282. cli = this->ht_.begin(0);
  1283. }
  1284. TYPED_TEST(HashtableAllTest, NormalIterators)
  1285. {
  1286. EXPECT_TRUE(this->ht_.begin() == this->ht_.end());
  1287. this->ht_.insert(this->UniqueObject(1));
  1288. {
  1289. typename TypeParam::iterator it = this->ht_.begin();
  1290. EXPECT_TRUE(it != this->ht_.end());
  1291. ++it;
  1292. EXPECT_TRUE(it == this->ht_.end());
  1293. }
  1294. }
  1295. #if !defined(SPP_NO_CXX11_VARIADIC_TEMPLATES)
  1296. template <class T> struct MyHash;
  1297. typedef std::pair<std::string, std::string> StringPair;
  1298. template<> struct MyHash<StringPair>
  1299. {
  1300. size_t operator()(StringPair const& p) const
  1301. {
  1302. return std::hash<string>()(p.first);
  1303. }
  1304. };
  1305. class MovableOnlyType
  1306. {
  1307. std::string _str;
  1308. std::uint64_t _int;
  1309. public:
  1310. // Make object movable and non-copyable
  1311. MovableOnlyType(MovableOnlyType &&) = default;
  1312. MovableOnlyType(const MovableOnlyType &) = delete;
  1313. MovableOnlyType& operator=(MovableOnlyType &&) = default;
  1314. MovableOnlyType& operator=(const MovableOnlyType &) = delete;
  1315. MovableOnlyType() : _str("whatever"), _int(2) {}
  1316. };
  1317. void movable_emplace_test(std::size_t iterations, int container_size)
  1318. {
  1319. for (std::size_t i=0;i<iterations;++i)
  1320. {
  1321. spp::sparse_hash_map<std::string,MovableOnlyType> m;
  1322. m.reserve(static_cast<size_t>(container_size));
  1323. char buff[20];
  1324. for (int j=0; j<container_size; ++j)
  1325. {
  1326. sprintf(buff, "%d", j);
  1327. m.emplace(buff, MovableOnlyType());
  1328. }
  1329. }
  1330. }
  1331. TEST(HashtableTest, Emplace)
  1332. {
  1333. {
  1334. sparse_hash_map<std::string, std::string> mymap;
  1335. mymap.emplace ("NCC-1701", "J.T. Kirk");
  1336. mymap.emplace ("NCC-1701-D", "J.L. Picard");
  1337. mymap.emplace ("NCC-74656", "K. Janeway");
  1338. EXPECT_TRUE(mymap["NCC-74656"] == std::string("K. Janeway"));
  1339. sparse_hash_set<StringPair, MyHash<StringPair> > myset;
  1340. myset.emplace ("NCC-1701", "J.T. Kirk");
  1341. }
  1342. movable_emplace_test(10, 50);
  1343. }
  1344. #endif
  1345. #if !defined(SPP_NO_CXX11_VARIADIC_TEMPLATES)
  1346. TEST(HashtableTest, IncompleteTypes)
  1347. {
  1348. int i;
  1349. sparse_hash_map<int *, int> ht2;
  1350. ht2[&i] = 3;
  1351. struct Bogus;
  1352. sparse_hash_map<Bogus *, int> ht3;
  1353. ht3[(Bogus *)0] = 8;
  1354. }
  1355. #endif
  1356. #if !defined(SPP_NO_CXX11_VARIADIC_TEMPLATES)
  1357. TEST(HashtableTest, ReferenceWrapper)
  1358. {
  1359. sparse_hash_map<int, std::reference_wrapper<int>> x;
  1360. int a = 5;
  1361. x.insert(std::make_pair(3, std::ref(a)));
  1362. EXPECT_EQ(x.at(3), 5);
  1363. }
  1364. #endif
  1365. TEST(HashtableTest, ModifyViaIterator)
  1366. {
  1367. // This only works for hash-maps, since only they have non-const values.
  1368. {
  1369. sparse_hash_map<int, int> ht;
  1370. ht[1] = 2;
  1371. sparse_hash_map<int, int>::iterator it = ht.find(1);
  1372. EXPECT_TRUE(it != ht.end());
  1373. EXPECT_EQ(1, it->first);
  1374. EXPECT_EQ(2, it->second);
  1375. it->second = 5;
  1376. it = ht.find(1);
  1377. EXPECT_TRUE(it != ht.end());
  1378. EXPECT_EQ(5, it->second);
  1379. }
  1380. }
  1381. TYPED_TEST(HashtableAllTest, ConstIterators)
  1382. {
  1383. this->ht_.insert(this->UniqueObject(1));
  1384. typename TypeParam::const_iterator it = this->ht_.begin();
  1385. EXPECT_TRUE(it != (typename TypeParam::const_iterator)this->ht_.end());
  1386. ++it;
  1387. EXPECT_TRUE(it == (typename TypeParam::const_iterator)this->ht_.end());
  1388. }
  1389. TYPED_TEST(HashtableAllTest, LocalIterators)
  1390. {
  1391. // Now, tr1 begin/end (the local iterator that takes a bucket-number).
  1392. // ht::bucket() returns the bucket that this key would be inserted in.
  1393. this->ht_.insert(this->UniqueObject(1));
  1394. const typename TypeParam::size_type bucknum =
  1395. this->ht_.bucket(this->UniqueKey(1));
  1396. typename TypeParam::local_iterator b = this->ht_.begin(bucknum);
  1397. typename TypeParam::local_iterator e = this->ht_.end(bucknum);
  1398. EXPECT_TRUE(b != e);
  1399. b++;
  1400. EXPECT_TRUE(b == e);
  1401. // Check an empty bucket. We can just xor the bottom bit and be sure
  1402. // of getting a legal bucket, since #buckets is always a power of 2.
  1403. EXPECT_TRUE(this->ht_.begin(bucknum ^ 1) == this->ht_.end(bucknum ^ 1));
  1404. // Another test, this time making sure we're using the right types.
  1405. typename TypeParam::local_iterator b2 = this->ht_.begin(bucknum ^ 1);
  1406. typename TypeParam::local_iterator e2 = this->ht_.end(bucknum ^ 1);
  1407. EXPECT_TRUE(b2 == e2);
  1408. }
  1409. TYPED_TEST(HashtableAllTest, ConstLocalIterators)
  1410. {
  1411. this->ht_.insert(this->UniqueObject(1));
  1412. const typename TypeParam::size_type bucknum =
  1413. this->ht_.bucket(this->UniqueKey(1));
  1414. typename TypeParam::const_local_iterator b = this->ht_.begin(bucknum);
  1415. typename TypeParam::const_local_iterator e = this->ht_.end(bucknum);
  1416. EXPECT_TRUE(b != e);
  1417. b++;
  1418. EXPECT_TRUE(b == e);
  1419. typename TypeParam::const_local_iterator b2 = this->ht_.begin(bucknum ^ 1);
  1420. typename TypeParam::const_local_iterator e2 = this->ht_.end(bucknum ^ 1);
  1421. EXPECT_TRUE(b2 == e2);
  1422. }
  1423. TYPED_TEST(HashtableAllTest, Iterating)
  1424. {
  1425. // Test a bit more iterating than just one ++.
  1426. this->ht_.insert(this->UniqueObject(1));
  1427. this->ht_.insert(this->UniqueObject(11));
  1428. this->ht_.insert(this->UniqueObject(111));
  1429. this->ht_.insert(this->UniqueObject(1111));
  1430. this->ht_.insert(this->UniqueObject(11111));
  1431. this->ht_.insert(this->UniqueObject(111111));
  1432. this->ht_.insert(this->UniqueObject(1111111));
  1433. this->ht_.insert(this->UniqueObject(11111111));
  1434. this->ht_.insert(this->UniqueObject(111111111));
  1435. typename TypeParam::iterator it = this->ht_.begin();
  1436. for (int i = 1; i <= 9; i++) { // start at 1 so i is never 0
  1437. // && here makes it easier to tell what loop iteration the test failed on.
  1438. EXPECT_TRUE(i && (it++ != this->ht_.end()));
  1439. }
  1440. EXPECT_TRUE(it == this->ht_.end());
  1441. }
  1442. TYPED_TEST(HashtableIntTest, Constructors)
  1443. {
  1444. // The key/value types don't matter here, so I just test on one set
  1445. // of tables, the ones with int keys, which can easily handle the
  1446. // placement-news we have to do below.
  1447. Hasher hasher(1); // 1 is a unique id
  1448. int alloc_count = 0;
  1449. Alloc<typename TypeParam::key_type> alloc(2, &alloc_count);
  1450. TypeParam ht_noarg;
  1451. TypeParam ht_onearg(100);
  1452. TypeParam ht_twoarg(100, hasher);
  1453. TypeParam ht_threearg(100, hasher, hasher); // hasher serves as key_equal too
  1454. TypeParam ht_fourarg(100, hasher, hasher, alloc);
  1455. // The allocator should have been called at most once, for the last ht.
  1456. EXPECT_GE(1, alloc_count);
  1457. int old_alloc_count = alloc_count;
  1458. const typename TypeParam::value_type input[] = {
  1459. this->UniqueObject(1),
  1460. this->UniqueObject(2),
  1461. this->UniqueObject(4),
  1462. this->UniqueObject(8)
  1463. };
  1464. const int num_inputs = sizeof(input) / sizeof(input[0]);
  1465. const typename TypeParam::value_type *begin = &input[0];
  1466. const typename TypeParam::value_type *end = begin + num_inputs;
  1467. TypeParam ht_iter_noarg(begin, end);
  1468. TypeParam ht_iter_onearg(begin, end, 100);
  1469. TypeParam ht_iter_twoarg(begin, end, 100, hasher);
  1470. TypeParam ht_iter_threearg(begin, end, 100, hasher, hasher);
  1471. TypeParam ht_iter_fourarg(begin, end, 100, hasher, hasher, alloc);
  1472. // Now the allocator should have been called more.
  1473. EXPECT_GT(alloc_count, old_alloc_count);
  1474. old_alloc_count = alloc_count;
  1475. // Let's do a lot more inserting and make sure the alloc-count goes up
  1476. for (int i = 2; i < 2000; i++)
  1477. ht_fourarg.insert(this->UniqueObject(i));
  1478. EXPECT_GT(alloc_count, old_alloc_count);
  1479. EXPECT_LT(ht_noarg.bucket_count(), 100u);
  1480. EXPECT_GE(ht_onearg.bucket_count(), 100u);
  1481. EXPECT_GE(ht_twoarg.bucket_count(), 100u);
  1482. EXPECT_GE(ht_threearg.bucket_count(), 100u);
  1483. EXPECT_GE(ht_fourarg.bucket_count(), 100u);
  1484. EXPECT_GE(ht_iter_onearg.bucket_count(), 100u);
  1485. // When we pass in a hasher -- it can serve both as the hash-function
  1486. // and the key-equal function -- its id should be 1. Where we don't
  1487. // pass it in and use the default Hasher object, the id should be 0.
  1488. EXPECT_EQ(0, ht_noarg.hash_funct().id());
  1489. EXPECT_EQ(0, ht_noarg.key_eq().id());
  1490. EXPECT_EQ(0, ht_onearg.hash_funct().id());
  1491. EXPECT_EQ(0, ht_onearg.key_eq().id());
  1492. EXPECT_EQ(1, ht_twoarg.hash_funct().id());
  1493. EXPECT_EQ(0, ht_twoarg.key_eq().id());
  1494. EXPECT_EQ(1, ht_threearg.hash_funct().id());
  1495. EXPECT_EQ(1, ht_threearg.key_eq().id());
  1496. EXPECT_EQ(0, ht_iter_noarg.hash_funct().id());
  1497. EXPECT_EQ(0, ht_iter_noarg.key_eq().id());
  1498. EXPECT_EQ(0, ht_iter_onearg.hash_funct().id());
  1499. EXPECT_EQ(0, ht_iter_onearg.key_eq().id());
  1500. EXPECT_EQ(1, ht_iter_twoarg.hash_funct().id());
  1501. EXPECT_EQ(0, ht_iter_twoarg.key_eq().id());
  1502. EXPECT_EQ(1, ht_iter_threearg.hash_funct().id());
  1503. EXPECT_EQ(1, ht_iter_threearg.key_eq().id());
  1504. // Likewise for the allocator
  1505. EXPECT_EQ(0, ht_threearg.get_allocator().id());
  1506. EXPECT_EQ(0, ht_iter_threearg.get_allocator().id());
  1507. EXPECT_EQ(2, ht_fourarg.get_allocator().id());
  1508. EXPECT_EQ(2, ht_iter_fourarg.get_allocator().id());
  1509. }
  1510. TYPED_TEST(HashtableAllTest, OperatorEquals)
  1511. {
  1512. {
  1513. TypeParam ht1, ht2;
  1514. ht1.set_deleted_key(this->UniqueKey(1));
  1515. ht2.set_deleted_key(this->UniqueKey(2));
  1516. ht1.insert(this->UniqueObject(10));
  1517. ht2.insert(this->UniqueObject(20));
  1518. EXPECT_FALSE(ht1 == ht2);
  1519. ht1 = ht2;
  1520. EXPECT_TRUE(ht1 == ht2);
  1521. }
  1522. {
  1523. TypeParam ht1, ht2;
  1524. ht1.insert(this->UniqueObject(30));
  1525. ht1 = ht2;
  1526. EXPECT_EQ(0u, ht1.size());
  1527. }
  1528. {
  1529. TypeParam ht1, ht2;
  1530. ht1.set_deleted_key(this->UniqueKey(1));
  1531. ht2.insert(this->UniqueObject(1)); // has same key as ht1.delkey
  1532. ht1 = ht2; // should reset deleted-key to 'unset'
  1533. EXPECT_EQ(1u, ht1.size());
  1534. EXPECT_EQ(1u, ht1.count(this->UniqueKey(1)));
  1535. }
  1536. }
  1537. TYPED_TEST(HashtableAllTest, Clear)
  1538. {
  1539. for (int i = 1; i < 200; i++) {
  1540. this->ht_.insert(this->UniqueObject(i));
  1541. }
  1542. this->ht_.clear();
  1543. EXPECT_EQ(0u, this->ht_.size());
  1544. // TODO(csilvers): do we want to enforce that the hashtable has or
  1545. // has not shrunk? It does for dense_* but not sparse_*.
  1546. }
  1547. TYPED_TEST(HashtableAllTest, ClearNoResize)
  1548. {
  1549. if (!this->ht_.supports_clear_no_resize())
  1550. return;
  1551. typename TypeParam::size_type empty_bucket_count = this->ht_.bucket_count();
  1552. int last_element = 1;
  1553. while (this->ht_.bucket_count() == empty_bucket_count) {
  1554. this->ht_.insert(this->UniqueObject(last_element));
  1555. ++last_element;
  1556. }
  1557. typename TypeParam::size_type last_bucket_count = this->ht_.bucket_count();
  1558. this->ht_.clear_no_resize();
  1559. EXPECT_EQ(last_bucket_count, this->ht_.bucket_count());
  1560. EXPECT_TRUE(this->ht_.empty());
  1561. // When inserting the same number of elements again, no resize
  1562. // should be necessary.
  1563. for (int i = 1; i < last_element; ++i) {
  1564. this->ht_.insert(this->UniqueObject(last_element + i));
  1565. EXPECT_EQ(last_bucket_count, this->ht_.bucket_count());
  1566. }
  1567. }
  1568. TYPED_TEST(HashtableAllTest, Swap)
  1569. {
  1570. // Let's make a second hashtable with its own hasher, key_equal, etc.
  1571. Hasher hasher(1); // 1 is a unique id
  1572. TypeParam other_ht(200, hasher, hasher);
  1573. this->ht_.set_deleted_key(this->UniqueKey(1));
  1574. other_ht.set_deleted_key(this->UniqueKey(2));
  1575. for (int i = 3; i < 2000; i++) {
  1576. this->ht_.insert(this->UniqueObject(i));
  1577. }
  1578. this->ht_.erase(this->UniqueKey(1000));
  1579. other_ht.insert(this->UniqueObject(2001));
  1580. typename TypeParam::size_type expected_buckets = other_ht.bucket_count();
  1581. this->ht_.swap(other_ht);
  1582. EXPECT_EQ(this->UniqueKey(2), this->ht_.deleted_key());
  1583. EXPECT_EQ(this->UniqueKey(1), other_ht.deleted_key());
  1584. EXPECT_EQ(1, this->ht_.hash_funct().id());
  1585. EXPECT_EQ(0, other_ht.hash_funct().id());
  1586. EXPECT_EQ(1, this->ht_.key_eq().id());
  1587. EXPECT_EQ(0, other_ht.key_eq().id());
  1588. EXPECT_EQ(expected_buckets, this->ht_.bucket_count());
  1589. EXPECT_GT(other_ht.bucket_count(), 200u);
  1590. EXPECT_EQ(1u, this->ht_.size());
  1591. EXPECT_EQ(1996u, other_ht.size()); // because we erased 1000
  1592. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(111)));
  1593. EXPECT_EQ(1u, other_ht.count(this->UniqueKey(111)));
  1594. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(2001)));
  1595. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(2001)));
  1596. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(1000)));
  1597. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(1000)));
  1598. // We purposefully don't swap allocs -- they're not necessarily swappable.
  1599. // Now swap back, using the free-function swap
  1600. // NOTE: MSVC seems to have trouble with this free swap, not quite
  1601. // sure why. I've given up trying to fix it though.
  1602. #ifdef _MSC_VER
  1603. other_ht.swap(this->ht_);
  1604. #else
  1605. std::swap(this->ht_, other_ht);
  1606. #endif
  1607. EXPECT_EQ(this->UniqueKey(1), this->ht_.deleted_key());
  1608. EXPECT_EQ(this->UniqueKey(2), other_ht.deleted_key());
  1609. EXPECT_EQ(0, this->ht_.hash_funct().id());
  1610. EXPECT_EQ(1, other_ht.hash_funct().id());
  1611. EXPECT_EQ(1996u, this->ht_.size());
  1612. EXPECT_EQ(1u, other_ht.size());
  1613. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(111)));
  1614. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(111)));
  1615. // A user reported a crash with this code using swap to clear.
  1616. // We've since fixed the bug; this prevents a regression.
  1617. TypeParam swap_to_clear_ht;
  1618. swap_to_clear_ht.set_deleted_key(this->UniqueKey(1));
  1619. for (int i = 2; i < 10000; ++i) {
  1620. swap_to_clear_ht.insert(this->UniqueObject(i));
  1621. }
  1622. TypeParam empty_ht;
  1623. empty_ht.swap(swap_to_clear_ht);
  1624. swap_to_clear_ht.set_deleted_key(this->UniqueKey(1));
  1625. for (int i = 2; i < 10000; ++i) {
  1626. swap_to_clear_ht.insert(this->UniqueObject(i));
  1627. }
  1628. }
  1629. TYPED_TEST(HashtableAllTest, Size)
  1630. {
  1631. EXPECT_EQ(0u, this->ht_.size());
  1632. for (int i = 1; i < 1000; i++) { // go through some resizes
  1633. this->ht_.insert(this->UniqueObject(i));
  1634. EXPECT_EQ(static_cast<typename TypeParam::size_type>(i), this->ht_.size());
  1635. }
  1636. this->ht_.clear();
  1637. EXPECT_EQ(0u, this->ht_.size());
  1638. this->ht_.set_deleted_key(this->UniqueKey(1));
  1639. EXPECT_EQ(0u, this->ht_.size()); // deleted key doesn't count
  1640. for (int i = 2; i < 1000; i++) { // go through some resizes
  1641. this->ht_.insert(this->UniqueObject(i));
  1642. this->ht_.erase(this->UniqueKey(i));
  1643. EXPECT_EQ(0u, this->ht_.size());
  1644. }
  1645. }
  1646. TEST(HashtableTest, MaxSizeAndMaxBucketCount)
  1647. {
  1648. // The max size depends on the allocator. So we can't use the
  1649. // built-in allocator type; instead, we make our own types.
  1650. sparse_hash_set<int, Hasher, Hasher, Alloc<int> > ht_default;
  1651. sparse_hash_set<int, Hasher, Hasher, Alloc<int, unsigned char> > ht_char;
  1652. sparse_hash_set<int, Hasher, Hasher, Alloc<int, unsigned char, 104> > ht_104;
  1653. EXPECT_GE(ht_default.max_size(), 256u);
  1654. EXPECT_EQ(255u, ht_char.max_size());
  1655. EXPECT_EQ(104u, ht_104.max_size());
  1656. // In our implementations, MaxBucketCount == MaxSize.
  1657. EXPECT_EQ(ht_default.max_size(), ht_default.max_bucket_count());
  1658. EXPECT_EQ(ht_char.max_size(), ht_char.max_bucket_count());
  1659. EXPECT_EQ(ht_104.max_size(), ht_104.max_bucket_count());
  1660. }
  1661. TYPED_TEST(HashtableAllTest, Empty)
  1662. {
  1663. EXPECT_TRUE(this->ht_.empty());
  1664. this->ht_.insert(this->UniqueObject(1));
  1665. EXPECT_FALSE(this->ht_.empty());
  1666. this->ht_.clear();
  1667. EXPECT_TRUE(this->ht_.empty());
  1668. TypeParam empty_ht;
  1669. this->ht_.insert(this->UniqueObject(1));
  1670. this->ht_.swap(empty_ht);
  1671. EXPECT_TRUE(this->ht_.empty());
  1672. }
  1673. TYPED_TEST(HashtableAllTest, BucketCount)
  1674. {
  1675. TypeParam ht(100);
  1676. // constructor arg is number of *items* to be inserted, not the
  1677. // number of buckets, so we expect more buckets.
  1678. EXPECT_GT(ht.bucket_count(), 100u);
  1679. for (int i = 1; i < 200; i++) {
  1680. ht.insert(this->UniqueObject(i));
  1681. }
  1682. EXPECT_GT(ht.bucket_count(), 200u);
  1683. }
  1684. TYPED_TEST(HashtableAllTest, BucketAndBucketSize)
  1685. {
  1686. const typename TypeParam::size_type expected_bucknum = this->ht_.bucket(
  1687. this->UniqueKey(1));
  1688. EXPECT_EQ(0u, this->ht_.bucket_size(expected_bucknum));
  1689. this->ht_.insert(this->UniqueObject(1));
  1690. EXPECT_EQ(expected_bucknum, this->ht_.bucket(this->UniqueKey(1)));
  1691. EXPECT_EQ(1u, this->ht_.bucket_size(expected_bucknum));
  1692. // Check that a bucket we didn't insert into, has a 0 size. Since
  1693. // we have an even number of buckets, bucknum^1 is guaranteed in range.
  1694. EXPECT_EQ(0u, this->ht_.bucket_size(expected_bucknum ^ 1));
  1695. }
  1696. TYPED_TEST(HashtableAllTest, LoadFactor)
  1697. {
  1698. const typename TypeParam::size_type kSize = 16536;
  1699. // Check growing past various thresholds and then shrinking below
  1700. // them.
  1701. for (float grow_threshold = 0.2f;
  1702. grow_threshold <= 0.8f;
  1703. grow_threshold += 0.2f)
  1704. {
  1705. TypeParam ht;
  1706. ht.set_deleted_key(this->UniqueKey(1));
  1707. ht.max_load_factor(grow_threshold);
  1708. ht.min_load_factor(0.0);
  1709. EXPECT_EQ(grow_threshold, ht.max_load_factor());
  1710. EXPECT_EQ(0.0, ht.min_load_factor());
  1711. ht.resize(kSize);
  1712. size_t bucket_count = ht.bucket_count();
  1713. // Erase and insert an element to set consider_shrink = true,
  1714. // which should not cause a shrink because the threshold is 0.0.
  1715. ht.insert(this->UniqueObject(2));
  1716. ht.erase(this->UniqueKey(2));
  1717. for (int i = 2;; ++i)
  1718. {
  1719. ht.insert(this->UniqueObject(i));
  1720. if (static_cast<float>(ht.size())/bucket_count < grow_threshold) {
  1721. EXPECT_EQ(bucket_count, ht.bucket_count());
  1722. } else {
  1723. EXPECT_GT(ht.bucket_count(), bucket_count);
  1724. break;
  1725. }
  1726. }
  1727. // Now set a shrink threshold 1% below the current size and remove
  1728. // items until the size falls below that.
  1729. const float shrink_threshold = static_cast<float>(ht.size()) /
  1730. ht.bucket_count() - 0.01f;
  1731. // This time around, check the old set_resizing_parameters interface.
  1732. ht.set_resizing_parameters(shrink_threshold, 1.0);
  1733. EXPECT_EQ(1.0, ht.max_load_factor());
  1734. EXPECT_EQ(shrink_threshold, ht.min_load_factor());
  1735. bucket_count = ht.bucket_count();
  1736. for (int i = 2;; ++i)
  1737. {
  1738. ht.erase(this->UniqueKey(i));
  1739. // A resize is only triggered by an insert, so add and remove a
  1740. // value every iteration to trigger the shrink as soon as the
  1741. // threshold is passed.
  1742. ht.erase(this->UniqueKey(i+1));
  1743. ht.insert(this->UniqueObject(i+1));
  1744. if (static_cast<float>(ht.size())/bucket_count > shrink_threshold) {
  1745. EXPECT_EQ(bucket_count, ht.bucket_count());
  1746. } else {
  1747. EXPECT_LT(ht.bucket_count(), bucket_count);
  1748. break;
  1749. }
  1750. }
  1751. }
  1752. }
  1753. TYPED_TEST(HashtableAllTest, ResizeAndRehash)
  1754. {
  1755. // resize() and rehash() are synonyms. rehash() is the tr1 name.
  1756. TypeParam ht(10000);
  1757. ht.max_load_factor(0.8f); // for consistency's sake
  1758. for (int i = 1; i < 100; ++i)
  1759. ht.insert(this->UniqueObject(i));
  1760. ht.resize(0);
  1761. // Now ht should be as small as possible.
  1762. EXPECT_LT(ht.bucket_count(), 300u);
  1763. ht.rehash(9000); // use the 'rehash' version of the name.
  1764. // Bucket count should be next power of 2, after considering max_load_factor.
  1765. EXPECT_EQ(16384u, ht.bucket_count());
  1766. for (int i = 101; i < 200; ++i)
  1767. ht.insert(this->UniqueObject(i));
  1768. // Adding a few hundred buckets shouldn't have caused a resize yet.
  1769. EXPECT_EQ(ht.bucket_count(), 16384u);
  1770. }
  1771. TYPED_TEST(HashtableAllTest, FindAndCountAndEqualRange)
  1772. {
  1773. pair<typename TypeParam::iterator, typename TypeParam::iterator> eq_pair;
  1774. pair<typename TypeParam::const_iterator,
  1775. typename TypeParam::const_iterator> const_eq_pair;
  1776. EXPECT_TRUE(this->ht_.empty());
  1777. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) == this->ht_.end());
  1778. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(1)));
  1779. eq_pair = this->ht_.equal_range(this->UniqueKey(1));
  1780. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1781. this->ht_.insert(this->UniqueObject(1));
  1782. EXPECT_FALSE(this->ht_.empty());
  1783. this->ht_.insert(this->UniqueObject(11));
  1784. this->ht_.insert(this->UniqueObject(111));
  1785. this->ht_.insert(this->UniqueObject(1111));
  1786. this->ht_.insert(this->UniqueObject(11111));
  1787. this->ht_.insert(this->UniqueObject(111111));
  1788. this->ht_.insert(this->UniqueObject(1111111));
  1789. this->ht_.insert(this->UniqueObject(11111111));
  1790. this->ht_.insert(this->UniqueObject(111111111));
  1791. EXPECT_EQ(9u, this->ht_.size());
  1792. typename TypeParam::const_iterator it = this->ht_.find(this->UniqueKey(1));
  1793. EXPECT_EQ(it.key(), this->UniqueKey(1));
  1794. // Allow testing the const version of the methods as well.
  1795. const TypeParam ht = this->ht_;
  1796. // Some successful lookups (via find, count, and equal_range).
  1797. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) != this->ht_.end());
  1798. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1)));
  1799. eq_pair = this->ht_.equal_range(this->UniqueKey(1));
  1800. EXPECT_TRUE(eq_pair.first != eq_pair.second);
  1801. EXPECT_EQ(eq_pair.first.key(), this->UniqueKey(1));
  1802. ++eq_pair.first;
  1803. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1804. EXPECT_TRUE(ht.find(this->UniqueKey(1)) != ht.end());
  1805. EXPECT_EQ(1u, ht.count(this->UniqueKey(1)));
  1806. const_eq_pair = ht.equal_range(this->UniqueKey(1));
  1807. EXPECT_TRUE(const_eq_pair.first != const_eq_pair.second);
  1808. EXPECT_EQ(const_eq_pair.first.key(), this->UniqueKey(1));
  1809. ++const_eq_pair.first;
  1810. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1811. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11111)) != this->ht_.end());
  1812. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(11111)));
  1813. eq_pair = this->ht_.equal_range(this->UniqueKey(11111));
  1814. EXPECT_TRUE(eq_pair.first != eq_pair.second);
  1815. EXPECT_EQ(eq_pair.first.key(), this->UniqueKey(11111));
  1816. ++eq_pair.first;
  1817. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1818. EXPECT_TRUE(ht.find(this->UniqueKey(11111)) != ht.end());
  1819. EXPECT_EQ(1u, ht.count(this->UniqueKey(11111)));
  1820. const_eq_pair = ht.equal_range(this->UniqueKey(11111));
  1821. EXPECT_TRUE(const_eq_pair.first != const_eq_pair.second);
  1822. EXPECT_EQ(const_eq_pair.first.key(), this->UniqueKey(11111));
  1823. ++const_eq_pair.first;
  1824. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1825. // Some unsuccessful lookups (via find, count, and equal_range).
  1826. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11112)) == this->ht_.end());
  1827. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(11112)));
  1828. eq_pair = this->ht_.equal_range(this->UniqueKey(11112));
  1829. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1830. EXPECT_TRUE(ht.find(this->UniqueKey(11112)) == ht.end());
  1831. EXPECT_EQ(0u, ht.count(this->UniqueKey(11112)));
  1832. const_eq_pair = ht.equal_range(this->UniqueKey(11112));
  1833. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1834. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11110)) == this->ht_.end());
  1835. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(11110)));
  1836. eq_pair = this->ht_.equal_range(this->UniqueKey(11110));
  1837. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1838. EXPECT_TRUE(ht.find(this->UniqueKey(11110)) == ht.end());
  1839. EXPECT_EQ(0u, ht.count(this->UniqueKey(11110)));
  1840. const_eq_pair = ht.equal_range(this->UniqueKey(11110));
  1841. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1842. }
  1843. TYPED_TEST(HashtableAllTest, BracketInsert)
  1844. {
  1845. // tests operator[], for those types that support it.
  1846. if (!this->ht_.supports_brackets())
  1847. return;
  1848. // bracket_equal is equivalent to ht_[a] == b. It should insert a if
  1849. // it doesn't already exist.
  1850. EXPECT_TRUE(this->ht_.bracket_equal(this->UniqueKey(1),
  1851. this->ht_.default_data()));
  1852. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) != this->ht_.end());
  1853. // bracket_assign is equivalent to ht_[a] = b.
  1854. this->ht_.bracket_assign(this->UniqueKey(2),
  1855. this->ht_.get_data(this->UniqueObject(4)));
  1856. EXPECT_TRUE(this->ht_.find(this->UniqueKey(2)) != this->ht_.end());
  1857. EXPECT_TRUE(this->ht_.bracket_equal(
  1858. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(4))));
  1859. this->ht_.bracket_assign(
  1860. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6)));
  1861. EXPECT_TRUE(this->ht_.bracket_equal(
  1862. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6))));
  1863. // bracket_equal shouldn't have modified the value.
  1864. EXPECT_TRUE(this->ht_.bracket_equal(
  1865. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6))));
  1866. // Verify that an operator[] that doesn't cause a resize, also
  1867. // doesn't require an extra rehash.
  1868. TypeParam ht(100);
  1869. EXPECT_EQ(0, ht.hash_funct().num_hashes());
  1870. ht.bracket_assign(this->UniqueKey(2), ht.get_data(this->UniqueObject(2)));
  1871. EXPECT_EQ(1, ht.hash_funct().num_hashes());
  1872. // And overwriting, likewise, should only cause one extra hash.
  1873. ht.bracket_assign(this->UniqueKey(2), ht.get_data(this->UniqueObject(2)));
  1874. EXPECT_EQ(2, ht.hash_funct().num_hashes());
  1875. }
  1876. TYPED_TEST(HashtableAllTest, InsertValue)
  1877. {
  1878. // First, try some straightforward insertions.
  1879. EXPECT_TRUE(this->ht_.empty());
  1880. this->ht_.insert(this->UniqueObject(1));
  1881. EXPECT_FALSE(this->ht_.empty());
  1882. this->ht_.insert(this->UniqueObject(11));
  1883. this->ht_.insert(this->UniqueObject(111));
  1884. this->ht_.insert(this->UniqueObject(1111));
  1885. this->ht_.insert(this->UniqueObject(11111));
  1886. this->ht_.insert(this->UniqueObject(111111));
  1887. this->ht_.insert(this->UniqueObject(1111111));
  1888. this->ht_.insert(this->UniqueObject(11111111));
  1889. this->ht_.insert(this->UniqueObject(111111111));
  1890. EXPECT_EQ(9u, this->ht_.size());
  1891. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1)));
  1892. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1111)));
  1893. // Check the return type.
  1894. pair<typename TypeParam::iterator, bool> insert_it;
  1895. insert_it = this->ht_.insert(this->UniqueObject(1));
  1896. EXPECT_EQ(false, insert_it.second); // false: already present
  1897. EXPECT_TRUE(*insert_it.first == this->UniqueObject(1));
  1898. insert_it = this->ht_.insert(this->UniqueObject(2));
  1899. EXPECT_EQ(true, insert_it.second); // true: not already present
  1900. EXPECT_TRUE(*insert_it.first == this->UniqueObject(2));
  1901. }
  1902. TYPED_TEST(HashtableIntTest, InsertRange)
  1903. {
  1904. // We just test the ints here, to make the placement-new easier.
  1905. TypeParam ht_source;
  1906. ht_source.insert(this->UniqueObject(10));
  1907. ht_source.insert(this->UniqueObject(100));
  1908. ht_source.insert(this->UniqueObject(1000));
  1909. ht_source.insert(this->UniqueObject(10000));
  1910. ht_source.insert(this->UniqueObject(100000));
  1911. ht_source.insert(this->UniqueObject(1000000));
  1912. const typename TypeParam::value_type input[] = {
  1913. // This is a copy of the first element in ht_source.
  1914. *ht_source.begin(),
  1915. this->UniqueObject(2),
  1916. this->UniqueObject(4),
  1917. this->UniqueObject(8)
  1918. };
  1919. set<typename TypeParam::value_type> set_input;
  1920. set_input.insert(this->UniqueObject(1111111));
  1921. set_input.insert(this->UniqueObject(111111));
  1922. set_input.insert(this->UniqueObject(11111));
  1923. set_input.insert(this->UniqueObject(1111));
  1924. set_input.insert(this->UniqueObject(111));
  1925. set_input.insert(this->UniqueObject(11));
  1926. // Insert from ht_source, an iterator of the same type as us.
  1927. typename TypeParam::const_iterator begin = ht_source.begin();
  1928. typename TypeParam::const_iterator end = begin;
  1929. std::advance(end, 3);
  1930. this->ht_.insert(begin, end); // insert 3 elements from ht_source
  1931. EXPECT_EQ(3u, this->ht_.size());
  1932. EXPECT_TRUE(*this->ht_.begin() == this->UniqueObject(10) ||
  1933. *this->ht_.begin() == this->UniqueObject(100) ||
  1934. *this->ht_.begin() == this->UniqueObject(1000) ||
  1935. *this->ht_.begin() == this->UniqueObject(10000) ||
  1936. *this->ht_.begin() == this->UniqueObject(100000) ||
  1937. *this->ht_.begin() == this->UniqueObject(1000000));
  1938. // And insert from set_input, a separate, non-random-access iterator.
  1939. typename set<typename TypeParam::value_type>::const_iterator set_begin;
  1940. typename set<typename TypeParam::value_type>::const_iterator set_end;
  1941. set_begin = set_input.begin();
  1942. set_end = set_begin;
  1943. std::advance(set_end, 3);
  1944. this->ht_.insert(set_begin, set_end);
  1945. EXPECT_EQ(6u, this->ht_.size());
  1946. // Insert from input as well, a separate, random-access iterator.
  1947. // The first element of input overlaps with an existing element
  1948. // of ht_, so this should only up the size by 2.
  1949. this->ht_.insert(&input[0], &input[3]);
  1950. EXPECT_EQ(8u, this->ht_.size());
  1951. }
  1952. TEST(HashtableTest, InsertValueToMap)
  1953. {
  1954. // For the maps in particular, ensure that inserting doesn't change
  1955. // the value.
  1956. sparse_hash_map<int, int> shm;
  1957. pair<sparse_hash_map<int,int>::iterator, bool> shm_it;
  1958. shm[1] = 2; // test a different method of inserting
  1959. shm_it = shm.insert(pair<int, int>(1, 3));
  1960. EXPECT_EQ(false, shm_it.second);
  1961. EXPECT_EQ(1, shm_it.first->first);
  1962. EXPECT_EQ(2, shm_it.first->second);
  1963. shm_it.first->second = 20;
  1964. EXPECT_EQ(20, shm[1]);
  1965. shm_it = shm.insert(pair<int, int>(2, 4));
  1966. EXPECT_EQ(true, shm_it.second);
  1967. EXPECT_EQ(2, shm_it.first->first);
  1968. EXPECT_EQ(4, shm_it.first->second);
  1969. EXPECT_EQ(4, shm[2]);
  1970. }
  1971. TYPED_TEST(HashtableStringTest, EmptyKey)
  1972. {
  1973. // Only run the string tests, to make it easier to know what the
  1974. // empty key should be.
  1975. if (!this->ht_.supports_empty_key())
  1976. return;
  1977. EXPECT_EQ(kEmptyString, this->ht_.empty_key());
  1978. }
  1979. TYPED_TEST(HashtableAllTest, DeletedKey)
  1980. {
  1981. if (!this->ht_.supports_deleted_key())
  1982. return;
  1983. this->ht_.insert(this->UniqueObject(10));
  1984. this->ht_.insert(this->UniqueObject(20));
  1985. this->ht_.set_deleted_key(this->UniqueKey(1));
  1986. EXPECT_EQ(this->ht_.deleted_key(), this->UniqueKey(1));
  1987. EXPECT_EQ(2u, this->ht_.size());
  1988. this->ht_.erase(this->UniqueKey(20));
  1989. EXPECT_EQ(1u, this->ht_.size());
  1990. // Changing the deleted key is fine.
  1991. this->ht_.set_deleted_key(this->UniqueKey(2));
  1992. EXPECT_EQ(this->ht_.deleted_key(), this->UniqueKey(2));
  1993. EXPECT_EQ(1u, this->ht_.size());
  1994. }
  1995. TYPED_TEST(HashtableAllTest, Erase)
  1996. {
  1997. this->ht_.set_deleted_key(this->UniqueKey(1));
  1998. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(20)));
  1999. this->ht_.insert(this->UniqueObject(10));
  2000. this->ht_.insert(this->UniqueObject(20));
  2001. EXPECT_EQ(1u, this->ht_.erase(this->UniqueKey(20)));
  2002. EXPECT_EQ(1u, this->ht_.size());
  2003. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(20)));
  2004. EXPECT_EQ(1u, this->ht_.size());
  2005. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(19)));
  2006. EXPECT_EQ(1u, this->ht_.size());
  2007. typename TypeParam::iterator it = this->ht_.find(this->UniqueKey(10));
  2008. EXPECT_TRUE(it != this->ht_.end());
  2009. this->ht_.erase(it);
  2010. EXPECT_EQ(0u, this->ht_.size());
  2011. for (int i = 10; i < 100; i++)
  2012. this->ht_.insert(this->UniqueObject(i));
  2013. EXPECT_EQ(90u, this->ht_.size());
  2014. this->ht_.erase(this->ht_.begin(), this->ht_.end());
  2015. EXPECT_EQ(0u, this->ht_.size());
  2016. }
  2017. TYPED_TEST(HashtableAllTest, EraseDoesNotResize)
  2018. {
  2019. this->ht_.set_deleted_key(this->UniqueKey(1));
  2020. for (int i = 10; i < 2000; i++) {
  2021. this->ht_.insert(this->UniqueObject(i));
  2022. }
  2023. const typename TypeParam::size_type old_count = this->ht_.bucket_count();
  2024. for (int i = 10; i < 1000; i++) { // erase half one at a time
  2025. EXPECT_EQ(1u, this->ht_.erase(this->UniqueKey(i)));
  2026. }
  2027. this->ht_.erase(this->ht_.begin(), this->ht_.end()); // and the rest at once
  2028. EXPECT_EQ(0u, this->ht_.size());
  2029. EXPECT_EQ(old_count, this->ht_.bucket_count());
  2030. }
  2031. TYPED_TEST(HashtableAllTest, Equals)
  2032. {
  2033. // The real test here is whether two hashtables are equal if they
  2034. // have the same items but in a different order.
  2035. TypeParam ht1;
  2036. TypeParam ht2;
  2037. EXPECT_TRUE(ht1 == ht1);
  2038. EXPECT_FALSE(ht1 != ht1);
  2039. EXPECT_TRUE(ht1 == ht2);
  2040. EXPECT_FALSE(ht1 != ht2);
  2041. ht1.set_deleted_key(this->UniqueKey(1));
  2042. // Only the contents affect equality, not things like deleted-key.
  2043. EXPECT_TRUE(ht1 == ht2);
  2044. EXPECT_FALSE(ht1 != ht2);
  2045. ht1.resize(2000);
  2046. EXPECT_TRUE(ht1 == ht2);
  2047. // The choice of allocator/etc doesn't matter either.
  2048. Hasher hasher(1);
  2049. Alloc<typename TypeParam::key_type> alloc(2, NULL);
  2050. TypeParam ht3(5, hasher, hasher, alloc);
  2051. EXPECT_TRUE(ht1 == ht3);
  2052. EXPECT_FALSE(ht1 != ht3);
  2053. ht1.insert(this->UniqueObject(2));
  2054. EXPECT_TRUE(ht1 != ht2);
  2055. EXPECT_FALSE(ht1 == ht2); // this should hold as well!
  2056. ht2.insert(this->UniqueObject(2));
  2057. EXPECT_TRUE(ht1 == ht2);
  2058. for (int i = 3; i <= 2000; i++) {
  2059. ht1.insert(this->UniqueObject(i));
  2060. }
  2061. for (int i = 2000; i >= 3; i--) {
  2062. ht2.insert(this->UniqueObject(i));
  2063. }
  2064. EXPECT_TRUE(ht1 == ht2);
  2065. }
  2066. TEST(HashtableTest, IntIO)
  2067. {
  2068. // Since the set case is just a special (easier) case than the map case, I
  2069. // just test on sparse_hash_map. This handles the easy case where we can
  2070. // use the standard reader and writer.
  2071. sparse_hash_map<int, int> ht_out;
  2072. ht_out.set_deleted_key(0);
  2073. for (int i = 1; i < 1000; i++) {
  2074. ht_out[i] = i * i;
  2075. }
  2076. ht_out.erase(563); // just to test having some erased keys when we write.
  2077. ht_out.erase(22);
  2078. string file(TmpFile("intio"));
  2079. FILE* fp = fopen(file.c_str(), "wb");
  2080. if (fp)
  2081. {
  2082. EXPECT_TRUE(fp != NULL);
  2083. EXPECT_TRUE(ht_out.write_metadata(fp));
  2084. EXPECT_TRUE(ht_out.write_nopointer_data(fp));
  2085. fclose(fp);
  2086. }
  2087. sparse_hash_map<int, int> ht_in;
  2088. fp = fopen(file.c_str(), "rb");
  2089. if (fp)
  2090. {
  2091. EXPECT_TRUE(fp != NULL);
  2092. EXPECT_TRUE(ht_in.read_metadata(fp));
  2093. EXPECT_TRUE(ht_in.read_nopointer_data(fp));
  2094. fclose(fp);
  2095. }
  2096. EXPECT_EQ(1, ht_in[1]);
  2097. EXPECT_EQ(998001, ht_in[999]);
  2098. EXPECT_EQ(100, ht_in[10]);
  2099. EXPECT_EQ(441, ht_in[21]);
  2100. EXPECT_EQ(0, ht_in[22]); // should not have been saved
  2101. EXPECT_EQ(0, ht_in[563]);
  2102. }
  2103. TEST(HashtableTest, StringIO)
  2104. {
  2105. // Since the set case is just a special (easier) case than the map case,
  2106. // I just test on sparse_hash_map. This handles the difficult case where
  2107. // we have to write our own custom reader/writer for the data.
  2108. typedef sparse_hash_map<string, string, Hasher, Hasher> SP;
  2109. SP ht_out;
  2110. ht_out.set_deleted_key(string(""));
  2111. for (int i = 32; i < 128; i++) {
  2112. // This maps 'a' to 32 a's, 'b' to 33 b's, etc.
  2113. ht_out[string(1, (char)i)] = string((size_t)i, (char)i);
  2114. }
  2115. ht_out.erase("c"); // just to test having some erased keys when we write.
  2116. ht_out.erase("y");
  2117. string file(TmpFile("stringio"));
  2118. FILE* fp = fopen(file.c_str(), "wb");
  2119. if (fp)
  2120. {
  2121. EXPECT_TRUE(fp != NULL);
  2122. EXPECT_TRUE(ht_out.write_metadata(fp));
  2123. for (SP::const_iterator it = ht_out.cbegin(); it != ht_out.cend(); ++it)
  2124. {
  2125. const string::size_type first_size = it->first.length();
  2126. fwrite(&first_size, sizeof(first_size), 1, fp); // ignore endianness issues
  2127. fwrite(it->first.c_str(), first_size, 1, fp);
  2128. const string::size_type second_size = it->second.length();
  2129. fwrite(&second_size, sizeof(second_size), 1, fp);
  2130. fwrite(it->second.c_str(), second_size, 1, fp);
  2131. }
  2132. fclose(fp);
  2133. }
  2134. sparse_hash_map<string, string, Hasher, Hasher> ht_in;
  2135. fp = fopen(file.c_str(), "rb");
  2136. if (fp)
  2137. {
  2138. EXPECT_TRUE(fp != NULL);
  2139. EXPECT_TRUE(ht_in.read_metadata(fp));
  2140. for (sparse_hash_map<string, string, Hasher, Hasher>::iterator
  2141. it = ht_in.begin(); it != ht_in.end(); ++it) {
  2142. string::size_type first_size;
  2143. EXPECT_EQ(1u, fread(&first_size, sizeof(first_size), 1, fp));
  2144. char* first = new char[first_size];
  2145. EXPECT_EQ(1u, fread(first, first_size, 1, fp));
  2146. string::size_type second_size;
  2147. EXPECT_EQ(1u, fread(&second_size, sizeof(second_size), 1, fp));
  2148. char* second = new char[second_size];
  2149. EXPECT_EQ(1u, fread(second, second_size, 1, fp));
  2150. // it points to garbage, so we have to use placement-new to initialize.
  2151. // We also have to use const-cast since it->first is const.
  2152. new(const_cast<string*>(&it->first)) string(first, first_size);
  2153. new(&it->second) string(second, second_size);
  2154. delete[] first;
  2155. delete[] second;
  2156. }
  2157. fclose(fp);
  2158. }
  2159. EXPECT_EQ(string(" "), ht_in[" "]);
  2160. EXPECT_EQ(string("+++++++++++++++++++++++++++++++++++++++++++"), ht_in["+"]);
  2161. EXPECT_EQ(string(""), ht_in["c"]); // should not have been saved
  2162. EXPECT_EQ(string(""), ht_in["y"]);
  2163. }
  2164. TYPED_TEST(HashtableAllTest, Serialization)
  2165. {
  2166. if (!this->ht_.supports_serialization()) return;
  2167. TypeParam ht_out;
  2168. ht_out.set_deleted_key(this->UniqueKey(2000));
  2169. for (int i = 1; i < 100; i++) {
  2170. ht_out.insert(this->UniqueObject(i));
  2171. }
  2172. // just to test having some erased keys when we write.
  2173. ht_out.erase(this->UniqueKey(56));
  2174. ht_out.erase(this->UniqueKey(22));
  2175. string file(TmpFile("serialization"));
  2176. FILE* fp = fopen(file.c_str(), "wb");
  2177. if (fp)
  2178. {
  2179. EXPECT_TRUE(fp != NULL);
  2180. EXPECT_TRUE(ht_out.serialize(ValueSerializer(), fp));
  2181. fclose(fp);
  2182. }
  2183. TypeParam ht_in;
  2184. fp = fopen(file.c_str(), "rb");
  2185. if (fp)
  2186. {
  2187. EXPECT_TRUE(fp != NULL);
  2188. EXPECT_TRUE(ht_in.unserialize(ValueSerializer(), fp));
  2189. fclose(fp);
  2190. }
  2191. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2192. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2193. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2194. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2195. // should not have been saved
  2196. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2197. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2198. }
  2199. TYPED_TEST(HashtableIntTest, NopointerSerialization)
  2200. {
  2201. if (!this->ht_.supports_serialization()) return;
  2202. TypeParam ht_out;
  2203. ht_out.set_deleted_key(this->UniqueKey(2000));
  2204. for (int i = 1; i < 100; i++) {
  2205. ht_out.insert(this->UniqueObject(i));
  2206. }
  2207. // just to test having some erased keys when we write.
  2208. ht_out.erase(this->UniqueKey(56));
  2209. ht_out.erase(this->UniqueKey(22));
  2210. string file(TmpFile("nopointer_serialization"));
  2211. FILE* fp = fopen(file.c_str(), "wb");
  2212. if (fp)
  2213. {
  2214. EXPECT_TRUE(fp != NULL);
  2215. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(), fp));
  2216. fclose(fp);
  2217. }
  2218. TypeParam ht_in;
  2219. fp = fopen(file.c_str(), "rb");
  2220. if (fp)
  2221. {
  2222. EXPECT_TRUE(fp != NULL);
  2223. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(), fp));
  2224. fclose(fp);
  2225. }
  2226. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2227. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2228. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2229. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2230. // should not have been saved
  2231. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2232. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2233. }
  2234. // We don't support serializing to a string by default, but you can do
  2235. // it by writing your own custom input/output class.
  2236. class StringIO {
  2237. public:
  2238. explicit StringIO(string* s) : s_(s) {}
  2239. size_t Write(const void* buf, size_t len) {
  2240. s_->append(reinterpret_cast<const char*>(buf), len);
  2241. return len;
  2242. }
  2243. size_t Read(void* buf, size_t len) {
  2244. if (s_->length() < len)
  2245. len = s_->length();
  2246. memcpy(reinterpret_cast<char*>(buf), s_->data(), len);
  2247. s_->erase(0, len);
  2248. return len;
  2249. }
  2250. private:
  2251. StringIO& operator=(const StringIO&);
  2252. string* const s_;
  2253. };
  2254. TYPED_TEST(HashtableIntTest, SerializingToString)
  2255. {
  2256. if (!this->ht_.supports_serialization()) return;
  2257. TypeParam ht_out;
  2258. ht_out.set_deleted_key(this->UniqueKey(2000));
  2259. for (int i = 1; i < 100; i++) {
  2260. ht_out.insert(this->UniqueObject(i));
  2261. }
  2262. // just to test having some erased keys when we write.
  2263. ht_out.erase(this->UniqueKey(56));
  2264. ht_out.erase(this->UniqueKey(22));
  2265. string stringbuf;
  2266. StringIO stringio(&stringbuf);
  2267. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2268. &stringio));
  2269. TypeParam ht_in;
  2270. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(),
  2271. &stringio));
  2272. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2273. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2274. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2275. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2276. // should not have been saved
  2277. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2278. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2279. }
  2280. // An easier way to do the above would be to use the existing stream methods.
  2281. TYPED_TEST(HashtableIntTest, SerializingToStringStream)
  2282. {
  2283. if (!this->ht_.supports_serialization()) return;
  2284. TypeParam ht_out;
  2285. ht_out.set_deleted_key(this->UniqueKey(2000));
  2286. for (int i = 1; i < 100; i++) {
  2287. ht_out.insert(this->UniqueObject(i));
  2288. }
  2289. // just to test having some erased keys when we write.
  2290. ht_out.erase(this->UniqueKey(56));
  2291. ht_out.erase(this->UniqueKey(22));
  2292. std::stringstream string_buffer;
  2293. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2294. &string_buffer));
  2295. TypeParam ht_in;
  2296. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(),
  2297. &string_buffer));
  2298. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2299. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2300. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2301. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2302. // should not have been saved
  2303. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2304. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2305. }
  2306. // Verify that the metadata serialization is endianness and word size
  2307. // agnostic.
  2308. TYPED_TEST(HashtableAllTest, MetadataSerializationAndEndianness)
  2309. {
  2310. TypeParam ht_out;
  2311. string kExpectedDense("\x13W\x86""B\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0",
  2312. 24);
  2313. // GP change - switched size from 20 to formula, because the sparsegroup bitmap is 4 or 8 bytes and not 6
  2314. string kExpectedSparse("$hu1\0\0\0 \0\0\0\0\0\0\0\0\0\0\0", 12 + sizeof(group_bm_type));
  2315. if (ht_out.supports_readwrite()) {
  2316. size_t num_bytes = 0;
  2317. string file(TmpFile("metadata_serialization"));
  2318. FILE* fp = fopen(file.c_str(), "wb");
  2319. if (fp)
  2320. {
  2321. EXPECT_TRUE(fp != NULL);
  2322. EXPECT_TRUE(ht_out.write_metadata(fp));
  2323. EXPECT_TRUE(ht_out.write_nopointer_data(fp));
  2324. num_bytes = (const size_t)ftell(fp);
  2325. fclose(fp);
  2326. }
  2327. char contents[24] = {0};
  2328. fp = fopen(file.c_str(), "rb");
  2329. if (fp)
  2330. {
  2331. EXPECT_LE(num_bytes, static_cast<size_t>(24));
  2332. EXPECT_EQ(num_bytes, fread(contents, 1, num_bytes <= 24 ? num_bytes : 24, fp));
  2333. EXPECT_EQ(EOF, fgetc(fp)); // check we're *exactly* the right size
  2334. fclose(fp);
  2335. }
  2336. // TODO(csilvers): check type of ht_out instead of looking at the 1st byte.
  2337. if (contents[0] == kExpectedDense[0]) {
  2338. EXPECT_EQ(kExpectedDense, string(contents, num_bytes));
  2339. } else {
  2340. EXPECT_EQ(kExpectedSparse, string(contents, num_bytes));
  2341. }
  2342. }
  2343. // Do it again with new-style serialization. Here we can use StringIO.
  2344. if (ht_out.supports_serialization()) {
  2345. string stringbuf;
  2346. StringIO stringio(&stringbuf);
  2347. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2348. &stringio));
  2349. if (stringbuf[0] == kExpectedDense[0]) {
  2350. EXPECT_EQ(kExpectedDense, stringbuf);
  2351. } else {
  2352. EXPECT_EQ(kExpectedSparse, stringbuf);
  2353. }
  2354. }
  2355. }
  2356. // ------------------------------------------------------------------------
  2357. // The above tests test the general API for correctness. These tests
  2358. // test a few corner cases that have tripped us up in the past, and
  2359. // more general, cross-API issues like memory management.
  2360. TYPED_TEST(HashtableAllTest, BracketOperatorCrashing)
  2361. {
  2362. this->ht_.set_deleted_key(this->UniqueKey(1));
  2363. for (int iters = 0; iters < 10; iters++) {
  2364. // We start at 33 because after shrinking, we'll be at 32 buckets.
  2365. for (int i = 33; i < 133; i++) {
  2366. this->ht_.bracket_assign(this->UniqueKey(i),
  2367. this->ht_.get_data(this->UniqueObject(i)));
  2368. }
  2369. this->ht_.clear_no_resize();
  2370. // This will force a shrink on the next insert, which we want to test.
  2371. this->ht_.bracket_assign(this->UniqueKey(2),
  2372. this->ht_.get_data(this->UniqueObject(2)));
  2373. this->ht_.erase(this->UniqueKey(2));
  2374. }
  2375. }
  2376. // For data types with trivial copy-constructors and destructors, we
  2377. // should use an optimized routine for data-copying, that involves
  2378. // memmove. We test this by keeping count of how many times the
  2379. // copy-constructor is called; it should be much less with the
  2380. // optimized code.
  2381. struct Memmove
  2382. {
  2383. public:
  2384. Memmove(): i(0) {}
  2385. explicit Memmove(int ival): i(ival) {}
  2386. Memmove(const Memmove& that) { this->i = that.i; num_copies++; }
  2387. int i;
  2388. static int num_copies;
  2389. };
  2390. int Memmove::num_copies = 0;
  2391. struct NoMemmove
  2392. {
  2393. public:
  2394. NoMemmove(): i(0) {}
  2395. explicit NoMemmove(int ival): i(ival) {}
  2396. NoMemmove(const NoMemmove& that) { this->i = that.i; num_copies++; }
  2397. int i;
  2398. static int num_copies;
  2399. };
  2400. int NoMemmove::num_copies = 0;
  2401. } // unnamed namespace
  2402. #if 0
  2403. // This is what tells the hashtable code it can use memmove for this class:
  2404. namespace google {
  2405. template<> struct has_trivial_copy<Memmove> : true_type { };
  2406. template<> struct has_trivial_destructor<Memmove> : true_type { };
  2407. };
  2408. #endif
  2409. namespace
  2410. {
  2411. TEST(HashtableTest, SimpleDataTypeOptimizations)
  2412. {
  2413. // Only sparsehashtable optimizes moves in this way.
  2414. sparse_hash_map<int, Memmove, Hasher, Hasher> memmove;
  2415. sparse_hash_map<int, NoMemmove, Hasher, Hasher> nomemmove;
  2416. sparse_hash_map<int, Memmove, Hasher, Hasher, Alloc<int> >
  2417. memmove_nonstandard_alloc;
  2418. Memmove::num_copies = 0;
  2419. for (int i = 10000; i > 0; i--) {
  2420. memmove[i] = Memmove(i);
  2421. }
  2422. // GP change - const int memmove_copies = Memmove::num_copies;
  2423. NoMemmove::num_copies = 0;
  2424. for (int i = 10000; i > 0; i--) {
  2425. nomemmove[i] = NoMemmove(i);
  2426. }
  2427. // GP change - const int nomemmove_copies = NoMemmove::num_copies;
  2428. Memmove::num_copies = 0;
  2429. for (int i = 10000; i > 0; i--) {
  2430. memmove_nonstandard_alloc[i] = Memmove(i);
  2431. }
  2432. // GP change - const int memmove_nonstandard_alloc_copies = Memmove::num_copies;
  2433. // GP change - commented out following two lines
  2434. //EXPECT_GT(nomemmove_copies, memmove_copies);
  2435. //EXPECT_EQ(nomemmove_copies, memmove_nonstandard_alloc_copies);
  2436. }
  2437. TYPED_TEST(HashtableAllTest, ResizeHysteresis)
  2438. {
  2439. // We want to make sure that when we create a hashtable, and then
  2440. // add and delete one element, the size of the hashtable doesn't
  2441. // change.
  2442. this->ht_.set_deleted_key(this->UniqueKey(1));
  2443. typename TypeParam::size_type old_bucket_count = this->ht_.bucket_count();
  2444. this->ht_.insert(this->UniqueObject(4));
  2445. this->ht_.erase(this->UniqueKey(4));
  2446. this->ht_.insert(this->UniqueObject(4));
  2447. this->ht_.erase(this->UniqueKey(4));
  2448. EXPECT_EQ(old_bucket_count, this->ht_.bucket_count());
  2449. // Try it again, but with a hashtable that starts very small
  2450. TypeParam ht(2);
  2451. EXPECT_LT(ht.bucket_count(), 32u); // verify we really do start small
  2452. ht.set_deleted_key(this->UniqueKey(1));
  2453. old_bucket_count = ht.bucket_count();
  2454. ht.insert(this->UniqueObject(4));
  2455. ht.erase(this->UniqueKey(4));
  2456. ht.insert(this->UniqueObject(4));
  2457. ht.erase(this->UniqueKey(4));
  2458. EXPECT_EQ(old_bucket_count, ht.bucket_count());
  2459. }
  2460. TEST(HashtableTest, ConstKey)
  2461. {
  2462. // Sometimes people write hash_map<const int, int>, even though the
  2463. // const isn't necessary. Make sure we handle this cleanly.
  2464. sparse_hash_map<const int, int, Hasher, Hasher> shm;
  2465. shm.set_deleted_key(1);
  2466. shm[10] = 20;
  2467. }
  2468. TYPED_TEST(HashtableAllTest, ResizeActuallyResizes)
  2469. {
  2470. // This tests for a problem we had where we could repeatedly "resize"
  2471. // a hashtable to the same size it was before, on every insert.
  2472. // -----------------------------------------------------------------
  2473. const typename TypeParam::size_type kSize = 1<<10; // Pick any power of 2
  2474. const float kResize = 0.8f; // anything between 0.5 and 1 is fine.
  2475. const int kThreshold = static_cast<int>(kSize * kResize - 1);
  2476. this->ht_.set_resizing_parameters(0, kResize);
  2477. this->ht_.set_deleted_key(this->UniqueKey(kThreshold + 100));
  2478. // Get right up to the resizing threshold.
  2479. for (int i = 0; i <= kThreshold; i++) {
  2480. this->ht_.insert(this->UniqueObject(i+1));
  2481. }
  2482. // The bucket count should equal kSize.
  2483. EXPECT_EQ(kSize, this->ht_.bucket_count());
  2484. // Now start doing erase+insert pairs. This should cause us to
  2485. // copy the hashtable at most once.
  2486. const int pre_copies = this->ht_.num_table_copies();
  2487. for (int i = 0; i < static_cast<int>(kSize); i++) {
  2488. this->ht_.erase(this->UniqueKey(kThreshold));
  2489. this->ht_.insert(this->UniqueObject(kThreshold));
  2490. }
  2491. EXPECT_LT(this->ht_.num_table_copies(), pre_copies + 2);
  2492. // Now create a hashtable where we go right to the threshold, then
  2493. // delete everything and do one insert. Even though our hashtable
  2494. // is now tiny, we should still have at least kSize buckets, because
  2495. // our shrink threshhold is 0.
  2496. // -----------------------------------------------------------------
  2497. TypeParam ht2;
  2498. ht2.set_deleted_key(this->UniqueKey(kThreshold + 100));
  2499. ht2.set_resizing_parameters(0, kResize);
  2500. EXPECT_LT(ht2.bucket_count(), kSize);
  2501. for (int i = 0; i <= kThreshold; i++) {
  2502. ht2.insert(this->UniqueObject(i+1));
  2503. }
  2504. EXPECT_EQ(ht2.bucket_count(), kSize);
  2505. for (int i = 0; i <= kThreshold; i++) {
  2506. ht2.erase(this->UniqueKey(i+1));
  2507. EXPECT_EQ(ht2.bucket_count(), kSize);
  2508. }
  2509. ht2.insert(this->UniqueObject(kThreshold+2));
  2510. EXPECT_GE(ht2.bucket_count(), kSize);
  2511. }
  2512. TEST(HashtableTest, CXX11)
  2513. {
  2514. #if !defined(SPP_NO_CXX11_HDR_INITIALIZER_LIST)
  2515. {
  2516. // Initializer lists
  2517. // -----------------
  2518. typedef sparse_hash_map<int, int> Smap;
  2519. Smap smap({ {1, 1}, {2, 2} });
  2520. EXPECT_EQ(smap.size(), 2);
  2521. smap = { {1, 1}, {2, 2}, {3, 4} };
  2522. EXPECT_EQ(smap.size(), 3);
  2523. smap.insert({{5, 1}, {6, 1}});
  2524. EXPECT_EQ(smap.size(), 5);
  2525. EXPECT_EQ(smap[6], 1);
  2526. EXPECT_EQ(smap.at(6), 1);
  2527. try
  2528. {
  2529. EXPECT_EQ(smap.at(999), 1);
  2530. }
  2531. catch (...)
  2532. {};
  2533. sparse_hash_set<int> sset({ 1, 3, 4, 5 });
  2534. EXPECT_EQ(sset.size(), 4);
  2535. }
  2536. #endif
  2537. }
  2538. TEST(HashtableTest, NestedHashtables)
  2539. {
  2540. // People can do better than to have a hash_map of hash_maps, but we
  2541. // should still support it. I try a few different mappings.
  2542. sparse_hash_map<string, sparse_hash_map<int, string>, Hasher, Hasher> ht1;
  2543. ht1["hi"]; // create a sub-ht with the default values
  2544. ht1["lo"][1] = "there";
  2545. sparse_hash_map<string, sparse_hash_map<int, string>, Hasher, Hasher>
  2546. ht1copy = ht1;
  2547. }
  2548. TEST(HashtableDeathTest, ResizeOverflow)
  2549. {
  2550. sparse_hash_map<int, int> ht2;
  2551. EXPECT_DEATH(ht2.resize(static_cast<size_t>(-1)), "overflows size_type");
  2552. }
  2553. TEST(HashtableDeathTest, InsertSizeTypeOverflow)
  2554. {
  2555. static const int kMax = 256;
  2556. vector<int> test_data(kMax);
  2557. for (int i = 0; i < kMax; ++i) {
  2558. test_data[(size_t)i] = i+1000;
  2559. }
  2560. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2561. // Test we are using the correct allocator
  2562. EXPECT_TRUE(shs.get_allocator().is_custom_alloc());
  2563. // Test size_type overflow in insert(it, it)
  2564. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.end()), "overflows size_type");
  2565. }
  2566. TEST(HashtableDeathTest, InsertMaxSizeOverflow)
  2567. {
  2568. static const int kMax = 256;
  2569. vector<int> test_data(kMax);
  2570. for (int i = 0; i < kMax; ++i) {
  2571. test_data[(size_t)i] = i+1000;
  2572. }
  2573. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2574. // Test max_size overflow
  2575. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.begin() + 11), "exceed max_size");
  2576. }
  2577. TEST(HashtableDeathTest, ResizeSizeTypeOverflow)
  2578. {
  2579. // Test min-buckets overflow, when we want to resize too close to size_type
  2580. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2581. EXPECT_DEATH(shs.resize(250), "overflows size_type");
  2582. }
  2583. TEST(HashtableDeathTest, ResizeDeltaOverflow)
  2584. {
  2585. static const int kMax = 256;
  2586. vector<int> test_data(kMax);
  2587. for (int i = 0; i < kMax; ++i) {
  2588. test_data[(size_t)i] = i+1000;
  2589. }
  2590. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 255> > shs;
  2591. for (int i = 0; i < 9; i++) {
  2592. shs.insert(i);
  2593. }
  2594. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.begin() + 250),
  2595. "overflows size_type");
  2596. }
  2597. // ------------------------------------------------------------------------
  2598. // This informational "test" comes last so it's easy to see.
  2599. // Also, benchmarks.
  2600. TYPED_TEST(HashtableAllTest, ClassSizes)
  2601. {
  2602. std::cout << "sizeof(" << typeid(TypeParam).name() << "): "
  2603. << sizeof(this->ht_) << "\n";
  2604. }
  2605. } // unnamed namespace
  2606. int main(int, char **)
  2607. {
  2608. // All the work is done in the static constructors. If they don't
  2609. // die, the tests have all passed.
  2610. cout << "PASS\n";
  2611. return 0;
  2612. }