• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 ImmutableValueGraph} . */
26 @RunWith(JUnit4.class)
27 public class ImmutableValueGraphTest {
28 
29   @Test
immutableValueGraph()30   public void immutableValueGraph() {
31     MutableValueGraph<String, Integer> mutableValueGraph = ValueGraphBuilder.directed().build();
32     mutableValueGraph.addNode("A");
33     ImmutableValueGraph<String, Integer> immutableValueGraph =
34         ImmutableValueGraph.copyOf(mutableValueGraph);
35 
36     assertThat(immutableValueGraph.asGraph()).isInstanceOf(ImmutableGraph.class);
37     assertThat(immutableValueGraph).isNotInstanceOf(MutableValueGraph.class);
38     assertThat(immutableValueGraph).isEqualTo(mutableValueGraph);
39 
40     mutableValueGraph.addNode("B");
41     assertThat(immutableValueGraph).isNotEqualTo(mutableValueGraph);
42   }
43 
44   @Test
copyOfImmutableValueGraph_optimized()45   public void copyOfImmutableValueGraph_optimized() {
46     ValueGraph<String, Integer> graph1 =
47         ImmutableValueGraph.copyOf(ValueGraphBuilder.directed().<String, Integer>build());
48     ValueGraph<String, Integer> graph2 = ImmutableValueGraph.copyOf(graph1);
49 
50     assertThat(graph2).isSameInstanceAs(graph1);
51   }
52 
53   @Test
incidentEdgeOrder_stable()54   public void incidentEdgeOrder_stable() {
55     ImmutableValueGraph<String, Integer> immutableValueGraph =
56         ImmutableValueGraph.copyOf(ValueGraphBuilder.directed().<String, Integer>build());
57 
58     assertThat(immutableValueGraph.incidentEdgeOrder()).isEqualTo(ElementOrder.stable());
59   }
60 
61   @Test
incidentEdgeOrder_fromUnorderedGraph_stable()62   public void incidentEdgeOrder_fromUnorderedGraph_stable() {
63     ImmutableValueGraph<String, Integer> immutableValueGraph =
64         ImmutableValueGraph.copyOf(
65             ValueGraphBuilder.directed()
66                 .incidentEdgeOrder(ElementOrder.unordered())
67                 .<String, Integer>build());
68 
69     assertThat(immutableValueGraph.incidentEdgeOrder()).isEqualTo(ElementOrder.stable());
70   }
71 
72   @Test
immutableValueGraphBuilder_appliesGraphBuilderConfig()73   public void immutableValueGraphBuilder_appliesGraphBuilderConfig() {
74     ImmutableValueGraph<String, Integer> emptyGraph =
75         ValueGraphBuilder.directed()
76             .allowsSelfLoops(true)
77             .nodeOrder(ElementOrder.<String>natural())
78             .<String, Integer>immutable()
79             .build();
80 
81     assertThat(emptyGraph.isDirected()).isTrue();
82     assertThat(emptyGraph.allowsSelfLoops()).isTrue();
83     assertThat(emptyGraph.nodeOrder()).isEqualTo(ElementOrder.<String>natural());
84   }
85 
86   /**
87    * Tests that the ImmutableValueGraph.Builder doesn't change when the creating ValueGraphBuilder
88    * changes.
89    */
90   @Test
91   @SuppressWarnings("CheckReturnValue")
immutableValueGraphBuilder_copiesGraphBuilder()92   public void immutableValueGraphBuilder_copiesGraphBuilder() {
93     ValueGraphBuilder<String, Object> graphBuilder =
94         ValueGraphBuilder.directed()
95             .allowsSelfLoops(true)
96             .<String>nodeOrder(ElementOrder.<String>natural());
97     ImmutableValueGraph.Builder<String, Integer> immutableValueGraphBuilder =
98         graphBuilder.<String, Integer>immutable();
99 
100     // Update ValueGraphBuilder, but this shouldn't impact immutableValueGraphBuilder
101     graphBuilder.allowsSelfLoops(false).nodeOrder(ElementOrder.<String>unordered());
102 
103     ImmutableValueGraph<String, Integer> emptyGraph = immutableValueGraphBuilder.build();
104 
105     assertThat(emptyGraph.isDirected()).isTrue();
106     assertThat(emptyGraph.allowsSelfLoops()).isTrue();
107     assertThat(emptyGraph.nodeOrder()).isEqualTo(ElementOrder.<String>natural());
108   }
109 
110   @Test
immutableValueGraphBuilder_addNode()111   public void immutableValueGraphBuilder_addNode() {
112     ImmutableValueGraph<String, Integer> graph =
113         ValueGraphBuilder.directed().<String, Integer>immutable().addNode("A").build();
114 
115     assertThat(graph.nodes()).containsExactly("A");
116     assertThat(graph.edges()).isEmpty();
117   }
118 
119   @Test
immutableValueGraphBuilder_putEdgeFromNodes()120   public void immutableValueGraphBuilder_putEdgeFromNodes() {
121     ImmutableValueGraph<String, Integer> graph =
122         ValueGraphBuilder.directed()
123             .<String, Integer>immutable()
124             .putEdgeValue("A", "B", 10)
125             .build();
126 
127     assertThat(graph.nodes()).containsExactly("A", "B");
128     assertThat(graph.edges()).containsExactly(EndpointPair.ordered("A", "B"));
129     assertThat(graph.edgeValueOrDefault("A", "B", null)).isEqualTo(10);
130   }
131 
132   @Test
immutableValueGraphBuilder_putEdgeFromEndpointPair()133   public void immutableValueGraphBuilder_putEdgeFromEndpointPair() {
134     ImmutableValueGraph<String, Integer> graph =
135         ValueGraphBuilder.directed()
136             .<String, Integer>immutable()
137             .putEdgeValue(EndpointPair.ordered("A", "B"), 10)
138             .build();
139 
140     assertThat(graph.nodes()).containsExactly("A", "B");
141     assertThat(graph.edges()).containsExactly(EndpointPair.ordered("A", "B"));
142     assertThat(graph.edgeValueOrDefault("A", "B", null)).isEqualTo(10);
143   }
144 
145   @Test
immutableValueGraphBuilder_incidentEdges_preservesIncidentEdgesOrder()146   public void immutableValueGraphBuilder_incidentEdges_preservesIncidentEdgesOrder() {
147     ImmutableValueGraph<Integer, String> graph =
148         ValueGraphBuilder.directed()
149             .<Integer, String>immutable()
150             .putEdgeValue(2, 1, "2-1")
151             .putEdgeValue(2, 3, "2-3")
152             .putEdgeValue(1, 2, "1-2")
153             .build();
154 
155     assertThat(graph.incidentEdges(2))
156         .containsExactly(
157             EndpointPair.ordered(2, 1), EndpointPair.ordered(2, 3), EndpointPair.ordered(1, 2))
158         .inOrder();
159   }
160 
161   @Test
immutableValueGraphBuilder_incidentEdgeOrder_stable()162   public void immutableValueGraphBuilder_incidentEdgeOrder_stable() {
163     ImmutableValueGraph<Integer, String> graph =
164         ValueGraphBuilder.directed().<Integer, String>immutable().build();
165 
166     assertThat(graph.incidentEdgeOrder()).isEqualTo(ElementOrder.stable());
167   }
168 
169   @Test
immutableValueGraphBuilder_fromUnorderedBuilder_incidentEdgeOrder_stable()170   public void immutableValueGraphBuilder_fromUnorderedBuilder_incidentEdgeOrder_stable() {
171     ImmutableValueGraph<Integer, String> graph =
172         ValueGraphBuilder.directed()
173             .incidentEdgeOrder(ElementOrder.unordered())
174             .<Integer, String>immutable()
175             .build();
176 
177     assertThat(graph.incidentEdgeOrder()).isEqualTo(ElementOrder.stable());
178   }
179 }
180