123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #include "test.h"
- #include "codec_nt.h"
- #define TERM_CT 10
- static LSUP_Term **
- init_terms (void)
- {
- LSUP_Term **terms = malloc (TERM_CT * sizeof (*terms));
- terms[0] = LSUP_uri_new ("urn:local:s1");
- terms[1] = LSUP_uri_new ("http://example.org/p1");
- terms[2] = LSUP_term_new (LSUP_TERM_LITERAL, "hello", NULL, NULL);
- terms[3] = LSUP_term_new (LSUP_TERM_LITERAL, "hello", NULL, "en-US");
- terms[4] = LSUP_term_new (
- LSUP_TERM_LITERAL, "hello",
- "http://www.w3.org/2001/XMLSchema#string", "es-ES");
- terms[5] = LSUP_term_new (
- LSUP_TERM_LITERAL, "25",
- "http://www.w3.org/2001/XMLSchema#integer", NULL);
- terms[6] = LSUP_term_new (
- LSUP_TERM_LITERAL, "This \\is\\ a \"multi-line\"\n'literal'\t.",
- NULL, NULL);
- terms[7] = LSUP_term_new (LSUP_TERM_BNODE, "bn1", NULL, NULL);
- terms[8] = LSUP_term_new (LSUP_TERM_UNDEFINED, "bogus", NULL, NULL);
- terms[9] = TERM_DUMMY;
- return terms;
- }
- static void
- free_terms (LSUP_Term **terms)
- {
- for (int i = 0; i < TERM_CT; i++)
- LSUP_term_free (terms[i]);
- free (terms);
- }
- static int
- test_encode_nt_term()
- {
- LSUP_Term **terms = init_terms();
- LSUP_NSMap *nsm = LSUP_nsmap_new();
- LSUP_nsmap_add (nsm, "local", "urn:local:");
- LSUP_nsmap_add (nsm, "ext", "http://example.org");
- char *out = NULL;
- EXPECT_PASS (nt_codec.term_encoder (terms[0], NULL, &out));
- EXPECT_STR_EQ (out, "<urn:local:s1>");
- EXPECT_PASS (nt_codec.term_encoder (terms[0], nsm, &out));
- EXPECT_STR_EQ (out, "<urn:local:s1>");
- EXPECT_PASS (nt_codec.term_encoder (terms[1], NULL, &out));
- EXPECT_STR_EQ (out, "<http://example.org/p1>");
- EXPECT_PASS (nt_codec.term_encoder (terms[2], NULL, &out));
- EXPECT_STR_EQ (out, "\"hello\"");
- EXPECT_PASS (nt_codec.term_encoder (terms[3], NULL, &out));
- EXPECT_STR_EQ (out, "\"hello\"@en-US");
- EXPECT_PASS (nt_codec.term_encoder (terms[4], NULL, &out));
- EXPECT_STR_EQ (out, "\"hello\"@es-ES");
- EXPECT_PASS (nt_codec.term_encoder (terms[5], NULL, &out));
- EXPECT_STR_EQ (out, "\"25\"^^http://www.w3.org/2001/XMLSchema#integer");
- EXPECT_PASS (nt_codec.term_encoder (terms[6], NULL, &out));
- EXPECT_STR_EQ (
- out,
- "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"");
- EXPECT_PASS (nt_codec.term_encoder (terms[7], NULL, &out));
- EXPECT_STR_EQ (out, "_:bn1");
- EXPECT_INT_EQ (nt_codec.term_encoder (terms[8], NULL, &out), LSUP_VALUE_ERR);
- ASSERT (out == NULL, "Encoding of undefined term should be NULL!");
- EXPECT_INT_EQ (nt_codec.term_encoder (terms[9], NULL, &out), LSUP_VALUE_ERR);
- ASSERT (out == NULL, "Encoding of undefined term should be NULL!");
- free (out);
- LSUP_nsmap_free (nsm);
- free_terms(terms);
- return 0;
- }
- static int test_encode_nt_graph()
- {
- LSUP_Term **terms = init_terms();
- LSUP_Graph *gr = LSUP_graph_new (LSUP_STORE_MEM);
- if (!gr) return LSUP_MEM_ERR;
- LSUP_Triple trp[8];
- memset (trp, 0, sizeof (LSUP_Triple) * 8); // Last NULL is a sentinel
- LSUP_triple_init (trp + 0, terms[0], terms[1], terms[2]);
- LSUP_triple_init (trp + 1, terms[0], terms[1], terms[3]);
- LSUP_triple_init (trp + 2, terms[0], terms[1], terms[4]);
- LSUP_triple_init (trp + 3, terms[0], terms[1], terms[7]);
- LSUP_triple_init (trp + 4, terms[7], terms[1], terms[4]);
- LSUP_triple_init (trp + 5, terms[7], terms[1], terms[5]);
- LSUP_triple_init (trp + 6, terms[7], terms[1], terms[6]);
- size_t ins;
- LSUP_graph_add_trp (gr, trp, &ins);
- char *out = calloc (1, 1);
- LSUP_CodecIterator *it = nt_codec.gr_encode_init (gr);
- ASSERT (it != NULL, "Error creating codec iterator!");
- char *tmp = NULL;
- LSUP_rc rc;
- while ((rc = nt_codec.gr_encode_iter (it, (void**)&tmp)) != LSUP_END) {
- ASSERT (rc >= 0, "Encoding step failed!");
- out = realloc (out, strlen(out) + strlen (tmp) + 1);
- out = strcat (out, tmp);
- }
- nt_codec.gr_encode_done (it);
- free (tmp);
- LSUP_graph_free (gr);
- //printf("Serialized graph: %s\n", out);
- // Triples must not be checked in strict order.
- char *test_out[6] = {
- "<urn:local:s1> <http://example.org/p1> \"hello\" .\n",
- "<urn:local:s1> <http://example.org/p1> \"hello\"@es-ES .\n",
- "<urn:local:s1> <http://example.org/p1> _:bn1 .\n",
- "_:bn1 <http://example.org/p1> \"hello\"@es-ES .\n",
- "_:bn1 <http://example.org/p1> "
- "\"25\"^^http://www.w3.org/2001/XMLSchema#integer .\n",
- "_:bn1 <http://example.org/p1> "
- "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\""
- " .\n",
- };
- for (int i = 0; i < 6; i++)
- ASSERT (strstr (out, test_out[i]) != NULL, "String not found!");
- free (out);
- free_terms(terms);
- return 0;
- }
- int codec_nt_tests()
- {
- RUN (test_encode_nt_term);
- RUN (test_encode_nt_graph);
- return 0;
- }
|