Last active
August 29, 2015 14:18
-
-
Save rohitdatta/3383f54ce801d008e984 to your computer and use it in GitHub Desktop.
Set Tester for 314 Assignment 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright 2015 Rohit Datta, Chirag Khatri and Kapil Krishnakumar. Licensed under MIT License. Must maintain this copyright in all subsequent versions | |
* Assignment created by Mike Scott at UT Austin | |
* Runs on Java V7.0 and up | |
* */ | |
import java.util.Scanner; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import javax.swing.JFileChooser; | |
import javax.swing.UIManager; | |
import java.util.ArrayList; | |
import java.util.TreeSet; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.Iterator; | |
public class SetTester | |
{ | |
public static void main(String[] args){ | |
minTests(); | |
maxTests(); | |
addTests(); | |
addAllTests(); | |
clearTests(); | |
differenceTests(); | |
intersectionTests(); | |
unionTests(); | |
iteratorTests(); | |
sizeTests(); | |
containsTests(); | |
containsAllTests(); | |
equalsTests(); | |
toStringTests(); | |
removeTests(); | |
} | |
private static void removeTests() { | |
String testName = "remove tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
uset1.add("ROY'S"); | |
uset1.add("OUR"); | |
uset1.add("BOY"); | |
boolean removed = uset1.remove("OUR"); | |
assertEquals(removed && !(uset1.contains("OUR")), true, testName + testNum); | |
testNum++; | |
removed = uset1.remove("OUR"); | |
assertEquals(removed && !(uset1.contains("OUR")), false, testName + testNum); | |
testNum++; | |
removed = uset1.remove("BOY"); | |
assertEquals(removed && !(uset1.contains("BOY")), true, testName + testNum); | |
testNum++; | |
removed = uset1.remove("ROY'S"); | |
assertEquals(removed && uset1.size() == 0, true, testName+ testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() | |
{ | |
uset1.remove(null); | |
} | |
}, testName + testNum); | |
} | |
private static void toStringTests() { | |
String testName = "toString tests abstract/unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<>(); | |
uset1.add("PIKACHU"); | |
uset1.add("ROY'S"); | |
uset1.add("OUR"); | |
uset1.add("BOY"); | |
String expected = "(PIKACHU, ROY'S, OUR, BOY)"; | |
assertEquals(uset1.toString(), expected, testName + testNum); | |
testNum++; | |
testName = "toString tests for sorted:"; | |
final AbstractSet<String> sSet1 = new SortedSet<>(); | |
sSet1.add("PIKACHU"); | |
sSet1.add("ROY'S"); | |
sSet1.add("OUR"); | |
sSet1.add("BOY"); | |
expected = "(BOY, OUR, PIKACHU, ROY'S)"; | |
assertEquals(sSet1.toString(), expected, testName + testNum); | |
} | |
private static void equalsTests() { | |
String testName = "equals tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset2 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset3 = new SortedSet<String>(); | |
uset2.add("PIKACHU"); | |
uset1.add("PIKACHU"); | |
assertEquals(uset1.equals(uset2), true, testName + testNum); | |
testNum++; | |
assertEquals(uset2.equals(uset1), true, testName + testNum); | |
testNum++; | |
uset2.add("CHAR!!!"); | |
assertEquals(uset2.equals(uset1), false, testName + testNum); | |
testNum++; | |
uset3.add("PIKACHU"); | |
assertEquals(uset1.equals(uset3), true, testName+ testNum); | |
testNum++; | |
//note this checks the equals for sortedSet. sortedSet.equals(unsortedSet) | |
assertEquals(uset3.equals(uset1), true, testName+ testNum); | |
testNum++; | |
testName = "equals tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sSet1 = new SortedSet<>(); | |
final AbstractSet<String> sSet2 = new SortedSet<>(); | |
final AbstractSet<String> sSet3 = new SortedSet<>(); | |
sSet2.add("PIKACHU"); | |
sSet1.add("PIKACHU"); | |
assertEquals(sSet1.equals(sSet2), true, testName + testNum); | |
testNum++; | |
assertEquals(sSet2.equals(sSet1), true, testName + testNum); | |
testNum++; | |
sSet2.add("CHAR!!!"); | |
assertEquals(sSet2.equals(sSet1), false, testName + testNum); | |
testNum++; | |
sSet3.add("PIKACHU"); | |
assertEquals(sSet1.equals(sSet3), true, testName+ testNum); | |
testNum++; | |
assertEquals(uset3.equals("MARTH"), false, testName+ testNum); | |
testNum++; | |
} | |
private static void sizeTests() { | |
String testName = "size tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
uset1.add("ROY'S"); | |
uset1.add("OUR"); | |
uset1.add("BOY"); | |
assertEquals(uset1.size() == 3, true, testName + testNum); | |
testNum++; | |
uset1.remove("OUR"); | |
assertEquals(uset1.size() == 2, true, testName + testNum); | |
testNum++; | |
uset1.remove("BOY"); | |
assertEquals(uset1.size() == 1, true, testName + testNum); | |
testNum++; | |
uset1.add("ROY'S"); | |
assertEquals(uset1.size() == 1, true, testName+ testNum); | |
testNum++; | |
testName = "size tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sSet1 = new SortedSet<>(); | |
sSet1.add("ROY'S"); | |
sSet1.add("OUR"); | |
sSet1.add("BOY"); | |
assertEquals(sSet1.size() == 3, true, testName + testNum); | |
testNum++; | |
sSet1.remove("ROY'S"); | |
assertEquals(sSet1.size() == 2, true, testName + testNum); | |
testNum++; | |
sSet1.clear(); | |
assertEquals(sSet1.size() == 0, true, testName + testNum); | |
} | |
private static void iteratorTests() { | |
String testName = "iterator tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset2 = new UnsortedSet<String>(); | |
assertEquals(uset1.iterator() != null, true, testName + testNum); | |
testNum++; | |
uset2.clear(); | |
assertEquals(uset2.iterator() != null, true, testName + testNum); | |
testNum++; | |
testName = "iterator tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sSet1 = new SortedSet<>(); | |
sSet1.add("ROY"); | |
Iterator<String> it1 = sSet1.iterator(); | |
assertEquals(it1.hasNext(), true, testName + testNum); | |
testNum++; | |
assertEquals(it1.next(), "ROY", testName + testNum); | |
} | |
private static void unionTests() { | |
String testName = "union tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset2 = new UnsortedSet<String>(); | |
final ISet<String> uset3; | |
uset1.add("ROY'S"); | |
uset1.add("OUR"); | |
uset1.add("BOY"); | |
uset2.add("BULBSAUR"); | |
uset2.add("SQUIRTLE"); | |
uset2.add("ROY'S"); | |
uset3 = uset1.union(uset2); | |
assertEquals(uset3.containsAll(uset1), true, testName + testNum); | |
testNum++; | |
assertEquals(uset3.containsAll(uset2), true, testName + testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() | |
{ | |
uset1.union(null); | |
} | |
}, testName + testNum); | |
testName = "union tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sSet1 = new SortedSet<>(); | |
final AbstractSet<String> sSet2 = new SortedSet<>(); | |
ISet<String> sSet3; | |
final AbstractSet<String> sSet4 = new SortedSet<>(); | |
final AbstractSet<String> sSet5 = new SortedSet<>(); | |
sSet1.add("ROY'S"); | |
sSet1.add("OUR"); | |
sSet1.add("BOY"); | |
sSet2.add("BULBASAUR"); | |
sSet2.add("SQUIRTLE"); | |
sSet2.add("ROY'S"); | |
sSet3 = sSet1.union(sSet2); | |
sSet4.add("ROY'S"); | |
sSet4.add("OUR"); | |
sSet4.add("BOY"); | |
sSet4.add("SQUIRTLE"); | |
sSet4.add("BULBASAUR"); | |
assertEquals(sSet3.equals(sSet4), true, testName + testNum); | |
testNum++; | |
sSet3 = sSet2.union(sSet1); | |
sSet5.add("ROY'S"); | |
sSet5.add("OUR"); | |
sSet5.add("BOY"); | |
sSet5.add("SQUIRTLE"); | |
sSet5.add("BULBASAUR"); | |
assertEquals(sSet3.equals(sSet5), true, testName + testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() | |
{ | |
sSet1.union(null); | |
} | |
}, testName + testNum); | |
} | |
private static void intersectionTests() { | |
String testName = "intersection tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset2 = new UnsortedSet<String>(); | |
ISet<String> uset3; | |
final AbstractSet<String> uset4 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset5 = new UnsortedSet<String>(); | |
uset1.add("ROY'S"); | |
uset1.add("OUR"); | |
uset1.add("BOY"); | |
uset2.add("BULBSAUR"); | |
uset2.add("SQUIRTLE"); | |
uset2.add("ROY'S"); | |
uset3 = uset1.intersection(uset2); | |
uset4.add("ROY'S"); | |
assertEquals(uset3.containsAll(uset4), true, testName + testNum); | |
testNum++; | |
uset3 = uset2.intersection(uset1); | |
uset5.add("ROY'S"); | |
assertEquals(uset3.containsAll(uset5), true, testName + testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() | |
{ | |
uset1.intersection(null); | |
} | |
}, testName + testNum); | |
testName = "intersection tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sSet1 = new SortedSet<>(); | |
final AbstractSet<String> sSet2 = new SortedSet<>(); | |
ISet<String> sSet3; | |
final AbstractSet<String> sSet4 = new SortedSet<>(); | |
final AbstractSet<String> sSet5 = new SortedSet<>(); | |
sSet1.add("ROY'S"); | |
sSet1.add("OUR"); | |
sSet1.add("BOY"); | |
sSet2.add("BULBSAUR"); | |
sSet2.add("SQUIRTLE"); | |
sSet2.add("ROY'S"); | |
sSet3 = sSet1.intersection(sSet2); | |
sSet4.add("ROY'S"); | |
assertEquals(sSet3.equals(sSet4), true, testName + testNum); | |
testNum++; | |
sSet3 = sSet2.intersection(sSet1); | |
sSet5.add("ROY'S"); | |
assertEquals(sSet3.equals(sSet5), true, testName + testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() | |
{ | |
sSet1.intersection(null); | |
} | |
}, testName + testNum); | |
} | |
private static void differenceTests() { | |
String testName = "difference tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset2 = new UnsortedSet<String>(); | |
ISet<String> uset3; | |
final AbstractSet<String> uset4 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset5 = new UnsortedSet<String>(); | |
uset1.add("ROY'S"); | |
uset1.add("OUR"); | |
uset1.add("BOY"); | |
uset2.add("BULBSAUR"); | |
uset2.add("SQUIRTLE"); | |
uset2.add("ROY'S"); | |
uset3 = uset1.difference(uset2); | |
uset4.add("OUR"); | |
uset4.add("BOY"); | |
assertEquals(uset3.containsAll(uset4), true, testName + testNum); | |
testNum++; | |
uset3 = uset2.difference(uset1); | |
uset5.add("BULBSAUR"); | |
uset5.add("SQUIRTLE"); | |
assertEquals(uset3.containsAll(uset5), true, testName + testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() | |
{ | |
uset1.difference(null); | |
} | |
}, testName + testNum); | |
testNum = 1; | |
testName = " difference tests sorted: "; | |
final AbstractSet<String> sSet1 = new SortedSet<>(); | |
final AbstractSet<String> sSet2 = new SortedSet<>(); | |
ISet<String> sSet3; | |
final AbstractSet<String> sSet4 = new SortedSet<>(); | |
final AbstractSet<String> sSet5 = new SortedSet<>(); | |
sSet1.add("ROY'S"); | |
sSet1.add("OUR"); | |
sSet1.add("BOY"); | |
sSet2.add("BULBSAUR"); | |
sSet2.add("SQUIRTLE"); | |
sSet2.add("ROY'S"); | |
sSet3 = sSet1.difference(sSet2); | |
sSet4.add("OUR"); | |
sSet4.add("BOY"); | |
assertEquals(sSet3.equals(sSet4), true, testName + testNum); | |
testNum++; | |
sSet3 = sSet2.difference(sSet1); | |
sSet5.add("BULBSAUR"); | |
sSet5.add("SQUIRTLE"); | |
assertEquals(sSet3.equals(sSet5), true, testName + testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() | |
{ | |
sSet1.difference(null); | |
} | |
}, testName + testNum); | |
} | |
private static void clearTests() { | |
String testName = "clear tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset2 = new UnsortedSet<String>(); | |
uset1.add("ROY'S"); | |
uset1.add("OUR"); | |
uset1.add("BOY"); | |
uset2.add("BULBSAUR"); | |
uset2.add("SQUIRTLE"); | |
uset2.add("CHARMANDER"); | |
uset1.clear(); | |
assertEquals(uset1.size() == 0, true, testName + testNum); | |
testNum++; | |
uset2.clear(); | |
assertEquals(uset2.size() == 0, true, testName + testNum); | |
testNum++; | |
testName = " clear tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sSet1 = new SortedSet<>(); | |
final AbstractSet<String> sSet2 = new SortedSet<>(); | |
sSet1.add("A"); | |
sSet1.add("B"); | |
sSet1.add("Hey"); | |
sSet1.clear(); | |
assertEquals(sSet1.size() == 0, true, testName + testNum); | |
testNum++; | |
sSet2.add("C"); | |
sSet2.clear(); | |
assertEquals(sSet2.size() == 0, true, testName + testNum); | |
} | |
private static void addAllTests() { | |
String testName = "addAll tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset2 = new UnsortedSet<String>(); | |
uset2.add("BULBASAUR"); | |
uset2.add("SQUIRTLE"); | |
uset2.add("CHARMANDER"); | |
boolean added = uset1.addAll(uset2); | |
assertEquals(added && uset1.containsAll(uset2), true, testName + testNum); | |
testNum++; | |
added = uset1.addAll(uset2); | |
assertEquals(added && uset1.containsAll(uset2), false, testName + testNum); | |
testNum++; | |
uset2.add("PIKACHU"); | |
added = uset1.addAll(uset2); | |
assertEquals(added && uset1.containsAll(uset2), true, testName + testNum); | |
testNum++; | |
uset1.add("YOLOGUCCISWAG"); | |
added = uset2.addAll(uset1); | |
assertEquals(added && uset2.containsAll(uset1), true, testName+ testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() { | |
uset1.addAll(null); | |
} | |
}, testName + testNum); | |
testName = "addAll tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sset1 = new SortedSet<String>(); | |
final AbstractSet<String> sset2 = new SortedSet<String>(); | |
assertEquals(sset1.addAll(sset2), false, testName + testNum); | |
testNum++; | |
sset2.add("A"); | |
assertEquals(sset1.addAll(sset2), true, testName + testNum); | |
testNum++; | |
assertEquals(sset1.addAll(sset1), false, testName + testNum); | |
testNum++; | |
sset2.clear(); | |
assertEquals(sset1.addAll(sset2), false, testName+ testNum); | |
testNum++; | |
assertException(new Executable() { | |
public void run() { | |
sset1.addAll(null); | |
} | |
}, testName + testNum); | |
} | |
private static void addTests() { | |
String testName = "add tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
boolean added = uset1.add("PIKACHU"); | |
assertEquals(added && uset1.contains("PIKACHU"), true, testName + testNum); | |
testNum++; | |
added = uset1.add("PIKACHU"); | |
assertEquals(added && uset1.contains("PIKACHU"), false, testName + testNum); | |
testNum++; | |
added = uset1.add("BULBASAUR"); | |
assertEquals(added && uset1.contains("BULBASAUR"), true, testName + testNum); | |
testNum++; | |
added = uset1.add("CHARIZARD"); | |
assertEquals(added && uset1.contains("CHARIZARD"), true, testName+ testNum); | |
testNum++; | |
assertException(new Executable() | |
{ | |
public void run() { | |
uset1.add(null); | |
} | |
}, testName + testNum); | |
testName = "add tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sset1 = new SortedSet<String>(); | |
final AbstractSet<Integer> sset2 = new SortedSet<Integer>(); | |
assertEquals(sset1.add("A"), true, testName + testNum); | |
testNum++; | |
assertEquals(sset1.add("A"), false, testName + testNum); | |
testNum++; | |
assertEquals(sset1.add("B"), true, testName + testNum); | |
testNum++; | |
assertEquals(sset1.add("C"), true, testName+ testNum); | |
testNum++; | |
assertException(new Executable() { | |
public void run() { | |
sset1.add(null); | |
} | |
}, testName + testNum); | |
} | |
private static void maxTests() { | |
String testName = "max tests sorted"; | |
int testNum = 1; | |
final SortedSet<String> sSet = new SortedSet<String>(); | |
sSet.add("A"); | |
assertEquals(sSet.max(), "A", testName + testNum); | |
testNum++; | |
sSet.add("A"); | |
assertEquals(sSet.max(), "A", testName + testNum); | |
testNum++; | |
sSet.add("B"); | |
assertEquals(sSet.max(), "B", testName + testNum); | |
testNum++; | |
sSet.clear(); | |
sSet.add("C"); | |
sSet.add("B"); | |
assertEquals(sSet.max(), "C", testName + testNum); | |
testNum++; | |
sSet.clear(); | |
assertException(new Executable() { | |
public void run() { | |
sSet.max(); | |
} | |
}, testName + testNum); | |
} | |
private static void minTests() { | |
String testName = "min tests sorted "; | |
int testNum = 1; | |
final SortedSet<String> sSet = new SortedSet<String>(); | |
sSet.add("A"); | |
assertEquals(sSet.min(), "A", testName + testNum); | |
testNum++; | |
sSet.add("A"); | |
assertEquals(sSet.min(), "A", testName + testNum); | |
testNum++; | |
sSet.add("B"); | |
assertEquals(sSet.min(), "A", testName + testNum); | |
testNum++; | |
sSet.clear(); | |
sSet.add("C"); | |
sSet.add("B"); | |
assertEquals(sSet.min(), "B", testName + testNum); | |
testNum++; | |
sSet.clear(); | |
assertException(new Executable() { | |
public void run() { | |
sSet.min(); | |
} | |
}, testName + testNum); | |
} | |
private static void containsTests(){ | |
String testName = "contains tests unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<Integer> uset2 = new UnsortedSet<Integer>(); | |
//size 0 | |
assertEquals(uset1.contains("A"), false, testName + testNum); | |
testNum++; | |
uset1.add("A"); | |
assertEquals(uset1.contains("A"), true, testName + testNum); | |
testNum++; | |
uset1.add("B"); | |
assertEquals(uset1.contains("B"), true, testName + testNum); | |
testNum++; | |
assertEquals(uset1.contains("C"), false, testName+ testNum); | |
testNum++; | |
uset2.add(1); | |
assertEquals(uset2.contains(1), true, testName + testNum); | |
testNum++; | |
assertException(new Executable() { | |
public void run() { | |
uset1.contains(null); | |
} | |
}, testName + testNum); | |
testName = "contains tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sset1 = new SortedSet<String>(); | |
final AbstractSet<Integer> sset2 = new SortedSet<Integer>(); | |
assertEquals(sset1.contains("A"), false, testName + testNum); | |
testNum++; | |
sset1.add("A"); | |
assertEquals(sset1.contains("A"), true, testName + testNum); | |
testNum++; | |
sset1.add("B"); | |
assertEquals(sset1.contains("B"), true, testName + testNum); | |
testNum++; | |
assertEquals(sset1.contains("C"), false, testName+ testNum); | |
testNum++; | |
sset2.add(1); | |
assertEquals(sset2.contains(1), true, testName + testNum); | |
testNum++; | |
assertException(new Executable() { | |
public void run() { | |
sset1.contains(null); | |
} | |
}, testName + testNum); | |
} | |
private static void containsAllTests(){ | |
String testName = "containsAll test unsorted: "; | |
int testNum = 1; | |
final AbstractSet<String> uset1 = new UnsortedSet<String>(); | |
final AbstractSet<String> uset2 = new UnsortedSet<String>(); | |
//size 0 | |
uset2.add("A"); | |
assertEquals(uset1.containsAll(uset2), false, testName + testNum); | |
testNum++; | |
uset1.add("A"); | |
assertEquals(uset1.containsAll(uset2), true, testName + testNum); | |
testNum++; | |
assertEquals(uset2.containsAll(uset1), true, testName + testNum); | |
testNum++; | |
uset1.add("B"); | |
assertEquals(uset1.containsAll(uset2), true, testName + testNum); | |
testNum++; | |
assertEquals(uset2.containsAll(uset1), false, testName+ testNum); | |
testNum++; | |
assertException(new Executable() { | |
public void run() { | |
uset1.containsAll(null); | |
} | |
}, testName + testNum); | |
testName = "containsAll tests sorted: "; | |
testNum = 1; | |
final AbstractSet<String> sset1 = new SortedSet<String>(); | |
final AbstractSet<String> sset2 = new SortedSet<String>(); | |
sset2.add("A"); | |
assertEquals(sset1.containsAll(sset2), false, testName + testNum); | |
testNum++; | |
sset1.add("A"); | |
assertEquals(sset1.containsAll(sset2), true, testName + testNum); | |
testNum++; | |
assertEquals(sset2.containsAll(sset1), true, testName + testNum); | |
testNum++; | |
sset1.add("B"); | |
assertEquals(sset1.containsAll(sset2), true, testName + testNum); | |
testNum++; | |
assertEquals(sset2.containsAll(sset1), false, testName+ testNum); | |
testNum++; | |
assertException(new Executable() { | |
public void run() { | |
sset1.containsAll(null); | |
} | |
}, testName + testNum); | |
} | |
private static interface Executable { void run(); } | |
private static void assertException(Executable exec, String testName) { | |
try { | |
exec.run(); | |
System.out.println("*******Failed test: " + testName); | |
System.out.println(" Expected a thrown exception"); | |
throw new RuntimeException("Failed test: " + testName); | |
} catch (Exception ex) { | |
System.out.println("Passed test: " + testName); | |
} | |
} | |
private static void assertEquals(Object o1, Object o2, String testName) { | |
if (!o1.equals(o2)) { | |
System.out.println("*******Failed test: " + testName); | |
System.out.println(" " + o1 + " does not equal " + o2); | |
throw new RuntimeException("Failed test: " + testName); | |
} | |
System.out.println("Passed test: " + testName); | |
} | |
/* | |
* Method asks user for file and compares run times to add words from file to | |
* various sets, including CS314 UnsortedSet and SortedSet and Java's | |
* TreeSet and HashSet. | |
*/ | |
private static void largeTest(){ | |
System.out.println(); | |
System.out.println("Opening Window to select file. You may have to minimize other windows."); | |
String text = convertFileToString(); | |
System.out.println(); | |
System.out.println("***** CS314 SortedSet: *****"); | |
processTextCS314Sets(new SortedSet<String>(), text); | |
System.out.println("****** CS314 UnsortedSet: *****"); | |
processTextCS314Sets(new UnsortedSet<String>(), text); | |
System.out.println("***** Java HashSet ******"); | |
processTextJavaSets( new HashSet<String>(), text); | |
System.out.println("***** Java TreeSet ******"); | |
processTextJavaSets( new TreeSet<String>(), text); | |
} | |
/* | |
* pre: set != null, text != null | |
* Method to add all words in text to the given set. Words are delimited by | |
* white space. | |
* This version for CS314 sets. | |
*/ | |
private static void processTextCS314Sets(ISet<String> set, String text){ | |
Stopwatch s = new Stopwatch(); | |
Scanner sc = new Scanner(text); | |
int totalWords = 0; | |
s.start(); | |
while( sc.hasNext() ){ | |
totalWords++; | |
set.add(sc.next()); | |
} | |
s.stop(); | |
sc.close(); | |
showResultsAndWords(set, s, totalWords, set.size()); | |
} | |
/* | |
* pre: set != null, text != null | |
* Method to add all words in text to the given set. Words are delimited by | |
* white space. | |
* This version for Java Sets. | |
*/ | |
private static void processTextJavaSets(Set<String> set, String text){ | |
Stopwatch s = new Stopwatch(); | |
Scanner sc = new Scanner(text); | |
int totalWords = 0; | |
s.start(); | |
while( sc.hasNext() ){ | |
totalWords++; | |
set.add(sc.next()); | |
} | |
s.stop(); | |
sc.close(); | |
showResultsAndWords(set, s, totalWords, set.size()); | |
} | |
/* | |
* Show results of add words to given set. | |
*/ | |
private static <E> void showResultsAndWords(Iterable<E> set, Stopwatch s, | |
int totalWords, int setSize) { | |
System.out.println("Time to add the elements in the text to this set: " + s.toString() ); | |
System.out.println("Total number of words in text including duplicates: " + totalWords); | |
System.out.println("Number of distinct words in this text " + setSize); | |
System.out.print("Enter y to see the contents of this set: "); | |
Scanner sc = new Scanner(System.in); | |
String response = sc.next(); | |
if( response != null && response.length() > 0 && response.substring(0,1).equalsIgnoreCase("y") ){ | |
for(Object o : set) | |
System.out.println(o); | |
} | |
System.out.println(); | |
} | |
/* | |
* Ask user to pick a file via a file choosing window and | |
* convert that file to a String. Since we are evalutatin the file | |
* with many sets convert to string once instead of reading through | |
* file multiple times. | |
*/ | |
private static String convertFileToString() { | |
//create a GUI window to pick the text to evaluate | |
JFileChooser chooser = new JFileChooser("."); | |
StringBuilder text = new StringBuilder(); | |
int retval = chooser.showOpenDialog(null); | |
chooser.grabFocus(); | |
//read in the file | |
if (retval == JFileChooser.APPROVE_OPTION) { | |
File source = chooser.getSelectedFile(); | |
try { | |
Scanner s = new Scanner( new FileReader( source ) ); | |
while( s.hasNextLine() ) { | |
text.append( s.nextLine() ); | |
text.append(" "); | |
} | |
s.close(); | |
} | |
catch(IOException e) { | |
System.out.println("An error occured while trying to read from the file: " + e); | |
} | |
} | |
return text.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment