From 308a6691b7a939e5391d5648e79d9c987ca848c2 Mon Sep 17 00:00:00 2001 From: Marcel Kost Date: Thu, 14 Jun 2018 19:50:21 -0400 Subject: [PATCH] Fix HashTable::BuildLazy if it contains zero emelemts (#1408) * Fix HashTable::BuildLazy if it contains zero emelemts * Add test case for empty hash table --- src/codegen/util/hash_table.cpp | 3 +++ test/codegen/hash_table_test.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/codegen/util/hash_table.cpp b/src/codegen/util/hash_table.cpp index e6ed90c946c..4b6c8c9bb77 100644 --- a/src/codegen/util/hash_table.cpp +++ b/src/codegen/util/hash_table.cpp @@ -191,6 +191,9 @@ char *HashTable::Insert(uint64_t hash) { } void HashTable::BuildLazy() { + // Early exit if no elements have been added (hash table is still valid) + if (num_elems_ == 0) return; + // Grab entry head Entry *head = directory_[0]; diff --git a/test/codegen/hash_table_test.cpp b/test/codegen/hash_table_test.cpp index 327fdcc44f1..c298cca7863 100644 --- a/test/codegen/hash_table_test.cpp +++ b/test/codegen/hash_table_test.cpp @@ -105,6 +105,33 @@ TEST_F(HashTableTest, CanInsertUniqueKeys) { } } +TEST_F(HashTableTest, BuildEmptyHashTable) { + codegen::util::HashTable table{GetMemPool(), sizeof(Key), sizeof(Value)}; + + std::vector keys; + + // Insert keys + for (uint32_t i = 0; i < 10; i++) { + Key k{1, i}; + // do NOT insert into hash table + + keys.emplace_back(k); + } + + EXPECT_EQ(0, table.NumElements()); + + // Build lazy + table.BuildLazy(); + + // Lookups should succeed + for (const auto &key : keys) { + std::function f = + [](UNUSED_ATTRIBUTE const Value &v) {}; + auto ret = table.TypedProbe(key.Hash(), key, f); + EXPECT_EQ(false, ret); + } +} + TEST_F(HashTableTest, CanInsertDuplicateKeys) { codegen::util::HashTable table{GetMemPool(), sizeof(Key), sizeof(Value)};