1 package jalview.ext.android;
3 import static org.testng.Assert.assertEquals;
5 import org.testng.annotations.Test;
7 public class SparseDoubleArrayTest
11 public void testConstructor()
13 double[] d = new double[] { 0d, 0d, 1.2d, 0d, 0d, 3.4d };
14 SparseDoubleArray s = new SparseDoubleArray(d);
15 for (int i = 0; i < d.length; i++)
17 assertEquals(s.get(i), d[i], "At [" + i + "]");
24 double[] d = new double[] { 0d, 0d, 1.2d, 0d, 0d, 3.4d };
25 SparseDoubleArray s = new SparseDoubleArray(d);
26 // add to zero (absent)
28 assertEquals(s.get(0), 3.2d);
31 assertEquals(s.get(0), 5.7d);
34 assertEquals(s.get(2), -4.1d);
37 assertEquals(s.get(12), 9.8d);
41 public void testDivide()
43 double delta = 1.0e-10;
44 double[] d = new double[] { 0d, 2.4d, 1.2d, 0d, -4.8d, -3.6d };
45 SparseDoubleArray s = new SparseDoubleArray(d);
46 assertEquals(s.divide(0, 1d), 0d); // no such entry
47 assertEquals(s.divide(2, 0d), 0d); // zero divisor
48 assertEquals(s.divide(1, 2d), 1.2d, delta); // + / +
49 assertEquals(s.divide(2, -2d), -0.6d, delta); // + / -
50 assertEquals(s.divide(4, 3d), -1.6d, delta); // - / +
51 assertEquals(s.divide(5, -3d), 1.2d, delta); // - / -