001: ///////////////////////////////////////////////////////////////////////////////
002: // Copyright (c) 2001-2006, Eric D. Friedman All Rights Reserved.
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: ///////////////////////////////////////////////////////////////////////////////
018: package gnu.trove;
019:
020: import gnu.trove.*;
021: import junit.framework.TestCase;
022:
023: import java.io.*;
024:
025: /**
026: *
027: */
028: public class SerializationTest extends TestCase {
029: public SerializationTest(String name) {
030: super (name);
031: }
032:
033: public void testP2PMap() {
034: // Long-long
035: TLongLongHashMap llmap = new TLongLongHashMap();
036: assertTrue(serializesCorrectly(llmap));
037: llmap.put(0, 1);
038: assertTrue(serializesCorrectly(llmap));
039: llmap.put(Long.MIN_VALUE, Long.MIN_VALUE);
040: assertTrue(serializesCorrectly(llmap));
041: llmap.put(Long.MAX_VALUE, Long.MAX_VALUE);
042: assertTrue(serializesCorrectly(llmap));
043:
044: // Int-int
045: TIntIntHashMap iimap = new TIntIntHashMap();
046: assertTrue(serializesCorrectly(iimap));
047: iimap.put(0, 1);
048: assertTrue(serializesCorrectly(iimap));
049: iimap.put(Integer.MIN_VALUE, Integer.MIN_VALUE);
050: assertTrue(serializesCorrectly(iimap));
051: iimap.put(Integer.MAX_VALUE, Integer.MAX_VALUE);
052: assertTrue(serializesCorrectly(iimap));
053:
054: // Double-double
055: TDoubleDoubleHashMap ddmap = new TDoubleDoubleHashMap();
056: assertTrue(serializesCorrectly(ddmap));
057: ddmap.put(0, 1);
058: assertTrue(serializesCorrectly(ddmap));
059: ddmap.put(Double.MIN_VALUE, Double.MIN_VALUE);
060: assertTrue(serializesCorrectly(ddmap));
061: ddmap.put(Double.MAX_VALUE, Double.MAX_VALUE);
062: assertTrue(serializesCorrectly(ddmap));
063: // NOTE: trove doesn't deal well with NaN
064: // ddmap.put( Double.NaN, Double.NaN );
065: // assertTrue( serializesCorrectly( ddmap ) );
066: ddmap.put(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
067: assertTrue(serializesCorrectly(ddmap));
068: ddmap.put(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
069: assertTrue(serializesCorrectly(ddmap));
070:
071: // Float-float
072: TFloatFloatHashMap ffmap = new TFloatFloatHashMap();
073: assertTrue(serializesCorrectly(ffmap));
074: ffmap.put(0, 1);
075: assertTrue(serializesCorrectly(ffmap));
076: ffmap.put(Float.MIN_VALUE, Float.MIN_VALUE);
077: assertTrue(serializesCorrectly(ffmap));
078: ffmap.put(Float.MAX_VALUE, Float.MAX_VALUE);
079: assertTrue(serializesCorrectly(ffmap));
080: // NOTE: trove doesn't deal well with NaN
081: // ffmap.put( Float.NaN, Float.NaN );
082: // assertTrue( serializesCorrectly( ffmap ) );
083: ffmap.put(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
084: assertTrue(serializesCorrectly(ffmap));
085: ffmap.put(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
086: assertTrue(serializesCorrectly(ffmap));
087: }
088:
089: public void testP2OMap() {
090: // Long-long
091: TLongObjectHashMap lomap = new TLongObjectHashMap();
092: assertTrue(serializesCorrectly(lomap));
093: lomap.put(0, Long.valueOf(1));
094: assertTrue(serializesCorrectly(lomap));
095: lomap.put(Long.MIN_VALUE, Long.valueOf(Long.MIN_VALUE));
096: assertTrue(serializesCorrectly(lomap));
097: lomap.put(Long.MAX_VALUE, Long.valueOf(Long.MAX_VALUE));
098: assertTrue(serializesCorrectly(lomap));
099:
100: // Int-int
101: TIntObjectHashMap iomap = new TIntObjectHashMap();
102: assertTrue(serializesCorrectly(iomap));
103: iomap.put(0, Integer.valueOf(1));
104: assertTrue(serializesCorrectly(iomap));
105: iomap
106: .put(Integer.MIN_VALUE, Integer
107: .valueOf(Integer.MIN_VALUE));
108: assertTrue(serializesCorrectly(iomap));
109: iomap
110: .put(Integer.MAX_VALUE, Integer
111: .valueOf(Integer.MAX_VALUE));
112: assertTrue(serializesCorrectly(iomap));
113:
114: // Double-double
115: TDoubleObjectHashMap domap = new TDoubleObjectHashMap();
116: assertTrue(serializesCorrectly(domap));
117: domap.put(0, Double.valueOf(1));
118: assertTrue(serializesCorrectly(domap));
119: domap.put(Double.MIN_VALUE, Double.valueOf(Double.MIN_VALUE));
120: assertTrue(serializesCorrectly(domap));
121: domap.put(Double.MAX_VALUE, Double.valueOf(Double.MAX_VALUE));
122: assertTrue(serializesCorrectly(domap));
123: // NOTE: trove doesn't deal well with NaN
124: // ddmap.put( Double.NaN, Double.NaN );
125: // assertTrue( serializesCorrectly( ddmap ) );
126: domap.put(Double.POSITIVE_INFINITY, Double
127: .valueOf(Double.POSITIVE_INFINITY));
128: assertTrue(serializesCorrectly(domap));
129: domap.put(Double.NEGATIVE_INFINITY, Double
130: .valueOf(Double.NEGATIVE_INFINITY));
131: assertTrue(serializesCorrectly(domap));
132:
133: // Float-float
134: TFloatObjectHashMap fomap = new TFloatObjectHashMap();
135: assertTrue(serializesCorrectly(fomap));
136: fomap.put(0, Float.valueOf(1));
137: assertTrue(serializesCorrectly(fomap));
138: fomap.put(Float.MIN_VALUE, Float.valueOf(Float.MIN_VALUE));
139: assertTrue(serializesCorrectly(fomap));
140: fomap.put(Float.MAX_VALUE, Float.valueOf(Float.MAX_VALUE));
141: assertTrue(serializesCorrectly(fomap));
142: // NOTE: trove doesn't deal well with NaN
143: // ffmap.put( Float.NaN, Float.NaN );
144: // assertTrue( serializesCorrectly( ffmap ) );
145: fomap.put(Float.POSITIVE_INFINITY, Float
146: .valueOf(Float.POSITIVE_INFINITY));
147: assertTrue(serializesCorrectly(fomap));
148: fomap.put(Float.NEGATIVE_INFINITY, Float
149: .valueOf(Float.NEGATIVE_INFINITY));
150: assertTrue(serializesCorrectly(fomap));
151: }
152:
153: public void testO2PMap() {
154: // Long-long
155: TObjectLongHashMap olmap = new TObjectLongHashMap();
156: assertTrue(serializesCorrectly(olmap));
157: olmap.put(Long.valueOf(0), 1);
158: assertTrue(serializesCorrectly(olmap));
159: olmap.put(Long.valueOf(Long.MIN_VALUE), Long.MIN_VALUE);
160: assertTrue(serializesCorrectly(olmap));
161: olmap.put(Long.valueOf(Long.MAX_VALUE), Long.MAX_VALUE);
162: assertTrue(serializesCorrectly(olmap));
163:
164: // Int-int
165: TObjectIntHashMap oimap = new TObjectIntHashMap();
166: assertTrue(serializesCorrectly(oimap));
167: oimap.put(Integer.valueOf(0), 1);
168: assertTrue(serializesCorrectly(oimap));
169: oimap
170: .put(Integer.valueOf(Integer.MIN_VALUE),
171: Integer.MIN_VALUE);
172: assertTrue(serializesCorrectly(oimap));
173: oimap
174: .put(Integer.valueOf(Integer.MAX_VALUE),
175: Integer.MAX_VALUE);
176: assertTrue(serializesCorrectly(oimap));
177:
178: // Double-double
179: TObjectDoubleHashMap odmap = new TObjectDoubleHashMap();
180: assertTrue(serializesCorrectly(odmap));
181: odmap.put(Double.valueOf(0), 1);
182: assertTrue(serializesCorrectly(odmap));
183: odmap.put(Double.valueOf(Double.MIN_VALUE), Double.MIN_VALUE);
184: assertTrue(serializesCorrectly(odmap));
185: odmap.put(Double.valueOf(Double.MAX_VALUE), Double.MAX_VALUE);
186: assertTrue(serializesCorrectly(odmap));
187: // NOTE: trove doesn't deal well with NaN
188: // ddmap.put( Double.NaN, Double.NaN );
189: // assertTrue( serializesCorrectly( ddmap ) );
190: odmap.put(Double.valueOf(Double.POSITIVE_INFINITY),
191: Double.POSITIVE_INFINITY);
192: assertTrue(serializesCorrectly(odmap));
193: odmap.put(Double.valueOf(Double.NEGATIVE_INFINITY),
194: Double.NEGATIVE_INFINITY);
195: assertTrue(serializesCorrectly(odmap));
196:
197: // Float-float
198: TObjectFloatHashMap ofmap = new TObjectFloatHashMap();
199: assertTrue(serializesCorrectly(ofmap));
200: ofmap.put(Float.valueOf(0), 1);
201: assertTrue(serializesCorrectly(ofmap));
202: ofmap.put(Float.valueOf(Float.MIN_VALUE), Float.MIN_VALUE);
203: assertTrue(serializesCorrectly(ofmap));
204: ofmap.put(Float.valueOf(Float.MAX_VALUE), Float.MAX_VALUE);
205: assertTrue(serializesCorrectly(ofmap));
206: // NOTE: trove doesn't deal well with NaN
207: // ffmap.put( Float.NaN, Float.NaN );
208: // assertTrue( serializesCorrectly( ffmap ) );
209: ofmap.put(Float.valueOf(Float.POSITIVE_INFINITY),
210: Float.POSITIVE_INFINITY);
211: assertTrue(serializesCorrectly(ofmap));
212: ofmap.put(Float.valueOf(Float.NEGATIVE_INFINITY),
213: Float.NEGATIVE_INFINITY);
214: assertTrue(serializesCorrectly(ofmap));
215: }
216:
217: public void testList() {
218: // Long-long
219: TLongArrayList llist = new TLongArrayList();
220: assertTrue(serializesCorrectly(llist));
221: llist.add(0);
222: llist.add(1);
223: assertTrue(serializesCorrectly(llist));
224: llist.add(Long.MIN_VALUE);
225: assertTrue(serializesCorrectly(llist));
226: llist.add(Long.MAX_VALUE);
227: assertTrue(serializesCorrectly(llist));
228:
229: // Int-int
230: TIntArrayList ilist = new TIntArrayList();
231: assertTrue(serializesCorrectly(ilist));
232: ilist.add(0);
233: ilist.add(1);
234: assertTrue(serializesCorrectly(ilist));
235: ilist.add(Integer.MIN_VALUE);
236: assertTrue(serializesCorrectly(ilist));
237: ilist.add(Integer.MAX_VALUE);
238: assertTrue(serializesCorrectly(ilist));
239:
240: // Double-double
241: TDoubleArrayList dlist = new TDoubleArrayList();
242: assertTrue(serializesCorrectly(dlist));
243: dlist.add(0);
244: dlist.add(1);
245: assertTrue(serializesCorrectly(dlist));
246: dlist.add(Double.MIN_VALUE);
247: assertTrue(serializesCorrectly(dlist));
248: dlist.add(Double.MAX_VALUE);
249: assertTrue(serializesCorrectly(dlist));
250: // NOTE: trove doesn't deal well with NaN
251: // ddmap.add( Double.NaN, Double.NaN );
252: // assertTrue( serializesCorrectly( ddmap ) );
253: dlist.add(Double.POSITIVE_INFINITY);
254: assertTrue(serializesCorrectly(dlist));
255: dlist.add(Double.NEGATIVE_INFINITY);
256: assertTrue(serializesCorrectly(dlist));
257:
258: // Float-float
259: TFloatArrayList flist = new TFloatArrayList();
260: assertTrue(serializesCorrectly(flist));
261: flist.add(0);
262: flist.add(1);
263: assertTrue(serializesCorrectly(flist));
264: flist.add(Float.MIN_VALUE);
265: assertTrue(serializesCorrectly(flist));
266: flist.add(Float.MAX_VALUE);
267: assertTrue(serializesCorrectly(flist));
268: // NOTE: trove doesn't deal well with NaN
269: // ffmap.add( Float.NaN );
270: // assertTrue( serializesCorrectly( ffmap ) );
271: flist.add(Float.POSITIVE_INFINITY);
272: assertTrue(serializesCorrectly(flist));
273: flist.add(Float.NEGATIVE_INFINITY);
274: assertTrue(serializesCorrectly(flist));
275: }
276:
277: public void testSet() {
278: // Long-long
279: TLongHashSet llist = new TLongHashSet();
280: assertTrue(serializesCorrectly(llist));
281: llist.add(0);
282: llist.add(1);
283: assertTrue(serializesCorrectly(llist));
284: llist.add(Long.MIN_VALUE);
285: assertTrue(serializesCorrectly(llist));
286: llist.add(Long.MAX_VALUE);
287: assertTrue(serializesCorrectly(llist));
288:
289: // Int-int
290: TIntHashSet ilist = new TIntHashSet();
291: assertTrue(serializesCorrectly(ilist));
292: ilist.add(0);
293: ilist.add(1);
294: assertTrue(serializesCorrectly(ilist));
295: ilist.add(Integer.MIN_VALUE);
296: assertTrue(serializesCorrectly(ilist));
297: ilist.add(Integer.MAX_VALUE);
298: assertTrue(serializesCorrectly(ilist));
299:
300: // Double-double
301: TDoubleHashSet dlist = new TDoubleHashSet();
302: assertTrue(serializesCorrectly(dlist));
303: dlist.add(0);
304: dlist.add(1);
305: assertTrue(serializesCorrectly(dlist));
306: dlist.add(Double.MIN_VALUE);
307: assertTrue(serializesCorrectly(dlist));
308: dlist.add(Double.MAX_VALUE);
309: assertTrue(serializesCorrectly(dlist));
310: // NOTE: trove doesn't deal well with NaN
311: // ddmap.add( Double.NaN, Double.NaN );
312: // assertTrue( serializesCorrectly( ddmap ) );
313: dlist.add(Double.POSITIVE_INFINITY);
314: assertTrue(serializesCorrectly(dlist));
315: dlist.add(Double.NEGATIVE_INFINITY);
316: assertTrue(serializesCorrectly(dlist));
317:
318: // Float-float
319: TFloatHashSet flist = new TFloatHashSet();
320: assertTrue(serializesCorrectly(flist));
321: flist.add(0);
322: flist.add(1);
323: assertTrue(serializesCorrectly(flist));
324: flist.add(Float.MIN_VALUE);
325: assertTrue(serializesCorrectly(flist));
326: flist.add(Float.MAX_VALUE);
327: assertTrue(serializesCorrectly(flist));
328: // NOTE: trove doesn't deal well with NaN
329: // ffmap.add( Float.NaN );
330: // assertTrue( serializesCorrectly( ffmap ) );
331: flist.add(Float.POSITIVE_INFINITY);
332: assertTrue(serializesCorrectly(flist));
333: flist.add(Float.NEGATIVE_INFINITY);
334: assertTrue(serializesCorrectly(flist));
335: }
336:
337: public void testLinkedList() {
338: TLinkedList list = new TLinkedList();
339: list.add(new LinkedNode(0));
340: list.add(new LinkedNode(1));
341: list.add(new LinkedNode(2));
342: list.add(new LinkedNode(3));
343:
344: assertTrue(serializesCorrectly(list));
345: }
346:
347: private boolean serializesCorrectly(Serializable obj) {
348: assert obj instanceof Externalizable : obj
349: + " is not Externalizable";
350:
351: ObjectOutputStream oout = null;
352: ObjectInputStream oin = null;
353: try {
354: ByteArrayOutputStream bout = new ByteArrayOutputStream();
355: oout = new ObjectOutputStream(bout);
356:
357: oout.writeObject(obj);
358: oout.close();
359:
360: ByteArrayInputStream bin = new ByteArrayInputStream(bout
361: .toByteArray());
362: oin = new ObjectInputStream(bin);
363:
364: Object new_obj = oin.readObject();
365: return obj.equals(new_obj);
366: } catch (Exception ex) {
367: ex.printStackTrace();
368: return false;
369: } finally {
370: if (oout != null) {
371: try {
372: oout.close();
373: } catch (IOException ex) {
374: // ignore
375: }
376: }
377: if (oin != null) {
378: try {
379: oin.close();
380: } catch (IOException ex) {
381: // ignore
382: }
383: }
384: }
385: }
386:
387: private static class LinkedNode extends TLinkableAdapter {
388: private final int value;
389:
390: LinkedNode(int value) {
391: this .value = value;
392: }
393:
394: public boolean equals(Object o) {
395: if (this == o)
396: return true;
397: if (o == null || getClass() != o.getClass())
398: return false;
399:
400: LinkedNode that = (LinkedNode) o;
401:
402: if (value != that.value)
403: return false;
404:
405: return true;
406: }
407:
408: public int hashCode() {
409: return value;
410: }
411: }
412: }
|