Skip to content

Commit da5189b

Browse files
WeakValueHashMap: add test for custom Hasher and Keyeq
1 parent e62cf14 commit da5189b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Tests/DiligentCoreTest/src/Common/WeakValueHashMapTest.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,70 @@ TEST(Common_WeakValueHashMap, GetOrInsert)
125125
}
126126

127127

128+
TEST(Common_WeakValueHashMap, CustomHashKeyeq)
129+
{
130+
struct Key
131+
{
132+
int Val = 0;
133+
134+
Key() = default;
135+
136+
explicit Key(int v) :
137+
Val{v}
138+
{}
139+
140+
struct Hasher
141+
{
142+
size_t operator()(const Key& k) const
143+
{
144+
return std::hash<int>()(k.Val);
145+
}
146+
};
147+
148+
bool operator==(const Key& rhs) const
149+
{
150+
return Val == rhs.Val;
151+
}
152+
153+
struct KeyEq
154+
{
155+
bool operator()(const Key& lhs, const Key& rhs) const
156+
{
157+
return lhs.Val == rhs.Val;
158+
}
159+
};
160+
};
161+
162+
{
163+
WeakValueHashMap<Key, std::string, Key::Hasher> Map;
164+
165+
auto Handle1 = Map.GetOrInsert(Key{1}, "Value1");
166+
auto Handle2 = Map.GetOrInsert(Key{2}, "Value2");
167+
auto Handle3 = Map.Get(Key{2});
168+
EXPECT_TRUE(Handle1);
169+
EXPECT_TRUE(Handle2);
170+
EXPECT_TRUE(Handle3);
171+
EXPECT_STREQ(Handle1->c_str(), "Value1");
172+
EXPECT_STREQ(Handle2->c_str(), "Value2");
173+
EXPECT_EQ(*Handle2, *Handle3);
174+
}
175+
176+
{
177+
WeakValueHashMap<Key, std::string, Key::Hasher, Key::KeyEq> Map;
178+
179+
auto Handle1 = Map.GetOrInsert(Key{1}, "Value1");
180+
auto Handle2 = Map.GetOrInsert(Key{2}, "Value2");
181+
auto Handle3 = Map.Get(Key{2});
182+
EXPECT_TRUE(Handle1);
183+
EXPECT_TRUE(Handle2);
184+
EXPECT_TRUE(Handle3);
185+
EXPECT_STREQ(Handle1->c_str(), "Value1");
186+
EXPECT_STREQ(Handle2->c_str(), "Value2");
187+
EXPECT_EQ(*Handle2, *Handle3);
188+
}
189+
}
190+
191+
128192
static constexpr size_t kNumThreads = 8;
129193
#ifdef DILIGENT_DEBUG
130194
static constexpr int kNumParallelKeys = 1024;

0 commit comments

Comments
 (0)