Browse files

Add triple patterns matching test

  • Loading branch information...
1 parent c63b88e commit b65bceaba9ace4016f5dd8496fc81e1fa3c632e1 @dajobe committed Sep 23, 2009
Showing with 231 additions and 2 deletions.
  1. +3 −1 data/Makefile.am
  2. +29 −0 data/triples.ttl
  3. +5 −1 tests/engine/Makefile.am
  4. +194 −0 tests/engine/rasqal_triples_test.c
View
4 data/Makefile.am
@@ -1,3 +1,5 @@
EXTRA_DIST=dc.rdf \
animals.nt letters.nt one.nt \
-graph-a.ttl graph-b.ttl graph-c.ttl
+graph-a.ttl graph-b.ttl graph-c.ttl \
+triples.ttl
+
View
29 data/triples.ttl
@@ -0,0 +1,29 @@
+# DAWG test data basic/data-2.ttl with collections flattened to
+# triples and more readable named bnodes
+#
+@prefix : <http://example.org/ns#> .
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+
+# :x :list0 () .
+:x :list0 rdf:nil .
+
+# :x :list1 (1) .
+:x :list1 _:a1 .
+_:a1 rdf:first 1 .
+_:a1 rdf:rest rdf:nil .
+
+# :x :list2 (11 22) .
+:x :list2 _:b1 .
+_:b1 rdf:first 11 .
+_:b1 rdf:rest _:b2 .
+_:b2 rdf:first 22 .
+_:b2 rdf:rest rdf:nil .
+
+# :x :list3 (111 222 333) .
+:x :list3 _:c1 .
+_:c1 rdf:first 111 .
+_:c1 rdf:rest _:c2 .
+_:c2 rdf:first 222 .
+_:c2 rdf:rest _:c3 .
+_:c3 rdf:first 333 .
+_:c3 rdf:rest rdf:nil .
View
6 tests/engine/Makefile.am
@@ -20,7 +20,8 @@
#
local_tests=rasqal_order_test$(EXEEXT) rasqal_graph_test$(EXEEXT) \
-rasqal_construct_test$(EXEEXT) rasqal_limit_test$(EXEEXT)
+rasqal_construct_test$(EXEEXT) rasqal_limit_test$(EXEEXT) \
+rasqal_triples_test$(EXEEXT)
EXTRA_PROGRAMS=$(local_tests)
@@ -42,6 +43,9 @@ rasqal_construct_test_LDADD = $(top_builddir)/src/librasqal.la
rasqal_limit_test_SOURCES = rasqal_limit_test.c
rasqal_limit_test_LDADD = $(top_builddir)/src/librasqal.la
+rasqal_triples_test_SOURCES = rasqal_triples_test.c
+rasqal_triples_test_LDADD = $(top_builddir)/src/librasqal.la
+
# These are compiled here and used elsewhere for running tests
check-local: $(local_tests) run-rasqal-tests
View
194 tests/engine/rasqal_triples_test.c
@@ -0,0 +1,194 @@
+/* -*- Mode: c; c-basic-offset: 2 -*-
+ *
+ * rasqal_triples_test.c - Rasqal RDF Query Triple Patterns Tests
+ *
+ * Copyright (C) 2009, David Beckett http://www.dajobe.org/
+ *
+ * This package is Free Software and part of Redland http://librdf.org/
+ *
+ * It is licensed under the following three licenses as alternatives:
+ * 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
+ * 2. GNU General Public License (GPL) V2 or any newer version
+ * 3. Apache License, V2.0 or any newer version
+ *
+ * You may not use this file except in compliance with at least one of
+ * the above three licenses.
+ *
+ * See LICENSE.html or LICENSE.txt at the top of this package for the
+ * complete terms and further detail along with the license texts for
+ * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
+ *
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <rasqal_config.h>
+#endif
+
+#ifdef WIN32
+#include <win32_rasqal_config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#include <stdarg.h>
+
+#include "rasqal.h"
+#include "rasqal_internal.h"
+
+
+#define DATA_FILE_NAME (const unsigned char*)"triples.ttl"
+
+#ifdef RASQAL_QUERY_SPARQL
+#define QUERY_LANGUAGE "sparql"
+#define QUERY_FORMAT " \
+PREFIX : <http://example.org/ns#> \
+PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \
+SELECT ?p ?v ?w \
+{ \
+ :x ?p _:node1 . \
+ _:node2 rdf:first ?w . \
+ _:node2 rdf:rest rdf:nil . \
+ _:node1 rdf:first ?v . \
+ _:node1 rdf:rest _:node2 \
+} \
+"
+#else
+#ifdef RASQAL_QUERY_RDQL
+#define QUERY_LANGUAGE "rdql"
+#define QUERY_FORMAT "SELECT ?p, ?v, ?w \
+ WHERE \
+ (?p, ?v, ?w) USING \
+ rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#>, \
+ ex FOR <http://example.org/ns#>"
+#else
+#define NO_QUERY_LANGUAGE
+#endif
+#endif
+
+#define EXPECTED_RESULTS_COUNT 1
+
+
+#ifdef NO_QUERY_LANGUAGE
+int
+main(int argc, char **argv) {
+ const char *program = rasqal_basename(argv[0]);
+ fprintf(stderr, "%s: No supported query language available, skipping test\n", program);
+ return(0);
+}
+#else
+
+int
+main(int argc, char **argv) {
+ const char *program = rasqal_basename(argv[0]);
+ rasqal_query *query = NULL;
+ rasqal_query_results *results = NULL;
+ raptor_uri *base_uri;
+ unsigned char *data_dir_string;
+ raptor_uri* data_dir_uri;
+ unsigned char *uri_string;
+ const char *query_language_name = QUERY_LANGUAGE;
+ const unsigned char *query_string = (const unsigned char*)QUERY_FORMAT;
+ int count;
+ rasqal_world *world;
+ raptor_uri *data_file_uri;
+
+ world=rasqal_new_world();
+ if(!world || rasqal_world_open(world)) {
+ fprintf(stderr, "%s: rasqal_world init failed\n", program);
+ return(1);
+ }
+
+ uri_string = raptor_uri_filename_to_uri_string("");
+#ifdef RAPTOR_V2_AVAILABLE
+ base_uri = raptor_new_uri_v2(world->raptor_world_ptr, uri_string);
+#else
+ base_uri = raptor_new_uri(uri_string);
+#endif
+ raptor_free_memory(uri_string);
+
+ data_dir_string =raptor_uri_filename_to_uri_string(argv[1]);
+#ifdef RAPTOR_V2_AVAILABLE
+ data_dir_uri = raptor_new_uri_v2(world->raptor_world_ptr, data_dir_string);
+#else
+ data_dir_uri = raptor_new_uri(data_dir_string);
+#endif
+
+ query = rasqal_new_query(world, query_language_name, NULL);
+ if(!query) {
+ fprintf(stderr, "%s: creating query in language %s FAILED\n", program,
+ query_language_name);
+ return(1);
+ }
+
+ printf("%s: preparing %s query\n", program, query_language_name);
+ if(rasqal_query_prepare(query, query_string, base_uri)) {
+ fprintf(stderr, "%s: %s query prepare FAILED\n", program,
+ query_language_name);
+ return(1);
+ }
+
+#ifdef RAPTOR_V2_AVAILABLE
+ data_file_uri = raptor_new_uri_relative_to_base_v2(world->raptor_world_ptr,
+ data_dir_uri,
+ DATA_FILE_NAME);
+#else
+ data_file_uri = raptor_new_uri_relative_to_base(data_dir_uri, DATA_FILE_NAME);
+#endif
+
+ rasqal_query_add_data_graph(query, data_file_uri, NULL,
+ RASQAL_DATA_GRAPH_BACKGROUND);
+
+#ifdef RAPTOR_V2_AVAILABLE
+ raptor_free_uri_v2(world->raptor_world_ptr, data_file_uri);
+#else
+ raptor_free_uri(data_file_uri);
+#endif
+ data_file_uri = NULL;
+
+ printf("%s: executing query\n", program);
+ results = rasqal_query_execute(query);
+ if(!results) {
+ fprintf(stderr, "%s: query execution FAILED\n", program);
+ return(1);
+ }
+
+ count = 0;
+ while(results && !rasqal_query_results_finished(results)) {
+ int i;
+ for(i = 0; i < rasqal_query_results_get_bindings_count(results); i++) {
+ const unsigned char *name = rasqal_query_results_get_binding_name(results, i);
+ rasqal_literal *value = rasqal_query_results_get_binding_value(results, i);
+
+ printf("result %d: variable %s=", count+1, (char*)name);
+ rasqal_literal_print(value, stdout);
+ putchar('\n');
+ }
+ rasqal_query_results_next(results);
+ count++;
+ }
+ if(results)
+ rasqal_free_query_results(results);
+ if(count != EXPECTED_RESULTS_COUNT) {
+ fprintf(stderr, "%s: query execution returned %d results, expected %d\n",
+ program, count, EXPECTED_RESULTS_COUNT);
+ return(1);
+ }
+
+ rasqal_free_query(query);
+
+#ifdef RAPTOR_V2_AVAILABLE
+ raptor_free_uri_v2(world->raptor_world_ptr, base_uri);
+#else
+ raptor_free_uri(base_uri);
+#endif
+
+ rasqal_free_world(world);
+
+ return 0;
+}
+
+#endif

0 comments on commit b65bcea

Please sign in to comment.