summaryrefslogtreecommitdiffstats
path: root/scilab
diff options
context:
space:
mode:
authorCedric Delamarre <cdl@esi-group.com>2020-10-19 16:54:36 +0200
committerAntoine ELIAS <antoine.elias@esi-group.com>2020-10-27 16:27:08 +0100
commitbc87e5e8ec8bdd171f6ecfbf846418663687b5f9 (patch)
tree7da101b52bd14cdf084c5afb67d33b7e2cc63970 /scilab
parentd0c8c1144062176686c40c61f604e14cbca0e757 (diff)
downloadscilab-bc87e5e8ec8bdd171f6ecfbf846418663687b5f9.zip
scilab-bc87e5e8ec8bdd171f6ecfbf846418663687b5f9.tar.gz
insertion in container with ref > 1 fixed
https://bugzilla.scilab.org/show_bug.cgi?id=16556 test_run ast insert mode_nwni_profiling test_run ast bug_16556 mode_nwni_profiling Change-Id: Iae642d598b558d4ebc1afab1c526798cff58cc3c
Diffstat (limited to 'scilab')
-rw-r--r--scilab/modules/ast/src/cpp/ast/visitor_common.cpp19
-rw-r--r--scilab/modules/ast/tests/nonreg_tests/bug_16556.tst45
-rw-r--r--scilab/modules/ast/tests/unit_tests/insert.dia.ref426
-rw-r--r--scilab/modules/ast/tests/unit_tests/insert.tst19
4 files changed, 73 insertions, 436 deletions
diff --git a/scilab/modules/ast/src/cpp/ast/visitor_common.cpp b/scilab/modules/ast/src/cpp/ast/visitor_common.cpp
index d1c5c3b..717c185 100644
--- a/scilab/modules/ast/src/cpp/ast/visitor_common.cpp
+++ b/scilab/modules/ast/src/cpp/ast/visitor_common.cpp
@@ -930,7 +930,6 @@ types::InternalType* evaluateFields(const ast::Exp* _pExp, std::list<ExpHistory*
930 workFields.pop_front(); 930 workFields.pop_front();
931 931
932 types::InternalType* pITCurrent = pEH->getCurrent(); 932 types::InternalType* pITCurrent = pEH->getCurrent();
933
934 if (pEH->isCellExp() && pITCurrent->isCell() == false) 933 if (pEH->isCellExp() && pITCurrent->isCell() == false)
935 { 934 {
936 std::wostringstream os; 935 std::wostringstream os;
@@ -938,18 +937,18 @@ types::InternalType* evaluateFields(const ast::Exp* _pExp, std::list<ExpHistory*
938 throw ast::InternalError(os.str(), 999, _pExp->getLocation()); 937 throw ast::InternalError(os.str(), 999, _pExp->getLocation());
939 } 938 }
940 939
940 // In the case where pITCurrent is in several scilab variables or containers,
941 // we have to clone it to keep the originals one unchanged.
942 if (pITCurrent->getRef() > 1)
943 {
944 pITCurrent = pITCurrent->clone();
945 pEH->setCurrent(pITCurrent);
946 pEH->setReinsertion();
947 }
948
941 if (pITCurrent->isStruct()) 949 if (pITCurrent->isStruct())
942 { 950 {
943 types::Struct* pStruct = pITCurrent->getAs<types::Struct>(); 951 types::Struct* pStruct = pITCurrent->getAs<types::Struct>();
944 // In case where pStruct is in several scilab variable,
945 // we have to clone it for keep the other variables unchanged.
946 if (pStruct->getRef() > 1)
947 {
948 pStruct = pStruct->clone();
949 pEH->setCurrent(pStruct);
950 pEH->setReinsertion();
951 }
952
953 std::wstring pwcsFieldname = (*iterFields)->getExpAsString(); 952 std::wstring pwcsFieldname = (*iterFields)->getExpAsString();
954 953
955 if (pEH->needResize()) 954 if (pEH->needResize())
diff --git a/scilab/modules/ast/tests/nonreg_tests/bug_16556.tst b/scilab/modules/ast/tests/nonreg_tests/bug_16556.tst
new file mode 100644
index 0000000..e1b40a5
--- /dev/null
+++ b/scilab/modules/ast/tests/nonreg_tests/bug_16556.tst
@@ -0,0 +1,45 @@
1// =============================================================================
2// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3// Copyright (C) 2020 - Cedric Delamarre
4//
5// This file is distributed under the same license as the Scilab package.
6// =============================================================================
7//
8// <-- CLI SHELL MODE -->
9// <-- NO CHECK REF -->
10//
11// <-- Non-regression test for bug 16556 -->
12//
13// <-- Bugzilla URL -->
14// http://bugzilla.scilab.org/16556
15//
16// <-- Short Description -->
17// Modifying nested lists/arrays in function changes variable in calling scope
18
19// Define variables
20testArray = list();
21for i=1:1:10
22 testArray($+1) = list();
23 for j=1:1:10
24 testArray(i)($+1) = [];
25 testArray(i)(j).testMember = 1;
26 end
27end
28testArrayOrig = testArray;
29
30// Define functions
31function outArray = testFunc(inArray)
32 for i=1:1:10
33 for j=1:1:10
34 inArray(i)(j).testMember = 5
35 end
36 end
37 outArray = inArray;
38endfunction
39
40// Send the variable as argument to the function
41testArrayOut = testFunc(testArray);
42
43assert_checkequal(testArrayOut(1)(1).testMember, 5);
44assert_checkequal(testArrayOrig(1)(1).testMember, 1);
45assert_checkequal(testArray(1)(1).testMember, 1);
diff --git a/scilab/modules/ast/tests/unit_tests/insert.dia.ref b/scilab/modules/ast/tests/unit_tests/insert.dia.ref
deleted file mode 100644
index 1a659a8..0000000
--- a/scilab/modules/ast/tests/unit_tests/insert.dia.ref
+++ /dev/null
@@ -1,426 +0,0 @@
1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) 2012 - Scilab Enterprises - Antoine ELIAS
3//
4// Copyright (C) 2012 - 2016 - Scilab Enterprises
5//
6// This file is hereby licensed under the terms of the GNU GPL v2.0,
7// pursuant to article 5.3.4 of the CeCILL v.2.1.
8// This file was originally licensed under the terms of the CeCILL v2.1,
9// and continues to be available under such terms.
10// For more information, see the COPYING file which you should have received
11// along with this program.
12//
13//
14// <-- CLI SHELL MODE -->
15oldFuncProt = funcprot(0);
16ref = [0 0 0 ; 0 1 4 ; 0 2 5];
17a = zeros(3,3);a(:) = 1:9;
18b(2:3, 2:3) = a(1:2,1:2);
19assert_checkequal(b, ref);
20a=2;
21a.b=3;
22assert_checkequal(a.b, 3);
23a.b.c=4;
24assert_checkequal(a.b.c, 4);
25clear a
26a.b = 2;
27a.c = 3;
28a.d = 4;
29e.f = 7;
30e.d = 55;
31e.g = 12;
32a(2) = e;
33assert_checkequal(fieldnames(a(1)), fieldnames(a(2)));
34assert_checkequal(a(1).d, 4);
35assert_checkequal(a(2).d, 55);
36clear a;
37clear e;
38a.a.a = -2;
39assert_checkequal(a.a.a, -2);
40a(2, 1:3, [2 6 7]).b = 12;
41computed = a.b;
42expected = list();
43for i=1:(2*3*7)
44 expected(i) = [];
45end
46expected(8) = 12;
47expected(10) = 12;
48expected(12) = 12;
49expected(32) = 12;
50expected(34) = 12;
51expected(36) = 12;
52expected(38) = 12;
53expected(40) = 12;
54expected(42) = 12;
55assert_checkequal(a.b, expected);
56clear a;
57a(1,3).b = 2;
58assert_checkequal(size(a), [1 3]);
59a(5).b = 2;
60assert_checkequal(size(a), [1 5]);
61a(2,3).b = 2;
62assert_checkequal(size(a), [2 5]);
63clear a;
64st.f.e = 22;
65st.f(3).e = 112;
66assert_checkequal(st.f.e, list(22, [], 112));
67st.f(2).j = 1124;
68assert_checkequal(st.f.j, list([], 1124, []));
69st.f.e = 5;
70assert_checkequal(st.f.e, list(5, 5, 5));
71clear st;
72//tlist
73tl=tlist(["myTlist" "gg"], 22);
74tll=tlist(["myTlOfTl" "tt"],tl);
75a.b=tll;
76a.b.tt(2) = 4;
77assert_checkequal(a.b.tt(2), 4);
78assert_checkequal(a.b.tt.gg, 4);
79a.b.tt.gg = 5;
80assert_checkequal(a.b.tt(2), 5);
81assert_checkequal(a.b.tt.gg, 5);
82clear a;
83rl = rlist(%s,2);
84a.b = [rl rl rl];
85a.b(2) = [4 4 4];
86assert_checkequal(a.b.num, [4 4 4]);
87assert_checkequal(a.b.den, [2 2 2]);
88clear a;
89a.b = [rl rl rl];
90%r_6 = %r_e;
91a.b(1,2).num = 14;
92assert_checkequal(a.b.num, [%s 14 %s]);
93assert_checkequal(a.b.den, [2 2 2]);
94a(2).b = [rl rl rl] * %s;
95assert_checkequal(size(a), [2 1]);
96assert_checkequal(a(2).b.num, [%s**2 %s**2 %s**2]);
97err = execstr("a.b(1,3).num = 5;", "errcatch");
98assert_checktrue(err <> 0);
99a(3).b.num = 12;
100assert_checkequal(a(3).b.num, 12);
101execstr("a.b(1,1).num = -9;", "errcatch");
102assert_checktrue(err <> 0);
103clear a;
104tt = tlist(["toto" "gg" "ff"] , 12, 13);
105tt(3).c = 23;
106assert_checkequal(tt.ff.c, 23);
107tt(2).c = 22;
108assert_checkequal(tt.gg.c, 22);
109err = execstr("tt([2 3]).d = 99;", "errcatch");
110assert_checktrue(err <> 0);
111err = execstr("tt([2 3]).d.e = 98;", "errcatch");
112assert_checktrue(err <> 0);
113err = execstr("tt([2 3]).d(3).e = 78;", "errcatch");
114assert_checktrue(err <> 0);
115tt(2).d.e(4) = 12;
116assert_checkequal(tt(2).d.e, [0; 0; 0; 12]);
117tt(2).d(2).o = 15;
118err = execstr("tt(2).d.e(4) = 16;", "errcatch");
119assert_checktrue(err <> 0);
120clear tt;
121tl=tlist(["toto" "gg"],11);
122function tutu(tl)
123 tl.gg = 12;
124 assert_checkequal(tl.gg, 12);
125endfunction
126tutu(tl);
127assert_checkequal(tl.gg, 11);
128clear tl;
129// mlist
130function varargout=%toto_e(varargin)
131 args = varargin(1);
132 ml = varargin($);
133 varargout = list();
134 for i=args
135 if i == 1
136 varargout($+1) = ["toto" "gg" "ff"];
137 elseif i == 2
138 varargout($+1) = ml.gg;
139 elseif i == 3
140 varargout($+1) = ml.ff;
141 end
142 end
143endfunction
144%toto_6 = %toto_e;
145function r=%l_i_toto(varargin)
146 args = varargin(1);
147 l = varargin($-1);
148 r = varargin($);
149 for i=1:size(args, "*")
150 if args(i) == 2
151 r.gg = l(i);
152 elseif args(i) == 3
153 r.ff = l(i);
154 end
155 end
156endfunction
157function r=%st_i_toto(varargin)
158 args = varargin(1);
159 st = varargin($-1);
160 r = varargin($);
161 for i=args
162 if i == 2
163 r.gg = st;
164 elseif i == 3
165 r.ff = st;
166 end
167 end
168endfunction
169mm = tlist(["toto" "gg" "ff"] , 12, 13);
170mm(3).c = 23;
171assert_checkequal(mm.ff.c, 23);
172mm(2).c = 22;
173assert_checkequal(mm.gg.c, 22);
174err = execstr("mm([2 3]).d = 99;", "errcatch");
175assert_checktrue(err <> 0);
176err = execstr("mm([2 3]).d.e = 98;", "errcatch");
177assert_checktrue(err <> 0);
178err = execstr("mm([2 3]).d(3).e = 78;", "errcatch");
179assert_checktrue(err <> 0);
180mm(2).d.e(4) = 12;
181assert_checkequal(mm(2).d.e, [0; 0; 0; 12]);
182mm(2).d(2).o = 15;
183err = execstr("mm(2).d.e(4) = 16;", "errcatch");
184assert_checktrue(err <> 0);
185clear mm;
186ml = mlist(["tutu" "t"], 12);
187std=struct("d", 5);
188stc=struct("c", 6);
189a=std;
190assert_checkequal(a, std);
191a=stc;
192assert_checkequal(a, stc);
193ml.t = std;
194assert_checkequal(ml.t.d, 5);
195ml.t = stc;
196assert_checkequal(ml.t.c, 6);
197ml = mlist(["tutu" "t"], 12);
198ml.t = 21;
199assert_checkequal(ml.t, 21);
200function r=%s_i_tutu(varargin)
201 s1=varargin($-1);
202 s2=varargin($);
203 r=s2;
204 r.t = s1;
205endfunction
206function r=%tutu_e(varargin)
207 r=varargin($).t;
208endfunction
209function r=%st_i_tutu(varargin)
210 s1=varargin($-1);
211 s2=varargin($);
212 //s2(i,j)=s1 s1 is a struct
213 r=s2;
214 r.t = s1;
215endfunction
216%tutu_6 = %tutu_e;
217a.b = ml;
218assert_checkequal(a.b(2), 21);
219a.b(2) = stc;
220assert_checkequal(a.b(2), stc);
221a.b(2).c=44;
222assert_checkequal(a.b(2).c, 44);
223clear a;
224//tlist/mlist
225tl =tlist(["toto" "gg"], tlist(["tutu" "tt" "ee"], 22, 23));
226tl(2)(3)=12;
227assert_checkequal(tl.gg.ee, 12);
228tl(2)(3)(4)=88;
229assert_checkequal(tl.gg.ee, [12; 0; 0; 88]);
230function r=%tata_e(varargin)
231 r=varargin($).ff;
232endfunction
233%tata_6 = %tata_e;
234function r=%toto_i_tata(varargin)
235 s1=varargin($-1);
236 s2=varargin($);
237 r=s2;
238 r.ff = s1;
239endfunction
240ml = mlist(["tata" "ff"], tl);
241assert_checkequal(ml.ff.gg.ee, [12; 0; 0; 88]);
242ml(2)(2)(3) = 44;
243assert_checkequal(ml.ff.gg.ee, 44);
244clear tl;
245// List
246l=list();
247// call insertion overload
248err = execstr("l.x = 2;","errcatch");
249assert_checkequal(err, 999);
250st.l = list([1 2 3], "toto");
251st.l = 44;
252assert_checkequal(st.l, 44);
253st.l = list([1 2 3], "toto");
254st.l(3) = 55;
255assert_checkequal(st.l(3), 55);
256st.l(1)(2) = -2;
257assert_checkequal(st.l(1), [1 -2 3]);
258st.l(3) = tlist(["toto", "gg"], 112);
259function r=%l_6(varargin)
260 r=[]
261 for in = varargin(2)
262 if type(in) == 16 // tlist
263 if find(varargin(1) == in(1)) <> [] // search field in tlist
264 r = in(varargin(1));
265 end
266 end
267 end
268endfunction
269function l=%st_i_l(varargin)
270 l=list();
271 pos = 0;
272 for in = varargin(3)
273 pos = pos + 1;
274 if type(in) == 16 // tlist
275 in((varargin(1))) = varargin(2);
276 end
277 l(pos) = in;
278 end
279endfunction
280st.l.gg.c = 12;
281assert_checkequal(st.l(1), [1 -2 3]);
282assert_checkequal(st.l(2), "toto");
283assert_checkequal(st.l(3).gg, struct("c", 12));
284clear st;
285st.l = list([1 2 3], "toto");
286function r=%s_i_l(varargin)
287 r=struct(varargin(1), varargin(2)) ;
288endfunction
289st.l.c = 24;
290assert_checkequal(st.l, struct("c", 24));
291clear st;
292// check error overload
293errmsg = msprintf(gettext("%s: Affection of a string in a matrix of numbers is not implemented.\n"), "%c_i_s");
294assert_checkerror("a=1;a(1,1)=""d""", errmsg);
295// list delete
296tl = tlist(["toto" "gg" "ff" "uu"], 45, 89, 87);
297tl(2) = null();
298assert_checkequal(tl(1), ["toto" "gg" "ff" "uu"]);
299assert_checkequal(tl(2), 89);
300assert_checkequal(tl(3), 87);
301tl = tlist(["toto" "gg" "ff" "uu"], 45, 89, 87);
302// call overload
303err = execstr("tl.gg = null();","errcatch");
304assert_checkequal(err, 999);
305function l=%0_i_toto(varargin)
306 idx = find(varargin(3)(1) == varargin(1));
307 l = varargin(3);
308 l(idx) = null();
309endfunction
310tl.gg = null();
311assert_checkequal(tl(1), ["toto" "gg" "ff" "uu"]);
312assert_checkequal(tl(2), 89);
313assert_checkequal(tl(3), 87);
314clear tl;
315l = list(["toto" "gg" "ff" "uu"], 45, 89, 87);
316l(2) = null();
317assert_checkequal(l(1), ["toto" "gg" "ff" "uu"]);
318assert_checkequal(l(2), 89);
319assert_checkequal(l(3), 87);
320l = list(["toto" "gg" "ff" "uu"], 45, 89, 87);
321// call overload
322err = execstr("l.gg = null();","errcatch");
323assert_checkequal(err, 999);
324clear l;
325// cell
326function varargout=%toto_e(varargin)
327 args = varargin(1);
328 ml = varargin($);
329 varargout = list();
330 for i=args
331 if i == 1
332 varargout($+1) = ["toto" "ff"];
333 elseif i == 2
334 varargout($+1) = ml.ff;
335 end
336 end
337endfunction
338%toto_6 = %toto_e;
339function ml=%s_i_toto(varargin)
340 args = varargin(1);
341 s = varargin($-1);
342 ml = varargin($);
343 for i=1:size(args, "*")
344 if args(i) == 2
345 ml.ff = s;
346 end
347 end
348endfunction
349tl = tlist(["tutu" "gg"], 12);
350ml = mlist(["toto" "ff"], 22);
351a{2} = 2;
352assert_checkequal(a{2}, 2);
353a{1,3} = tl;
354assert_checkequal(a{5}(1), ["tutu" "gg"]);
355assert_checkequal(a{5}(2), 12);
356a{[3 6]} = ml;
357assert_checkequal(a{3}(1), ["toto" "ff"]);
358assert_checkequal(a{3}(2), 22);
359assert_checkequal(a{6}(1), ["toto" "ff"]);
360assert_checkequal(a{6}(2), 22);
361a{2}.c = 123;
362assert_checkequal(a{2}.c, 123);
363a{2}(3).e = 12;
364assert_checkequal(a{2}.e, list([], [], 12));
365assert_checkequal(a{2}.c, list(123, [], []));
366a{2}.b = 12;
367assert_checkequal(a{2}.b, list(12, 12, 12));
368a{2}.e = 99;
369assert_checkequal(a{2}.e, list(99, 99, 99));
370a{6}(2) = 8;
371assert_checkequal(a{6}(1), ["toto" "ff"]);
372assert_checkequal(a{6}(2), 8);
373a{1, [1 3]}.gg = 14;
374assert_checkequal(a{1}.gg, 14);
375assert_checkequal(a{5}.gg, 14);
376a{1:2, [1 3], 2}.g = "A string";
377assert_checkequal(a{7}.g, "A string");
378assert_checkequal(a{8}.g, "A string");
379assert_checkequal(a{11}.g, "A string");
380assert_checkequal(a{12}.g, "A string");
381clear a;
382a{1,3}.b = 2;
383assert_checkequal(size(a), [1 3]);
384a{5}.b = 2;
385assert_checkequal(size(a), [1 5]);
386a{2,3}.b = 2;
387assert_checkequal(size(a), [2 5]);
388clear a;
389a{1,3} = 2;
390assert_checkequal(size(a), [1 3]);
391a{5} = 2;
392assert_checkequal(size(a), [1 5]);
393a{2,3} = 2;
394assert_checkequal(size(a), [2 5]);
395clear a;
396tl = tlist(["mytlist", "f1"], 12);
397ml = mlist(["mymlist", "f"], tl);
398mml = mlist(["mymmlist", "ff1"], ml);
399mml(list("ff1", "f", 2)) = 14;
400assert_checkequal(tl.f1, 12);
401assert_checkequal(ml.f.f1, 12);
402assert_checkequal(mml.ff1.f.f1, 14);
403funcprot(oldFuncProt);
404// multiple insertion in struct
405function r = initst()
406 r.f.e = list()
407 r.f(3).e = 3
408endfunction
409st=initst();
410st.f(:).e = 5;
411assert_checkequal(st.f.e, list(5,5,5));
412st=initst();
413st.f(1:4).e = 5;
414assert_checkequal(st.f.e, list(5,5,5,5));
415st=initst();
416err=execstr("st.f(1:2).e(2) = 5;", "errcatch");
417assert_checktrue(err <> 0);
418st=initst();
419err=execstr("st(1:2).f(1:4).e = 5;", "errcatch");
420assert_checktrue(err <> 0);
421// insert without arguments
422errmsg = msprintf(gettext("Wrong insertion : Cannot insert without arguments."));
423assert_checkerror("a()=1;", errmsg);
424assert_checkerror("a{}=1;", errmsg);
425a=1;
426assert_checkerror("a()=1;", errmsg);
diff --git a/scilab/modules/ast/tests/unit_tests/insert.tst b/scilab/modules/ast/tests/unit_tests/insert.tst
index 50937d2..8ed3275 100644
--- a/scilab/modules/ast/tests/unit_tests/insert.tst
+++ b/scilab/modules/ast/tests/unit_tests/insert.tst
@@ -12,6 +12,7 @@
12// 12//
13// 13//
14 14
15// <-- NO CHECK REF -->
15// <-- CLI SHELL MODE --> 16// <-- CLI SHELL MODE -->
16 17
17oldFuncProt = funcprot(0); 18oldFuncProt = funcprot(0);
@@ -529,3 +530,21 @@ assert_checkerror("a()=1;", errmsg);
529assert_checkerror("a{}=1;", errmsg); 530assert_checkerror("a{}=1;", errmsg);
530a=1; 531a=1;
531assert_checkerror("a()=1;", errmsg); 532assert_checkerror("a()=1;", errmsg);
533
534// insertion on element with ref > 1
535origin = list(list([]));
536origin(1)(1).test = 1;
537twin = origin;
538twin(1)(1).test = 5;
539assert_checkequal(origin(1)(1).test, 1);
540assert_checkequal(twin(1)(1).test, 5);
541
542twin = origin(1);
543twin(1).test = 5;
544assert_checkequal(origin(1)(1).test, 1);
545assert_checkequal(twin(1).test, 5);
546
547twin=origin(1)(1);
548twin.test = 5;
549assert_checkequal(origin(1)(1).test, 1);
550assert_checkequal(twin.test, 5);