diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/nodes_test.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/tests/nodes_test.c b/tests/nodes_test.c new file mode 100644 index 00000000..08e08ff1 --- /dev/null +++ b/tests/nodes_test.c @@ -0,0 +1,105 @@ +/* + Copyright 2018 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 "serd/serd.h" + +#include "test_utils.h" + +static int +test_intern(void) +{ + SerdNodes* nodes = serd_nodes_new(); + SerdNode* node = serd_new_string("node"); + + const SerdNode* interned1 = serd_nodes_intern(nodes, node); + if (!serd_node_equals(node, interned1)) { + FAIL("First interned node does not equal original\n"); + } + + const SerdNode* interned2 = serd_nodes_intern(nodes, node); + if (!serd_node_equals(node, interned2)) { + FAIL("Second interned node does not equal original\n"); + } + + if (interned1 != interned2) { + FAIL("Equivalent interned nodes have different addresses\n"); + } + + serd_node_free(node); + serd_nodes_free(nodes); + return 0; +} + +static int +test_manage(void) +{ + SerdNodes* nodes = serd_nodes_new(); + SerdNode* node = serd_new_string("node"); + + if (serd_nodes_manage(nodes, NULL)) { + FAIL("Managing NULL returned a node\n"); + } + + const SerdNode* managed1 = serd_nodes_manage(nodes, node); + if (managed1 != node) { + FAIL("First managed node has a different address than original\n"); + } + + SerdNode* equal = serd_new_string("node"); + const SerdNode* managed2 = serd_nodes_manage(nodes, equal); + if (managed2 != node) { + FAIL("Double managed node has a different address than original\n"); + } + + serd_nodes_free(nodes); + return 0; +} + +static int +test_deref(void) +{ + SerdNodes* nodes = serd_nodes_new(); + const SerdNode* managed = serd_nodes_manage(nodes, serd_new_string("node")); + + serd_nodes_deref(nodes, managed); + + SerdNode* node = serd_new_string("node"); + const SerdNode* interned = serd_nodes_intern(nodes, node); + + if (interned == node) { + FAIL("Dereferenced node was not deleted\n"); + } + + serd_node_free(node); + serd_nodes_free(nodes); + return 0; +} + +int +main(int argc, char** argv) +{ + typedef int (*TestFunc)(void); + + const TestFunc tests[] = { test_intern, test_manage, test_deref, NULL }; + + for (const TestFunc* t = tests; *t; ++t) { + if ((*t)()) { + return 1; + } + } + + return 0; +} |