* bug #8356 fixed - sci2exp() was wrong for m-t-lists with undefined fields 16/18416/3
Samuel GOUGEON [Fri, 5 Aug 2016 08:42:30 +0000 (10:42 +0200)]
  http://bugzilla.scilab.org/8356
  + code factorization

Change-Id: I972777e55b977eeecf0451d1ef72cbef4284993b

scilab/CHANGES.md
scilab/modules/string/macros/sci2exp.sci
scilab/modules/string/tests/nonreg_tests/bug_8356.dia.ref [new file with mode: 0644]
scilab/modules/string/tests/nonreg_tests/bug_8356.tst [new file with mode: 0644]

index d1aa5ca..dcd2b05 100644 (file)
@@ -249,6 +249,7 @@ Bug Fixes
 * [Bug #5723](http://bugzilla.scilab.org/show_bug.cgi?id=5723) fixed   - Cross-references were missing between axis_properties and axes_properties help pages
 * [Bug #7649](http://bugzilla.scilab.org/show_bug.cgi?id=7649) fixed - `isempty` returned `%F` on `struct()`, `{}` or `list(,)` and was not shortcut
 * [Bug #7696](http://bugzilla.scilab.org/show_bug.cgi?id=7696) fixed   - The `parallel_run` help page was poorly formated
+* [Bug #8356](http://bugzilla.scilab.org/show_bug.cgi?id=8356) fixed - `sci2exp` applied to lists, tlists or mlists having undefined fields yielded an error or a wrong result.
 * [Bug #9912](http://bugzilla.scilab.org/show_bug.cgi?id=9912) fixed   - In case of missing translated help page, the default en_US was sometimes ignored
 * [Bug #9153](http://bugzilla.scilab.org/9153) fixed                   - The `isqualbitwise` help page was inaccurate and badly located
 * [Bug #11375](http://bugzilla.scilab.org/show_bug.cgi?id=11375) fixed - When a localized help subdirectory has only a CHAPTER file specifying the section title, it was ignored.
index 45089bf..1982d7f 100644 (file)
@@ -1,5 +1,6 @@
 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 // Copyright (C) INRIA -
+// Copyright (C) 2016 - Samuel GOUGEON
 //
 // Copyright (C) 2012 - 2016 - Scilab Enterprises
 //
@@ -10,7 +11,6 @@
 // For more information, see the COPYING file which you should have received
 // along with this program.
 
-
 function t=sci2exp(a,nom,lmax)
     // sci2exp - convert a variable to an expression
     //%SYNTAX
@@ -250,8 +250,8 @@ endfunction
 
 function t=pol2exp(a,lmax)
     $
-    if rhs<2 then lmax=0,end
-    [lhs,rhs]=argn(0)
+    if rhs<2 then lmax = 0, end
+    [lhs,rhs] = argn(0)
 
     [m,n]=size(a),var=" ";lvar=1
     var=varn(a),lvar=length(var);
@@ -337,107 +337,60 @@ function t=pol2exp(a,lmax)
     end
 endfunction
 
-function t=list2exp(l,lmax)
-    if rhs<2 then lmax=0,end
-    [lhs,rhs]=argn(0)
-    dots="."+".";
-    t="list("
-    n=length(l)
-    for k=1:n
-        lk=l(k)
-        sep=",",if k==1 then sep=emptystr(),end
-        if type(lk)==9 then
-            t1=h2exp(lk,lmax)
-        elseif type(lk)==15 then
-            t1=list2exp(lk,lmax)
-        elseif type(lk)==16 then
-            t1=tlist2exp(lk,lmax)
-        elseif type(lk)==17 then
-            t1=mlist2exp(lk,lmax)
+function t = glist2exp(listType, l,lmax)
+    [lhs,rhs] = argn(0)
+    if rhs<3 then lmax = 0, end
+    dots = "."+".";
+    t = listType+"("
+    for k = 1:length(l)
+        sep = ",", if k==1 then sep = "", end
+        clear lk
+        if listType ~= "mlist"
+            lk = l(k)
         else
-            t1=sci2exp(lk,lmax)
+            lk = getfield(k,l)
         end
-        if size(t1,"*")==1&(lmax==0|max(length(t1))+length(t($))<lmax) then
-            t($)=t($)+sep+t1
+        if ~isdef("lk","local")
+            t1=""
         else
-            t($)=t($)+sep+dots
-            t=[t;t1]
-        end
-        lk=null()
-    end
-    t($)=t($)+")"
-endfunction
-
-function t=tlist2exp(l,lmax)
-    $;
-    if rhs<2 then lmax=0,end
-    [lhs,rhs]=argn(0)
-    dots="."+".";
-    t="tlist("
-    n=length(l)
-    for k=1:n
-        lk=l(k)
-        sep=",",if k==1 then sep=emptystr(),end
-        if type(lk)==15 then
-            t1=list2exp(lk,lmax)
-        elseif type(lk)==16 then
-            t1=tlist2exp(lk,lmax)
-        elseif type(lk)==17 then
-            t1=mlist2exp(lk,lmax)
-        elseif type(lk)==9 then
-            t1=h2exp(lk,lmax,1)
-        else
-            t1=sci2exp(lk,lmax)
+            if type(lk)==9 then
+                t1 = h2exp(lk,lmax)
+            elseif type(lk)==15 then
+                t1 = list2exp(lk,lmax)
+            elseif type(lk)==16 then
+                t1 = tlist2exp(lk,lmax)
+            elseif type(lk)==17 then
+                t1 = mlist2exp(lk,lmax)
+            elseif type(lk)==128 then
+                t1 = mlist2exp(user2mlist(lk),lmax)
+            else
+                t1 = sci2exp(lk,lmax)
+            end
         end
         if size(t1,"*")==1&(lmax==0|max(length(t1))+length(t($))<lmax) then
-            t($)=t($)+sep+t1
+            t($) = t($)+sep+t1
         else
-            t($)=t($)+sep+dots
-            t=[t;t1]
+            t($) = t($)+sep+dots
+            t = [t; t1]
         end
     end
-    t($)=t($)+")"
-
+    t($) = t($)+")"
 endfunction
 
-function t=mlist2exp(l,lmax)
-    $;
-    if rhs<2 then lmax=0,end
-    [lhs,rhs]=argn(0)
-    dots="."+".";
-    t="mlist("
-    n=size(definedfields(l),"*")
-    for k=1:n
-        lk=getfield(k,l)
-        sep=",",if k==1 then sep=emptystr(),end
-        if type(lk)==15 then
-            t1=list2exp(lk,lmax)
-        elseif type(lk)==16 then
-            t1=tlist2exp(lk,lmax)
-        elseif type(lk)==17 then
-            t1=mlist2exp(lk,lmax)
-        elseif type(lk)==9 then
-            t1=h2exp(lk,lmax)
-        elseif type(lk)==128 then
-            t1=mlist2exp(user2mlist(lk),lmax)
-        else
-            t1=sci2exp(lk,lmax)
-        end
-        if size(t1,"*")==1&(lmax==0|max(length(t1))+length(t($))<lmax) then
-            t($)=t($)+sep+t1
-        else
-            t($)=t($)+sep+dots
-            t=[t;t1]
-        end
-    end
-    t($)=t($)+")"
-
+function t = list2exp(l, lmax)
+    t = glist2exp("list", l, lmax)
+endfunction
+function t = tlist2exp(l, lmax)
+    t = glist2exp("tlist", l, lmax)
+endfunction
+function t = mlist2exp(l, lmax)
+    t = glist2exp("mlist", l, lmax)
 endfunction
 
 function t=log2exp(a,lmax)
     $;
-    if rhs<2 then lmax=0,end
-    [lhs,rhs]=argn(0)
+    if rhs<2 then lmax = 0, end
+    [lhs,rhs] = argn(0)
     [m,n]=size(a),
     a1=matrix(a,m*n,1)
     F="%f"
diff --git a/scilab/modules/string/tests/nonreg_tests/bug_8356.dia.ref b/scilab/modules/string/tests/nonreg_tests/bug_8356.dia.ref
new file mode 100644 (file)
index 0000000..d282e6c
--- /dev/null
@@ -0,0 +1,35 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2016 - Samuel GOUGEON
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+//
+// <-- Non-regression test for bug 8356 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=8356
+//
+// <-- Short Description -->
+// sci2exp() applied to a list having undefined fields yielded an error
+// sci2exp() applied to tlists or mlists having undefined fields gave wrong results
+
+format("v",10);
+L = list(%pi, , "Hello");
+m = mlist(["test","a","b","c"], , 3.1415927, , ,"Hello");
+t = tlist(["test" "a" "b" "c"], %pi, , %z);
+assert_checkequal(sci2exp(L), "list(3.1415927,,""Hello"")");
+assert_checkequal(sci2exp(L,15), ["list(3.1415927,,..";"""Hello"")"]);
+assert_checkequal(sci2exp(t), "tlist([""test"",""a"",""b"",""c""],3.1415927,,z)");
+assert_checkequal(sci2exp(t,20), ["tlist(..";"[""test"",""a"",""b"",""c""],..";"3.1415927,,z)"]);
+assert_checkequal(sci2exp(m), "mlist([""test"",""a"",""b"",""c""],,3.1415927,,,""Hello"")");
+assert_checkequal(sci2exp(m, 30), ["mlist([""test"",""a"",""b"",""c""],,.."; "3.1415927,,,""Hello"")"]);
+
+c = tlist(["test","b","u","t","L"],%t,,"It works",list(%z,,%i));
+assert_checkequal(sci2exp(c), "tlist([""test"",""b"",""u"",""t"",""L""],%t,,""It works"",list(z,,%i))");
+r = ["tlist([""test"",""b"",""u"",""t"",""L""],%t,,.."
+     """It works"",list(z,,%i))"
+     ];
+assert_checkequal(sci2exp(c,40),r);
diff --git a/scilab/modules/string/tests/nonreg_tests/bug_8356.tst b/scilab/modules/string/tests/nonreg_tests/bug_8356.tst
new file mode 100644 (file)
index 0000000..d282e6c
--- /dev/null
@@ -0,0 +1,35 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2016 - Samuel GOUGEON
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+//
+// <-- Non-regression test for bug 8356 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/show_bug.cgi?id=8356
+//
+// <-- Short Description -->
+// sci2exp() applied to a list having undefined fields yielded an error
+// sci2exp() applied to tlists or mlists having undefined fields gave wrong results
+
+format("v",10);
+L = list(%pi, , "Hello");
+m = mlist(["test","a","b","c"], , 3.1415927, , ,"Hello");
+t = tlist(["test" "a" "b" "c"], %pi, , %z);
+assert_checkequal(sci2exp(L), "list(3.1415927,,""Hello"")");
+assert_checkequal(sci2exp(L,15), ["list(3.1415927,,..";"""Hello"")"]);
+assert_checkequal(sci2exp(t), "tlist([""test"",""a"",""b"",""c""],3.1415927,,z)");
+assert_checkequal(sci2exp(t,20), ["tlist(..";"[""test"",""a"",""b"",""c""],..";"3.1415927,,z)"]);
+assert_checkequal(sci2exp(m), "mlist([""test"",""a"",""b"",""c""],,3.1415927,,,""Hello"")");
+assert_checkequal(sci2exp(m, 30), ["mlist([""test"",""a"",""b"",""c""],,.."; "3.1415927,,,""Hello"")"]);
+
+c = tlist(["test","b","u","t","L"],%t,,"It works",list(%z,,%i));
+assert_checkequal(sci2exp(c), "tlist([""test"",""b"",""u"",""t"",""L""],%t,,""It works"",list(z,,%i))");
+r = ["tlist([""test"",""b"",""u"",""t"",""L""],%t,,.."
+     """It works"",list(z,,%i))"
+     ];
+assert_checkequal(sci2exp(c,40),r);