@Test
public void testSlice() throws Exception {

    int n = 8;
    int[] indices = new int[]{1, 3, 5, 7};
    double[] values = new double[]{2.0, 3.0, 4.0, 5.0};
    SparseVector v = new SparseVector(n, indices, values);
    int[] indices1 = new int[]{5, 4, 3};
    SparseVector vec1 = v.slice(indices1);
    Assert.assertEquals(vec1.size(), 3);
    Assert.assertArrayEquals(vec1.getIndices(), new int[]{0, 2});
    Assert.assertArrayEquals(vec1.getValues(), new double[]{4.0, 3.0}, 0.0);
    int[] indices2 = new int[]{3, 5};
    SparseVector vec2 = v.slice(indices2);
    Assert.assertArrayEquals(vec2.getIndices(), new int[]{0, 1});
    Assert.assertArrayEquals(vec2.getValues(), new double[]{3.0, 4.0}, 0.0);
    int[] indices3 = new int[]{2, 4};
    SparseVector vec3 = v.slice(indices3);
    Assert.assertEquals(vec3.size(), 2);
    Assert.assertArrayEquals(vec3.getIndices(), new int[]{});
    Assert.assertArrayEquals(vec3.getValues(), new double[]{}, 0.0);
    int[] indices4 = new int[]{2, 2, 4, 4};
    SparseVector vec4 = v.slice(indices4);
    Assert.assertEquals(vec4.size(), 4);
    Assert.assertArrayEquals(vec4.getIndices(), new int[]{});
    Assert.assertArrayEquals(vec4.getValues(), new double[]{}, 0.0);
}