http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java new file mode 100644 index 0000000..7c2d415 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.storage.matrix; + +import org.apache.ignite.Ignite; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.ml.math.StorageConstants; +import org.apache.ignite.ml.math.impls.MathTestConstants; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.testframework.junits.common.GridCommonTest; + +/** + * Tests for {@link SparseDistributedMatrixStorage}. + */ +@GridCommonTest(group = "Distributed Models") +public class SparseDistributedMatrixStorageTest extends GridCommonAbstractTest { + /** Number of nodes in grid */ + private static final int NODE_COUNT = 3; + /** Cache name. */ + private static final String CACHE_NAME = "test-cache"; + /** */ + private static final String UNEXPECTED_ATTRIBUTE_VALUE = "Unexpected attribute value."; + /** Grid instance. */ + private Ignite ignite; + + /** + * Default constructor. + */ + public SparseDistributedMatrixStorageTest() { + super(false); + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + for (int i = 1; i <= NODE_COUNT; i++) + startGrid(i); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** + * {@inheritDoc} + */ + @Override protected void beforeTest() throws Exception { + ignite = grid(NODE_COUNT); + + ignite.configuration().setPeerClassLoadingEnabled(true); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + ignite.destroyCache(CACHE_NAME); + } + + /** */ + public void testCacheCreation() throws Exception { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + final int rows = MathTestConstants.STORAGE_SIZE; + final int cols = MathTestConstants.STORAGE_SIZE; + + SparseDistributedMatrixStorage storage = new SparseDistributedMatrixStorage(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); + + assertNotNull("SparseDistributedMatrixStorage cache is null.", storage.cache()); + } + + /** */ + public void testSetGet() throws Exception { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + final int rows = MathTestConstants.STORAGE_SIZE; + final int cols = MathTestConstants.STORAGE_SIZE; + + SparseDistributedMatrixStorage storage = new SparseDistributedMatrixStorage(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + double v = Math.random(); + storage.set(i, j, v); + + assert Double.compare(v, storage.get(i, j)) == 0; + assert Double.compare(v, storage.get(i, j)) == 0; + } + } + } + + /** */ + public void testAttributes() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + final int rows = MathTestConstants.STORAGE_SIZE; + final int cols = MathTestConstants.STORAGE_SIZE; + + SparseDistributedMatrixStorage storage = new SparseDistributedMatrixStorage(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); + + assertEquals(UNEXPECTED_ATTRIBUTE_VALUE, storage.rowSize(), rows); + assertEquals(UNEXPECTED_ATTRIBUTE_VALUE, storage.columnSize(), cols); + + assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, storage.isArrayBased()); + assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, storage.isDense()); + assertTrue(UNEXPECTED_ATTRIBUTE_VALUE, storage.isDistributed()); + + assertEquals(UNEXPECTED_ATTRIBUTE_VALUE, storage.isRandomAccess(), !storage.isSequentialAccess()); + assertTrue(UNEXPECTED_ATTRIBUTE_VALUE, storage.isRandomAccess()); + + } + +}
http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/RandomAccessSparseVectorStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/RandomAccessSparseVectorStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/RandomAccessSparseVectorStorageTest.java new file mode 100644 index 0000000..6578e14 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/RandomAccessSparseVectorStorageTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.storage.vector; + +import org.apache.ignite.ml.math.StorageConstants; +import org.apache.ignite.ml.math.impls.MathTestConstants; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; + +/** + * Unit tests for {@link SparseLocalOnHeapVectorStorage}. + */ +public class RandomAccessSparseVectorStorageTest extends VectorBaseStorageTest<SparseLocalOnHeapVectorStorage> { + /** */ + @Override public void setUp() { + storage = new SparseLocalOnHeapVectorStorage(MathTestConstants.STORAGE_SIZE, StorageConstants.RANDOM_ACCESS_MODE); + } + + /** */ + @Test + public void data() throws Exception { + assertNull(MathTestConstants.NULL_VAL, storage.data()); + } + + /** */ + @Test + public void isSequentialAccess() throws Exception { + assertFalse(MathTestConstants.UNEXPECTED_VAL, storage.isSequentialAccess()); + } + + /** */ + @Test + public void isDense() throws Exception { + assertFalse(MathTestConstants.UNEXPECTED_VAL, storage.isDense()); + } + + /** */ + @Test + public void isArrayBased() throws Exception { + assertFalse(MathTestConstants.UNEXPECTED_VAL, storage.isArrayBased()); + } + +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/SparseLocalOffHeapVectorStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/SparseLocalOffHeapVectorStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/SparseLocalOffHeapVectorStorageTest.java new file mode 100644 index 0000000..7e5fc48 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/SparseLocalOffHeapVectorStorageTest.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.storage.vector; + +import org.apache.ignite.ml.math.ExternalizeTest; +import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.apache.ignite.ml.math.impls.MathTestConstants.STORAGE_SIZE; +import static org.apache.ignite.ml.math.impls.MathTestConstants.UNEXPECTED_VAL; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Tests for {@link SparseLocalOffHeapVectorStorage}. + */ +public class SparseLocalOffHeapVectorStorageTest extends ExternalizeTest<SparseLocalOffHeapVectorStorage> { + /** */ private SparseLocalOffHeapVectorStorage testVectorStorage; + + /** */ + @Before + public void setup() { + testVectorStorage = new SparseLocalOffHeapVectorStorage(STORAGE_SIZE); + } + + /** */ + @After + public void teardown() { + testVectorStorage.destroy(); + testVectorStorage = null; + } + + /** */ + @Test + public void testBasic() { + for (int i = 0; i < STORAGE_SIZE; i++) { + double testVal = Math.random(); + testVectorStorage.set(i, testVal); + assertEquals(UNEXPECTED_VAL, testVal, testVectorStorage.get(i), 0d); + } + } + + /** {@inheritDoc} */ + @Test(expected = UnsupportedOperationException.class) + @Override public void externalizeTest() { + super.externalizeTest(new SparseLocalOffHeapVectorStorage(STORAGE_SIZE)); + } + + /** */ + @Test + public void testAttributes() { + SparseLocalOffHeapVectorStorage testVectorStorage = new SparseLocalOffHeapVectorStorage(STORAGE_SIZE); + + assertTrue(UNEXPECTED_VAL, testVectorStorage.isRandomAccess()); + assertFalse(UNEXPECTED_VAL, testVectorStorage.isSequentialAccess()); + assertFalse(UNEXPECTED_VAL, testVectorStorage.isDense()); + assertFalse(UNEXPECTED_VAL, testVectorStorage.isArrayBased()); + assertFalse(UNEXPECTED_VAL, testVectorStorage.isDistributed()); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorArrayStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorArrayStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorArrayStorageTest.java new file mode 100644 index 0000000..ce62b3e --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorArrayStorageTest.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.storage.vector; + +import java.util.Arrays; +import org.apache.ignite.ml.math.impls.MathTestConstants; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Unit test for {@link ArrayVectorStorage}. + */ +public class VectorArrayStorageTest extends VectorBaseStorageTest<ArrayVectorStorage> { + /** */ + @Override public void setUp() { + storage = new ArrayVectorStorage(MathTestConstants.STORAGE_SIZE); + } + + /** */ + @Test + public void isArrayBased() throws Exception { + assertTrue(MathTestConstants.WRONG_ATTRIBUTE_VAL, storage.isArrayBased()); + + assertTrue(MathTestConstants.WRONG_ATTRIBUTE_VAL, new ArrayVectorStorage().isArrayBased()); + } + + /** */ + @Test + public void data() throws Exception { + assertNotNull(MathTestConstants.NULL_DATA_STORAGE, storage.data()); + + assertEquals(MathTestConstants.WRONG_DATA_SIZE, storage.data().length, MathTestConstants.STORAGE_SIZE); + + assertTrue(MathTestConstants.UNEXPECTED_DATA_VAL, Arrays.equals(storage.data(), new double[MathTestConstants.STORAGE_SIZE])); + + assertNull(MathTestConstants.UNEXPECTED_DATA_VAL, new ArrayVectorStorage().data()); + } + +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorBaseStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorBaseStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorBaseStorageTest.java new file mode 100644 index 0000000..206fe85 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorBaseStorageTest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.storage.vector; + +import org.apache.ignite.ml.math.ExternalizeTest; +import org.apache.ignite.ml.math.VectorStorage; +import org.apache.ignite.ml.math.impls.MathTestConstants; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Abstract class with base tests for each vector storage. + */ +public abstract class VectorBaseStorageTest<T extends VectorStorage> extends ExternalizeTest<T> { + /** */ + protected T storage; + + /** */ + @Before + public abstract void setUp(); + + /** */ + @After + public void tearDown() throws Exception { + storage.destroy(); + } + + /** */ + @Test + public void getSet() throws Exception { + for (int i = 0; i < MathTestConstants.STORAGE_SIZE; i++) { + double random = Math.random(); + + storage.set(i, random); + + assertEquals(MathTestConstants.WRONG_DATA_ELEMENT, storage.get(i), random, MathTestConstants.NIL_DELTA); + } + } + + /** */ + @Test + public void size() { + assertTrue(MathTestConstants.UNEXPECTED_VAL, storage.size() == MathTestConstants.STORAGE_SIZE); + } + + /** */ + @Override public void externalizeTest() { + super.externalizeTest(storage); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorOffheapStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorOffheapStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorOffheapStorageTest.java new file mode 100644 index 0000000..6b719d0 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/VectorOffheapStorageTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.storage.vector; + +import org.apache.ignite.ml.math.impls.MathTestConstants; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Unit tests for {@link DenseLocalOffHeapVectorStorage}. + */ +public class VectorOffheapStorageTest extends VectorBaseStorageTest<DenseLocalOffHeapVectorStorage> { + /** */ + @Before + public void setUp() { + storage = new DenseLocalOffHeapVectorStorage(MathTestConstants.STORAGE_SIZE); + } + + /** */ + @Test + public void isArrayBased() throws Exception { + assertFalse(MathTestConstants.UNEXPECTED_VAL, storage.isArrayBased()); + } + + /** */ + @Test + public void data() throws Exception { + assertNull(MathTestConstants.NULL_VAL, storage.data()); + } + + /** */ + @Test + public void isSequentialAccess() throws Exception { + assertTrue(MathTestConstants.UNEXPECTED_VAL, storage.isSequentialAccess()); + } + + /** */ + @Test + public void isDense() throws Exception { + assertTrue(MathTestConstants.UNEXPECTED_VAL, storage.isDense()); + } + + /** */ + @Test + public void equalsTest() { + //noinspection EqualsWithItself + assertTrue(MathTestConstants.VAL_NOT_EQUALS, storage.equals(storage)); + + //noinspection EqualsBetweenInconvertibleTypes + assertFalse(MathTestConstants.VALUES_SHOULD_BE_NOT_EQUALS, + storage.equals(new ArrayVectorStorage(MathTestConstants.STORAGE_SIZE))); + } + +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/AbstractVectorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/AbstractVectorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/AbstractVectorTest.java new file mode 100644 index 0000000..fa66769 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/AbstractVectorTest.java @@ -0,0 +1,543 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import java.util.Arrays; +import java.util.stream.StreamSupport; +import org.apache.ignite.ml.math.Matrix; +import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.VectorStorage; +import org.apache.ignite.ml.math.exceptions.IndexException; +import org.apache.ignite.ml.math.functions.Functions; +import org.apache.ignite.ml.math.impls.MathTestConstants; +import org.apache.ignite.ml.math.impls.storage.vector.ArrayVectorStorage; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Unit test for {@link AbstractVector}. + */ +public class AbstractVectorTest { + /** */ + private AbstractVector testVector; + + /** */ + @Before + public void setUp() { + testVector = getAbstractVector(); + } + + /** */ + @Test + public void setStorage() { + testVector.setStorage(createStorage()); + + assertTrue(testVector.size() == MathTestConstants.STORAGE_SIZE); + } + + /** */ + @Test + public void size() { + testVector.setStorage(createStorage()); + assertTrue(testVector.size() == MathTestConstants.STORAGE_SIZE); + + testVector.setStorage(new ArrayVectorStorage(MathTestConstants.STORAGE_SIZE + MathTestConstants.STORAGE_SIZE)); + assertTrue(testVector.size() == MathTestConstants.STORAGE_SIZE + MathTestConstants.STORAGE_SIZE); + + testVector = getAbstractVector(createStorage()); + assertTrue(testVector.size() == MathTestConstants.STORAGE_SIZE); + } + + /** */ + @Test + public void getPositive() { + testVector = getAbstractVector(createStorage()); + + for (int i = 0; i < MathTestConstants.STORAGE_SIZE; i++) + assertNotNull(MathTestConstants.NULL_VALUES, testVector.get(i)); + + } + + /** */ + @Test(expected = NullPointerException.class) + public void getNegative0() { + testVector.get(0); + } + + /** */ + @Test(expected = IndexException.class) + public void getNegative1() { + testVector.setStorage(createStorage()); + + testVector.get(-1); + } + + /** */ + @Test(expected = IndexException.class) + public void getNegative2() { + testVector.setStorage(createStorage()); + + testVector.get(testVector.size() + 1); + } + + /** */ + @Test(expected = NullPointerException.class) + public void getXNegative0() { + testVector.getX(0); + } + + /** */ + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void getXNegative1() { + testVector.setStorage(createStorage()); + + testVector.getX(-1); + } + + /** */ + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void getXNegative2() { + testVector.setStorage(createStorage()); + + testVector.getX(MathTestConstants.STORAGE_SIZE + 1); + } + + /** */ + @Test + public void set() { + double[] data = initVector(); + + for (int i = 0; i < MathTestConstants.STORAGE_SIZE; i++) + testVector.set(i, Math.exp(data[i])); + + for (int i = 0; i < MathTestConstants.STORAGE_SIZE; i++) + assertEquals(MathTestConstants.VAL_NOT_EQUALS, testVector.get(i), Math.exp(data[i]), MathTestConstants.NIL_DELTA); + } + + /** */ + @Test(expected = IndexException.class) + public void setNegative0() { + testVector.set(-1, -1); + } + + /** */ + @Test(expected = IndexException.class) + public void setNegative1() { + initVector(); + + testVector.set(-1, -1); + } + + /** */ + @Test(expected = IndexException.class) + public void setNegative2() { + initVector(); + + testVector.set(MathTestConstants.STORAGE_SIZE + 1, -1); + } + + /** */ + @Test(expected = IndexOutOfBoundsException.class) + public void setXNegative0() { + initVector(); + + testVector.setX(-1, -1); + } + + /** */ + @Test(expected = IndexOutOfBoundsException.class) + public void setXNegative1() { + initVector(); + + testVector.setX(MathTestConstants.STORAGE_SIZE + 1, -1); + } + + /** */ + @Test(expected = NullPointerException.class) + public void setXNegative2() { + testVector.setX(-1, -1); + } + + /** */ + @Test + public void isZero() { + assertTrue(MathTestConstants.UNEXPECTED_VAL, testVector.isZero(0d)); + + assertFalse(MathTestConstants.UNEXPECTED_VAL, testVector.isZero(1d)); + } + + /** */ + @Test + public void guid() { + assertNotNull(MathTestConstants.NULL_GUID, testVector.guid()); + + assertEquals(MathTestConstants.UNEXPECTED_GUID_VAL, testVector.guid(), testVector.guid()); + + assertFalse(MathTestConstants.EMPTY_GUID, testVector.guid().toString().isEmpty()); + + testVector = getAbstractVector(createStorage()); + + assertNotNull(MathTestConstants.NULL_GUID, testVector.guid()); + + assertEquals(MathTestConstants.UNEXPECTED_GUID_VAL, testVector.guid(), testVector.guid()); + + assertFalse(MathTestConstants.EMPTY_GUID, testVector.guid().toString().isEmpty()); + } + + /** */ + @Test + public void equalsTest() { + VectorStorage storage = createStorage(); + + AbstractVector testVector1 = getAbstractVector(); + + testVector1.setStorage(storage); + + AbstractVector testVector2 = getAbstractVector(); + + assertEquals(MathTestConstants.VAL_NOT_EQUALS, testVector, testVector); + + testVector2.setStorage(storage); + + assertTrue(MathTestConstants.VAL_NOT_EQUALS, testVector1.equals(testVector2)); + + assertFalse(MathTestConstants.VALUES_SHOULD_BE_NOT_EQUALS, testVector1.equals(testVector)); + } + + /** */ + @Test(expected = NullPointerException.class) + public void all() { + assertNotNull(MathTestConstants.NULL_VAL, testVector.all()); + + assertNotNull(MathTestConstants.NULL_VAL, getAbstractVector(createStorage()).all()); + + getAbstractVector().all().iterator().next(); + } + + /** */ + @Test + public void nonZeroElements() { + VectorStorage storage = createStorage(); + + double[] data = storage.data(); + + testVector = getAbstractVector(storage); + + assertEquals(MathTestConstants.VAL_NOT_EQUALS, testVector.nonZeroElements(), Arrays.stream(data).filter(x -> x != 0d).count()); + + addNilValues(data); + + assertEquals(MathTestConstants.VAL_NOT_EQUALS, testVector.nonZeroElements(), Arrays.stream(data).filter(x -> x != 0d).count()); + } + + /** */ + @Test + public void foldMapWithSecondVector() { + double[] data0 = initVector(); + + VectorStorage storage1 = createStorage(); + + double[] data1 = storage1.data().clone(); + + AbstractVector testVector1 = getAbstractVector(storage1); + + String testVal = ""; + + for (int i = 0; i < data0.length; i++) + testVal += data0[i] + data1[i]; + + assertEquals(MathTestConstants.VAL_NOT_EQUALS, testVector.foldMap(testVector1, (string, xi) -> string.concat(xi.toString()), Functions.PLUS, ""), testVal); + } + + /** */ + @Test + public void nonZeroes() { + assertNotNull(MathTestConstants.NULL_VAL, testVector.nonZeroes()); + + double[] data = initVector(); + + assertNotNull(MathTestConstants.NULL_VAL, testVector.nonZeroes()); + + Assert.assertEquals(MathTestConstants.VAL_NOT_EQUALS, StreamSupport.stream(testVector.nonZeroes().spliterator(), false).count(), Arrays.stream(data).filter(x -> x != 0d).count()); + + addNilValues(data); + + Assert.assertEquals(MathTestConstants.VAL_NOT_EQUALS, StreamSupport.stream(testVector.nonZeroes().spliterator(), false).count(), Arrays.stream(data).filter(x -> x != 0d).count()); + } + + /** */ + @Test(expected = NullPointerException.class) + public void nonZeroesEmpty() { + testVector.nonZeroes().iterator().next(); + } + + /** */ + @Test(expected = NullPointerException.class) + public void assign() { + testVector.assign(MathTestConstants.TEST_VAL); + } + + /** */ + @Test(expected = NullPointerException.class) + public void assignArr() { + testVector.assign(new double[1]); + } + + /** */ + @Test(expected = NullPointerException.class) + public void assignArrEmpty() { + testVector.assign(new double[0]); + } + + /** */ + @Test(expected = NullPointerException.class) + public void dotNegative() { + testVector.dot(getAbstractVector(createEmptyStorage())); + } + + /** */ + @Test + public void dotSelf() { + double[] data = initVector(); + + assertEquals(MathTestConstants.VAL_NOT_EQUALS, testVector.dotSelf(), Arrays.stream(data).reduce(0, (x, y) -> x + y * y), MathTestConstants.NIL_DELTA); + } + + /** */ + @Test + public void getStorage() { + assertNotNull(MathTestConstants.NULL_VAL, getAbstractVector(createEmptyStorage())); + assertNotNull(MathTestConstants.NULL_VAL, getAbstractVector(createStorage())); + testVector.setStorage(createStorage()); + assertNotNull(MathTestConstants.NULL_VAL, testVector.getStorage()); + } + + /** */ + @Test + public void getElement() { + double[] data = initVector(); + + for (int i = 0; i < data.length; i++) { + assertNotNull(MathTestConstants.NULL_VAL, testVector.getElement(i)); + + assertEquals(MathTestConstants.UNEXPECTED_VAL, testVector.getElement(i).get(), data[i], MathTestConstants.NIL_DELTA); + + testVector.getElement(i).set(++data[i]); + + assertEquals(MathTestConstants.UNEXPECTED_VAL, testVector.getElement(i).get(), data[i], MathTestConstants.NIL_DELTA); + } + } + + /** + * Create {@link AbstractVector} with storage for tests. + * + * @param storage {@link VectorStorage} + * @return AbstractVector. + */ + private AbstractVector getAbstractVector(VectorStorage storage) { + return new AbstractVector(storage) { // TODO: find out how to fix warning about missing constructor + /** */ + @Override public boolean isDense() { + return false; + } + + /** */ + @Override public boolean isSequentialAccess() { + return false; + } + + /** */ + @Override public Matrix likeMatrix(int rows, int cols) { + return null; + } + + /** */ + @Override public Vector copy() { + return getAbstractVector(this.getStorage()); + } + + /** */ + @Override public Vector like(int crd) { + return null; + } + + /** */ + @Override public Vector normalize() { + return null; + } + + /** */ + @Override public Vector normalize(double power) { + return null; + } + + /** */ + @Override public Vector logNormalize() { + return null; + } + + /** */ + @Override public Vector logNormalize(double power) { + return null; + } + + /** */ + @Override public Vector viewPart(int off, int len) { + return null; + } + + /** {@inheritDoc} */ + @Override public boolean isRandomAccess() { + return true; + } + + /** {@inheritDoc} */ + @Override public boolean isDistributed() { + return false; + } + }; + } + + /** + * Create empty {@link AbstractVector} for tests. + * + * @return AbstractVector. + */ + private AbstractVector getAbstractVector() { + return new AbstractVector() { // TODO: find out how to fix warning about missing constructor + /** */ + @Override public boolean isDense() { + return false; + } + + /** */ + @Override public Matrix likeMatrix(int rows, int cols) { + return null; + } + + /** */ + @Override public boolean isSequentialAccess() { + return false; + } + + /** */ + @Override public Vector copy() { + return getAbstractVector(this.getStorage()); + } + + /** */ + @Override public Vector like(int crd) { + return null; + } + + /** */ + @Override public Vector normalize() { + return null; + } + + /** */ + @Override public Vector normalize(double power) { + return null; + } + + /** */ + @Override public Vector logNormalize() { + return null; + } + + /** */ + @Override public Vector logNormalize(double power) { + return null; + } + + /** */ + @Override public Vector viewPart(int off, int len) { + return null; + } + + /** {@inheritDoc} */ + @Override public boolean isRandomAccess() { + return true; + } + + /** {@inheritDoc} */ + @Override public boolean isDistributed() { + return false; + } + }; + } + + /** + * Create {@link VectorStorage} for tests. + * + * @return VectorStorage + */ + private VectorStorage createEmptyStorage() { + return new ArrayVectorStorage(MathTestConstants.STORAGE_SIZE); + } + + /** + * Create filled {@link VectorStorage} for tests. + * + * @return VectorStorage. + */ + private VectorStorage createStorage() { + ArrayVectorStorage storage = new ArrayVectorStorage(MathTestConstants.STORAGE_SIZE); + + for (int i = 0; i < MathTestConstants.STORAGE_SIZE; i++) + storage.set(i, Math.random()); + + return storage; + } + + /** + * Init vector and return initialized values. + * + * @return Initial values. + */ + private double[] initVector() { + VectorStorage storage = createStorage(); + double[] data = storage.data().clone(); + + testVector = getAbstractVector(storage); + return data; + } + + /** + * Add some zeroes to vector elements. + */ + private void addNilValues() { + testVector.set(10, 0); + testVector.set(50, 0); + } + + /** + * Add some zeroes to vector elements. Also set zeroes to the same elements in reference array data + */ + private void addNilValues(double[] testRef) { + addNilValues(); + testRef[10] = 0; + testRef[50] = 0; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/CacheVectorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/CacheVectorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/CacheVectorTest.java new file mode 100644 index 0000000..0026e2b --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/CacheVectorTest.java @@ -0,0 +1,434 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.stream.IntStream; +import junit.framework.TestCase; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.ml.math.IdentityValueMapper; +import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.VectorKeyMapper; +import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; +import org.apache.ignite.ml.math.functions.Functions; +import org.apache.ignite.ml.math.impls.MathTestConstants; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.apache.ignite.testframework.junits.common.GridCommonTest; + +/** + * Tests for {@link CacheVector}. + */ +@GridCommonTest(group = "Distributed Models") +public class CacheVectorTest extends GridCommonAbstractTest { + /** Number of nodes in grid */ + private static final int NODE_COUNT = 3; + /** Cache name. */ + private static final String CACHE_NAME = "test-cache"; + /** Cache size. */ + private static final int size = MathTestConstants.STORAGE_SIZE; + /** Grid instance. */ + private Ignite ignite; + /** Default key mapper. */ + private VectorKeyMapper<Integer> keyMapper = new TestKeyMapper(); + + /** + * Default constructor. + */ + public CacheVectorTest() { + super(false); + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + for (int i = 1; i <= NODE_COUNT; i++) + startGrid(i); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + } + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + ignite = grid(NODE_COUNT); + + ignite.configuration().setPeerClassLoadingEnabled(true); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + ignite.destroyCache(CACHE_NAME); + } + + /** */ + public void testGetSet() throws Exception { + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + for (int i = 0; i < size; i++) { + double random = Math.random(); + cacheVector.set(i, random); + assertEquals("Unexpected value.", random, cacheVector.get(i), 0d); + } + } + + /** */ + public void testMap() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + initVector(cacheVector); + + cacheVector.map(value -> 110d); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), 110d, 0d); + } + + /** */ + public void testMapBiFunc() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + initVector(cacheVector); + + cacheVector.map(Functions.PLUS, 1d); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), 1d, 0d); + } + + /** */ + public void testSum() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + initVector(cacheVector); + + assertEquals("Unexpected value.", cacheVector.sum(), 0d, 0d); + + cacheVector.assign(1d); + + assertEquals("Unexpected value.", cacheVector.sum(), size, 0d); + } + + /** */ + public void testSumNegative() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + try { + double d = cacheVector.sum(); + fail(); + } + catch (NullPointerException e) { + // No-op. + } + } + + /** */ + public void testAssign() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + initVector(cacheVector); + + cacheVector.assign(1d); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), 1d, 0d); + } + + /** */ + public void testAssignRange() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + cacheVector.assign(IntStream.range(0, size).asDoubleStream().toArray()); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), i, 0d); + } + + /** */ + public void testAssignVector() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); + + cacheVector.assign(testVec); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), testVec.get(i), 0d); + } + + /** */ + public void testAssignFunc() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + cacheVector.assign(idx -> idx); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), i, 0d); + } + + /** */ + public void testPlus() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + initVector(cacheVector); + + cacheVector.plus(1d); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), 1d, 0d); + } + + /** */ + public void testPlusVec() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); + + try { + cacheVector.plus(testVec); + TestCase.fail(); + } + catch (UnsupportedOperationException ignored) { + + } + } + + /** */ + public void testDivide() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + final int size = MathTestConstants.STORAGE_SIZE; + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + initVector(cacheVector); + cacheVector.assign(1d); + + cacheVector.divide(2d); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), 1d / 2d, 0d); + } + + /** */ + public void testTimes() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + final int size = MathTestConstants.STORAGE_SIZE; + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + initVector(cacheVector); + cacheVector.assign(1d); + + cacheVector.times(2d); + + for (int i = 0; i < size; i++) + assertEquals("Unexpected value.", cacheVector.get(i), 2d, 0d); + } + + /** */ + public void testTimesVector() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + cacheVector.assign(1d); + Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); + + try { + cacheVector.times(testVec); + TestCase.fail(); + } + catch (UnsupportedOperationException ignored) { + + } + + } + + /** */ + public void testMin() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); + + cacheVector.assign(testVec); + + assertEquals("Unexpected value.", cacheVector.minValue(), 0d, 0d); + } + + /** */ + public void testMax() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); + + cacheVector.assign(testVec); + + assertEquals("Unexpected value.", cacheVector.maxValue(), testVec.get(size - 1), 0d); + } + + /** */ + public void testLike() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + try { + cacheVector.like(size); + TestCase.fail("Unsupported case"); + } + catch (UnsupportedOperationException ignored) { + + } + } + + /** */ + public void testLikeMatrix() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + try { + cacheVector.likeMatrix(size, size); + TestCase.fail("Unsupported case"); + } + catch (UnsupportedOperationException ignored) { + + } + } + + /** */ + public void testCopy() { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + try { + cacheVector.copy(); + TestCase.fail("Unsupported case"); + } + catch (UnsupportedOperationException ignored) { + + } + } + + /** */ + public void testExternalize() throws IOException, ClassNotFoundException { + IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); + + IdentityValueMapper valMapper = new IdentityValueMapper(); + CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); + + cacheVector.set(1, 1.0); + + ByteArrayOutputStream byteArrOutputStream = new ByteArrayOutputStream(); + ObjectOutputStream objOutputStream = new ObjectOutputStream(byteArrOutputStream); + + objOutputStream.writeObject(cacheVector); + + ByteArrayInputStream byteArrInputStream = new ByteArrayInputStream(byteArrOutputStream.toByteArray()); + ObjectInputStream objInputStream = new ObjectInputStream(byteArrInputStream); + + CacheVector objRestored = (CacheVector)objInputStream.readObject(); + + assertTrue(MathTestConstants.VAL_NOT_EQUALS, cacheVector.equals(objRestored)); + assertEquals(MathTestConstants.VAL_NOT_EQUALS, objRestored.get(1), 1.0, 0.0); + } + + /** */ + private void initVector(CacheVector cacheVector) { + for (int i = 0; i < cacheVector.size(); i++) + cacheVector.set(i, 0d); + } + + /** */ + private IgniteCache<Integer, Double> getCache() { + assert ignite != null; + + CacheConfiguration cfg = new CacheConfiguration(); + cfg.setName(CACHE_NAME); + + IgniteCache<Integer, Double> cache = ignite.getOrCreateCache(CACHE_NAME); + + assert cache != null; + return cache; + } + + /** */ private static class TestKeyMapper implements VectorKeyMapper<Integer> { + /** {@inheritDoc} */ + @Override public Integer apply(int i) { + return i; + } + + /** {@inheritDoc} */ + @Override public boolean isValid(Integer i) { + return i < size; + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/ConstantVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/ConstantVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/ConstantVectorConstructorTest.java new file mode 100644 index 0000000..c5c0bbd --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/ConstantVectorConstructorTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** */ +public class ConstantVectorConstructorTest { + /** */ + private static final int IMPOSSIBLE_SIZE = -1; + + /** */ + @Test(expected = AssertionError.class) + public void negativeSizeTest() { + assertEquals("Negative size.", IMPOSSIBLE_SIZE, + new ConstantVector(-1, 1).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void zeroSizeTest() { + assertEquals("Zero size.", IMPOSSIBLE_SIZE, + new ConstantVector(0, 1).size()); + } + + /** */ + @Test + public void primitiveTest() { + assertEquals("1 size.", 1, + new ConstantVector(1, 1).size()); + + assertEquals("2 size.", 2, + new ConstantVector(2, 1).size()); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DelegatingVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DelegatingVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DelegatingVectorConstructorTest.java new file mode 100644 index 0000000..1a9884f --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DelegatingVectorConstructorTest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import org.apache.ignite.ml.math.Vector; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** */ +public class DelegatingVectorConstructorTest { + /** */ + private static final int IMPOSSIBLE_SIZE = -1; + + /** */ + @Test + public void basicTest() { + final Vector parent = new DenseLocalOnHeapVector(new double[] {0, 1}); + + final Vector delegate = new DelegatingVector(parent); + + final int size = parent.size(); + + assertEquals("Delegate size differs from expected.", size, delegate.size()); + + for (int idx = 0; idx < size; idx++) + assertDelegate(parent, delegate, idx); + } + + /** */ + private void assertDelegate(Vector parent, Vector delegate, int idx) { + assertValue(parent, delegate, idx); + + parent.set(idx, parent.get(idx) + 1); + + assertValue(parent, delegate, idx); + + delegate.set(idx, delegate.get(idx) + 2); + + assertValue(parent, delegate, idx); + } + + /** */ + private void assertValue(Vector parent, Vector delegate, int idx) { + assertEquals("Unexpected value at index " + idx, parent.get(idx), delegate.get(idx), 0d); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DenseLocalOffHeapVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DenseLocalOffHeapVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DenseLocalOffHeapVectorConstructorTest.java new file mode 100644 index 0000000..1a979bd --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DenseLocalOffHeapVectorConstructorTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** */ +public class DenseLocalOffHeapVectorConstructorTest { + /** */ + private static final int IMPOSSIBLE_SIZE = -1; + + /** */ + @Test(expected = AssertionError.class) + public void negativeSizeTest() { + assertEquals("Negative size.", IMPOSSIBLE_SIZE, + new DenseLocalOffHeapVector(-1).size()); + } + + /** */ + @Test(expected = NullPointerException.class) + public void nullArrayTest() { + assertEquals("Null array.", IMPOSSIBLE_SIZE, + new DenseLocalOffHeapVector(null).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void zeroSizeTest() { + assertEquals("0 size.", IMPOSSIBLE_SIZE, + new DenseLocalOffHeapVector(new double[0]).size()); + } + + /** */ + @Test + public void primitiveTest() { + assertEquals("1 size.", 1, + new DenseLocalOffHeapVector(new double[1]).size()); + + assertEquals("2 size.", 2, + new DenseLocalOffHeapVector(new double[2]).size()); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DenseLocalOnHeapVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DenseLocalOnHeapVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DenseLocalOnHeapVectorConstructorTest.java new file mode 100644 index 0000000..df504f3 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/DenseLocalOnHeapVectorConstructorTest.java @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import java.util.HashMap; +import java.util.Map; +import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** */ +public class DenseLocalOnHeapVectorConstructorTest { + /** */ + private static final int IMPOSSIBLE_SIZE = -1; + + /** */ + @Test(expected = UnsupportedOperationException.class) + public void mapInvalidArgsTest() { + assertEquals("Expect exception due to invalid args.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector(new HashMap<String, Object>() {{ + put("invalid", 99); + }}).size()); + } + + /** */ + @Test(expected = UnsupportedOperationException.class) + public void mapMissingArgsTest() { + final Map<String, Object> test = new HashMap<String, Object>() {{ + put("arr", new double[0]); + put("shallowCopyMissing", "whatever"); + }}; + + assertEquals("Expect exception due to missing args.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector(test).size()); + } + + /** */ + @Test(expected = ClassCastException.class) + public void mapInvalidArrTypeTest() { + final Map<String, Object> test = new HashMap<String, Object>() {{ + put("size", "whatever"); + }}; + + assertEquals("Expect exception due to invalid arr type.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector(test).size()); + } + + /** */ + @Test(expected = UnsupportedOperationException.class) + public void mapInvalidCopyTypeTest() { + final Map<String, Object> test = new HashMap<String, Object>() {{ + put("arr", new double[0]); + put("shallowCopy", 0); + }}; + + assertEquals("Expect exception due to invalid copy type.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector(test).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void mapNullTest() { + //noinspection ConstantConditions + assertEquals("Null map args.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector((Map<String, Object>)null).size()); + } + + /** */ + @Test + public void mapTest() { + assertEquals("Size from args.", 99, + new DenseLocalOnHeapVector(new HashMap<String, Object>() {{ + put("size", 99); + }}).size()); + + final double[] test = new double[99]; + + assertEquals("Size from array in args.", test.length, + new DenseLocalOnHeapVector(new HashMap<String, Object>() {{ + put("arr", test); + put("copy", false); + }}).size()); + + assertEquals("Size from array in args, shallow copy.", test.length, + new DenseLocalOnHeapVector(new HashMap<String, Object>() {{ + put("arr", test); + put("copy", true); + }}).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void negativeSizeTest() { + assertEquals("Negative size.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector(-1).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void nullCopyTest() { + assertEquals("Null array to non-shallow copy.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector(null, false).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void nullDefaultCopyTest() { + assertEquals("Null array default copy.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector((double[])null).size()); + } + + /** */ + @Test(expected = NullPointerException.class) + public void defaultConstructorTest() { + assertEquals("Default constructor.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector().size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void nullArrShallowCopyTest() { + assertEquals("Null array shallow copy.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector(null, true).size()); + } + + /** */ + @Test + public void primitiveTest() { + assertEquals("0 size shallow copy.", 0, + new DenseLocalOnHeapVector(new double[0], true).size()); + + assertEquals("0 size.", 0, + new DenseLocalOnHeapVector(new double[0], false).size()); + + assertEquals("1 size shallow copy.", 1, + new DenseLocalOnHeapVector(new double[1], true).size()); + + assertEquals("1 size.", 1, + new DenseLocalOnHeapVector(new double[1], false).size()); + + assertEquals("0 size default copy.", 0, + new DenseLocalOnHeapVector(new double[0]).size()); + + assertEquals("1 size default copy.", 1, + new DenseLocalOnHeapVector(new double[1]).size()); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/FunctionVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/FunctionVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/FunctionVectorConstructorTest.java new file mode 100644 index 0000000..1e99f7e --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/FunctionVectorConstructorTest.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.IntToDoubleFunction; +import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; +import org.apache.ignite.ml.math.functions.IgniteFunction; +import org.apache.ignite.ml.math.functions.IntDoubleToVoidFunction; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** */ +public class FunctionVectorConstructorTest { + /** */ + private static final int IMPOSSIBLE_SIZE = -1; + + /** */ + @Test(expected = UnsupportedOperationException.class) + public void mapInvalidArgsTest() { + assertEquals("Expect exception due to invalid args.", IMPOSSIBLE_SIZE, + new FunctionVector(new HashMap<String, Object>() {{ + put("invalid", 99); + }}).size()); + } + + /** */ + @Test(expected = UnsupportedOperationException.class) + public void mapMissingArgsTest() { + final Map<String, Object> test = new HashMap<String, Object>() {{ + put("size", 1); + put("paramMissing", "whatever"); + }}; + + assertEquals("Expect exception due to missing args.", + -1, new FunctionVector(test).size()); + } + + /** */ + @Test(expected = ClassCastException.class) + public void mapInvalidParamTypeTest() { + final Map<String, Object> test = new HashMap<String, Object>() {{ + put("size", "whatever"); + + put("getFunc", (IntToDoubleFunction)i -> i); + }}; + + assertEquals("Expect exception due to invalid param type.", IMPOSSIBLE_SIZE, + new FunctionVector(test).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void mapNullTest() { + //noinspection ConstantConditions + assertEquals("Null map args.", IMPOSSIBLE_SIZE, + new FunctionVector(null).size()); + } + + /** */ + @Test + public void mapTest() { + assertEquals("Size from args.", 99, + new FunctionVector(new HashMap<String, Object>() {{ + put("size", 99); + + put("getFunc", (IgniteFunction<Integer, Double>)i -> (double)i); + }}).size()); + + assertEquals("Size from args with setFunc.", 99, + new FunctionVector(new HashMap<String, Object>() {{ + put("size", 99); + + put("getFunc", (IgniteFunction<Integer, Double>)i -> (double)i); + + put("setFunc", (IntDoubleToVoidFunction)(integer, aDouble) -> { + }); + }}).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void negativeSizeTest() { + assertEquals("Negative size.", IMPOSSIBLE_SIZE, + new FunctionVector(-1, (i) -> (double)i).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void zeroSizeTest() { + assertEquals("0 size.", IMPOSSIBLE_SIZE, + new FunctionVector(0, (i) -> (double)i).size()); + } + + /** */ + @Test + public void primitiveTest() { + assertEquals("1 size.", 1, + new FunctionVector(1, (i) -> (double)i).size()); + + assertEquals("2 size.", 2, + new FunctionVector(2, (i) -> (double)i).size()); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/MatrixVectorViewTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/MatrixVectorViewTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/MatrixVectorViewTest.java new file mode 100644 index 0000000..c861a0e --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/MatrixVectorViewTest.java @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import org.apache.ignite.ml.math.Matrix; +import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.exceptions.IndexException; +import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Tests for {@link MatrixVectorView}. + */ +public class MatrixVectorViewTest { + /** */ + private static final String UNEXPECTED_VALUE = "Unexpected value"; + /** */ + private static final int SMALL_SIZE = 3; + /** */ + private static final int IMPOSSIBLE_SIZE = -1; + + /** */ + private Matrix parent; + + /** */ + @Before + public void setup() { + parent = newMatrix(SMALL_SIZE, SMALL_SIZE); + } + + /** */ + @Test + public void testDiagonal() { + Vector vector = parent.viewDiagonal(); + + for (int i = 0; i < SMALL_SIZE; i++) + assertView(i, i, vector, i); + } + + /** */ + @Test + public void testRow() { + for (int i = 0; i < SMALL_SIZE; i++) { + Vector viewRow = parent.viewRow(i); + + for (int j = 0; j < SMALL_SIZE; j++) + assertView(i, j, viewRow, j); + } + } + + /** */ + @Test + public void testCols() { + for (int i = 0; i < SMALL_SIZE; i++) { + Vector viewCol = parent.viewColumn(i); + + for (int j = 0; j < SMALL_SIZE; j++) + assertView(j, i, viewCol, j); + } + } + + /** */ + @Test + public void basicTest() { + for (int rowSize : new int[] {1, 2, 3, 4}) + for (int colSize : new int[] {1, 2, 3, 4}) + for (int row = 0; row < rowSize; row++) + for (int col = 0; col < colSize; col++) + for (int rowStride = 0; rowStride < rowSize; rowStride++) + for (int colStride = 0; colStride < colSize; colStride++) + if (rowStride != 0 || colStride != 0) + assertMatrixVectorView(newMatrix(rowSize, colSize), row, col, rowStride, colStride); + } + + /** */ + @Test(expected = AssertionError.class) + public void parentNullTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(null, 1, 1, 1, 1).size()); + } + + /** */ + @Test(expected = IndexException.class) + public void rowNegativeTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, -1, 1, 1, 1).size()); + } + + /** */ + @Test(expected = IndexException.class) + public void colNegativeTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, 1, -1, 1, 1).size()); + } + + /** */ + @Test(expected = IndexException.class) + public void rowTooLargeTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, parent.rowSize() + 1, 1, 1, 1).size()); + } + + /** */ + @Test(expected = IndexException.class) + public void colTooLargeTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, 1, parent.columnSize() + 1, 1, 1).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void rowStrideNegativeTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, 1, 1, -1, 1).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void colStrideNegativeTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, 1, 1, 1, -1).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void rowStrideTooLargeTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, 1, 1, parent.rowSize() + 1, 1).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void colStrideTooLargeTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, 1, 1, 1, parent.columnSize() + 1).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void bothStridesZeroTest() { + //noinspection ConstantConditions + assertEquals(IMPOSSIBLE_SIZE, + new MatrixVectorView(parent, 1, 1, 0, 0).size()); + } + + /** */ + private void assertMatrixVectorView(Matrix parent, int row, int col, int rowStride, int colStride) { + MatrixVectorView view = new MatrixVectorView(parent, row, col, rowStride, colStride); + + String desc = "parent [" + parent.rowSize() + "x" + parent.columnSize() + "], view [" + + row + "x" + col + "], strides [" + rowStride + ", " + colStride + "]"; + + final int size = view.size(); + + final int sizeByRows = rowStride == 0 ? IMPOSSIBLE_SIZE : (parent.rowSize() - row) / rowStride; + final int sizeByCols = colStride == 0 ? IMPOSSIBLE_SIZE : (parent.columnSize() - col) / colStride; + + assertTrue("Size " + size + " differs from expected for " + desc, + size == sizeByRows || size == sizeByCols); + + for (int idx = 0; idx < size; idx++) { + final int rowIdx = row + idx * rowStride; + final int colIdx = col + idx * colStride; + + assertEquals(UNEXPECTED_VALUE + " at view index " + idx + desc, + parent.get(rowIdx, colIdx), view.get(idx), 0d); + } + } + + /** */ + private Matrix newMatrix(int rowSize, int colSize) { + Matrix res = new DenseLocalOnHeapMatrix(rowSize, colSize); + + for (int i = 0; i < res.rowSize(); i++) + for (int j = 0; j < res.columnSize(); j++) + res.set(i, j, i * res.rowSize() + j); + + return res; + } + + /** */ + private void assertView(int row, int col, Vector view, int viewIdx) { + assertValue(row, col, view, viewIdx); + + parent.set(row, col, parent.get(row, col) + 1); + + assertValue(row, col, view, viewIdx); + + view.set(viewIdx, view.get(viewIdx) + 2); + + assertValue(row, col, view, viewIdx); + } + + /** */ + private void assertValue(int row, int col, Vector view, int viewIdx) { + assertEquals(UNEXPECTED_VALUE + " at row " + row + " col " + col, parent.get(row, col), view.get(viewIdx), 0d); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorViewConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorViewConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorViewConstructorTest.java new file mode 100644 index 0000000..72be324 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorViewConstructorTest.java @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.ml.math.impls.vector; + +import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.exceptions.CardinalityException; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +/** */ +public class PivotedVectorViewConstructorTest { + /** */ + private static final int IMPOSSIBLE_SIZE = -1; + + /** */ + private static final SampleParams sampleParams = new SampleParams(); + + /** */ + @Test(expected = NullPointerException.class) + public void nullVecParamTest() { + assertEquals("Expect exception due to null vector param.", IMPOSSIBLE_SIZE, + new PivotedVectorView(null, sampleParams.pivot).size()); + } + + /** */ + @Test(expected = NullPointerException.class) + public void nullVecParam2Test() { + assertEquals("Expect exception due to null vector param, with unpivot.", IMPOSSIBLE_SIZE, + new PivotedVectorView(null, sampleParams.pivot, sampleParams.unpivot).size()); + } + + /** */ + @Test(expected = NullPointerException.class) + public void nullPivotParamTest() { + assertEquals("Expect exception due to null pivot param.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, null).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void nullPivotParam2Test() { + assertEquals("Expect exception due to null pivot param, with unpivot.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, null, sampleParams.unpivot).size()); + } + + /** */ + @Test(expected = AssertionError.class) + public void nullUnpivotParam2Test() { + assertEquals("Expect exception due to null unpivot param.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, sampleParams.pivot, null).size()); + } + + /** */ + @Test(expected = CardinalityException.class) + public void emptyPivotTest() { + assertEquals("Expect exception due to empty pivot param.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, new int[] {}).size()); + } + + /** */ + @Test(expected = CardinalityException.class) + public void emptyPivot2Test() { + assertEquals("Expect exception due to empty pivot param, with unpivot.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, new int[] {}, sampleParams.unpivot).size()); + } + + /** */ + @Test(expected = CardinalityException.class) + public void wrongPivotTest() { + assertEquals("Expect exception due to wrong pivot param.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, new int[] {0}).size()); + } + + /** */ + @Test(expected = CardinalityException.class) + public void wrongPivot2Test() { + assertEquals("Expect exception due to wrong pivot param, with unpivot.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, new int[] {0}, sampleParams.unpivot).size()); + } + + /** */ + @Test(expected = CardinalityException.class) + public void emptyUnpivotTest() { + assertEquals("Expect exception due to empty unpivot param.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, sampleParams.pivot, new int[] {}).size()); + } + + /** */ + @Test(expected = CardinalityException.class) + public void wrongUnpivotTest() { + assertEquals("Expect exception due to wrong unpivot param, with unpivot.", IMPOSSIBLE_SIZE, + new PivotedVectorView(sampleParams.vec, sampleParams.pivot, new int[] {0}).size()); + } + + /** */ + @Test + public void basicPivotTest() { + final PivotedVectorView pvv = new PivotedVectorView(sampleParams.vec, sampleParams.pivot); + + final int size = sampleParams.vec.size(); + + assertEquals("View size differs from expected.", size, pvv.size()); + + assertSame("Base vector differs from expected.", sampleParams.vec, pvv.getBaseVector()); + + for (int idx = 0; idx < size; idx++) { + assertEquals("Sample pivot and unpivot differ from expected", + idx, sampleParams.unpivot[sampleParams.pivot[idx]]); + + assertEquals("Pivot differs from expected at index " + idx, + sampleParams.pivot[idx], pvv.pivot(idx)); + + assertEquals("Default unpivot differs from expected at index " + idx, + sampleParams.unpivot[idx], pvv.unpivot(idx)); + + final Metric metric = new Metric(sampleParams.vec.get(idx), pvv.get(pvv.pivot(idx))); + + assertTrue("Not close enough at index " + idx + ", " + metric, metric.closeEnough()); + } + + for (int idx = 0; idx < size; idx++) { + sampleParams.vec.set(idx, sampleParams.vec.get(idx) + idx + 1); + + final Metric metric = new Metric(sampleParams.vec.get(idx), pvv.get(pvv.pivot(idx))); + + assertTrue("Modified value not close enough at index " + idx + ", " + metric, metric.closeEnough()); + } + } + + /** */ + @Test + public void basicUnpivotTest() { + final PivotedVectorView pvv = new PivotedVectorView(sampleParams.vec, sampleParams.pivot, sampleParams.unpivot); + + final int size = sampleParams.vec.size(); + + assertEquals("View size differs from expected.", size, pvv.size()); + + for (int idx = 0; idx < size; idx++) { + assertEquals("Unpivot differs from expected at index " + idx, + sampleParams.unpivot[idx], pvv.unpivot(idx)); + + final Metric metric = new Metric(sampleParams.vec.get(idx), pvv.get(pvv.unpivot(idx))); + + assertTrue("Not close enough at index " + idx + ", " + metric, metric.closeEnough()); + } + } + + /** */ + private static class SampleParams { + /** */ + final double[] data = new double[] {0, 1}; + /** */ + final Vector vec = new DenseLocalOnHeapVector(data); + /** */ + final int[] pivot = new int[] {1, 0}; + /** */ + final int[] unpivot = new int[] {1, 0}; + } + + /** */ + private static class Metric { // todo consider if softer tolerance (like say 0.1 or 0.01) would make sense here + /** */ + private final double exp; + + /** */ + private final double obtained; + + /** **/ + Metric(double exp, double obtained) { + this.exp = exp; + this.obtained = obtained; + } + + /** */ + boolean closeEnough() { + return new Double(exp).equals(obtained) || closeEnoughToZero(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "Metric{" + "expected=" + exp + + ", obtained=" + obtained + + '}'; + } + + /** */ + private boolean closeEnoughToZero() { + return (new Double(exp).equals(0.0) && new Double(obtained).equals(-0.0)) + || (new Double(exp).equals(-0.0) && new Double(obtained).equals(0.0)); + } + } +}