void HashTableTest::testInsert() { std::string s1("str1"); std::string s2("str2"); HashTable<std::string, int> hashTable; assert (!hashTable.exists(s1)); hashTable.insert(s1, 13); assert (hashTable.exists(s1)); assert (hashTable.get(s1) == 13); int retVal = 0; assert (hashTable.get(s1, retVal)); assert (retVal == 13); try { hashTable.insert(s1, 22); failmsg ("duplicate insert must fail"); } catch (Exception&){} try { hashTable.get(s2); failmsg ("getting a non inserted item must fail"); } catch (Exception&){} assert (!hashTable.exists(s2)); hashTable.insert(s2, 13); assert (hashTable.exists(s2)); }
int TestHashTable_String() { HashTable<std::string, std::string> ht; char buffer1[32]; char buffer2[32]; const unsigned long max = 1000; unsigned long i; for (i = 0; i < max; i++) { sprintf(buffer1, "%lu", i); sprintf(buffer2, "%lu", max - i); ht.insert(std::string(buffer1), std::string(buffer2)); } for (i = 0; i < max; i += 2) { sprintf(buffer1, "%lu", i); TEST_ASSERT(ht.exists(std::string(buffer1))); } for (i = 0; i < max; i += 2) { sprintf(buffer1, "%lu", i); TEST_ASSERT(ht.erase(std::string(buffer1))); } for (i = 0; i < max; i += 2) { sprintf(buffer1, "%lu", i); TEST_ASSERT(!ht.exists(std::string(buffer1))); } std::string rec; for (i = 1; i < max; i += 2) { sprintf(buffer1, "%lu", i); sprintf(buffer2, "%lu", max - i); TEST_ASSERT(ht.find(std::string(buffer1), rec)); TEST_ASSERT(std::string(rec) == std::string(buffer2)); } /* Rest of the cleanup */ for (i = 1; i < max; i += 2) { sprintf(buffer1, "%lu", i); TEST_ASSERT(ht.erase(std::string(buffer1))); } return 0; }
void HashTableTest::testUpdate() { // add code for second test here std::string s1("str1"); std::string s2("str2"); HashTable<std::string, int> hashTable; hashTable.insert(s1, 13); hashTable.update(s1, 14); assert (hashTable.exists(s1)); assert (hashTable.get(s1) == 14); int retVal = 0; assert (hashTable.get(s1, retVal)); assert (retVal == 14); // updating a non existing item must work too hashTable.update(s2, 15); assert (hashTable.get(s2) == 15); }
void LinearHashTableTest::testPerformanceStr() { const int N = 5000000; Stopwatch sw; std::vector<std::string> values; for (int i = 0; i < N; ++i) { values.push_back(NumberFormatter::format0(i, 8)); } { LinearHashTable<std::string, Hash<std::string> > lht(N); sw.start(); for (int i = 0; i < N; ++i) { lht.insert(values[i]); } sw.stop(); std::cout << "Insert LHT: " << sw.elapsedSeconds() << std::endl; sw.reset(); sw.start(); for (int i = 0; i < N; ++i) { lht.find(values[i]); } sw.stop(); std::cout << "Find LHT: " << sw.elapsedSeconds() << std::endl; sw.reset(); } { HashTable<std::string, int> ht; sw.start(); for (int i = 0; i < N; ++i) { ht.insert(values[i], i); } sw.stop(); std::cout << "Insert HT: " << sw.elapsedSeconds() << std::endl; sw.reset(); sw.start(); for (int i = 0; i < N; ++i) { ht.exists(values[i]); } sw.stop(); std::cout << "Find HT: " << sw.elapsedSeconds() << std::endl; } { std::set<std::string> s; sw.start(); for (int i = 0; i < N; ++i) { s.insert(values[i]); } sw.stop(); std::cout << "Insert set: " << sw.elapsedSeconds() << std::endl; sw.reset(); sw.start(); for (int i = 0; i < N; ++i) { s.find(values[i]); } sw.stop(); std::cout << "Find set: " << sw.elapsedSeconds() << std::endl; sw.reset(); } }
void LinearHashTableTest::testPerformanceInt() { const int N = 5000000; Stopwatch sw; { LinearHashTable<int, Hash<int> > lht(N); sw.start(); for (int i = 0; i < N; ++i) { lht.insert(i); } sw.stop(); std::cout << "Insert LHT: " << sw.elapsedSeconds() << std::endl; sw.reset(); sw.start(); for (int i = 0; i < N; ++i) { lht.find(i); } sw.stop(); std::cout << "Find LHT: " << sw.elapsedSeconds() << std::endl; sw.reset(); } { HashTable<int, int> ht; sw.start(); for (int i = 0; i < N; ++i) { ht.insert(i, i); } sw.stop(); std::cout << "Insert HT: " << sw.elapsedSeconds() << std::endl; sw.reset(); sw.start(); for (int i = 0; i < N; ++i) { ht.exists(i); } sw.stop(); std::cout << "Find HT: " << sw.elapsedSeconds() << std::endl; } { std::set<int> s; sw.start(); for (int i = 0; i < N; ++i) { s.insert(i); } sw.stop(); std::cout << "Insert set: " << sw.elapsedSeconds() << std::endl; sw.reset(); sw.start(); for (int i = 0; i < N; ++i) { s.find(i); } sw.stop(); std::cout << "Find set: " << sw.elapsedSeconds() << std::endl; sw.reset(); } }