001 /*
002 * TestRBTreeMap.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Valentin Tablan, 09/02/2000
013 *
014 * $Id: TestRBTreeMap.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015 */
016
017 package gate.util;
018
019 import junit.framework.*;
020
021 /** Tests for the RBTreeMap class
022 */
023 public class TestRBTreeMap extends TestCase
024 {
025 /** Debug flag */
026 private static final boolean DEBUG = false;
027
028 /** Construction */
029 public TestRBTreeMap(String name) { super(name); }
030
031 /** Create a map with sparse values as keys */
032 public void setUp() {
033 myTree=new RBTreeMap();
034 myTree.put(new Long(10),"Ten");
035 myTree.put(new Long(20),"Twenty");
036 myTree.put(new Long(30),"Thirty");
037 myTree.put(new Long(40),"Forty");
038 myTree.put(new Long(50),"Fifty");
039 } // setUp
040
041 /** A test test */
042 public void testExact() {
043 Object result;
044 Long key;
045 String expected;
046
047 //try the first entry
048 key=new Long(10);
049 expected="Ten";
050 result=myTree.get(key);
051 assertEquals(expected,result);
052
053 //try some entry
054 key=new Long(30);
055 expected="Thirty";
056 result=myTree.get(key);
057 assertEquals(expected,result);
058
059 //try the last entry
060 key=new Long(50);
061 expected="Fifty";
062 result=myTree.get(key);
063 assertEquals(expected,result);
064
065 //try the last entry
066 key=new Long(15);
067 result=myTree.get(key);
068 assertNull(result);
069
070 } // testExact
071
072 public void testClosestMatch(){
073 Object[] result;
074 Long key;
075 Object[] expected;
076
077 //try a match
078 key=new Long(10);
079 expected=new Object[]{"Ten","Ten"};
080 result=myTree.getClosestMatch(key);
081 assertEquals("TestCM 1",expected[0],result[0]);
082 assertEquals("TestCM 2",expected[1],result[1]);
083
084 //try glb=null
085 key=new Long(5);
086 expected=new Object[]{null,"Ten"};
087 result=myTree.getClosestMatch(key);
088 assertNull("TestCM 3",result[0]);
089 assertEquals("TestCM 4",expected[1],result[1]);
090
091 //the normal case
092 key=new Long(15);
093 expected=new Object[]{"Ten","Twenty"};
094 result=myTree.getClosestMatch(key);
095 assertEquals("TestCM 5",expected[0],result[0]);
096 assertEquals("TestCM 6",expected[1],result[1]);
097
098 //try lub=null
099 key=new Long(55);
100 expected=new Object[]{"Fifty",null};
101 result=myTree.getClosestMatch(key);
102 assertEquals("TestCM 7",expected[0],result[0]);
103 assertNull("TestCM 8",result[1]);
104
105 //empty the tree
106 myTree=new RBTreeMap();
107
108 //try glb=lub=null
109 key=new Long(15);
110 expected=new Object[]{null,null};
111 result=myTree.getClosestMatch(key);
112 assertNull("TestCM 9",result[0]);
113 assertNull("TestCM 10",result[1]);
114 }
115
116 public void testGetNextOf(){
117 Object result;
118 Long key;
119 String expected;
120
121 //try the first entry
122 key=new Long(5);
123 expected="Ten";
124 result=myTree.getNextOf(key);
125 assertEquals(expected,result);
126
127 //try some entry
128 key=new Long(20);
129 expected="Twenty";
130 result=myTree.getNextOf(key);
131 assertEquals(expected,result);
132
133 //try the "next" case
134 key=new Long(15);
135 expected="Twenty";
136 result=myTree.getNextOf(key);
137 assertEquals(expected,result);
138
139 //try the last case
140 key=new Long(55);
141 result=myTree.getNextOf(key);
142 assertNull(result);
143
144 //empty the tree
145 myTree=new RBTreeMap();
146 key=new Long(15);
147 result=myTree.getNextOf(key);
148 assertNull(result);
149 }
150
151
152 /** Test suite routine for the test runner */
153 public static Test suite() {
154 return new TestSuite(TestRBTreeMap.class);
155 } // suite
156
157
158 private RBTreeMap myTree;
159
160 } // class TestRBTreeMap
|