1 /////////////////////////////////////////////////////////////////////////////// 2 // Copyright (c) 2002, Eric D. Friedman All Rights Reserved. 3 // Copyright (c) 2009, Robert D. Eden All Rights Reserved. 4 // Copyright (c) 2009, Jeff Randall All Rights Reserved. 5 // 6 // This library is free software; you can redistribute it and/or 7 // modify it under the terms of the GNU Lesser General Public 8 // License as published by the Free Software Foundation; either 9 // version 2.1 of the License, or (at your option) any later version. 10 // 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU Lesser General Public 17 // License along with this program; if not, write to the Free Software 18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 /////////////////////////////////////////////////////////////////////////////// 20 21 package gnu.trove.decorator; 22 23 import gnu.trove.map.TIntObjectMap; 24 import gnu.trove.map.hash.TIntObjectHashMap; 25 import gnu.trove.TDecorators; 26 import gnu.trove.set.TIntSet; 27 import gnu.trove.set.hash.TIntHashSet; 28 29 import junit.framework.TestCase; 30 31 import java.io.ByteArrayOutputStream; 32 import java.io.ObjectOutputStream; 33 import java.io.ByteArrayInputStream; 34 import java.io.ObjectInputStream; 35 import java.util.*; 36 37 38 /** 39 * Test the primitive key/Object value map decorators 40 * 41 * @author Eric D. Friedman 42 * @author Robert D. Eden 43 * @author Jeff Randall 44 */ 45 public class TPrimitiveObjectMapDecoratorTest extends TestCase { 46 TPrimitiveObjectMapDecoratorTest(String name)47 public TPrimitiveObjectMapDecoratorTest(String name) { 48 super(name); 49 } 50 51 testConstructors()52 public void testConstructors() { 53 int element_count = 20; 54 int[] keys = new int[element_count]; 55 String[] vals = new String[element_count]; 56 57 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 58 for (int i = 0; i < element_count; i++) { 59 keys[i] = i + 1; 60 vals[i] = Integer.toString(i + 1); 61 raw_map.put(keys[i], vals[i]); 62 } 63 Map<Integer, String> map = TDecorators.wrap(raw_map); 64 65 TIntObjectMap<String> raw_capacity = 66 new TIntObjectHashMap<String>(20); 67 for (int i = 0; i < element_count; i++) { 68 raw_capacity.put(keys[i], vals[i]); 69 } 70 Map<Integer, String> capacity = TDecorators.wrap(raw_capacity); 71 assertEquals(map, capacity); 72 73 TIntObjectMap<String> raw_cap_and_factor = 74 new TIntObjectHashMap<String>(20, 0.75f); 75 for (int i = 0; i < element_count; i++) { 76 raw_cap_and_factor.put(keys[i], vals[i]); 77 } 78 Map<Integer, String> cap_and_factor = TDecorators.wrap(raw_cap_and_factor); 79 assertEquals(map, cap_and_factor); 80 81 TIntObjectMap<String> raw_fully_specified = 82 new TIntObjectHashMap<String>(20, 0.75f, Integer.MIN_VALUE); 83 for (int i = 0; i < element_count; i++) { 84 raw_fully_specified.put(keys[i], vals[i]); 85 } 86 Map<Integer, String> fully_specified = TDecorators.wrap(raw_fully_specified); 87 assertEquals(map, fully_specified); 88 89 TIntObjectMap<String> raw_copy = 90 new TIntObjectHashMap<String>(raw_map); 91 Map<Integer, String> copy = TDecorators.wrap(raw_copy); 92 assertEquals(map, copy); 93 } 94 95 testGet()96 public void testGet() { 97 int element_count = 20; 98 int[] keys = new int[element_count]; 99 String[] vals = new String[element_count]; 100 101 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 102 for (int i = 0; i < element_count; i++) { 103 keys[i] = i + 1; 104 vals[i] = Integer.toString(i + 1); 105 raw_map.put(keys[i], vals[i]); 106 } 107 Map<Integer, String> map = TDecorators.wrap(raw_map); 108 109 assertEquals(vals[10], map.get(Integer.valueOf(keys[10]))); 110 assertNull(map.get(Integer.valueOf(1138))); 111 112 Integer key = Integer.valueOf(1138); 113 map.put(key, null); 114 assertTrue(map.containsKey(key)); 115 assertNull(map.get(key)); 116 117 Long long_key = Long.valueOf(1138); 118 //noinspection SuspiciousMethodCalls 119 assertNull(map.get(long_key)); 120 121 map.put(null, "null-key"); 122 assertEquals("null-key", map.get(null)); 123 } 124 125 testContainsKey()126 public void testContainsKey() { 127 int element_count = 20; 128 int[] keys = new int[element_count]; 129 String[] vals = new String[element_count]; 130 131 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 132 for (int i = 0; i < element_count; i++) { 133 keys[i] = i + 1; 134 vals[i] = Integer.toString(i + 1); 135 raw_map.put(keys[i], vals[i]); 136 } 137 Map<Integer, String> map = TDecorators.wrap(raw_map); 138 139 for (int i = 0; i < element_count; i++) { 140 assertTrue("Key should be present: " + keys[i] + ", map: " + map, 141 map.containsKey(keys[i])); 142 } 143 144 int key = 1138; 145 assertFalse("Key should not be present: " + key + ", map: " + map, 146 map.containsKey(key)); 147 } 148 149 testContainsValue()150 public void testContainsValue() { 151 int element_count = 20; 152 int[] keys = new int[element_count]; 153 String[] vals = new String[element_count]; 154 155 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 156 for (int i = 0; i < element_count; i++) { 157 keys[i] = i + 1; 158 vals[i] = Integer.toString(i + 1); 159 raw_map.put(keys[i], vals[i]); 160 } 161 Map<Integer, String> map = TDecorators.wrap(raw_map); 162 163 for (int i = 0; i < element_count; i++) { 164 assertTrue("Value should be present: " + vals[i] + ", map: " + map, 165 map.containsValue(vals[i])); 166 } 167 168 String val = "1138"; 169 assertFalse("Key should not be present: " + val + ", map: " + map, 170 map.containsValue(val)); 171 172 //noinspection SuspiciousMethodCalls 173 assertFalse("Random object should not be present in map: " + map, 174 map.containsValue(new Object())); 175 } 176 177 testRemove()178 public void testRemove() { 179 int element_count = 20; 180 int[] keys = new int[element_count]; 181 String[] vals = new String[element_count]; 182 183 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 184 for (int i = 0; i < element_count; i++) { 185 keys[i] = i + 1; 186 vals[i] = Integer.toString(i + 1); 187 raw_map.put(keys[i], vals[i]); 188 } 189 Map<Integer, String> map = TDecorators.wrap(raw_map); 190 191 for (int i = 0; i < element_count; i++) { 192 if (i % 2 == 1) { 193 assertEquals("Remove should have modified map: " + keys[i] + ", map: " + map, 194 vals[i], map.remove(keys[i])); 195 } 196 } 197 198 for (int i = 0; i < element_count; i++) { 199 if (i % 2 == 1) { 200 assertTrue("Removed key still in map: " + keys[i] + ", map: " + map, 201 map.get(keys[i]) == null); 202 } else { 203 assertTrue("Key should still be in map: " + keys[i] + ", map: " + map, 204 map.get(keys[i]).equals(vals[i])); 205 } 206 } 207 208 assertNull(map.get(1138)); 209 //noinspection SuspiciousMethodCalls 210 assertNull(map.get(Integer.valueOf(1138))); 211 assertNull(map.get(null)); 212 213 map.put(null, "null-value"); 214 assertEquals("null-value", raw_map.get(raw_map.getNoEntryKey())); 215 assertTrue(map.containsKey(null)); 216 String value = map.get(null); 217 assertEquals("value: " + value, "null-value", value); 218 assertEquals("null-value", map.remove(null)); 219 assertFalse(map.containsKey(null)); 220 221 //noinspection SuspiciousMethodCalls 222 assertNull(map.remove(Long.valueOf(1138))); 223 } 224 225 testPutAllMap()226 public void testPutAllMap() { 227 int element_count = 20; 228 int[] keys = new int[element_count]; 229 String[] vals = new String[element_count]; 230 231 TIntObjectMap<String> raw_control = new TIntObjectHashMap<String>(); 232 for (int i = 0; i < element_count; i++) { 233 keys[i] = i + 1; 234 vals[i] = Integer.toString(i + 1); 235 raw_control.put(keys[i], vals[i]); 236 } 237 Map<Integer, String> control = TDecorators.wrap(raw_control); 238 239 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 240 Map<Integer, String> map = TDecorators.wrap(raw_map); 241 242 Map<Integer, String> source = new HashMap<Integer, String>(); 243 for (int i = 0; i < element_count; i++) { 244 source.put(keys[i], vals[i]); 245 } 246 247 map.putAll(source); 248 assertEquals(control, map); 249 } 250 251 testPutAll()252 public void testPutAll() throws Exception { 253 TIntObjectMap<String> raw_t = new TIntObjectHashMap<String>(); 254 Map<Integer, String> t = TDecorators.wrap(raw_t); 255 TIntObjectMap<String> raw_m = new TIntObjectHashMap<String>(); 256 Map<Integer, String> m = TDecorators.wrap(raw_m); 257 m.put(2, "one"); 258 m.put(4, "two"); 259 m.put(6, "three"); 260 261 t.put(5, "four"); 262 assertEquals(1, t.size()); 263 264 t.putAll(m); 265 assertEquals(4, t.size()); 266 assertEquals("two", t.get(4)); 267 } 268 269 testClear()270 public void testClear() { 271 int element_count = 20; 272 int[] keys = new int[element_count]; 273 String[] vals = new String[element_count]; 274 275 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 276 for (int i = 0; i < element_count; i++) { 277 keys[i] = i + 1; 278 vals[i] = Integer.toString(i + 1); 279 raw_map.put(keys[i], vals[i]); 280 } 281 Map<Integer, String> map = TDecorators.wrap(raw_map); 282 assertEquals(element_count, map.size()); 283 284 map.clear(); 285 assertTrue(map.isEmpty()); 286 assertEquals(0, map.size()); 287 288 assertNull(map.get(keys[5])); 289 } 290 291 292 @SuppressWarnings({"ToArrayCallWithZeroLengthArrayArgument"}) testKeySet()293 public void testKeySet() { 294 int element_count = 20; 295 int[] keys = new int[element_count]; 296 String[] vals = new String[element_count]; 297 298 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 299 for (int i = 0; i < element_count; i++) { 300 keys[i] = i + 1; 301 vals[i] = Integer.toString(i + 1); 302 raw_map.put(keys[i], vals[i]); 303 } 304 Map<Integer, String> map = TDecorators.wrap(raw_map); 305 assertEquals(element_count, map.size()); 306 307 Set<Integer> keyset = map.keySet(); 308 for (int i = 0; i < keyset.size(); i++) { 309 assertTrue(keyset.contains(keys[i])); 310 } 311 assertFalse(keyset.isEmpty()); 312 313 Object[] keys_object_array = keyset.toArray(); 314 int count = 0; 315 Iterator<Integer> iter = keyset.iterator(); 316 while (iter.hasNext()) { 317 int key = iter.next(); 318 assertTrue(keyset.contains(key)); 319 assertEquals(keys_object_array[count], key); 320 count++; 321 } 322 323 Integer[] keys_array = keyset.toArray(new Integer[0]); 324 count = 0; 325 iter = keyset.iterator(); 326 while (iter.hasNext()) { 327 Integer key = iter.next(); 328 assertTrue(keyset.contains(key)); 329 assertEquals(keys_array[count], key); 330 count++; 331 } 332 333 keys_array = keyset.toArray(new Integer[keyset.size()]); 334 count = 0; 335 iter = keyset.iterator(); 336 while (iter.hasNext()) { 337 Integer key = iter.next(); 338 assertTrue(keyset.contains(key)); 339 assertEquals(keys_array[count], key); 340 count++; 341 } 342 343 keys_array = keyset.toArray(new Integer[keyset.size() * 2]); 344 count = 0; 345 iter = keyset.iterator(); 346 while (iter.hasNext()) { 347 Integer key = iter.next(); 348 assertTrue(keyset.contains(key)); 349 assertEquals(keys_array[count], key); 350 count++; 351 } 352 assertNull(keys_array[keyset.size()]); 353 354 TIntSet raw_other = new TIntHashSet(keyset); 355 Set<Integer> other = TDecorators.wrap(raw_other); 356 assertFalse(keyset.retainAll(other)); 357 other.remove(keys[5]); 358 assertTrue(keyset.retainAll(other)); 359 assertFalse(keyset.contains(keys[5])); 360 assertFalse(map.containsKey(keys[5])); 361 362 keyset.clear(); 363 assertTrue(keyset.isEmpty()); 364 365 366 } 367 368 testKeySetAdds()369 public void testKeySetAdds() { 370 int element_count = 20; 371 int[] keys = new int[element_count]; 372 String[] vals = new String[element_count]; 373 374 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 375 for (int i = 0; i < element_count; i++) { 376 keys[i] = i + 1; 377 vals[i] = Integer.toString(i + 1); 378 raw_map.put(keys[i], vals[i]); 379 } 380 Map<Integer, String> map = TDecorators.wrap(raw_map); 381 assertEquals(element_count, map.size()); 382 383 Set<Integer> keyset = map.keySet(); 384 for (int i = 0; i < keyset.size(); i++) { 385 assertTrue(keyset.contains(keys[i])); 386 } 387 assertFalse(keyset.isEmpty()); 388 389 try { 390 keyset.add(1138); 391 fail("Expected UnsupportedOperationException"); 392 } catch (UnsupportedOperationException ex) { 393 // Expected 394 } 395 396 try { 397 Set<Integer> test = new HashSet<Integer>(); 398 test.add(Integer.valueOf(1138)); 399 keyset.addAll(test); 400 fail("Expected UnsupportedOperationException"); 401 } catch (UnsupportedOperationException ex) { 402 // Expected 403 } 404 405 try { 406 Set<Integer> test = new HashSet<Integer>(); 407 test.add(1138); 408 keyset.addAll(test); 409 fail("Expected UnsupportedOperationException"); 410 } catch (UnsupportedOperationException ex) { 411 // Expected 412 } 413 } 414 415 416 @SuppressWarnings({"ToArrayCallWithZeroLengthArrayArgument"}) testKeys()417 public void testKeys() { 418 int element_count = 20; 419 int[] keys = new int[element_count]; 420 String[] vals = new String[element_count]; 421 422 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 423 for (int i = 0; i < element_count; i++) { 424 keys[i] = i + 1; 425 vals[i] = Integer.toString(i + 1); 426 raw_map.put(keys[i], vals[i]); 427 } 428 Map<Integer, String> map = TDecorators.wrap(raw_map); 429 assertEquals(element_count, map.size()); 430 431 // No argument 432 Integer[] keys_array = map.keySet().toArray(new Integer[map.size()]); 433 assertEquals(element_count, keys_array.length); 434 List<Integer> keys_list = Arrays.asList(keys_array); 435 for (int i = 0; i < element_count; i++) { 436 assertTrue(keys_list.contains(keys[i])); 437 } 438 439 // Zero length array 440 //noinspection ToArrayCallWithZeroLengthArrayArgument 441 keys_array = map.keySet().toArray(new Integer[0]); 442 assertEquals(element_count, keys_array.length); 443 keys_list = Arrays.asList(keys_array); 444 for (int i = 0; i < element_count; i++) { 445 assertTrue(keys_list.contains(keys[i])); 446 } 447 448 // appropriate length array 449 keys_array = map.keySet().toArray(new Integer[map.size()]); 450 assertEquals(element_count, keys_array.length); 451 keys_list = Arrays.asList(keys_array); 452 for (int i = 0; i < element_count; i++) { 453 assertTrue(keys_list.contains(keys[i])); 454 } 455 456 // longer array 457 keys_array = map.keySet().toArray(new Integer[element_count * 2]); 458 assertEquals(element_count * 2, keys_array.length); 459 keys_list = Arrays.asList(keys_array); 460 for (int i = 0; i < element_count; i++) { 461 assertTrue(keys_list.contains(keys[i])); 462 } 463 assertNull(keys_array[element_count]); 464 } 465 466 testValueCollectionToArray()467 public void testValueCollectionToArray() { 468 int element_count = 20; 469 int[] keys = new int[element_count]; 470 String[] vals = new String[element_count]; 471 472 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 473 for (int i = 0; i < element_count; i++) { 474 keys[i] = i + 1; 475 vals[i] = Integer.toString(i + 1); 476 raw_map.put(keys[i], vals[i]); 477 } 478 Map<Integer, String> map = TDecorators.wrap(raw_map); 479 assertEquals(element_count, map.size()); 480 481 Collection<String> collection = map.values(); 482 for (int i = 0; i < collection.size(); i++) { 483 assertTrue(collection.contains(vals[i])); 484 } 485 assertFalse(collection.isEmpty()); 486 487 Object[] values_obj_array = collection.toArray(); 488 int count = 0; 489 Iterator<String> iter = collection.iterator(); 490 while (iter.hasNext()) { 491 String value = iter.next(); 492 assertTrue(collection.contains(value)); 493 assertEquals(values_obj_array[count], value); 494 count++; 495 } 496 497 //noinspection ToArrayCallWithZeroLengthArrayArgument 498 String[] values_array = collection.toArray(new String[0]); 499 count = 0; 500 iter = collection.iterator(); 501 while (iter.hasNext()) { 502 String value = iter.next(); 503 assertTrue(collection.contains(value)); 504 assertEquals(values_array[count], value); 505 count++; 506 } 507 508 values_array = collection.toArray(new String[collection.size()]); 509 count = 0; 510 iter = collection.iterator(); 511 while (iter.hasNext()) { 512 String value = iter.next(); 513 assertTrue(collection.contains(value)); 514 assertEquals(values_array[count], value); 515 count++; 516 } 517 518 values_array = collection.toArray(new String[collection.size() * 2]); 519 count = 0; 520 iter = collection.iterator(); 521 while (iter.hasNext()) { 522 String value = iter.next(); 523 assertTrue(collection.contains(value)); 524 assertEquals(values_array[count], value); 525 count++; 526 } 527 assertNull(values_array[collection.size()]); 528 assertNull(values_array[collection.size()]); 529 530 Collection<String> other = new ArrayList<String>(collection); 531 assertFalse(collection.retainAll(other)); 532 other.remove(vals[5]); 533 assertTrue(collection.retainAll(other)); 534 assertFalse(collection.contains(vals[5])); 535 assertFalse(map.containsKey(keys[5])); 536 537 collection.clear(); 538 assertTrue(collection.isEmpty()); 539 } 540 541 testValueCollectionAdds()542 public void testValueCollectionAdds() { 543 int element_count = 20; 544 int[] keys = new int[element_count]; 545 String[] vals = new String[element_count]; 546 547 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 548 for (int i = 0; i < element_count; i++) { 549 keys[i] = i + 1; 550 vals[i] = Integer.toString(i + 1); 551 raw_map.put(keys[i], vals[i]); 552 } 553 Map<Integer, String> map = TDecorators.wrap(raw_map); 554 assertEquals(element_count, map.size()); 555 556 Collection<String> collection = map.values(); 557 for (int i = 0; i < collection.size(); i++) { 558 assertTrue(collection.contains(vals[i])); 559 } 560 assertFalse(collection.isEmpty()); 561 562 try { 563 collection.add("1138"); 564 fail("Expected UnsupportedOperationException"); 565 } catch (UnsupportedOperationException ex) { 566 // Expected 567 } 568 569 try { 570 Set<String> test = new HashSet<String>(); 571 test.add("1138"); 572 collection.addAll(test); 573 fail("Expected UnsupportedOperationException"); 574 } catch (UnsupportedOperationException ex) { 575 // Expected 576 } 577 578 try { 579 Collection<String> test = new ArrayList<String>(); 580 test.add("1138"); 581 collection.addAll(test); 582 fail("Expected UnsupportedOperationException"); 583 } catch (UnsupportedOperationException ex) { 584 // Expected 585 } 586 587 try { 588 collection.addAll(Arrays.asList(vals)); 589 fail("Expected UnsupportedOperationException"); 590 } catch (UnsupportedOperationException ex) { 591 // Expected 592 } 593 } 594 595 testValueCollectionContainsAll()596 public void testValueCollectionContainsAll() { 597 int element_count = 20; 598 int[] keys = new int[element_count]; 599 String[] vals = new String[element_count]; 600 601 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 602 for (int i = 0; i < element_count; i++) { 603 keys[i] = i + 1; 604 vals[i] = Integer.toString(i + 1); 605 raw_map.put(keys[i], vals[i]); 606 } 607 Map<Integer, String> map = TDecorators.wrap(raw_map); 608 assertEquals(element_count, map.size()); 609 610 Collection<String> collection = map.values(); 611 for (int i = 0; i < collection.size(); i++) { 612 assertTrue(collection.contains(vals[i])); 613 } 614 assertFalse(collection.isEmpty()); 615 616 List<String> java_list = new ArrayList<String>(); 617 java_list.addAll(Arrays.asList(vals)); 618 assertTrue("collection: " + collection + ", should contain all in list: " + 619 java_list, collection.containsAll(java_list)); 620 java_list.add(String.valueOf(1138)); 621 assertFalse("collection: " + collection + ", should not contain all in list: " + 622 java_list, collection.containsAll(java_list)); 623 624 List<CharSequence> number_list = new ArrayList<CharSequence>(); 625 for (String value : vals) { 626 if (value.equals("5")) { 627 number_list.add(new StringBuilder().append(value)); 628 } else { 629 number_list.add(String.valueOf(value)); 630 } 631 } 632 assertFalse("collection: " + collection + ", should not contain all in list: " + 633 java_list, collection.containsAll(number_list)); 634 635 Collection<String> other = new ArrayList<String>(collection); 636 assertTrue("collection: " + collection + ", should contain all in other: " + 637 other, collection.containsAll(other)); 638 other.add("1138"); 639 assertFalse("collection: " + collection + ", should not contain all in other: " + 640 other, collection.containsAll(other)); 641 } 642 643 testValueCollectionRetainAllCollection()644 public void testValueCollectionRetainAllCollection() { 645 int element_count = 20; 646 int[] keys = new int[element_count]; 647 String[] vals = new String[element_count]; 648 649 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 650 for (int i = 0; i < element_count; i++) { 651 keys[i] = i + 1; 652 vals[i] = Integer.toString(i + 1); 653 raw_map.put(keys[i], vals[i]); 654 } 655 Map<Integer, String> map = TDecorators.wrap(raw_map); 656 assertEquals(element_count, map.size()); 657 658 Collection<String> collection = map.values(); 659 for (int i = 0; i < collection.size(); i++) { 660 assertTrue(collection.contains(vals[i])); 661 } 662 assertFalse(collection.isEmpty()); 663 664 List<String> java_list = new ArrayList<String>(); 665 java_list.addAll(Arrays.asList(vals)); 666 assertFalse("collection: " + collection + ", should contain all in list: " + 667 java_list, collection.retainAll(java_list)); 668 669 java_list.remove(5); 670 assertTrue("collection: " + collection + ", should contain all in list: " + 671 java_list, collection.retainAll(java_list)); 672 assertFalse(collection.contains(vals[5])); 673 assertFalse(map.containsKey(keys[5])); 674 assertFalse(map.containsValue(vals[5])); 675 assertTrue("collection: " + collection + ", should contain all in list: " + 676 java_list, collection.containsAll(java_list)); 677 } 678 679 testValueCollectionRetainAllTCollection()680 public void testValueCollectionRetainAllTCollection() { 681 int element_count = 20; 682 int[] keys = new int[element_count]; 683 String[] vals = new String[element_count]; 684 685 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 686 for (int i = 0; i < element_count; i++) { 687 keys[i] = i + 1; 688 vals[i] = Integer.toString(i + 1); 689 raw_map.put(keys[i], vals[i]); 690 } 691 Map<Integer, String> map = TDecorators.wrap(raw_map); 692 assertEquals(element_count, map.size()); 693 694 Collection<String> collection = map.values(); 695 for (int i = 0; i < collection.size(); i++) { 696 assertTrue(collection.contains(vals[i])); 697 } 698 assertFalse(collection.isEmpty()); 699 700 assertFalse("collection: " + collection + ", should be unmodified.", 701 collection.retainAll(collection)); 702 703 Collection<String> other = new ArrayList<String>(collection); 704 assertFalse("collection: " + collection + ", should be unmodified. other: " + 705 other, collection.retainAll(other)); 706 707 other.remove(vals[5]); 708 assertTrue("collection: " + collection + ", should be modified. other: " + 709 other, collection.retainAll(other)); 710 assertFalse(collection.contains(vals[5])); 711 assertFalse(map.containsKey(keys[5])); 712 assertFalse(map.containsValue(vals[5])); 713 assertTrue("collection: " + collection + ", should contain all in other: " + 714 other, collection.containsAll(other)); 715 } 716 717 testValueCollectionRemoveAllCollection()718 public void testValueCollectionRemoveAllCollection() { 719 int element_count = 20; 720 int[] keys = new int[element_count]; 721 String[] vals = new String[element_count]; 722 723 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 724 for (int i = 0; i < element_count; i++) { 725 keys[i] = i + 1; 726 vals[i] = Integer.toString(i + 1); 727 raw_map.put(keys[i], vals[i]); 728 } 729 Map<Integer, String> map = TDecorators.wrap(raw_map); 730 assertEquals(element_count, map.size()); 731 732 Collection<String> collection = map.values(); 733 for (int i = 0; i < collection.size(); i++) { 734 assertTrue(collection.contains(vals[i])); 735 } 736 assertFalse(collection.isEmpty()); 737 738 List<String> java_list = new ArrayList<String>(); 739 assertFalse("collection: " + collection + ", should contain all in list: " + 740 java_list, collection.removeAll(java_list)); 741 742 java_list.add(vals[5]); 743 assertTrue("collection: " + collection + ", should contain all in list: " + 744 java_list, collection.removeAll(java_list)); 745 assertFalse(collection.contains(vals[5])); 746 assertFalse(map.containsKey(keys[5])); 747 assertFalse(map.containsValue(vals[5])); 748 749 java_list = new ArrayList<String>(); 750 java_list.addAll(Arrays.asList(vals)); 751 assertTrue("collection: " + collection + ", should contain all in list: " + 752 java_list, collection.removeAll(java_list)); 753 assertTrue(collection.isEmpty()); 754 } 755 756 testValueCollectionRemoveAllTCollection()757 public void testValueCollectionRemoveAllTCollection() { 758 int element_count = 20; 759 int[] keys = new int[element_count]; 760 String[] vals = new String[element_count]; 761 762 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 763 for (int i = 0; i < element_count; i++) { 764 keys[i] = i + 1; 765 vals[i] = Integer.toString(i + 1); 766 raw_map.put(keys[i], vals[i]); 767 } 768 Map<Integer, String> map = TDecorators.wrap(raw_map); 769 assertEquals(element_count, map.size()); 770 771 Collection<String> collection = map.values(); 772 for (int i = 0; i < collection.size(); i++) { 773 assertTrue(collection.contains(vals[i])); 774 } 775 assertFalse(collection.isEmpty()); 776 777 Collection<String> other = new ArrayList<String>(); 778 assertFalse("collection: " + collection + ", should be unmodified.", 779 collection.removeAll(other)); 780 781 other = new ArrayList<String>(collection); 782 other.remove(vals[5]); 783 assertTrue("collection: " + collection + ", should be modified. other: " + 784 other, collection.removeAll(other)); 785 assertEquals(1, collection.size()); 786 for (int i = 0; i < element_count; i++) { 787 if (i == 5) { 788 assertTrue(collection.contains(vals[i])); 789 assertTrue(map.containsKey(keys[i])); 790 assertTrue(map.containsValue(vals[i])); 791 } else { 792 assertFalse(collection.contains(vals[i])); 793 assertFalse(map.containsKey(keys[i])); 794 assertFalse(map.containsValue(vals[i])); 795 } 796 } 797 798 assertFalse("collection: " + collection + ", should be unmodified. other: " + 799 other, collection.removeAll(other)); 800 801 assertTrue("collection: " + collection + ", should be modified. other: " + 802 other, collection.removeAll(collection)); 803 assertTrue(collection.isEmpty()); 804 } 805 806 testValues()807 public void testValues() { 808 int element_count = 20; 809 int[] keys = new int[element_count]; 810 String[] vals = new String[element_count]; 811 812 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 813 for (int i = 0; i < element_count; i++) { 814 keys[i] = i + 1; 815 vals[i] = Integer.toString(i + 1); 816 raw_map.put(keys[i], vals[i]); 817 } 818 Map<Integer, String> map = TDecorators.wrap(raw_map); 819 assertEquals(element_count, map.size()); 820 821 // No argument 822 Object[] values_object_array = map.values().toArray(); 823 assertEquals(element_count, values_object_array.length); 824 List<Object> values_object_list = Arrays.asList(values_object_array); 825 for (int i = 0; i < element_count; i++) { 826 assertTrue(values_object_list.contains(vals[i])); 827 } 828 829 // Zero length array 830 //noinspection ToArrayCallWithZeroLengthArrayArgument 831 String[] values_array = map.values().toArray(new String[0]); 832 assertEquals(element_count, values_array.length); 833 List<String> values_list = Arrays.asList(values_array); 834 for (int i = 0; i < element_count; i++) { 835 assertTrue(values_list.contains(vals[i])); 836 } 837 838 // appropriate length array 839 values_array = map.values().toArray(new String[map.size()]); 840 assertEquals(element_count, values_array.length); 841 values_list = Arrays.asList(values_array); 842 for (int i = 0; i < element_count; i++) { 843 assertTrue(values_list.contains(vals[i])); 844 } 845 846 // longer array 847 values_array = map.values().toArray(new String[element_count * 2]); 848 assertEquals(element_count * 2, values_array.length); 849 values_list = Arrays.asList(values_array); 850 for (int i = 0; i < element_count; i++) { 851 assertTrue(values_list.contains(vals[i])); 852 } 853 assertEquals(null, values_array[element_count]); 854 } 855 856 testEntrySet()857 public void testEntrySet() { 858 int element_count = 20; 859 Integer[] keys = new Integer[element_count]; 860 String[] vals = new String[element_count]; 861 862 TIntObjectMap<String> raw_map = 863 new TIntObjectHashMap<String>(element_count, 0.5f, Integer.MIN_VALUE); 864 Map<Integer, String> map = TDecorators.wrap(raw_map); 865 866 for (int i = 0; i < element_count; i++) { 867 keys[i] = Integer.valueOf(i + 1); 868 vals[i] = Integer.toString(i + 1); 869 map.put(keys[i], vals[i]); 870 } 871 assertEquals(element_count, map.size()); 872 873 Set<Map.Entry<Integer, String>> entries = map.entrySet(); 874 assertEquals(element_count, entries.size()); 875 assertFalse(entries.isEmpty()); 876 //noinspection unchecked 877 Map.Entry<Integer, String>[] array = 878 entries.toArray(new Map.Entry[entries.size()]); 879 for (Map.Entry<Integer, String> entry : array) { 880 assertTrue(entries.contains(entry)); 881 } 882 assertFalse(entries.contains(null)); 883 884 assertEquals(array[0].hashCode(), array[0].hashCode()); 885 assertTrue(array[0].hashCode() != array[1].hashCode()); 886 887 assertTrue(array[0].equals(array[0])); 888 assertFalse(array[0].equals(array[1])); 889 Integer key = array[0].getKey(); 890 Integer old_value = Integer.valueOf(array[0].getValue()); 891 assertEquals(Integer.toString(old_value), 892 array[0].setValue(Integer.toString(old_value * 2))); 893 assertEquals(Integer.toString(old_value * 2), map.get(key)); 894 assertEquals(Integer.toString(old_value * 2), array[0].getValue()); 895 896 // Adds are not allowed 897 Map.Entry<Integer, String> invalid_entry = new Map.Entry<Integer, String>() { 898 public Integer getKey() { 899 return null; 900 } 901 902 public String getValue() { 903 return null; 904 } 905 906 public String setValue(String value) { 907 return null; 908 } 909 }; 910 List<Map.Entry<Integer, String>> invalid_entry_list = 911 new ArrayList<Map.Entry<Integer, String>>(); 912 invalid_entry_list.add(invalid_entry); 913 914 try { 915 entries.add(invalid_entry); 916 fail("Expected OperationUnsupportedException"); 917 } catch (UnsupportedOperationException ex) { 918 // Expected 919 } 920 921 try { 922 entries.addAll(invalid_entry_list); 923 fail("Expected OperationUnsupportedException"); 924 } catch (UnsupportedOperationException ex) { 925 // Expected 926 } 927 928 assertFalse(entries.containsAll(invalid_entry_list)); 929 assertFalse(entries.removeAll(invalid_entry_list)); 930 931 List<Map.Entry<Integer, String>> partial_list = 932 new ArrayList<Map.Entry<Integer, String>>(); 933 partial_list.add(array[3]); 934 partial_list.add(array[4]); 935 assertTrue(entries.removeAll(partial_list)); 936 assertEquals(element_count - 2, entries.size()); 937 assertEquals(element_count - 2, map.size()); 938 939 entries.clear(); 940 assertTrue(entries.isEmpty()); 941 assertTrue(map.isEmpty()); 942 } 943 944 testEquals()945 public void testEquals() { 946 int element_count = 20; 947 int[] keys = new int[element_count]; 948 String[] vals = new String[element_count]; 949 950 TIntObjectMap<String> raw_map = 951 new TIntObjectHashMap<String>(element_count, 0.5f, Integer.MIN_VALUE); 952 for (int i = 0; i < element_count; i++) { 953 keys[i] = i + 1; 954 vals[i] = Integer.toString(i + 1); 955 raw_map.put(keys[i], vals[i]); 956 } 957 Map<Integer, String> map = TDecorators.wrap(raw_map); 958 assertEquals(element_count, map.size()); 959 960 TIntObjectHashMap<String> raw_fully_specified = 961 new TIntObjectHashMap<String>(20, 0.75f, Integer.MIN_VALUE); 962 for (int i = 0; i < element_count; i++) { 963 raw_fully_specified.put(keys[i], vals[i]); 964 } 965 Map<Integer, String> fully_specified = TDecorators.wrap(raw_fully_specified); 966 assertEquals(map, fully_specified); 967 968 assertFalse("shouldn't equal random object", map.equals(new Object())); 969 970 assertSame(raw_map, ((TIntObjectMapDecorator) map).getMap()); 971 } 972 973 974 @SuppressWarnings({"unchecked"}) testSerialize()975 public void testSerialize() throws Exception { 976 int element_count = 20; 977 int[] keys = new int[element_count]; 978 String[] vals = new String[element_count]; 979 980 TIntObjectMap<String> raw_map = new TIntObjectHashMap<String>(); 981 for (int i = 0; i < element_count; i++) { 982 keys[i] = i + 1; 983 vals[i] = Integer.toString(i + 1); 984 raw_map.put(keys[i], vals[i]); 985 } 986 Map<Integer, String> map = TDecorators.wrap(raw_map); 987 988 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 989 ObjectOutputStream oos = new ObjectOutputStream(baos); 990 oos.writeObject(map); 991 992 ByteArrayInputStream bias = new ByteArrayInputStream(baos.toByteArray()); 993 ObjectInputStream ois = new ObjectInputStream(bias); 994 995 Map<Integer, String> deserialized = (Map<Integer, String>) ois.readObject(); 996 997 assertEquals(map, deserialized); 998 } 999 1000 testToString()1001 public void testToString() { 1002 TIntObjectHashMap<String> raw_map = new TIntObjectHashMap<String>(); 1003 Map<Integer, String> map = TDecorators.wrap(raw_map); 1004 map.put(11, "One"); 1005 map.put(22, "Two"); 1006 1007 String to_string = map.toString(); 1008 assertTrue(to_string, to_string.equals("{11=One, 22=Two}") 1009 || to_string.equals("{22=Two, 11=One}")); 1010 } 1011 testBug3432175()1012 public void testBug3432175() throws Exception { 1013 Map<Integer, Object> trove = new TIntObjectMapDecorator<Object>(new TIntObjectHashMap<Object>()); 1014 trove.put(null, new Object()); 1015 assertFalse(trove.isEmpty()); 1016 assertEquals(1, trove.size()); 1017 assertEquals(1, trove.entrySet().size()); 1018 assertEquals(1, trove.keySet().size()); 1019 assertEquals(null, trove.keySet().iterator().next()); 1020 } 1021 } 1022