test_codec_nt.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "test.h"
  2. #include "codec_nt.h"
  3. #define TERM_CT 10
  4. static LSUP_Term **
  5. init_terms (void)
  6. {
  7. LSUP_Term **terms = malloc (TERM_CT * sizeof (*terms));
  8. terms[0] = LSUP_uri_new ("urn:local:s1");
  9. terms[1] = LSUP_uri_new ("http://example.org/p1");
  10. terms[2] = LSUP_term_new (LSUP_TERM_LITERAL, "hello", NULL, NULL);
  11. terms[3] = LSUP_term_new (LSUP_TERM_LITERAL, "hello", NULL, "en-US");
  12. terms[4] = LSUP_term_new (
  13. LSUP_TERM_LITERAL, "hello",
  14. "http://www.w3.org/2001/XMLSchema#string", "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. static void
  27. free_terms (LSUP_Term **terms)
  28. {
  29. for (int i = 0; i < TERM_CT; i++)
  30. LSUP_term_free (terms[i]);
  31. free (terms);
  32. }
  33. static int
  34. test_encode_nt_term()
  35. {
  36. LSUP_Term **terms = init_terms();
  37. LSUP_NSMap *nsm = LSUP_nsmap_new();
  38. LSUP_nsmap_add (nsm, "local", "urn:local:");
  39. LSUP_nsmap_add (nsm, "ext", "http://example.org");
  40. char *out = NULL;
  41. EXPECT_PASS (nt_codec.term_encoder (terms[0], NULL, &out));
  42. EXPECT_STR_EQ (out, "<urn:local:s1>");
  43. EXPECT_PASS (nt_codec.term_encoder (terms[0], nsm, &out));
  44. EXPECT_STR_EQ (out, "<urn:local:s1>");
  45. EXPECT_PASS (nt_codec.term_encoder (terms[1], NULL, &out));
  46. EXPECT_STR_EQ (out, "<http://example.org/p1>");
  47. EXPECT_PASS (nt_codec.term_encoder (terms[2], NULL, &out));
  48. EXPECT_STR_EQ (out, "\"hello\"");
  49. EXPECT_PASS (nt_codec.term_encoder (terms[3], NULL, &out));
  50. EXPECT_STR_EQ (out, "\"hello\"@en-US");
  51. EXPECT_PASS (nt_codec.term_encoder (terms[4], NULL, &out));
  52. EXPECT_STR_EQ (out, "\"hello\"@es-ES");
  53. EXPECT_PASS (nt_codec.term_encoder (terms[5], NULL, &out));
  54. EXPECT_STR_EQ (out, "\"25\"^^http://www.w3.org/2001/XMLSchema#integer");
  55. EXPECT_PASS (nt_codec.term_encoder (terms[6], NULL, &out));
  56. EXPECT_STR_EQ (
  57. out,
  58. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"");
  59. EXPECT_PASS (nt_codec.term_encoder (terms[7], NULL, &out));
  60. EXPECT_STR_EQ (out, "_:bn1");
  61. EXPECT_INT_EQ (nt_codec.term_encoder (terms[8], NULL, &out), LSUP_VALUE_ERR);
  62. ASSERT (out == NULL, "Encoding of undefined term should be NULL!");
  63. EXPECT_INT_EQ (nt_codec.term_encoder (terms[9], NULL, &out), LSUP_VALUE_ERR);
  64. ASSERT (out == NULL, "Encoding of undefined term should be NULL!");
  65. free (out);
  66. LSUP_nsmap_free (nsm);
  67. free_terms(terms);
  68. return 0;
  69. }
  70. static int test_encode_nt_graph()
  71. {
  72. LSUP_Term **terms = init_terms();
  73. LSUP_Graph *gr = LSUP_graph_new (LSUP_STORE_MEM);
  74. if (!gr) return LSUP_MEM_ERR;
  75. LSUP_Triple trp[8];
  76. memset (trp, 0, sizeof (LSUP_Triple) * 8); // Last NULL is a sentinel
  77. LSUP_triple_init (trp + 0, terms[0], terms[1], terms[2]);
  78. LSUP_triple_init (trp + 1, terms[0], terms[1], terms[3]);
  79. LSUP_triple_init (trp + 2, terms[0], terms[1], terms[4]);
  80. LSUP_triple_init (trp + 3, terms[0], terms[1], terms[7]);
  81. LSUP_triple_init (trp + 4, terms[7], terms[1], terms[4]);
  82. LSUP_triple_init (trp + 5, terms[7], terms[1], terms[5]);
  83. LSUP_triple_init (trp + 6, terms[7], terms[1], terms[6]);
  84. size_t ins;
  85. LSUP_graph_add_trp (gr, trp, &ins);
  86. char *out = calloc (1, 1);
  87. LSUP_CodecIterator *it = nt_codec.gr_encode_init (gr);
  88. ASSERT (it != NULL, "Error creating codec iterator!");
  89. char *tmp = NULL;
  90. LSUP_rc rc;
  91. while ((rc = nt_codec.gr_encode_iter (it, (void**)&tmp)) != LSUP_END) {
  92. ASSERT (rc >= 0, "Encoding step failed!");
  93. out = realloc (out, strlen(out) + strlen (tmp) + 1);
  94. out = strcat (out, tmp);
  95. }
  96. nt_codec.gr_encode_done (it);
  97. free (tmp);
  98. LSUP_graph_free (gr);
  99. //printf("Serialized graph: %s\n", out);
  100. // Triples must not be checked in strict order.
  101. char *test_out[6] = {
  102. "<urn:local:s1> <http://example.org/p1> \"hello\" .\n",
  103. "<urn:local:s1> <http://example.org/p1> \"hello\"@es-ES .\n",
  104. "<urn:local:s1> <http://example.org/p1> _:bn1 .\n",
  105. "_:bn1 <http://example.org/p1> \"hello\"@es-ES .\n",
  106. "_:bn1 <http://example.org/p1> "
  107. "\"25\"^^http://www.w3.org/2001/XMLSchema#integer .\n",
  108. "_:bn1 <http://example.org/p1> "
  109. "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\""
  110. " .\n",
  111. };
  112. for (int i = 0; i < 6; i++)
  113. ASSERT (strstr (out, test_out[i]) != NULL, "String not found!");
  114. free (out);
  115. free_terms(terms);
  116. return 0;
  117. }
  118. int codec_nt_tests()
  119. {
  120. RUN (test_encode_nt_term);
  121. RUN (test_encode_nt_graph);
  122. return 0;
  123. }