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.

2988 lines
110 KiB

2 months ago
  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/spp.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. size_type erase(const key_type& key) { return ht_.erase(key); }
  309. void erase(typename HT::iterator it) { ht_.erase(it); }
  310. void erase(typename HT::iterator f, typename HT::iterator l) {
  311. ht_.erase(f, l);
  312. }
  313. bool operator==(const BaseHashtableInterface& other) const {
  314. return ht_ == other.ht_;
  315. }
  316. bool operator!=(const BaseHashtableInterface& other) const {
  317. return ht_ != other.ht_;
  318. }
  319. template <typename ValueSerializer, typename OUTPUT>
  320. bool serialize(ValueSerializer serializer, OUTPUT *fp) {
  321. return ht_.serialize(serializer, fp);
  322. }
  323. template <typename ValueSerializer, typename INPUT>
  324. bool unserialize(ValueSerializer serializer, INPUT *fp) {
  325. return ht_.unserialize(serializer, fp);
  326. }
  327. template <typename OUTPUT>
  328. bool write_metadata(OUTPUT *fp) {
  329. return ht_.write_metadata(fp);
  330. }
  331. template <typename INPUT>
  332. bool read_metadata(INPUT *fp) {
  333. return ht_.read_metadata(fp);
  334. }
  335. template <typename OUTPUT>
  336. bool write_nopointer_data(OUTPUT *fp) {
  337. return ht_.write_nopointer_data(fp);
  338. }
  339. template <typename INPUT>
  340. bool read_nopointer_data(INPUT *fp) {
  341. return ht_.read_nopointer_data(fp);
  342. }
  343. // low-level stats
  344. int num_table_copies() const { return (int)ht_.num_table_copies(); }
  345. // Not part of the hashtable API, but is provided to make testing easier.
  346. virtual key_type get_key(const value_type& value) const = 0;
  347. // All subclasses should define get_data(value_type) as well. I don't
  348. // provide an abstract-virtual definition here, because the return type
  349. // differs between subclasses (not all subclasses define data_type).
  350. //virtual data_type get_data(const value_type& value) const = 0;
  351. //virtual data_type default_data() const = 0;
  352. // These allow introspection into the interface. "Supports" means
  353. // that the implementation of this functionality isn't a noop.
  354. virtual bool supports_clear_no_resize() const = 0;
  355. virtual bool supports_empty_key() const = 0;
  356. virtual bool supports_deleted_key() const = 0;
  357. virtual bool supports_brackets() const = 0; // has a 'real' operator[]
  358. virtual bool supports_readwrite() const = 0;
  359. virtual bool supports_num_table_copies() const = 0;
  360. virtual bool supports_serialization() const = 0;
  361. protected:
  362. HT ht_;
  363. // These are what subclasses have to define to get class-specific behavior
  364. virtual key_type it_to_key(const iterator& it) const = 0;
  365. virtual key_type it_to_key(const const_iterator& it) const = 0;
  366. virtual key_type it_to_key(const local_iterator& it) const = 0;
  367. virtual key_type it_to_key(const const_local_iterator& it) const = 0;
  368. };
  369. // ---------------------------------------------------------------------
  370. // ---------------------------------------------------------------------
  371. template <class Key, class T,
  372. class HashFcn = SPP_HASH_CLASS<Key>,
  373. class EqualKey = std::equal_to<Key>,
  374. class Alloc = SPP_DEFAULT_ALLOCATOR<std::pair<const Key, T> > >
  375. class HashtableInterface_SparseHashMap
  376. : public BaseHashtableInterface< sparse_hash_map<Key, T, HashFcn,
  377. EqualKey, Alloc> >
  378. {
  379. private:
  380. typedef sparse_hash_map<Key, T, HashFcn, EqualKey, Alloc> ht;
  381. typedef BaseHashtableInterface<ht> p; // parent
  382. public:
  383. explicit HashtableInterface_SparseHashMap(
  384. typename p::size_type expected_max_items = 0,
  385. const typename p::hasher& hf = typename p::hasher(),
  386. const typename p::key_equal& eql = typename p::key_equal(),
  387. const typename p::allocator_type& alloc = typename p::allocator_type())
  388. : BaseHashtableInterface<ht>(expected_max_items, hf, eql, alloc) { }
  389. template <class InputIterator>
  390. HashtableInterface_SparseHashMap(
  391. InputIterator f, InputIterator l,
  392. typename p::size_type expected_max_items = 0,
  393. const typename p::hasher& hf = typename p::hasher(),
  394. const typename p::key_equal& eql = typename p::key_equal(),
  395. const typename p::allocator_type& alloc = typename p::allocator_type())
  396. : BaseHashtableInterface<ht>(f, l, expected_max_items, hf, eql, alloc) { }
  397. typename p::key_type get_key(const typename p::value_type& value) const {
  398. return value.first;
  399. }
  400. typename ht::data_type get_data(const typename p::value_type& value) const {
  401. return value.second;
  402. }
  403. typename ht::data_type default_data() const {
  404. return typename ht::data_type();
  405. }
  406. bool supports_clear_no_resize() const { return false; }
  407. bool supports_empty_key() const { return false; }
  408. bool supports_deleted_key() const { return false; }
  409. bool supports_brackets() const { return true; }
  410. bool supports_readwrite() const { return true; }
  411. bool supports_num_table_copies() const { return false; }
  412. bool supports_serialization() const { return true; }
  413. void set_empty_key(const typename p::key_type&) { }
  414. void clear_empty_key() { }
  415. typename p::key_type empty_key() const { return typename p::key_type(); }
  416. int num_table_copies() const { return 0; }
  417. typedef typename ht::NopointerSerializer NopointerSerializer;
  418. protected:
  419. template <class K2, class T2, class H2, class E2, class A2>
  420. friend void swap(HashtableInterface_SparseHashMap<K2,T2,H2,E2,A2>& a,
  421. HashtableInterface_SparseHashMap<K2,T2,H2,E2,A2>& b);
  422. typename p::key_type it_to_key(const typename p::iterator& it) const {
  423. return it->first;
  424. }
  425. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  426. return it->first;
  427. }
  428. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  429. return it->first;
  430. }
  431. typename p::key_type it_to_key(const typename p::const_local_iterator& it) const {
  432. return it->first;
  433. }
  434. };
  435. // ---------------------------------------------------------------------
  436. // ---------------------------------------------------------------------
  437. template <class K, class T, class H, class E, class A>
  438. void swap(HashtableInterface_SparseHashMap<K,T,H,E,A>& a,
  439. HashtableInterface_SparseHashMap<K,T,H,E,A>& b)
  440. {
  441. swap(a.ht_, b.ht_);
  442. }
  443. // ---------------------------------------------------------------------
  444. // ---------------------------------------------------------------------
  445. template <class Value,
  446. class HashFcn = SPP_HASH_CLASS<Value>,
  447. class EqualKey = std::equal_to<Value>,
  448. class Alloc = SPP_DEFAULT_ALLOCATOR<Value> >
  449. class HashtableInterface_SparseHashSet
  450. : public BaseHashtableInterface< sparse_hash_set<Value, HashFcn,
  451. EqualKey, Alloc> >
  452. {
  453. private:
  454. typedef sparse_hash_set<Value, HashFcn, EqualKey, Alloc> ht;
  455. typedef BaseHashtableInterface<ht> p; // parent
  456. public:
  457. explicit HashtableInterface_SparseHashSet(
  458. typename p::size_type expected_max_items = 0,
  459. const typename p::hasher& hf = typename p::hasher(),
  460. const typename p::key_equal& eql = typename p::key_equal(),
  461. const typename p::allocator_type& alloc = typename p::allocator_type())
  462. : BaseHashtableInterface<ht>(expected_max_items, hf, eql, alloc) { }
  463. template <class InputIterator>
  464. HashtableInterface_SparseHashSet(
  465. InputIterator f, InputIterator l,
  466. typename p::size_type expected_max_items = 0,
  467. const typename p::hasher& hf = typename p::hasher(),
  468. const typename p::key_equal& eql = typename p::key_equal(),
  469. const typename p::allocator_type& alloc = typename p::allocator_type())
  470. : BaseHashtableInterface<ht>(f, l, expected_max_items, hf, eql, alloc) { }
  471. template<typename AssignValue>
  472. bool bracket_equal(const typename p::key_type& key, const AssignValue&) {
  473. return this->ht_.find(key) != this->ht_.end();
  474. }
  475. template<typename AssignValue>
  476. void bracket_assign(const typename p::key_type& key, const AssignValue&) {
  477. this->ht_.insert(key);
  478. }
  479. typename p::key_type get_key(const typename p::value_type& value) const {
  480. return value;
  481. }
  482. // For sets, the only 'data' is that an item is actually inserted.
  483. bool get_data(const typename p::value_type&) const {
  484. return true;
  485. }
  486. bool default_data() const {
  487. return true;
  488. }
  489. bool supports_clear_no_resize() const { return false; }
  490. bool supports_empty_key() const { return false; }
  491. bool supports_deleted_key() const { return false; }
  492. bool supports_brackets() const { return false; }
  493. bool supports_readwrite() const { return true; }
  494. bool supports_num_table_copies() const { return false; }
  495. bool supports_serialization() const { return true; }
  496. void set_empty_key(const typename p::key_type&) { }
  497. void clear_empty_key() { }
  498. typename p::key_type empty_key() const { return typename p::key_type(); }
  499. int num_table_copies() const { return 0; }
  500. typedef typename ht::NopointerSerializer NopointerSerializer;
  501. protected:
  502. template <class K2, class H2, class E2, class A2>
  503. friend void swap(HashtableInterface_SparseHashSet<K2,H2,E2,A2>& a,
  504. HashtableInterface_SparseHashSet<K2,H2,E2,A2>& b);
  505. typename p::key_type it_to_key(const typename p::iterator& it) const {
  506. return *it;
  507. }
  508. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  509. return *it;
  510. }
  511. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  512. return *it;
  513. }
  514. typename p::key_type it_to_key(const typename p::const_local_iterator& it)
  515. const {
  516. return *it;
  517. }
  518. };
  519. // ---------------------------------------------------------------------
  520. // ---------------------------------------------------------------------
  521. template <class K, class H, class E, class A>
  522. void swap(HashtableInterface_SparseHashSet<K,H,E,A>& a,
  523. HashtableInterface_SparseHashSet<K,H,E,A>& b)
  524. {
  525. swap(a.ht_, b.ht_);
  526. }
  527. // ---------------------------------------------------------------------
  528. // ---------------------------------------------------------------------
  529. template <class Value, class Key, class HashFcn, class ExtractKey,
  530. class SetKey, class EqualKey, class Alloc>
  531. class HashtableInterface_SparseHashtable
  532. : public BaseHashtableInterface< sparse_hashtable<Value, Key, HashFcn,
  533. ExtractKey, SetKey,
  534. EqualKey, Alloc> >
  535. {
  536. private:
  537. typedef sparse_hashtable<Value, Key, HashFcn, ExtractKey, SetKey,
  538. EqualKey, Alloc> ht;
  539. typedef BaseHashtableInterface<ht> p; // parent
  540. public:
  541. explicit HashtableInterface_SparseHashtable(
  542. typename p::size_type expected_max_items = 0,
  543. const typename p::hasher& hf = typename p::hasher(),
  544. const typename p::key_equal& eql = typename p::key_equal(),
  545. const typename p::allocator_type& alloc = typename p::allocator_type())
  546. : BaseHashtableInterface<ht>(expected_max_items, hf, eql,
  547. ExtractKey(), SetKey(), alloc) { }
  548. template <class InputIterator>
  549. HashtableInterface_SparseHashtable(
  550. InputIterator f, InputIterator l,
  551. typename p::size_type expected_max_items = 0,
  552. const typename p::hasher& hf = typename p::hasher(),
  553. const typename p::key_equal& eql = typename p::key_equal(),
  554. const typename p::allocator_type& alloc = typename p::allocator_type())
  555. : BaseHashtableInterface<ht>(expected_max_items, hf, eql,
  556. ExtractKey(), SetKey(), alloc) {
  557. this->insert(f, l);
  558. }
  559. float max_load_factor() const {
  560. float shrink, grow;
  561. this->ht_.get_resizing_parameters(&shrink, &grow);
  562. return grow;
  563. }
  564. void max_load_factor(float new_grow) {
  565. float shrink, grow;
  566. this->ht_.get_resizing_parameters(&shrink, &grow);
  567. this->ht_.set_resizing_parameters(shrink, new_grow);
  568. }
  569. float min_load_factor() const {
  570. float shrink, grow;
  571. this->ht_.get_resizing_parameters(&shrink, &grow);
  572. return shrink;
  573. }
  574. void min_load_factor(float new_shrink) {
  575. float shrink, grow;
  576. this->ht_.get_resizing_parameters(&shrink, &grow);
  577. this->ht_.set_resizing_parameters(new_shrink, grow);
  578. }
  579. template<typename AssignValue>
  580. bool bracket_equal(const typename p::key_type&, const AssignValue&) {
  581. return false;
  582. }
  583. template<typename AssignValue>
  584. void bracket_assign(const typename p::key_type&, const AssignValue&) {
  585. }
  586. typename p::key_type get_key(const typename p::value_type& value) const {
  587. return extract_key(value);
  588. }
  589. typename p::value_type get_data(const typename p::value_type& value) const {
  590. return value;
  591. }
  592. typename p::value_type default_data() const {
  593. return typename p::value_type();
  594. }
  595. bool supports_clear_no_resize() const { return false; }
  596. bool supports_empty_key() const { return false; }
  597. bool supports_deleted_key() const { return false; }
  598. bool supports_brackets() const { return false; }
  599. bool supports_readwrite() const { return true; }
  600. bool supports_num_table_copies() const { return true; }
  601. bool supports_serialization() const { return true; }
  602. void set_empty_key(const typename p::key_type&) { }
  603. void clear_empty_key() { }
  604. typename p::key_type empty_key() const { return typename p::key_type(); }
  605. // These tr1 names aren't defined for sparse_hashtable.
  606. typename p::hasher hash_function() { return this->hash_funct(); }
  607. void rehash(typename p::size_type hint) { this->resize(hint); }
  608. // TODO(csilvers): also support/test destructive_begin()/destructive_end()?
  609. typedef typename ht::NopointerSerializer NopointerSerializer;
  610. protected:
  611. template <class V2, class K2, class HF2, class EK2, class SK2, class Eq2,
  612. class A2>
  613. friend void swap(
  614. HashtableInterface_SparseHashtable<V2,K2,HF2,EK2,SK2,Eq2,A2>& a,
  615. HashtableInterface_SparseHashtable<V2,K2,HF2,EK2,SK2,Eq2,A2>& b);
  616. typename p::key_type it_to_key(const typename p::iterator& it) const {
  617. return extract_key(*it);
  618. }
  619. typename p::key_type it_to_key(const typename p::const_iterator& it) const {
  620. return extract_key(*it);
  621. }
  622. typename p::key_type it_to_key(const typename p::local_iterator& it) const {
  623. return extract_key(*it);
  624. }
  625. typename p::key_type it_to_key(const typename p::const_local_iterator& it)
  626. const {
  627. return extract_key(*it);
  628. }
  629. private:
  630. ExtractKey extract_key;
  631. };
  632. // ---------------------------------------------------------------------
  633. // ---------------------------------------------------------------------
  634. template <class V, class K, class HF, class EK, class SK, class Eq, class A>
  635. void swap(HashtableInterface_SparseHashtable<V,K,HF,EK,SK,Eq,A>& a,
  636. HashtableInterface_SparseHashtable<V,K,HF,EK,SK,Eq,A>& b) {
  637. swap(a.ht_, b.ht_);
  638. }
  639. void EXPECT_TRUE(bool cond)
  640. {
  641. if (!cond)
  642. {
  643. ::fputs("Test failed:\n", stderr);
  644. ::exit(1);
  645. }
  646. }
  647. namespace spp_
  648. {
  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. } // namespace spp_
  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<std::pair<const int, 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<std::pair<const string, 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<std::pair<const char* const, ValueType> > >, \
  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. (void)vt; // value_type may not be copyable. Easiest not to try.
  1269. h = this->ht_.hash_funct();
  1270. ke = this->ht_.key_eq();
  1271. at = this->ht_.get_allocator();
  1272. st = this->ht_.size();
  1273. (void)dt;
  1274. (void)p;
  1275. (void)cp;
  1276. (void)kt;
  1277. (void)st;
  1278. i = this->ht_.begin();
  1279. ci = this->ht_.begin();
  1280. li = this->ht_.begin(0);
  1281. cli = this->ht_.begin(0);
  1282. }
  1283. TYPED_TEST(HashtableAllTest, NormalIterators)
  1284. {
  1285. EXPECT_TRUE(this->ht_.begin() == this->ht_.end());
  1286. this->ht_.insert(this->UniqueObject(1));
  1287. {
  1288. typename TypeParam::iterator it = this->ht_.begin();
  1289. EXPECT_TRUE(it != this->ht_.end());
  1290. ++it;
  1291. EXPECT_TRUE(it == this->ht_.end());
  1292. }
  1293. }
  1294. #if !defined(SPP_NO_CXX11_VARIADIC_TEMPLATES)
  1295. template <class T> struct MyHash;
  1296. typedef std::pair<std::string, std::string> StringPair;
  1297. template<> struct MyHash<StringPair>
  1298. {
  1299. size_t operator()(StringPair const& p) const
  1300. {
  1301. return std::hash<string>()(p.first);
  1302. }
  1303. };
  1304. class MovableOnlyType
  1305. {
  1306. std::string _str;
  1307. std::uint64_t _int;
  1308. public:
  1309. // Make object movable and non-copyable
  1310. MovableOnlyType(MovableOnlyType &&) = default;
  1311. MovableOnlyType(const MovableOnlyType &) = delete;
  1312. MovableOnlyType& operator=(MovableOnlyType &&) = default;
  1313. MovableOnlyType& operator=(const MovableOnlyType &) = delete;
  1314. MovableOnlyType() : _str("whatever"), _int(2) {}
  1315. };
  1316. void movable_emplace_test(std::size_t iterations, int container_size)
  1317. {
  1318. for (std::size_t i=0;i<iterations;++i)
  1319. {
  1320. spp::sparse_hash_map<std::string,MovableOnlyType> m;
  1321. m.reserve(static_cast<size_t>(container_size));
  1322. char buff[20];
  1323. for (int j=0; j<container_size; ++j)
  1324. {
  1325. sprintf(buff, "%d", j);
  1326. m.emplace(buff, MovableOnlyType());
  1327. }
  1328. }
  1329. }
  1330. TEST(HashtableTest, Emplace)
  1331. {
  1332. {
  1333. sparse_hash_map<std::string, std::string> mymap;
  1334. mymap.emplace ("NCC-1701", "J.T. Kirk");
  1335. mymap.emplace ("NCC-1701-D", "J.L. Picard");
  1336. mymap.emplace ("NCC-74656", "K. Janeway");
  1337. EXPECT_TRUE(mymap["NCC-74656"] == std::string("K. Janeway"));
  1338. sparse_hash_set<StringPair, MyHash<StringPair> > myset;
  1339. myset.emplace ("NCC-1701", "J.T. Kirk");
  1340. }
  1341. movable_emplace_test(10, 50);
  1342. }
  1343. #endif
  1344. #if !defined(SPP_NO_CXX11_VARIADIC_TEMPLATES)
  1345. TEST(HashtableTest, IncompleteTypes)
  1346. {
  1347. int i;
  1348. sparse_hash_map<int *, int> ht2;
  1349. ht2[&i] = 3;
  1350. struct Bogus;
  1351. sparse_hash_map<Bogus *, int> ht3;
  1352. ht3[(Bogus *)0] = 8;
  1353. }
  1354. #endif
  1355. #if !defined(SPP_NO_CXX11_VARIADIC_TEMPLATES)
  1356. TEST(HashtableTest, ReferenceWrapper)
  1357. {
  1358. sparse_hash_map<int, std::reference_wrapper<int>> x;
  1359. int a = 5;
  1360. x.insert(std::make_pair(3, std::ref(a)));
  1361. EXPECT_EQ(x.at(3), 5);
  1362. }
  1363. #endif
  1364. #if !defined(SPP_NO_CXX11_RVALUE_REFERENCES)
  1365. class CNonCopyable
  1366. {
  1367. public:
  1368. CNonCopyable(CNonCopyable const &) = delete;
  1369. const CNonCopyable& operator=(CNonCopyable const &) = delete;
  1370. CNonCopyable() = default;
  1371. };
  1372. struct Probe : CNonCopyable
  1373. {
  1374. Probe() {}
  1375. Probe(Probe &&) {}
  1376. void operator=(Probe &&) {}
  1377. private:
  1378. Probe(const Probe &);
  1379. Probe& operator=(const Probe &);
  1380. };
  1381. TEST(HashtableTest, NonCopyable)
  1382. {
  1383. typedef spp::sparse_hash_map<uint64_t, Probe> THashMap;
  1384. THashMap probes;
  1385. probes.insert(THashMap::value_type(27, Probe()));
  1386. EXPECT_EQ(probes.begin()->first, 27);
  1387. }
  1388. #endif
  1389. TEST(HashtableTest, ModifyViaIterator)
  1390. {
  1391. // This only works for hash-maps, since only they have non-const values.
  1392. {
  1393. sparse_hash_map<int, int> ht;
  1394. ht[1] = 2;
  1395. sparse_hash_map<int, int>::iterator it = ht.find(1);
  1396. EXPECT_TRUE(it != ht.end());
  1397. EXPECT_EQ(1, it->first);
  1398. EXPECT_EQ(2, it->second);
  1399. it->second = 5;
  1400. it = ht.find(1);
  1401. EXPECT_TRUE(it != ht.end());
  1402. EXPECT_EQ(5, it->second);
  1403. }
  1404. }
  1405. TYPED_TEST(HashtableAllTest, ConstIterators)
  1406. {
  1407. this->ht_.insert(this->UniqueObject(1));
  1408. typename TypeParam::const_iterator it = this->ht_.begin();
  1409. EXPECT_TRUE(it != (typename TypeParam::const_iterator)this->ht_.end());
  1410. ++it;
  1411. EXPECT_TRUE(it == (typename TypeParam::const_iterator)this->ht_.end());
  1412. }
  1413. TYPED_TEST(HashtableAllTest, LocalIterators)
  1414. {
  1415. // Now, tr1 begin/end (the local iterator that takes a bucket-number).
  1416. // ht::bucket() returns the bucket that this key would be inserted in.
  1417. this->ht_.insert(this->UniqueObject(1));
  1418. const typename TypeParam::size_type bucknum =
  1419. this->ht_.bucket(this->UniqueKey(1));
  1420. typename TypeParam::local_iterator b = this->ht_.begin(bucknum);
  1421. typename TypeParam::local_iterator e = this->ht_.end(bucknum);
  1422. EXPECT_TRUE(b != e);
  1423. b++;
  1424. EXPECT_TRUE(b == e);
  1425. // Check an empty bucket. We can just xor the bottom bit and be sure
  1426. // of getting a legal bucket, since #buckets is always a power of 2.
  1427. EXPECT_TRUE(this->ht_.begin(bucknum ^ 1) == this->ht_.end(bucknum ^ 1));
  1428. // Another test, this time making sure we're using the right types.
  1429. typename TypeParam::local_iterator b2 = this->ht_.begin(bucknum ^ 1);
  1430. typename TypeParam::local_iterator e2 = this->ht_.end(bucknum ^ 1);
  1431. EXPECT_TRUE(b2 == e2);
  1432. }
  1433. TYPED_TEST(HashtableAllTest, ConstLocalIterators)
  1434. {
  1435. this->ht_.insert(this->UniqueObject(1));
  1436. const typename TypeParam::size_type bucknum =
  1437. this->ht_.bucket(this->UniqueKey(1));
  1438. typename TypeParam::const_local_iterator b = this->ht_.begin(bucknum);
  1439. typename TypeParam::const_local_iterator e = this->ht_.end(bucknum);
  1440. EXPECT_TRUE(b != e);
  1441. b++;
  1442. EXPECT_TRUE(b == e);
  1443. typename TypeParam::const_local_iterator b2 = this->ht_.begin(bucknum ^ 1);
  1444. typename TypeParam::const_local_iterator e2 = this->ht_.end(bucknum ^ 1);
  1445. EXPECT_TRUE(b2 == e2);
  1446. }
  1447. TYPED_TEST(HashtableAllTest, Iterating)
  1448. {
  1449. // Test a bit more iterating than just one ++.
  1450. this->ht_.insert(this->UniqueObject(1));
  1451. this->ht_.insert(this->UniqueObject(11));
  1452. this->ht_.insert(this->UniqueObject(111));
  1453. this->ht_.insert(this->UniqueObject(1111));
  1454. this->ht_.insert(this->UniqueObject(11111));
  1455. this->ht_.insert(this->UniqueObject(111111));
  1456. this->ht_.insert(this->UniqueObject(1111111));
  1457. this->ht_.insert(this->UniqueObject(11111111));
  1458. this->ht_.insert(this->UniqueObject(111111111));
  1459. typename TypeParam::iterator it = this->ht_.begin();
  1460. for (int i = 1; i <= 9; i++) { // start at 1 so i is never 0
  1461. // && here makes it easier to tell what loop iteration the test failed on.
  1462. EXPECT_TRUE(i && (it++ != this->ht_.end()));
  1463. }
  1464. EXPECT_TRUE(it == this->ht_.end());
  1465. }
  1466. TYPED_TEST(HashtableIntTest, Constructors)
  1467. {
  1468. // The key/value types don't matter here, so I just test on one set
  1469. // of tables, the ones with int keys, which can easily handle the
  1470. // placement-news we have to do below.
  1471. Hasher hasher(1); // 1 is a unique id
  1472. int alloc_count = 0;
  1473. Alloc<typename TypeParam::value_type> alloc(2, &alloc_count);
  1474. TypeParam ht_noarg;
  1475. TypeParam ht_onearg(100);
  1476. TypeParam ht_twoarg(100, hasher);
  1477. TypeParam ht_threearg(100, hasher, hasher); // hasher serves as key_equal too
  1478. TypeParam ht_fourarg(100, hasher, hasher, alloc);
  1479. // The allocator should have been called at most once, for the last ht.
  1480. EXPECT_GE(1, alloc_count);
  1481. int old_alloc_count = alloc_count;
  1482. const typename TypeParam::value_type input[] = {
  1483. this->UniqueObject(1),
  1484. this->UniqueObject(2),
  1485. this->UniqueObject(4),
  1486. this->UniqueObject(8)
  1487. };
  1488. const int num_inputs = sizeof(input) / sizeof(input[0]);
  1489. const typename TypeParam::value_type *begin = &input[0];
  1490. const typename TypeParam::value_type *end = begin + num_inputs;
  1491. TypeParam ht_iter_noarg(begin, end);
  1492. TypeParam ht_iter_onearg(begin, end, 100);
  1493. TypeParam ht_iter_twoarg(begin, end, 100, hasher);
  1494. TypeParam ht_iter_threearg(begin, end, 100, hasher, hasher);
  1495. TypeParam ht_iter_fourarg(begin, end, 100, hasher, hasher, alloc);
  1496. // Now the allocator should have been called more.
  1497. EXPECT_GT(alloc_count, old_alloc_count);
  1498. old_alloc_count = alloc_count;
  1499. // Let's do a lot more inserting and make sure the alloc-count goes up
  1500. for (int i = 2; i < 2000; i++)
  1501. ht_fourarg.insert(this->UniqueObject(i));
  1502. EXPECT_GT(alloc_count, old_alloc_count);
  1503. EXPECT_LT(ht_noarg.bucket_count(), 100u);
  1504. EXPECT_GE(ht_onearg.bucket_count(), 100u);
  1505. EXPECT_GE(ht_twoarg.bucket_count(), 100u);
  1506. EXPECT_GE(ht_threearg.bucket_count(), 100u);
  1507. EXPECT_GE(ht_fourarg.bucket_count(), 100u);
  1508. EXPECT_GE(ht_iter_onearg.bucket_count(), 100u);
  1509. // When we pass in a hasher -- it can serve both as the hash-function
  1510. // and the key-equal function -- its id should be 1. Where we don't
  1511. // pass it in and use the default Hasher object, the id should be 0.
  1512. EXPECT_EQ(0, ht_noarg.hash_funct().id());
  1513. EXPECT_EQ(0, ht_noarg.key_eq().id());
  1514. EXPECT_EQ(0, ht_onearg.hash_funct().id());
  1515. EXPECT_EQ(0, ht_onearg.key_eq().id());
  1516. EXPECT_EQ(1, ht_twoarg.hash_funct().id());
  1517. EXPECT_EQ(0, ht_twoarg.key_eq().id());
  1518. EXPECT_EQ(1, ht_threearg.hash_funct().id());
  1519. EXPECT_EQ(1, ht_threearg.key_eq().id());
  1520. EXPECT_EQ(0, ht_iter_noarg.hash_funct().id());
  1521. EXPECT_EQ(0, ht_iter_noarg.key_eq().id());
  1522. EXPECT_EQ(0, ht_iter_onearg.hash_funct().id());
  1523. EXPECT_EQ(0, ht_iter_onearg.key_eq().id());
  1524. EXPECT_EQ(1, ht_iter_twoarg.hash_funct().id());
  1525. EXPECT_EQ(0, ht_iter_twoarg.key_eq().id());
  1526. EXPECT_EQ(1, ht_iter_threearg.hash_funct().id());
  1527. EXPECT_EQ(1, ht_iter_threearg.key_eq().id());
  1528. // Likewise for the allocator
  1529. EXPECT_EQ(0, ht_threearg.get_allocator().id());
  1530. EXPECT_EQ(0, ht_iter_threearg.get_allocator().id());
  1531. EXPECT_EQ(2, ht_fourarg.get_allocator().id());
  1532. EXPECT_EQ(2, ht_iter_fourarg.get_allocator().id());
  1533. }
  1534. TYPED_TEST(HashtableAllTest, OperatorEquals)
  1535. {
  1536. {
  1537. TypeParam ht1, ht2;
  1538. ht1.set_deleted_key(this->UniqueKey(1));
  1539. ht2.set_deleted_key(this->UniqueKey(2));
  1540. ht1.insert(this->UniqueObject(10));
  1541. ht2.insert(this->UniqueObject(20));
  1542. EXPECT_FALSE(ht1 == ht2);
  1543. ht1 = ht2;
  1544. EXPECT_TRUE(ht1 == ht2);
  1545. }
  1546. {
  1547. TypeParam ht1, ht2;
  1548. ht1.insert(this->UniqueObject(30));
  1549. ht1 = ht2;
  1550. EXPECT_EQ(0u, ht1.size());
  1551. }
  1552. {
  1553. TypeParam ht1, ht2;
  1554. ht1.set_deleted_key(this->UniqueKey(1));
  1555. ht2.insert(this->UniqueObject(1)); // has same key as ht1.delkey
  1556. ht1 = ht2; // should reset deleted-key to 'unset'
  1557. EXPECT_EQ(1u, ht1.size());
  1558. EXPECT_EQ(1u, ht1.count(this->UniqueKey(1)));
  1559. }
  1560. }
  1561. TYPED_TEST(HashtableAllTest, Clear)
  1562. {
  1563. for (int i = 1; i < 200; i++) {
  1564. this->ht_.insert(this->UniqueObject(i));
  1565. }
  1566. this->ht_.clear();
  1567. EXPECT_EQ(0u, this->ht_.size());
  1568. // TODO(csilvers): do we want to enforce that the hashtable has or
  1569. // has not shrunk? It does for dense_* but not sparse_*.
  1570. }
  1571. TYPED_TEST(HashtableAllTest, ClearNoResize)
  1572. {
  1573. if (!this->ht_.supports_clear_no_resize())
  1574. return;
  1575. typename TypeParam::size_type empty_bucket_count = this->ht_.bucket_count();
  1576. int last_element = 1;
  1577. while (this->ht_.bucket_count() == empty_bucket_count) {
  1578. this->ht_.insert(this->UniqueObject(last_element));
  1579. ++last_element;
  1580. }
  1581. typename TypeParam::size_type last_bucket_count = this->ht_.bucket_count();
  1582. this->ht_.clear_no_resize();
  1583. EXPECT_EQ(last_bucket_count, this->ht_.bucket_count());
  1584. EXPECT_TRUE(this->ht_.empty());
  1585. // When inserting the same number of elements again, no resize
  1586. // should be necessary.
  1587. for (int i = 1; i < last_element; ++i) {
  1588. this->ht_.insert(this->UniqueObject(last_element + i));
  1589. EXPECT_EQ(last_bucket_count, this->ht_.bucket_count());
  1590. }
  1591. }
  1592. TYPED_TEST(HashtableAllTest, Swap)
  1593. {
  1594. // Let's make a second hashtable with its own hasher, key_equal, etc.
  1595. Hasher hasher(1); // 1 is a unique id
  1596. TypeParam other_ht(200, hasher, hasher);
  1597. this->ht_.set_deleted_key(this->UniqueKey(1));
  1598. other_ht.set_deleted_key(this->UniqueKey(2));
  1599. for (int i = 3; i < 2000; i++) {
  1600. this->ht_.insert(this->UniqueObject(i));
  1601. }
  1602. this->ht_.erase(this->UniqueKey(1000));
  1603. other_ht.insert(this->UniqueObject(2001));
  1604. typename TypeParam::size_type expected_buckets = other_ht.bucket_count();
  1605. this->ht_.swap(other_ht);
  1606. EXPECT_EQ(1, this->ht_.hash_funct().id());
  1607. EXPECT_EQ(0, other_ht.hash_funct().id());
  1608. EXPECT_EQ(1, this->ht_.key_eq().id());
  1609. EXPECT_EQ(0, other_ht.key_eq().id());
  1610. EXPECT_EQ(expected_buckets, this->ht_.bucket_count());
  1611. EXPECT_GT(other_ht.bucket_count(), 200u);
  1612. EXPECT_EQ(1u, this->ht_.size());
  1613. EXPECT_EQ(1996u, other_ht.size()); // because we erased 1000
  1614. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(111)));
  1615. EXPECT_EQ(1u, other_ht.count(this->UniqueKey(111)));
  1616. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(2001)));
  1617. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(2001)));
  1618. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(1000)));
  1619. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(1000)));
  1620. // We purposefully don't swap allocs -- they're not necessarily swappable.
  1621. // Now swap back, using the free-function swap
  1622. // NOTE: MSVC seems to have trouble with this free swap, not quite
  1623. // sure why. I've given up trying to fix it though.
  1624. #ifdef _MSC_VER
  1625. other_ht.swap(this->ht_);
  1626. #else
  1627. std::swap(this->ht_, other_ht);
  1628. #endif
  1629. EXPECT_EQ(0, this->ht_.hash_funct().id());
  1630. EXPECT_EQ(1, other_ht.hash_funct().id());
  1631. EXPECT_EQ(1996u, this->ht_.size());
  1632. EXPECT_EQ(1u, other_ht.size());
  1633. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(111)));
  1634. EXPECT_EQ(0u, other_ht.count(this->UniqueKey(111)));
  1635. // A user reported a crash with this code using swap to clear.
  1636. // We've since fixed the bug; this prevents a regression.
  1637. TypeParam swap_to_clear_ht;
  1638. swap_to_clear_ht.set_deleted_key(this->UniqueKey(1));
  1639. for (int i = 2; i < 10000; ++i) {
  1640. swap_to_clear_ht.insert(this->UniqueObject(i));
  1641. }
  1642. TypeParam empty_ht;
  1643. empty_ht.swap(swap_to_clear_ht);
  1644. swap_to_clear_ht.set_deleted_key(this->UniqueKey(1));
  1645. for (int i = 2; i < 10000; ++i) {
  1646. swap_to_clear_ht.insert(this->UniqueObject(i));
  1647. }
  1648. }
  1649. TYPED_TEST(HashtableAllTest, Size)
  1650. {
  1651. EXPECT_EQ(0u, this->ht_.size());
  1652. for (int i = 1; i < 1000; i++) { // go through some resizes
  1653. this->ht_.insert(this->UniqueObject(i));
  1654. EXPECT_EQ(static_cast<typename TypeParam::size_type>(i), this->ht_.size());
  1655. }
  1656. this->ht_.clear();
  1657. EXPECT_EQ(0u, this->ht_.size());
  1658. this->ht_.set_deleted_key(this->UniqueKey(1));
  1659. EXPECT_EQ(0u, this->ht_.size()); // deleted key doesn't count
  1660. for (int i = 2; i < 1000; i++) { // go through some resizes
  1661. this->ht_.insert(this->UniqueObject(i));
  1662. this->ht_.erase(this->UniqueKey(i));
  1663. EXPECT_EQ(0u, this->ht_.size());
  1664. }
  1665. }
  1666. TEST(HashtableTest, MaxSizeAndMaxBucketCount)
  1667. {
  1668. // The max size depends on the allocator. So we can't use the
  1669. // built-in allocator type; instead, we make our own types.
  1670. sparse_hash_set<int, Hasher, Hasher, Alloc<int> > ht_default;
  1671. sparse_hash_set<int, Hasher, Hasher, Alloc<int, unsigned char> > ht_char;
  1672. sparse_hash_set<int, Hasher, Hasher, Alloc<int, unsigned char, 104> > ht_104;
  1673. EXPECT_GE(ht_default.max_size(), 256u);
  1674. EXPECT_EQ(255u, ht_char.max_size());
  1675. EXPECT_EQ(104u, ht_104.max_size());
  1676. // In our implementations, MaxBucketCount == MaxSize.
  1677. EXPECT_EQ(ht_default.max_size(), ht_default.max_bucket_count());
  1678. EXPECT_EQ(ht_char.max_size(), ht_char.max_bucket_count());
  1679. EXPECT_EQ(ht_104.max_size(), ht_104.max_bucket_count());
  1680. }
  1681. TYPED_TEST(HashtableAllTest, Empty)
  1682. {
  1683. EXPECT_TRUE(this->ht_.empty());
  1684. this->ht_.insert(this->UniqueObject(1));
  1685. EXPECT_FALSE(this->ht_.empty());
  1686. this->ht_.clear();
  1687. EXPECT_TRUE(this->ht_.empty());
  1688. TypeParam empty_ht;
  1689. this->ht_.insert(this->UniqueObject(1));
  1690. this->ht_.swap(empty_ht);
  1691. EXPECT_TRUE(this->ht_.empty());
  1692. }
  1693. TYPED_TEST(HashtableAllTest, BucketCount)
  1694. {
  1695. TypeParam ht(100);
  1696. // constructor arg is number of *items* to be inserted, not the
  1697. // number of buckets, so we expect more buckets.
  1698. EXPECT_GT(ht.bucket_count(), 100u);
  1699. for (int i = 1; i < 200; i++) {
  1700. ht.insert(this->UniqueObject(i));
  1701. }
  1702. EXPECT_GT(ht.bucket_count(), 200u);
  1703. }
  1704. TYPED_TEST(HashtableAllTest, BucketAndBucketSize)
  1705. {
  1706. const typename TypeParam::size_type expected_bucknum = this->ht_.bucket(
  1707. this->UniqueKey(1));
  1708. EXPECT_EQ(0u, this->ht_.bucket_size(expected_bucknum));
  1709. this->ht_.insert(this->UniqueObject(1));
  1710. EXPECT_EQ(expected_bucknum, this->ht_.bucket(this->UniqueKey(1)));
  1711. EXPECT_EQ(1u, this->ht_.bucket_size(expected_bucknum));
  1712. // Check that a bucket we didn't insert into, has a 0 size. Since
  1713. // we have an even number of buckets, bucknum^1 is guaranteed in range.
  1714. EXPECT_EQ(0u, this->ht_.bucket_size(expected_bucknum ^ 1));
  1715. }
  1716. TYPED_TEST(HashtableAllTest, LoadFactor)
  1717. {
  1718. const typename TypeParam::size_type kSize = 16536;
  1719. // Check growing past various thresholds and then shrinking below
  1720. // them.
  1721. for (float grow_threshold = 0.2f;
  1722. grow_threshold <= 0.8f;
  1723. grow_threshold += 0.2f)
  1724. {
  1725. TypeParam ht;
  1726. ht.set_deleted_key(this->UniqueKey(1));
  1727. ht.max_load_factor(grow_threshold);
  1728. ht.min_load_factor(0.0);
  1729. EXPECT_EQ(grow_threshold, ht.max_load_factor());
  1730. EXPECT_EQ(0.0, ht.min_load_factor());
  1731. ht.resize(kSize);
  1732. size_t bucket_count = ht.bucket_count();
  1733. // Erase and insert an element to set consider_shrink = true,
  1734. // which should not cause a shrink because the threshold is 0.0.
  1735. ht.insert(this->UniqueObject(2));
  1736. ht.erase(this->UniqueKey(2));
  1737. for (int i = 2;; ++i)
  1738. {
  1739. ht.insert(this->UniqueObject(i));
  1740. if (static_cast<float>(ht.size())/bucket_count < grow_threshold) {
  1741. EXPECT_EQ(bucket_count, ht.bucket_count());
  1742. } else {
  1743. EXPECT_GT(ht.bucket_count(), bucket_count);
  1744. break;
  1745. }
  1746. }
  1747. // Now set a shrink threshold 1% below the current size and remove
  1748. // items until the size falls below that.
  1749. const float shrink_threshold = static_cast<float>(ht.size()) /
  1750. ht.bucket_count() - 0.01f;
  1751. // This time around, check the old set_resizing_parameters interface.
  1752. ht.set_resizing_parameters(shrink_threshold, 1.0);
  1753. EXPECT_EQ(1.0, ht.max_load_factor());
  1754. EXPECT_EQ(shrink_threshold, ht.min_load_factor());
  1755. bucket_count = ht.bucket_count();
  1756. for (int i = 2;; ++i)
  1757. {
  1758. ht.erase(this->UniqueKey(i));
  1759. // A resize is only triggered by an insert, so add and remove a
  1760. // value every iteration to trigger the shrink as soon as the
  1761. // threshold is passed.
  1762. ht.erase(this->UniqueKey(i+1));
  1763. ht.insert(this->UniqueObject(i+1));
  1764. if (static_cast<float>(ht.size())/bucket_count > shrink_threshold) {
  1765. EXPECT_EQ(bucket_count, ht.bucket_count());
  1766. } else {
  1767. EXPECT_LT(ht.bucket_count(), bucket_count);
  1768. break;
  1769. }
  1770. }
  1771. }
  1772. }
  1773. TYPED_TEST(HashtableAllTest, ResizeAndRehash)
  1774. {
  1775. // resize() and rehash() are synonyms. rehash() is the tr1 name.
  1776. TypeParam ht(10000);
  1777. ht.max_load_factor(0.8f); // for consistency's sake
  1778. for (int i = 1; i < 100; ++i)
  1779. ht.insert(this->UniqueObject(i));
  1780. ht.resize(0);
  1781. // Now ht should be as small as possible.
  1782. EXPECT_LT(ht.bucket_count(), 300u);
  1783. ht.rehash(9000); // use the 'rehash' version of the name.
  1784. // Bucket count should be next power of 2, after considering max_load_factor.
  1785. EXPECT_EQ(16384u, ht.bucket_count());
  1786. for (int i = 101; i < 200; ++i)
  1787. ht.insert(this->UniqueObject(i));
  1788. // Adding a few hundred buckets shouldn't have caused a resize yet.
  1789. EXPECT_EQ(ht.bucket_count(), 16384u);
  1790. }
  1791. TYPED_TEST(HashtableAllTest, FindAndCountAndEqualRange)
  1792. {
  1793. pair<typename TypeParam::iterator, typename TypeParam::iterator> eq_pair;
  1794. pair<typename TypeParam::const_iterator,
  1795. typename TypeParam::const_iterator> const_eq_pair;
  1796. EXPECT_TRUE(this->ht_.empty());
  1797. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) == this->ht_.end());
  1798. EXPECT_EQ(0u, 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. this->ht_.insert(this->UniqueObject(1));
  1802. EXPECT_FALSE(this->ht_.empty());
  1803. this->ht_.insert(this->UniqueObject(11));
  1804. this->ht_.insert(this->UniqueObject(111));
  1805. this->ht_.insert(this->UniqueObject(1111));
  1806. this->ht_.insert(this->UniqueObject(11111));
  1807. this->ht_.insert(this->UniqueObject(111111));
  1808. this->ht_.insert(this->UniqueObject(1111111));
  1809. this->ht_.insert(this->UniqueObject(11111111));
  1810. this->ht_.insert(this->UniqueObject(111111111));
  1811. EXPECT_EQ(9u, this->ht_.size());
  1812. typename TypeParam::const_iterator it = this->ht_.find(this->UniqueKey(1));
  1813. EXPECT_EQ(it.key(), this->UniqueKey(1));
  1814. // Allow testing the const version of the methods as well.
  1815. const TypeParam ht = this->ht_;
  1816. // Some successful lookups (via find, count, and equal_range).
  1817. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) != this->ht_.end());
  1818. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1)));
  1819. eq_pair = this->ht_.equal_range(this->UniqueKey(1));
  1820. EXPECT_TRUE(eq_pair.first != eq_pair.second);
  1821. EXPECT_EQ(eq_pair.first.key(), this->UniqueKey(1));
  1822. ++eq_pair.first;
  1823. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1824. EXPECT_TRUE(ht.find(this->UniqueKey(1)) != ht.end());
  1825. EXPECT_EQ(1u, ht.count(this->UniqueKey(1)));
  1826. const_eq_pair = ht.equal_range(this->UniqueKey(1));
  1827. EXPECT_TRUE(const_eq_pair.first != const_eq_pair.second);
  1828. EXPECT_EQ(const_eq_pair.first.key(), this->UniqueKey(1));
  1829. ++const_eq_pair.first;
  1830. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1831. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11111)) != this->ht_.end());
  1832. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(11111)));
  1833. eq_pair = this->ht_.equal_range(this->UniqueKey(11111));
  1834. EXPECT_TRUE(eq_pair.first != eq_pair.second);
  1835. EXPECT_EQ(eq_pair.first.key(), this->UniqueKey(11111));
  1836. ++eq_pair.first;
  1837. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1838. EXPECT_TRUE(ht.find(this->UniqueKey(11111)) != ht.end());
  1839. EXPECT_EQ(1u, ht.count(this->UniqueKey(11111)));
  1840. const_eq_pair = ht.equal_range(this->UniqueKey(11111));
  1841. EXPECT_TRUE(const_eq_pair.first != const_eq_pair.second);
  1842. EXPECT_EQ(const_eq_pair.first.key(), this->UniqueKey(11111));
  1843. ++const_eq_pair.first;
  1844. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1845. // Some unsuccessful lookups (via find, count, and equal_range).
  1846. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11112)) == this->ht_.end());
  1847. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(11112)));
  1848. eq_pair = this->ht_.equal_range(this->UniqueKey(11112));
  1849. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1850. EXPECT_TRUE(ht.find(this->UniqueKey(11112)) == ht.end());
  1851. EXPECT_EQ(0u, ht.count(this->UniqueKey(11112)));
  1852. const_eq_pair = ht.equal_range(this->UniqueKey(11112));
  1853. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1854. EXPECT_TRUE(this->ht_.find(this->UniqueKey(11110)) == this->ht_.end());
  1855. EXPECT_EQ(0u, this->ht_.count(this->UniqueKey(11110)));
  1856. eq_pair = this->ht_.equal_range(this->UniqueKey(11110));
  1857. EXPECT_TRUE(eq_pair.first == eq_pair.second);
  1858. EXPECT_TRUE(ht.find(this->UniqueKey(11110)) == ht.end());
  1859. EXPECT_EQ(0u, ht.count(this->UniqueKey(11110)));
  1860. const_eq_pair = ht.equal_range(this->UniqueKey(11110));
  1861. EXPECT_TRUE(const_eq_pair.first == const_eq_pair.second);
  1862. }
  1863. TYPED_TEST(HashtableAllTest, BracketInsert)
  1864. {
  1865. // tests operator[], for those types that support it.
  1866. if (!this->ht_.supports_brackets())
  1867. return;
  1868. // bracket_equal is equivalent to ht_[a] == b. It should insert a if
  1869. // it doesn't already exist.
  1870. EXPECT_TRUE(this->ht_.bracket_equal(this->UniqueKey(1),
  1871. this->ht_.default_data()));
  1872. EXPECT_TRUE(this->ht_.find(this->UniqueKey(1)) != this->ht_.end());
  1873. // bracket_assign is equivalent to ht_[a] = b.
  1874. this->ht_.bracket_assign(this->UniqueKey(2),
  1875. this->ht_.get_data(this->UniqueObject(4)));
  1876. EXPECT_TRUE(this->ht_.find(this->UniqueKey(2)) != this->ht_.end());
  1877. EXPECT_TRUE(this->ht_.bracket_equal(
  1878. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(4))));
  1879. this->ht_.bracket_assign(
  1880. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6)));
  1881. EXPECT_TRUE(this->ht_.bracket_equal(
  1882. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6))));
  1883. // bracket_equal shouldn't have modified the value.
  1884. EXPECT_TRUE(this->ht_.bracket_equal(
  1885. this->UniqueKey(2), this->ht_.get_data(this->UniqueObject(6))));
  1886. // Verify that an operator[] that doesn't cause a resize, also
  1887. // doesn't require an extra rehash.
  1888. TypeParam ht(100);
  1889. EXPECT_EQ(0, ht.hash_funct().num_hashes());
  1890. ht.bracket_assign(this->UniqueKey(2), ht.get_data(this->UniqueObject(2)));
  1891. EXPECT_EQ(1, ht.hash_funct().num_hashes());
  1892. // And overwriting, likewise, should only cause one extra hash.
  1893. ht.bracket_assign(this->UniqueKey(2), ht.get_data(this->UniqueObject(2)));
  1894. EXPECT_EQ(2, ht.hash_funct().num_hashes());
  1895. }
  1896. TYPED_TEST(HashtableAllTest, InsertValue)
  1897. {
  1898. // First, try some straightforward insertions.
  1899. EXPECT_TRUE(this->ht_.empty());
  1900. this->ht_.insert(this->UniqueObject(1));
  1901. EXPECT_FALSE(this->ht_.empty());
  1902. this->ht_.insert(this->UniqueObject(11));
  1903. this->ht_.insert(this->UniqueObject(111));
  1904. this->ht_.insert(this->UniqueObject(1111));
  1905. this->ht_.insert(this->UniqueObject(11111));
  1906. this->ht_.insert(this->UniqueObject(111111));
  1907. this->ht_.insert(this->UniqueObject(1111111));
  1908. this->ht_.insert(this->UniqueObject(11111111));
  1909. this->ht_.insert(this->UniqueObject(111111111));
  1910. EXPECT_EQ(9u, this->ht_.size());
  1911. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1)));
  1912. EXPECT_EQ(1u, this->ht_.count(this->UniqueKey(1111)));
  1913. // Check the return type.
  1914. pair<typename TypeParam::iterator, bool> insert_it;
  1915. insert_it = this->ht_.insert(this->UniqueObject(1));
  1916. EXPECT_EQ(false, insert_it.second); // false: already present
  1917. EXPECT_TRUE(*insert_it.first == this->UniqueObject(1));
  1918. insert_it = this->ht_.insert(this->UniqueObject(2));
  1919. EXPECT_EQ(true, insert_it.second); // true: not already present
  1920. EXPECT_TRUE(*insert_it.first == this->UniqueObject(2));
  1921. }
  1922. TYPED_TEST(HashtableIntTest, InsertRange)
  1923. {
  1924. // We just test the ints here, to make the placement-new easier.
  1925. TypeParam ht_source;
  1926. ht_source.insert(this->UniqueObject(10));
  1927. ht_source.insert(this->UniqueObject(100));
  1928. ht_source.insert(this->UniqueObject(1000));
  1929. ht_source.insert(this->UniqueObject(10000));
  1930. ht_source.insert(this->UniqueObject(100000));
  1931. ht_source.insert(this->UniqueObject(1000000));
  1932. const typename TypeParam::value_type input[] = {
  1933. // This is a copy of the first element in ht_source.
  1934. *ht_source.begin(),
  1935. this->UniqueObject(2),
  1936. this->UniqueObject(4),
  1937. this->UniqueObject(8)
  1938. };
  1939. set<typename TypeParam::value_type> set_input;
  1940. set_input.insert(this->UniqueObject(1111111));
  1941. set_input.insert(this->UniqueObject(111111));
  1942. set_input.insert(this->UniqueObject(11111));
  1943. set_input.insert(this->UniqueObject(1111));
  1944. set_input.insert(this->UniqueObject(111));
  1945. set_input.insert(this->UniqueObject(11));
  1946. // Insert from ht_source, an iterator of the same type as us.
  1947. typename TypeParam::const_iterator begin = ht_source.begin();
  1948. typename TypeParam::const_iterator end = begin;
  1949. std::advance(end, 3);
  1950. this->ht_.insert(begin, end); // insert 3 elements from ht_source
  1951. EXPECT_EQ(3u, this->ht_.size());
  1952. EXPECT_TRUE(*this->ht_.begin() == this->UniqueObject(10) ||
  1953. *this->ht_.begin() == this->UniqueObject(100) ||
  1954. *this->ht_.begin() == this->UniqueObject(1000) ||
  1955. *this->ht_.begin() == this->UniqueObject(10000) ||
  1956. *this->ht_.begin() == this->UniqueObject(100000) ||
  1957. *this->ht_.begin() == this->UniqueObject(1000000));
  1958. // And insert from set_input, a separate, non-random-access iterator.
  1959. typename set<typename TypeParam::value_type>::const_iterator set_begin;
  1960. typename set<typename TypeParam::value_type>::const_iterator set_end;
  1961. set_begin = set_input.begin();
  1962. set_end = set_begin;
  1963. std::advance(set_end, 3);
  1964. this->ht_.insert(set_begin, set_end);
  1965. EXPECT_EQ(6u, this->ht_.size());
  1966. // Insert from input as well, a separate, random-access iterator.
  1967. // The first element of input overlaps with an existing element
  1968. // of ht_, so this should only up the size by 2.
  1969. this->ht_.insert(&input[0], &input[3]);
  1970. EXPECT_EQ(8u, this->ht_.size());
  1971. }
  1972. TEST(HashtableTest, InsertValueToMap)
  1973. {
  1974. // For the maps in particular, ensure that inserting doesn't change
  1975. // the value.
  1976. sparse_hash_map<int, int> shm;
  1977. pair<sparse_hash_map<int,int>::iterator, bool> shm_it;
  1978. shm[1] = 2; // test a different method of inserting
  1979. shm_it = shm.insert(pair<int, int>(1, 3));
  1980. EXPECT_EQ(false, shm_it.second);
  1981. EXPECT_EQ(1, shm_it.first->first);
  1982. EXPECT_EQ(2, shm_it.first->second);
  1983. shm_it.first->second = 20;
  1984. EXPECT_EQ(20, shm[1]);
  1985. shm_it = shm.insert(pair<int, int>(2, 4));
  1986. EXPECT_EQ(true, shm_it.second);
  1987. EXPECT_EQ(2, shm_it.first->first);
  1988. EXPECT_EQ(4, shm_it.first->second);
  1989. EXPECT_EQ(4, shm[2]);
  1990. }
  1991. TYPED_TEST(HashtableStringTest, EmptyKey)
  1992. {
  1993. // Only run the string tests, to make it easier to know what the
  1994. // empty key should be.
  1995. if (!this->ht_.supports_empty_key())
  1996. return;
  1997. EXPECT_EQ(kEmptyString, this->ht_.empty_key());
  1998. }
  1999. TYPED_TEST(HashtableAllTest, Erase)
  2000. {
  2001. this->ht_.set_deleted_key(this->UniqueKey(1));
  2002. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(20)));
  2003. this->ht_.insert(this->UniqueObject(10));
  2004. this->ht_.insert(this->UniqueObject(20));
  2005. EXPECT_EQ(1u, this->ht_.erase(this->UniqueKey(20)));
  2006. EXPECT_EQ(1u, this->ht_.size());
  2007. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(20)));
  2008. EXPECT_EQ(1u, this->ht_.size());
  2009. EXPECT_EQ(0u, this->ht_.erase(this->UniqueKey(19)));
  2010. EXPECT_EQ(1u, this->ht_.size());
  2011. typename TypeParam::iterator it = this->ht_.find(this->UniqueKey(10));
  2012. EXPECT_TRUE(it != this->ht_.end());
  2013. this->ht_.erase(it);
  2014. EXPECT_EQ(0u, this->ht_.size());
  2015. for (int i = 10; i < 100; i++)
  2016. this->ht_.insert(this->UniqueObject(i));
  2017. EXPECT_EQ(90u, this->ht_.size());
  2018. this->ht_.erase(this->ht_.begin(), this->ht_.end());
  2019. EXPECT_EQ(0u, this->ht_.size());
  2020. }
  2021. TYPED_TEST(HashtableAllTest, EraseDoesNotResize)
  2022. {
  2023. this->ht_.set_deleted_key(this->UniqueKey(1));
  2024. for (int i = 10; i < 2000; i++) {
  2025. this->ht_.insert(this->UniqueObject(i));
  2026. }
  2027. const typename TypeParam::size_type old_count = this->ht_.bucket_count();
  2028. for (int i = 10; i < 1000; i++) { // erase half one at a time
  2029. EXPECT_EQ(1u, this->ht_.erase(this->UniqueKey(i)));
  2030. }
  2031. this->ht_.erase(this->ht_.begin(), this->ht_.end()); // and the rest at once
  2032. EXPECT_EQ(0u, this->ht_.size());
  2033. EXPECT_EQ(old_count, this->ht_.bucket_count());
  2034. }
  2035. TYPED_TEST(HashtableAllTest, Equals)
  2036. {
  2037. // The real test here is whether two hashtables are equal if they
  2038. // have the same items but in a different order.
  2039. TypeParam ht1;
  2040. TypeParam ht2;
  2041. EXPECT_TRUE(ht1 == ht1);
  2042. EXPECT_FALSE(ht1 != ht1);
  2043. EXPECT_TRUE(ht1 == ht2);
  2044. EXPECT_FALSE(ht1 != ht2);
  2045. ht1.set_deleted_key(this->UniqueKey(1));
  2046. // Only the contents affect equality, not things like deleted-key.
  2047. EXPECT_TRUE(ht1 == ht2);
  2048. EXPECT_FALSE(ht1 != ht2);
  2049. ht1.resize(2000);
  2050. EXPECT_TRUE(ht1 == ht2);
  2051. // The choice of allocator/etc doesn't matter either.
  2052. Hasher hasher(1);
  2053. Alloc<typename TypeParam::value_type> alloc(2, NULL);
  2054. TypeParam ht3(5, hasher, hasher, alloc);
  2055. EXPECT_TRUE(ht1 == ht3);
  2056. EXPECT_FALSE(ht1 != ht3);
  2057. ht1.insert(this->UniqueObject(2));
  2058. EXPECT_TRUE(ht1 != ht2);
  2059. EXPECT_FALSE(ht1 == ht2); // this should hold as well!
  2060. ht2.insert(this->UniqueObject(2));
  2061. EXPECT_TRUE(ht1 == ht2);
  2062. for (int i = 3; i <= 2000; i++) {
  2063. ht1.insert(this->UniqueObject(i));
  2064. }
  2065. for (int i = 2000; i >= 3; i--) {
  2066. ht2.insert(this->UniqueObject(i));
  2067. }
  2068. EXPECT_TRUE(ht1 == ht2);
  2069. }
  2070. TEST(HashtableTest, IntIO)
  2071. {
  2072. // Since the set case is just a special (easier) case than the map case, I
  2073. // just test on sparse_hash_map. This handles the easy case where we can
  2074. // use the standard reader and writer.
  2075. sparse_hash_map<int, int> ht_out;
  2076. ht_out.set_deleted_key(0);
  2077. for (int i = 1; i < 1000; i++) {
  2078. ht_out[i] = i * i;
  2079. }
  2080. ht_out.erase(563); // just to test having some erased keys when we write.
  2081. ht_out.erase(22);
  2082. string file(TmpFile("intio"));
  2083. FILE* fp = fopen(file.c_str(), "wb");
  2084. if (fp)
  2085. {
  2086. EXPECT_TRUE(fp != NULL);
  2087. EXPECT_TRUE(ht_out.write_metadata(fp));
  2088. EXPECT_TRUE(ht_out.write_nopointer_data(fp));
  2089. fclose(fp);
  2090. }
  2091. sparse_hash_map<int, int> ht_in;
  2092. fp = fopen(file.c_str(), "rb");
  2093. if (fp)
  2094. {
  2095. EXPECT_TRUE(fp != NULL);
  2096. EXPECT_TRUE(ht_in.read_metadata(fp));
  2097. EXPECT_TRUE(ht_in.read_nopointer_data(fp));
  2098. fclose(fp);
  2099. }
  2100. EXPECT_EQ(1, ht_in[1]);
  2101. EXPECT_EQ(998001, ht_in[999]);
  2102. EXPECT_EQ(100, ht_in[10]);
  2103. EXPECT_EQ(441, ht_in[21]);
  2104. EXPECT_EQ(0, ht_in[22]); // should not have been saved
  2105. EXPECT_EQ(0, ht_in[563]);
  2106. }
  2107. TEST(HashtableTest, StringIO)
  2108. {
  2109. // Since the set case is just a special (easier) case than the map case,
  2110. // I just test on sparse_hash_map. This handles the difficult case where
  2111. // we have to write our own custom reader/writer for the data.
  2112. typedef sparse_hash_map<string, string, Hasher, Hasher> SP;
  2113. SP ht_out;
  2114. ht_out.set_deleted_key(string(""));
  2115. for (int i = 32; i < 128; i++) {
  2116. // This maps 'a' to 32 a's, 'b' to 33 b's, etc.
  2117. ht_out[string(1, (char)i)] = string((size_t)i, (char)i);
  2118. }
  2119. ht_out.erase("c"); // just to test having some erased keys when we write.
  2120. ht_out.erase("y");
  2121. string file(TmpFile("stringio"));
  2122. FILE* fp = fopen(file.c_str(), "wb");
  2123. if (fp)
  2124. {
  2125. EXPECT_TRUE(fp != NULL);
  2126. EXPECT_TRUE(ht_out.write_metadata(fp));
  2127. for (SP::const_iterator it = ht_out.cbegin(); it != ht_out.cend(); ++it)
  2128. {
  2129. const string::size_type first_size = it->first.length();
  2130. fwrite(&first_size, sizeof(first_size), 1, fp); // ignore endianness issues
  2131. fwrite(it->first.c_str(), first_size, 1, fp);
  2132. const string::size_type second_size = it->second.length();
  2133. fwrite(&second_size, sizeof(second_size), 1, fp);
  2134. fwrite(it->second.c_str(), second_size, 1, fp);
  2135. }
  2136. fclose(fp);
  2137. }
  2138. sparse_hash_map<string, string, Hasher, Hasher> ht_in;
  2139. fp = fopen(file.c_str(), "rb");
  2140. if (fp)
  2141. {
  2142. EXPECT_TRUE(fp != NULL);
  2143. EXPECT_TRUE(ht_in.read_metadata(fp));
  2144. for (sparse_hash_map<string, string, Hasher, Hasher>::iterator
  2145. it = ht_in.begin(); it != ht_in.end(); ++it) {
  2146. string::size_type first_size;
  2147. EXPECT_EQ(1u, fread(&first_size, sizeof(first_size), 1, fp));
  2148. char* first = new char[first_size];
  2149. EXPECT_EQ(1u, fread(first, first_size, 1, fp));
  2150. string::size_type second_size;
  2151. EXPECT_EQ(1u, fread(&second_size, sizeof(second_size), 1, fp));
  2152. char* second = new char[second_size];
  2153. EXPECT_EQ(1u, fread(second, second_size, 1, fp));
  2154. // it points to garbage, so we have to use placement-new to initialize.
  2155. // We also have to use const-cast since it->first is const.
  2156. new(const_cast<string*>(&it->first)) string(first, first_size);
  2157. new(&it->second) string(second, second_size);
  2158. delete[] first;
  2159. delete[] second;
  2160. }
  2161. fclose(fp);
  2162. }
  2163. EXPECT_EQ(string(" "), ht_in[" "]);
  2164. EXPECT_EQ(string("+++++++++++++++++++++++++++++++++++++++++++"), ht_in["+"]);
  2165. EXPECT_EQ(string(""), ht_in["c"]); // should not have been saved
  2166. EXPECT_EQ(string(""), ht_in["y"]);
  2167. }
  2168. TYPED_TEST(HashtableAllTest, Serialization)
  2169. {
  2170. if (!this->ht_.supports_serialization()) return;
  2171. TypeParam ht_out;
  2172. ht_out.set_deleted_key(this->UniqueKey(2000));
  2173. for (int i = 1; i < 100; i++) {
  2174. ht_out.insert(this->UniqueObject(i));
  2175. }
  2176. // just to test having some erased keys when we write.
  2177. ht_out.erase(this->UniqueKey(56));
  2178. ht_out.erase(this->UniqueKey(22));
  2179. string file(TmpFile("serialization"));
  2180. FILE* fp = fopen(file.c_str(), "wb");
  2181. if (fp)
  2182. {
  2183. EXPECT_TRUE(fp != NULL);
  2184. EXPECT_TRUE(ht_out.serialize(ValueSerializer(), fp));
  2185. fclose(fp);
  2186. }
  2187. TypeParam ht_in;
  2188. fp = fopen(file.c_str(), "rb");
  2189. if (fp)
  2190. {
  2191. EXPECT_TRUE(fp != NULL);
  2192. EXPECT_TRUE(ht_in.unserialize(ValueSerializer(), fp));
  2193. fclose(fp);
  2194. }
  2195. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2196. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2197. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2198. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2199. // should not have been saved
  2200. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2201. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2202. }
  2203. TYPED_TEST(HashtableIntTest, NopointerSerialization)
  2204. {
  2205. if (!this->ht_.supports_serialization()) return;
  2206. TypeParam ht_out;
  2207. ht_out.set_deleted_key(this->UniqueKey(2000));
  2208. for (int i = 1; i < 100; i++) {
  2209. ht_out.insert(this->UniqueObject(i));
  2210. }
  2211. // just to test having some erased keys when we write.
  2212. ht_out.erase(this->UniqueKey(56));
  2213. ht_out.erase(this->UniqueKey(22));
  2214. string file(TmpFile("nopointer_serialization"));
  2215. FILE* fp = fopen(file.c_str(), "wb");
  2216. if (fp)
  2217. {
  2218. EXPECT_TRUE(fp != NULL);
  2219. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(), fp));
  2220. fclose(fp);
  2221. }
  2222. TypeParam ht_in;
  2223. fp = fopen(file.c_str(), "rb");
  2224. if (fp)
  2225. {
  2226. EXPECT_TRUE(fp != NULL);
  2227. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(), fp));
  2228. fclose(fp);
  2229. }
  2230. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2231. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2232. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2233. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2234. // should not have been saved
  2235. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2236. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2237. }
  2238. // We don't support serializing to a string by default, but you can do
  2239. // it by writing your own custom input/output class.
  2240. class StringIO {
  2241. public:
  2242. explicit StringIO(string* s) : s_(s) {}
  2243. size_t Write(const void* buf, size_t len) {
  2244. s_->append(reinterpret_cast<const char*>(buf), len);
  2245. return len;
  2246. }
  2247. size_t Read(void* buf, size_t len) {
  2248. if (s_->length() < len)
  2249. len = s_->length();
  2250. memcpy(reinterpret_cast<char*>(buf), s_->data(), len);
  2251. s_->erase(0, len);
  2252. return len;
  2253. }
  2254. private:
  2255. StringIO& operator=(const StringIO&);
  2256. string* const s_;
  2257. };
  2258. TYPED_TEST(HashtableIntTest, SerializingToString)
  2259. {
  2260. if (!this->ht_.supports_serialization()) return;
  2261. TypeParam ht_out;
  2262. ht_out.set_deleted_key(this->UniqueKey(2000));
  2263. for (int i = 1; i < 100; i++) {
  2264. ht_out.insert(this->UniqueObject(i));
  2265. }
  2266. // just to test having some erased keys when we write.
  2267. ht_out.erase(this->UniqueKey(56));
  2268. ht_out.erase(this->UniqueKey(22));
  2269. string stringbuf;
  2270. StringIO stringio(&stringbuf);
  2271. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2272. &stringio));
  2273. TypeParam ht_in;
  2274. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(),
  2275. &stringio));
  2276. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2277. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2278. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2279. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2280. // should not have been saved
  2281. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2282. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2283. }
  2284. // An easier way to do the above would be to use the existing stream methods.
  2285. TYPED_TEST(HashtableIntTest, SerializingToStringStream)
  2286. {
  2287. if (!this->ht_.supports_serialization()) return;
  2288. TypeParam ht_out;
  2289. ht_out.set_deleted_key(this->UniqueKey(2000));
  2290. for (int i = 1; i < 100; i++) {
  2291. ht_out.insert(this->UniqueObject(i));
  2292. }
  2293. // just to test having some erased keys when we write.
  2294. ht_out.erase(this->UniqueKey(56));
  2295. ht_out.erase(this->UniqueKey(22));
  2296. std::stringstream string_buffer;
  2297. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2298. &string_buffer));
  2299. TypeParam ht_in;
  2300. EXPECT_TRUE(ht_in.unserialize(typename TypeParam::NopointerSerializer(),
  2301. &string_buffer));
  2302. EXPECT_EQ(this->UniqueObject(1), *ht_in.find(this->UniqueKey(1)));
  2303. EXPECT_EQ(this->UniqueObject(99), *ht_in.find(this->UniqueKey(99)));
  2304. EXPECT_FALSE(ht_in.count(this->UniqueKey(100)));
  2305. EXPECT_EQ(this->UniqueObject(21), *ht_in.find(this->UniqueKey(21)));
  2306. // should not have been saved
  2307. EXPECT_FALSE(ht_in.count(this->UniqueKey(22)));
  2308. EXPECT_FALSE(ht_in.count(this->UniqueKey(56)));
  2309. }
  2310. // Verify that the metadata serialization is endianness and word size
  2311. // agnostic.
  2312. TYPED_TEST(HashtableAllTest, MetadataSerializationAndEndianness)
  2313. {
  2314. TypeParam ht_out;
  2315. string kExpectedDense("\x13W\x86""B\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0",
  2316. 24);
  2317. // GP change - switched size from 20 to formula, because the sparsegroup bitmap is 4 or 8 bytes and not 6
  2318. string kExpectedSparse("$hu1\0\0\0 \0\0\0\0\0\0\0\0\0\0\0", 12 + sizeof(group_bm_type));
  2319. if (ht_out.supports_readwrite()) {
  2320. size_t num_bytes = 0;
  2321. string file(TmpFile("metadata_serialization"));
  2322. FILE* fp = fopen(file.c_str(), "wb");
  2323. if (fp)
  2324. {
  2325. EXPECT_TRUE(fp != NULL);
  2326. EXPECT_TRUE(ht_out.write_metadata(fp));
  2327. EXPECT_TRUE(ht_out.write_nopointer_data(fp));
  2328. num_bytes = (const size_t)ftell(fp);
  2329. fclose(fp);
  2330. }
  2331. char contents[24] = {0};
  2332. fp = fopen(file.c_str(), "rb");
  2333. if (fp)
  2334. {
  2335. EXPECT_LE(num_bytes, static_cast<size_t>(24));
  2336. EXPECT_EQ(num_bytes, fread(contents, 1, num_bytes <= 24 ? num_bytes : 24, fp));
  2337. EXPECT_EQ(EOF, fgetc(fp)); // check we're *exactly* the right size
  2338. fclose(fp);
  2339. }
  2340. // TODO(csilvers): check type of ht_out instead of looking at the 1st byte.
  2341. if (contents[0] == kExpectedDense[0]) {
  2342. EXPECT_EQ(kExpectedDense, string(contents, num_bytes));
  2343. } else {
  2344. EXPECT_EQ(kExpectedSparse, string(contents, num_bytes));
  2345. }
  2346. }
  2347. // Do it again with new-style serialization. Here we can use StringIO.
  2348. if (ht_out.supports_serialization()) {
  2349. string stringbuf;
  2350. StringIO stringio(&stringbuf);
  2351. EXPECT_TRUE(ht_out.serialize(typename TypeParam::NopointerSerializer(),
  2352. &stringio));
  2353. if (stringbuf[0] == kExpectedDense[0]) {
  2354. EXPECT_EQ(kExpectedDense, stringbuf);
  2355. } else {
  2356. EXPECT_EQ(kExpectedSparse, stringbuf);
  2357. }
  2358. }
  2359. }
  2360. // ------------------------------------------------------------------------
  2361. // The above tests test the general API for correctness. These tests
  2362. // test a few corner cases that have tripped us up in the past, and
  2363. // more general, cross-API issues like memory management.
  2364. TYPED_TEST(HashtableAllTest, BracketOperatorCrashing)
  2365. {
  2366. this->ht_.set_deleted_key(this->UniqueKey(1));
  2367. for (int iters = 0; iters < 10; iters++) {
  2368. // We start at 33 because after shrinking, we'll be at 32 buckets.
  2369. for (int i = 33; i < 133; i++) {
  2370. this->ht_.bracket_assign(this->UniqueKey(i),
  2371. this->ht_.get_data(this->UniqueObject(i)));
  2372. }
  2373. this->ht_.clear_no_resize();
  2374. // This will force a shrink on the next insert, which we want to test.
  2375. this->ht_.bracket_assign(this->UniqueKey(2),
  2376. this->ht_.get_data(this->UniqueObject(2)));
  2377. this->ht_.erase(this->UniqueKey(2));
  2378. }
  2379. }
  2380. // For data types with trivial copy-constructors and destructors, we
  2381. // should use an optimized routine for data-copying, that involves
  2382. // memmove. We test this by keeping count of how many times the
  2383. // copy-constructor is called; it should be much less with the
  2384. // optimized code.
  2385. struct Memmove
  2386. {
  2387. public:
  2388. Memmove(): i(0) {}
  2389. explicit Memmove(int ival): i(ival) {}
  2390. Memmove(const Memmove& that) { this->i = that.i; num_copies++; }
  2391. int i;
  2392. static int num_copies;
  2393. };
  2394. int Memmove::num_copies = 0;
  2395. struct NoMemmove
  2396. {
  2397. public:
  2398. NoMemmove(): i(0) {}
  2399. explicit NoMemmove(int ival): i(ival) {}
  2400. NoMemmove(const NoMemmove& that) { this->i = that.i; num_copies++; }
  2401. int i;
  2402. static int num_copies;
  2403. };
  2404. int NoMemmove::num_copies = 0;
  2405. } // unnamed namespace
  2406. #if 0
  2407. // This is what tells the hashtable code it can use memmove for this class:
  2408. namespace google {
  2409. template<> struct has_trivial_copy<Memmove> : true_type { };
  2410. template<> struct has_trivial_destructor<Memmove> : true_type { };
  2411. };
  2412. #endif
  2413. namespace
  2414. {
  2415. TEST(HashtableTest, SimpleDataTypeOptimizations)
  2416. {
  2417. // Only sparsehashtable optimizes moves in this way.
  2418. sparse_hash_map<int, Memmove, Hasher, Hasher> memmove;
  2419. sparse_hash_map<int, NoMemmove, Hasher, Hasher> nomemmove;
  2420. sparse_hash_map<int, Memmove, Hasher, Hasher, Alloc<std::pair<const int, Memmove> > >
  2421. memmove_nonstandard_alloc;
  2422. Memmove::num_copies = 0;
  2423. for (int i = 10000; i > 0; i--) {
  2424. memmove[i] = Memmove(i);
  2425. }
  2426. // GP change - const int memmove_copies = Memmove::num_copies;
  2427. NoMemmove::num_copies = 0;
  2428. for (int i = 10000; i > 0; i--) {
  2429. nomemmove[i] = NoMemmove(i);
  2430. }
  2431. // GP change - const int nomemmove_copies = NoMemmove::num_copies;
  2432. Memmove::num_copies = 0;
  2433. for (int i = 10000; i > 0; i--) {
  2434. memmove_nonstandard_alloc[i] = Memmove(i);
  2435. }
  2436. // GP change - const int memmove_nonstandard_alloc_copies = Memmove::num_copies;
  2437. // GP change - commented out following two lines
  2438. //EXPECT_GT(nomemmove_copies, memmove_copies);
  2439. //EXPECT_EQ(nomemmove_copies, memmove_nonstandard_alloc_copies);
  2440. }
  2441. TYPED_TEST(HashtableAllTest, ResizeHysteresis)
  2442. {
  2443. // We want to make sure that when we create a hashtable, and then
  2444. // add and delete one element, the size of the hashtable doesn't
  2445. // change.
  2446. this->ht_.set_deleted_key(this->UniqueKey(1));
  2447. typename TypeParam::size_type old_bucket_count = this->ht_.bucket_count();
  2448. this->ht_.insert(this->UniqueObject(4));
  2449. this->ht_.erase(this->UniqueKey(4));
  2450. this->ht_.insert(this->UniqueObject(4));
  2451. this->ht_.erase(this->UniqueKey(4));
  2452. EXPECT_EQ(old_bucket_count, this->ht_.bucket_count());
  2453. // Try it again, but with a hashtable that starts very small
  2454. TypeParam ht(2);
  2455. EXPECT_LT(ht.bucket_count(), 32u); // verify we really do start small
  2456. ht.set_deleted_key(this->UniqueKey(1));
  2457. old_bucket_count = ht.bucket_count();
  2458. ht.insert(this->UniqueObject(4));
  2459. ht.erase(this->UniqueKey(4));
  2460. ht.insert(this->UniqueObject(4));
  2461. ht.erase(this->UniqueKey(4));
  2462. EXPECT_EQ(old_bucket_count, ht.bucket_count());
  2463. }
  2464. TEST(HashtableTest, ConstKey)
  2465. {
  2466. // Sometimes people write hash_map<const int, int>, even though the
  2467. // const isn't necessary. Make sure we handle this cleanly.
  2468. sparse_hash_map<const int, int, Hasher, Hasher> shm;
  2469. shm.set_deleted_key(1);
  2470. shm[10] = 20;
  2471. }
  2472. TYPED_TEST(HashtableAllTest, ResizeActuallyResizes)
  2473. {
  2474. // This tests for a problem we had where we could repeatedly "resize"
  2475. // a hashtable to the same size it was before, on every insert.
  2476. // -----------------------------------------------------------------
  2477. const typename TypeParam::size_type kSize = 1<<10; // Pick any power of 2
  2478. const float kResize = 0.8f; // anything between 0.5 and 1 is fine.
  2479. const int kThreshold = static_cast<int>(kSize * kResize - 1);
  2480. this->ht_.set_resizing_parameters(0, kResize);
  2481. this->ht_.set_deleted_key(this->UniqueKey(kThreshold + 100));
  2482. // Get right up to the resizing threshold.
  2483. for (int i = 0; i <= kThreshold; i++) {
  2484. this->ht_.insert(this->UniqueObject(i+1));
  2485. }
  2486. // The bucket count should equal kSize.
  2487. EXPECT_EQ(kSize, this->ht_.bucket_count());
  2488. // Now start doing erase+insert pairs. This should cause us to
  2489. // copy the hashtable at most once.
  2490. const int pre_copies = this->ht_.num_table_copies();
  2491. for (int i = 0; i < static_cast<int>(kSize); i++) {
  2492. this->ht_.erase(this->UniqueKey(kThreshold));
  2493. this->ht_.insert(this->UniqueObject(kThreshold));
  2494. }
  2495. EXPECT_LT(this->ht_.num_table_copies(), pre_copies + 2);
  2496. // Now create a hashtable where we go right to the threshold, then
  2497. // delete everything and do one insert. Even though our hashtable
  2498. // is now tiny, we should still have at least kSize buckets, because
  2499. // our shrink threshhold is 0.
  2500. // -----------------------------------------------------------------
  2501. TypeParam ht2;
  2502. ht2.set_deleted_key(this->UniqueKey(kThreshold + 100));
  2503. ht2.set_resizing_parameters(0, kResize);
  2504. EXPECT_LT(ht2.bucket_count(), kSize);
  2505. for (int i = 0; i <= kThreshold; i++) {
  2506. ht2.insert(this->UniqueObject(i+1));
  2507. }
  2508. EXPECT_EQ(ht2.bucket_count(), kSize);
  2509. for (int i = 0; i <= kThreshold; i++) {
  2510. ht2.erase(this->UniqueKey(i+1));
  2511. EXPECT_EQ(ht2.bucket_count(), kSize);
  2512. }
  2513. ht2.insert(this->UniqueObject(kThreshold+2));
  2514. EXPECT_GE(ht2.bucket_count(), kSize);
  2515. }
  2516. TEST(HashtableTest, CXX11)
  2517. {
  2518. #if !defined(SPP_NO_CXX11_HDR_INITIALIZER_LIST)
  2519. {
  2520. // Initializer lists
  2521. // -----------------
  2522. typedef sparse_hash_map<int, int> Smap;
  2523. Smap smap({ {1, 1}, {2, 2} });
  2524. EXPECT_EQ(smap.size(), 2);
  2525. smap = { {1, 1}, {2, 2}, {3, 4} };
  2526. EXPECT_EQ(smap.size(), 3);
  2527. smap.insert({{5, 1}, {6, 1}});
  2528. EXPECT_EQ(smap.size(), 5);
  2529. EXPECT_EQ(smap[6], 1);
  2530. EXPECT_EQ(smap.at(6), 1);
  2531. try
  2532. {
  2533. EXPECT_EQ(smap.at(999), 1);
  2534. }
  2535. catch (...)
  2536. {};
  2537. sparse_hash_set<int> sset({ 1, 3, 4, 5 });
  2538. EXPECT_EQ(sset.size(), 4);
  2539. }
  2540. #endif
  2541. }
  2542. TEST(HashtableTest, NestedHashtables)
  2543. {
  2544. // People can do better than to have a hash_map of hash_maps, but we
  2545. // should still support it. I try a few different mappings.
  2546. sparse_hash_map<string, sparse_hash_map<int, string>, Hasher, Hasher> ht1;
  2547. ht1["hi"]; // create a sub-ht with the default values
  2548. ht1["lo"][1] = "there";
  2549. sparse_hash_map<string, sparse_hash_map<int, string>, Hasher, Hasher>
  2550. ht1copy = ht1;
  2551. }
  2552. TEST(HashtableDeathTest, ResizeOverflow)
  2553. {
  2554. sparse_hash_map<int, int> ht2;
  2555. EXPECT_DEATH(ht2.resize(static_cast<size_t>(-1)), "overflows size_type");
  2556. }
  2557. TEST(HashtableDeathTest, InsertSizeTypeOverflow)
  2558. {
  2559. static const int kMax = 256;
  2560. vector<int> test_data(kMax);
  2561. for (int i = 0; i < kMax; ++i) {
  2562. test_data[(size_t)i] = i+1000;
  2563. }
  2564. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2565. // Test we are using the correct allocator
  2566. EXPECT_TRUE(shs.get_allocator().is_custom_alloc());
  2567. // Test size_type overflow in insert(it, it)
  2568. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.end()), "overflows size_type");
  2569. }
  2570. TEST(HashtableDeathTest, InsertMaxSizeOverflow)
  2571. {
  2572. static const int kMax = 256;
  2573. vector<int> test_data(kMax);
  2574. for (int i = 0; i < kMax; ++i) {
  2575. test_data[(size_t)i] = i+1000;
  2576. }
  2577. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2578. // Test max_size overflow
  2579. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.begin() + 11), "exceed max_size");
  2580. }
  2581. TEST(HashtableDeathTest, ResizeSizeTypeOverflow)
  2582. {
  2583. // Test min-buckets overflow, when we want to resize too close to size_type
  2584. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 10> > shs;
  2585. EXPECT_DEATH(shs.resize(250), "overflows size_type");
  2586. }
  2587. TEST(HashtableDeathTest, ResizeDeltaOverflow)
  2588. {
  2589. static const int kMax = 256;
  2590. vector<int> test_data(kMax);
  2591. for (int i = 0; i < kMax; ++i) {
  2592. test_data[(size_t)i] = i+1000;
  2593. }
  2594. sparse_hash_set<int, Hasher, Hasher, Alloc<int, uint8, 255> > shs;
  2595. for (int i = 0; i < 9; i++) {
  2596. shs.insert(i);
  2597. }
  2598. EXPECT_DEATH(shs.insert(test_data.begin(), test_data.begin() + 250),
  2599. "overflows size_type");
  2600. }
  2601. // ------------------------------------------------------------------------
  2602. // This informational "test" comes last so it's easy to see.
  2603. // Also, benchmarks.
  2604. TYPED_TEST(HashtableAllTest, ClassSizes)
  2605. {
  2606. std::cout << "sizeof(" << typeid(TypeParam).name() << "): "
  2607. << sizeof(this->ht_) << "\n";
  2608. }
  2609. } // unnamed namespace
  2610. int main(int, char **)
  2611. {
  2612. // All the work is done in the static constructors. If they don't
  2613. // die, the tests have all passed.
  2614. cout << "PASS\n";
  2615. return 0;
  2616. }