Assert

Assert Commit Details

Date:2011-04-13 14:11:41 (7 years 7 months ago)
Author:Michael Baudin
Commit:121
Parents: 120
Message:Back-ported the changes from Scilab/master/assert_checkerror.
Changes:
M/tests/unit_tests/checkerror.dia.ref
M/tests/unit_tests/checkerror.tst

File differences

tests/unit_tests/checkerror.tst
11
2
2
33
44
55
......
88
99
1010
11
1211
1312
1413
......
1918
2019
2120
22
23
24
25
26
21
22
23
24
25
26
27
28
29
30
2731
2832
29
30
31
32
33
34
35
36
37
38
39
33
34
35
36
37
38
39
40
41
42
43
44
4045
4146
4247
43
48
49
50
4451
4552
4653
4754
4855
4956
50
57
58
5159
5260
5361
5462
5563
56
64
65
5766
5867
59
68
69
6070
6171
6272
6373
6474
6575
66
76
77
6778
6879
6980
......
7182
7283
7384
74
85
7586
7687
7788
7889
79
90
8091
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
81134
82135
83136
......
90143
91144
92145
93
146
94147
148
149
150
151
152
153
154
155
95156
96157
97158
98159
99
100
101
102
160
161
162
163
164
165
103166
104
167
105168
106169
107
170
108171
109172
110173
111
174
112175
113176
114
177
115178
116179
117180
118
181
119182
120183
121184
122185
123
186
124187
125188
126189
190
191
192
193
194
195
127196
128197
129198
......
133202
134203
135204
136
205
206
207
137208
138209
139210
140211
141212
142
213
214
215
143216
144
217
145218
146219
147220
148
221
222
223
149224
150225
151226
152
227
228
229
230
153231
154232
155233
156234
157
235
158236
159237
160238
161239
162240
163
241
164242
165243
166
// Copyright (C) 2008 - INRIA - Michael Baudin
// Copyright (C) 2010 - DIGITEO - Michael Baudin
// Copyright (C) 2010 - 2011 - DIGITEO - Michael Baudin
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
// <-- JVM NOT MANDATORY -->
// <-- ENGLISH IMPOSED -->
function flag = MY_assert_equal ( computed , expected )
if computed==expected then
if flag <> 1 then pause,end
endfunction
// Which error number to generate ?
// 201 "%s: Wrong type for argument %d: Real or complex matrix expected."
// 209 "%s: Wrong type for argument %d: Matrix expected."
// 53 "Wrong type for input argument %d: Real or complex matrix expected."
// 81 "%s: Wrong type for argument %d: Real or complex matrix expected."
// These tests are rather abstract.
// This is because we test the assert_checkerror function,
// which generates errors.
// To check that assert_checkerror performs well, we use
// execstr.
// We obviously do not want to use assert_checkerror, since a
// bug in assert_checkerror would make the current test fail in a
// weird way.
// Moreover, the current tests are localized, so that
// a Scilab in English or in French equally pass the test.
function y = f(x)
[lhs,rhs]=argn()
if ( rhs <> 1 ) then
errmsg = sprintf ( gettext ( "%s: Unexpected number of input arguments : %d provided while %d to %d are expected.") , "f" , rhs , 1 , 1 )
error(errmsg)
end
if ( typeof(x) <> "constant" ) then
localstr = gettext ( "%s: Unexpected type of input argument #%d : variable %s has type %s while %s is expected.")
errmsg = sprintf ( localstr , "f" , 1 , "x" , typeof(x) , "constant" )
error(errmsg,123456789)
end
y = x
[lhs,rhs]=argn()
if ( rhs <> 1 ) then
lstr=gettext("%s: Wrong number of input argument: %d expected.\n")
errmsg = sprintf ( lstr , "f" , 1 )
error(errmsg)
end
if ( typeof(x) <> "constant" ) then
lstr=gettext("%s: Wrong type for argument %d: Matrix expected.\n")
errmsg = sprintf ( lstr , "f" , 1 )
error(errmsg,123456789)
end
y = x
endfunction
///////////////////////////////////////////////
// Check our test-function f (the old shool way)
// 1. Check our test-function f (the old shool way),
// i.e. check that the function function is correctly written.
// The test of the assert_checkerror function starts at step 2.
MY_assert_equal ( f(2) , 2 );
//
instr = "f()";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." );
ferrmsg = msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"f",1);
MY_assert_equal ( lerr , ferrmsg );
//
instr = "f(""aa"")";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 123456789 );
lerr = lasterror();
MY_assert_equal ( lerr , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." );
ferrmsg = msprintf(gettext("%s: Wrong type for argument %d: Matrix expected.\n"),"f",1);
MY_assert_equal ( lerr , ferrmsg );
//
///////////////////////////////////////////////
// Check the error messages produced by assert_checkerror in case of wrong use of assert_checkerror
// 2. Check the error messages produced by assert_checkerror
// in case of wrong use of assert_checkerror
//
// Check error message when number of input arguments is false
instr = "assert_checkerror ( )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Unexpected number of input arguments : 0 provided while 2 to 3 are expected." );
asserterrmsg = sprintf ( gettext ( "%s: Wrong number of input argument: At least %d expected.\n") , "assert_checkerror" , 2 );
MY_assert_equal ( lerr , asserterrmsg );
//
// Check when number of output arguments is false
//
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 59 );
//
// Check error message when type of arguments of assert_checkerror is false
// Check error message when type of 1st argument of assert_checkerror is false
instr = "assert_checkerror ( 1 , """" )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Unexpected type of input argument #1 : variable instr has type constant while string is expected." );
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix of strings expected.\n") , "assert_checkerror" , 1 ) );
//
// Check error message when type of 2nd argument of assert_checkerror is false
instr = "assert_checkerror ( """" , 1 )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix of strings expected.\n") , "assert_checkerror" , 2 ) );
//
// Check error message when type of 3d argument of assert_checkerror is false
instr = "assert_checkerror ( """" , """" , """" )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix expected.\n") , "assert_checkerror" , 3 ) );
//
// Check error message when size of 1st argument of assert_checkerror is false
instr = "assert_checkerror ( ["""" """"], """" )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 1 , 1, 1) );
//
// Check error message when size of 2nd argument of assert_checkerror is false
instr = "assert_checkerror ( """" , ["""" """"] )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 2 , 1, 1) );
//
// Check error message when size of 3d argument of assert_checkerror is false
instr = "assert_checkerror ( """" , """", [1 2] )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 3 , 1, 1) );
//
// Check error message when value of 3d argument of assert_checkerror is false
instr = "assert_checkerror ( """" , """", -12 )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong value for input argument #%d: Non-negative integers expected.\n" ) , "assert_checkerror" , 3 ) );
//
// Check error message when no error is produced by f
instr = "assert_checkerror ( ""y=f(1)"" , ""foo"" )";
ierr=execstr(instr,"errcatch");
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: No error was produced while evaluating ""y=f(1)""." );
MY_assert_equal ( lerr , sprintf( gettext ( "%s: No error was produced while evaluating ""%s"".") , "assert_checkerror" , "y=f(1)" ) );
//
// Check error message when formatting the error message generates an error (!).
instr = "assert_checkerror ( ""y=f()"" , ""foo"" , [] , 2 )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
msprerrmsg = sprintf(gettext("%s: Wrong number of input arguments: at most %d expected.\n"),"msprintf",0);
MY_assert_equal ( lerr , sprintf( gettext ( "%s: Error while formatting the error message: ""%s""") , "assert_checkerror" , msprerrmsg ) );
//
///////////////////////////////////////////////
//
// Typical use-cases : the test pass
//
assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." );
assert_checkerror ( "y=f(""a"")" , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." );
assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." , 10000 );
assert_checkerror ( "y=f(""a"")" , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." , 123456789 );
msg1=msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"f",1);
msg2=msprintf(gettext("%s: Wrong type for argument %d: Matrix expected.\n"),"f",1);
assert_checkerror ( "y=f()" , msg1 );
assert_checkerror ( "y=f(""a"")" , msg2 );
assert_checkerror ( "y=f()" , msg1 , 10000 );
assert_checkerror ( "y=f(""a"")" , msg2 , 123456789 );
// Check error message when the good error is produced by f (and errmsg is not given)
flag = assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." );
flag = assert_checkerror ( "y=f()" , msg1 );
MY_assert_equal ( flag , %t );
//
flag = assert_checkerror ( "y=f(""a"")" , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." );
flag = assert_checkerror ( "y=f(""a"")" , msg2 );
MY_assert_equal ( flag , %t );
//
// Check error message and error number
flag = assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." , 10000 );
flag = assert_checkerror ( "y=f()" , msg1 , 10000 );
MY_assert_equal ( flag , %t );
//
flag = assert_checkerror ( "y=f(""a"")" , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." , 123456789 );
flag = assert_checkerror ( "y=f(""a"")" , msg2 , 123456789 );
MY_assert_equal ( flag , %t );
//
// Check error message when the good error is produced by f (and errmsg is an output argument)
[flag,errmsg] = assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." );
[flag,errmsg] = assert_checkerror ( "y=f()" , msg1 );
MY_assert_equal ( flag , %t );
MY_assert_equal ( errmsg , "" );
//
// Check error message and error number (and errmsg is given is an output argument)
[flag,errmsg] = assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." , 10000 );
[flag,errmsg] = assert_checkerror ( "y=f()" , msg1 , 10000 );
MY_assert_equal ( flag , %t );
MY_assert_equal ( errmsg , "" );
//
// Let assert_checkerror localize the message
lcl1="%s: Wrong number of input argument: %d expected.\n";
lcl2="%s: Wrong type for argument %d: Matrix expected.\n";
assert_checkerror ( "y=f()" , lcl1 , [] , "f" , 1);
assert_checkerror ( "y=f(""a"")" , lcl2 , [] , "f" , 1);
//
///////////////////////////////////////////////
//
// Typical use-cases : the test fails because the message is no the same
ierr = execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Assertion failed: expected error message = ""oups"" while computed error message = ""f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected.""." );
localstr = gettext("%s: Assertion failed: expected error message = ""%s"" while computed error message = ""%s"".");
asserterrmsg = msprintf(localstr,"assert_checkerror","oups",msg1);
MY_assert_equal ( lerr , asserterrmsg );
//
instr = "assert_checkerror ( ""y=f(""""a"""")"" , ""oups"" )";
ierr = execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Assertion failed: expected error message = ""oups"" while computed error message = ""f: Unexpected type of input argument #1 : variable x has type string while constant is expected.""." );
localstr = gettext("%s: Assertion failed: expected error message = ""%s"" while computed error message = ""%s"".");
asserterrmsg = msprintf(localstr,"assert_checkerror","oups",msg2);
MY_assert_equal ( lerr , asserterrmsg );
//
// Check when errmsg is given
// Check when errmsg output argument is given
//
[flag,errmsg]=assert_checkerror ( "y=f()" , "oups" );
MY_assert_equal ( flag , %f );
MY_assert_equal ( errmsg , "assert_checkerror: Assertion failed: expected error message = ""oups"" while computed error message = ""f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected.""." );
localstr = gettext("%s: Assertion failed: expected error message = ""%s"" while computed error message = ""%s"".");
asserterrmsg = msprintf(localstr,"assert_checkerror","oups",msg1);
MY_assert_equal ( errmsg , asserterrmsg );
//
[flag,errmsg]=assert_checkerror ( "y=f(""a"")" , "oups" );
MY_assert_equal ( flag , %f );
MY_assert_equal ( errmsg , "assert_checkerror: Assertion failed: expected error message = ""oups"" while computed error message = ""f: Unexpected type of input argument #1 : variable x has type string while constant is expected.""." );
localstr = gettext("%s: Assertion failed: expected error message = ""%s"" while computed error message = ""%s"".");
asserterrmsg = msprintf(localstr,"assert_checkerror","oups",msg2);
MY_assert_equal ( errmsg , asserterrmsg );
///////////////////////////////////////////////
//
// Typical use-cases : the test fails because the error number is no the same
//
instr = "assert_checkerror ( ""y=f()"" , ""f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected."" , 12 )";
instr = "assert_checkerror ( ""y=f()"" , "+sci2exp(msg1)+" , 12 )";
ierr = execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Assertion failed: expected error number = 12 while computed error number = 10000." );
//
[flag,errmsg]=assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." , 12 );
[flag,errmsg]=assert_checkerror ( "y=f()" , msg1 , 12 );
MY_assert_equal ( flag , %f );
MY_assert_equal ( errmsg , "assert_checkerror: Assertion failed: expected error number = 12 while computed error number = 10000." );
tests/unit_tests/checkerror.dia.ref
11
2
2
33
44
55
......
77
88
99
10
1110
1211
1312
......
1615
1716
1817
19
20
21
22
23
18
19
20
21
22
23
24
25
26
27
2428
25
26
27
28
29
30
31
32
33
34
35
29
30
31
32
33
34
35
36
37
38
39
40
3641
3742
38
43
44
45
3946
4047
4148
4249
4350
4451
45
52
53
4654
4755
4856
4957
5058
51
59
60
5261
5362
54
63
64
5565
5666
5767
5868
5969
6070
61
71
72
6273
6374
6475
......
6677
6778
6879
69
80
7081
7182
7283
7384
74
85
7586
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
76129
77130
78131
......
85138
86139
87140
88
141
89142
143
144
145
146
147
148
149
150
90151
91152
92153
93154
94
95
96
97
155
156
157
158
159
160
98161
99
162
100163
101164
102
165
103166
104167
105168
106
169
107170
108171
109
172
110173
111174
112175
113
176
114177
115178
116179
117180
118
181
119182
120183
121184
185
186
187
188
189
190
122191
123192
124193
......
128197
129198
130199
131
200
201
202
132203
133204
134205
135206
136207
137
208
209
210
138211
139
212
140213
141214
142215
143
216
217
218
144219
145220
146221
147
222
223
224
148225
149226
150227
151228
152
229
153230
154231
155232
156233
157234
158
235
159236
160237
// Copyright (C) 2008 - INRIA - Michael Baudin
// Copyright (C) 2010 - DIGITEO - Michael Baudin
// Copyright (C) 2010 - 2011 - DIGITEO - Michael Baudin
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
// <-- JVM NOT MANDATORY -->
// <-- ENGLISH IMPOSED -->
function flag = MY_assert_equal ( computed , expected )
if computed==expected then
flag = 1;
end
if flag <> 1 then bugmes();quit;end
endfunction
// Which error number to generate ?
// 201 "%s: Wrong type for argument %d: Real or complex matrix expected."
// 209 "%s: Wrong type for argument %d: Matrix expected."
// 53 "Wrong type for input argument %d: Real or complex matrix expected."
// 81 "%s: Wrong type for argument %d: Real or complex matrix expected."
// These tests are rather abstract.
// This is because we test the assert_checkerror function,
// which generates errors.
// To check that assert_checkerror performs well, we use
// execstr.
// We obviously do not want to use assert_checkerror, since a
// bug in assert_checkerror would make the current test fail in a
// weird way.
// Moreover, the current tests are localized, so that
// a Scilab in English or in French equally pass the test.
function y = f(x)
[lhs,rhs]=argn()
if ( rhs <> 1 ) then
errmsg = sprintf ( gettext ( "%s: Unexpected number of input arguments : %d provided while %d to %d are expected.") , "f" , rhs , 1 , 1 )
error(errmsg)
end
if ( typeof(x) <> "constant" ) then
localstr = gettext ( "%s: Unexpected type of input argument #%d : variable %s has type %s while %s is expected.")
errmsg = sprintf ( localstr , "f" , 1 , "x" , typeof(x) , "constant" )
error(errmsg,123456789)
end
y = x
[lhs,rhs]=argn()
if ( rhs <> 1 ) then
lstr=gettext("%s: Wrong number of input argument: %d expected.\n")
errmsg = sprintf ( lstr , "f" , 1 )
error(errmsg)
end
if ( typeof(x) <> "constant" ) then
lstr=gettext("%s: Wrong type for argument %d: Matrix expected.\n")
errmsg = sprintf ( lstr , "f" , 1 )
error(errmsg,123456789)
end
y = x
endfunction
///////////////////////////////////////////////
// Check our test-function f (the old shool way)
// 1. Check our test-function f (the old shool way),
// i.e. check that the function function is correctly written.
// The test of the assert_checkerror function starts at step 2.
MY_assert_equal ( f(2) , 2 );
//
instr = "f()";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." );
ferrmsg = msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"f",1);
MY_assert_equal ( lerr , ferrmsg );
//
instr = "f(""aa"")";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 123456789 );
lerr = lasterror();
MY_assert_equal ( lerr , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." );
ferrmsg = msprintf(gettext("%s: Wrong type for argument %d: Matrix expected.\n"),"f",1);
MY_assert_equal ( lerr , ferrmsg );
//
///////////////////////////////////////////////
// Check the error messages produced by assert_checkerror in case of wrong use of assert_checkerror
// 2. Check the error messages produced by assert_checkerror
// in case of wrong use of assert_checkerror
//
// Check error message when number of input arguments is false
instr = "assert_checkerror ( )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Unexpected number of input arguments : 0 provided while 2 to 3 are expected." );
asserterrmsg = sprintf ( gettext ( "%s: Wrong number of input argument: At least %d expected.\n") , "assert_checkerror" , 2 );
MY_assert_equal ( lerr , asserterrmsg );
//
// Check when number of output arguments is false
//
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 59 );
//
// Check error message when type of arguments of assert_checkerror is false
// Check error message when type of 1st argument of assert_checkerror is false
instr = "assert_checkerror ( 1 , """" )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Unexpected type of input argument #1 : variable instr has type constant while string is expected." );
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix of strings expected.\n") , "assert_checkerror" , 1 ) );
//
// Check error message when type of 2nd argument of assert_checkerror is false
instr = "assert_checkerror ( """" , 1 )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix of strings expected.\n") , "assert_checkerror" , 2 ) );
//
// Check error message when type of 3d argument of assert_checkerror is false
instr = "assert_checkerror ( """" , """" , """" )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong type for argument %d: Matrix expected.\n") , "assert_checkerror" , 3 ) );
//
// Check error message when size of 1st argument of assert_checkerror is false
instr = "assert_checkerror ( ["""" """"], """" )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 1 , 1, 1) );
//
// Check error message when size of 2nd argument of assert_checkerror is false
instr = "assert_checkerror ( """" , ["""" """"] )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 2 , 1, 1) );
//
// Check error message when size of 3d argument of assert_checkerror is false
instr = "assert_checkerror ( """" , """", [1 2] )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong size for input argument #%d: %d-by-%d matrix expected.\n") , "assert_checkerror" , 3 , 1, 1) );
//
// Check error message when value of 3d argument of assert_checkerror is false
instr = "assert_checkerror ( """" , """", -12 )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , sprintf ( gettext ( "%s: Wrong value for input argument #%d: Non-negative integers expected.\n" ) , "assert_checkerror" , 3 ) );
//
// Check error message when no error is produced by f
instr = "assert_checkerror ( ""y=f(1)"" , ""foo"" )";
ierr=execstr(instr,"errcatch");
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: No error was produced while evaluating ""y=f(1)""." );
MY_assert_equal ( lerr , sprintf( gettext ( "%s: No error was produced while evaluating ""%s"".") , "assert_checkerror" , "y=f(1)" ) );
//
// Check error message when formatting the error message generates an error (!).
instr = "assert_checkerror ( ""y=f()"" , ""foo"" , [] , 2 )";
ierr=execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
msprerrmsg = sprintf(gettext("%s: Wrong number of input arguments: at most %d expected.\n"),"msprintf",0);
MY_assert_equal ( lerr , sprintf( gettext ( "%s: Error while formatting the error message: ""%s""") , "assert_checkerror" , msprerrmsg ) );
//
///////////////////////////////////////////////
//
// Typical use-cases : the test pass
//
assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." );
assert_checkerror ( "y=f(""a"")" , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." );
assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." , 10000 );
assert_checkerror ( "y=f(""a"")" , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." , 123456789 );
msg1=msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"f",1);
msg2=msprintf(gettext("%s: Wrong type for argument %d: Matrix expected.\n"),"f",1);
assert_checkerror ( "y=f()" , msg1 );
assert_checkerror ( "y=f(""a"")" , msg2 );
assert_checkerror ( "y=f()" , msg1 , 10000 );
assert_checkerror ( "y=f(""a"")" , msg2 , 123456789 );
// Check error message when the good error is produced by f (and errmsg is not given)
flag = assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." );
flag = assert_checkerror ( "y=f()" , msg1 );
MY_assert_equal ( flag , %t );
//
flag = assert_checkerror ( "y=f(""a"")" , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." );
flag = assert_checkerror ( "y=f(""a"")" , msg2 );
MY_assert_equal ( flag , %t );
//
// Check error message and error number
flag = assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." , 10000 );
flag = assert_checkerror ( "y=f()" , msg1 , 10000 );
MY_assert_equal ( flag , %t );
//
flag = assert_checkerror ( "y=f(""a"")" , "f: Unexpected type of input argument #1 : variable x has type string while constant is expected." , 123456789 );
flag = assert_checkerror ( "y=f(""a"")" , msg2 , 123456789 );
MY_assert_equal ( flag , %t );
//
// Check error message when the good error is produced by f (and errmsg is an output argument)
[flag,errmsg] = assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." );
[flag,errmsg] = assert_checkerror ( "y=f()" , msg1 );
MY_assert_equal ( flag , %t );
MY_assert_equal ( errmsg , "" );
//
// Check error message and error number (and errmsg is given is an output argument)
[flag,errmsg] = assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." , 10000 );
[flag,errmsg] = assert_checkerror ( "y=f()" , msg1 , 10000 );
MY_assert_equal ( flag , %t );
MY_assert_equal ( errmsg , "" );
//
// Let assert_checkerror localize the message
lcl1="%s: Wrong number of input argument: %d expected.\n";
lcl2="%s: Wrong type for argument %d: Matrix expected.\n";
assert_checkerror ( "y=f()" , lcl1 , [] , "f" , 1);
assert_checkerror ( "y=f(""a"")" , lcl2 , [] , "f" , 1);
//
///////////////////////////////////////////////
//
// Typical use-cases : the test fails because the message is no the same
ierr = execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Assertion failed: expected error message = ""oups"" while computed error message = ""f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected.""." );
localstr = gettext("%s: Assertion failed: expected error message = ""%s"" while computed error message = ""%s"".");
asserterrmsg = msprintf(localstr,"assert_checkerror","oups",msg1);
MY_assert_equal ( lerr , asserterrmsg );
//
instr = "assert_checkerror ( ""y=f(""""a"""")"" , ""oups"" )";
ierr = execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Assertion failed: expected error message = ""oups"" while computed error message = ""f: Unexpected type of input argument #1 : variable x has type string while constant is expected.""." );
localstr = gettext("%s: Assertion failed: expected error message = ""%s"" while computed error message = ""%s"".");
asserterrmsg = msprintf(localstr,"assert_checkerror","oups",msg2);
MY_assert_equal ( lerr , asserterrmsg );
//
// Check when errmsg is given
// Check when errmsg output argument is given
//
[flag,errmsg]=assert_checkerror ( "y=f()" , "oups" );
MY_assert_equal ( flag , %f );
MY_assert_equal ( errmsg , "assert_checkerror: Assertion failed: expected error message = ""oups"" while computed error message = ""f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected.""." );
localstr = gettext("%s: Assertion failed: expected error message = ""%s"" while computed error message = ""%s"".");
asserterrmsg = msprintf(localstr,"assert_checkerror","oups",msg1);
MY_assert_equal ( errmsg , asserterrmsg );
//
[flag,errmsg]=assert_checkerror ( "y=f(""a"")" , "oups" );
MY_assert_equal ( flag , %f );
MY_assert_equal ( errmsg , "assert_checkerror: Assertion failed: expected error message = ""oups"" while computed error message = ""f: Unexpected type of input argument #1 : variable x has type string while constant is expected.""." );
localstr = gettext("%s: Assertion failed: expected error message = ""%s"" while computed error message = ""%s"".");
asserterrmsg = msprintf(localstr,"assert_checkerror","oups",msg2);
MY_assert_equal ( errmsg , asserterrmsg );
///////////////////////////////////////////////
//
// Typical use-cases : the test fails because the error number is no the same
//
instr = "assert_checkerror ( ""y=f()"" , ""f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected."" , 12 )";
instr = "assert_checkerror ( ""y=f()"" , "+sci2exp(msg1)+" , 12 )";
ierr = execstr(instr,"errcatch");
MY_assert_equal ( ierr , 10000 );
lerr = lasterror();
MY_assert_equal ( lerr , "assert_checkerror: Assertion failed: expected error number = 12 while computed error number = 10000." );
//
[flag,errmsg]=assert_checkerror ( "y=f()" , "f: Unexpected number of input arguments : 0 provided while 1 to 1 are expected." , 12 );
[flag,errmsg]=assert_checkerror ( "y=f()" , msg1 , 12 );
MY_assert_equal ( flag , %f );
MY_assert_equal ( errmsg , "assert_checkerror: Assertion failed: expected error number = 12 while computed error number = 10000." );

Archive Download the corresponding diff file

Revision: 121