Created
November 3, 2016 19:50
-
-
Save shreydesai/aee9fd06c83e06122071b0f6c7d69fcf to your computer and use it in GitHub Desktop.
CS 314 Assignment 9 Tests
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
public class SetTester { | |
/** | |
* Runs tests on primitive data types | |
* pre: exp != null, actual != null, test != null | |
* post: output test failure or success | |
*/ | |
private static boolean test(Object exp, Object actual, String test) { | |
String output = null; | |
if (exp.equals(actual)) { | |
output = "✔ Passed " + test; | |
System.out.println(output + "\n"); | |
return true; | |
} | |
output = "***** FAILED ***** " + test + "\n" + | |
"Expected: " + exp + ", Actual: " + actual; | |
System.out.println(output + "\n"); | |
return false; | |
} | |
public static void main(String[] args){ | |
ISet<Integer> u1, u2, u3; | |
boolean changed; | |
// UnsortedSet - add | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
u1.add(3); | |
test(3, u1.size(), "UnsortedSet add Test 1a"); | |
test("(1, 2, 3)", u1.toString(), "UnsortedSet add Test 1b"); | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(3); | |
u1.add(2); | |
u1.add(2); | |
u1.add(3); | |
u1.add(2); | |
u1.add(1); | |
test(3, u1.size(), "UnsortedSet add Test 2a"); | |
test("(3, 2, 1)", u1.toString(), "Unsorted add Test 2b"); | |
// UnsortedSet - addAll | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
u1.add(3); | |
u2 = new UnsortedSet<Integer>(); | |
u2.add(3); | |
u2.add(4); | |
u2.add(5); | |
u1.addAll(u2); | |
test(5, u1.size(), "UnsortedSet addAll Test 1a"); | |
test("(1, 2, 3, 4, 5)", u1.toString(), "Unsorted addAll Test 1b"); | |
u1 = new UnsortedSet<Integer>(); | |
u1.addAll(u2); | |
test(3, u1.size(), "UnsortedSet addAll Test 2a"); | |
u1 = new UnsortedSet<Integer>(); | |
u2 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
u1.add(3); | |
u2.add(1); | |
u2.add(2); | |
u2.add(3); | |
changed = u1.addAll(u2); | |
test(false, changed, "UnsortedSet addAll Test 2b"); | |
// UnsortedSet - clear | |
u1 = new UnsortedSet<Integer>(); | |
for (int i = 0; i < 100; i++) { | |
u1.add(i); | |
} | |
test(100, u1.size(), "UnsortedSet clear Test 1a"); | |
u1.clear(); | |
test(0, u1.size(), "UnsortedSet clear Test 1b"); | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
u2 = new UnsortedSet<Integer>(); | |
u2.add(1); | |
u2.add(2); | |
u2.add(3); | |
u1.addAll(u2); | |
test(3, u1.size(), "UnsortedSet clear Test 2a"); | |
u1.clear(); | |
test(0, u1.size(), "UnsortedSet clear Test 2b"); | |
// UnsortedSet - contains | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
u1.add(3); | |
test(true, u1.contains(1), "UnsortedSet contains Test 1a"); | |
test(true, u1.contains(2), "UnsortedSet contains Test 1b"); | |
test(true, u1.contains(3), "UnsortedSet contains Test 1c"); | |
test(false, u1.contains(4), "UnsortedSet contains Test 1d"); | |
// UnsortedSet - containsAll | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
u1.add(3); | |
u2 = new UnsortedSet<Integer>(); | |
test(true, u1.containsAll(u2), "UnsortedSet containsAll Test 1a"); | |
u2.add(1); | |
u2.add(2); | |
u2.add(3); | |
test(true, u1.containsAll(u2), "UnsortedSet containsAll Test 2a"); | |
u1 = new UnsortedSet<Integer>(); | |
u2 = new UnsortedSet<Integer>(); | |
test(true, u1.containsAll(u2), "UnsortedSet containsAll Test 2b"); | |
// UnsortedSet - difference | |
u1 = new UnsortedSet<Integer>(); | |
for (int i = 0; i < 5; i++) { | |
u1.add(i); | |
} | |
u2 = new UnsortedSet<Integer>(); | |
for (int i = 3; i < 5; i++) { | |
u2.add(i); | |
} | |
u3 = u1.difference(u2); | |
test(3, u3.size(), "UnsortedSet difference Test 1a"); | |
test(true, u3.contains(0), "UnsortedSet difference Test 1b"); | |
test(true, u3.contains(1), "UnsortedSet difference Test 1c"); | |
test(true, u3.contains(2), "UnsortedSet difference Test 1d"); | |
u1 = new UnsortedSet<Integer>(); | |
u2 = new UnsortedSet<Integer>(); | |
u3 = u1.difference(u3); | |
test(0, u3.size(), "UnsortedSet difference Test 2a"); | |
// UnsortedSet - equals | |
u1 = new UnsortedSet<Integer>(); | |
u2 = new UnsortedSet<Integer>(); | |
test(true, u1.equals(u2), "UnsortedSet equals Test 1a"); | |
u2.add(1); | |
test(false, u1.equals(u2), "UnsortedSet equals Test 1b"); | |
test(false, u1.equals(null), "UnsortedSet equals Test 2a"); | |
test(false, u2.equals(new String()), "UnsortedSet equals Test 2b"); | |
// UnsortedSet - intersection | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
u1.add(3); | |
u1.add(4); | |
u2 = new UnsortedSet<Integer>(); | |
u2.add(3); | |
u2.add(4); | |
u3 = u1.intersection(u2); | |
test(2, u3.size(), "UnsortedSet intersection Test 1a"); | |
u1 = new UnsortedSet<Integer>(); | |
u2 = new UnsortedSet<Integer>(); | |
u3 = u1.intersection(u2); | |
test(0, u3.size(), "UnsortedSet intersection Test 2a"); | |
// UnsortedSet - iterator | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
Iterator<Integer> it = u1.iterator(); | |
it.next(); | |
test(false, it.hasNext(), "UnsortedSet iterator Test 1a"); | |
// UnsortedSet - remove | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
test(2, u1.size(), "UnsortedSet remove Test 1a"); | |
u1.remove(1); | |
test(1, u1.size(), "UnsortedSet remove Test 1b"); | |
test("(2)", u1.toString(), "Unsorted remove Test 1c"); | |
// UnsortedSet - size | |
u1 = new UnsortedSet<Integer>(); | |
for (int i = 0; i < 100; i++) { | |
u1.add(i); | |
} | |
test(100, u1.size(), "UnsortedSet size Test 1a"); | |
u1.clear(); | |
test(0, u1.size(), "UnsortedSet size Test 2a"); | |
// UnsortedSet - union | |
u1 = new UnsortedSet<Integer>(); | |
u1.add(1); | |
u1.add(2); | |
u1.add(3); | |
u2 = new UnsortedSet<Integer>(); | |
u2.add(1); | |
u2.add(2); | |
u2.add(3); | |
u3 = u1.union(u2); | |
test(3, u3.size(), "UnsortedSet union Test 1a"); | |
test(true, u3.equals(u1), "UnsortedSet union Test 1b"); | |
test(true, u3.equals(u2), "UnsortedSet union Test 1c"); | |
u2 = new UnsortedSet<Integer>(); | |
u2.add(4); | |
u2.add(5); | |
u2.add(6); | |
u3 = u1.union(u2); | |
test(6, u3.size(), "UnsortedSet union Test 2a"); | |
test("(1, 2, 3, 4, 5, 6)", u3.toString(), "UnsortedSet union Test 2b"); | |
ISet<Integer> s1, s2, s3; | |
// SortedSet - add | |
s1 = new SortedSet<Integer>(); | |
s1.add(1); | |
s1.add(2); | |
s1.add(2); | |
s1.add(3); | |
s1.add(1); | |
s1.add(0); | |
test(4, s1.size(), "SortedSet add Test 1a"); | |
test("(0, 1, 2, 3)", s1.toString(), "SortedSet add Test 1b"); | |
// SortedSet - addAll | |
s1 = new SortedSet<Integer>(); | |
s1.add(0); | |
s1.add(2); | |
s1.add(4); | |
s2 = new SortedSet<Integer>(); | |
s1.add(1); | |
s1.add(3); | |
s1.add(5); | |
s1.addAll(s2); | |
test(6, s1.size(), "SortedSet addAll Test 1a"); | |
test("(0, 1, 2, 3, 4, 5)", s1.toString(), "SortedSet addAll Test 1b"); | |
s1 = new SortedSet<Integer>(); | |
s1.add(0); | |
s1.add(2); | |
s1.add(4); | |
s2 = new SortedSet<Integer>(); | |
s2.add(0); | |
s2.add(1); | |
s2.add(2); | |
s1.addAll(s2); | |
test(4, s1.size(), "SortedSet addAll Test 2a"); | |
// SortedSet - clear | |
s1 = new SortedSet<Integer>(); | |
for (int i = 0; i < 100; i++) { | |
s1.add(i); | |
} | |
test(100, s1.size(), "SortedSet clear Test 1a"); | |
s1.clear(); | |
test(0, s1.size(), "SortedSet clear Test 1b"); | |
// SortedSet - contains | |
s1 = new SortedSet<Integer>(); | |
s1.add(0); | |
s1.add(2); | |
s1.add(4); | |
test(false, s1.contains(-1), "SortedSet contains Test 1a"); | |
test(true, s1.contains(2), "SortedSet contains Test 1b"); | |
s2 = new SortedSet<Integer>(); | |
s2.add(1); | |
s1.addAll(s2); | |
test(true, s1.contains(1), "SortedSet contains Test 2a"); | |
// SortedSet - containsAll | |
s1 = new SortedSet<Integer>(); | |
s1.add(1); | |
s1.add(2); | |
s1.add(3); | |
s2 = new SortedSet<Integer>(); | |
s2.add(1); | |
s2.add(2); | |
s2.add(3); | |
test(true, s1.containsAll(s2), "SortedSet containsAll Test 1a"); | |
s2 = new SortedSet<Integer>(); | |
for (int i = 0; i < 5; i++) { | |
s2.add(i); | |
} | |
test(false, s1.containsAll(s2), "SortedSet containsAll Test 2a"); | |
s2 = new SortedSet<Integer>(); | |
s2.add(0); | |
s2.add(1); | |
s2.add(2); | |
test(false, s1.containsAll(s2), "SortedSet containsAll Test 3a"); | |
s2.remove(0); | |
test(true, s1.containsAll(s2), "SortedSet containsAll Test 3b"); | |
// SortedSet - difference | |
s1 = new SortedSet<Integer>(); | |
s2 = new SortedSet<Integer>(); | |
s3 = s1.difference(s2); | |
test("()", s3.toString(), "SortedSet difference Test 1a"); | |
s1.add(1); | |
s1.add(2); | |
s2.add(1); | |
s2.add(2); | |
s2.add(3); | |
s3 = s1.difference(s2); | |
test("()", s3.toString(), "SortedSet difference Test 2a"); | |
s2.clear(); | |
s3 = s1.difference(s2); | |
test("(1, 2)", s3.toString(), "SortedSet difference Test 2b"); | |
s1 = new SortedSet<Integer>(); | |
for (int i = 0; i < 4; i++) { | |
s1.add(i); | |
} | |
s2 = new SortedSet<Integer>(); | |
s3 = s1.difference(s2); | |
test("(0, 1, 2, 3)", s3.toString(), "SortedSet difference Test 2c"); | |
// SortedSet - equals | |
s1 = new SortedSet<Integer>(); | |
s2 = new SortedSet<Integer>(); | |
test(true, s1.equals(s2), "SortedSet equals Test 1a"); | |
s1 = new SortedSet<Integer>(); | |
s2 = new SortedSet<Integer>(); | |
for (int i = 0; i < 100; i++) { | |
s1.add(i); | |
s2.add(i); | |
} | |
test(true, s1.equals(s2), "SortedSet equals Test 2a"); | |
s2.add(0); | |
test(true, s1.equals(s2), "SortedSet equals Test 2b"); | |
s1 = new SortedSet<Integer>(); | |
s1.add(1); | |
s1.add(2); | |
s1.add(3); | |
s2 = new SortedSet<Integer>(); | |
s2.add(1); | |
s2.add(3); | |
s2.add(3); | |
test(false, s1.equals(s2), "SortedSet equals Test 3a"); | |
// SortedSet - iterator | |
s1 = new SortedSet<Integer>(); | |
s1.add(1); | |
it = s1.iterator(); | |
it.next(); | |
test(false, it.hasNext(), "SortedSet iterator Test 1a"); | |
// SortedSet - remove | |
s1 = new SortedSet<Integer>(); | |
s1.add(2); | |
changed = s1.remove(0); | |
test(false, changed, "SortedSet remove Test 1a"); | |
changed = s1.remove(2); | |
test(true, changed, "SortedSet remove Test 1b"); | |
// SortedSet - size | |
s1 = new SortedSet<Integer>(); | |
for (int i = 0; i < 100; i++) { | |
s1.add(i); | |
} | |
test(100, s1.size(), "SortedSet size Test 1a"); | |
// SortedSet - union | |
s1 = new SortedSet<Integer>(); | |
s2 = new SortedSet<Integer>(); | |
s2.add(1); | |
s2.add(2); | |
s3 = s1.union(s2); | |
test("(1, 2)", s3.toString(), "SortedSet union Test 1a"); | |
s1.add(1); | |
s1.add(1); | |
s1.add(2); | |
s3 = s1.union(s2); | |
test("(1, 2)", s3.toString(), "SortedSet union Test 1b"); | |
s1.add(3); | |
s3 = s1.union(s2); | |
test("(1, 2, 3)", s3.toString(), "SortedSet union Test 1c"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment