Home > utilities > testmex.m

# testmex

## PURPOSE

TESTMEX tests that the behaviour of all the mex files is correct

## SYNOPSIS

function []=testmex()

## DESCRIPTION

     TESTMEX tests that the behaviour of all the mex files is correct

## CROSS-REFERENCE INFORMATION

This function calls:

- testmexsumsparse TESTMEXSUMSPARSE tests that the behaviour of mexsumsparse() is correct
- testmextrdsdsmat TESTMEXTRDSDSMAT tests that the behaviour of mextrdsdsmat() is correct

This function is called by:

## SUBFUNCTIONS

- function []=testmextrdsdsmat()
- function [ok]=onetestmextrdsds(testname,A,S1,B,S2,ref)
- function [] = testmexsumsparse()
- function [ok] = onerandtestmexsumsparse(testname,n,m,density)

## SOURCE CODE

    0001
    0002 function []=testmex()
    0003 % TESTMEX tests that the behaviour of all the mex files is correct
    0004
    0005 % This file is a part of PENLAB package distributed under GPLv3 license
    0006 % Copyright (c) 2013 by  J. Fiala, M. Kocvara, M. Stingl
    0007 % Last Modified: 27 Nov 2013
    0008
    0009   disp('Testing mextrdsdsmat()...');
    0010   testmextrdsdsmat()
    0011
    0012   disp('Testing mexsumsparse()...');
    0013   testmexsumsparse()
    0014
    0015 end
    0016
    0017 % run all tests on mextrdsdsmat()
    0018 function []=testmextrdsdsmat()
    0019
    0020   onetestmextrdsds('n=0, all empty', [],[],[],[],0);
    0021   onetestmextrdsds('n=1, all ones', 1,1,1,1,2);
    0022   onetestmextrdsds('n=1, all nonzeros', 2,3,-4,5,-120*2);
    0023   onetestmextrdsds('n=1, A=0', 0,3,-4,5,0);
    0024   onetestmextrdsds('n=1, S1=0', 2,0,-4,5,0);
    0025   onetestmextrdsds('n=1, B=0', 2,3,0,5,0);
    0026   onetestmextrdsds('n=1, S2=0', 2,3,-4,0,0);
    0027   %onetestmextrdsds('n=2, all numbers, A diag', diag([2 3]),[-2.1,3.2;3.2,4.4],magic(2)+3,-magic(2)^2); % magic is not symmetric!!!
    0028   onetestmextrdsds('n=2, all numbers, A diag', diag([2 3]),[-2.1,3.2;3.2,4.4],[4,6;6,5],[-13,-9;-9,-16]);
    0029   onetestmextrdsds('n=2, all numbers, A diag, B=[]', diag([2 3]),[-2.1,3.2;3.2,4.4],magic(2)+3,sparse(2,2),0);
    0030
    0031   % build special examples with very sparse matrices
    0032   n=5;
    0033   A=diag(1:n);
    0034   B=magic(n)+magic(n)';
    0035   S1=sparse(n,n); S1(2,1)=2;
    0036   S1=S1+S1';
    0037   S2=sparse(n,n); S2(4,4)=9;
    0038   onetestmextrdsds('n=5, very sparse', A,S1,B,S2);
    0039
    0040   n=5;
    0041   A=diag(1:n);
    0042   B=magic(n)+magic(n)';
    0043   S1=sparse(n,n); S1(2,1)=2;
    0044   S1=S1+S1';
    0045   S2=sparse(n,n); S2(2,2)=9;
    0046   onetestmextrdsds('n=5, very sparse2', A,S1,B,S2);
    0047
    0048   n=5;
    0049   A=zeros(n,n);
    0050   A(:)=[1:n^2]-10;
    0051   A=A+A';
    0052   B=magic(n)+magic(n)';
    0053   S1=sparse(n,n); S1(2,1)=2;
    0054   S1=S1+S1';
    0055   S2=sparse(n,n); S2(2,2)=9;
    0056   onetestmextrdsds('n=5, very sparse3', A,S1,B,S2);
    0057
    0058   n=5;
    0059   A=zeros(n,n);
    0060   A(:)=[1:n^2]-10;
    0061   A=A+A';
    0062   B=magic(n)+magic(n)';
    0063   S1=sparse(n,n); S1(2,1)=2;
    0064   S1=S1+S1';
    0065   S2=sparse(diag([5:9]));
    0066   onetestmextrdsds('n=5, sparse, S2 spdiag', A,S1,B,S2);
    0067
    0068
    0069   % random of different size
    0070   for n=[5 5 5 10 10 10 100 100 999 1000]
    0071   %for n=[2 5 5]
    0072     A=rand(n,n);
    0073     A=A+A';
    0074     B=rand(n,n);
    0075     B=B+B';
    0076     S1=sprandsym(n,0.1);
    0077     S2=sprandsym(n,0.05);
    0078     testname=sprintf('n=%i all random',n);
    0079     onetestmextrdsds(testname, A,S1,B,S2);
    0080   end
    0081
    0082
    0083 end
    0084
    0085 % run one test with mextrdsdsmat()
    0086 %   testname - string to print out
    0087 %   A,B,S1,S2 - matrices for the test
    0088 %   red [optional] - reference result, if not present, computed in Matlab
    0089 function [ok]=onetestmextrdsds(testname,A,S1,B,S2,ref)
    0090
    0091   ok=false;
    0092
    0093   if (~issparse(S1))
    0094     S1=sparse(S1);
    0095   end
    0096   if (~issparse(S2))
    0097     S2=sparse(S2);
    0098   end
    0099
    0100   if (nargin==5)
    0101     %ref = trace(A*S1*B*S2);
    0102     ref = trace(A*S1*B*S2) + trace(A*S2*B*S1);
    0103   end
    0104   %result = mextrdsdsmat(A,S1,B,S2);
    0105   result = mextrcolumn(A,S1,B,{S2});
    0106
    0107   if (result==ref)
    0108     fprintf(' test %-30s: ok %15.6e\n', testname,result);
    0109     ok=true;
    0110   elseif (abs(result-ref)<1e-10*(abs(result+ref)))
    0111     fprintf(' test %-30s: ok (tol)  %15.6e %15.6e\n', testname,ref,result);
    0112     ok=true;
    0113   else
    0114     fprintf(' test %-30s: FAILED  %15.6e %15.6e\n', testname,ref,result);
    0115   end
    0116
    0117 end
    0118
    0119 % run all tests on mexsumsparse()
    0120 function [] = testmexsumsparse()
    0121
    0122   % some of Si will be empty...
    0123   %onerandtestmexsumsparse('n=1, a few',1,20,0.8);
    0124
    0125   onerandtestmexsumsparse('n=5, a few',5,20,0.8);
    0126
    0127   onerandtestmexsumsparse('n=30 x1, a few',30,1,0.3);
    0128   onerandtestmexsumsparse('n=30 x2, a few',30,2,0.3);
    0129   onerandtestmexsumsparse('n=30 x50, a few',30,50,0.1);
    0130
    0131   onerandtestmexsumsparse('n=401 x50, a few',401,50,0.1);
    0132
    0133   %onerandtestmexsumsparse(testname,n,m,density);
    0134
    0135 end
    0136
    0137 % run one random test of mexsumsparse
    0138 %   n - dimension of matrices
    0139 %   m - number of matrices
    0140 %   density (0..1) - density of the matrices
    0141 function [ok] = onerandtestmexsumsparse(testname,n,m,density)
    0142
    0143   ok = false;
    0144
    0145   % generate the problem data
    0146   S = cell(m,1);
    0147   w = rand(m,1);
    0148   for i=1:m
    0149     S{i} = sprandsym(n,density);
    0150   end
    0151
    0152   % compute reference result (as dense)
    0153   ref = zeros(n,n);
    0154   for i=1:m
    0155     ref = ref + w(i)*S{i};
    0156   end
    0157
    0158   % call mex to generate result as dense
    0159   rd = mexsumsparse(w,S,-1);
    0160
    0161   err = norm(ref-rd,inf);
    0162   nrm = norm(ref,inf);
    0163
    0164   if (err<1e-10*nrm)
    0165     fprintf(' test (dense) %-30s: ok (tol)  %15.6e %15.6e\n', testname,err,nrm);
    0166     ok=true;
    0167   else
    0168     fprintf(' test (dense) %-30s: FAILED  %15.6e %15.6e\n', testname,err,nrm);
    0169   end
    0170
    0171   % call mex to generate result as sparse (auto)
    0172   rd = mexsumsparse(w,S);
    0173
    0174   err = norm(ref-rd,inf);
    0175
    0176   if (err<1e-10*nrm)
    0177     fprintf(' test (auto)  %-30s: ok (tol)  %15.6e %15.6e\n', testname,err,nrm);
    0178     ok=true;
    0179   else
    0180     fprintf(' test (auto)  %-30s: FAILED  %15.6e %15.6e\n', testname,err,nrm);
    0181   end
    0182
    0183 end