License Header change: Removed the LICENSE_END before beta
[scilab.git] / scilab / modules / ast / tests / unit_tests / struct.tst
1 //
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2011 - DIGITEO - Antoine ELIAS
4 //
5 // Copyright (C) 2012 - 2016 - Scilab Enterprises
6 //
7 // This file is hereby licensed under the terms of the GNU GPL v2.0,
8 // pursuant to article 5.3.4 of the CeCILL v.2.1.
9 // This file was originally licensed under the terms of the CeCILL v2.1,
10 // and continues to be available under such terms.
11 // For more information, see the COPYING file which you should have received
12 // along with this program.
13 //
14 //
15
16 // <-- CLI SHELL MODE -->
17
18 //function struct
19
20 firstname       = "firstname";
21 firstname_new   = "firstname_new";
22 lastname        = "lastname";
23 lastname_new    = "lastname_new";
24 email           = "email";
25 email_new       = "email_new";
26 phone_new       = "phone_new";
27
28 // create a one dimensional struct
29 st = struct("firstname", firstname, "lastname", lastname, "email", email);
30
31 assert_checkequal(st.firstname, firstname);
32 assert_checkequal(st.lastname, lastname);
33 assert_checkequal(st.email, email);
34
35 //change some values
36 st.firstname    = firstname_new;
37 st.lastname     = lastname_new;
38 st.email        = email_new;
39
40 // add a phone field
41 st.phone        = phone_new;
42
43 assert_checkequal(st.firstname, firstname_new);
44 assert_checkequal(st.lastname, lastname_new);
45 assert_checkequal(st.email, email_new);
46 assert_checkequal(st.phone, phone_new);
47 clear st;
48
49 //create multi-dimentional struct (4x3x2)
50 n1 = 4;
51 n2 = 3;
52 n3 = 2;
53
54 firstnames{n1,n2,n3}    = [];
55 lastnames{n1,n2,n3}     = [];
56 emails{n1,n2,n3}        = [];
57
58
59 for i = 1:(n1*n2*n3)
60     sz              = string(i);
61     firstnames{i}   = "firstname_" + sz;
62     lastnames{i}    = "lastname_" + sz;
63     emails{i}       = "email_" + sz;
64 end
65
66 st = struct("firstname", firstnames, "lastname", lastnames, "email", emails);
67
68 for i = 1:24
69     if st(i).firstname <> firstnames{i} then pause end
70     if st(i).lastname <> lastnames{i} then pause end
71     if st(i).email <> emails{i} then pause end
72 end
73 clear firstnames
74 clear lastnames
75 clear emails
76 clear st;
77
78 //creating one dimensional structure by insertion
79 firstname   = "firstname";
80 lastname    = "lastname";
81 email       = "email";
82
83 st.firstname    = firstname;
84 st.lastname     = lastname;
85 st.email        = email;
86
87 assert_checkequal(st.firstname, firstname);
88 assert_checkequal(st.lastname, lastname);
89 assert_checkequal(st.email, email);
90 clear st;
91
92 //create multi dimentional struct by insertion
93 n1 = 4;
94 n2 = 3;
95 n3 = 2;
96
97 firstnames(n1,n2,n3)    = "";
98 lastnames(n1,n2,n3)     = "";
99 emails(n1,n2,n3)        = "";
100
101
102 for i = 1:(n1*n2*n3)
103     sz              = string(i);
104     firstnames(i)   = "firstname_" + sz;
105     lastnames(i)    = "lastname_" + sz;
106     emails(i)       = "email_" + sz;
107 end
108
109 //set dimension to 4x3x2
110 st(4,3,2).firstname = firstnames($);
111 st(4,3,2).lastname = lastnames($);
112
113 //fill struct as vector
114 for i = 1:(n1*n2*n3)
115     st(i).firstname = firstnames(i);
116     st(i).lastname  = lastnames(i);
117     st(i).email     = emails(i);
118 end
119
120 //check data on each dimension
121 for i = 1:n1
122     for j = i:n2
123         for k = 1:n3
124             assert_checkequal(st(i,j,k).firstname, firstnames(i,j,k));
125             assert_checkequal(st(i,j,k).lastname, lastnames(i,j,k));
126             assert_checkequal(st(i,j,k).email, emails(i,j,k));
127         end
128     end
129 end
130 clear firstnames
131 clear lastnames
132 clear emails
133 clear st;
134
135 //fields of an empty struct
136 fields = getfield(1,struct());
137 assert_checkequal(fields(1), "st");
138 assert_checkequal(fields(2), "dims");
139
140