2 * Copyright © 2018 Intel Corporation
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:
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
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
24 #include <gtest/gtest.h>
25 #include "util/hash_table.h"
30 struct set
*s
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
31 _mesa_key_pointer_equal
);
32 struct set_entry
*entry
;
34 const void *a
= (const void *)10;
35 const void *b
= (const void *)20;
39 EXPECT_EQ(s
->entries
, 2);
42 EXPECT_EQ(s
->entries
, 2);
44 entry
= _mesa_set_search(s
, a
);
46 EXPECT_EQ(entry
->key
, a
);
48 _mesa_set_remove(s
, entry
);
49 EXPECT_EQ(s
->entries
, 1);
51 entry
= _mesa_set_search(s
, a
);
54 _mesa_set_destroy(s
, NULL
);
59 struct set
*s
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
60 _mesa_key_pointer_equal
);
61 struct set_entry
*entry
;
63 const void *a
= (const void *)10;
64 const void *b
= (const void *)20;
65 const void *c
= (const void *)30;
71 entry
= _mesa_set_search(s
, c
);
73 EXPECT_EQ(entry
->key
, c
);
75 _mesa_set_remove(s
, entry
);
76 EXPECT_EQ(s
->entries
, 2);
78 struct set
*clone
= _mesa_set_clone(s
, NULL
);
79 EXPECT_EQ(clone
->entries
, 2);
81 EXPECT_TRUE(_mesa_set_search(clone
, a
));
82 EXPECT_TRUE(_mesa_set_search(clone
, b
));
83 EXPECT_FALSE(_mesa_set_search(clone
, c
));
85 _mesa_set_destroy(s
, NULL
);
86 _mesa_set_destroy(clone
, NULL
);
91 struct set
*s
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
92 _mesa_key_pointer_equal
);
94 const void *a
= (const void *)10;
95 const void *b
= (const void *)20;
96 const void *c
= (const void *)30;
100 EXPECT_EQ(s
->entries
, 2);
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
));
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
));
114 _mesa_set_destroy(s
, NULL
);
117 static uint32_t hash_int(const void *p
)
119 int i
= *(const int *)p
;
123 static bool cmp_int(const void *p1
, const void *p2
)
125 int i1
= *(const int *)p1
, i2
= *(const int *)p2
;
129 TEST(set
, search_or_add
)
131 struct set
*s
= _mesa_set_create(NULL
, hash_int
, cmp_int
);
133 int a
= 10, b
= 20, c
= 20, d
= 30;
135 _mesa_set_add(s
, &a
);
136 _mesa_set_add(s
, &b
);
137 EXPECT_EQ(s
->entries
, 2);
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);
143 struct set_entry
*entry3
= _mesa_set_search_or_add(s
, &d
);
144 EXPECT_EQ(entry3
->key
, &d
);
145 EXPECT_EQ(s
->entries
, 3);
147 _mesa_set_destroy(s
, NULL
);