KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > SerializationTest


1 ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2001-2006, Eric D. Friedman All Rights Reserved.
3
//
4
// This library is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU Lesser General Public
6
// License as published by the Free Software Foundation; either
7
// version 2.1 of the License, or (at your option) any later version.
8
//
9
// This library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU Lesser General Public
15
// License along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
///////////////////////////////////////////////////////////////////////////////
18
package gnu.trove;
19
20 import gnu.trove.*;
21 import junit.framework.TestCase;
22
23 import java.io.*;
24
25
26 /**
27  *
28  */

29 public class SerializationTest extends TestCase {
30     public SerializationTest( String JavaDoc name ) {
31         super( name );
32     }
33
34     public void testP2PMap() {
35         // Long-long
36
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         // Int-int
46
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         // Double-double
56
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         // NOTE: trove doesn't deal well with NaN
65
// ddmap.put( Double.NaN, Double.NaN );
66
// assertTrue( serializesCorrectly( ddmap ) );
67
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         // Float-float
73
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         // NOTE: trove doesn't deal well with NaN
82
// ffmap.put( Float.NaN, Float.NaN );
83
// assertTrue( serializesCorrectly( ffmap ) );
84
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         // Long-long
93
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         // Int-int
103
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         // Double-double
113
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         // NOTE: trove doesn't deal well with NaN
122
// ddmap.put( Double.NaN, Double.NaN );
123
// assertTrue( serializesCorrectly( ddmap ) );
124
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         // Float-float
130
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         // NOTE: trove doesn't deal well with NaN
139
// ffmap.put( Float.NaN, Float.NaN );
140
// assertTrue( serializesCorrectly( ffmap ) );
141
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         // Long-long
149
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         // Int-int
159
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         // Double-double
169
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         // NOTE: trove doesn't deal well with NaN
178
// ddmap.put( Double.NaN, Double.NaN );
179
// assertTrue( serializesCorrectly( ddmap ) );
180
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         // Float-float
186
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         // NOTE: trove doesn't deal well with NaN
195
// ffmap.put( Float.NaN, Float.NaN );
196
// assertTrue( serializesCorrectly( ffmap ) );
197
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         // Long-long
206
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         // Int-int
217
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         // Double-double
228
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         // NOTE: trove doesn't deal well with NaN
238
// ddmap.add( Double.NaN, Double.NaN );
239
// assertTrue( serializesCorrectly( ddmap ) );
240
dlist.add( Double.POSITIVE_INFINITY );
241         assertTrue( serializesCorrectly( dlist ) );
242         dlist.add( Double.NEGATIVE_INFINITY );
243         assertTrue( serializesCorrectly( dlist ) );
244
245         // Float-float
246
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         // NOTE: trove doesn't deal well with NaN
256
// ffmap.add( Float.NaN );
257
// assertTrue( serializesCorrectly( ffmap ) );
258
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         // Long-long
267
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         // Int-int
278
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         // Double-double
289
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         // NOTE: trove doesn't deal well with NaN
299
// ddmap.add( Double.NaN, Double.NaN );
300
// assertTrue( serializesCorrectly( ddmap ) );
301
dlist.add( Double.POSITIVE_INFINITY );
302         assertTrue( serializesCorrectly( dlist ) );
303         dlist.add( Double.NEGATIVE_INFINITY );
304         assertTrue( serializesCorrectly( dlist ) );
305
306         // Float-float
307
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         // NOTE: trove doesn't deal well with NaN
317
// ffmap.add( Float.NaN );
318
// assertTrue( serializesCorrectly( ffmap ) );
319
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 JavaDoc new_obj = oin.readObject();
353             return obj.equals( new_obj );
354         }
355         catch( Exception JavaDoc 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                     // ignore
366
}
367             }
368             if ( oin != null ) {
369                 try {
370                     oin.close();
371                 }
372                 catch( IOException ex ) {
373                     // ignore
374
}
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 JavaDoc 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