]> Creatis software - CreaPhase.git/blob - octave_packages/struct-1.0.10/test_struct.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / struct-1.0.10 / test_struct.m
1 ## Copyright (C) 2000 Etienne Grossmann <etienne@egdn.net>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 ##       errors = test_struct
17 ##
18 ## Test whether struct functions behave, and returns the number of errors.
19 ##   
20 ## Sets the global variables test_struct_errors (number of errors)
21 ##                     and   test_struct_cnt    (number of tests) 
22 ##
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).
27 ##
28
29 1 ;
30 global test_struct_errors  ;
31 global test_struct_cnt  ;
32 test_struct_verbose = test_struct_errors = test_struct_cnt = 0 ;
33
34 if ! exist ("verbose"), verbose = 0; end
35 if exist ("verbose") && ! isglobal ("verbose")
36   tmp = verbose;
37   global verbose = tmp;
38 end
39
40 function mytest( val, tag )     # My test function ###################
41
42 global test_struct_cnt ;
43 global test_struct_errors  ;
44 global verbose ;
45
46 % if ! exist("test_struct_verbose"), test_struct_verbose = 0 ; end
47
48 if val ,
49   if verbose
50     printf("OK %i\n",test_struct_cnt) ; 
51   end
52 else
53   if verbose 
54     printf("NOT OK %-4i : %s\n",test_struct_cnt,tag) ;
55   end
56   test_struct_errors++ ;
57 end
58 test_struct_cnt++ ;
59 endfunction                     # EOF my test function ###############
60
61
62
63
64 s.hello = 1 ;
65 s.world = 2 ;
66 mytest( isstruct(s)                   , "isstruct" ) ;
67 mytest( s.hello == getfields(s,"hello"), "getfields 1" ) ;
68 mytest( s.world == getfields(s,"world"), "getfields 2" ) ; 
69
70 t = struct ("hello",1,"world",2) ;
71 mytest( t.hello == s.hello            , "struct 1" ) ;
72 mytest( t.world == s.world            , "struct 2" ) ;
73
74 s.foo = "bar" ;
75 s.bye = "ciao" ;
76 t = setfields (t,"foo","bar","bye","ciao") ;
77 mytest( t.foo == s.foo                , "setfields 1" ) ;
78 mytest( t.bye == s.bye                , "setfields 2" ) ;
79
80 % s = struct() ;
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" ) ;
89
90
91                                 # Test tars, getfield
92 x = 2 ; y = 3 ; z = "foo" ;
93 s = tars (x,y,z);
94
95 mytest( x == s.x                          , "tars 1" );
96 mytest( y == s.y                          , "tars 2" );
97 mytest( z == s.z                          , "tars 3" );
98
99 a = "x" ; b = "y" ; 
100 [xx,yy,zz] = getfields (s,a,b,"z") ;
101
102 mytest( x == xx                           , "getfields 1" );
103 mytest( y == yy                           , "getfields 2" );
104 mytest( z == zz                           , "getfields 3" );
105
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" );
110
111 ## Broken
112 ##oo(1,1).f0= 1;
113 ##oo= setfield(oo,{1,2},'fd',{3},'b', 6);
114 ##mytest( getfield(oo,{1,2},'fd',{3},'b') == 6, "getfield 6" );
115
116 try                             # Should not return inexistent fields
117   [nothing] = getfields (s,"foo");
118   found_nothing = 0;
119 catch
120   found_nothing = 1;
121 end
122 mytest( found_nothing                     , "getfields 4" );
123
124
125 ok = test_struct_errors == 0;
126 if verbose
127   if ok
128     printf ("All %d tests ok\n", test_struct_cnt);
129   else
130     printf("There were %d errors in of %d tests\n",...
131            test_struct_errors,test_struct_cnt) ;
132   end
133 end