1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// =============================================================================
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2011 - DIGITEO - Michael Baudin
//
// This file is distributed under the same license as the Scilab package.
// =============================================================================
// <-- JVM NOT MANDATORY -->
function [flag,errmsg] = assert_equal ( computed , expected )
// Check that computed and expected are equal.
function flag = comparedoubles ( computed , expected )
[cnonan , cnumb] = mythrownan(computed)
[enonan , enumb] = mythrownan(expected)
if ( and(enonan == cnonan) & and(enumb == cnumb) ) then
flag = %t
else
flag = %f
end
endfunction
function [nonan,numb] = mythrownan(x)
[lhs,rhs]=argn(0)
if ( rhs<>1 ) then
error(msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"thrownan",1))
end
numb=find(bool2s(~isnan(x)))
nonan=x(~isnan(x))
endfunction
[lhs,rhs]=argn()
if ( rhs <> 2 ) then
errmsg = sprintf ( gettext ( "%s: Wrong number of input arguments: %d expected.\n") , "assert_checkequal" , 2 )
error(errmsg)
end
//
// Check types of variables
if ( typeof(computed) <> typeof(expected) ) then
errmsg = sprintf ( gettext ( "%s: Incompatible input arguments #%d and #%d: Same types expected.\n" ) , "assert_checkequal" , 1 , 2 )
error(errmsg)
end
//
// Check sizes of variables
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_checkequal" , 1 , 2 )
error(errmsg)
end
//
if ( type(computed) == 1 & type(expected) == 1 ) then
// These are two matrices of doubles
cisreal = isreal(computed)
eisreal = isreal(expected)
if ( cisreal & ~eisreal ) then
errmsg = sprintf ( gettext ( "%s: Computed is real, but expected is complex.") , "assert_checkequal" )
error(errmsg)
end
if ( ~cisreal & eisreal ) then
errmsg = sprintf ( gettext ( "%s: Computed is complex, but expected is real.") , "assert_checkequal" )
error(errmsg)
end
if ( cisreal & eisreal ) then
flag = comparedoubles ( computed , expected )
else
flagreal = comparedoubles ( real(computed) , real(expected) )
if ( flagreal ) then
flagimag = comparedoubles ( imag(computed) , imag(expected) )
flag = flagimag
else
flag = %f
end
end
else
if ( and ( computed == expected ) ) then
flag = %t
else
flag = %f
end
end
if ( flag == %t ) then
errmsg = ""
else
// Change the message if the matrix contains more than one value
if ( size(expected,"*") == 1 ) then
estr = string(expected)
else
estr = "[" + string(expected(1)) + " ...]"
end
if ( size(computed,"*") == 1 ) then
cstr = string(computed)
else
cstr = "[" + string(computed(1)) + " ...]"
end
errmsg = msprintf(gettext("%s: Assertion failed: expected = %s while computed = %s"),"assert_checkequal",estr,cstr)
if ( lhs < 2 ) then
// If no output variable is given, generate an error
error ( errmsg )
end
end
endfunction
c = complex(1,2);
assert_equal([real(c);imag(c)],[1;2]);
//
c = complex([1 2],[3 4]);
assert_equal([real(c);imag(c)],[1 2;3 4]);
//
c = complex(%inf,%inf);
assert_equal([real(c);imag(c)],[%inf;%inf]);
//
c = complex(%inf,%nan);
assert_equal([real(c);imag(c)],[%inf;%nan]);
//
c = complex(%nan,%nan);
assert_equal([real(c);imag(c)],[%nan;%nan]);
//
c = complex(%nan,%inf);
assert_equal([real(c);imag(c)],[%nan;%inf]);
//
c = complex(0,%nan);
assert_equal([real(c);imag(c)],[0;%nan]);
//
c = complex(0,%inf);
assert_equal([real(c);imag(c)],[0;%inf]);
//
c = complex(1);
assert_equal([real(c);imag(c)],[1;0]);
//
c = complex([1 2]);
assert_equal([real(c);imag(c)],[1 2;0 0]);
//
c = complex([1 2 3], 4);
assert_equal([real(c);imag(c)],[1 2 3;4 4 4]);
//
c = complex(1, [2 3 4]);
assert_equal([real(c);imag(c)],[1 1 1;2 3 4]);
|