test_codec_nt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 (NULL, NULL, NULL);
  136. if (!gr) return LSUP_MEM_ERR;
  137. size_t ins;
  138. LSUP_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. LSUP_rc rc;
  144. while ((rc = codec.encode_graph_iter (it, &tmp)) != LSUP_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. LSUP_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. LSUP_Term *term;
  165. EXPECT_PASS (codec.decode_term (start_nt[i], NULL, &term));
  166. LSUP_term_free (term);
  167. }
  168. return 0;
  169. }
  170. int
  171. test_decode_nt_graph()
  172. {
  173. FILE *input = fmemopen ((void *)start_nt_doc, strlen (start_nt_doc), "r");
  174. LSUP_Graph *gr;
  175. size_t ct;
  176. char *err;
  177. EXPECT_PASS (codec.decode_graph (input, &gr, &ct, &err));
  178. fclose (input);
  179. ASSERT (err == NULL, "Error string is not NULL!");
  180. #if 1
  181. // For debugging: dump the decoded graph into NT.
  182. char *out = calloc (1, 1);
  183. char *tmp = NULL;
  184. void *it = codec.encode_graph_init (gr);
  185. while (codec.encode_graph_iter (it, &tmp) != LSUP_END) {
  186. out = realloc (out, strlen(out) + strlen (tmp) + 1);
  187. out = strcat (out, tmp);
  188. }
  189. codec.encode_graph_done (it);
  190. free (tmp);
  191. log_trace ("Serialized graph: \n%s", out);
  192. free (out);
  193. #endif
  194. EXPECT_INT_EQ (ct, 8);
  195. EXPECT_INT_EQ (LSUP_graph_size (gr), 7);
  196. for (int i = 0; i < 7; i++) {
  197. log_info ("Checking triple #%d.", i);
  198. EXPECT_INT_EQ (LSUP_graph_contains (gr, trp + i), 1);
  199. }
  200. LSUP_graph_free (gr);
  201. return 0;
  202. }
  203. int
  204. test_decode_nt_bad_graph()
  205. {
  206. log_info ("testing illegal NT document.");
  207. FILE *input = fmemopen ((void *)bad_nt_doc, strlen (start_nt_doc), "r");
  208. LSUP_Graph *gr;
  209. size_t ct;
  210. char *err;
  211. LSUP_rc rc = codec.decode_graph (input, &gr, &ct, &err);
  212. EXPECT_INT_EQ (rc, LSUP_PARSE_ERR);
  213. log_info ("Error: %s", err);
  214. ASSERT (strstr (err, "`@ \"Bad Data.\"") != NULL, "Wrong error string report!");
  215. ASSERT (strstr (err, "line 3") != NULL, "Wrong error line report!");
  216. ASSERT (strstr (err, "character 16") != NULL, "Wrong error char report!");
  217. free (err);
  218. fclose (input);
  219. LSUP_graph_free (gr);
  220. return 0;
  221. }
  222. int codec_nt_tests()
  223. {
  224. LSUP_Term **terms = init_terms();
  225. init_triples (terms);
  226. codec = nt_codec;
  227. RUN (test_encode_nt_term);
  228. RUN (test_encode_nt_graph);
  229. RUN (test_decode_nt_term);
  230. RUN (test_decode_nt_graph);
  231. RUN (test_decode_nt_bad_graph);
  232. free_terms(terms);
  233. return 0;
  234. }