1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
package gnu.trove.impl.hash;
import junit.framework.TestCase;
import gnu.trove.set.hash.THashSet;
import gnu.trove.map.hash.THashMap;
import gnu.trove.map.hash.TIntLongHashMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.map.hash.TObjectIntHashMap;
import gnu.trove.map.TIntLongMap;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.TObjectIntMap;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
/**
* tests that need access to internals of THash or THashSet
*/
public class THashTest extends TestCase {
public THashTest( String name ) {
super( name );
}
public void setUp() throws Exception {
super.setUp();
}
public void tearDown() throws Exception {
super.tearDown();
}
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
public void testNormalLoad() throws Exception {
THashSet<Integer> set = new THashSet<Integer>( 11, 0.5f );
assertEquals( set._maxSize, 11 );
for ( int i = 0; i < 12; i++ ) {
set.add( new Integer( i ) );
}
assertTrue( set._maxSize > 12 );
}
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
public void testMaxLoad() throws Exception {
THashSet<Integer> set = new THashSet<Integer>( 11, 1.0f );
assertEquals( 10, set._maxSize );
for ( int i = 0; i < 12; i++ ) {
set.add( new Integer( i ) );
}
assertTrue( set._maxSize > 12 );
}
public void testReusesRemovedSlotsOnCollision() {
THashSet<Object> set = new THashSet<Object>( 11, 0.5f );
class Foo {
public int hashCode() {
return 4;
}
}
Foo f1 = new Foo();
Foo f2 = new Foo();
Foo f3 = new Foo();
set.add( f1 );
int idx = set.insertionIndex( f2 );
set.add( f2 );
assertEquals( f2, set._set[idx] );
set.remove( f2 );
assertEquals( THashSet.REMOVED, set._set[idx] );
assertEquals( idx, set.insertionIndex( f3 ) );
set.add( f3 );
assertEquals( f3, set._set[idx] );
}
public void testCompact() throws Exception {
THashMap<Integer,Integer> map = new THashMap<Integer,Integer>();
Integer[] data = new Integer[1000];
for (int i = 0; i < 1000; i++) {
data[i] = new Integer(i);
map.put(data[i], data[i]);
}
assertTrue(map._maxSize > 1000);
for (int i = 0; i < 1000; i+=2) {
// try {
map.remove(data[i]);
// }
// catch( RuntimeException ex ) {
// System.err.println("Error on i: " + i);
// System.out.println("Hash codes:");
// for( int j = 0 ; j < data.length; j++ ) {
// if ( ( j % 8 ) == 0 ) {
// System.out.println(",");
// }
// else System.out.print(",");
// System.out.print(map._hashingStrategy.computeHashCode(data[j]));
// }
//
//
// System.out.println("Remove:");
// for( int j = 0 ; j <= i; j+=2 ) {
// if ( ( j % 8 ) == 0 ) {
// System.out.println(",");
// }
// else System.out.print(",");
// System.out.print(map._hashingStrategy.computeHashCode(data[j]));
// }
// throw ex;
// }
}
assertEquals(500, map.size());
map.compact();
assertEquals(500, map.size());
assertTrue(map._maxSize < 1000);
}
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
public void testTPHashMapConstructors() {
int cap = 20;
THashMap cap_and_factor = new THashMap( cap, 0.75f );
assertTrue( "capacity not sufficient: " + cap + ", " + cap_and_factor.capacity(),
cap <= cap_and_factor.capacity() );
assertEquals( 0.75f, cap_and_factor._loadFactor );
}
public void testTPrimitivePrimitveHashMapConstructors() {
int cap = 20;
TIntLongMap cap_and_factor = new TIntLongHashMap( cap, 0.75f );
TPrimitiveHash cap_and_factor_hash = (TPrimitiveHash) cap_and_factor;
assertTrue( "capacity not sufficient: " + cap + ", " + cap_and_factor_hash.capacity(),
cap <= cap_and_factor_hash.capacity() );
assertEquals( 0.75f, cap_and_factor_hash._loadFactor );
TIntLongMap fully_specified =
new TIntLongHashMap( cap, 0.5f, Integer.MIN_VALUE, Long.MIN_VALUE );
TPrimitiveHash fully_specified_hash = (TPrimitiveHash) fully_specified;
assertTrue( "capacity not sufficient: " + cap + ", " + fully_specified_hash.capacity(),
cap <= fully_specified_hash.capacity() );
assertEquals( 0.5f, fully_specified_hash._loadFactor );
assertEquals( Integer.MIN_VALUE, fully_specified.getNoEntryKey() );
assertEquals( Long.MIN_VALUE, fully_specified.getNoEntryValue() );
}
// test all the way up the chain to THash
public void testTPrimitivePrimitveHashMapSerialize() throws Exception {
int[] keys = {1138, 42, 86, 99, 101, 727, 117};
long[] vals = new long[keys.length];
TIntLongMap original_map =
new TIntLongHashMap( 200, 0.75f, Integer.MIN_VALUE, Long.MIN_VALUE );
for ( int i = 0; i < keys.length; i++ ) {
vals[i] = keys[i] * 2;
original_map.put( keys[i], vals[i] );
}
THash original_hash = ( THash ) original_map;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( original_map );
ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() );
ObjectInputStream ois = new ObjectInputStream( bias );
TIntLongMap deserialized_map = ( TIntLongMap ) ois.readObject();
THash deserialized_hash = ( THash ) deserialized_map;
assertEquals( original_map, deserialized_map );
assertEquals( original_map.getNoEntryKey(), deserialized_map.getNoEntryKey() );
assertEquals( original_map.getNoEntryValue(), deserialized_map.getNoEntryValue() );
assertEquals( original_hash._loadFactor, deserialized_hash._loadFactor );
}
// test all the way up the chain to THash
public void testTPrimitiveObjectHashMapSerialize() throws Exception {
int[] keys = {1138, 42, 86, 99, 101, 727, 117};
String[] vals = new String[keys.length];
TIntObjectMap<String> original_map =
new TIntObjectHashMap<String>( 200, 0.75f, Integer.MIN_VALUE );
for ( int i = 0; i < keys.length; i++ ) {
vals[i] = String.valueOf( keys[i] * 2 );
original_map.put( keys[i], vals[i] );
}
THash original_hash = ( THash ) original_map;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( original_map );
ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() );
ObjectInputStream ois = new ObjectInputStream( bias );
TIntObjectMap deserialized_map = ( TIntObjectMap ) ois.readObject();
THash deserialized_hash = ( THash ) deserialized_map;
assertEquals( original_map, deserialized_map );
assertEquals( original_map.getNoEntryKey(), deserialized_map.getNoEntryKey() );
assertEquals( original_hash._loadFactor, deserialized_hash._loadFactor );
}
// test all the way up the chain to THash
public void testTObjectPrimitiveHashMapSerialize() throws Exception {
int[] vals = {1138, 42, 86, 99, 101, 727, 117};
String[] keys = new String[vals.length];
TObjectIntMap<String> original_map =
new TObjectIntHashMap<String>( 200, 0.75f, Integer.MIN_VALUE );
for ( int i = 0; i < keys.length; i++ ) {
keys[i] = String.valueOf( vals[i] * 2 );
original_map.put( keys[i], vals[i] );
}
THash original_hash = ( THash ) original_map;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( original_map );
ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() );
ObjectInputStream ois = new ObjectInputStream( bias );
TObjectIntMap deserialized_map = ( TObjectIntMap ) ois.readObject();
THash deserialized_hash = ( THash ) deserialized_map;
assertEquals( original_map, deserialized_map );
assertEquals( original_map.getNoEntryValue(), deserialized_map.getNoEntryValue() );
assertEquals( original_hash._loadFactor, deserialized_hash._loadFactor );
}
}
|