• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /** Tests for {@link ImmutableNetwork}. */
26 @RunWith(JUnit4.class)
27 public class ImmutableNetworkTest {
28 
29   @Test
immutableNetwork()30   public void immutableNetwork() {
31     MutableNetwork<String, Integer> mutableNetwork = NetworkBuilder.directed().build();
32     mutableNetwork.addNode("A");
33     ImmutableNetwork<String, Integer> immutableNetwork = ImmutableNetwork.copyOf(mutableNetwork);
34 
35     assertThat(immutableNetwork.asGraph()).isInstanceOf(ImmutableGraph.class);
36     assertThat(immutableNetwork).isNotInstanceOf(MutableNetwork.class);
37     assertThat(immutableNetwork).isEqualTo(mutableNetwork);
38 
39     mutableNetwork.addNode("B");
40     assertThat(immutableNetwork).isNotEqualTo(mutableNetwork);
41   }
42 
43   @Test
copyOfImmutableNetwork_optimized()44   public void copyOfImmutableNetwork_optimized() {
45     Network<String, String> network1 =
46         ImmutableNetwork.copyOf(NetworkBuilder.directed().<String, String>build());
47     Network<String, String> network2 = ImmutableNetwork.copyOf(network1);
48 
49     assertThat(network2).isSameInstanceAs(network1);
50   }
51 
52   @Test
edgesConnecting_directed()53   public void edgesConnecting_directed() {
54     MutableNetwork<String, String> mutableNetwork =
55         NetworkBuilder.directed().allowsSelfLoops(true).build();
56     mutableNetwork.addEdge("A", "A", "AA");
57     mutableNetwork.addEdge("A", "B", "AB");
58     Network<String, String> network = ImmutableNetwork.copyOf(mutableNetwork);
59 
60     assertThat(network.edgesConnecting("A", "A")).containsExactly("AA");
61     assertThat(network.edgesConnecting("A", "B")).containsExactly("AB");
62     assertThat(network.edgesConnecting("B", "A")).isEmpty();
63   }
64 
65   @Test
edgesConnecting_undirected()66   public void edgesConnecting_undirected() {
67     MutableNetwork<String, String> mutableNetwork =
68         NetworkBuilder.undirected().allowsSelfLoops(true).build();
69     mutableNetwork.addEdge("A", "A", "AA");
70     mutableNetwork.addEdge("A", "B", "AB");
71     Network<String, String> network = ImmutableNetwork.copyOf(mutableNetwork);
72 
73     assertThat(network.edgesConnecting("A", "A")).containsExactly("AA");
74     assertThat(network.edgesConnecting("A", "B")).containsExactly("AB");
75     assertThat(network.edgesConnecting("B", "A")).containsExactly("AB");
76   }
77 
78   @Test
immutableNetworkBuilder_appliesNetworkBuilderConfig()79   public void immutableNetworkBuilder_appliesNetworkBuilderConfig() {
80     ImmutableNetwork<String, Integer> emptyNetwork =
81         NetworkBuilder.directed()
82             .allowsSelfLoops(true)
83             .nodeOrder(ElementOrder.<String>natural())
84             .<String, Integer>immutable()
85             .build();
86 
87     assertThat(emptyNetwork.isDirected()).isTrue();
88     assertThat(emptyNetwork.allowsSelfLoops()).isTrue();
89     assertThat(emptyNetwork.nodeOrder()).isEqualTo(ElementOrder.<String>natural());
90   }
91 
92   /**
93    * Tests that the ImmutableNetwork.Builder doesn't change when the creating NetworkBuilder
94    * changes.
95    */
96   @Test
97   @SuppressWarnings("CheckReturnValue")
immutableNetworkBuilder_copiesNetworkBuilder()98   public void immutableNetworkBuilder_copiesNetworkBuilder() {
99     NetworkBuilder<String, Object> networkBuilder =
100         NetworkBuilder.directed()
101             .allowsSelfLoops(true)
102             .<String>nodeOrder(ElementOrder.<String>natural());
103     ImmutableNetwork.Builder<String, Integer> immutableNetworkBuilder =
104         networkBuilder.<String, Integer>immutable();
105 
106     // Update NetworkBuilder, but this shouldn't impact immutableNetworkBuilder
107     networkBuilder.allowsSelfLoops(false).nodeOrder(ElementOrder.<String>unordered());
108 
109     ImmutableNetwork<String, Integer> emptyNetwork = immutableNetworkBuilder.build();
110 
111     assertThat(emptyNetwork.isDirected()).isTrue();
112     assertThat(emptyNetwork.allowsSelfLoops()).isTrue();
113     assertThat(emptyNetwork.nodeOrder()).isEqualTo(ElementOrder.<String>natural());
114   }
115 
116   @Test
immutableNetworkBuilder_addNode()117   public void immutableNetworkBuilder_addNode() {
118     ImmutableNetwork<String, Integer> network =
119         NetworkBuilder.directed().<String, Integer>immutable().addNode("A").build();
120 
121     assertThat(network.nodes()).containsExactly("A");
122     assertThat(network.edges()).isEmpty();
123   }
124 
125   @Test
immutableNetworkBuilder_putEdgeFromNodes()126   public void immutableNetworkBuilder_putEdgeFromNodes() {
127     ImmutableNetwork<String, Integer> network =
128         NetworkBuilder.directed().<String, Integer>immutable().addEdge("A", "B", 10).build();
129 
130     assertThat(network.nodes()).containsExactly("A", "B");
131     assertThat(network.edges()).containsExactly(10);
132     assertThat(network.incidentNodes(10)).isEqualTo(EndpointPair.ordered("A", "B"));
133   }
134 
135   @Test
immutableNetworkBuilder_putEdgeFromEndpointPair()136   public void immutableNetworkBuilder_putEdgeFromEndpointPair() {
137     ImmutableNetwork<String, Integer> network =
138         NetworkBuilder.directed()
139             .<String, Integer>immutable()
140             .addEdge(EndpointPair.ordered("A", "B"), 10)
141             .build();
142 
143     assertThat(network.nodes()).containsExactly("A", "B");
144     assertThat(network.edges()).containsExactly(10);
145     assertThat(network.incidentNodes(10)).isEqualTo(EndpointPair.ordered("A", "B"));
146   }
147 }
148