Updated Branches: refs/heads/trunk 96968fdca -> af21be3b7
http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/main/java/org/apache/giraph/aggregators/matrix/sparse/package-info.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/main/java/org/apache/giraph/aggregators/matrix/sparse/package-info.java b/giraph-core/src/main/java/org/apache/giraph/aggregators/matrix/sparse/package-info.java new file mode 100644 index 0000000..dfba3de --- /dev/null +++ b/giraph-core/src/main/java/org/apache/giraph/aggregators/matrix/sparse/package-info.java @@ -0,0 +1,21 @@ +/* + * 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 of sparce matrix aggregator. + */ +package org.apache.giraph.aggregators.matrix.sparse; http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestDoubleMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestDoubleMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestDoubleMatrix.java deleted file mode 100644 index d67eda1..0000000 --- a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestDoubleMatrix.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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.giraph.aggregators.matrix; - -import static org.junit.Assert.assertEquals; - -import org.apache.giraph.utils.WritableUtils; -import org.junit.Test; - -public class TestDoubleMatrix { - private static double E = 0.0001f; - - @Test - public void testVectorAdd() { - // The default value should be 0 - DoubleVector vec1 = new DoubleVector(); - assertEquals(0.0, vec1.get(0), E); - - // Basic get/set - vec1.set(0, 0.1); - vec1.set(10, 1.4); - assertEquals(0.1, vec1.get(0), E); - assertEquals(0.0, vec1.get(5), E); - assertEquals(1.4, vec1.get(10), E); - - // Add another vector - DoubleVector vec2 = new DoubleVector(); - vec2.set(0, 0.5); - vec2.set(5, 1.7); - - vec1.add(vec2); - assertEquals(0.6, vec1.get(0), E); - assertEquals(1.7, vec1.get(5), E); - assertEquals(1.4, vec1.get(10), E); - assertEquals(0.0, vec1.get(15), E); - } - - @Test - public void testVectorSerialize() throws Exception { - int size = 100; - - // Serialize from - DoubleVector from = new DoubleVector(size); - from.set(0, 10.0); - from.set(10, 5.0); - from.set(12, 1.0); - byte[] data = WritableUtils.writeToByteArray(from); - - // De-serialize to - DoubleVector to = new DoubleVector(); - WritableUtils.readFieldsFromByteArray(data, to); - - // The vectors should be equal - for (int i = 0; i < size; ++i) { - assertEquals(from.get(i), to.get(i), E); - } - } -} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestFloatMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestFloatMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestFloatMatrix.java deleted file mode 100644 index d0f9bb0..0000000 --- a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestFloatMatrix.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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.giraph.aggregators.matrix; - -import static org.junit.Assert.assertEquals; - -import org.apache.giraph.utils.WritableUtils; -import org.junit.Test; - -public class TestFloatMatrix { - private static float E = 0.0001f; - - @Test - public void testVectorAdd() { - // The default value should be 0 - FloatVector vec1 = new FloatVector(); - assertEquals(0.0, vec1.get(0), E); - - // Basic get/set - vec1.set(0, 0.1f); - vec1.set(10, 1.4f); - assertEquals(0.1, vec1.get(0), E); - assertEquals(0.0, vec1.get(5), E); - assertEquals(1.4, vec1.get(10), E); - - // Add another vector - FloatVector vec2 = new FloatVector(); - vec2.set(0, 0.5f); - vec2.set(5, 1.7f); - - vec1.add(vec2); - assertEquals(0.6, vec1.get(0), E); - assertEquals(1.7, vec1.get(5), E); - assertEquals(1.4, vec1.get(10), E); - assertEquals(0.0, vec1.get(15), E); - } - - @Test - public void testVectorSerialize() throws Exception { - int size = 100; - - // Serialize from - FloatVector from = new FloatVector(size); - from.set(0, 10.0f); - from.set(10, 5.0f); - from.set(12, 1.0f); - byte[] data = WritableUtils.writeToByteArray(from); - - // De-serialize to - FloatVector to = new FloatVector(); - WritableUtils.readFieldsFromByteArray(data, to); - - // The vectors should be equal - for (int i = 0; i < size; ++i) { - assertEquals(from.get(i), to.get(i), E); - } - } -} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestIntMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestIntMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestIntMatrix.java deleted file mode 100644 index e8d3561..0000000 --- a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestIntMatrix.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.giraph.aggregators.matrix; - -import static org.junit.Assert.assertEquals; - -import org.apache.giraph.utils.WritableUtils; -import org.junit.Test; - -public class TestIntMatrix { - - @Test - public void testVectorAdd() { - // The default value should be 0 - IntVector vec1 = new IntVector(); - assertEquals(0, vec1.get(0)); - - // Basic get/set - vec1.set(0, 1); - vec1.set(10, 14); - assertEquals(1, vec1.get(0)); - assertEquals(0, vec1.get(5)); - assertEquals(14, vec1.get(10)); - - // Add another vector - IntVector vec2 = new IntVector(); - vec2.set(0, 5); - vec2.set(5, 17); - - vec1.add(vec2); - assertEquals(6, vec1.get(0)); - assertEquals(17, vec1.get(5)); - assertEquals(14, vec1.get(10)); - assertEquals(0, vec1.get(15)); - } - - @Test - public void testVectorSerialize() throws Exception { - int size = 100; - - // Serialize from - IntVector from = new IntVector(size); - from.set(0, 10); - from.set(10, 5); - from.set(12, 1); - byte[] data = WritableUtils.writeToByteArray(from); - - // De-serialize to - IntVector to = new IntVector(); - WritableUtils.readFieldsFromByteArray(data, to); - - // The vectors should be equal - for (int i = 0; i < size; ++i) { - assertEquals(from.get(i), to.get(i)); - } - } -} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestLongMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestLongMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestLongMatrix.java deleted file mode 100644 index a0a7000..0000000 --- a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/TestLongMatrix.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.giraph.aggregators.matrix; - -import static org.junit.Assert.assertEquals; - -import org.apache.giraph.utils.WritableUtils; -import org.junit.Test; - -public class TestLongMatrix { - - @Test - public void testVectorAdd() { - // The default value should be 0 - LongVector vec1 = new LongVector(); - assertEquals(0, vec1.get(0)); - - // Basic get/set - vec1.set(0, 1); - vec1.set(10, 14); - assertEquals(1, vec1.get(0)); - assertEquals(0, vec1.get(5)); - assertEquals(14, vec1.get(10)); - - // Add another vector - LongVector vec2 = new LongVector(); - vec2.set(0, 5); - vec2.set(5, 17); - - vec1.add(vec2); - assertEquals(6, vec1.get(0)); - assertEquals(17, vec1.get(5)); - assertEquals(14, vec1.get(10)); - assertEquals(0, vec1.get(15)); - } - - @Test - public void testVectorSerialize() throws Exception { - int size = 100; - - // Serialize from - LongVector from = new LongVector(size); - from.set(0, 10); - from.set(10, 5); - from.set(12, 1); - byte[] data = WritableUtils.writeToByteArray(from); - - // De-serialize to - LongVector to = new LongVector(); - WritableUtils.readFieldsFromByteArray(data, to); - - // The vectors should be equal - for (int i = 0; i < size; ++i) { - assertEquals(from.get(i), to.get(i)); - } - } -} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestDoubleDenseMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestDoubleDenseMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestDoubleDenseMatrix.java new file mode 100644 index 0000000..289390c --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestDoubleDenseMatrix.java @@ -0,0 +1,111 @@ +/* + * 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.giraph.aggregators.matrix.dense; + +import org.apache.giraph.aggregators.matrix.dense.DoubleDenseVector; +import static org.junit.Assert.assertEquals; + +import org.apache.giraph.utils.WritableUtils; +import org.junit.Test; + +public class TestDoubleDenseMatrix { + private static double E = 0.0001f; + + @Test + public void testVectorSingleton() { + DoubleDenseVector vec1 = new DoubleDenseVector(10); + vec1.set(0, 0.1); + vec1.set(6, 1.4); + + DoubleDenseVector vec2 = new DoubleDenseVector(); + vec2.setSingleton(6, 1.0); + vec1.add(vec2); + assertEquals(2.4, vec1.get(6), E); + + vec2.setSingleton(15, 1.5); + vec1.add(vec2); + assertEquals(1.5, vec1.get(15), E); + } + + @Test + public void testVectorAdd() { + // The default value should be 0 + DoubleDenseVector vec1 = new DoubleDenseVector(10); + assertEquals(0.0, vec1.get(0), E); + + // Basic get/set + vec1.set(0, 0.1); + vec1.set(6, 1.4); + assertEquals(0.1, vec1.get(0), E); + assertEquals(0.0, vec1.get(4), E); + assertEquals(1.4, vec1.get(6), E); + assertEquals(0.0, vec1.get(15), E); + + // Add another vector + DoubleDenseVector vec2 = new DoubleDenseVector(20); + vec2.set(0, 0.5); + vec2.set(5, 1.7); + + vec1.add(vec2); + assertEquals(0.6, vec1.get(0), E); + assertEquals(1.7, vec1.get(5), E); + assertEquals(1.4, vec1.get(6), E); + assertEquals(0.0, vec1.get(15), E); + } + + @Test + public void testVectorSerialize() throws Exception { + int size = 100; + + // Serialize from + DoubleDenseVector from = new DoubleDenseVector(size); + from.set(0, 10.0); + from.set(10, 5.0); + from.set(12, 1.0); + byte[] data = WritableUtils.writeToByteArray(from, from); + + // De-serialize to + DoubleDenseVector to1 = new DoubleDenseVector(); + DoubleDenseVector to2 = new DoubleDenseVector(); + WritableUtils.readFieldsFromByteArray(data, to1, to2); + + // The vectors should be equal + for (int i = 0; i < size; ++i) { + assertEquals(from.get(i), to1.get(i), E); + assertEquals(from.get(i), to2.get(i), E); + } + } + + @Test + public void testVectorSerializeSingleton() throws Exception { + DoubleDenseVector from = new DoubleDenseVector(); + from.setSingleton(3, 10.0); + + byte[] data = WritableUtils.writeToByteArray(from, from); + + DoubleDenseVector to1 = new DoubleDenseVector(); + DoubleDenseVector to2 = new DoubleDenseVector(); + WritableUtils.readFieldsFromByteArray(data, to1, to2); + + assertEquals(from.getSingletonIndex(), to1.getSingletonIndex()); + assertEquals(from.getSingletonIndex(), to2.getSingletonIndex()); + assertEquals(from.getSingletonValue(), to2.getSingletonValue(), E); + assertEquals(from.getSingletonValue(), to2.getSingletonValue(), E); + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestFloatDenseMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestFloatDenseMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestFloatDenseMatrix.java new file mode 100644 index 0000000..170701f --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestFloatDenseMatrix.java @@ -0,0 +1,111 @@ +/* + * 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.giraph.aggregators.matrix.dense; + +import org.apache.giraph.aggregators.matrix.dense.FloatDenseVector; +import static org.junit.Assert.assertEquals; + +import org.apache.giraph.utils.WritableUtils; +import org.junit.Test; + +public class TestFloatDenseMatrix { + private static double E = 0.0001f; + + @Test + public void testVectorSingleton() { + FloatDenseVector vec1 = new FloatDenseVector(10); + vec1.set(0, 0.1f); + vec1.set(6, 1.4f); + + FloatDenseVector vec2 = new FloatDenseVector(); + vec2.setSingleton(6, 1.0f); + vec1.add(vec2); + assertEquals(2.4, vec1.get(6), E); + + vec2.setSingleton(15, 1.5f); + vec1.add(vec2); + assertEquals(1.5, vec1.get(15), E); + } + + @Test + public void testVectorAdd() { + // The default value should be 0 + FloatDenseVector vec1 = new FloatDenseVector(10); + assertEquals(0.0, vec1.get(0), E); + + // Basic get/set + vec1.set(0, 0.1f); + vec1.set(6, 1.4f); + assertEquals(0.1, vec1.get(0), E); + assertEquals(0.0, vec1.get(4), E); + assertEquals(1.4, vec1.get(6), E); + assertEquals(0.0, vec1.get(15), E); + + // Add another vector + FloatDenseVector vec2 = new FloatDenseVector(20); + vec2.set(0, 0.5f); + vec2.set(5, 1.7f); + + vec1.add(vec2); + assertEquals(0.6, vec1.get(0), E); + assertEquals(1.7, vec1.get(5), E); + assertEquals(1.4, vec1.get(6), E); + assertEquals(0.0, vec1.get(15), E); + } + + @Test + public void testVectorSerialize() throws Exception { + int size = 100; + + // Serialize from + FloatDenseVector from = new FloatDenseVector(size); + from.set(0, 10.0f); + from.set(10, 5.0f); + from.set(12, 1.0f); + byte[] data = WritableUtils.writeToByteArray(from, from); + + // De-serialize to + FloatDenseVector to1 = new FloatDenseVector(); + FloatDenseVector to2 = new FloatDenseVector(); + WritableUtils.readFieldsFromByteArray(data, to1, to2); + + // The vectors should be equal + for (int i = 0; i < size; ++i) { + assertEquals(from.get(i), to1.get(i), E); + assertEquals(from.get(i), to2.get(i), E); + } + } + + @Test + public void testVectorSerializeSingleton() throws Exception { + FloatDenseVector from = new FloatDenseVector(); + from.setSingleton(3, 10.0f); + + byte[] data = WritableUtils.writeToByteArray(from, from); + + FloatDenseVector to1 = new FloatDenseVector(); + FloatDenseVector to2 = new FloatDenseVector(); + WritableUtils.readFieldsFromByteArray(data, to1, to2); + + assertEquals(from.getSingletonIndex(), to1.getSingletonIndex()); + assertEquals(from.getSingletonIndex(), to2.getSingletonIndex()); + assertEquals(from.getSingletonValue(), to2.getSingletonValue(), E); + assertEquals(from.getSingletonValue(), to2.getSingletonValue(), E); + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestIntDenseMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestIntDenseMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestIntDenseMatrix.java new file mode 100644 index 0000000..d2fee0f --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestIntDenseMatrix.java @@ -0,0 +1,110 @@ +/* + * 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.giraph.aggregators.matrix.dense; + +import org.apache.giraph.aggregators.matrix.dense.IntDenseVector; +import static org.junit.Assert.assertEquals; + +import org.apache.giraph.utils.WritableUtils; +import org.junit.Test; + +public class TestIntDenseMatrix { + + @Test + public void testVectorSingleton() { + IntDenseVector vec1 = new IntDenseVector(10); + vec1.set(0, 0); + vec1.set(6, 14); + + IntDenseVector vec2 = new IntDenseVector(); + vec2.setSingleton(6, 10); + vec1.add(vec2); + assertEquals(24, vec1.get(6)); + + vec2.setSingleton(15, 15); + vec1.add(vec2); + assertEquals(15, vec1.get(15)); + } + + @Test + public void testVectorAdd() { + // The default value should be 0 + IntDenseVector vec1 = new IntDenseVector(10); + assertEquals(0, vec1.get(0)); + + // Basic get/set + vec1.set(0, 1); + vec1.set(6, 14); + assertEquals(1, vec1.get(0)); + assertEquals(0, vec1.get(4)); + assertEquals(14, vec1.get(6)); + assertEquals(0, vec1.get(15)); + + // Add another vector + IntDenseVector vec2 = new IntDenseVector(20); + vec2.set(0, 5); + vec2.set(5, 17); + + vec1.add(vec2); + assertEquals(6, vec1.get(0)); + assertEquals(17, vec1.get(5)); + assertEquals(14, vec1.get(6)); + assertEquals(0, vec1.get(15)); + } + + @Test + public void testVectorSerialize() throws Exception { + int size = 100; + + // Serialize from + IntDenseVector from = new IntDenseVector(size); + from.set(0, 100); + from.set(10, 50); + from.set(12, 10); + byte[] data = WritableUtils.writeToByteArray(from, from); + + // De-serialize to + IntDenseVector to1 = new IntDenseVector(); + IntDenseVector to2 = new IntDenseVector(); + WritableUtils.readFieldsFromByteArray(data, to1, to2); + + // The vectors should be equal + for (int i = 0; i < size; ++i) { + assertEquals(from.get(i), to1.get(i)); + assertEquals(from.get(i), to2.get(i)); + } + } + + @Test + public void testVectorSerializeSingleton() throws Exception { + IntDenseVector from = new IntDenseVector(); + from.setSingleton(3, 100); + + byte[] data = WritableUtils.writeToByteArray(from, from); + + IntDenseVector to1 = new IntDenseVector(); + IntDenseVector to2 = new IntDenseVector(); + WritableUtils.readFieldsFromByteArray(data, to1, to2); + + assertEquals(from.getSingletonIndex(), to1.getSingletonIndex()); + assertEquals(from.getSingletonIndex(), to2.getSingletonIndex()); + assertEquals(from.getSingletonValue(), to2.getSingletonValue()); + assertEquals(from.getSingletonValue(), to2.getSingletonValue()); + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestLongDenseMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestLongDenseMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestLongDenseMatrix.java new file mode 100644 index 0000000..39e2e6e --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/dense/TestLongDenseMatrix.java @@ -0,0 +1,110 @@ +/* + * 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.giraph.aggregators.matrix.dense; + +import org.apache.giraph.aggregators.matrix.dense.LongDenseVector; +import static org.junit.Assert.assertEquals; + +import org.apache.giraph.utils.WritableUtils; +import org.junit.Test; + +public class TestLongDenseMatrix { + + @Test + public void testVectorSingleton() { + LongDenseVector vec1 = new LongDenseVector(10); + vec1.set(0, 1); + vec1.set(6, 14); + + LongDenseVector vec2 = new LongDenseVector(); + vec2.setSingleton(6, 10); + vec1.add(vec2); + assertEquals(24, vec1.get(6)); + + vec2.setSingleton(15, 15); + vec1.add(vec2); + assertEquals(15, vec1.get(15)); + } + + @Test + public void testVectorAdd() { + // The default value should be 0 + LongDenseVector vec1 = new LongDenseVector(10); + assertEquals(0, vec1.get(0)); + + // Basic get/set + vec1.set(0, 1); + vec1.set(6, 14); + assertEquals(1, vec1.get(0)); + assertEquals(0, vec1.get(4)); + assertEquals(14, vec1.get(6)); + assertEquals(0, vec1.get(15)); + + // Add another vector + LongDenseVector vec2 = new LongDenseVector(20); + vec2.set(0, 5); + vec2.set(5, 17); + + vec1.add(vec2); + assertEquals(6, vec1.get(0)); + assertEquals(17, vec1.get(5)); + assertEquals(14, vec1.get(6)); + assertEquals(0, vec1.get(15)); + } + + @Test + public void testVectorSerialize() throws Exception { + int size = 100; + + // Serialize from + LongDenseVector from = new LongDenseVector(size); + from.set(0, 100); + from.set(10, 50); + from.set(12, 10); + byte[] data = WritableUtils.writeToByteArray(from, from); + + // De-serialize to + LongDenseVector to1 = new LongDenseVector(); + LongDenseVector to2 = new LongDenseVector(); + WritableUtils.readFieldsFromByteArray(data, to1, to2); + + // The vectors should be equal + for (int i = 0; i < size; ++i) { + assertEquals(from.get(i), to1.get(i)); + assertEquals(from.get(i), to2.get(i)); + } + } + + @Test + public void testVectorSerializeSingleton() throws Exception { + LongDenseVector from = new LongDenseVector(); + from.setSingleton(3, 10); + + byte[] data = WritableUtils.writeToByteArray(from, from); + + LongDenseVector to1 = new LongDenseVector(); + LongDenseVector to2 = new LongDenseVector(); + WritableUtils.readFieldsFromByteArray(data, to1, to2); + + assertEquals(from.getSingletonIndex(), to1.getSingletonIndex()); + assertEquals(from.getSingletonIndex(), to2.getSingletonIndex()); + assertEquals(from.getSingletonValue(), to2.getSingletonValue()); + assertEquals(from.getSingletonValue(), to2.getSingletonValue()); + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestDoubleSparseMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestDoubleSparseMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestDoubleSparseMatrix.java new file mode 100644 index 0000000..2f04982 --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestDoubleSparseMatrix.java @@ -0,0 +1,74 @@ +/* + * 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.giraph.aggregators.matrix.sparse; + +import static org.junit.Assert.assertEquals; + +import org.apache.giraph.utils.WritableUtils; +import org.junit.Test; + +public class TestDoubleSparseMatrix { + private static double E = 0.0001f; + + @Test + public void testVectorAdd() { + // The default value should be 0 + DoubleSparseVector vec1 = new DoubleSparseVector(); + assertEquals(0.0, vec1.get(0), E); + + // Basic get/set + vec1.set(0, 0.1); + vec1.set(10, 1.4); + assertEquals(0.1, vec1.get(0), E); + assertEquals(0.0, vec1.get(5), E); + assertEquals(1.4, vec1.get(10), E); + + // Add another vector + DoubleSparseVector vec2 = new DoubleSparseVector(); + vec2.set(0, 0.5); + vec2.set(5, 1.7); + + vec1.add(vec2); + assertEquals(0.6, vec1.get(0), E); + assertEquals(1.7, vec1.get(5), E); + assertEquals(1.4, vec1.get(10), E); + assertEquals(0.0, vec1.get(15), E); + } + + @Test + public void testVectorSerialize() throws Exception { + int size = 100; + + // Serialize from + DoubleSparseVector from = new DoubleSparseVector(size); + from.set(0, 10.0); + from.set(10, 5.0); + from.set(12, 1.0); + byte[] data = WritableUtils.writeToByteArray(from); + + // De-serialize to + DoubleSparseVector to = new DoubleSparseVector(); + WritableUtils.readFieldsFromByteArray(data, to); + + // The vectors should be equal + for (int i = 0; i < size; ++i) { + assertEquals(from.get(i), to.get(i), E); + } + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestFloatSparseMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestFloatSparseMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestFloatSparseMatrix.java new file mode 100644 index 0000000..b0abb86 --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestFloatSparseMatrix.java @@ -0,0 +1,74 @@ +/* + * 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.giraph.aggregators.matrix.sparse; + +import static org.junit.Assert.assertEquals; + +import org.apache.giraph.utils.WritableUtils; +import org.junit.Test; + +public class TestFloatSparseMatrix { + private static float E = 0.0001f; + + @Test + public void testVectorAdd() { + // The default value should be 0 + FloatSparseVector vec1 = new FloatSparseVector(); + assertEquals(0.0, vec1.get(0), E); + + // Basic get/set + vec1.set(0, 0.1f); + vec1.set(10, 1.4f); + assertEquals(0.1, vec1.get(0), E); + assertEquals(0.0, vec1.get(5), E); + assertEquals(1.4, vec1.get(10), E); + + // Add another vector + FloatSparseVector vec2 = new FloatSparseVector(); + vec2.set(0, 0.5f); + vec2.set(5, 1.7f); + + vec1.add(vec2); + assertEquals(0.6, vec1.get(0), E); + assertEquals(1.7, vec1.get(5), E); + assertEquals(1.4, vec1.get(10), E); + assertEquals(0.0, vec1.get(15), E); + } + + @Test + public void testVectorSerialize() throws Exception { + int size = 100; + + // Serialize from + FloatSparseVector from = new FloatSparseVector(size); + from.set(0, 10.0f); + from.set(10, 5.0f); + from.set(12, 1.0f); + byte[] data = WritableUtils.writeToByteArray(from); + + // De-serialize to + FloatSparseVector to = new FloatSparseVector(); + WritableUtils.readFieldsFromByteArray(data, to); + + // The vectors should be equal + for (int i = 0; i < size; ++i) { + assertEquals(from.get(i), to.get(i), E); + } + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestIntSparseMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestIntSparseMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestIntSparseMatrix.java new file mode 100644 index 0000000..79575ce --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestIntSparseMatrix.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.giraph.aggregators.matrix.sparse; + +import static org.junit.Assert.assertEquals; + +import org.apache.giraph.utils.WritableUtils; +import org.junit.Test; + +public class TestIntSparseMatrix { + + @Test + public void testVectorAdd() { + // The default value should be 0 + IntSparseVector vec1 = new IntSparseVector(); + assertEquals(0, vec1.get(0)); + + // Basic get/set + vec1.set(0, 1); + vec1.set(10, 14); + assertEquals(1, vec1.get(0)); + assertEquals(0, vec1.get(5)); + assertEquals(14, vec1.get(10)); + + // Add another vector + IntSparseVector vec2 = new IntSparseVector(); + vec2.set(0, 5); + vec2.set(5, 17); + + vec1.add(vec2); + assertEquals(6, vec1.get(0)); + assertEquals(17, vec1.get(5)); + assertEquals(14, vec1.get(10)); + assertEquals(0, vec1.get(15)); + } + + @Test + public void testVectorSerialize() throws Exception { + int size = 100; + + // Serialize from + IntSparseVector from = new IntSparseVector(size); + from.set(0, 10); + from.set(10, 5); + from.set(12, 1); + byte[] data = WritableUtils.writeToByteArray(from); + + // De-serialize to + IntSparseVector to = new IntSparseVector(); + WritableUtils.readFieldsFromByteArray(data, to); + + // The vectors should be equal + for (int i = 0; i < size; ++i) { + assertEquals(from.get(i), to.get(i)); + } + } +} http://git-wip-us.apache.org/repos/asf/giraph/blob/af21be3b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestLongSparseMatrix.java ---------------------------------------------------------------------- diff --git a/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestLongSparseMatrix.java b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestLongSparseMatrix.java new file mode 100644 index 0000000..45664d1 --- /dev/null +++ b/giraph-core/src/test/java/org/apache/giraph/aggregators/matrix/sparse/TestLongSparseMatrix.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.giraph.aggregators.matrix.sparse; + +import static org.junit.Assert.assertEquals; + +import org.apache.giraph.utils.WritableUtils; +import org.junit.Test; + +public class TestLongSparseMatrix { + + @Test + public void testVectorAdd() { + // The default value should be 0 + LongSparseVector vec1 = new LongSparseVector(); + assertEquals(0, vec1.get(0)); + + // Basic get/set + vec1.set(0, 1); + vec1.set(10, 14); + assertEquals(1, vec1.get(0)); + assertEquals(0, vec1.get(5)); + assertEquals(14, vec1.get(10)); + + // Add another vector + LongSparseVector vec2 = new LongSparseVector(); + vec2.set(0, 5); + vec2.set(5, 17); + + vec1.add(vec2); + assertEquals(6, vec1.get(0)); + assertEquals(17, vec1.get(5)); + assertEquals(14, vec1.get(10)); + assertEquals(0, vec1.get(15)); + } + + @Test + public void testVectorSerialize() throws Exception { + int size = 100; + + // Serialize from + LongSparseVector from = new LongSparseVector(size); + from.set(0, 10); + from.set(10, 5); + from.set(12, 1); + byte[] data = WritableUtils.writeToByteArray(from); + + // De-serialize to + LongSparseVector to = new LongSparseVector(); + WritableUtils.readFieldsFromByteArray(data, to); + + // The vectors should be equal + for (int i = 0; i < size; ++i) { + assertEquals(from.get(i), to.get(i)); + } + } +}
