diff options
Diffstat (limited to 'test/test_cursor.c')
-rw-r--r-- | test/test_cursor.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/test/test_cursor.c b/test/test_cursor.c new file mode 100644 index 00000000..cade0621 --- /dev/null +++ b/test/test_cursor.c @@ -0,0 +1,114 @@ +/* + Copyright 2011-2021 David Robillard <d@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. +*/ + +#undef NDEBUG + +#include "serd/serd.h" + +#include <assert.h> +#include <stddef.h> + +static void +test_copy(void) +{ + assert(!serd_cursor_copy(NULL)); + + SerdWorld* const world = serd_world_new(); + SerdModel* const model = serd_model_new(world, SERD_ORDER_SPO, 0u); + SerdCursor* const begin = serd_model_begin(model); + SerdCursor* const copy = serd_cursor_copy(begin); + + assert(serd_cursor_equals(copy, begin)); + + serd_cursor_free(copy); + serd_cursor_free(begin); + serd_model_free(model); + serd_world_free(world); +} + +static void +test_comparison(void) +{ + SerdWorld* const world = serd_world_new(); + SerdNodes* const nodes = serd_world_nodes(world); + SerdModel* const model = serd_model_new(world, SERD_ORDER_SPO, 0u); + + serd_model_add_index(model, SERD_ORDER_OPS); + + const SerdNode* const a = + serd_nodes_uri(nodes, SERD_STRING("http://example.org/a")); + const SerdNode* const b = + serd_nodes_uri(nodes, SERD_STRING("http://example.org/b")); + const SerdNode* const c = + serd_nodes_uri(nodes, SERD_STRING("http://example.org/c")); + + // Add a single statement + assert(!serd_model_add(model, a, b, c, NULL)); + + // Make cursors that point to the statement but via different patterns + SerdCursor* const c1 = serd_model_find(model, a, NULL, NULL, NULL); + SerdCursor* const c2 = serd_model_find(model, a, b, NULL, NULL); + SerdCursor* const c3 = serd_model_find(model, NULL, b, c, NULL); + + // Ensure that they refer to the same statement but are not equal + assert(serd_cursor_get(c1) == serd_cursor_get(c2)); + assert(serd_cursor_get(c2) == serd_cursor_get(c3)); + assert(!serd_cursor_equals(c1, c2)); + assert(!serd_cursor_equals(c2, c3)); + assert(!serd_cursor_equals(c1, c3)); + + // Check that none are equal to begin (which has a different mode) or end + SerdCursor* const begin = serd_model_begin(model); + assert(!serd_cursor_equals(c1, begin)); + assert(!serd_cursor_equals(c2, begin)); + assert(!serd_cursor_equals(c3, begin)); + assert(!serd_cursor_equals(c1, serd_model_end(model))); + assert(!serd_cursor_equals(c2, serd_model_end(model))); + assert(!serd_cursor_equals(c3, serd_model_end(model))); + serd_cursor_free(begin); + + // Check that a cursor that points to it via the same pattern is equal + SerdCursor* const c4 = serd_model_find(model, a, b, NULL, NULL); + assert(serd_cursor_get(c4) == serd_cursor_get(c1)); + assert(serd_cursor_equals(c4, c2)); + assert(!serd_cursor_equals(c4, c3)); + serd_cursor_free(c4); + + // Advance everything to the end + assert(serd_cursor_advance(c1) == SERD_FAILURE); + assert(serd_cursor_advance(c2) == SERD_FAILURE); + assert(serd_cursor_advance(c3) == SERD_FAILURE); + + // Check that they are now equal, and equal to the model's end + assert(serd_cursor_equals(c1, c2)); + assert(serd_cursor_equals(c1, serd_model_end(model))); + assert(serd_cursor_equals(c2, serd_model_end(model))); + + serd_cursor_free(c3); + serd_cursor_free(c2); + serd_cursor_free(c1); + serd_model_free(model); + serd_world_free(world); +} + +int +main(void) +{ + test_copy(); + test_comparison(); + + return 0; +} |