e127cb9f0d976030e8fe3242a0938de09b48f12e
[scilab.git] / scilab / modules / ast / tests / unit_tests / struct.dia.ref
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 // <-- CLI SHELL MODE -->
16 //function struct
17 firstname       = "firstname";
18 firstname_new   = "firstname_new";
19 lastname        = "lastname";
20 lastname_new    = "lastname_new";
21 email           = "email";
22 email_new       = "email_new";
23 phone_new       = "phone_new";
24 // create a one dimensional struct
25 st = struct("firstname", firstname, "lastname", lastname, "email", email);
26 assert_checkequal(st.firstname, firstname);
27 assert_checkequal(st.lastname, lastname);
28 assert_checkequal(st.email, email);
29 //change some values
30 st.firstname    = firstname_new;
31 st.lastname     = lastname_new;
32 st.email        = email_new;
33 // add a phone field
34 st.phone        = phone_new;
35 assert_checkequal(st.firstname, firstname_new);
36 assert_checkequal(st.lastname, lastname_new);
37 assert_checkequal(st.email, email_new);
38 assert_checkequal(st.phone, phone_new);
39 clear st;
40 //create multi-dimentional struct (4x3x2)
41 n1 = 4;
42 n2 = 3;
43 n3 = 2;
44 firstnames{n1,n2,n3}    = [];
45 lastnames{n1,n2,n3}     = [];
46 emails{n1,n2,n3}        = [];
47 for i = 1:(n1*n2*n3)
48     sz              = string(i);
49     firstnames{i}   = "firstname_" + sz;
50     lastnames{i}    = "lastname_" + sz;
51     emails{i}       = "email_" + sz;
52 end
53 st = struct("firstname", firstnames, "lastname", lastnames, "email", emails);
54 for i = 1:24
55     if st(i).firstname <> firstnames{i} then pause end
56     if st(i).lastname <> lastnames{i} then pause end
57     if st(i).email <> emails{i} then pause end
58 end
59 clear firstnames
60 clear lastnames
61 clear emails
62 clear st;
63 //creating one dimensional structure by insertion
64 firstname   = "firstname";
65 lastname    = "lastname";
66 email       = "email";
67 st.firstname    = firstname;
68 st.lastname     = lastname;
69 st.email        = email;
70 assert_checkequal(st.firstname, firstname);
71 assert_checkequal(st.lastname, lastname);
72 assert_checkequal(st.email, email);
73 clear st;
74 //create multi dimentional struct by insertion
75 n1 = 4;
76 n2 = 3;
77 n3 = 2;
78 firstnames(n1,n2,n3)    = "";
79 lastnames(n1,n2,n3)     = "";
80 emails(n1,n2,n3)        = "";
81 for i = 1:(n1*n2*n3)
82     sz              = string(i);
83     firstnames(i)   = "firstname_" + sz;
84     lastnames(i)    = "lastname_" + sz;
85     emails(i)       = "email_" + sz;
86 end
87 //set dimension to 4x3x2
88 st(4,3,2).firstname = firstnames($);
89 st(4,3,2).lastname = lastnames($);
90 //fill struct as vector
91 for i = 1:(n1*n2*n3)
92     st(i).firstname = firstnames(i);
93     st(i).lastname  = lastnames(i);
94     st(i).email     = emails(i);
95 end
96 //check data on each dimension
97 for i = 1:n1
98     for j = i:n2
99         for k = 1:n3
100             assert_checkequal(st(i,j,k).firstname, firstnames(i,j,k));
101             assert_checkequal(st(i,j,k).lastname, lastnames(i,j,k));
102             assert_checkequal(st(i,j,k).email, emails(i,j,k));
103         end
104     end
105 end
106 clear firstnames
107 clear lastnames
108 clear emails
109 clear st;
110 //fields of an empty struct
111 fields = getfield(1,struct());
112 assert_checkequal(fields(1), "st");
113 assert_checkequal(fields(2), "dims");