1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) 2010 - DIGITEO - Vincent COUVERT <vincent.couvert@scilab.org>
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
14function testOk = m2sciTestExecution(functionName)
15
16    mfilesPath = "SCI/modules/m2sci/tests/unit_tests/mfiles/";
17
18    exec("SCI/modules/m2sci/tests/unit_tests/utils/sci_m2sciUnknownDims.sci", -1);
19    exec("SCI/modules/m2sci/tests/unit_tests/utils/sci_m2sciUnknownType.sci", -1);
20
21    mfile2sci(mfilesPath + functionName + ".m", TMPDIR, %F, %F, 0, %T);
22
23    loadmatfile(mfilesPath + functionName + ".mat");
24
25    // Rename all res* variable to matres*
26    allVars = who("local");
27    numberOfMatRes = 0;
28    for kVar = 1:size(allVars, "*")
29        if part(allVars(kVar), 1:3)=="res" then
30            execstr("mat" + allVars(kVar) + "=" + allVars(kVar));
31            execstr("clear " + allVars(kVar));
32            numberOfMatRes = numberOfMatRes + 1;
33        end
34    end
35
36    exec("SCI/modules/m2sci/tests/unit_tests/utils/m2sciUnknownDims.sci", -1);
37    exec("SCI/modules/m2sci/tests/unit_tests/utils/m2sciUnknownType.sci", -1);
38
39    exec(TMPDIR + filesep() + functionName + ".sci", -1);
40
41    // Check that Scilab defined as many res* as Matlab
42    allVars = who("local");
43    allResNames = [];
44    for kVar = 1:size(allVars, "*")
45        if part(allVars(kVar), 1:3)=="res" then
46            allResNames($+1) = allVars(kVar);
47        end
48    end
49
50    if size(allResNames, "*")<>(numberOfMatRes/2) then
51        disp("Wrong number of results: " + string(size(allResNames, "*")) + " <> " + string(numberOfMatRes/2));
52        testOk = %F;
53        return
54    end
55
56    exec("SCI/modules/m2sci/tests/unit_tests/utils/m2sciParseInfos.sci", -1);
57    exec("SCI/modules/m2sci/tests/unit_tests/utils/m2sciCompareResults.sci", -1);
58    for kRes = 1: size(allResNames, "*")
59        resName = allResNames(kRes);
60
61        // For each result, compare the information
62        [matSize, matType, matProp] = m2sciParseInfos(evstr("mat" + resName + "_Infos"));
63
64        sciRes = evstr(resName);
65        sciSize = size(sciRes);
66
67        if type(sciRes)==10 then
68            sciType = "String";
69            sciProp = "Real";
70        elseif type(sciRes)==4 then
71            sciType = "Boolean";
72            sciProp = "Real";
73        elseif type(sciRes)==6 then
74            sciType = "Sparse";
75            sciProp = "Real";
76        elseif type(sciRes)==5 then
77            if isreal(sciRes,0) then
78                sciType = "Sparse";
79                sciProp = "Real";
80            else
81                sciType = "Sparse";
82                sciProp = "Complex";
83            end
84        else
85            if isreal(sciRes,0) then
86                sciType = "Double";
87                sciProp = "Real";
88            else
89                sciType = "Double";
90                sciProp = "Complex";
91            end
92        end
93
94        // Verify size between Scilab and Matlab
95        if sciType=="String" then
96            if or(sciSize<>matSize) & sciSize(matSize<>sciSize)<>1 then
97                // Error
98                disp(resName + ": " + sci2exp(sciSize) + " <> " + sci2exp(matSize))
99                testOk = %F
100                return
101            elseif or(sciSize<>matSize) then
102                // WARNING: Error for string length
103                disp(resName + ": " + sci2exp(sciSize) + " <> " + sci2exp(matSize));
104            end
105        else
106            if or(sciSize<>matSize) then
107                disp(resName + ": " + sci2exp(sciSize) + " <> " + sci2exp(matSize));
108                if prod(sciSize)==0 & prod(matSize)==0 then // Empty matrix size
109                    // WARNING: Error for empty matrix
110                else
111                    // Error
112                    testOk = %F
113                    return
114                end
115            end
116        end
117
118        // Verify type between Scilab and Matlab
119        if sciType<>matType then
120            disp(resName + ": " + string(sciType) + " <> " + string(matType));
121            if sciType<>"constant" & matType<>"Boolean" then
122                // Error
123                testOk = %F
124                return
125            else
126                // WARNING: Error for empty matrix of boolean
127            end
128        end
129
130        // Verify prop between Scilab and Matlab
131        if sciProp<>matProp then
132            disp(resName + ": " + string(sciProp) + " <> " + string(matProp));
133            testOk = %F;
134            return
135        end
136
137        // Compare results
138        if ~m2sciCompareResults(sciRes, evstr("mat" + resName)) then
139            disp([resName + " (scilab): " + sci2exp(sciRes, 0); resName + " (matlab): " + sci2exp(evstr("mat" + resName), 0)])
140            testOk = %F
141            return
142        end
143    end
144
145    testOk = %T
146
147endfunction
148
149