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

Java example source code file (GraphPropertiesTest.java)

This example Java source code file (GraphPropertiesTest.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

e11, e12, e13, e23, e31, integer, mutablenetwork, string, test

The GraphPropertiesTest.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.graph.GraphProperties.isCyclic;
import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for {@link GraphProperties}.
 */
@RunWith(JUnit4.class)
public class GraphPropertiesTest {
  private static final Integer N1 = 1;
  private static final Integer N2 = 2;
  private static final Integer N3 = 3;
  private static final String E11 = "1-1";
  private static final String E12 = "1-2";
  private static final String E12_A = "1-2a";
  private static final String E13 = "1-3";
  private static final String E21 = "2-1";
  private static final String E23 = "2-3";
  private static final String E31 = "3-1";

  @Test
  public void isCyclic_emptyGraph() {
    Network<Integer, String> directedGraph = NetworkBuilder.directed().build();
    assertThat(isCyclic(directedGraph)).isFalse();
  }

  @Test
  public void isCyclic_isolatedNodes() {
    MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build();
    directedGraph.addNode(N1);
    assertThat(isCyclic(directedGraph)).isFalse();
    directedGraph.addNode(N2);
    assertThat(isCyclic(directedGraph)).isFalse();
  }

  @Test
  public void isCyclic_oneEdge() {
    MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build();
    directedGraph.addEdge(E12, N1, N2);
    assertThat(isCyclic(directedGraph)).isFalse();
  }

  @Test
  public void isCyclic_selfLoopEdge() {
    MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build();
    directedGraph.addEdge(E11, N1, N1);
    assertThat(isCyclic(directedGraph)).isTrue();
  }

  @Test
  public void isCyclic_twoParallelEdges() {
    MutableNetwork<Integer, String> directedGraph =
        NetworkBuilder.directed().allowsParallelEdges(true).build();
    directedGraph.addEdge(E12, N1, N2);
    directedGraph.addEdge(E12_A, N1, N2);
    assertThat(isCyclic(directedGraph)).isFalse();
  }

  @Test
  public void isCyclic_twoAcyclicEdges() {
    MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build();
    directedGraph.addEdge(E12, N1, N2);
    directedGraph.addEdge(E13, N1, N3);
    assertThat(isCyclic(directedGraph)).isFalse();
  }

  @Test
  public void isCyclic_twoCyclicEdges() {
    MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build();
    directedGraph.addEdge(E12, N1, N2);
    directedGraph.addEdge(E21, N2, N1);
    assertThat(isCyclic(directedGraph)).isTrue();
  }

  @Test
  public void isCyclic_threeAcyclicEdges() {
    MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build();
    directedGraph.addEdge(E12, N1, N2);
    directedGraph.addEdge(E23, N2, N3);
    directedGraph.addEdge(E13, N1, N3);
    assertThat(isCyclic(directedGraph)).isFalse();
  }

  @Test
  public void isCyclic_threeCyclicEdges() {
    MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build();
    directedGraph.addEdge(E12, N1, N2);
    directedGraph.addEdge(E23, N2, N3);
    directedGraph.addEdge(E31, N3, N1);
    assertThat(isCyclic(directedGraph)).isTrue();
  }

  @Test
  public void isCyclic_disconnectedCyclicGraph() {
    MutableNetwork<Integer, String> directedGraph = NetworkBuilder.directed().build();
    directedGraph.addEdge(E12, N1, N2);
    directedGraph.addEdge(E21, N2, N1);
    directedGraph.addNode(N3);
    assertThat(isCyclic(directedGraph)).isTrue();
  }

  @Test
  public void isCyclic_cyclicMultigraph() {
    MutableNetwork<Integer, String> directedGraph =
        NetworkBuilder.directed().allowsParallelEdges(true).build();
    directedGraph.addEdge(E12, N1, N2);
    directedGraph.addEdge(E12_A, N1, N2);
    directedGraph.addEdge(E23, N2, N3);
    directedGraph.addEdge(E31, N3, N1);
    assertThat(isCyclic(directedGraph)).isTrue();
  }

  @Test
  public void isCyclic_multipleCycles() {
    MutableNetwork<Integer, String> directedGraph =
        NetworkBuilder.directed().allowsParallelEdges(true).build();
    directedGraph.addEdge(E12, N1, N2);
    directedGraph.addEdge(E21, N2, N1);
    directedGraph.addEdge(E23, N2, N3);
    directedGraph.addEdge(E31, N3, N1);
    assertThat(isCyclic(directedGraph)).isTrue();
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java GraphPropertiesTest.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.