diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/test_env.c | 94 | ||||
-rw-r--r-- | test/test_free_null.c | 12 | ||||
-rw-r--r-- | test/test_node.c | 239 | ||||
-rw-r--r-- | test/test_read_chunk.c | 117 | ||||
-rw-r--r-- | test/test_reader_writer.c | 529 | ||||
-rw-r--r-- | test/test_string.c | 42 | ||||
-rw-r--r-- | test/test_uri.c | 216 |
7 files changed, 635 insertions, 614 deletions
diff --git a/test/test_env.c b/test/test_env.c index 36b8f6c3..2fa9d3b5 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -27,74 +27,76 @@ static SerdStatus count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri) { - (void)name; - (void)uri; + (void)name; + (void)uri; - ++*(int*)handle; - return SERD_SUCCESS; + ++*(int*)handle; + return SERD_SUCCESS; } static void test_env(void) { - SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.org/foo")); - SerdNode b = serd_node_from_string(SERD_CURIE, USTR("invalid")); - SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg.2:b")); - SerdEnv* env = serd_env_new(NULL); - serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/")); + SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.org/foo")); + SerdNode b = serd_node_from_string(SERD_CURIE, USTR("invalid")); + SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg.2:b")); + SerdEnv* env = serd_env_new(NULL); + serd_env_set_prefix_from_strings( + env, USTR("eg.2"), USTR("http://example.org/")); - assert(!serd_env_set_base_uri(env, NULL)); - assert(serd_env_set_base_uri(env, &SERD_NODE_NULL)); - assert(serd_node_equals(serd_env_get_base_uri(env, NULL), &SERD_NODE_NULL)); + assert(!serd_env_set_base_uri(env, NULL)); + assert(serd_env_set_base_uri(env, &SERD_NODE_NULL)); + assert(serd_node_equals(serd_env_get_base_uri(env, NULL), &SERD_NODE_NULL)); - SerdChunk prefix; - SerdChunk suffix; - assert(serd_env_expand(env, &b, &prefix, &suffix)); + SerdChunk prefix; + SerdChunk suffix; + assert(serd_env_expand(env, &b, &prefix, &suffix)); - SerdNode xnode = serd_env_expand_node(env, &SERD_NODE_NULL); - assert(serd_node_equals(&xnode, &SERD_NODE_NULL)); + SerdNode xnode = serd_env_expand_node(env, &SERD_NODE_NULL); + assert(serd_node_equals(&xnode, &SERD_NODE_NULL)); - SerdNode xu = serd_env_expand_node(env, &u); - assert(!strcmp((const char*)xu.buf, "http://example.org/foo")); - serd_node_free(&xu); + SerdNode xu = serd_env_expand_node(env, &u); + assert(!strcmp((const char*)xu.buf, "http://example.org/foo")); + serd_node_free(&xu); - SerdNode badpre = serd_node_from_string(SERD_CURIE, USTR("hm:what")); - SerdNode xbadpre = serd_env_expand_node(env, &badpre); - assert(serd_node_equals(&xbadpre, &SERD_NODE_NULL)); + SerdNode badpre = serd_node_from_string(SERD_CURIE, USTR("hm:what")); + SerdNode xbadpre = serd_env_expand_node(env, &badpre); + assert(serd_node_equals(&xbadpre, &SERD_NODE_NULL)); - SerdNode xc = serd_env_expand_node(env, &c); - assert(!strcmp((const char*)xc.buf, "http://example.org/b")); - serd_node_free(&xc); + SerdNode xc = serd_env_expand_node(env, &c); + assert(!strcmp((const char*)xc.buf, "http://example.org/b")); + serd_node_free(&xc); - assert(serd_env_set_prefix(env, &SERD_NODE_NULL, &SERD_NODE_NULL)); + assert(serd_env_set_prefix(env, &SERD_NODE_NULL, &SERD_NODE_NULL)); - const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello")); - assert(serd_env_set_prefix(env, &b, &lit)); + const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello")); + assert(serd_env_set_prefix(env, &b, &lit)); - const SerdNode blank = serd_node_from_string(SERD_BLANK, USTR("b1")); - const SerdNode xblank = serd_env_expand_node(env, &blank); - assert(serd_node_equals(&xblank, &SERD_NODE_NULL)); + const SerdNode blank = serd_node_from_string(SERD_BLANK, USTR("b1")); + const SerdNode xblank = serd_env_expand_node(env, &blank); + assert(serd_node_equals(&xblank, &SERD_NODE_NULL)); - int n_prefixes = 0; - serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/")); - serd_env_foreach(env, count_prefixes, &n_prefixes); - assert(n_prefixes == 1); + int n_prefixes = 0; + serd_env_set_prefix_from_strings( + env, USTR("eg.2"), USTR("http://example.org/")); + serd_env_foreach(env, count_prefixes, &n_prefixes); + assert(n_prefixes == 1); - SerdNode shorter_uri = serd_node_from_string(SERD_URI, USTR("urn:foo")); - SerdNode prefix_name; - assert(!serd_env_qualify(env, &shorter_uri, &prefix_name, &suffix)); + SerdNode shorter_uri = serd_node_from_string(SERD_URI, USTR("urn:foo")); + SerdNode prefix_name; + assert(!serd_env_qualify(env, &shorter_uri, &prefix_name, &suffix)); - assert(!serd_env_set_base_uri(env, &u)); - assert(serd_node_equals(serd_env_get_base_uri(env, NULL), &u)); - assert(!serd_env_set_base_uri(env, NULL)); - assert(!serd_env_get_base_uri(env, NULL)->buf); + assert(!serd_env_set_base_uri(env, &u)); + assert(serd_node_equals(serd_env_get_base_uri(env, NULL), &u)); + assert(!serd_env_set_base_uri(env, NULL)); + assert(!serd_env_get_base_uri(env, NULL)->buf); - serd_env_free(env); + serd_env_free(env); } int main(void) { - test_env(); - return 0; + test_env(); + return 0; } diff --git a/test/test_free_null.c b/test/test_free_null.c index 121a66cd..a544a005 100644 --- a/test/test_free_null.c +++ b/test/test_free_null.c @@ -23,11 +23,11 @@ int main(void) { - serd_free(NULL); - serd_node_free(NULL); - serd_env_free(NULL); - serd_reader_free(NULL); - serd_writer_free(NULL); + serd_free(NULL); + serd_node_free(NULL); + serd_env_free(NULL); + serd_reader_free(NULL); + serd_writer_free(NULL); - return 0; + return 0; } diff --git a/test/test_node.c b/test/test_node.c index 45c40aa4..f65a7c22 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -30,174 +30,183 @@ #define USTR(s) ((const uint8_t*)(s)) #ifndef INFINITY -# define INFINITY (DBL_MAX + DBL_MAX) +# define INFINITY (DBL_MAX + DBL_MAX) #endif #ifndef NAN -# define NAN (INFINITY - INFINITY) +# define NAN (INFINITY - INFINITY) #endif static void test_strtod(double dbl, double max_delta) { - char buf[1024]; - snprintf(buf, sizeof(buf), "%f", dbl); + char buf[1024]; + snprintf(buf, sizeof(buf), "%f", dbl); - char* endptr = NULL; - const double out = serd_strtod(buf, &endptr); + char* endptr = NULL; + const double out = serd_strtod(buf, &endptr); + const double diff = fabs(out - dbl); - const double diff = fabs(out - dbl); - assert(diff <= max_delta); + assert(diff <= max_delta); } static void test_string_to_double(void) { - const double expt_test_nums[] = { - 2.0E18, -5e19, +8e20, 2e+24, -5e-5, 8e0, 9e-0, 2e+0 - }; + const double expt_test_nums[] = { + 2.0E18, -5e19, +8e20, 2e+24, -5e-5, 8e0, 9e-0, 2e+0}; - const char* expt_test_strs[] = { - "02e18", "-5e019", "+8e20", "2E+24", "-5E-5", "8E0", "9e-0", " 2e+0" - }; + const char* expt_test_strs[] = { + "02e18", "-5e019", "+8e20", "2E+24", "-5E-5", "8E0", "9e-0", " 2e+0"}; - for (size_t i = 0; i < sizeof(expt_test_nums) / sizeof(double); ++i) { - const double num = serd_strtod(expt_test_strs[i], NULL); - const double delta = fabs(num - expt_test_nums[i]); - assert(delta <= DBL_EPSILON); + for (size_t i = 0; i < sizeof(expt_test_nums) / sizeof(double); ++i) { + const double num = serd_strtod(expt_test_strs[i], NULL); + const double delta = fabs(num - expt_test_nums[i]); + assert(delta <= DBL_EPSILON); - test_strtod(expt_test_nums[i], DBL_EPSILON); - } + test_strtod(expt_test_nums[i], DBL_EPSILON); + } } static void test_double_to_node(void) { - const double dbl_test_nums[] = { - 0.0, 9.0, 10.0, .01, 2.05, -16.00001, 5.000000005, 0.0000000001, NAN, INFINITY - }; - - const char* dbl_test_strs[] = { - "0.0", "9.0", "10.0", "0.01", "2.05", "-16.00001", "5.00000001", "0.0", NULL, NULL - }; - - for (size_t i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { - SerdNode node = serd_node_new_decimal(dbl_test_nums[i], 8); - const bool pass = (node.buf && dbl_test_strs[i]) - ? !strcmp((const char*)node.buf, dbl_test_strs[i]) - : ((const char*)node.buf == dbl_test_strs[i]); - assert(pass); - const size_t len = node.buf ? strlen((const char*)node.buf) : 0; - assert(node.n_bytes == len && node.n_chars == len); - serd_node_free(&node); - } + const double dbl_test_nums[] = {0.0, + 9.0, + 10.0, + .01, + 2.05, + -16.00001, + 5.000000005, + 0.0000000001, + NAN, + INFINITY}; + + const char* dbl_test_strs[] = {"0.0", + "9.0", + "10.0", + "0.01", + "2.05", + "-16.00001", + "5.00000001", + "0.0", + NULL, + NULL}; + + for (size_t i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { + SerdNode node = serd_node_new_decimal(dbl_test_nums[i], 8); + const bool pass = (node.buf && dbl_test_strs[i]) + ? !strcmp((const char*)node.buf, dbl_test_strs[i]) + : ((const char*)node.buf == dbl_test_strs[i]); + assert(pass); + const size_t len = node.buf ? strlen((const char*)node.buf) : 0; + assert(node.n_bytes == len && node.n_chars == len); + serd_node_free(&node); + } } static void test_integer_to_node(void) { - const long int_test_nums[] = { - 0, -0, -23, 23, -12340, 1000, -1000 - }; - - const char* int_test_strs[] = { - "0", "0", "-23", "23", "-12340", "1000", "-1000" - }; - - for (size_t i = 0; i < sizeof(int_test_nums) / sizeof(double); ++i) { - SerdNode node = serd_node_new_integer(int_test_nums[i]); - assert(!strcmp((const char*)node.buf, (const char*)int_test_strs[i])); - const size_t len = strlen((const char*)node.buf); - assert(node.n_bytes == len && node.n_chars == len); - serd_node_free(&node); - } + const long int_test_nums[] = {0, -0, -23, 23, -12340, 1000, -1000}; + + const char* int_test_strs[] = { + "0", "0", "-23", "23", "-12340", "1000", "-1000"}; + + for (size_t i = 0; i < sizeof(int_test_nums) / sizeof(double); ++i) { + SerdNode node = serd_node_new_integer(int_test_nums[i]); + assert(!strcmp((const char*)node.buf, (const char*)int_test_strs[i])); + const size_t len = strlen((const char*)node.buf); + assert(node.n_bytes == len && node.n_chars == len); + serd_node_free(&node); + } } static void test_blob_to_node(void) { - for (size_t size = 1; size < 256; ++size) { - uint8_t* const data = (uint8_t*)malloc(size); - for (size_t i = 0; i < size; ++i) { - data[i] = (uint8_t)((size + i) % 256); - } - - SerdNode blob = serd_node_new_blob(data, size, size % 5); - - assert(blob.n_bytes == blob.n_chars); - assert(blob.n_bytes == strlen((const char*)blob.buf)); - - size_t out_size = 0; - uint8_t* out = (uint8_t*)serd_base64_decode( - blob.buf, blob.n_bytes, &out_size); - assert(out_size == size); - - for (size_t i = 0; i < size; ++i) { - assert(out[i] == data[i]); - } - - serd_node_free(&blob); - serd_free(out); - free(data); - } + for (size_t size = 1; size < 256; ++size) { + uint8_t* const data = (uint8_t*)malloc(size); + for (size_t i = 0; i < size; ++i) { + data[i] = (uint8_t)((size + i) % 256); + } + + SerdNode blob = serd_node_new_blob(data, size, size % 5); + + assert(blob.n_bytes == blob.n_chars); + assert(blob.n_bytes == strlen((const char*)blob.buf)); + + size_t out_size = 0; + uint8_t* out = + (uint8_t*)serd_base64_decode(blob.buf, blob.n_bytes, &out_size); + assert(out_size == size); + + for (size_t i = 0; i < size; ++i) { + assert(out[i] == data[i]); + } + + serd_node_free(&blob); + serd_free(out); + free(data); + } } static void test_node_equals(void) { - const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 }; - SerdNode lhs = serd_node_from_string(SERD_LITERAL, replacement_char_str); - SerdNode rhs = serd_node_from_string(SERD_LITERAL, USTR("123")); - assert(!serd_node_equals(&lhs, &rhs)); + const uint8_t replacement_char_str[] = {0xEF, 0xBF, 0xBD, 0}; + SerdNode lhs = serd_node_from_string(SERD_LITERAL, replacement_char_str); + SerdNode rhs = serd_node_from_string(SERD_LITERAL, USTR("123")); + assert(!serd_node_equals(&lhs, &rhs)); - SerdNode qnode = serd_node_from_string(SERD_CURIE, USTR("foo:bar")); - assert(!serd_node_equals(&lhs, &qnode)); - assert(serd_node_equals(&lhs, &lhs)); + SerdNode qnode = serd_node_from_string(SERD_CURIE, USTR("foo:bar")); + assert(!serd_node_equals(&lhs, &qnode)); + assert(serd_node_equals(&lhs, &lhs)); - SerdNode null_copy = serd_node_copy(&SERD_NODE_NULL); - assert(serd_node_equals(&SERD_NODE_NULL, &null_copy)); + SerdNode null_copy = serd_node_copy(&SERD_NODE_NULL); + assert(serd_node_equals(&SERD_NODE_NULL, &null_copy)); } static void test_node_from_string(void) { - SerdNode node = serd_node_from_string(SERD_LITERAL, (const uint8_t*)"hello\""); - assert(node.n_bytes == 6 && node.n_chars == 6 && - node.flags == SERD_HAS_QUOTE && - !strcmp((const char*)node.buf, "hello\"")); + SerdNode node = + serd_node_from_string(SERD_LITERAL, (const uint8_t*)"hello\""); - node = serd_node_from_string(SERD_URI, NULL); - assert(serd_node_equals(&node, &SERD_NODE_NULL)); + assert(node.n_bytes == 6 && node.n_chars == 6 && + node.flags == SERD_HAS_QUOTE && + !strcmp((const char*)node.buf, "hello\"")); + + node = serd_node_from_string(SERD_URI, NULL); + assert(serd_node_equals(&node, &SERD_NODE_NULL)); } static void test_node_from_substring(void) { - SerdNode empty = serd_node_from_substring(SERD_LITERAL, NULL, 32); - assert(!empty.buf && !empty.n_bytes && !empty.n_chars && !empty.flags && - !empty.type); - - SerdNode a_b = serd_node_from_substring(SERD_LITERAL, USTR("a\"bc"), 3); - assert(a_b.n_bytes == 3 && a_b.n_chars == 3 && - a_b.flags == SERD_HAS_QUOTE && - !strncmp((const char*)a_b.buf, "a\"b", 3)); - - a_b = serd_node_from_substring(SERD_LITERAL, USTR("a\"bc"), 10); - assert(a_b.n_bytes == 4 && a_b.n_chars == 4 && - a_b.flags == SERD_HAS_QUOTE && - !strncmp((const char*)a_b.buf, "a\"bc", 4)); + SerdNode empty = serd_node_from_substring(SERD_LITERAL, NULL, 32); + assert(!empty.buf && !empty.n_bytes && !empty.n_chars && !empty.flags && + !empty.type); + + SerdNode a_b = serd_node_from_substring(SERD_LITERAL, USTR("a\"bc"), 3); + assert(a_b.n_bytes == 3 && a_b.n_chars == 3 && a_b.flags == SERD_HAS_QUOTE && + !strncmp((const char*)a_b.buf, "a\"b", 3)); + + a_b = serd_node_from_substring(SERD_LITERAL, USTR("a\"bc"), 10); + assert(a_b.n_bytes == 4 && a_b.n_chars == 4 && a_b.flags == SERD_HAS_QUOTE && + !strncmp((const char*)a_b.buf, "a\"bc", 4)); } int main(void) { - test_string_to_double(); - test_double_to_node(); - test_integer_to_node(); - test_blob_to_node(); - test_node_equals(); - test_node_from_string(); - test_node_from_substring(); - - printf("Success\n"); - return 0; + test_string_to_double(); + test_double_to_node(); + test_integer_to_node(); + test_blob_to_node(); + test_node_equals(); + test_node_from_string(); + test_node_from_substring(); + + printf("Success\n"); + return 0; } diff --git a/test/test_read_chunk.c b/test/test_read_chunk.c index cc9c2e19..2e7f2b0d 100644 --- a/test/test_read_chunk.c +++ b/test/test_read_chunk.c @@ -30,22 +30,22 @@ static size_t n_end = 0; static SerdStatus on_base(void* handle, const SerdNode* uri) { - (void)handle; - (void)uri; + (void)handle; + (void)uri; - ++n_base; - return SERD_SUCCESS; + ++n_base; + return SERD_SUCCESS; } static SerdStatus on_prefix(void* handle, const SerdNode* name, const SerdNode* uri) { - (void)handle; - (void)name; - (void)uri; + (void)handle; + (void)name; + (void)uri; - ++n_prefix; - return SERD_SUCCESS; + ++n_prefix; + return SERD_SUCCESS; } static SerdStatus @@ -58,64 +58,65 @@ on_statement(void* handle, const SerdNode* object_datatype, const SerdNode* object_lang) { - (void)handle; - (void)flags; - (void)graph; - (void)subject; - (void)predicate; - (void)object; - (void)object_datatype; - (void)object_lang; - - ++n_statement; - return SERD_SUCCESS; + (void)handle; + (void)flags; + (void)graph; + (void)subject; + (void)predicate; + (void)object; + (void)object_datatype; + (void)object_lang; + + ++n_statement; + return SERD_SUCCESS; } static SerdStatus on_end(void* handle, const SerdNode* node) { - (void)handle; - (void)node; + (void)handle; + (void)node; - ++n_end; - return SERD_SUCCESS; + ++n_end; + return SERD_SUCCESS; } int main(void) { - FILE* file = tmpfile(); - - fprintf(file, - "@prefix eg: <http://example.org/> .\n" - "@base <http://example.org/base> .\n" - "eg:s1 eg:p1 eg:o1 ;\n" - " eg:p2 eg:o2 ,\n" - " eg:o3 .\n" - "eg:s2 eg:p1 eg:o1 ;\n" - " eg:p2 eg:o2 .\n" - "eg:s3 eg:p1 eg:o1 .\n" - "eg:s4 eg:p1 [ eg:p3 eg:o1 ] .\n"); - - fseek(file, 0, SEEK_SET); - - SerdReader* reader = serd_reader_new( - SERD_TURTLE, NULL, NULL, on_base, on_prefix, on_statement, on_end); - assert(reader); - - assert(!serd_reader_start_stream(reader, file, NULL, true)); - - assert(!serd_reader_read_chunk(reader) && n_prefix == 1); - assert(!serd_reader_read_chunk(reader) && n_base == 1); - assert(!serd_reader_read_chunk(reader) && n_statement == 3); - assert(!serd_reader_read_chunk(reader) && n_statement == 5); - assert(!serd_reader_read_chunk(reader) && n_statement == 6); - assert(!serd_reader_read_chunk(reader) && n_statement == 8 && n_end == 1); - assert(serd_reader_read_chunk(reader) == SERD_FAILURE); - assert(serd_reader_read_chunk(reader) == SERD_FAILURE); - assert(!serd_reader_end_stream(reader)); - - serd_reader_free(reader); - fclose(file); - return 0; + FILE* file = tmpfile(); + + fprintf(file, + "@prefix eg: <http://example.org/> .\n" + "@base <http://example.org/base> .\n" + "eg:s1 eg:p1 eg:o1 ;\n" + " eg:p2 eg:o2 ,\n" + " eg:o3 .\n" + "eg:s2 eg:p1 eg:o1 ;\n" + " eg:p2 eg:o2 .\n" + "eg:s3 eg:p1 eg:o1 .\n" + "eg:s4 eg:p1 [ eg:p3 eg:o1 ] .\n"); + + fseek(file, 0, SEEK_SET); + + SerdReader* reader = serd_reader_new( + SERD_TURTLE, NULL, NULL, on_base, on_prefix, on_statement, on_end); + + assert(reader); + assert(!serd_reader_start_stream(reader, file, NULL, true)); + + assert(!serd_reader_read_chunk(reader) && n_prefix == 1); + assert(!serd_reader_read_chunk(reader) && n_base == 1); + assert(!serd_reader_read_chunk(reader) && n_statement == 3); + assert(!serd_reader_read_chunk(reader) && n_statement == 5); + assert(!serd_reader_read_chunk(reader) && n_statement == 6); + assert(!serd_reader_read_chunk(reader) && n_statement == 8 && n_end == 1); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + + assert(!serd_reader_end_stream(reader)); + serd_reader_free(reader); + fclose(file); + + return 0; } diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 22576f72..d47bcccc 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -28,8 +28,8 @@ #define USTR(s) ((const uint8_t*)(s)) typedef struct { - int n_statements; - const SerdNode* graph; + int n_statements; + const SerdNode* graph; } ReaderTest; static SerdStatus @@ -42,315 +42,322 @@ test_sink(void* handle, const SerdNode* object_datatype, const SerdNode* object_lang) { - (void)flags; - (void)subject; - (void)predicate; - (void)object; - (void)object_datatype; - (void)object_lang; - - ReaderTest* rt = (ReaderTest*)handle; - ++rt->n_statements; - rt->graph = graph; - return SERD_SUCCESS; + (void)flags; + (void)subject; + (void)predicate; + (void)object; + (void)object_datatype; + (void)object_lang; + + ReaderTest* rt = (ReaderTest*)handle; + ++rt->n_statements; + rt->graph = graph; + return SERD_SUCCESS; } /// Returns EOF after a statement, then succeeds again (like a socket) static size_t eof_test_read(void* buf, size_t size, size_t nmemb, void* stream) { - assert(nmemb == 1); + assert(nmemb == 1); - static const char* const string = "_:s1 <http://example.org/p> _:o1 .\n" - "_:s2 <http://example.org/p> _:o2 .\n"; + static const char* const string = "_:s1 <http://example.org/p> _:o1 .\n" + "_:s2 <http://example.org/p> _:o2 .\n"; - size_t* count = (size_t*)stream; - if (*count == 34 || *count == 35 || *count + nmemb >= strlen(string)) { - ++*count; - return 0; - } + size_t* count = (size_t*)stream; + if (*count == 34 || *count == 35 || *count + nmemb >= strlen(string)) { + ++*count; + return 0; + } - memcpy((char*)buf, string + *count, size * nmemb); - *count += nmemb; - return nmemb; + memcpy((char*)buf, string + *count, size * nmemb); + *count += nmemb; + return nmemb; } static int eof_test_error(void* stream) { - (void)stream; - return 0; + (void)stream; + return 0; } static void test_read_chunks(void) { - ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - FILE* const f = tmpfile(); - static const char null = 0; - SerdReader* const reader = - serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); - - assert(reader); - assert(serd_reader_get_handle(reader) == rt); - assert(f); - - SerdStatus st = serd_reader_start_stream(reader, f, NULL, false); - assert(st == SERD_SUCCESS); - - // Write two statement separated by null characters - fprintf(f, "@prefix eg: <http://example.org/> .\n"); - fprintf(f, "eg:s eg:p eg:o1 .\n"); - fwrite(&null, sizeof(null), 1, f); - fprintf(f, "eg:s eg:p eg:o2 .\n"); - fwrite(&null, sizeof(null), 1, f); - fseek(f, 0, SEEK_SET); - - // Read prefix - st = serd_reader_read_chunk(reader); - assert(st == SERD_SUCCESS); - assert(rt->n_statements == 0); - - // Read first statement - st = serd_reader_read_chunk(reader); - assert(st == SERD_SUCCESS); - assert(rt->n_statements == 1); - - // Read terminator - st = serd_reader_read_chunk(reader); - assert(st == SERD_FAILURE); - assert(rt->n_statements == 1); - - // Read second statement (after null terminator) - st = serd_reader_read_chunk(reader); - assert(st == SERD_SUCCESS); - assert(rt->n_statements == 2); - - // Read terminator - st = serd_reader_read_chunk(reader); - assert(st == SERD_FAILURE); - assert(rt->n_statements == 2); - - // EOF - st = serd_reader_read_chunk(reader); - assert(st == SERD_FAILURE); - assert(rt->n_statements == 2); - - serd_reader_free(reader); - fclose(f); + ReaderTest* const rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + FILE* const f = tmpfile(); + static const char null = 0; + SerdReader* const reader = + serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + + assert(reader); + assert(serd_reader_get_handle(reader) == rt); + assert(f); + + SerdStatus st = serd_reader_start_stream(reader, f, NULL, false); + assert(st == SERD_SUCCESS); + + // Write two statement separated by null characters + fprintf(f, "@prefix eg: <http://example.org/> .\n"); + fprintf(f, "eg:s eg:p eg:o1 .\n"); + fwrite(&null, sizeof(null), 1, f); + fprintf(f, "eg:s eg:p eg:o2 .\n"); + fwrite(&null, sizeof(null), 1, f); + fseek(f, 0, SEEK_SET); + + // Read prefix + st = serd_reader_read_chunk(reader); + assert(st == SERD_SUCCESS); + assert(rt->n_statements == 0); + + // Read first statement + st = serd_reader_read_chunk(reader); + assert(st == SERD_SUCCESS); + assert(rt->n_statements == 1); + + // Read terminator + st = serd_reader_read_chunk(reader); + assert(st == SERD_FAILURE); + assert(rt->n_statements == 1); + + // Read second statement (after null terminator) + st = serd_reader_read_chunk(reader); + assert(st == SERD_SUCCESS); + assert(rt->n_statements == 2); + + // Read terminator + st = serd_reader_read_chunk(reader); + assert(st == SERD_FAILURE); + assert(rt->n_statements == 2); + + // EOF + st = serd_reader_read_chunk(reader); + assert(st == SERD_FAILURE); + assert(rt->n_statements == 2); + + serd_reader_free(reader); + fclose(f); } static void test_read_string(void) { - ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdReader* reader = - serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + SerdReader* reader = + serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); - assert(reader); - assert(serd_reader_get_handle(reader) == rt); + assert(reader); + assert(serd_reader_get_handle(reader) == rt); - // Test reading a string that ends exactly at the end of input (no newline) - const SerdStatus st = - serd_reader_read_string(reader, - USTR("<http://example.org/s> <http://example.org/p> " - "<http://example.org/o> .")); + // Test reading a string that ends exactly at the end of input (no newline) + const SerdStatus st = serd_reader_read_string( + reader, + USTR("<http://example.org/s> <http://example.org/p> " + "<http://example.org/o> .")); - assert(!st); - assert(rt->n_statements == 1); + assert(!st); + assert(rt->n_statements == 1); - serd_reader_free(reader); + serd_reader_free(reader); } static void test_writer(const char* const path) { - FILE* fd = fopen(path, "wb"); - SerdEnv* env = serd_env_new(NULL); - assert(fd); - - SerdWriter* writer = serd_writer_new( - SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd); - assert(writer); - - serd_writer_chop_blank_prefix(writer, USTR("tmp")); - serd_writer_chop_blank_prefix(writer, NULL); - - const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello")); - - assert(serd_writer_set_base_uri(writer, &lit)); - assert(serd_writer_set_prefix(writer, &lit, &lit)); - assert(serd_writer_end_anon(writer, NULL)); - assert(serd_writer_get_env(writer) == env); - - uint8_t buf[] = { 0x80, 0, 0, 0, 0 }; - SerdNode s = serd_node_from_string(SERD_URI, USTR("")); - SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/pred")); - SerdNode o = serd_node_from_string(SERD_LITERAL, buf); - - // Write 3 invalid statements (should write nothing) - const SerdNode* junk[][5] = { { &s, &p, &SERD_NODE_NULL, NULL, NULL }, - { &s, &SERD_NODE_NULL, &o, NULL, NULL }, - { &SERD_NODE_NULL, &p, &o, NULL, NULL }, - { &s, &o, &o, NULL, NULL }, - { &o, &p, &o, NULL, NULL }, - { &s, &p, &SERD_NODE_NULL, NULL, NULL } }; - for (size_t i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 5); ++i) { - assert(serd_writer_write_statement( - writer, 0, NULL, - junk[i][0], junk[i][1], junk[i][2], junk[i][3], junk[i][4])); - } - - const SerdNode t = serd_node_from_string(SERD_URI, USTR("urn:Type")); - const SerdNode l = serd_node_from_string(SERD_LITERAL, USTR("en")); - const SerdNode* good[][5] = { { &s, &p, &o, NULL, NULL }, - { &s, &p, &o, &SERD_NODE_NULL, &SERD_NODE_NULL }, - { &s, &p, &o, &t, NULL }, - { &s, &p, &o, NULL, &l }, - { &s, &p, &o, &t, &l }, - { &s, &p, &o, &t, &SERD_NODE_NULL }, - { &s, &p, &o, &SERD_NODE_NULL, &l }, - { &s, &p, &o, NULL, &SERD_NODE_NULL }, - { &s, &p, &o, &SERD_NODE_NULL, NULL }, - { &s, &p, &o, &SERD_NODE_NULL, NULL } }; - for (size_t i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 5); ++i) { - assert(!serd_writer_write_statement( - writer, 0, NULL, - good[i][0], good[i][1], good[i][2], good[i][3], good[i][4])); - } - - // Write statements with bad UTF-8 (should be replaced) - const uint8_t bad_str[] = { 0xFF, 0x90, 'h', 'i', 0 }; - SerdNode bad_lit = serd_node_from_string(SERD_LITERAL, bad_str); - SerdNode bad_uri = serd_node_from_string(SERD_URI, bad_str); - assert(!serd_writer_write_statement(writer, 0, NULL, - &s, &p, &bad_lit, NULL, NULL)); - assert(!serd_writer_write_statement(writer, 0, NULL, - &s, &p, &bad_uri, NULL, NULL)); - - // Write 1 valid statement - o = serd_node_from_string(SERD_LITERAL, USTR("hello")); - assert(!serd_writer_write_statement(writer, 0, NULL, - &s, &p, &o, NULL, NULL)); - - serd_writer_free(writer); - - // Test chunk sink - SerdChunk chunk = { NULL, 0 }; - writer = serd_writer_new( - SERD_TURTLE, (SerdStyle)0, env, NULL, serd_chunk_sink, &chunk); - - o = serd_node_from_string(SERD_URI, USTR("http://example.org/base")); - assert(!serd_writer_set_base_uri(writer, &o)); - - serd_writer_free(writer); - uint8_t* out = serd_chunk_sink_finish(&chunk); - - assert(!strcmp((const char*)out, "@base <http://example.org/base> .\n")); - serd_free(out); - - // Test writing empty node - SerdNode nothing = serd_node_from_string(SERD_NOTHING, USTR("")); - FILE* const empty = tmpfile(); - - writer = serd_writer_new( - SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, empty); - - // FIXME: error handling - serd_writer_write_statement(writer, 0, NULL, - &s, &p, ¬hing, NULL, NULL); - - assert((size_t)ftell(empty) == strlen("<>\n\t<http://example.org/pred> ")); - - serd_writer_free(writer); - fclose(empty); - - serd_env_free(env); - fclose(fd); + FILE* fd = fopen(path, "wb"); + SerdEnv* env = serd_env_new(NULL); + assert(fd); + + SerdWriter* writer = + serd_writer_new(SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd); + assert(writer); + + serd_writer_chop_blank_prefix(writer, USTR("tmp")); + serd_writer_chop_blank_prefix(writer, NULL); + + const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello")); + + assert(serd_writer_set_base_uri(writer, &lit)); + assert(serd_writer_set_prefix(writer, &lit, &lit)); + assert(serd_writer_end_anon(writer, NULL)); + assert(serd_writer_get_env(writer) == env); + + uint8_t buf[] = {0x80, 0, 0, 0, 0}; + SerdNode s = serd_node_from_string(SERD_URI, USTR("")); + SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/pred")); + SerdNode o = serd_node_from_string(SERD_LITERAL, buf); + + // Write 3 invalid statements (should write nothing) + const SerdNode* junk[][5] = {{&s, &p, &SERD_NODE_NULL, NULL, NULL}, + {&s, &SERD_NODE_NULL, &o, NULL, NULL}, + {&SERD_NODE_NULL, &p, &o, NULL, NULL}, + {&s, &o, &o, NULL, NULL}, + {&o, &p, &o, NULL, NULL}, + {&s, &p, &SERD_NODE_NULL, NULL, NULL}}; + for (size_t i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 5); ++i) { + assert(serd_writer_write_statement(writer, + 0, + NULL, + junk[i][0], + junk[i][1], + junk[i][2], + junk[i][3], + junk[i][4])); + } + + const SerdNode t = serd_node_from_string(SERD_URI, USTR("urn:Type")); + const SerdNode l = serd_node_from_string(SERD_LITERAL, USTR("en")); + const SerdNode* good[][5] = {{&s, &p, &o, NULL, NULL}, + {&s, &p, &o, &SERD_NODE_NULL, &SERD_NODE_NULL}, + {&s, &p, &o, &t, NULL}, + {&s, &p, &o, NULL, &l}, + {&s, &p, &o, &t, &l}, + {&s, &p, &o, &t, &SERD_NODE_NULL}, + {&s, &p, &o, &SERD_NODE_NULL, &l}, + {&s, &p, &o, NULL, &SERD_NODE_NULL}, + {&s, &p, &o, &SERD_NODE_NULL, NULL}, + {&s, &p, &o, &SERD_NODE_NULL, NULL}}; + for (size_t i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 5); ++i) { + assert(!serd_writer_write_statement(writer, + 0, + NULL, + good[i][0], + good[i][1], + good[i][2], + good[i][3], + good[i][4])); + } + + // Write statements with bad UTF-8 (should be replaced) + const uint8_t bad_str[] = {0xFF, 0x90, 'h', 'i', 0}; + SerdNode bad_lit = serd_node_from_string(SERD_LITERAL, bad_str); + SerdNode bad_uri = serd_node_from_string(SERD_URI, bad_str); + assert(!serd_writer_write_statement( + writer, 0, NULL, &s, &p, &bad_lit, NULL, NULL)); + assert(!serd_writer_write_statement( + writer, 0, NULL, &s, &p, &bad_uri, NULL, NULL)); + + // Write 1 valid statement + o = serd_node_from_string(SERD_LITERAL, USTR("hello")); + assert(!serd_writer_write_statement(writer, 0, NULL, &s, &p, &o, NULL, NULL)); + + serd_writer_free(writer); + + // Test chunk sink + SerdChunk chunk = {NULL, 0}; + writer = serd_writer_new( + SERD_TURTLE, (SerdStyle)0, env, NULL, serd_chunk_sink, &chunk); + + o = serd_node_from_string(SERD_URI, USTR("http://example.org/base")); + assert(!serd_writer_set_base_uri(writer, &o)); + + serd_writer_free(writer); + uint8_t* out = serd_chunk_sink_finish(&chunk); + + assert(!strcmp((const char*)out, "@base <http://example.org/base> .\n")); + serd_free(out); + + // Test writing empty node + SerdNode nothing = serd_node_from_string(SERD_NOTHING, USTR("")); + FILE* const empty = tmpfile(); + + writer = serd_writer_new( + SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, empty); + + // FIXME: error handling + serd_writer_write_statement(writer, 0, NULL, &s, &p, ¬hing, NULL, NULL); + + assert((size_t)ftell(empty) == strlen("<>\n\t<http://example.org/pred> ")); + + serd_writer_free(writer); + fclose(empty); + + serd_env_free(env); + fclose(fd); } static void test_reader(const char* path) { - ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); - SerdReader* reader = serd_reader_new( - SERD_TURTLE, rt, free, - NULL, NULL, test_sink, NULL); - assert(reader); - assert(serd_reader_get_handle(reader) == rt); + ReaderTest* rt = (ReaderTest*)calloc(1, sizeof(ReaderTest)); + SerdReader* reader = + serd_reader_new(SERD_TURTLE, rt, free, NULL, NULL, test_sink, NULL); + assert(reader); + assert(serd_reader_get_handle(reader) == rt); - SerdNode g = serd_node_from_string(SERD_URI, USTR("http://example.org/")); - serd_reader_set_default_graph(reader, &g); - serd_reader_add_blank_prefix(reader, USTR("tmp")); + SerdNode g = serd_node_from_string(SERD_URI, USTR("http://example.org/")); + serd_reader_set_default_graph(reader, &g); + serd_reader_add_blank_prefix(reader, USTR("tmp")); #if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wnonnull" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wnonnull" #endif - serd_reader_add_blank_prefix(reader, NULL); + serd_reader_add_blank_prefix(reader, NULL); #if defined(__GNUC__) -# pragma GCC diagnostic pop +# pragma GCC diagnostic pop #endif - assert(serd_reader_read_file(reader, USTR("http://notafile"))); - assert(serd_reader_read_file(reader, USTR("file:///better/not/exist"))); - assert(serd_reader_read_file(reader, USTR("file://"))); - - const SerdStatus st = serd_reader_read_file(reader, USTR(path)); - assert(!st); - assert(rt->n_statements == 13); - assert(rt->graph && rt->graph->buf && - !strcmp((const char*)rt->graph->buf, "http://example.org/")); - - assert(serd_reader_read_string(reader, USTR("This isn't Turtle at all."))); - - // A read of a big page hits EOF then fails to read chunks immediately - { - FILE* temp = tmpfile(); - assert(temp); - fprintf(temp, "_:s <http://example.org/p> _:o .\n"); - fflush(temp); - fseek(temp, 0L, SEEK_SET); - - serd_reader_start_stream(reader, temp, NULL, true); - - assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); - assert(serd_reader_read_chunk(reader) == SERD_FAILURE); - assert(serd_reader_read_chunk(reader) == SERD_FAILURE); - - serd_reader_end_stream(reader); - fclose(temp); - } - - // A byte-wise reader that hits EOF once then continues (like a socket) - { - size_t n_reads = 0; - serd_reader_start_source_stream(reader, - (SerdSource)eof_test_read, - (SerdStreamErrorFunc)eof_test_error, - &n_reads, - NULL, - 1); - - assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); - assert(serd_reader_read_chunk(reader) == SERD_FAILURE); - assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); - assert(serd_reader_read_chunk(reader) == SERD_FAILURE); - } - - serd_reader_free(reader); + assert(serd_reader_read_file(reader, USTR("http://notafile"))); + assert(serd_reader_read_file(reader, USTR("file:///better/not/exist"))); + assert(serd_reader_read_file(reader, USTR("file://"))); + + const SerdStatus st = serd_reader_read_file(reader, USTR(path)); + assert(!st); + assert(rt->n_statements == 13); + assert(rt->graph && rt->graph->buf && + !strcmp((const char*)rt->graph->buf, "http://example.org/")); + + assert(serd_reader_read_string(reader, USTR("This isn't Turtle at all."))); + + // A read of a big page hits EOF then fails to read chunks immediately + { + FILE* temp = tmpfile(); + assert(temp); + fprintf(temp, "_:s <http://example.org/p> _:o .\n"); + fflush(temp); + fseek(temp, 0L, SEEK_SET); + + serd_reader_start_stream(reader, temp, NULL, true); + + assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + + serd_reader_end_stream(reader); + fclose(temp); + } + + // A byte-wise reader that hits EOF once then continues (like a socket) + { + size_t n_reads = 0; + serd_reader_start_source_stream(reader, + (SerdSource)eof_test_read, + (SerdStreamErrorFunc)eof_test_error, + &n_reads, + NULL, + 1); + + assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + assert(serd_reader_read_chunk(reader) == SERD_SUCCESS); + assert(serd_reader_read_chunk(reader) == SERD_FAILURE); + } + + serd_reader_free(reader); } int main(void) { - test_read_chunks(); - test_read_string(); + test_read_chunks(); + test_read_string(); - const char* const path = "serd_test.ttl"; - test_writer(path); - test_reader(path); + const char* const path = "serd_test.ttl"; + test_writer(path); + test_reader(path); - printf("Success\n"); - return 0; + printf("Success\n"); + return 0; } diff --git a/test/test_string.c b/test/test_string.c index 1e11f5b4..225eca4f 100644 --- a/test/test_string.c +++ b/test/test_string.c @@ -26,39 +26,39 @@ static void test_strlen(void) { - const uint8_t str[] = { '"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0 }; + const uint8_t str[] = {'"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0}; - size_t n_bytes = 0; - SerdNodeFlags flags = 0; - size_t len = serd_strlen(str, &n_bytes, &flags); - assert(len == 5 && n_bytes == 7 && - flags == (SERD_HAS_QUOTE | SERD_HAS_NEWLINE)); - len = serd_strlen(str, NULL, &flags); - assert(len == 5); + size_t n_bytes = 0; + SerdNodeFlags flags = 0; + size_t len = serd_strlen(str, &n_bytes, &flags); + assert(len == 5 && n_bytes == 7 && + flags == (SERD_HAS_QUOTE | SERD_HAS_NEWLINE)); + len = serd_strlen(str, NULL, &flags); + assert(len == 5); - assert(serd_strlen(str, &n_bytes, NULL) == 5); + assert(serd_strlen(str, &n_bytes, NULL) == 5); } static void test_strerror(void) { - const uint8_t* msg = serd_strerror(SERD_SUCCESS); - assert(!strcmp((const char*)msg, "Success")); - for (int i = SERD_FAILURE; i <= SERD_ERR_INTERNAL; ++i) { - msg = serd_strerror((SerdStatus)i); - assert(strcmp((const char*)msg, "Success")); - } + const uint8_t* msg = serd_strerror(SERD_SUCCESS); + assert(!strcmp((const char*)msg, "Success")); + for (int i = SERD_FAILURE; i <= SERD_ERR_INTERNAL; ++i) { + msg = serd_strerror((SerdStatus)i); + assert(strcmp((const char*)msg, "Success")); + } - msg = serd_strerror((SerdStatus)-1); - assert(!strcmp((const char*)msg, "Unknown error")); + msg = serd_strerror((SerdStatus)-1); + assert(!strcmp((const char*)msg, "Unknown error")); } int main(void) { - test_strlen(); - test_strerror(); + test_strlen(); + test_strerror(); - printf("Success\n"); - return 0; + printf("Success\n"); + return 0; } diff --git a/test/test_uri.c b/test/test_uri.c index 9538ffb0..f910e1f0 100644 --- a/test/test_uri.c +++ b/test/test_uri.c @@ -33,151 +33,153 @@ test_file_uri(const char* hostname, const char* expected_uri, const char* expected_path) { - if (!expected_path) { - expected_path = path; - } - - SerdNode node = serd_node_new_file_uri( - USTR(path), USTR(hostname), 0, escape); - - uint8_t* out_hostname = NULL; - uint8_t* out_path = serd_file_uri_parse(node.buf, &out_hostname); - assert(!strcmp((const char*)node.buf, expected_uri)); - assert((hostname && out_hostname) || (!hostname && !out_hostname)); - assert(!strcmp((const char*)out_path, (const char*)expected_path)); - - serd_free(out_path); - serd_free(out_hostname); - serd_node_free(&node); + if (!expected_path) { + expected_path = path; + } + + SerdNode node = serd_node_new_file_uri(USTR(path), USTR(hostname), 0, escape); + + uint8_t* out_hostname = NULL; + uint8_t* out_path = serd_file_uri_parse(node.buf, &out_hostname); + assert(!strcmp((const char*)node.buf, expected_uri)); + assert((hostname && out_hostname) || (!hostname && !out_hostname)); + assert(!strcmp((const char*)out_path, (const char*)expected_path)); + + serd_free(out_path); + serd_free(out_hostname); + serd_node_free(&node); } #if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wdeprecated-declarations" +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif static void test_uri_to_path(void) { - const uint8_t* uri = (const uint8_t*)"file:///home/user/foo.ttl"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")); + const uint8_t* uri = (const uint8_t*)"file:///home/user/foo.ttl"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")); - uri = (const uint8_t*)"file://localhost/home/user/foo.ttl"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")); + uri = (const uint8_t*)"file://localhost/home/user/foo.ttl"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")); - uri = (const uint8_t*)"file:illegal/file/uri"; - assert(!serd_uri_to_path(uri)); + uri = (const uint8_t*)"file:illegal/file/uri"; + assert(!serd_uri_to_path(uri)); - uri = (const uint8_t*)"file:///c:/awful/system"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "c:/awful/system")); + uri = (const uint8_t*)"file:///c:/awful/system"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "c:/awful/system")); - uri = (const uint8_t*)"file:///c:awful/system"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "/c:awful/system")); + uri = (const uint8_t*)"file:///c:awful/system"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "/c:awful/system")); - uri = (const uint8_t*)"file:///0/1"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "/0/1")); + uri = (const uint8_t*)"file:///0/1"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "/0/1")); - uri = (const uint8_t*)"C:\\Windows\\Sucks"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "C:\\Windows\\Sucks")); + uri = (const uint8_t*)"C:\\Windows\\Sucks"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "C:\\Windows\\Sucks")); - uri = (const uint8_t*)"C|/Windows/Sucks"; - assert(!strcmp((const char*)serd_uri_to_path(uri), "C|/Windows/Sucks")); + uri = (const uint8_t*)"C|/Windows/Sucks"; + assert(!strcmp((const char*)serd_uri_to_path(uri), "C|/Windows/Sucks")); - uri = (const uint8_t*)"http://example.org/path"; - assert(!serd_uri_to_path(uri)); + uri = (const uint8_t*)"http://example.org/path"; + assert(!serd_uri_to_path(uri)); } #if defined(__GNUC__) -# pragma GCC diagnostic pop +# pragma GCC diagnostic pop #endif static void test_uri_parsing(void) { - test_file_uri(NULL, "C:/My 100%", true, - "file:///C:/My%20100%%", NULL); - test_file_uri("ahost", "C:\\Pointless Space", true, - "file://ahost/C:/Pointless%20Space", "C:/Pointless Space"); - test_file_uri(NULL, "/foo/bar", true, - "file:///foo/bar", NULL); - test_file_uri("bhost", "/foo/bar", true, - "file://bhost/foo/bar", NULL); - test_file_uri(NULL, "a/relative path", false, - "a/relative path", NULL); - test_file_uri(NULL, "a/relative <path>", true, - "a/relative%20%3Cpath%3E", NULL); - - // Test tolerance of parsing junk URI escapes - - uint8_t* out_path = serd_file_uri_parse(USTR("file:///foo/%0Xbar"), NULL); - assert(!strcmp((const char*)out_path, "/foo/bar")); - serd_free(out_path); + test_file_uri(NULL, "C:/My 100%", true, "file:///C:/My%20100%%", NULL); + test_file_uri("ahost", + "C:\\Pointless Space", + true, + "file://ahost/C:/Pointless%20Space", + "C:/Pointless Space"); + test_file_uri(NULL, "/foo/bar", true, "file:///foo/bar", NULL); + test_file_uri("bhost", "/foo/bar", true, "file://bhost/foo/bar", NULL); + test_file_uri(NULL, "a/relative path", false, "a/relative path", NULL); + test_file_uri( + NULL, "a/relative <path>", true, "a/relative%20%3Cpath%3E", NULL); + + // Test tolerance of parsing junk URI escapes + + uint8_t* out_path = serd_file_uri_parse(USTR("file:///foo/%0Xbar"), NULL); + assert(!strcmp((const char*)out_path, "/foo/bar")); + serd_free(out_path); } static void test_uri_from_string(void) { - SerdNode nonsense = serd_node_new_uri_from_string(NULL, NULL, NULL); - assert(nonsense.type == SERD_NOTHING); - - SerdURI base_uri; - SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"), - NULL, &base_uri); - SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL); - SerdNode nil2 = serd_node_new_uri_from_string(USTR(""), &base_uri, NULL); - assert(nil.type == SERD_URI); - assert(!strcmp((const char*)nil.buf, (const char*)base.buf)); - assert(nil2.type == SERD_URI); - assert(!strcmp((const char*)nil2.buf, (const char*)base.buf)); - serd_node_free(&nil); - serd_node_free(&nil2); - - serd_node_free(&base); + SerdNode nonsense = serd_node_new_uri_from_string(NULL, NULL, NULL); + assert(nonsense.type == SERD_NOTHING); + + SerdURI base_uri; + SerdNode base = + serd_node_new_uri_from_string(USTR("http://example.org/"), NULL, &base_uri); + SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL); + SerdNode nil2 = serd_node_new_uri_from_string(USTR(""), &base_uri, NULL); + assert(nil.type == SERD_URI); + assert(!strcmp((const char*)nil.buf, (const char*)base.buf)); + assert(nil2.type == SERD_URI); + assert(!strcmp((const char*)nil2.buf, (const char*)base.buf)); + serd_node_free(&nil); + serd_node_free(&nil2); + + serd_node_free(&base); } static void test_relative_uri(void) { - SerdURI base_uri; - SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"), - NULL, &base_uri); - - SerdNode abs = serd_node_from_string(SERD_URI, USTR("http://example.org/foo/bar")); - SerdURI abs_uri; - serd_uri_parse(abs.buf, &abs_uri); - - SerdURI rel_uri; - SerdNode rel = serd_node_new_relative_uri(&abs_uri, &base_uri, NULL, &rel_uri); - assert(!strcmp((const char*)rel.buf, "/foo/bar")); - - SerdNode up = serd_node_new_relative_uri(&base_uri, &abs_uri, NULL, NULL); - assert(!strcmp((const char*)up.buf, "../")); - - SerdNode noup = serd_node_new_relative_uri(&base_uri, &abs_uri, &abs_uri, NULL); - assert(!strcmp((const char*)noup.buf, "http://example.org/")); - - SerdNode x = serd_node_from_string(SERD_URI, USTR("http://example.org/foo/x")); - SerdURI x_uri; - serd_uri_parse(x.buf, &x_uri); - - SerdNode x_rel = serd_node_new_relative_uri(&x_uri, &abs_uri, &abs_uri, NULL); - assert(!strcmp((const char*)x_rel.buf, "x")); - - serd_node_free(&x_rel); - serd_node_free(&noup); - serd_node_free(&up); - serd_node_free(&rel); - serd_node_free(&base); + SerdURI base_uri; + SerdNode base = + serd_node_new_uri_from_string(USTR("http://example.org/"), NULL, &base_uri); + + SerdNode abs = + serd_node_from_string(SERD_URI, USTR("http://example.org/foo/bar")); + SerdURI abs_uri; + serd_uri_parse(abs.buf, &abs_uri); + + SerdURI rel_uri; + SerdNode rel = + serd_node_new_relative_uri(&abs_uri, &base_uri, NULL, &rel_uri); + assert(!strcmp((const char*)rel.buf, "/foo/bar")); + + SerdNode up = serd_node_new_relative_uri(&base_uri, &abs_uri, NULL, NULL); + assert(!strcmp((const char*)up.buf, "../")); + + SerdNode noup = + serd_node_new_relative_uri(&base_uri, &abs_uri, &abs_uri, NULL); + assert(!strcmp((const char*)noup.buf, "http://example.org/")); + + SerdNode x = + serd_node_from_string(SERD_URI, USTR("http://example.org/foo/x")); + SerdURI x_uri; + serd_uri_parse(x.buf, &x_uri); + + SerdNode x_rel = serd_node_new_relative_uri(&x_uri, &abs_uri, &abs_uri, NULL); + assert(!strcmp((const char*)x_rel.buf, "x")); + + serd_node_free(&x_rel); + serd_node_free(&noup); + serd_node_free(&up); + serd_node_free(&rel); + serd_node_free(&base); } int main(void) { - test_uri_to_path(); - test_uri_parsing(); - test_uri_from_string(); - test_relative_uri(); + test_uri_to_path(); + test_uri_parsing(); + test_uri_from_string(); + test_relative_uri(); - printf("Success\n"); - return 0; + printf("Success\n"); + return 0; } |