summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorAndrew Shadura <bugzilla@tut.by>2011-11-17 19:46:12 +0100
committerAndrew Shadura <bugzilla@tut.by>2011-11-17 19:46:12 +0100
commit1de62163f0237e2fdd7d9b5ee7c29c57851ce87e (patch)
treea95ba9fa7b572f833e2d360d4fa03806d80407d2 /src/examples
initial import
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/Makefile3
-rw-r--r--src/examples/dicttest/Makefile7
-rw-r--r--src/examples/dicttest/dicttest.c79
-rw-r--r--src/examples/formattertest/Makefile7
-rw-r--r--src/examples/formattertest/formattertest.c46
-rw-r--r--src/examples/listsort/Makefile7
-rw-r--r--src/examples/listsort/listsort.c117
-rw-r--r--src/examples/patriciatest/Makefile7
-rw-r--r--src/examples/patriciatest/patriciatest.c157
-rw-r--r--src/examples/patriciatest2/Makefile7
-rw-r--r--src/examples/patriciatest2/patriciatest2.c153
-rw-r--r--src/examples/randomtest/Makefile7
-rw-r--r--src/examples/randomtest/randomtest.c51
13 files changed, 648 insertions, 0 deletions
diff --git a/src/examples/Makefile b/src/examples/Makefile
new file mode 100644
index 0000000..d56d9ad
--- /dev/null
+++ b/src/examples/Makefile
@@ -0,0 +1,3 @@
+SUBDIRS = randomtest listsort formattertest dicttest patriciatest patriciatest2
+
+include ../../buildsys.mk
diff --git a/src/examples/dicttest/Makefile b/src/examples/dicttest/Makefile
new file mode 100644
index 0000000..f018538
--- /dev/null
+++ b/src/examples/dicttest/Makefile
@@ -0,0 +1,7 @@
+PROG_NOINST = dicttest${PROG_SUFFIX}
+SRCS = dicttest.c
+
+include ../../../buildsys.mk
+
+CPPFLAGS += -I../../libmowgli
+LIBS += -L../../libmowgli -lmowgli
diff --git a/src/examples/dicttest/dicttest.c b/src/examples/dicttest/dicttest.c
new file mode 100644
index 0000000..c5eaec5
--- /dev/null
+++ b/src/examples/dicttest/dicttest.c
@@ -0,0 +1,79 @@
+/*
+ * libmowgli: A collection of useful routines for programming.
+ * dicttest.c: Testing of the mowgli.dictionary engine.
+ *
+ * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <mowgli.h>
+
+#ifdef _WIN32
+#define strcasecmp _stricmp
+#define snprintf _snprintf
+#endif
+
+int main(int argc, const char *argv[])
+{
+ mowgli_dictionary_t *test_dict;
+ mowgli_random_t *r;
+ char key[10];
+ long ans[100], i;
+ int pass = 0, fail = 0;
+
+ mowgli_init();
+
+ test_dict = mowgli_dictionary_create(strcasecmp);
+ r = mowgli_random_create();
+
+ for (i = 0; i < 100; i++)
+ {
+ ans[i] = mowgli_random_int(r);
+ snprintf(key, 10, "%ldkey%ld", i, i);
+ mowgli_dictionary_add(test_dict, key, (void *) ans[i]);
+ }
+
+ for (i = 0; i < 100; i++)
+ {
+ snprintf(key, 10, "%ldkey%ld", i, i);
+
+ if ( (long) mowgli_dictionary_retrieve(test_dict, key) != ans[i])
+ {
+ printf("FAIL %ld %p[%p]\n", i, mowgli_dictionary_retrieve(test_dict, key), (void*) ans[i]);
+ fail++;
+ }
+ else
+ {
+ printf("PASS %ld %p[%p]\n", i, mowgli_dictionary_retrieve(test_dict, key), (void*) ans[i]);
+ pass++;
+ }
+ }
+
+ printf("%d tests failed, %d tests passed.\n", fail, pass);
+ return 0;
+}
diff --git a/src/examples/formattertest/Makefile b/src/examples/formattertest/Makefile
new file mode 100644
index 0000000..23c4517
--- /dev/null
+++ b/src/examples/formattertest/Makefile
@@ -0,0 +1,7 @@
+PROG_NOINST = formattertest${PROG_SUFFIX}
+SRCS = formattertest.c
+
+include ../../../buildsys.mk
+
+CPPFLAGS += -I../../libmowgli
+LIBS += -L../../libmowgli -lmowgli
diff --git a/src/examples/formattertest/formattertest.c b/src/examples/formattertest/formattertest.c
new file mode 100644
index 0000000..26a6e4b
--- /dev/null
+++ b/src/examples/formattertest/formattertest.c
@@ -0,0 +1,46 @@
+/*
+ * libmowgli: A collection of useful routines for programming.
+ * formattertest.c: Testsuite for mowgli.formatter.
+ *
+ * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <mowgli.h>
+
+int main(int argc, char *argv[])
+{
+ char buf[65535];
+ mowgli_init();
+
+ mowgli_formatter_format(buf, 65535, "%1! %2 %3 %4.", "sdpb", "Hello World", 1, 0xDEADBEEF, TRUE);
+
+ puts(buf);
+
+ return 0;
+}
diff --git a/src/examples/listsort/Makefile b/src/examples/listsort/Makefile
new file mode 100644
index 0000000..2517a77
--- /dev/null
+++ b/src/examples/listsort/Makefile
@@ -0,0 +1,7 @@
+PROG_NOINST = listsort${PROG_SUFFIX}
+SRCS = listsort.c
+
+include ../../../buildsys.mk
+
+CPPFLAGS += -I../../libmowgli
+LIBS += -L../../libmowgli -lmowgli
diff --git a/src/examples/listsort/listsort.c b/src/examples/listsort/listsort.c
new file mode 100644
index 0000000..43b9745
--- /dev/null
+++ b/src/examples/listsort/listsort.c
@@ -0,0 +1,117 @@
+/*
+ * libmowgli: A collection of useful routines for programming.
+ * listsort.c: Testing of the list sorting routine.
+ *
+ * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <mowgli.h>
+
+#ifdef _WIN32
+#define strcasecmp _stricmp
+#endif
+
+int str_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque)
+{
+ int ret;
+ ret = strcasecmp(n->data, n2->data);
+
+ return ret;
+}
+
+void test_strings(void)
+{
+ mowgli_list_t l = { NULL, NULL, 0 };
+ mowgli_node_t *n, *tn;
+
+ mowgli_node_add("foo", mowgli_node_create(), &l);
+ mowgli_node_add("bar", mowgli_node_create(), &l);
+ mowgli_node_add("baz", mowgli_node_create(), &l);
+ mowgli_node_add("splork", mowgli_node_create(), &l);
+ mowgli_node_add("rabbit", mowgli_node_create(), &l);
+ mowgli_node_add("meow", mowgli_node_create(), &l);
+ mowgli_node_add("hi", mowgli_node_create(), &l);
+ mowgli_node_add("konnichiwa", mowgli_node_create(), &l);
+ mowgli_node_add("absolutely", mowgli_node_create(), &l);
+ mowgli_node_add("cat", mowgli_node_create(), &l);
+ mowgli_node_add("dog", mowgli_node_create(), &l);
+ mowgli_node_add("woof", mowgli_node_create(), &l);
+ mowgli_node_add("moon", mowgli_node_create(), &l);
+ mowgli_node_add("new", mowgli_node_create(), &l);
+ mowgli_node_add("delete", mowgli_node_create(), &l);
+ mowgli_node_add("alias", mowgli_node_create(), &l);
+
+ mowgli_list_sort(&l, str_comparator, NULL);
+
+ printf("\nString test results\n");
+
+ MOWGLI_LIST_FOREACH_SAFE(n, tn, l.head)
+ {
+ printf(" %s\n", (char*) n->data);
+ mowgli_node_delete(n, &l);
+ }
+}
+
+int int_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque)
+{
+ long a = (long) n->data;
+ long b = (long) n2->data;
+
+ return a - b;
+}
+
+void test_integers(void)
+{
+ mowgli_list_t l = { NULL, NULL, 0 };
+ mowgli_node_t *n, *tn;
+
+ mowgli_node_add((void *) 3, mowgli_node_create(), &l);
+ mowgli_node_add((void *) 2, mowgli_node_create(), &l);
+ mowgli_node_add((void *) 4, mowgli_node_create(), &l);
+ mowgli_node_add((void *) 1, mowgli_node_create(), &l);
+
+ mowgli_list_sort(&l, int_comparator, NULL);
+
+ printf("\nInteger test results\n");
+
+ MOWGLI_LIST_FOREACH_SAFE(n, tn, l.head)
+ {
+ printf(" %ld\n", (long) n->data);
+ mowgli_node_delete(n, &l);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ mowgli_init();
+
+ test_strings();
+ test_integers();
+ return EXIT_SUCCESS;
+}
diff --git a/src/examples/patriciatest/Makefile b/src/examples/patriciatest/Makefile
new file mode 100644
index 0000000..6669293
--- /dev/null
+++ b/src/examples/patriciatest/Makefile
@@ -0,0 +1,7 @@
+PROG_NOINST = patriciatest${PROG_SUFFIX}
+SRCS = patriciatest.c
+
+include ../../../buildsys.mk
+
+CPPFLAGS += -I../../libmowgli
+LIBS += -L../../libmowgli -lmowgli
diff --git a/src/examples/patriciatest/patriciatest.c b/src/examples/patriciatest/patriciatest.c
new file mode 100644
index 0000000..bf5f641
--- /dev/null
+++ b/src/examples/patriciatest/patriciatest.c
@@ -0,0 +1,157 @@
+/*
+ * libmowgli: A collection of useful routines for programming.
+ * patriciatest.c: Testing of the patricia tree.
+ *
+ * Copyright (c) 2008 Jilles Tjoelker
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <mowgli.h>
+
+int errors = 0;
+
+void str_canon(char *key)
+{
+ return;
+}
+
+void statscb(const char *line, void *data)
+{
+ printf("%s\n", line);
+}
+
+/* assumes data is key */
+static void check_all_retrievable(mowgli_patricia_t *dtree)
+{
+ mowgli_patricia_iteration_state_t state;
+ void *elem, *elem2;
+ unsigned int n1 = 0, n2;
+
+ mowgli_patricia_stats(dtree, statscb, NULL);
+ printf("Checking consistency...");
+ fflush(stdout);
+ n2 = mowgli_patricia_size(dtree);
+ MOWGLI_PATRICIA_FOREACH(elem, &state, dtree)
+ {
+ elem2 = mowgli_patricia_retrieve(dtree, (const char *)elem);
+ if (elem2 == NULL)
+ {
+ errors++;
+ printf("failed to find element %s\n",
+ (const char *)elem);
+ }
+ else if (strcmp(elem2, elem))
+ {
+ printf("element %s != %s\n",
+ (const char *)elem,
+ (const char *)elem2);
+ errors++;
+ }
+ else
+ printf(".");
+ fflush(stdout);
+ n1++;
+ if (n1 > n2 * 2)
+ break;
+ }
+ if (n1 != n2)
+ {
+ errors++;
+ printf("number of iterated elements %u != size %u\n", n1, n2);
+ }
+ printf("\n");
+ fflush(stdout);
+}
+
+void test_patricia(void)
+{
+ mowgli_patricia_t *dtree;
+ mowgli_patricia_iteration_state_t state;
+ void *elem;
+
+ dtree = mowgli_patricia_create(str_canon);
+#define ADD(x) printf("Adding %s\n", x); mowgli_patricia_add(dtree, x, x); check_all_retrievable(dtree)
+ ADD("\1\1");
+ ADD("alias");
+ ADD("\377");
+ ADD("\377\377\377");
+ ADD("foo");
+ ADD("bar");
+ ADD("baz");
+ ADD("splork");
+ ADD("rabbit");
+ ADD("\1");
+ ADD("meow");
+ ADD("hi");
+ ADD("konnichiwa");
+ ADD("absolutely");
+ ADD("cat");
+ ADD("dog");
+ ADD("woof");
+ ADD("moon");
+ ADD("new");
+ ADD("delete");
+
+ MOWGLI_PATRICIA_FOREACH(elem, &state, dtree)
+ {
+ printf("element -> %s\n", (const char *)elem);
+ }
+ printf("End of elements\n");
+ mowgli_patricia_stats(dtree, statscb, NULL);
+
+ check_all_retrievable(dtree);
+
+#define TESTRETRIEVE(x) elem = mowgli_patricia_retrieve(dtree, x); printf("element %s: %s\n", x, elem ? (errors++, "YES") : "NO")
+ TESTRETRIEVE("meows");
+ TESTRETRIEVE("meo");
+ TESTRETRIEVE("deletes");
+ TESTRETRIEVE("z");
+ TESTRETRIEVE("0");
+
+#define TESTDELETE(x) mowgli_patricia_delete(dtree, x); elem = mowgli_patricia_retrieve(dtree, x); printf("deleting %s: %s\n", x, elem ? (errors++, "STILL PRESENT") : "GONE"); check_all_retrievable(dtree)
+ TESTDELETE("YYY");
+ TESTDELETE("foo");
+ TESTDELETE("splork");
+ ADD("spork");
+ ADD("foo");
+ TESTDELETE("absolutely");
+ TESTDELETE("cat");
+ ADD("absolutely");
+ TESTDELETE("dog");
+
+ mowgli_patricia_destroy(dtree, NULL, NULL);
+}
+
+int main(int argc, char *argv[])
+{
+ mowgli_init();
+
+ test_patricia();
+
+ return errors == 0 ? 0 : 1;
+}
diff --git a/src/examples/patriciatest2/Makefile b/src/examples/patriciatest2/Makefile
new file mode 100644
index 0000000..cb7ce12
--- /dev/null
+++ b/src/examples/patriciatest2/Makefile
@@ -0,0 +1,7 @@
+PROG_NOINST = patriciatest2${PROG_SUFFIX}
+SRCS = patriciatest2.c
+
+include ../../../buildsys.mk
+
+CPPFLAGS += -I../../libmowgli
+LIBS += -L../../libmowgli -lmowgli
diff --git a/src/examples/patriciatest2/patriciatest2.c b/src/examples/patriciatest2/patriciatest2.c
new file mode 100644
index 0000000..de70ac2
--- /dev/null
+++ b/src/examples/patriciatest2/patriciatest2.c
@@ -0,0 +1,153 @@
+/*
+ * libmowgli: A collection of useful routines for programming.
+ * patriciatest2.c: More testing of the patricia tree.
+ *
+ * Copyright (c) 2008-2010 Jilles Tjoelker
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <mowgli.h>
+
+#define TESTSIZE 10000
+
+int errors = 0;
+
+void str_canon(char *key)
+{
+ return;
+}
+
+void statscb(const char *line, void *data)
+{
+}
+
+/* assumes data is key */
+static void check_all_retrievable(mowgli_patricia_t *dtree)
+{
+ mowgli_patricia_iteration_state_t state;
+ void *elem, *elem2;
+ unsigned int n1 = 0, n2;
+
+ mowgli_patricia_stats(dtree, statscb, NULL);
+ n2 = mowgli_patricia_size(dtree);
+ MOWGLI_PATRICIA_FOREACH(elem, &state, dtree)
+ {
+ elem2 = mowgli_patricia_retrieve(dtree, (const char *)elem);
+ if (elem2 == NULL)
+ {
+ errors++;
+ printf("failed to find element %s\n",
+ (const char *)elem);
+ }
+ else if (strcmp(elem2, elem))
+ {
+ printf("element %s != %s\n",
+ (const char *)elem,
+ (const char *)elem2);
+ errors++;
+ }
+ n1++;
+ if (n1 > n2 * 2)
+ break;
+ }
+ if (n1 != n2)
+ {
+ errors++;
+ printf("number of iterated elements %u != size %u\n", n1, n2);
+ }
+}
+
+void test_patricia(void)
+{
+ mowgli_patricia_t *dtree;
+ int i, j;
+ char buf[100], *strings[TESTSIZE];
+
+ srandom(12346);
+ for (i = 0; i < TESTSIZE; i++)
+ {
+ for (j = 0; j < 40; j++)
+ buf[j] = 'a' + random() % 26;
+ buf[20 + random() % 20] = '\0';
+ strings[i] = strdup(buf);
+ }
+
+ dtree = mowgli_patricia_create(str_canon);
+
+ for (i = 0; i < TESTSIZE; i++)
+ {
+ mowgli_patricia_add(dtree, strings[i], strings[i]);
+ check_all_retrievable(dtree);
+ }
+
+ check_all_retrievable(dtree);
+
+ for (i = 0; i < TESTSIZE / 2; i++)
+ {
+ mowgli_patricia_delete(dtree, strings[i]);
+ if (mowgli_patricia_retrieve(dtree, strings[i]))
+ {
+ printf("still retrievable after delete: %s\n",
+ strings[i]);
+ errors++;
+ }
+ check_all_retrievable(dtree);
+ }
+
+ for (i = 0; i < TESTSIZE / 2; i++)
+ {
+ mowgli_patricia_add(dtree, strings[i], strings[i]);
+ check_all_retrievable(dtree);
+ }
+
+ for (i = 0; i < TESTSIZE; i++)
+ {
+ mowgli_patricia_delete(dtree, strings[i]);
+ if (mowgli_patricia_retrieve(dtree, strings[i]))
+ {
+ printf("still retrievable after delete: %s\n",
+ strings[i]);
+ errors++;
+ }
+ check_all_retrievable(dtree);
+ }
+
+ mowgli_patricia_destroy(dtree, NULL, NULL);
+
+ for (i = 0; i < TESTSIZE; i++)
+ free(strings[i]);
+}
+
+int main(int argc, char *argv[])
+{
+ mowgli_init();
+
+ test_patricia();
+
+ return errors == 0 ? 0 : 1;
+}
diff --git a/src/examples/randomtest/Makefile b/src/examples/randomtest/Makefile
new file mode 100644
index 0000000..b64193e
--- /dev/null
+++ b/src/examples/randomtest/Makefile
@@ -0,0 +1,7 @@
+PROG_NOINST = randomtest${PROG_SUFFIX}
+SRCS = randomtest.c
+
+include ../../../buildsys.mk
+
+CPPFLAGS += -I../../libmowgli
+LIBS += -L../../libmowgli -lmowgli
diff --git a/src/examples/randomtest/randomtest.c b/src/examples/randomtest/randomtest.c
new file mode 100644
index 0000000..2c4b2d7
--- /dev/null
+++ b/src/examples/randomtest/randomtest.c
@@ -0,0 +1,51 @@
+/*
+ * libmowgli: A collection of useful routines for programming.
+ * randomtest.c: Testsuite for the random number generator.
+ *
+ * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
+ * Algorithm copyright (c) 1999-2007 Takuji Nishimura and Makoto Matsumoto
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <mowgli.h>
+
+int main(int argc, char *argv[])
+{
+ mowgli_random_t *r = mowgli_random_create();
+ int i;
+
+ printf("1000 iterations:\n");
+ for (i = 0; i < 1000; i++)
+ {
+ printf("%10u ", mowgli_random_int(r));
+ if (i % 5 == 4) printf("\n");
+ }
+
+ mowgli_object_unref(r);
+
+ return 0;
+}