test_codec_nt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. static void
  53. free_terms (LSUP_Term **terms)
  54. {
  55. for (int i = 0; i < TERM_CT; i++)
  56. LSUP_term_free (terms[i]);
  57. free (terms);
  58. }
  59. static int
  60. test_encode_nt_term()
  61. {
  62. LSUP_Term **terms = init_terms();
  63. LSUP_NSMap *nsm = LSUP_nsmap_new();
  64. LSUP_nsmap_add (nsm, "local", "urn:local:");
  65. LSUP_nsmap_add (nsm, "ext", "http://example.org");
  66. char *out = NULL;
  67. // Test that passing a NS map has no effect.
  68. EXPECT_PASS (nt_codec.term_encoder (terms[0], nsm, &out));
  69. EXPECT_STR_EQ (out, end_nt[0]);
  70. for (int i = 0; i < TERM_CT - 2; i++) {
  71. EXPECT_PASS (nt_codec.term_encoder (terms[i], NULL, &out));
  72. EXPECT_STR_EQ (out, end_nt[i]);
  73. }
  74. EXPECT_INT_EQ (nt_codec.term_encoder (terms[8], NULL, &out), LSUP_VALUE_ERR);
  75. ASSERT (out == NULL, "Encoding of undefined term should be NULL!");
  76. EXPECT_INT_EQ (nt_codec.term_encoder (terms[9], NULL, &out), LSUP_VALUE_ERR);
  77. ASSERT (out == NULL, "Encoding of undefined term should be NULL!");
  78. free (out);
  79. LSUP_nsmap_free (nsm);
  80. free_terms(terms);
  81. return 0;
  82. }
  83. static int test_encode_nt_graph()
  84. {
  85. LSUP_Term **terms = init_terms();
  86. LSUP_Graph *gr = LSUP_graph_new (LSUP_STORE_MEM);
  87. if (!gr) return LSUP_MEM_ERR;
  88. LSUP_Triple trp[8];
  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. size_t ins;
  98. LSUP_graph_add_trp (gr, trp, &ins);
  99. char *out = calloc (1, 1);
  100. LSUP_CodecIterator *it = nt_codec.gr_encode_init (gr);
  101. ASSERT (it != NULL, "Error creating codec iterator!");
  102. char *tmp = NULL;
  103. LSUP_rc rc;
  104. while ((rc = nt_codec.gr_encode_iter (it, (void**)&tmp)) != LSUP_END) {
  105. ASSERT (rc >= 0, "Encoding step failed!");
  106. out = realloc (out, strlen(out) + strlen (tmp) + 1);
  107. out = strcat (out, tmp);
  108. }
  109. nt_codec.gr_encode_done (it);
  110. free (tmp);
  111. LSUP_graph_free (gr);
  112. //printf("Serialized graph: %s\n", out);
  113. // Triples must not be checked in strict order.
  114. char *test_out[6] = {
  115. "<urn:local:s1> <http://example.org/p1> \"hello\" .\n",
  116. "<urn:local:s1> <http://example.org/p1> \"hello\"@es-ES .\n",
  117. "<urn:local:s1> <http://example.org/p1> _:bn1 .\n",
  118. "_:bn1 <http://example.org/p1> \"hello\"@es-ES .\n",
  119. "_:bn1 <http://example.org/p1> "
  120. "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n",
  121. "_:bn1 <http://example.org/p1> "
  122. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\""
  123. " .\n",
  124. };
  125. for (int i = 0; i < 6; i++)
  126. ASSERT (strstr (out, test_out[i]) != NULL, test_out[i]);
  127. free (out);
  128. free_terms(terms);
  129. return 0;
  130. }
  131. static int
  132. test_decode_nt_term()
  133. {
  134. for (int i = 0; i < TERM_CT - 2; i++) {
  135. LSUP_Term *term;
  136. EXPECT_PASS (nt_codec.term_decoder (start_nt[i], NULL, &term));
  137. }
  138. return 0;
  139. }
  140. int codec_nt_tests()
  141. {
  142. RUN (test_encode_nt_term);
  143. RUN (test_encode_nt_graph);
  144. RUN (test_decode_nt_term);
  145. return 0;
  146. }