This project has retired. For details please refer to its Attic page.
TestByteValueVertex 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  package org.apache.giraph.graph;
19  
20  import org.apache.giraph.conf.GiraphConfiguration;
21  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
22  import org.apache.giraph.edge.ArrayListEdges;
23  import org.apache.giraph.edge.OutEdges;
24  import org.apache.hadoop.io.DoubleWritable;
25  import org.apache.hadoop.io.FloatWritable;
26  import org.apache.hadoop.io.LongWritable;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.*;
30  
31  /**
32   * Tests whether ByteValueVertex works -- same test as for DefaultVertex
33   * but with different factory method for vertices.
34   */
35  public class TestByteValueVertex extends TestVertexAndEdges {
36  
37      protected 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);
43  
44        ImmutableClassesGiraphConfiguration immutableClassesGiraphConfiguration =
45                  new ImmutableClassesGiraphConfiguration(giraphConfiguration);
46        Vertex bv = immutableClassesGiraphConfiguration.createVertex();
47        assertTrue(bv instanceof ByteValueVertex);
48        return bv;
49      }
50  
51      @Test
52      public void testCachedValue() {
53        ByteValueVertex<LongWritable, FloatWritable, DoubleWritable> byteValueVertex =
54          (ByteValueVertex<LongWritable, FloatWritable, DoubleWritable>)
55            instantiateVertex(ArrayListEdges.class);
56  
57        FloatWritable origValue = new FloatWritable(492.2f);
58        byteValueVertex.setValue(origValue);
59  
60        // Check value is correct
61        assertEquals(492.2f, byteValueVertex.getValue().get(), 0.0f);
62  
63        // Change value and see it is reflected correctly
64        FloatWritable gotValue = byteValueVertex.getValue();
65        gotValue.set(33.3f);
66        assertEquals(33.3f, byteValueVertex.getValue().get(), 0.0f);
67  
68        // Change the object and set that the cached value also changes
69        FloatWritable newValue = new FloatWritable(99.9f);
70        byteValueVertex.setValue(newValue);
71        assertEquals(99.9f, byteValueVertex.getValue().get(), 0.0f);
72  
73        // Reference should be now newValue
74        assertTrue(newValue == byteValueVertex.getValue());
75  
76        // Commit the changes... (called after vertex update)
77        byteValueVertex.unwrapMutableEdges();
78  
79        // Now the value reference should be new
80        assertFalse(newValue == byteValueVertex.getValue());
81  
82        // But value data should be correct
83        assertEquals(99.9f, byteValueVertex.getValue().get(), 0.0f);
84      }
85  }