1 /* 2 * Copyright (C) 2014 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.graph; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.junit.runners.JUnit4; 24 25 /** 26 * Tests for {@link ImmutableGraph} and {@link ImmutableGraph.Builder} that are not ready covered by 27 * {@link StandardImmutableDirectedGraphTest}. 28 */ 29 @RunWith(JUnit4.class) 30 public class StandardImmutableGraphAdditionalTest { 31 32 @Test immutableGraph()33 public void immutableGraph() { 34 MutableGraph<String> mutableGraph = GraphBuilder.directed().build(); 35 mutableGraph.addNode("A"); 36 ImmutableGraph<String> immutableGraph = ImmutableGraph.copyOf(mutableGraph); 37 38 assertThat(immutableGraph).isNotInstanceOf(MutableValueGraph.class); 39 assertThat(immutableGraph).isEqualTo(mutableGraph); 40 41 mutableGraph.addNode("B"); 42 assertThat(immutableGraph).isNotEqualTo(mutableGraph); 43 } 44 45 @Test copyOfImmutableGraph_optimized()46 public void copyOfImmutableGraph_optimized() { 47 Graph<String> graph1 = ImmutableGraph.copyOf(GraphBuilder.directed().<String>build()); 48 Graph<String> graph2 = ImmutableGraph.copyOf(graph1); 49 50 assertThat(graph2).isSameInstanceAs(graph1); 51 } 52 53 @Test immutableGraphBuilder_appliesGraphBuilderConfig()54 public void immutableGraphBuilder_appliesGraphBuilderConfig() { 55 ImmutableGraph<String> emptyGraph = 56 GraphBuilder.directed() 57 .allowsSelfLoops(true) 58 .nodeOrder(ElementOrder.<String>natural()) 59 .immutable() 60 .build(); 61 62 assertThat(emptyGraph.isDirected()).isTrue(); 63 assertThat(emptyGraph.allowsSelfLoops()).isTrue(); 64 assertThat(emptyGraph.nodeOrder()).isEqualTo(ElementOrder.<String>natural()); 65 } 66 67 /** 68 * Tests that the ImmutableGraph.Builder doesn't change when the creating GraphBuilder changes. 69 */ 70 @Test 71 @SuppressWarnings("CheckReturnValue") immutableGraphBuilder_copiesGraphBuilder()72 public void immutableGraphBuilder_copiesGraphBuilder() { 73 GraphBuilder<String> graphBuilder = 74 GraphBuilder.directed() 75 .allowsSelfLoops(true) 76 .<String>nodeOrder(ElementOrder.<String>natural()); 77 ImmutableGraph.Builder<String> immutableGraphBuilder = graphBuilder.immutable(); 78 79 // Update GraphBuilder, but this shouldn't impact immutableGraphBuilder 80 graphBuilder.allowsSelfLoops(false).nodeOrder(ElementOrder.<String>unordered()); 81 82 ImmutableGraph<String> emptyGraph = immutableGraphBuilder.build(); 83 84 assertThat(emptyGraph.isDirected()).isTrue(); 85 assertThat(emptyGraph.allowsSelfLoops()).isTrue(); 86 assertThat(emptyGraph.nodeOrder()).isEqualTo(ElementOrder.<String>natural()); 87 } 88 89 @Test copyOf_incidentEdgeOrder()90 public void copyOf_incidentEdgeOrder() { 91 ImmutableGraph<Object> graph = ImmutableGraph.copyOf(GraphBuilder.undirected().build()); 92 93 assertThat(graph.incidentEdgeOrder()).isEqualTo(ElementOrder.stable()); 94 } 95 96 @Test copyOf_fromUnorderedGraph_incidentEdgeOrder()97 public void copyOf_fromUnorderedGraph_incidentEdgeOrder() { 98 ImmutableGraph<Object> graph = 99 ImmutableGraph.copyOf( 100 GraphBuilder.undirected().incidentEdgeOrder(ElementOrder.unordered()).build()); 101 102 assertThat(graph.incidentEdgeOrder()).isEqualTo(ElementOrder.stable()); 103 } 104 105 @Test immutableGraphBuilder_addNode()106 public void immutableGraphBuilder_addNode() { 107 ImmutableGraph<String> graph = GraphBuilder.directed().<String>immutable().addNode("A").build(); 108 109 assertThat(graph.nodes()).containsExactly("A"); 110 assertThat(graph.edges()).isEmpty(); 111 } 112 113 @Test immutableGraphBuilder_putEdgeFromNodes()114 public void immutableGraphBuilder_putEdgeFromNodes() { 115 ImmutableGraph<String> graph = 116 GraphBuilder.directed().<String>immutable().putEdge("A", "B").build(); 117 118 assertThat(graph.nodes()).containsExactly("A", "B"); 119 assertThat(graph.edges()).containsExactly(EndpointPair.ordered("A", "B")); 120 } 121 122 @Test immutableGraphBuilder_putEdgeFromEndpointPair()123 public void immutableGraphBuilder_putEdgeFromEndpointPair() { 124 ImmutableGraph<String> graph = 125 GraphBuilder.directed().<String>immutable().putEdge(EndpointPair.ordered("A", "B")).build(); 126 127 assertThat(graph.nodes()).containsExactly("A", "B"); 128 assertThat(graph.edges()).containsExactly(EndpointPair.ordered("A", "B")); 129 } 130 } 131