File: TCustomHashSetTest.java

package info (click to toggle)
trove3 3.0.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,924 kB
  • sloc: java: 16,429; xml: 331; makefile: 21; sh: 10
file content (81 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download | duplicates (5)
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
package gnu.trove.set.hash;

import gnu.trove.strategy.HashingStrategy;
import gnu.trove.map.hash.ArrayHashingStrategy;
import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Set;


/**
 *
 */
public class TCustomHashSetTest extends TestCase {
	public void testArray() {
		char[] foo = new char[] { 'a', 'b', 'c' };
		char[] bar = new char[] { 'a', 'b', 'c' };

		assertFalse( foo.hashCode() == bar.hashCode() );
		//noinspection ArrayEquals
		assertFalse( foo.equals( bar ) );

		HashingStrategy<char[]> strategy = new ArrayHashingStrategy();
		assertTrue( strategy.computeHashCode( foo ) ==
			strategy.computeHashCode( bar ) );
		assertTrue( strategy.equals( foo, bar ) );

		Set<char[]> set = new TCustomHashSet<char[]>( strategy );
		set.add( foo );
		assertTrue( set.contains( foo ) );
		assertTrue( set.contains( bar ) );

		set.remove( bar );

		assertTrue( set.isEmpty() );
	}


	public void testSerialization() throws Exception {
		char[] foo = new char[] { 'a', 'b', 'c' };
		char[] bar = new char[] { 'a', 'b', 'c' };

		HashingStrategy<char[]> strategy = new ArrayHashingStrategy();
		Set<char[]> set = new TCustomHashSet<char[]>( strategy );

		set.add( foo );

		// Make sure it still works after being serialized
		ObjectOutputStream oout = null;
		ByteArrayOutputStream bout = null;
		ObjectInputStream oin = null;
		ByteArrayInputStream bin = null;
		try {
			bout = new ByteArrayOutputStream();
			oout = new ObjectOutputStream( bout );

			oout.writeObject( set );

			bin = new ByteArrayInputStream( bout.toByteArray() );
			oin = new ObjectInputStream( bin );

			set = ( Set<char[]> ) oin.readObject();
		}
		finally {
			if ( oin != null ) oin.close();
			if ( bin != null ) bin.close();
			if ( oout != null ) oout.close();
			if ( bout != null ) bout.close();
		}

		assertTrue( set.contains( foo ) );
		assertTrue( set.contains( bar ) );

		set.remove( bar );

		assertTrue( set.isEmpty() );
	}
}