annotate src/serd-0.18.2/tests/serd_test.c @ 148:b4bfdf10c4b3

Update Win64 capnp builds to v0.6
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 22 May 2017 18:56:49 +0100
parents 545efbb81310
children
rev   line source
cannam@85 1 /*
cannam@85 2 Copyright 2011-2012 David Robillard <http://drobilla.net>
cannam@85 3
cannam@85 4 Permission to use, copy, modify, and/or distribute this software for any
cannam@85 5 purpose with or without fee is hereby granted, provided that the above
cannam@85 6 copyright notice and this permission notice appear in all copies.
cannam@85 7
cannam@85 8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
cannam@85 9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
cannam@85 10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
cannam@85 11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
cannam@85 12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
cannam@85 13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
cannam@85 14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
cannam@85 15 */
cannam@85 16
cannam@85 17 #include <float.h>
cannam@85 18 #include <math.h>
cannam@85 19 #include <stdarg.h>
cannam@85 20 #include <stdio.h>
cannam@85 21 #include <stdlib.h>
cannam@85 22 #include <string.h>
cannam@85 23
cannam@85 24 #include "serd/serd.h"
cannam@85 25
cannam@85 26 #define USTR(s) ((const uint8_t*)(s))
cannam@85 27
cannam@85 28 #ifdef _WIN32
cannam@85 29 # define INFINITY (DBL_MAX + DBL_MAX)
cannam@85 30 # define NAN (INFINITY - INFINITY)
cannam@85 31 #endif
cannam@85 32
cannam@85 33 static int
cannam@85 34 failure(const char* fmt, ...)
cannam@85 35 {
cannam@85 36 va_list args;
cannam@85 37 va_start(args, fmt);
cannam@85 38 fprintf(stderr, "error: ");
cannam@85 39 vfprintf(stderr, fmt, args);
cannam@85 40 va_end(args);
cannam@85 41 return 1;
cannam@85 42 }
cannam@85 43
cannam@85 44 static bool
cannam@85 45 test_strtod(double dbl, double max_delta)
cannam@85 46 {
cannam@85 47 char buf[1024];
cannam@85 48 snprintf(buf, sizeof(buf), "%lf", dbl);
cannam@85 49
cannam@85 50 char* endptr = NULL;
cannam@85 51 const double out = serd_strtod(buf, &endptr);
cannam@85 52
cannam@85 53 const double diff = fabs(out - dbl);
cannam@85 54 if (diff > max_delta) {
cannam@85 55 return !failure("Parsed %lf != %lf (delta %lf)\n", dbl, out, diff);
cannam@85 56 }
cannam@85 57 return true;
cannam@85 58 }
cannam@85 59
cannam@85 60 static SerdStatus
cannam@85 61 count_prefixes(void* handle, const SerdNode* name, const SerdNode* uri)
cannam@85 62 {
cannam@85 63 ++*(int*)handle;
cannam@85 64 return SERD_SUCCESS;
cannam@85 65 }
cannam@85 66
cannam@85 67 typedef struct {
cannam@85 68 int n_statements;
cannam@85 69 const SerdNode* graph;
cannam@85 70 } ReaderTest;
cannam@85 71
cannam@85 72 static SerdStatus
cannam@85 73 test_sink(void* handle,
cannam@85 74 SerdStatementFlags flags,
cannam@85 75 const SerdNode* graph,
cannam@85 76 const SerdNode* subject,
cannam@85 77 const SerdNode* predicate,
cannam@85 78 const SerdNode* object,
cannam@85 79 const SerdNode* object_datatype,
cannam@85 80 const SerdNode* object_lang)
cannam@85 81 {
cannam@85 82 ReaderTest* rt = (ReaderTest*)handle;
cannam@85 83 ++rt->n_statements;
cannam@85 84 rt->graph = graph;
cannam@85 85 return SERD_SUCCESS;
cannam@85 86 }
cannam@85 87
cannam@85 88 int
cannam@85 89 main(void)
cannam@85 90 {
cannam@85 91 #define MAX 1000000
cannam@85 92 #define NUM_TESTS 1000
cannam@85 93 for (int i = 0; i < NUM_TESTS; ++i) {
cannam@85 94 double dbl = rand() % MAX;
cannam@85 95 dbl += (rand() % MAX) / (double)MAX;
cannam@85 96
cannam@85 97 if (!test_strtod(dbl, 1 / (double)MAX)) {
cannam@85 98 return 1;
cannam@85 99 }
cannam@85 100 }
cannam@85 101
cannam@85 102 const double expt_test_nums[] = {
cannam@85 103 2.0E18, -5e19, +8e20, 2e+34, -5e-5, 8e0, 9e-0, 2e+0
cannam@85 104 };
cannam@85 105
cannam@85 106 const char* expt_test_strs[] = {
cannam@85 107 "02e18", "-5e019", "+8e20", "2E+34", "-5E-5", "8E0", "9e-0", " 2e+0"
cannam@85 108 };
cannam@85 109
cannam@85 110 for (unsigned i = 0; i < sizeof(expt_test_nums) / sizeof(double); ++i) {
cannam@85 111 const double num = serd_strtod(expt_test_strs[i], NULL);
cannam@85 112 const double delta = fabs(num - expt_test_nums[i]);
cannam@85 113 if (delta > DBL_EPSILON) {
cannam@85 114 return failure("Parsed `%s' %lf != %lf (delta %lf)\n",
cannam@85 115 expt_test_strs[i], num, expt_test_nums[i], delta);
cannam@85 116 }
cannam@85 117 }
cannam@85 118
cannam@85 119 // Test serd_node_new_decimal
cannam@85 120
cannam@85 121 const double dbl_test_nums[] = {
cannam@85 122 0.0, 9.0, 10.0, .01, 2.05, -16.00001, 5.000000005, 0.0000000001, NAN, INFINITY
cannam@85 123 };
cannam@85 124
cannam@85 125 const char* dbl_test_strs[] = {
cannam@85 126 "0.0", "9.0", "10.0", "0.01", "2.05", "-16.00001", "5.00000001", "0.0", NULL, NULL
cannam@85 127 };
cannam@85 128
cannam@85 129 for (unsigned i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) {
cannam@85 130 SerdNode node = serd_node_new_decimal(dbl_test_nums[i], 8);
cannam@85 131 const bool pass = (node.buf && dbl_test_strs[i])
cannam@85 132 ? !strcmp((const char*)node.buf, (const char*)dbl_test_strs[i])
cannam@85 133 : ((const char*)node.buf == dbl_test_strs[i]);
cannam@85 134 if (!pass) {
cannam@85 135 return failure("Serialised `%s' != %s\n",
cannam@85 136 node.buf, dbl_test_strs[i]);
cannam@85 137 }
cannam@85 138 const size_t len = node.buf ? strlen((const char*)node.buf) : 0;
cannam@85 139 if (node.n_bytes != len || node.n_chars != len) {
cannam@85 140 return failure("Length %zu,%zu != %zu\n",
cannam@85 141 node.n_bytes, node.n_chars, len);
cannam@85 142 }
cannam@85 143 serd_node_free(&node);
cannam@85 144 }
cannam@85 145
cannam@85 146 // Test serd_node_new_integer
cannam@85 147
cannam@85 148 const long int_test_nums[] = {
cannam@85 149 0, -0, -23, 23, -12340, 1000, -1000
cannam@85 150 };
cannam@85 151
cannam@85 152 const char* int_test_strs[] = {
cannam@85 153 "0", "0", "-23", "23", "-12340", "1000", "-1000"
cannam@85 154 };
cannam@85 155
cannam@85 156 for (unsigned i = 0; i < sizeof(int_test_nums) / sizeof(double); ++i) {
cannam@85 157 SerdNode node = serd_node_new_integer(int_test_nums[i]);
cannam@85 158 if (strcmp((const char*)node.buf, (const char*)int_test_strs[i])) {
cannam@85 159 return failure("Serialised `%s' != %s\n",
cannam@85 160 node.buf, int_test_strs[i]);
cannam@85 161 }
cannam@85 162 const size_t len = strlen((const char*)node.buf);
cannam@85 163 if (node.n_bytes != len || node.n_chars != len) {
cannam@85 164 return failure("Length %zu,%zu != %zu\n",
cannam@85 165 node.n_bytes, node.n_chars, len);
cannam@85 166 }
cannam@85 167 serd_node_free(&node);
cannam@85 168 }
cannam@85 169
cannam@85 170 // Test serd_node_new_blob
cannam@85 171 for (size_t size = 0; size < 256; ++size) {
cannam@85 172 uint8_t* data = (uint8_t*)malloc(size);
cannam@85 173 for (size_t i = 0; i < size; ++i) {
cannam@85 174 data[i] = (uint8_t)(rand() % 256);
cannam@85 175 }
cannam@85 176
cannam@85 177 SerdNode blob = serd_node_new_blob(data, size, size % 5);
cannam@85 178
cannam@85 179 if (blob.n_bytes != blob.n_chars) {
cannam@85 180 return failure("Blob %zu bytes != %zu chars\n",
cannam@85 181 blob.n_bytes, blob.n_chars);
cannam@85 182 }
cannam@85 183
cannam@85 184 size_t out_size;
cannam@85 185 uint8_t* out = (uint8_t*)serd_base64_decode(
cannam@85 186 blob.buf, blob.n_bytes, &out_size);
cannam@85 187 if (out_size != size) {
cannam@85 188 return failure("Blob size %zu != %zu\n", out_size, size);
cannam@85 189 }
cannam@85 190
cannam@85 191 for (size_t i = 0; i < size; ++i) {
cannam@85 192 if (out[i] != data[i]) {
cannam@85 193 return failure("Corrupt blob at byte %zu\n", i);
cannam@85 194 }
cannam@85 195 }
cannam@85 196
cannam@85 197 serd_node_free(&blob);
cannam@85 198 free(out);
cannam@85 199 free(data);
cannam@85 200 }
cannam@85 201
cannam@85 202 // Test serd_strlen
cannam@85 203
cannam@85 204 const uint8_t str[] = { '"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0 };
cannam@85 205
cannam@85 206 size_t n_bytes;
cannam@85 207 SerdNodeFlags flags;
cannam@85 208 size_t len = serd_strlen(str, &n_bytes, &flags);
cannam@85 209 if (len != 5 || n_bytes != 7
cannam@85 210 || flags != (SERD_HAS_QUOTE|SERD_HAS_NEWLINE)) {
cannam@85 211 return failure("Bad serd_strlen(%s) len=%zu n_bytes=%zu flags=%u\n",
cannam@85 212 str, len, n_bytes, flags);
cannam@85 213 }
cannam@85 214 len = serd_strlen(str, NULL, &flags);
cannam@85 215 if (len != 5) {
cannam@85 216 return failure("Bad serd_strlen(%s) len=%zu flags=%u\n",
cannam@85 217 str, len, flags);
cannam@85 218 }
cannam@85 219
cannam@85 220 // Test serd_strerror
cannam@85 221
cannam@85 222 const uint8_t* msg = NULL;
cannam@85 223 if (strcmp((const char*)(msg = serd_strerror(SERD_SUCCESS)), "Success")) {
cannam@85 224 return failure("Bad message `%s' for SERD_SUCCESS\n", msg);
cannam@85 225 }
cannam@85 226 for (int i = SERD_FAILURE; i <= SERD_ERR_INTERNAL; ++i) {
cannam@85 227 msg = serd_strerror((SerdStatus)i);
cannam@85 228 if (!strcmp((const char*)msg, "Success")) {
cannam@85 229 return failure("Bad message `%s' for (SerdStatus)%d\n", msg, i);
cannam@85 230 }
cannam@85 231 }
cannam@85 232 msg = serd_strerror((SerdStatus)-1);
cannam@85 233
cannam@85 234 // Test serd_uri_to_path
cannam@85 235
cannam@85 236 const uint8_t* uri = (const uint8_t*)"file:///home/user/foo.ttl";
cannam@85 237 if (strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")) {
cannam@85 238 return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
cannam@85 239 }
cannam@85 240 uri = (const uint8_t*)"file://localhost/home/user/foo.ttl";
cannam@85 241 if (strcmp((const char*)serd_uri_to_path(uri), "/home/user/foo.ttl")) {
cannam@85 242 return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
cannam@85 243 }
cannam@85 244 uri = (const uint8_t*)"file:illegal/file/uri";
cannam@85 245 if (serd_uri_to_path(uri)) {
cannam@85 246 return failure("Converted invalid URI `%s' to path `%s'\n",
cannam@85 247 uri, serd_uri_to_path(uri));
cannam@85 248 }
cannam@85 249 uri = (const uint8_t*)"file:///c:/awful/system";
cannam@85 250 if (strcmp((const char*)serd_uri_to_path(uri), "c:/awful/system")) {
cannam@85 251 return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
cannam@85 252 }
cannam@85 253 uri = (const uint8_t*)"file:///c:awful/system";
cannam@85 254 if (strcmp((const char*)serd_uri_to_path(uri), "/c:awful/system")) {
cannam@85 255 return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
cannam@85 256 }
cannam@85 257 uri = (const uint8_t*)"file:///0/1";
cannam@85 258 if (strcmp((const char*)serd_uri_to_path(uri), "/0/1")) {
cannam@85 259 return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
cannam@85 260 }
cannam@85 261 uri = (const uint8_t*)"C:\\Windows\\Sucks";
cannam@85 262 if (strcmp((const char*)serd_uri_to_path(uri), "C:\\Windows\\Sucks")) {
cannam@85 263 return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
cannam@85 264 }
cannam@85 265 uri = (const uint8_t*)"C|/Windows/Sucks";
cannam@85 266 if (strcmp((const char*)serd_uri_to_path(uri), "C|/Windows/Sucks")) {
cannam@85 267 return failure("Bad path %s for %s\n", serd_uri_to_path(uri), uri);
cannam@85 268 }
cannam@85 269
cannam@85 270 // Test serd_node_new_file_uri and serd_file_uri_parse
cannam@85 271 SerdURI furi;
cannam@85 272 const uint8_t* path_str = USTR("C:/My 100%");
cannam@85 273 SerdNode file_node = serd_node_new_file_uri(path_str, 0, &furi, true);
cannam@85 274 uint8_t* hostname = NULL;
cannam@85 275 uint8_t* out_path = serd_file_uri_parse(file_node.buf, &hostname);
cannam@85 276 if (strcmp((const char*)file_node.buf, "file:///C:/My%20100%%")) {
cannam@85 277 return failure("Bad URI %s\n", file_node.buf);
cannam@85 278 } else if (hostname) {
cannam@85 279 return failure("hostname `%s' shouldn't exist\n", hostname);
cannam@85 280 } else if (strcmp((const char*)path_str, (const char*)out_path)) {
cannam@85 281 return failure("path=>URI=>path failure %s => %s => %s\n",
cannam@85 282 path_str, file_node.buf, out_path);
cannam@85 283 }
cannam@85 284 free(out_path);
cannam@85 285 serd_node_free(&file_node);
cannam@85 286
cannam@85 287 path_str = USTR("C:\\Pointless Space");
cannam@85 288 file_node = serd_node_new_file_uri(path_str, USTR("pwned"), 0, true);
cannam@85 289 hostname = NULL;
cannam@85 290 out_path = serd_file_uri_parse(file_node.buf, &hostname);
cannam@85 291 if (strcmp((const char*)file_node.buf, "file://pwned/C:/Pointless%20Space")) {
cannam@85 292 return failure("Bad URI %s\n", file_node.buf);
cannam@85 293 } else if (!hostname || strcmp((const char*)hostname, "pwned")) {
cannam@85 294 return failure("Bad hostname `%s'\n", hostname);
cannam@85 295 } else if (strcmp((const char*)out_path, "C:/Pointless Space")) {
cannam@85 296 return failure("path=>URI=>path failure %s => %s => %s\n",
cannam@85 297 path_str, file_node.buf, out_path);
cannam@85 298 }
cannam@85 299 free(hostname);
cannam@85 300 free(out_path);
cannam@85 301 serd_node_free(&file_node);
cannam@85 302
cannam@85 303 path_str = USTR("/foo/bar");
cannam@85 304 file_node = serd_node_new_file_uri(path_str, 0, 0, true);
cannam@85 305 hostname = NULL;
cannam@85 306 out_path = serd_file_uri_parse(file_node.buf, &hostname);
cannam@85 307 if (strcmp((const char*)file_node.buf, "file:///foo/bar")) {
cannam@85 308 return failure("Bad URI %s\n", file_node.buf);
cannam@85 309 } else if (hostname) {
cannam@85 310 return failure("hostname `%s' shouldn't exist\n", hostname);
cannam@85 311 } else if (strcmp((const char*)path_str, (const char*)out_path)) {
cannam@85 312 return failure("path=>URI=>path failure %s => %s => %s\n",
cannam@85 313 path_str, file_node.buf, out_path);
cannam@85 314 }
cannam@85 315 free(out_path);
cannam@85 316 serd_node_free(&file_node);
cannam@85 317
cannam@85 318 path_str = USTR("/foo/bar");
cannam@85 319 file_node = serd_node_new_file_uri(path_str, USTR("localhost"), 0, true);
cannam@85 320 out_path = serd_file_uri_parse(file_node.buf, &hostname);
cannam@85 321 if (strcmp((const char*)file_node.buf, "file://localhost/foo/bar")) {
cannam@85 322 return failure("Bad URI %s\n", file_node.buf);
cannam@85 323 } else if (strcmp((const char*)hostname, "localhost")) {
cannam@85 324 return failure("incorrect hostname `%s'\n", hostname);
cannam@85 325 } else if (strcmp((const char*)path_str, (const char*)out_path)) {
cannam@85 326 return failure("path=>URI=>path failure %s => %s => %s\n",
cannam@85 327 path_str, file_node.buf, out_path);
cannam@85 328 }
cannam@85 329 free(hostname);
cannam@85 330 free(out_path);
cannam@85 331 serd_node_free(&file_node);
cannam@85 332
cannam@85 333 path_str = USTR("a/relative path");
cannam@85 334 file_node = serd_node_new_file_uri(path_str, 0, 0, false);
cannam@85 335 out_path = serd_file_uri_parse(file_node.buf, &hostname);
cannam@85 336 if (strcmp((const char*)file_node.buf, "a/relative path")) {
cannam@85 337 return failure("Bad URI %s\n", file_node.buf);
cannam@85 338 } else if (hostname) {
cannam@85 339 return failure("hostname `%s' shouldn't exist\n", hostname);
cannam@85 340 } else if (strcmp((const char*)path_str, (const char*)out_path)) {
cannam@85 341 return failure("path=>URI=>path failure %s => %s => %s\n",
cannam@85 342 path_str, file_node.buf, out_path);
cannam@85 343 }
cannam@85 344 free(hostname);
cannam@85 345 free(out_path);
cannam@85 346 serd_node_free(&file_node);
cannam@85 347
cannam@85 348 if (serd_file_uri_parse(USTR("file://invalid"), NULL)) {
cannam@85 349 return failure("successfully parsed bogus URI <file://invalid>\n");
cannam@85 350 }
cannam@85 351
cannam@85 352 out_path = serd_file_uri_parse(USTR("file://host/foo/%XYbar"), NULL);
cannam@85 353 if (strcmp((const char*)out_path, "/foo/bar")) {
cannam@85 354 return failure("bad tolerance of junk escape: `%s'\n", out_path);
cannam@85 355 }
cannam@85 356 free(out_path);
cannam@85 357 out_path = serd_file_uri_parse(USTR("file://host/foo/%0Abar"), NULL);
cannam@85 358 if (strcmp((const char*)out_path, "/foo/bar")) {
cannam@85 359 return failure("bad tolerance of junk escape: `%s'\n", out_path);
cannam@85 360 }
cannam@85 361 free(out_path);
cannam@85 362
cannam@85 363 // Test serd_node_equals
cannam@85 364
cannam@85 365 const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 };
cannam@85 366 SerdNode lhs = serd_node_from_string(SERD_LITERAL, replacement_char_str);
cannam@85 367 SerdNode rhs = serd_node_from_string(SERD_LITERAL, USTR("123"));
cannam@85 368 if (serd_node_equals(&lhs, &rhs)) {
cannam@85 369 return failure("%s == %s\n", lhs.buf, rhs.buf);
cannam@85 370 }
cannam@85 371
cannam@85 372 SerdNode qnode = serd_node_from_string(SERD_CURIE, USTR("foo:bar"));
cannam@85 373 if (serd_node_equals(&lhs, &qnode)) {
cannam@85 374 return failure("%s == %s\n", lhs.buf, qnode.buf);
cannam@85 375 }
cannam@85 376
cannam@85 377 if (!serd_node_equals(&lhs, &lhs)) {
cannam@85 378 return failure("%s != %s\n", lhs.buf, lhs.buf);
cannam@85 379 }
cannam@85 380
cannam@85 381 // Test serd_node_from_string
cannam@85 382
cannam@85 383 SerdNode node = serd_node_from_string(SERD_LITERAL, (const uint8_t*)"hello\"");
cannam@85 384 if (node.n_bytes != 6 || node.n_chars != 6 || node.flags != SERD_HAS_QUOTE
cannam@85 385 || strcmp((const char*)node.buf, "hello\"")) {
cannam@85 386 return failure("Bad node %s %zu %zu %d %d\n",
cannam@85 387 node.buf, node.n_bytes, node.n_chars, node.flags, node.type);
cannam@85 388 }
cannam@85 389
cannam@85 390 node = serd_node_from_string(SERD_URI, NULL);
cannam@85 391 if (!serd_node_equals(&node, &SERD_NODE_NULL)) {
cannam@85 392 return failure("Creating node from NULL string failed\n");
cannam@85 393 }
cannam@85 394
cannam@85 395 // Test serd_node_new_uri_from_string
cannam@85 396
cannam@85 397 SerdURI base_uri;
cannam@85 398 SerdNode base = serd_node_new_uri_from_string(USTR("http://example.org/"),
cannam@85 399 NULL, &base_uri);
cannam@85 400 SerdNode nil = serd_node_new_uri_from_string(NULL, &base_uri, NULL);
cannam@85 401 if (nil.type != SERD_URI || strcmp((const char*)nil.buf, (const char*)base.buf)) {
cannam@85 402 return failure("URI %s != base %s\n", nil.buf, base.buf);
cannam@85 403 }
cannam@85 404 serd_node_free(&base);
cannam@85 405 serd_node_free(&nil);
cannam@85 406
cannam@85 407 // Test SerdEnv
cannam@85 408
cannam@85 409 SerdNode u = serd_node_from_string(SERD_URI, USTR("http://example.org/foo"));
cannam@85 410 SerdNode b = serd_node_from_string(SERD_CURIE, USTR("invalid"));
cannam@85 411 SerdNode c = serd_node_from_string(SERD_CURIE, USTR("eg.2:b"));
cannam@85 412 SerdEnv* env = serd_env_new(NULL);
cannam@85 413 serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/"));
cannam@85 414
cannam@85 415 if (!serd_env_set_base_uri(env, &node)) {
cannam@85 416 return failure("Set base URI to %s\n", node.buf);
cannam@85 417 }
cannam@85 418
cannam@85 419 SerdChunk prefix, suffix;
cannam@85 420 if (!serd_env_expand(env, &b, &prefix, &suffix)) {
cannam@85 421 return failure("Expanded invalid curie %s\n", b.buf);
cannam@85 422 }
cannam@85 423
cannam@85 424 SerdNode xnode = serd_env_expand_node(env, &node);
cannam@85 425 if (!serd_node_equals(&xnode, &SERD_NODE_NULL)) {
cannam@85 426 return failure("Expanded %s to %s\n", c.buf, xnode.buf);
cannam@85 427 }
cannam@85 428
cannam@85 429 SerdNode xu = serd_env_expand_node(env, &u);
cannam@85 430 if (strcmp((const char*)xu.buf, "http://example.org/foo")) {
cannam@85 431 return failure("Expanded %s to %s\n", c.buf, xu.buf);
cannam@85 432 }
cannam@85 433 serd_node_free(&xu);
cannam@85 434
cannam@85 435 SerdNode badpre = serd_node_from_string(SERD_CURIE, USTR("hm:what"));
cannam@85 436 SerdNode xbadpre = serd_env_expand_node(env, &badpre);
cannam@85 437 if (!serd_node_equals(&xbadpre, &SERD_NODE_NULL)) {
cannam@85 438 return failure("Expanded invalid curie %s\n", badpre.buf);
cannam@85 439 }
cannam@85 440
cannam@85 441 SerdNode xc = serd_env_expand_node(env, &c);
cannam@85 442 if (strcmp((const char*)xc.buf, "http://example.org/b")) {
cannam@85 443 return failure("Expanded %s to %s\n", c.buf, xc.buf);
cannam@85 444 }
cannam@85 445 serd_node_free(&xc);
cannam@85 446
cannam@85 447 if (!serd_env_set_prefix(env, &SERD_NODE_NULL, &SERD_NODE_NULL)) {
cannam@85 448 return failure("Set NULL prefix\n");
cannam@85 449 }
cannam@85 450
cannam@85 451 const SerdNode lit = serd_node_from_string(SERD_LITERAL, USTR("hello"));
cannam@85 452 if (!serd_env_set_prefix(env, &b, &lit)) {
cannam@85 453 return failure("Set prefix to literal\n");
cannam@85 454 }
cannam@85 455
cannam@85 456 int n_prefixes = 0;
cannam@85 457 serd_env_set_prefix_from_strings(env, USTR("eg.2"), USTR("http://example.org/"));
cannam@85 458 serd_env_foreach(env, count_prefixes, &n_prefixes);
cannam@85 459 if (n_prefixes != 1) {
cannam@85 460 return failure("Bad prefix count %d\n", n_prefixes);
cannam@85 461 }
cannam@85 462
cannam@85 463 SerdNode shorter_uri = serd_node_from_string(SERD_URI, USTR("urn:foo"));
cannam@85 464 SerdNode prefix_name;
cannam@85 465 if (serd_env_qualify(env, &shorter_uri, &prefix_name, &suffix)) {
cannam@85 466 return failure("Qualified %s\n", shorter_uri.buf);
cannam@85 467 }
cannam@85 468
cannam@85 469 // Test SerdReader and SerdWriter
cannam@85 470
cannam@85 471 const char* path = "serd_test.ttl";
cannam@85 472 FILE* fd = fopen(path, "w");
cannam@85 473 if (!fd) {
cannam@85 474 return failure("Failed to open file %s\n", path);
cannam@85 475 }
cannam@85 476
cannam@85 477 SerdWriter* writer = serd_writer_new(
cannam@85 478 SERD_TURTLE, (SerdStyle)0, env, NULL, serd_file_sink, fd);
cannam@85 479 if (!writer) {
cannam@85 480 return failure("Failed to create writer\n");
cannam@85 481 }
cannam@85 482
cannam@85 483 serd_writer_chop_blank_prefix(writer, USTR("tmp"));
cannam@85 484 serd_writer_chop_blank_prefix(writer, NULL);
cannam@85 485
cannam@85 486 if (!serd_writer_set_base_uri(writer, &lit)) {
cannam@85 487 return failure("Set base URI to %s\n", lit.buf);
cannam@85 488 }
cannam@85 489
cannam@85 490 if (!serd_writer_set_prefix(writer, &lit, &lit)) {
cannam@85 491 return failure("Set prefix %s to %s\n", lit.buf, lit.buf);
cannam@85 492 }
cannam@85 493
cannam@85 494 if (!serd_writer_end_anon(writer, NULL)) {
cannam@85 495 return failure("Ended non-existent anonymous node\n");
cannam@85 496 }
cannam@85 497
cannam@85 498 if (serd_writer_get_env(writer) != env) {
cannam@85 499 return failure("Writer has incorrect env\n");
cannam@85 500 }
cannam@85 501
cannam@85 502 uint8_t buf[] = { 0x80, 0, 0, 0, 0 };
cannam@85 503 SerdNode s = serd_node_from_string(SERD_URI, USTR(""));
cannam@85 504 SerdNode p = serd_node_from_string(SERD_URI, USTR("http://example.org/pred"));
cannam@85 505 SerdNode o = serd_node_from_string(SERD_LITERAL, buf);
cannam@85 506
cannam@85 507 // Write 3 invalid statements (should write nothing)
cannam@85 508 const SerdNode* junk[][5] = { { &s, &p, NULL, NULL, NULL },
cannam@85 509 { &s, NULL, &o, NULL, NULL },
cannam@85 510 { NULL, &p, &o, NULL, NULL },
cannam@85 511 { &s, &p, &SERD_NODE_NULL, NULL, NULL },
cannam@85 512 { &s, &SERD_NODE_NULL, &o, NULL, NULL },
cannam@85 513 { &SERD_NODE_NULL, &p, &o, NULL, NULL },
cannam@85 514 { &s, &o, &o, NULL, NULL },
cannam@85 515 { &o, &p, &o, NULL, NULL },
cannam@85 516 { NULL, NULL, NULL, NULL, NULL } };
cannam@85 517 for (unsigned i = 0; i < sizeof(junk) / (sizeof(SerdNode*) * 5); ++i) {
cannam@85 518 if (!serd_writer_write_statement(
cannam@85 519 writer, 0, NULL,
cannam@85 520 junk[i][0], junk[i][1], junk[i][2], junk[i][3], junk[i][4])) {
cannam@85 521 return failure("Successfully wrote junk statement %d\n", i);
cannam@85 522 }
cannam@85 523 }
cannam@85 524
cannam@85 525 const SerdNode t = serd_node_from_string(SERD_URI, USTR("urn:Type"));
cannam@85 526 const SerdNode l = serd_node_from_string(SERD_LITERAL, USTR("en"));
cannam@85 527 const SerdNode* good[][5] = { { &s, &p, &o, NULL, NULL },
cannam@85 528 { &s, &p, &o, &SERD_NODE_NULL, &SERD_NODE_NULL },
cannam@85 529 { &s, &p, &o, &t, NULL },
cannam@85 530 { &s, &p, &o, NULL, &l },
cannam@85 531 { &s, &p, &o, &t, &l },
cannam@85 532 { &s, &p, &o, &t, &SERD_NODE_NULL },
cannam@85 533 { &s, &p, &o, &SERD_NODE_NULL, &l },
cannam@85 534 { &s, &p, &o, NULL, &SERD_NODE_NULL },
cannam@85 535 { &s, &p, &o, &SERD_NODE_NULL, NULL },
cannam@85 536 { &s, &p, &o, &SERD_NODE_NULL, NULL } };
cannam@85 537 for (unsigned i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 5); ++i) {
cannam@85 538 if (serd_writer_write_statement(
cannam@85 539 writer, 0, NULL,
cannam@85 540 good[i][0], good[i][1], good[i][2], good[i][3], good[i][4])) {
cannam@85 541 return failure("Failed to write good statement %d\n", i);
cannam@85 542 }
cannam@85 543 }
cannam@85 544
cannam@85 545 // Write 1 statement with bad UTF-8 (should be replaced)
cannam@85 546 if (serd_writer_write_statement(writer, 0, NULL,
cannam@85 547 &s, &p, &o, NULL, NULL)) {
cannam@85 548 return failure("Failed to write junk UTF-8\n");
cannam@85 549 }
cannam@85 550
cannam@85 551 // Write 1 valid statement
cannam@85 552 o = serd_node_from_string(SERD_LITERAL, USTR("hello"));
cannam@85 553 if (serd_writer_write_statement(writer, 0, NULL,
cannam@85 554 &s, &p, &o, NULL, NULL)) {
cannam@85 555 return failure("Failed to write valid statement\n");
cannam@85 556 }
cannam@85 557
cannam@85 558 serd_writer_free(writer);
cannam@85 559
cannam@85 560 // Test chunk sink
cannam@85 561 SerdChunk chunk = { NULL, 0 };
cannam@85 562 writer = serd_writer_new(
cannam@85 563 SERD_TURTLE, (SerdStyle)0, env, NULL, serd_chunk_sink, &chunk);
cannam@85 564
cannam@85 565 o = serd_node_from_string(SERD_URI, USTR("http://example.org/base"));
cannam@85 566 if (serd_writer_set_base_uri(writer, &o)) {
cannam@85 567 return failure("Failed to write to chunk sink\n");
cannam@85 568 }
cannam@85 569
cannam@85 570 serd_writer_free(writer);
cannam@85 571 uint8_t* out = serd_chunk_sink_finish(&chunk);
cannam@85 572
cannam@85 573 if (strcmp((const char*)out, "@base <http://example.org/base> .\n")) {
cannam@85 574 return failure("Incorrect chunk output:\n%s\n", chunk.buf);
cannam@85 575 }
cannam@85 576
cannam@85 577 free(out);
cannam@85 578
cannam@85 579 // Rewind and test reader
cannam@85 580 fseek(fd, 0, SEEK_SET);
cannam@85 581
cannam@85 582 ReaderTest* rt = (ReaderTest*)malloc(sizeof(ReaderTest));
cannam@85 583 rt->n_statements = 0;
cannam@85 584 rt->graph = NULL;
cannam@85 585
cannam@85 586 SerdReader* reader = serd_reader_new(
cannam@85 587 SERD_TURTLE, rt, free,
cannam@85 588 NULL, NULL, test_sink, NULL);
cannam@85 589 if (!reader) {
cannam@85 590 return failure("Failed to create reader\n");
cannam@85 591 }
cannam@85 592 if (serd_reader_get_handle(reader) != rt) {
cannam@85 593 return failure("Corrupt reader handle\n");
cannam@85 594 }
cannam@85 595
cannam@85 596 SerdNode g = serd_node_from_string(SERD_URI, USTR("http://example.org/"));
cannam@85 597 serd_reader_set_default_graph(reader, &g);
cannam@85 598 serd_reader_add_blank_prefix(reader, USTR("tmp"));
cannam@85 599 serd_reader_add_blank_prefix(reader, NULL);
cannam@85 600
cannam@85 601 if (!serd_reader_read_file(reader, USTR("http://notafile"))) {
cannam@85 602 return failure("Apparently read an http URI\n");
cannam@85 603 }
cannam@85 604 if (!serd_reader_read_file(reader, USTR("file:///better/not/exist"))) {
cannam@85 605 return failure("Apprently read a non-existent file\n");
cannam@85 606 }
cannam@85 607 SerdStatus st = serd_reader_read_file(reader, USTR(path));
cannam@85 608 if (st) {
cannam@85 609 return failure("Error reading file (%s)\n", serd_strerror(st));
cannam@85 610 }
cannam@85 611
cannam@85 612 if (rt->n_statements != 12) {
cannam@85 613 return failure("Bad statement count %d\n", rt->n_statements);
cannam@85 614 } else if (!rt->graph || !rt->graph->buf ||
cannam@85 615 strcmp((const char*)rt->graph->buf, "http://example.org/")) {
cannam@85 616 return failure("Bad graph %p\n", rt->graph);
cannam@85 617 }
cannam@85 618
cannam@85 619 if (!serd_reader_read_string(reader, USTR("This isn't Turtle at all."))) {
cannam@85 620 return failure("Parsed invalid string successfully.\n");
cannam@85 621 }
cannam@85 622
cannam@85 623 serd_reader_free(reader);
cannam@85 624 fclose(fd);
cannam@85 625
cannam@85 626 serd_env_free(env);
cannam@85 627
cannam@85 628 printf("Success\n");
cannam@85 629 return 0;
cannam@85 630 }