summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEric Anholt <eric@anholt.net>2011-08-18 10:02:14 -0700
committerEric Anholt <eric@anholt.net>2011-08-18 10:02:14 -0700
commit02d2ecbce44fb46bd001164b2509cec35b2f65f1 (patch)
tree16db82e8bc8ad28defd83c3a9958c7bf4ddefe27 /tests
parent99ad0f60d58f88dc3e12fdce59c76897f220c59b (diff)
Add a set implementation derived from the hash_table implementation.
While it was easy to produce, it's easier to do it once and maintain it than leave this up to every possible user. Plus, it means it gets testing.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/set/.gitignore9
-rw-r--r--tests/set/Makefile.am36
-rw-r--r--tests/set/delete_and_lookup.c75
-rw-r--r--tests/set/delete_management.c88
-rw-r--r--tests/set/destroy_callback.c67
-rw-r--r--tests/set/insert_and_lookup.c58
-rw-r--r--tests/set/insert_many.c73
-rw-r--r--tests/set/null_destroy.c37
-rw-r--r--tests/set/null_remove.c46
-rw-r--r--tests/set/random_entry.c89
-rw-r--r--tests/set/replacement.c63
12 files changed, 643 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index df9bccc..2a1f7eb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -18,6 +18,8 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+SUBDIRS = set
+
AM_CFLAGS = $(WARN_CFLAGS) -I..
LDADD = ../libhash_table.la
diff --git a/tests/set/.gitignore b/tests/set/.gitignore
new file mode 100644
index 0000000..9cd5918
--- /dev/null
+++ b/tests/set/.gitignore
@@ -0,0 +1,9 @@
+delete_and_lookup
+delete_management
+destroy_callback
+insert_and_lookup
+insert_many
+null_destroy
+null_remove
+random_entry
+replacement
diff --git a/tests/set/Makefile.am b/tests/set/Makefile.am
new file mode 100644
index 0000000..f37e710
--- /dev/null
+++ b/tests/set/Makefile.am
@@ -0,0 +1,36 @@
+# Copyright © 2009 Intel Corporation
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# on the rights to use, copy, modify, merge, publish, distribute, sub
+# license, and/or sell copies of the Software, and to permit persons to whom
+# the Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+# ADAM JACKSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+AM_CFLAGS = $(WARN_CFLAGS) -I../..
+LDADD = ../../libset.la
+
+TESTS = \
+ delete_and_lookup \
+ delete_management \
+ destroy_callback \
+ insert_and_lookup \
+ insert_many \
+ null_destroy \
+ null_remove \
+ random_entry \
+ replacement \
+ $()
+
+EXTRA_PROGRAMS = $(TESTS)
diff --git a/tests/set/delete_and_lookup.c b/tests/set/delete_and_lookup.c
new file mode 100644
index 0000000..d05c819
--- /dev/null
+++ b/tests/set/delete_and_lookup.c
@@ -0,0 +1,75 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "set.h"
+#include "fnv_hash.h"
+
+/* Return collisions, so we can test the deletion behavior for chained
+ * objects.
+ */
+static uint32_t
+badhash(const void *key)
+{
+ return 1;
+}
+
+int
+main(int argc, char **argv)
+{
+ struct set *set;
+ const char *str1 = "test1";
+ const char *str2 = "test2";
+ uint32_t hash_str1 = badhash(str1);
+ uint32_t hash_str2 = badhash(str2);
+ struct set_entry *entry;
+
+ set = set_create(string_key_equals);
+
+ set_add(set, hash_str1, str1);
+ set_add(set, hash_str2, str2);
+
+ entry = set_search(set, hash_str2, str2);
+ assert(strcmp(entry->key, str2) == 0);
+
+ entry = set_search(set, hash_str1, str1);
+ assert(strcmp(entry->key, str1) == 0);
+
+ set_remove(set, entry);
+
+ entry = set_search(set, hash_str1, str1);
+ assert(entry == NULL);
+
+ entry = set_search(set, hash_str2, str2);
+ assert(strcmp(entry->key, str2) == 0);
+
+ set_destroy(set, NULL);
+
+ return 0;
+}
diff --git a/tests/set/delete_management.c b/tests/set/delete_management.c
new file mode 100644
index 0000000..b5c16fe
--- /dev/null
+++ b/tests/set/delete_management.c
@@ -0,0 +1,88 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "set.h"
+#include "fnv_hash.h"
+
+static uint32_t
+key_value(const void *key)
+{
+ return *(const uint32_t *)key;
+}
+
+static int
+uint32_t_key_equals(const void *a, const void *b)
+{
+ return key_value(a) == key_value(b);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct set *set;
+ struct set_entry *entry;
+ int size = 10000;
+ uint32_t keys[size];
+ uint32_t i;
+
+ set = set_create(uint32_t_key_equals);
+
+ for (i = 0; i < size; i++) {
+ keys[i] = i;
+
+ set_add(set, i, &keys[i]);
+
+ if (i >= 100) {
+ uint32_t delete_value = i - 100;
+ entry = set_search(set, delete_value, &delete_value);
+ set_remove(set, entry);
+ }
+ }
+
+ /* Make sure that all our entries were present at the end. */
+ for (i = size - 100; i < size; i++) {
+ entry = set_search(set, i, &keys[i]);
+ assert(entry);
+ assert(key_value(entry->key) == i);
+ }
+
+ /* Make sure that no extra entries got in */
+ for (entry = set_next_entry(set, NULL);
+ entry != NULL;
+ entry = set_next_entry(set, entry)) {
+ assert(key_value(entry->key) >= size - 100 &&
+ key_value(entry->key) < size);
+ }
+ assert(set->entries == 100);
+
+ set_destroy(set, NULL);
+
+ return 0;
+}
diff --git a/tests/set/destroy_callback.c b/tests/set/destroy_callback.c
new file mode 100644
index 0000000..6dff3e1
--- /dev/null
+++ b/tests/set/destroy_callback.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "set.h"
+#include "fnv_hash.h"
+
+static const char *str1 = "test1";
+static const char *str2 = "test2";
+static int delete_str1 = 0;
+static int delete_str2 = 0;
+
+static void
+delete_callback(struct set_entry *entry)
+{
+ if (strcmp(entry->key, str1) == 0)
+ delete_str1 = 1;
+ else if (strcmp(entry->key, str2) == 0)
+ delete_str2 = 1;
+ else
+ abort();
+}
+
+int
+main(int argc, char **argv)
+{
+ struct set *set;
+ uint32_t hash_str1 = fnv1_hash_string(str1);
+ uint32_t hash_str2 = fnv1_hash_string(str2);
+
+ set = set_create(string_key_equals);
+
+ set_add(set, hash_str1, str1);
+ set_add(set, hash_str2, str2);
+
+ set_destroy(set, delete_callback);
+
+ assert(delete_str1 && delete_str2);
+
+ return 0;
+}
diff --git a/tests/set/insert_and_lookup.c b/tests/set/insert_and_lookup.c
new file mode 100644
index 0000000..367daac
--- /dev/null
+++ b/tests/set/insert_and_lookup.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "set.h"
+#include "fnv_hash.h"
+
+int
+main(int argc, char **argv)
+{
+ struct set *set;
+ const char *str1 = "test1";
+ const char *str2 = "test2";
+ uint32_t hash_str1 = fnv1_hash_string(str1);
+ uint32_t hash_str2 = fnv1_hash_string(str2);
+ struct set_entry *entry;
+
+ set = set_create(string_key_equals);
+
+ set_add(set, hash_str1, str1);
+ set_add(set, hash_str2, str2);
+
+ entry = set_search(set, hash_str1, str1);
+ assert(strcmp(entry->key, str1) == 0);
+
+ entry = set_search(set, hash_str2, str2);
+ assert(strcmp(entry->key, str2) == 0);
+
+ set_destroy(set, NULL);
+
+ return 0;
+}
diff --git a/tests/set/insert_many.c b/tests/set/insert_many.c
new file mode 100644
index 0000000..4c8dfd0
--- /dev/null
+++ b/tests/set/insert_many.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "set.h"
+#include "fnv_hash.h"
+
+static uint32_t
+key_value(const void *key)
+{
+ return *(const uint32_t *)key;
+}
+
+static int
+uint32_t_key_equals(const void *a, const void *b)
+{
+ return key_value(a) == key_value(b);
+}
+
+int
+main(int argc, char **argv)
+{
+ struct set *set;
+ struct set_entry *entry;
+ int size = 10000;
+ uint32_t keys[size];
+ uint32_t i;
+
+ set = set_create(uint32_t_key_equals);
+
+ for (i = 0; i < size; i++) {
+ keys[i] = i;
+
+ set_add(set, i, keys + i);
+ }
+
+ for (i = 0; i < size; i++) {
+ entry = set_search(set, i, keys + i);
+ assert(entry);
+ assert(key_value(entry->key) == i);
+ }
+ assert(set->entries == size);
+
+ set_destroy(set, NULL);
+
+ return 0;
+}
diff --git a/tests/set/null_destroy.c b/tests/set/null_destroy.c
new file mode 100644
index 0000000..cf7fc7a
--- /dev/null
+++ b/tests/set/null_destroy.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright © 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "set.h"
+
+int
+main(int argc, char **argv)
+{
+ set_destroy(NULL, NULL);
+
+ return 0;
+}
diff --git a/tests/set/null_remove.c b/tests/set/null_remove.c
new file mode 100644
index 0000000..e607062
--- /dev/null
+++ b/tests/set/null_remove.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "set.h"
+#include "fnv_hash.h"
+
+int
+main(int argc, char **argv)
+{
+ struct set *set;
+
+ set = set_create(string_key_equals);
+
+ set_remove(set, NULL);
+
+ set_destroy(set, NULL);
+
+ return 0;
+}
diff --git a/tests/set/random_entry.c b/tests/set/random_entry.c
new file mode 100644
index 0000000..371d02f
--- /dev/null
+++ b/tests/set/random_entry.c
@@ -0,0 +1,89 @@
+/*
+ * Copyrigset © 2009 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rigsets to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyrigset notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGSET HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "set.h"
+#include "fnv_hash.h"
+
+static uint32_t
+key_value(const void *key)
+{
+ return *(const uint32_t *)key;
+}
+
+static int
+uint32_t_key_equals(const void *a, const void *b)
+{
+ return key_value(a) == key_value(b);
+}
+
+static int
+uint32_t_key_is_even(struct set_entry *entry)
+{
+ return (key_value(entry->key) & 1) == 0;
+}
+
+int
+main(int argc, char **argv)
+{
+ struct set *set;
+ struct set_entry *entry;
+ int size = 10000;
+ uint32_t keys[size];
+ uint32_t i, random_value;
+
+ set = set_create(uint32_t_key_equals);
+
+ for (i = 0; i < size; i++) {
+ keys[i] = i;
+
+ set_add(set, i, keys + i);
+ }
+
+ /* Test the no-predicate case. */
+ entry = set_random_entry(set, NULL);
+ assert(entry);
+
+ /* Check that we're getting different entries and that the predicate
+ * works.
+ */
+ for (i = 0; i < 100; i++) {
+ entry = set_random_entry(set, uint32_t_key_is_even);
+ assert(entry);
+ assert((key_value(entry->key) & 1) == 0);
+ if (i == 0 || key_value(entry->key) != random_value)
+ break;
+ random_value = key_value(entry->key);
+ }
+ assert(i != 100);
+
+ set_destroy(set, NULL);
+
+ return 0;
+}
diff --git a/tests/set/replacement.c b/tests/set/replacement.c
new file mode 100644
index 0000000..1d6d924
--- /dev/null
+++ b/tests/set/replacement.c
@@ -0,0 +1,63 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric@anholt.net>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include "set.h"
+#include "fnv_hash.h"
+
+int
+main(int argc, char **argv)
+{
+ struct set *set;
+ char *str1 = strdup("test1");
+ char *str2 = strdup("test1");
+ uint32_t hash_str1 = fnv1_hash_string(str1);
+ uint32_t hash_str2 = fnv1_hash_string(str2);
+ struct set_entry *entry;
+
+ set = set_create(string_key_equals);
+
+ set_add(set, hash_str1, str1);
+ set_add(set, hash_str2, str2);
+
+ entry = set_search(set, hash_str1, str1);
+ assert(entry);
+ assert(entry->key == str2);
+
+ set_remove(set, entry);
+
+ entry = set_search(set, hash_str1, str1);
+ assert(!entry);
+
+ set_destroy(set, NULL);
+ free(str1);
+ free(str2);
+
+ return 0;
+}