test_codec_nt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "test.h"
  2. #include "codec/codec_nt.h"
  3. #define TERM_CT 11
  4. typedef char nt_str[64];
  5. LSUP_Term **
  6. init_terms (void)
  7. {
  8. LSUP_Term **terms = malloc (TERM_CT * sizeof (*terms));
  9. terms[0] = LSUP_iriref_new("urn:local:s1", NULL);
  10. terms[1] = LSUP_iriref_new("http://example.org/p1", NULL);
  11. terms[2] = LSUP_iriref_new("http://example.org/p2", NULL);
  12. terms[3] = LSUP_literal_new ("hello", NULL);
  13. terms[4] = LSUP_lt_literal_new ("hello", "en-US");
  14. terms[5] = LSUP_lt_literal_new ("hello", "es-ES");
  15. terms[6] = LSUP_literal_new (
  16. "25",
  17. LSUP_iriref_new ("http://www.w3.org/2001/XMLSchema#integer", NULL));
  18. terms[7] = LSUP_literal_new (
  19. "This \\is\\ a \"multi-line\"\n'literal'\t.", NULL);
  20. terms[8] = LSUP_bnode_new ("bn1");
  21. terms[9] = LSUP_term_new (LSUP_TERM_UNDEFINED, "bogus", NULL);
  22. terms[10] = TERM_DUMMY;
  23. return terms;
  24. }
  25. // Starting NT term strings. They may have comments and junk whitespace.
  26. static nt_str start_nt[TERM_CT] = {
  27. "<urn:local:s1>",
  28. "<http://example.org/p1> ",
  29. "<http://example.org/p2> ",
  30. "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string>",
  31. "\"hello\" @en-US",
  32. "\"hello\"@es-ES # Does \"Hello\" in Spanish mean \"hola\"?",
  33. "\"25\" ^^<http://www.w3.org/2001/XMLSchema#integer> ",
  34. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"",
  35. "_:bn1",
  36. "This is nothing.",
  37. "",
  38. };
  39. // End result NT term strings, as they should be produced by the NT codec.
  40. static nt_str end_nt[TERM_CT] = {
  41. "<urn:local:s1>",
  42. "<http://example.org/p1>",
  43. "<http://example.org/p2>",
  44. "\"hello\"",
  45. "\"hello\"@en-US",
  46. "\"hello\"@es-ES",
  47. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer>",
  48. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"",
  49. "_:bn1",
  50. "",
  51. "",
  52. };
  53. // Start NT document. It may have comments and junk whitespace.
  54. char *start_nt_doc = (
  55. "<urn:local:s1> <http://example.org/p1> \"hello\" . # Comment here.\n"
  56. "<urn:local:s1> <http://example.org/p1> "
  57. "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string> .\n"
  58. "<urn:local:s1> <http://example.org/p2>\"hello\"@en-US.\n"
  59. "<urn:local:s1> <http://example.org/p2>\"hello\"@es-ES .\n"
  60. "# Some comments\n# To make it a bit \n #less boring.\n"
  61. "<urn:local:s1> <http://example.org/p2> _:bn1 .\n"
  62. "_:bn1 <http://example.org/p1> \"hello\"@es-ES.\n"
  63. " _:bn1 <http://example.org/p1> "
  64. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n"
  65. "_:bn1 <http://example.org/p1> "
  66. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\""
  67. " .\n"
  68. "# FREE WHITESPACE!\n\n\n\n\n\n\n \n\n\n\n"
  69. );
  70. char *bad_nt_doc = (
  71. "<urn:local:s1> <http://example.org/p1> \"hello\" . # Comment here.\n"
  72. "<urn:local:s1> <http://example.org/p1>\"hello\"@es-ES .\n"
  73. "<urn:local:s1> @ \"Bad Data.\" ."
  74. );
  75. // End result NT document as it should be produced by the NT codec.
  76. // Lines should not be checked in strict order.
  77. char *end_nt_doc[7] = {
  78. "<urn:local:s1> <http://example.org/p1> \"hello\" .\n",
  79. "<urn:local:s1> <http://example.org/p2> \"hello\"@en-US .\n",
  80. "<urn:local:s1> <http://example.org/p2> \"hello\"@es-ES .\n",
  81. "<urn:local:s1> <http://example.org/p2> _:bn1 .\n",
  82. "_:bn1 <http://example.org/p1> \"hello\"@es-ES .\n",
  83. "_:bn1 <http://example.org/p1> "
  84. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n",
  85. "_:bn1 <http://example.org/p1> "
  86. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\" .\n",
  87. };
  88. static LSUP_Triple trp[8];
  89. void
  90. init_triples (LSUP_Term **terms)
  91. {
  92. memset (trp, 0, sizeof (LSUP_Triple) * 8); // Last NULL is a sentinel
  93. LSUP_triple_init (trp + 0, terms[0], terms[1], terms[3]);
  94. LSUP_triple_init (trp + 1, terms[0], terms[2], terms[4]);
  95. LSUP_triple_init (trp + 2, terms[0], terms[2], terms[5]);
  96. LSUP_triple_init (trp + 3, terms[0], terms[2], terms[8]);
  97. LSUP_triple_init (trp + 4, terms[8], terms[1], terms[5]);
  98. LSUP_triple_init (trp + 5, terms[8], terms[1], terms[6]);
  99. LSUP_triple_init (trp + 6, terms[8], terms[1], terms[7]);
  100. }
  101. void
  102. free_terms (LSUP_Term **terms)
  103. {
  104. for (int i = 0; i < TERM_CT; i++)
  105. LSUP_term_free (terms[i]);
  106. free (terms);
  107. }
  108. int
  109. test_encode_nt_term()
  110. {
  111. LSUP_Term **terms = init_terms();
  112. LSUP_NSMap *nsm = LSUP_nsmap_new();
  113. LSUP_nsmap_add (nsm, "local", "urn:local:");
  114. LSUP_nsmap_add (nsm, "ext", "http://example.org");
  115. char *out = NULL;
  116. // Test that passing a NS map has no effect.
  117. EXPECT_PASS (codec.encode_term (terms[0], nsm, &out));
  118. EXPECT_STR_EQ (out, end_nt[0]);
  119. for (int i = 0; i < TERM_CT - 2; i++) {
  120. log_info ("Test encoding term #%d of %d.", i, TERM_CT - 2);
  121. EXPECT_PASS (codec.encode_term (terms[i], NULL, &out));
  122. EXPECT_STR_EQ (out, end_nt[i]);
  123. }
  124. EXPECT_INT_EQ (codec.encode_term (terms[9], NULL, &out), LSUP_PARSE_ERR);
  125. EXPECT_INT_EQ (codec.encode_term (terms[10], NULL, &out), LSUP_PARSE_ERR);
  126. free (out);
  127. LSUP_nsmap_free (nsm);
  128. free_terms(terms);
  129. return 0;
  130. }
  131. int
  132. test_encode_nt_graph()
  133. {
  134. log_info ("Test encoding graph document.");
  135. LSUP_Graph *gr = LSUP_graph_new (
  136. LSUP_iriref_new (NULL, NULL), LSUP_STORE_HTABLE, NULL, NULL, 0);
  137. if (!gr) return LSUP_MEM_ERR;
  138. size_t ins;
  139. LSUP_graph_add (gr, trp, &ins);
  140. char *out = calloc (1, 1);
  141. void *it = codec.encode_graph_init (gr);
  142. ASSERT (it != NULL, "Error creating codec iterator!");
  143. char *tmp = NULL;
  144. LSUP_rc rc;
  145. while ((rc = codec.encode_graph_iter (it, &tmp)) != LSUP_END) {
  146. ASSERT (rc >= 0, "Encoding step failed!");
  147. out = realloc (out, strlen(out) + strlen (tmp) + 1);
  148. out = strcat (out, tmp);
  149. }
  150. codec.encode_graph_done (it);
  151. free (tmp);
  152. LSUP_graph_free (gr);
  153. //log_info ("Serialized graph: %s\n", out);
  154. for (int i = 0; i < 7; i++)
  155. ASSERT (strstr (out, end_nt_doc[i]) != NULL, end_nt_doc[i]);
  156. free (out);
  157. return 0;
  158. }
  159. static int
  160. test_decode_nt_term()
  161. {
  162. log_info ("Test decoding terms.");
  163. for (int i = 0; i < TERM_CT - 2; i++) {
  164. log_debug ("Decoding term %d/%d.", i, TERM_CT - 3);
  165. LSUP_Term *term;
  166. EXPECT_PASS (codec.decode_term (start_nt[i], NULL, &term));
  167. LSUP_term_free (term);
  168. }
  169. return 0;
  170. }
  171. int
  172. test_decode_nt_graph()
  173. {
  174. FILE *input = fmemopen ((void *)start_nt_doc, strlen (start_nt_doc), "r");
  175. LSUP_Graph *gr;
  176. size_t ct;
  177. char *err;
  178. EXPECT_PASS (codec.decode_graph (input, &gr, &ct, &err));
  179. fclose (input);
  180. ASSERT (err == NULL, "Error string is not NULL!");
  181. #if 1
  182. // For debugging: dump the decoded graph into NT.
  183. char *out = calloc (1, 1);
  184. char *tmp = NULL;
  185. void *it = codec.encode_graph_init (gr);
  186. while (codec.encode_graph_iter (it, &tmp) != LSUP_END) {
  187. out = realloc (out, strlen(out) + strlen (tmp) + 1);
  188. out = strcat (out, tmp);
  189. }
  190. codec.encode_graph_done (it);
  191. free (tmp);
  192. log_trace ("Serialized graph: \n%s", out);
  193. free (out);
  194. #endif
  195. EXPECT_INT_EQ (ct, 8);
  196. EXPECT_INT_EQ (LSUP_graph_size (gr), 7);
  197. for (int i = 0; i < 7; i++) {
  198. log_info ("Checking triple #%d.", i);
  199. EXPECT_INT_EQ (LSUP_graph_contains (gr, trp + i), 1);
  200. }
  201. LSUP_graph_free (gr);
  202. return 0;
  203. }
  204. int
  205. test_decode_nt_bad_graph()
  206. {
  207. log_info ("testing illegal NT document.");
  208. FILE *input = fmemopen ((void *)bad_nt_doc, strlen (start_nt_doc), "r");
  209. LSUP_Graph *gr;
  210. size_t ct;
  211. char *err;
  212. LSUP_rc rc = codec.decode_graph (input, &gr, &ct, &err);
  213. EXPECT_INT_EQ (rc, LSUP_PARSE_ERR);
  214. log_info ("Error: %s", err);
  215. ASSERT (strstr (err, "`@ \"Bad Data.\"") != NULL, "Wrong error string report!");
  216. ASSERT (strstr (err, "line 3") != NULL, "Wrong error line report!");
  217. ASSERT (strstr (err, "character 16") != NULL, "Wrong error char report!");
  218. free (err);
  219. fclose (input);
  220. LSUP_graph_free (gr);
  221. return 0;
  222. }
  223. int codec_nt_tests()
  224. {
  225. LSUP_Term **terms = init_terms();
  226. init_triples (terms);
  227. codec = nt_codec;
  228. RUN (test_encode_nt_term);
  229. RUN (test_encode_nt_graph);
  230. RUN (test_decode_nt_term);
  231. RUN (test_decode_nt_graph);
  232. RUN (test_decode_nt_bad_graph);
  233. free_terms(terms);
  234. return 0;
  235. }