KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > trove > O2PHashMapTest


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.decorator.TObjectIntHashMapDecorator;
21 import junit.framework.TestCase;
22
23 import java.util.*;
24
25
26 /**
27  *
28  */

29 public class O2PHashMapTest extends TestCase {
30
31     public O2PHashMapTest( String JavaDoc name ) {
32         super( name );
33     }
34
35
36     public void testKeys() {
37         TObjectIntHashMap<String JavaDoc> map = new TObjectIntHashMap<String JavaDoc>();
38
39         map.put( "one", 1 );
40         map.put( "two", 2 );
41
42         assertEquals( 2, map.size() );
43
44         String JavaDoc[] keys = map.keys( new String JavaDoc[ map.size() ] );
45         assertEquals( 2, keys.length );
46         List<String JavaDoc> keys_list = Arrays.asList( keys );
47         
48         assertTrue( keys_list.contains( "one" ) );
49         assertTrue( keys_list.contains( "two" ) );
50
51         Object JavaDoc[] keys2 = map.keys();
52         assertEquals( 2, keys2.length );
53         List keys_list2 = Arrays.asList( keys2 );
54
55         assertTrue( keys_list2.contains( "one" ) );
56         assertTrue( keys_list2.contains( "two" ) );
57     }
58
59
60     public void testDecorator() {
61         TObjectIntHashMap<String JavaDoc> map = new TObjectIntHashMap<String JavaDoc>();
62
63         map.put( "one", 1 );
64         map.put( "two", 2 );
65
66         Map<String JavaDoc,Integer JavaDoc> decorator = new TObjectIntHashMapDecorator<String JavaDoc>( map );
67
68         assertEquals( 2, decorator.size() );
69         assertEquals( Integer.valueOf( 1 ), decorator.get( "one" ) );
70         assertEquals( Integer.valueOf( 2 ), decorator.get( "two" ) );
71
72         Set<String JavaDoc> decorator_keys = decorator.keySet();
73         assertEquals( 2, decorator_keys.size() );
74         Iterator<String JavaDoc> it = decorator_keys.iterator();
75         int count = 0;
76         while( it.hasNext() ) {
77             count++;
78             System.out.println(it.next());
79         }
80         assertEquals( 2, count );
81
82         assertSame(map, ( ( TObjectIntHashMapDecorator ) decorator ).getMap() );
83     }
84
85
86     public void testIterator() {
87         TObjectIntHashMap<String JavaDoc> map = new TObjectIntHashMap<String JavaDoc>();
88
89         TObjectIntIterator<String JavaDoc> iterator = map.iterator();
90         assertFalse( iterator.hasNext() );
91
92         map.put( "one", 1 );
93         map.put( "two", 2 );
94
95         iterator = map.iterator();
96         assertTrue( iterator.hasNext() );
97         iterator.advance();
98         assertTrue( iterator.hasNext() );
99         iterator.advance();
100         assertFalse( iterator.hasNext() );
101     }
102 }
103
Popular Tags