Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 89145200C6B for ; Mon, 17 Apr 2017 18:01:48 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 879FF160BAB; Mon, 17 Apr 2017 16:01:48 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id F171B160BB2 for ; Mon, 17 Apr 2017 18:01:45 +0200 (CEST) Received: (qmail 64436 invoked by uid 500); 17 Apr 2017 16:01:45 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 64105 invoked by uid 99); 17 Apr 2017 16:01:43 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 17 Apr 2017 16:01:43 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 45623DFC4A; Mon, 17 Apr 2017 16:01:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: av@apache.org To: commits@ignite.apache.org Date: Mon, 17 Apr 2017 16:01:45 -0000 Message-Id: <8a351047e5984496b4ba93518a343e22@git.apache.org> In-Reply-To: <7bc1acfb896b40699a7b469ab9cc87c4@git.apache.org> References: <7bc1acfb896b40699a7b469ab9cc87c4@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [03/26] ignite git commit: IGNITE-5000 Rename Ignite Math module to Ignite ML module archived-at: Mon, 17 Apr 2017 16:01:48 -0000 http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixBaseStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixBaseStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixBaseStorageTest.java new file mode 100644 index 0000000..8df19e4 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixBaseStorageTest.java @@ -0,0 +1,89 @@ +/* + * 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.math.impls.storage.matrix; + +import org.apache.ignite.math.ExternalizeTest; +import org.apache.ignite.math.MatrixStorage; +import org.apache.ignite.math.impls.MathTestConstants; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Abstract class with base tests for each matrix storage. + */ +public abstract class MatrixBaseStorageTest extends ExternalizeTest { + /** */ + protected T storage; + + /** */ + @Before + public abstract void setUp(); + + /** */ + @After + public void tearDown() throws Exception { + storage.destroy(); + } + + /** */ + @Test + public void getSet() throws Exception { + int rows = MathTestConstants.STORAGE_SIZE; + int cols = MathTestConstants.STORAGE_SIZE; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + double data = Math.random(); + + storage.set(i, j, data); + + Assert.assertEquals(MathTestConstants.VAL_NOT_EQUALS, storage.get(i, j), data, MathTestConstants.NIL_DELTA); + } + } + } + + /** */ + @Test + public void columnSize() throws Exception { + assertEquals(MathTestConstants.VAL_NOT_EQUALS, storage.columnSize(), MathTestConstants.STORAGE_SIZE); + } + + /** */ + @Test + public void rowSize() throws Exception { + assertEquals(MathTestConstants.VAL_NOT_EQUALS, storage.rowSize(), MathTestConstants.STORAGE_SIZE); + } + + /** */ + @Override public void externalizeTest() { + fillMatrix(); + super.externalizeTest(storage); + } + + /** */ + protected void fillMatrix() { + for (int i = 0; i < storage.rowSize(); i++) { + for (int j = 0; j < storage.columnSize(); j++) + storage.set(i, j, Math.random()); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixOffHeapStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixOffHeapStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixOffHeapStorageTest.java new file mode 100644 index 0000000..a3b21bb --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixOffHeapStorageTest.java @@ -0,0 +1,39 @@ +/* + * 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.math.impls.storage.matrix; + +import org.apache.ignite.math.impls.MathTestConstants; +import org.junit.Test; + +import static org.junit.Assert.assertNull; + +/** + * Unit tests for {@link DenseOffHeapMatrixStorage}. + */ +public class MatrixOffHeapStorageTest extends MatrixBaseStorageTest { + /** {@inheritDoc} */ + @Override public void setUp() { + storage = new DenseOffHeapMatrixStorage(MathTestConstants.STORAGE_SIZE, MathTestConstants.STORAGE_SIZE); + } + + /** */ + @Test + public void data() throws Exception { + assertNull(MathTestConstants.UNEXPECTED_VAL, storage.data()); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixStorageFixtures.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixStorageFixtures.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixStorageFixtures.java new file mode 100644 index 0000000..6353c38 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixStorageFixtures.java @@ -0,0 +1,141 @@ +/* + * 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.math.impls.storage.matrix; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.function.BiConsumer; +import java.util.function.Supplier; +import org.apache.ignite.math.MatrixStorage; +import org.jetbrains.annotations.NotNull; + +import static org.apache.ignite.math.StorageConstants.COLUMN_STORAGE_MODE; +import static org.apache.ignite.math.StorageConstants.RANDOM_ACCESS_MODE; +import static org.apache.ignite.math.StorageConstants.ROW_STORAGE_MODE; +import static org.apache.ignite.math.StorageConstants.SEQUENTIAL_ACCESS_MODE; + +/** + * + */ +class MatrixStorageFixtures { + /** */ + private static final List>> suppliers = Collections.singletonList( + (Supplier>) SparseLocalMatrixStorageFixture::new + ); + + /** */ + void consumeSampleStorages(BiConsumer paramsConsumer, + BiConsumer consumer) { + for (Supplier> fixtureSupplier : suppliers) { + final Iterable fixture = fixtureSupplier.get(); + + for (MatrixStorage matrixStorage : fixture) { + if (paramsConsumer != null) + paramsConsumer.accept(matrixStorage.rowSize(), matrixStorage.columnSize()); + + consumer.accept(matrixStorage, fixture.toString()); + } + } + } + + /** */ + private static class SparseLocalMatrixStorageFixture implements Iterable { + /** */ + private final Integer[] rows = new Integer[] {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 512, 1024, null}; + /** */ + private final Integer[] cols = new Integer[] {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 1024, 512, null}; + /** */ + private final Integer[] randomAccess = new Integer[] {SEQUENTIAL_ACCESS_MODE, RANDOM_ACCESS_MODE, null}; + /** */ + private final Integer[] rowStorage = new Integer[] {ROW_STORAGE_MODE, COLUMN_STORAGE_MODE, null}; + /** */ + private int sizeIdx = 0; + /** */ + private int acsModeIdx = 0; + /** */ + private int stoModeIdx = 0; + + /** {@inheritDoc} */ + @NotNull + @Override public Iterator iterator() { + return new Iterator() { + /** {@inheritDoc} */ + @Override public boolean hasNext() { + return hasNextCol(sizeIdx) && hasNextRow(sizeIdx) + && hasNextAcsMode(acsModeIdx) && hasNextStoMode(stoModeIdx); + } + + /** {@inheritDoc} */ + @Override public MatrixStorage next() { + if (!hasNext()) + throw new NoSuchElementException(SparseLocalMatrixStorageFixture.this.toString()); + + MatrixStorage storage = new SparseLocalOnHeapMatrixStorage( + rows[sizeIdx], cols[sizeIdx], randomAccess[acsModeIdx], rowStorage[stoModeIdx]); + + nextIdx(); + + return storage; + } + + private void nextIdx() { + if (hasNextStoMode(stoModeIdx + 1)) { + stoModeIdx++; + + return; + } + + stoModeIdx = 0; + + if (hasNextAcsMode(acsModeIdx + 1)) { + acsModeIdx++; + + return; + } + + acsModeIdx = 0; + sizeIdx++; + } + }; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "SparseLocalMatrixStorageFixture{ " + "rows=" + rows[sizeIdx] + ", cols=" + cols[sizeIdx] + + ", access mode=" + randomAccess[acsModeIdx] + ", storage mode=" + rowStorage[stoModeIdx] + "}"; + } + + /** */ private boolean hasNextRow(int idx) { + return rows[idx] != null; + } + + /** */ private boolean hasNextCol(int idx) { + return cols[idx] != null; + } + + /** */ private boolean hasNextAcsMode(int idx) { + return randomAccess[idx] != null; + } + + /** */ private boolean hasNextStoMode(int idx) { + return rowStorage[idx] != null; + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixStorageImplementationTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixStorageImplementationTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixStorageImplementationTest.java new file mode 100644 index 0000000..6ec09bd --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/MatrixStorageImplementationTest.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.math.impls.storage.matrix; + +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BiConsumer; +import org.apache.ignite.math.ExternalizeTest; +import org.apache.ignite.math.MatrixStorage; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * Unit tests for {@link MatrixStorage} implementations. + * + * TODO: add attribute tests. + */ +public class MatrixStorageImplementationTest extends ExternalizeTest { + /** + * The columnSize() and the rowSize() test. + */ + @Test + public void sizeTest() { + final AtomicReference expRowSize = new AtomicReference<>(0); + final AtomicReference expColSize = new AtomicReference<>(0); + + consumeSampleStorages((x, y) -> { + expRowSize.set(x); + expColSize.set(y); + }, + (ms, desc) -> assertTrue("Expected size for " + desc, expColSize.get().equals(ms.columnSize()) && expRowSize.get().equals(ms.rowSize()))); + } + + /** */ + @Test + public void getSetTest() { + consumeSampleStorages(null, (ms, desc) -> { + for (int i = 0; i < ms.rowSize(); i++) { + for (int j = 0; j < ms.columnSize(); j++) { + double random = Math.random(); + ms.set(i, j, random); + assertTrue("Unexpected value for " + desc + " x:" + i + ", y:" + j, Double.compare(random, ms.get(i, j)) == 0); + } + } + }); + } + + /** */ + @Override public void externalizeTest() { + consumeSampleStorages(null, (ms, desc) -> externalizeTest(ms)); + } + + /** */ + private void consumeSampleStorages(BiConsumer paramsConsumer, + BiConsumer consumer) { + new MatrixStorageFixtures().consumeSampleStorages(paramsConsumer, consumer); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java new file mode 100644 index 0000000..66fe9b4 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.storage.matrix; + +import org.apache.ignite.Ignite; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.math.StorageConstants; +import org.apache.ignite.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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/RandomAccessSparseVectorStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/RandomAccessSparseVectorStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/RandomAccessSparseVectorStorageTest.java new file mode 100644 index 0000000..8a223fc --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.storage.vector; + +import org.apache.ignite.math.StorageConstants; +import org.apache.ignite.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 { + /** */ + @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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/SparseLocalOffHeapVectorStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/SparseLocalOffHeapVectorStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/SparseLocalOffHeapVectorStorageTest.java new file mode 100644 index 0000000..a7751d8 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.storage.vector; + +import org.apache.ignite.math.ExternalizeTest; +import org.apache.ignite.math.exceptions.UnsupportedOperationException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.apache.ignite.math.impls.MathTestConstants.STORAGE_SIZE; +import static org.apache.ignite.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 { + /** */ 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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorArrayStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorArrayStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorArrayStorageTest.java new file mode 100644 index 0000000..2afc97b --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.storage.vector; + +import java.util.Arrays; +import org.apache.ignite.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 { + /** */ + @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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorBaseStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorBaseStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorBaseStorageTest.java new file mode 100644 index 0000000..988f8c3 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.storage.vector; + +import org.apache.ignite.math.ExternalizeTest; +import org.apache.ignite.math.VectorStorage; +import org.apache.ignite.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 extends ExternalizeTest { + /** */ + 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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorOffheapStorageTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorOffheapStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/storage/vector/VectorOffheapStorageTest.java new file mode 100644 index 0000000..2645926 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.storage.vector; + +import org.apache.ignite.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 { + /** */ + @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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/AbstractVectorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/AbstractVectorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/AbstractVectorTest.java new file mode 100644 index 0000000..c7927a5 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.vector; + +import java.util.Arrays; +import java.util.stream.StreamSupport; +import org.apache.ignite.math.Matrix; +import org.apache.ignite.math.Vector; +import org.apache.ignite.math.VectorStorage; +import org.apache.ignite.math.exceptions.IndexException; +import org.apache.ignite.math.functions.Functions; +import org.apache.ignite.math.impls.MathTestConstants; +import org.apache.ignite.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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/CacheVectorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/CacheVectorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/CacheVectorTest.java new file mode 100644 index 0000000..f276820 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/CacheVectorTest.java @@ -0,0 +1,417 @@ +package org.apache.ignite.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.math.IdentityValueMapper; +import org.apache.ignite.math.Vector; +import org.apache.ignite.math.VectorKeyMapper; +import org.apache.ignite.math.exceptions.UnsupportedOperationException; +import org.apache.ignite.math.functions.Functions; +import org.apache.ignite.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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 getCache() { + assert ignite != null; + + CacheConfiguration cfg = new CacheConfiguration(); + cfg.setName(CACHE_NAME); + + IgniteCache cache = ignite.getOrCreateCache(CACHE_NAME); + + assert cache != null; + return cache; + } + + /** */ private static class TestKeyMapper implements VectorKeyMapper { + /** {@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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/ConstantVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/ConstantVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/ConstantVectorConstructorTest.java new file mode 100644 index 0000000..2ed42bc --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DelegatingVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DelegatingVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DelegatingVectorConstructorTest.java new file mode 100644 index 0000000..82e4e0b --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.vector; + +import org.apache.ignite.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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DenseLocalOffHeapVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DenseLocalOffHeapVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DenseLocalOffHeapVectorConstructorTest.java new file mode 100644 index 0000000..d62f35f --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DenseLocalOnHeapVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DenseLocalOnHeapVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/DenseLocalOnHeapVectorConstructorTest.java new file mode 100644 index 0000000..c10778e --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.vector; + +import java.util.HashMap; +import java.util.Map; +import org.apache.ignite.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 = org.apache.ignite.math.exceptions.UnsupportedOperationException.class) + public void mapInvalidArgsTest() { + assertEquals("Expect exception due to invalid args.", IMPOSSIBLE_SIZE, + new DenseLocalOnHeapVector(new HashMap() {{ + put("invalid", 99); + }}).size()); + } + + /** */ + @Test(expected = UnsupportedOperationException.class) + public void mapMissingArgsTest() { + final Map test = new HashMap() {{ + 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 test = new HashMap() {{ + 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 test = new HashMap() {{ + 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)null).size()); + } + + /** */ + @Test + public void mapTest() { + assertEquals("Size from args.", 99, + new DenseLocalOnHeapVector(new HashMap() {{ + put("size", 99); + }}).size()); + + final double[] test = new double[99]; + + assertEquals("Size from array in args.", test.length, + new DenseLocalOnHeapVector(new HashMap() {{ + put("arr", test); + put("copy", false); + }}).size()); + + assertEquals("Size from array in args, shallow copy.", test.length, + new DenseLocalOnHeapVector(new HashMap() {{ + 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/732dfea9/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/FunctionVectorConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/FunctionVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/vector/FunctionVectorConstructorTest.java new file mode 100644 index 0000000..57c96d1 --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/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.math.impls.vector; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.IntToDoubleFunction; +import org.apache.ignite.math.exceptions.UnsupportedOperationException; +import org.apache.ignite.math.functions.IgniteFunction; +import org.apache.ignite.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 = org.apache.ignite.math.exceptions.UnsupportedOperationException.class) + public void mapInvalidArgsTest() { + assertEquals("Expect exception due to invalid args.", IMPOSSIBLE_SIZE, + new FunctionVector(new HashMap() {{ + put("invalid", 99); + }}).size()); + } + + /** */ + @Test(expected = UnsupportedOperationException.class) + public void mapMissingArgsTest() { + final Map test = new HashMap() {{ + 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 test = new HashMap() {{ + 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() {{ + put("size", 99); + + put("getFunc", (IgniteFunction)i -> (double)i); + }}).size()); + + assertEquals("Size from args with setFunc.", 99, + new FunctionVector(new HashMap() {{ + put("size", 99); + + put("getFunc", (IgniteFunction)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()); + } +}