#include "test.h" #include "codec/codec_nt.h" #define TERM_CT 11 #define TRP_CT 8 typedef char nt_str[64]; LSUP_Term ** init_terms (void) { LSUP_Term **terms = malloc (TERM_CT * sizeof (*terms)); terms[0] = LSUP_iriref_new("urn:local:s1", NULL); terms[1] = LSUP_iriref_new("http://example.org/p1", NULL); terms[2] = LSUP_iriref_new("http://example.org/p2", NULL); terms[3] = LSUP_literal_new ("hello", NULL); terms[4] = LSUP_lt_literal_new ("hello", "en-US"); terms[5] = LSUP_lt_literal_new ("hello", "es-ES"); terms[6] = LSUP_literal_new ( "25", LSUP_iriref_new ("http://www.w3.org/2001/XMLSchema#integer", NULL)); terms[7] = LSUP_literal_new ( "This \\is\\ a \"multi-line\"\n'literal'\t.", NULL); terms[8] = LSUP_bnode_new ("bn1"); terms[9] = LSUP_term_new (LSUP_TERM_UNDEFINED, "bogus", NULL); terms[10] = TERM_DUMMY; return terms; } // Starting NT term strings. They may have comments and junk whitespace. static nt_str start_nt[TERM_CT] = { "", " ", " ", "\"hello\"^^", "\"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", "", "", }; // Start NT document. It may have comments and junk whitespace. char *start_nt_doc = ( " \"hello\" . # Comment here.\n" " " "\"hello\"^^ .\n" " \"hello\"@en-US.\n" " \"hello\"@es-ES .\n" "# Some comments\n# To make it a bit \n #less boring.\n" " _:bn1 .\n" "_:bn1 \"hello\"@es-ES.\n" " _:bn1 " "\"25\"^^ .\n" "_:bn1 " "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\"" " .\n" "# FREE WHITESPACE!\n\n\n\n\n\n\n \n\n\n\n" ); char *bad_nt_doc = ( " \"hello\" . # Comment here.\n" " \"hello\"@es-ES .\n" " @ \"Bad Data.\" ." ); // End result NT document as it should be produced by the NT codec. // Lines should not be checked in strict order. char *end_nt_doc[7] = { " \"hello\" .\n", " \"hello\"@en-US .\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", }; static LSUP_Triple *trp[TRP_CT]; void init_triples (LSUP_Term **terms) { trp[0] = LSUP_triple_new (terms[0], terms[1], terms[3]); trp[1] = LSUP_triple_new (terms[0], terms[2], terms[4]); trp[2] = LSUP_triple_new (terms[0], terms[2], terms[5]); trp[3] = LSUP_triple_new (terms[0], terms[2], terms[8]); trp[4] = LSUP_triple_new (terms[8], terms[1], terms[5]); trp[5] = LSUP_triple_new (terms[8], terms[1], terms[6]); trp[6] = LSUP_triple_new (terms[8], terms[1], terms[7]); trp[7] = NULL; } void free_terms (LSUP_Term **terms) { for (int i = 0; i < TERM_CT; i++) LSUP_term_free (terms[i]); free (terms); } 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 (codec.encode_term (terms[0], nsm, &out)); EXPECT_STR_EQ (out, end_nt[0]); for (int i = 0; i < TERM_CT - 2; i++) { log_info ("Test encoding term #%d of %d.", i, TERM_CT - 2); EXPECT_PASS (codec.encode_term (terms[i], NULL, &out)); EXPECT_STR_EQ (out, end_nt[i]); } EXPECT_INT_EQ (codec.encode_term (terms[9], NULL, &out), LSUP_PARSE_ERR); EXPECT_INT_EQ (codec.encode_term (terms[10], NULL, &out), LSUP_PARSE_ERR); free (out); LSUP_nsmap_free (nsm); free_terms (terms); return 0; } int test_encode_nt_graph() { log_info ("Test encoding graph document."); LSUP_Graph *gr = LSUP_graph_new (NULL, NULL, NULL); if (!gr) return LSUP_MEM_ERR; size_t ins; LSUP_graph_add (gr, trp, &ins); char *out = calloc (1, 1); void *it = codec.encode_graph_init (gr); ASSERT (it != NULL, "Error creating codec iterator!"); char *tmp = NULL; LSUP_rc rc; while ((rc = codec.encode_graph_iter (it, &tmp)) != LSUP_END) { ASSERT (rc >= 0, "Encoding step failed!"); out = realloc (out, strlen(out) + strlen (tmp) + 1); out = strcat (out, tmp); } codec.encode_graph_done (it); free (tmp); LSUP_graph_free (gr); //log_info ("Serialized graph: %s\n", out); for (int i = 0; i < 7; i++) ASSERT (strstr (out, end_nt_doc[i]) != NULL, end_nt_doc[i]); free (out); return 0; } static int test_decode_nt_term() { log_info ("Test decoding terms."); for (int i = 0; i < TERM_CT - 2; i++) { log_debug ("Decoding term %d/%d.", i, TERM_CT - 3); LSUP_Term *term; EXPECT_PASS (codec.decode_term (start_nt[i], NULL, &term)); LSUP_term_free (term); } return 0; } int test_decode_nt_graph() { FILE *input = fmemopen ((void *)start_nt_doc, strlen (start_nt_doc), "r"); LSUP_Graph *gr; size_t ct; char *err; EXPECT_PASS (codec.decode_graph (input, &gr, &ct, &err)); fclose (input); ASSERT (err == NULL, "Error string is not NULL!"); #if 1 // For debugging: dump the decoded graph into NT. char *out = calloc (1, 1); char *tmp = NULL; void *it = codec.encode_graph_init (gr); while (codec.encode_graph_iter (it, &tmp) != LSUP_END) { out = realloc (out, strlen(out) + strlen (tmp) + 1); out = strcat (out, tmp); } codec.encode_graph_done (it); free (tmp); log_trace ("Serialized graph: \n%s", out); free (out); #endif EXPECT_INT_EQ (ct, 8); EXPECT_INT_EQ (LSUP_graph_size (gr), 7); for (int i = 0; i < 7; i++) { log_info ("Checking triple #%d.", i); EXPECT_INT_EQ (LSUP_graph_contains (gr, trp[i]), 1); } LSUP_graph_free (gr); return 0; } int test_decode_nt_bad_graph() { log_info ("testing illegal NT document."); FILE *input = fmemopen ((void *)bad_nt_doc, strlen (start_nt_doc), "r"); LSUP_Graph *gr; size_t ct; char *err; LSUP_rc rc = codec.decode_graph (input, &gr, &ct, &err); EXPECT_INT_EQ (rc, LSUP_PARSE_ERR); log_info ("Error: %s", err); ASSERT (strstr (err, "`@ \"Bad Data.\"") != NULL, "Wrong error string report!"); ASSERT (strstr (err, "line 3") != NULL, "Wrong error line report!"); ASSERT (strstr (err, "character 16") != NULL, "Wrong error char report!"); free (err); fclose (input); LSUP_graph_free (gr); return 0; } int codec_nt_tests() { LSUP_Term **terms = init_terms(); init_triples (terms); codec = nt_codec; RUN (test_encode_nt_term); RUN (test_encode_nt_graph); RUN (test_decode_nt_term); RUN (test_decode_nt_graph); RUN (test_decode_nt_bad_graph); free_terms (terms); for (int i = 0; i < TRP_CT; i++) free (trp[i]); return 0; }