summaryrefslogtreecommitdiff
path: root/graphics/asymptote/tests/array
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /graphics/asymptote/tests/array
Initial commit
Diffstat (limited to 'graphics/asymptote/tests/array')
-rw-r--r--graphics/asymptote/tests/array/array.asy49
-rw-r--r--graphics/asymptote/tests/array/delete.asy41
-rw-r--r--graphics/asymptote/tests/array/determinant.asy18
-rw-r--r--graphics/asymptote/tests/array/fields.asy103
-rw-r--r--graphics/asymptote/tests/array/slice.asy232
-rw-r--r--graphics/asymptote/tests/array/solve.asy35
-rw-r--r--graphics/asymptote/tests/array/sort.asy39
-rw-r--r--graphics/asymptote/tests/array/transpose.asy55
8 files changed, 572 insertions, 0 deletions
diff --git a/graphics/asymptote/tests/array/array.asy b/graphics/asymptote/tests/array/array.asy
new file mode 100644
index 0000000000..106856fd80
--- /dev/null
+++ b/graphics/asymptote/tests/array/array.asy
@@ -0,0 +1,49 @@
+import TestLib;
+
+StartTest("array");
+
+{
+ int[] x=array(10, 7);
+ assert(x.length == 10);
+ for (int i=0; i<x.length; ++i)
+ assert(x[i] == 7);
+}
+{
+ int[][] y=array(10, array(10, 7));
+ assert(y.length == 10);
+ for (int i=0; i<y.length; ++i) {
+ assert(y[i].length == 10);
+ for (int j=0; j<y[i].length; ++j)
+ assert(y[i][j] == 7);
+ }
+}
+{
+ int[][] y=array(10, array(10, 7));
+ y[4][5] = 9;
+ assert(y.length == 10);
+ for (int i=0; i<y.length; ++i) {
+ assert(y[i].length == 10);
+ for (int j=0; j<y[i].length; ++j)
+ if (i==4 && j==5)
+ assert(y[i][j] == 9);
+ else
+ assert(y[i][j] == 7);
+ }
+}
+{
+ int[][] y=array(10, array(10, 7), depth=0);
+ y[4][5] = 9;
+ assert(y.length == 10);
+ for (int i=0; i<y.length; ++i) {
+ assert(y[i].length == 10);
+ for (int j=0; j<y[i].length; ++j)
+ if (j==5)
+ assert(y[i][j] == 9);
+ else
+ assert(y[i][j] == 7);
+ }
+}
+
+
+
+EndTest();
diff --git a/graphics/asymptote/tests/array/delete.asy b/graphics/asymptote/tests/array/delete.asy
new file mode 100644
index 0000000000..d92a3c082c
--- /dev/null
+++ b/graphics/asymptote/tests/array/delete.asy
@@ -0,0 +1,41 @@
+import TestLib;
+import math;
+
+StartTest("delete");
+
+int[] a=sequence(4);
+a.delete(2);
+assert(all(a == new int[] {0,1,3}));
+
+int[] a=sequence(4);
+a.delete(0,2);
+assert(all(a == new int[] {3}));
+
+int[] a=sequence(4);
+a.delete(1,2);
+assert(all(a == new int[] {0,3}));
+
+int[] a=sequence(4);
+a.delete(2,2);
+assert(all(a == new int[] {0,1,3}));
+
+int[] a=sequence(4);
+a.delete(2,3);
+assert(all(a == new int[] {0,1}));
+
+int[] a=sequence(4);
+a.cyclic=true;
+a.delete(2,3);
+assert(all(a == new int[] {0,1}));
+
+int[] a=sequence(4);
+a.cyclic=true;
+a.delete(2,4);
+assert(all(a == new int[] {1}));
+
+int[] a=sequence(4);
+a.cyclic=true;
+a.delete(3,1);
+assert(all(a == new int[] {2}));
+
+EndTest();
diff --git a/graphics/asymptote/tests/array/determinant.asy b/graphics/asymptote/tests/array/determinant.asy
new file mode 100644
index 0000000000..e6086d0b04
--- /dev/null
+++ b/graphics/asymptote/tests/array/determinant.asy
@@ -0,0 +1,18 @@
+import TestLib;
+import math;
+
+StartTest("determinant");
+assert(determinant(new real[][] {{0}}) == 0);
+assert(determinant(new real[][] {{1}}) == 1);
+assert(determinant(new real[][] {{1,2},{3,4}}) == -2);
+real e=1e-20;
+assert(close(determinant(new real[][] {{1e,2e},{3e,4e}}),-2e-40));
+assert(close(determinant(new real[][] {{1,2,3},{4,5,6},{7,8,9}}),0));
+assert(close(determinant(new real[][] {{1,2},{1,2}}),0));
+assert(close(determinant(new real[][] {{1,2,3,4},
+ {5,6,7,8},{9,10,11,12},{13,14,15,16}}),0));
+assert(close(determinant(new real[][] {{1,2,3,4},
+ {5,0,7,8},{9,10,0,12},{13,14,15,16}}),-2376));
+assert(close(determinant(new real[][]{{1,-2,3,0},{4,-5,6,2},{-7,-8,10,5},
+ {1,50,1,-2}}),-4588));
+EndTest();
diff --git a/graphics/asymptote/tests/array/fields.asy b/graphics/asymptote/tests/array/fields.asy
new file mode 100644
index 0000000000..9b5926d374
--- /dev/null
+++ b/graphics/asymptote/tests/array/fields.asy
@@ -0,0 +1,103 @@
+import TestLib;
+
+StartTest("fields");
+
+{
+ int[] z = {1, 2, 3};
+ assert(z.length == 3);
+ int[] keys = z.keys;
+ assert(keys.length == 3);
+ for (int i; i<3; ++i)
+ assert(keys[i] == i);
+ for (int j = 0; j < 10; ++j) {
+ assert(z.cyclic == false);
+ z.cyclic=true;
+ assert(z.cyclic == true);
+ z.cyclic=false;
+ }
+}
+
+{
+ int[] z = {2, 3, 5};
+ for (int k = -100; k <= 100; ++k)
+ assert(z.initialized(k) == (k >= 0 && k < 3));
+}
+
+{
+ int[] z;
+ for (int i=0; i<10; ++i) {
+ for (int k = 0; k <= 100; ++k) {
+ assert(z.length == k);
+ z.push(k*k+3k+1);
+ assert(z.length == k+1);
+ }
+ for (int k = 100; k >= 0; --k) {
+ assert(z.length == k+1);
+ assert(z.pop() == k*k+3k+1);
+ assert(z.length == k);
+ }
+ }
+ z.cyclic=true;
+ for (int i=0; i<10; ++i) {
+ for (int k = 0; k <= 100; ++k) {
+ assert(z.length == k);
+ z.push(k*k+3k+1);
+ assert(z.length == k+1);
+ }
+ for (int k = 100; k >= 0; --k) {
+ assert(z.length == k+1);
+ z.delete(quotient(k,2));
+ assert(z.length == k);
+ }
+ }
+}
+
+{
+ int[] base={4,5,9,5,0,2,3};
+ int[] z;
+ for (int i=0; i<9; ++i) {
+ assert(z.length == i*base.length);
+ for (int j : z.keys)
+ assert(z[j] == base[j%base.length]);
+ z.append(base);
+ }
+}
+
+{
+ int[] z = {1,2,3,4,6,7,8,9};
+ assert(z.length == 8);
+ z.insert(4, 5);
+ assert(z.length == 9);
+ z.insert(0, 0);
+ assert(z.length == 10);
+ for (int i=0; i<10; ++i)
+ assert(z[i] == i);
+ z.insert(7, 100, 101, 102, 103);
+ assert(z.length == 14);
+
+ // TODO: Test inserting/deleting lengths more seriously.
+}
+
+{
+ // Test extended for.
+ int[] a = {1,4,6,2,7,4,8,9,1,3,-1};
+ int i = 0;
+ for (int x : a) {
+ assert(x == a[i]);
+ ++i;
+ }
+ assert(i == a.length);
+}
+
+{
+ // Test extended for.
+ int[] a = {1,4,6,2,7,4,8,9,1,3,-1};
+ int i = 0;
+ for (var x : a) {
+ assert(x == a[i]);
+ ++i;
+ }
+ assert(i == a.length);
+}
+
+EndTest();
diff --git a/graphics/asymptote/tests/array/slice.asy b/graphics/asymptote/tests/array/slice.asy
new file mode 100644
index 0000000000..9010f0b5d8
--- /dev/null
+++ b/graphics/asymptote/tests/array/slice.asy
@@ -0,0 +1,232 @@
+import TestLib;
+
+StartTest("slice");
+
+int[] x={0,1,2,3,4,5,6,7,8,9};
+
+// Non-cyclic cases.
+assert(all(x[:] == x));
+assert(!alias(x[:],x));
+
+assert(all(x[0:4] == new int[] {0,1,2,3} ));
+assert(all(x[2:4] == new int[] {2,3} ));
+
+assert(all(x[5:] == new int[] {5,6,7,8,9} ));
+assert(all(x[:5] == new int[] {0,1,2,3,4} ));
+
+assert(all(x[3:3] == new int[] {} ));
+assert(all(x[3:4] == new int[] {3} ));
+assert(all(x[98:99] == new int[] {} ));
+
+assert(x[:].cyclic == false);
+assert(x[2:].cyclic == false);
+assert(x[:7].cyclic == false);
+assert(x[3:3].cyclic == false);
+assert(x[2:9].cyclic == false);
+
+// Cyclic cases
+x.cyclic=true;
+
+assert(all(x[:] == new int[] {0,1,2,3,4,5,6,7,8,9} ));
+assert(all(x[0:4] == new int[] {0,1,2,3} ));
+assert(all(x[2:4] == new int[] {2,3} ));
+
+assert(all(x[5:] == new int[] {5,6,7,8,9} ));
+assert(all(x[-5:] == new int[] {5,6,7,8,9,0,1,2,3,4,5,6,7,8,9} ));
+assert(all(x[:5] == new int[] {0,1,2,3,4} ));
+
+assert(all(x[3:3] == new int[] {} ));
+assert(all(x[3:4] == new int[] {3} ));
+
+assert(all(x[-1:1] == new int[] {9,0} ));
+assert(all(x[9:11] == new int[] {9,0} ));
+assert(all(x[9:21] == new int[] {9,0,1,2,3,4,5,6,7,8,9,0} ));
+assert(all(x[-15:15] == new int[] {5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,
+ 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4}));
+assert(all(x[6728:6729] == new int[] {8} ));
+assert(all(x[-6729:-6728] == new int[] {1} ));
+
+assert(x[:].cyclic == false);
+assert(x[2:].cyclic == false);
+assert(x[:7].cyclic == false);
+assert(x[3:3].cyclic == false);
+assert(x[2:9].cyclic == false);
+assert(x[5:100].cyclic == false);
+
+pair[] z={(1,2), (3,4), (5,6)};
+assert(all(z[1:1] == new pair[] {}));
+assert(all(z[:1] == new pair[] {(1,2)}));
+assert(all(z[1:] == new pair[] {(3,4), (5,6)}));
+assert(all(z[:] == z));
+assert(all(z[1:2] == new pair[] {(3,4)}));
+
+// Writing tests.
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ int[] z={56,67,78};
+
+ y[:] = z;
+ assert(all(y == z));
+ assert(!alias(y,z));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ int[] z={56,67,78};
+ z.cyclic=true;
+
+ y[:] = z;
+ assert(all(y == z));
+ assert(!alias(y,z));
+ assert(y.cyclic == false);
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+
+ y[2:3] = y[5:6] = new int[] {77};
+ assert(all(y == new int[] {0,1,77,3,4,77,6,7,8,9}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+
+ y[:3] = y[7:] = new int[] {};
+ assert(all(y == new int[] {3,4,5,6}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+
+ y[3:5] = new int[] {13,14,15,16,17};
+ assert(all(y == new int[] {0,1,2,13,14,15,16,17,5,6,7,8,9}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+ int[] z={56,67,78};
+
+ y[:] = z;
+ assert(all(y == z));
+ assert(!alias(y,z));
+ assert(y.cyclic == true);
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+ int[] z={56,67,78};
+ z.cyclic=true;
+
+ y[:] = z;
+ assert(all(y == z));
+ assert(!alias(y,z));
+ assert(y.cyclic == true);
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[2:3] = y[5:6] = new int[] {77};
+ assert(all(y == new int[] {0,1,77,3,4,77,6,7,8,9}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[:3] = y[7:] = new int[] {};
+ assert(all(y == new int[] {3,4,5,6}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[8:] = new int[] {18,19,20,21,22};
+ assert(all(y == new int[] {0,1,2,3,4,5,6,7,18,19,20,21,22}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[-2:0] = new int[] {18,19,20,21,22};
+ assert(all(y == new int[] {0,1,2,3,4,5,6,7,18,19,20,21,22}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[18:20] = new int[] {18,19,20,21,22};
+ assert(all(y == new int[] {0,1,2,3,4,5,6,7,18,19,20,21,22}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[3:5] = new int[] {13,14,15,16,17};
+ assert(all(y == new int[] {0,1,2,13,14,15,16,17,5,6,7,8,9}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[13:15] = new int[] {13,14,15,16,17};
+ assert(all(y == new int[] {0,1,2,13,14,15,16,17,5,6,7,8,9}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[3-10:5-10] = new int[] {13,14,15,16,17};
+ assert(all(y == new int[] {0,1,2,13,14,15,16,17,5,6,7,8,9}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[8:12] = new int[] {18,19,20,21};
+ assert(all(y == new int[] {20,21,2,3,4,5,6,7,18,19}));
+}
+{
+ int[] y={0,1,2,3,4,5,6,7,8,9};
+ y.cyclic=true;
+
+ y[-2:2] = new int[] {18,19,20,21};
+ assert(all(y == new int[] {20,21,2,3,4,5,6,7,18,19}));
+}
+
+// Side Effect Test
+{
+ int state=0;
+ int[] x={0,1,2,3,4,5,6,7,8,9};
+
+ int[] a() {
+ assert(state==0);
+ ++state;
+
+ return x;
+ }
+
+ int l() {
+ assert(state==1);
+ ++state;
+
+ return 2;
+ }
+
+ int r() {
+ assert(state==2);
+ ++state;
+
+ return 6;
+ }
+
+ int[] b() {
+ assert(state==3);
+ ++state;
+
+ return new int[] {77,77};
+ }
+
+ assert(state==0);
+ a()[l():r()]=b();
+ assert(state==4);
+ assert(all(x == new int[] {0,1,77,77,6,7,8,9}));
+}
+
+
+EndTest();
diff --git a/graphics/asymptote/tests/array/solve.asy b/graphics/asymptote/tests/array/solve.asy
new file mode 100644
index 0000000000..ec1c4416ba
--- /dev/null
+++ b/graphics/asymptote/tests/array/solve.asy
@@ -0,0 +1,35 @@
+import TestLib;
+
+StartTest("solve");
+real[][] a=new real[][] {{1,1,1},{1,2,2},{0,0,1}};
+real[][] b=new real[][] {{3,9},{5,11},{1,3}};
+real[][] x=new real[][] {{1,7},{1,-1,},{1,3}};
+real[][] c=solve(a,b);
+for(int i=0; i < c.length; ++i)
+ for(int j=0; j < c[i].length; ++j)
+ assert(close(c[i][j],x[i][j]));
+
+real[][] a={{1,-2,3,0},{4,-5,6,2},{-7,-8,10,5},{1,50,1,-2}};
+real[] b={7,19,33,3};
+real[] x=solve(a,b);
+real[] c=a*x;
+for(int i=0; i < c.length; ++i)
+ assert(close(c[i],b[i]));
+EndTest();
+
+StartTest("inverse");
+real[][] a=new real[][] {{1,1,1},{1,2,2},{0,0,1}};
+real[][] ainverse=new real[][] {{2,-1,0},{-1,1,-1},{0,0,1}};
+real[][] d=inverse(a);
+real[][] l=d*a;
+real[][] r=a*d;
+real[][] I=identity(a.length);
+for(int i=0; i < d.length; ++i) {
+ for(int j=0; j < d[i].length; ++j) {
+ assert(close(d[i][j],ainverse[i][j]));
+ assert(I[i][j] == (i == j ? 1 : 0));
+ assert(close(l[i][j],I[i][j]));
+ assert(close(r[i][j],I[i][j]));
+ }
+}
+EndTest();
diff --git a/graphics/asymptote/tests/array/sort.asy b/graphics/asymptote/tests/array/sort.asy
new file mode 100644
index 0000000000..03c585b717
--- /dev/null
+++ b/graphics/asymptote/tests/array/sort.asy
@@ -0,0 +1,39 @@
+import TestLib;
+import math;
+
+string[] a={"bob","alice","pete","alice"};
+string[] b={"alice","alice","bob","pete"};
+
+StartTest("sort");
+
+assert(all(sort(a) == b));
+
+EndTest();
+
+StartTest("search");
+
+assert(search(b,"a") == -1);
+assert(search(b,"bob") == 2);
+assert(search(b,"z") == b.length-1);
+
+EndTest();
+
+StartTest("sort2");
+
+string[][] a={{"bob","9"},{"alice","5"},{"pete","7"},{"alice","4"}};
+string[][] b={{"alice","4"},{"alice","5"},{"bob","9"},{"pete","7"}};
+
+assert(sort(a) == b);
+
+EndTest();
+
+pair[] a={(2,1),(0,0),(1,1),(1,0)};
+pair[] b={(0,0),(1,0),(1,1),(2,1)};
+
+StartTest("lexicographical sort");
+assert(all(sort(a,lexorder) == b));
+EndTest();
+
+StartTest("lexicographical search");
+assert(search(b,(1,0),lexorder) == 1);
+EndTest();
diff --git a/graphics/asymptote/tests/array/transpose.asy b/graphics/asymptote/tests/array/transpose.asy
new file mode 100644
index 0000000000..f0366ba8fd
--- /dev/null
+++ b/graphics/asymptote/tests/array/transpose.asy
@@ -0,0 +1,55 @@
+import TestLib;
+import math;
+
+StartTest("transpose");
+int n=3;
+real[][] a=new real[n][n];
+real[][] b=new real[n][n];
+for(int i=0; i < n; ++i) {
+ for(int j=0; j < n; ++j) {
+ a[i][j]=b[j][i]=rand();
+ }
+}
+
+bool operator == (real[][] a, real[][] b)
+{
+ int n=a.length;
+ for(int i=0; i < n; ++i)
+ if(!all(a[i] == b[i])) return false;
+ return true;
+}
+
+bool operator == (real[][][] a, real[][][] b)
+{
+ int n=a.length;
+ for(int i=0; i < n; ++i) {
+ real[][] ai=a[i];
+ real[][] bi=b[i];
+ int m=ai.length;
+ for(int j=0; j < m; ++j) {
+ if(!all(ai[j] == bi[j])) return false;
+ }
+ }
+ return true;
+}
+
+assert(a == transpose(b));
+
+int n=3;
+real[][][] a=new real[n][n][n];
+real[][][] b=new real[n][n][n];
+real[][][] c=new real[n][n][n];
+real[][][] d=new real[n][n][n];
+for(int i=0; i < n; ++i) {
+ for(int j=0; j < n; ++j) {
+ for(int k=0; k < n; ++k) {
+ a[i][j][k]=b[j][i][k]=c[i][k][j]=d[k][j][i]=rand();
+ }
+ }
+}
+
+assert(a == transpose(b,new int[] {1,0,2}));
+assert(a == transpose(c,new int[] {0,2,1}));
+assert(a == transpose(d,new int[] {2,1,0}));
+
+EndTest();