This project has retired. For details please refer to its Attic page.
TestFloatDenseMatrix 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.dense;
20  
21  import org.apache.giraph.aggregators.matrix.dense.FloatDenseVector;
22  import static org.junit.Assert.assertEquals;
23  
24  import org.apache.giraph.utils.WritableUtils;
25  import org.junit.Test;
26  
27  public class TestFloatDenseMatrix {
28    private static double E = 0.0001f;
29  
30    @Test
31    public void testVectorSingleton() {
32      FloatDenseVector vec1 = new FloatDenseVector(10);
33      vec1.set(0, 0.1f);
34      vec1.set(6, 1.4f);
35  
36      FloatDenseVector vec2 = new FloatDenseVector();
37      vec2.setSingleton(6, 1.0f);
38      vec1.add(vec2);
39      assertEquals(2.4, vec1.get(6), E);
40  
41      vec2.setSingleton(15, 1.5f);
42      vec1.add(vec2);
43      assertEquals(1.5, vec1.get(15), E);
44    }
45  
46    @Test
47    public void testVectorAdd() {
48      // The default value should be 0
49      FloatDenseVector vec1 = new FloatDenseVector(10);
50      assertEquals(0.0, vec1.get(0), E);
51  
52      // Basic get/set
53      vec1.set(0, 0.1f);
54      vec1.set(6, 1.4f);
55      assertEquals(0.1, vec1.get(0), E);
56      assertEquals(0.0, vec1.get(4), E);
57      assertEquals(1.4, vec1.get(6), E);
58      assertEquals(0.0, vec1.get(15), E);
59  
60      // Add another vector
61      FloatDenseVector vec2 = new FloatDenseVector(20);
62      vec2.set(0, 0.5f);
63      vec2.set(5, 1.7f);
64  
65      vec1.add(vec2);
66      assertEquals(0.6, vec1.get(0), E);
67      assertEquals(1.7, vec1.get(5), E);
68      assertEquals(1.4, vec1.get(6), E);
69      assertEquals(0.0, vec1.get(15), E);
70    }
71  
72    @Test
73    public void testVectorSerialize() throws Exception {
74      int size = 100;
75  
76      // Serialize from
77      FloatDenseVector from = new FloatDenseVector(size);
78      from.set(0, 10.0f);
79      from.set(10, 5.0f);
80      from.set(12, 1.0f);
81      byte[] data = WritableUtils.writeToByteArray(from, from);
82  
83      // De-serialize to
84      FloatDenseVector to1 = new FloatDenseVector();
85      FloatDenseVector to2 = new FloatDenseVector();
86      WritableUtils.readFieldsFromByteArray(data, to1, to2);
87  
88      // The vectors should be equal
89      for (int i = 0; i < size; ++i) {
90        assertEquals(from.get(i), to1.get(i), E);
91        assertEquals(from.get(i), to2.get(i), E);
92      }
93    }
94  
95    @Test
96    public void testVectorSerializeSingleton() throws Exception {
97      FloatDenseVector from = new FloatDenseVector();
98      from.setSingleton(3, 10.0f);
99  
100     byte[] data = WritableUtils.writeToByteArray(from, from);
101 
102     FloatDenseVector to1 = new FloatDenseVector();
103     FloatDenseVector to2 = new FloatDenseVector();
104     WritableUtils.readFieldsFromByteArray(data, to1, to2);
105 
106     assertEquals(from.getSingletonIndex(), to1.getSingletonIndex());
107     assertEquals(from.getSingletonIndex(), to2.getSingletonIndex());
108     assertEquals(from.getSingletonValue(), to2.getSingletonValue(), E);
109     assertEquals(from.getSingletonValue(), to2.getSingletonValue(), E);
110   }
111 }