1 ## Copyright (C) 2000 Etienne Grossmann <etienne@egdn.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## errors = test_struct
18 ## Test whether struct functions behave, and returns the number of errors.
20 ## Sets the global variables test_struct_errors (number of errors)
21 ## and test_struct_cnt (number of tests)
23 ## If a workspace variable 'verbose' is set to -1 the output is verbose.
24 ## If it is set to 1, output is minimal (error and test counts).
25 ## Otherwise, each error is reported with a short message and the error and ...
26 ## test counts are displayed at the end of the script (if it is reached).
30 global test_struct_errors ;
31 global test_struct_cnt ;
32 test_struct_verbose = test_struct_errors = test_struct_cnt = 0 ;
34 if ! exist ("verbose"), verbose = 0; end
35 if exist ("verbose") && ! isglobal ("verbose")
40 function mytest( val, tag ) # My test function ###################
42 global test_struct_cnt ;
43 global test_struct_errors ;
46 % if ! exist("test_struct_verbose"), test_struct_verbose = 0 ; end
50 printf("OK %i\n",test_struct_cnt) ;
54 printf("NOT OK %-4i : %s\n",test_struct_cnt,tag) ;
56 test_struct_errors++ ;
59 endfunction # EOF my test function ###############
66 mytest( isstruct(s) , "isstruct" ) ;
67 mytest( s.hello == getfields(s,"hello"), "getfields 1" ) ;
68 mytest( s.world == getfields(s,"world"), "getfields 2" ) ;
70 t = struct ("hello",1,"world",2) ;
71 mytest( t.hello == s.hello , "struct 1" ) ;
72 mytest( t.world == s.world , "struct 2" ) ;
76 t = setfields (t,"foo","bar","bye","ciao") ;
77 mytest( t.foo == s.foo , "setfields 1" ) ;
78 mytest( t.bye == s.bye , "setfields 2" ) ;
81 % t = rmfield (t,"foo","bye","hello") ; % no longer works with octave func
82 t = rmfield( t, "foo");
83 t = rmfield( t, "bye");
84 t = rmfield( t, "hello");
85 mytest( ! isfield(t,"foo") , "rmfield 1" ) ;
86 mytest( ! isfield(t,"bye") , "rmfield 2" ) ;
87 mytest( ! isfield(t,"hello") , "rmfield 3" ) ;
88 mytest( t.world == s.world , "rmfield 4" ) ;
92 x = 2 ; y = 3 ; z = "foo" ;
95 mytest( x == s.x , "tars 1" );
96 mytest( y == s.y , "tars 2" );
97 mytest( z == s.z , "tars 3" );
100 [xx,yy,zz] = getfields (s,a,b,"z") ;
102 mytest( x == xx , "getfields 1" );
103 mytest( y == yy , "getfields 2" );
104 mytest( z == zz , "getfields 3" );
106 [x3,z3,z4] = getfields (s,"x","z","z") ;
107 mytest( x == x3 , "getfields 4" );
108 mytest( z == z3 , "getfields 5" );
109 mytest( z == z4 , "getfields 6" );
113 ##oo= setfield(oo,{1,2},'fd',{3},'b', 6);
114 ##mytest( getfield(oo,{1,2},'fd',{3},'b') == 6, "getfield 6" );
116 try # Should not return inexistent fields
117 [nothing] = getfields (s,"foo");
122 mytest( found_nothing , "getfields 4" );
125 ok = test_struct_errors == 0;
128 printf ("All %d tests ok\n", test_struct_cnt);
130 printf("There were %d errors in of %d tests\n",...
131 test_struct_errors,test_struct_cnt) ;