1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.aggregators.matrix.sparse;
2021importstatic org.junit.Assert.assertEquals;
2223import org.apache.giraph.utils.WritableUtils;
24import org.junit.Test;
2526publicclassTestDoubleSparseMatrix {
27privatestaticdouble E = 0.0001f;
2829 @Test
30publicvoid testVectorAdd() {
31// The default value should be 032 DoubleSparseVector vec1 = new DoubleSparseVector();
33 assertEquals(0.0, vec1.get(0), E);
3435// Basic get/set36 vec1.set(0, 0.1);
37 vec1.set(10, 1.4);
38 assertEquals(0.1, vec1.get(0), E);
39 assertEquals(0.0, vec1.get(5), E);
40 assertEquals(1.4, vec1.get(10), E);
4142// Add another vector43 DoubleSparseVector vec2 = new DoubleSparseVector();
44 vec2.set(0, 0.5);
45 vec2.set(5, 1.7);
4647 vec1.add(vec2);
48 assertEquals(0.6, vec1.get(0), E);
49 assertEquals(1.7, vec1.get(5), E);
50 assertEquals(1.4, vec1.get(10), E);
51 assertEquals(0.0, vec1.get(15), E);
52 }
5354 @Test
55publicvoid testVectorSerialize() throws Exception {
56int size = 100;
5758// Serialize from59 DoubleSparseVector from = new DoubleSparseVector(size);
60 from.set(0, 10.0);
61 from.set(10, 5.0);
62 from.set(12, 1.0);
63 byte[] data = WritableUtils.writeToByteArray(from);
6465// De-serialize to66 DoubleSparseVector to = new DoubleSparseVector();
67 WritableUtils.readFieldsFromByteArray(data, to);
6869// The vectors should be equal70for (int i = 0; i < size; ++i) {
71 assertEquals(from.get(i), to.get(i), E);
72 }
73 }
74 }