diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/ampatree_test.c | 115 | ||||
-rw-r--r-- | test/bitset_test.c | 82 | ||||
-rw-r--r-- | test/trie_test.c | 115 |
3 files changed, 312 insertions, 0 deletions
diff --git a/test/ampatree_test.c b/test/ampatree_test.c new file mode 100644 index 0000000..94379f6 --- /dev/null +++ b/test/ampatree_test.c @@ -0,0 +1,115 @@ +/* + Copyright 2011-2016 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include <stdarg.h> +#include <stdio.h> +#include <string.h> + +#include "zix/ampatree.h" + +static const char* strings[] = { + "http://example.org/foo", + "http://example.org/bar", + "http://example.org/baz", + "http://example.net/foo", + "http://example.net/bar", + "http://example.net/baz", + "http://drobilla.net/", + "http://drobilla.net/software/zix", + "http://www.gbengasesan.com/blog", + "http://www.gbengasesan.com", + "http://echo.jpl.nasa.gov/~lance/delta_v/delta_v.rendezvous.html", + "http://echo.jpl.nasa.gov/asteroids/1986da/1986DA.html", + "http://echo.jpl.nasa.gov/", + "http://echo.jpl.nasa.gov/asteroids/1620_Geographos/geographos.html", + "http://echo.jpl.nasa.gov/~ostro/KY26/", + "http://echo.jpl.nasa.gov/~ostro/KY26/JPL_press_release.text", + "http://echo.jpl.nasa.gov", + "http://echo.jpl.nasa.gov/asteroids/4179_Toutatis/toutatis.html", + "http://echo.jpl.nasa.gov/asteroids/4769_Castalia/cast01.html", + "http://echo.jpl.nasa.gov/publications/review_abs.html", +}; + +static int +test_fail(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + va_end(args); + return 1; +} + +int +main(int argc, char** argv) +{ + ZixAMPatree* patree = zix_ampatree_new(); + + const size_t n_strings = sizeof(strings) / sizeof(char*); + + // Insert each string + for (size_t i = 0; i < n_strings; ++i) { + ZixStatus st = zix_ampatree_insert(patree, strings[i], strlen(strings[i])); + if (st) { + return test_fail("Failed to insert `%s'\n", strings[i]); + } + } + + FILE* dot_file = fopen("ampatree.dot", "w"); + zix_ampatree_print_dot(patree, dot_file); + fclose(dot_file); + + // Attempt to insert each string again + for (size_t i = 0; i < n_strings; ++i) { + ZixStatus st = zix_ampatree_insert(patree, strings[i], strlen(strings[i])); + if (st != ZIX_STATUS_EXISTS) { + return test_fail("Double inserted `%s'\n", strings[i]); + } + } + + // Search for each string + for (size_t i = 0; i < n_strings; ++i) { + const char* match = NULL; + ZixStatus st = zix_ampatree_find(patree, strings[i], &match); + if (st) { + return test_fail("Failed to find `%s'\n", strings[i]); + } + if (match != strings[i]) { + return test_fail("Bad match for `%s'\n", strings[i]); + } + } + + // Try some false matches + const char* not_indexed[] = { + "ftp://example.org/not-there-at-all", + "http://example.org/foobar", + "http://", + "http://otherdomain.com" + }; + const size_t n_not_indexed = sizeof(not_indexed) / sizeof(char*); + for (size_t i = 0; i < n_not_indexed; ++i) { + const char* match = NULL; + ZixStatus st = zix_ampatree_find(patree, not_indexed[i], &match); + if (st != ZIX_STATUS_NOT_FOUND) { + return test_fail("Unexpectedly found `%s'\n", not_indexed[i]); + } + } + + zix_ampatree_free(patree); + + return 0; +} diff --git a/test/bitset_test.c b/test/bitset_test.c new file mode 100644 index 0000000..0bb3cd0 --- /dev/null +++ b/test/bitset_test.c @@ -0,0 +1,82 @@ +/* + Copyright 2014-2016 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include <stdarg.h> +#include <stdio.h> + +#include "zix/bitset.h" + +#define N_BITS 256 +#define N_ELEMS (ZIX_BITSET_ELEMS(N_BITS)) + +static int +test_fail(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + va_end(args); + return 1; +} + +int +main(int argc, char** argv) +{ + ZixBitset b[N_ELEMS]; + ZixBitsetTally t[N_ELEMS]; + + zix_bitset_clear(b, t, N_BITS); + if (zix_bitset_count_up_to(b, t, N_BITS) != 0) { + return test_fail("Cleared bitset has non-zero count\n"); + } + + for (size_t i = 0; i < N_BITS; ++i) { + zix_bitset_set(b, t, i); + const size_t count = zix_bitset_count_up_to(b, t, N_BITS); + if (count != i + 1) { + return test_fail("Count %zu != %zu\n", count, i + 1); + } else if (!zix_bitset_get(b, i)) { + return test_fail("Bit %zu is not set\n", i); + } + } + + for (size_t i = 0; i <= N_BITS; ++i) { + const size_t count = zix_bitset_count_up_to(b, t, i); + if (count != i) { + return test_fail("Count up to %zu is %zu != %zu\n", i, count, i); + } + } + + for (size_t i = 0; i <= N_BITS; ++i) { + zix_bitset_reset(b, t, i); + const size_t count = zix_bitset_count_up_to(b, t, i); + if (count != 0) { + return test_fail("Count up to %zu is %zu != %zu\n", i, count, 0); + } + } + + zix_bitset_clear(b, t, N_BITS); + for (size_t i = 0; i <= N_BITS; i += 2) { + zix_bitset_set(b, t, i); + const size_t count = zix_bitset_count_up_to(b, t, i + 1); + if (count != i / 2 + 1) { + return test_fail("Count up to %zu is %zu != %zu\n", i, count, i / 2 + 1); + } + } + + return 0; +} diff --git a/test/trie_test.c b/test/trie_test.c new file mode 100644 index 0000000..42bf5dc --- /dev/null +++ b/test/trie_test.c @@ -0,0 +1,115 @@ +/* + Copyright 2011-2016 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include <stdarg.h> +#include <stdio.h> +#include <string.h> + +#include "zix/trie.h" + +static const char* strings[] = { + "http://example.org/foo", + "http://example.org/bar", + "http://example.org/baz", + "http://example.net/foo", + "http://example.net/bar", + "http://example.net/baz", + "http://drobilla.net/", + "http://drobilla.net/software/zix", + "http://www.gbengasesan.com/blog", + "http://www.gbengasesan.com", + "http://echo.jpl.nasa.gov/~lance/delta_v/delta_v.rendezvous.html", + "http://echo.jpl.nasa.gov/asteroids/1986da/1986DA.html", + "http://echo.jpl.nasa.gov/", + "http://echo.jpl.nasa.gov/asteroids/1620_Geographos/geographos.html", + "http://echo.jpl.nasa.gov/~ostro/KY26/", + "http://echo.jpl.nasa.gov/~ostro/KY26/JPL_press_release.text", + "http://echo.jpl.nasa.gov", + "http://echo.jpl.nasa.gov/asteroids/4179_Toutatis/toutatis.html", + "http://echo.jpl.nasa.gov/asteroids/4769_Castalia/cast01.html", + "http://echo.jpl.nasa.gov/publications/review_abs.html", +}; + +static int +test_fail(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stderr, "error: "); + vfprintf(stderr, fmt, args); + va_end(args); + return 1; +} + +int +main(int argc, char** argv) +{ + ZixTrie* trie = zix_trie_new(); + + const size_t n_strings = sizeof(strings) / sizeof(char*); + + // Insert each string + for (size_t i = 0; i < n_strings; ++i) { + ZixStatus st = zix_trie_insert(trie, strings[i], strlen(strings[i])); + if (st) { + return test_fail("Failed to insert `%s'\n", strings[i]); + } + } + + FILE* dot_file = fopen("trie.dot", "w"); + zix_trie_print_dot(trie, dot_file); + fclose(dot_file); + + // Attempt to insert each string again + for (size_t i = 0; i < n_strings; ++i) { + ZixStatus st = zix_trie_insert(trie, strings[i], strlen(strings[i])); + if (st != ZIX_STATUS_EXISTS) { + return test_fail("Double inserted `%s'\n", strings[i]); + } + } + + // Search for each string + for (size_t i = 0; i < n_strings; ++i) { + const char* match = NULL; + ZixStatus st = zix_trie_find(trie, strings[i], &match); + if (st) { + return test_fail("Failed to find `%s'\n", strings[i]); + } + if (match != strings[i]) { + return test_fail("Bad match for `%s'\n", strings[i]); + } + } + + // Try some false matches + const char* not_indexed[] = { + "ftp://example.org/not-there-at-all", + "http://example.org/foobar", + "http://", + "http://otherdomain.com" + }; + const size_t n_not_indexed = sizeof(not_indexed) / sizeof(char*); + for (size_t i = 0; i < n_not_indexed; ++i) { + const char* match = NULL; + ZixStatus st = zix_trie_find(trie, not_indexed[i], &match); + if (st != ZIX_STATUS_NOT_FOUND) { + return test_fail("Unexpectedly found `%s'\n", not_indexed[i]); + } + } + + zix_trie_free(trie); + + return 0; +} |