test_codec_nt.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include <unistd.h>
  2. #include "volksdata/codec/codec_nt.h"
  3. #include "test.h"
  4. #define TERM_CT 11
  5. #define TRP_CT 8
  6. typedef char nt_str[64];
  7. VOLK_Term **
  8. init_terms (void)
  9. {
  10. VOLK_Term **terms = malloc (TERM_CT * sizeof (*terms));
  11. terms[0] = VOLK_iriref_new("urn:local:s1");
  12. terms[1] = VOLK_iriref_new("http://example.org/p1");
  13. terms[2] = VOLK_iriref_new("http://example.org/p2");
  14. terms[3] = VOLK_literal_new ("hello", NULL);
  15. terms[4] = VOLK_lt_literal_new ("hello", "en-US");
  16. terms[5] = VOLK_lt_literal_new ("hello", "es-ES");
  17. terms[6] = VOLK_literal_new (
  18. "25",
  19. VOLK_iriref_new ("http://www.w3.org/2001/XMLSchema#integer"));
  20. terms[7] = VOLK_literal_new (
  21. "This \\is\\ a \"multi-line\"\n'literal'\t.", NULL);
  22. terms[8] = VOLK_bnode_new ("bn1");
  23. terms[9] = VOLK_term_new (VOLK_TERM_UNDEFINED, "bogus", NULL);
  24. terms[10] = TERM_DUMMY;
  25. return terms;
  26. }
  27. // Starting NT term strings. They may have comments and junk whitespace.
  28. static nt_str start_nt[TERM_CT] = {
  29. "<urn:local:s1>",
  30. "<http://example.org/p1> ",
  31. "<http://example.org/p2> ",
  32. "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string>",
  33. "\"hello\" @en-US",
  34. "\"hello\"@es-ES # Does \"Hello\" in Spanish mean \"hola\"?",
  35. "\"25\" ^^<http://www.w3.org/2001/XMLSchema#integer> ",
  36. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"",
  37. "_:bn1",
  38. "This is nothing.",
  39. "",
  40. };
  41. // End result NT term strings, as they should be produced by the NT codec.
  42. static nt_str end_nt[TERM_CT] = {
  43. "<urn:local:s1>",
  44. "<http://example.org/p1>",
  45. "<http://example.org/p2>",
  46. "\"hello\"",
  47. "\"hello\"@en-US",
  48. "\"hello\"@es-ES",
  49. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer>",
  50. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"",
  51. "_:bn1",
  52. "",
  53. "",
  54. };
  55. // Start NT document. It may have comments and junk whitespace.
  56. char *start_nt_doc = (
  57. "<urn:local:s1> <http://example.org/p1> \"hello\" . # Comment here.\n"
  58. "<urn:local:s1> <http://example.org/p1> "
  59. "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string> .\n"
  60. "<urn:local:s1> <http://example.org/p2>\"hello\"@en-US.\n"
  61. "<urn:local:s1> <http://example.org/p2>\"hello\"@es-ES .\n"
  62. "# Some comments\n# To make it a bit \n #less boring.\n"
  63. "<urn:local:s1> <http://example.org/p2> _:bn1 .\n"
  64. "_:bn1 <http://example.org/p1> \"hello\"@es-ES.\n"
  65. " _:bn1 <http://example.org/p1> "
  66. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n"
  67. "_:bn1 <http://example.org/p1> "
  68. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\""
  69. " .\n"
  70. "# FREE WHITESPACE!\n\n\n\n\n\n\n \n\n\n\n"
  71. );
  72. char *bad_nt_doc = (
  73. "<urn:local:s1> <http://example.org/p1> \"hello\" . # Comment here.\n"
  74. "<urn:local:s1> <http://example.org/p1>\"hello\"@es-ES .\n"
  75. "<urn:local:s1> @ \"Bad Data.\" ."
  76. );
  77. // End result NT document as it should be produced by the NT codec.
  78. // Lines should not be checked in strict order.
  79. char *end_nt_doc[7] = {
  80. "<urn:local:s1> <http://example.org/p1> \"hello\" .\n",
  81. "<urn:local:s1> <http://example.org/p2> \"hello\"@en-US .\n",
  82. "<urn:local:s1> <http://example.org/p2> \"hello\"@es-ES .\n",
  83. "<urn:local:s1> <http://example.org/p2> _:bn1 .\n",
  84. "_:bn1 <http://example.org/p1> \"hello\"@es-ES .\n",
  85. "_:bn1 <http://example.org/p1> "
  86. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n",
  87. "_:bn1 <http://example.org/p1> "
  88. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\" .\n",
  89. };
  90. static VOLK_Triple *trp[TRP_CT];
  91. void
  92. init_triples (VOLK_Term **terms)
  93. {
  94. trp[0] = VOLK_triple_new (terms[0], terms[1], terms[3]);
  95. trp[1] = VOLK_triple_new (terms[0], terms[2], terms[4]);
  96. trp[2] = VOLK_triple_new (terms[0], terms[2], terms[5]);
  97. trp[3] = VOLK_triple_new (terms[0], terms[2], terms[8]);
  98. trp[4] = VOLK_triple_new (terms[8], terms[1], terms[5]);
  99. trp[5] = VOLK_triple_new (terms[8], terms[1], terms[6]);
  100. trp[6] = VOLK_triple_new (terms[8], terms[1], terms[7]);
  101. trp[7] = NULL;
  102. }
  103. void
  104. free_terms (VOLK_Term **terms)
  105. {
  106. for (int i = 0; i < TERM_CT; i++)
  107. VOLK_term_free (terms[i]);
  108. free (terms);
  109. }
  110. int
  111. test_encode_nt_term()
  112. {
  113. VOLK_Term **terms = init_terms();
  114. VOLK_nsmap_add ("local", "urn:local:");
  115. VOLK_nsmap_add ("ext", "http://example.org");
  116. char *out = NULL;
  117. // Test that passing a NS map has no effect.
  118. EXPECT_PASS (codec.encode_term (terms[0], &out));
  119. EXPECT_STR_EQ (out, end_nt[0]);
  120. for (int i = 0; i < TERM_CT - 2; i++) {
  121. log_info ("Test encoding term #%d of %d.", i, TERM_CT - 2);
  122. EXPECT_PASS (codec.encode_term (terms[i], &out));
  123. EXPECT_STR_EQ (out, end_nt[i]);
  124. }
  125. EXPECT_INT_EQ (codec.encode_term (terms[9], &out), VOLK_PARSE_ERR);
  126. EXPECT_INT_EQ (codec.encode_term (terms[10], &out), VOLK_PARSE_ERR);
  127. free (out);
  128. free_terms (terms);
  129. return 0;
  130. }
  131. int
  132. test_encode_nt_graph()
  133. {
  134. log_info ("Test encoding graph document.");
  135. VOLK_Graph *gr = VOLK_graph_new (NULL, NULL);
  136. if (!gr) return VOLK_MEM_ERR;
  137. size_t ins;
  138. VOLK_graph_add (gr, trp, &ins);
  139. char *out = calloc (1, 1);
  140. void *it = codec.encode_graph_init (gr);
  141. ASSERT (it != NULL, "Error creating codec iterator!");
  142. char *tmp = NULL;
  143. VOLK_rc rc;
  144. while ((rc = codec.encode_graph_iter (it, &tmp)) != VOLK_END) {
  145. ASSERT (rc >= 0, "Encoding step failed!");
  146. out = realloc (out, strlen(out) + strlen (tmp) + 1);
  147. out = strcat (out, tmp);
  148. }
  149. codec.encode_graph_done (it);
  150. free (tmp);
  151. VOLK_graph_free (gr);
  152. //log_info ("Serialized graph: %s\n", out);
  153. for (int i = 0; i < 7; i++)
  154. ASSERT (strstr (out, end_nt_doc[i]) != NULL, end_nt_doc[i]);
  155. free (out);
  156. return 0;
  157. }
  158. static int
  159. test_decode_nt_term()
  160. {
  161. log_info ("Test decoding terms.");
  162. for (int i = 0; i < TERM_CT - 2; i++) {
  163. LOG_DEBUG("Decoding term %d/%d.", i, TERM_CT - 3);
  164. VOLK_Term *term;
  165. EXPECT_PASS (codec.decode_term (start_nt[i], &term));
  166. VOLK_term_free (term);
  167. }
  168. return 0;
  169. }
  170. int
  171. test_decode_nt_graph()
  172. {
  173. VOLK_Graph *gr;
  174. size_t ct;
  175. char *err;
  176. EXPECT_PASS (codec.decode_graph (NULL, start_nt_doc, &gr, &ct, &err));
  177. ASSERT (err == NULL, "Error string is not NULL!");
  178. #if 1
  179. // For debugging: dump the decoded graph into NT.
  180. char *out = calloc (1, 1);
  181. char *tmp = NULL;
  182. void *it = codec.encode_graph_init (gr);
  183. while (codec.encode_graph_iter (it, &tmp) != VOLK_END) {
  184. out = realloc (out, strlen(out) + strlen (tmp) + 1);
  185. out = strcat (out, tmp);
  186. }
  187. codec.encode_graph_done (it);
  188. free (tmp);
  189. LOG_TRACE("Serialized graph: \n%s", out);
  190. free (out);
  191. #endif
  192. EXPECT_INT_EQ (ct, 8);
  193. EXPECT_INT_EQ (VOLK_graph_size (gr), 7);
  194. for (int i = 0; i < 7; i++) {
  195. log_info ("Checking triple #%d.", i);
  196. EXPECT_INT_EQ (VOLK_graph_contains (gr, trp[i]), 1);
  197. }
  198. VOLK_graph_free (gr);
  199. return 0;
  200. }
  201. int
  202. test_decode_nt_file()
  203. {
  204. VOLK_Graph *gr;
  205. size_t ct;
  206. char *err;
  207. FILE *fh = fopen ("test/assets/test2.nt", "r");
  208. EXPECT_PASS (codec.decode_graph (fh, NULL, &gr, &ct, &err));
  209. EXPECT_INT_EQ (VOLK_graph_size (gr), 7);
  210. EXPECT_INT_EQ (ct, 8);
  211. VOLK_graph_free (gr);
  212. fclose(fh);
  213. return 0;
  214. }
  215. #define LARGE_LIT_SIZE CHUNK_SIZE * 2 + 1 // More than 2 buffer pages.
  216. int
  217. test_decode_large_lit_file()
  218. {
  219. VOLK_Graph *gr;
  220. size_t ct;
  221. char *err;
  222. const char *fpath = "/tmp/test_large_lit.nt";
  223. FILE *fh = fopen (fpath, "w");
  224. fprintf (fh, "<urn:s:1> <urn:p:1> \"");
  225. for (unsigned i = 0; i < LARGE_LIT_SIZE; i++)
  226. fputc (rand() % 25 + 65, fh); // A-Z
  227. fprintf(fh, "\" .\n");
  228. fclose(fh);
  229. fh = fopen (fpath, "r");
  230. EXPECT_PASS (codec.decode_graph (fh, NULL, &gr, &ct, &err));
  231. EXPECT_INT_EQ (VOLK_graph_size (gr), 1);
  232. EXPECT_INT_EQ (ct, 1);
  233. VOLK_graph_free (gr);
  234. fclose(fh);
  235. unlink (fpath);
  236. return 0;
  237. }
  238. int
  239. test_decode_nt_bad_graph()
  240. {
  241. log_info ("testing illegal NT document.");
  242. VOLK_Graph *gr;
  243. size_t ct;
  244. char *err;
  245. VOLK_rc rc = codec.decode_graph (NULL, bad_nt_doc, &gr, &ct, &err);
  246. EXPECT_INT_EQ (rc, VOLK_PARSE_ERR);
  247. log_info ("Error: %s", err);
  248. ASSERT (strstr (err, "`@ \"Bad Data.\"") != NULL, "Wrong error string report!");
  249. ASSERT (strstr (err, "line 3") != NULL, "Wrong error line report!");
  250. ASSERT (strstr (err, "character 16") != NULL, "Wrong error char report!");
  251. free (err);
  252. VOLK_graph_free (gr);
  253. return 0;
  254. }
  255. int codec_nt_tests()
  256. {
  257. VOLK_Term **terms = init_terms();
  258. init_triples (terms);
  259. codec = nt_codec;
  260. RUN (test_encode_nt_term);
  261. RUN (test_encode_nt_graph);
  262. RUN (test_decode_nt_term);
  263. RUN (test_decode_nt_graph);
  264. RUN (test_decode_nt_file);
  265. //RUN (test_decode_large_lit_file); // FIXME large literals still not working.
  266. RUN (test_decode_nt_bad_graph);
  267. free_terms (terms);
  268. for (int i = 0; i < TRP_CT; i++)
  269. free (trp[i]);
  270. return 0;
  271. }