SE251Ex:Generic Pair TestSetOperations
Copy and paste the code below.
import java.util.HashSet; import java.util.Set; import junit.framework.TestCase; public class TestSetOperations extends TestCase { private Set<String> set1, set2, set3, set4; protected void setUp() { set1 = new HashSet<String>(); set1.add("a"); set1.add("b"); set1.add("c"); set1.add("d"); set1.add("e"); set2 = new HashSet<String>(); set2.add("b"); set2.add("d"); set2.add("g"); set2.add("z"); set3 = new HashSet<String>(); set3.add("b"); set3.add("d"); set3.add("g"); set3.add("x"); set3.add("y"); set3.add("z"); set4 = new HashSet<String>(); set4.add("g"); set4.add("x"); set4.add("h"); set4.add("z"); } public void testUnionMutuallyExclusive() { Set<String> union = SetOperations.union(set1, set4); assertEquals(9, union.size()); assertTrue(union.containsAll(set1)); assertTrue(union.containsAll(set4)); } public void testUnionNonME() { Set<String> union = SetOperations.union(set1, set2); assertEquals(7, union.size()); assertTrue(union.containsAll(set1)); assertTrue(union.containsAll(set2)); } public void testIntersectionMutuallyExclusive() { Set<String> intersection = SetOperations.intersection(set1, set4); assertTrue(intersection.isEmpty()); } public void testIntersectionNonME() { Set<String> intersection = SetOperations.intersection(set3, set4); assertEquals(3, intersection.size()); assertTrue(intersection.contains("g")); assertTrue(intersection.contains("x")); assertTrue(intersection.contains("z")); } public void testDifferenceMutuallyExclusive() { Set<String> diff1 = SetOperations.difference(set1, set4); assertEquals(5, diff1.size()); assertTrue(diff1.containsAll(set1)); Set<String> diff4 = SetOperations.difference(set4, set1); assertEquals(4, diff4.size()); assertTrue(diff4.containsAll(set4)); } public void testDifferenceNonME() { Set<String> diff2 = SetOperations.difference(set2, set3); assertTrue(diff2.isEmpty()); Set<String> diff3 = SetOperations.difference(set3, set2); assertEquals(2, diff3.size()); assertTrue(diff3.contains("x")); assertTrue(diff3.contains("y")); } public void testCartesianProduct() { Set<Pair<String,String>> product = SetOperations.cartesianProduct(set1, set4); assertEquals(set1.size() * set4.size(), product.size()); assertTrue(product.contains(new Pair<String,String>("a","g"))); assertTrue(product.contains(new Pair<String,String>("a","x"))); assertTrue(product.contains(new Pair<String,String>("a","h"))); assertTrue(product.contains(new Pair<String,String>("a","z"))); assertTrue(product.contains(new Pair<String,String>("b","g"))); assertTrue(product.contains(new Pair<String,String>("b","x"))); assertTrue(product.contains(new Pair<String,String>("b","h"))); assertTrue(product.contains(new Pair<String,String>("b","z"))); assertTrue(product.contains(new Pair<String,String>("c","g"))); assertTrue(product.contains(new Pair<String,String>("c","x"))); assertTrue(product.contains(new Pair<String,String>("c","h"))); assertTrue(product.contains(new Pair<String,String>("c","z"))); assertTrue(product.contains(new Pair<String,String>("d","g"))); assertTrue(product.contains(new Pair<String,String>("d","x"))); assertTrue(product.contains(new Pair<String,String>("d","h"))); assertTrue(product.contains(new Pair<String,String>("d","z"))); assertTrue(product.contains(new Pair<String,String>("e","g"))); assertTrue(product.contains(new Pair<String,String>("e","x"))); assertTrue(product.contains(new Pair<String,String>("e","h"))); assertTrue(product.contains(new Pair<String,String>("e","z"))); } }