add a unit test for Bimap

- rename find(Value&) to findValue() to avoid issues if the key and
values have the same type.

- fix a leak if Bitmap is destroyed and not empty (thankfully this
didn't happen in practice in filament, because the Bimap was always
empty when destroyed)

- fix a use after free in erase. we were destroying the Key before
removing it from the maps, which theoretically could need to access
them.

- slightly improved the API and added some comments about preconditions.
This commit is contained in:
Mathias Agopian
2025-07-24 09:29:06 -07:00
committed by Mathias Agopian
parent 1f8248ba99
commit 8982d7161e
6 changed files with 316 additions and 21 deletions
+80 -18
View File
@@ -70,10 +70,32 @@ class Bimap {
public:
Bimap() = default;
explicit Bimap(Allocator&& allocator)
: mAllocator(std::forward<Allocator>(allocator)) {
}
~Bimap() noexcept {
clear();
}
Bimap(Bimap const&) = delete;
Bimap& operator=(Bimap const&) = delete;
Bimap(Bimap&&) = delete;
Bimap& operator=(Bimap&&) = delete;
void clear() noexcept {
// We only need to iterate one map, as they both point to the same keys.
for (auto& pair : mForwardMap) {
// Manually call the destructor on the key...
pair.first.pKey->~Key();
// ...then deallocate the memory.
mAllocator.deallocate(const_cast<Key*>(pair.first.pKey), 1);
}
mForwardMap.clear();
mBackwardMap.clear();
}
void reserve(size_t capacity) {
mForwardMap.reserve(capacity);
mBackwardMap.reserve(capacity);
@@ -83,15 +105,26 @@ public:
return mForwardMap.empty() && mBackwardMap.empty();
}
// insert a new key/value pair
// insert a new key/value pair. duplicate are not allowed.
void insert(Key const& key, Value const& value) noexcept {
assert_invariant(find(key) == end() && findValue(value) == mBackwardMap.end());
Key* pKey = mAllocator.allocate(1); // allocate storage for the key
new((void*)pKey) Key{ key }; // copy-construct the key
new(static_cast<void*>(pKey)) Key{ key }; // copy-construct the key
// TODO: we can leak the Key if the calls below throw
mForwardMap.insert({{ pKey }, value });
mBackwardMap.insert({ value, { pKey }});
}
typename ForwardMap::iterator begin() { return mForwardMap.begin(); }
typename ForwardMap::const_iterator begin() const { return mForwardMap.begin(); }
typename ForwardMap::const_iterator cbegin() const { return mForwardMap.cbegin(); }
typename ForwardMap::iterator end() { return mForwardMap.end(); }
typename ForwardMap::const_iterator end() const { return mForwardMap.end(); }
typename ForwardMap::const_iterator cend() const { return mForwardMap.cend(); }
typename BackwardMap::iterator endValue() { return mBackwardMap.end(); }
typename BackwardMap::const_iterator endValue() const { return mBackwardMap.end(); }
// Find the value iterator from the key in O(1)
typename ForwardMap::const_iterator find(Key const& key) const {
@@ -102,28 +135,57 @@ public:
}
// Find the key iterator from the value in O(1). precondition, the value must exist.
typename BackwardMap::const_iterator find(Value const& value) const {
auto pos = mBackwardMap.find(value);
assert_invariant( pos != mBackwardMap.end() );
return pos;
typename BackwardMap::const_iterator findValue(Value const& value) const {
return mBackwardMap.find(value);
}
typename BackwardMap::iterator find(Value& value) const {
typename BackwardMap::iterator findValue(Value& value) {
return mBackwardMap.find(value);
}
// erase a key/value pair using an iterator to the value
// Erase by key
bool erase(Key const& key) {
auto forward_it = find(key);
if (forward_it != end()) {
erase(forward_it);
return true;
}
return false;
}
// Erase by forward map iterator
void erase(typename ForwardMap::const_iterator it) {
// Get a stable pointer to the key object before erasing.
Key const* const pKey = const_cast<Key*>(it->first.pKey);
// Find the corresponding entry in the backward map.
auto backward_it = findValue(it->second);
assert_invariant(backward_it != mBackwardMap.end());
// Erase from both maps while the key is still valid.
mBackwardMap.erase(backward_it);
mForwardMap.erase(it);
// Now that no map refers to the key, safely destroy and deallocate it.
pKey->~Key();
mAllocator.deallocate(const_cast<Key*>(pKey), 1);
}
// erase by backward map iterator
void erase(typename BackwardMap::const_iterator it) {
// find the key
Key const& key = *(it->second.pKey);
// and its iterator
auto pos = find(key);
// destroy the key
it->second.pKey->~Key();
// free its memory
mAllocator.deallocate(const_cast<Key *>(it->second.pKey), 1);
// remove the entries from both maps
mForwardMap.erase(pos);
// Get a stable pointer to the key object.
Key const* const pKey = it->second.pKey;
// Find the corresponding iterator in the forward map before erasing.
auto forward_it = find(*pKey);
assert_invariant(forward_it != end());
// Erase from both maps while the key object is still valid.
mForwardMap.erase(forward_it);
mBackwardMap.erase(it);
// Now that no map refers to the key, we can safely destroy and deallocate it.
pKey->~Key();
mAllocator.deallocate(const_cast<Key*>(pKey), 1);
}
};
@@ -93,7 +93,7 @@ auto HwDescriptorSetLayoutFactory::create(DriverApi& driver,
void HwDescriptorSetLayoutFactory::destroy(DriverApi& driver, Handle handle) noexcept {
// look for this handle in our map
auto pos = mBimap.find(Value{ handle });
auto pos = mBimap.findValue(Value{ handle });
if (--pos->second.pKey->refs == 0) {
mBimap.erase(pos);
driver.destroyDescriptorSetLayout(handle);
+1 -1
View File
@@ -82,7 +82,7 @@ auto HwRenderPrimitiveFactory::create(DriverApi& driver,
void HwRenderPrimitiveFactory::destroy(DriverApi& driver, Handle handle) noexcept {
// look for this handle in our map
auto pos = mBimap.find(Value{ handle });
auto pos = mBimap.findValue(Value{ handle });
if (--pos->second.pKey->refs == 0) {
mBimap.erase(pos);
driver.destroyRenderPrimitive(handle);
+1 -1
View File
@@ -83,7 +83,7 @@ auto HwVertexBufferInfoFactory::create(DriverApi& driver,
void HwVertexBufferInfoFactory::destroy(DriverApi& driver, Handle handle) noexcept {
// look for this handle in our map
auto pos = mBimap.find(Value{ handle });
auto pos = mBimap.findValue(Value{ handle });
if (--(pos->second.pKey->refs) == 0) {
mBimap.erase(pos);
driver.destroyVertexBufferInfo(handle);
+1
View File
@@ -45,6 +45,7 @@ if (TNT_DEV)
filament_AtlasAllocator_test.cpp
filament_test_exposure.cpp
filament_rendering_test.cpp
filament_bimap_test.cpp
filament_framegraph_test.cpp
filament_test.cpp)
+232
View File
@@ -0,0 +1,232 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include "../src/Bimap.h"
#include <string>
#include <vector>
using namespace filament;
// Test basic insertion, lookup by key and value, and emptiness.
TEST(BimapTest, InsertAndFind) {
Bimap<int, std::string> bimap;
EXPECT_TRUE(bimap.empty());
bimap.insert(10, "ten");
bimap.insert(20, "twenty");
EXPECT_FALSE(bimap.empty());
// Test forward lookup (Key -> Value)
auto f_it_10 = bimap.find(10);
ASSERT_NE(f_it_10, bimap.end());
EXPECT_EQ(*f_it_10->first.pKey, 10);
EXPECT_EQ(f_it_10->second, "ten");
auto f_it_20 = bimap.find(20);
ASSERT_NE(f_it_20, bimap.end());
EXPECT_EQ(*f_it_20->first.pKey, 20);
EXPECT_EQ(f_it_20->second, "twenty");
// Test backward lookup (Value -> Key)
auto b_it_ten = bimap.findValue("ten");
ASSERT_NE(b_it_ten, bimap.endValue());
EXPECT_EQ(b_it_ten->first, "ten");
EXPECT_EQ(*b_it_ten->second.pKey, 10);
auto b_it_twenty = bimap.findValue("twenty");
ASSERT_NE(b_it_twenty, bimap.endValue());
EXPECT_EQ(b_it_twenty->first, "twenty");
EXPECT_EQ(*b_it_twenty->second.pKey, 20);
}
// Test finding elements that do not exist in the map.
TEST(BimapTest, FindNonExistent) {
Bimap<int, std::string> bimap;
bimap.insert(10, "ten");
// Finding a non-existent key should return the end iterator.
EXPECT_EQ(bimap.find(999), bimap.end());
// Finding a non-existent value should return the value-end iterator.
EXPECT_EQ(bimap.findValue("nine-ninety-nine"), bimap.endValue());
}
// A test structure to track construction/destruction to verify correct memory management.
struct LifecycleKey {
int id;
static inline int constructions = 0;
static inline int destructions = 0;
explicit LifecycleKey(int i) : id(i) { constructions++; }
LifecycleKey(const LifecycleKey& other) : id(other.id) { constructions++; }
LifecycleKey(LifecycleKey&&) = delete; // Bimap uses copy-construction
~LifecycleKey() { destructions++; }
bool operator==(const LifecycleKey& rhs) const { return id == rhs.id; }
static void reset() {
constructions = 0;
destructions = 0;
}
};
// Specialize std::hash for the test key.
namespace std {
template <> struct hash<LifecycleKey> {
size_t operator()(const LifecycleKey& k) const { return hash<int>()(k.id); }
};
}
TEST(BimapTest, KeyLifecycleOnClear) {
LifecycleKey::reset();
{
Bimap<LifecycleKey, int> bimap;
bimap.insert(LifecycleKey(10), 100);
bimap.insert(LifecycleKey(20), 200);
// 2 on stack, 2 copied into bimap
ASSERT_EQ(LifecycleKey::constructions, 4);
// 2 on stack are destroyed after insert() calls
ASSERT_EQ(LifecycleKey::destructions, 2);
bimap.clear();
EXPECT_EQ(LifecycleKey::destructions, 4);
EXPECT_TRUE(bimap.empty());
} // Bimap destructor runs on an empty map
EXPECT_EQ(LifecycleKey::constructions, LifecycleKey::destructions);
}
// A test fixture to provide a pre-populated Bimap for each test case.
class BimapEraseTest : public ::testing::Test {
protected:
void SetUp() override {
// Insert some initial data before each test runs.
bimap.insert(10, "ten");
bimap.insert(20, "twenty");
bimap.insert(30, "thirty");
}
// Verifies that an element is completely gone from the bimap.
void verifyElementIsErased(int key, const std::string& value) {
// Check both forward and backward lookups fail.
EXPECT_EQ(bimap.find(key), bimap.end());
EXPECT_EQ(bimap.findValue(value), bimap.endValue());
}
// Verifies that an element still exists and is consistent.
void verifyElementExists(int key, const std::string& value) {
auto f_it = bimap.find(key);
ASSERT_NE(f_it, bimap.end());
EXPECT_EQ(f_it->second, value);
auto b_it = bimap.findValue(value);
ASSERT_NE(b_it, bimap.endValue());
EXPECT_EQ(*(b_it->second.pKey), key);
}
Bimap<int, std::string> bimap;
};
// Test case for `bool erase(Key const& key)`
TEST_F(BimapEraseTest, EraseByKey) {
bool result = bimap.erase(20);
EXPECT_TRUE(result);
verifyElementIsErased(20, "twenty");
// Ensure other elements were not affected.
verifyElementExists(10, "ten");
verifyElementExists(30, "thirty");
}
TEST_F(BimapEraseTest, EraseByKeyNonExistent) {
bool result = bimap.erase(999);
EXPECT_FALSE(result);
// Ensure no elements were affected.
verifyElementExists(10, "ten");
verifyElementExists(20, "twenty");
verifyElementExists(30, "thirty");
}
// Test case for `void erase(typename ForwardMap::const_iterator it)`
TEST_F(BimapEraseTest, EraseByForwardIterator) {
auto it_to_erase = bimap.find(30);
ASSERT_NE(it_to_erase, bimap.end());
bimap.erase(it_to_erase);
verifyElementIsErased(30, "thirty");
// Ensure other elements were not affected.
verifyElementExists(10, "ten");
verifyElementExists(20, "twenty");
}
// Test case for `void erase(typename BackwardMap::const_iterator it)`
TEST_F(BimapEraseTest, EraseByBackwardIterator) {
auto it_to_erase = bimap.findValue("ten");
ASSERT_NE(it_to_erase, bimap.endValue());
bimap.erase(it_to_erase);
verifyElementIsErased(10, "ten");
// Ensure other elements were not affected.
verifyElementExists(20, "twenty");
verifyElementExists(30, "thirty");
}
TEST(BimapEraseLifecycleTest, EraseCallsKeyDestructor) {
LifecycleKey::reset();
{
Bimap<LifecycleKey, int> bimap;
// Insert three items.
bimap.insert(LifecycleKey(1), 100);
bimap.insert(LifecycleKey(2), 200);
bimap.insert(LifecycleKey(3), 300);
// 3 on stack (destroyed) + 3 in bimap
ASSERT_EQ(LifecycleKey::constructions, 6);
ASSERT_EQ(LifecycleKey::destructions, 3);
// Erase one item by key. This should trigger two destructor calls.
bimap.erase(LifecycleKey(1));
EXPECT_EQ(LifecycleKey::destructions, 5);
// Erase the second item with a forward iterator (Two destructions)
auto f_it = bimap.find(LifecycleKey(2));
ASSERT_NE(f_it, bimap.end());
bimap.erase(f_it);
EXPECT_EQ(LifecycleKey::destructions, 7);
// Erase the third item with a backward iterator.
auto b_it = bimap.findValue(300);
ASSERT_NE(b_it, bimap.endValue());
bimap.erase(b_it);
EXPECT_EQ(LifecycleKey::destructions, 8);
EXPECT_TRUE(bimap.empty());
}
// Final check: All constructions must be matched by destructions.
EXPECT_EQ(LifecycleKey::constructions, LifecycleKey::destructions);
}