#include "test.h"
#include "codec/codec_nt.h"

#define TERM_CT 10

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_literal_new ("hello", NULL);
    terms[3] = LSUP_lt_literal_new ("hello", "en-US");
    terms[4] = LSUP_lt_literal_new ("hello", "es-ES");
    terms[5] = LSUP_literal_new (
            "25",
            LSUP_iriref_new ("http://www.w3.org/2001/XMLSchema#integer", NULL));
    terms[6] = LSUP_literal_new (
            "This \\is\\ a \"multi-line\"\n'literal'\t.", NULL);
    terms[7] = LSUP_bnode_new ("bn1");
    terms[8] = LSUP_term_new (LSUP_TERM_UNDEFINED, "bogus", 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] = {
    "<urn:local:s1>",
    "<http://example.org/p1> ",
    "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string>",
    "\"hello\" @en-US",
    "\"hello\"@es-ES   # Does \"Hello\" in Spanish mean \"hola\"?",
    "\"25\"   ^^<http://www.w3.org/2001/XMLSchema#integer>  ",
    "\"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] = {
    "<urn:local:s1>",
    "<http://example.org/p1>",
    "\"hello\"",
    "\"hello\"@en-US",
    "\"hello\"@es-ES",
    "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer>",
    "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"",
    "_:bn1",
    "",
    "",
};

// Start NT document. It may have comments and junk whitespace.
char *start_nt_doc = (
    "<urn:local:s1> <http://example.org/p1> \"hello\" . #  Comment here.\n"
    "<urn:local:s1> <http://example.org/p1> "
        "\"hello\"^^<http://www.w3.org/2001/XMLSchema#string> .\n"
    "<urn:local:s1> <http://example.org/p1>\"hello\"@en-US.\n"
    "<urn:local:s1> <http://example.org/p1>\"hello\"@es-ES .\n"
    "# Some comments\n# To make it a bit \n   #less boring.\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"
    "# FREE WHITESPACE!\n\n\n\n\n\n\n      \n\n\n\n"
);


char *bad_nt_doc = (
    "<urn:local:s1> <http://example.org/p1> \"hello\" . #  Comment here.\n"
    "<urn:local:s1> <http://example.org/p1>\"hello\"@es-ES .\n"
    "<urn:local:s1> @ \"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] = {
    "<urn:local:s1> <http://example.org/p1> \"hello\" .\n",
    "<urn:local:s1> <http://example.org/p1> \"hello\"@en-US .\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",
};


static LSUP_Triple trp[8];


void
init_triples (LSUP_Term **terms)
{
    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]);
}


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[8], NULL, &out), LSUP_PARSE_ERR);

    EXPECT_INT_EQ (codec.encode_term (terms[9], 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 (
            LSUP_iriref_new (NULL, NULL), LSUP_STORE_HTABLE, NULL, NULL, 0);
    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);
    //printf("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);

    return 0;
}