Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new test cases #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jwetherell.algorithms.data_structures.test;

import static org.junit.Assert.assertTrue;

import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -136,4 +138,48 @@ private static final String toString(DisjointSet.Item<Integer>[] items) {
builder.append("}\n");
return builder.toString();
}
}

@Test
public void testDisjointSet3() {
final int max = 10;
final int[] array = new int[max];
for (int i=0; i<array.length; i++)
array[i] = i+1;

final DisjointSet.Item<Integer>[] items = new DisjointSet.Item[array.length];
for (int i=0; i<items.length; i++) {
final int v = array[i];
final DisjointSet.Item<Integer> s = DisjointSet.makeSet(v);
items[i] = s;
}

final int half = items.length/2;

// first half set
DisjointSet.Item<Integer> first = items[0];
for (int i=1; i<half; i++) {
final DisjointSet.Item<Integer> item = items[i];
first = DisjointSet.union(item, first);
}

final DisjointSet.Item<Integer> nullValue = DisjointSet.find(null);
Assert.assertTrue(nullValue==null);

final DisjointSet.Item<Integer> xyNull = DisjointSet.union(null, null);
Assert.assertTrue(xyNull==null);

final DisjointSet.Item<Integer> xNull = DisjointSet.union(null, first);
Assert.assertTrue(xNull.getValue()==first.getValue());

final DisjointSet.Item<Integer> yNull = DisjointSet.union(first, null);
Assert.assertTrue(yNull.getValue()==first.getValue());

String fresult = first.toString();
String expectedResult = "parent=2 rank=1 value=2";
assertTrue(fresult.equals(expectedResult));

String temp = "Sample";
Boolean result = first.equals(temp);
assertTrue(!result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,60 @@ public void testCostPathPair() {
Assert.assertTrue(p2.equals(p3) && p2.hashCode()==p3.hashCode());
Assert.assertTrue(p3.equals(p2) && p3.hashCode()==p2.hashCode());
}
}

@Test
public void testGraphtoString() {
Graph g1 = new Graph();

final List<Vertex<Integer>> vertices = new ArrayList<Vertex<Integer>>();
final Vertex<Integer> p1 = new Vertex<Integer>(10,1);
vertices.add(p1);
final Vertex<Integer> p2 = new Vertex<Integer>(10,2);
vertices.add(p2);

final List<Edge<Integer>> edges = new ArrayList<Edge<Integer>>();
final Edge<Integer> e1 = new Edge<Integer>(1,p1,p2);
edges.add(e1);
final Edge<Integer> e2 = new Edge<Integer>(1,p2,p1);
edges.add(e2);

Graph g2 = new Graph(vertices, edges);
List got_e1 = g2.getEdges();

Integer temp = 1;
g1.toString();

p1.getValue();
p1.getWeight();
p1.setWeight(5);
p1.getEdge(p2);
p1.pathTo(p2);
p1.toString();
p1.compareTo(p2);

Edge<Integer> e3 = new Edge<Integer>(e1);
e1.getCost();
e1.setCost(0);

Graph.CostVertexPair<Integer> c1 = new Graph.CostVertexPair<Integer>(1, new Vertex<Integer>(10));
Graph.CostVertexPair<Integer> c2 = new Graph.CostVertexPair<Integer>(1, new Vertex<Integer>(11));
Graph.CostVertexPair<Integer> c3 = new Graph.CostVertexPair<Integer>(1, new Vertex<Integer>(9));
c1.getCost();
c1.setCost(5);
c3.setCost(50);
c1.getVertex();
c1.compareTo(c2);
c1.compareTo(c3);
c1.toString();

final List<Edge<Integer>> s1 = new ArrayList<Graph.Edge<Integer>>(3);
s1.add(new Edge<Integer>(1, new Vertex<Integer>(10), new Vertex<Integer>(20)));
s1.add(new Edge<Integer>(2, new Vertex<Integer>(20), new Vertex<Integer>(30)));
final Graph.CostPathPair<Integer> cp1 = new Graph.CostPathPair<Integer>(1, s1);
cp1.getCost();
cp1.setCost(0);
cp1.toString();

Assert.assertFalse(g2.equals(temp));
}
}
Loading