test_codec_nt.c 7.7 KB

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