| 14 | }; |
| 15 | |
| 16 | TEST(Hashtable, insertAndFind) { |
| 17 | Hashmap ht; |
| 18 | const auto nrEntries = 5; |
| 19 | ht.setSize(nrEntries); |
| 20 | std::vector<Entry> entries; |
| 21 | entries.reserve(nrEntries); |
| 22 | for (int i = 0; i < nrEntries; ++i) { |
| 23 | entries.push_back(Entry()); |
| 24 | auto& e = entries.back(); |
| 25 | e.k = i; |
| 26 | e.v = i + 50; |
| 27 | e.h.hash = std::hash<uint64_t>()(e.k); |
| 28 | ht.insert(&e.h, e.h.hash); |
| 29 | } |
| 30 | |
| 31 | for (unsigned i = 0; i < nrEntries; ++i) { |
| 32 | auto hash = std::hash<uint64_t>()(i); |
| 33 | auto entry = ht.find_chain(hash); |
| 34 | bool found = false; |
| 35 | for (; entry != ht.end(); entry = entry->next) |
| 36 | if (reinterpret_cast<uint64_t*>(entry)[3] == i + 50) found = true; |
| 37 | ASSERT_EQ(found, true); |
| 38 | } |
| 39 | } |