int main(int argc, char *argv[]) { librdf_world *world; unsigned char* id; const char *program=librdf_basename((const char*)argv[0]); /* Minimal setup-cleanup test without opening the world */ fprintf(stdout, "%s: Creating new world\n", program); world = librdf_new_world(); if(!world) { fprintf(stderr, "%s: librdf_new_world failed\n", program); return 1; } fprintf(stdout, "%s: Deleting world\n", program); librdf_free_world(world); /* Test id generation */ fprintf(stdout, "%s: Creating new world\n", program); world = librdf_new_world(); if(!world) { fprintf(stderr, "%s: librdf_new_world failed\n", program); return 1; } fprintf(stdout, "%s: Generating an identifier\n", program); id = librdf_world_get_genid(world); if(id == NULL || strlen(id) < 6) { fprintf(stderr, "%s: librdf_world_get_genid failed\n", program); return 1; } fprintf(stdout, "%s: New identifier is: '%s'\n", program, id); LIBRDF_FREE(cstring, id); fprintf(stdout, "%s: Deleting world\n", program); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { const char *program=librdf_basename((const char*)argv[0]); librdf_world *world; librdf_uri* uri; librdf_node* node; unsigned char* actual; world=librdf_new_world(); librdf_world_open(world); uri=LIBRDF_MS_Seq_URI(world); if(!uri) { fprintf(stderr, "%s: Got no concept URI for rdf:Seq\n", program); exit(1); } actual=librdf_uri_as_string(uri); if(strcmp(EXPECTED_STRING, (const char*)actual)) { fprintf(stderr, "%s: Expected URI: <%s> Got: <%s>\n", program, EXPECTED_STRING, actual); exit(1); } node=librdf_get_concept_resource_by_index(world, LIBRDF_CONCEPT_LAST); if(!node) { fprintf(stderr, "%s: Got no concept node for the last concept\n", program); exit(1); } librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { librdf_statement *statement, *statement2; int size, size2; const char *program=librdf_basename((const char*)argv[0]); char *s, *buffer; librdf_world *world; raptor_iostream *iostr; world=librdf_new_world(); librdf_world_open(world); iostr = raptor_new_iostream_to_file_handle(world->raptor_world_ptr, stdout); fprintf(stdout, "%s: Creating statement\n", program); statement=librdf_new_statement(world); fprintf(stdout, "%s: Empty statement: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); librdf_statement_set_subject(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/net/dajobe/")); librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/#Creator")); librdf_statement_set_object(statement, librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", NULL, 0)); fprintf(stdout, "%s: Resulting statement: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); size = librdf_statement_encode2(world, statement, NULL, 0); fprintf(stdout, "%s: Encoding statement requires %d bytes\n", program, size); buffer=(char*)LIBRDF_MALLOC(cstring, size); fprintf(stdout, "%s: Encoding statement in buffer\n", program); size2 = librdf_statement_encode2(world, statement, (unsigned char*)buffer, size); if(size2 != size) { fprintf(stdout, "%s: Encoding statement used %d bytes, expected it to use %d\n", program, size2, size); return(1); } fprintf(stdout, "%s: Creating new statement\n", program); statement2=librdf_new_statement(world); fprintf(stdout, "%s: Decoding statement from buffer\n", program); if(!librdf_statement_decode2(world, statement2, NULL, (unsigned char*)buffer, size)) { fprintf(stdout, "%s: Decoding statement failed\n", program); return(1); } LIBRDF_FREE(cstring, buffer); fprintf(stdout, "%s: New statement is: ", program); librdf_statement_write(statement, iostr); fputs("\n", stdout); fprintf(stdout, "%s: Freeing statements\n", program); librdf_free_statement(statement2); librdf_free_statement(statement); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { const unsigned char *hp_string=(const unsigned char*)"http://purl.org/net/dajobe/"; librdf_uri *uri1, *uri2, *uri3, *uri4, *uri5, *uri6, *uri7, *uri8, *uri9; librdf_digest *d; const char *program=librdf_basename((const char*)argv[0]); const char *file_string="/big/long/directory/file"; const unsigned char *file_uri_string=(const unsigned char*)"file:///big/long/directory/file"; const unsigned char *uri_string=(const unsigned char*)"http://example.com/big/long/directory/blah#frag"; const unsigned char *relative_uri_string1=(const unsigned char*)"#foo"; const unsigned char *relative_uri_string2=(const unsigned char*)"bar"; librdf_world *world; world=librdf_new_world(); librdf_world_open(world); fprintf(stderr, "%s: Creating new URI from string\n", program); uri1=librdf_new_uri(world, hp_string); if(!uri1) { fprintf(stderr, "%s: Failed to create URI from string '%s'\n", program, hp_string); return(1); } fprintf(stderr, "%s: Home page URI is ", program); librdf_uri_print(uri1, stderr); fputs("\n", stderr); fprintf(stderr, "%s: Creating URI from URI\n", program); uri2=librdf_new_uri_from_uri(uri1); if(!uri2) { fprintf(stderr, "%s: Failed to create new URI from old one\n", program); return(1); } fprintf(stderr, "%s: New URI is ", program); librdf_uri_print(uri2, stderr); fputs("\n", stderr); fprintf(stderr, "%s: Getting digest for URI\n", program); d = librdf_uri_get_digest(world, uri2); if(!d) { fprintf(stderr, "%s: Failed to get digest for URI %s\n", program, librdf_uri_as_string(uri2)); return(1); } fprintf(stderr, "%s: Digest is: ", program); librdf_digest_print(d, stderr); fputs("\n", stderr); librdf_free_digest(d); uri3=librdf_new_uri(world, (const unsigned char*)"file:/big/long/directory/"); uri4=librdf_new_uri(world, (const unsigned char*)"http://somewhere/dir/"); fprintf(stderr, "%s: Source URI is ", program); librdf_uri_print(uri3, stderr); fputs("\n", stderr); fprintf(stderr, "%s: Base URI is ", program); librdf_uri_print(uri4, stderr); fputs("\n", stderr); fprintf(stderr, "%s: URI string is '%s'\n", program, uri_string); uri5=librdf_new_uri_normalised_to_base(uri_string, uri3, uri4); fprintf(stderr, "%s: Normalised URI is ", program); librdf_uri_print(uri5, stderr); fputs("\n", stderr); uri6=librdf_new_uri_relative_to_base(uri5, relative_uri_string1); fprintf(stderr, "%s: URI + Relative URI %s gives ", program, relative_uri_string1); librdf_uri_print(uri6, stderr); fputs("\n", stderr); uri7=librdf_new_uri_relative_to_base(uri5, relative_uri_string2); fprintf(stderr, "%s: URI + Relative URI %s gives ", program, relative_uri_string2); librdf_uri_print(uri7, stderr); fputs("\n", stderr); uri8=librdf_new_uri_from_filename(world, file_string); uri9=librdf_new_uri(world, file_uri_string); if(!librdf_uri_equals(uri8, uri9)) { fprintf(stderr, "%s: URI string from filename %s returned %s, expected %s\n", program, file_string, librdf_uri_as_string(uri8), file_uri_string); return(1); } fprintf(stderr, "%s: Freeing URIs\n", program); librdf_free_uri(uri1); librdf_free_uri(uri2); librdf_free_uri(uri3); librdf_free_uri(uri4); librdf_free_uri(uri5); librdf_free_uri(uri6); librdf_free_uri(uri7); librdf_free_uri(uri8); librdf_free_uri(uri9); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { librdf_statement *statement; librdf_stream* stream; const char *program=librdf_basename((const char*)argv[0]); librdf_world *world; librdf_uri* prefix_uri; librdf_node* nodes[STREAM_NODES_COUNT]; int i; librdf_iterator* iterator; int count; world=librdf_new_world(); librdf_world_open(world); prefix_uri=librdf_new_uri(world, (const unsigned char*)NODE_URI_PREFIX); if(!prefix_uri) { fprintf(stderr, "%s: Failed to create prefix URI\n", program); return(1); } for(i=0; i < STREAM_NODES_COUNT; i++) { unsigned char buf[2]; buf[0]='a'+i; buf[1]='\0'; nodes[i]=librdf_new_node_from_uri_local_name(world, prefix_uri, buf); if(!nodes[i]) { fprintf(stderr, "%s: Failed to create node %i (%s)\n", program, i, buf); return(1); } } fprintf(stdout, "%s: Creating static node iterator\n", program); iterator = librdf_node_new_static_node_iterator(world, nodes, STREAM_NODES_COUNT); if(!iterator) { fprintf(stderr, "%s: Failed to create static node iterator\n", program); return(1); } statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/resource"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/property"), NULL); if(!statement) { fprintf(stderr, "%s: Failed to create statement\n", program); return(1); } fprintf(stdout, "%s: Creating stream from node iterator\n", program); stream=librdf_new_stream_from_node_iterator(iterator, statement, LIBRDF_STATEMENT_OBJECT); if(!stream) { fprintf(stderr, "%s: Failed to createstatic node stream\n", program); return(1); } /* This is to check that the stream_from_node_iterator code * *really* takes a copy of what it needs from statement */ fprintf(stdout, "%s: Freeing statement\n", program); librdf_free_statement(statement); fprintf(stdout, "%s: Listing static node stream\n", program); count=0; while(!librdf_stream_end(stream)) { librdf_statement* s_statement=librdf_stream_get_object(stream); if(!s_statement) { fprintf(stderr, "%s: librdf_stream_current returned NULL when not end of stream\n", program); return(1); } fprintf(stdout, "%s: statement %d is: ", program, count); librdf_statement_print(s_statement, stdout); fputc('\n', stdout); librdf_stream_next(stream); count++; } if(count != STREAM_NODES_COUNT) { fprintf(stderr, "%s: Stream returned %d statements, expected %d\n", program, count, STREAM_NODES_COUNT); return(1); } fprintf(stdout, "%s: stream from node iterator worked ok\n", program); fprintf(stdout, "%s: Freeing stream\n", program); librdf_free_stream(stream); fprintf(stdout, "%s: Freeing nodes\n", program); for (i=0; i<STREAM_NODES_COUNT; i++) { librdf_free_node(nodes[i]); } librdf_free_uri(prefix_uri); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { librdf_world *world; librdf_uri* prefix_uri; librdf_node* nodes[ITERATOR_NODES_COUNT]; int i; librdf_iterator* iterator; int count; const char *program=librdf_basename((const char*)argv[0]); world=librdf_new_world(); librdf_world_open(world); prefix_uri=librdf_new_uri(world, (const unsigned char*)NODE_URI_PREFIX); if(!prefix_uri) { fprintf(stderr, "%s: Failed to create prefix URI\n", program); return(1); } for(i=0; i < ITERATOR_NODES_COUNT; i++) { unsigned char buf[2]; buf[0]='a'+i; buf[1]='\0'; nodes[i]=librdf_new_node_from_uri_local_name(world, prefix_uri, buf); if(!nodes[i]) { fprintf(stderr, "%s: Failed to create node %i (%s)\n", program, i, buf); return(1); } } fprintf(stdout, "%s: Creating static node iterator\n", program); iterator = librdf_node_new_static_node_iterator(world, nodes, ITERATOR_NODES_COUNT); if(!iterator) { fprintf(stderr, "%s: Failed to createstatic node iterator\n", program); return(1); } fprintf(stdout, "%s: Listing static node iterator\n", program); count=0; while(!librdf_iterator_end(iterator)) { librdf_node* i_node=(librdf_node*)librdf_iterator_get_object(iterator); if(!i_node) { fprintf(stderr, "%s: librdf_iterator_current return NULL when not end o fiterator\n", program); return(1); } fprintf(stdout, "%s: node %d is: ", program, count); librdf_node_print(i_node, stdout); fputc('\n', stdout); if(!librdf_node_equals(i_node, nodes[count])) { fprintf(stderr, "%s: static node iterator node %i returned unexpected node\n", program, count); librdf_node_print(i_node, stderr); fputs(" rather than ", stdout); librdf_node_print(nodes[count], stderr); fputc('\n', stdout); return(1); } librdf_iterator_next(iterator); count++; } librdf_free_iterator(iterator); if(count != ITERATOR_NODES_COUNT) { fprintf(stderr, "%s: Iterator returned %d nodes, expected %d\n", program, count, ITERATOR_NODES_COUNT); return(1); } fprintf(stdout, "%s: Static node iterator worked ok\n", program); fprintf(stdout, "%s: Freeing nodes\n", program); for (i=0; i<ITERATOR_NODES_COUNT; i++) { librdf_free_node(nodes[i]); } librdf_free_uri(prefix_uri); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { const char *program = librdf_basename(argv[0]); #define ITEM_COUNT 8 const char *items[ITEM_COUNT+1] = { "ron", "amy", "jen", "bij", "jib", "daj", "jim", "def", NULL }; #define DELETE_COUNT 2 const char *delete_items[DELETE_COUNT+1] = { "daj", "jim", NULL }; #define RESULT_COUNT (ITEM_COUNT-DELETE_COUNT) const char *results[RESULT_COUNT+1] = { "amy", "bij", /*"daj",*/ "def", "jen", "jib", /*"jim",*/"ron", NULL}; librdf_world* world; librdf_avltree* tree; librdf_iterator* iter; visit_state vs; int i; world = librdf_new_world(); tree = librdf_new_avltree(compare_strings, NULL); /* no free as they are static pointers above */ if(!tree) { fprintf(stderr, "%s: Failed to create tree\n", program); exit(1); } for(i = 0; items[i]; i++) { int rc; void* node; #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Adding tree item '%s'\n", program, items[i]); #endif rc = librdf_avltree_add(tree, (void*)items[i]); if(rc) { fprintf(stderr, "%s: Adding tree item %d '%s' failed, returning error %d\n", program, i, items[i], rc); exit(1); } #ifdef LIBRDF_DEBUG librdf_avltree_check(tree); #endif node = librdf_avltree_search(tree, (void*)items[i]); if(!node) { fprintf(stderr, "%s: Tree did NOT contain item %d '%s' as expected\n", program, i, items[i]); exit(1); } } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Printing tree\n", program); vs.fh = stderr; vs.count = 0; librdf_avltree_visit(tree, print_string, &vs); //fprintf(stderr, "%s: Dumping tree\n", program); //librdf_avltree_dump(tree, stderr, print_string); #endif for(i = 0; delete_items[i]; i++) { int rc; #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Deleting tree item '%s'\n", program, delete_items[i]); #endif rc = librdf_avltree_delete(tree, (void*)delete_items[i]); if(!rc) { fprintf(stderr, "%s: Deleting tree item %d '%s' failed, returning error %d\n", program, i, delete_items[i], rc); exit(1); } #ifdef LIBRDF_DEBUG librdf_avltree_check(tree); #endif } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Walking tree forwards via iterator\n", program); #endif iter = librdf_avltree_get_iterator_start(world, tree, NULL, NULL); for(i = 0; 1; i++) { const char* data = (const char*)librdf_iterator_get_object(iter); const char* result = results[i]; if((!data && data != result) || (data && strcmp(data, result))) { fprintf(stderr, "%3d: Forwards iterator expected '%s' but found '%s'\n", i, result, data); exit(1); } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%3d: Got '%s'\n", i, data); #endif if(librdf_iterator_next(iter)) break; if(i > RESULT_COUNT) { fprintf(stderr, "Forward iterator did not end on result %i as expected\n", i); exit(1); } } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Checking tree\n", program); #endif vs.count = 0; vs.results=results; vs.failed = 0; librdf_avltree_visit(tree, check_string, &vs); if(vs.failed) { fprintf(stderr, "%s: Checking tree failed\n", program); exit(1); } for(i = 0; results[i]; i++) { const char* result = results[i]; char* data = (char*)librdf_avltree_remove(tree, (void*)result); if(!data) { fprintf(stderr, "%s: remove %i failed at item '%s'\n", program, i, result); exit(1); } if(strcmp(data, result)) { fprintf(stderr, "%s: remove %i returned %s not %s as expected\n", program, i, data, result); exit(1); } } #if LIBRDF_DEBUG > 1 fprintf(stderr, "%s: Freeing tree\n", program); #endif librdf_free_avltree(tree); librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }
int main(int argc, char *argv[]) { const char *program=librdf_basename((const char*)argv[0]); const char *test_serializer_types[]={"rdfxml", "ntriples", NULL}; int i; const char *type; unsigned char *string; size_t string_length; librdf_world *world; librdf_storage *storage; librdf_model* model; librdf_uri* base_uri; librdf_statement* statement; librdf_serializer* serializer; librdf_parser* parser; librdf_stream* stream; FILE *fh; struct stat st_buf; world=librdf_new_world(); librdf_world_open(world); librdf_world_set_logger(world, &LogData, log_handler); for(i=0; (type=test_serializer_types[i]); i++) { fprintf(stderr, "%s: Trying to create new %s serializer\n", program, type); serializer=librdf_new_serializer(world, type, NULL, NULL); if(!serializer) { fprintf(stderr, "%s: Failed to create new serializer type %s\n", program, type); continue; } fprintf(stderr, "%s: Freeing serializer\n", program); librdf_free_serializer(serializer); } storage=librdf_new_storage(world, NULL, NULL, NULL); model=librdf_new_model(world, storage, NULL); /* ERROR: Subject URI is bad UTF-8 */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo\xfc"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/bar"), librdf_new_node_from_literal(world, (const unsigned char*)"blah", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); /* ERROR: Predicate URI is not serializable */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://bad.example.org/"), librdf_new_node_from_literal(world, (const unsigned char*)"blah", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); /* ERROR: Object literal is bad UTF-8 */ statement=librdf_new_statement_from_nodes(world, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/foo"), librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/abc"), librdf_new_node_from_literal(world, (const unsigned char*)"\xfc", NULL, 0)); librdf_model_add_statement(model, statement); librdf_free_statement(statement); serializer=librdf_new_serializer(world, "rdfxml", NULL, NULL); base_uri=librdf_new_uri(world, (const unsigned char*)"http://example.org/base#"); string=librdf_serializer_serialize_model_to_counted_string(serializer, base_uri, model, &string_length); #define EXPECTED_BAD_STRING_LENGTH 382 if(string_length != EXPECTED_BAD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising model to RDF/XML returned string '%s' size %d, expected %d\n", program, string, (int)string_length, EXPECTED_BAD_STRING_LENGTH); return 1; } if(string) free(string); librdf_free_uri(base_uri); base_uri=NULL; librdf_free_model(model); model=NULL; librdf_free_storage(storage); storage=NULL; if(LogData.errors != EXPECTED_ERRORS) { fprintf(stderr, "%s: Serialising to RDF/XML returned %d errors, expected %d\n", program, LogData.errors, EXPECTED_ERRORS); return 1; } if(LogData.warnings != EXPECTED_WARNINGS) { fprintf(stderr, "%s: Serialising to RDF/XML returned %d warnings, expected %d\n", program, LogData.warnings, EXPECTED_WARNINGS); return 1; } /* Good model to serialize */ storage=librdf_new_storage(world, NULL, NULL, NULL); model=librdf_new_model(world, storage, NULL); parser=librdf_new_parser(world, SYNTAX_TYPE, NULL, NULL); if(!parser) { fprintf(stderr, "%s: Failed to create new parser type %s\n", program, SYNTAX_TYPE); return 1; } fprintf(stderr, "%s: Adding %s string content\n", program, SYNTAX_TYPE); if(librdf_parser_parse_string_into_model(parser, (const unsigned char*)SYNTAX_CONTENT, NULL /* no base URI*/, model)) { fprintf(stderr, "%s: Failed to parse RDF from %s string into model\n", SYNTAX_TYPE, program); return 1; } librdf_free_parser(parser); fprintf(stderr, "%s: Serializing stream to a string\n", program); stream=librdf_model_as_stream(model); string_length=0; string=librdf_serializer_serialize_stream_to_counted_string(serializer, NULL, stream, &string_length); #define EXPECTED_GOOD_STRING_LENGTH 668 if(string_length != EXPECTED_GOOD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising stream to RDF/XML returned string '%s' size %d, expected %d\n", program, string, (int)string_length, EXPECTED_GOOD_STRING_LENGTH); return 1; } librdf_free_stream(stream); if(string) free(string); fprintf(stderr, "%s: Serializing stream to a file handle\n", program); stream=librdf_model_as_stream(model); #define FILENAME "test.rdf" fh=fopen(FILENAME, "w"); if(!fh) { fprintf(stderr, "%s: Failed to fopen for writing '%s' - %s\n", program, FILENAME, strerror(errno)); return 1; } librdf_serializer_serialize_stream_to_file_handle(serializer, fh, NULL, stream); fclose(fh); stat(FILENAME, &st_buf); if((int)st_buf.st_size != EXPECTED_GOOD_STRING_LENGTH) { fprintf(stderr, "%s: Serialising stream to file handle returned file '%s' of size %d bytes, expected %d\n", program, FILENAME, (int)st_buf.st_size, EXPECTED_GOOD_STRING_LENGTH); return 1; } unlink(FILENAME); librdf_free_stream(stream); librdf_free_serializer(serializer); serializer=NULL; librdf_free_model(model); model=NULL; librdf_free_storage(storage); storage=NULL; librdf_free_world(world); /* keep gcc -Wall happy */ return(0); }