hash_table_unittest.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <gtest/gtest.h>
  16. #include <boost/shared_ptr.hpp>
  17. #include <string.h>
  18. #include <iostream>
  19. #include "hash_table.h"
  20. #include "hash_key.h"
  21. #include "nsas_entry_compare.h"
  22. #include "nsas_test.h"
  23. using namespace std;
  24. namespace isc {
  25. namespace nsas {
  26. /// \brief Text Fixture Class
  27. ///
  28. /// Many of the tests are based on checking reference counts. In all tests,
  29. /// objects named dummyN_ have a reference count of 1 because they are a member
  30. /// of this class which has been instantiated for the test. The reference count
  31. /// is increased as they are added to the hash table for testing and decreased
  32. /// as they are removed.
  33. class HashTableTest : public ::testing::Test {
  34. protected:
  35. // Constructor - initialize the objects
  36. HashTableTest() :
  37. table_(new NsasEntryCompare<TestEntry>()),
  38. dummy1_(new TestEntry("test", 1)),
  39. dummy2_(new TestEntry("test", 1)),
  40. dummy3_(new TestEntry("Something_Else", 1)),
  41. dummy4_(new TestEntry("test", 3))
  42. {}
  43. // Members.
  44. HashTable<TestEntry> table_;
  45. boost::shared_ptr<TestEntry> dummy1_;
  46. boost::shared_ptr<TestEntry> dummy2_;
  47. boost::shared_ptr<TestEntry> dummy3_;
  48. boost::shared_ptr<TestEntry> dummy4_;
  49. };
  50. // Test of the constructor
  51. TEST_F(HashTableTest, Constructor) {
  52. // Default constructor
  53. HashTable<TestEntry> table1(new NsasEntryCompare<TestEntry>());
  54. EXPECT_EQ(HASHTABLE_DEFAULT_SIZE, table1.tableSize());
  55. // Non default constructor
  56. EXPECT_NE(42, HASHTABLE_DEFAULT_SIZE);
  57. HashTable<TestEntry> table2(new NsasEntryCompare<TestEntry>(), 42);
  58. EXPECT_EQ(42, table2.tableSize());
  59. }
  60. // Basic test of addition
  61. TEST_F(HashTableTest, AddTest) {
  62. // Using two objects with the same name and class,
  63. EXPECT_EQ(dummy1_->getName(), dummy2_->getName());
  64. EXPECT_EQ(dummy1_->getClass(), dummy2_->getClass());
  65. EXPECT_EQ(1, dummy1_.use_count());
  66. EXPECT_EQ(1, dummy2_.use_count());
  67. // Add first one to the hash table_
  68. bool result = table_.add(dummy1_, dummy1_->hashKey());
  69. EXPECT_TRUE(result);
  70. EXPECT_EQ(2, dummy1_.use_count());
  71. EXPECT_EQ(1, dummy2_.use_count());
  72. // Attempt to add the second object with the same name and class fails.
  73. result = table_.add(dummy2_, dummy2_->hashKey());
  74. EXPECT_FALSE(result);
  75. EXPECT_EQ(2, dummy1_.use_count());
  76. EXPECT_EQ(1, dummy2_.use_count());
  77. // Replacing an entry should work though
  78. result = table_.add(dummy2_, dummy2_->hashKey(), true);
  79. EXPECT_TRUE(result);
  80. EXPECT_EQ(1, dummy1_.use_count());
  81. EXPECT_EQ(2, dummy2_.use_count());
  82. }
  83. // Test the remove functionality
  84. TEST_F(HashTableTest, RemoveTest) {
  85. // Using two objects with different names but the same class
  86. EXPECT_NE(dummy1_->getName(), dummy3_->getName());
  87. EXPECT_EQ(dummy1_->getClass(), dummy3_->getClass());
  88. EXPECT_EQ(1, dummy1_.use_count());
  89. EXPECT_EQ(1, dummy3_.use_count());
  90. // Add first one to the hash table_
  91. bool result = table_.add(dummy1_, dummy1_->hashKey());
  92. EXPECT_TRUE(result);
  93. EXPECT_EQ(2, dummy1_.use_count());
  94. EXPECT_EQ(1, dummy3_.use_count());
  95. // Now remove it.
  96. result = table_.remove(dummy1_->hashKey());
  97. EXPECT_TRUE(result);
  98. EXPECT_EQ(1, dummy1_.use_count());
  99. EXPECT_EQ(1, dummy3_.use_count());
  100. // Attempt to remove it again.
  101. result = table_.remove(dummy1_->hashKey());
  102. EXPECT_FALSE(result);
  103. EXPECT_EQ(1, dummy1_.use_count());
  104. EXPECT_EQ(1, dummy3_.use_count());
  105. // Add both entries to table_, then remove one (checks that it will
  106. // remove the correct one).
  107. result = table_.add(dummy1_, dummy1_->hashKey());
  108. EXPECT_TRUE(result);
  109. result = table_.add(dummy3_, dummy3_->hashKey());
  110. EXPECT_TRUE(result);
  111. EXPECT_EQ(2, dummy1_.use_count());
  112. EXPECT_EQ(2, dummy3_.use_count());
  113. result = table_.remove(dummy1_->hashKey());
  114. EXPECT_TRUE(result);
  115. EXPECT_EQ(1, dummy1_.use_count());
  116. EXPECT_EQ(2, dummy3_.use_count());
  117. }
  118. // Test the "get" functionality
  119. TEST_F(HashTableTest, GetTest) {
  120. // Using two objects with different names but the same class
  121. EXPECT_NE(dummy1_->getName(), dummy3_->getName());
  122. EXPECT_EQ(dummy1_->getClass(), dummy3_->getClass());
  123. EXPECT_EQ(1, dummy1_.use_count());
  124. EXPECT_EQ(1, dummy3_.use_count());
  125. // Add both to the hash table
  126. bool result = table_.add(dummy1_, dummy1_->hashKey());
  127. EXPECT_TRUE(result);
  128. result = table_.add(dummy3_, dummy3_->hashKey());
  129. EXPECT_TRUE(result);
  130. // Lookup the first
  131. boost::shared_ptr<TestEntry> value = table_.get(dummy1_->hashKey());
  132. EXPECT_EQ(value.get(), dummy1_.get());
  133. // ... and the second
  134. value = table_.get(dummy3_->hashKey());
  135. EXPECT_EQ(value.get(), dummy3_.get());
  136. // Remove the first
  137. result = table_.remove(dummy1_->hashKey());
  138. EXPECT_TRUE(result);
  139. // ... and a lookup should return empty
  140. value = table_.get(dummy1_->hashKey());
  141. EXPECT_TRUE(value.get() == NULL);
  142. }
  143. // Test that objects with the same name and different classes are distinct.
  144. TEST_F(HashTableTest, ClassTest) {
  145. // Using two objects with the same name and different classes
  146. EXPECT_EQ(dummy1_->getName(), dummy4_->getName());
  147. EXPECT_NE(dummy1_->getClass(), dummy4_->getClass());
  148. EXPECT_EQ(1, dummy1_.use_count());
  149. EXPECT_EQ(1, dummy4_.use_count());
  150. // Add both to the hash table
  151. bool result = table_.add(dummy1_, dummy1_->hashKey());
  152. EXPECT_TRUE(result);
  153. EXPECT_EQ(2, dummy1_.use_count());
  154. result = table_.add(dummy4_, dummy4_->hashKey());
  155. EXPECT_TRUE(result);
  156. EXPECT_EQ(2, dummy4_.use_count());
  157. // Lookup the first
  158. boost::shared_ptr<TestEntry> value1 = table_.get(dummy1_->hashKey());
  159. EXPECT_EQ(value1.get(), dummy1_.get());
  160. EXPECT_EQ(3, dummy1_.use_count());
  161. EXPECT_EQ(2, dummy4_.use_count());
  162. // ... and the second
  163. boost::shared_ptr<TestEntry> value4 = table_.get(dummy4_->hashKey());
  164. EXPECT_EQ(value4.get(), dummy4_.get());
  165. EXPECT_EQ(3, dummy1_.use_count());
  166. EXPECT_EQ(3, dummy4_.use_count());
  167. // ... and check they are different
  168. EXPECT_NE(dummy1_.get(), dummy4_.get());
  169. // Remove the first
  170. result = table_.remove(dummy1_->hashKey());
  171. EXPECT_TRUE(result);
  172. EXPECT_EQ(2, dummy1_.use_count());
  173. EXPECT_EQ(3, dummy4_.use_count());
  174. // ... and a lookup should return empty
  175. boost::shared_ptr<TestEntry> value1a = table_.get(dummy1_->hashKey());
  176. EXPECT_TRUE(value1a.get() == NULL);
  177. }
  178. } // namespace nsas
  179. } // namespace isc