summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorCarl Worth <cworth@cworth.org>2013-10-24 15:02:53 -0700
committerEric Anholt <eric@anholt.net>2013-10-25 15:17:59 -0700
commitf9d22af764336897158a3477ab648a5bdaa26cf2 (patch)
tree5ef8e1fccfb0a46d50faa5933c209b14c9fdf4e2 /tests
parentdd09488648767bcda8d75f417baca1f7b7940ff1 (diff)
Add a new int-set structure for a set of integers
Much like the motivation for the original set.c, this int-set.c code is not difficult, but it makes sense to do this up-front in the original hash_table module (with testing) rather than expecting users to repeat this work.
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/int-set/.gitignore11
-rw-r--r--tests/int-set/Makefile.am36
-rw-r--r--tests/int-set/delete_and_lookup.c69
-rw-r--r--tests/int-set/delete_management.c74
-rw-r--r--tests/int-set/insert_and_lookup.c56
-rw-r--r--tests/int-set/insert_many.c58
-rw-r--r--tests/int-set/null_destroy.c38
-rw-r--r--tests/int-set/null_remove.c46
-rw-r--r--tests/int-set/random_entry.c74
-rw-r--r--tests/int-set/replacement.c58
11 files changed, 521 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index edffe40..f280ee3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -19,7 +19,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-SUBDIRS = set
+SUBDIRS = set int-set
AM_CFLAGS = $(WARN_CFLAGS) -I..
LDADD = ../libhash_table.la
diff --git a/tests/int-set/.gitignore b/tests/int-set/.gitignore
new file mode 100644
index 0000000..2aeb095
--- /dev/null
+++ b/tests/int-set/.gitignore
@@ -0,0 +1,11 @@
+*.log
+*.trs
+delete_and_lookup
+delete_management
+destroy_callback
+insert_and_lookup
+insert_many
+null_destroy
+null_remove
+random_entry
+replacement
diff --git a/tests/int-set/Makefile.am b/tests/int-set/Makefile.am
new file mode 100644
index 0000000..23c1e5b
--- /dev/null
+++ b/tests/int-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
+# 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.
+
+AM_CFLAGS = $(WARN_CFLAGS) -I../..
+LDADD = ../../libint-set.la
+
+TESTS = \
+ delete_and_lookup \
+ delete_management \
+ insert_and_lookup \
+ insert_many \
+ null_destroy \
+ null_remove \
+ random_entry \
+ replacement \
+ $()
+
+EXTRA_PROGRAMS = $(TESTS)
diff --git a/tests/int-set/delete_and_lookup.c b/tests/int-set/delete_and_lookup.c
new file mode 100644
index 0000000..8810b5a
--- /dev/null
+++ b/tests/int-set/delete_and_lookup.c
@@ -0,0 +1,69 @@
+/*
+ * Copyright © 2009,2013 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>
+ * Carl Worth <cworth@cworth.org>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "int-set.h"
+
+int
+main(int argc, char **argv)
+{
+ struct int_set *set;
+
+ /* Use two values with the lowest bits in common so they will
+ * hash to the same initial entry, so we can test the deletion
+ * behavior for chained objects. */
+ uint32_t value1 = 0x00000123;
+ uint32_t value2 = 0x10000123;
+ struct int_set_entry *entry;
+
+ set = int_set_create();
+
+ int_set_add(set, value1);
+ int_set_add(set, value2);
+
+ entry = int_set_search(set, value1);
+ assert(entry->value == value1);
+
+ entry = int_set_search(set, value2);
+ assert(entry->value == value2);
+
+ int_set_remove(set, entry);
+
+ entry = int_set_search(set, value2);
+ assert(entry == NULL);
+
+ entry = int_set_search(set, value1);
+ assert(entry->value == value1);
+
+ int_set_destroy(set);
+
+ return 0;
+}
diff --git a/tests/int-set/delete_management.c b/tests/int-set/delete_management.c
new file mode 100644
index 0000000..cc0ea9d
--- /dev/null
+++ b/tests/int-set/delete_management.c
@@ -0,0 +1,74 @@
+/*
+ * 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 "int-set.h"
+
+int
+main(int argc, char **argv)
+{
+ struct int_set *set;
+ struct int_set_entry *entry;
+ int size = 10000;
+ uint32_t keys[size];
+ uint32_t i;
+
+ set = int_set_create();
+
+ for (i = 0; i < size; i++) {
+ int_set_add(set, i);
+
+ if (i >= 100) {
+ uint32_t delete_value = i - 100;
+ entry = int_set_search(set, delete_value);
+ int_set_remove(set, entry);
+ }
+ }
+
+ /* Make sure that all our entries were present at the end. */
+ for (i = size - 100; i < size; i++) {
+ entry = int_set_search(set, i);
+ assert(entry);
+ assert(entry->value == i);
+ }
+
+ /* Make sure that no extra entries got in */
+ for (entry = int_set_next_entry(set, NULL);
+ entry != NULL;
+ entry = int_set_next_entry(set, entry)) {
+ assert(entry->value >= size - 100 &&
+ entry->value < size);
+ }
+ assert(set->entries == 100);
+
+ int_set_destroy(set);
+
+ return 0;
+}
diff --git a/tests/int-set/insert_and_lookup.c b/tests/int-set/insert_and_lookup.c
new file mode 100644
index 0000000..575aac7
--- /dev/null
+++ b/tests/int-set/insert_and_lookup.c
@@ -0,0 +1,56 @@
+/*
+ * 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 "int-set.h"
+
+int
+main(int argc, char **argv)
+{
+ struct int_set *set;
+ uint32_t value1 = 0x14b5fbc8;
+ uint32_t value2 = 0x5303d1ff;
+ struct int_set_entry *entry;
+
+ set = int_set_create();
+
+ int_set_add(set, value1);
+ int_set_add(set, value2);
+
+ entry = int_set_search(set, value1);
+ assert(entry->value == value1);
+
+ entry = int_set_search(set, value2);
+ assert(entry->value == value2);
+
+ int_set_destroy(set);
+
+ return 0;
+}
diff --git a/tests/int-set/insert_many.c b/tests/int-set/insert_many.c
new file mode 100644
index 0000000..99ad9ea
--- /dev/null
+++ b/tests/int-set/insert_many.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 "int-set.h"
+
+int
+main(int argc, char **argv)
+{
+ struct int_set *set;
+ struct int_set_entry *entry;
+ int size = 1;
+ uint32_t i;
+
+ set = int_set_create();
+
+ for (i = 0; i < size; i++) {
+ int_set_add(set, i);
+ }
+
+ for (i = 0; i < size; i++) {
+ entry = int_set_search(set, i);
+ assert(entry);
+ assert(entry->value == i);
+ }
+ assert(set->entries == size);
+
+ int_set_destroy(set);
+
+ return 0;
+}
diff --git a/tests/int-set/null_destroy.c b/tests/int-set/null_destroy.c
new file mode 100644
index 0000000..09e1aea
--- /dev/null
+++ b/tests/int-set/null_destroy.c
@@ -0,0 +1,38 @@
+/*
+ * 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 "int-set.h"
+
+int
+main(int argc, char **argv)
+{
+ int_set_destroy(NULL);
+
+ return 0;
+}
diff --git a/tests/int-set/null_remove.c b/tests/int-set/null_remove.c
new file mode 100644
index 0000000..318c570
--- /dev/null
+++ b/tests/int-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 "int-set.h"
+
+int
+main(int argc, char **argv)
+{
+ struct int_set *set;
+
+ set = int_set_create();
+
+ int_set_remove(set, NULL);
+
+ int_set_destroy(set);
+
+ return 0;
+}
diff --git a/tests/int-set/random_entry.c b/tests/int-set/random_entry.c
new file mode 100644
index 0000000..204595e
--- /dev/null
+++ b/tests/int-set/random_entry.c
@@ -0,0 +1,74 @@
+/*
+ * 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 "int-set.h"
+
+static int
+uint32_t_key_is_even(struct int_set_entry *entry)
+{
+ return (entry->value & 1) == 0;
+}
+
+int
+main(int argc, char **argv)
+{
+ struct int_set *set;
+ struct int_set_entry *entry;
+ int size = 10000;
+ uint32_t i, random_value = 0;
+
+ set = int_set_create();
+
+ for (i = 0; i < size; i++) {
+ int_set_add(set, i);
+ }
+
+ /* Test the no-predicate case. */
+ entry = int_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 = int_set_random_entry(set, uint32_t_key_is_even);
+ assert(entry);
+ assert((entry->value & 1)== 0);
+ if (i == 0 || entry->value != random_value)
+ break;
+ random_value = entry->value;
+ }
+ assert(i != 100);
+
+ int_set_destroy(set);
+
+ return 0;
+}
diff --git a/tests/int-set/replacement.c b/tests/int-set/replacement.c
new file mode 100644
index 0000000..71436bb
--- /dev/null
+++ b/tests/int-set/replacement.c
@@ -0,0 +1,58 @@
+/*
+ * 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 "int-set.h"
+
+int
+main(int argc, char **argv)
+{
+ struct int_set *set;
+ uint32_t value = 0x11f9feca;
+ struct int_set_entry *entry;
+
+ set = int_set_create();
+
+ int_set_add(set, value);
+ int_set_add(set, value);
+
+ entry = int_set_search(set, value);
+ assert(entry);
+ assert(entry->value == value);
+
+ int_set_remove(set, entry);
+
+ entry = int_set_search(set, value);
+ assert(!entry);
+
+ int_set_destroy(set);
+
+ return 0;
+}