util/set: Add a _mesa_set_search_or_add() function
[mesa.git] / src / util / tests / set / set_test.cpp
1 /*
2 * Copyright © 2018 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <gtest/gtest.h>
25 #include "util/hash_table.h"
26 #include "util/set.h"
27
28 TEST(set, basic)
29 {
30 struct set *s = _mesa_set_create(NULL, _mesa_hash_pointer,
31 _mesa_key_pointer_equal);
32 struct set_entry *entry;
33
34 const void *a = (const void *)10;
35 const void *b = (const void *)20;
36
37 _mesa_set_add(s, a);
38 _mesa_set_add(s, b);
39 EXPECT_EQ(s->entries, 2);
40
41 _mesa_set_add(s, a);
42 EXPECT_EQ(s->entries, 2);
43
44 entry = _mesa_set_search(s, a);
45 EXPECT_TRUE(entry);
46 EXPECT_EQ(entry->key, a);
47
48 _mesa_set_remove(s, entry);
49 EXPECT_EQ(s->entries, 1);
50
51 entry = _mesa_set_search(s, a);
52 EXPECT_FALSE(entry);
53
54 _mesa_set_destroy(s, NULL);
55 }
56
57 TEST(set, clone)
58 {
59 struct set *s = _mesa_set_create(NULL, _mesa_hash_pointer,
60 _mesa_key_pointer_equal);
61 struct set_entry *entry;
62
63 const void *a = (const void *)10;
64 const void *b = (const void *)20;
65 const void *c = (const void *)30;
66
67 _mesa_set_add(s, a);
68 _mesa_set_add(s, b);
69 _mesa_set_add(s, c);
70
71 entry = _mesa_set_search(s, c);
72 EXPECT_TRUE(entry);
73 EXPECT_EQ(entry->key, c);
74
75 _mesa_set_remove(s, entry);
76 EXPECT_EQ(s->entries, 2);
77
78 struct set *clone = _mesa_set_clone(s, NULL);
79 EXPECT_EQ(clone->entries, 2);
80
81 EXPECT_TRUE(_mesa_set_search(clone, a));
82 EXPECT_TRUE(_mesa_set_search(clone, b));
83 EXPECT_FALSE(_mesa_set_search(clone, c));
84
85 _mesa_set_destroy(s, NULL);
86 _mesa_set_destroy(clone, NULL);
87 }
88
89 TEST(set, remove_key)
90 {
91 struct set *s = _mesa_set_create(NULL, _mesa_hash_pointer,
92 _mesa_key_pointer_equal);
93
94 const void *a = (const void *)10;
95 const void *b = (const void *)20;
96 const void *c = (const void *)30;
97
98 _mesa_set_add(s, a);
99 _mesa_set_add(s, b);
100 EXPECT_EQ(s->entries, 2);
101
102 /* Remove existing key. */
103 _mesa_set_remove_key(s, a);
104 EXPECT_EQ(s->entries, 1);
105 EXPECT_FALSE(_mesa_set_search(s, a));
106 EXPECT_TRUE(_mesa_set_search(s, b));
107
108 /* Remove non-existing key. */
109 _mesa_set_remove_key(s, c);
110 EXPECT_EQ(s->entries, 1);
111 EXPECT_FALSE(_mesa_set_search(s, a));
112 EXPECT_TRUE(_mesa_set_search(s, b));
113
114 _mesa_set_destroy(s, NULL);
115 }
116
117 static uint32_t hash_int(const void *p)
118 {
119 int i = *(const int *)p;
120 return i;
121 }
122
123 static bool cmp_int(const void *p1, const void *p2)
124 {
125 int i1 = *(const int *)p1, i2 = *(const int *)p2;
126 return i1 == i2;
127 }
128
129 TEST(set, search_or_add)
130 {
131 struct set *s = _mesa_set_create(NULL, hash_int, cmp_int);
132
133 int a = 10, b = 20, c = 20, d = 30;
134
135 _mesa_set_add(s, &a);
136 _mesa_set_add(s, &b);
137 EXPECT_EQ(s->entries, 2);
138
139 struct set_entry *entry = _mesa_set_search_or_add(s, &c);
140 EXPECT_EQ(entry->key, (void *)&b);
141 EXPECT_EQ(s->entries, 2);
142
143 struct set_entry *entry3 = _mesa_set_search_or_add(s, &d);
144 EXPECT_EQ(entry3->key, &d);
145 EXPECT_EQ(s->entries, 3);
146
147 _mesa_set_destroy(s, NULL);
148 }