浏览代码

Fix some tests.

Stefano Cossu 3 年之前
父节点
当前提交
fb86247594
共有 6 个文件被更改,包括 81 次插入41 次删除
  1. 20 8
      include/codec_base.h
  2. 2 1
      include/core.h
  3. 1 1
      src/codec/Makefile
  4. 2 2
      src/codec_nt.c
  5. 3 0
      src/term.c
  6. 53 29
      test/test_codec_nt.c

+ 20 - 8
include/codec_base.h

@@ -42,12 +42,6 @@ typedef LSUP_rc (*term_enc_fn_t)(
         const LSUP_Term *term, const LSUP_NSMap *nsm, char **rep);
 
 
-/** TODO
- */
-typedef LSUP_rc (*term_dec_fn_t)(
-        const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term);
-
-
 /** @brief Initialize a graph encoding loop.
  *
  * This prototype is to be implemented by graph encoding loops. It should
@@ -85,6 +79,24 @@ typedef LSUP_rc (*gr_codec_iter_fn_t)(LSUP_CodecIterator *it, void **res);
 typedef void (*gr_codec_done_fn_t)(LSUP_CodecIterator *it);
 
 
+/** @brief Prototype for decoding a string into a LSUP_Term.
+ *
+ * Implementations MAY ignore any other tokens after finding the first one.
+ *
+ * @param[in] rep NT representation of the term.
+ *
+ * @param[in] nsm Namespace map handle.
+ *
+ * @param[out] Pointer to the term handle to be created. Implementaions SHOULD
+ *  return NULL on a parse error.
+ *
+ * @return Implementations MUST return LSUP_OK on success and a negative value
+ *  on parsing error.
+ */
+typedef LSUP_rc (*term_decode_fn_t)(
+        const char *rep, const LSUP_NSMap *nsm, LSUP_Term **term);
+
+
 /** @brief Prototype for decoding a complete RDF document into a graph.
  *
  * Implementations SHOULD consume data from the file handle in chunks.
@@ -140,8 +152,8 @@ typedef struct codec_t {
     gr_codec_done_fn_t  gr_encode_done; // Graph encoder finalization.
 
     // Decoding.
-    term_dec_fn_t       term_decoder;   // Term decoder function.
-    gr_decode_fn_t      gr_decoder;      // Graph decoder function.
+    term_decode_fn_t    term_decoder;   // Term decoder function.
+    gr_decode_fn_t      gr_decoder;     // Graph decoder function.
 } LSUP_Codec;
 
 #endif

+ 2 - 1
include/core.h

@@ -132,7 +132,8 @@ inline uint16_t utf8_to_bytes (uint16_t c) {
  *
  * @param out - output buffer (min 5 characters), will be 0-terminated
  * @param utf - code point 0-0x10FFFF
- * @return number of bytes on success, 0 on failure (also produces U+FFFD, which uses 3 bytes)
+ * @return number of bytes on success, 0 on failure (also produces U+FFFD,
+ *  which uses 3 bytes)
  */
 inline int utf8_encode(const uint32_t utf, unsigned char *out)
 {

+ 1 - 1
src/codec/Makefile

@@ -12,5 +12,5 @@ $(parsers): %_parser.c: %_lexer.re %_grammar.c
 	$(LEXER) $< -o $@ -T --case-ranges
 
 %_grammar.c: %_grammar.y
-	$(PARSER) $< -c -T../../ext/lemon/lempar.c
+	$(PARSER) $< -c -q -T../../ext/lemon/lempar.c
 

+ 2 - 2
src/codec_nt.c

@@ -52,7 +52,7 @@ term_to_nt (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
             buf_len = strlen (escaped) + 3; // Room for "" and terminator
 
             if (term->datatype && strcmp (term->datatype, XSD_STRING) != 0)
-                buf_len += strlen (term->datatype) + 2; // Room for ^^
+                buf_len += strlen (term->datatype) + 4; // Room for ^^<>
 
             if (strlen (term->lang) > 0)
                 buf_len += strlen(term->lang) + 1; // Room for @
@@ -68,7 +68,7 @@ term_to_nt (const LSUP_Term *term, const LSUP_NSMap *nsm, char **out_p)
 
             // Always suppress xsd:string data type.
             if (term->datatype && strcmp (term->datatype, XSD_STRING) != 0)
-                out = strcat (strcat (out, "^^"), term->datatype);
+                out = strcat (strcat (strcat (out, "^^<"), term->datatype), ">");
 
             if (strlen (term->lang) > 0)
                 out = strcat (strcat (out, "@"), term->lang);

+ 3 - 0
src/term.c

@@ -113,6 +113,9 @@ LSUP_term_init(
     term->data = data_tmp;
     strcpy (term->data, data);
 
+    if (term->type == LSUP_TERM_LITERAL && !datatype)
+        datatype = DEFAULT_DTYPE;
+
     if (datatype) {
         data_tmp = realloc (term->datatype, strlen (datatype) + 1);
         if (UNLIKELY (!data_tmp)) return LSUP_MEM_ERR;

+ 53 - 29
test/test_codec_nt.c

@@ -3,6 +3,8 @@
 
 #define TERM_CT 10
 
+typedef char nt_str[64];
+
 static LSUP_Term **
 init_terms (void)
 {
@@ -12,8 +14,7 @@ init_terms (void)
     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",
-            "http://www.w3.org/2001/XMLSchema#string", "es-ES");
+            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);
@@ -27,6 +28,34 @@ init_terms (void)
     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",
+    "",
+    "",
+};
+
 
 static void
 free_terms (LSUP_Term **terms)
@@ -37,6 +66,7 @@ free_terms (LSUP_Term **terms)
     free (terms);
 }
 
+
 static int
 test_encode_nt_term()
 {
@@ -47,34 +77,15 @@ test_encode_nt_term()
     LSUP_nsmap_add (nsm, "ext", "http://example.org");
 
     char *out = NULL;
-    EXPECT_PASS (nt_codec.term_encoder (terms[0], NULL, &out));
-    EXPECT_STR_EQ (out, "<urn:local:s1>");
 
+    // Test that passing a NS map has no effect.
     EXPECT_PASS (nt_codec.term_encoder (terms[0], nsm, &out));
-    EXPECT_STR_EQ (out, "<urn:local:s1>");
-
-    EXPECT_PASS (nt_codec.term_encoder (terms[1], NULL, &out));
-    EXPECT_STR_EQ (out, "<http://example.org/p1>");
-
-    EXPECT_PASS (nt_codec.term_encoder (terms[2], NULL, &out));
-    EXPECT_STR_EQ (out, "\"hello\"");
-
-    EXPECT_PASS (nt_codec.term_encoder (terms[3], NULL, &out));
-    EXPECT_STR_EQ (out, "\"hello\"@en-US");
-
-    EXPECT_PASS (nt_codec.term_encoder (terms[4], NULL, &out));
-    EXPECT_STR_EQ (out, "\"hello\"@es-ES");
-
-    EXPECT_PASS (nt_codec.term_encoder (terms[5], NULL, &out));
-    EXPECT_STR_EQ (out, "\"25\"^^http://www.w3.org/2001/XMLSchema#integer");
-
-    EXPECT_PASS (nt_codec.term_encoder (terms[6], NULL, &out));
-    EXPECT_STR_EQ (
-            out,
-            "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\'literal\\'\\t.\"");
+    EXPECT_STR_EQ (out, end_nt[0]);
 
-    EXPECT_PASS (nt_codec.term_encoder (terms[7], NULL, &out));
-    EXPECT_STR_EQ (out, "_:bn1");
+    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!");
@@ -134,13 +145,13 @@ static int test_encode_nt_graph()
         "<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",
+            "\"25\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n",
         "_:bn1 <http://example.org/p1> "
             "\"This \\\\is\\\\ a \\\"multi-line\\\"\\n\\\'literal\\\'\\t.\""
             " .\n",
     };
     for (int i = 0; i < 6; i++)
-        ASSERT (strstr (out, test_out[i]) != NULL, "String not found!");
+        ASSERT (strstr (out, test_out[i]) != NULL, test_out[i]);
 
     free (out);
 
@@ -150,9 +161,22 @@ static int test_encode_nt_graph()
 }
 
 
+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;
 }