X-Git-Url: http://gitweb.scilab.org/?p=scilab.git;a=blobdiff_plain;f=scilab%2Fmodules%2Fdevelopment_tools%2Fmacros%2Fassert%2Fassert_checkalmostequal.sci;h=0f647d5d09704b918a8f909087637c2b38d4b1c9;hp=f851fd8928faee9981b03643d878aa3627be6399;hb=ff833d1a46b5dfb3accbba9e9911c22857ae31d4;hpb=07dffc43cdfbf52b957307da35584d1ebe653c9d diff --git a/scilab/modules/development_tools/macros/assert/assert_checkalmostequal.sci b/scilab/modules/development_tools/macros/assert/assert_checkalmostequal.sci index f851fd8..0f647d5 100644 --- a/scilab/modules/development_tools/macros/assert/assert_checkalmostequal.sci +++ b/scilab/modules/development_tools/macros/assert/assert_checkalmostequal.sci @@ -1,6 +1,7 @@ // Copyright (C) 2008 - 2009 - INRIA - Michael Baudin // Copyright (C) 2009 - 2011 - DIGITEO - Michael Baudin // Copyright (C) 2012 - Michael Baudin +// Copyright (C) 2017 - Samuel GOUGEON // // Copyright (C) 2012 - 2016 - Scilab Enterprises // @@ -12,23 +13,25 @@ // along with this program. function [flag,errmsg] = assert_checkalmostequal ( varargin ) - // Returns the indices where - // * kpinf : x(kpinf) == +%inf, - // * kninf : x(kninf) == -%inf, - // * knan : x(knan) is a %nan - // * kreg : x(kreg) is not an infinity, not a nan - // * xreg = x(kreg) - // These 4 sets of indices have no intersection. - // - // Example : - // x = [1 2 3 -%inf %inf %nan %inf %nan -%inf 4 5 6] - // [kpinf , kninf , knan , kreg , xreg] = infnanindices ( x ) - // xreg = [1 2 3 4 5 6] - // kreg = [1 2 3 10 11 12] - // knan = [6 8] - // kninf = [4 9] - // kpinf = [5 7] + function [kpinf , kninf , knan , kreg , xreg] = infnanindices ( x ) + // Returns the indices where + // * kpinf : x(kpinf) == +%inf, + // * kninf : x(kninf) == -%inf, + // * knan : x(knan) is a %nan + // * kreg : x(kreg) is not an infinity, not a nan + // * xreg = x(kreg) + // These 4 sets of indices have no intersection. + // + // Example : + // x = [1 2 3 -%inf %inf %nan %inf %nan -%inf 4 5 6] + // [kpinf , kninf , knan , kreg , xreg] = infnanindices ( x ) + // xreg = [1 2 3 4 5 6] + // kreg = [1 2 3 10 11 12] + // knan = [6 8] + // kninf = [4 9] + // kpinf = [5 7] + kpinf = find(x==%inf) kninf = find(x==-%inf) knan = find(isnan(x)) @@ -36,7 +39,6 @@ function [flag,errmsg] = assert_checkalmostequal ( varargin ) xreg = x(kreg) endfunction - function areequal = assert_arealmostequal ( computed , expected , reltol , abstol , comptype ) // // Decompose the expected value into nan indices, inf indices and regular indices @@ -69,6 +71,7 @@ function [flag,errmsg] = assert_checkalmostequal ( varargin ) // * the +%inf must be at the same place, // * the -%inf must be at the same place, // * the %nan must be at the same place. + areequal = ( areclose & and(kcpinf == kepinf) & and(kcninf == keninf) & and(kcnan == kenan) ) endfunction @@ -86,14 +89,14 @@ function [flag,errmsg] = assert_checkalmostequal ( varargin ) end end endfunction - + // ============================================================================================ // Check that computed and expected are numerically close. [lhs,rhs]=argn() if ( and(rhs <> [2 3 4 5] ) ) then - errmsg = sprintf ( gettext ( "%s: Wrong number of input arguments: %d to %d expected.") , "assert_checkalmostequal" , 2 , 5 ) - error(errmsg) + errmsg = gettext("%s: Wrong number of input arguments: %d to %d expected.") + error(msprintf(errmsg, "assert_checkalmostequal" , 2 , 5 )) end // // Get arguments @@ -104,61 +107,75 @@ function [flag,errmsg] = assert_checkalmostequal ( varargin ) comptype = assert_argindefault ( rhs , varargin , 5 , "element" ) // // Check types of variables - if ( and(typeof(computed) <> ["constant" "sparse" "hypermat"]) ) then - errmsg = sprintf ( gettext ( "%s: Wrong type for input argument #%d: Matrix expected.\n") , "assert_checkalmostequal" , 1 ) - error(errmsg) + if ( and(typeof(computed) <> ["constant" "sparse" "polynomial"]) ) then + errmsg = gettext("%s: Wrong type for input argument #%d: Matrix expected.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 1)) + end + if ( and(typeof(expected) <> ["constant" "sparse" "polynomial"]) ) then + errmsg = gettext("%s: Wrong type for input argument #%d: Matrix expected.\n") + error(msprintf (errmsg, "assert_checkalmostequal" , 2 )) end - if ( and(typeof(expected) <> ["constant" "sparse" "hypermat"]) ) then - errmsg = sprintf ( gettext ( "%s: Wrong type for input argument #%d: Matrix expected.\n") , "assert_checkalmostequal" , 2 ) - error(errmsg) + if typeof(computed)=="polynomial" & typeof(expected)~="polynomial" then + errmsg = gettext("%s: Wrong type for input argument #%d: Polynomial expected.\n") + error(msprintf (errmsg, "assert_checkalmostequal" , 2 )) end if ( typeof(reltol) <> "constant" ) then - errmsg = sprintf ( gettext ( "%s: Wrong type for input argument #%d: Matrix expected.\n") , "assert_checkalmostequal" , 3 ) - error(errmsg) + errmsg = gettext("%s: Wrong type for input argument #%d: Matrix expected.\n") + error(sprintf(errmsg, "assert_checkalmostequal", 3 )) end if ( typeof(abstol) <> "constant" ) then - errmsg = sprintf ( gettext ( "%s: Wrong type for input argument #%d: Matrix expected.\n") , "assert_checkalmostequal" , 4 ) - error(errmsg) + errmsg = gettext("%s: Wrong type for input argument #%d: Matrix expected.\n") + error(sprintf(errmsg, "assert_checkalmostequal", 4 )) end if ( typeof(comptype) <> "string" ) then - errmsg = sprintf ( gettext ( "%s: Wrong type for input argument #%d: Matrix of strings expected.\n") , "assert_checkalmostequal" , 5 ) - error(errmsg) + errmsg = gettext("%s: Wrong type for input argument #%d: Matrix of strings expected.\n") + error(sprintf(errmsg, "assert_checkalmostequal", 5)) end // // Check sizes of variables if ( size(reltol,"*") <> 1 ) then - errmsg = sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkalmostequal" , 3 , 1 , 1 ) - error(errmsg) + errmsg = gettext("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 3, 1, 1)) end if ( size(abstol,"*") <> 1 ) then - errmsg = sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkalmostequal" , 4 , 1 , 1 ) - error(errmsg) + errmsg = gettext("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 4, 1, 1)) end if ( size(comptype,"*") <> 1 ) then - errmsg = sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkalmostequal" , 5 , 1 , 1 ) - error(errmsg) + errmsg = gettext("%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 5, 1, 1)) end // // Check values of variables if ( reltol < 0 ) then - errmsg = sprintf ( gettext ( "%s: Wrong value for input argument #%d: Must be > %d.\n") , "assert_checkalmostequal" , 3 , 0 ) - error(errmsg) + errmsg = gettext("%s: Wrong value for input argument #%d: Must be > %d.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 3, 0)) end if ( abstol < 0 ) then - errmsg = sprintf ( gettext ( "%s: Wrong value for input argument #%d: Must be > %d.\n") , "assert_checkalmostequal" , 4 , 0 ) - error(errmsg) + errmsg = gettext("%s: Wrong value for input argument #%d: Must be > %d.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 4, 0)) end if ( and ( comptype <> ["matrix" "element"] ) ) then - errmsg = sprintf ( gettext ( "%s: Wrong value for input argument #%d: Must be in the set {%s}.\n") , "assert_checkalmostequal" , 5 , """matrix"",""element""" ) - error(errmsg) + errmsg = gettext("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 5, """matrix"",""element""")) end // // Proceed... ncom = size(computed) nexp = size(expected) if ( or(ncom <> nexp) ) then - errmsg = sprintf ( gettext ( "%s: Incompatible input arguments #%d and #%d: Same sizes expected.\n" ) , "assert_checkalmostequal" , 1 , 2 ) - error(errmsg) + errmsg = gettext("%s: Incompatible input arguments #%d and #%d: Same sizes expected.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 1, 2)) + end + if typeof(computed)=="polynomial" then + if or(degree(computed) <> degree(expected) ) then + errmsg = gettext("%s: Arguments #%d and #%d: Same degrees expected.\n") + error(msprintf(errmsg, "assert_checkalmostequal", 1, 2)) + end + computed0 = computed; + expected0 = expected; + computed = coeff(computed(:)); + expected = coeff(expected(:)); end // areequal_re = assert_arealmostequal ( real(computed) , real(expected) , reltol , abstol , comptype ) @@ -173,6 +190,10 @@ function [flag,errmsg] = assert_checkalmostequal ( varargin ) errmsg = "" else flag = %f + if isdef("computed0","l") then + computed = computed0; + expected = expected0; + end // Change the message if the matrix contains more than one value if ( size(expected,"*") == 1 ) then estr = string(expected) @@ -186,7 +207,8 @@ function [flag,errmsg] = assert_checkalmostequal ( varargin ) end relstr = string(reltol) absstr = string(abstol) - errmsg = msprintf(gettext("%s: Assertion failed: expected = %s while computed = %s"),"assert_checkalmostequal",estr,cstr) + errmsg = gettext("%s: Assertion failed: expected = %s while computed = %s") + errmsg = msprintf(errmsg, "assert_checkalmostequal", estr, cstr) if ( lhs < 2 ) then // If no output variable is given, generate an error assert_generror ( errmsg )