Chris@0: /* Chris@0: Copyright 2011-2012 David Robillard Chris@0: Chris@0: Permission to use, copy, modify, and/or distribute this software for any Chris@0: purpose with or without fee is hereby granted, provided that the above Chris@0: copyright notice and this permission notice appear in all copies. Chris@0: Chris@0: THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES Chris@0: WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF Chris@0: MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR Chris@0: ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES Chris@0: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN Chris@0: ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF Chris@0: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include "serd/serd.h" Chris@0: Chris@0: #define USTR(s) ((const uint8_t*)(s)) Chris@0: Chris@0: #ifdef _WIN32 Chris@0: # define INFINITY (DBL_MAX + DBL_MAX) Chris@0: # define NAN (INFINITY - INFINITY) Chris@0: #endif Chris@0: Chris@0: static int Chris@0: failure(const char* fmt, ...) Chris@0: { Chris@0: va_list args; Chris@0: va_start(args, fmt); Chris@0: fprintf(stderr, "error: "); Chris@0: vfprintf(stderr, fmt, args); Chris@0: va_end(args); Chris@0: return 1; Chris@0: } Chris@0: Chris@0: static bool Chris@0: test_strtod(double dbl, double max_delta) Chris@0: { Chris@0: char buf[1024]; Chris@0: snprintf(buf, sizeof(buf), "%lf", dbl); Chris@0: Chris@0: char* endptr = NULL; Chris@0: const double out = serd_strtod(buf, &endptr); Chris@0: Chris@0: const double diff = fabs(out - dbl); Chris@0: if (diff > max_delta) { Chris@0: return !failure("Parsed %lf != %lf (delta %lf)\n", dbl, out, diff); Chris@0: } Chris@0: return true; Chris@0: } Chris@0: Chris@0: static SerdStatus Chris@0: count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri) Chris@0: { Chris@0: ++*(int*)handle; Chris@0: return SERD_SUCCESS; Chris@0: } Chris@0: Chris@0: typedef struct { Chris@0: int n_statements; Chris@0: const SerdNode* graph; Chris@0: } ReaderTest; Chris@0: Chris@0: static SerdStatus Chris@0: test_sink(void* handle, Chris@0: SerdStatementFlags flags, Chris@0: const SerdNode* graph, Chris@0: const SerdNode* subject, Chris@0: const SerdNode* predicate, Chris@0: const SerdNode* object, Chris@0: const SerdNode* object_datatype, Chris@0: const SerdNode* object_lang) Chris@0: { Chris@0: ReaderTest* rt = (ReaderTest*)handle; Chris@0: ++rt->n_statements; Chris@0: rt->graph = graph; Chris@0: return SERD_SUCCESS; Chris@0: } Chris@0: Chris@0: int Chris@0: main(void) Chris@0: { Chris@0: #define MAX 1000000 Chris@0: #define NUM_TESTS 1000 Chris@0: for (int i = 0; i < NUM_TESTS; ++i) { Chris@0: double dbl = rand() % MAX; Chris@0: dbl += (rand() % MAX) / (double)MAX; Chris@0: Chris@0: if (!test_strtod(dbl, 1 / (double)MAX)) { Chris@0: return 1; Chris@0: } Chris@0: } Chris@0: Chris@0: const double expt_test_nums[] = { Chris@0: 2.0E18, -5e19, +8e20, 2e+34, -5e-5, 8e0, 9e-0, 2e+0 Chris@0: }; Chris@0: Chris@0: const char* expt_test_strs[] = { Chris@0: "02e18", "-5e019", "+8e20", "2E+34", "-5E-5", "8E0", "9e-0", " 2e+0" Chris@0: }; Chris@0: Chris@0: for (unsigned i = 0; i < sizeof(expt_test_nums) / sizeof(double); ++i) { Chris@0: const double num = serd_strtod(expt_test_strs[i], NULL); Chris@0: const double delta = fabs(num - expt_test_nums[i]); Chris@0: if (delta > DBL_EPSILON) { Chris@0: return failure("Parsed `%s' %lf != %lf (delta %lf)\n", Chris@0: expt_test_strs[i], num, expt_test_nums[i], delta); Chris@0: } Chris@0: } Chris@0: Chris@0: // Test serd_node_new_decimal Chris@0: Chris@0: const double dbl_test_nums[] = { Chris@0: 0.0, 9.0, 10.0, .01, 2.05, -16.00001, 5.000000005, 0.0000000001, NAN, INFINITY Chris@0: }; Chris@0: Chris@0: const char* dbl_test_strs[] = { Chris@0: "0.0", "9.0", "10.0", "0.01", "2.05", "-16.00001", "5.00000001", "0.0", NULL, NULL Chris@0: }; Chris@0: Chris@0: for (unsigned i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { Chris@0: SerdNode node = serd_node_new_decimal(dbl_test_nums[i], 8); Chris@0: const bool pass = (node.buf && dbl_test_strs[i]) Chris@0: ? !strcmp((const char*)node.buf, (const char*)dbl_test_strs[i]) Chris@0: : ((const char*)node.buf == dbl_test_strs[i]); Chris@0: if (!pass) { Chris@0: return failure("Serialised `%s' != %s\n", Chris@0: node.buf, dbl_test_strs[i]); Chris@0: } Chris@0: const size_t len = node.buf ? strlen((const char*)node.buf) : 0; Chris@0: if (node.n_bytes != len || node.n_chars != len) { Chris@0: return failure("Length %zu,%zu != %zu\n", Chris@0: node.n_bytes, node.n_chars, len); Chris@0: } Chris@0: serd_node_free(&node); Chris@0: } Chris@0: Chris@0: // Test serd_node_new_integer Chris@0: Chris@0: const long int_test_nums[] = { Chris@0: 0, -0, -23, 23, -12340, 1000, -1000 Chris@0: }; Chris@0: Chris@0: const char* int_test_strs[] = { Chris@0: "0", "0", "-23", "23", "-12340", "1000", "-1000" Chris@0: }; Chris@0: Chris@0: for (unsigned i = 0; i < sizeof(int_test_nums) / sizeof(double); ++i) { Chris@0: SerdNode node = serd_node_new_integer(int_test_nums[i]); Chris@0: if (strcmp((const char*)node.buf, (const char*)int_test_strs[i])) { Chris@0: return failure("Serialised `%s' != %s\n", Chris@0: node.buf, int_test_strs[i]); Chris@0: } Chris@0: const size_t len = strlen((const char*)node.buf); Chris@0: if (node.n_bytes != len || node.n_chars != len) { Chris@0: return failure("Length %zu,%zu != %zu\n", Chris@0: node.n_bytes, node.n_chars, len); Chris@0: } Chris@0: serd_node_free(&node); Chris@0: } Chris@0: Chris@0: // Test serd_node_new_blob Chris@0: for (size_t size = 0; size < 256; ++size) { Chris@0: uint8_t* data = (uint8_t*)malloc(size); Chris@0: for (size_t i = 0; i < size; ++i) { Chris@0: data[i] = (uint8_t)(rand() % 256); Chris@0: } Chris@0: Chris@0: SerdNode blob = serd_node_new_blob(data, size, size % 5); Chris@0: Chris@0: if (blob.n_bytes != blob.n_chars) { Chris@0: return failure("Blob %zu bytes != %zu chars\n", Chris@0: blob.n_bytes, blob.n_chars); Chris@0: } Chris@0: Chris@0: size_t out_size; Chris@0: uint8_t* out = (uint8_t*)serd_base64_decode( Chris@0: blob.buf, blob.n_bytes, &out_size); Chris@0: if (out_size != size) { Chris@0: return failure("Blob size %zu != %zu\n", out_size, size); Chris@0: } Chris@0: Chris@0: for (size_t i = 0; i < size; ++i) { Chris@0: if (out[i] != data[i]) { Chris@0: return failure("Corrupt blob at byte %zu\n", i); Chris@0: } Chris@0: } Chris@0: Chris@0: serd_node_free(&blob); Chris@0: free(out); Chris@0: free(data); Chris@0: } Chris@0: Chris@0: // Test serd_strlen Chris@0: Chris@0: const uint8_t str[] = { '"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0 }; Chris@0: Chris@0: size_t n_bytes; Chris@0: SerdNodeFlags flags; Chris@0: size_t len = serd_strlen(str, &n_bytes, &flags); Chris@0: if (len != 5 || n_bytes != 7 Chris@0: || flags != (SERD_HAS_QUOTE|SERD_HAS_NEWLINE)) { Chris@0: return failure("Bad serd_strlen(%s) len=%zu n_bytes=%zu flags=%u\n", Chris@0: str, len, n_bytes, flags); Chris@0: } Chris@0: len = serd_strlen(str, NULL, &flags); Chris@0: if (len != 5) { Chris@0: return failure("Bad serd_strlen(%s) len=%zu flags=%u\n", Chris@0: str, len, flags); Chris@0: } Chris@0: Chris@0: // Test serd_strerror Chris@0: Chris@0: const uint8_t* msg = NULL; Chris@0: if (strcmp((const char*)(msg = serd_strerror(SERD_SUCCESS)), "Success")) { Chris@0: return failure("Bad message `%s' for SERD_SUCCESS\n", msg); Chris@0: } Chris@0: for (int i = SERD_FAILURE; i <= SERD_ERR_INTERNAL; ++i) { Chris@0: msg = serd_strerror((SerdStatus)i); Chris@0: if (!strcmp((const char*)msg, "Success")) { Chris@0: return failure("Bad message `%s' for (SerdStatus)%d\n", msg, i); Chris@0: } Chris@0: } Chris@0: msg = serd_strerror((SerdStatus)-1); Chris@0: Chris@0: // Test serd_uri_to_path Chris@0: Chris@0: const uint8_t* uri = (const uint8_t*)"file:///home/user/foo.ttl"; Chris@0: if (strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")) { Chris@0: return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri); Chris@0: } Chris@0: uri = (const uint8_t*)"file://localhost/home/user/foo.ttl"; Chris@0: if (strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")) { Chris@0: return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri); Chris@0: } Chris@0: uri = (const uint8_t*)"file:illegal/file/uri"; Chris@0: if (serd_uri_to_path(uri)) { Chris@0: return failure("Converted invalid URI `%s' to path `%s'\n", Chris@0: uri, serd_uri_to_path(uri)); Chris@0: } Chris@0: uri = (const uint8_t*)"file:///c:/awful/system"; Chris@0: if (strcmp((const char*)serd_uri_to_path(uri), "c:/awful/system")) { Chris@0: return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri); Chris@0: } Chris@0: uri = (const uint8_t*)"file:///c:awful/system"; Chris@0: if (strcmp((const char*)serd_uri_to_path(uri), "/c:awful/system")) { Chris@0: return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri); Chris@0: } Chris@0: uri = (const uint8_t*)"file:///0/1"; Chris@0: if (strcmp((const char*)serd_uri_to_path(uri), "/0/1")) { Chris@0: return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri); Chris@0: } Chris@0: uri = (const uint8_t*)"C:\\Windows\\Sucks"; Chris@0: if (strcmp((const char*)serd_uri_to_path(uri), "C:\\Windows\\Sucks")) { Chris@0: return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri); Chris@0: } Chris@0: uri = (const uint8_t*)"C|/Windows/Sucks"; Chris@0: if (strcmp((const char*)serd_uri_to_path(uri), "C|/Windows/Sucks")) { Chris@0: return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri); Chris@0: } Chris@0: Chris@0: // Test serd_node_new_file_uri and serd_file_uri_parse Chris@0: SerdURI furi; Chris@0: const uint8_t* path_str = USTR("C:/My 100%"); Chris@0: SerdNode file_node = serd_node_new_file_uri(path_str, 0, &furi, true); Chris@0: uint8_t* hostname = NULL; Chris@0: uint8_t* out_path = serd_file_uri_parse(file_node.buf, &hostname); Chris@0: if (strcmp((const char*)file_node.buf, "file:///C:/My%20100%%")) { Chris@0: return failure("Bad URI %s\n", file_node.buf); Chris@0: } else if (hostname) { Chris@0: return failure("hostname `%s' shouldn't exist\n", hostname); Chris@0: } else if (strcmp((const char*)path_str, (const char*)out_path)) { Chris@0: return failure("path=>URI=>path failure %s => %s => %s\n", Chris@0: path_str, file_node.buf, out_path); Chris@0: } Chris@0: free(out_path); Chris@0: serd_node_free(&file_node); Chris@0: Chris@0: path_str = USTR("C:\\Pointless Space"); Chris@0: file_node = serd_node_new_file_uri(path_str, USTR("pwned"), 0, true); Chris@0: hostname = NULL; Chris@0: out_path = serd_file_uri_parse(file_node.buf, &hostname); Chris@0: if (strcmp((const char*)file_node.buf, "file://pwned/C:/Pointless%20Space")) { Chris@0: return failure("Bad URI %s\n", file_node.buf); Chris@0: } else if (!hostname || strcmp((const char*)hostname, "pwned")) { Chris@0: return failure("Bad hostname `%s'\n", hostname); Chris@0: } else if (strcmp((const char*)out_path, "C:/Pointless Space")) { Chris@0: return failure("path=>URI=>path failure %s => %s => %s\n", Chris@0: path_str, file_node.buf, out_path); Chris@0: } Chris@0: free(hostname); Chris@0: free(out_path); Chris@0: serd_node_free(&file_node); Chris@0: Chris@0: path_str = USTR("/foo/bar"); Chris@0: file_node = serd_node_new_file_uri(path_str, 0, 0, true); Chris@0: hostname = NULL; Chris@0: out_path = serd_file_uri_parse(file_node.buf, &hostname); Chris@0: if (strcmp((const char*)file_node.buf, "file:///foo/bar")) { Chris@0: return failure("Bad URI %s\n", file_node.buf); Chris@0: } else if (hostname) { Chris@0: return failure("hostname `%s' shouldn't exist\n", hostname); Chris@0: } else if (strcmp((const char*)path_str, (const char*)out_path)) { Chris@0: return failure("path=>URI=>path failure %s => %s => %s\n", Chris@0: path_str, file_node.buf, out_path); Chris@0: } Chris@0: free(out_path); Chris@0: serd_node_free(&file_node); Chris@0: Chris@0: path_str = USTR("/foo/bar"); Chris@0: file_node = serd_node_new_file_uri(path_str, USTR("localhost"), 0, true); Chris@0: out_path = serd_file_uri_parse(file_node.buf, &hostname); Chris@0: if (strcmp((const char*)file_node.buf, "file://localhost/foo/bar")) { Chris@0: return failure("Bad URI %s\n", file_node.buf); Chris@0: } else if (strcmp((const char*)hostname, "localhost")) { Chris@0: return failure("incorrect hostname `%s'\n", hostname); Chris@0: } else if (strcmp((const char*)path_str, (const char*)out_path)) { Chris@0: return failure("path=>URI=>path failure %s => %s => %s\n", Chris@0: path_str, file_node.buf, out_path); Chris@0: } Chris@0: free(hostname); Chris@0: free(out_path); Chris@0: serd_node_free(&file_node); Chris@0: Chris@0: path_str = USTR("a/relative path"); Chris@0: file_node = serd_node_new_file_uri(path_str, 0, 0, false); Chris@0: out_path = serd_file_uri_parse(file_node.buf, &hostname); Chris@0: if (strcmp((const char*)file_node.buf, "a/relative path")) { Chris@0: return failure("Bad URI %s\n", file_node.buf); Chris@0: } else if (hostname) { Chris@0: return failure("hostname `%s' shouldn't exist\n", hostname); Chris@0: } else if (strcmp((const char*)path_str, (const char*)out_path)) { Chris@0: return failure("path=>URI=>path failure %s => %s => %s\n", Chris@0: path_str, file_node.buf, out_path); Chris@0: } Chris@0: free(hostname); Chris@0: free(out_path); Chris@0: serd_node_free(&file_node); Chris@0: Chris@0: if (serd_file_uri_parse(USTR("file://invalid"), NULL)) { Chris@0: return failure("successfully parsed bogus URI \n"); Chris@0: } Chris@0: Chris@0: out_path = serd_file_uri_parse(USTR("file://host/foo/%XYbar"), NULL); Chris@0: if (strcmp((const char*)out_path, "/foo/bar")) { Chris@0: return failure("bad tolerance of junk escape: `%s'\n", out_path); Chris@0: } Chris@0: free(out_path); Chris@0: out_path = serd_file_uri_parse(USTR("file://host/foo/%0Abar"), NULL); Chris@0: if (strcmp((const char*)out_path, "/foo/bar")) { Chris@0: return failure("bad tolerance of junk escape: `%s'\n", out_path); Chris@0: } Chris@0: free(out_path); Chris@0: Chris@0: // Test serd_node_equals Chris@0: Chris@0: const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 }; Chris@0: SerdNode lhs = serd_node_from_string(SERD_LITERAL, replacement_char_str); Chris@0: SerdNode rhs = serd_node_from_string(SERD_LITERAL, USTR("123")); Chris@0: if (serd_node_equals(&lhs, &rhs)) { Chris@0: return failure("%s == %s\n", lhs.buf, rhs.buf); Chris@0: } Chris@0: Chris@0: SerdNode qnode = serd_node_from_string(SERD_CURIE, USTR("foo:bar")); Chris@0: if (serd_node_equals(&lhs, &qnode)) { Chris@0: return failure("%s == %s\n", lhs.buf, qnode.buf); Chris@0: } Chris@0: Chris@0: if (!serd_node_equals(&lhs, &lhs)) { Chris@0: return failure("%s != %s\n", lhs.buf, lhs.buf); Chris@0: } Chris@0: Chris@0: // Test serd_node_from_string Chris@0: Chris@0: SerdNode node = serd_node_from_string(SERD_LITERAL, (const uint8_t*)"hello\""); Chris@0: if (node.n_bytes != 6 || node.n_chars != 6 || node.flags != SERD_HAS_QUOTE Chris@0: || strcmp((const char*)node.buf, "hello\"")) { Chris@0: return failure("Bad node %s %zu %zu %d %d\n", Chris@0: node.buf, node.n_bytes, node.n_chars, node.flags, node.type); Chris@0: } Chris@0: Chris@0: node = serd_node_from_string(SERD_URI, NULL); Chris@0: if (!serd_node_equals(&node, &SERD_NODE_NULL)) { Chris@0: return failure("Creating node from NULL string failed\n"); Chris@0: } Chris@0: Chris@0: // Test serd_node_new_uri_from_string Chris@0: Chris@0: SerdURI base_uri; Chris@0: SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"), Chris@0: NULL, &base_uri); Chris@0: SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL); Chris@0: if (nil.type != SERD_URI || strcmp((const char*)nil.buf, (const char*)base.buf)) { Chris@0: return failure("URI %s != base %s\n", nil.buf, base.buf); Chris@0: } Chris@0: serd_node_free(&base); Chris@0: serd_node_free(&nil); Chris@0: Chris@0: // Test SerdEnv Chris@0: Chris@0: SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.org/foo")); Chris@0: SerdNode b = serd_node_from_string(SERD_CURIE, USTR("invalid")); Chris@0: SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg.2:b")); Chris@0: SerdEnv* env = serd_env_new(NULL); Chris@0: serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/")); Chris@0: Chris@0: if (!serd_env_set_base_uri(env, &node)) { Chris@0: return failure("Set base URI to %s\n", node.buf); Chris@0: } Chris@0: Chris@0: SerdChunk prefix, suffix; Chris@0: if (!serd_env_expand(env, &b, &prefix, &suffix)) { Chris@0: return failure("Expanded invalid curie %s\n", b.buf); Chris@0: } Chris@0: Chris@0: SerdNode xnode = serd_env_expand_node(env, &node); Chris@0: if (!serd_node_equals(&xnode, &SERD_NODE_NULL)) { Chris@0: return failure("Expanded %s to %s\n", c.buf, xnode.buf); Chris@0: } Chris@0: Chris@0: SerdNode xu = serd_env_expand_node(env, &u); Chris@0: if (strcmp((const char*)xu.buf, "http://example.org/foo")) { Chris@0: return failure("Expanded %s to %s\n", c.buf, xu.buf); Chris@0: } Chris@0: serd_node_free(&xu); Chris@0: Chris@0: SerdNode badpre = serd_node_from_string(SERD_CURIE, USTR("hm:what")); Chris@0: SerdNode xbadpre = serd_env_expand_node(env, &badpre); Chris@0: if (!serd_node_equals(&xbadpre, &SERD_NODE_NULL)) { Chris@0: return failure("Expanded invalid curie %s\n", badpre.buf); Chris@0: } Chris@0: Chris@0: SerdNode xc = serd_env_expand_node(env, &c); Chris@0: if (strcmp((const char*)xc.buf, "http://example.org/b")) { Chris@0: return failure("Expanded %s to %s\n", c.buf, xc.buf); Chris@0: } Chris@0: serd_node_free(&xc); Chris@0: Chris@0: if (!serd_env_set_prefix(env, &SERD_NODE_NULL, &SERD_NODE_NULL)) { Chris@0: return failure("Set NULL prefix\n"); Chris@0: } Chris@0: Chris@0: const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello")); Chris@0: if (!serd_env_set_prefix(env, &b, &lit)) { Chris@0: return failure("Set prefix to literal\n"); Chris@0: } Chris@0: Chris@0: int n_prefixes = 0; Chris@0: serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/")); Chris@0: serd_env_foreach(env, count_prefixes, &n_prefixes); Chris@0: if (n_prefixes != 1) { Chris@0: return failure("Bad prefix count %d\n", n_prefixes); Chris@0: } Chris@0: Chris@0: SerdNode shorter_uri = serd_node_from_string(SERD_URI, USTR("urn:foo")); Chris@0: SerdNode prefix_name; Chris@0: if (serd_env_qualify(env, &shorter_uri, &prefix_name, &suffix)) { Chris@0: return failure("Qualified %s\n", shorter_uri.buf); Chris@0: } Chris@0: Chris@0: // Test SerdReader and SerdWriter Chris@0: Chris@0: const char* path = "serd_test.ttl"; Chris@0: FILE* fd = fopen(path, "w"); Chris@0: if (!fd) { Chris@0: return failure("Failed to open file %s\n", path); Chris@0: } Chris@0: Chris@0: SerdWriter* writer = serd_writer_new( Chris@0: SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd); Chris@0: if (!writer) { Chris@0: return failure("Failed to create writer\n"); Chris@0: } Chris@0: Chris@0: serd_writer_chop_blank_prefix(writer, USTR("tmp")); Chris@0: serd_writer_chop_blank_prefix(writer, NULL); Chris@0: Chris@0: if (!serd_writer_set_base_uri(writer, &lit)) { Chris@0: return failure("Set base URI to %s\n", lit.buf); Chris@0: } Chris@0: Chris@0: if (!serd_writer_set_prefix(writer, &lit, &lit)) { Chris@0: return failure("Set prefix %s to %s\n", lit.buf, lit.buf); Chris@0: } Chris@0: Chris@0: if (!serd_writer_end_anon(writer, NULL)) { Chris@0: return failure("Ended non-existent anonymous node\n"); Chris@0: } Chris@0: Chris@0: if (serd_writer_get_env(writer) != env) { Chris@0: return failure("Writer has incorrect env\n"); Chris@0: } Chris@0: Chris@0: uint8_t buf[] = { 0x80, 0, 0, 0, 0 }; Chris@0: SerdNode s = serd_node_from_string(SERD_URI, USTR("")); Chris@0: SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/pred")); Chris@0: SerdNode o = serd_node_from_string(SERD_LITERAL, buf); Chris@0: Chris@0: // Write 3 invalid statements (should write nothing) Chris@0: const SerdNode* junk[][5] = { { &s, &p, NULL, NULL, NULL }, Chris@0: { &s, NULL, &o, NULL, NULL }, Chris@0: { NULL, &p, &o, NULL, NULL }, Chris@0: { &s, &p, &SERD_NODE_NULL, NULL, NULL }, Chris@0: { &s, &SERD_NODE_NULL, &o, NULL, NULL }, Chris@0: { &SERD_NODE_NULL, &p, &o, NULL, NULL }, Chris@0: { &s, &o, &o, NULL, NULL }, Chris@0: { &o, &p, &o, NULL, NULL }, Chris@0: { NULL, NULL, NULL, NULL, NULL } }; Chris@0: for (unsigned i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 5); ++i) { Chris@0: if (!serd_writer_write_statement( Chris@0: writer, 0, NULL, Chris@0: junk[i][0], junk[i][1], junk[i][2], junk[i][3], junk[i][4])) { Chris@0: return failure("Successfully wrote junk statement %d\n", i); Chris@0: } Chris@0: } Chris@0: Chris@0: const SerdNode t = serd_node_from_string(SERD_URI, USTR("urn:Type")); Chris@0: const SerdNode l = serd_node_from_string(SERD_LITERAL, USTR("en")); Chris@0: const SerdNode* good[][5] = { { &s, &p, &o, NULL, NULL }, Chris@0: { &s, &p, &o, &SERD_NODE_NULL, &SERD_NODE_NULL }, Chris@0: { &s, &p, &o, &t, NULL }, Chris@0: { &s, &p, &o, NULL, &l }, Chris@0: { &s, &p, &o, &t, &l }, Chris@0: { &s, &p, &o, &t, &SERD_NODE_NULL }, Chris@0: { &s, &p, &o, &SERD_NODE_NULL, &l }, Chris@0: { &s, &p, &o, NULL, &SERD_NODE_NULL }, Chris@0: { &s, &p, &o, &SERD_NODE_NULL, NULL }, Chris@0: { &s, &p, &o, &SERD_NODE_NULL, NULL } }; Chris@0: for (unsigned i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 5); ++i) { Chris@0: if (serd_writer_write_statement( Chris@0: writer, 0, NULL, Chris@0: good[i][0], good[i][1], good[i][2], good[i][3], good[i][4])) { Chris@0: return failure("Failed to write good statement %d\n", i); Chris@0: } Chris@0: } Chris@0: Chris@0: // Write 1 statement with bad UTF-8 (should be replaced) Chris@0: if (serd_writer_write_statement(writer, 0, NULL, Chris@0: &s, &p, &o, NULL, NULL)) { Chris@0: return failure("Failed to write junk UTF-8\n"); Chris@0: } Chris@0: Chris@0: // Write 1 valid statement Chris@0: o = serd_node_from_string(SERD_LITERAL, USTR("hello")); Chris@0: if (serd_writer_write_statement(writer, 0, NULL, Chris@0: &s, &p, &o, NULL, NULL)) { Chris@0: return failure("Failed to write valid statement\n"); Chris@0: } Chris@0: Chris@0: serd_writer_free(writer); Chris@0: Chris@0: // Test chunk sink Chris@0: SerdChunk chunk = { NULL, 0 }; Chris@0: writer = serd_writer_new( Chris@0: SERD_TURTLE, (SerdStyle)0, env, NULL, serd_chunk_sink, &chunk); Chris@0: Chris@0: o = serd_node_from_string(SERD_URI, USTR("http://example.org/base")); Chris@0: if (serd_writer_set_base_uri(writer, &o)) { Chris@0: return failure("Failed to write to chunk sink\n"); Chris@0: } Chris@0: Chris@0: serd_writer_free(writer); Chris@0: uint8_t* out = serd_chunk_sink_finish(&chunk); Chris@0: Chris@0: if (strcmp((const char*)out, "@base .\n")) { Chris@0: return failure("Incorrect chunk output:\n%s\n", chunk.buf); Chris@0: } Chris@0: Chris@0: free(out); Chris@0: Chris@0: // Rewind and test reader Chris@0: fseek(fd, 0, SEEK_SET); Chris@0: Chris@0: ReaderTest* rt = (ReaderTest*)malloc(sizeof(ReaderTest)); Chris@0: rt->n_statements = 0; Chris@0: rt->graph = NULL; Chris@0: Chris@0: SerdReader* reader = serd_reader_new( Chris@0: SERD_TURTLE, rt, free, Chris@0: NULL, NULL, test_sink, NULL); Chris@0: if (!reader) { Chris@0: return failure("Failed to create reader\n"); Chris@0: } Chris@0: if (serd_reader_get_handle(reader) != rt) { Chris@0: return failure("Corrupt reader handle\n"); Chris@0: } Chris@0: Chris@0: SerdNode g = serd_node_from_string(SERD_URI, USTR("http://example.org/")); Chris@0: serd_reader_set_default_graph(reader, &g); Chris@0: serd_reader_add_blank_prefix(reader, USTR("tmp")); Chris@0: serd_reader_add_blank_prefix(reader, NULL); Chris@0: Chris@0: if (!serd_reader_read_file(reader, USTR("http://notafile"))) { Chris@0: return failure("Apparently read an http URI\n"); Chris@0: } Chris@0: if (!serd_reader_read_file(reader, USTR("file:///better/not/exist"))) { Chris@0: return failure("Apprently read a non-existent file\n"); Chris@0: } Chris@0: SerdStatus st = serd_reader_read_file(reader, USTR(path)); Chris@0: if (st) { Chris@0: return failure("Error reading file (%s)\n", serd_strerror(st)); Chris@0: } Chris@0: Chris@0: if (rt->n_statements != 12) { Chris@0: return failure("Bad statement count %d\n", rt->n_statements); Chris@0: } else if (!rt->graph || !rt->graph->buf || Chris@0: strcmp((const char*)rt->graph->buf, "http://example.org/")) { Chris@0: return failure("Bad graph %p\n", rt->graph); Chris@0: } Chris@0: Chris@0: if (!serd_reader_read_string(reader, USTR("This isn't Turtle at all."))) { Chris@0: return failure("Parsed invalid string successfully.\n"); Chris@0: } Chris@0: Chris@0: serd_reader_free(reader); Chris@0: fclose(fd); Chris@0: Chris@0: serd_env_free(env); Chris@0: Chris@0: printf("Success\n"); Chris@0: return 0; Chris@0: }