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.dense;
2021import org.apache.giraph.aggregators.matrix.dense.FloatDenseVector;
22importstatic org.junit.Assert.assertEquals;
2324import org.apache.giraph.utils.WritableUtils;
25import org.junit.Test;
2627publicclassTestFloatDenseMatrix {
28privatestaticdouble E = 0.0001f;
2930 @Test
31publicvoid testVectorSingleton() {
32 FloatDenseVector vec1 = new FloatDenseVector(10);
33 vec1.set(0, 0.1f);
34 vec1.set(6, 1.4f);
3536 FloatDenseVector vec2 = new FloatDenseVector();
37 vec2.setSingleton(6, 1.0f);
38 vec1.add(vec2);
39 assertEquals(2.4, vec1.get(6), E);
4041 vec2.setSingleton(15, 1.5f);
42 vec1.add(vec2);
43 assertEquals(1.5, vec1.get(15), E);
44 }
4546 @Test
47publicvoid testVectorAdd() {
48// The default value should be 049 FloatDenseVector vec1 = new FloatDenseVector(10);
50 assertEquals(0.0, vec1.get(0), E);
5152// Basic get/set53 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);
5960// Add another vector61 FloatDenseVector vec2 = new FloatDenseVector(20);
62 vec2.set(0, 0.5f);
63 vec2.set(5, 1.7f);
6465 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 }
7172 @Test
73publicvoid testVectorSerialize() throws Exception {
74int size = 100;
7576// Serialize from77 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);
8283// De-serialize to84 FloatDenseVector to1 = new FloatDenseVector();
85 FloatDenseVector to2 = new FloatDenseVector();
86 WritableUtils.readFieldsFromByteArray(data, to1, to2);
8788// The vectors should be equal89for (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 }
9495 @Test
96publicvoid testVectorSerializeSingleton() throws Exception {
97 FloatDenseVector from = new FloatDenseVector();
98 from.setSingleton(3, 10.0f);
99100 byte[] data = WritableUtils.writeToByteArray(from, from);
101102 FloatDenseVector to1 = new FloatDenseVector();
103 FloatDenseVector to2 = new FloatDenseVector();
104 WritableUtils.readFieldsFromByteArray(data, to1, to2);
105106 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 }