summaryrefslogtreecommitdiffstats
path: root/scilab/modules/data_structures
diff options
context:
space:
mode:
authorPierre MARECHAL <pierre.marechal@scilab.org>2009-03-23 14:00:40 +0100
committerPierre MARECHAL <pierre.marechal@scilab.org>2009-03-23 14:00:40 +0100
commit443093754e54c86f26479703ffb353021775cbf3 (patch)
tree9069d1f9cd225dccbc11aad0405e7557ebe0b4ec /scilab/modules/data_structures
parent1fd523020e4af82f74c4c80203584324910da8eb (diff)
downloadscilab-443093754e54c86f26479703ffb353021775cbf3.zip
scilab-443093754e54c86f26479703ffb353021775cbf3.tar.gz
- isfield : check input parameters
- Add some tests of isfield functions - struct : bring precision to the error message
Diffstat (limited to 'scilab/modules/data_structures')
-rw-r--r--scilab/modules/data_structures/macros/isfield.sci30
-rw-r--r--scilab/modules/data_structures/macros/struct.sci2
-rw-r--r--scilab/modules/data_structures/tests/unit_tests/isfield.dia.ref32
-rw-r--r--scilab/modules/data_structures/tests/unit_tests/isfield.tst45
4 files changed, 102 insertions, 7 deletions
diff --git a/scilab/modules/data_structures/macros/isfield.sci b/scilab/modules/data_structures/macros/isfield.sci
index 5635bef..b85f9ef 100644
--- a/scilab/modules/data_structures/macros/isfield.sci
+++ b/scilab/modules/data_structures/macros/isfield.sci
@@ -1,5 +1,6 @@
1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) ????-2008 - INRIA 2// Copyright (C) 2004 - INRIA
3// Copyright (C) 2009 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
3// 4//
4// This file must be used under the terms of the CeCILL. 5// This file must be used under the terms of the CeCILL.
5// This source file is licensed as described in the file COPYING, which 6// This source file is licensed as described in the file COPYING, which
@@ -7,10 +8,27 @@
7// are also available at 8// are also available at
8// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt 9// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
9 10
11// This function returns TRUE if "field" is a field of the struct "s", FALSE otherwise
10 12
11function r=isfield(s,field) 13function r = isfield(s,field)
12if ~isstruct(s) then 14
13r=0;return;end 15 r = %f;
14w=getfield(1,s); 16
15r=bool2s(or(w(3:$)==field)); 17 rhs = argn(2);
18
19 if rhs <> 2 then
20 error(msprintf(gettext("%s: Wrong number of input argument(s): %d expected.\n"),"isfield",2));
21 end
22
23 if ~isstruct(s) then
24 error(msprintf(gettext("%s: Wrong type for input argument #%d: struct array expected.\n"),"isfield",1));
25 end
26
27 if type(field) <> 10 then
28 error(msprintf(gettext("%s: Wrong type for input argument #%d: A string expected.\n"),"isfield",2));
29 end
30
31 w = getfield(1,s);
32 r = or(w(3:$)==field);
33
16endfunction 34endfunction
diff --git a/scilab/modules/data_structures/macros/struct.sci b/scilab/modules/data_structures/macros/struct.sci
index 759d5f7..de41446 100644
--- a/scilab/modules/data_structures/macros/struct.sci
+++ b/scilab/modules/data_structures/macros/struct.sci
@@ -22,7 +22,7 @@ if rhs==0 then
22end 22end
23 23
24if floor(rhs/2)*2<>rhs then 24if floor(rhs/2)*2<>rhs then
25 error(msprintf(gettext("%s: Wrong number of input argument(s)."),'struct')); 25 error(msprintf(gettext("%s: Wrong number of input argument(s) : an even number is expected."),"struct"));
26end 26end
27 27
28nbfields=size(varargin)/2 28nbfields=size(varargin)/2
diff --git a/scilab/modules/data_structures/tests/unit_tests/isfield.dia.ref b/scilab/modules/data_structures/tests/unit_tests/isfield.dia.ref
new file mode 100644
index 0000000..e2cb1f6
--- /dev/null
+++ b/scilab/modules/data_structures/tests/unit_tests/isfield.dia.ref
@@ -0,0 +1,32 @@
1// =============================================================================
2// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3// Copyright (C) 2009 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
4//
5// This file is distributed under the same license as the Scilab package.
6// =============================================================================
7// <-- ENGLISH IMPOSED -->
8// <-- JVM NOT MANDATORY -->
9// unit tests for isfield function
10// =============================================================================
11my_struct = struct("field_1",123,"field_2",456);
12// syntax
13ierr = execstr("isfield()","errcatch");
14if ierr == 0 then bugmes();quit;end
15if lasterror() <> "isfield: Wrong number of input argument(s): 2 expected." then bugmes();quit;end
16ierr = execstr("isfield(my_struct)","errcatch");
17if ierr == 0 then bugmes();quit;end
18if lasterror() <> "isfield: Wrong number of input argument(s): 2 expected." then bugmes();quit;end
19ierr = execstr("isfield(my_struct,''field_1'',''field_2'')","errcatch");
20if ierr == 0 then bugmes();quit;end
21ierr = execstr("isfield(my_struct,2)","errcatch");
22if ierr == 0 then bugmes();quit;end
23if lasterror() <> "isfield: Wrong type for input argument #2: A string expected." then bugmes();quit;end
24my_struct = ["field_1","field_2"];
25ierr = execstr("isfield(my_struct,''field_1'')","errcatch");
26if ierr == 0 then bugmes();quit;end
27if lasterror() <> "isfield: Wrong type for input argument #1: struct array expected." then bugmes();quit;end
28// Fonctionnality
29my_struct = struct("field_1",123,"field_2",456);
30if ~ isfield(my_struct,"field_1") then bugmes();quit;end
31if ~ isfield(my_struct,"field_2") then bugmes();quit;end
32if isfield(my_struct,"field_3") then bugmes();quit;end
diff --git a/scilab/modules/data_structures/tests/unit_tests/isfield.tst b/scilab/modules/data_structures/tests/unit_tests/isfield.tst
new file mode 100644
index 0000000..b6665b6
--- /dev/null
+++ b/scilab/modules/data_structures/tests/unit_tests/isfield.tst
@@ -0,0 +1,45 @@
1// =============================================================================
2// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3// Copyright (C) 2009 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
4//
5// This file is distributed under the same license as the Scilab package.
6// =============================================================================
7
8// <-- ENGLISH IMPOSED -->
9// <-- JVM NOT MANDATORY -->
10
11// unit tests for isfield function
12// =============================================================================
13
14my_struct = struct("field_1",123,"field_2",456);
15
16// syntax
17
18ierr = execstr("isfield()","errcatch");
19if ierr == 0 then pause,end
20if lasterror() <> "isfield: Wrong number of input argument(s): 2 expected." then pause, end
21
22ierr = execstr("isfield(my_struct)","errcatch");
23if ierr == 0 then pause,end
24if lasterror() <> "isfield: Wrong number of input argument(s): 2 expected." then pause, end
25
26ierr = execstr("isfield(my_struct,''field_1'',''field_2'')","errcatch");
27if ierr == 0 then pause,end
28
29ierr = execstr("isfield(my_struct,2)","errcatch");
30if ierr == 0 then pause,end
31if lasterror() <> "isfield: Wrong type for input argument #2: A string expected." then pause, end
32
33my_struct = ["field_1","field_2"];
34
35ierr = execstr("isfield(my_struct,''field_1'')","errcatch");
36if ierr == 0 then pause,end
37if lasterror() <> "isfield: Wrong type for input argument #1: struct array expected." then pause, end
38
39// Fonctionnality
40
41my_struct = struct("field_1",123,"field_2",456);
42
43if ~ isfield(my_struct,"field_1") then pause, end
44if ~ isfield(my_struct,"field_2") then pause, end
45if isfield(my_struct,"field_3") then pause, end