scilab: check value and overflow errors in integer typemaps

This commit is contained in:
Simon Marchetto 2014-02-07 11:53:19 +01:00
commit ccab675c57
5 changed files with 97 additions and 42 deletions

View file

@ -1,6 +1,10 @@
exec("swigtest.start", -1);
// Check passing by value
if (val_double(42) <> 42) then swigtesterror(); end
if (val_float(42) <> 42) then swigtesterror(); end
if (val_char('a') <> 'a') then swigtesterror(); end
if (val_schar(42) <> 42) then swigtesterror(); end
if (val_schar(int8(42)) <> 42) then swigtesterror(); end
@ -18,18 +22,17 @@ if (val_long(42) <> 42) then swigtesterror(); end
if (val_long(int32(42)) <> 42) then swigtesterror(); end
if (val_ulong(uint32(42)) <> uint32(42)) then swigtesterror(); end
if (val_bool(%t) <> %t) then swigtesterror(); end
//if (val_bool(1) <> %t) then swigtesterror(); end
//if (val_llong(42) <> 42) then swigtesterror(); end
//if (val_llong(int64(42)) <> 42) then swigtesterror(); end
//if (val_ullong(uint64(42)) <> uint64(42)) then swigtesterror(); end
if (val_float(42) <> 42) then swigtesterror(); end
if (val_double(42) <> 42) then swigtesterror(); end
if (val_bool(%t) <> %t) then swigtesterror(); end
//if (val_bool(1) <> %t) then swigtesterror(); end
// Check passing by reference
//if (ref_float(42) <> 42) then swigtesterror(); end
//if (ref_double(42) <> 42) then swigtesterror(); end
if (ref_char('a') <> 'a') then swigtesterror(); end
if (ref_schar(42) <> 42) then swigtesterror(); end
if (ref_schar(int8(42)) <> 42) then swigtesterror(); end
@ -47,15 +50,31 @@ if (ref_long(42) <> 42) then swigtesterror(); end
if (ref_long(int32(42)) <> 42) then swigtesterror(); end
if (ref_ulong(uint32(42)) <> uint32(42)) then swigtesterror(); end
if (ref_bool(%t) <> %t) then swigtesterror(); end
//if (ref_bool(1) <> %t) then swigtesterror(); end
//if (ref_llong(42) <> 42) then swigtesterror(); end
//if (ref_llong(int64(42)) <> 42) then swigtesterror(); end
//if (ref_ullong(42) <> 42) then swigtesterror(); end
//if (ref_ullong(uint64(42)) <> uint64(42)) then swigtesterror(); end
//if (ref_float(42) <> 42) then swigtesterror(); end
//if (ref_double(42) <> 42) then swigtesterror(); end
// check errors
ierr = execstr('val_schar(2^9)', 'errcatch');
if ierr <> 999 then swigtesterror(); end
ierr = execstr('val_schar(100.2)', 'errcatch');
if ierr <> 999 then swigtesterror(); end
ierr = execstr('val_short(2^17)', 'errcatch');
if ierr <> 999 then swigtesterror(); end
ierr = execstr('val_short(100.2)', 'errcatch');
if ierr <> 999 then swigtesterror(); end
ierr = execstr('val_int(2^33)', 'errcatch');
if ierr <> 999 then swigtesterror(); end
ierr = execstr('val_int(100.2)', 'errcatch');
if ierr <> 999 then swigtesterror(); end
ierr = execstr('val_long(2^65)', 'errcatch');
if ierr <> 999 then swigtesterror(); end
ierr = execstr('val_long(100.2)', 'errcatch');
if ierr <> 999 then swigtesterror(); end
if (ref_bool(%t) <> %t) then swigtesterror(); end
//if (ref_bool(1) <> %t) then swigtesterror(); end
exec("swigtest.quit", -1);

View file

@ -1,9 +1,9 @@
/*
* C-type: int
* Scilab type: double or 32-bit signed integer
* Scilab type: double or int32
*/
%fragment(SWIG_AsVal_frag(int), "header", fragment="SWIG_SciDoubleOrInt32_AsInt") {
%fragment(SWIG_AsVal_frag(int), "header", fragment="SWIG_SciDoubleOrInt32_AsInt", fragment="<limits.h>") {
%#define SWIG_AsVal_int(scilabValue, valuePointer) SWIG_SciDoubleOrInt32_AsInt(pvApiCtx, scilabValue, valuePointer, SWIG_Scilab_GetFname())
}
%fragment("SWIG_SciDoubleOrInt32_AsInt", "header") {
@ -39,9 +39,8 @@ SWIG_SciDoubleOrInt32_AsInt(void *_pvApiCtx, SwigSciObject _iVar, int *_piValue,
}
if (iPrec != SCI_INT32) {
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
sciErr = getMatrixOfInteger32(_pvApiCtx, piAddrVar, &iRows, &iCols, &piData);
if (sciErr.iErr) {
printError(&sciErr, 0);
@ -49,12 +48,13 @@ SWIG_SciDoubleOrInt32_AsInt(void *_pvApiCtx, SwigSciObject _iVar, int *_piValue,
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A 32-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
*_piValue = *piData;
}
else if (iType == sci_matrix) {
double *pdData = NULL;
double dValue = 0.0f;
sciErr = getMatrixOfDouble(_pvApiCtx, piAddrVar, &iRows, &iCols, &pdData);
if (sciErr.iErr) {
@ -63,13 +63,22 @@ SWIG_SciDoubleOrInt32_AsInt(void *_pvApiCtx, SwigSciObject _iVar, int *_piValue,
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A 32-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
*_piValue = (int) *pdData;
dValue = *pdData;
if (dValue != floor(dValue)) {
Scierror(999, _("%s: Incorrect value for input argument #%d: The double value cannot be converted to a 32-bit signed integer.\n"), _fname, _iVar);
return SWIG_ValueError;
}
if ((dValue < INT_MIN) || (dValue > INT_MAX)) {
Scierror(999, _("%s: Overflow error for input argument #%d: The double value cannot be converted to a 32-bit signed integer.\n"), _fname, _iVar);
return SWIG_OverflowError;
}
*_piValue = (int) dValue;
}
else {
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
return SWIG_OK;

View file

@ -3,7 +3,7 @@
* Scilab type: double or int32
*/
%fragment(SWIG_AsVal_frag(long), "header", fragment="SWIG_SciDoubleOrInt32_AsLong") {
%fragment(SWIG_AsVal_frag(long), "header", fragment="SWIG_SciDoubleOrInt32_AsLong", fragment="<limits.h>") {
%#define SWIG_AsVal_long(scilabValue, valuePointer) SWIG_SciDoubleOrInt32_AsLong(pvApiCtx, scilabValue, valuePointer, SWIG_Scilab_GetFname());
}
%fragment("SWIG_SciDoubleOrInt32_AsLong", "header") {
@ -38,9 +38,8 @@ SWIG_SciDoubleOrInt32_AsLong(void *_pvApiCtx, SwigSciObject _iVar, long *_plValu
}
if (iPrec != SCI_INT32) {
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
sciErr = getMatrixOfInteger32(_pvApiCtx, piAddrVar, &iRows, &iCols, &piData);
if (sciErr.iErr) {
printError(&sciErr, 0);
@ -48,12 +47,13 @@ SWIG_SciDoubleOrInt32_AsLong(void *_pvApiCtx, SwigSciObject _iVar, long *_plValu
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A 32-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
*_plValue = (long) *piData;
}
else if (iType == sci_matrix) {
double *pdData = NULL;
double dValue = 0.0f;
sciErr = getMatrixOfDouble(_pvApiCtx, piAddrVar, &iRows, &iCols, &pdData);
if (sciErr.iErr) {
@ -62,13 +62,22 @@ SWIG_SciDoubleOrInt32_AsLong(void *_pvApiCtx, SwigSciObject _iVar, long *_plValu
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A 32-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
*_plValue = (long) *pdData;
dValue = *pdData;
if (dValue != floor(dValue)) {
Scierror(999, _("%s: Incorrect value for input argument #%d: The double value cannot be converted to a 32-bit signed integer.\n"), _fname, _iVar);
return SWIG_ValueError;
}
if ((dValue < LONG_MIN) || (dValue > LONG_MAX)) {
Scierror(999, _("%s: Overflow error for input argument #%d: The double value cannot be converted to a 32-bit signed integer.\n"), _fname, _iVar);
return SWIG_OverflowError;
}
*_plValue = (long) dValue;
}
else {
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
return SWIG_OK;

View file

@ -3,7 +3,7 @@
* Scilab type: double or int16
*/
%fragment(SWIG_AsVal_frag(short), "header", fragment="SWIG_SciDoubleOrInt16_AsShort") {
%fragment(SWIG_AsVal_frag(short), "header", fragment="SWIG_SciDoubleOrInt16_AsShort", fragment="<limits.h>") {
#define SWIG_AsVal_short(scilabValue, valuePointer) SWIG_SciDoubleOrInt16_AsShort(pvApiCtx, scilabValue, valuePointer, SWIG_Scilab_GetFname())
}
%fragment("SWIG_SciDoubleOrInt16_AsShort", "header") {
@ -38,22 +38,22 @@ SWIG_SciDoubleOrInt16_AsShort(void *_pvApiCtx, int _iVar, short *_psValue, char
}
if (iPrec != SCI_INT16) {
Scierror(999, _("%s: Wrong type for input argument #%d: A 16-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
sciErr = getMatrixOfInteger16(_pvApiCtx, piAddrVar, &iRows, &iCols, &psData);
if (sciErr.iErr) {
printError(&sciErr, 0);
return SWIG_ERROR;
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A 16-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
Scierror(999, _("%s: Wrong size for input argument #%d: A 16-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_TypeError;
}
*_psValue = *psData;
}
else if (iType == sci_matrix) {
double *pdData = NULL;
double dValue = 0.0f;
sciErr = getMatrixOfDouble(_pvApiCtx, piAddrVar, &iRows, &iCols, &pdData);
if (sciErr.iErr) {
@ -62,13 +62,22 @@ SWIG_SciDoubleOrInt16_AsShort(void *_pvApiCtx, int _iVar, short *_psValue, char
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A 16-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
*_psValue = (short) *pdData;
dValue = *pdData;
if (dValue != floor(dValue)) {
Scierror(999, _("%s: Incorrect value for input argument #%d: The double value cannot be converted to a 16-bit signed integer.\n"), _fname, _iVar);
return SWIG_ValueError;
}
if ((dValue < SHRT_MIN) || (dValue > SHRT_MAX)) {
Scierror(999, _("%s: Overflow error for input argument #%d: The double value cannot be converted to a 16-bit signed integer.\n"), _fname, _iVar);
return SWIG_OverflowError;
}
*_psValue = (short) dValue;
}
else {
Scierror(999, _("%s: Wrong type for input argument #%d: A 16-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
return SWIG_OK;

View file

@ -1,8 +1,8 @@
/*
* C-type: signed char
* Scilab type: int8 scalar
* Scilab type: int8
*/
%fragment(SWIG_AsVal_frag(signed char), "header", fragment="SWIG_SciDoubleOrInt8_AsShort") {
%fragment(SWIG_AsVal_frag(signed char), "header", fragment="SWIG_SciDoubleOrInt8_AsShort", fragment="<limits.h>") {
#define SWIG_AsVal_signed_SS_char(scilabValue, valuePointer) SWIG_SciDoubleOrInt8_AsShort(pvApiCtx, scilabValue, valuePointer, fname)
}
%fragment("SWIG_SciDoubleOrInt8_AsShort", "header") {
@ -37,9 +37,8 @@ SWIG_SciDoubleOrInt8_AsShort(void *_pvApiCtx, int _iVar, signed char *_pscValue,
}
if (iPrec != SCI_INT8) {
Scierror(999, _("%s: Wrong type for input argument #%d: A 8-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
sciErr = getMatrixOfInteger8(_pvApiCtx, piAddrVar, &iRows, &iCols, &pcData);
if (sciErr.iErr) {
printError(&sciErr, 0);
@ -47,12 +46,13 @@ SWIG_SciDoubleOrInt8_AsShort(void *_pvApiCtx, int _iVar, signed char *_pscValue,
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A 8-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
*_pscValue = *pcData;
}
else if (iType == sci_matrix) {
double *pdData = NULL;
double dValue = 0.0f;
sciErr = getMatrixOfDouble(_pvApiCtx, piAddrVar, &iRows, &iCols, &pdData);
if (sciErr.iErr) {
@ -61,13 +61,22 @@ SWIG_SciDoubleOrInt8_AsShort(void *_pvApiCtx, int _iVar, signed char *_pscValue,
}
if (iRows * iCols != 1) {
Scierror(999, _("%s: Wrong size for input argument #%d: A 8-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
*_pscValue = (short) *pdData;
dValue = *pdData;
if (dValue != floor(dValue)) {
Scierror(999, _("%s: Incorrect value for input argument #%d: The double value cannot be converted to a 8-bit signed integer.\n"), _fname, _iVar);
return SWIG_ValueError;
}
if ((dValue < SCHAR_MIN) || (dValue > SCHAR_MAX)) {
Scierror(999, _("%s: Overflow error for input argument #%d: The double value cannot be converted to a 8-bit signed integer.\n"), _fname, _iVar);
return SWIG_OverflowError;
}
*_pscValue = (signed char) dValue;
}
else {
Scierror(999, _("%s: Wrong type for input argument #%d: A 8-bit signed integer or a double expected.\n"), _fname, _iVar);
return SWIG_ERROR;
return SWIG_TypeError;
}
return SWIG_OK;