test_codec_nt.c 7.4 KB

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