summaryrefslogtreecommitdiffstats
path: root/src/util/tests/set/set_test.cpp
diff options
context:
space:
mode:
authorConnor Abbott <[email protected]>2019-03-27 12:00:54 +0100
committerConnor Abbott <[email protected]>2019-05-31 19:13:45 +0200
commit8a838e172f3f796b2d5d01cb51e05b37ae6f48f5 (patch)
tree665ab3631440346c62107ea29da55e6fec91f61c /src/util/tests/set/set_test.cpp
parent1db86d8b62860380c34af77ae62b019ed2376443 (diff)
util/set: Add a _mesa_set_search_or_add() function
Unlike _mesa_set_search_and_add(), it doesn't replace an entry if it's found, returning it instead. This is useful for nir_instr_set, where we have to know both the original original instruction and its equivalent. Reviewed-by: Eric Anholt <[email protected]> Acked-by: Jason Ekstrand <[email protected]>
Diffstat (limited to 'src/util/tests/set/set_test.cpp')
-rw-r--r--src/util/tests/set/set_test.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/util/tests/set/set_test.cpp b/src/util/tests/set/set_test.cpp
index a1eef0b3d98..0b4355af128 100644
--- a/src/util/tests/set/set_test.cpp
+++ b/src/util/tests/set/set_test.cpp
@@ -113,3 +113,36 @@ TEST(set, remove_key)
_mesa_set_destroy(s, NULL);
}
+
+static uint32_t hash_int(const void *p)
+{
+ int i = *(const int *)p;
+ return i;
+}
+
+static bool cmp_int(const void *p1, const void *p2)
+{
+ int i1 = *(const int *)p1, i2 = *(const int *)p2;
+ return i1 == i2;
+}
+
+TEST(set, search_or_add)
+{
+ struct set *s = _mesa_set_create(NULL, hash_int, cmp_int);
+
+ int a = 10, b = 20, c = 20, d = 30;
+
+ _mesa_set_add(s, &a);
+ _mesa_set_add(s, &b);
+ EXPECT_EQ(s->entries, 2);
+
+ struct set_entry *entry = _mesa_set_search_or_add(s, &c);
+ EXPECT_EQ(entry->key, (void *)&b);
+ EXPECT_EQ(s->entries, 2);
+
+ struct set_entry *entry3 = _mesa_set_search_or_add(s, &d);
+ EXPECT_EQ(entry3->key, &d);
+ EXPECT_EQ(s->entries, 3);
+
+ _mesa_set_destroy(s, NULL);
+}