0001: /*
0002: * Copyright 2001-2004 The Apache Software Foundation
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.apache.commons.collections;
0017:
0018: import java.util.Collection;
0019: import java.util.ConcurrentModificationException;
0020: import java.util.HashMap;
0021: import java.util.HashSet;
0022: import java.util.Iterator;
0023: import java.util.LinkedList;
0024: import java.util.Map;
0025: import java.util.NoSuchElementException;
0026: import java.util.Set;
0027:
0028: import junit.framework.Test;
0029:
0030: import org.apache.commons.collections.map.AbstractTestMap;
0031:
0032: /**
0033: * Class TestDoubleOrderedMap
0034: * <p>
0035: * Test cases for DoubleOrderedMap. This class cannot
0036: * implement TestMap.SupportsPut, because it is a special
0037: * Map that does not support duplicate keys, duplicate
0038: * values, or null values.
0039: *
0040: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
0041: *
0042: * @author Marc Johnson
0043: * @author Stephen Colebourne
0044: */
0045: public class TestDoubleOrderedMap extends AbstractTestMap {
0046:
0047: /**
0048: * constructor
0049: *
0050: * @param name
0051: */
0052: public TestDoubleOrderedMap(final String name) {
0053: super (name);
0054: }
0055:
0056: /**
0057: * create a suite of the tests in this class
0058: *
0059: * @return the test suite
0060: */
0061: public static Test suite() {
0062: return BulkTest.makeSuite(TestDoubleOrderedMap.class);
0063: }
0064:
0065: /**
0066: * The default comparator in double ordered map does not allow null keys.
0067: **/
0068: public boolean isAllowNullKey() {
0069: return false;
0070: }
0071:
0072: /**
0073: * The default comparator in double ordered map does not allow null keys,
0074: * and values are keys in this map.
0075: **/
0076: public boolean isAllowNullValue() {
0077: return false;
0078: }
0079:
0080: /**
0081: * Double ordered map does not support duplicate values
0082: **/
0083: public boolean isAllowDuplicateValues() {
0084: return false;
0085: }
0086:
0087: /**
0088: * Change the Map.put() test because it tries put with the same key
0089: * which is invalid in the modified double ordered map contract. (The
0090: * DoubleOrderedMap documentation states that an IllegalArgumentException
0091: * is thrown when a key is tried to be put into the map again. This
0092: * differs from the standard Map contract which would replace the value
0093: * for that key and return it.
0094: */
0095: public boolean isPutChangeSupported() {
0096: return false;
0097: }
0098:
0099: /**
0100: * setValue() is not supported as it can change the map.
0101: */
0102: public boolean isSetValueSupported() {
0103: return false;
0104: }
0105:
0106: public Map makeEmptyMap() {
0107: return new DoubleOrderedMap();
0108: }
0109:
0110: protected Map makeMap() {
0111: return new DoubleOrderedMap();
0112: }
0113:
0114: /**
0115: * test size() method
0116: */
0117: public void testSize() {
0118:
0119: Map m = makeMap();
0120:
0121: assertEquals(0, m.size());
0122:
0123: LocalTestNode nodes[] = makeLocalNodes();
0124:
0125: for (int k = 0; k < nodes.length; k++) {
0126: m.put(nodes[k].getKey(), nodes[k].getValue());
0127: assertEquals(k + 1, m.size());
0128: }
0129:
0130: int count = m.size();
0131:
0132: for (int k = 0; k < nodes.length; k++) {
0133: m.remove(nodes[k].getKey());
0134:
0135: --count;
0136:
0137: assertEquals(count, m.size());
0138:
0139: // failed remove should not affect size
0140: m.remove(nodes[k].getKey());
0141: assertEquals(count, m.size());
0142: }
0143: }
0144:
0145: /**
0146: * test IsEmpty() method
0147: */
0148: public void testIsEmpty() {
0149:
0150: Map m = makeMap();
0151:
0152: assertTrue(m.isEmpty());
0153:
0154: LocalTestNode nodes[] = makeLocalNodes();
0155:
0156: for (int k = 0; k < nodes.length; k++) {
0157: m.put(nodes[k].getKey(), nodes[k].getValue());
0158: assertTrue(!m.isEmpty());
0159: }
0160:
0161: int count = m.size();
0162:
0163: for (int k = 0; k < nodes.length; k++) {
0164: m.remove(nodes[k].getKey());
0165:
0166: --count;
0167:
0168: if (count == 0) {
0169: assertTrue(m.isEmpty());
0170: } else {
0171: assertTrue(!m.isEmpty());
0172: }
0173:
0174: // failed remove should not affect emptiness
0175: m.remove(nodes[k].getKey());
0176:
0177: if (count == 0) {
0178: assertTrue(m.isEmpty());
0179: } else {
0180: assertTrue(!m.isEmpty());
0181: }
0182: }
0183: }
0184:
0185: /**
0186: * test containsKey() method
0187: */
0188: public void testContainsKey() {
0189:
0190: Map m = makeMap();
0191:
0192: try {
0193: m.containsKey(new Object());
0194: fail("should have caught ClassCastException");
0195: } catch (ClassCastException ignored) {
0196: }
0197:
0198: try {
0199: m.containsKey(null);
0200: fail("should have caught NullPointerException");
0201: } catch (NullPointerException ignored) {
0202: }
0203:
0204: assertTrue(!m.containsKey("foo"));
0205:
0206: LocalTestNode nodes[] = makeLocalNodes();
0207:
0208: for (int k = 0; k < nodes.length; k++) {
0209: m.put(nodes[k].getKey(), nodes[k]);
0210: assertTrue(m.containsKey(nodes[k].getKey()));
0211: }
0212:
0213: assertTrue(!m.containsKey(new Integer(-1)));
0214:
0215: try {
0216: m.containsKey("foo");
0217: fail("Should have caught ClassCastException");
0218: } catch (ClassCastException ignored) {
0219: }
0220:
0221: for (int k = 0; k < nodes.length; k++) {
0222: m.remove(nodes[k].getKey());
0223: assertTrue(!m.containsKey(nodes[k].getKey()));
0224: }
0225: }
0226:
0227: /**
0228: * test containsValue() method
0229: */
0230: public void testContainsValue() {
0231:
0232: Map m = (DoubleOrderedMap) makeMap();
0233: LocalTestNode nodes[] = makeLocalNodes();
0234:
0235: for (int k = 0; k < nodes.length; k++) {
0236: m.put(nodes[k].getKey(), nodes[k]);
0237: assertTrue(m.containsValue(nodes[k]));
0238: }
0239:
0240: for (int k = 0; k < nodes.length; k++) {
0241: m.remove(nodes[k].getKey());
0242: assertTrue(!m.containsValue(nodes[k]));
0243: }
0244: }
0245:
0246: /**
0247: * test get() method
0248: */
0249: public void testGet() {
0250:
0251: Map m = makeMap();
0252:
0253: try {
0254: m.get(new Object());
0255: fail("should have caught ClassCastException");
0256: } catch (ClassCastException ignored) {
0257: }
0258:
0259: try {
0260: m.get(null);
0261: fail("should have caught NullPointerException");
0262: } catch (NullPointerException ignored) {
0263: }
0264:
0265: assertNull(m.get("foo"));
0266:
0267: LocalTestNode nodes[] = makeLocalNodes();
0268:
0269: for (int k = 0; k < nodes.length; k++) {
0270: m.put(nodes[k].getKey(), nodes[k]);
0271: assertSame(m.get(nodes[k].getKey()), nodes[k]);
0272: }
0273:
0274: assertNull(m.get(new Integer(-1)));
0275:
0276: try {
0277: m.get("foo");
0278: fail("Should have caught ClassCastException");
0279: } catch (ClassCastException ignored) {
0280: }
0281:
0282: for (int k = 0; k < nodes.length; k++) {
0283: assertNotNull(m.get(nodes[k].getKey()));
0284: m.remove(nodes[k].getKey());
0285: assertNull(m.get(nodes[k].getKey()));
0286: }
0287: }
0288:
0289: /**
0290: * test put() method
0291: */
0292: public void testPut() {
0293:
0294: Map m = makeMap();
0295:
0296: try {
0297: m.put(new Object(), "foo");
0298: fail("should have caught ClassCastException");
0299: } catch (ClassCastException ignored) {
0300: }
0301:
0302: try {
0303: m.put(null, "foo");
0304: fail("should have caught NullPointerException");
0305: } catch (NullPointerException ignored) {
0306: }
0307:
0308: try {
0309: m.put("foo", null);
0310: fail("should have caught NullPointerException");
0311: } catch (NullPointerException ignored) {
0312: }
0313:
0314: try {
0315: m.put("foo", new Object());
0316: fail("should have caught ClassCastException");
0317: } catch (ClassCastException ignored) {
0318: }
0319:
0320: LocalTestNode[] nodes = makeLocalNodes();
0321:
0322: for (int k = 0; k < nodes.length; k++) {
0323: assertNull(m.put(nodes[k].getKey(), nodes[k].getValue()));
0324:
0325: try {
0326: m.put(nodes[k].getKey(), "foo");
0327: } catch (IllegalArgumentException ignored) {
0328: }
0329: }
0330: }
0331:
0332: /**
0333: * test remove() method
0334: */
0335: public void testRemove() {
0336:
0337: DoubleOrderedMap m = (DoubleOrderedMap) makeMap();
0338: LocalTestNode nodes[] = makeLocalNodes();
0339:
0340: for (int k = 0; k < nodes.length; k++) {
0341: m.put(nodes[k].getKey(), nodes[k]);
0342: }
0343:
0344: try {
0345: m.remove(null);
0346: fail("should have caught NullPointerException");
0347: } catch (NullPointerException ignored) {
0348: }
0349:
0350: try {
0351: m.remove(new Object());
0352: fail("should have caught ClassCastException");
0353: } catch (ClassCastException ignored) {
0354: }
0355:
0356: assertNull(m.remove(new Integer(-1)));
0357:
0358: try {
0359: m.remove("foo");
0360: fail("should have caught ClassCastException");
0361: } catch (ClassCastException ignored) {
0362: }
0363:
0364: for (int k = 0; k < nodes.length; k += 2) {
0365: Comparable key = nodes[k].getKey();
0366:
0367: assertNotNull(m.get(key));
0368: assertSame(nodes[k], m.remove(key));
0369: assertNull(m.remove(key));
0370: assertNull(m.get(key));
0371: }
0372:
0373: for (int k = 1; k < nodes.length; k += 2) {
0374: Comparable key = nodes[k].getKey();
0375:
0376: assertNotNull(m.get(key));
0377: assertSame(nodes[k], m.remove(key));
0378: assertNull(m.remove(key));
0379: assertNull(m.get(key));
0380: }
0381:
0382: assertTrue(m.isEmpty());
0383: }
0384:
0385: /**
0386: * Method testPutAll
0387: */
0388: public void testPutAll() {
0389:
0390: Map m = (DoubleOrderedMap) makeMap();
0391: LocalTestNode nodes[] = makeLocalNodes();
0392:
0393: for (int k = 0; k < nodes.length; k++) {
0394: m.put(nodes[k].getKey(), nodes[k]);
0395: }
0396:
0397: Map m1 = new HashMap();
0398:
0399: m1.put(null, "foo");
0400:
0401: try {
0402: m.putAll(m1);
0403: fail("Should have caught NullPointerException");
0404: } catch (NullPointerException ignored) {
0405: }
0406:
0407: m1 = new HashMap();
0408:
0409: m1.put(new Object(), "bar");
0410:
0411: try {
0412: m.putAll(m1);
0413: fail("Should have caught ClassCastException");
0414: } catch (ClassCastException ignored) {
0415: }
0416:
0417: m1 = new HashMap();
0418:
0419: m1.put("fubar", null);
0420:
0421: try {
0422: m.putAll(m1);
0423: fail("Should have caught NullPointerException");
0424: } catch (NullPointerException ignored) {
0425: }
0426:
0427: m1 = new HashMap();
0428:
0429: m1.put("fubar", new Object());
0430:
0431: try {
0432: m.putAll(m1);
0433: fail("Should have caught ClassCastException");
0434: } catch (ClassCastException ignored) {
0435: }
0436:
0437: assertEquals(nodes.length, m.size());
0438:
0439: m = (DoubleOrderedMap) makeMap();
0440: m1 = new HashMap();
0441:
0442: for (int k = 0; k < nodes.length; k++) {
0443: m1.put(nodes[k].getKey(), nodes[k].getValue());
0444: }
0445:
0446: m.putAll(m1);
0447: assertEquals(nodes.length, m.size());
0448:
0449: for (int k = 0; k < nodes.length; k++) {
0450: assertSame(nodes[k].getValue(), m.get(nodes[k].getKey()));
0451: }
0452: }
0453:
0454: /**
0455: * test clear() method
0456: */
0457: public void testClear() {
0458:
0459: Map m = (DoubleOrderedMap) makeMap();
0460: LocalTestNode nodes[] = makeLocalNodes();
0461:
0462: for (int k = 0; k < nodes.length; k++) {
0463: m.put(nodes[k].getKey(), nodes[k].getValue());
0464: assertTrue(!m.isEmpty());
0465: }
0466:
0467: assertTrue(!m.isEmpty());
0468:
0469: for (int k = 0; k < nodes.length; k++) {
0470: assertTrue(m.containsKey(nodes[k].getKey()));
0471: assertTrue(m.containsValue(nodes[k].getValue()));
0472: }
0473:
0474: m.clear();
0475: assertTrue(m.isEmpty());
0476:
0477: for (int k = 0; k < nodes.length; k++) {
0478: assertTrue(!m.containsKey(nodes[k].getKey()));
0479: assertTrue(!m.containsValue(nodes[k].getValue()));
0480: }
0481: }
0482:
0483: /**
0484: * test keySet() method
0485: */
0486: public void testKeySet() {
0487:
0488: testKeySet((DoubleOrderedMap) makeMap());
0489:
0490: Map m = (DoubleOrderedMap) makeMap();
0491: LocalTestNode nodes[] = makeLocalNodes();
0492:
0493: for (int k = 0; k < nodes.length; k++) {
0494: m.put(nodes[k].getKey(), nodes[k]);
0495: }
0496:
0497: testKeySet(m);
0498:
0499: m = (DoubleOrderedMap) makeMap();
0500:
0501: for (int k = 0; k < nodes.length; k++) {
0502: m.put(nodes[k].getKey(), nodes[k]);
0503: }
0504:
0505: int count = m.size();
0506:
0507: for (Iterator iter = m.keySet().iterator(); iter.hasNext();) {
0508: iter.next();
0509: iter.remove();
0510:
0511: --count;
0512:
0513: assertEquals(count, m.size());
0514: }
0515:
0516: assertTrue(m.isEmpty());
0517:
0518: m = (DoubleOrderedMap) makeMap();
0519:
0520: for (int k = 0; k < nodes.length; k++) {
0521: m.put(nodes[k].getKey(), nodes[k]);
0522: }
0523:
0524: Set s = m.keySet();
0525:
0526: try {
0527: s.remove(null);
0528: fail("should have caught NullPointerException");
0529: } catch (NullPointerException ignored) {
0530: }
0531:
0532: try {
0533: s.remove(new Object());
0534: fail("should have caught ClassCastException");
0535: } catch (ClassCastException ignored) {
0536: }
0537:
0538: for (int k = 0; k < nodes.length; k++) {
0539: Comparable key = nodes[k].getKey();
0540:
0541: assertTrue(s.remove(key));
0542: assertTrue(!s.contains(key));
0543: assertTrue(!m.containsKey(key));
0544: assertTrue(!m.containsValue(nodes[k]));
0545: }
0546:
0547: assertTrue(m.isEmpty());
0548:
0549: m = (DoubleOrderedMap) makeMap();
0550:
0551: Collection c1 = new LinkedList();
0552: Collection c2 = new LinkedList();
0553:
0554: c2.add(new Integer(-99));
0555:
0556: for (int k = 0; k < nodes.length; k++) {
0557: m.put(nodes[k].getKey(), nodes[k]);
0558: c1.add(nodes[k].getKey());
0559: c2.add(nodes[k].getKey());
0560: }
0561:
0562: assertTrue(m.keySet().containsAll(c1));
0563: assertTrue(!m.keySet().containsAll(c2));
0564:
0565: m = (DoubleOrderedMap) makeMap();
0566: c1 = new LinkedList();
0567:
0568: c1.add(new Integer(-55));
0569:
0570: try {
0571: m.keySet().addAll(c1);
0572: fail("should have caught exception of addAll()");
0573: } catch (UnsupportedOperationException ignored) {
0574: }
0575:
0576: for (int k = 0; k < nodes.length; k++) {
0577: m.put(nodes[k].getKey(), nodes[k]);
0578: c1.add(nodes[k].getKey());
0579: }
0580:
0581: assertTrue(!m.keySet().retainAll(c1));
0582: assertEquals(nodes.length, m.size());
0583:
0584: m = (DoubleOrderedMap) makeMap();
0585: c1 = new LinkedList();
0586:
0587: for (int k = 0; k < nodes.length; k++) {
0588: m.put(nodes[k].getKey(), nodes[k]);
0589:
0590: if (k % 2 == 1) {
0591: c1.add(nodes[k].getKey());
0592: }
0593: }
0594:
0595: assertTrue(m.keySet().retainAll(c1));
0596: assertEquals(nodes.length / 2, m.size());
0597:
0598: m = (DoubleOrderedMap) makeMap();
0599: c1 = new LinkedList();
0600:
0601: for (int k = 0; k < nodes.length; k++) {
0602: m.put(nodes[k].getKey(), nodes[k]);
0603: }
0604:
0605: assertTrue(m.keySet().retainAll(c1));
0606: assertEquals(0, m.size());
0607:
0608: m = (DoubleOrderedMap) makeMap();
0609: c1 = new LinkedList();
0610:
0611: for (int k = 0; k < nodes.length; k++) {
0612: m.put(nodes[k].getKey(), nodes[k]);
0613: }
0614:
0615: assertTrue(!m.keySet().removeAll(c1));
0616: assertEquals(nodes.length, m.size());
0617:
0618: m = (DoubleOrderedMap) makeMap();
0619: c1 = new LinkedList();
0620:
0621: for (int k = 0; k < nodes.length; k++) {
0622: m.put(nodes[k].getKey(), nodes[k]);
0623:
0624: if (k % 2 == 0) {
0625: c1.add(nodes[k].getKey());
0626: }
0627: }
0628:
0629: assertTrue(m.keySet().removeAll(c1));
0630: assertEquals(nodes.length / 2, m.size());
0631:
0632: m = (DoubleOrderedMap) makeMap();
0633: c1 = new LinkedList();
0634:
0635: for (int k = 0; k < nodes.length; k++) {
0636: m.put(nodes[k].getKey(), nodes[k]);
0637: c1.add(nodes[k].getKey());
0638: }
0639:
0640: assertTrue(m.keySet().removeAll(c1));
0641: assertEquals(0, m.size());
0642:
0643: m = (DoubleOrderedMap) makeMap();
0644:
0645: for (int k = 0; k < nodes.length; k++) {
0646: m.put(nodes[k].getKey(), nodes[k]);
0647: }
0648:
0649: m.keySet().clear();
0650: assertEquals(0, m.size());
0651: }
0652:
0653: /**
0654: * test values() method
0655: */
0656: public void testValues() {
0657:
0658: testValues((DoubleOrderedMap) makeMap());
0659:
0660: Map m = (DoubleOrderedMap) makeMap();
0661: LocalTestNode nodes[] = makeLocalNodes();
0662:
0663: for (int k = 0; k < nodes.length; k++) {
0664: m.put(nodes[k].getKey(), nodes[k]);
0665: }
0666:
0667: testValues(m);
0668:
0669: m = (DoubleOrderedMap) makeMap();
0670:
0671: for (int k = 0; k < nodes.length; k++) {
0672: m.put(nodes[k].getKey(), nodes[k]);
0673: }
0674:
0675: int count = m.size();
0676:
0677: for (Iterator iter = m.values().iterator(); iter.hasNext();) {
0678: iter.next();
0679: iter.remove();
0680:
0681: --count;
0682:
0683: assertEquals(count, m.size());
0684: }
0685:
0686: assertTrue(m.isEmpty());
0687:
0688: m = (DoubleOrderedMap) makeMap();
0689:
0690: for (int k = 0; k < nodes.length; k++) {
0691: m.put(nodes[k].getKey(), nodes[k]);
0692: }
0693:
0694: count = m.size();
0695:
0696: Collection s = m.values();
0697:
0698: for (int k = 0; k < count; k++) {
0699: assertTrue(s.remove(nodes[k]));
0700: assertTrue(!s.contains(nodes[k]));
0701: assertTrue(!m.containsKey(nodes[k].getKey()));
0702: assertTrue(!m.containsValue(nodes[k]));
0703: }
0704:
0705: assertTrue(m.isEmpty());
0706:
0707: m = (DoubleOrderedMap) makeMap();
0708:
0709: Collection c1 = new LinkedList();
0710: Collection c2 = new LinkedList();
0711:
0712: c2.add(new LocalTestNode(-123));
0713:
0714: for (int k = 0; k < nodes.length; k++) {
0715: m.put(nodes[k].getKey(), nodes[k]);
0716: c1.add(nodes[k]);
0717: c2.add(nodes[k]);
0718: }
0719:
0720: assertTrue(m.values().containsAll(c1));
0721: assertTrue(!m.values().containsAll(c2));
0722:
0723: m = (DoubleOrderedMap) makeMap();
0724: c1 = new LinkedList();
0725:
0726: for (int k = 0; k < nodes.length; k++) {
0727: m.put(nodes[k].getKey(), nodes[k]);
0728: c1.add(nodes[k]);
0729: }
0730:
0731: try {
0732: m.values().addAll(c1);
0733: fail("should have caught exception of addAll()");
0734: } catch (UnsupportedOperationException ignored) {
0735: }
0736:
0737: m = (DoubleOrderedMap) makeMap();
0738: c1 = new LinkedList();
0739:
0740: for (int k = 0; k < nodes.length; k++) {
0741: m.put(nodes[k].getKey(), nodes[k]);
0742: c1.add(nodes[k]);
0743: }
0744:
0745: assertTrue(!m.values().retainAll(c1));
0746: assertEquals(nodes.length, m.size());
0747:
0748: m = (DoubleOrderedMap) makeMap();
0749: c1 = new LinkedList();
0750:
0751: for (int k = 0; k < nodes.length; k++) {
0752: m.put(nodes[k].getKey(), nodes[k]);
0753:
0754: if (k % 2 == 1) {
0755: c1.add(nodes[k]);
0756: }
0757: }
0758:
0759: assertTrue(m.values().retainAll(c1));
0760: assertEquals(nodes.length / 2, m.size());
0761:
0762: m = (DoubleOrderedMap) makeMap();
0763: c1 = new LinkedList();
0764:
0765: for (int k = 0; k < nodes.length; k++) {
0766: m.put(nodes[k].getKey(), nodes[k]);
0767: }
0768:
0769: assertTrue(m.values().retainAll(c1));
0770: assertEquals(0, m.size());
0771:
0772: m = (DoubleOrderedMap) makeMap();
0773: c1 = new LinkedList();
0774:
0775: for (int k = 0; k < nodes.length; k++) {
0776: m.put(nodes[k].getKey(), nodes[k]);
0777: }
0778:
0779: assertTrue(!m.values().removeAll(c1));
0780: assertEquals(nodes.length, m.size());
0781:
0782: m = (DoubleOrderedMap) makeMap();
0783: c1 = new LinkedList();
0784:
0785: for (int k = 0; k < nodes.length; k++) {
0786: m.put(nodes[k].getKey(), nodes[k]);
0787:
0788: if (k % 2 == 0) {
0789: c1.add(nodes[k]);
0790: }
0791: }
0792:
0793: assertTrue(m.values().removeAll(c1));
0794: assertEquals(nodes.length / 2, m.size());
0795:
0796: m = (DoubleOrderedMap) makeMap();
0797: c1 = new LinkedList();
0798:
0799: for (int k = 0; k < nodes.length; k++) {
0800: m.put(nodes[k].getKey(), nodes[k]);
0801: c1.add(nodes[k]);
0802: }
0803:
0804: assertTrue(m.values().removeAll(c1));
0805: assertEquals(0, m.size());
0806:
0807: m = (DoubleOrderedMap) makeMap();
0808:
0809: for (int k = 0; k < nodes.length; k++) {
0810: m.put(nodes[k].getKey(), nodes[k]);
0811: }
0812:
0813: m.values().clear();
0814: assertEquals(0, m.size());
0815: }
0816:
0817: /**
0818: * test entrySet() method
0819: */
0820: public void testEntrySet() {
0821:
0822: testEntrySet((DoubleOrderedMap) makeMap());
0823:
0824: Map m = (DoubleOrderedMap) makeMap();
0825: LocalTestNode nodes[] = makeLocalNodes();
0826:
0827: for (int k = 0; k < nodes.length; k++) {
0828: m.put(nodes[k].getKey(), nodes[k]);
0829: }
0830:
0831: testEntrySet(m);
0832:
0833: m = (DoubleOrderedMap) makeMap();
0834:
0835: for (int k = 0; k < nodes.length; k++) {
0836: m.put(nodes[k].getKey(), nodes[k]);
0837: }
0838:
0839: try {
0840: ((Map.Entry) m.entrySet().iterator().next())
0841: .setValue(new LocalTestNode(-1));
0842: fail("Should have caught UnsupportedOperationException");
0843: } catch (UnsupportedOperationException ignored) {
0844: }
0845:
0846: int count = m.size();
0847:
0848: for (Iterator iter = m.entrySet().iterator(); iter.hasNext();) {
0849: iter.next();
0850: iter.remove();
0851:
0852: --count;
0853:
0854: assertEquals(count, m.size());
0855: }
0856:
0857: assertTrue(m.isEmpty());
0858:
0859: m = (DoubleOrderedMap) makeMap();
0860:
0861: Collection c1 = new LinkedList();
0862:
0863: for (int k = 0; k < nodes.length; k++) {
0864: m.put(nodes[k].getKey(), nodes[k]);
0865: c1.add(nodes[k].getKey());
0866: }
0867:
0868: try {
0869: m.entrySet().addAll(c1);
0870: fail("should have caught exception of addAll()");
0871: } catch (UnsupportedOperationException ignored) {
0872: }
0873:
0874: m = (DoubleOrderedMap) makeMap();
0875:
0876: for (int k = 0; k < nodes.length; k++) {
0877: m.put(nodes[k].getKey(), nodes[k]);
0878: }
0879:
0880: m.entrySet().clear();
0881: assertEquals(0, m.size());
0882:
0883: m = (DoubleOrderedMap) makeMap();
0884:
0885: for (int k = 0; k < nodes.length; k++) {
0886: m.put(nodes[k].getKey(), nodes[k]);
0887: }
0888:
0889: int x = 0;
0890:
0891: for (Iterator iter = m.entrySet().iterator(); iter.hasNext();) {
0892: Map.Entry entry = (Map.Entry) iter.next();
0893:
0894: assertSame(entry.getKey(), nodes[x].getKey());
0895: assertSame(entry.getValue(), nodes[x]);
0896:
0897: x++;
0898: }
0899: }
0900:
0901: /**
0902: * Method testEquals
0903: */
0904: public void testEquals() {
0905:
0906: Map m = (DoubleOrderedMap) makeMap();
0907: LocalTestNode nodes[] = makeLocalNodes();
0908:
0909: for (int k = 0; k < nodes.length; k++) {
0910: m.put(nodes[k].getKey(), nodes[k]);
0911: }
0912:
0913: assertTrue(!m.equals(null));
0914: assertEquals(m, m);
0915:
0916: Map m1 = new HashMap();
0917:
0918: for (int k = 0; k < nodes.length; k++) {
0919: m1.put(nodes[k].getKey(), nodes[k]);
0920: }
0921:
0922: assertEquals(m, m1);
0923:
0924: m1 = (DoubleOrderedMap) makeMap();
0925:
0926: for (int k = 0; k < (nodes.length - 1); k++) {
0927: m1.put(nodes[k].getKey(), nodes[k]);
0928: }
0929:
0930: assertTrue(!m.equals(m1));
0931:
0932: m1 = (DoubleOrderedMap) makeMap();
0933:
0934: for (int k = 0; k < nodes.length; k++) {
0935: m1.put(nodes[k].getKey(), nodes[k]);
0936: }
0937:
0938: LocalTestNode node1 = new LocalTestNode(-1000);
0939:
0940: m1.put(node1.getKey(), node1);
0941: assertTrue(!m.equals(m1));
0942:
0943: m1 = (DoubleOrderedMap) makeMap();
0944:
0945: for (int k = 0; k < nodes.length; k++) {
0946: m1.put(nodes[k].getKey(), nodes[nodes.length - (k + 1)]);
0947: }
0948:
0949: assertTrue(!m.equals(m1));
0950:
0951: m1 = (DoubleOrderedMap) makeMap();
0952:
0953: for (int k = nodes.length - 1; k >= 0; k--) {
0954: m1.put(nodes[k].getKey(), nodes[k]);
0955: }
0956:
0957: assertEquals(m, m1);
0958: }
0959:
0960: /**
0961: * test hashCode() method
0962: */
0963: public void testHashCode() {
0964:
0965: Map m = (DoubleOrderedMap) makeMap();
0966: LocalTestNode nodes[] = makeLocalNodes();
0967:
0968: for (int k = 0; k < nodes.length; k++) {
0969: m.put(nodes[k].getKey(), nodes[k]);
0970: }
0971:
0972: Map m1 = (DoubleOrderedMap) makeMap();
0973:
0974: for (int k = nodes.length - 1; k >= 0; k--) {
0975: m1.put(nodes[k].getKey(), nodes[k]);
0976: }
0977:
0978: assertEquals(m.hashCode(), m1.hashCode());
0979: }
0980:
0981: /**
0982: * test constructors
0983: */
0984: public void testConstructors() {
0985:
0986: DoubleOrderedMap m = (DoubleOrderedMap) makeMap();
0987:
0988: assertTrue(m.isEmpty());
0989:
0990: DoubleOrderedMap m1 = new DoubleOrderedMap(m);
0991:
0992: assertTrue(m1.isEmpty());
0993:
0994: m1 = (DoubleOrderedMap) makeMap();
0995:
0996: LocalTestNode nodes[] = makeLocalNodes();
0997:
0998: for (int k = 0; k < nodes.length; k++) {
0999: m1.put(nodes[k].getKey(), nodes[k]);
1000: }
1001:
1002: m = new DoubleOrderedMap(m1);
1003:
1004: assertEquals(m, m1);
1005:
1006: Map m2 = new HashMap();
1007:
1008: for (int k = 0; k < nodes.length; k++) {
1009: m2.put(nodes[k].getKey(), nodes[k]);
1010: }
1011:
1012: m = new DoubleOrderedMap(m2);
1013:
1014: assertEquals(m, m2);
1015:
1016: // reject duplicated values
1017: m2 = new HashMap();
1018:
1019: m2.put("1", "foo");
1020: m2.put("2", "foo");
1021:
1022: try {
1023: m = new DoubleOrderedMap(m2);
1024:
1025: fail("Should have caught IllegalArgumentException");
1026: } catch (IllegalArgumentException ignored) {
1027: }
1028:
1029: // reject null values
1030: m2.put("2", null);
1031:
1032: try {
1033: m = new DoubleOrderedMap(m2);
1034:
1035: fail("Should have caught NullPointerException");
1036: } catch (NullPointerException ignored) {
1037: }
1038:
1039: // reject non-Comparable values
1040: m2.put("2", new Object());
1041:
1042: try {
1043: m = new DoubleOrderedMap(m2);
1044:
1045: fail("Should have caught ClassCastException");
1046: } catch (ClassCastException ignored) {
1047: }
1048:
1049: // reject incompatible values
1050: m2.put("2", new Integer(2));
1051:
1052: try {
1053: m = new DoubleOrderedMap(m2);
1054:
1055: fail("Should have caught ClassCastException");
1056: } catch (ClassCastException ignored) {
1057: }
1058:
1059: // reject incompatible keys
1060: m2.remove("2");
1061: m2.put(new Integer(2), "bad key");
1062:
1063: try {
1064: m = new DoubleOrderedMap(m2);
1065:
1066: fail("Should have caught ClassCastException");
1067: } catch (ClassCastException ignored) {
1068: }
1069:
1070: // reject non-Comparable keys
1071: m2.clear();
1072: m2.put("1", "foo");
1073: m2.put(new Object(), "bad key");
1074:
1075: try {
1076: m = new DoubleOrderedMap(m2);
1077:
1078: fail("Should have caught ClassCastException");
1079: } catch (ClassCastException ignored) {
1080: }
1081: }
1082:
1083: /**
1084: * test getKeyForValue() method
1085: */
1086: public void testGetKeyForValue() {
1087:
1088: DoubleOrderedMap m = (DoubleOrderedMap) makeMap();
1089:
1090: try {
1091: m.getKeyForValue(new Object());
1092: fail("should have caught ClassCastException");
1093: } catch (ClassCastException ignored) {
1094: }
1095:
1096: try {
1097: m.getKeyForValue(null);
1098: fail("should have caught NullPointerException");
1099: } catch (NullPointerException ignored) {
1100: }
1101:
1102: assertNull(m.getKeyForValue("foo"));
1103:
1104: LocalTestNode nodes[] = makeLocalNodes();
1105:
1106: for (int k = 0; k < nodes.length; k++) {
1107: m.put(nodes[k].getKey(), nodes[k]);
1108: assertSame(m.getKeyForValue(nodes[k]), nodes[k].getKey());
1109: }
1110:
1111: assertNull(m.getKeyForValue(new LocalTestNode(-1)));
1112:
1113: try {
1114: m.getKeyForValue("foo");
1115: fail("Should have caught ClassCastException");
1116: } catch (ClassCastException ignored) {
1117: }
1118:
1119: for (int k = 0; k < nodes.length; k++) {
1120: assertNotNull(m.getKeyForValue(nodes[k]));
1121: m.remove(nodes[k].getKey());
1122: assertNull(m.getKeyForValue(nodes[k]));
1123: }
1124: }
1125:
1126: /**
1127: * test removeValue() method
1128: */
1129: public void testRemoveValue() {
1130:
1131: DoubleOrderedMap m = (DoubleOrderedMap) makeMap();
1132: LocalTestNode nodes[] = makeLocalNodes();
1133:
1134: for (int k = 0; k < nodes.length; k++) {
1135: m.put(nodes[k].getKey(), nodes[k]);
1136: }
1137:
1138: try {
1139: m.removeValue(null);
1140: fail("should have caught NullPointerException");
1141: } catch (NullPointerException ignored) {
1142: }
1143:
1144: try {
1145: m.removeValue(new Object());
1146: fail("should have caught ClassCastException");
1147: } catch (ClassCastException ignored) {
1148: }
1149:
1150: assertNull(m.remove(new Integer(-1)));
1151:
1152: try {
1153: m.removeValue("foo");
1154: fail("should have caught ClassCastException");
1155: } catch (ClassCastException ignored) {
1156: }
1157:
1158: for (int k = 0; k < nodes.length; k += 2) {
1159: assertNotNull(m.getKeyForValue(nodes[k]));
1160: assertSame(nodes[k].getKey(), m.removeValue(nodes[k]));
1161: assertNull(m.removeValue(nodes[k]));
1162: assertNull(m.getKeyForValue(nodes[k]));
1163: }
1164:
1165: for (int k = 1; k < nodes.length; k += 2) {
1166: assertNotNull(m.getKeyForValue(nodes[k]));
1167: assertSame(nodes[k].getKey(), m.removeValue(nodes[k]));
1168: assertNull(m.removeValue(nodes[k]));
1169: assertNull(m.getKeyForValue(nodes[k]));
1170: }
1171:
1172: assertTrue(m.isEmpty());
1173: }
1174:
1175: /**
1176: * test entrySetByValue() method
1177: */
1178: public void testEntrySetByValue() {
1179:
1180: testEntrySetByValue((DoubleOrderedMap) makeMap());
1181:
1182: DoubleOrderedMap m = (DoubleOrderedMap) makeMap();
1183: LocalTestNode nodes[] = makeLocalNodes();
1184:
1185: for (int k = 0; k < nodes.length; k++) {
1186: m.put(nodes[k].getKey(), nodes[k]);
1187: }
1188:
1189: testEntrySetByValue(m);
1190:
1191: m = (DoubleOrderedMap) makeMap();
1192:
1193: for (int k = 0; k < nodes.length; k++) {
1194: m.put(nodes[k].getKey(), nodes[k]);
1195: }
1196:
1197: try {
1198: ((Map.Entry) m.entrySetByValue().iterator().next())
1199: .setValue(new LocalTestNode(-1));
1200: fail("Should have caught UnsupportedOperationException");
1201: } catch (UnsupportedOperationException ignored) {
1202: }
1203:
1204: int count = m.size();
1205:
1206: for (Iterator iter = m.entrySetByValue().iterator(); iter
1207: .hasNext();) {
1208: iter.next();
1209: iter.remove();
1210:
1211: --count;
1212:
1213: assertEquals(count, m.size());
1214: }
1215:
1216: assertTrue(m.isEmpty());
1217:
1218: m = (DoubleOrderedMap) makeMap();
1219:
1220: Collection c1 = new LinkedList();
1221:
1222: for (int k = 0; k < nodes.length; k++) {
1223: m.put(nodes[k].getKey(), nodes[k]);
1224: c1.add(nodes[k].getKey());
1225: }
1226:
1227: try {
1228: m.entrySetByValue().addAll(c1);
1229: fail("should have caught exception of addAll()");
1230: } catch (UnsupportedOperationException ignored) {
1231: }
1232:
1233: m = (DoubleOrderedMap) makeMap();
1234:
1235: for (int k = 0; k < nodes.length; k++) {
1236: m.put(nodes[k].getKey(), nodes[k]);
1237: }
1238:
1239: m.entrySetByValue().clear();
1240: assertEquals(0, m.size());
1241:
1242: m = (DoubleOrderedMap) makeMap();
1243:
1244: for (int k = 0; k < nodes.length; k++) {
1245: m.put(nodes[k].getKey(), nodes[k]);
1246: }
1247:
1248: int x = 0;
1249:
1250: for (Iterator iter = m.entrySetByValue().iterator(); iter
1251: .hasNext();) {
1252: Map.Entry entry = (Map.Entry) iter.next();
1253:
1254: assertSame(entry.getKey(), nodes[x].getKey());
1255: assertSame(entry.getValue(), nodes[x]);
1256:
1257: x++;
1258: }
1259: }
1260:
1261: /**
1262: * test keySetByValue() method
1263: */
1264: public void testKeySetByValue() {
1265:
1266: testKeySetByValue((DoubleOrderedMap) makeMap());
1267:
1268: DoubleOrderedMap m = (DoubleOrderedMap) makeMap();
1269: LocalTestNode nodes[] = makeLocalNodes();
1270:
1271: for (int k = 0; k < nodes.length; k++) {
1272: m.put(nodes[k].getKey(), nodes[k]);
1273: }
1274:
1275: testKeySetByValue(m);
1276:
1277: m = (DoubleOrderedMap) makeMap();
1278:
1279: for (int k = 0; k < nodes.length; k++) {
1280: m.put(nodes[k].getKey(), nodes[k]);
1281: }
1282:
1283: int count = m.size();
1284:
1285: for (Iterator iter = m.keySetByValue().iterator(); iter
1286: .hasNext();) {
1287: iter.next();
1288: iter.remove();
1289:
1290: --count;
1291:
1292: assertEquals(count, m.size());
1293: }
1294:
1295: assertTrue(m.isEmpty());
1296:
1297: m = (DoubleOrderedMap) makeMap();
1298:
1299: for (int k = 0; k < nodes.length; k++) {
1300: m.put(nodes[k].getKey(), nodes[k]);
1301: }
1302:
1303: Set s = m.keySetByValue();
1304:
1305: try {
1306: s.remove(null);
1307: fail("should have caught NullPointerException");
1308: } catch (NullPointerException ignored) {
1309: }
1310:
1311: try {
1312: s.remove(new Object());
1313: fail("should have caught ClassCastException");
1314: } catch (ClassCastException ignored) {
1315: }
1316:
1317: for (int k = 0; k < nodes.length; k++) {
1318: Comparable key = nodes[k].getKey();
1319:
1320: assertTrue(s.remove(key));
1321: assertTrue(!s.contains(key));
1322: assertTrue(!m.containsKey(key));
1323: assertTrue(!m.containsValue(nodes[k]));
1324: }
1325:
1326: assertTrue(m.isEmpty());
1327:
1328: m = (DoubleOrderedMap) makeMap();
1329:
1330: Collection c1 = new LinkedList();
1331: Collection c2 = new LinkedList();
1332:
1333: c2.add(new Integer(-99));
1334:
1335: for (int k = 0; k < nodes.length; k++) {
1336: m.put(nodes[k].getKey(), nodes[k]);
1337: c1.add(nodes[k].getKey());
1338: c2.add(nodes[k].getKey());
1339: }
1340:
1341: assertTrue(m.keySetByValue().containsAll(c1));
1342: assertTrue(!m.keySetByValue().containsAll(c2));
1343:
1344: m = (DoubleOrderedMap) makeMap();
1345: c1 = new LinkedList();
1346:
1347: c1.add(new Integer(-55));
1348:
1349: try {
1350: m.keySetByValue().addAll(c1);
1351: fail("should have caught exception of addAll()");
1352: } catch (UnsupportedOperationException ignored) {
1353: }
1354:
1355: for (int k = 0; k < nodes.length; k++) {
1356: m.put(nodes[k].getKey(), nodes[k]);
1357: c1.add(nodes[k].getKey());
1358: }
1359:
1360: assertTrue(!m.keySetByValue().retainAll(c1));
1361: assertEquals(nodes.length, m.size());
1362:
1363: m = (DoubleOrderedMap) makeMap();
1364: c1 = new LinkedList();
1365:
1366: for (int k = 0; k < nodes.length; k++) {
1367: m.put(nodes[k].getKey(), nodes[k]);
1368:
1369: if (k % 2 == 1) {
1370: c1.add(nodes[k].getKey());
1371: }
1372: }
1373:
1374: assertTrue(m.keySetByValue().retainAll(c1));
1375: assertEquals(nodes.length / 2, m.size());
1376:
1377: m = (DoubleOrderedMap) makeMap();
1378: c1 = new LinkedList();
1379:
1380: for (int k = 0; k < nodes.length; k++) {
1381: m.put(nodes[k].getKey(), nodes[k]);
1382: }
1383:
1384: assertTrue(m.keySetByValue().retainAll(c1));
1385: assertEquals(0, m.size());
1386:
1387: m = (DoubleOrderedMap) makeMap();
1388: c1 = new LinkedList();
1389:
1390: for (int k = 0; k < nodes.length; k++) {
1391: m.put(nodes[k].getKey(), nodes[k]);
1392: }
1393:
1394: assertTrue(!m.keySetByValue().removeAll(c1));
1395: assertEquals(nodes.length, m.size());
1396:
1397: m = (DoubleOrderedMap) makeMap();
1398: c1 = new LinkedList();
1399:
1400: for (int k = 0; k < nodes.length; k++) {
1401: m.put(nodes[k].getKey(), nodes[k]);
1402:
1403: if (k % 2 == 0) {
1404: c1.add(nodes[k].getKey());
1405: }
1406: }
1407:
1408: assertTrue(m.keySetByValue().removeAll(c1));
1409: assertEquals(nodes.length / 2, m.size());
1410:
1411: m = (DoubleOrderedMap) makeMap();
1412: c1 = new LinkedList();
1413:
1414: for (int k = 0; k < nodes.length; k++) {
1415: m.put(nodes[k].getKey(), nodes[k]);
1416: c1.add(nodes[k].getKey());
1417: }
1418:
1419: assertTrue(m.keySetByValue().removeAll(c1));
1420: assertEquals(0, m.size());
1421:
1422: m = (DoubleOrderedMap) makeMap();
1423:
1424: for (int k = 0; k < nodes.length; k++) {
1425: m.put(nodes[k].getKey(), nodes[k]);
1426: }
1427:
1428: m.keySetByValue().clear();
1429: assertEquals(0, m.size());
1430: }
1431:
1432: /**
1433: * test valuesByValue() method
1434: */
1435: public void testValuesByValue() {
1436:
1437: testValuesByValue((DoubleOrderedMap) makeMap());
1438:
1439: DoubleOrderedMap m = (DoubleOrderedMap) makeMap();
1440: LocalTestNode nodes[] = makeLocalNodes();
1441:
1442: for (int k = 0; k < nodes.length; k++) {
1443: m.put(nodes[k].getKey(), nodes[k]);
1444: }
1445:
1446: testValuesByValue(m);
1447:
1448: m = (DoubleOrderedMap) makeMap();
1449:
1450: for (int k = 0; k < nodes.length; k++) {
1451: m.put(nodes[k].getKey(), nodes[k]);
1452: }
1453:
1454: int count = m.size();
1455:
1456: for (Iterator iter = m.valuesByValue().iterator(); iter
1457: .hasNext();) {
1458: iter.next();
1459: iter.remove();
1460:
1461: --count;
1462:
1463: assertEquals(count, m.size());
1464: }
1465:
1466: assertTrue(m.isEmpty());
1467:
1468: m = (DoubleOrderedMap) makeMap();
1469:
1470: for (int k = 0; k < nodes.length; k++) {
1471: m.put(nodes[k].getKey(), nodes[k]);
1472: }
1473:
1474: count = m.size();
1475:
1476: Collection s = m.valuesByValue();
1477:
1478: for (int k = 0; k < count; k++) {
1479: assertTrue(s.remove(nodes[k]));
1480: assertTrue(!s.contains(nodes[k]));
1481: assertTrue(!m.containsKey(nodes[k].getKey()));
1482: assertTrue(!m.containsValue(nodes[k]));
1483: }
1484:
1485: assertTrue(m.isEmpty());
1486:
1487: m = (DoubleOrderedMap) makeMap();
1488:
1489: Collection c1 = new LinkedList();
1490: Collection c2 = new LinkedList();
1491:
1492: c2.add(new LocalTestNode(-123));
1493:
1494: for (int k = 0; k < nodes.length; k++) {
1495: m.put(nodes[k].getKey(), nodes[k]);
1496: c1.add(nodes[k]);
1497: c2.add(nodes[k]);
1498: }
1499:
1500: assertTrue(m.valuesByValue().containsAll(c1));
1501: assertTrue(!m.valuesByValue().containsAll(c2));
1502:
1503: m = (DoubleOrderedMap) makeMap();
1504: c1 = new LinkedList();
1505:
1506: for (int k = 0; k < nodes.length; k++) {
1507: m.put(nodes[k].getKey(), nodes[k]);
1508: c1.add(nodes[k]);
1509: }
1510:
1511: try {
1512: m.valuesByValue().addAll(c1);
1513: fail("should have caught exception of addAll()");
1514: } catch (UnsupportedOperationException ignored) {
1515: }
1516:
1517: m = (DoubleOrderedMap) makeMap();
1518: c1 = new LinkedList();
1519:
1520: for (int k = 0; k < nodes.length; k++) {
1521: m.put(nodes[k].getKey(), nodes[k]);
1522: c1.add(nodes[k]);
1523: }
1524:
1525: assertTrue(!m.valuesByValue().retainAll(c1));
1526: assertEquals(nodes.length, m.size());
1527:
1528: m = (DoubleOrderedMap) makeMap();
1529: c1 = new LinkedList();
1530:
1531: for (int k = 0; k < nodes.length; k++) {
1532: m.put(nodes[k].getKey(), nodes[k]);
1533:
1534: if (k % 2 == 1) {
1535: c1.add(nodes[k]);
1536: }
1537: }
1538:
1539: assertTrue(m.valuesByValue().retainAll(c1));
1540: assertEquals(nodes.length / 2, m.size());
1541:
1542: m = (DoubleOrderedMap) makeMap();
1543: c1 = new LinkedList();
1544:
1545: for (int k = 0; k < nodes.length; k++) {
1546: m.put(nodes[k].getKey(), nodes[k]);
1547: }
1548:
1549: assertTrue(m.valuesByValue().retainAll(c1));
1550: assertEquals(0, m.size());
1551:
1552: m = (DoubleOrderedMap) makeMap();
1553: c1 = new LinkedList();
1554:
1555: for (int k = 0; k < nodes.length; k++) {
1556: m.put(nodes[k].getKey(), nodes[k]);
1557: }
1558:
1559: assertTrue(!m.valuesByValue().removeAll(c1));
1560: assertEquals(nodes.length, m.size());
1561:
1562: m = (DoubleOrderedMap) makeMap();
1563: c1 = new LinkedList();
1564:
1565: for (int k = 0; k < nodes.length; k++) {
1566: m.put(nodes[k].getKey(), nodes[k]);
1567:
1568: if (k % 2 == 0) {
1569: c1.add(nodes[k]);
1570: }
1571: }
1572:
1573: assertTrue(m.valuesByValue().removeAll(c1));
1574: assertEquals(nodes.length / 2, m.size());
1575:
1576: m = (DoubleOrderedMap) makeMap();
1577: c1 = new LinkedList();
1578:
1579: for (int k = 0; k < nodes.length; k++) {
1580: m.put(nodes[k].getKey(), nodes[k]);
1581: c1.add(nodes[k]);
1582: }
1583:
1584: assertTrue(m.valuesByValue().removeAll(c1));
1585: assertEquals(0, m.size());
1586:
1587: m = (DoubleOrderedMap) makeMap();
1588:
1589: for (int k = 0; k < nodes.length; k++) {
1590: m.put(nodes[k].getKey(), nodes[k]);
1591: }
1592:
1593: m.valuesByValue().clear();
1594: assertEquals(0, m.size());
1595: }
1596:
1597: /* ********** START helper methods ********** */
1598: private void testKeySet(final Map m) {
1599:
1600: Set s = m.keySet();
1601:
1602: assertEquals(m.size(), s.size());
1603: assertEquals(m.isEmpty(), s.isEmpty());
1604:
1605: LocalTestNode node = new LocalTestNode(-1);
1606:
1607: m.put(node.getKey(), node);
1608: assertTrue(s.contains(node.getKey()));
1609: assertEquals(m.size(), s.size());
1610: assertEquals(m.isEmpty(), s.isEmpty());
1611: m.remove(node.getKey());
1612: assertTrue(!s.contains(node.getKey()));
1613: assertEquals(m.size(), s.size());
1614: assertEquals(m.isEmpty(), s.isEmpty());
1615:
1616: try {
1617: s.contains(null);
1618: fail("should have caught NullPointerException");
1619: } catch (NullPointerException ignored) {
1620: }
1621:
1622: try {
1623: s.contains(new Object());
1624: fail("should have caught ClassCastException");
1625: } catch (ClassCastException ignored) {
1626: }
1627:
1628: for (int k = 0; k < m.size(); k++) {
1629: assertTrue(s.contains(new Integer(k)));
1630: }
1631:
1632: int count = 0;
1633:
1634: for (Iterator iter = s.iterator(); iter.hasNext();) {
1635: iter.next();
1636:
1637: ++count;
1638: }
1639:
1640: assertEquals(count, s.size());
1641:
1642: // force the map to have some content
1643: m.put(node.getKey(), node);
1644:
1645: Iterator iter = m.keySet().iterator();
1646: LocalTestNode node2 = new LocalTestNode(-2);
1647:
1648: m.put(node2.getKey(), node2);
1649:
1650: try {
1651: iter.next();
1652: fail("next() should have thrown an exception after a put");
1653: } catch (ConcurrentModificationException ignored) {
1654: }
1655:
1656: m.remove(node2.getKey());
1657:
1658: iter = s.iterator();
1659:
1660: m.remove(node.getKey());
1661:
1662: try {
1663: iter.next();
1664: fail("next() should have thrown an exception after a Map remove");
1665: } catch (ConcurrentModificationException ignored) {
1666: }
1667:
1668: m.put(node.getKey(), node);
1669:
1670: iter = s.iterator();
1671:
1672: s.remove(node.getKey());
1673:
1674: try {
1675: iter.next();
1676: fail("next() should have thrown an exception after a Set remove");
1677: } catch (ConcurrentModificationException ignored) {
1678: }
1679:
1680: iter = s.iterator();
1681: count = 0;
1682:
1683: boolean terminated = false;
1684:
1685: try {
1686: while (true) {
1687: iter.next();
1688:
1689: ++count;
1690: }
1691: } catch (NoSuchElementException ignored) {
1692: terminated = true;
1693: }
1694:
1695: assertTrue(terminated);
1696: assertEquals(m.size(), count);
1697:
1698: iter = s.iterator();
1699:
1700: try {
1701: iter.remove();
1702: fail("Should have thrown exception");
1703: } catch (IllegalStateException ignored) {
1704: }
1705:
1706: m.put(node.getKey(), node);
1707:
1708: iter = s.iterator();
1709:
1710: iter.next();
1711: m.put(node2.getKey(), node2);
1712:
1713: try {
1714: iter.remove();
1715: fail("should have thrown exception");
1716: } catch (ConcurrentModificationException ignored) {
1717: }
1718:
1719: Iterator iter2 = s.iterator();
1720:
1721: iter2.next();
1722:
1723: LocalTestNode node3 = new LocalTestNode(-3);
1724:
1725: m.put(node3.getKey(), node3);
1726:
1727: try {
1728: iter2.remove();
1729: fail("should have thrown exception");
1730: } catch (ConcurrentModificationException ignored) {
1731: }
1732:
1733: int removalCount = 0;
1734:
1735: for (iter = s.iterator(); iter.hasNext();) {
1736: if (iter.next().equals(node.getKey())) {
1737: try {
1738: iter.remove();
1739:
1740: ++removalCount;
1741:
1742: iter.remove();
1743: fail("2nd remove should have failed");
1744: } catch (IllegalStateException ignored) {
1745: assertEquals(1, removalCount);
1746: }
1747: }
1748: }
1749:
1750: assertEquals(1, removalCount);
1751: assertTrue(!s.contains(node.getKey()));
1752:
1753: removalCount = 0;
1754:
1755: m.put(node.getKey(), node);
1756:
1757: Object[] a1 = s.toArray();
1758:
1759: assertEquals(s.size(), a1.length);
1760:
1761: if (a1.length > 1) {
1762: Comparable first = (Comparable) a1[0];
1763:
1764: for (int k = 1; k < a1.length; k++) {
1765: Comparable second = (Comparable) a1[k];
1766:
1767: assertTrue(first.compareTo(second) < 0);
1768:
1769: first = second;
1770: }
1771:
1772: iter = s.iterator();
1773: first = (Comparable) iter.next();
1774:
1775: for (; iter.hasNext();) {
1776: Comparable second = (Comparable) iter.next();
1777:
1778: assertTrue(first.compareTo(second) < 0);
1779:
1780: first = second;
1781: }
1782: }
1783:
1784: try {
1785: String array2[] = (String[]) s.toArray(new String[0]);
1786:
1787: if (s.size() != 0) {
1788: fail("should have caught exception creating an invalid array");
1789: }
1790: } catch (ArrayStoreException ignored) {
1791: }
1792:
1793: Comparable array2[] = (Comparable[]) s
1794: .toArray(new Comparable[0]);
1795: Integer array3[] = (Integer[]) s.toArray(new Integer[s.size()]);
1796:
1797: if (array3.length > 1) {
1798: Integer first = array3[0];
1799:
1800: for (int k = 1; k < array3.length; k++) {
1801: Integer second = array3[k];
1802:
1803: assertTrue(first.compareTo(second) < 0);
1804:
1805: first = second;
1806: }
1807: }
1808:
1809: try {
1810: s.add("foo");
1811: fail("should have thrown an exception");
1812: } catch (UnsupportedOperationException ignored) {
1813: }
1814:
1815: assertTrue(!s.equals(null));
1816: assertEquals(s, s);
1817:
1818: Set hs = new HashSet(s);
1819:
1820: assertEquals(s, hs);
1821: assertEquals(hs, s);
1822: assertEquals(s.hashCode(), hs.hashCode());
1823: }
1824:
1825: private void testKeySetByValue(final DoubleOrderedMap m) {
1826:
1827: Set s = m.keySetByValue();
1828:
1829: assertEquals(m.size(), s.size());
1830: assertEquals(m.isEmpty(), s.isEmpty());
1831:
1832: LocalTestNode node = new LocalTestNode(-1);
1833:
1834: m.put(node.getKey(), node);
1835: assertTrue(s.contains(node.getKey()));
1836: assertEquals(m.size(), s.size());
1837: assertEquals(m.isEmpty(), s.isEmpty());
1838: m.remove(node.getKey());
1839: assertTrue(!s.contains(node.getKey()));
1840: assertEquals(m.size(), s.size());
1841: assertEquals(m.isEmpty(), s.isEmpty());
1842:
1843: try {
1844: s.contains(null);
1845: fail("should have caught NullPointerException");
1846: } catch (NullPointerException ignored) {
1847: }
1848:
1849: try {
1850: s.contains(new Object());
1851: fail("should have caught ClassCastException");
1852: } catch (ClassCastException ignored) {
1853: }
1854:
1855: for (int k = 0; k < m.size(); k++) {
1856: assertTrue(s.contains(new Integer(k)));
1857: }
1858:
1859: int count = 0;
1860:
1861: for (Iterator iter = s.iterator(); iter.hasNext();) {
1862: iter.next();
1863:
1864: ++count;
1865: }
1866:
1867: assertEquals(count, s.size());
1868:
1869: // force the map to have some content
1870: m.put(node.getKey(), node);
1871:
1872: Iterator iter = m.keySetByValue().iterator();
1873: LocalTestNode node2 = new LocalTestNode(-2);
1874:
1875: m.put(node2.getKey(), node2);
1876:
1877: try {
1878: iter.next();
1879: fail("next() should have thrown an exception after a put");
1880: } catch (ConcurrentModificationException ignored) {
1881: }
1882:
1883: m.remove(node2.getKey());
1884:
1885: iter = s.iterator();
1886:
1887: m.remove(node.getKey());
1888:
1889: try {
1890: iter.next();
1891: fail("next() should have thrown an exception after a Map remove");
1892: } catch (ConcurrentModificationException ignored) {
1893: }
1894:
1895: m.put(node.getKey(), node);
1896:
1897: iter = s.iterator();
1898:
1899: s.remove(node.getKey());
1900:
1901: try {
1902: iter.next();
1903: fail("next() should have thrown an exception after a Set remove");
1904: } catch (ConcurrentModificationException ignored) {
1905: }
1906:
1907: iter = s.iterator();
1908: count = 0;
1909:
1910: boolean terminated = false;
1911:
1912: try {
1913: while (true) {
1914: iter.next();
1915:
1916: ++count;
1917: }
1918: } catch (NoSuchElementException ignored) {
1919: terminated = true;
1920: }
1921:
1922: assertTrue(terminated);
1923: assertEquals(m.size(), count);
1924:
1925: iter = s.iterator();
1926:
1927: try {
1928: iter.remove();
1929: fail("Should have thrown exception");
1930: } catch (IllegalStateException ignored) {
1931: }
1932:
1933: m.put(node.getKey(), node);
1934:
1935: iter = s.iterator();
1936:
1937: iter.next();
1938: m.put(node2.getKey(), node2);
1939:
1940: try {
1941: iter.remove();
1942: fail("should have thrown exception");
1943: } catch (ConcurrentModificationException ignored) {
1944: }
1945:
1946: Iterator iter2 = s.iterator();
1947:
1948: iter2.next();
1949:
1950: LocalTestNode node3 = new LocalTestNode(-3);
1951:
1952: m.put(node3.getKey(), node3);
1953:
1954: try {
1955: iter2.remove();
1956: fail("should have thrown exception");
1957: } catch (ConcurrentModificationException ignored) {
1958: }
1959:
1960: int removalCount = 0;
1961:
1962: for (iter = s.iterator(); iter.hasNext();) {
1963: if (iter.next().equals(node.getKey())) {
1964: try {
1965: iter.remove();
1966:
1967: ++removalCount;
1968:
1969: iter.remove();
1970: fail("2nd remove should have failed");
1971: } catch (IllegalStateException ignored) {
1972: assertEquals(1, removalCount);
1973: }
1974: }
1975: }
1976:
1977: assertEquals(1, removalCount);
1978: assertTrue(!s.contains(node.getKey()));
1979:
1980: removalCount = 0;
1981:
1982: m.put(node.getKey(), node);
1983:
1984: Object[] a1 = s.toArray();
1985:
1986: assertEquals(s.size(), a1.length);
1987:
1988: // if (a1.length > 1)
1989: // {
1990: // Comparable first = ( Comparable ) a1[ 0 ];
1991: // for (int k = 1; k < a1.length; k++)
1992: // {
1993: // Comparable second = ( Comparable ) a1[ k ];
1994: // assertTrue(first.compareTo(second) < 0);
1995: // first = second;
1996: // }
1997: // iter = s.iterator();
1998: // first = ( Comparable ) iter.next();
1999: // for (; iter.hasNext(); )
2000: // {
2001: // Comparable second = ( Comparable ) iter.next();
2002: // assertTrue(first.compareTo(second) < 0);
2003: // first = second;
2004: // }
2005: // }
2006: try {
2007: String array2[] = (String[]) s.toArray(new String[0]);
2008:
2009: if (s.size() != 0) {
2010: fail("should have caught exception creating an invalid array");
2011: }
2012: } catch (ArrayStoreException ignored) {
2013: }
2014:
2015: Comparable array2[] = (Comparable[]) s
2016: .toArray(new Comparable[0]);
2017: Integer array3[] = (Integer[]) s.toArray(new Integer[s.size()]);
2018:
2019: // if (array3.length > 1)
2020: // {
2021: // Integer first = array3[ 0 ];
2022: // for (int k = 1; k < array3.length; k++)
2023: // {
2024: // Integer second = array3[ k ];
2025: // assertTrue(first.compareTo(second) < 0);
2026: // first = second;
2027: // }
2028: // }
2029: try {
2030: s.add("foo");
2031: fail("should have thrown an exception");
2032: } catch (UnsupportedOperationException ignored) {
2033: }
2034:
2035: assertTrue(!s.equals(null));
2036: assertEquals(s, s);
2037:
2038: Set hs = new HashSet(s);
2039:
2040: assertEquals(s, hs);
2041: assertEquals(hs, s);
2042: assertEquals(s.hashCode(), hs.hashCode());
2043: }
2044:
2045: private void testValues(Map m) {
2046:
2047: Collection s = m.values();
2048:
2049: assertEquals(m.size(), s.size());
2050: assertEquals(m.isEmpty(), s.isEmpty());
2051:
2052: LocalTestNode node = new LocalTestNode(-1);
2053:
2054: m.put(node.getKey(), node);
2055: assertEquals(m.size(), s.size());
2056: assertEquals(m.isEmpty(), s.isEmpty());
2057: m.remove(node.getKey());
2058: assertEquals(m.size(), s.size());
2059: assertEquals(m.isEmpty(), s.isEmpty());
2060: assertTrue(!s.contains(node));
2061:
2062: for (int k = 0; k < m.size(); k++) {
2063: assertTrue(s.contains(new LocalTestNode(k)));
2064: }
2065:
2066: m.put(node.getKey(), node);
2067: assertTrue(s.contains(node));
2068: m.remove(node.getKey());
2069: assertTrue(!s.contains(node));
2070:
2071: int count = 0;
2072:
2073: for (Iterator iter = s.iterator(); iter.hasNext();) {
2074: iter.next();
2075:
2076: ++count;
2077: }
2078:
2079: assertEquals(s.size(), count);
2080:
2081: LocalTestNode node4 = new LocalTestNode(-4);
2082:
2083: m.put(node4.getKey(), node4);
2084:
2085: Iterator iter = s.iterator();
2086:
2087: m.put(node.getKey(), node);
2088:
2089: try {
2090: iter.next();
2091: fail("next() should have thrown an exception after a put");
2092: } catch (ConcurrentModificationException ignored) {
2093: }
2094:
2095: iter = s.iterator();
2096:
2097: m.remove(node.getKey());
2098:
2099: try {
2100: iter.next();
2101: fail("next() should have thrown an exception after a Map remove");
2102: } catch (ConcurrentModificationException ignored) {
2103: }
2104:
2105: m.put(node.getKey(), node);
2106:
2107: iter = s.iterator();
2108:
2109: s.remove(node);
2110:
2111: try {
2112: iter.next();
2113: fail("next() should have thrown an exception after a Set remove");
2114: } catch (ConcurrentModificationException ignored) {
2115: }
2116:
2117: iter = s.iterator();
2118: count = 0;
2119:
2120: boolean terminated = false;
2121:
2122: try {
2123: while (true) {
2124: iter.next();
2125:
2126: ++count;
2127: }
2128: } catch (NoSuchElementException ignored) {
2129: terminated = true;
2130: }
2131:
2132: assertTrue(terminated);
2133: assertEquals(m.size(), count);
2134:
2135: iter = s.iterator();
2136:
2137: try {
2138: iter.remove();
2139: fail("Should have thrown exception");
2140: } catch (IllegalStateException ignored) {
2141: }
2142:
2143: Iterator iter2 = s.iterator();
2144:
2145: try {
2146: iter2.remove();
2147: fail("Should have thrown exception");
2148: } catch (IllegalStateException ignored) {
2149: }
2150:
2151: m.put(node.getKey(), node);
2152:
2153: iter = s.iterator();
2154:
2155: iter.next();
2156:
2157: LocalTestNode node2 = new LocalTestNode(-2);
2158:
2159: m.put(node2.getKey(), node2);
2160:
2161: try {
2162: iter.remove();
2163: fail("should have thrown exception");
2164: } catch (ConcurrentModificationException ignored) {
2165: }
2166:
2167: LocalTestNode node3 = new LocalTestNode(-3);
2168:
2169: m.put(node3.getKey(), node3);
2170:
2171: iter2 = s.iterator();
2172:
2173: while (iter2.hasNext()) {
2174: iter2.next();
2175: }
2176:
2177: int removalCount = 0;
2178:
2179: for (iter = s.iterator(); iter.hasNext();) {
2180: if (iter.next().equals(node3)) {
2181: try {
2182: iter.remove();
2183:
2184: ++removalCount;
2185:
2186: iter.remove();
2187: fail("2nd remove should have failed");
2188: } catch (IllegalStateException ignored) {
2189: assertEquals(1, removalCount);
2190: }
2191: }
2192: }
2193:
2194: assertEquals(1, removalCount);
2195: assertTrue(!s.contains(node3));
2196:
2197: Object[] a1 = s.toArray();
2198:
2199: assertEquals(s.size(), a1.length);
2200:
2201: if (a1.length > 1) {
2202: Comparable first = (Comparable) a1[0];
2203:
2204: for (int k = 1; k < a1.length; k++) {
2205: Comparable second = (Comparable) a1[k];
2206:
2207: assertTrue(first.compareTo(second) < 0);
2208:
2209: first = second;
2210: }
2211:
2212: iter = s.iterator();
2213: first = (Comparable) iter.next();
2214:
2215: for (; iter.hasNext();) {
2216: Comparable second = (Comparable) iter.next();
2217:
2218: assertTrue(first.compareTo(second) < 0);
2219:
2220: first = second;
2221: }
2222: }
2223:
2224: try {
2225: String array2[] = (String[]) s.toArray(new String[0]);
2226:
2227: if (s.size() != 0) {
2228: fail("should have caught exception creating an invalid array");
2229: }
2230: } catch (ArrayStoreException ignored) {
2231: }
2232:
2233: m.remove(node.getKey());
2234: m.remove(node2.getKey());
2235: m.remove(node3.getKey());
2236:
2237: LocalTestNode array2[] = (LocalTestNode[]) s
2238: .toArray(new LocalTestNode[0]);
2239: LocalTestNode array3[] = (LocalTestNode[]) s
2240: .toArray(new LocalTestNode[s.size()]);
2241:
2242: if (array3.length > 1) {
2243: LocalTestNode first = array3[0];
2244:
2245: for (int k = 1; k < array3.length; k++) {
2246: LocalTestNode second = array3[k];
2247:
2248: assertTrue(first.compareTo(second) < 0);
2249:
2250: first = second;
2251: }
2252: }
2253:
2254: try {
2255: s.add(node.getKey());
2256: fail("should have thrown an exception");
2257: } catch (UnsupportedOperationException ignored) {
2258: }
2259:
2260: assertTrue(!s.equals(null));
2261: assertEquals(s, s);
2262:
2263: Set hs = new HashSet(s);
2264:
2265: assertTrue(!s.equals(hs));
2266: assertTrue(!hs.equals(s));
2267: }
2268:
2269: private void testValuesByValue(DoubleOrderedMap m) {
2270:
2271: Collection s = m.valuesByValue();
2272:
2273: assertEquals(m.size(), s.size());
2274: assertEquals(m.isEmpty(), s.isEmpty());
2275:
2276: LocalTestNode node = new LocalTestNode(-1);
2277:
2278: m.put(node.getKey(), node);
2279: assertEquals(m.size(), s.size());
2280: assertEquals(m.isEmpty(), s.isEmpty());
2281: m.remove(node.getKey());
2282: assertEquals(m.size(), s.size());
2283: assertEquals(m.isEmpty(), s.isEmpty());
2284: assertTrue(!s.contains(node));
2285:
2286: for (int k = 0; k < m.size(); k++) {
2287: assertTrue(s.contains(new LocalTestNode(k)));
2288: }
2289:
2290: m.put(node.getKey(), node);
2291: assertTrue(s.contains(node));
2292: m.remove(node.getKey());
2293: assertTrue(!s.contains(node));
2294:
2295: int count = 0;
2296:
2297: for (Iterator iter = s.iterator(); iter.hasNext();) {
2298: iter.next();
2299:
2300: ++count;
2301: }
2302:
2303: assertEquals(s.size(), count);
2304:
2305: LocalTestNode node4 = new LocalTestNode(-4);
2306:
2307: m.put(node4.getKey(), node4);
2308:
2309: Iterator iter = s.iterator();
2310:
2311: m.put(node.getKey(), node);
2312:
2313: try {
2314: iter.next();
2315: fail("next() should have thrown an exception after a put");
2316: } catch (ConcurrentModificationException ignored) {
2317: }
2318:
2319: iter = s.iterator();
2320:
2321: m.remove(node.getKey());
2322:
2323: try {
2324: iter.next();
2325: fail("next() should have thrown an exception after a Map remove");
2326: } catch (ConcurrentModificationException ignored) {
2327: }
2328:
2329: m.put(node.getKey(), node);
2330:
2331: iter = s.iterator();
2332:
2333: s.remove(node);
2334:
2335: try {
2336: iter.next();
2337: fail("next() should have thrown an exception after a Set remove");
2338: } catch (ConcurrentModificationException ignored) {
2339: }
2340:
2341: iter = s.iterator();
2342: count = 0;
2343:
2344: boolean terminated = false;
2345:
2346: try {
2347: while (true) {
2348: iter.next();
2349:
2350: ++count;
2351: }
2352: } catch (NoSuchElementException ignored) {
2353: terminated = true;
2354: }
2355:
2356: assertTrue(terminated);
2357: assertEquals(m.size(), count);
2358:
2359: iter = s.iterator();
2360:
2361: try {
2362: iter.remove();
2363: fail("Should have thrown exception");
2364: } catch (IllegalStateException ignored) {
2365: }
2366:
2367: Iterator iter2 = s.iterator();
2368:
2369: try {
2370: iter2.remove();
2371: fail("Should have thrown exception");
2372: } catch (IllegalStateException ignored) {
2373: }
2374:
2375: m.put(node.getKey(), node);
2376:
2377: iter = s.iterator();
2378:
2379: iter.next();
2380:
2381: LocalTestNode node2 = new LocalTestNode(-2);
2382:
2383: m.put(node2.getKey(), node2);
2384:
2385: try {
2386: iter.remove();
2387: fail("should have thrown exception");
2388: } catch (ConcurrentModificationException ignored) {
2389: }
2390:
2391: LocalTestNode node3 = new LocalTestNode(-3);
2392:
2393: m.put(node3.getKey(), node3);
2394:
2395: iter2 = s.iterator();
2396:
2397: while (iter2.hasNext()) {
2398: iter2.next();
2399: }
2400:
2401: int removalCount = 0;
2402:
2403: for (iter = s.iterator(); iter.hasNext();) {
2404: if (iter.next().equals(node3)) {
2405: try {
2406: iter.remove();
2407:
2408: ++removalCount;
2409:
2410: iter.remove();
2411: fail("2nd remove should have failed");
2412: } catch (IllegalStateException ignored) {
2413: assertEquals(1, removalCount);
2414: }
2415: }
2416: }
2417:
2418: assertEquals(1, removalCount);
2419: assertTrue(!s.contains(node3));
2420:
2421: Object[] a1 = s.toArray();
2422:
2423: assertEquals(s.size(), a1.length);
2424:
2425: try {
2426: String array2[] = (String[]) s.toArray(new String[0]);
2427:
2428: if (s.size() != 0) {
2429: fail("should have caught exception creating an invalid array");
2430: }
2431: } catch (ArrayStoreException ignored) {
2432: }
2433:
2434: m.remove(node.getKey());
2435: m.remove(node2.getKey());
2436: m.remove(node3.getKey());
2437:
2438: LocalTestNode array2[] = (LocalTestNode[]) s
2439: .toArray(new LocalTestNode[0]);
2440: LocalTestNode array3[] = (LocalTestNode[]) s
2441: .toArray(new LocalTestNode[s.size()]);
2442:
2443: try {
2444: s.add(node.getKey());
2445: fail("should have thrown an exception");
2446: } catch (UnsupportedOperationException ignored) {
2447: }
2448:
2449: assertTrue(!s.equals(null));
2450: assertEquals(s, s);
2451:
2452: Set hs = new HashSet(s);
2453:
2454: assertTrue(!s.equals(hs));
2455: assertTrue(!hs.equals(s));
2456: }
2457:
2458: private void testEntrySet(Map m) {
2459:
2460: Set s = m.entrySet();
2461:
2462: assertEquals(m.size(), s.size());
2463: assertEquals(m.isEmpty(), s.isEmpty());
2464:
2465: LocalTestNode node = new LocalTestNode(-1);
2466:
2467: m.put(node.getKey(), node);
2468: assertEquals(m.size(), s.size());
2469: assertEquals(m.isEmpty(), s.isEmpty());
2470: m.remove(node.getKey());
2471: assertEquals(m.size(), s.size());
2472: assertEquals(m.isEmpty(), s.isEmpty());
2473:
2474: int count = 0;
2475:
2476: for (Iterator iter = s.iterator(); iter.hasNext();) {
2477: iter.next();
2478:
2479: ++count;
2480: }
2481:
2482: assertEquals(s.size(), count);
2483:
2484: LocalTestNode node2 = new LocalTestNode(-2);
2485:
2486: if (m.size() == 0) {
2487: m.put(node2.getKey(), node2);
2488: }
2489:
2490: Iterator iter = s.iterator();
2491:
2492: m.put(node.getKey(), node);
2493:
2494: try {
2495: iter.next();
2496: fail("next() should have thrown an exception after a put");
2497: } catch (ConcurrentModificationException ignored) {
2498: }
2499:
2500: m.remove(node2.getKey());
2501:
2502: iter = s.iterator();
2503:
2504: m.remove(node.getKey());
2505:
2506: try {
2507: iter.next();
2508: fail("next() should have thrown an exception after a Map remove");
2509: } catch (ConcurrentModificationException ignored) {
2510: }
2511:
2512: m.put(node.getKey(), node);
2513:
2514: iter = s.iterator();
2515: count = 0;
2516:
2517: boolean terminated = false;
2518:
2519: try {
2520: while (true) {
2521: iter.next();
2522:
2523: ++count;
2524: }
2525: } catch (NoSuchElementException ignored) {
2526: terminated = true;
2527: }
2528:
2529: assertTrue(terminated);
2530: assertEquals(m.size(), count);
2531:
2532: iter = s.iterator();
2533:
2534: try {
2535: iter.remove();
2536: fail("Should have thrown exception");
2537: } catch (IllegalStateException ignored) {
2538: }
2539:
2540: iter = s.iterator();
2541:
2542: iter.next();
2543:
2544: LocalTestNode node3 = new LocalTestNode(-3);
2545:
2546: m.put(node3.getKey(), node3);
2547:
2548: try {
2549: iter.remove();
2550: fail("should have thrown exception");
2551: } catch (ConcurrentModificationException ignored) {
2552: }
2553:
2554: int removalCount = 0;
2555: int when = m.size() / 2;
2556: int timer = 0;
2557:
2558: for (iter = s.iterator(); iter.hasNext();) {
2559: iter.next();
2560:
2561: if (timer == when) {
2562: try {
2563: iter.remove();
2564:
2565: ++removalCount;
2566:
2567: iter.remove();
2568: fail("2nd remove should have failed");
2569: } catch (IllegalStateException ignored) {
2570: assertEquals(1, removalCount);
2571: }
2572: }
2573:
2574: timer++;
2575: }
2576:
2577: assertEquals(1, removalCount);
2578:
2579: Iterator iter2 = s.iterator();
2580:
2581: try {
2582: iter2.remove();
2583: fail("Should have thrown exception");
2584: } catch (IllegalStateException ignored) {
2585: }
2586:
2587: iter2 = s.iterator();
2588:
2589: while (iter2.hasNext()) {
2590: iter2.next();
2591: }
2592:
2593: LocalTestNode node4 = new LocalTestNode(-4);
2594:
2595: m.put(node4.getKey(), node4);
2596:
2597: try {
2598: iter2.remove();
2599: fail("should have thrown exception");
2600: } catch (ConcurrentModificationException ignored) {
2601: }
2602:
2603: Object[] a1 = s.toArray();
2604:
2605: assertEquals(s.size(), a1.length);
2606:
2607: if (a1.length > 1) {
2608: Map.Entry first = (Map.Entry) a1[0];
2609:
2610: for (int k = 1; k < a1.length; k++) {
2611: Map.Entry second = (Map.Entry) a1[k];
2612:
2613: assertTrue(((Comparable) first.getKey())
2614: .compareTo((Comparable) second.getKey()) < 0);
2615:
2616: first = second;
2617: }
2618:
2619: iter = s.iterator();
2620: first = (Map.Entry) iter.next();
2621:
2622: for (; iter.hasNext();) {
2623: Map.Entry second = (Map.Entry) iter.next();
2624:
2625: assertTrue(((Comparable) first.getKey())
2626: .compareTo((Comparable) second.getKey()) < 0);
2627:
2628: first = second;
2629: }
2630: }
2631:
2632: try {
2633: Integer array2[] = (Integer[]) s.toArray(new Integer[0]);
2634:
2635: if (s.size() != 0) {
2636: fail("should have caught exception creating an invalid array");
2637: }
2638: } catch (ArrayStoreException ignored) {
2639: }
2640:
2641: Map.Entry array2[] = (Map.Entry[]) s.toArray(new Map.Entry[0]);
2642: Map.Entry array3[] = (Map.Entry[]) s.toArray(new Map.Entry[s
2643: .size()]);
2644:
2645: if (array3.length > 1) {
2646: Comparable first = (Comparable) ((Map.Entry) array3[0])
2647: .getKey();
2648:
2649: for (int k = 1; k < array3.length; k++) {
2650: Comparable second = (Comparable) ((Map.Entry) array3[k])
2651: .getKey();
2652:
2653: assertTrue(first.compareTo(second) < 0);
2654:
2655: first = second;
2656: }
2657: }
2658:
2659: try {
2660: s.add(node.getKey());
2661: fail("should have thrown an exception");
2662: } catch (UnsupportedOperationException ignored) {
2663: }
2664:
2665: assertTrue(!s.equals(null));
2666: assertEquals("SetEquality 1", s, s);
2667:
2668: Set hs = new HashSet(s);
2669:
2670: assertEquals("SetEquality 2", s, hs);
2671: assertEquals("SetEquality 3", hs, s);
2672: assertEquals(s.hashCode(), hs.hashCode());
2673: }
2674:
2675: private void testEntrySetByValue(DoubleOrderedMap m) {
2676:
2677: Set s = m.entrySetByValue();
2678:
2679: assertEquals(m.size(), s.size());
2680: assertEquals(m.isEmpty(), s.isEmpty());
2681:
2682: LocalTestNode node = new LocalTestNode(-1);
2683:
2684: m.put(node.getKey(), node);
2685: assertEquals(m.size(), s.size());
2686: assertEquals(m.isEmpty(), s.isEmpty());
2687: m.remove(node.getKey());
2688: assertEquals(m.size(), s.size());
2689: assertEquals(m.isEmpty(), s.isEmpty());
2690:
2691: int count = 0;
2692:
2693: for (Iterator iter = s.iterator(); iter.hasNext();) {
2694: iter.next();
2695:
2696: ++count;
2697: }
2698:
2699: assertEquals(s.size(), count);
2700:
2701: LocalTestNode node2 = new LocalTestNode(-2);
2702:
2703: if (m.size() == 0) {
2704: m.put(node2.getKey(), node2);
2705: }
2706:
2707: Iterator iter = s.iterator();
2708:
2709: m.put(node.getKey(), node);
2710:
2711: try {
2712: iter.next();
2713: fail("next() should have thrown an exception after a put");
2714: } catch (ConcurrentModificationException ignored) {
2715: }
2716:
2717: m.remove(node2.getKey());
2718:
2719: iter = s.iterator();
2720:
2721: m.remove(node.getKey());
2722:
2723: try {
2724: iter.next();
2725: fail("next() should have thrown an exception after a Map remove");
2726: } catch (ConcurrentModificationException ignored) {
2727: }
2728:
2729: m.put(node.getKey(), node);
2730:
2731: iter = s.iterator();
2732: count = 0;
2733:
2734: boolean terminated = false;
2735:
2736: try {
2737: while (true) {
2738: iter.next();
2739:
2740: ++count;
2741: }
2742: } catch (NoSuchElementException ignored) {
2743: terminated = true;
2744: }
2745:
2746: assertTrue(terminated);
2747: assertEquals(m.size(), count);
2748:
2749: iter = s.iterator();
2750:
2751: try {
2752: iter.remove();
2753: fail("Should have thrown exception");
2754: } catch (IllegalStateException ignored) {
2755: }
2756:
2757: iter = s.iterator();
2758:
2759: iter.next();
2760:
2761: LocalTestNode node3 = new LocalTestNode(-3);
2762:
2763: m.put(node3.getKey(), node3);
2764:
2765: try {
2766: iter.remove();
2767: fail("should have thrown exception");
2768: } catch (ConcurrentModificationException ignored) {
2769: }
2770:
2771: int removalCount = 0;
2772: int when = m.size() / 2;
2773: int timer = 0;
2774:
2775: for (iter = s.iterator(); iter.hasNext();) {
2776: iter.next();
2777:
2778: if (timer == when) {
2779: try {
2780: iter.remove();
2781:
2782: ++removalCount;
2783:
2784: iter.remove();
2785: fail("2nd remove should have failed");
2786: } catch (IllegalStateException ignored) {
2787: assertEquals(1, removalCount);
2788: }
2789: }
2790:
2791: timer++;
2792: }
2793:
2794: assertEquals(1, removalCount);
2795:
2796: Iterator iter2 = s.iterator();
2797:
2798: try {
2799: iter2.remove();
2800: fail("Should have thrown exception");
2801: } catch (IllegalStateException ignored) {
2802: }
2803:
2804: iter2 = s.iterator();
2805:
2806: while (iter2.hasNext()) {
2807: iter2.next();
2808: }
2809:
2810: LocalTestNode node4 = new LocalTestNode(-4);
2811:
2812: m.put(node4.getKey(), node4);
2813:
2814: try {
2815: iter2.remove();
2816: fail("should have thrown exception");
2817: } catch (ConcurrentModificationException ignored) {
2818: }
2819:
2820: Object[] a1 = s.toArray();
2821:
2822: assertEquals(s.size(), a1.length);
2823:
2824: if (a1.length > 1) {
2825: Map.Entry first = (Map.Entry) a1[0];
2826:
2827: for (int k = 1; k < a1.length; k++) {
2828: Map.Entry second = (Map.Entry) a1[k];
2829:
2830: assertTrue(((Comparable) first.getKey())
2831: .compareTo((Comparable) second.getKey()) < 0);
2832:
2833: first = second;
2834: }
2835:
2836: iter = s.iterator();
2837: first = (Map.Entry) iter.next();
2838:
2839: for (; iter.hasNext();) {
2840: Map.Entry second = (Map.Entry) iter.next();
2841:
2842: assertTrue(((Comparable) first.getKey())
2843: .compareTo((Comparable) second.getKey()) < 0);
2844:
2845: first = second;
2846: }
2847: }
2848:
2849: try {
2850: Integer array2[] = (Integer[]) s.toArray(new Integer[0]);
2851:
2852: if (s.size() != 0) {
2853: fail("should have caught exception creating an invalid array");
2854: }
2855: } catch (ArrayStoreException ignored) {
2856: }
2857:
2858: Map.Entry array2[] = (Map.Entry[]) s.toArray(new Map.Entry[0]);
2859: Map.Entry array3[] = (Map.Entry[]) s.toArray(new Map.Entry[s
2860: .size()]);
2861:
2862: if (array3.length > 1) {
2863: Comparable first = (Comparable) ((Map.Entry) array3[0])
2864: .getValue();
2865:
2866: for (int k = 1; k < array3.length; k++) {
2867: Comparable second = (Comparable) ((Map.Entry) array3[k])
2868: .getValue();
2869:
2870: assertTrue(first.compareTo(second) < 0);
2871:
2872: first = second;
2873: }
2874: }
2875:
2876: try {
2877: s.add(node.getKey());
2878: fail("should have thrown an exception");
2879: } catch (UnsupportedOperationException ignored) {
2880: }
2881:
2882: assertTrue(!s.equals(null));
2883: assertEquals("SetEquality 1", s, s);
2884:
2885: Set hs = new HashSet(s);
2886:
2887: assertEquals("SetEquality 2", s, hs);
2888: assertEquals("SetEquality 3", hs, s);
2889: assertEquals(s.hashCode(), hs.hashCode());
2890: }
2891:
2892: private LocalTestNode[] makeLocalNodes() {
2893:
2894: LocalTestNode nodes[] = new LocalTestNode[1023];
2895:
2896: for (int k = 0; k < nodes.length; k++) {
2897: nodes[k] = new LocalTestNode(k);
2898: }
2899:
2900: return nodes;
2901: }
2902:
2903: /* ********** END helper methods ********** */
2904:
2905: /**
2906: * Method main
2907: *
2908: * @param unusedArgs
2909: */
2910: public static void main(final String unusedArgs[]) {
2911: junit.textui.TestRunner.run(TestDoubleOrderedMap.class);
2912: }
2913:
2914: }
|