diff options
author | Pierre MARECHAL <pierre.marechal@scilab.org> | 2009-03-23 14:00:40 +0100 |
---|---|---|
committer | Pierre MARECHAL <pierre.marechal@scilab.org> | 2009-03-23 14:00:40 +0100 |
commit | 443093754e54c86f26479703ffb353021775cbf3 (patch) | |
tree | 9069d1f9cd225dccbc11aad0405e7557ebe0b4ec /scilab | |
parent | 1fd523020e4af82f74c4c80203584324910da8eb (diff) | |
download | scilab-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')
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 | ||
11 | function r=isfield(s,field) | 13 | function r = isfield(s,field) |
12 | if ~isstruct(s) then | 14 | |
13 | r=0;return;end | 15 | r = %f; |
14 | w=getfield(1,s); | 16 | |
15 | r=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 | |||
16 | endfunction | 34 | endfunction |
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 | |||
22 | end | 22 | end |
23 | 23 | ||
24 | if floor(rhs/2)*2<>rhs then | 24 | if 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")); |
26 | end | 26 | end |
27 | 27 | ||
28 | nbfields=size(varargin)/2 | 28 | nbfields=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 | // ============================================================================= | ||
11 | my_struct = struct("field_1",123,"field_2",456); | ||
12 | // syntax | ||
13 | ierr = execstr("isfield()","errcatch"); | ||
14 | if ierr == 0 then bugmes();quit;end | ||
15 | if lasterror() <> "isfield: Wrong number of input argument(s): 2 expected." then bugmes();quit;end | ||
16 | ierr = execstr("isfield(my_struct)","errcatch"); | ||
17 | if ierr == 0 then bugmes();quit;end | ||
18 | if lasterror() <> "isfield: Wrong number of input argument(s): 2 expected." then bugmes();quit;end | ||
19 | ierr = execstr("isfield(my_struct,''field_1'',''field_2'')","errcatch"); | ||
20 | if ierr == 0 then bugmes();quit;end | ||
21 | ierr = execstr("isfield(my_struct,2)","errcatch"); | ||
22 | if ierr == 0 then bugmes();quit;end | ||
23 | if lasterror() <> "isfield: Wrong type for input argument #2: A string expected." then bugmes();quit;end | ||
24 | my_struct = ["field_1","field_2"]; | ||
25 | ierr = execstr("isfield(my_struct,''field_1'')","errcatch"); | ||
26 | if ierr == 0 then bugmes();quit;end | ||
27 | if lasterror() <> "isfield: Wrong type for input argument #1: struct array expected." then bugmes();quit;end | ||
28 | // Fonctionnality | ||
29 | my_struct = struct("field_1",123,"field_2",456); | ||
30 | if ~ isfield(my_struct,"field_1") then bugmes();quit;end | ||
31 | if ~ isfield(my_struct,"field_2") then bugmes();quit;end | ||
32 | if 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 | |||
14 | my_struct = struct("field_1",123,"field_2",456); | ||
15 | |||
16 | // syntax | ||
17 | |||
18 | ierr = execstr("isfield()","errcatch"); | ||
19 | if ierr == 0 then pause,end | ||
20 | if lasterror() <> "isfield: Wrong number of input argument(s): 2 expected." then pause, end | ||
21 | |||
22 | ierr = execstr("isfield(my_struct)","errcatch"); | ||
23 | if ierr == 0 then pause,end | ||
24 | if lasterror() <> "isfield: Wrong number of input argument(s): 2 expected." then pause, end | ||
25 | |||
26 | ierr = execstr("isfield(my_struct,''field_1'',''field_2'')","errcatch"); | ||
27 | if ierr == 0 then pause,end | ||
28 | |||
29 | ierr = execstr("isfield(my_struct,2)","errcatch"); | ||
30 | if ierr == 0 then pause,end | ||
31 | if lasterror() <> "isfield: Wrong type for input argument #2: A string expected." then pause, end | ||
32 | |||
33 | my_struct = ["field_1","field_2"]; | ||
34 | |||
35 | ierr = execstr("isfield(my_struct,''field_1'')","errcatch"); | ||
36 | if ierr == 0 then pause,end | ||
37 | if lasterror() <> "isfield: Wrong type for input argument #1: struct array expected." then pause, end | ||
38 | |||
39 | // Fonctionnality | ||
40 | |||
41 | my_struct = struct("field_1",123,"field_2",456); | ||
42 | |||
43 | if ~ isfield(my_struct,"field_1") then pause, end | ||
44 | if ~ isfield(my_struct,"field_2") then pause, end | ||
45 | if isfield(my_struct,"field_3") then pause, end | ||