/*
* This file is part of the TimeFinder project.
* Visit http://www.timefinder.de for more information.
* Copyright 2008 the original author or 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 de.timefinder.algo.graph;
import de.timefinder.algo.graph.WeightedGraph;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net
*/
public class WeightedGraphTest {
/**
* Test of addEdge method, of class BipartiteWeightedGraph2.
*/
@Test
public void testAddEdge() {
System.out.println("addEdge");
WeightedGraph graph = new WeightedGraph(10);
graph.addEdge(3, 4, 7f);
graph.addEdge(3, 4, 8f);
graph.addEdge(2, 4, 9f);
assertEquals(2, graph.getNoOfEdges());
assertTrue(graph.getNeighbors(4).containsValue(8f));
boolean ret = graph.removeEdge(3, 4);
assertTrue(ret);
assertEquals(1, graph.getNoOfEdges());
ret = graph.removeEdge(3, 4);
assertFalse(ret);
assertEquals(1, graph.getNoOfEdges());
}
/**
* Test of getXNeighbors method, of class BipartiteWeightedGraph2.
*/
@Test
public void testGetNeighbors() {
System.out.println("getNeighbors");
WeightedGraph graph = new WeightedGraph(10);
graph.addEdge(3, 4, 7f);
graph.addEdge(3, 4, 7f);
graph.addEdge(2, 4, 7f);
assertEquals(1, graph.getNeighbors(2).size());
assertEquals(2, graph.getNeighbors(4).size());
}
/**
* Test of getOneNodeWithAnEdge method, of class WeightedGraph.
*/
@Test
public void testGetOneNodeWithAnEdge() {
System.out.println("getOneNodeWithAnEdge");
WeightedGraph graph = new WeightedGraph(10);
graph.addEdge(3, 4, 7f);
graph.addEdge(3, 4, 7f);
graph.addEdge(2, 4, 7f);
assertTrue(graph.getOneNodeWithAnEdge() != -1);
graph.removeEdge(3, 4);
assertTrue(graph.getOneNodeWithAnEdge() != -1);
graph.removeEdge(2, 4);
assertTrue(graph.getOneNodeWithAnEdge() == -1);
}
/**
* Test of removeNodeAndNeighbors method, of class WeightedGraph.
*/
@Test
public void testRemoveNodeAndNeighbors() {
System.out.println("removeNodeAndNeighbors");
WeightedGraph graph = new WeightedGraph(10);
graph.addEdge(3, 4, 7f);
graph.addEdge(1, 5, 7f);
graph.addEdge(2, 4, 7f);
assertTrue(graph.removeNodeAndNeighbors(4));
assertEquals(graph.getNoOfEdges(), 1);
assertTrue(graph.getOneNodeWithAnEdge() != -1);
assertEquals(graph.getNeighbors(4).keySet().size(), 0);
assertEquals(graph.getNeighbors(1).keySet().iterator().next(), 5d, 0.001);
assertTrue(graph.removeNodeAndNeighbors(5));
assertTrue(graph.getOneNodeWithAnEdge() == -1);
}
}