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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2001-2006, Eric D. Friedman All Rights Reserved.
// Copyright (c) 2009, Rob Eden All Rights Reserved.
// Copyright (c) 2009, Jeff Randall All Rights Reserved.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
///////////////////////////////////////////////////////////////////////////////
package gnu.trove.set.hash;
import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
/**
* Created: Sat Nov 3 10:33:15 2001
*
* @author Eric D. Friedman
* @author Rob Eden
* @author Jeff Randall
* @version $Id: THashSetTest.java,v 1.1.2.3 2010/03/02 04:09:50 robeden Exp $
*/
public class THashSetTest extends TestCase {
public THashSetTest( String name ) {
super( name );
}
public void setUp() throws Exception {
super.setUp();
}
public void tearDown() throws Exception {
super.tearDown();
}
public void testConstructors() throws Exception {
Set<String> set = new THashSet<String>();
assertNotNull( set );
String[] strings = {"a", "b", "c", "d"};
set.addAll( Arrays.asList( strings ) );
Set<String> copy = new THashSet<String>( set );
assertTrue( "set not a copy: " + set + ", " + copy, set.equals( copy ) );
Set<String> another = new THashSet<String>( 20 );
another.addAll( Arrays.asList( strings ) );
assertTrue( "set not equal: " + set + ", " + copy, set.equals( another ) );
another = new THashSet<String>( 2, 1.0f );
another.addAll( Arrays.asList( strings ) );
assertTrue( "set not equal: " + set + ", " + copy, set.equals( another ) );
}
public void testIsEmpty() throws Exception {
Set<String> s = new THashSet<String>();
assertTrue( "new set wasn't empty", s.isEmpty() );
s.add( "One" );
assertTrue( "set with element reports empty", !s.isEmpty() );
s.clear();
assertTrue( "cleared set reports not-empty", s.isEmpty() );
}
public void testContains() throws Exception {
Set<String> s = new THashSet<String>();
String o = "testContains";
s.add( o );
assertTrue( "contains failed", s.contains( o ) );
}
@SuppressWarnings({"ForLoopReplaceableByForEach"})
public void testContainsAll() throws Exception {
Set<String> s = new THashSet<String>();
String[] o = {"Hello World", "Goodbye World", "Hello Goodbye"};
s.addAll( Arrays.asList( o ) );
for ( int i = 0; i < o.length; i++ ) {
assertTrue( o[i], s.contains( o[i] ) );
}
assertTrue( "containsAll failed: " + s,
s.containsAll( Arrays.asList( o ) ) );
String[] more = {"Hello World", "Goodbye World", "Hello Goodbye", "Not There"};
assertFalse( "containsAll failed: " + s,
s.containsAll( Arrays.asList( more ) ) );
}
public void testRetainAll() throws Exception {
Set<String> set = new THashSet<String>();
String[] strings = {"Hello World", "Goodbye World", "Hello Goodbye", "Remove Me"};
set.addAll( Arrays.asList( strings ) );
for ( String string : strings ) {
assertTrue( string, set.contains( string ) );
}
String[] retain = {"Hello World", "Goodbye World", "Hello Goodbye"};
assertTrue( "retainAll failed: " + set,
set.retainAll( Arrays.asList( retain ) ) );
assertTrue( "containsAll failed: " + set,
set.containsAll( Arrays.asList( retain ) ) );
}
public void testRemoveAll() throws Exception {
Set<String> set = new THashSet<String>();
String[] strings = {"Hello World", "Goodbye World", "Hello Goodbye", "Keep Me"};
set.addAll( Arrays.asList( strings ) );
for ( String string : strings ) {
assertTrue( string, set.contains( string ) );
}
String[] remove = {"Hello World", "Goodbye World", "Hello Goodbye"};
assertTrue( "removeAll failed: " + set,
set.removeAll( Arrays.asList( remove ) ) );
assertTrue( "removeAll failed: " + set,
set.containsAll( Arrays.asList( "Keep Me" ) ) );
for ( String element : remove ) {
assertFalse( element + " still in set: " + set, set.contains( element ) );
}
assertEquals( 1, set.size() );
}
public void testAdd() throws Exception {
Set<String> s = new THashSet<String>();
assertTrue( "add failed", s.add( "One" ) );
assertTrue( "duplicated add succeded", !s.add( "One" ) );
}
public void testRemove() throws Exception {
Set<String> s = new THashSet<String>();
s.add( "One" );
s.add( "Two" );
assertTrue( "One was not added", s.contains( "One" ) );
assertTrue( "One was not removed", s.remove( "One" ) );
assertTrue( "One was not removed", !s.contains( "One" ) );
assertTrue( "Two was removed", s.contains( "Two" ) );
assertEquals( 1, s.size() );
}
public void testRemoveObjectNotInSet() throws Exception {
Set<String> set = new THashSet<String>();
set.add( "One" );
set.add( "Two" );
assertTrue( "One was not added", set.contains( "One" ) );
assertTrue( "One was not removed", set.remove( "One" ) );
assertTrue( "One was not removed", !set.contains( "One" ) );
assertTrue( "Two was removed", set.contains( "Two" ) );
assertFalse( "Three was removed (non-existant)", set.remove( "Three" ) );
assertEquals( 1, set.size() );
}
public void testSize() throws Exception {
Set<Object> o = new THashSet<Object>();
assertEquals( "initial size was not 0", 0, o.size() );
for ( int i = 0; i < 99; i++ ) {
o.add( new Object() );
assertEquals( "size did not increase after add", i + 1, o.size() );
}
}
public void testClear() throws Exception {
Set<String> s = new THashSet<String>();
s.addAll( Arrays.asList( "one", "two", "three" ) );
assertEquals( "size was not 3", 3, s.size() );
s.clear();
assertEquals( "initial size was not 0", 0, s.size() );
}
public void testSerialize() throws Exception {
Set<String> s = new THashSet<String>();
s.addAll( Arrays.asList( "one", "two", "three" ) );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( s );
ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
ObjectInputStream ois = new ObjectInputStream( bais );
THashSet s2 = (THashSet) ois.readObject();
assertEquals( s, s2 );
}
public void testToArray() {
Set<String> s = new THashSet<String>();
String[] str = {"hi", "bye", "hello", "goodbye"};
s.addAll( Arrays.asList( str ) );
Object[] res = s.toArray();
Arrays.sort( str );
Arrays.sort( res );
assertTrue( Arrays.equals( str, res ) );
}
public void testToArrayWithParams() {
Set<String> s = new THashSet<String>();
String[] str = {"hi", "bye", "hello", "goodbye"};
s.addAll( Arrays.asList( str ) );
String[] sink = new String[str.length + 2];
sink[sink.length - 1] = "residue";
sink[sink.length - 2] = "will be cleared";
String[] res = s.toArray( sink );
Set<String> copy = new HashSet<String>();
copy.addAll( Arrays.asList( res ) );
Set<String> bogey = new HashSet<String>();
bogey.addAll( Arrays.asList( str ) );
bogey.add( "residue" );
bogey.add( null );
assertEquals( bogey, copy );
}
@SuppressWarnings({"ToArrayCallWithZeroLengthArrayArgument", "SuspiciousToArrayCall"})
public void testToArrayAnotherType() throws Exception {
Set<Number> set = new THashSet<Number>();
Number[] nums = {1138, 42, 86, 99, 101};
set.addAll( Arrays.asList( nums ) );
Integer[] to_int_array_zero = set.toArray( new Integer[0] );
assertTrue( "set and array mismatch: " + set +
", " + Arrays.asList( to_int_array_zero ),
set.containsAll( Arrays.asList( to_int_array_zero ) ) );
Integer[] to_int_array_size = set.toArray( new Integer[set.size()] );
assertTrue( "set and array mismatch: " + set +
", " + Arrays.asList( to_int_array_size ),
set.containsAll( Arrays.asList( to_int_array_size ) ) );
Number[] to_num_array_zero = set.toArray( new Number[0] );
assertTrue( "set and array mismatch: " + set +
", " + Arrays.asList( to_num_array_zero ),
set.containsAll( Arrays.asList( to_num_array_zero ) ) );
Number[] to_num_array_size = set.toArray( new Number[set.size()] );
assertTrue( "set and array mismatch: " + set +
", " + Arrays.asList( to_num_array_size ),
set.containsAll( Arrays.asList( to_num_array_size ) ) );
}
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
public void testRehashing() throws Exception {
Set<Integer> s = new THashSet<Integer>();
for ( int i = 0; i < 10000; i++ ) {
s.add( new Integer( i ) );
}
}
/**
* this tests that we throw when people violate the
* general contract for hashcode on java.lang.Object
*/
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
public void testSomeBadlyWrittenObject() {
Set<Object> s = new THashSet<Object>();
boolean didThrow = false;
int i = 0;
try {
for (; i < 101; i++ ) {
s.add( new Crap() );
}
}
catch ( IllegalArgumentException e ) {
didThrow = true;
}
assertTrue( "expected THashSet to throw an IllegalArgumentException", didThrow );
}
public void testIterable() {
Set<String> set = new THashSet<String>();
set.add( "One" );
set.add( "Two" );
for ( String s : set ) {
assertTrue( s.equals( "One" ) || s.equals( "Two" ) );
}
}
public void testToString() {
Set<String> set = new THashSet<String>();
set.add( "One" );
set.add( "Two" );
String to_string = set.toString();
assertTrue( to_string,
to_string.equals( "{One, Two}" ) || to_string.equals( "{Two, One}" ) );
}
public void testEquals() {
String[] strings = {"hi", "bye", "hello", "goodbye"};
Set<String> set = new THashSet<String>();
set.addAll( Arrays.asList( strings ) );
Set<String> other = new THashSet<String>();
other.addAll( Arrays.asList( strings ) );
assertTrue( "sets incorrectly not equal: " + set + ", " + other,
set.equals( other ) );
String[] mismatched = {"heyya", "whassup", "seeya", "blargh"};
Set<String> unequal = new THashSet<String>();
unequal.addAll( Arrays.asList( mismatched ) );
assertFalse( "sets incorrectly equal: " + set + ", " + unequal,
set.equals( unequal ) );
// Change length, different code branch
unequal.add( "whee!" );
assertFalse( "sets incorrectly equal: " + set + ", " + unequal,
set.equals( unequal ) );
}
public void testEqualsNonSet() {
String[] strings = {"hi", "bye", "hello", "goodbye"};
Set<String> set = new THashSet<String>();
set.addAll( Arrays.asList( strings ) );
List<String> other = new ArrayList<String>();
other.addAll( Arrays.asList( strings ) );
assertFalse( "sets incorrectly equals list: " + set + ", " + other,
set.equals( other ) );
Map<String, String> map = new HashMap<String, String>();
for ( String string : strings ) {
map.put( string, string );
}
assertFalse( "sets incorrectly equals map: " + set + ", " + map,
set.equals( map ) );
}
public void testHashcode() {
String[] strings = {"hi", "bye", "hello", "goodbye"};
Set<String> set = new THashSet<String>();
set.addAll( Arrays.asList( strings ) );
Set<String> other = new THashSet<String>();
other.addAll( Arrays.asList( strings ) );
assertTrue( "hashcodes incorrectly not equal: " + set + ", " + other,
set.hashCode() == other.hashCode() );
String[] mismatched = {"heyya", "whassup", "seeya", "blargh"};
Set<String> unequal = new THashSet<String>();
unequal.addAll( Arrays.asList( mismatched ) );
assertFalse( "hashcodes unlikely equal: " + set + ", " + unequal,
set.hashCode() == unequal.hashCode() );
}
public void testCompact() {
int max_size = 10000;
int reduced_size = 100;
THashSet<Integer> set = new THashSet<Integer>( max_size, 1.0f );
for ( int index = 1; index <= max_size; index++ ) {
set.add( Integer.valueOf( index ) );
}
assertEquals( max_size, set.size() );
int max_length = set._set.length;
for ( int index = max_size; index > reduced_size; index-- ) {
set.remove( Integer.valueOf( index ) );
}
assertEquals( reduced_size, set.size() );
set.compact();
int compacted_length = set._set.length;
assertFalse( max_length + " should != " + compacted_length,
max_length == compacted_length );
}
public void testDisabledAutoCompact() {
int max_size = 10000;
int reduced_size = 100;
THashSet<Integer> set = new THashSet<Integer>( max_size, 1.0f );
set.setAutoCompactionFactor( 0.0f ); // Disable
for ( int index = 1; index <= max_size; index++ ) {
set.add( Integer.valueOf( index ) );
}
assertEquals( max_size, set.size() );
int max_length = set._set.length;
for ( int index = max_size; index > reduced_size; index-- ) {
set.remove( Integer.valueOf( index ) );
}
assertEquals( reduced_size, set.size() );
int uncompacted_length = set._set.length;
assertEquals( max_length, uncompacted_length );
set.compact();
int compacted_length = set._set.length;
assertFalse( uncompacted_length + " should != " + compacted_length,
uncompacted_length == compacted_length );
}
// in this junk class, all instances hash to the same
// address, but some objects claim to be equal where
// others do not.
public static class Crap {
public boolean equals( Object other ) {
return other instanceof Crap;
}
public int hashCode() {
return System.identityHashCode( this );
}
}
public static void main( String[] args ) throws Exception {
junit.textui.TestRunner.run( new THashSetTest( "testBadlyWrittenObject" ) );
}
} // THashSetTests
|