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.

serd_test.c 21KB

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