1 // Copyright (C) 2008-2009 - INRIA - Michael Baudin
2 // Copyright (C) 2010 - 2011 - DIGITEO - Michael Baudin
3 // Copyright (C) 2012 - 2016 - Scilab Enterprises
4 // Copyright (C) 2019 - 2020 - Samuel GOUGEON
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.
13 function [flag, errmsg] = assert_checkequal(computed, expected)
14 // Check that computed and expected are equal.
17 errmsg = gettext("%s: Wrong number of input arguments: %d expected.\n")
18 error(msprintf(errmsg, "assert_checkequal", 2))
21 // Check types of variables
22 if ( typeof(computed) <> typeof(expected) ) then
23 errmsg = gettext("%s: Incompatible input arguments #%d and #%d: Same types expected.\n")
24 error(msprintf(errmsg, "assert_checkequal", 1, 2))
28 // Check sizes of variables
29 if type(computed)==15 then
30 ncom = length(computed)
31 nexp = length(expected)
32 elseif or(typeof(computed)==["ce" "st"])
39 catch // non-sizeable objects: 1:$, iolib, sin, sind, etc
44 if ( or(ncom <> nexp) ) then
45 errmsg = msprintf(gettext ( "%s: Incompatible input arguments #%d and #%d: Same sizes expected.\n"), "assert_checkequal", 1 , 2)
49 // sparse or full real or complex matrices
50 if or(type(computed) == [1 5]) then
51 cisreal = isreal(computed)
52 eisreal = isreal(expected)
53 if ( cisreal & ~eisreal ) then
54 errmsg = msprintf(gettext("%s: Computed is real, but expected is complex."), "assert_checkequal")
57 if ( ~cisreal & eisreal ) then
58 errmsg = msprintf(gettext("%s: Computed is complex, but expected is real."), "assert_checkequal")
61 if cisreal & eisreal then
62 [flag, k] = comparedoubles ( computed , expected )
64 [flag, k] = comparedoubles ( real(computed) , real(expected) )
66 [flag ,k] = comparedoubles ( imag(computed) , imag(expected) )
69 // k is the index of the first discrepancy (or [] if none)
71 elseif or(typeof(computed)==["implicitlist" "fptr" "function"])
72 // http://bugzilla.scilab.org/16104 C) D) E)
73 flag = computed==expected
75 if typeof(computed) == "implicitlist"
76 errmsg = _("%s: Assertion failed: expected= %s while computed= %s")
77 errmsg = msprintf(errmsg,"assert_checkequal",string(expected),string(computed))
79 elseif typeof(computed) == "function"
80 c = macr2tree(computed).name+"()"
81 e = macr2tree(expected).name+"()"
82 errmsg = _("%s: Assertion failed: expected= %s while computed= %s")
83 errmsg = msprintf(errmsg,"assert_checkequal", e, c)
86 // no way to get the names of built-in functions
87 errmsg = _("%s: Assertion failed: expected and computed are two distinct built-in functions.")
88 errmsg = msprintf(errmsg,"assert_checkequal")
91 assert_generror ( errmsg )
96 elseif type(computed) == 14 // library : http://bugzilla.scilab.org/16104#c1
97 flag = and(string(computed)==string(expected))
99 errmsg = gettext("%s: Assertion failed: expected= %s while computed= %s")
100 c = "lib@" + string(computed)(1)
101 e = "lib@" + string(expected)(1)
102 errmsg = msprintf(errmsg,"assert_checkequal", e, c)
104 assert_generror ( errmsg )
109 elseif or(type(computed)==[15 16 17 ])
110 [flag, k] = compareContainers(computed , expected)
112 elseif type(computed) == 0
116 b = and(computed == expected)
117 flag = b || isequal(computed, expected)
119 k = find(computed<>expected, 1);
127 // Sets the message according to the type and size of the pair:
128 if or(typeof(expected) == ["sparse", "boolean sparse"])
129 estr = string(full(expected(k)))
132 if isdef("k","l") & k <> []
135 err = execstr("e = "+s+"; t = type("+s+")", "errcatch")
143 estr = msprintf("%s(uid:%d)", e.type, e.uid)
149 if or(typeof(computed) == ["sparse", "boolean sparse"])
150 cstr = full(computed(k))
153 if isdef("k","l") & k <> []
156 err = execstr("c = "+s+"; t = type("+s+")", "errcatch")
164 cstr = msprintf("%s(uid:%d)", c.type, c.uid)
170 if isdef("k","l") & k <> [] & length(computed)>1
171 estr = msprintf(_("expected(%d)= "),k) + estr
172 cstr = msprintf(_("computed(%d)= "),k) + cstr
174 estr = _("expected= ") + estr
175 cstr = _("computed= ") + cstr
178 ierr = execstr("mdiff = string(mean(computed - expected))", "errcatch");
179 if ( ierr == 0 ) then
180 errmsg = msprintf(gettext("%s: Assertion failed: %s while %s (mean diff = %s)"),"assert_checkequal",estr, cstr, mdiff)
182 errmsg = msprintf(gettext("%s: Assertion failed: %s while %s"),"assert_checkequal", estr, cstr)
185 // If no output variable is given, generate an error
186 assert_generror ( errmsg )
190 // ---------------------------------------------------------------------------
191 function [flag, k] = comparedoubles ( computed , expected )
192 rand("seed",getdate("s"))
194 while find(expected==joker | computed==joker,1)<>[]
197 computed(isnan(computed)) = joker;
198 expected(isnan(expected)) = joker;
199 k = find(expected<>computed,1);
202 // ---------------------------------------------------------------------------
203 function [areEqual, k] = compareContainers(computed , expected)
204 // http://bugzilla.scilab.org/15293
205 // http://bugzilla.scilab.org/16274
206 tc = typeof(computed)
207 te = typeof(expected)
213 if or(type(computed)==[1 5])
214 if and(computed == expected)
217 if isreal(computed) <> isreal(expected)
221 [areEqual, k] = comparedoubles(real(computed), real(expected))
223 [areEqual, k] = comparedoubles(imag(computed), imag(expected))
226 elseif or(type(computed)==[16 17]) then
227 if and(computed == expected)
230 if or(size(computed) <> size(expected)) then
234 fc = fieldnames(computed)
235 areEqual = and(fc == fieldnames(expected))
241 [areEqual, k] = compareContainers(computed(f) , expected(f))
247 [areEqual, k] = compareContainers(computed{:} , expected{:})
253 elseif type(computed)==14 // Libraries
254 areEqual = and(string(computed)==string(expected))
257 if and(computed == expected)
260 if length(computed) <> length(expected)
264 dfc = definedfields(computed)
265 dfe = definedfields(expected)
267 if length(dfc)==length(dfe)
268 k = find(dfc <> dfe, 1)
270 tmp = union(setdiff(dfc, dfe), setdiff(dfe, dfc))
277 areEqual = compareContainers(computed(k) , expected(k))
283 elseif (tc=="void" & te=="void")
286 elseif type(computed) <> 0
287 b = and(computed == expected)
288 areEqual = b || isequal(computed, expected)
290 k = find(computed <> expected, 1);