alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (AbstractDirectedNetworkTest.java)

This example Java source code file (AbstractDirectedNetworkTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractdirectednetworktest, abstractnetworktest, after, endpoints, illegalargumentexception, immutableset, integer, set, string, test, util

The AbstractDirectedNetworkTest.java Java example source code

/*
 * Copyright (C) 2014 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.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableSet;

import org.junit.After;
import org.junit.Test;

import java.util.Collections;
import java.util.Set;

/**
 * Abstract base class for testing implementations of {@link Network} interface.
 *
 * <p>This class is responsible for testing that a directed implementation of {@link Network}
 * is correctly handling directed edges. Implementation-dependent test cases are left to
 * subclasses. Test cases that do not require the graph to be directed are found in superclasses.
 *
 */
public abstract class AbstractDirectedNetworkTest extends AbstractNetworkTest {

  @After
  public void validateSourceAndTarget() {
    for (Integer node : graph.nodes()) {
      for (String inEdge : graph.inEdges(node)) {
        Integer oppositeNode = Graphs.oppositeNode(graph, inEdge, node);
        Endpoints<Integer> endpoints = graph.incidentNodes(inEdge);
        assertThat(endpoints.source()).isEqualTo(oppositeNode);
        assertThat(endpoints.target()).isEqualTo(node);
      }

      for (String outEdge : graph.outEdges(node)) {
        Integer oppositeNode = Graphs.oppositeNode(graph, outEdge, node);
        Endpoints<Integer> endpoints = graph.incidentNodes(outEdge);
        assertThat(endpoints.source()).isEqualTo(node);
        assertThat(endpoints.target()).isEqualTo(oppositeNode);
      }

      for (Integer adjacentNode : graph.adjacentNodes(node)) {
        Set<String> edges = graph.edgesConnecting(node, adjacentNode);
        Set<String> antiParallelEdges = graph.edgesConnecting(adjacentNode, node);
        assertTrue(node.equals(adjacentNode) || Collections.disjoint(edges, antiParallelEdges));
      }
    }
  }

  @Override
  @Test
  public void incidentNodes_oneEdge() {
    addEdge(E12, N1, N2);
    assertThat(graph.incidentNodes(E12)).containsExactly(N1, N2).inOrder();
  }

  @Test
  public void edgesConnecting_oneEdge() {
    addEdge(E12, N1, N2);
    assertThat(graph.edgesConnecting(N1, N2)).containsExactly(E12);
    // Passed nodes should be in the correct edge direction, first is the
    // source node and the second is the target node
    assertThat(graph.edgesConnecting(N2, N1)).isEmpty();
  }

  @Test
  public void inEdges_oneEdge() {
    addEdge(E12, N1, N2);
    assertThat(graph.inEdges(N2)).containsExactly(E12);
    // Edge direction handled correctly
    assertThat(graph.inEdges(N1)).isEmpty();
  }

  @Test
  public void outEdges_oneEdge() {
    addEdge(E12, N1, N2);
    assertThat(graph.outEdges(N1)).containsExactly(E12);
    // Edge direction handled correctly
    assertThat(graph.outEdges(N2)).isEmpty();
  }

  @Test
  public void predecessors_oneEdge() {
    addEdge(E12, N1, N2);
    assertThat(graph.predecessors(N2)).containsExactly(N1);
    // Edge direction handled correctly
    assertThat(graph.predecessors(N1)).isEmpty();
  }

  @Test
  public void successors_oneEdge() {
    addEdge(E12, N1, N2);
    assertThat(graph.successors(N1)).containsExactly(N2);
    // Edge direction handled correctly
    assertThat(graph.successors(N2)).isEmpty();
  }

  @Test
  public void inDegree_oneEdge() {
    addEdge(E12, N1, N2);
    assertEquals(1, graph.inDegree(N2));
    // Edge direction handled correctly
    assertEquals(0, graph.inDegree(N1));
  }

  @Test
  public void outDegree_oneEdge() {
    addEdge(E12, N1, N2);
    assertEquals(1, graph.outDegree(N1));
    // Edge direction handled correctly
    assertEquals(0, graph.outDegree(N2));
  }

  @Test
  public void source_oneEdge() {
    addEdge(E12, N1, N2);
    assertEquals(N1, graph.incidentNodes(E12).source());
  }

  @Test
  public void source_edgeNotInGraph() {
    try {
      graph.incidentNodes(EDGE_NOT_IN_GRAPH).source();
      fail(ERROR_EDGE_NOT_IN_GRAPH);
    } catch (IllegalArgumentException e) {
      assertEdgeNotInGraphErrorMessage(e);
    }
  }

  @Test
  public void target_oneEdge() {
    addEdge(E12, N1, N2);
    assertEquals(N2, graph.incidentNodes(E12).target());
  }

  @Test
  public void target_edgeNotInGraph() {
    try {
      graph.incidentNodes(EDGE_NOT_IN_GRAPH).target();
      fail(ERROR_EDGE_NOT_IN_GRAPH);
    } catch (IllegalArgumentException e) {
      assertEdgeNotInGraphErrorMessage(e);
    }
  }

  // Element Mutation

  @Test
  public void addEdge_existingNodes() {
    // Adding nodes initially for safety (insulating from possible future
    // modifications to proxy methods)
    addNode(N1);
    addNode(N2);
    assertTrue(addEdge(E12, N1, N2));
    assertThat(graph.edges()).contains(E12);
    assertThat(graph.edgesConnecting(N1, N2)).containsExactly(E12);
    // Direction of the added edge is correctly handled
    assertThat(graph.edgesConnecting(N2, N1)).isEmpty();
  }

  @Test
  public void addEdge_existingEdgeBetweenSameNodes() {
    addEdge(E12, N1, N2);
    ImmutableSet<String> edges = ImmutableSet.copyOf(graph.edges());
    assertFalse(addEdge(E12, N1, N2));
    assertThat(graph.edges()).containsExactlyElementsIn(edges);
  }

  @Test
  public void addEdge_existingEdgeBetweenDifferentNodes() {
    addEdge(E12, N1, N2);
    try {
      // Edge between totally different nodes
      addEdge(E12, N4, N5);
      fail(ERROR_ADDED_EXISTING_EDGE);
    } catch (IllegalArgumentException e) {
      assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE);
    }
    try {
      // Edge between same nodes but in reverse direction
      addEdge(E12, N2, N1);
      fail(ERROR_ADDED_EXISTING_EDGE);
    } catch (IllegalArgumentException e) {
      assertThat(e.getMessage()).contains(ERROR_REUSE_EDGE);
    }
  }

  @Test
  public void addEdge_parallelEdge() {
    addEdge(E12, N1, N2);
    try {
      addEdge(EDGE_NOT_IN_GRAPH, N1, N2);
      fail(ERROR_ADDED_PARALLEL_EDGE);
    } catch (IllegalArgumentException e) {
      assertThat(e.getMessage()).contains(ERROR_PARALLEL_EDGE);
    }
  }

  @Test
  public void removeEdge_existingEdge() {
    addEdge(E12, N1, N2);
    assertTrue(graph.removeEdge(E12));
    assertThat(graph.edges()).doesNotContain(E12);
    assertThat(graph.edgesConnecting(N1, N2)).isEmpty();
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java AbstractDirectedNetworkTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.