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
28TEST(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
57TEST(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
89TEST(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