Repository: SINTEF/dlite (language: python)

Issue:
FIX: for assertion error and add unit tests
in PR: https://github.com/SINTEF/dlite/pull/541
which was a temporary fix for the assertion error: _python: /project/src/dlite-json.c:306: dlite_json_asprint: Assertion `0 <= m && m < (int)*size' failed._
 Added one more byte to the allocated buffer than expected, which seems to fix the assertion. Requiring this extra byte is unexpected. It seems to be caused by dlite_json_sprint() reporting one byte too little when called with size=0 for a collection with relations! 

- [ ] Create a proper fix for this issue!
- [ ]  add unit tests


Discussion / hints:

Fix patch:
diff --git a/src/dlite-json.c b/src/dlite-json.c
index a7ea61a06..2dbdad2b8 100644
--- a/src/dlite-json.c
+++ b/src/dlite-json.c
@@ -343,6 +343,9 @@ int dlite_json_asprint(char **dest, size_t *size, size_t pos,
   // If `inst` is a collection with relations, then
   // dlite_json_sprint() seems to report one byte too little when
   // called with size=0.
+  //
+  // Update: Added dedicated test for collection to test_json.c.
+  // That test is not able to reproduce this error.
   newsize = m + pos + 2;
   if (!(q = realloc(*dest, newsize)))
     return err(dliteMemoryError, "allocation failure");


Test patch:
diff --git a/src/tests/test-collection.json b/src/tests/test-collection.json
new file mode 100644
index 000000000..ea9ac1c9c
--- /dev/null
+++ b/src/tests/test-collection.json
@@ -0,0 +1,33 @@
+{
+  "e076a856-e36e-5335-967e-2f2fd153c17d":   {
+    "uri": "my_test_instance",
+    "meta": "http://onto-ns.com/meta/0.1/test-entity",
+    "dimensions": {
+      "L": 2,
+      "M": 1,
+      "N": 3
+    },
+    "properties": {
+      "myblob": "0a0010",
+      "mydouble": 3.14,
+      "myfixstring": "Al",
+      "mystring": "allocated string...",
+      "myshort": 17,
+      "myarray": [[[1, 2, 3]], [[4, 5, 6]]]
+    }
+  },
+  "58432e52-ee57-43b0-9daf-ef37e696da25":   {
+    "meta": "http://onto-ns.com/meta/0.1/Collection",
+    "dimensions": {
+      "nrelations": 4
+    },
+    "properties": {
+      "relations": [
+        ["inst", "_is-a", "Instance"],
+        ["inst", "_has-uuid", "e076a856-e36e-5335-967e-2f2fd153c17d"],
+        ["inst", "_has-meta", "http://onto-ns.com/meta/0.1/test-entity"],
+        ["inst", "rdfs:subClassOf", "dcat:Dataset"]
+      ]
+    }
+  }
+}
diff --git a/src/tests/test_json.c b/src/tests/test_json.c
index f688af892..e67f98c84 100644
--- a/src/tests/test_json.c
+++ b/src/tests/test_json.c
@@ -12,7 +12,7 @@
 #define THISDIR STRINGIFY(dlite_SOURCE_DIR) "/src/tests/"
 #define PREFIX  "json://" THISDIR
 
-DLiteInstance *inst=NULL;
+DLiteInstance *inst=NULL, *coll=NULL;
 DLiteMeta *meta=NULL;
 
 
@@ -26,6 +26,10 @@ MU_TEST(test_load)
   url = PREFIX "test-data.json?mode=r#e076a856-e36e-5335-967e-2f2fd153c17d";
   inst = dlite_instance_load_url(url);
   mu_check(inst);
+
+  url = PREFIX "test-collection.json?mode=r#58432e52-ee57-43b0-9daf-ef37e696da25";
+  coll = dlite_instance_load_url(url);
+  mu_check(coll);
 }
 
 
@@ -77,8 +81,6 @@ MU_TEST(test_sprint)
   //printf("%s\n", buf);
   mu_assert_int_eq(1165, m);
 
-
-
   /* Tests for PR #541 */
   m = dlite_json_sprint(buf, 0, inst, 4, dliteJsonSingle);
   mu_assert_int_eq(404, m);
@@ -86,7 +88,15 @@ MU_TEST(test_sprint)
   m = dlite_json_sprint(NULL, 0, inst, 4, dliteJsonSingle);
   mu_assert_int_eq(404, m);
 
+  
+  /* More tests for issue #543 */
+  m = dlite_json_sprint(NULL, 0, coll, 0, 0);
+  mu_assert_int_eq(406, m);
+
+  m = dlite_json_sprint(buf, sizeof(buf), coll, 0, 0);
+  mu_assert_int_eq(406, m);
 
+  
   /* Tests for proper quoting */
   DLiteCollection *coll = dlite_collection_create(NULL);
   dlite_collection_add_relation(coll, "s", "p", "\"o\"");
@@ -135,8 +145,10 @@ MU_TEST(test_append)
 
 MU_TEST(test_decref)
 {
+  dlite_instance_decref(coll);
   dlite_instance_decref(inst);
   dlite_meta_decref(meta);
+  coll = NULL;
   inst = NULL;
   meta = NULL;
 }