#include "test.h" #include "codec_nt.h" #define TERM_CT 10 typedef char nt_str[64]; 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", DEFAULT_DTYPE, "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; } // Starting NT term strings. They may have comments and junk whitespace. static nt_str start_nt[TERM_CT] = { "", " ", "\"hello\"^^http://www.w3.org/2001/XMLSchema#string", "\"hello\" @en-US", "\"hello\"@es-ES # Does \"Hello\" in Spanish mean \"hola\"?", "\"25\" ^^ ", "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"", "_:bn1", "This is nothing.", "", }; // End result NT term strings, as they should be produced by the NT codec. static nt_str end_nt[TERM_CT] = { "", "", "\"hello\"", "\"hello\"@en-US", "\"hello\"@es-ES", "\"25\"^^", "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"", "_:bn1", "", "", }; 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; // Test that passing a NS map has no effect. EXPECT_PASS (nt_codec.term_encoder (terms[0], nsm, &out)); EXPECT_STR_EQ (out, end_nt[0]); for (int i = 0; i < TERM_CT - 2; i++) { EXPECT_PASS (nt_codec.term_encoder (terms[i], NULL, &out)); EXPECT_STR_EQ (out, end_nt[i]); } 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] = { " \"hello\" .\n", " \"hello\"@es-ES .\n", " _:bn1 .\n", "_:bn1 \"hello\"@es-ES .\n", "_:bn1 " "\"25\"^^ .\n", "_:bn1 " "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\"" " .\n", }; for (int i = 0; i < 6; i++) ASSERT (strstr (out, test_out[i]) != NULL, test_out[i]); free (out); free_terms(terms); return 0; } static int test_decode_nt_term() { for (int i = 0; i < TERM_CT - 2; i++) { LSUP_Term *term; EXPECT_PASS (nt_codec.term_decoder (start_nt[i], NULL, &term)); } return 0; } int codec_nt_tests() { RUN (test_encode_nt_term); RUN (test_encode_nt_graph); RUN (test_decode_nt_term); return 0; }