test_codec_nt.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "test.h"
  2. #include "codec/codec_nt.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", NULL);
  11. terms[1] = LSUP_iriref_new("http://example.org/p1", NULL);
  12. terms[2] = LSUP_iriref_new("http://example.org/p2", NULL);
  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", NULL));
  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 *nsm = LSUP_nsmap_new();
  114. LSUP_nsmap_add (nsm, "local", "urn:local:");
  115. LSUP_nsmap_add (nsm, "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], nsm, &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], NULL, &out));
  123. EXPECT_STR_EQ (out, end_nt[i]);
  124. }
  125. EXPECT_INT_EQ (codec.encode_term (terms[9], NULL, &out), LSUP_PARSE_ERR);
  126. EXPECT_INT_EQ (codec.encode_term (terms[10], NULL, &out), LSUP_PARSE_ERR);
  127. free (out);
  128. LSUP_nsmap_free (nsm);
  129. free_terms (terms);
  130. return 0;
  131. }
  132. int
  133. test_encode_nt_graph()
  134. {
  135. log_info ("Test encoding graph document.");
  136. LSUP_Graph *gr = LSUP_graph_new (NULL, NULL, NULL);
  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. for (int i = 0; i < TRP_CT; i++)
  235. free (trp[i]);
  236. return 0;
  237. }