This project has retired. For details please refer to its Attic page.
TestDoubleSparseMatrix xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.aggregators.matrix.sparse;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import org.apache.giraph.utils.WritableUtils;
24  import org.junit.Test;
25  
26  public class TestDoubleSparseMatrix {
27    private static double E = 0.0001f;
28  
29    @Test
30    public void testVectorAdd() {
31      // The default value should be 0
32      DoubleSparseVector vec1 = new DoubleSparseVector();
33      assertEquals(0.0, vec1.get(0), E);
34  
35      // Basic get/set
36      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);
41  
42      // Add another vector
43      DoubleSparseVector vec2 = new DoubleSparseVector();
44      vec2.set(0, 0.5);
45      vec2.set(5, 1.7);
46  
47      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    }
53  
54    @Test
55    public void testVectorSerialize() throws Exception {
56      int size = 100;
57  
58      // Serialize from
59      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);
64  
65      // De-serialize to
66      DoubleSparseVector to = new DoubleSparseVector();
67      WritableUtils.readFieldsFromByteArray(data, to);
68  
69      // The vectors should be equal
70      for (int i = 0; i < size; ++i) {
71        assertEquals(from.get(i), to.get(i), E);
72      }
73    }
74  }