• 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).isSameAs(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