1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2010 - DIGITEO - Sylvestre LEDRU
4 // Copyright (C) 2011 - DIGITEO - Antoine ELIAS
6 // This file is distributed under the same license as the Scilab package.
7 // =============================================================================
9 // <-- ENGLISH IMPOSED -->
10 // <-- CLI SHELL MODE -->
11 // <-- NO CHECK REF -->
13 // unit tests for structs
14 // =============================================================================
16 date_st=struct('jour',25,'mois','DEC','annee',2006);
18 if date_st.jour <> 25 then pause, end
19 if date_st.mois <> 'DEC' then pause, end
20 if date_st.annee <> 2006 then pause, end
23 if date_st.jour <> 19 then pause, end
26 if date_st.mois <> 'AOU' then pause, end
29 if date_st.annee <> 1973 then pause, end
32 if date_st.semaine <> 32 then pause, end
34 // Example from bug #7244
41 if [1; 3; 2] <> foo then pause, end
47 foo = foo([%T %F %T]);
49 if or([1; 3; 2] <> foo) then pause, end
57 if foo(1).bar <> 1 then pause, end
58 if foo(2).bar <> 3 then pause, end
59 if foo(3).bar <> 2 then pause, end
63 if size(out,'*') <> 1 then pause, end
66 if or(size(out) <> [2 3]) then pause, end
68 s=struct("txt","Hello","num",%pi,"pol",%z^2+1);
69 if s.pol <> %z^2+1 then pause, end
70 if s.txt <> "Hello" then pause, end
72 s.txt=null();s.num=null();s.pol=null();
73 if isfield( s , "txt" ) then pause, end
74 if isfield( s , "num" ) then pause, end
75 if isfield( s , "pol" ) then pause, end
76 if or(isfield( s , ["pol", "num", "txt"] )) then pause, end
77 if or(size(s) <> [1 1]) then pause, end
86 if z.m.o.y.m.i.j.k.l.y <> 42 then pause, end
87 if ~isfield(z.m.o.y.m.i.j.k.l,"y") then pause, end
92 if z.z <> 21 then pause, end
93 if z.b.c.z <> 21 then pause, end
94 if ~isfield(z,"b") then pause, end
95 if ~isfield(z.b,"c") then pause, end
98 if execstr('z(42).x','errcatch')<>999 then pause,end
100 // -----------------------------------------------------------------
104 firstname = "firstname";
105 firstname_new = "firstname_new";
106 lastname = "lastname";
107 lastname_new = "lastname_new";
109 email_new = "email_new";
110 phone_new = "phone_new";
112 // create a one dimensional struct
113 st = struct("firstname", firstname, "lastname", lastname, "email", email);
115 assert_checkequal(st.firstname, firstname);
116 assert_checkequal(st.lastname, lastname);
117 assert_checkequal(st.email, email);
120 st.firstname = firstname_new;
121 st.lastname = lastname_new;
122 st.email = email_new;
125 st.phone = phone_new;
127 assert_checkequal(st.firstname, firstname_new);
128 assert_checkequal(st.lastname, lastname_new);
129 assert_checkequal(st.email, email_new);
130 assert_checkequal(st.phone, phone_new);
133 //create multi-dimentional struct (4x3x2)
138 firstnames{n1,n2,n3} = [];
139 lastnames{n1,n2,n3} = [];
140 emails{n1,n2,n3} = [];
145 firstnames{i} = "firstname_" + sz;
146 lastnames{i} = "lastname_" + sz;
147 emails{i} = "email_" + sz;
150 st = struct("firstname", firstnames, "lastname", lastnames, "email", emails);
153 if st(i).firstname <> firstnames{i} then pause end
154 if st(i).lastname <> lastnames{i} then pause end
155 if st(i).email <> emails{i} then pause end
157 clear firstnames lastnames emails st
159 //creating one dimensional structure by insertion
160 firstname = "firstname";
161 lastname = "lastname";
164 st.firstname = firstname;
165 st.lastname = lastname;
168 assert_checkequal(st.firstname, firstname);
169 assert_checkequal(st.lastname, lastname);
170 assert_checkequal(st.email, email);
173 //create multi dimentional struct by insertion
178 firstnames(n1,n2,n3) = "";
179 lastnames(n1,n2,n3) = "";
180 emails(n1,n2,n3) = "";
185 firstnames(i) = "firstname_" + sz;
186 lastnames(i) = "lastname_" + sz;
187 emails(i) = "email_" + sz;
190 //set dimension to 4x3x2
191 st(4,3,2).firstname = firstnames($);
192 st(4,3,2).lastname = lastnames($);
194 //fill struct as vector
196 st(i).firstname = firstnames(i);
197 st(i).lastname = lastnames(i);
198 st(i).email = emails(i);
201 //check data on each dimension
205 assert_checkequal(st(i,j,k).firstname, firstnames(i,j,k));
206 assert_checkequal(st(i,j,k).lastname, lastnames(i,j,k));
207 assert_checkequal(st(i,j,k).email, emails(i,j,k));
211 clear firstnames lastnames emails st
213 //fields of an empty struct
214 fields = getfield(1,struct());
215 assert_checkequal(fields(1), "st");
216 assert_checkequal(fields(2), "dims");