001: /*
002: * FindBugs - Find Bugs in Java programs
003: * Copyright (C) 2006, University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs.gui2;
021:
022: import java.util.Collections;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * @author pugh
028: */
029: public class HashListTest extends TestCase {
030:
031: /**
032: *
033: */
034: private static final BadHasher ZERO = new BadHasher(0);
035: private static final BadHasher ONE = new BadHasher(1);
036:
037: private static final BadHasher TWO = new BadHasher(2);
038:
039: private static final BadHasher THREE = new BadHasher(3);
040:
041: public void testAdd() {
042: HashList<Integer> lst = new HashList<Integer>();
043: lst.add(0, 1);
044: lst.add(0, 2);
045: lst.add(0, 3);
046: assertEquals(2, lst.indexOf(1));
047: assertEquals(1, lst.indexOf(2));
048: assertEquals(0, lst.indexOf(3));
049: }
050:
051: public void testBadHasher() {
052: HashList<BadHasher> lst = new HashList<BadHasher>();
053: lst.add(0, ZERO);
054: lst.add(0, ONE);
055: lst.add(1, TWO);
056: // lst.add(0,THREE);
057: assertEquals(2, lst.indexOf(ZERO));
058: assertEquals(0, lst.indexOf(ONE));
059: assertEquals(1, lst.indexOf(TWO));
060: }
061:
062: static class BadHasher {
063: int value;
064:
065: public BadHasher(int value) {
066: this .value = value;
067: }
068:
069: @Override
070: public String toString() {
071: return Integer.toString(value);
072: }
073:
074: @Override
075: public int hashCode() {
076: return 42;
077: }
078:
079: @Override
080: public boolean equals(Object v) {
081: if (!(v instanceof BadHasher))
082: return false;
083: return value == ((BadHasher) v).value;
084: }
085: }
086:
087: public void testRetainAllEmptu() {
088: HashList<Integer> lst = new HashList<Integer>();
089: lst.add(0, 1);
090: lst.add(0, 2);
091: lst.add(0, 3);
092: lst.retainAll(Collections.emptySet());
093: assertEquals(0, lst.size());
094:
095: }
096:
097: public void testRetainAllEven() {
098: HashList<BadHasher> lst = new HashList<BadHasher>();
099: for (int i = 0; i < 20; i++)
100: lst.add(new BadHasher(i));
101: HashList<BadHasher> even = new HashList<BadHasher>();
102: for (int i = 0; i < 20; i += 2)
103: even.add(new BadHasher(i));
104: lst.retainAll(even);
105: assertEquals(10, lst.size());
106:
107: }
108:
109: }
|