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;
2526publicclassTestLongSparseMatrix {
2728 @Test
29publicvoid testVectorAdd() {
30// The default value should be 031 LongSparseVector vec1 = new LongSparseVector();
32 assertEquals(0, vec1.get(0));
3334// Basic get/set35 vec1.set(0, 1);
36 vec1.set(10, 14);
37 assertEquals(1, vec1.get(0));
38 assertEquals(0, vec1.get(5));
39 assertEquals(14, vec1.get(10));
4041// Add another vector42 LongSparseVector vec2 = new LongSparseVector();
43 vec2.set(0, 5);
44 vec2.set(5, 17);
4546 vec1.add(vec2);
47 assertEquals(6, vec1.get(0));
48 assertEquals(17, vec1.get(5));
49 assertEquals(14, vec1.get(10));
50 assertEquals(0, vec1.get(15));
51 }
5253 @Test
54publicvoid testVectorSerialize() throws Exception {
55int size = 100;
5657// Serialize from58 LongSparseVector from = new LongSparseVector(size);
59 from.set(0, 10);
60 from.set(10, 5);
61 from.set(12, 1);
62 byte[] data = WritableUtils.writeToByteArray(from);
6364// De-serialize to65 LongSparseVector to = new LongSparseVector();
66 WritableUtils.readFieldsFromByteArray(data, to);
6768// The vectors should be equal69for (int i = 0; i < size; ++i) {
70 assertEquals(from.get(i), to.get(i));
71 }
72 }
73 }