test_codec_nt.c 8.0 KB

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