Source Code Cross Referenced for TestBinaryTree.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Collaboration » poi 3.0.2 beta2 » org.apache.poi.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* ====================================================================
0002:         Licensed to the Apache Software Foundation (ASF) under one or more
0003:         contributor license agreements.  See the NOTICE file distributed with
0004:         this work for additional information regarding copyright ownership.
0005:         The ASF licenses this file to You under the Apache License, Version 2.0
0006:         (the "License"); you may not use this file except in compliance with
0007:         the License.  You may obtain a copy of the License at
0008:
0009:         http://www.apache.org/licenses/LICENSE-2.0
0010:
0011:         Unless required by applicable law or agreed to in writing, software
0012:         distributed under the License is distributed on an "AS IS" BASIS,
0013:         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         See the License for the specific language governing permissions and
0015:         limitations under the License.
0016:         ==================================================================== */
0017:
0018:        package org.apache.poi.util;
0019:
0020:        import junit.framework.*;
0021:
0022:        import java.util.*;
0023:
0024:        /**
0025:         * Class TestBinaryTree
0026:         *
0027:         * @author Marc Johnson (mjohnson at apache dot org)
0028:         */
0029:
0030:        public class TestBinaryTree extends TestCase {
0031:
0032:            /**
0033:             * constructor
0034:             *
0035:             * @param name
0036:             */
0037:
0038:            public TestBinaryTree(final String name) {
0039:                super (name);
0040:            }
0041:
0042:            /**
0043:             * test size() method
0044:             */
0045:
0046:            public void testSize() {
0047:                Map m = new BinaryTree();
0048:
0049:                assertEquals(0, m.size());
0050:                LocalTestNode nodes[] = makeLocalNodes();
0051:
0052:                for (int k = 0; k < nodes.length; k++) {
0053:                    m.put(nodes[k].getKey(), nodes[k].getValue());
0054:                    assertEquals(k + 1, m.size());
0055:                }
0056:                int count = m.size();
0057:
0058:                for (int k = 0; k < nodes.length; k++) {
0059:                    m.remove(nodes[k].getKey());
0060:                    --count;
0061:                    assertEquals(count, m.size());
0062:
0063:                    // failed remove should not affect size
0064:                    m.remove(nodes[k].getKey());
0065:                    assertEquals(count, m.size());
0066:                }
0067:            }
0068:
0069:            /**
0070:             * test IsEmpty() method
0071:             */
0072:
0073:            public void testIsEmpty() {
0074:                Map m = new BinaryTree();
0075:
0076:                assertTrue(m.isEmpty());
0077:                LocalTestNode nodes[] = makeLocalNodes();
0078:
0079:                for (int k = 0; k < nodes.length; k++) {
0080:                    m.put(nodes[k].getKey(), nodes[k].getValue());
0081:                    assertTrue(!m.isEmpty());
0082:                }
0083:                int count = m.size();
0084:
0085:                for (int k = 0; k < nodes.length; k++) {
0086:                    m.remove(nodes[k].getKey());
0087:                    --count;
0088:                    if (count == 0) {
0089:                        assertTrue(m.isEmpty());
0090:                    } else {
0091:                        assertTrue(!m.isEmpty());
0092:                    }
0093:
0094:                    // failed remove should not affect emptiness
0095:                    m.remove(nodes[k].getKey());
0096:                    if (count == 0) {
0097:                        assertTrue(m.isEmpty());
0098:                    } else {
0099:                        assertTrue(!m.isEmpty());
0100:                    }
0101:                }
0102:            }
0103:
0104:            /**
0105:             * test containsKey() method
0106:             */
0107:
0108:            public void testContainsKey() {
0109:                Map m = new BinaryTree();
0110:
0111:                try {
0112:                    m.containsKey(new Object());
0113:                    fail("should have caught ClassCastException");
0114:                } catch (ClassCastException ignored) {
0115:                }
0116:                try {
0117:                    m.containsKey(null);
0118:                    fail("should have caught NullPointerException");
0119:                } catch (NullPointerException ignored) {
0120:                }
0121:                assertTrue(!m.containsKey("foo"));
0122:                LocalTestNode nodes[] = makeLocalNodes();
0123:
0124:                for (int k = 0; k < nodes.length; k++) {
0125:                    m.put(nodes[k].getKey(), nodes[k]);
0126:                    assertTrue(m.containsKey(nodes[k].getKey()));
0127:                }
0128:                assertTrue(!m.containsKey(new Integer(-1)));
0129:                try {
0130:                    m.containsKey("foo");
0131:                    fail("Should have caught ClassCastException");
0132:                } catch (ClassCastException ignored) {
0133:                }
0134:                for (int k = 0; k < nodes.length; k++) {
0135:                    m.remove(nodes[k].getKey());
0136:                    assertTrue(!m.containsKey(nodes[k].getKey()));
0137:                }
0138:            }
0139:
0140:            /**
0141:             * test containsValue() method
0142:             */
0143:
0144:            public void testContainsValue() {
0145:                Map m = new BinaryTree();
0146:                LocalTestNode nodes[] = makeLocalNodes();
0147:
0148:                for (int k = 0; k < nodes.length; k++) {
0149:                    m.put(nodes[k].getKey(), nodes[k]);
0150:                    assertTrue(m.containsValue(nodes[k]));
0151:                }
0152:                for (int k = 0; k < nodes.length; k++) {
0153:                    m.remove(nodes[k].getKey());
0154:                    assertTrue(!m.containsValue(nodes[k]));
0155:                }
0156:            }
0157:
0158:            /**
0159:             * test get() method
0160:             */
0161:
0162:            public void testGet() {
0163:                Map m = new BinaryTree();
0164:
0165:                try {
0166:                    m.get(new Object());
0167:                    fail("should have caught ClassCastException");
0168:                } catch (ClassCastException ignored) {
0169:                }
0170:                try {
0171:                    m.get(null);
0172:                    fail("should have caught NullPointerException");
0173:                } catch (NullPointerException ignored) {
0174:                }
0175:                assertNull(m.get("foo"));
0176:                LocalTestNode nodes[] = makeLocalNodes();
0177:
0178:                for (int k = 0; k < nodes.length; k++) {
0179:                    m.put(nodes[k].getKey(), nodes[k]);
0180:                    assertSame(m.get(nodes[k].getKey()), nodes[k]);
0181:                }
0182:                assertNull(m.get(new Integer(-1)));
0183:                try {
0184:                    m.get("foo");
0185:                    fail("Should have caught ClassCastException");
0186:                } catch (ClassCastException ignored) {
0187:                }
0188:                for (int k = 0; k < nodes.length; k++) {
0189:                    assertNotNull(m.get(nodes[k].getKey()));
0190:                    m.remove(nodes[k].getKey());
0191:                    assertNull(m.get(nodes[k].getKey()));
0192:                }
0193:            }
0194:
0195:            /**
0196:             * test put() method
0197:             */
0198:
0199:            public void testPut() {
0200:                Map m = new BinaryTree();
0201:
0202:                try {
0203:                    m.put(new Object(), "foo");
0204:                    fail("should have caught ClassCastException");
0205:                } catch (ClassCastException ignored) {
0206:                }
0207:                try {
0208:                    m.put(null, "foo");
0209:                    fail("should have caught NullPointerException");
0210:                } catch (NullPointerException ignored) {
0211:                }
0212:                try {
0213:                    m.put("foo", null);
0214:                    fail("should have caught NullPointerException");
0215:                } catch (NullPointerException ignored) {
0216:                }
0217:                try {
0218:                    m.put("foo", new Object());
0219:                    fail("should have caught ClassCastException");
0220:                } catch (ClassCastException ignored) {
0221:                }
0222:                LocalTestNode[] nodes = makeLocalNodes();
0223:
0224:                for (int k = 0; k < nodes.length; k++) {
0225:                    assertNull(m.put(nodes[k].getKey(), nodes[k].getValue()));
0226:                    try {
0227:                        m.put(nodes[k].getKey(), "foo");
0228:                    } catch (IllegalArgumentException ignored) {
0229:                    }
0230:                }
0231:            }
0232:
0233:            /**
0234:             * test remove() method
0235:             */
0236:
0237:            public void testRemove() {
0238:                BinaryTree m = new BinaryTree();
0239:                LocalTestNode nodes[] = makeLocalNodes();
0240:
0241:                for (int k = 0; k < nodes.length; k++) {
0242:                    m.put(nodes[k].getKey(), nodes[k]);
0243:                }
0244:                try {
0245:                    m.remove(null);
0246:                    fail("should have caught NullPointerException");
0247:                } catch (NullPointerException ignored) {
0248:                }
0249:                try {
0250:                    m.remove(new Object());
0251:                    fail("should have caught ClassCastException");
0252:                } catch (ClassCastException ignored) {
0253:                }
0254:                assertNull(m.remove(new Integer(-1)));
0255:                try {
0256:                    m.remove("foo");
0257:                    fail("should have caught ClassCastException");
0258:                } catch (ClassCastException ignored) {
0259:                }
0260:                for (int k = 0; k < nodes.length; k += 2) {
0261:                    Comparable key = nodes[k].getKey();
0262:
0263:                    assertNotNull(m.get(key));
0264:                    assertSame(nodes[k], m.remove(key));
0265:                    assertNull(m.remove(key));
0266:                    assertNull(m.get(key));
0267:                }
0268:                for (int k = 1; k < nodes.length; k += 2) {
0269:                    Comparable key = nodes[k].getKey();
0270:
0271:                    assertNotNull(m.get(key));
0272:                    assertSame(nodes[k], m.remove(key));
0273:                    assertNull(m.remove(key));
0274:                    assertNull(m.get(key));
0275:                }
0276:                assertTrue(m.isEmpty());
0277:            }
0278:
0279:            /**
0280:             * Method testPutAll
0281:             */
0282:
0283:            public void testPutAll() {
0284:                Map m = new BinaryTree();
0285:                LocalTestNode nodes[] = makeLocalNodes();
0286:
0287:                for (int k = 0; k < nodes.length; k++) {
0288:                    m.put(nodes[k].getKey(), nodes[k]);
0289:                }
0290:                Map m1 = new HashMap();
0291:
0292:                m1.put(null, "foo");
0293:                try {
0294:                    m.putAll(m1);
0295:                    fail("Should have caught NullPointerException");
0296:                } catch (NullPointerException ignored) {
0297:                }
0298:                m1 = new HashMap();
0299:                m1.put(new Object(), "bar");
0300:                try {
0301:                    m.putAll(m1);
0302:                    fail("Should have caught ClassCastException");
0303:                } catch (ClassCastException ignored) {
0304:                }
0305:                m1 = new HashMap();
0306:                m1.put("fubar", null);
0307:                try {
0308:                    m.putAll(m1);
0309:                    fail("Should have caught NullPointerException");
0310:                } catch (NullPointerException ignored) {
0311:                }
0312:                m1 = new HashMap();
0313:                m1.put("fubar", new Object());
0314:                try {
0315:                    m.putAll(m1);
0316:                    fail("Should have caught ClassCastException");
0317:                } catch (ClassCastException ignored) {
0318:                }
0319:                assertEquals(nodes.length, m.size());
0320:                m = new BinaryTree();
0321:                m1 = new HashMap();
0322:                for (int k = 0; k < nodes.length; k++) {
0323:                    m1.put(nodes[k].getKey(), nodes[k].getValue());
0324:                }
0325:                m.putAll(m1);
0326:                assertEquals(nodes.length, m.size());
0327:                for (int k = 0; k < nodes.length; k++) {
0328:                    assertSame(nodes[k].getValue(), m.get(nodes[k].getKey()));
0329:                }
0330:            }
0331:
0332:            /**
0333:             * test clear() method
0334:             */
0335:
0336:            public void testClear() {
0337:                Map m = new BinaryTree();
0338:                LocalTestNode nodes[] = makeLocalNodes();
0339:
0340:                for (int k = 0; k < nodes.length; k++) {
0341:                    m.put(nodes[k].getKey(), nodes[k].getValue());
0342:                    assertTrue(!m.isEmpty());
0343:                }
0344:                assertTrue(!m.isEmpty());
0345:                for (int k = 0; k < nodes.length; k++) {
0346:                    assertTrue(m.containsKey(nodes[k].getKey()));
0347:                    assertTrue(m.containsValue(nodes[k].getValue()));
0348:                }
0349:                m.clear();
0350:                assertTrue(m.isEmpty());
0351:                for (int k = 0; k < nodes.length; k++) {
0352:                    assertTrue(!m.containsKey(nodes[k].getKey()));
0353:                    assertTrue(!m.containsValue(nodes[k].getValue()));
0354:                }
0355:            }
0356:
0357:            /**
0358:             * test keySet() method
0359:             */
0360:
0361:            public void testKeySet() {
0362:                testKeySet(new BinaryTree());
0363:                Map m = new BinaryTree();
0364:                LocalTestNode nodes[] = makeLocalNodes();
0365:
0366:                for (int k = 0; k < nodes.length; k++) {
0367:                    m.put(nodes[k].getKey(), nodes[k]);
0368:                }
0369:                testKeySet(m);
0370:                m = new BinaryTree();
0371:                for (int k = 0; k < nodes.length; k++) {
0372:                    m.put(nodes[k].getKey(), nodes[k]);
0373:                }
0374:                int count = m.size();
0375:
0376:                for (Iterator iter = m.keySet().iterator(); iter.hasNext();) {
0377:                    iter.next();
0378:                    iter.remove();
0379:                    --count;
0380:                    assertEquals(count, m.size());
0381:                }
0382:                assertTrue(m.isEmpty());
0383:                m = new BinaryTree();
0384:                for (int k = 0; k < nodes.length; k++) {
0385:                    m.put(nodes[k].getKey(), nodes[k]);
0386:                }
0387:                Set s = m.keySet();
0388:
0389:                try {
0390:                    s.remove(null);
0391:                    fail("should have caught NullPointerException");
0392:                } catch (NullPointerException ignored) {
0393:                }
0394:                try {
0395:                    s.remove(new Object());
0396:                    fail("should have caught ClassCastException");
0397:                } catch (ClassCastException ignored) {
0398:                }
0399:                for (int k = 0; k < nodes.length; k++) {
0400:                    Comparable key = nodes[k].getKey();
0401:
0402:                    assertTrue(s.remove(key));
0403:                    assertTrue(!s.contains(key));
0404:                    assertTrue(!m.containsKey(key));
0405:                    assertTrue(!m.containsValue(nodes[k]));
0406:                }
0407:                assertTrue(m.isEmpty());
0408:                m = new BinaryTree();
0409:                Collection c1 = new LinkedList();
0410:                Collection c2 = new LinkedList();
0411:
0412:                c2.add(new Integer(-99));
0413:                for (int k = 0; k < nodes.length; k++) {
0414:                    m.put(nodes[k].getKey(), nodes[k]);
0415:                    c1.add(nodes[k].getKey());
0416:                    c2.add(nodes[k].getKey());
0417:                }
0418:                assertTrue(m.keySet().containsAll(c1));
0419:                assertTrue(!m.keySet().containsAll(c2));
0420:                m = new BinaryTree();
0421:                c1 = new LinkedList();
0422:                c1.add(new Integer(-55));
0423:                try {
0424:                    m.keySet().addAll(c1);
0425:                    fail("should have caught exception of addAll()");
0426:                } catch (UnsupportedOperationException ignored) {
0427:                }
0428:                for (int k = 0; k < nodes.length; k++) {
0429:                    m.put(nodes[k].getKey(), nodes[k]);
0430:                    c1.add(nodes[k].getKey());
0431:                }
0432:                assertTrue(!m.keySet().retainAll(c1));
0433:                assertEquals(nodes.length, m.size());
0434:                m = new BinaryTree();
0435:                c1 = new LinkedList();
0436:                for (int k = 0; k < nodes.length; k++) {
0437:                    m.put(nodes[k].getKey(), nodes[k]);
0438:                    if (k % 2 == 1) {
0439:                        c1.add(nodes[k].getKey());
0440:                    }
0441:                }
0442:                assertTrue(m.keySet().retainAll(c1));
0443:                assertEquals(nodes.length / 2, m.size());
0444:                m = new BinaryTree();
0445:                c1 = new LinkedList();
0446:                for (int k = 0; k < nodes.length; k++) {
0447:                    m.put(nodes[k].getKey(), nodes[k]);
0448:                }
0449:                assertTrue(m.keySet().retainAll(c1));
0450:                assertEquals(0, m.size());
0451:                m = new BinaryTree();
0452:                c1 = new LinkedList();
0453:                for (int k = 0; k < nodes.length; k++) {
0454:                    m.put(nodes[k].getKey(), nodes[k]);
0455:                }
0456:                assertTrue(!m.keySet().removeAll(c1));
0457:                assertEquals(nodes.length, m.size());
0458:                m = new BinaryTree();
0459:                c1 = new LinkedList();
0460:                for (int k = 0; k < nodes.length; k++) {
0461:                    m.put(nodes[k].getKey(), nodes[k]);
0462:                    if (k % 2 == 0) {
0463:                        c1.add(nodes[k].getKey());
0464:                    }
0465:                }
0466:                assertTrue(m.keySet().removeAll(c1));
0467:                assertEquals(nodes.length / 2, m.size());
0468:                m = new BinaryTree();
0469:                c1 = new LinkedList();
0470:                for (int k = 0; k < nodes.length; k++) {
0471:                    m.put(nodes[k].getKey(), nodes[k]);
0472:                    c1.add(nodes[k].getKey());
0473:                }
0474:                assertTrue(m.keySet().removeAll(c1));
0475:                assertTrue(m.size() == 0);
0476:                m = new BinaryTree();
0477:                for (int k = 0; k < nodes.length; k++) {
0478:                    m.put(nodes[k].getKey(), nodes[k]);
0479:                }
0480:                m.keySet().clear();
0481:                assertTrue(m.size() == 0);
0482:            }
0483:
0484:            /**
0485:             * test values() method
0486:             */
0487:
0488:            public void testValues() {
0489:                testValues(new BinaryTree());
0490:                Map m = new BinaryTree();
0491:                LocalTestNode nodes[] = makeLocalNodes();
0492:
0493:                for (int k = 0; k < nodes.length; k++) {
0494:                    m.put(nodes[k].getKey(), nodes[k]);
0495:                }
0496:                testValues(m);
0497:                m = new BinaryTree();
0498:                for (int k = 0; k < nodes.length; k++) {
0499:                    m.put(nodes[k].getKey(), nodes[k]);
0500:                }
0501:                int count = m.size();
0502:
0503:                for (Iterator iter = m.values().iterator(); iter.hasNext();) {
0504:                    iter.next();
0505:                    iter.remove();
0506:                    --count;
0507:                    assertEquals(count, m.size());
0508:                }
0509:                assertTrue(m.isEmpty());
0510:                m = new BinaryTree();
0511:                for (int k = 0; k < nodes.length; k++) {
0512:                    m.put(nodes[k].getKey(), nodes[k]);
0513:                }
0514:                count = m.size();
0515:                Collection s = m.values();
0516:
0517:                for (int k = 0; k < count; k++) {
0518:                    assertTrue(s.remove(nodes[k]));
0519:                    assertTrue(!s.contains(nodes[k]));
0520:                    assertTrue(!m.containsKey(nodes[k].getKey()));
0521:                    assertTrue(!m.containsValue(nodes[k]));
0522:                }
0523:                assertTrue(m.isEmpty());
0524:                m = new BinaryTree();
0525:                Collection c1 = new LinkedList();
0526:                Collection c2 = new LinkedList();
0527:
0528:                c2.add(new LocalTestNode(-123));
0529:                for (int k = 0; k < nodes.length; k++) {
0530:                    m.put(nodes[k].getKey(), nodes[k]);
0531:                    c1.add(nodes[k]);
0532:                    c2.add(nodes[k]);
0533:                }
0534:                assertTrue(m.values().containsAll(c1));
0535:                assertTrue(!m.values().containsAll(c2));
0536:                m = new BinaryTree();
0537:                c1 = new LinkedList();
0538:                for (int k = 0; k < nodes.length; k++) {
0539:                    m.put(nodes[k].getKey(), nodes[k]);
0540:                    c1.add(nodes[k]);
0541:                }
0542:                try {
0543:                    m.values().addAll(c1);
0544:                    fail("should have caught exception of addAll()");
0545:                } catch (UnsupportedOperationException ignored) {
0546:                }
0547:                m = new BinaryTree();
0548:                c1 = new LinkedList();
0549:                for (int k = 0; k < nodes.length; k++) {
0550:                    m.put(nodes[k].getKey(), nodes[k]);
0551:                    c1.add(nodes[k]);
0552:                }
0553:                assertTrue(!m.values().retainAll(c1));
0554:                assertEquals(nodes.length, m.size());
0555:                m = new BinaryTree();
0556:                c1 = new LinkedList();
0557:                for (int k = 0; k < nodes.length; k++) {
0558:                    m.put(nodes[k].getKey(), nodes[k]);
0559:                    if (k % 2 == 1) {
0560:                        c1.add(nodes[k]);
0561:                    }
0562:                }
0563:                assertTrue(m.values().retainAll(c1));
0564:                assertEquals(nodes.length / 2, m.size());
0565:                m = new BinaryTree();
0566:                c1 = new LinkedList();
0567:                for (int k = 0; k < nodes.length; k++) {
0568:                    m.put(nodes[k].getKey(), nodes[k]);
0569:                }
0570:                assertTrue(m.values().retainAll(c1));
0571:                assertEquals(0, m.size());
0572:                m = new BinaryTree();
0573:                c1 = new LinkedList();
0574:                for (int k = 0; k < nodes.length; k++) {
0575:                    m.put(nodes[k].getKey(), nodes[k]);
0576:                }
0577:                assertTrue(!m.values().removeAll(c1));
0578:                assertEquals(nodes.length, m.size());
0579:                m = new BinaryTree();
0580:                c1 = new LinkedList();
0581:                for (int k = 0; k < nodes.length; k++) {
0582:                    m.put(nodes[k].getKey(), nodes[k]);
0583:                    if (k % 2 == 0) {
0584:                        c1.add(nodes[k]);
0585:                    }
0586:                }
0587:                assertTrue(m.values().removeAll(c1));
0588:                assertEquals(nodes.length / 2, m.size());
0589:                m = new BinaryTree();
0590:                c1 = new LinkedList();
0591:                for (int k = 0; k < nodes.length; k++) {
0592:                    m.put(nodes[k].getKey(), nodes[k]);
0593:                    c1.add(nodes[k]);
0594:                }
0595:                assertTrue(m.values().removeAll(c1));
0596:                assertEquals(0, m.size());
0597:                m = new BinaryTree();
0598:                for (int k = 0; k < nodes.length; k++) {
0599:                    m.put(nodes[k].getKey(), nodes[k]);
0600:                }
0601:                m.values().clear();
0602:                assertEquals(0, m.size());
0603:            }
0604:
0605:            /**
0606:             * test entrySet() method
0607:             */
0608:
0609:            public void testEntrySet() {
0610:                testEntrySet(new BinaryTree());
0611:                Map m = new BinaryTree();
0612:                LocalTestNode nodes[] = makeLocalNodes();
0613:
0614:                for (int k = 0; k < nodes.length; k++) {
0615:                    m.put(nodes[k].getKey(), nodes[k]);
0616:                }
0617:                testEntrySet(m);
0618:                m = new BinaryTree();
0619:                for (int k = 0; k < nodes.length; k++) {
0620:                    m.put(nodes[k].getKey(), nodes[k]);
0621:                }
0622:                try {
0623:                    ((Map.Entry) m.entrySet().iterator().next())
0624:                            .setValue(new LocalTestNode(-1));
0625:                    fail("Should have caught UnsupportedOperationException");
0626:                } catch (UnsupportedOperationException ignored) {
0627:                }
0628:                int count = m.size();
0629:
0630:                for (Iterator iter = m.entrySet().iterator(); iter.hasNext();) {
0631:                    iter.next();
0632:                    iter.remove();
0633:                    --count;
0634:                    assertEquals(count, m.size());
0635:                }
0636:                assertTrue(m.isEmpty());
0637:                m = new BinaryTree();
0638:                Collection c1 = new LinkedList();
0639:
0640:                for (int k = 0; k < nodes.length; k++) {
0641:                    m.put(nodes[k].getKey(), nodes[k]);
0642:                    c1.add(nodes[k].getKey());
0643:                }
0644:                try {
0645:                    m.entrySet().addAll(c1);
0646:                    fail("should have caught exception of addAll()");
0647:                } catch (UnsupportedOperationException ignored) {
0648:                }
0649:                m = new BinaryTree();
0650:                for (int k = 0; k < nodes.length; k++) {
0651:                    m.put(nodes[k].getKey(), nodes[k]);
0652:                }
0653:                m.entrySet().clear();
0654:                assertEquals(0, m.size());
0655:                m = new BinaryTree();
0656:                for (int k = 0; k < nodes.length; k++) {
0657:                    m.put(nodes[k].getKey(), nodes[k]);
0658:                }
0659:                int x = 0;
0660:
0661:                for (Iterator iter = m.entrySet().iterator(); iter.hasNext();) {
0662:                    Map.Entry entry = (Map.Entry) iter.next();
0663:
0664:                    assertSame(entry.getKey(), nodes[x].getKey());
0665:                    assertSame(entry.getValue(), nodes[x]);
0666:                    x++;
0667:                }
0668:            }
0669:
0670:            /**
0671:             * Method testEquals
0672:             */
0673:
0674:            public void testEquals() {
0675:                Map m = new BinaryTree();
0676:                LocalTestNode nodes[] = makeLocalNodes();
0677:
0678:                for (int k = 0; k < nodes.length; k++) {
0679:                    m.put(nodes[k].getKey(), nodes[k]);
0680:                }
0681:                assertTrue(!m.equals(null));
0682:                assertEquals(m, m);
0683:                Map m1 = new HashMap();
0684:
0685:                for (int k = 0; k < nodes.length; k++) {
0686:                    m1.put(nodes[k].getKey(), nodes[k]);
0687:                }
0688:                assertEquals(m, m1);
0689:                m1 = new BinaryTree();
0690:                for (int k = 0; k < (nodes.length - 1); k++) {
0691:                    m1.put(nodes[k].getKey(), nodes[k]);
0692:                }
0693:                assertTrue(!m.equals(m1));
0694:                m1 = new BinaryTree();
0695:                for (int k = 0; k < nodes.length; k++) {
0696:                    m1.put(nodes[k].getKey(), nodes[k]);
0697:                }
0698:                LocalTestNode node1 = new LocalTestNode(-1000);
0699:
0700:                m1.put(node1.getKey(), node1);
0701:                assertTrue(!m.equals(m1));
0702:                m1 = new BinaryTree();
0703:                for (int k = 0; k < nodes.length; k++) {
0704:                    m1.put(nodes[k].getKey(), nodes[nodes.length - (k + 1)]);
0705:                }
0706:                assertTrue(!m.equals(m1));
0707:                m1 = new BinaryTree();
0708:                for (int k = nodes.length - 1; k >= 0; k--) {
0709:                    m1.put(nodes[k].getKey(), nodes[k]);
0710:                }
0711:                assertEquals(m, m1);
0712:            }
0713:
0714:            /**
0715:             * test hashCode() method
0716:             */
0717:
0718:            public void testHashCode() {
0719:                Map m = new BinaryTree();
0720:                LocalTestNode nodes[] = makeLocalNodes();
0721:
0722:                for (int k = 0; k < nodes.length; k++) {
0723:                    m.put(nodes[k].getKey(), nodes[k]);
0724:                }
0725:                Map m1 = new BinaryTree();
0726:
0727:                for (int k = nodes.length - 1; k >= 0; k--) {
0728:                    m1.put(nodes[k].getKey(), nodes[k]);
0729:                }
0730:                assertTrue(m.hashCode() == m1.hashCode());
0731:            }
0732:
0733:            /**
0734:             * test constructors
0735:             */
0736:
0737:            public void testConstructors() {
0738:                BinaryTree m = new BinaryTree();
0739:
0740:                assertTrue(m.isEmpty());
0741:                BinaryTree m1 = new BinaryTree(m);
0742:
0743:                assertTrue(m1.isEmpty());
0744:                m1 = new BinaryTree();
0745:                LocalTestNode nodes[] = makeLocalNodes();
0746:
0747:                for (int k = 0; k < nodes.length; k++) {
0748:                    m1.put(nodes[k].getKey(), nodes[k]);
0749:                }
0750:                m = new BinaryTree(m1);
0751:                assertEquals(m, m1);
0752:                Map m2 = new HashMap();
0753:
0754:                for (int k = 0; k < nodes.length; k++) {
0755:                    m2.put(nodes[k].getKey(), nodes[k]);
0756:                }
0757:                m = new BinaryTree(m2);
0758:                assertEquals(m, m2);
0759:
0760:                // reject duplicated values
0761:                m2 = new HashMap();
0762:                m2.put("1", "foo");
0763:                m2.put("2", "foo");
0764:                try {
0765:                    m = new BinaryTree(m2);
0766:                    fail("Should have caught IllegalArgumentException");
0767:                } catch (IllegalArgumentException ignored) {
0768:                }
0769:
0770:                // reject null values
0771:                m2.put("2", null);
0772:                try {
0773:                    m = new BinaryTree(m2);
0774:                    fail("Should have caught NullPointerException");
0775:                } catch (NullPointerException ignored) {
0776:                }
0777:
0778:                // reject non-Comparable values
0779:                m2.put("2", new Object());
0780:                try {
0781:                    m = new BinaryTree(m2);
0782:                    fail("Should have caught ClassCastException");
0783:                } catch (ClassCastException ignored) {
0784:                }
0785:
0786:                // reject incompatible values
0787:                m2.put("2", new Integer(2));
0788:                try {
0789:                    m = new BinaryTree(m2);
0790:                    fail("Should have caught ClassCastException");
0791:                } catch (ClassCastException ignored) {
0792:                }
0793:
0794:                // reject incompatible keys
0795:                m2.remove("2");
0796:                m2.put(new Integer(2), "bad key");
0797:                try {
0798:                    m = new BinaryTree(m2);
0799:                    fail("Should have caught ClassCastException");
0800:                } catch (ClassCastException ignored) {
0801:                }
0802:
0803:                // reject non-Comparable keys
0804:                m2.clear();
0805:                m2.put("1", "foo");
0806:                m2.put(new Object(), "bad key");
0807:                try {
0808:                    m = new BinaryTree(m2);
0809:                    fail("Should have caught ClassCastException");
0810:                } catch (ClassCastException ignored) {
0811:                }
0812:            }
0813:
0814:            /**
0815:             * test getKeyForValue() method
0816:             */
0817:
0818:            public void testGetKeyForValue() {
0819:                BinaryTree m = new BinaryTree();
0820:
0821:                try {
0822:                    m.getKeyForValue(new Object());
0823:                    fail("should have caught ClassCastException");
0824:                } catch (ClassCastException ignored) {
0825:                }
0826:                try {
0827:                    m.getKeyForValue(null);
0828:                    fail("should have caught NullPointerException");
0829:                } catch (NullPointerException ignored) {
0830:                }
0831:                assertNull(m.getKeyForValue("foo"));
0832:                LocalTestNode nodes[] = makeLocalNodes();
0833:
0834:                for (int k = 0; k < nodes.length; k++) {
0835:                    m.put(nodes[k].getKey(), nodes[k]);
0836:                    assertSame(m.getKeyForValue(nodes[k]), nodes[k].getKey());
0837:                }
0838:                assertNull(m.getKeyForValue(new LocalTestNode(-1)));
0839:                try {
0840:                    m.getKeyForValue("foo");
0841:                    fail("Should have caught ClassCastException");
0842:                } catch (ClassCastException ignored) {
0843:                }
0844:                for (int k = 0; k < nodes.length; k++) {
0845:                    assertNotNull(m.getKeyForValue(nodes[k]));
0846:                    m.remove(nodes[k].getKey());
0847:                    assertNull(m.getKeyForValue(nodes[k]));
0848:                }
0849:            }
0850:
0851:            /**
0852:             * test removeValue() method
0853:             */
0854:
0855:            public void testRemoveValue() {
0856:                BinaryTree m = new BinaryTree();
0857:                LocalTestNode nodes[] = makeLocalNodes();
0858:
0859:                for (int k = 0; k < nodes.length; k++) {
0860:                    m.put(nodes[k].getKey(), nodes[k]);
0861:                }
0862:                try {
0863:                    m.removeValue(null);
0864:                    fail("should have caught NullPointerException");
0865:                } catch (NullPointerException ignored) {
0866:                }
0867:                try {
0868:                    m.removeValue(new Object());
0869:                    fail("should have caught ClassCastException");
0870:                } catch (ClassCastException ignored) {
0871:                }
0872:                assertNull(m.remove(new Integer(-1)));
0873:                try {
0874:                    m.removeValue("foo");
0875:                    fail("should have caught ClassCastException");
0876:                } catch (ClassCastException ignored) {
0877:                }
0878:                for (int k = 0; k < nodes.length; k += 2) {
0879:                    assertNotNull(m.getKeyForValue(nodes[k]));
0880:                    assertSame(nodes[k].getKey(), m.removeValue(nodes[k]));
0881:                    assertNull(m.removeValue(nodes[k]));
0882:                    assertNull(m.getKeyForValue(nodes[k]));
0883:                }
0884:                for (int k = 1; k < nodes.length; k += 2) {
0885:                    assertNotNull(m.getKeyForValue(nodes[k]));
0886:                    assertSame(nodes[k].getKey(), m.removeValue(nodes[k]));
0887:                    assertNull(m.removeValue(nodes[k]));
0888:                    assertNull(m.getKeyForValue(nodes[k]));
0889:                }
0890:                assertTrue(m.isEmpty());
0891:            }
0892:
0893:            /**
0894:             * test entrySetByValue() method
0895:             */
0896:
0897:            public void testEntrySetByValue() {
0898:                testEntrySetByValue(new BinaryTree());
0899:                BinaryTree m = new BinaryTree();
0900:                LocalTestNode nodes[] = makeLocalNodes();
0901:
0902:                for (int k = 0; k < nodes.length; k++) {
0903:                    m.put(nodes[k].getKey(), nodes[k]);
0904:                }
0905:                testEntrySetByValue(m);
0906:                m = new BinaryTree();
0907:                for (int k = 0; k < nodes.length; k++) {
0908:                    m.put(nodes[k].getKey(), nodes[k]);
0909:                }
0910:                try {
0911:                    ((Map.Entry) m.entrySetByValue().iterator().next())
0912:                            .setValue(new LocalTestNode(-1));
0913:                    fail("Should have caught UnsupportedOperationException");
0914:                } catch (UnsupportedOperationException ignored) {
0915:                }
0916:                int count = m.size();
0917:
0918:                for (Iterator iter = m.entrySetByValue().iterator(); iter
0919:                        .hasNext();) {
0920:                    iter.next();
0921:                    iter.remove();
0922:                    --count;
0923:                    assertEquals(count, m.size());
0924:                }
0925:                assertTrue(m.isEmpty());
0926:                m = new BinaryTree();
0927:                Collection c1 = new LinkedList();
0928:
0929:                for (int k = 0; k < nodes.length; k++) {
0930:                    m.put(nodes[k].getKey(), nodes[k]);
0931:                    c1.add(nodes[k].getKey());
0932:                }
0933:                try {
0934:                    m.entrySetByValue().addAll(c1);
0935:                    fail("should have caught exception of addAll()");
0936:                } catch (UnsupportedOperationException ignored) {
0937:                }
0938:                m = new BinaryTree();
0939:                for (int k = 0; k < nodes.length; k++) {
0940:                    m.put(nodes[k].getKey(), nodes[k]);
0941:                }
0942:                m.entrySetByValue().clear();
0943:                assertEquals(0, m.size());
0944:                m = new BinaryTree();
0945:                for (int k = 0; k < nodes.length; k++) {
0946:                    m.put(nodes[k].getKey(), nodes[k]);
0947:                }
0948:                int x = 0;
0949:
0950:                for (Iterator iter = m.entrySetByValue().iterator(); iter
0951:                        .hasNext();) {
0952:                    Map.Entry entry = (Map.Entry) iter.next();
0953:
0954:                    assertSame(entry.getKey(), nodes[x].getKey());
0955:                    assertSame(entry.getValue(), nodes[x]);
0956:                    x++;
0957:                }
0958:            }
0959:
0960:            /**
0961:             * test keySetByValue() method
0962:             */
0963:
0964:            public void testKeySetByValue() {
0965:                testKeySetByValue(new BinaryTree());
0966:                BinaryTree m = new BinaryTree();
0967:                LocalTestNode nodes[] = makeLocalNodes();
0968:
0969:                for (int k = 0; k < nodes.length; k++) {
0970:                    m.put(nodes[k].getKey(), nodes[k]);
0971:                }
0972:                testKeySetByValue(m);
0973:                m = new BinaryTree();
0974:                for (int k = 0; k < nodes.length; k++) {
0975:                    m.put(nodes[k].getKey(), nodes[k]);
0976:                }
0977:                int count = m.size();
0978:
0979:                for (Iterator iter = m.keySetByValue().iterator(); iter
0980:                        .hasNext();) {
0981:                    iter.next();
0982:                    iter.remove();
0983:                    --count;
0984:                    assertEquals(count, m.size());
0985:                }
0986:                assertTrue(m.isEmpty());
0987:                m = new BinaryTree();
0988:                for (int k = 0; k < nodes.length; k++) {
0989:                    m.put(nodes[k].getKey(), nodes[k]);
0990:                }
0991:                Set s = m.keySetByValue();
0992:
0993:                try {
0994:                    s.remove(null);
0995:                    fail("should have caught NullPointerException");
0996:                } catch (NullPointerException ignored) {
0997:                }
0998:                try {
0999:                    s.remove(new Object());
1000:                    fail("should have caught ClassCastException");
1001:                } catch (ClassCastException ignored) {
1002:                }
1003:                for (int k = 0; k < nodes.length; k++) {
1004:                    Comparable key = nodes[k].getKey();
1005:
1006:                    assertTrue(s.remove(key));
1007:                    assertTrue(!s.contains(key));
1008:                    assertTrue(!m.containsKey(key));
1009:                    assertTrue(!m.containsValue(nodes[k]));
1010:                }
1011:                assertTrue(m.isEmpty());
1012:                m = new BinaryTree();
1013:                Collection c1 = new LinkedList();
1014:                Collection c2 = new LinkedList();
1015:
1016:                c2.add(new Integer(-99));
1017:                for (int k = 0; k < nodes.length; k++) {
1018:                    m.put(nodes[k].getKey(), nodes[k]);
1019:                    c1.add(nodes[k].getKey());
1020:                    c2.add(nodes[k].getKey());
1021:                }
1022:                assertTrue(m.keySetByValue().containsAll(c1));
1023:                assertTrue(!m.keySetByValue().containsAll(c2));
1024:                m = new BinaryTree();
1025:                c1 = new LinkedList();
1026:                c1.add(new Integer(-55));
1027:                try {
1028:                    m.keySetByValue().addAll(c1);
1029:                    fail("should have caught exception of addAll()");
1030:                } catch (UnsupportedOperationException ignored) {
1031:                }
1032:                for (int k = 0; k < nodes.length; k++) {
1033:                    m.put(nodes[k].getKey(), nodes[k]);
1034:                    c1.add(nodes[k].getKey());
1035:                }
1036:                assertTrue(!m.keySetByValue().retainAll(c1));
1037:                assertEquals(nodes.length, m.size());
1038:                m = new BinaryTree();
1039:                c1 = new LinkedList();
1040:                for (int k = 0; k < nodes.length; k++) {
1041:                    m.put(nodes[k].getKey(), nodes[k]);
1042:                    if (k % 2 == 1) {
1043:                        c1.add(nodes[k].getKey());
1044:                    }
1045:                }
1046:                assertTrue(m.keySetByValue().retainAll(c1));
1047:                assertEquals(nodes.length / 2, m.size());
1048:                m = new BinaryTree();
1049:                c1 = new LinkedList();
1050:                for (int k = 0; k < nodes.length; k++) {
1051:                    m.put(nodes[k].getKey(), nodes[k]);
1052:                }
1053:                assertTrue(m.keySetByValue().retainAll(c1));
1054:                assertEquals(0, m.size());
1055:                m = new BinaryTree();
1056:                c1 = new LinkedList();
1057:                for (int k = 0; k < nodes.length; k++) {
1058:                    m.put(nodes[k].getKey(), nodes[k]);
1059:                }
1060:                assertTrue(!m.keySetByValue().removeAll(c1));
1061:                assertEquals(nodes.length, m.size());
1062:                m = new BinaryTree();
1063:                c1 = new LinkedList();
1064:                for (int k = 0; k < nodes.length; k++) {
1065:                    m.put(nodes[k].getKey(), nodes[k]);
1066:                    if (k % 2 == 0) {
1067:                        c1.add(nodes[k].getKey());
1068:                    }
1069:                }
1070:                assertTrue(m.keySetByValue().removeAll(c1));
1071:                assertEquals(nodes.length / 2, m.size());
1072:                m = new BinaryTree();
1073:                c1 = new LinkedList();
1074:                for (int k = 0; k < nodes.length; k++) {
1075:                    m.put(nodes[k].getKey(), nodes[k]);
1076:                    c1.add(nodes[k].getKey());
1077:                }
1078:                assertTrue(m.keySetByValue().removeAll(c1));
1079:                assertTrue(m.size() == 0);
1080:                m = new BinaryTree();
1081:                for (int k = 0; k < nodes.length; k++) {
1082:                    m.put(nodes[k].getKey(), nodes[k]);
1083:                }
1084:                m.keySetByValue().clear();
1085:                assertTrue(m.size() == 0);
1086:            }
1087:
1088:            /**
1089:             * test valuesByValue() method
1090:             */
1091:
1092:            public void testValuesByValue() {
1093:                testValuesByValue(new BinaryTree());
1094:                BinaryTree m = new BinaryTree();
1095:                LocalTestNode nodes[] = makeLocalNodes();
1096:
1097:                for (int k = 0; k < nodes.length; k++) {
1098:                    m.put(nodes[k].getKey(), nodes[k]);
1099:                }
1100:                testValuesByValue(m);
1101:                m = new BinaryTree();
1102:                for (int k = 0; k < nodes.length; k++) {
1103:                    m.put(nodes[k].getKey(), nodes[k]);
1104:                }
1105:                int count = m.size();
1106:
1107:                for (Iterator iter = m.valuesByValue().iterator(); iter
1108:                        .hasNext();) {
1109:                    iter.next();
1110:                    iter.remove();
1111:                    --count;
1112:                    assertEquals(count, m.size());
1113:                }
1114:                assertTrue(m.isEmpty());
1115:                m = new BinaryTree();
1116:                for (int k = 0; k < nodes.length; k++) {
1117:                    m.put(nodes[k].getKey(), nodes[k]);
1118:                }
1119:                count = m.size();
1120:                Collection s = m.valuesByValue();
1121:
1122:                for (int k = 0; k < count; k++) {
1123:                    assertTrue(s.remove(nodes[k]));
1124:                    assertTrue(!s.contains(nodes[k]));
1125:                    assertTrue(!m.containsKey(nodes[k].getKey()));
1126:                    assertTrue(!m.containsValue(nodes[k]));
1127:                }
1128:                assertTrue(m.isEmpty());
1129:                m = new BinaryTree();
1130:                Collection c1 = new LinkedList();
1131:                Collection c2 = new LinkedList();
1132:
1133:                c2.add(new LocalTestNode(-123));
1134:                for (int k = 0; k < nodes.length; k++) {
1135:                    m.put(nodes[k].getKey(), nodes[k]);
1136:                    c1.add(nodes[k]);
1137:                    c2.add(nodes[k]);
1138:                }
1139:                assertTrue(m.valuesByValue().containsAll(c1));
1140:                assertTrue(!m.valuesByValue().containsAll(c2));
1141:                m = new BinaryTree();
1142:                c1 = new LinkedList();
1143:                for (int k = 0; k < nodes.length; k++) {
1144:                    m.put(nodes[k].getKey(), nodes[k]);
1145:                    c1.add(nodes[k]);
1146:                }
1147:                try {
1148:                    m.valuesByValue().addAll(c1);
1149:                    fail("should have caught exception of addAll()");
1150:                } catch (UnsupportedOperationException ignored) {
1151:                }
1152:                m = new BinaryTree();
1153:                c1 = new LinkedList();
1154:                for (int k = 0; k < nodes.length; k++) {
1155:                    m.put(nodes[k].getKey(), nodes[k]);
1156:                    c1.add(nodes[k]);
1157:                }
1158:                assertTrue(!m.valuesByValue().retainAll(c1));
1159:                assertEquals(nodes.length, m.size());
1160:                m = new BinaryTree();
1161:                c1 = new LinkedList();
1162:                for (int k = 0; k < nodes.length; k++) {
1163:                    m.put(nodes[k].getKey(), nodes[k]);
1164:                    if (k % 2 == 1) {
1165:                        c1.add(nodes[k]);
1166:                    }
1167:                }
1168:                assertTrue(m.valuesByValue().retainAll(c1));
1169:                assertEquals(nodes.length / 2, m.size());
1170:                m = new BinaryTree();
1171:                c1 = new LinkedList();
1172:                for (int k = 0; k < nodes.length; k++) {
1173:                    m.put(nodes[k].getKey(), nodes[k]);
1174:                }
1175:                assertTrue(m.valuesByValue().retainAll(c1));
1176:                assertEquals(0, m.size());
1177:                m = new BinaryTree();
1178:                c1 = new LinkedList();
1179:                for (int k = 0; k < nodes.length; k++) {
1180:                    m.put(nodes[k].getKey(), nodes[k]);
1181:                }
1182:                assertTrue(!m.valuesByValue().removeAll(c1));
1183:                assertEquals(nodes.length, m.size());
1184:                m = new BinaryTree();
1185:                c1 = new LinkedList();
1186:                for (int k = 0; k < nodes.length; k++) {
1187:                    m.put(nodes[k].getKey(), nodes[k]);
1188:                    if (k % 2 == 0) {
1189:                        c1.add(nodes[k]);
1190:                    }
1191:                }
1192:                assertTrue(m.valuesByValue().removeAll(c1));
1193:                assertEquals(nodes.length / 2, m.size());
1194:                m = new BinaryTree();
1195:                c1 = new LinkedList();
1196:                for (int k = 0; k < nodes.length; k++) {
1197:                    m.put(nodes[k].getKey(), nodes[k]);
1198:                    c1.add(nodes[k]);
1199:                }
1200:                assertTrue(m.valuesByValue().removeAll(c1));
1201:                assertEquals(0, m.size());
1202:                m = new BinaryTree();
1203:                for (int k = 0; k < nodes.length; k++) {
1204:                    m.put(nodes[k].getKey(), nodes[k]);
1205:                }
1206:                m.valuesByValue().clear();
1207:                assertEquals(0, m.size());
1208:            }
1209:
1210:            /* ********** START helper methods ********** */
1211:            private void testKeySet(final Map m) {
1212:                Set s = m.keySet();
1213:
1214:                assertEquals(m.size(), s.size());
1215:                assertEquals(m.isEmpty(), s.isEmpty());
1216:                LocalTestNode node = new LocalTestNode(-1);
1217:
1218:                m.put(node.getKey(), node);
1219:                assertTrue(s.contains(node.getKey()));
1220:                assertEquals(m.size(), s.size());
1221:                assertEquals(m.isEmpty(), s.isEmpty());
1222:                m.remove(node.getKey());
1223:                assertTrue(!s.contains(node.getKey()));
1224:                assertEquals(m.size(), s.size());
1225:                assertEquals(m.isEmpty(), s.isEmpty());
1226:                try {
1227:                    s.contains(null);
1228:                    fail("should have caught NullPointerException");
1229:                } catch (NullPointerException ignored) {
1230:                }
1231:                try {
1232:                    s.contains(new Object());
1233:                    fail("should have caught ClassCastException");
1234:                } catch (ClassCastException ignored) {
1235:                }
1236:                for (int k = 0; k < m.size(); k++) {
1237:                    assertTrue(s.contains(new Integer(k)));
1238:                }
1239:                int count = 0;
1240:
1241:                for (Iterator iter = s.iterator(); iter.hasNext();) {
1242:                    iter.next();
1243:                    ++count;
1244:                }
1245:                assertEquals(count, s.size());
1246:
1247:                // force the map to have some content
1248:                m.put(node.getKey(), node);
1249:                Iterator iter = m.keySet().iterator();
1250:                LocalTestNode node2 = new LocalTestNode(-2);
1251:
1252:                m.put(node2.getKey(), node2);
1253:                try {
1254:                    iter.next();
1255:                    fail("next() should have thrown an exception after a put");
1256:                } catch (ConcurrentModificationException ignored) {
1257:                }
1258:                m.remove(node2.getKey());
1259:                iter = s.iterator();
1260:                m.remove(node.getKey());
1261:                try {
1262:                    iter.next();
1263:                    fail("next() should have thrown an exception after a Map remove");
1264:                } catch (ConcurrentModificationException ignored) {
1265:                }
1266:                m.put(node.getKey(), node);
1267:                iter = s.iterator();
1268:                s.remove(node.getKey());
1269:                try {
1270:                    iter.next();
1271:                    fail("next() should have thrown an exception after a Set remove");
1272:                } catch (ConcurrentModificationException ignored) {
1273:                }
1274:                iter = s.iterator();
1275:                count = 0;
1276:                boolean terminated = false;
1277:
1278:                try {
1279:                    while (true) {
1280:                        iter.next();
1281:                        ++count;
1282:                    }
1283:                } catch (NoSuchElementException ignored) {
1284:                    terminated = true;
1285:                }
1286:                assertTrue(terminated);
1287:                assertEquals(m.size(), count);
1288:                iter = s.iterator();
1289:                try {
1290:                    iter.remove();
1291:                    fail("Should have thrown exception");
1292:                } catch (IllegalStateException ignored) {
1293:                }
1294:                m.put(node.getKey(), node);
1295:                iter = s.iterator();
1296:                iter.next();
1297:                m.put(node2.getKey(), node2);
1298:                try {
1299:                    iter.remove();
1300:                    fail("should have thrown exception");
1301:                } catch (ConcurrentModificationException ignored) {
1302:                }
1303:                Iterator iter2 = s.iterator();
1304:
1305:                iter2.next();
1306:                LocalTestNode node3 = new LocalTestNode(-3);
1307:
1308:                m.put(node3.getKey(), node3);
1309:                try {
1310:                    iter2.remove();
1311:                    fail("should have thrown exception");
1312:                } catch (ConcurrentModificationException ignored) {
1313:                }
1314:                int r_count = 0;
1315:
1316:                for (iter = s.iterator(); iter.hasNext();) {
1317:                    if (iter.next().equals(node.getKey())) {
1318:                        try {
1319:                            iter.remove();
1320:                            ++r_count;
1321:                            iter.remove();
1322:                            fail("2nd remove should have failed");
1323:                        } catch (IllegalStateException ignored) {
1324:                            assertEquals(1, r_count);
1325:                        }
1326:                    }
1327:                }
1328:                assertEquals(1, r_count);
1329:                assertTrue(!s.contains(node.getKey()));
1330:                r_count = 0;
1331:                m.put(node.getKey(), node);
1332:                Object[] a1 = s.toArray();
1333:
1334:                assertEquals(s.size(), a1.length);
1335:                if (a1.length > 1) {
1336:                    Comparable first = (Comparable) a1[0];
1337:
1338:                    for (int k = 1; k < a1.length; k++) {
1339:                        Comparable second = (Comparable) a1[k];
1340:
1341:                        assertTrue(first.compareTo(second) < 0);
1342:                        first = second;
1343:                    }
1344:                    iter = s.iterator();
1345:                    first = (Comparable) iter.next();
1346:                    for (; iter.hasNext();) {
1347:                        Comparable second = (Comparable) iter.next();
1348:
1349:                        assertTrue(first.compareTo(second) < 0);
1350:                        first = second;
1351:                    }
1352:                }
1353:                try {
1354:                    String array2[] = (String[]) s.toArray(new String[0]);
1355:
1356:                    if (s.size() != 0) {
1357:                        fail("should have caught exception creating an invalid array");
1358:                    }
1359:                } catch (ArrayStoreException ignored) {
1360:                }
1361:                Comparable array2[] = (Comparable[]) s
1362:                        .toArray(new Comparable[0]);
1363:                Integer array3[] = (Integer[]) s.toArray(new Integer[s.size()]);
1364:
1365:                if (array3.length > 1) {
1366:                    Integer first = array3[0];
1367:
1368:                    for (int k = 1; k < array3.length; k++) {
1369:                        Integer second = array3[k];
1370:
1371:                        assertTrue(first.compareTo(second) < 0);
1372:                        first = second;
1373:                    }
1374:                }
1375:                try {
1376:                    s.add("foo");
1377:                    fail("should have thrown an exception");
1378:                } catch (UnsupportedOperationException ignored) {
1379:                }
1380:                assertTrue(!s.equals(null));
1381:                assertEquals(s, s);
1382:                Set hs = new HashSet(s);
1383:
1384:                assertEquals(s, hs);
1385:                assertEquals(hs, s);
1386:                assertTrue(s.hashCode() == hs.hashCode());
1387:            }
1388:
1389:            private void testKeySetByValue(final BinaryTree m) {
1390:                Set s = m.keySetByValue();
1391:
1392:                assertEquals(m.size(), s.size());
1393:                assertEquals(m.isEmpty(), s.isEmpty());
1394:                LocalTestNode node = new LocalTestNode(-1);
1395:
1396:                m.put(node.getKey(), node);
1397:                assertTrue(s.contains(node.getKey()));
1398:                assertEquals(m.size(), s.size());
1399:                assertEquals(m.isEmpty(), s.isEmpty());
1400:                m.remove(node.getKey());
1401:                assertTrue(!s.contains(node.getKey()));
1402:                assertEquals(m.size(), s.size());
1403:                assertEquals(m.isEmpty(), s.isEmpty());
1404:                try {
1405:                    s.contains(null);
1406:                    fail("should have caught NullPointerException");
1407:                } catch (NullPointerException ignored) {
1408:                }
1409:                try {
1410:                    s.contains(new Object());
1411:                    fail("should have caught ClassCastException");
1412:                } catch (ClassCastException ignored) {
1413:                }
1414:                for (int k = 0; k < m.size(); k++) {
1415:                    assertTrue(s.contains(new Integer(k)));
1416:                }
1417:                int count = 0;
1418:
1419:                for (Iterator iter = s.iterator(); iter.hasNext();) {
1420:                    iter.next();
1421:                    ++count;
1422:                }
1423:                assertEquals(count, s.size());
1424:
1425:                // force the map to have some content
1426:                m.put(node.getKey(), node);
1427:                Iterator iter = m.keySetByValue().iterator();
1428:                LocalTestNode node2 = new LocalTestNode(-2);
1429:
1430:                m.put(node2.getKey(), node2);
1431:                try {
1432:                    iter.next();
1433:                    fail("next() should have thrown an exception after a put");
1434:                } catch (ConcurrentModificationException ignored) {
1435:                }
1436:                m.remove(node2.getKey());
1437:                iter = s.iterator();
1438:                m.remove(node.getKey());
1439:                try {
1440:                    iter.next();
1441:                    fail("next() should have thrown an exception after a Map remove");
1442:                } catch (ConcurrentModificationException ignored) {
1443:                }
1444:                m.put(node.getKey(), node);
1445:                iter = s.iterator();
1446:                s.remove(node.getKey());
1447:                try {
1448:                    iter.next();
1449:                    fail("next() should have thrown an exception after a Set remove");
1450:                } catch (ConcurrentModificationException ignored) {
1451:                }
1452:                iter = s.iterator();
1453:                count = 0;
1454:                boolean terminated = false;
1455:
1456:                try {
1457:                    while (true) {
1458:                        iter.next();
1459:                        ++count;
1460:                    }
1461:                } catch (NoSuchElementException ignored) {
1462:                    terminated = true;
1463:                }
1464:                assertTrue(terminated);
1465:                assertEquals(m.size(), count);
1466:                iter = s.iterator();
1467:                try {
1468:                    iter.remove();
1469:                    fail("Should have thrown exception");
1470:                } catch (IllegalStateException ignored) {
1471:                }
1472:                m.put(node.getKey(), node);
1473:                iter = s.iterator();
1474:                iter.next();
1475:                m.put(node2.getKey(), node2);
1476:                try {
1477:                    iter.remove();
1478:                    fail("should have thrown exception");
1479:                } catch (ConcurrentModificationException ignored) {
1480:                }
1481:                Iterator iter2 = s.iterator();
1482:
1483:                iter2.next();
1484:                LocalTestNode node3 = new LocalTestNode(-3);
1485:
1486:                m.put(node3.getKey(), node3);
1487:                try {
1488:                    iter2.remove();
1489:                    fail("should have thrown exception");
1490:                } catch (ConcurrentModificationException ignored) {
1491:                }
1492:                int r_count = 0;
1493:
1494:                for (iter = s.iterator(); iter.hasNext();) {
1495:                    if (iter.next().equals(node.getKey())) {
1496:                        try {
1497:                            iter.remove();
1498:                            ++r_count;
1499:                            iter.remove();
1500:                            fail("2nd remove should have failed");
1501:                        } catch (IllegalStateException ignored) {
1502:                            assertEquals(1, r_count);
1503:                        }
1504:                    }
1505:                }
1506:                assertEquals(1, r_count);
1507:                assertTrue(!s.contains(node.getKey()));
1508:                r_count = 0;
1509:                m.put(node.getKey(), node);
1510:                Object[] a1 = s.toArray();
1511:
1512:                assertEquals(s.size(), a1.length);
1513:
1514:                //          if (a1.length > 1)
1515:                //          {
1516:                //              Comparable first = ( Comparable ) a1[ 0 ];
1517:                //              for (int k = 1; k < a1.length; k++)
1518:                //              {
1519:                //                  Comparable second = ( Comparable ) a1[ k ];
1520:                //                  assertTrue(first.compareTo(second) < 0);
1521:                //                  first = second;
1522:                //              }
1523:                //              iter  = s.iterator();
1524:                //              first = ( Comparable ) iter.next();
1525:                //              for (; iter.hasNext(); )
1526:                //              {
1527:                //                  Comparable second = ( Comparable ) iter.next();
1528:                //                  assertTrue(first.compareTo(second) < 0);
1529:                //                  first = second;
1530:                //              }
1531:                //          }
1532:                try {
1533:                    String array2[] = (String[]) s.toArray(new String[0]);
1534:
1535:                    if (s.size() != 0) {
1536:                        fail("should have caught exception creating an invalid array");
1537:                    }
1538:                } catch (ArrayStoreException ignored) {
1539:                }
1540:                Comparable array2[] = (Comparable[]) s
1541:                        .toArray(new Comparable[0]);
1542:                Integer array3[] = (Integer[]) s.toArray(new Integer[s.size()]);
1543:
1544:                //          if (array3.length > 1)
1545:                //          {
1546:                //              Integer first = array3[ 0 ];
1547:                //              for (int k = 1; k < array3.length; k++)
1548:                //              {
1549:                //                  Integer second = array3[ k ];
1550:                //                  assertTrue(first.compareTo(second) < 0);
1551:                //                  first = second;
1552:                //              }
1553:                //          }
1554:                try {
1555:                    s.add("foo");
1556:                    fail("should have thrown an exception");
1557:                } catch (UnsupportedOperationException ignored) {
1558:                }
1559:                assertTrue(!s.equals(null));
1560:                assertEquals(s, s);
1561:                Set hs = new HashSet(s);
1562:
1563:                assertEquals(s, hs);
1564:                assertEquals(hs, s);
1565:                assertTrue(s.hashCode() == hs.hashCode());
1566:            }
1567:
1568:            private void testValues(Map m) {
1569:                Collection s = m.values();
1570:
1571:                assertEquals(m.size(), s.size());
1572:                assertEquals(m.isEmpty(), s.isEmpty());
1573:                LocalTestNode node = new LocalTestNode(-1);
1574:
1575:                m.put(node.getKey(), node);
1576:                assertEquals(m.size(), s.size());
1577:                assertEquals(m.isEmpty(), s.isEmpty());
1578:                m.remove(node.getKey());
1579:                assertEquals(m.size(), s.size());
1580:                assertEquals(m.isEmpty(), s.isEmpty());
1581:                assertTrue(!s.contains(node));
1582:                for (int k = 0; k < m.size(); k++) {
1583:                    assertTrue(s.contains(new LocalTestNode(k)));
1584:                }
1585:                m.put(node.getKey(), node);
1586:                assertTrue(s.contains(node));
1587:                m.remove(node.getKey());
1588:                assertTrue(!s.contains(node));
1589:                int count = 0;
1590:
1591:                for (Iterator iter = s.iterator(); iter.hasNext();) {
1592:                    iter.next();
1593:                    ++count;
1594:                }
1595:                assertEquals(s.size(), count);
1596:                LocalTestNode node4 = new LocalTestNode(-4);
1597:
1598:                m.put(node4.getKey(), node4);
1599:                Iterator iter = s.iterator();
1600:
1601:                m.put(node.getKey(), node);
1602:                try {
1603:                    iter.next();
1604:                    fail("next() should have thrown an exception after a put");
1605:                } catch (ConcurrentModificationException ignored) {
1606:                }
1607:                iter = s.iterator();
1608:                m.remove(node.getKey());
1609:                try {
1610:                    iter.next();
1611:                    fail("next() should have thrown an exception after a Map remove");
1612:                } catch (ConcurrentModificationException ignored) {
1613:                }
1614:                m.put(node.getKey(), node);
1615:                iter = s.iterator();
1616:                s.remove(node);
1617:                try {
1618:                    iter.next();
1619:                    fail("next() should have thrown an exception after a Set remove");
1620:                } catch (ConcurrentModificationException ignored) {
1621:                }
1622:                iter = s.iterator();
1623:                count = 0;
1624:                boolean terminated = false;
1625:
1626:                try {
1627:                    while (true) {
1628:                        iter.next();
1629:                        ++count;
1630:                    }
1631:                } catch (NoSuchElementException ignored) {
1632:                    terminated = true;
1633:                }
1634:                assertTrue(terminated);
1635:                assertEquals(m.size(), count);
1636:                iter = s.iterator();
1637:                try {
1638:                    iter.remove();
1639:                    fail("Should have thrown exception");
1640:                } catch (IllegalStateException ignored) {
1641:                }
1642:                Iterator iter2 = s.iterator();
1643:
1644:                try {
1645:                    iter2.remove();
1646:                    fail("Should have thrown exception");
1647:                } catch (IllegalStateException ignored) {
1648:                }
1649:                m.put(node.getKey(), node);
1650:                iter = s.iterator();
1651:                iter.next();
1652:                LocalTestNode node2 = new LocalTestNode(-2);
1653:
1654:                m.put(node2.getKey(), node2);
1655:                try {
1656:                    iter.remove();
1657:                    fail("should have thrown exception");
1658:                } catch (ConcurrentModificationException ignored) {
1659:                }
1660:                LocalTestNode node3 = new LocalTestNode(-3);
1661:
1662:                m.put(node3.getKey(), node3);
1663:                iter2 = s.iterator();
1664:                while (iter2.hasNext()) {
1665:                    iter2.next();
1666:                }
1667:                int r_count = 0;
1668:
1669:                for (iter = s.iterator(); iter.hasNext();) {
1670:                    if (iter.next().equals(node3)) {
1671:                        try {
1672:                            iter.remove();
1673:                            ++r_count;
1674:                            iter.remove();
1675:                            fail("2nd remove should have failed");
1676:                        } catch (IllegalStateException ignored) {
1677:                            assertEquals(1, r_count);
1678:                        }
1679:                    }
1680:                }
1681:                assertEquals(1, r_count);
1682:                assertTrue(!s.contains(node3));
1683:                Object[] a1 = s.toArray();
1684:
1685:                assertTrue(a1.length == s.size());
1686:                if (a1.length > 1) {
1687:                    Comparable first = (Comparable) a1[0];
1688:
1689:                    for (int k = 1; k < a1.length; k++) {
1690:                        Comparable second = (Comparable) a1[k];
1691:
1692:                        assertTrue(first.compareTo(second) < 0);
1693:                        first = second;
1694:                    }
1695:                    iter = s.iterator();
1696:                    first = (Comparable) iter.next();
1697:                    for (; iter.hasNext();) {
1698:                        Comparable second = (Comparable) iter.next();
1699:
1700:                        assertTrue(first.compareTo(second) < 0);
1701:                        first = second;
1702:                    }
1703:                }
1704:                try {
1705:                    String array2[] = (String[]) s.toArray(new String[0]);
1706:
1707:                    if (s.size() != 0) {
1708:                        fail("should have caught exception creating an invalid array");
1709:                    }
1710:                } catch (ArrayStoreException ignored) {
1711:                }
1712:                m.remove(node.getKey());
1713:                m.remove(node2.getKey());
1714:                m.remove(node3.getKey());
1715:                LocalTestNode array2[] = (LocalTestNode[]) s
1716:                        .toArray(new LocalTestNode[0]);
1717:                LocalTestNode array3[] = (LocalTestNode[]) s
1718:                        .toArray(new LocalTestNode[s.size()]);
1719:
1720:                if (array3.length > 1) {
1721:                    LocalTestNode first = array3[0];
1722:
1723:                    for (int k = 1; k < array3.length; k++) {
1724:                        LocalTestNode second = array3[k];
1725:
1726:                        assertTrue(first.compareTo(second) < 0);
1727:                        first = second;
1728:                    }
1729:                }
1730:                try {
1731:                    s.add(node.getKey());
1732:                    fail("should have thrown an exception");
1733:                } catch (UnsupportedOperationException ignored) {
1734:                }
1735:                assertTrue(!s.equals(null));
1736:                assertEquals(s, s);
1737:                Set hs = new HashSet(s);
1738:
1739:                assertTrue(!s.equals(hs));
1740:                assertTrue(!hs.equals(s));
1741:            }
1742:
1743:            private void testValuesByValue(BinaryTree m) {
1744:                Collection s = m.valuesByValue();
1745:
1746:                assertEquals(m.size(), s.size());
1747:                assertEquals(m.isEmpty(), s.isEmpty());
1748:                LocalTestNode node = new LocalTestNode(-1);
1749:
1750:                m.put(node.getKey(), node);
1751:                assertEquals(m.size(), s.size());
1752:                assertEquals(m.isEmpty(), s.isEmpty());
1753:                m.remove(node.getKey());
1754:                assertEquals(m.size(), s.size());
1755:                assertEquals(m.isEmpty(), s.isEmpty());
1756:                assertTrue(!s.contains(node));
1757:                for (int k = 0; k < m.size(); k++) {
1758:                    assertTrue(s.contains(new LocalTestNode(k)));
1759:                }
1760:                m.put(node.getKey(), node);
1761:                assertTrue(s.contains(node));
1762:                m.remove(node.getKey());
1763:                assertTrue(!s.contains(node));
1764:                int count = 0;
1765:
1766:                for (Iterator iter = s.iterator(); iter.hasNext();) {
1767:                    iter.next();
1768:                    ++count;
1769:                }
1770:                assertEquals(s.size(), count);
1771:                LocalTestNode node4 = new LocalTestNode(-4);
1772:
1773:                m.put(node4.getKey(), node4);
1774:                Iterator iter = s.iterator();
1775:
1776:                m.put(node.getKey(), node);
1777:                try {
1778:                    iter.next();
1779:                    fail("next() should have thrown an exception after a put");
1780:                } catch (ConcurrentModificationException ignored) {
1781:                }
1782:                iter = s.iterator();
1783:                m.remove(node.getKey());
1784:                try {
1785:                    iter.next();
1786:                    fail("next() should have thrown an exception after a Map remove");
1787:                } catch (ConcurrentModificationException ignored) {
1788:                }
1789:                m.put(node.getKey(), node);
1790:                iter = s.iterator();
1791:                s.remove(node);
1792:                try {
1793:                    iter.next();
1794:                    fail("next() should have thrown an exception after a Set remove");
1795:                } catch (ConcurrentModificationException ignored) {
1796:                }
1797:                iter = s.iterator();
1798:                count = 0;
1799:                boolean terminated = false;
1800:
1801:                try {
1802:                    while (true) {
1803:                        iter.next();
1804:                        ++count;
1805:                    }
1806:                } catch (NoSuchElementException ignored) {
1807:                    terminated = true;
1808:                }
1809:                assertTrue(terminated);
1810:                assertEquals(m.size(), count);
1811:                iter = s.iterator();
1812:                try {
1813:                    iter.remove();
1814:                    fail("Should have thrown exception");
1815:                } catch (IllegalStateException ignored) {
1816:                }
1817:                Iterator iter2 = s.iterator();
1818:
1819:                try {
1820:                    iter2.remove();
1821:                    fail("Should have thrown exception");
1822:                } catch (IllegalStateException ignored) {
1823:                }
1824:                m.put(node.getKey(), node);
1825:                iter = s.iterator();
1826:                iter.next();
1827:                LocalTestNode node2 = new LocalTestNode(-2);
1828:
1829:                m.put(node2.getKey(), node2);
1830:                try {
1831:                    iter.remove();
1832:                    fail("should have thrown exception");
1833:                } catch (ConcurrentModificationException ignored) {
1834:                }
1835:                LocalTestNode node3 = new LocalTestNode(-3);
1836:
1837:                m.put(node3.getKey(), node3);
1838:                iter2 = s.iterator();
1839:                while (iter2.hasNext()) {
1840:                    iter2.next();
1841:                }
1842:                int r_count = 0;
1843:
1844:                for (iter = s.iterator(); iter.hasNext();) {
1845:                    if (iter.next().equals(node3)) {
1846:                        try {
1847:                            iter.remove();
1848:                            ++r_count;
1849:                            iter.remove();
1850:                            fail("2nd remove should have failed");
1851:                        } catch (IllegalStateException ignored) {
1852:                            assertEquals(1, r_count);
1853:                        }
1854:                    }
1855:                }
1856:                assertEquals(1, r_count);
1857:                assertTrue(!s.contains(node3));
1858:                Object[] a1 = s.toArray();
1859:
1860:                assertTrue(a1.length == s.size());
1861:                try {
1862:                    String array2[] = (String[]) s.toArray(new String[0]);
1863:
1864:                    if (s.size() != 0) {
1865:                        fail("should have caught exception creating an invalid array");
1866:                    }
1867:                } catch (ArrayStoreException ignored) {
1868:                }
1869:                m.remove(node.getKey());
1870:                m.remove(node2.getKey());
1871:                m.remove(node3.getKey());
1872:                LocalTestNode array2[] = (LocalTestNode[]) s
1873:                        .toArray(new LocalTestNode[0]);
1874:                LocalTestNode array3[] = (LocalTestNode[]) s
1875:                        .toArray(new LocalTestNode[s.size()]);
1876:
1877:                try {
1878:                    s.add(node.getKey());
1879:                    fail("should have thrown an exception");
1880:                } catch (UnsupportedOperationException ignored) {
1881:                }
1882:                assertTrue(!s.equals(null));
1883:                assertEquals(s, s);
1884:                Set hs = new HashSet(s);
1885:
1886:                assertTrue(!s.equals(hs));
1887:                assertTrue(!hs.equals(s));
1888:            }
1889:
1890:            private void testEntrySet(Map m) {
1891:                Set s = m.entrySet();
1892:
1893:                assertEquals(m.size(), s.size());
1894:                assertEquals(m.isEmpty(), s.isEmpty());
1895:                LocalTestNode node = new LocalTestNode(-1);
1896:
1897:                m.put(node.getKey(), node);
1898:                assertEquals(m.size(), s.size());
1899:                assertEquals(m.isEmpty(), s.isEmpty());
1900:                m.remove(node.getKey());
1901:                assertEquals(m.size(), s.size());
1902:                assertEquals(m.isEmpty(), s.isEmpty());
1903:                int count = 0;
1904:
1905:                for (Iterator iter = s.iterator(); iter.hasNext();) {
1906:                    iter.next();
1907:                    ++count;
1908:                }
1909:                assertEquals(s.size(), count);
1910:                LocalTestNode node2 = new LocalTestNode(-2);
1911:
1912:                if (m.size() == 0) {
1913:                    m.put(node2.getKey(), node2);
1914:                }
1915:                Iterator iter = s.iterator();
1916:
1917:                m.put(node.getKey(), node);
1918:                try {
1919:                    iter.next();
1920:                    fail("next() should have thrown an exception after a put");
1921:                } catch (ConcurrentModificationException ignored) {
1922:                }
1923:                m.remove(node2.getKey());
1924:                iter = s.iterator();
1925:                m.remove(node.getKey());
1926:                try {
1927:                    iter.next();
1928:                    fail("next() should have thrown an exception after a Map remove");
1929:                } catch (ConcurrentModificationException ignored) {
1930:                }
1931:                m.put(node.getKey(), node);
1932:                iter = s.iterator();
1933:                count = 0;
1934:                boolean terminated = false;
1935:
1936:                try {
1937:                    while (true) {
1938:                        iter.next();
1939:                        ++count;
1940:                    }
1941:                } catch (NoSuchElementException ignored) {
1942:                    terminated = true;
1943:                }
1944:                assertTrue(terminated);
1945:                assertEquals(m.size(), count);
1946:                iter = s.iterator();
1947:                try {
1948:                    iter.remove();
1949:                    fail("Should have thrown exception");
1950:                } catch (IllegalStateException ignored) {
1951:                }
1952:                iter = s.iterator();
1953:                iter.next();
1954:                LocalTestNode node3 = new LocalTestNode(-3);
1955:
1956:                m.put(node3.getKey(), node3);
1957:                try {
1958:                    iter.remove();
1959:                    fail("should have thrown exception");
1960:                } catch (ConcurrentModificationException ignored) {
1961:                }
1962:                int r_count = 0;
1963:                int when = m.size() / 2;
1964:                int timer = 0;
1965:
1966:                for (iter = s.iterator(); iter.hasNext();) {
1967:                    iter.next();
1968:                    if (timer == when) {
1969:                        try {
1970:                            iter.remove();
1971:                            ++r_count;
1972:                            iter.remove();
1973:                            fail("2nd remove should have failed");
1974:                        } catch (IllegalStateException ignored) {
1975:                            assertEquals(1, r_count);
1976:                        }
1977:                    }
1978:                    timer++;
1979:                }
1980:                assertEquals(1, r_count);
1981:                Iterator iter2 = s.iterator();
1982:
1983:                try {
1984:                    iter2.remove();
1985:                    fail("Should have thrown exception");
1986:                } catch (IllegalStateException ignored) {
1987:                }
1988:                iter2 = s.iterator();
1989:                while (iter2.hasNext()) {
1990:                    iter2.next();
1991:                }
1992:                LocalTestNode node4 = new LocalTestNode(-4);
1993:
1994:                m.put(node4.getKey(), node4);
1995:                try {
1996:                    iter2.remove();
1997:                    fail("should have thrown exception");
1998:                } catch (ConcurrentModificationException ignored) {
1999:                }
2000:                Object[] a1 = s.toArray();
2001:
2002:                assertTrue(a1.length == s.size());
2003:                if (a1.length > 1) {
2004:                    Map.Entry first = (Map.Entry) a1[0];
2005:
2006:                    for (int k = 1; k < a1.length; k++) {
2007:                        Map.Entry second = (Map.Entry) a1[k];
2008:
2009:                        assertTrue(((Comparable) first.getKey())
2010:                                .compareTo((Comparable) second.getKey()) < 0);
2011:                        first = second;
2012:                    }
2013:                    iter = s.iterator();
2014:                    first = (Map.Entry) iter.next();
2015:                    for (; iter.hasNext();) {
2016:                        Map.Entry second = (Map.Entry) iter.next();
2017:
2018:                        assertTrue(((Comparable) first.getKey())
2019:                                .compareTo((Comparable) second.getKey()) < 0);
2020:                        first = second;
2021:                    }
2022:                }
2023:                try {
2024:                    Integer array2[] = (Integer[]) s.toArray(new Integer[0]);
2025:
2026:                    if (s.size() != 0) {
2027:                        fail("should have caught exception creating an invalid array");
2028:                    }
2029:                } catch (ArrayStoreException ignored) {
2030:                }
2031:                Map.Entry array2[] = (Map.Entry[]) s.toArray(new Map.Entry[0]);
2032:                Map.Entry array3[] = (Map.Entry[]) s.toArray(new Map.Entry[s
2033:                        .size()]);
2034:
2035:                if (array3.length > 1) {
2036:                    Comparable first = (Comparable) ((Map.Entry) array3[0])
2037:                            .getKey();
2038:
2039:                    for (int k = 1; k < array3.length; k++) {
2040:                        Comparable second = (Comparable) ((Map.Entry) array3[k])
2041:                                .getKey();
2042:
2043:                        assertTrue(first.compareTo(second) < 0);
2044:                        first = second;
2045:                    }
2046:                }
2047:                try {
2048:                    s.add(node.getKey());
2049:                    fail("should have thrown an exception");
2050:                } catch (UnsupportedOperationException ignored) {
2051:                }
2052:                assertTrue(!s.equals(null));
2053:                assertEquals("SetEquality 1", s, s);
2054:                Set hs = new HashSet(s);
2055:
2056:                assertEquals("SetEquality 2", s, hs);
2057:                assertEquals("SetEquality 3", hs, s);
2058:                assertTrue(s.hashCode() == hs.hashCode());
2059:            }
2060:
2061:            private void testEntrySetByValue(BinaryTree m) {
2062:                Set s = m.entrySetByValue();
2063:
2064:                assertEquals(m.size(), s.size());
2065:                assertEquals(m.isEmpty(), s.isEmpty());
2066:                LocalTestNode node = new LocalTestNode(-1);
2067:
2068:                m.put(node.getKey(), node);
2069:                assertEquals(m.size(), s.size());
2070:                assertEquals(m.isEmpty(), s.isEmpty());
2071:                m.remove(node.getKey());
2072:                assertEquals(m.size(), s.size());
2073:                assertEquals(m.isEmpty(), s.isEmpty());
2074:                int count = 0;
2075:
2076:                for (Iterator iter = s.iterator(); iter.hasNext();) {
2077:                    iter.next();
2078:                    ++count;
2079:                }
2080:                assertEquals(s.size(), count);
2081:                LocalTestNode node2 = new LocalTestNode(-2);
2082:
2083:                if (m.size() == 0) {
2084:                    m.put(node2.getKey(), node2);
2085:                }
2086:                Iterator iter = s.iterator();
2087:
2088:                m.put(node.getKey(), node);
2089:                try {
2090:                    iter.next();
2091:                    fail("next() should have thrown an exception after a put");
2092:                } catch (ConcurrentModificationException ignored) {
2093:                }
2094:                m.remove(node2.getKey());
2095:                iter = s.iterator();
2096:                m.remove(node.getKey());
2097:                try {
2098:                    iter.next();
2099:                    fail("next() should have thrown an exception after a Map remove");
2100:                } catch (ConcurrentModificationException ignored) {
2101:                }
2102:                m.put(node.getKey(), node);
2103:                iter = s.iterator();
2104:                count = 0;
2105:                boolean terminated = false;
2106:
2107:                try {
2108:                    while (true) {
2109:                        iter.next();
2110:                        ++count;
2111:                    }
2112:                } catch (NoSuchElementException ignored) {
2113:                    terminated = true;
2114:                }
2115:                assertTrue(terminated);
2116:                assertEquals(m.size(), count);
2117:                iter = s.iterator();
2118:                try {
2119:                    iter.remove();
2120:                    fail("Should have thrown exception");
2121:                } catch (IllegalStateException ignored) {
2122:                }
2123:                iter = s.iterator();
2124:                iter.next();
2125:                LocalTestNode node3 = new LocalTestNode(-3);
2126:
2127:                m.put(node3.getKey(), node3);
2128:                try {
2129:                    iter.remove();
2130:                    fail("should have thrown exception");
2131:                } catch (ConcurrentModificationException ignored) {
2132:                }
2133:                int r_count = 0;
2134:                int when = m.size() / 2;
2135:                int timer = 0;
2136:
2137:                for (iter = s.iterator(); iter.hasNext();) {
2138:                    iter.next();
2139:                    if (timer == when) {
2140:                        try {
2141:                            iter.remove();
2142:                            ++r_count;
2143:                            iter.remove();
2144:                            fail("2nd remove should have failed");
2145:                        } catch (IllegalStateException ignored) {
2146:                            assertEquals(1, r_count);
2147:                        }
2148:                    }
2149:                    timer++;
2150:                }
2151:                assertEquals(1, r_count);
2152:                Iterator iter2 = s.iterator();
2153:
2154:                try {
2155:                    iter2.remove();
2156:                    fail("Should have thrown exception");
2157:                } catch (IllegalStateException ignored) {
2158:                }
2159:                iter2 = s.iterator();
2160:                while (iter2.hasNext()) {
2161:                    iter2.next();
2162:                }
2163:                LocalTestNode node4 = new LocalTestNode(-4);
2164:
2165:                m.put(node4.getKey(), node4);
2166:                try {
2167:                    iter2.remove();
2168:                    fail("should have thrown exception");
2169:                } catch (ConcurrentModificationException ignored) {
2170:                }
2171:                Object[] a1 = s.toArray();
2172:
2173:                assertTrue(a1.length == s.size());
2174:                if (a1.length > 1) {
2175:                    Map.Entry first = (Map.Entry) a1[0];
2176:
2177:                    for (int k = 1; k < a1.length; k++) {
2178:                        Map.Entry second = (Map.Entry) a1[k];
2179:
2180:                        assertTrue(((Comparable) first.getKey())
2181:                                .compareTo((Comparable) second.getKey()) < 0);
2182:                        first = second;
2183:                    }
2184:                    iter = s.iterator();
2185:                    first = (Map.Entry) iter.next();
2186:                    for (; iter.hasNext();) {
2187:                        Map.Entry second = (Map.Entry) iter.next();
2188:
2189:                        assertTrue(((Comparable) first.getKey())
2190:                                .compareTo((Comparable) second.getKey()) < 0);
2191:                        first = second;
2192:                    }
2193:                }
2194:                try {
2195:                    Integer array2[] = (Integer[]) s.toArray(new Integer[0]);
2196:
2197:                    if (s.size() != 0) {
2198:                        fail("should have caught exception creating an invalid array");
2199:                    }
2200:                } catch (ArrayStoreException ignored) {
2201:                }
2202:                Map.Entry array2[] = (Map.Entry[]) s.toArray(new Map.Entry[0]);
2203:                Map.Entry array3[] = (Map.Entry[]) s.toArray(new Map.Entry[s
2204:                        .size()]);
2205:
2206:                if (array3.length > 1) {
2207:                    Comparable first = (Comparable) ((Map.Entry) array3[0])
2208:                            .getValue();
2209:
2210:                    for (int k = 1; k < array3.length; k++) {
2211:                        Comparable second = (Comparable) ((Map.Entry) array3[k])
2212:                                .getValue();
2213:
2214:                        assertTrue(first.compareTo(second) < 0);
2215:                        first = second;
2216:                    }
2217:                }
2218:                try {
2219:                    s.add(node.getKey());
2220:                    fail("should have thrown an exception");
2221:                } catch (UnsupportedOperationException ignored) {
2222:                }
2223:                assertTrue(!s.equals(null));
2224:                assertEquals("SetEquality 1", s, s);
2225:                Set hs = new HashSet(s);
2226:
2227:                assertEquals("SetEquality 2", s, hs);
2228:                assertEquals("SetEquality 3", hs, s);
2229:                assertTrue(s.hashCode() == hs.hashCode());
2230:            }
2231:
2232:            private LocalTestNode[] makeLocalNodes() {
2233:                LocalTestNode nodes[] = new LocalTestNode[1023];
2234:
2235:                for (int k = 0; k < nodes.length; k++) {
2236:                    nodes[k] = new LocalTestNode(k);
2237:                }
2238:                return nodes;
2239:            }
2240:
2241:            /* **********  END  helper methods ********** */
2242:
2243:            /**
2244:             * Method main
2245:             *
2246:             * @param unused_args
2247:             */
2248:
2249:            public static void main(final String unused_args[]) {
2250:                junit.textui.TestRunner.run(TestBinaryTree.class);
2251:            }
2252:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.