* Tests of the SoftSet
* @throws Exception
*/
public void testSoftValueSet() throws Exception
{
SoftSet set = new SoftSet();
StringBuffer akey = new StringBuffer("Key#");
for(int n = 0; n < 1000; n ++)
{
String key = "Key#"+n;
set.add(key);
assertTrue("set has key", set.contains(key));
akey.setLength(4);
akey.append(""+n);
String key2 = akey.toString();
assertEquals(key, key2);
assertEquals(key.hashCode(), key2.hashCode());
assertTrue("set has akey", set.contains(key2));
}
assertEquals("Size == 1000", 1000, set.size());
assertEquals("Set.isEmpty is false", false, set.isEmpty());
String[] keys = new String[1000];
set.toArray(keys);
for(int n = 0; n < 1000; n ++)
{
String key = keys[n];
assertTrue("set has key", set.contains(key));
}
HashSet set2 = new HashSet();
set2.add("Key#1000");
assertFalse("set has not Key#1000", set.contains("Key#1000"));
assertTrue("Key#1000 was added", set.addAll(set2));
assertEquals("Size == 1001", 1001, set.size());
assertTrue("Key#1000 was removed", set.removeAll(set2));
assertEquals("Size == 1000", 1000, set.size());
set.add("Key#1000");
assertTrue("Key#1000 was removed", set.retainAll(set2));
assertEquals("Size == 1", 1, set.size());
assertTrue("set contains [Key#1000]", set.containsAll(set2));
set.clear();
assertEquals("Size == 0", 0, set.size());
assertEquals("Size is empty", true, set.isEmpty());
for(int n = 0; n < 1000; n ++)
{
String key = keys[n];
set.add(key);
assertTrue("set has key", set.contains(key));
}
for(int n = 0; n < 1000; n ++)
{
String key = "Key#"+n;
set.remove(key);
assertFalse("key was removed", set.contains(key));
}
for(int n = 0; n < 1000; n ++)
{
String key = "Key#"+n;
set.add(key);
assertTrue("set has key", set.contains(key));
}
Iterator iter = set.iterator();
while( iter.hasNext() )
{
Object o = iter.next();
assertTrue("o instanceof String", o instanceof String);
}
forceSoftRefCollection();
assertEquals("Size == 0 after gc", 0, set.size());
}