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.LongDenseVector;
22importstatic org.junit.Assert.assertEquals;
2324import org.apache.giraph.utils.WritableUtils;
25import org.junit.Test;
2627publicclassTestLongDenseMatrix {
2829 @Test
30publicvoid testVectorSingleton() {
31 LongDenseVector vec1 = new LongDenseVector(10);
32 vec1.set(0, 1);
33 vec1.set(6, 14);
3435 LongDenseVector vec2 = new LongDenseVector();
36 vec2.setSingleton(6, 10);
37 vec1.add(vec2);
38 assertEquals(24, vec1.get(6));
3940 vec2.setSingleton(15, 15);
41 vec1.add(vec2);
42 assertEquals(15, vec1.get(15));
43 }
4445 @Test
46publicvoid testVectorAdd() {
47// The default value should be 048 LongDenseVector vec1 = new LongDenseVector(10);
49 assertEquals(0, vec1.get(0));
5051// Basic get/set52 vec1.set(0, 1);
53 vec1.set(6, 14);
54 assertEquals(1, vec1.get(0));
55 assertEquals(0, vec1.get(4));
56 assertEquals(14, vec1.get(6));
57 assertEquals(0, vec1.get(15));
5859// Add another vector60 LongDenseVector vec2 = new LongDenseVector(20);
61 vec2.set(0, 5);
62 vec2.set(5, 17);
6364 vec1.add(vec2);
65 assertEquals(6, vec1.get(0));
66 assertEquals(17, vec1.get(5));
67 assertEquals(14, vec1.get(6));
68 assertEquals(0, vec1.get(15));
69 }
7071 @Test
72publicvoid testVectorSerialize() throws Exception {
73int size = 100;
7475// Serialize from76 LongDenseVector from = new LongDenseVector(size);
77 from.set(0, 100);
78 from.set(10, 50);
79 from.set(12, 10);
80 byte[] data = WritableUtils.writeToByteArray(from, from);
8182// De-serialize to83 LongDenseVector to1 = new LongDenseVector();
84 LongDenseVector to2 = new LongDenseVector();
85 WritableUtils.readFieldsFromByteArray(data, to1, to2);
8687// The vectors should be equal88for (int i = 0; i < size; ++i) {
89 assertEquals(from.get(i), to1.get(i));
90 assertEquals(from.get(i), to2.get(i));
91 }
92 }
9394 @Test
95publicvoid testVectorSerializeSingleton() throws Exception {
96 LongDenseVector from = new LongDenseVector();
97 from.setSingleton(3, 10);
9899 byte[] data = WritableUtils.writeToByteArray(from, from);
100101 LongDenseVector to1 = new LongDenseVector();
102 LongDenseVector to2 = new LongDenseVector();
103 WritableUtils.readFieldsFromByteArray(data, to1, to2);
104105 assertEquals(from.getSingletonIndex(), to1.getSingletonIndex());
106 assertEquals(from.getSingletonIndex(), to2.getSingletonIndex());
107 assertEquals(from.getSingletonValue(), to2.getSingletonValue());
108 assertEquals(from.getSingletonValue(), to2.getSingletonValue());
109 }
110 }