aboutsummaryrefslogtreecommitdiff
path: root/guava-tests/test/com/google/common/graph/DefaultNetworkImplementationsTest.java
blob: b773a36e63156564534b8d32e99de93aa02433b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (C) 2017 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.graph;

import static com.google.common.graph.TestUtil.EdgeType.DIRECTED;
import static com.google.common.graph.TestUtil.EdgeType.UNDIRECTED;
import static com.google.common.graph.TestUtil.assertNodeNotInGraphErrorMessage;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.common.graph.TestUtil.EdgeType;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * Test for {@link Network} methods which have default implementations. Currently those
 * implementations are in {@link AbstractNetwork}; in future they might be in {@link Network}
 * itself, once we are willing to use Java 8 default methods.
 */
@AndroidIncompatible
// TODO(cpovirk): Figure out Android JUnit 4 support. Does it work with Gingerbread? @RunWith?
@RunWith(Parameterized.class)
public final class DefaultNetworkImplementationsTest {
  private MutableNetwork<Integer, String> network;
  private NetworkForTest<Integer, String> networkForTest;
  private static final Integer N1 = 1;
  private static final Integer N2 = 2;
  private static final Integer NODE_NOT_IN_GRAPH = 1000;
  private static final String E11 = "1-1";
  private static final String E11_A = "1-1a";
  private static final String E12 = "1-2";
  private static final String E12_A = "1-2a";
  private static final String E21 = "2-1";
  private static final String E23 = "2-3";

  @Parameters
  public static Collection<Object[]> parameters() {
    return Arrays.asList(
        new Object[][] {
          {UNDIRECTED}, {DIRECTED},
        });
  }

  private final EdgeType edgeType;

  public DefaultNetworkImplementationsTest(EdgeType edgeType) {
    this.edgeType = edgeType;
  }

  @Before
  public void setUp() throws Exception {
    NetworkBuilder<Object, Object> builder =
        (edgeType == EdgeType.DIRECTED) ? NetworkBuilder.directed() : NetworkBuilder.undirected();

    network = builder.allowsSelfLoops(true).allowsParallelEdges(true).build();
    networkForTest = NetworkForTest.from(network);
  }

  @Test
  public void edgesConnecting_disconnectedNodes() {
    network.addNode(N1);
    network.addNode(N2);
    assertThat(networkForTest.edgesConnecting(N1, N2)).isEmpty();
  }

  @Test
  public void edgesConnecting_nodesNotInGraph() {
    network.addNode(N1);
    network.addNode(N2);
    IllegalArgumentException e =
        assertThrows(
            IllegalArgumentException.class,
            () -> networkForTest.edgesConnecting(N1, NODE_NOT_IN_GRAPH));
    assertNodeNotInGraphErrorMessage(e);
    e =
        assertThrows(
            IllegalArgumentException.class,
            () -> networkForTest.edgesConnecting(NODE_NOT_IN_GRAPH, N2));
    assertNodeNotInGraphErrorMessage(e);
    e =
        assertThrows(
            IllegalArgumentException.class,
            () -> networkForTest.edgesConnecting(NODE_NOT_IN_GRAPH, NODE_NOT_IN_GRAPH));
    assertNodeNotInGraphErrorMessage(e);
  }

  @Test
  public void edgesConnecting_checkReturnedSetMutability() {
    network.addNode(N1);
    network.addNode(N2);
    Set<String> edgesConnecting = network.edgesConnecting(N1, N2);
    UnsupportedOperationException e =
        assertThrows(UnsupportedOperationException.class, () -> edgesConnecting.add(E23));
    network.addEdge(N1, N2, E12);
    assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactlyElementsIn(edgesConnecting);
  }

  @Test
  public void edgesConnecting_oneEdge() {
    network.addEdge(N1, N2, E12);
    assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12);
    if (edgeType == EdgeType.DIRECTED) {
      assertThat(networkForTest.edgesConnecting(N2, N1)).isEmpty();
    } else {
      assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E12);
    }
  }

  @Test
  public void edgesConnecting_selfLoop() {
    network.addEdge(N1, N1, E11);
    assertThat(networkForTest.edgesConnecting(N1, N1)).containsExactly(E11);
    network.addEdge(N1, N2, E12);
    assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12);
    assertThat(networkForTest.edgesConnecting(N1, N1)).containsExactly(E11);
  }

  @Test
  public void edgesConnecting_parallelEdges() {
    network.addEdge(N1, N2, E12);
    network.addEdge(N1, N2, E12_A);
    network.addEdge(N2, N1, E21);
    if (edgeType == EdgeType.DIRECTED) {
      assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12, E12_A);
      assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E21);
    } else {
      assertThat(networkForTest.edgesConnecting(N1, N2)).containsExactly(E12, E12_A, E21);
      assertThat(networkForTest.edgesConnecting(N2, N1)).containsExactly(E12, E12_A, E21);
    }
  }

  @Test
  public void edgesConnecting_parallelSelfLoopEdges() {
    network.addEdge(N1, N1, E11);
    network.addEdge(N1, N1, E11_A);
    assertThat(network.edgesConnecting(N1, N1)).containsExactly(E11, E11_A);
  }

  private static class NetworkForTest<N, E> extends AbstractNetwork<N, E> {
    private final Network<N, E> network;

    NetworkForTest(Network<N, E> network) {
      this.network = network;
    }

    static <N, E> NetworkForTest<N, E> from(Network<N, E> network) {
      return new NetworkForTest<>(network);
    }

    @Override
    public Set<N> nodes() {
      return network.nodes();
    }

    @Override
    public Set<E> edges() {
      return network.edges();
    }

    @Override
    public boolean isDirected() {
      return network.isDirected();
    }

    @Override
    public boolean allowsParallelEdges() {
      return network.allowsParallelEdges();
    }

    @Override
    public boolean allowsSelfLoops() {
      return network.allowsSelfLoops();
    }

    @Override
    public ElementOrder<N> nodeOrder() {
      return network.nodeOrder();
    }

    @Override
    public ElementOrder<E> edgeOrder() {
      return network.edgeOrder();
    }

    @Override
    public Set<N> adjacentNodes(N node) {
      return network.adjacentNodes(node);
    }

    @Override
    public Set<N> predecessors(N node) {
      return network.predecessors(node);
    }

    @Override
    public Set<N> successors(N node) {
      return network.successors(node);
    }

    @Override
    public Set<E> incidentEdges(N node) {
      return network.incidentEdges(node);
    }

    @Override
    public Set<E> inEdges(N node) {
      return network.inEdges(node);
    }

    @Override
    public Set<E> outEdges(N node) {
      return network.outEdges(node);
    }

    @Override
    public EndpointPair<N> incidentNodes(E edge) {
      return network.incidentNodes(edge);
    }

    @Override
    public Set<E> adjacentEdges(E edge) {
      return network.adjacentEdges(edge);
    }

    // _don't_ override edge*Connecting*; we want the behavior from AbstractNetwork
  }
}