Try to give more information in case of error
[scilab.git] / scilab / modules / development_tools / macros / assert / assert_checkequal.sci
1 // Copyright (C) 2008-2009 - INRIA - Michael Baudin
2 // Copyright (C) 2010 - 2011 - DIGITEO - Michael Baudin
3 //
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
9
10 function [flag,errmsg] = assert_checkequal ( computed , expected )
11     //  Check that computed and expected are equal.
12
13     function flag = comparedoubles ( computed , expected )
14         [cnonan , cnumb] = mythrownan(computed)
15         [enonan , enumb] = mythrownan(expected)
16         if ( and(enonan == cnonan) & and(enumb == cnumb) ) then
17             flag = %t
18         else
19             flag = %f
20         end
21     endfunction
22
23     function [nonan,numb] = mythrownan(x)
24         //
25         //
26         // Copyright (C) 2000 - INRIA - Carlos Klimann
27         // Copyright (C) 2010 - 2011 - DIGITEO - Michael Baudin
28         //This  function  returns  in  vector  nonan  the  values
29         //(ignoring the NANs) of a  vector or matrix x and in the
30         //corresponding places of vector  numb the indexes of the
31         //value.
32         //
33         //For  a  vector  or matrix  x,  [nonan,numb]=thrownan(x)
34         //considers x, whatever his dimensions are, like a linear
35         //vector (columns  first).
36         //
37         //
38         [lhs,rhs]=argn(0)
39         if ( rhs<>1 ) then 
40             error(msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"thrownan",1))
41         end
42         numb=find(bool2s(~isnan(x)))
43         nonan=x(~isnan(x))
44     endfunction
45
46     [lhs,rhs]=argn()
47     if ( rhs <> 2 ) then
48         errmsg = sprintf ( gettext ( "%s: Wrong number of input arguments: %d expected.\n") , "assert_checkequal" , 2 )
49         error(errmsg)
50     end
51     //
52     // Check types of variables
53     if ( typeof(computed) <> typeof(expected) ) then
54         errmsg = sprintf ( gettext ( "%s: Incompatible input arguments #%d and #%d: Same types expected.\n" ) , "assert_checkequal" , 1 , 2 )
55         error(errmsg)
56     end
57     //
58     // Check sizes of variables
59     if ( or(type(computed)==[16 17]) ) then
60         ncom = length(computed)
61     else
62         ncom = size(computed)
63     end
64     if ( or(type(expected)==[16 17]) ) then
65         nexp = length(expected)
66     else
67         nexp = size(expected)
68     end
69     if ( or(ncom <> nexp) ) then
70         errmsg = sprintf ( gettext ( "%s: Incompatible input arguments #%d and #%d: Same sizes expected.\n") , "assert_checkequal" , 1 , 2 )
71         error(errmsg)
72     end  
73     //
74     if ( type(computed) == 1 & type(expected) == 1 ) then
75         // These are two matrices of doubles
76         cisreal = isreal(computed)
77         eisreal = isreal(expected)
78         if ( cisreal & ~eisreal ) then
79             errmsg = sprintf ( gettext ( "%s: Computed is real, but expected is complex.") , "assert_checkequal" )
80             error(errmsg)
81         end
82         if ( ~cisreal & eisreal ) then
83             errmsg = sprintf ( gettext ( "%s: Computed is complex, but expected is real.") , "assert_checkequal" )
84             error(errmsg)
85         end
86         if ( cisreal & eisreal ) then
87             flag = comparedoubles ( computed , expected )
88         else
89             flagreal = comparedoubles ( real(computed) , real(expected) )
90             if ( flagreal ) then
91                 flagimag = comparedoubles ( imag(computed) , imag(expected) )
92                 flag = flagimag
93             else
94                 flag = %f
95             end
96         end
97     else
98         if ( and ( computed == expected ) ) then
99             flag = %t
100         else
101             flag = %f
102         end
103     end
104     if ( flag == %t ) then
105         errmsg = ""
106     else
107         // Change the message if the matrix contains more than one value
108         if ( size(expected,"*") == 1 ) then
109             if ( typeof(expected) == "sparse") then
110                 val = full(expected)
111             else
112                 val = expected
113             end
114             estr = string(val)
115         else
116             if ( typeof(expected) == "sparse") then
117                 val = full(expected(1))
118             else
119                 val = expected(1)
120             end
121             estr = "[" + string(val) + " ...]"
122         end
123         if ( size(computed,"*") == 1 ) then
124             if ( typeof(computed) == "sparse") then
125                 val = full(computed)
126             else
127                 val = computed
128             end
129             cstr = string(val)
130         else
131             if ( typeof(computed) == "sparse") then
132                 val = full(computed(1))
133             else
134                 val = computed(1)
135             end
136             cstr = "[" + string(val) + " ...]"
137         end
138         ierr = execstr("mdiff = string(mean(computed - expected))", "errcatch");
139         if ( ierr == 0 ) then
140             errmsg = msprintf(gettext("%s: Assertion failed: expected = %s while computed = %s (mean diff = %s)"),"assert_checkequal",estr,cstr,mdiff)
141         else
142             errmsg = msprintf(gettext("%s: Assertion failed: expected = %s while computed = %s"),"assert_checkequal",estr,cstr)
143         end
144         if ( lhs < 2 ) then
145             // If no output variable is given, generate an error
146             assert_generror ( errmsg )
147         end
148     end
149 endfunction
150
151