1//<-- CLI SHELL MODE -->
2// =============================================================================
3// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4// Copyright (C) ????-2008 - INRIA Michael Baudin
5//
6//  This file is distributed under the same license as the Scilab package.
7// =============================================================================
8//
9// for.tst --
10//   Test "for" for several data types : integer, string, vector, matrix,
11//   polynomials, and complex polynomials.
12//
13
14// Loop over an integer range
15computed=0;
16for i=1:5
17  computed=computed+i;
18end
19assert_checkequal(computed, 1+2+3+4+5);
20
21// Loop over a vector of integers
22computed=0;
23values=[1 3 5 7 9];
24for i=values
25  computed=computed+i;
26end
27assert_checkequal(computed, 1+3+5+7+9);
28
29// Loop over an integer with n1:step:n2 syntax
30computed=0;
31step=2;
32for i=1:step:10
33  computed=computed+i;
34end
35assert_checkequal(computed, 1+3+5+7+9);
36
37// Loop over a vector of strings (test also concatenation of strings)
38computed="";
39values=["this+" "is+" "my+" "string"];
40for i=values
41  computed=computed+i;
42end
43assert_checkequal(computed, "this+is+my+string");
44
45// Loop over a row vector of real values
46computed=0.;
47values=[1. 2. 3. 4. 5.];
48for x=values
49  computed=computed+x;
50end
51assert_checkequal(computed, 1.+2.+3.+4.+5.);
52
53// Loop over a list of real vectors
54computed=[0. 0. 0.];
55mylist=list([1. 2. 3.],[4. 5. 6.],[7. 8. 9.]);
56for v=mylist
57  computed=computed+v;
58end
59assert_checkequal(computed, [(1.+4.+7.) (2.+5.+8.) (3.+6.+9.)]);
60
61// Loop over a vector of real polynomials
62computed=0.;
63myvector=[(1.+2*%s) (3.+4.*%s)];
64for v=myvector
65  computed=computed+v;
66end
67assert_checkequal(computed, 4.+6.*%s);
68
69// Loop over a vector of complex polynomials
70computed=0.;
71p1=1.+2.*%i+3*%s;
72p2=4.+5.*%i+6*%s;
73myvector=[p1 p2];
74for p=myvector
75  computed=computed+p;
76end
77assert_checkequal(computed, 5.+7*%i+9*%s);
78
79// Loop over a column vector of real values
80// Note : there is only one loop here
81computed=[0;0;0;0;0];
82values=[1.;2.;3.;4.;5.];
83for x=values
84  computed=computed+x;
85end
86assert_checkequal(computed, [1.;2.;3.;4.;5.]);
87
88// Loop over a matrix of real values
89// Note : the loop is over the columns
90computed=[0;0];
91values=[1. 2.;3. 4.];
92for x=values
93  computed=computed+x;
94end
95assert_checkequal(computed, [3.;7.]);
96