1 package gnu.trove; 19 20 import gnu.trove.*; 21 import junit.framework.TestCase; 22 23 import java.io.*; 24 25 26 29 public class SerializationTest extends TestCase { 30 public SerializationTest( String name ) { 31 super( name ); 32 } 33 34 public void testP2PMap() { 35 TLongLongHashMap llmap = new TLongLongHashMap(); 37 assertTrue( serializesCorrectly( llmap ) ); 38 llmap.put( 0, 1 ); 39 assertTrue( serializesCorrectly( llmap ) ); 40 llmap.put( Long.MIN_VALUE, Long.MIN_VALUE ); 41 assertTrue( serializesCorrectly( llmap ) ); 42 llmap.put( Long.MAX_VALUE, Long.MAX_VALUE ); 43 assertTrue( serializesCorrectly( llmap ) ); 44 45 TIntIntHashMap iimap = new TIntIntHashMap(); 47 assertTrue( serializesCorrectly( iimap ) ); 48 iimap.put( 0, 1 ); 49 assertTrue( serializesCorrectly( iimap ) ); 50 iimap.put( Integer.MIN_VALUE, Integer.MIN_VALUE ); 51 assertTrue( serializesCorrectly( iimap ) ); 52 iimap.put( Integer.MAX_VALUE, Integer.MAX_VALUE ); 53 assertTrue( serializesCorrectly( iimap ) ); 54 55 TDoubleDoubleHashMap ddmap = new TDoubleDoubleHashMap(); 57 assertTrue( serializesCorrectly( ddmap ) ); 58 ddmap.put( 0, 1 ); 59 assertTrue( serializesCorrectly( ddmap ) ); 60 ddmap.put( Double.MIN_VALUE, Double.MIN_VALUE ); 61 assertTrue( serializesCorrectly( ddmap ) ); 62 ddmap.put( Double.MAX_VALUE, Double.MAX_VALUE ); 63 assertTrue( serializesCorrectly( ddmap ) ); 64 ddmap.put( Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY ); 68 assertTrue( serializesCorrectly( ddmap ) ); 69 ddmap.put( Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY ); 70 assertTrue( serializesCorrectly( ddmap ) ); 71 72 TFloatFloatHashMap ffmap = new TFloatFloatHashMap(); 74 assertTrue( serializesCorrectly( ffmap ) ); 75 ffmap.put( 0, 1 ); 76 assertTrue( serializesCorrectly( ffmap ) ); 77 ffmap.put( Float.MIN_VALUE, Float.MIN_VALUE ); 78 assertTrue( serializesCorrectly( ffmap ) ); 79 ffmap.put( Float.MAX_VALUE, Float.MAX_VALUE ); 80 assertTrue( serializesCorrectly( ffmap ) ); 81 ffmap.put( Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY ); 85 assertTrue( serializesCorrectly( ffmap ) ); 86 ffmap.put( Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY ); 87 assertTrue( serializesCorrectly( ffmap ) ); 88 } 89 90 91 public void testP2OMap() { 92 TLongObjectHashMap lomap = new TLongObjectHashMap(); 94 assertTrue( serializesCorrectly( lomap ) ); 95 lomap.put( 0, Long.valueOf( 1 ) ); 96 assertTrue( serializesCorrectly( lomap ) ); 97 lomap.put( Long.MIN_VALUE, Long.valueOf( Long.MIN_VALUE ) ); 98 assertTrue( serializesCorrectly( lomap ) ); 99 lomap.put( Long.MAX_VALUE, Long.valueOf( Long.MAX_VALUE ) ); 100 assertTrue( serializesCorrectly( lomap ) ); 101 102 TIntObjectHashMap iomap = new TIntObjectHashMap(); 104 assertTrue( serializesCorrectly( iomap ) ); 105 iomap.put( 0, Integer.valueOf( 1 ) ); 106 assertTrue( serializesCorrectly( iomap ) ); 107 iomap.put( Integer.MIN_VALUE, Integer.valueOf( Integer.MIN_VALUE ) ); 108 assertTrue( serializesCorrectly( iomap ) ); 109 iomap.put( Integer.MAX_VALUE, Integer.valueOf( Integer.MAX_VALUE ) ); 110 assertTrue( serializesCorrectly( iomap ) ); 111 112 TDoubleObjectHashMap domap = new TDoubleObjectHashMap(); 114 assertTrue( serializesCorrectly( domap ) ); 115 domap.put( 0, Double.valueOf( 1 ) ); 116 assertTrue( serializesCorrectly( domap ) ); 117 domap.put( Double.MIN_VALUE, Double.valueOf( Double.MIN_VALUE ) ); 118 assertTrue( serializesCorrectly( domap ) ); 119 domap.put( Double.MAX_VALUE, Double.valueOf( Double.MAX_VALUE ) ); 120 assertTrue( serializesCorrectly( domap ) ); 121 domap.put( Double.POSITIVE_INFINITY, Double.valueOf( Double.POSITIVE_INFINITY ) ); 125 assertTrue( serializesCorrectly( domap ) ); 126 domap.put( Double.NEGATIVE_INFINITY, Double.valueOf( Double.NEGATIVE_INFINITY ) ); 127 assertTrue( serializesCorrectly( domap ) ); 128 129 TFloatObjectHashMap fomap = new TFloatObjectHashMap(); 131 assertTrue( serializesCorrectly( fomap ) ); 132 fomap.put( 0, Float.valueOf( 1 ) ); 133 assertTrue( serializesCorrectly( fomap ) ); 134 fomap.put( Float.MIN_VALUE, Float.valueOf( Float.MIN_VALUE ) ); 135 assertTrue( serializesCorrectly( fomap ) ); 136 fomap.put( Float.MAX_VALUE, Float.valueOf( Float.MAX_VALUE ) ); 137 assertTrue( serializesCorrectly( fomap ) ); 138 fomap.put( Float.POSITIVE_INFINITY, Float.valueOf( Float.POSITIVE_INFINITY ) ); 142 assertTrue( serializesCorrectly( fomap ) ); 143 fomap.put( Float.NEGATIVE_INFINITY, Float.valueOf( Float.NEGATIVE_INFINITY ) ); 144 assertTrue( serializesCorrectly( fomap ) ); 145 } 146 147 public void testO2PMap() { 148 TObjectLongHashMap olmap = new TObjectLongHashMap(); 150 assertTrue( serializesCorrectly( olmap ) ); 151 olmap.put( Long.valueOf( 0 ), 1 ); 152 assertTrue( serializesCorrectly( olmap ) ); 153 olmap.put( Long.valueOf( Long.MIN_VALUE ), Long.MIN_VALUE ); 154 assertTrue( serializesCorrectly( olmap ) ); 155 olmap.put( Long.valueOf( Long.MAX_VALUE ), Long.MAX_VALUE ); 156 assertTrue( serializesCorrectly( olmap ) ); 157 158 TObjectIntHashMap oimap = new TObjectIntHashMap(); 160 assertTrue( serializesCorrectly( oimap ) ); 161 oimap.put( Integer.valueOf( 0 ), 1 ); 162 assertTrue( serializesCorrectly( oimap ) ); 163 oimap.put( Integer.valueOf( Integer.MIN_VALUE ), Integer.MIN_VALUE ); 164 assertTrue( serializesCorrectly( oimap ) ); 165 oimap.put( Integer.valueOf( Integer.MAX_VALUE ), Integer.MAX_VALUE ); 166 assertTrue( serializesCorrectly( oimap ) ); 167 168 TObjectDoubleHashMap odmap = new TObjectDoubleHashMap(); 170 assertTrue( serializesCorrectly( odmap ) ); 171 odmap.put( Double.valueOf( 0 ), 1 ); 172 assertTrue( serializesCorrectly( odmap ) ); 173 odmap.put( Double.valueOf( Double.MIN_VALUE ), Double.MIN_VALUE ); 174 assertTrue( serializesCorrectly( odmap ) ); 175 odmap.put( Double.valueOf( Double.MAX_VALUE ), Double.MAX_VALUE ); 176 assertTrue( serializesCorrectly( odmap ) ); 177 odmap.put( Double.valueOf( Double.POSITIVE_INFINITY ), Double.POSITIVE_INFINITY ); 181 assertTrue( serializesCorrectly( odmap ) ); 182 odmap.put( Double.valueOf( Double.NEGATIVE_INFINITY ), Double.NEGATIVE_INFINITY ); 183 assertTrue( serializesCorrectly( odmap ) ); 184 185 TObjectFloatHashMap ofmap = new TObjectFloatHashMap(); 187 assertTrue( serializesCorrectly( ofmap ) ); 188 ofmap.put( Float.valueOf( 0 ), 1 ); 189 assertTrue( serializesCorrectly( ofmap ) ); 190 ofmap.put( Float.valueOf( Float.MIN_VALUE ), Float.MIN_VALUE ); 191 assertTrue( serializesCorrectly( ofmap ) ); 192 ofmap.put( Float.valueOf( Float.MAX_VALUE ), Float.MAX_VALUE ); 193 assertTrue( serializesCorrectly( ofmap ) ); 194 ofmap.put( Float.valueOf( Float.POSITIVE_INFINITY ), Float.POSITIVE_INFINITY ); 198 assertTrue( serializesCorrectly( ofmap ) ); 199 ofmap.put( Float.valueOf( Float.NEGATIVE_INFINITY ), Float.NEGATIVE_INFINITY ); 200 assertTrue( serializesCorrectly( ofmap ) ); 201 } 202 203 204 public void testList() { 205 TLongArrayList llist = new TLongArrayList(); 207 assertTrue( serializesCorrectly( llist ) ); 208 llist.add( 0 ); 209 llist.add( 1 ); 210 assertTrue( serializesCorrectly( llist ) ); 211 llist.add( Long.MIN_VALUE ); 212 assertTrue( serializesCorrectly( llist ) ); 213 llist.add( Long.MAX_VALUE ); 214 assertTrue( serializesCorrectly( llist ) ); 215 216 TIntArrayList ilist = new TIntArrayList(); 218 assertTrue( serializesCorrectly( ilist ) ); 219 ilist.add( 0 ); 220 ilist.add( 1 ); 221 assertTrue( serializesCorrectly( ilist ) ); 222 ilist.add( Integer.MIN_VALUE ); 223 assertTrue( serializesCorrectly( ilist ) ); 224 ilist.add( Integer.MAX_VALUE ); 225 assertTrue( serializesCorrectly( ilist ) ); 226 227 TDoubleArrayList dlist = new TDoubleArrayList(); 229 assertTrue( serializesCorrectly( dlist ) ); 230 dlist.add( 0 ); 231 dlist.add( 1 ); 232 assertTrue( serializesCorrectly( dlist ) ); 233 dlist.add( Double.MIN_VALUE ); 234 assertTrue( serializesCorrectly( dlist ) ); 235 dlist.add( Double.MAX_VALUE ); 236 assertTrue( serializesCorrectly( dlist ) ); 237 dlist.add( Double.POSITIVE_INFINITY ); 241 assertTrue( serializesCorrectly( dlist ) ); 242 dlist.add( Double.NEGATIVE_INFINITY ); 243 assertTrue( serializesCorrectly( dlist ) ); 244 245 TFloatArrayList flist = new TFloatArrayList(); 247 assertTrue( serializesCorrectly( flist ) ); 248 flist.add( 0 ); 249 flist.add( 1 ); 250 assertTrue( serializesCorrectly( flist ) ); 251 flist.add( Float.MIN_VALUE ); 252 assertTrue( serializesCorrectly( flist ) ); 253 flist.add( Float.MAX_VALUE ); 254 assertTrue( serializesCorrectly( flist ) ); 255 flist.add( Float.POSITIVE_INFINITY ); 259 assertTrue( serializesCorrectly( flist ) ); 260 flist.add( Float.NEGATIVE_INFINITY ); 261 assertTrue( serializesCorrectly( flist ) ); 262 } 263 264 265 public void testSet() { 266 TLongHashSet llist = new TLongHashSet(); 268 assertTrue( serializesCorrectly( llist ) ); 269 llist.add( 0 ); 270 llist.add( 1 ); 271 assertTrue( serializesCorrectly( llist ) ); 272 llist.add( Long.MIN_VALUE ); 273 assertTrue( serializesCorrectly( llist ) ); 274 llist.add( Long.MAX_VALUE ); 275 assertTrue( serializesCorrectly( llist ) ); 276 277 TIntHashSet ilist = new TIntHashSet(); 279 assertTrue( serializesCorrectly( ilist ) ); 280 ilist.add( 0 ); 281 ilist.add( 1 ); 282 assertTrue( serializesCorrectly( ilist ) ); 283 ilist.add( Integer.MIN_VALUE ); 284 assertTrue( serializesCorrectly( ilist ) ); 285 ilist.add( Integer.MAX_VALUE ); 286 assertTrue( serializesCorrectly( ilist ) ); 287 288 TDoubleHashSet dlist = new TDoubleHashSet(); 290 assertTrue( serializesCorrectly( dlist ) ); 291 dlist.add( 0 ); 292 dlist.add( 1 ); 293 assertTrue( serializesCorrectly( dlist ) ); 294 dlist.add( Double.MIN_VALUE ); 295 assertTrue( serializesCorrectly( dlist ) ); 296 dlist.add( Double.MAX_VALUE ); 297 assertTrue( serializesCorrectly( dlist ) ); 298 dlist.add( Double.POSITIVE_INFINITY ); 302 assertTrue( serializesCorrectly( dlist ) ); 303 dlist.add( Double.NEGATIVE_INFINITY ); 304 assertTrue( serializesCorrectly( dlist ) ); 305 306 TFloatHashSet flist = new TFloatHashSet(); 308 assertTrue( serializesCorrectly( flist ) ); 309 flist.add( 0 ); 310 flist.add( 1 ); 311 assertTrue( serializesCorrectly( flist ) ); 312 flist.add( Float.MIN_VALUE ); 313 assertTrue( serializesCorrectly( flist ) ); 314 flist.add( Float.MAX_VALUE ); 315 assertTrue( serializesCorrectly( flist ) ); 316 flist.add( Float.POSITIVE_INFINITY ); 320 assertTrue( serializesCorrectly( flist ) ); 321 flist.add( Float.NEGATIVE_INFINITY ); 322 assertTrue( serializesCorrectly( flist ) ); 323 } 324 325 326 public void testLinkedList() { 327 TLinkedList list = new TLinkedList(); 328 list.add( new LinkedNode( 0 ) ); 329 list.add( new LinkedNode( 1 ) ); 330 list.add( new LinkedNode( 2 ) ); 331 list.add( new LinkedNode( 3 ) ); 332 333 assertTrue( serializesCorrectly( list ) ); 334 } 335 336 337 private boolean serializesCorrectly( Serializable obj ) { 338 assert obj instanceof Externalizable : obj + " is not Externalizable"; 339 340 ObjectOutputStream oout = null; 341 ObjectInputStream oin = null; 342 try { 343 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 344 oout = new ObjectOutputStream( bout ); 345 346 oout.writeObject( obj ); 347 oout.close(); 348 349 ByteArrayInputStream bin = new ByteArrayInputStream( bout.toByteArray() ); 350 oin = new ObjectInputStream( bin ); 351 352 Object new_obj = oin.readObject(); 353 return obj.equals( new_obj ); 354 } 355 catch( Exception ex ) { 356 ex.printStackTrace(); 357 return false; 358 } 359 finally { 360 if ( oout != null ) { 361 try { 362 oout.close(); 363 } 364 catch( IOException ex ) { 365 } 367 } 368 if ( oin != null ) { 369 try { 370 oin.close(); 371 } 372 catch( IOException ex ) { 373 } 375 } 376 } 377 } 378 379 380 private static class LinkedNode extends TLinkableAdapter { 381 private final int value; 382 383 LinkedNode( int value ) { 384 this.value = value; 385 } 386 387 public boolean equals( Object o ) { 388 if ( this == o ) return true; 389 if ( o == null || getClass() != o.getClass() ) return false; 390 391 LinkedNode that = ( LinkedNode ) o; 392 393 if ( value != that.value ) return false; 394 395 return true; 396 } 397 398 public int hashCode() { 399 return value; 400 } 401 } 402 } 403 | Popular Tags |