Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scilab: new test scilab_li_matrix for matrix.i library
- Loading branch information
Simon Marchetto
committed
Feb 20, 2014
1 parent
687163b
commit fc99562
Showing
4 changed files
with
168 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters