call: fix allocation and check with string argument 19/19519/2
Antoine ELIAS [Fri, 27 Oct 2017 15:42:21 +0000 (17:42 +0200)]
Change-Id: I46401c21ebd149b6d6ee9e1114c473b72401db91

scilab/modules/dynamic_link/sci_gateway/cpp/sci_call.cpp
scilab/modules/dynamic_link/tests/unit_tests/call.tst
scilab/modules/dynamic_link/tests/unit_tests/call_tests.c

index 73beb20..6d71e46 100644 (file)
@@ -287,10 +287,23 @@ int sci_call(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int
                         Scierror(999, _("%s: incompatible type between input and output variables.\n"), fname);
                         return 1;
                     }
-                    if (p.row != dims[0] || p.col != dims[1])
+
+                    if (p.type == 'c')
                     {
-                        Scierror(999, _("%s: incompatible sizes between input and output variables.\n"), fname);
-                        return 1;
+                        
+                        if (strlen((char*)p.data) != dims[0] * dims[1])
+                        {
+                            Scierror(999, _("%s: incompatible sizes between input and output variables.\n"), fname);
+                            return 1;
+                        }
+                    }
+                    else
+                    {
+                        if (p.row != dims[0] || p.col != dims[1])
+                        {
+                            Scierror(999, _("%s: incompatible sizes between input and output variables.\n"), fname);
+                            return 1;
+                        }
                     }
 
                     //need to clone double input data to avoid modification
index 5a04cb6..e5a441b 100644 (file)
@@ -13,7 +13,7 @@ copyfile(fullfile(SCI, "modules/dynamic_link/tests/unit_tests", "call_tests.c"),
 old = pwd();
 cd(TMPDIR);
 
-intef = ["call_0_0" "call_i1_0" "call_d1_0" "call_0_i1" "call_0_d1" "call_i1_i1" "call_d1_d1" "call_i1_i1_bis" "call_d1_d1_bis" "call_i1_d1"];
+intef = ["call_0_0" "call_i1_0" "call_d1_0" "call_0_i1" "call_0_d1" "call_i1_i1" "call_d1_d1" "call_i1_i1_bis" "call_d1_d1_bis" "call_i1_d1", "call_0_s1", "call_s1_0", "call_s1_s1"];
 
 ilib_for_link(intef, "call_tests.c", [], "c");
 exec loader.sce;
@@ -59,5 +59,17 @@ assert_checkequal(v, 2*%pi);
 v = call("call_i1_d1", 21, 1, "i", "out", [1 1], 2, "d");
 assert_checkequal(v, 21*%pi);
 
+//1 in string
+call("call_0_s1", "abcdefghij", 1, "c");
+
+//1 out string
+v = call("call_s1_0", "out", [1 10], 1, "c");
+assert_checkequal(v, "abcdefghij");
+
+//1 in string, 1 out string
+v = call("call_s1_s1", "abcdefghij", 1, "c", "out", [1 10], 1, "c");
+assert_checkequal(v, "bcdefghijk");
+
+
 ulink();
 cd(old);
index 4933ba4..bdcdd0c 100644 (file)
@@ -61,5 +61,34 @@ void call_i1_d1(int* i1, double* o2)
     *o2 = *i1 * M_PI;
 }
 
+void call_0_s1(char* s1)
+{
+    printf("call_0_s1: %s\n", s1);
+}
 
+void call_s1_0(char* s1)
+{
+    int size = 10;
+    int i = 0;
+    for (i = 0; i < size; ++i)
+    {
+        s1[i] = 'a' + i;
+    }
+
+    s1[size] = '\0';
+    
+    printf("call_s1_0: %s\n", s1);
+}
+
+void call_s1_s1(char* s1)
+{
+    printf("call_s1_s1: %s -> ", s1);
+    int i = 0;
+    for(i = 0; i < 10; ++i)
+    {
+        s1[i] += 1;
+    }
+
+    printf("%s\n", s1);
+}