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

Add files via upload #195

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
184 changes: 184 additions & 0 deletions DisjointSetTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package com.jwetherell.algorithms.data_structures.test;

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

import com.jwetherell.algorithms.data_structures.DisjointSet;

@SuppressWarnings("unchecked")
public class DisjointSetTests {

private static boolean DEBUG = false;

@Test
public void testDisjointSet1() {
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 DisjointSet.Item<Integer> f = DisjointSet.find(s);
Assert.assertTrue(f!=null);
Assert.assertTrue(f.getValue() == v);
}
if (DEBUG)
System.out.println(toString(items));

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(first, item);
}
if (DEBUG)
System.out.println(toString(items));
// second half set
DisjointSet.Item<Integer> second = items[half];
for (int i=half+1; i<items.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
second = DisjointSet.union(second, item);
}
if (DEBUG)
System.out.println(toString(items));

// Make sure all items in first set are actually in first set and vice versa
for (int i=0; i<half; i++) {
final DisjointSet.Item<Integer> item = items[i];
final DisjointSet.Item<Integer> result = DisjointSet.find(item);
Assert.assertTrue(result.equals(first));
}
for (int i=half; i<items.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
final DisjointSet.Item<Integer> result = DisjointSet.find(item);
Assert.assertTrue(result.equals(second));
}

// union the second set and the first set and make sure the superset is created properly
DisjointSet.Item<Integer> superSet = first;
for (int i=0; i<items.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
superSet = DisjointSet.union(superSet, item);
}
if (DEBUG)
System.out.println(toString(items));
for (int i=0; i<items.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
final DisjointSet.Item<Integer> result = DisjointSet.find(item);
Assert.assertTrue(result.equals(superSet));
}
}

@Test
public void testDisjointSet2() {
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 DisjointSet.Item<Integer> f = DisjointSet.find(s);
Assert.assertTrue(f!=null);
Assert.assertTrue(f.getValue() == v);
}
if (DEBUG)
System.out.println(toString(items));

final int quarter = items.length/4;

DisjointSet.Item<Integer> i1 = items[0];
for (int i=1; i<quarter; i++) {
final DisjointSet.Item<Integer> item = items[i];
i1 = DisjointSet.union(i1, item);
}
DisjointSet.Item<Integer> i2 = items[quarter];
for (int i=quarter+1; i<2*quarter; i++) {
final DisjointSet.Item<Integer> item = items[i];
i2 = DisjointSet.union(i2, item);
}
DisjointSet.Item<Integer> i3 = items[2*quarter];
for (int i=2*quarter+1; i<3*quarter; i++) {
final DisjointSet.Item<Integer> item = items[i];
i3 = DisjointSet.union(i3, item);
}
DisjointSet.Item<Integer> i4 = items[3*quarter];
for (int i=3*quarter+1; i<array.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
i4 = DisjointSet.union(i4, item);
}
if (DEBUG)
System.out.println(toString(items));

DisjointSet.Item<Integer> s1 = DisjointSet.union(i1, i2);
DisjointSet.Item<Integer> s2 = DisjointSet.union(i3, i4);
DisjointSet.Item<Integer> s3 = DisjointSet.union(s1, s2);
Assert.assertTrue(s3.getValue()==1 && s3.getRank()==3);
if (DEBUG)
System.out.println(toString(items));
}

private static final String toString(DisjointSet.Item<Integer>[] items) {
final StringBuilder builder = new StringBuilder();
builder.append("{\n");
for (DisjointSet.Item<Integer> item : items) {
builder.append('\t').append(item.toString()).append('\n');
}
builder.append("}\n");
return builder.toString();
}

@Test
public void testDisjointSet3() {
final int max = 10;
final int[] arr = new int[max];
for (int i=0; i<arr.length; i++)
arr[i] = i+1;
final DisjointSet.Item<Integer>[] items = new DisjointSet.Item[arr.length];
for (int i=0; i<items.length; i++) {
final int v = arr[i];
final DisjointSet.Item<Integer> s = DisjointSet.makeSet(v);
items[i] = s;
}
DisjointSet.Item<Integer> i1 = items[0];
DisjointSet.Item<Integer> i2 = items[1];
DisjointSet.Item<Integer> i3 = items[2];
DisjointSet.Item<Integer> i4 = items[3];

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

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

final DisjointSet.Item<Integer> equalRoots = DisjointSet.union(i2, i2);
Assert.assertTrue(equalRoots.getValue()==i2.getValue());

Assert.assertNull(DisjointSet.find(null));

Assert.assertNull(DisjointSet.union(null, null));

DisjointSet.Item<Integer> unequalIt1 = DisjointSet.union(i1, i2);
Assert.assertNotNull(unequalIt1);
DisjointSet.Item<Integer> unequalIt2 = DisjointSet.union(i1, i3);
Assert.assertNotNull(unequalIt2);
DisjointSet.Item<Integer> unequalIt3 = DisjointSet.union(i4, i1);
Assert.assertNotNull(unequalIt3);

Assert.assertFalse(unequalIt1.equals("False"));

String iString = i2.toString();
String expected = "parent=1 value=2";
Assert.assertTrue(iString.equals(expected));

}
}
71 changes: 71 additions & 0 deletions SkipListMapTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.jwetherell.algorithms.data_structures.test;

import static org.junit.Assert.assertTrue;

import static org.junit.Assert.*;
import org.junit.Test;

import com.jwetherell.algorithms.data_structures.List;
import com.jwetherell.algorithms.data_structures.SkipListMap;
import com.jwetherell.algorithms.data_structures.test.common.JavaMapTest;
import com.jwetherell.algorithms.data_structures.test.common.MapTest;
import com.jwetherell.algorithms.data_structures.test.common.Utils;
import com.jwetherell.algorithms.data_structures.test.common.Utils.TestData;

public class SkipListMapTests {

@Test
public void testSkipListMap() {
TestData data = Utils.generateTestData(1000);

String mapName = "SkipListMap";
SkipListMap<String,Integer> map = new SkipListMap<String,Integer>();
java.util.Map<String,Integer> jMap = map.toMap();

assertTrue(MapTest.testMap(map, String.class, mapName,
data.unsorted, data.invalid));
assertTrue(JavaMapTest.testJavaMap(jMap, Integer.class, mapName,
data.unsorted, data.sorted, data.invalid));
}

@Test
public void testPutAndGet(){
SkipListMap<String,Integer> map1 = new SkipListMap<String,Integer>();
SkipListMap<String,Integer> map2 = new SkipListMap<String,Integer>();
map1.put("Green", 3);
map2.put("Green", 3);
assertTrue(map1.contains("Green"));
assertNotNull(map1.get("Green"));
assertNull(map1.get("Blue"));
assertEquals("Green=3", map1.toString());
}

@Test
public void testListFunctions(){
List.ArrayList<Integer> list = new List.ArrayList<Integer>();
SkipListMap<String,Integer> skipListTest = new SkipListMap<String,Integer>();
java.util.Map<String, Integer> jMap = skipListTest.toMap();
list.add(1);
list.clear();
skipListTest.clear();
jMap.clear();
assertNotNull(skipListTest.toString());
assertNotNull(jMap.toString());
}

@Test
public void testToString(){
StringBuilder sb = new StringBuilder();
sb.append("sb");
}

@Test
public void testValidate() {
SkipListMap<String,Integer> map3 = new SkipListMap<String,Integer>();
map3.put("Yellow", null);
map3.validate();
assertTrue(!map3.validate());
}


}
36 changes: 36 additions & 0 deletions SuffixTrieTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.jwetherell.algorithms.data_structures.test;

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import java.util.Set;
import java.util.TreeSet;

import com.jwetherell.algorithms.data_structures.SuffixTrie;
import com.jwetherell.algorithms.data_structures.test.common.SuffixTreeTest;

public class SuffixTrieTests {

@Test
public void testSuffixTrie() {
String bookkeeper = "bookkeeper";
SuffixTrie<String> trie = new SuffixTrie<String>(bookkeeper);
assertTrue(SuffixTreeTest.suffixTreeTest(trie, bookkeeper));
}

@Test
public void testAdd() {
SuffixTrie<String> suffixTrieTest = new SuffixTrie<String>("book");
String part = "pen";
assertTrue(suffixTrieTest.add(part));
assertNotNull(suffixTrieTest.getSuffixes());
}

@Test
public void testString() {
SuffixTrie<String> suffixTrieTest2 = new SuffixTrie<String>("paper");
assertNotNull(suffixTrieTest2.toString());
}
}
75 changes: 75 additions & 0 deletions TrieMapTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.jwetherell.algorithms.data_structures.test;

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.jwetherell.algorithms.data_structures.List;
import com.jwetherell.algorithms.data_structures.TrieMap;
import com.jwetherell.algorithms.data_structures.test.common.JavaMapTest;
import com.jwetherell.algorithms.data_structures.test.common.MapTest;
import com.jwetherell.algorithms.data_structures.test.common.Utils;
import com.jwetherell.algorithms.data_structures.test.common.Utils.TestData;

public class TrieMapTests {

@Test
public void testTrieMap() {
TestData data = Utils.generateTestData(1000);

String mapName = "TrieMap";
TrieMap<String,Integer> map = new TrieMap<String,Integer>();
java.util.Map<String,Integer> jMap = map.toMap();

assertTrue(MapTest.testMap(map, String.class, mapName,
data.unsorted, data.invalid));
assertTrue(JavaMapTest.testJavaMap(jMap, String.class, mapName,
data.unsorted, data.sorted, data.invalid));
}

@Test
public void testPutAndGet() {
TrieMap<String,Integer> testTrieMap1 = new TrieMap<String,Integer>();
TrieMap<String,Integer> testTrieMap2 = new TrieMap<String,Integer>();
String key = "Blue";
Integer value = 2;
testTrieMap1.put(key,value);
testTrieMap2.put(key,value);
assertTrue(testTrieMap1.contains(key));
assertNotNull(testTrieMap2.get(key));
assertNull(testTrieMap1.get("Red"));
assertEquals(testTrieMap1.toString(),testTrieMap2.toString());

}

@Test
public void testClear() {

List.ArrayList<Integer> list = new List.ArrayList<Integer>();
TrieMap<String,Integer> testTrieMap3 = new TrieMap<String,Integer>();
java.util.Map<String, Integer> map = testTrieMap3.toMap();
list.add(1);
list.clear();
testTrieMap3.clear();
map.clear();
assertNotNull(map.toString());

}

@Test
public void testToString() {
StringBuilder sb = new StringBuilder();
sb.append("sb");
}

@Test
public void testValidate() {
TrieMap<String,String> testTrieMap4 = new TrieMap<String,String>();
String key = "a";
String val = null;
testTrieMap4.put(key, val);
assertTrue(!testTrieMap4.validate());
}

}