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 */18package org.apache.giraph.graph;
1920import org.apache.giraph.conf.GiraphConfiguration;
21import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
22import org.apache.giraph.edge.ArrayListEdges;
23import org.apache.giraph.edge.OutEdges;
24import org.apache.hadoop.io.DoubleWritable;
25import org.apache.hadoop.io.FloatWritable;
26import org.apache.hadoop.io.LongWritable;
27import org.junit.Test;
2829importstatic org.junit.Assert.*;
3031/**32 * Tests whether ByteValueVertex works -- same test as for DefaultVertex33 * but with different factory method for vertices.34 */35publicclassTestByteValueVertexextendsTestVertexAndEdges {
3637protected Vertex<LongWritable, FloatWritable, DoubleWritable>
38 instantiateVertex(Class<? extends OutEdges> edgesClass) {
39 GiraphConfiguration giraphConfiguration = new GiraphConfiguration();
40 giraphConfiguration.setComputationClass(TestComputation.class);
41 giraphConfiguration.setOutEdgesClass(edgesClass);
42 giraphConfiguration.setVertexClass(ByteValueVertex.class);
4344 ImmutableClassesGiraphConfiguration immutableClassesGiraphConfiguration =
45new ImmutableClassesGiraphConfiguration(giraphConfiguration);
46 Vertex bv = immutableClassesGiraphConfiguration.createVertex();
47 assertTrue(bv instanceof ByteValueVertex);
48return bv;
49 }
5051 @Test
52publicvoid testCachedValue() {
53 ByteValueVertex<LongWritable, FloatWritable, DoubleWritable> byteValueVertex =
54 (ByteValueVertex<LongWritable, FloatWritable, DoubleWritable>)
55 instantiateVertex(ArrayListEdges.class);
5657 FloatWritable origValue = new FloatWritable(492.2f);
58 byteValueVertex.setValue(origValue);
5960// Check value is correct61 assertEquals(492.2f, byteValueVertex.getValue().get(), 0.0f);
6263// Change value and see it is reflected correctly64 FloatWritable gotValue = byteValueVertex.getValue();
65 gotValue.set(33.3f);
66 assertEquals(33.3f, byteValueVertex.getValue().get(), 0.0f);
6768// Change the object and set that the cached value also changes69 FloatWritable newValue = new FloatWritable(99.9f);
70 byteValueVertex.setValue(newValue);
71 assertEquals(99.9f, byteValueVertex.getValue().get(), 0.0f);
7273// Reference should be now newValue74 assertTrue(newValue == byteValueVertex.getValue());
7576// Commit the changes... (called after vertex update)77 byteValueVertex.unwrapMutableEdges();
7879// Now the value reference should be new80 assertFalse(newValue == byteValueVertex.getValue());
8182// But value data should be correct83 assertEquals(99.9f, byteValueVertex.getValue().get(), 0.0f);
84 }
85 }