#ifndef _TEST_ASSETS_H
#define _TEST_ASSETS_H

#include "term.h"

#define NUM_TRP 10

LSUP_Triple *create_triples()
{
    LSUP_NSMap *nsm = LSUP_nsmap_new();
    LSUP_nsmap_add (nsm, "ns1", "urn:s:");
    LSUP_nsmap_add (nsm, "ns2", "urn:p:");

    LSUP_Triple *trp;
    // Leave 1 spare NULL as a sentinel
    trp = calloc (NUM_TRP + 1, sizeof (LSUP_Triple));
    if (!trp) abort();

    trp[0].s = LSUP_iriref_new("urn:s:0", NULL);
    trp[0].p = LSUP_iriref_new("urn:p:0", NULL);
    trp[0].o = LSUP_iriref_new("urn:o:0", NULL);

    trp[1].s = LSUP_iriref_new("urn:s:1", NULL);
    trp[1].p = LSUP_iriref_new("urn:p:1", NULL);
    trp[1].o = LSUP_iriref_new("urn:o:1", NULL);

    trp[2].s = LSUP_iriref_new("urn:s:2", NULL);
    trp[2].p = LSUP_iriref_new("urn:p:2", NULL);
    trp[2].o = LSUP_iriref_new("urn:o:2", NULL);

    trp[3].s = LSUP_iriref_new("urn:s:0", NULL);
    trp[3].p = LSUP_iriref_new("urn:p:1", NULL);
    trp[3].o = LSUP_iriref_new("urn:o:2", NULL);

    trp[4].s = LSUP_iriref_new("urn:s:0", NULL);
    trp[4].p = LSUP_iriref_new("ns2:2", nsm);
    trp[4].o = LSUP_literal_new ("String 1", NULL);

    trp[5].s = LSUP_iriref_new("ns1:0", nsm);
    trp[5].p = LSUP_iriref_new("urn:p:5", NULL);
    trp[5].o = LSUP_literal_new(
            "String 1", LSUP_iriref_new ("urn:mydatatype:string", NULL));

    trp[6].s = LSUP_iriref_new("urn:s:1", NULL);
    trp[6].p = LSUP_iriref_new("urn:p:6", NULL);
    trp[6].o = LSUP_lt_literal_new("String 1", "es-ES");

    // Unique triple from reused pointers. Do not double-free.
    trp[7].s = trp[0].s; // <urn:s:0>
    trp[7].p = trp[2].p; // <urn:p:2>
    trp[7].o = trp[5].o; // "String 1"^^xsd:string

    // Duplicate of trp[7]. Do not double-free.
    trp[8].s = trp[0].s;
    trp[8].p = trp[2].p;
    trp[8].o = trp[5].o;

    // Duplicate of trp[7] from different term type but semantically identical.
    // Do not double-free.
    trp[9].s = trp[5].s;
    trp[9].p = trp[4].p;
    trp[9].o = trp[5].o;

    return trp;
}


void free_triples (LSUP_Triple *trp)
{
    LSUP_nsmap_free (LSUP_iriref_nsm (trp[4].p));

    // Last three triples are second pointers.
    for (int i=0; i < NUM_TRP - 3; i++) {
        LSUP_term_free (trp[i].s);
        LSUP_term_free (trp[i].p);
        LSUP_term_free (trp[i].o);
    }

    free (trp);
}
#endif  /* _TEST_ASSETS_H */