Skip to content

Commit

Permalink
scilab: new test scilab_li_matrix for matrix.i library
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Marchetto committed Feb 20, 2014
1 parent 687163b commit fc99562
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Examples/test-suite/scilab/Makefile.in
Expand Up @@ -14,6 +14,9 @@ C_TEST_CASES += \
scilab_enums \
scilab_consts \

CPP_TEST_CASES += \
scilab_li_matrix \

CPP_STD_TEST_CASES += \
primitive_types \
inout \
Expand Down
47 changes: 47 additions & 0 deletions Examples/test-suite/scilab/scilab_li_matrix_runme.sci
@@ -0,0 +1,47 @@
// test matrix.i library

exec("swigtest.start", -1);

// test matrix passed as output argument from fonction
function test_out_matrix(value_type, expected_out_matrix)
cmd = msprintf("out_matrix = out_%s_matrix_func();", value_type);
ierr = execstr(cmd, "errcatch");
if ierr <> 0 then swigtesterror(); end
disp(out_matrix);
if ~isdef('expected_out_matrix') | out_matrix <> expected_out_matrix then swigtesterror(); end
endfunction

// test matrix passed as input argument of fonction
function test_in_matrix(value_type, in_matrix, expected_ret_value)
cmd = msprintf("ret_value = in_%s_matrix_func(in_matrix);", value_type);
ierr = execstr(cmd, "errcatch");
if ierr <> 0 then swigtesterror(); end
if ~isdef('ret_value') | ret_value <> expected_ret_value then swigtesterror(); end
endfunction

// test matrixes passed as input and output arguments of fonction
function test_inout_matrix(value_type, in_matrix, expected_out_matrix)
cmd = msprintf("out_matrix = inout_%s_matrix_func(in_matrix);", value_type);
ierr = execstr(cmd, "errcatch");
if ierr <> 0 then swigtesterror(); end
if ~isdef('out_matrix') | out_matrix <> expected_out_matrix then swigtesterror(); end
endfunction

function test_matrix_typemaps(value_type, expected_out_matrix, expected_ret_value, expected_out_matrix2)
test_out_matrix(value_type, expected_out_matrix);
test_in_matrix(value_type, expected_out_matrix, expected_ret_value);
test_inout_matrix(value_type, expected_out_matrix, expected_out_matrix2);
endfunction


m = [0 3; 1 4; 2 5];
test_matrix_typemaps("int", m, sum(m), m .* m);
test_matrix_typemaps("int", int32(m), sum(m), m .* m);

test_matrix_typemaps("double", m, sum(m), m .* m);

//m = ["0" "3"; "1" "4"; "2" "5"]
//test_matrix_typemaps("charptr", m, strcat(m), m + m);


exec("swigtest.quit", -1);
106 changes: 106 additions & 0 deletions Examples/test-suite/scilab_li_matrix.i
@@ -0,0 +1,106 @@
%module scilab_li_matrix

%include matrix.i

%define %use_matrix_apply(TYPE...)
%apply (TYPE *matrixIn, int matrixInRowCount, int matrixInColCount) { (TYPE *inputMatrix, int nbRow, int nbCol) }
%apply (TYPE **matrixOut, int *matrixOutRowCount, int *matrixOutColCount) { (TYPE **resultMatrix, int *nbRowRes, int *nbColRes) }
%enddef

%use_matrix_apply(int);
%use_matrix_apply(double);
%use_matrix_apply(char *);

%{
#include <stdlib.h>
#include <stdio.h>
%}

%inline %{

// int and double matrix functions
template<typename T> T in_matrix_func(T *inputMatrix, int nbRow, int nbCol) {
T sum = 0;
int i;
for (i=0; i<nbRow*nbCol; i++)
sum += inputMatrix[i];
return sum;
}

template<typename T> void out_matrix_func(T **resultMatrix, int *nbRowRes, int *nbColRes) {
int size;
int i;
*nbRowRes = 3;
*nbColRes = 2;
size = (*nbRowRes) * (*nbColRes);
*resultMatrix = (T*) malloc(size * sizeof(T));
for (i=0; i<size; i++)
(*resultMatrix)[i] = i;
}

template<typename T> void inout_matrix_func(T *inputMatrix, int nbRow, int nbCol, T **resultMatrix, int *nbRowRes, int *nbColRes) {
int i;
int size = nbRow * nbCol;
*nbRowRes = nbRow;
*nbColRes = nbCol;
*resultMatrix = (T*) malloc(size * sizeof(T));
for (i=0; i<size; i++) {
(*resultMatrix)[i] = inputMatrix[i] * inputMatrix[i];
}
}

// char* matrix functions
template<> char* in_matrix_func(char **inputMatrix, int nbRow, int nbCol) {
char *s = (char *) malloc(nbRow * nbCol * sizeof(char) + 1);
int i;
for (i=0; i<nbRow*nbCol; i++)
strcat(s, inputMatrix[i]);
return s;
}

template<> void out_matrix_func(char ***resultMatrix, int *nbRowRes, int *nbColRes) {
int size;
char *s;
int i;
*nbRowRes = 3;
*nbColRes = 2;
size = (*nbRowRes) * (*nbColRes);
*resultMatrix = (char **) malloc(size * sizeof(char *));
for (i=0; i<size; i++) {
s = (char *) malloc(sizeof(char)+1);
sprintf(s, "%d", i);
(*resultMatrix)[i] = s;
}
}

template<> void inout_matrix_func(char **inputMatrix, int nbRow, int nbCol, char ***resultMatrix, int *nbRowRes, int *nbColRes) {
int i;
char *s;
int size = nbRow * nbCol;
*nbRowRes = nbRow;
*nbColRes = nbCol;
*resultMatrix = (char **) malloc(size * sizeof(char* ));
for (i=0; i<size; i++) {
s = (char *) malloc((2 * strlen(inputMatrix[i]) + 1) * sizeof(char));
sprintf(s, "%s%s", inputMatrix[i], inputMatrix[i]);
(*resultMatrix)[i] = s;
}
}
%}

%define %instantiate_matrix_template_functions(NAME, TYPE...)
%template(in_ ## NAME ## _matrix_func) in_matrix_func<TYPE>;
%template(out_ ## NAME ## _matrix_func) out_matrix_func<TYPE>;
%template(inout_ ## NAME ## _matrix_func) inout_matrix_func<TYPE>;
%enddef

%instantiate_matrix_template_functions(int, int);
%instantiate_matrix_template_functions(double, double);
%instantiate_matrix_template_functions(charptr, char *);
//%instantiate_matrix_template_functions(bool);






23 changes: 12 additions & 11 deletions Lib/scilab/sciint.swg
Expand Up @@ -128,7 +128,7 @@ SWIG_SciInt32_AsIntArrayAndSize(void *_pvApiCtx, int _iVar, int *_iRows, int *_i
}
if (iPrec != SCI_INT32)
{
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer matrix expected.\n"), _fname, _iVar);
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer or a double matrix expected.\n"), _fname, _iVar);
return SWIG_ERROR;
}
sciErr = getMatrixOfInteger32(_pvApiCtx, piAddrVar, _iRows, _iCols, _piValue);
Expand All @@ -141,26 +141,27 @@ SWIG_SciInt32_AsIntArrayAndSize(void *_pvApiCtx, int _iVar, int *_iRows, int *_i
}
else if (isDoubleType(_pvApiCtx, piAddrVar))
{
double **dblValue;
double *pdData = NULL;
int size = 0;
int i;

// Check if input matrix is not empty (empty matrix type is double)
sciErr = getMatrixOfDouble(_pvApiCtx, piAddrVar, _iRows, _iCols, dblValue);
sciErr = getMatrixOfDouble(_pvApiCtx, piAddrVar, _iRows, _iCols, &pdData);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return SWIG_ERROR;
}
if ((*_iRows > 0) || (*_iCols > 0))
{
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer matrix expected.\n"), _fname, _iVar);
return SWIG_ERROR;
}
*_piValue = (int*) malloc(sizeof(int*));

size = (*_iRows) * (*_iCols);
*_piValue = (int*) malloc(size * sizeof(int*));
for (i = 0; i < size; i++)
(*_piValue)[i] = (int) pdData[i];

return SWIG_OK;
}
else
{
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer matrix expected.\n"), _fname, _iVar);
Scierror(999, _("%s: Wrong type for input argument #%d: A 32-bit signed integer or a double matrix expected.\n"), _fname, _iVar);
return SWIG_ERROR;
}
return SWIG_OK;
Expand Down

0 comments on commit fc99562

Please sign in to comment.