summaryrefslogtreecommitdiff
path: root/Examples
diff options
context:
space:
mode:
Diffstat (limited to 'Examples')
-rw-r--r--Examples/scilab/matrix2/example.c30
-rw-r--r--Examples/scilab/matrix2/example.i4
-rw-r--r--Examples/scilab/matrix2/runme.sci8
-rw-r--r--Examples/test-suite/preproc_defined.i28
-rw-r--r--Examples/test-suite/python/preproc_defined_runme.py9
5 files changed, 74 insertions, 5 deletions
diff --git a/Examples/scilab/matrix2/example.c b/Examples/scilab/matrix2/example.c
index 476067df9..232c88824 100644
--- a/Examples/scilab/matrix2/example.c
+++ b/Examples/scilab/matrix2/example.c
@@ -29,12 +29,12 @@ void squareDoubleMatrix(double *inputMatrix, int nbRow, int nbCol, double** resu
}
}
-void getDoubleMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
+void getDoubleMatrix(int nbRow, int nbCol, double **resultMatrix, int *nbRowRes, int *nbColRes)
{
int i;
int size;
- *nbRowRes = 5;
- *nbColRes = 3;
+ *nbRowRes = nbRow;
+ *nbColRes = nbCol;
size = (*nbRowRes) * (*nbColRes);
*resultMatrix = (double*) malloc(size * sizeof(double));
for (i=0; i<size; i++)
@@ -43,6 +43,30 @@ void getDoubleMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
}
}
+void extractDoubleMatrix(double *inputMatrix, int nbRow, int nbCol, int* indexes, int nbIndexes, double **resultMatrix, int *nbRowRes, int *nbColRes)
+{
+ if (nbIndexes < 0)
+ {
+ int sz = nbRow * nbCol;
+ *nbRowRes = sz;
+ *nbColRes = 1;
+
+ *resultMatrix = malloc(sz * sizeof(double));
+ memcpy(*resultMatrix, inputMatrix, sz * sizeof(double));
+ } else {
+ int i;
+
+ *nbRowRes = nbIndexes;
+ *nbColRes = 1;
+
+ *resultMatrix = malloc(nbIndexes * sizeof(double));
+ for (i = 0; i < nbIndexes; i++)
+ {
+ (*resultMatrix)[i] = inputMatrix[indexes[i]];
+ }
+ }
+}
+
// Integer matrix functions
int sumIntegerMatrix(int *inputMatrix, int nbRow, int nbCol)
diff --git a/Examples/scilab/matrix2/example.i b/Examples/scilab/matrix2/example.i
index e37cd116c..1584691cc 100644
--- a/Examples/scilab/matrix2/example.i
+++ b/Examples/scilab/matrix2/example.i
@@ -4,6 +4,7 @@
%apply (double *IN, int IN_ROWCOUNT, int IN_COLCOUNT) { (double *inputMatrix, int nbRow, int nbCol) }
%apply (double **OUT, int *OUT_ROWCOUNT, int *OUT_COLCOUNT) { (double **resultMatrix, int *nbRowRes, int *nbColRes) }
+%apply (int *IN, int IN_SIZE) { (int* indexes, int nbIndexes) }
%apply (int *IN, int IN_ROWCOUNT, int IN_COLCOUNT) { (int *inputMatrix, int nbRow, int nbCol) }
%apply (int **OUT, int *OUT_ROWCOUNT, int *OUT_COLCOUNT) { (int **resultMatrix, int *nbRowRes, int *nbColRes) }
@@ -14,7 +15,8 @@
%inline %{
extern double sumDoubleMatrix(double *inputMatrix, int nbRow, int nbCol);
extern void squareDoubleMatrix(double *inputMatrix, int nbRow, int nbCol, double **resultMatrix, int *nbRowRes, int *nbColRes);
- extern void getDoubleMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes);
+ extern void getDoubleMatrix(int nbRow, int nbCol,double **resultMatrix, int *nbRowRes, int *nbColRes);
+ extern void extractDoubleMatrix(double *inputMatrix, int nbRow, int nbCol, int* indexes, int nbIndexes, double **resultMatrix, int *nbRowRes, int *nbColRes);
extern int sumIntegerMatrix(int *inputMatrix, int nbRow, int nbCol);
extern void squareIntegerMatrix(int *inputMatrix, int nbRow, int nbCol, int **resultMatrix, int *nbRowRes, int *nbColRes);
diff --git a/Examples/scilab/matrix2/runme.sci b/Examples/scilab/matrix2/runme.sci
index 0af7df4e7..4264991e8 100644
--- a/Examples/scilab/matrix2/runme.sci
+++ b/Examples/scilab/matrix2/runme.sci
@@ -8,7 +8,7 @@ end
// Test lib double matrix functions
disp("Call lib function getDoubleMatrix()");
-doubleMatrix = getDoubleMatrix();
+doubleMatrix = getDoubleMatrix(5, 3);
disp(doubleMatrix);
disp("Call lib function sumDoubleMatrix()");
@@ -19,6 +19,12 @@ disp("Call lib function squareDoubleMatrix()");
sqrd = squareDoubleMatrix(doubleMatrix);
disp(sqrd);
+disp("Extract various indexes");
+disp(extractDoubleMatrix(doubleMatrix, 2));
+disp(extractDoubleMatrix(doubleMatrix, 2:5));
+disp(extractDoubleMatrix(doubleMatrix, :));
+
+
// Test lib integer matrix functions
diff --git a/Examples/test-suite/preproc_defined.i b/Examples/test-suite/preproc_defined.i
index 5ebf0a099..9ae884691 100644
--- a/Examples/test-suite/preproc_defined.i
+++ b/Examples/test-suite/preproc_defined.i
@@ -123,3 +123,31 @@ void another_macro_checking(void) {
#if 0
# wobble wobble
#endif
+
+/* Regression test for https://sourceforge.net/p/swig/bugs/1163/
+ * ONE(1)(2) should expand to `2` but SWIG was expanding it to `TWO(2)`
+ * which results in the generated C wrapper failing to compile.
+ */
+#define ONE(X) TWO
+#define TWO(X) X
+%constant int a = ONE(1)(2);
+#define XXX TWO
+%constant int b = XXX(42);
+#undef ONE
+#undef TWO
+
+/*
+ * The behaviour of Self-Referential Macros is defined
+ * https://gcc.gnu.org/onlinedocs/gcc-4.8.5/cpp/Self-Referential-Macros.html
+ */
+%inline %{
+const int y = 0;
+%}
+
+#define x (4 + y)
+#define y (2 * x)
+
+%constant int z = y;
+
+#undef y
+#undef x
diff --git a/Examples/test-suite/python/preproc_defined_runme.py b/Examples/test-suite/python/preproc_defined_runme.py
index af46816be..37441db52 100644
--- a/Examples/test-suite/python/preproc_defined_runme.py
+++ b/Examples/test-suite/python/preproc_defined_runme.py
@@ -9,3 +9,12 @@ d.defined = 10
preproc_defined.thing(10)
preproc_defined.stuff(10)
preproc_defined.bumpf(10)
+
+if preproc_defined.a != 2:
+ raise RuntimeError
+
+if preproc_defined.b != 42:
+ raise RuntimeError
+
+if preproc_defined.z != 8:
+ raise RuntimeError