Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

657 lines
22KB

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