Pārlūkot izejas kodu

Fix test; realloc serialized fragments.

Stefano Cossu 2 gadi atpakaļ
vecāks
revīzija
7165f98d06
3 mainītis faili ar 21 papildinājumiem un 20 dzēšanām
  1. 15 13
      src/codec/codec_ttl.c
  2. 4 4
      test/test_codec_nt.c
  3. 2 3
      test/test_codec_ttl.c

+ 15 - 13
src/codec/codec_ttl.c

@@ -10,10 +10,13 @@
  */
 typedef struct {
     const LSUP_Codec *  codec;      ///< Codec that generated this iterator.
-    const LSUP_Graph    *gr;        ///< Graph being encoded.
+    const LSUP_Graph *  gr;        ///< Graph being encoded.
     LSUP_TermSet *      subjects;   ///< All subjects in the graph.
     size_t              s_cur;      ///< Term set cursor.
     LSUP_rc             rc;         ///< Internal return code.
+    char *              s_str;      ///< Serialized subject block (output).
+    char *              p_str;      ///< Serialized predicate block.
+    char *              o_str;      ///< Serialized object block.
 } LSUP_TTLCodecIterator;
 
 
@@ -172,13 +175,12 @@ static void *
 gr_to_ttl_init (const LSUP_Graph *gr)
 {
     LSUP_TTLCodecIterator *it;
-    MALLOC_GUARD (it, NULL);
+    CALLOC_GUARD (it, NULL);
 
     it->codec = &ttl_codec;
     it->gr = gr;
     it->subjects = LSUP_graph_unique_terms (gr, TRP_POS_S);
     // Sets the condition to build the prolog on 1st iteration.
-    it->s_cur = 0;
     it->rc = LSUP_NORESULT;
 
     return it;
@@ -249,36 +251,33 @@ gr_to_ttl_iter (void *h, char **res_p) {
 
     LSUP_LinkMapIterator *lmit = LSUP_link_map_iter_new (lmap, s);
     LSUP_Term *p = NULL;
-    char *p_str = NULL;
     LSUP_TermSet *o_ts = NULL;
     char *p_join = " ";
     // Begin predicate loop.
     while (LSUP_link_map_next (lmit, &p, &o_ts) != LSUP_END) {
         // Add predicate representation.
-        RCCK (term_to_ttl (p, LSUP_graph_namespace (it->gr), &p_str));
+        RCCK (term_to_ttl (p, LSUP_graph_namespace (it->gr), &it->p_str));
         char *tmp = realloc (
-                res, strlen (res) + strlen (p_str) + strlen (p_join) + 1);
+                res, strlen (res) + strlen (it->p_str) + strlen (p_join) + 1);
         if (UNLIKELY (!tmp)) goto memfail;
-        res = strcat (strcat (tmp, p_join), p_str);
+        res = strcat (strcat (tmp, p_join), it->p_str);
 
-        free (p_str);
         p_join = " ; ";
 
         // Add objects for predicate.
         size_t i = 0;
         LSUP_Term *o = NULL;
-        char *o_str = NULL;
         char *o_join = " ";
         while (LSUP_term_set_next (o_ts, &i, &o) != LSUP_END) {
-            it->rc = term_to_ttl (o, LSUP_graph_namespace (it->gr), &o_str);
+            it->rc = term_to_ttl (
+                    o, LSUP_graph_namespace (it->gr), &it->o_str);
             RCCK (it->rc);
             char *tmp = realloc (
-                    res, strlen (res) + strlen (o_str) + strlen (o_join) + 1);
+                    res, strlen (res) + strlen (it->o_str) + strlen (o_join) + 1);
             if (UNLIKELY (!tmp)) goto memfail;
-            res = strcat (strcat (tmp, o_join), o_str);
+            res = strcat (strcat (tmp, o_join), it->o_str);
             o_join = " , ";
         }
-        free (o_str);
     }
 
     char *s_sep = " .\n";
@@ -304,6 +303,9 @@ gr_to_ttl_done (void *h)
 {
     LSUP_TTLCodecIterator *it = h;
     LSUP_term_set_free (it->subjects);
+    free (it->s_str);
+    free (it->p_str);
+    free (it->o_str);
     free (it);
 }
 

+ 4 - 4
test/test_codec_nt.c

@@ -111,9 +111,9 @@ init_triples (LSUP_Term **terms)
     LSUP_triple_init (trp + 1, terms[0], terms[2], terms[4]);
     LSUP_triple_init (trp + 2, terms[0], terms[2], terms[5]);
     LSUP_triple_init (trp + 3, terms[0], terms[2], terms[8]);
-    LSUP_triple_init (trp + 4, terms[7], terms[1], terms[5]);
-    LSUP_triple_init (trp + 5, terms[7], terms[1], terms[6]);
-    LSUP_triple_init (trp + 6, terms[7], terms[1], terms[7]);
+    LSUP_triple_init (trp + 4, terms[8], terms[1], terms[5]);
+    LSUP_triple_init (trp + 5, terms[8], terms[1], terms[6]);
+    LSUP_triple_init (trp + 6, terms[8], terms[1], terms[7]);
 }
 
 
@@ -185,7 +185,7 @@ test_encode_nt_graph()
     codec.encode_graph_done (it);
     free (tmp);
     LSUP_graph_free (gr);
-    //printf("Serialized graph: %s\n", out);
+    //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]);

+ 2 - 3
test/test_codec_ttl.c

@@ -1,7 +1,6 @@
 #include "test.h"
 #include "codec/codec_ttl.h"
 
-#define TERM_CT 10
 #define W3C_POS_TEST_CT 30
 #define W3C_NEG_TEST_CT 14
 #define W3C_TEST_BASE "http://www.w3.org/2001/sw/DataAccess/df1/tests/"
@@ -29,12 +28,12 @@ test_encode_ttl_graph()
     char *tmp = NULL;
     LSUP_rc rc;
     while ((rc = codec.encode_graph_iter (it, &tmp)) != LSUP_END) {
-        printf ("Serialized fragment: %s\n", tmp);
+        log_info ("Serialized fragment: %s\n", tmp);
         ASSERT (rc >= 0, "Encoding step failed!");
         out = realloc (out, strlen(out) + strlen (tmp) + 1);
         out = strcat (out, tmp);
     }
-    printf ("Serialized graph: %s\n", out);
+    log_info ("Serialized graph: %s\n", out);
     codec.encode_graph_done (it);
     free (tmp);
     LSUP_graph_free (gr);