[Index for tmp_for_tar/struct]
[Return to Master Index]
test_struct
(tmp_for_tar/struct/test_struct.m)
Function Synopsis
mytest( val, ermesg )
Help text
errors = test_struct
Test whether struct functions behave, and returns the number of errors.
Sets the global variables test_struct_errors (number of errors)
and test_struct_cnt (number of tests)
If a workspace variable 'quiet' is set to -1 the output is verbose.
If it is set to 1, output is minimal (error and test counts).
Otherwise, each error is reported with a short message and the error and ...
test counts are displayed at the end of the script (if it is reached).
Last modified: January 2000
Listing of function file tmp_for_tar/struct/test_struct.m
## errors = test_struct
##
## Test whether struct functions behave, and returns the number of errors.
##
## Sets the global variables test_struct_errors (number of errors)
## and test_struct_cnt (number of tests)
##
## If a workspace variable 'quiet' is set to -1 the output is verbose.
## If it is set to 1, output is minimal (error and test counts).
## Otherwise, each error is reported with a short message and the error and ...
## test counts are displayed at the end of the script (if it is reached).
##
## Author: Etienne Grossmann <etienne@isr.ist.utl.pt>
## Last modified: January 2000
1 ;
global test_struct_errors ;
global test_struct_cnt ;
global test_struct_quiet ;
test_struct_quiet = test_struct_errors = test_struct_cnt = 0 ;
if exist("quiet") == 1 ,
test_struct_quiet = quiet ;
end
function mytest( val, ermesg )
global test_struct_cnt ;
global test_struct_errors ;
global test_struct_quiet ;
% if ! exist("test_struct_quiet"), test_struct_quiet = 0 ; end
if val ,
if test_struct_quiet == -1,
printf("OK %i\n",test_struct_cnt) ;
end
else
if test_struct_quiet != 1,
printf("NOT OK %-4i : %s\n",test_struct_cnt,ermesg) ;
end
test_struct_errors++ ;
end
test_struct_cnt++ ;
endfunction
s.hello = 1 ;
s.world = 2 ;
mytest( isstruct(s) , "isstruct is buggy" ) ;
mytest( s.hello == getfield(s,"hello"), "getfield is buggy 1" ) ;
mytest( s.world == getfield(s,"world"), "getfield is buggy 2" ) ;
t = struct("hello",1,"world",2) ;
mytest( t.hello == s.hello , "struct is buggy 1" ) ;
mytest( t.world == s.world , "struct is buggy 2" ) ;
s.foo = "bar" ;
s.bye = "ciao" ;
t = setfield(t,"foo","bar","bye","ciao") ;
mytest( t.foo == s.foo , "setfield is buggy 1" ) ;
mytest( t.bye == s.bye , "setfield is buggy 2" ) ;
% s = struct() ;
t = rmfield(t,"foo","bye","hello") ;
mytest( ! struct_contains(t,"foo") , "rmfield is buggy 1" ) ;
mytest( ! struct_contains(t,"bye") , "rmfield is buggy 2" ) ;
mytest( ! struct_contains(t,"hello") , "rmfield is buggy 3" ) ;
mytest( t.world == s.world , "rmfield is buggy 4" ) ;
% Test "fields"
t = struct("hello",1,"wo",3,"rld",2) ;
% k1 = list("hello","wo","rld") ;
k1 = ["hello";"wo";"rld"] ;
k2 = fields(t,"string") ;
ok = 0 ;
p = [1,2,3 ; 1,3,2 ; 2,1,3 ; 2,3,1 ; 3,1,2 ; 3,2,1 ];
for i = 1:6,
if strcmp( [ k2(p(i,1),:),k2(p(i,2),:),k2(p(i,3),:) ],...
[ (k1(1,:)),(k1(2,:)),(k1(3,:))] ),
ok = 1 ;
end
end
## keyboard
mytest( ok , "fields is buggy 1" ) ;
% Test "fields"
vernum = split (OCTAVE_VERSION, ".") ;
if all( str2num(deblank(vernum(:,:))) >= [2;1;14] ) ,
t = struct("hello",1,"wo",3,"rld",2) ;
k1 = ["hello";"wo";"rld"] ;
k2 = fields(t) ;
ok = 0 ;
p = [1,2,3 ; 1,3,2 ; 2,1,3 ; 2,3,1 ; 3,1,2 ; 3,2,1 ];
for i = 1:6,
if strcmp( [ nth(k2,p(i,1)), nth(k2,p(i,2)), nth(k2,p(i,3))],...
[ deblank(k1(1,:)), deblank(k1(2,:)), deblank(k1(3,:))] ),
ok = 1 ;
end
end
mytest( ok , "fields is buggy 2" ) ;
end
# Test cmpstruct
t = struct("hello",1,"wo",3,"rld",2) ;
s = struct("hello",1,"wo",3,"rld",2) ;
mytest( cmpstruct(s,t) , "cmpstruct is buggy 1" ) ;
t = struct("hello",1,"wo",3,"rld",2) ;
s = struct("hello",2,"wo",3,"rld",2) ;
mytest( ! cmpstruct(s,t) , "cmpstruct is buggy 2" ) ;
t = struct("hello",[1,2],"wo",3,"rld",2) ;
s = struct("hello",[1,3],"wo",3,"rld",2) ;
mytest( ! cmpstruct(s,t) , "cmpstruct is buggy 3" ) ;
t = struct("hello",[1,2] ,"wo",3,"rld",2) ;
s = struct("hello",[1,2]',"wo",3,"rld",2) ;
mytest( ! cmpstruct(s,t) , "cmpstruct is buggy 4" ) ;
t = struct("hello",1,"wo",3,"rld",2) ;
s = struct("hello",1,"wo",3,"rld",2,"xyz",[1,2,3]) ;
mytest( ! cmpstruct(s,t) , "cmpstruct is buggy 5" ) ;
mytest( ! cmpstruct(t,s) , "cmpstruct is buggy 6" ) ;
t = struct("hello","world","wo","toto","rld",2) ;
s = struct("hello",1,"wo",3,"rld",2,"xyz",[1,2,3]) ;
mytest( ! cmpstruct(s,t) , "cmpstruct is buggy 7" ) ;
t = struct("hello","world","wo","toto","rld",2) ;
s = struct("wo","toto","rld",2,"hello","world") ;
mytest( cmpstruct(s,t) , "cmpstruct is buggy 8" ) ;
# Test isfield
t = struct("hello","world","wo","toto","rld",2) ;
mytest( isfield(t,"hello") , "isfield is buggy 1" ) ;
mytest( isfield(t,"wo") , "isfield is buggy 2" ) ;
mytest( isfield(t,"rld") , "isfield is buggy 3" ) ;
mytest( ! isfield(t,"toto") , "isfield is buggy 4" ) ;
mytest( ! isfield(t,"helloo") , "isfield is buggy 5" ) ;
mytest( ! isfield(t,"rl") , "isfield is buggy 6" ) ;
# Test catstruct
s = struct("hello",1,"wo",1,"rld",1,"bird",1) ;
t = struct("wo",2,"rld",2,"hello",2) ;
u = struct("hey",3,"rld",3,"x",3) ;
v = catstruct(s,t,u);
mytest( v.bird == s.bird , "catstruct is buggy 1" );
mytest( v.hello== t.hello , "catstruct is buggy 2" );
mytest( v.wo == t.wo , "catstruct is buggy 3" );
mytest( v.rld == u.rld , "catstruct is buggy 4" );
mytest( v.hey == u.hey , "catstruct is buggy 5" );
mytest( v.x == u.x , "catstruct is buggy 6" );
# Test tar, untar
x = 2 ; y = 3 ; z = "foo" ;
s = tar(x,y,z) ;
mytest( x == s.x , "tar is buggy 1" );
mytest( y == s.y , "tar is buggy 2" );
mytest( z == s.z , "tar is buggy 3" );
a = "x" ; b = "y" ;
[xx,yy,zz] = untar(s,a,b,"z") ;
mytest( x == xx , "untar is buggy 1" );
mytest( y == yy , "untar is buggy 2" );
mytest( z == zz , "untar is buggy 3" );
[x3,z3,y3] = untar(s,"x","z","foobar") ;
mytest( x == x3 , "untar is buggy 4" );
mytest( isempty(y3) , "untar is buggy 5" );
mytest( z == z3 , "untar is buggy 6" );
printf("Number of errors : %d out of %d tests\n",...
test_struct_errors,test_struct_cnt) ;
clear test_struct_quiet ;
Produced by oct2html on Wed Aug 8 19:47:02 2001
Cross-Directory links are: ON