test_codec_nt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "test.h"
  2. #include "codec/codec_nt.h"
  3. #define TERM_CT 10
  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_literal_new ("hello", NULL);
  12. terms[3] = LSUP_lt_literal_new ("hello", "en-US");
  13. terms[4] = LSUP_lt_literal_new ("hello", "es-ES");
  14. terms[5] = LSUP_literal_new (
  15. "25",
  16. LSUP_iriref_new ("http://www.w3.org/2001/XMLSchema#integer", NULL));
  17. terms[6] = LSUP_literal_new (
  18. "This \\is\\ a \"multi-line\"\n'literal'\t.", NULL);
  19. terms[7] = LSUP_bnode_new ("bn1");
  20. terms[8] = LSUP_term_new (LSUP_TERM_UNDEFINED, "bogus", NULL);
  21. terms[9] = TERM_DUMMY;
  22. return terms;
  23. }
  24. // Starting NT term strings. They may have comments and junk whitespace.
  25. static nt_str start_nt[TERM_CT] = {
  26. "<urn:local:s1>",
  27. "<http://example.org/p1> ",
  28. "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string>",
  29. "\"hello\" @en-US",
  30. "\"hello\"@es-ES # Does \"Hello\" in Spanish mean \"hola\"?",
  31. "\"25\" ^^<http://www.w3.org/2001/XMLSchema#integer> ",
  32. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"",
  33. "_:bn1",
  34. "This is nothing.",
  35. "",
  36. };
  37. // End result NT term strings, as they should be produced by the NT codec.
  38. static nt_str end_nt[TERM_CT] = {
  39. "<urn:local:s1>",
  40. "<http://example.org/p1>",
  41. "\"hello\"",
  42. "\"hello\"@en-US",
  43. "\"hello\"@es-ES",
  44. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer>",
  45. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"",
  46. "_:bn1",
  47. "",
  48. "",
  49. };
  50. // Start NT document. It may have comments and junk whitespace.
  51. char *start_nt_doc = (
  52. "<urn:local:s1> <http://example.org/p1> \"hello\" . # Comment here.\n"
  53. "<urn:local:s1> <http://example.org/p1> "
  54. "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string> .\n"
  55. "<urn:local:s1> <http://example.org/p1>\"hello\"@en-US .\n"
  56. "<urn:local:s1> <http://example.org/p1>\"hello\"@es-ES .\n"
  57. "# Some comments\n# To make it a bit \n #less boring.\n"
  58. "<urn:local:s1> <http://example.org/p1> _:bn1 .\n"
  59. "_:bn1 <http://example.org/p1> \"hello\"@es-ES.\n"
  60. " _:bn1 <http://example.org/p1> "
  61. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n"
  62. "_:bn1 <http://example.org/p1> "
  63. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\""
  64. " .\n"
  65. "# FREE WHITESPACE!\n\n\n\n\n\n\n \n\n\n\n"
  66. );
  67. char *bad_nt_doc = (
  68. "<urn:local:s1> <http://example.org/p1> \"hello\" . # Comment here.\n"
  69. "<urn:local:s1> <http://example.org/p1>\"hello\"@es-ES .\n"
  70. "<urn:local:s1> dc:title \"Bad Data.\" ."
  71. );
  72. // End result NT document as it should be produced by the NT codec.
  73. // Lines should not be checked in strict order.
  74. char *end_nt_doc[7] = {
  75. "<urn:local:s1> <http://example.org/p1> \"hello\" .\n",
  76. "<urn:local:s1> <http://example.org/p1> \"hello\"@en-US .\n",
  77. "<urn:local:s1> <http://example.org/p1> \"hello\"@es-ES .\n",
  78. "<urn:local:s1> <http://example.org/p1> _:bn1 .\n",
  79. "_:bn1 <http://example.org/p1> \"hello\"@es-ES .\n",
  80. "_:bn1 <http://example.org/p1> "
  81. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n",
  82. "_:bn1 <http://example.org/p1> "
  83. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\" .\n",
  84. };
  85. static LSUP_Triple trp[8];
  86. void
  87. init_triples (LSUP_Term **terms)
  88. {
  89. memset (trp, 0, sizeof (LSUP_Triple) * 8); // Last NULL is a sentinel
  90. LSUP_triple_init (trp + 0, terms[0], terms[1], terms[2]);
  91. LSUP_triple_init (trp + 1, terms[0], terms[1], terms[3]);
  92. LSUP_triple_init (trp + 2, terms[0], terms[1], terms[4]);
  93. LSUP_triple_init (trp + 3, terms[0], terms[1], terms[7]);
  94. LSUP_triple_init (trp + 4, terms[7], terms[1], terms[4]);
  95. LSUP_triple_init (trp + 5, terms[7], terms[1], terms[5]);
  96. LSUP_triple_init (trp + 6, terms[7], terms[1], terms[6]);
  97. }
  98. void
  99. free_terms (LSUP_Term **terms)
  100. {
  101. for (int i = 0; i < TERM_CT; i++)
  102. LSUP_term_free (terms[i]);
  103. free (terms);
  104. }
  105. int
  106. test_encode_nt_term()
  107. {
  108. LSUP_Term **terms = init_terms();
  109. LSUP_NSMap *nsm = LSUP_nsmap_new();
  110. LSUP_nsmap_add (nsm, "local", "urn:local:");
  111. LSUP_nsmap_add (nsm, "ext", "http://example.org");
  112. char *out = NULL;
  113. // Test that passing a NS map has no effect.
  114. EXPECT_PASS (codec.encode_term (terms[0], nsm, &out));
  115. EXPECT_STR_EQ (out, end_nt[0]);
  116. for (int i = 0; i < TERM_CT - 2; i++) {
  117. log_info ("Test encoding term #%d of %d.", i, TERM_CT - 2);
  118. EXPECT_PASS (codec.encode_term (terms[i], NULL, &out));
  119. EXPECT_STR_EQ (out, end_nt[i]);
  120. }
  121. EXPECT_INT_EQ (codec.encode_term (terms[8], NULL, &out), LSUP_VALUE_ERR);
  122. ASSERT (out == NULL, "Encoding of undefined term should be NULL!");
  123. EXPECT_INT_EQ (codec.encode_term (terms[9], NULL, &out), LSUP_VALUE_ERR);
  124. ASSERT (out == NULL, "Encoding of undefined term should be NULL!");
  125. free (out);
  126. LSUP_nsmap_free (nsm);
  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 (
  135. LSUP_iriref_new (NULL, NULL), LSUP_STORE_HTABLE, NULL, NULL, 0);
  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. LSUP_CodecIterator *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, (unsigned char **)&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. //printf("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. static 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. EXPECT_INT_EQ (ct, 8);
  181. EXPECT_INT_EQ (LSUP_graph_size (gr), 7);
  182. for (int i = 0; i < 7; i++) {
  183. log_info ("Checking triple #%d.", i);
  184. EXPECT_INT_EQ (LSUP_graph_contains (gr, trp + i), 1);
  185. }
  186. LSUP_graph_free (gr);
  187. return 0;
  188. }
  189. static int
  190. test_decode_nt_bad_graph()
  191. {
  192. FILE *input = fmemopen ((void *)bad_nt_doc, strlen (start_nt_doc), "r");
  193. LSUP_Graph *gr;
  194. size_t ct;
  195. char *err;
  196. EXPECT_INT_EQ (codec.decode_graph (input, &gr, &ct, &err), LSUP_VALUE_ERR);
  197. log_info ("Error: %s", err);
  198. ASSERT (strstr (err, "`dc:title") != NULL, "Wrong error string report!");
  199. ASSERT (strstr (err, "line 3") != NULL, "Wrong error line report!");
  200. ASSERT (strstr (err, "character 16") != NULL, "Wrong error char report!");
  201. free (err);
  202. fclose (input);
  203. LSUP_graph_free (gr);
  204. return 0;
  205. }
  206. int codec_nt_tests()
  207. {
  208. LSUP_Term **terms = init_terms();
  209. init_triples (terms);
  210. codec = nt_codec;
  211. RUN (test_encode_nt_term);
  212. RUN (test_encode_nt_graph);
  213. RUN (test_decode_nt_term);
  214. RUN (test_decode_nt_graph);
  215. RUN (test_decode_nt_bad_graph);
  216. free_terms(terms);
  217. return 0;
  218. }