001 /* 002 * Created on Sep 20, 2004 003 * 004 */ 005 package aima.test.utiltest; 006 007 import java.util.ArrayList; 008 import java.util.HashSet; 009 import java.util.List; 010 import java.util.Set; 011 012 import junit.framework.TestCase; 013 import aima.util.SetOps; 014 015 /** 016 * @author Ravi Mohan 017 * 018 */ 019 020 public class SetTest extends TestCase { 021 Set<Integer> s1, s2; 022 023 @Override 024 public void setUp() { 025 s1 = new HashSet<Integer>(); 026 s1.add(new Integer(1)); 027 s1.add(new Integer(2)); 028 s1.add(new Integer(3)); 029 s1.add(new Integer(4)); 030 031 s2 = new HashSet<Integer>(); 032 s2.add(new Integer(4)); 033 s2.add(new Integer(5)); 034 s2.add(new Integer(6)); 035 } 036 037 public void testUnion() { 038 Set<Integer> union = new SetOps<Integer>().union(s1, s2); 039 assertEquals(6, union.size()); 040 assertEquals(4, s1.size()); 041 assertEquals(3, s2.size()); 042 043 s1.remove(new Integer(1)); 044 assertEquals(6, union.size()); 045 assertEquals(3, s1.size()); 046 assertEquals(3, s2.size()); 047 } 048 049 public void testIntersection() { 050 Set<Integer> intersection = new SetOps<Integer>().intersection(s1, s2); 051 assertEquals(1, intersection.size()); 052 assertEquals(4, s1.size()); 053 assertEquals(3, s2.size()); 054 055 s1.remove(new Integer(1)); 056 assertEquals(1, intersection.size()); 057 assertEquals(3, s1.size()); 058 assertEquals(3, s2.size()); 059 } 060 061 public void testDifference() { 062 Set<Integer> difference = new SetOps<Integer>().difference(s1, s2); 063 assertEquals(3, difference.size()); 064 assertTrue(difference.contains(new Integer(1))); 065 assertTrue(difference.contains(new Integer(2))); 066 assertTrue(difference.contains(new Integer(3))); 067 068 } 069 070 public void testDifference2() { 071 Set<Integer> one = new HashSet<Integer>(); 072 Set<Integer> two = new HashSet<Integer>(); 073 one.add(new Integer(1)); 074 two.add(new Integer(1)); 075 Set difference = new SetOps<Integer>().difference(one, two); 076 assertTrue(difference.isEmpty()); 077 078 } 079 080 public void testListRemove() { 081 List<Integer> one = new ArrayList<Integer>(); 082 one.add(new Integer(1)); 083 assertEquals(1, one.size()); 084 one.remove(0); 085 assertEquals(0, one.size()); 086 087 } 088 089 }